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:
68 case MFI_PD_STATE_COPYBACK:
70 case MFI_PD_STATE_SYSTEM:
73 sprintf(buf, "PSTATE 0x%04x", state);
79 mfi_lookup_drive(int fd, char *drive, uint16_t *device_id)
81 struct mfi_pd_list *list;
88 /* Look for a raw device id first. */
89 val = strtol(drive, &cp, 0);
91 if (val < 0 || val >= 0xffff)
97 /* Support for MegaCli style [Exx]:Syy notation. */
98 if (toupper(drive[0]) == 'E' || toupper(drive[0]) == 'S') {
102 if (toupper(drive[0]) == 'E') {
104 val = strtol(cp, &cp, 0);
105 if (val < 0 || val > 0xff || *cp != ':')
109 if (toupper(*cp) != 'S')
116 val = strtol(cp, &cp, 0);
117 if (val < 0 || val > 0xff || *cp != '\0')
121 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
123 warn("Failed to fetch drive list");
127 for (i = 0; i < list->count; i++) {
128 if (list->addr[i].scsi_dev_type != 0)
131 if (((encl == 0xff &&
132 list->addr[i].encl_device_id == 0xffff) ||
133 list->addr[i].encl_index == encl) &&
134 list->addr[i].slot_number == slot) {
135 *device_id = list->addr[i].device_id;
141 warnx("Unknown drive %s", drive);
146 warnx("Invalid drive number %s", drive);
151 mbox_store_device_id(uint8_t *mbox, uint16_t device_id)
154 mbox[0] = device_id & 0xff;
155 mbox[1] = device_id >> 8;
159 mbox_store_pdref(uint8_t *mbox, union mfi_pd_ref *ref)
162 mbox[0] = ref->v.device_id & 0xff;
163 mbox[1] = ref->v.device_id >> 8;
164 mbox[2] = ref->v.seq_num & 0xff;
165 mbox[3] = ref->v.seq_num >> 8;
169 mfi_pd_get_list(int fd, struct mfi_pd_list **listp, uint8_t *statusp)
171 struct mfi_pd_list *list;
175 * Keep fetching the list in a loop until we have a large enough
176 * buffer to hold the entire list.
181 list = reallocf(list, list_size);
184 if (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_LIST, list, list_size, NULL,
190 if (list->size > list_size) {
191 list_size = list->size;
200 mfi_pd_get_info(int fd, uint16_t device_id, struct mfi_pd_info *info,
205 mbox_store_device_id(&mbox[0], device_id);
206 return (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_INFO, info,
207 sizeof(struct mfi_pd_info), mbox, 2, statusp));
211 cam_strvis(char *dst, const char *src, int srclen, int dstlen)
214 /* Trim leading/trailing spaces, nulls. */
215 while (srclen > 0 && src[0] == ' ')
218 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
221 while (srclen > 0 && dstlen > 1) {
225 /* SCSI-II Specifies that these should never occur. */
226 /* non-printable character */
229 *cur_pos++ = ((*src & 0300) >> 6) + '0';
230 *cur_pos++ = ((*src & 0070) >> 3) + '0';
231 *cur_pos++ = ((*src & 0007) >> 0) + '0';
236 /* normal character */
241 dstlen -= cur_pos - dst;
247 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
249 mfi_pd_inq_string(struct mfi_pd_info *info)
251 struct scsi_inquiry_data *inq_data;
252 char vendor[16], product[48], revision[16], rstr[12], serial[SID_VENDOR_SPECIFIC_0_SIZE];
253 static char inq_string[64];
255 inq_data = (struct scsi_inquiry_data *)info->inquiry_data;
256 if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
258 if (SID_TYPE(inq_data) != T_DIRECT)
260 if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
263 cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
265 cam_strvis(product, inq_data->product, sizeof(inq_data->product),
267 cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
269 cam_strvis(serial, (char *)inq_data->vendor_specific0, sizeof(inq_data->vendor_specific0),
272 /* Hack for SATA disks, no idea how to tell speed. */
273 if (strcmp(vendor, "ATA") == 0) {
274 snprintf(inq_string, sizeof(inq_string), "<%s %s serial=%s> SATA",
275 product, revision, serial);
279 switch (SID_ANSI_REV(inq_data)) {
281 strcpy(rstr, "SCSI-CCS");
287 snprintf(rstr, sizeof (rstr), "SCSI-%d",
288 SID_ANSI_REV(inq_data));
291 snprintf(inq_string, sizeof(inq_string), "<%s %s %s serial=%s> %s", vendor,
292 product, revision, serial, rstr);
296 /* Helper function to set a drive to a given state. */
298 drive_set_state(char *drive, uint16_t new_state)
300 struct mfi_pd_info info;
305 fd = mfi_open(mfi_unit);
312 error = mfi_lookup_drive(fd, drive, &device_id);
316 /* Get the info for this drive. */
317 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
319 warn("Failed to fetch info for drive %u", device_id);
323 /* Try to change the state. */
324 if (info.fw_state == new_state) {
325 warnx("Drive %u is already in the desired state", device_id);
329 mbox_store_pdref(&mbox[0], &info.ref);
330 mbox[4] = new_state & 0xff;
331 mbox[5] = new_state >> 8;
332 if (mfi_dcmd_command(fd, MFI_DCMD_PD_STATE_SET, NULL, 0, mbox, 6,
335 warn("Failed to set drive %u to %s", device_id,
336 mfi_pdstate(new_state));
346 fail_drive(int ac, char **av)
350 warnx("fail: %s", ac > 2 ? "extra arguments" :
355 return (drive_set_state(av[1], MFI_PD_STATE_FAILED));
357 MFI_COMMAND(top, fail, fail_drive);
360 good_drive(int ac, char **av)
364 warnx("good: %s", ac > 2 ? "extra arguments" :
369 return (drive_set_state(av[1], MFI_PD_STATE_UNCONFIGURED_GOOD));
371 MFI_COMMAND(top, good, good_drive);
374 rebuild_drive(int ac, char **av)
378 warnx("rebuild: %s", ac > 2 ? "extra arguments" :
383 return (drive_set_state(av[1], MFI_PD_STATE_REBUILD));
385 MFI_COMMAND(top, rebuild, rebuild_drive);
388 start_rebuild(int ac, char **av)
390 struct mfi_pd_info info;
396 warnx("start rebuild: %s", ac > 2 ? "extra arguments" :
401 fd = mfi_open(mfi_unit);
408 error = mfi_lookup_drive(fd, av[1], &device_id);
412 /* Get the info for this drive. */
413 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
415 warn("Failed to fetch info for drive %u", device_id);
419 /* Check the state, must be REBUILD. */
420 if (info.fw_state != MFI_PD_STATE_REBUILD) {
421 warnx("Drive %d is not in the REBUILD state", device_id);
425 /* Start the rebuild. */
426 mbox_store_pdref(&mbox[0], &info.ref);
427 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_START, NULL, 0, mbox, 4,
430 warn("Failed to start rebuild on drive %u", device_id);
437 MFI_COMMAND(start, rebuild, start_rebuild);
440 abort_rebuild(int ac, char **av)
442 struct mfi_pd_info info;
448 warnx("abort rebuild: %s", ac > 2 ? "extra arguments" :
453 fd = mfi_open(mfi_unit);
460 error = mfi_lookup_drive(fd, av[1], &device_id);
464 /* Get the info for this drive. */
465 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
467 warn("Failed to fetch info for drive %u", device_id);
471 /* Check the state, must be REBUILD. */
472 if (info.fw_state != MFI_PD_STATE_REBUILD) {
473 warn("Drive %d is not in the REBUILD state", device_id);
477 /* Abort the rebuild. */
478 mbox_store_pdref(&mbox[0], &info.ref);
479 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_ABORT, NULL, 0, mbox, 4,
482 warn("Failed to abort rebuild on drive %u", device_id);
489 MFI_COMMAND(abort, rebuild, abort_rebuild);
492 drive_progress(int ac, char **av)
494 struct mfi_pd_info info;
499 warnx("drive progress: %s", ac > 2 ? "extra arguments" :
504 fd = mfi_open(mfi_unit);
511 error = mfi_lookup_drive(fd, av[1], &device_id);
515 /* Get the info for this drive. */
516 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
518 warn("Failed to fetch info for drive %u", device_id);
523 /* Display any of the active events. */
524 if (info.prog_info.active & MFI_PD_PROGRESS_REBUILD)
525 mfi_display_progress("Rebuild", &info.prog_info.rbld);
526 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL)
527 mfi_display_progress("Patrol Read", &info.prog_info.patrol);
528 if (info.prog_info.active & MFI_PD_PROGRESS_CLEAR)
529 mfi_display_progress("Clear", &info.prog_info.clear);
530 if ((info.prog_info.active & (MFI_PD_PROGRESS_REBUILD |
531 MFI_PD_PROGRESS_PATROL | MFI_PD_PROGRESS_CLEAR)) == 0)
532 printf("No activity in progress for drive %u.\n", device_id);
536 MFI_COMMAND(drive, progress, drive_progress);
539 drive_clear(int ac, char **av)
541 struct mfi_pd_info info;
549 warnx("drive clear: %s", ac > 3 ? "extra arguments" :
550 "drive and action requires");
554 for (s1 = av[2]; *s1 != '\0'; s1++)
556 if (strcmp(av[2], "start") == 0)
557 opcode = MFI_DCMD_PD_CLEAR_START;
558 else if ((strcmp(av[2], "stop") == 0) || (strcmp(av[2], "abort") == 0))
559 opcode = MFI_DCMD_PD_CLEAR_ABORT;
561 warnx("drive clear: invalid action, must be 'start' or 'stop'\n");
565 fd = mfi_open(mfi_unit);
572 error = mfi_lookup_drive(fd, av[1], &device_id);
576 /* Get the info for this drive. */
577 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
579 warn("Failed to fetch info for drive %u", device_id);
583 mbox_store_pdref(&mbox[0], &info.ref);
584 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
586 warn("Failed to %s clear on drive %u",
587 opcode == MFI_DCMD_PD_CLEAR_START ? "start" : "stop",
595 MFI_COMMAND(drive, clear, drive_clear);
598 drive_locate(int ac, char **av)
606 warnx("locate: %s", ac > 3 ? "extra arguments" :
607 "drive and state required");
611 if (strcasecmp(av[2], "on") == 0 || strcasecmp(av[2], "start") == 0)
612 opcode = MFI_DCMD_PD_LOCATE_START;
613 else if (strcasecmp(av[2], "off") == 0 ||
614 strcasecmp(av[2], "stop") == 0)
615 opcode = MFI_DCMD_PD_LOCATE_STOP;
617 warnx("locate: invalid state %s", av[2]);
621 fd = mfi_open(mfi_unit);
628 error = mfi_lookup_drive(fd, av[1], &device_id);
633 mbox_store_device_id(&mbox[0], device_id);
636 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
638 warn("Failed to %s locate on drive %u",
639 opcode == MFI_DCMD_PD_LOCATE_START ? "start" : "stop",
647 MFI_COMMAND(top, locate, drive_locate);