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