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