1/*-
2 * Copyright (c) 2008, 2009 Yahoo!, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 *    permission.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32#include <sys/types.h>
33#include <sys/errno.h>
34#include <err.h>
35#include <fcntl.h>
36#include <libutil.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41#include "mfiutil.h"
42
43static const char* foreign_state = " (FOREIGN)";
44
45MFI_TABLE(top, show);
46
47void
48format_stripe(char *buf, size_t buflen, uint8_t stripe)
49{
50
51	humanize_number(buf, buflen, (1 << stripe) * 512, "", HN_AUTOSCALE,
52	    HN_B | HN_NOSPACE);
53}
54
55static int
56show_adapter(int ac, char **av __unused)
57{
58	struct mfi_ctrl_info info;
59	char stripe[5];
60	int error, fd, comma;
61
62	if (ac != 1) {
63		warnx("show adapter: extra arguments");
64		return (EINVAL);
65	}
66
67	fd = mfi_open(mfi_unit, O_RDONLY);
68	if (fd < 0) {
69		error = errno;
70		warn("mfi_open");
71		return (error);
72	}
73
74	if (mfi_ctrl_get_info(fd, &info, NULL) < 0) {
75		error = errno;
76		warn("Failed to get controller info");
77		close(fd);
78		return (error);
79	}
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:");
86#ifdef DEBUG
87	printf(" (%#x)", info.raid_levels);
88#endif
89	comma = 0;
90	if (info.raid_levels & MFI_INFO_RAID_0) {
91		printf(" JBOD, RAID0");
92		comma = 1;
93	}
94	if (info.raid_levels & MFI_INFO_RAID_1) {
95		printf("%s RAID1", comma ? "," : "");
96		comma = 1;
97	}
98	if (info.raid_levels & MFI_INFO_RAID_5) {
99		printf("%s RAID5", comma ? "," : "");
100		comma = 1;
101	}
102	if (info.raid_levels & MFI_INFO_RAID_1E) {
103		printf("%s RAID1E", comma ? "," : "");
104		comma = 1;
105	}
106	if (info.raid_levels & MFI_INFO_RAID_6) {
107		printf("%s RAID6", comma ? "," : "");
108		comma = 1;
109	}
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 ? "," : "");
113		comma = 1;
114	}
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 ? "," : "");
118		comma = 1;
119	}
120	printf("\n");
121	printf("  Battery Backup: ");
122	if (info.hw_present & MFI_INFO_HW_BBU)
123		printf("present\n");
124	else
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);
133
134	close(fd);
135
136	return (0);
137}
138MFI_COMMAND(show, adapter, show_adapter);
139
140static int
141show_battery(int ac, char **av __unused)
142{
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;
147	uint8_t status;
148	int comma, error, fd, show_capacity, show_props;
149	char buf[32];
150
151	if (ac != 1) {
152		warnx("show battery: extra arguments");
153		return (EINVAL);
154	}
155
156	fd = mfi_open(mfi_unit, O_RDONLY);
157	if (fd < 0) {
158		error = errno;
159		warn("mfi_open");
160		return (error);
161	}
162
163	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_CAPACITY_INFO, &cap,
164	    sizeof(cap), NULL, 0, &status) < 0) {
165		error = errno;
166		warn("Failed to get capacity info");
167		close(fd);
168		return (error);
169	}
170	if (status == MFI_STAT_NO_HW_PRESENT) {
171		printf("mfi%d: No battery present\n", mfi_unit);
172		close(fd);
173		return (0);
174	}
175	show_capacity = (status == MFI_STAT_OK);
176
177	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_DESIGN_INFO, &design,
178	    sizeof(design), NULL, 0, NULL) < 0) {
179		error = errno;
180		warn("Failed to get design info");
181		close(fd);
182		return (error);
183	}
184
185	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_STATUS, &stat, sizeof(stat),
186	    NULL, 0, NULL) < 0) {
187		error = errno;
188		warn("Failed to get status");
189		close(fd);
190		return (error);
191	}
192
193	if (mfi_bbu_get_props(fd, &props, &status) < 0) {
194		error = errno;
195		warn("Failed to get properties");
196		close(fd);
197		return (error);
198	}
199	show_props = (status == MFI_STAT_OK);
200
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);
209	if (show_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);
216	}
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);
220	if (show_props) {
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");
225		else
226			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			busy = 1;
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