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