2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008, 2009 Yahoo!, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/errno.h>
46 #include <cam/scsi/scsi_all.h>
49 MFI_TABLE(top, drive);
52 * Print the name of a drive either by drive number as %2u or by enclosure:slot
53 * as Exx:Sxx (or both). Use default unless command line options override it
54 * and the command allows this (which we usually do unless we already print
55 * both). We prefer pinfo if given, otherwise try to look it up by device_id.
58 mfi_drive_name(struct mfi_pd_info *pinfo, uint16_t device_id, uint32_t def)
60 struct mfi_pd_info info;
65 if ((def & MFI_DNAME_HONOR_OPTS) != 0 &&
66 (mfi_opts & (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID)) != 0)
67 def = mfi_opts & (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID);
70 if (pinfo == NULL && def & MFI_DNAME_ES) {
71 /* Fallback in case of error, just ignore flags. */
72 if (device_id == 0xffff)
73 snprintf(buf, sizeof(buf), "MISSING");
75 snprintf(buf, sizeof(buf), "%2u", device_id);
77 fd = mfi_open(mfi_unit, O_RDWR);
83 /* Get the info for this drive. */
84 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
85 warn("Failed to fetch info for drive %2u", device_id);
96 if (def & MFI_DNAME_DEVICE_ID) {
97 if (device_id == 0xffff)
98 error = snprintf(p, len, "MISSING");
100 error = snprintf(p, len, "%2u", device_id);
106 if ((def & (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID)) ==
107 (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID) && len >= 2) {
113 if (def & MFI_DNAME_ES) {
114 if (pinfo->encl_device_id == 0xffff)
115 error = snprintf(p, len, "S%u",
117 else if (pinfo->encl_device_id == pinfo->ref.v.device_id)
118 error = snprintf(p, len, "E%u",
121 error = snprintf(p, len, "E%u:S%u",
122 pinfo->encl_index, pinfo->slot_number);
133 mfi_pdstate(enum mfi_pd_state state)
138 case MFI_PD_STATE_UNCONFIGURED_GOOD:
139 return ("UNCONFIGURED GOOD");
140 case MFI_PD_STATE_UNCONFIGURED_BAD:
141 return ("UNCONFIGURED BAD");
142 case MFI_PD_STATE_HOT_SPARE:
143 return ("HOT SPARE");
144 case MFI_PD_STATE_OFFLINE:
146 case MFI_PD_STATE_FAILED:
148 case MFI_PD_STATE_REBUILD:
150 case MFI_PD_STATE_ONLINE:
152 case MFI_PD_STATE_COPYBACK:
154 case MFI_PD_STATE_SYSTEM:
157 sprintf(buf, "PSTATE 0x%04x", state);
163 mfi_lookup_drive(int fd, char *drive, uint16_t *device_id)
165 struct mfi_pd_list *list;
172 /* Look for a raw device id first. */
173 val = strtol(drive, &cp, 0);
175 if (val < 0 || val >= 0xffff)
181 /* Support for MegaCli style [Exx]:Syy notation. */
182 if (toupper(drive[0]) == 'E' || toupper(drive[0]) == 'S') {
183 if (drive[1] == '\0')
186 if (toupper(drive[0]) == 'E') {
188 val = strtol(cp, &cp, 0);
189 if (val < 0 || val > 0xff || *cp != ':')
193 if (toupper(*cp) != 'S')
200 val = strtol(cp, &cp, 0);
201 if (val < 0 || val > 0xff || *cp != '\0')
205 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
207 warn("Failed to fetch drive list");
211 for (i = 0; i < list->count; i++) {
212 if (list->addr[i].scsi_dev_type != 0)
215 if (((encl == 0xff &&
216 list->addr[i].encl_device_id == 0xffff) ||
217 list->addr[i].encl_index == encl) &&
218 list->addr[i].slot_number == slot) {
219 *device_id = list->addr[i].device_id;
225 warnx("Unknown drive %s", drive);
230 warnx("Invalid drive number %s", drive);
235 mbox_store_device_id(uint8_t *mbox, uint16_t device_id)
238 mbox[0] = device_id & 0xff;
239 mbox[1] = device_id >> 8;
243 mbox_store_pdref(uint8_t *mbox, union mfi_pd_ref *ref)
246 mbox[0] = ref->v.device_id & 0xff;
247 mbox[1] = ref->v.device_id >> 8;
248 mbox[2] = ref->v.seq_num & 0xff;
249 mbox[3] = ref->v.seq_num >> 8;
253 mfi_pd_get_list(int fd, struct mfi_pd_list **listp, uint8_t *statusp)
255 struct mfi_pd_list *list;
259 * Keep fetching the list in a loop until we have a large enough
260 * buffer to hold the entire list.
265 list = reallocf(list, list_size);
268 if (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_LIST, list, list_size, NULL,
274 if (list->size > list_size) {
275 list_size = list->size;
284 mfi_pd_get_info(int fd, uint16_t device_id, struct mfi_pd_info *info,
289 mbox_store_device_id(&mbox[0], device_id);
290 return (mfi_dcmd_command(fd, MFI_DCMD_PD_GET_INFO, info,
291 sizeof(struct mfi_pd_info), mbox, 2, statusp));
295 cam_strvis(char *dst, const char *src, int srclen, int dstlen)
298 /* Trim leading/trailing spaces, nulls. */
299 while (srclen > 0 && src[0] == ' ')
302 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
305 while (srclen > 0 && dstlen > 1) {
309 /* SCSI-II Specifies that these should never occur. */
310 /* non-printable character */
313 *cur_pos++ = ((*src & 0300) >> 6) + '0';
314 *cur_pos++ = ((*src & 0070) >> 3) + '0';
315 *cur_pos++ = ((*src & 0007) >> 0) + '0';
320 /* normal character */
325 dstlen -= cur_pos - dst;
331 /* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
333 mfi_pd_inq_string(struct mfi_pd_info *info)
335 struct scsi_inquiry_data iqd, *inq_data = &iqd;
336 char vendor[16], product[48], revision[16], rstr[12], serial[SID_VENDOR_SPECIFIC_0_SIZE];
337 static char inq_string[64];
339 memcpy(inq_data, info->inquiry_data,
340 (sizeof (iqd) < sizeof (info->inquiry_data))?
341 sizeof (iqd) : sizeof (info->inquiry_data));
342 if (SID_QUAL_IS_VENDOR_UNIQUE(inq_data))
344 if (SID_TYPE(inq_data) != T_DIRECT)
346 if (SID_QUAL(inq_data) != SID_QUAL_LU_CONNECTED)
349 cam_strvis(vendor, inq_data->vendor, sizeof(inq_data->vendor),
351 cam_strvis(product, inq_data->product, sizeof(inq_data->product),
353 cam_strvis(revision, inq_data->revision, sizeof(inq_data->revision),
355 cam_strvis(serial, (char *)inq_data->vendor_specific0, sizeof(inq_data->vendor_specific0),
358 /* Hack for SATA disks, no idea how to tell speed. */
359 if (strcmp(vendor, "ATA") == 0) {
360 snprintf(inq_string, sizeof(inq_string), "<%s %s serial=%s> SATA",
361 product, revision, serial);
365 switch (SID_ANSI_REV(inq_data)) {
367 strcpy(rstr, "SCSI-CCS");
373 snprintf(rstr, sizeof (rstr), "SCSI-%d",
374 SID_ANSI_REV(inq_data));
377 snprintf(inq_string, sizeof(inq_string), "<%s %s %s serial=%s> %s", vendor,
378 product, revision, serial, rstr);
382 /* Helper function to set a drive to a given state. */
384 drive_set_state(char *drive, uint16_t new_state)
386 struct mfi_pd_info info;
391 fd = mfi_open(mfi_unit, O_RDWR);
398 error = mfi_lookup_drive(fd, drive, &device_id);
404 /* Get the info for this drive. */
405 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
407 warn("Failed to fetch info for drive %u", device_id);
412 /* Try to change the state. */
413 if (info.fw_state == new_state) {
414 warnx("Drive %u is already in the desired state", device_id);
419 mbox_store_pdref(&mbox[0], &info.ref);
420 mbox[4] = new_state & 0xff;
421 mbox[5] = new_state >> 8;
422 if (mfi_dcmd_command(fd, MFI_DCMD_PD_STATE_SET, NULL, 0, mbox, 6,
425 warn("Failed to set drive %u to %s", device_id,
426 mfi_pdstate(new_state));
437 fail_drive(int ac, char **av)
441 warnx("fail: %s", ac > 2 ? "extra arguments" :
446 return (drive_set_state(av[1], MFI_PD_STATE_FAILED));
448 MFI_COMMAND(top, fail, fail_drive);
451 good_drive(int ac, char **av)
455 warnx("good: %s", ac > 2 ? "extra arguments" :
460 return (drive_set_state(av[1], MFI_PD_STATE_UNCONFIGURED_GOOD));
462 MFI_COMMAND(top, good, good_drive);
465 rebuild_drive(int ac, char **av)
469 warnx("rebuild: %s", ac > 2 ? "extra arguments" :
474 return (drive_set_state(av[1], MFI_PD_STATE_REBUILD));
476 MFI_COMMAND(top, rebuild, rebuild_drive);
479 syspd_drive(int ac, char **av)
483 warnx("syspd: %s", ac > 2 ? "extra arguments" :
488 return (drive_set_state(av[1], MFI_PD_STATE_SYSTEM));
490 MFI_COMMAND(top, syspd, syspd_drive);
493 start_rebuild(int ac, char **av)
495 struct mfi_pd_info info;
501 warnx("start rebuild: %s", ac > 2 ? "extra arguments" :
506 fd = mfi_open(mfi_unit, O_RDWR);
513 error = mfi_lookup_drive(fd, av[1], &device_id);
519 /* Get the info for this drive. */
520 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
522 warn("Failed to fetch info for drive %u", device_id);
527 /* Check the state, must be REBUILD. */
528 if (info.fw_state != MFI_PD_STATE_REBUILD) {
529 warnx("Drive %d is not in the REBUILD state", device_id);
534 /* Start the rebuild. */
535 mbox_store_pdref(&mbox[0], &info.ref);
536 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_START, NULL, 0, mbox, 4,
539 warn("Failed to start rebuild on drive %u", device_id);
547 MFI_COMMAND(start, rebuild, start_rebuild);
550 abort_rebuild(int ac, char **av)
552 struct mfi_pd_info info;
558 warnx("abort rebuild: %s", ac > 2 ? "extra arguments" :
563 fd = mfi_open(mfi_unit, O_RDWR);
570 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);
584 /* Check the state, must be REBUILD. */
585 if (info.fw_state != MFI_PD_STATE_REBUILD) {
586 warn("Drive %d is not in the REBUILD state", device_id);
591 /* Abort the rebuild. */
592 mbox_store_pdref(&mbox[0], &info.ref);
593 if (mfi_dcmd_command(fd, MFI_DCMD_PD_REBUILD_ABORT, NULL, 0, mbox, 4,
596 warn("Failed to abort rebuild on drive %u", device_id);
604 MFI_COMMAND(abort, rebuild, abort_rebuild);
607 drive_progress(int ac, char **av)
609 struct mfi_pd_info info;
614 warnx("drive progress: %s", ac > 2 ? "extra arguments" :
619 fd = mfi_open(mfi_unit, O_RDWR);
626 error = mfi_lookup_drive(fd, av[1], &device_id);
632 /* Get the info for this drive. */
633 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
635 warn("Failed to fetch info for drive %u", device_id);
641 /* Display any of the active events. */
642 if (info.prog_info.active & MFI_PD_PROGRESS_REBUILD)
643 mfi_display_progress("Rebuild", &info.prog_info.rbld);
644 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL)
645 mfi_display_progress("Patrol Read", &info.prog_info.patrol);
646 if (info.prog_info.active & MFI_PD_PROGRESS_CLEAR)
647 mfi_display_progress("Clear", &info.prog_info.clear);
648 if ((info.prog_info.active & (MFI_PD_PROGRESS_REBUILD |
649 MFI_PD_PROGRESS_PATROL | MFI_PD_PROGRESS_CLEAR)) == 0)
650 printf("No activity in progress for drive %s.\n",
651 mfi_drive_name(NULL, device_id,
652 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
656 MFI_COMMAND(drive, progress, drive_progress);
659 drive_clear(int ac, char **av)
661 struct mfi_pd_info info;
669 warnx("drive clear: %s", ac > 3 ? "extra arguments" :
670 "drive and action requires");
674 for (s1 = av[2]; *s1 != '\0'; s1++)
676 if (strcmp(av[2], "start") == 0)
677 opcode = MFI_DCMD_PD_CLEAR_START;
678 else if ((strcmp(av[2], "stop") == 0) || (strcmp(av[2], "abort") == 0))
679 opcode = MFI_DCMD_PD_CLEAR_ABORT;
681 warnx("drive clear: invalid action, must be 'start' or 'stop'\n");
685 fd = mfi_open(mfi_unit, O_RDWR);
692 error = mfi_lookup_drive(fd, av[1], &device_id);
698 /* Get the info for this drive. */
699 if (mfi_pd_get_info(fd, device_id, &info, NULL) < 0) {
701 warn("Failed to fetch info for drive %u", device_id);
706 mbox_store_pdref(&mbox[0], &info.ref);
707 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
709 warn("Failed to %s clear on drive %u",
710 opcode == MFI_DCMD_PD_CLEAR_START ? "start" : "stop",
719 MFI_COMMAND(drive, clear, drive_clear);
722 drive_locate(int ac, char **av)
730 warnx("locate: %s", ac > 3 ? "extra arguments" :
731 "drive and state required");
735 if (strcasecmp(av[2], "on") == 0 || strcasecmp(av[2], "start") == 0)
736 opcode = MFI_DCMD_PD_LOCATE_START;
737 else if (strcasecmp(av[2], "off") == 0 ||
738 strcasecmp(av[2], "stop") == 0)
739 opcode = MFI_DCMD_PD_LOCATE_STOP;
741 warnx("locate: invalid state %s", av[2]);
745 fd = mfi_open(mfi_unit, O_RDWR);
752 error = mfi_lookup_drive(fd, av[1], &device_id);
759 mbox_store_device_id(&mbox[0], device_id);
762 if (mfi_dcmd_command(fd, opcode, NULL, 0, mbox, 4, NULL) < 0) {
764 warn("Failed to %s locate on drive %u",
765 opcode == MFI_DCMD_PD_LOCATE_START ? "start" : "stop",
774 MFI_COMMAND(top, locate, drive_locate);