]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.sbin/mptutil/mpt_drive.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 (-1);
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, i, j;
150
151         ioc2 = mpt_read_ioc_page(fd, 2, NULL);
152         if (ioc2 == NULL) {
153                 warn("Failed to fetch volume list");
154                 return (NULL);
155         }
156
157         ioc3 = mpt_read_ioc_page(fd, 3, NULL);
158         if (ioc3 == NULL) {
159                 warn("Failed to fetch drive list");
160                 free(ioc2);
161                 return (NULL);
162         }
163
164         ioc5 = mpt_read_ioc_page(fd, 5, NULL);
165         if (ioc5 == NULL) {
166                 warn("Failed to fetch spare list");
167                 free(ioc3);
168                 free(ioc2);
169                 return (NULL);
170         }
171
172         /*
173          * Go ahead and read the info for all the volumes.  For this
174          * pass we figure out how many physical drives there are.
175          */
176         volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
177         count = 0;
178         vol = ioc2->RaidVolume;
179         for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
180                 volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
181                     NULL);
182                 if (volumes[i] == NULL) {
183                         warn("Failed to read volume info");
184                         return (NULL);
185                 }
186                 count += volumes[i]->NumPhysDisks;
187         }
188         count += ioc3->NumPhysDisks;
189         count += ioc5->NumHotSpares;
190
191         /* Walk the various lists enumerating drives. */
192         list = malloc(sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) *
193             count);
194         list->ndrives = 0;
195
196         for (i = 0; i < ioc2->NumActiveVolumes; i++) {
197                 rdisk = volumes[i]->PhysDisk;
198                 for (j = 0; j < volumes[i]->NumPhysDisks; rdisk++, j++)
199                         if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0)
200                                 return (NULL);
201                 free(volumes[i]);
202         }
203         free(ioc2);
204         free(volumes);
205
206         spare = ioc5->HotSpare;
207         for (i = 0; i < ioc5->NumHotSpares; spare++, i++)
208                 if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0)
209                         return (NULL);
210         free(ioc5);
211
212         disk = ioc3->PhysDisk;
213         for (i = 0; i < ioc3->NumPhysDisks; disk++, i++)
214                 if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0)
215                         return (NULL);
216         free(ioc3);
217
218         return (list);
219 }
220
221 void
222 mpt_free_pd_list(struct mpt_drive_list *list)
223 {
224         int i;
225
226         for (i = 0; i < list->ndrives; i++)
227                 free(list->drives[i]);
228         free(list);
229 }
230
231 int
232 mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
233     U8 *PhysDiskNum)
234 {
235         long val;
236         uint8_t bus, id;
237         char *cp;
238
239         /* Look for a raw device id first. */
240         val = strtol(drive, &cp, 0);
241         if (*cp == '\0') {
242                 if (val < 0 || val > 0xff)
243                         goto bad;
244                 *PhysDiskNum = val;
245                 return (0);
246         }
247
248         /* Look for a <bus>:<id> string. */
249         if (*cp == ':') {
250                 if (val < 0 || val > 0xff)
251                         goto bad;
252                 bus = val;
253                 val = strtol(cp + 1, &cp, 0);
254                 if (*cp != '\0')
255                         goto bad;
256                 if (val < 0 || val > 0xff)
257                         goto bad;
258                 id = val;
259
260                 for (val = 0; val < list->ndrives; val++) {
261                         if (list->drives[val]->PhysDiskBus == bus &&
262                             list->drives[val]->PhysDiskID == id) {
263                                 *PhysDiskNum = list->drives[val]->PhysDiskNum;
264                                 return (0);
265                         }
266                 }
267                 errno = ENOENT;
268                 return (-1);
269         }
270
271 bad:
272         errno = EINVAL;
273         return (-1);
274 }
275
276 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
277 const char *
278 mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info)
279 {
280         RAID_PHYS_DISK0_INQUIRY_DATA *inq_data;
281         u_char vendor[9], product[17], revision[5];
282         static char inq_string[64];
283
284         inq_data = &pd_info->InquiryData;
285         cam_strvis(vendor, inq_data->VendorID, sizeof(inq_data->VendorID),
286             sizeof(vendor));
287         cam_strvis(product, inq_data->ProductID, sizeof(inq_data->ProductID),
288             sizeof(product));
289         cam_strvis(revision, inq_data->ProductRevLevel,
290             sizeof(inq_data->ProductRevLevel), sizeof(revision));
291
292         /* Total hack. */
293         if (strcmp(vendor, "ATA") == 0)
294                 snprintf(inq_string, sizeof(inq_string), "<%s %s> SATA",
295                     product, revision);
296         else
297                 snprintf(inq_string, sizeof(inq_string), "<%s %s %s> SAS",
298                     vendor, product, revision);
299         return (inq_string);
300 }
301
302 /* Helper function to set a drive to a given state. */
303 static int
304 drive_set_state(char *drive, U8 Action, U8 State, const char *name)
305 {
306         CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
307         struct mpt_drive_list *list;
308         U8 PhysDiskNum;
309         int fd;
310
311         fd = mpt_open(mpt_unit);
312         if (fd < 0) {
313                 warn("mpt_open");
314                 return (errno);
315         }
316
317         list = mpt_pd_list(fd);
318         if (list == NULL)
319                 return (errno);
320
321         if (mpt_lookup_drive(list, drive, &PhysDiskNum) < 0) {
322                 warn("Failed to find drive %s", drive);
323                 return (errno);
324         }
325         mpt_free_pd_list(list);
326
327         /* Get the info for this drive. */
328         info = mpt_pd_info(fd, PhysDiskNum, NULL);
329         if (info == NULL) {
330                 warn("Failed to fetch info for drive %u", PhysDiskNum);
331                 return (errno);
332         }
333
334         /* Try to change the state. */
335         if (info->PhysDiskStatus.State == State) {
336                 warnx("Drive %u is already in the desired state", PhysDiskNum);
337                 return (EINVAL);
338         }
339
340         if (mpt_raid_action(fd, Action, 0, 0, PhysDiskNum, 0, NULL, 0, NULL,
341             NULL, 0, NULL, NULL, 0) < 0) {
342                 warn("Failed to set drive %u to %s", PhysDiskNum, name);
343                 return (errno);
344         }
345
346         free(info);
347         close(fd);
348
349         return (0);
350 }
351
352 static int
353 fail_drive(int ac, char **av)
354 {
355
356         if (ac != 2) {
357                 warnx("fail: %s", ac > 2 ? "extra arguments" :
358                     "drive required");
359                 return (EINVAL);
360         }
361
362         return (drive_set_state(av[1], MPI_RAID_ACTION_FAIL_PHYSDISK,
363             MPI_PHYSDISK0_STATUS_FAILED_REQUESTED, "FAILED"));
364 }
365 MPT_COMMAND(top, fail, fail_drive);
366
367 static int
368 online_drive(int ac, char **av)
369 {
370
371         if (ac != 2) {
372                 warnx("online: %s", ac > 2 ? "extra arguments" :
373                     "drive required");
374                 return (EINVAL);
375         }
376
377         return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_ONLINE,
378             MPI_PHYSDISK0_STATUS_ONLINE, "ONLINE"));
379 }
380 MPT_COMMAND(top, online, online_drive);
381
382 static int
383 offline_drive(int ac, char **av)
384 {
385
386         if (ac != 2) {
387                 warnx("offline: %s", ac > 2 ? "extra arguments" :
388                     "drive required");
389                 return (EINVAL);
390         }
391
392         return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_OFFLINE,
393             MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED, "OFFLINE"));
394 }
395 MPT_COMMAND(top, offline, offline_drive);