2 * Copyright (c) 2008 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/errno.h>
47 #include <cam/scsi/scsi_all.h>
52 mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info)
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)
64 case MPI_PHYSDISK0_STATUS_MISSING:
66 case MPI_PHYSDISK0_STATUS_NOT_COMPATIBLE:
67 return ("NOT COMPATIBLE");
68 case MPI_PHYSDISK0_STATUS_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");
79 sprintf(buf, "PSTATE 0x%02x", info->PhysDiskStatus.State);
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
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
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
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.
109 mpt_pd_insert(int fd, struct mpt_drive_list *list, U8 PhysDiskNum)
114 * First, do a simple linear search to see if we have already
117 for (i = 0; i < list->ndrives; i++) {
118 if (list->drives[i]->PhysDiskNum == PhysDiskNum)
120 if (list->drives[i]->PhysDiskNum > PhysDiskNum)
125 * 'i' is our slot for the 'new' drive. Make room and then
126 * read the drive info.
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)
137 struct mpt_drive_list *
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;
151 ioc2 = mpt_read_ioc_page(fd, 2, NULL);
154 warn("Failed to fetch volume list");
159 ioc3 = mpt_read_ioc_page(fd, 3, NULL);
162 warn("Failed to fetch drive list");
168 ioc5 = mpt_read_ioc_page(fd, 5, NULL);
171 warn("Failed to fetch spare list");
179 * Go ahead and read the info for all the volumes. For this
180 * pass we figure out how many physical drives there are.
182 volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
184 vol = ioc2->RaidVolume;
185 for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
186 volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
188 if (volumes[i] == NULL) {
190 warn("Failed to read volume info");
194 count += volumes[i]->NumPhysDisks;
196 count += ioc3->NumPhysDisks;
197 count += ioc5->NumHotSpares;
199 /* Walk the various lists enumerating drives. */
200 list = malloc(sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) *
204 for (i = 0; i < ioc2->NumActiveVolumes; i++) {
205 rdisk = volumes[i]->PhysDisk;
206 for (j = 0; j < volumes[i]->NumPhysDisks; rdisk++, j++)
207 if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0)
214 spare = ioc5->HotSpare;
215 for (i = 0; i < ioc5->NumHotSpares; spare++, i++)
216 if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0)
220 disk = ioc3->PhysDisk;
221 for (i = 0; i < ioc3->NumPhysDisks; disk++, i++)
222 if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0)
230 mpt_free_pd_list(struct mpt_drive_list *list)
234 for (i = 0; i < list->ndrives; i++)
235 free(list->drives[i]);
240 mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
247 /* Look for a raw device id first. */
248 val = strtol(drive, &cp, 0);
250 if (val < 0 || val > 0xff)
256 /* Look for a <bus>:<id> string. */
258 if (val < 0 || val > 0xff)
261 val = strtol(cp + 1, &cp, 0);
264 if (val < 0 || val > 0xff)
268 for (val = 0; val < list->ndrives; val++) {
269 if (list->drives[val]->PhysDiskBus == bus &&
270 list->drives[val]->PhysDiskID == id) {
271 *PhysDiskNum = list->drives[val]->PhysDiskNum;
282 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
284 mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info)
286 RAID_PHYS_DISK0_INQUIRY_DATA *inq_data;
287 u_char vendor[9], product[17], revision[5];
288 static char inq_string[64];
290 inq_data = &pd_info->InquiryData;
291 cam_strvis(vendor, inq_data->VendorID, sizeof(inq_data->VendorID),
293 cam_strvis(product, inq_data->ProductID, sizeof(inq_data->ProductID),
295 cam_strvis(revision, inq_data->ProductRevLevel,
296 sizeof(inq_data->ProductRevLevel), sizeof(revision));
299 if (strcmp(vendor, "ATA") == 0)
300 snprintf(inq_string, sizeof(inq_string), "<%s %s> SATA",
303 snprintf(inq_string, sizeof(inq_string), "<%s %s %s> SAS",
304 vendor, product, revision);
308 /* Helper function to set a drive to a given state. */
310 drive_set_state(char *drive, U8 Action, U8 State, const char *name)
312 CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
313 struct mpt_drive_list *list;
317 fd = mpt_open(mpt_unit);
324 list = mpt_pd_list(fd);
328 if (mpt_lookup_drive(list, drive, &PhysDiskNum) < 0) {
330 warn("Failed to find drive %s", drive);
333 mpt_free_pd_list(list);
335 /* Get the info for this drive. */
336 info = mpt_pd_info(fd, PhysDiskNum, NULL);
339 warn("Failed to fetch info for drive %u", PhysDiskNum);
343 /* Try to change the state. */
344 if (info->PhysDiskStatus.State == State) {
345 warnx("Drive %u is already in the desired state", PhysDiskNum);
349 error = mpt_raid_action(fd, Action, 0, 0, PhysDiskNum, 0, NULL, 0, NULL,
350 NULL, 0, NULL, NULL, 0);
352 warnc(error, "Failed to set drive %u to %s", PhysDiskNum, name);
363 fail_drive(int ac, char **av)
367 warnx("fail: %s", ac > 2 ? "extra arguments" :
372 return (drive_set_state(av[1], MPI_RAID_ACTION_FAIL_PHYSDISK,
373 MPI_PHYSDISK0_STATUS_FAILED_REQUESTED, "FAILED"));
375 MPT_COMMAND(top, fail, fail_drive);
378 online_drive(int ac, char **av)
382 warnx("online: %s", ac > 2 ? "extra arguments" :
387 return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_ONLINE,
388 MPI_PHYSDISK0_STATUS_ONLINE, "ONLINE"));
390 MPT_COMMAND(top, online, online_drive);
393 offline_drive(int ac, char **av)
397 warnx("offline: %s", ac > 2 ? "extra arguments" :
402 return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_OFFLINE,
403 MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED, "OFFLINE"));
405 MPT_COMMAND(top, offline, offline_drive);