mfi_show.c revision 260324
160786Sps/*-
263128Sps * Copyright (c) 2008, 2009 Yahoo!, Inc.
360786Sps * All rights reserved.
463128Sps *
560786Sps * Redistribution and use in source and binary forms, with or without
660786Sps * modification, are permitted provided that the following conditions
760786Sps * are met:
860786Sps * 1. Redistributions of source code must retain the above copyright
960786Sps *    notice, this list of conditions and the following disclaimer.
1060786Sps * 2. Redistributions in binary form must reproduce the above copyright
1160786Sps *    notice, this list of conditions and the following disclaimer in the
1260786Sps *    documentation and/or other materials provided with the distribution.
1360786Sps * 3. The names of the authors may not be used to endorse or promote
1460786Sps *    products derived from this software without specific prior written
1560786Sps *    permission.
1660786Sps *
1760786Sps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1860786Sps * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1960786Sps * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2060786Sps * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2160786Sps * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2260786Sps * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2360786Sps * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2460786Sps * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2560786Sps * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2660786Sps * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2760786Sps * SUCH DAMAGE.
2860786Sps *
2960786Sps * $FreeBSD: stable/10/usr.sbin/mfiutil/mfi_show.c 260324 2014-01-05 17:29:53Z sbruno $
3060786Sps */
3160786Sps
3260786Sps#include <sys/types.h>
3360786Sps#include <sys/errno.h>
3460786Sps#include <err.h>
3560786Sps#include <fcntl.h>
3660786Sps#include <libutil.h>
3760786Sps#include <stdio.h>
3860786Sps#include <stdlib.h>
3960786Sps#include <string.h>
4060786Sps#include <unistd.h>
4160786Sps#include "mfiutil.h"
4260786Sps
4360786Spsstatic const char* foreign_state = " (FOREIGN)";
4460786Sps
4560786SpsMFI_TABLE(top, show);
4660786Sps
4760786Spsvoid
4860786Spsformat_stripe(char *buf, size_t buflen, uint8_t stripe)
4960786Sps{
5060786Sps
5160786Sps	humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
5260786Sps	    HN_B | HN_NOSPACE);
5360786Sps}
5460786Sps
5560786Spsstatic int
5660786Spsshow_adapter(int ac, char **av __unused)
5760786Sps{
5860786Sps	struct mfi_ctrl_info info;
5960786Sps	char stripe[5];
6060786Sps	int error, fd, comma;
6160786Sps
6260786Sps	if (ac != 1) {
6360786Sps		warnx("show adapter: extra arguments");
6460786Sps		return (EINVAL);
6560786Sps	}
6660786Sps
6760786Sps	fd = mfi_open(mfi_unit, O_RDONLY);
6860786Sps	if (fd < 0) {
6960786Sps		error = errno;
7060786Sps		warn("mfi_open");
7160786Sps		return (error);
7260786Sps	}
7360786Sps
7460786Sps	if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
7560786Sps		error = errno;
7660786Sps		warn("Failed to get controller info");
7760786Sps		close(fd);
7860786Sps		return (error);
7960786Sps	}
8060786Sps	printf("mfi%d Adapter:\n", mfi_unit);
8160786Sps	printf("    Product Name: %.80s\n", info.product_name);
8260786Sps	printf("   Serial Number: %.32s\n", info.serial_number);
8360786Sps	if (info.package_version[0] != '\0')
8460786Sps		printf("        Firmware: %s\n", info.package_version);
8560786Sps	printf("     RAID Levels:");
8660786Sps#ifdef DEBUG
8760786Sps	printf(" (%#x)", info.raid_levels);
8860786Sps#endif
8960786Sps	comma = 0;
9060786Sps	if (info.raid_levels & MFI_INFO_RAID_0) {
9160786Sps		printf(" JBOD, RAID0");
9260786Sps		comma = 1;
9360786Sps	}
9460786Sps	if (info.raid_levels & MFI_INFO_RAID_1) {
9560786Sps		printf("%s RAID1", comma ? "," : "");
9660786Sps		comma = 1;
9760786Sps	}
9860786Sps	if (info.raid_levels & MFI_INFO_RAID_5) {
9960786Sps		printf("%s RAID5", comma ? "," : "");
10060786Sps		comma = 1;
10160786Sps	}
10260786Sps	if (info.raid_levels & MFI_INFO_RAID_1E) {
10360786Sps		printf("%s RAID1E", comma ? "," : "");
10460786Sps		comma = 1;
10560786Sps	}
10660786Sps	if (info.raid_levels & MFI_INFO_RAID_6) {
10760786Sps		printf("%s RAID6", comma ? "," : "");
10860786Sps		comma = 1;
10963128Sps	}
11060786Sps	if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) ==
11160786Sps	    (MFI_INFO_RAID_0 | MFI_INFO_RAID_1)) {
11260786Sps		printf("%s RAID10", comma ? "," : "");
11360786Sps		comma = 1;
11460786Sps	}
11560786Sps	if ((info.raid_levels & (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) ==
11660786Sps	    (MFI_INFO_RAID_0 | MFI_INFO_RAID_5)) {
11760786Sps		printf("%s RAID50", comma ? "," : "");
11860786Sps		comma = 1;
11960786Sps	}
12060786Sps	printf("\n");
12160786Sps	printf("  Battery Backup: ");
12260786Sps	if (info.hw_present & MFI_INFO_HW_BBU)
12360786Sps		printf("present\n");
12460786Sps	else
12560786Sps		printf("not present\n");
12660786Sps	if (info.hw_present & MFI_INFO_HW_NVRAM)
12760786Sps		printf("           NVRAM: %uK\n", info.nvram_size);
12860786Sps	printf("  Onboard Memory: %uM\n", info.memory_size);
12960786Sps	format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.min);
13060786Sps	printf("  Minimum Stripe: %s\n", stripe);
13160786Sps	format_stripe(stripe, sizeof(stripe), info.stripe_sz_ops.max);
13260786Sps	printf("  Maximum Stripe: %s\n", stripe);
13360786Sps
13460786Sps	close(fd);
13560786Sps
13660786Sps	return (0);
13760786Sps}
13860786SpsMFI_COMMAND(show, adapter, show_adapter);
13960786Sps
14060786Spsstatic int
14160786Spsshow_battery(int ac, char **av __unused)
14260786Sps{
14360786Sps	struct mfi_bbu_capacity_info cap;
14460786Sps	struct mfi_bbu_design_info design;
14560786Sps	struct mfi_bbu_properties props;
14660786Sps	struct mfi_bbu_status stat;
14760786Sps	uint8_t status;
14860786Sps	int comma, error, fd, show_capacity, show_props;
14960786Sps	char buf[32];
15060786Sps
15160786Sps	if (ac != 1) {
15260786Sps		warnx("show battery: extra arguments");
15360786Sps		return (EINVAL);
15460786Sps	}
15560786Sps
15660786Sps	fd = mfi_open(mfi_unit, O_RDONLY);
15760786Sps	if (fd < 0) {
15860786Sps		error = errno;
15960786Sps		warn("mfi_open");
16060786Sps		return (error);
16160786Sps	}
16260786Sps
16360786Sps	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
16460786Sps	    sizeof(cap), NULL, 0, &status) < 0) {
16560786Sps		error = errno;
16660786Sps		warn("Failed to get capacity info");
16760786Sps		close(fd);
16860786Sps		return (error);
16960786Sps	}
17060786Sps	if (status == MFI_STAT_NO_HW_PRESENT) {
17160786Sps		printf("mfi%d: No battery present\n", mfi_unit);
17260786Sps		close(fd);
17360786Sps		return (0);
17460786Sps	}
17560786Sps	show_capacity = (status == MFI_STAT_OK);
17660786Sps
17760786Sps	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
17860786Sps	    sizeof(design), NULL, 0, NULL) < 0) {
17960786Sps		error = errno;
18060786Sps		warn("Failed to get design info");
18160786Sps		close(fd);
18260786Sps		return (error);
18360786Sps	}
18460786Sps
18560786Sps	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
18660786Sps	    NULL, 0, NULL) < 0) {
18760786Sps		error = errno;
18860786Sps		warn("Failed to get status");
18960786Sps		close(fd);
19060786Sps		return (error);
19160786Sps	}
19260786Sps
19360786Sps	if (mfi_bbu_get_props(fd, &props, &status) < 0) {
19460786Sps		error = errno;
19560786Sps		warn("Failed to get properties");
19660786Sps		close(fd);
19760786Sps		return (error);
19860786Sps	}
19960786Sps	show_props = (status == MFI_STAT_OK);
20060786Sps
20160786Sps	printf("mfi%d: Battery State:\n", mfi_unit);
20260786Sps	printf("     Manufacture Date: %d/%d/%d\n", design.mfg_date >> 5 & 0x0f,
20360786Sps	    design.mfg_date & 0x1f, design.mfg_date >> 9 & 0xffff);
20460786Sps	printf("        Serial Number: %d\n", design.serial_number);
20560786Sps	printf("         Manufacturer: %s\n", design.mfg_name);
20660786Sps	printf("                Model: %s\n", design.device_name);
20760786Sps	printf("            Chemistry: %s\n", design.device_chemistry);
20860786Sps	printf("      Design Capacity: %d mAh\n", design.design_capacity);
20960786Sps	if (show_capacity) {
21060786Sps		printf(" Full Charge Capacity: %d mAh\n",
21160786Sps		    cap.full_charge_capacity);
21260786Sps		printf("     Current Capacity: %d mAh\n",
21360786Sps		    cap.remaining_capacity);
21460786Sps		printf("        Charge Cycles: %d\n", cap.cycle_count);
21560786Sps		printf("       Current Charge: %d%%\n", cap.relative_charge);
21660786Sps	}
21760786Sps	printf("       Design Voltage: %d mV\n", design.design_voltage);
21860786Sps	printf("      Current Voltage: %d mV\n", stat.voltage);
21960786Sps	printf("          Temperature: %d C\n", stat.temperature);
22060786Sps	if (show_props) {
22160786Sps		mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf));
22260786Sps		printf("     Autolearn period: %s\n", buf);
22360786Sps		if (props.auto_learn_mode != 0)
22460786Sps			snprintf(buf, sizeof(buf), "never");
22560786Sps		else
22660786Sps			mfi_next_learn_time(props.next_learn_time, buf,
227			    sizeof(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);
236	}
237	printf("               Status:");
238	comma = 0;
239	if (stat.fw_status & MFI_BBU_STATE_PACK_MISSING) {
240		printf(" PACK_MISSING");
241		comma = 1;
242	}
243	if (stat.fw_status & MFI_BBU_STATE_VOLTAGE_LOW) {
244		printf("%s VOLTAGE_LOW", comma ? "," : "");
245		comma = 1;
246	}
247	if (stat.fw_status & MFI_BBU_STATE_TEMPERATURE_HIGH) {
248		printf("%s TEMPERATURE_HIGH", comma ? "," : "");
249		comma = 1;
250	}
251	if (stat.fw_status & MFI_BBU_STATE_CHARGE_ACTIVE) {
252		printf("%s CHARGING", comma ? "," : "");
253		comma = 1;
254	}
255	if (stat.fw_status & MFI_BBU_STATE_DISCHARGE_ACTIVE) {
256		printf("%s DISCHARGING", comma ? "," : "");
257		comma = 1;
258	}
259	if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_REQ) {
260		printf("%s LEARN_CYCLE_REQUESTED", comma ? "," : "");
261		comma = 1;
262	}
263	if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_ACTIVE) {
264		printf("%s LEARN_CYCLE_ACTIVE", comma ? "," : "");
265		comma = 1;
266	}
267	if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_FAIL) {
268		printf("%s LEARN_CYCLE_FAIL", comma ? "," : "");
269		comma = 1;
270	}
271	if (stat.fw_status & MFI_BBU_STATE_LEARN_CYC_TIMEOUT) {
272		printf("%s LEARN_CYCLE_TIMEOUT", comma ? "," : "");
273		comma = 1;
274	}
275	if (stat.fw_status & MFI_BBU_STATE_I2C_ERR_DETECT) {
276		printf("%s I2C_ERROR_DETECT", comma ? "," : "");
277		comma = 1;
278	}
279
280	if (!comma)
281		printf(" normal");
282	printf("\n");
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");
287		break;
288	}
289
290	close(fd);
291
292	return (0);
293}
294MFI_COMMAND(show, battery, show_battery);
295
296void
297print_ld(struct mfi_ld_info *info, int state_len)
298{
299	struct mfi_ld_params *params = &info->ld_config.params;
300	const char *level;
301	char size[6], stripe[5];
302
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);
309	if (state_len > 0)
310		printf("(%6s) %-8s %6s %-*s", size, level, stripe, state_len,
311		    mfi_ldstate(params->state));
312	else
313		printf("(%s) %s %s %s", size, level, stripe,
314		    mfi_ldstate(params->state));
315}
316
317void
318print_pd(struct mfi_pd_info *info, int state_len)
319{
320	const char *s;
321	char buf[256];
322
323	humanize_number(buf, 6, 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);
328		s = buf;
329	} else
330		s = mfi_pdstate(info->fw_state);
331	if (state_len > 0)
332		printf("%-*s", state_len, s);
333	else
334		printf("%s",s);
335	s = mfi_pd_inq_string(info);
336	if (s != NULL)
337		printf(" %s", s);
338}
339
340static int
341show_config(int ac, char **av __unused)
342{
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;
349	uint16_t device_id;
350	char *p;
351	int error, fd, i, j;
352
353	if (ac != 1) {
354		warnx("show config: extra arguments");
355		return (EINVAL);
356	}
357
358	fd = mfi_open(mfi_unit, O_RDONLY);
359	if (fd < 0) {
360		error = errno;
361		warn("mfi_open");
362		return (error);
363	}
364
365	/* Get the config from the controller. */
366	if (mfi_config_read(fd, &config) < 0) {
367		error = errno;
368		warn("Failed to get config");
369		close(fd);
370		return (error);
371	}
372
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;
378
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,
382		    ar->num_drives);
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,
386			    device_id,
387			    MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
388			if (device_id != 0xffff) {
389				if (mfi_pd_get_info(fd, device_id, &pinfo,
390				    NULL) < 0)
391					printf("%s",
392					    mfi_pdstate(ar->pd[j].fw_state));
393				else
394					print_pd(&pinfo, -1);
395			}
396			printf("\n");
397		}
398		p += config->array_size;
399	}
400
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,
406		    NULL) < 0) {
407			printf("%s %s",
408			    mfi_raid_level(ld->params.primary_raid_level,
409				ld->params.secondary_raid_level),
410			    mfi_ldstate(ld->params.state));
411		} else
412			print_ld(&linfo, -1);
413		if (ld->properties.name[0] != '\0')
414			printf(" <%s>", ld->properties.name);
415		printf(" spans:\n");
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;
419	}
420
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));
429		else
430			print_pd(&pinfo, -1);
431		if (sp->spare_type & MFI_SPARE_DEDICATED) {
432			printf(" backs:\n");
433			for (j = 0; j < sp->array_count; j++)
434				printf("        array %u\n", sp->array_ref[j]);
435		} else
436			printf("\n");
437		p += config->spares_size;
438	}
439	free(config);
440	close(fd);
441
442	return (0);
443}
444MFI_COMMAND(show, config, show_config);
445
446static int
447show_volumes(int ac, char **av __unused)
448{
449	struct mfi_ld_list list;
450	struct mfi_ld_info info;
451	int error, fd;
452	u_int i, len, state_len;
453
454	if (ac != 1) {
455		warnx("show volumes: extra arguments");
456		return (EINVAL);
457	}
458
459	fd = mfi_open(mfi_unit, O_RDONLY);
460	if (fd < 0) {
461		error = errno;
462		warn("mfi_open");
463		return (error);
464	}
465
466	/* Get the logical drive list from the controller. */
467	if (mfi_ld_get_list(fd, &list, NULL) < 0) {
468		error = errno;
469		warn("Failed to get volume list");
470		close(fd);
471		return (error);
472	}
473
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));
479		if (len > state_len)
480			state_len = len;
481	}
482	printf("  Id     Size    Level   Stripe ");
483	len = state_len - strlen("State");
484	for (i = 0; i < (len + 1) / 2; i++)
485		printf(" ");
486	printf("State");
487	for (i = 0; i < len / 2; i++)
488		printf(" ");
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,
492		    NULL) < 0) {
493			error = errno;
494			warn("Failed to get info for volume %d",
495			    list.ld_list[i].ld.v.target_id);
496			close(fd);
497			return (error);
498		}
499		printf("%6s ",
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)) {
505		case 0:
506			printf(" Disabled");
507			break;
508		case MR_LD_CACHE_ALLOW_READ_CACHE:
509			printf(" Reads   ");
510			break;
511		case MR_LD_CACHE_ALLOW_WRITE_CACHE:
512			printf(" Writes  ");
513			break;
514		case MR_LD_CACHE_ALLOW_WRITE_CACHE |
515		    MR_LD_CACHE_ALLOW_READ_CACHE:
516			printf(" Enabled ");
517			break;
518		}
519		if (info.ld_config.properties.name[0] != '\0')
520			printf(" <%s>", info.ld_config.properties.name);
521		printf("\n");
522	}
523	close(fd);
524
525	return (0);
526}
527MFI_COMMAND(show, volumes, show_volumes);
528
529static int
530show_drives(int ac, char **av __unused)
531{
532	struct mfi_pd_list *list;
533	struct mfi_pd_info info;
534	u_int i, len, state_len;
535	int error, fd;
536
537	if (ac != 1) {
538		warnx("show drives: extra arguments");
539		return (EINVAL);
540	}
541
542	fd = mfi_open(mfi_unit, O_RDONLY);
543	if (fd < 0) {
544		error = errno;
545		warn("mfi_open");
546		return (error);
547	}
548
549	list = NULL;
550	if (mfi_pd_get_list(fd, &list, NULL) < 0) {
551		error = errno;
552		warn("Failed to get drive list");
553		goto error;
554	}
555
556	/* Walk the list of drives to determine width of state column. */
557	state_len = 0;
558	for (i = 0; i < list->count; i++) {
559		if (list->addr[i].scsi_dev_type != 0)
560			continue;
561
562		if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
563		    NULL) < 0) {
564			error = errno;
565			warn("Failed to fetch info for drive %u",
566			    list->addr[i].device_id);
567			goto error;
568		}
569		len = strlen(mfi_pdstate(info.fw_state));
570		if (info.state.ddf.v.pd_type.is_foreign)
571			len += strlen(foreign_state);
572		if (len > state_len)
573			state_len = len;
574	}
575
576	/* List the drives. */
577	printf("mfi%d Physical Drives:\n", mfi_unit);
578	for (i = 0; i < list->count; i++) {
579
580		/* Skip non-hard disks. */
581		if (list->addr[i].scsi_dev_type != 0)
582			continue;
583
584		/* Fetch details for this drive. */
585		if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
586		    NULL) < 0) {
587			error = errno;
588			warn("Failed to fetch info for drive %u",
589			    list->addr[i].device_id);
590			goto error;
591		}
592
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,
597		    MFI_DNAME_ES));
598		printf("\n");
599	}
600	error = 0;
601error:
602	free(list);
603	close(fd);
604
605	return (error);
606}
607MFI_COMMAND(show, drives, show_drives);
608
609static int
610show_firmware(int ac, char **av __unused)
611{
612	struct mfi_ctrl_info info;
613	struct mfi_info_component header;
614	int error, fd;
615	u_int i;
616
617	if (ac != 1) {
618		warnx("show firmware: extra arguments");
619		return (EINVAL);
620	}
621
622	fd = mfi_open(mfi_unit, O_RDONLY);
623	if (fd < 0) {
624		error = errno;
625		warn("mfi_open");
626		return (error);
627	}
628
629	if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
630		error = errno;
631		warn("Failed to get controller info");
632		close(fd);
633		return (error);
634	}
635
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");
658
659	close(fd);
660
661	return (0);
662}
663MFI_COMMAND(show, firmware, show_firmware);
664
665static int
666show_progress(int ac, char **av __unused)
667{
668	struct mfi_ld_list llist;
669	struct mfi_pd_list *plist;
670	struct mfi_ld_info linfo;
671	struct mfi_pd_info pinfo;
672	int busy, error, fd;
673	u_int i;
674	uint16_t device_id;
675	uint8_t target_id;
676
677	if (ac != 1) {
678		warnx("show progress: extra arguments");
679		return (EINVAL);
680	}
681
682	fd = mfi_open(mfi_unit, O_RDONLY);
683	if (fd < 0) {
684		error = errno;
685		warn("mfi_open");
686		return (error);
687	}
688
689	if (mfi_ld_get_list(fd, &llist, NULL) < 0) {
690		error = errno;
691		warn("Failed to get volume list");
692		close(fd);
693		return (error);
694	}
695	if (mfi_pd_get_list(fd, &plist, NULL) < 0) {
696		error = errno;
697		warn("Failed to get drive list");
698		close(fd);
699		return (error);
700	}
701
702	busy = 0;
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) {
706			error = errno;
707			warn("Failed to get info for volume %s",
708			    mfi_volume_name(fd, target_id));
709			free(plist);
710			close(fd);
711			return (error);
712		}
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",
716			    &linfo.progress.cc);
717			busy = 1;
718		}
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);
723			busy = 1;
724		}
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);
729			busy = 1;
730		}
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);
735			busy = 1;
736		}
737	}
738
739	for (i = 0; i < plist->count; i++) {
740		if (plist->addr[i].scsi_dev_type != 0)
741			continue;
742
743		device_id = plist->addr[i].device_id;
744		if (mfi_pd_get_info(fd, device_id, &pinfo, NULL) < 0) {
745			error = errno;
746			warn("Failed to fetch info for drive %u", device_id);
747			free(plist);
748			close(fd);
749			return (error);
750		}
751
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);
756			busy = 1;
757		}
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);
763			busy = 1;
764		}
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);
769
770		}
771	}
772
773	free(plist);
774	close(fd);
775
776	if (!busy)
777		printf("No activity in progress for adapter mfi%d\n", mfi_unit);
778
779	return (0);
780}
781MFI_COMMAND(show, progress, show_progress);
782
783static int
784show_foreign(int ac, char **av)
785{
786	return(display_format(ac, av, 0/*normal display*/, MFI_DCMD_CFG_FOREIGN_DISPLAY));
787}
788MFI_COMMAND(show, foreign, show_foreign);
789