2 * Copyright (c) 2008, 2009 Yahoo!, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/errno.h>
43 #include <cam/scsi/scsi_all.h>
46 MFI_TABLE(top, drive);
49 mfi_pdstate(enum mfi_pd_state state)
54 case MFI_PD_STATE_UNCONFIGURED_GOOD:
55 return ("UNCONFIGURED GOOD");
56 case MFI_PD_STATE_UNCONFIGURED_BAD:
57 return ("UNCONFIGURED BAD");
58 case MFI_PD_STATE_HOT_SPARE:
60 case MFI_PD_STATE_OFFLINE:
62 case MFI_PD_STATE_FAILED:
64 case MFI_PD_STATE_REBUILD:
66 case MFI_PD_STATE_ONLINE:
69 sprintf(buf, "PSTATE 0x%04x", state);
75 mfi_lookup_drive(int fd, char *drive, uint16_t *device_id)
77 struct mfi_pd_list *list;
83 /* Look for a raw device id first. */
84 val = strtol(drive, &cp, 0);
86 if (val < 0 || val >= 0xffff)
92 /* Support for MegaCli style [Exx]:Syy notation. */
93 if (toupper(drive[0]) == 'E' || toupper(drive[0]) == 'S') {
97 if (toupper(drive[0]) == 'E') {
99 val = strtol(cp, &cp, 0);
100 if (val < 0 || val > 0xff || *cp != ':')
104 if (toupper(*cp) != 'S')
111 val = strtol(cp, &cp, 0);
112 if (val < 0 || val > 0xff || *cp != '\0')
116 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
117 warn("Failed to fetch drive list");
121 for (i = 0; i < list->count; i++) {
122 if (list->addr[i].scsi_dev_type != 0)
125 if (((encl == 0xff &&
126 list->addr[i].encl_device_id == 0xffff) ||
127 list->addr[i].encl_index == encl) &&
128 list->addr[i].slot_number == slot) {
129 *device_id = list->addr[i].device_id;
135 warnx("Unknown drive %s", drive);
140 warnx("Invalid drive number %s", drive);
145 mbox_store_device_id(uint8_t *mbox, uint16_t device_id)
148 mbox[0] = device_id & 0xff;
149 mbox[1] = device_id >> 8;
153 mbox_store_pdref(uint8_t *mbox, union mfi_pd_ref *ref)
156 mbox[0] = ref->v.device_id & 0xff;
157 mbox[1] = ref->v.device_id >> 8;
158 mbox[2] = ref->v.seq_num & 0xff;
159 mbox[3] = ref->v.seq_num >> 8;
163 mfi_pd_get_list(int fd, struct mfi_pd_list **listp, uint8_t *statusp)
165 struct mfi_pd_list *list;
169 * Keep fetching the list in a loop until we have a large enough
170 * buffer to hold the entire list.
175 list = reallocf(list, list_size);
178 if (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_LIST, list, list_size, NULL,
184 if (list->size > list_size) {
185 list_size = list->size;
194 mfi_pd_get_info(int fd, uint16_t device_id, struct mfi_pd_info *info,
199 mbox_store_device_id(&mbox[0], device_id);
200 return (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_INFO, info,
201 sizeof(struct mfi_pd_info), mbox, 2, statusp));
205 cam_strvis(char *dst, const char *src, int srclen, int dstlen)
208 /* Trim leading/trailing spaces, nulls. */
209 while (srclen > 0 && src[0] == ' ')
212 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
215 while (srclen > 0 && dstlen > 1) {
219 /* SCSI-II Specifies that these should never occur. */
220 /* non-printable character */
223 *cur_pos++ = ((*src & 0300) >> 6) + '0';
224 *cur_pos++ = ((*src & 0070) >> 3) + '0';
225 *cur_pos++ = ((*src & 0007) >> 0) + '0';
230 /* normal character */
235 dstlen -= cur_pos - dst;
241 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
243 mfi_pd_inq_string(struct mfi_pd_info *info)
245 struct scsi_inquiry_data *inq_data;
246 char vendor[16], product[48], revision[16], rstr[12], serial[SID_VENDOR_SPECIFIC_0_SIZE];
247 static char inq_string[64];
249 inq_data = (struct scsi_inquiry_data *)info->inquiry_data;
250 if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
252 if (SID_TYPE(inq_data) != T_DIRECT)
254 if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
257 cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
259 cam_strvis(product, inq_data->product, sizeof(inq_data->product),
261 cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
263 cam_strvis(serial, (char *)inq_data->vendor_specific0, sizeof(inq_data->vendor_specific0),
266 /* Hack for SATA disks, no idea how to tell speed. */
267 if (strcmp(vendor, "ATA") == 0) {
268 snprintf(inq_string, sizeof(inq_string), "<%s %s serial=%s> SATA",
269 product, revision, serial);
273 switch (SID_ANSI_REV(inq_data)) {
275 strcpy(rstr, "SCSI-CCS");
281 snprintf(rstr, sizeof (rstr), "SCSI-%d",
282 SID_ANSI_REV(inq_data));
285 snprintf(inq_string, sizeof(inq_string), "<%s %s %s serial=%s> %s", vendor,
286 product, revision, serial, rstr);
290 /* Helper function to set a drive to a given state. */
292 drive_set_state(char *drive, uint16_t new_state)
294 struct mfi_pd_info info;
299 fd = mfi_open(mfi_unit);
305 error = mfi_lookup_drive(fd, drive, &device_id);
309 /* Get the info for this drive. */
310 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
311 warn("Failed to fetch info for drive %u", device_id);
315 /* Try to change the state. */
316 if (info.fw_state == new_state) {
317 warnx("Drive %u is already in the desired state", device_id);
321 mbox_store_pdref(&mbox[0], &info.ref);
322 mbox[4] = new_state & 0xff;
323 mbox[5] = new_state >> 8;
324 if (mfi_dcmd_command(fd, MFI_DCMD_PD_STATE_SET, NULL, 0, mbox, 6,
326 warn("Failed to set drive %u to %s", device_id,
327 mfi_pdstate(new_state));
337 fail_drive(int ac, char **av)
341 warnx("fail: %s", ac > 2 ? "extra arguments" :
346 return (drive_set_state(av[1], MFI_PD_STATE_FAILED));
348 MFI_COMMAND(top, fail, fail_drive);
351 good_drive(int ac, char **av)
355 warnx("good: %s", ac > 2 ? "extra arguments" :
360 return (drive_set_state(av[1], MFI_PD_STATE_UNCONFIGURED_GOOD));
362 MFI_COMMAND(top, good, good_drive);
365 rebuild_drive(int ac, char **av)
369 warnx("rebuild: %s", ac > 2 ? "extra arguments" :
374 return (drive_set_state(av[1], MFI_PD_STATE_REBUILD));
376 MFI_COMMAND(top, rebuild, rebuild_drive);
379 start_rebuild(int ac, char **av)
381 struct mfi_pd_info info;
387 warnx("start rebuild: %s", ac > 2 ? "extra arguments" :
392 fd = mfi_open(mfi_unit);
398 error = mfi_lookup_drive(fd, av[1], &device_id);
402 /* Get the info for this drive. */
403 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
404 warn("Failed to fetch info for drive %u", device_id);
408 /* Check the state, must be REBUILD. */
409 if (info.fw_state != MFI_PD_STATE_REBUILD) {
410 warn("Drive %d is not in the REBUILD state", device_id);
414 /* Start the rebuild. */
415 mbox_store_pdref(&mbox[0], &info.ref);
416 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_START, NULL, 0, mbox, 4,
418 warn("Failed to start rebuild on drive %u", device_id);
425 MFI_COMMAND(start, rebuild, start_rebuild);
428 abort_rebuild(int ac, char **av)
430 struct mfi_pd_info info;
436 warnx("abort rebuild: %s", ac > 2 ? "extra arguments" :
441 fd = mfi_open(mfi_unit);
447 error = mfi_lookup_drive(fd, av[1], &device_id);
451 /* Get the info for this drive. */
452 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
453 warn("Failed to fetch info for drive %u", device_id);
457 /* Check the state, must be REBUILD. */
458 if (info.fw_state != MFI_PD_STATE_REBUILD) {
459 warn("Drive %d is not in the REBUILD state", device_id);
463 /* Abort the rebuild. */
464 mbox_store_pdref(&mbox[0], &info.ref);
465 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_ABORT, NULL, 0, mbox, 4,
467 warn("Failed to abort rebuild on drive %u", device_id);
474 MFI_COMMAND(abort, rebuild, abort_rebuild);
477 drive_progress(int ac, char **av)
479 struct mfi_pd_info info;
484 warnx("drive progress: %s", ac > 2 ? "extra arguments" :
489 fd = mfi_open(mfi_unit);
495 error = mfi_lookup_drive(fd, av[1], &device_id);
499 /* Get the info for this drive. */
500 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
501 warn("Failed to fetch info for drive %u", device_id);
506 /* Display any of the active events. */
507 if (info.prog_info.active & MFI_PD_PROGRESS_REBUILD)
508 mfi_display_progress("Rebuild", &info.prog_info.rbld);
509 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL)
510 mfi_display_progress("Patrol Read", &info.prog_info.patrol);
511 if (info.prog_info.active & MFI_PD_PROGRESS_CLEAR)
512 mfi_display_progress("Clear", &info.prog_info.clear);
513 if ((info.prog_info.active & (MFI_PD_PROGRESS_REBUILD |
514 MFI_PD_PROGRESS_PATROL | MFI_PD_PROGRESS_CLEAR)) == 0)
515 printf("No activity in progress for drive %u.\n", device_id);
519 MFI_COMMAND(drive, progress, drive_progress);
522 drive_clear(int ac, char **av)
524 struct mfi_pd_info info;
532 warnx("drive clear: %s", ac > 3 ? "extra arguments" :
533 "drive and action requires");
537 for (s1 = av[2]; *s1 != '\0'; s1++)
539 if (strcmp(av[2], "start") == 0)
540 opcode = MFI_DCMD_PD_CLEAR_START;
541 else if ((strcmp(av[2], "stop") == 0) || (strcmp(av[2], "abort") == 0))
542 opcode = MFI_DCMD_PD_CLEAR_ABORT;
544 warnx("drive clear: invalid action, must be 'start' or 'stop'\n");
548 fd = mfi_open(mfi_unit);
554 error = mfi_lookup_drive(fd, av[1], &device_id);
558 /* Get the info for this drive. */
559 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
560 warn("Failed to fetch info for drive %u", device_id);
564 mbox_store_pdref(&mbox[0], &info.ref);
565 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
566 warn("Failed to %s clear on drive %u",
567 opcode == MFI_DCMD_PD_CLEAR_START ? "start" : "stop",
575 MFI_COMMAND(drive, clear, drive_clear);
578 drive_locate(int ac, char **av)
586 warnx("locate: %s", ac > 3 ? "extra arguments" :
587 "drive and state required");
591 if (strcasecmp(av[2], "on") == 0 || strcasecmp(av[2], "start") == 0)
592 opcode = MFI_DCMD_PD_LOCATE_START;
593 else if (strcasecmp(av[2], "off") == 0 ||
594 strcasecmp(av[2], "stop") == 0)
595 opcode = MFI_DCMD_PD_LOCATE_STOP;
597 warnx("locate: invalid state %s", av[2]);
601 fd = mfi_open(mfi_unit);
607 error = mfi_lookup_drive(fd, av[1], &device_id);
612 mbox_store_device_id(&mbox[0], device_id);
615 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
616 warn("Failed to %s locate on drive %u",
617 opcode == MFI_DCMD_PD_LOCATE_START ? "start" : "stop",
625 MFI_COMMAND(top, locate, drive_locate);