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