1/*-
2 * Copyright (c) 2008 Yahoo!, Inc.
3 * All rights reserved.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__RCSID("$FreeBSD: stable/11/usr.sbin/mptutil/mpt_show.c 332603 2018-04-16 16:24:36Z asomers $");
33
34#include <sys/param.h>
35#include <sys/errno.h>
36#include <err.h>
37#include <libutil.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include "mptutil.h"
43
44MPT_TABLE(top, show);
45
46#define	STANDALONE_STATE	"ONLINE"
47
48static void
49format_stripe(char *buf, size_t buflen, U32 stripe)
50{
51
52	humanize_number(buf, buflen, stripe * 512, "", HN_AUTOSCALE,
53	    HN_B | HN_NOSPACE);
54}
55
56static void
57display_stripe_map(const char *label, U32 StripeMap)
58{
59	char stripe[5];
60	int comma, i;
61
62	comma = 0;
63	printf("%s: ", label);
64	for (i = 0; StripeMap != 0; i++, StripeMap >>= 1)
65		if (StripeMap & 1) {
66			format_stripe(stripe, sizeof(stripe), 1 << i);
67			if (comma)
68				printf(", ");
69			printf("%s", stripe);
70			comma = 1;
71		}
72	printf("\n");
73}
74
75static int
76show_adapter(int ac, char **av)
77{
78	CONFIG_PAGE_MANUFACTURING_0 *man0;
79	CONFIG_PAGE_IOC_2 *ioc2;
80	CONFIG_PAGE_IOC_6 *ioc6;
81	U16 IOCStatus;
82	int comma, error, fd;
83
84	if (ac != 1) {
85		warnx("show adapter: extra arguments");
86		return (EINVAL);
87	}
88
89	fd = mpt_open(mpt_unit);
90	if (fd < 0) {
91		error = errno;
92		warn("mpt_open");
93		return (error);
94	}
95
96	man0 = mpt_read_man_page(fd, 0, NULL);
97	if (man0 == NULL) {
98		error = errno;
99		warn("Failed to get controller info");
100		close(fd);
101		return (error);
102	}
103	if (man0->Header.PageLength < sizeof(*man0) / 4) {
104		warnx("Invalid controller info");
105		free(man0);
106		close(fd);
107		return (EINVAL);
108	}
109	printf("mpt%d Adapter:\n", mpt_unit);
110	printf("       Board Name: %.16s\n", man0->BoardName);
111	printf("   Board Assembly: %.16s\n", man0->BoardAssembly);
112	printf("        Chip Name: %.16s\n", man0->ChipName);
113	printf("    Chip Revision: %.16s\n", man0->ChipRevision);
114
115	free(man0);
116
117	ioc2 = mpt_read_ioc_page(fd, 2, &IOCStatus);
118	if (ioc2 != NULL) {
119		printf("      RAID Levels:");
120		comma = 0;
121		if (ioc2->CapabilitiesFlags &
122		    MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT) {
123			printf(" RAID0");
124			comma = 1;
125		}
126		if (ioc2->CapabilitiesFlags &
127		    MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT) {
128			printf("%s RAID1", comma ? "," : "");
129			comma = 1;
130		}
131		if (ioc2->CapabilitiesFlags &
132		    MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT) {
133			printf("%s RAID1E", comma ? "," : "");
134			comma = 1;
135		}
136		if (ioc2->CapabilitiesFlags &
137		    MPI_IOCPAGE2_CAP_FLAGS_RAID_5_SUPPORT) {
138			printf("%s RAID5", comma ? "," : "");
139			comma = 1;
140		}
141		if (ioc2->CapabilitiesFlags &
142		    MPI_IOCPAGE2_CAP_FLAGS_RAID_6_SUPPORT) {
143			printf("%s RAID6", comma ? "," : "");
144			comma = 1;
145		}
146		if (ioc2->CapabilitiesFlags &
147		    MPI_IOCPAGE2_CAP_FLAGS_RAID_10_SUPPORT) {
148			printf("%s RAID10", comma ? "," : "");
149			comma = 1;
150		}
151		if (ioc2->CapabilitiesFlags &
152		    MPI_IOCPAGE2_CAP_FLAGS_RAID_50_SUPPORT) {
153			printf("%s RAID50", comma ? "," : "");
154			comma = 1;
155		}
156		if (!comma)
157			printf(" none");
158		printf("\n");
159		free(ioc2);
160	} else if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
161	    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
162		warnx("mpt_read_ioc_page(2): %s", mpt_ioc_status(IOCStatus));
163
164	ioc6 = mpt_read_ioc_page(fd, 6, &IOCStatus);
165	if (ioc6 != NULL) {
166		display_stripe_map("    RAID0 Stripes",
167		    ioc6->SupportedStripeSizeMapIS);
168		display_stripe_map("   RAID1E Stripes",
169		    ioc6->SupportedStripeSizeMapIME);
170		printf(" RAID0 Drives/Vol: %u", ioc6->MinDrivesIS);
171		if (ioc6->MinDrivesIS != ioc6->MaxDrivesIS)
172			printf("-%u", ioc6->MaxDrivesIS);
173		printf("\n");
174		printf(" RAID1 Drives/Vol: %u", ioc6->MinDrivesIM);
175		if (ioc6->MinDrivesIM != ioc6->MaxDrivesIM)
176			printf("-%u", ioc6->MaxDrivesIM);
177		printf("\n");
178		printf("RAID1E Drives/Vol: %u", ioc6->MinDrivesIME);
179		if (ioc6->MinDrivesIME != ioc6->MaxDrivesIME)
180			printf("-%u", ioc6->MaxDrivesIME);
181		printf("\n");
182		free(ioc6);
183	} else if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
184	    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
185		warnx("mpt_read_ioc_page(6): %s", mpt_ioc_status(IOCStatus));
186
187	/* TODO: Add an ioctl to fetch IOC_FACTS and print firmware version. */
188
189	close(fd);
190
191	return (0);
192}
193MPT_COMMAND(show, adapter, show_adapter);
194
195static void
196print_vol(CONFIG_PAGE_RAID_VOL_0 *info, int state_len)
197{
198	uint64_t size;
199	const char *level, *state;
200	char buf[6], stripe[5];
201
202	size = ((uint64_t)info->MaxLBAHigh << 32) | info->MaxLBA;
203	humanize_number(buf, sizeof(buf), (size + 1) * 512, "", HN_AUTOSCALE,
204	    HN_B | HN_NOSPACE | HN_DECIMAL);
205	if (info->VolumeType == MPI_RAID_VOL_TYPE_IM)
206		stripe[0] = '\0';
207	else
208		format_stripe(stripe, sizeof(stripe), info->StripeSize);
209	level = mpt_raid_level(info->VolumeType);
210	state = mpt_volstate(info->VolumeStatus.State);
211	if (state_len > 0)
212		printf("(%6s) %-8s %6s %-*s", buf, level, stripe, state_len,
213		    state);
214	else if (stripe[0] != '\0')
215		printf("(%s) %s %s %s", buf, level, stripe, state);
216	else
217		printf("(%s) %s %s", buf, level, state);
218}
219
220static void
221print_pd(CONFIG_PAGE_RAID_PHYS_DISK_0 *info, int state_len, int location)
222{
223	const char *inq, *state;
224	char buf[6];
225
226	humanize_number(buf, sizeof(buf), ((uint64_t)info->MaxLBA + 1) * 512,
227	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
228	state = mpt_pdstate(info);
229	if (state_len > 0)
230		printf("(%6s) %-*s", buf, state_len, state);
231	else
232		printf("(%s) %s", buf, state);
233	inq = mpt_pd_inq_string(info);
234	if (inq != NULL)
235		printf(" %s", inq);
236	if (!location)
237		return;
238	printf(" bus %d id %d", info->PhysDiskBus, info->PhysDiskID);
239}
240
241static void
242print_standalone(struct mpt_standalone_disk *disk, int state_len, int location)
243{
244	char buf[6];
245
246	humanize_number(buf, sizeof(buf), (disk->maxlba + 1) * 512,
247	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
248	if (state_len > 0)
249		printf("(%6s) %-*s", buf, state_len, STANDALONE_STATE);
250	else
251		printf("(%s) %s", buf, STANDALONE_STATE);
252	if (disk->inqstring[0] != '\0')
253		printf(" %s", disk->inqstring);
254	if (!location)
255		return;
256	printf(" bus %d id %d", disk->bus, disk->target);
257}
258
259static void
260print_spare_pools(U8 HotSparePool)
261{
262	int i;
263
264	if (HotSparePool == 0) {
265		printf("none");
266		return;
267	}
268	for (i = 0; HotSparePool != 0; i++) {
269		if (HotSparePool & 1) {
270			printf("%d", i);
271			if (HotSparePool == 1)
272				break;
273			printf(", ");
274		}
275		HotSparePool >>= 1;
276	}
277}
278
279static int
280show_config(int ac, char **av)
281{
282	CONFIG_PAGE_IOC_2 *ioc2;
283	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
284	CONFIG_PAGE_IOC_5 *ioc5;
285	IOC_5_HOT_SPARE *spare;
286	CONFIG_PAGE_RAID_VOL_0 *vinfo;
287	RAID_VOL0_PHYS_DISK *disk;
288	CONFIG_PAGE_RAID_VOL_1 *vnames;
289	CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo;
290	struct mpt_standalone_disk *sdisks;
291	int error, fd, i, j, nsdisks;
292
293	if (ac != 1) {
294		warnx("show config: extra arguments");
295		return (EINVAL);
296	}
297
298	fd = mpt_open(mpt_unit);
299	if (fd < 0) {
300		error = errno;
301		warn("mpt_open");
302		return (error);
303	}
304
305	/* Get the config from the controller. */
306	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
307	ioc5 = mpt_read_ioc_page(fd, 5, NULL);
308	if (ioc2 == NULL || ioc5 == NULL) {
309		error = errno;
310		warn("Failed to get config");
311		free(ioc2);
312		close(fd);
313		return (error);
314	}
315	if (mpt_fetch_disks(fd, &nsdisks, &sdisks) < 0) {
316		error = errno;
317		warn("Failed to get standalone drive list");
318		free(ioc5);
319		free(ioc2);
320		close(fd);
321		return (error);
322	}
323
324	/* Dump out the configuration. */
325	printf("mpt%d Configuration: %d volumes, %d drives\n",
326	    mpt_unit, ioc2->NumActiveVolumes, ioc2->NumActivePhysDisks +
327	    nsdisks);
328	vol = ioc2->RaidVolume;
329	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
330		printf("    volume %s ", mpt_volume_name(vol->VolumeBus,
331		    vol->VolumeID));
332		vinfo = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL);
333		if (vinfo == NULL) {
334			printf("%s UNKNOWN", mpt_raid_level(vol->VolumeType));
335		} else
336			print_vol(vinfo, -1);
337		vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL);
338		if (vnames != NULL) {
339			if (vnames->Name[0] != '\0')
340				printf(" <%s>", vnames->Name);
341			free(vnames);
342		}
343		if (vinfo == NULL) {
344			printf("\n");
345			continue;
346		}
347		printf(" spans:\n");
348		disk = vinfo->PhysDisk;
349		for (j = 0; j < vinfo->NumPhysDisks; disk++, j++) {
350			printf("        drive %u ", disk->PhysDiskNum);
351			pinfo = mpt_pd_info(fd, disk->PhysDiskNum, NULL);
352			if (pinfo != NULL) {
353				print_pd(pinfo, -1, 0);
354				free(pinfo);
355			}
356			printf("\n");
357		}
358		if (vinfo->VolumeSettings.HotSparePool != 0) {
359			printf("        spare pools: ");
360			print_spare_pools(vinfo->VolumeSettings.HotSparePool);
361			printf("\n");
362		}
363		free(vinfo);
364	}
365
366	spare = ioc5->HotSpare;
367	for (i = 0; i < ioc5->NumHotSpares; spare++, i++) {
368		printf("    spare %u ", spare->PhysDiskNum);
369		pinfo = mpt_pd_info(fd, spare->PhysDiskNum, NULL);
370		if (pinfo != NULL) {
371			print_pd(pinfo, -1, 0);
372			free(pinfo);
373		}
374		printf(" backs pool %d\n", ffs(spare->HotSparePool) - 1);
375	}
376	for (i = 0; i < nsdisks; i++) {
377		printf("    drive %s ", sdisks[i].devname);
378		print_standalone(&sdisks[i], -1, 0);
379		printf("\n");
380	}
381	free(ioc2);
382	free(ioc5);
383	free(sdisks);
384	close(fd);
385
386	return (0);
387}
388MPT_COMMAND(show, config, show_config);
389
390static int
391show_volumes(int ac, char **av)
392{
393	CONFIG_PAGE_IOC_2 *ioc2;
394	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
395	CONFIG_PAGE_RAID_VOL_0 **volumes;
396	CONFIG_PAGE_RAID_VOL_1 *vnames;
397	int error, fd, i, len, state_len;
398
399	if (ac != 1) {
400		warnx("show volumes: extra arguments");
401		return (EINVAL);
402	}
403
404	fd = mpt_open(mpt_unit);
405	if (fd < 0) {
406		error = errno;
407		warn("mpt_open");
408		return (error);
409	}
410
411	/* Get the volume list from the controller. */
412	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
413	if (ioc2 == NULL) {
414		error = errno;
415		warn("Failed to get volume list");
416		return (error);
417	}
418
419	/*
420	 * Go ahead and read the info for all the volumes and figure
421	 * out the maximum width of the state field.
422	 */
423	volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
424	state_len = strlen("State");
425	vol = ioc2->RaidVolume;
426	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
427		volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
428		    NULL);
429		if (volumes[i] == NULL)
430			len = strlen("UNKNOWN");
431		else
432			len = strlen(mpt_volstate(
433			    volumes[i]->VolumeStatus.State));
434		if (len > state_len)
435			state_len = len;
436	}
437	printf("mpt%d Volumes:\n", mpt_unit);
438	printf("  Id     Size    Level   Stripe ");
439	len = state_len - strlen("State");
440	for (i = 0; i < (len + 1) / 2; i++)
441		printf(" ");
442	printf("State");
443	for (i = 0; i < len / 2; i++)
444		printf(" ");
445	printf(" Write-Cache  Name\n");
446	vol = ioc2->RaidVolume;
447	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
448		printf("%6s ", mpt_volume_name(vol->VolumeBus, vol->VolumeID));
449		if (volumes[i] != NULL)
450			print_vol(volumes[i], state_len);
451		else
452			printf("         %-8s %-*s",
453			    mpt_raid_level(vol->VolumeType), state_len,
454			    "UNKNOWN");
455		if (volumes[i] != NULL) {
456			if (volumes[i]->VolumeSettings.Settings &
457			    MPI_RAIDVOL0_SETTING_WRITE_CACHING_ENABLE)
458				printf("   Enabled   ");
459			else
460				printf("   Disabled  ");
461		} else
462			printf("             ");
463		free(volumes[i]);
464		vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL);
465		if (vnames != NULL) {
466			if (vnames->Name[0] != '\0')
467				printf(" <%s>", vnames->Name);
468			free(vnames);
469		}
470		printf("\n");
471	}
472	free(volumes);
473	free(ioc2);
474	close(fd);
475
476	return (0);
477}
478MPT_COMMAND(show, volumes, show_volumes);
479
480static int
481show_drives(int ac, char **av)
482{
483	struct mpt_drive_list *list;
484	struct mpt_standalone_disk *sdisks;
485	int error, fd, i, len, nsdisks, state_len;
486
487	if (ac != 1) {
488		warnx("show drives: extra arguments");
489		return (EINVAL);
490	}
491
492	fd = mpt_open(mpt_unit);
493	if (fd < 0) {
494		error = errno;
495		warn("mpt_open");
496		return (error);
497	}
498
499	/* Get the drive list. */
500	list = mpt_pd_list(fd);
501	if (list == NULL) {
502		error = errno;
503		close(fd);
504		warn("Failed to get drive list");
505		return (error);
506	}
507
508	/* Fetch the list of standalone disks for this controller. */
509	state_len = 0;
510	if (mpt_fetch_disks(fd, &nsdisks, &sdisks) != 0) {
511		nsdisks = 0;
512		sdisks = NULL;
513	}
514	if (nsdisks != 0)
515		state_len = strlen(STANDALONE_STATE);
516
517	/* Walk the drive list to determine width of state column. */
518	for (i = 0; i < list->ndrives; i++) {
519		len = strlen(mpt_pdstate(list->drives[i]));
520		if (len > state_len)
521			state_len = len;
522	}
523
524	/* List the drives. */
525	printf("mpt%d Physical Drives:\n", mpt_unit);
526	for (i = 0; i < list->ndrives; i++) {
527		printf("%4u ", list->drives[i]->PhysDiskNum);
528		print_pd(list->drives[i], state_len, 1);
529		printf("\n");
530	}
531	mpt_free_pd_list(list);
532	for (i = 0; i < nsdisks; i++) {
533		printf("%4s ", sdisks[i].devname);
534		print_standalone(&sdisks[i], state_len, 1);
535		printf("\n");
536	}
537	free(sdisks);
538
539	close(fd);
540
541	return (0);
542}
543MPT_COMMAND(show, drives, show_drives);
544
545#ifdef DEBUG
546static int
547show_physdisks(int ac, char **av)
548{
549	CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo;
550	U16 IOCStatus;
551	int error, fd, i;
552
553	if (ac != 1) {
554		warnx("show drives: extra arguments");
555		return (EINVAL);
556	}
557
558	fd = mpt_open(mpt_unit);
559	if (fd < 0) {
560		error = errno;
561		warn("mpt_open");
562		return (error);
563	}
564
565	/* Try to find each possible phys disk page. */
566	for (i = 0; i <= 0xff; i++) {
567		pinfo = mpt_pd_info(fd, i, &IOCStatus);
568		if (pinfo == NULL) {
569			if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
570			    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
571				warnx("mpt_pd_info(%d): %s", i,
572				    mpt_ioc_status(IOCStatus));
573			continue;
574		}
575		printf("%3u ", i);
576		print_pd(pinfo, -1, 1);
577		printf("\n");
578	}
579
580	close(fd);
581
582	return (0);
583}
584MPT_COMMAND(show, pd, show_physdisks);
585#endif
586