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 static const char* foreign_state = " (FOREIGN)";
48 format_stripe(char *buf, size_t buflen, uint8_t stripe)
51 humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
56 show_adapter(int ac, char **av __unused)
58 struct mfi_ctrl_info info;
63 warnx("show adapter: extra arguments");
67 fd = mfi_open(mfi_unit, O_RDONLY);
74 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
76 warn("Failed to get controller info");
80 printf("mfi%d Adapter:\n", mfi_unit);
81 printf(" Product Name: %.80s\n", info.product_name);
82 printf(" Serial Number: %.32s\n", info.serial_number);
83 if (info.package_version[0] != '\0')
84 printf(" Firmware: %s\n", info.package_version);
85 printf(" RAID Levels:");
87 printf(" (%#x)", info.raid_levels);
90 if (info.raid_levels & MFI_INFO_RAID_0) {
91 printf(" JBOD, RAID0");
94 if (info.raid_levels & MFI_INFO_RAID_1) {
95 printf("%s RAID1", comma ? "," : "");
98 if (info.raid_levels & MFI_INFO_RAID_5) {
99 printf("%s RAID5", comma ? "," : "");
102 if (info.raid_levels & MFI_INFO_RAID_1E) {
103 printf("%s RAID1E", comma ? "," : "");
106 if (info.raid_levels & MFI_INFO_RAID_6) {
107 printf("%s RAID6", comma ? "," : "");
110 if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) ==
111 (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) {
112 printf("%s RAID10", comma ? "," : "");
115 if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) ==
116 (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) {
117 printf("%s RAID50", comma ? "," : "");
121 printf(" Battery Backup: ");
122 if (info.hw_present & MFI_INFO_HW_BBU)
125 printf("not present\n");
126 if (info.hw_present & MFI_INFO_HW_NVRAM)
127 printf(" NVRAM: %uK\n", info.nvram_size);
128 printf(" Onboard Memory: %uM\n", info.memory_size);
129 format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.min);
130 printf(" Minimum Stripe: %s\n", stripe);
131 format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.max);
132 printf(" Maximum Stripe: %s\n", stripe);
138 MFI_COMMAND(show, adapter, show_adapter);
141 show_battery(int ac, char **av __unused)
143 struct mfi_bbu_capacity_info cap;
144 struct mfi_bbu_design_info design;
145 struct mfi_bbu_properties props;
146 struct mfi_bbu_status stat;
148 int comma, error, fd, show_capacity, show_props;
152 warnx("show battery: extra arguments");
156 fd = mfi_open(mfi_unit, O_RDONLY);
163 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
164 sizeof(cap), NULL, 0, &status) < 0) {
166 warn("Failed to get capacity info");
170 if (status == MFI_STAT_NO_HW_PRESENT) {
171 printf("mfi%d: No battery present\n", mfi_unit);
175 show_capacity = (status == MFI_STAT_OK);
177 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
178 sizeof(design), NULL, 0, NULL) < 0) {
180 warn("Failed to get design info");
185 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
186 NULL, 0, NULL) < 0) {
188 warn("Failed to get status");
193 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
195 warn("Failed to get properties");
199 show_props = (status == MFI_STAT_OK);
201 printf("mfi%d: Battery State:\n", mfi_unit);
202 printf(" Manufacture Date: %d/%d/%d\n", design.mfg_date >> 5 & 0x0f,
203 design.mfg_date & 0x1f, design.mfg_date >> 9 & 0xffff);
204 printf(" Serial Number: %d\n", design.serial_number);
205 printf(" Manufacturer: %s\n", design.mfg_name);
206 printf(" Model: %s\n", design.device_name);
207 printf(" Chemistry: %s\n", design.device_chemistry);
208 printf(" Design Capacity: %d mAh\n", design.design_capacity);
210 printf(" Full Charge Capacity: %d mAh\n",
211 cap.full_charge_capacity);
212 printf(" Current Capacity: %d mAh\n",
213 cap.remaining_capacity);
214 printf(" Charge Cycles: %d\n", cap.cycle_count);
215 printf(" Current Charge: %d%%\n", cap.relative_charge);
217 printf(" Design Voltage: %d mV\n", design.design_voltage);
218 printf(" Current Voltage: %d mV\n", stat.voltage);
219 printf(" Temperature: %d C\n", stat.temperature);
221 mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf));
222 printf(" Autolearn period: %s\n", buf);
223 if (props.auto_learn_mode != 0)
224 snprintf(buf, sizeof(buf), "never");
226 mfi_next_learn_time(props.next_learn_time, buf,
228 printf(" Next learn time: %s\n", buf);
229 printf(" Learn delay interval: %u hour%s\n",
230 props.learn_delay_interval,
231 props.learn_delay_interval != 1 ? "s" : "");
232 mfi_autolearn_mode(props.auto_learn_mode, buf, sizeof(buf));
233 printf(" Autolearn mode: %s\n", buf);
234 if (props.bbu_mode != 0)
235 printf(" BBU Mode: %d\n", props.bbu_mode);
239 if (stat.fw_status & MFI_BBU_STATE_PACK_MISSING) {
240 printf(" PACK_MISSING");
243 if (stat.fw_status & MFI_BBU_STATE_VOLTAGE_LOW) {
244 printf("%s VOLTAGE_LOW", comma ? "," : "");
247 if (stat.fw_status & MFI_BBU_STATE_TEMPERATURE_HIGH) {
248 printf("%s TEMPERATURE_HIGH", comma ? "," : "");
251 if (stat.fw_status & MFI_BBU_STATE_CHARGE_ACTIVE) {
252 printf("%s CHARGING", comma ? "," : "");
255 if (stat.fw_status & MFI_BBU_STATE_DISCHARGE_ACTIVE) {
256 printf("%s DISCHARGING", comma ? "," : "");
259 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_REQ) {
260 printf("%s LEARN_CYCLE_REQUESTED", comma ? "," : "");
263 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_ACTIVE) {
264 printf("%s LEARN_CYCLE_ACTIVE", comma ? "," : "");
267 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_FAIL) {
268 printf("%s LEARN_CYCLE_FAIL", comma ? "," : "");
271 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_TIMEOUT) {
272 printf("%s LEARN_CYCLE_TIMEOUT", comma ? "," : "");
275 if (stat.fw_status & MFI_BBU_STATE_I2C_ERR_DETECT) {
276 printf("%s I2C_ERROR_DETECT", comma ? "," : "");
283 switch (stat.battery_type) {
284 case MFI_BBU_TYPE_BBU:
285 printf(" State of Health: %s\n",
286 stat.detail.bbu.is_SOH_good ? "good" : "bad");
294 MFI_COMMAND(show, battery, show_battery);
297 print_ld(struct mfi_ld_info *info, int state_len)
299 struct mfi_ld_params *params = &info->ld_config.params;
301 char size[6], stripe[5];
303 humanize_number(size, sizeof(size), info->size * 512,
304 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
305 format_stripe(stripe, sizeof(stripe),
306 info->ld_config.params.stripe_size);
307 level = mfi_raid_level(params->primary_raid_level,
308 params->secondary_raid_level);
310 printf("(%6s) %-8s %6s %-*s", size, level, stripe, state_len,
311 mfi_ldstate(params->state));
313 printf("(%s) %s %s %s", size, level, stripe,
314 mfi_ldstate(params->state));
318 print_pd(struct mfi_pd_info *info, int state_len)
323 humanize_number(buf, sizeof(buf), info->raw_size * 512, "",
324 HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
325 printf("(%6s) ", buf);
326 if (info->state.ddf.v.pd_type.is_foreign) {
327 sprintf(buf, "%s%s", mfi_pdstate(info->fw_state), foreign_state);
330 s = mfi_pdstate(info->fw_state);
332 printf("%-*s", state_len, s);
335 s = mfi_pd_inq_string(info);
341 show_config(int ac, char **av __unused)
343 struct mfi_config_data *config;
344 struct mfi_array *ar;
345 struct mfi_ld_config *ld;
346 struct mfi_spare *sp;
347 struct mfi_ld_info linfo;
348 struct mfi_pd_info pinfo;
354 warnx("show config: extra arguments");
358 fd = mfi_open(mfi_unit, O_RDONLY);
365 /* Get the config from the controller. */
366 if (mfi_config_read(fd, &config) < 0) {
368 warn("Failed to get config");
373 /* Dump out the configuration. */
374 printf("mfi%d Configuration: %d arrays, %d volumes, %d spares\n",
375 mfi_unit, config->array_count, config->log_drv_count,
376 config->spares_count);
377 p = (char *)config->array;
379 for (i = 0; i < config->array_count; i++) {
380 ar = (struct mfi_array *)p;
381 printf(" array %u of %u drives:\n", ar->array_ref,
383 for (j = 0; j < ar->num_drives; j++) {
384 device_id = ar->pd[j].ref.v.device_id;
385 printf(" drive %s ", mfi_drive_name(NULL,
387 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
388 if (device_id != 0xffff) {
389 if (mfi_pd_get_info(fd, device_id, &pinfo,
392 mfi_pdstate(ar->pd[j].fw_state));
394 print_pd(&pinfo, -1);
398 p += config->array_size;
401 for (i = 0; i < config->log_drv_count; i++) {
402 ld = (struct mfi_ld_config *)p;
403 printf(" volume %s ",
404 mfi_volume_name(fd, ld->properties.ld.v.target_id));
405 if (mfi_ld_get_info(fd, ld->properties.ld.v.target_id, &linfo,
408 mfi_raid_level(ld->params.primary_raid_level,
409 ld->params.secondary_raid_level),
410 mfi_ldstate(ld->params.state));
412 print_ld(&linfo, -1);
413 if (ld->properties.name[0] != '\0')
414 printf(" <%s>", ld->properties.name);
416 for (j = 0; j < ld->params.span_depth; j++)
417 printf(" array %u\n", ld->span[j].array_ref);
418 p += config->log_drv_size;
421 for (i = 0; i < config->spares_count; i++) {
422 sp = (struct mfi_spare *)p;
423 printf(" %s spare %s ",
424 sp->spare_type & MFI_SPARE_DEDICATED ? "dedicated" :
425 "global", mfi_drive_name(NULL, sp->ref.v.device_id,
426 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
427 if (mfi_pd_get_info(fd, sp->ref.v.device_id, &pinfo, NULL) < 0)
428 printf("%s", mfi_pdstate(MFI_PD_STATE_HOT_SPARE));
430 print_pd(&pinfo, -1);
431 if (sp->spare_type & MFI_SPARE_DEDICATED) {
433 for (j = 0; j < sp->array_count; j++)
434 printf(" array %u\n", sp->array_ref[j]);
437 p += config->spares_size;
444 MFI_COMMAND(show, config, show_config);
447 show_volumes(int ac, char **av __unused)
449 struct mfi_ld_list list;
450 struct mfi_ld_info info;
452 u_int i, len, state_len;
455 warnx("show volumes: extra arguments");
459 fd = mfi_open(mfi_unit, O_RDONLY);
466 /* Get the logical drive list from the controller. */
467 if (mfi_ld_get_list(fd, &list, NULL) < 0) {
469 warn("Failed to get volume list");
474 /* List the volumes. */
475 printf("mfi%d Volumes:\n", mfi_unit);
476 state_len = strlen("State");
477 for (i = 0; i < list.ld_count; i++) {
478 len = strlen(mfi_ldstate(list.ld_list[i].state));
482 printf(" Id Size Level Stripe ");
483 len = state_len - strlen("State");
484 for (i = 0; i < (len + 1) / 2; i++)
487 for (i = 0; i < len / 2; i++)
489 printf(" Cache Name\n");
490 for (i = 0; i < list.ld_count; i++) {
491 if (mfi_ld_get_info(fd, list.ld_list[i].ld.v.target_id, &info,
494 warn("Failed to get info for volume %d",
495 list.ld_list[i].ld.v.target_id);
500 mfi_volume_name(fd, list.ld_list[i].ld.v.target_id));
501 print_ld(&info, state_len);
502 switch (info.ld_config.properties.current_cache_policy &
503 (MR_LD_CACHE_ALLOW_WRITE_CACHE |
504 MR_LD_CACHE_ALLOW_READ_CACHE)) {
508 case MR_LD_CACHE_ALLOW_READ_CACHE:
511 case MR_LD_CACHE_ALLOW_WRITE_CACHE:
514 case MR_LD_CACHE_ALLOW_WRITE_CACHE |
515 MR_LD_CACHE_ALLOW_READ_CACHE:
519 if (info.ld_config.properties.name[0] != '\0')
520 printf(" <%s>", info.ld_config.properties.name);
527 MFI_COMMAND(show, volumes, show_volumes);
530 show_drives(int ac, char **av __unused)
532 struct mfi_pd_list *list;
533 struct mfi_pd_info info;
534 u_int i, len, state_len;
538 warnx("show drives: extra arguments");
542 fd = mfi_open(mfi_unit, O_RDONLY);
550 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
552 warn("Failed to get drive list");
556 /* Walk the list of drives to determine width of state column. */
558 for (i = 0; i < list->count; i++) {
559 if (list->addr[i].scsi_dev_type != 0)
562 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
565 warn("Failed to fetch info for drive %u",
566 list->addr[i].device_id);
569 len = strlen(mfi_pdstate(info.fw_state));
570 if (info.state.ddf.v.pd_type.is_foreign)
571 len += strlen(foreign_state);
576 /* List the drives. */
577 printf("mfi%d Physical Drives:\n", mfi_unit);
578 for (i = 0; i < list->count; i++) {
580 /* Skip non-hard disks. */
581 if (list->addr[i].scsi_dev_type != 0)
584 /* Fetch details for this drive. */
585 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
588 warn("Failed to fetch info for drive %u",
589 list->addr[i].device_id);
593 printf("%s ", mfi_drive_name(&info, list->addr[i].device_id,
594 MFI_DNAME_DEVICE_ID));
595 print_pd(&info, state_len);
596 printf(" %s", mfi_drive_name(&info, list->addr[i].device_id,
607 MFI_COMMAND(show, drives, show_drives);
610 show_firmware(int ac, char **av __unused)
612 struct mfi_ctrl_info info;
613 struct mfi_info_component header;
618 warnx("show firmware: extra arguments");
622 fd = mfi_open(mfi_unit, O_RDONLY);
629 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
631 warn("Failed to get controller info");
636 if (info.package_version[0] != '\0')
637 printf("mfi%d Firmware Package Version: %s\n", mfi_unit,
638 info.package_version);
639 printf("mfi%d Firmware Images:\n", mfi_unit);
640 strcpy(header.name, "Name");
641 strcpy(header.version, "Version");
642 strcpy(header.build_date, "Date");
643 strcpy(header.build_time, "Time");
644 scan_firmware(&header);
645 if (info.image_component_count > 8)
646 info.image_component_count = 8;
647 for (i = 0; i < info.image_component_count; i++)
648 scan_firmware(&info.image_component[i]);
649 if (info.pending_image_component_count > 8)
650 info.pending_image_component_count = 8;
651 for (i = 0; i < info.pending_image_component_count; i++)
652 scan_firmware(&info.pending_image_component[i]);
653 display_firmware(&header, "Status");
654 for (i = 0; i < info.image_component_count; i++)
655 display_firmware(&info.image_component[i], "active");
656 for (i = 0; i < info.pending_image_component_count; i++)
657 display_firmware(&info.pending_image_component[i], "pending");
663 MFI_COMMAND(show, firmware, show_firmware);
666 show_progress(int ac, char **av __unused)
668 struct mfi_ld_list llist;
669 struct mfi_pd_list *plist;
670 struct mfi_ld_info linfo;
671 struct mfi_pd_info pinfo;
678 warnx("show progress: extra arguments");
682 fd = mfi_open(mfi_unit, O_RDONLY);
689 if (mfi_ld_get_list(fd, &llist, NULL) < 0) {
691 warn("Failed to get volume list");
695 if (mfi_pd_get_list(fd, &plist, NULL) < 0) {
697 warn("Failed to get drive list");
703 for (i = 0; i < llist.ld_count; i++) {
704 target_id = llist.ld_list[i].ld.v.target_id;
705 if (mfi_ld_get_info(fd, target_id, &linfo, NULL) < 0) {
707 warn("Failed to get info for volume %s",
708 mfi_volume_name(fd, target_id));
713 if (linfo.progress.active & MFI_LD_PROGRESS_CC) {
714 printf("volume %s ", mfi_volume_name(fd, target_id));
715 mfi_display_progress("Consistency Check",
719 if (linfo.progress.active & MFI_LD_PROGRESS_BGI) {
720 printf("volume %s ", mfi_volume_name(fd, target_id));
721 mfi_display_progress("Background Init",
722 &linfo.progress.bgi);
725 if (linfo.progress.active & MFI_LD_PROGRESS_FGI) {
726 printf("volume %s ", mfi_volume_name(fd, target_id));
727 mfi_display_progress("Foreground Init",
728 &linfo.progress.fgi);
731 if (linfo.progress.active & MFI_LD_PROGRESS_RECON) {
732 printf("volume %s ", mfi_volume_name(fd, target_id));
733 mfi_display_progress("Reconstruction",
734 &linfo.progress.recon);
739 for (i = 0; i < plist->count; i++) {
740 if (plist->addr[i].scsi_dev_type != 0)
743 device_id = plist->addr[i].device_id;
744 if (mfi_pd_get_info(fd, device_id, &pinfo, NULL) < 0) {
746 warn("Failed to fetch info for drive %u", device_id);
752 if (pinfo.prog_info.active & MFI_PD_PROGRESS_REBUILD) {
753 printf("drive %s ", mfi_drive_name(NULL, device_id,
754 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
755 mfi_display_progress("Rebuild", &pinfo.prog_info.rbld);
758 if (pinfo.prog_info.active & MFI_PD_PROGRESS_PATROL) {
759 printf("drive %s ", mfi_drive_name(NULL, device_id,
760 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
761 mfi_display_progress("Patrol Read",
762 &pinfo.prog_info.patrol);
765 if (pinfo.prog_info.active & MFI_PD_PROGRESS_CLEAR) {
766 printf("drive %s ", mfi_drive_name(NULL, device_id,
767 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
768 mfi_display_progress("Clear", &pinfo.prog_info.clear);
777 printf("No activity in progress for adapter mfi%d\n", mfi_unit);
781 MFI_COMMAND(show, progress, show_progress);
784 show_foreign(int ac, char **av)
786 return(display_format(ac, av, 0/*normal display*/, MFI_DCMD_CFG_FOREIGN_DISPLAY));
788 MFI_COMMAND(show, foreign, show_foreign);