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