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>
45 static const char* foreign_state = " (FOREIGN)";
50 format_stripe(char *buf, size_t buflen, uint8_t stripe)
53 humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
58 show_adapter(int ac, char **av __unused)
60 struct mfi_ctrl_info info;
65 warnx("show adapter: extra arguments");
69 fd = mfi_open(mfi_unit, O_RDONLY);
76 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
78 warn("Failed to get controller info");
82 printf("mfi%d Adapter:\n", mfi_unit);
83 printf(" Product Name: %.80s\n", info.product_name);
84 printf(" Serial Number: %.32s\n", info.serial_number);
85 if (info.package_version[0] != '\0')
86 printf(" Firmware: %s\n", info.package_version);
87 printf(" RAID Levels:");
89 printf(" (%#x)", info.raid_levels);
92 if (info.raid_levels & MFI_INFO_RAID_0) {
93 printf(" JBOD, RAID0");
96 if (info.raid_levels & MFI_INFO_RAID_1) {
97 printf("%s RAID1", comma ? "," : "");
100 if (info.raid_levels & MFI_INFO_RAID_5) {
101 printf("%s RAID5", comma ? "," : "");
104 if (info.raid_levels & MFI_INFO_RAID_1E) {
105 printf("%s RAID1E", comma ? "," : "");
108 if (info.raid_levels & MFI_INFO_RAID_6) {
109 printf("%s RAID6", comma ? "," : "");
112 if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) ==
113 (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) {
114 printf("%s RAID10", comma ? "," : "");
117 if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) ==
118 (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) {
119 printf("%s RAID50", comma ? "," : "");
123 printf(" Battery Backup: ");
124 if (info.hw_present & MFI_INFO_HW_BBU)
127 printf("not present\n");
128 if (info.hw_present & MFI_INFO_HW_NVRAM)
129 printf(" NVRAM: %uK\n", info.nvram_size);
130 printf(" Onboard Memory: %uM\n", info.memory_size);
131 format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.min);
132 printf(" Minimum Stripe: %s\n", stripe);
133 format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.max);
134 printf(" Maximum Stripe: %s\n", stripe);
140 MFI_COMMAND(show, adapter, show_adapter);
143 show_battery(int ac, char **av __unused)
145 struct mfi_bbu_capacity_info cap;
146 struct mfi_bbu_design_info design;
147 struct mfi_bbu_properties props;
148 struct mfi_bbu_status stat;
150 int comma, error, fd, show_capacity, show_props;
154 warnx("show battery: extra arguments");
158 fd = mfi_open(mfi_unit, O_RDONLY);
165 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
166 sizeof(cap), NULL, 0, &status) < 0) {
168 warn("Failed to get capacity info");
172 if (status == MFI_STAT_NO_HW_PRESENT) {
173 printf("mfi%d: No battery present\n", mfi_unit);
177 show_capacity = (status == MFI_STAT_OK);
179 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
180 sizeof(design), NULL, 0, NULL) < 0) {
182 warn("Failed to get design info");
187 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
188 NULL, 0, NULL) < 0) {
190 warn("Failed to get status");
195 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
197 warn("Failed to get properties");
201 show_props = (status == MFI_STAT_OK);
203 printf("mfi%d: Battery State:\n", mfi_unit);
204 printf(" Manufacture Date: %d/%d/%d\n", design.mfg_date >> 5 & 0x0f,
205 design.mfg_date & 0x1f, design.mfg_date >> 9 & 0xffff);
206 printf(" Serial Number: %d\n", design.serial_number);
207 printf(" Manufacturer: %s\n", design.mfg_name);
208 printf(" Model: %s\n", design.device_name);
209 printf(" Chemistry: %s\n", design.device_chemistry);
210 printf(" Design Capacity: %d mAh\n", design.design_capacity);
212 printf(" Full Charge Capacity: %d mAh\n",
213 cap.full_charge_capacity);
214 printf(" Current Capacity: %d mAh\n",
215 cap.remaining_capacity);
216 printf(" Charge Cycles: %d\n", cap.cycle_count);
217 printf(" Current Charge: %d%%\n", cap.relative_charge);
219 printf(" Design Voltage: %d mV\n", design.design_voltage);
220 printf(" Current Voltage: %d mV\n", stat.voltage);
221 printf(" Temperature: %d C\n", stat.temperature);
223 mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf));
224 printf(" Autolearn period: %s\n", buf);
225 if (props.auto_learn_mode != 0)
226 snprintf(buf, sizeof(buf), "never");
228 mfi_next_learn_time(props.next_learn_time, buf,
230 printf(" Next learn time: %s\n", buf);
231 printf(" Learn delay interval: %u hour%s\n",
232 props.learn_delay_interval,
233 props.learn_delay_interval != 1 ? "s" : "");
234 mfi_autolearn_mode(props.auto_learn_mode, buf, sizeof(buf));
235 printf(" Autolearn mode: %s\n", buf);
236 if (props.bbu_mode != 0)
237 printf(" BBU Mode: %d\n", props.bbu_mode);
241 if (stat.fw_status & MFI_BBU_STATE_PACK_MISSING) {
242 printf(" PACK_MISSING");
245 if (stat.fw_status & MFI_BBU_STATE_VOLTAGE_LOW) {
246 printf("%s VOLTAGE_LOW", comma ? "," : "");
249 if (stat.fw_status & MFI_BBU_STATE_TEMPERATURE_HIGH) {
250 printf("%s TEMPERATURE_HIGH", comma ? "," : "");
253 if (stat.fw_status & MFI_BBU_STATE_CHARGE_ACTIVE) {
254 printf("%s CHARGING", comma ? "," : "");
257 if (stat.fw_status & MFI_BBU_STATE_DISCHARGE_ACTIVE) {
258 printf("%s DISCHARGING", comma ? "," : "");
261 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_REQ) {
262 printf("%s LEARN_CYCLE_REQUESTED", comma ? "," : "");
265 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_ACTIVE) {
266 printf("%s LEARN_CYCLE_ACTIVE", comma ? "," : "");
269 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_FAIL) {
270 printf("%s LEARN_CYCLE_FAIL", comma ? "," : "");
273 if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_TIMEOUT) {
274 printf("%s LEARN_CYCLE_TIMEOUT", comma ? "," : "");
277 if (stat.fw_status & MFI_BBU_STATE_I2C_ERR_DETECT) {
278 printf("%s I2C_ERROR_DETECT", comma ? "," : "");
285 switch (stat.battery_type) {
286 case MFI_BBU_TYPE_BBU:
287 printf(" State of Health: %s\n",
288 stat.detail.bbu.is_SOH_good ? "good" : "bad");
296 MFI_COMMAND(show, battery, show_battery);
299 print_ld(struct mfi_ld_info *info, int state_len)
301 struct mfi_ld_params *params = &info->ld_config.params;
303 char size[6], stripe[5];
305 humanize_number(size, sizeof(size), info->size * 512,
306 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
307 format_stripe(stripe, sizeof(stripe),
308 info->ld_config.params.stripe_size);
309 level = mfi_raid_level(params->primary_raid_level,
310 params->secondary_raid_level);
312 printf("(%6s) %-8s %6s %-*s", size, level, stripe, state_len,
313 mfi_ldstate(params->state));
315 printf("(%s) %s %s %s", size, level, stripe,
316 mfi_ldstate(params->state));
320 print_pd(struct mfi_pd_info *info, int state_len)
325 humanize_number(buf, 6, info->raw_size * 512, "",
326 HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
327 printf("(%6s) ", buf);
328 if (info->state.ddf.v.pd_type.is_foreign) {
329 sprintf(buf, "%s%s", mfi_pdstate(info->fw_state), foreign_state);
332 s = mfi_pdstate(info->fw_state);
334 printf("%-*s", state_len, s);
337 s = mfi_pd_inq_string(info);
343 show_config(int ac, char **av __unused)
345 struct mfi_config_data *config;
346 struct mfi_array *ar;
347 struct mfi_ld_config *ld;
348 struct mfi_spare *sp;
349 struct mfi_ld_info linfo;
350 struct mfi_pd_info pinfo;
356 warnx("show config: extra arguments");
360 fd = mfi_open(mfi_unit, O_RDONLY);
367 /* Get the config from the controller. */
368 if (mfi_config_read(fd, &config) < 0) {
370 warn("Failed to get config");
375 /* Dump out the configuration. */
376 printf("mfi%d Configuration: %d arrays, %d volumes, %d spares\n",
377 mfi_unit, config->array_count, config->log_drv_count,
378 config->spares_count);
379 p = (char *)config->array;
381 for (i = 0; i < config->array_count; i++) {
382 ar = (struct mfi_array *)p;
383 printf(" array %u of %u drives:\n", ar->array_ref,
385 for (j = 0; j < ar->num_drives; j++) {
386 device_id = ar->pd[j].ref.v.device_id;
387 printf(" drive %s ", mfi_drive_name(NULL,
389 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
390 if (device_id != 0xffff) {
391 if (mfi_pd_get_info(fd, device_id, &pinfo,
394 mfi_pdstate(ar->pd[j].fw_state));
396 print_pd(&pinfo, -1);
400 p += config->array_size;
403 for (i = 0; i < config->log_drv_count; i++) {
404 ld = (struct mfi_ld_config *)p;
405 printf(" volume %s ",
406 mfi_volume_name(fd, ld->properties.ld.v.target_id));
407 if (mfi_ld_get_info(fd, ld->properties.ld.v.target_id, &linfo,
410 mfi_raid_level(ld->params.primary_raid_level,
411 ld->params.secondary_raid_level),
412 mfi_ldstate(ld->params.state));
414 print_ld(&linfo, -1);
415 if (ld->properties.name[0] != '\0')
416 printf(" <%s>", ld->properties.name);
418 for (j = 0; j < ld->params.span_depth; j++)
419 printf(" array %u\n", ld->span[j].array_ref);
420 p += config->log_drv_size;
423 for (i = 0; i < config->spares_count; i++) {
424 sp = (struct mfi_spare *)p;
425 printf(" %s spare %s ",
426 sp->spare_type & MFI_SPARE_DEDICATED ? "dedicated" :
427 "global", mfi_drive_name(NULL, sp->ref.v.device_id,
428 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
429 if (mfi_pd_get_info(fd, sp->ref.v.device_id, &pinfo, NULL) < 0)
430 printf("%s", mfi_pdstate(MFI_PD_STATE_HOT_SPARE));
432 print_pd(&pinfo, -1);
433 if (sp->spare_type & MFI_SPARE_DEDICATED) {
435 for (j = 0; j < sp->array_count; j++)
436 printf(" array %u\n", sp->array_ref[j]);
439 p += config->spares_size;
446 MFI_COMMAND(show, config, show_config);
449 show_volumes(int ac, char **av __unused)
451 struct mfi_ld_list list;
452 struct mfi_ld_info info;
454 u_int i, len, state_len;
457 warnx("show volumes: extra arguments");
461 fd = mfi_open(mfi_unit, O_RDONLY);
468 /* Get the logical drive list from the controller. */
469 if (mfi_ld_get_list(fd, &list, NULL) < 0) {
471 warn("Failed to get volume list");
476 /* List the volumes. */
477 printf("mfi%d Volumes:\n", mfi_unit);
478 state_len = strlen("State");
479 for (i = 0; i < list.ld_count; i++) {
480 len = strlen(mfi_ldstate(list.ld_list[i].state));
484 printf(" Id Size Level Stripe ");
485 len = state_len - strlen("State");
486 for (i = 0; i < (len + 1) / 2; i++)
489 for (i = 0; i < len / 2; i++)
491 printf(" Cache Name\n");
492 for (i = 0; i < list.ld_count; i++) {
493 if (mfi_ld_get_info(fd, list.ld_list[i].ld.v.target_id, &info,
496 warn("Failed to get info for volume %d",
497 list.ld_list[i].ld.v.target_id);
502 mfi_volume_name(fd, list.ld_list[i].ld.v.target_id));
503 print_ld(&info, state_len);
504 switch (info.ld_config.properties.current_cache_policy &
505 (MR_LD_CACHE_ALLOW_WRITE_CACHE |
506 MR_LD_CACHE_ALLOW_READ_CACHE)) {
510 case MR_LD_CACHE_ALLOW_READ_CACHE:
513 case MR_LD_CACHE_ALLOW_WRITE_CACHE:
516 case MR_LD_CACHE_ALLOW_WRITE_CACHE |
517 MR_LD_CACHE_ALLOW_READ_CACHE:
521 if (info.ld_config.properties.name[0] != '\0')
522 printf(" <%s>", info.ld_config.properties.name);
529 MFI_COMMAND(show, volumes, show_volumes);
532 show_drives(int ac, char **av __unused)
534 struct mfi_pd_list *list;
535 struct mfi_pd_info info;
536 u_int i, len, state_len;
540 warnx("show drives: extra arguments");
544 fd = mfi_open(mfi_unit, O_RDONLY);
552 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
554 warn("Failed to get drive list");
558 /* Walk the list of drives to determine width of state column. */
560 for (i = 0; i < list->count; i++) {
561 if (list->addr[i].scsi_dev_type != 0)
564 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
567 warn("Failed to fetch info for drive %u",
568 list->addr[i].device_id);
571 len = strlen(mfi_pdstate(info.fw_state));
572 if (info.state.ddf.v.pd_type.is_foreign)
573 len += strlen(foreign_state);
578 /* List the drives. */
579 printf("mfi%d Physical Drives:\n", mfi_unit);
580 for (i = 0; i < list->count; i++) {
582 /* Skip non-hard disks. */
583 if (list->addr[i].scsi_dev_type != 0)
586 /* Fetch details for this drive. */
587 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
590 warn("Failed to fetch info for drive %u",
591 list->addr[i].device_id);
595 printf("%s ", mfi_drive_name(&info, list->addr[i].device_id,
596 MFI_DNAME_DEVICE_ID));
597 print_pd(&info, state_len);
598 printf(" %s", mfi_drive_name(&info, list->addr[i].device_id,
609 MFI_COMMAND(show, drives, show_drives);
612 show_firmware(int ac, char **av __unused)
614 struct mfi_ctrl_info info;
615 struct mfi_info_component header;
620 warnx("show firmware: extra arguments");
624 fd = mfi_open(mfi_unit, O_RDONLY);
631 if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
633 warn("Failed to get controller info");
638 if (info.package_version[0] != '\0')
639 printf("mfi%d Firmware Package Version: %s\n", mfi_unit,
640 info.package_version);
641 printf("mfi%d Firmware Images:\n", mfi_unit);
642 strcpy(header.name, "Name");
643 strcpy(header.version, "Version");
644 strcpy(header.build_date, "Date");
645 strcpy(header.build_time, "Time");
646 scan_firmware(&header);
647 if (info.image_component_count > 8)
648 info.image_component_count = 8;
649 for (i = 0; i < info.image_component_count; i++)
650 scan_firmware(&info.image_component[i]);
651 if (info.pending_image_component_count > 8)
652 info.pending_image_component_count = 8;
653 for (i = 0; i < info.pending_image_component_count; i++)
654 scan_firmware(&info.pending_image_component[i]);
655 display_firmware(&header, "Status");
656 for (i = 0; i < info.image_component_count; i++)
657 display_firmware(&info.image_component[i], "active");
658 for (i = 0; i < info.pending_image_component_count; i++)
659 display_firmware(&info.pending_image_component[i], "pending");
665 MFI_COMMAND(show, firmware, show_firmware);
668 show_progress(int ac, char **av __unused)
670 struct mfi_ld_list llist;
671 struct mfi_pd_list *plist;
672 struct mfi_ld_info linfo;
673 struct mfi_pd_info pinfo;
680 warnx("show progress: extra arguments");
684 fd = mfi_open(mfi_unit, O_RDONLY);
691 if (mfi_ld_get_list(fd, &llist, NULL) < 0) {
693 warn("Failed to get volume list");
697 if (mfi_pd_get_list(fd, &plist, NULL) < 0) {
699 warn("Failed to get drive list");
705 for (i = 0; i < llist.ld_count; i++) {
706 target_id = llist.ld_list[i].ld.v.target_id;
707 if (mfi_ld_get_info(fd, target_id, &linfo, NULL) < 0) {
709 warn("Failed to get info for volume %s",
710 mfi_volume_name(fd, target_id));
715 if (linfo.progress.active & MFI_LD_PROGRESS_CC) {
716 printf("volume %s ", mfi_volume_name(fd, target_id));
717 mfi_display_progress("Consistency Check",
721 if (linfo.progress.active & MFI_LD_PROGRESS_BGI) {
722 printf("volume %s ", mfi_volume_name(fd, target_id));
723 mfi_display_progress("Background Init",
724 &linfo.progress.bgi);
727 if (linfo.progress.active & MFI_LD_PROGRESS_FGI) {
728 printf("volume %s ", mfi_volume_name(fd, target_id));
729 mfi_display_progress("Foreground Init",
730 &linfo.progress.fgi);
733 if (linfo.progress.active & MFI_LD_PROGRESS_RECON) {
734 printf("volume %s ", mfi_volume_name(fd, target_id));
735 mfi_display_progress("Reconstruction",
736 &linfo.progress.recon);
741 for (i = 0; i < plist->count; i++) {
742 if (plist->addr[i].scsi_dev_type != 0)
745 device_id = plist->addr[i].device_id;
746 if (mfi_pd_get_info(fd, device_id, &pinfo, NULL) < 0) {
748 warn("Failed to fetch info for drive %u", device_id);
754 if (pinfo.prog_info.active & MFI_PD_PROGRESS_REBUILD) {
755 printf("drive %s ", mfi_drive_name(NULL, device_id,
756 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
757 mfi_display_progress("Rebuild", &pinfo.prog_info.rbld);
760 if (pinfo.prog_info.active & MFI_PD_PROGRESS_PATROL) {
761 printf("drive %s ", mfi_drive_name(NULL, device_id,
762 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
763 mfi_display_progress("Patrol Read",
764 &pinfo.prog_info.patrol);
767 if (pinfo.prog_info.active & MFI_PD_PROGRESS_CLEAR) {
768 printf("drive %s ", mfi_drive_name(NULL, device_id,
769 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
770 mfi_display_progress("Clear", &pinfo.prog_info.clear);
779 printf("No activity in progress for adapter mfi%d\n", mfi_unit);
783 MFI_COMMAND(show, progress, show_progress);
786 show_foreign(int ac, char **av)
788 return(display_format(ac, av, 0/*normal display*/, MFI_DCMD_CFG_FOREIGN_DISPLAY));
790 MFI_COMMAND(show, foreign, show_foreign);