xref: /freebsd/usr.sbin/mptutil/mpt_drive.c (revision 4d65a7c6)
1fc58801cSScott Long /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4fc58801cSScott Long  * Copyright (c) 2008 Yahoo!, Inc.
5fc58801cSScott Long  * All rights reserved.
6fc58801cSScott Long  * Written by: John Baldwin <jhb@FreeBSD.org>
7fc58801cSScott Long  *
8fc58801cSScott Long  * Redistribution and use in source and binary forms, with or without
9fc58801cSScott Long  * modification, are permitted provided that the following conditions
10fc58801cSScott Long  * are met:
11fc58801cSScott Long  * 1. Redistributions of source code must retain the above copyright
12fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer.
13fc58801cSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
14fc58801cSScott Long  *    notice, this list of conditions and the following disclaimer in the
15fc58801cSScott Long  *    documentation and/or other materials provided with the distribution.
16fc58801cSScott Long  * 3. Neither the name of the author nor the names of any co-contributors
17fc58801cSScott Long  *    may be used to endorse or promote products derived from this software
18fc58801cSScott Long  *    without specific prior written permission.
19fc58801cSScott Long  *
20fc58801cSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21fc58801cSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22fc58801cSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23fc58801cSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24fc58801cSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25fc58801cSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26fc58801cSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27fc58801cSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28fc58801cSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29fc58801cSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30fc58801cSScott Long  * SUCH DAMAGE.
31fc58801cSScott Long  */
32fc58801cSScott Long 
33fc58801cSScott Long #include <sys/param.h>
34fc58801cSScott Long #include <sys/errno.h>
35fc58801cSScott Long #include <ctype.h>
36fc58801cSScott Long #include <err.h>
37fc58801cSScott Long #include <libutil.h>
38fc58801cSScott Long #include <limits.h>
39fc58801cSScott Long #include <stdio.h>
40fc58801cSScott Long #include <stdlib.h>
41fc58801cSScott Long #include <string.h>
42fc58801cSScott Long #include <strings.h>
43fc58801cSScott Long #include <unistd.h>
44fc58801cSScott Long 
45fc58801cSScott Long #include <camlib.h>
46fc58801cSScott Long #include <cam/scsi/scsi_all.h>
47fc58801cSScott Long 
48fc58801cSScott Long #include "mptutil.h"
49fc58801cSScott Long 
50fc58801cSScott Long const char *
mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 * info)51fc58801cSScott Long mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info)
52fc58801cSScott Long {
53fc58801cSScott Long 	static char buf[16];
54fc58801cSScott Long 
55fc58801cSScott Long 	switch (info->PhysDiskStatus.State) {
56fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_ONLINE:
57fc58801cSScott Long 		if ((info->PhysDiskStatus.Flags &
58fc58801cSScott Long 		    MPI_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC) &&
59fc58801cSScott Long 		    info->PhysDiskSettings.HotSparePool == 0)
60fc58801cSScott Long 			return ("REBUILD");
61fc58801cSScott Long 		else
62fc58801cSScott Long 			return ("ONLINE");
63fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_MISSING:
64fc58801cSScott Long 		return ("MISSING");
65fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_NOT_COMPATIBLE:
66fc58801cSScott Long 		return ("NOT COMPATIBLE");
67fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_FAILED:
68fc58801cSScott Long 		return ("FAILED");
69fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_INITIALIZING:
70fc58801cSScott Long 		return ("INITIALIZING");
71fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED:
72fc58801cSScott Long 		return ("OFFLINE REQUESTED");
73fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_FAILED_REQUESTED:
74fc58801cSScott Long 		return ("FAILED REQUESTED");
75fc58801cSScott Long 	case MPI_PHYSDISK0_STATUS_OTHER_OFFLINE:
76fc58801cSScott Long 		return ("OTHER OFFLINE");
77fc58801cSScott Long 	default:
78fc58801cSScott Long 		sprintf(buf, "PSTATE 0x%02x", info->PhysDiskStatus.State);
79fc58801cSScott Long 		return (buf);
80fc58801cSScott Long 	}
81fc58801cSScott Long }
82fc58801cSScott Long 
83fc58801cSScott Long /*
84fc58801cSScott Long  * There are several ways to enumerate physical disks.  Unfortunately,
85fc58801cSScott Long  * none of them are truly complete, so we have to build a union of all of
86fc58801cSScott Long  * them.  Specifically:
87fc58801cSScott Long  *
88fc58801cSScott Long  * - IOC2 : This gives us a list of volumes, and by walking the volumes we
89fc58801cSScott Long  *          can enumerate all of the drives attached to volumes including
90fc58801cSScott Long  *          online drives and failed drives.
91fc58801cSScott Long  * - IOC3 : This gives us a list of all online physical drives including
92fc58801cSScott Long  *          drives that are not part of a volume nor a spare drive.  It
93fc58801cSScott Long  *          does not include any failed drives.
94fc58801cSScott Long  * - IOC5 : This gives us a list of all spare drives including failed
95fc58801cSScott Long  *          spares.
96fc58801cSScott Long  *
97fc58801cSScott Long  * The specific edge cases are that 1) a failed volume member can only be
98fc58801cSScott Long  * found via IOC2, 2) a drive that is neither a volume member nor a spare
99fc58801cSScott Long  * can only be found via IOC3, and 3) a failed spare can only be found via
100fc58801cSScott Long  * IOC5.
101fc58801cSScott Long  *
102fc58801cSScott Long  * To handle this, walk all of the three lists and use the following
103fc58801cSScott Long  * routine to add each drive encountered.  It quietly succeeds if the
104fc58801cSScott Long  * drive is already present in the list.  It also sorts the list as it
105fc58801cSScott Long  * inserts new drives.
106fc58801cSScott Long  */
107fc58801cSScott Long static int
mpt_pd_insert(int fd,struct mpt_drive_list * list,U8 PhysDiskNum)108fc58801cSScott Long mpt_pd_insert(int fd, struct mpt_drive_list *list, U8 PhysDiskNum)
109fc58801cSScott Long {
110fc58801cSScott Long 	int i, j;
111fc58801cSScott Long 
112fc58801cSScott Long 	/*
113fc58801cSScott Long 	 * First, do a simple linear search to see if we have already
114fc58801cSScott Long 	 * seen this drive.
115fc58801cSScott Long 	 */
116fc58801cSScott Long 	for (i = 0; i < list->ndrives; i++) {
117fc58801cSScott Long 		if (list->drives[i]->PhysDiskNum == PhysDiskNum)
118fc58801cSScott Long 			return (0);
119fc58801cSScott Long 		if (list->drives[i]->PhysDiskNum > PhysDiskNum)
120fc58801cSScott Long 			break;
121fc58801cSScott Long 	}
122fc58801cSScott Long 
123fc58801cSScott Long 	/*
124fc58801cSScott Long 	 * 'i' is our slot for the 'new' drive.  Make room and then
125fc58801cSScott Long 	 * read the drive info.
126fc58801cSScott Long 	 */
127fc58801cSScott Long 	for (j = list->ndrives - 1; j >= i; j--)
128fc58801cSScott Long 		list->drives[j + 1] = list->drives[j];
129fc58801cSScott Long 	list->drives[i] = mpt_pd_info(fd, PhysDiskNum, NULL);
130fc58801cSScott Long 	if (list->drives[i] == NULL)
131c5f2b79dSJohn Baldwin 		return (errno);
132fc58801cSScott Long 	list->ndrives++;
133fc58801cSScott Long 	return (0);
134fc58801cSScott Long }
135fc58801cSScott Long 
136fc58801cSScott Long struct mpt_drive_list *
mpt_pd_list(int fd)137fc58801cSScott Long mpt_pd_list(int fd)
138fc58801cSScott Long {
139fc58801cSScott Long 	CONFIG_PAGE_IOC_2 *ioc2;
140fc58801cSScott Long 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
141fc58801cSScott Long 	CONFIG_PAGE_RAID_VOL_0 **volumes;
142fc58801cSScott Long 	RAID_VOL0_PHYS_DISK *rdisk;
143fc58801cSScott Long 	CONFIG_PAGE_IOC_3 *ioc3;
144fc58801cSScott Long 	IOC_3_PHYS_DISK *disk;
145fc58801cSScott Long 	CONFIG_PAGE_IOC_5 *ioc5;
146fc58801cSScott Long 	IOC_5_HOT_SPARE *spare;
147fc58801cSScott Long 	struct mpt_drive_list *list;
148c5f2b79dSJohn Baldwin 	int count, error, i, j;
149d68b76a9SAlan Somers 	size_t listsize;
150fc58801cSScott Long 
151fc58801cSScott Long 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
152fc58801cSScott Long 	if (ioc2 == NULL) {
153c5f2b79dSJohn Baldwin 		error = errno;
154fc58801cSScott Long 		warn("Failed to fetch volume list");
155c5f2b79dSJohn Baldwin 		errno = error;
156fc58801cSScott Long 		return (NULL);
157fc58801cSScott Long 	}
158fc58801cSScott Long 
159fc58801cSScott Long 	ioc3 = mpt_read_ioc_page(fd, 3, NULL);
160fc58801cSScott Long 	if (ioc3 == NULL) {
161c5f2b79dSJohn Baldwin 		error = errno;
162fc58801cSScott Long 		warn("Failed to fetch drive list");
163fc58801cSScott Long 		free(ioc2);
164c5f2b79dSJohn Baldwin 		errno = error;
165fc58801cSScott Long 		return (NULL);
166fc58801cSScott Long 	}
167fc58801cSScott Long 
168fc58801cSScott Long 	ioc5 = mpt_read_ioc_page(fd, 5, NULL);
169fc58801cSScott Long 	if (ioc5 == NULL) {
170c5f2b79dSJohn Baldwin 		error = errno;
171fc58801cSScott Long 		warn("Failed to fetch spare list");
172fc58801cSScott Long 		free(ioc3);
173fc58801cSScott Long 		free(ioc2);
174c5f2b79dSJohn Baldwin 		errno = error;
175fc58801cSScott Long 		return (NULL);
176fc58801cSScott Long 	}
177fc58801cSScott Long 
178fc58801cSScott Long 	/*
179fc58801cSScott Long 	 * Go ahead and read the info for all the volumes.  For this
180fc58801cSScott Long 	 * pass we figure out how many physical drives there are.
181fc58801cSScott Long 	 */
182fc58801cSScott Long 	volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
183fc58801cSScott Long 	count = 0;
184fc58801cSScott Long 	vol = ioc2->RaidVolume;
185fc58801cSScott Long 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
186fc58801cSScott Long 		volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
187fc58801cSScott Long 		    NULL);
188fc58801cSScott Long 		if (volumes[i] == NULL) {
189c5f2b79dSJohn Baldwin 			error = errno;
190fc58801cSScott Long 			warn("Failed to read volume info");
191c5f2b79dSJohn Baldwin 			errno = error;
192d68b76a9SAlan Somers 			free(volumes);
193d68b76a9SAlan Somers 			free(ioc5);
194d68b76a9SAlan Somers 			free(ioc3);
195d68b76a9SAlan Somers 			free(ioc2);
196fc58801cSScott Long 			return (NULL);
197fc58801cSScott Long 		}
198fc58801cSScott Long 		count += volumes[i]->NumPhysDisks;
199fc58801cSScott Long 	}
200fc58801cSScott Long 	count += ioc3->NumPhysDisks;
201fc58801cSScott Long 	count += ioc5->NumHotSpares;
202fc58801cSScott Long 
203fc58801cSScott Long 	/* Walk the various lists enumerating drives. */
204d68b76a9SAlan Somers 	listsize = sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) * count;
205d68b76a9SAlan Somers 	list = calloc(1, listsize);
206fc58801cSScott Long 
207fc58801cSScott Long 	for (i = 0; i < ioc2->NumActiveVolumes; i++) {
208fc58801cSScott Long 		rdisk = volumes[i]->PhysDisk;
209fc58801cSScott Long 		for (j = 0; j < volumes[i]->NumPhysDisks; rdisk++, j++)
210d68b76a9SAlan Somers 			if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0) {
211d68b76a9SAlan Somers 				mpt_free_pd_list(list);
212d68b76a9SAlan Somers 				free(volumes);
213d68b76a9SAlan Somers 				free(ioc5);
214d68b76a9SAlan Somers 				free(ioc3);
215d68b76a9SAlan Somers 				free(ioc2);
216fc58801cSScott Long 				return (NULL);
217d68b76a9SAlan Somers 			}
218fc58801cSScott Long 		free(volumes[i]);
219fc58801cSScott Long 	}
220fc58801cSScott Long 	free(ioc2);
221fc58801cSScott Long 	free(volumes);
222fc58801cSScott Long 
223fc58801cSScott Long 	spare = ioc5->HotSpare;
224fc58801cSScott Long 	for (i = 0; i < ioc5->NumHotSpares; spare++, i++)
225d68b76a9SAlan Somers 		if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0) {
226d68b76a9SAlan Somers 			mpt_free_pd_list(list);
227d68b76a9SAlan Somers 			free(ioc5);
228d68b76a9SAlan Somers 			free(ioc3);
229fc58801cSScott Long 			return (NULL);
230d68b76a9SAlan Somers 		}
231fc58801cSScott Long 	free(ioc5);
232fc58801cSScott Long 
233fc58801cSScott Long 	disk = ioc3->PhysDisk;
234fc58801cSScott Long 	for (i = 0; i < ioc3->NumPhysDisks; disk++, i++)
235d68b76a9SAlan Somers 		if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0) {
236d68b76a9SAlan Somers 			mpt_free_pd_list(list);
237d68b76a9SAlan Somers 			free(ioc3);
238fc58801cSScott Long 			return (NULL);
239d68b76a9SAlan Somers 		}
240fc58801cSScott Long 	free(ioc3);
241fc58801cSScott Long 
242fc58801cSScott Long 	return (list);
243fc58801cSScott Long }
244fc58801cSScott Long 
245fc58801cSScott Long void
mpt_free_pd_list(struct mpt_drive_list * list)246fc58801cSScott Long mpt_free_pd_list(struct mpt_drive_list *list)
247fc58801cSScott Long {
248fc58801cSScott Long 	int i;
249fc58801cSScott Long 
250fc58801cSScott Long 	for (i = 0; i < list->ndrives; i++)
251fc58801cSScott Long 		free(list->drives[i]);
252fc58801cSScott Long 	free(list);
253fc58801cSScott Long }
254fc58801cSScott Long 
255fc58801cSScott Long int
mpt_lookup_drive(struct mpt_drive_list * list,const char * drive,U8 * PhysDiskNum)256fc58801cSScott Long mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
257fc58801cSScott Long     U8 *PhysDiskNum)
258fc58801cSScott Long {
259fc58801cSScott Long 	long val;
260fc58801cSScott Long 	uint8_t bus, id;
261fc58801cSScott Long 	char *cp;
262fc58801cSScott Long 
263fc58801cSScott Long 	/* Look for a raw device id first. */
264fc58801cSScott Long 	val = strtol(drive, &cp, 0);
265fc58801cSScott Long 	if (*cp == '\0') {
266fc58801cSScott Long 		if (val < 0 || val > 0xff)
267fc58801cSScott Long 			goto bad;
268fc58801cSScott Long 		*PhysDiskNum = val;
269fc58801cSScott Long 		return (0);
270fc58801cSScott Long 	}
271fc58801cSScott Long 
272fc58801cSScott Long 	/* Look for a <bus>:<id> string. */
273fc58801cSScott Long 	if (*cp == ':') {
274fc58801cSScott Long 		if (val < 0 || val > 0xff)
275fc58801cSScott Long 			goto bad;
276fc58801cSScott Long 		bus = val;
277fc58801cSScott Long 		val = strtol(cp + 1, &cp, 0);
278fc58801cSScott Long 		if (*cp != '\0')
279fc58801cSScott Long 			goto bad;
280fc58801cSScott Long 		if (val < 0 || val > 0xff)
281fc58801cSScott Long 			goto bad;
282fc58801cSScott Long 		id = val;
283fc58801cSScott Long 
284fc58801cSScott Long 		for (val = 0; val < list->ndrives; val++) {
285fc58801cSScott Long 			if (list->drives[val]->PhysDiskBus == bus &&
286fc58801cSScott Long 			    list->drives[val]->PhysDiskID == id) {
287fc58801cSScott Long 				*PhysDiskNum = list->drives[val]->PhysDiskNum;
288fc58801cSScott Long 				return (0);
289fc58801cSScott Long 			}
290fc58801cSScott Long 		}
291c5f2b79dSJohn Baldwin 		return (ENOENT);
292fc58801cSScott Long 	}
293fc58801cSScott Long 
294fc58801cSScott Long bad:
295c5f2b79dSJohn Baldwin 	return (EINVAL);
296fc58801cSScott Long }
297fc58801cSScott Long 
298fc58801cSScott Long /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
299fc58801cSScott Long const char *
mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 * pd_info)300fc58801cSScott Long mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info)
301fc58801cSScott Long {
302fc58801cSScott Long 	RAID_PHYS_DISK0_INQUIRY_DATA *inq_data;
303fc58801cSScott Long 	u_char vendor[9], product[17], revision[5];
304fc58801cSScott Long 	static char inq_string[64];
305fc58801cSScott Long 
306fc58801cSScott Long 	inq_data = &pd_info->InquiryData;
307fc58801cSScott Long 	cam_strvis(vendor, inq_data->VendorID, sizeof(inq_data->VendorID),
308fc58801cSScott Long 	    sizeof(vendor));
309fc58801cSScott Long 	cam_strvis(product, inq_data->ProductID, sizeof(inq_data->ProductID),
310fc58801cSScott Long 	    sizeof(product));
311fc58801cSScott Long 	cam_strvis(revision, inq_data->ProductRevLevel,
312fc58801cSScott Long 	    sizeof(inq_data->ProductRevLevel), sizeof(revision));
313fc58801cSScott Long 
314fc58801cSScott Long 	/* Total hack. */
315fc58801cSScott Long 	if (strcmp(vendor, "ATA") == 0)
316fc58801cSScott Long 		snprintf(inq_string, sizeof(inq_string), "<%s %s> SATA",
317fc58801cSScott Long 		    product, revision);
318fc58801cSScott Long 	else
319fc58801cSScott Long 		snprintf(inq_string, sizeof(inq_string), "<%s %s %s> SAS",
320fc58801cSScott Long 		    vendor, product, revision);
321fc58801cSScott Long 	return (inq_string);
322fc58801cSScott Long }
323fc58801cSScott Long 
324fc58801cSScott Long /* Helper function to set a drive to a given state. */
325fc58801cSScott Long static int
drive_set_state(char * drive,U8 Action,U8 State,const char * name)326fc58801cSScott Long drive_set_state(char *drive, U8 Action, U8 State, const char *name)
327fc58801cSScott Long {
328fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
329fc58801cSScott Long 	struct mpt_drive_list *list;
330fc58801cSScott Long 	U8 PhysDiskNum;
331c5f2b79dSJohn Baldwin 	int error, fd;
332fc58801cSScott Long 
333fc58801cSScott Long 	fd = mpt_open(mpt_unit);
334fc58801cSScott Long 	if (fd < 0) {
335c5f2b79dSJohn Baldwin 		error = errno;
336fc58801cSScott Long 		warn("mpt_open");
337c5f2b79dSJohn Baldwin 		return (error);
338fc58801cSScott Long 	}
339fc58801cSScott Long 
340fc58801cSScott Long 	list = mpt_pd_list(fd);
341d68b76a9SAlan Somers 	if (list == NULL) {
342d68b76a9SAlan Somers 		close(fd);
343fc58801cSScott Long 		return (errno);
344d68b76a9SAlan Somers 	}
345fc58801cSScott Long 
346fc58801cSScott Long 	if (mpt_lookup_drive(list, drive, &PhysDiskNum) < 0) {
347c5f2b79dSJohn Baldwin 		error = errno;
348fc58801cSScott Long 		warn("Failed to find drive %s", drive);
349d68b76a9SAlan Somers 		close(fd);
350c5f2b79dSJohn Baldwin 		return (error);
351fc58801cSScott Long 	}
352fc58801cSScott Long 	mpt_free_pd_list(list);
353fc58801cSScott Long 
354fc58801cSScott Long 	/* Get the info for this drive. */
355fc58801cSScott Long 	info = mpt_pd_info(fd, PhysDiskNum, NULL);
356fc58801cSScott Long 	if (info == NULL) {
357c5f2b79dSJohn Baldwin 		error = errno;
358fc58801cSScott Long 		warn("Failed to fetch info for drive %u", PhysDiskNum);
359d68b76a9SAlan Somers 		close(fd);
360c5f2b79dSJohn Baldwin 		return (error);
361fc58801cSScott Long 	}
362fc58801cSScott Long 
363fc58801cSScott Long 	/* Try to change the state. */
364fc58801cSScott Long 	if (info->PhysDiskStatus.State == State) {
365fc58801cSScott Long 		warnx("Drive %u is already in the desired state", PhysDiskNum);
366d68b76a9SAlan Somers 		free(info);
367d68b76a9SAlan Somers 		close(fd);
368fc58801cSScott Long 		return (EINVAL);
369fc58801cSScott Long 	}
370fc58801cSScott Long 
371c5f2b79dSJohn Baldwin 	error = mpt_raid_action(fd, Action, 0, 0, PhysDiskNum, 0, NULL, 0, NULL,
372c5f2b79dSJohn Baldwin 	    NULL, 0, NULL, NULL, 0);
373c5f2b79dSJohn Baldwin 	if (error) {
374c5f2b79dSJohn Baldwin 		warnc(error, "Failed to set drive %u to %s", PhysDiskNum, name);
375d68b76a9SAlan Somers 		free(info);
376d68b76a9SAlan Somers 		close(fd);
377c5f2b79dSJohn Baldwin 		return (error);
378fc58801cSScott Long 	}
379fc58801cSScott Long 
380fc58801cSScott Long 	free(info);
381fc58801cSScott Long 	close(fd);
382fc58801cSScott Long 
383fc58801cSScott Long 	return (0);
384fc58801cSScott Long }
385fc58801cSScott Long 
386fc58801cSScott Long static int
fail_drive(int ac,char ** av)387fc58801cSScott Long fail_drive(int ac, char **av)
388fc58801cSScott Long {
389fc58801cSScott Long 
390fc58801cSScott Long 	if (ac != 2) {
391fc58801cSScott Long 		warnx("fail: %s", ac > 2 ? "extra arguments" :
392fc58801cSScott Long 		    "drive required");
393fc58801cSScott Long 		return (EINVAL);
394fc58801cSScott Long 	}
395fc58801cSScott Long 
396fc58801cSScott Long 	return (drive_set_state(av[1], MPI_RAID_ACTION_FAIL_PHYSDISK,
397fc58801cSScott Long 	    MPI_PHYSDISK0_STATUS_FAILED_REQUESTED, "FAILED"));
398fc58801cSScott Long }
399fc58801cSScott Long MPT_COMMAND(top, fail, fail_drive);
400fc58801cSScott Long 
401fc58801cSScott Long static int
online_drive(int ac,char ** av)402fc58801cSScott Long online_drive(int ac, char **av)
403fc58801cSScott Long {
404fc58801cSScott Long 
405fc58801cSScott Long 	if (ac != 2) {
406fc58801cSScott Long 		warnx("online: %s", ac > 2 ? "extra arguments" :
407fc58801cSScott Long 		    "drive required");
408fc58801cSScott Long 		return (EINVAL);
409fc58801cSScott Long 	}
410fc58801cSScott Long 
411fc58801cSScott Long 	return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_ONLINE,
412fc58801cSScott Long 	    MPI_PHYSDISK0_STATUS_ONLINE, "ONLINE"));
413fc58801cSScott Long }
414fc58801cSScott Long MPT_COMMAND(top, online, online_drive);
415fc58801cSScott Long 
416fc58801cSScott Long static int
offline_drive(int ac,char ** av)417fc58801cSScott Long offline_drive(int ac, char **av)
418fc58801cSScott Long {
419fc58801cSScott Long 
420fc58801cSScott Long 	if (ac != 2) {
421fc58801cSScott Long 		warnx("offline: %s", ac > 2 ? "extra arguments" :
422fc58801cSScott Long 		    "drive required");
423fc58801cSScott Long 		return (EINVAL);
424fc58801cSScott Long 	}
425fc58801cSScott Long 
426fc58801cSScott Long 	return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_OFFLINE,
427fc58801cSScott Long 	    MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED, "OFFLINE"));
428fc58801cSScott Long }
429fc58801cSScott Long MPT_COMMAND(top, offline, offline_drive);
430