xref: /freebsd/usr.sbin/mptutil/mpt_show.c (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
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$");
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 
44 MPT_TABLE(top, show);
45 
46 #define	STANDALONE_STATE	"ONLINE"
47 
48 static void
49 format_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 
56 static void
57 display_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 
75 static int
76 show_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 		return (error);
101 	}
102 	if (man0->Header.PageLength < sizeof(*man0) / 4) {
103 		warnx("Invalid controller info");
104 		return (EINVAL);
105 	}
106 	printf("mpt%d Adapter:\n", mpt_unit);
107 	printf("       Board Name: %.16s\n", man0->BoardName);
108 	printf("   Board Assembly: %.16s\n", man0->BoardAssembly);
109 	printf("        Chip Name: %.16s\n", man0->ChipName);
110 	printf("    Chip Revision: %.16s\n", man0->ChipRevision);
111 
112 	free(man0);
113 
114 	ioc2 = mpt_read_ioc_page(fd, 2, &IOCStatus);
115 	if (ioc2 != NULL) {
116 		printf("      RAID Levels:");
117 		comma = 0;
118 		if (ioc2->CapabilitiesFlags &
119 		    MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT) {
120 			printf(" RAID0");
121 			comma = 1;
122 		}
123 		if (ioc2->CapabilitiesFlags &
124 		    MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT) {
125 			printf("%s RAID1", comma ? "," : "");
126 			comma = 1;
127 		}
128 		if (ioc2->CapabilitiesFlags &
129 		    MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT) {
130 			printf("%s RAID1E", comma ? "," : "");
131 			comma = 1;
132 		}
133 		if (ioc2->CapabilitiesFlags &
134 		    MPI_IOCPAGE2_CAP_FLAGS_RAID_5_SUPPORT) {
135 			printf("%s RAID5", comma ? "," : "");
136 			comma = 1;
137 		}
138 		if (ioc2->CapabilitiesFlags &
139 		    MPI_IOCPAGE2_CAP_FLAGS_RAID_6_SUPPORT) {
140 			printf("%s RAID6", comma ? "," : "");
141 			comma = 1;
142 		}
143 		if (ioc2->CapabilitiesFlags &
144 		    MPI_IOCPAGE2_CAP_FLAGS_RAID_10_SUPPORT) {
145 			printf("%s RAID10", comma ? "," : "");
146 			comma = 1;
147 		}
148 		if (ioc2->CapabilitiesFlags &
149 		    MPI_IOCPAGE2_CAP_FLAGS_RAID_50_SUPPORT) {
150 			printf("%s RAID50", comma ? "," : "");
151 			comma = 1;
152 		}
153 		if (!comma)
154 			printf(" none");
155 		printf("\n");
156 		free(ioc2);
157 	} else if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
158 	    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
159 		warnx("mpt_read_ioc_page(2): %s", mpt_ioc_status(IOCStatus));
160 
161 	ioc6 = mpt_read_ioc_page(fd, 6, &IOCStatus);
162 	if (ioc6 != NULL) {
163 		display_stripe_map("    RAID0 Stripes",
164 		    ioc6->SupportedStripeSizeMapIS);
165 		display_stripe_map("   RAID1E Stripes",
166 		    ioc6->SupportedStripeSizeMapIME);
167 		printf(" RAID0 Drives/Vol: %u", ioc6->MinDrivesIS);
168 		if (ioc6->MinDrivesIS != ioc6->MaxDrivesIS)
169 			printf("-%u", ioc6->MaxDrivesIS);
170 		printf("\n");
171 		printf(" RAID1 Drives/Vol: %u", ioc6->MinDrivesIM);
172 		if (ioc6->MinDrivesIM != ioc6->MaxDrivesIM)
173 			printf("-%u", ioc6->MaxDrivesIM);
174 		printf("\n");
175 		printf("RAID1E Drives/Vol: %u", ioc6->MinDrivesIME);
176 		if (ioc6->MinDrivesIME != ioc6->MaxDrivesIME)
177 			printf("-%u", ioc6->MaxDrivesIME);
178 		printf("\n");
179 		free(ioc6);
180 	} else if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
181 	    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
182 		warnx("mpt_read_ioc_page(6): %s", mpt_ioc_status(IOCStatus));
183 
184 	/* TODO: Add an ioctl to fetch IOC_FACTS and print firmware version. */
185 
186 	close(fd);
187 
188 	return (0);
189 }
190 MPT_COMMAND(show, adapter, show_adapter);
191 
192 static void
193 print_vol(CONFIG_PAGE_RAID_VOL_0 *info, int state_len)
194 {
195 	uint64_t size;
196 	const char *level, *state;
197 	char buf[6], stripe[5];
198 
199 	size = ((uint64_t)info->MaxLBAHigh << 32) | info->MaxLBA;
200 	humanize_number(buf, sizeof(buf), (size + 1) * 512, "", HN_AUTOSCALE,
201 	    HN_B | HN_NOSPACE | HN_DECIMAL);
202 	if (info->VolumeType == MPI_RAID_VOL_TYPE_IM)
203 		stripe[0] = '\0';
204 	else
205 		format_stripe(stripe, sizeof(stripe), info->StripeSize);
206 	level = mpt_raid_level(info->VolumeType);
207 	state = mpt_volstate(info->VolumeStatus.State);
208 	if (state_len > 0)
209 		printf("(%6s) %-8s %6s %-*s", buf, level, stripe, state_len,
210 		    state);
211 	else if (stripe[0] != '\0')
212 		printf("(%s) %s %s %s", buf, level, stripe, state);
213 	else
214 		printf("(%s) %s %s", buf, level, state);
215 }
216 
217 static void
218 print_pd(CONFIG_PAGE_RAID_PHYS_DISK_0 *info, int state_len, int location)
219 {
220 	const char *inq, *state;
221 	char buf[6];
222 
223 	humanize_number(buf, sizeof(buf), ((uint64_t)info->MaxLBA + 1) * 512,
224 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
225 	state = mpt_pdstate(info);
226 	if (state_len > 0)
227 		printf("(%6s) %-*s", buf, state_len, state);
228 	else
229 		printf("(%s) %s", buf, state);
230 	inq = mpt_pd_inq_string(info);
231 	if (inq != NULL)
232 		printf(" %s", inq);
233 	if (!location)
234 		return;
235 	printf(" bus %d id %d", info->PhysDiskBus, info->PhysDiskID);
236 }
237 
238 static void
239 print_standalone(struct mpt_standalone_disk *disk, int state_len, int location)
240 {
241 	char buf[6];
242 
243 	humanize_number(buf, sizeof(buf), (disk->maxlba + 1) * 512,
244 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE |HN_DECIMAL);
245 	if (state_len > 0)
246 		printf("(%6s) %-*s", buf, state_len, STANDALONE_STATE);
247 	else
248 		printf("(%s) %s", buf, STANDALONE_STATE);
249 	if (disk->inqstring[0] != '\0')
250 		printf(" %s", disk->inqstring);
251 	if (!location)
252 		return;
253 	printf(" bus %d id %d", disk->bus, disk->target);
254 }
255 
256 static void
257 print_spare_pools(U8 HotSparePool)
258 {
259 	int i;
260 
261 	if (HotSparePool == 0) {
262 		printf("none");
263 		return;
264 	}
265 	for (i = 0; HotSparePool != 0; i++) {
266 		if (HotSparePool & 1) {
267 			printf("%d", i);
268 			if (HotSparePool == 1)
269 				break;
270 			printf(", ");
271 		}
272 		HotSparePool >>= 1;
273 	}
274 }
275 
276 static int
277 show_config(int ac, char **av)
278 {
279 	CONFIG_PAGE_IOC_2 *ioc2;
280 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
281 	CONFIG_PAGE_IOC_5 *ioc5;
282 	IOC_5_HOT_SPARE *spare;
283 	CONFIG_PAGE_RAID_VOL_0 *vinfo;
284 	RAID_VOL0_PHYS_DISK *disk;
285 	CONFIG_PAGE_RAID_VOL_1 *vnames;
286 	CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo;
287 	struct mpt_standalone_disk *sdisks;
288 	int error, fd, i, j, nsdisks;
289 
290 	if (ac != 1) {
291 		warnx("show config: extra arguments");
292 		return (EINVAL);
293 	}
294 
295 	fd = mpt_open(mpt_unit);
296 	if (fd < 0) {
297 		error = errno;
298 		warn("mpt_open");
299 		return (error);
300 	}
301 
302 	/* Get the config from the controller. */
303 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
304 	ioc5 = mpt_read_ioc_page(fd, 5, NULL);
305 	if (ioc2 == NULL || ioc5 == NULL) {
306 		error = errno;
307 		warn("Failed to get config");
308 		return (error);
309 	}
310 	if (mpt_fetch_disks(fd, &nsdisks, &sdisks) < 0) {
311 		error = errno;
312 		warn("Failed to get standalone drive list");
313 		return (error);
314 	}
315 
316 	/* Dump out the configuration. */
317 	printf("mpt%d Configuration: %d volumes, %d drives\n",
318 	    mpt_unit, ioc2->NumActiveVolumes, ioc2->NumActivePhysDisks +
319 	    nsdisks);
320 	vol = ioc2->RaidVolume;
321 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
322 		printf("    volume %s ", mpt_volume_name(vol->VolumeBus,
323 		    vol->VolumeID));
324 		vinfo = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL);
325 		if (vinfo == NULL) {
326 			printf("%s UNKNOWN", mpt_raid_level(vol->VolumeType));
327 		} else
328 			print_vol(vinfo, -1);
329 		vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL);
330 		if (vnames != NULL) {
331 			if (vnames->Name[0] != '\0')
332 				printf(" <%s>", vnames->Name);
333 			free(vnames);
334 		}
335 		if (vinfo == NULL) {
336 			printf("\n");
337 			continue;
338 		}
339 		printf(" spans:\n");
340 		disk = vinfo->PhysDisk;
341 		for (j = 0; j < vinfo->NumPhysDisks; disk++, j++) {
342 			printf("        drive %u ", disk->PhysDiskNum);
343 			pinfo = mpt_pd_info(fd, disk->PhysDiskNum, NULL);
344 			if (pinfo != NULL) {
345 				print_pd(pinfo, -1, 0);
346 				free(pinfo);
347 			}
348 			printf("\n");
349 		}
350 		if (vinfo->VolumeSettings.HotSparePool != 0) {
351 			printf("        spare pools: ");
352 			print_spare_pools(vinfo->VolumeSettings.HotSparePool);
353 			printf("\n");
354 		}
355 		free(vinfo);
356 	}
357 
358 	spare = ioc5->HotSpare;
359 	for (i = 0; i < ioc5->NumHotSpares; spare++, i++) {
360 		printf("    spare %u ", spare->PhysDiskNum);
361 		pinfo = mpt_pd_info(fd, spare->PhysDiskNum, NULL);
362 		if (pinfo != NULL) {
363 			print_pd(pinfo, -1, 0);
364 			free(pinfo);
365 		}
366 		printf(" backs pool %d\n", ffs(spare->HotSparePool) - 1);
367 	}
368 	for (i = 0; i < nsdisks; i++) {
369 		printf("    drive %s ", sdisks[i].devname);
370 		print_standalone(&sdisks[i], -1, 0);
371 		printf("\n");
372 	}
373 	free(ioc2);
374 	free(ioc5);
375 	free(sdisks);
376 	close(fd);
377 
378 	return (0);
379 }
380 MPT_COMMAND(show, config, show_config);
381 
382 static int
383 show_volumes(int ac, char **av)
384 {
385 	CONFIG_PAGE_IOC_2 *ioc2;
386 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
387 	CONFIG_PAGE_RAID_VOL_0 **volumes;
388 	CONFIG_PAGE_RAID_VOL_1 *vnames;
389 	int error, fd, i, len, state_len;
390 
391 	if (ac != 1) {
392 		warnx("show volumes: extra arguments");
393 		return (EINVAL);
394 	}
395 
396 	fd = mpt_open(mpt_unit);
397 	if (fd < 0) {
398 		error = errno;
399 		warn("mpt_open");
400 		return (error);
401 	}
402 
403 	/* Get the volume list from the controller. */
404 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
405 	if (ioc2 == NULL) {
406 		error = errno;
407 		warn("Failed to get volume list");
408 		return (error);
409 	}
410 
411 	/*
412 	 * Go ahead and read the info for all the volumes and figure
413 	 * out the maximum width of the state field.
414 	 */
415 	volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
416 	state_len = strlen("State");
417 	vol = ioc2->RaidVolume;
418 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
419 		volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
420 		    NULL);
421 		if (volumes[i] == NULL)
422 			len = strlen("UNKNOWN");
423 		else
424 			len = strlen(mpt_volstate(
425 			    volumes[i]->VolumeStatus.State));
426 		if (len > state_len)
427 			state_len = len;
428 	}
429 	printf("mpt%d Volumes:\n", mpt_unit);
430 	printf("  Id     Size    Level   Stripe ");
431 	len = state_len - strlen("State");
432 	for (i = 0; i < (len + 1) / 2; i++)
433 		printf(" ");
434 	printf("State");
435 	for (i = 0; i < len / 2; i++)
436 		printf(" ");
437 	printf(" Write-Cache  Name\n");
438 	vol = ioc2->RaidVolume;
439 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
440 		printf("%6s ", mpt_volume_name(vol->VolumeBus, vol->VolumeID));
441 		if (volumes[i] != NULL)
442 			print_vol(volumes[i], state_len);
443 		else
444 			printf("         %-8s %-*s",
445 			    mpt_raid_level(vol->VolumeType), state_len,
446 			    "UNKNOWN");
447 		if (volumes[i] != NULL) {
448 			if (volumes[i]->VolumeSettings.Settings &
449 			    MPI_RAIDVOL0_SETTING_WRITE_CACHING_ENABLE)
450 				printf("   Enabled   ");
451 			else
452 				printf("   Disabled  ");
453 		} else
454 			printf("             ");
455 		free(volumes[i]);
456 		vnames = mpt_vol_names(fd, vol->VolumeBus, vol->VolumeID, NULL);
457 		if (vnames != NULL) {
458 			if (vnames->Name[0] != '\0')
459 				printf(" <%s>", vnames->Name);
460 			free(vnames);
461 		}
462 		printf("\n");
463 	}
464 	free(ioc2);
465 	close(fd);
466 
467 	return (0);
468 }
469 MPT_COMMAND(show, volumes, show_volumes);
470 
471 static int
472 show_drives(int ac, char **av)
473 {
474 	struct mpt_drive_list *list;
475 	struct mpt_standalone_disk *sdisks;
476 	int error, fd, i, len, nsdisks, state_len;
477 
478 	if (ac != 1) {
479 		warnx("show drives: extra arguments");
480 		return (EINVAL);
481 	}
482 
483 	fd = mpt_open(mpt_unit);
484 	if (fd < 0) {
485 		error = errno;
486 		warn("mpt_open");
487 		return (error);
488 	}
489 
490 	/* Get the drive list. */
491 	list = mpt_pd_list(fd);
492 	if (list == NULL) {
493 		error = errno;
494 		warn("Failed to get drive list");
495 		return (error);
496 	}
497 
498 	/* Fetch the list of standalone disks for this controller. */
499 	state_len = 0;
500 	if (mpt_fetch_disks(fd, &nsdisks, &sdisks) != 0) {
501 		nsdisks = 0;
502 		sdisks = NULL;
503 	}
504 	if (nsdisks != 0)
505 		state_len = strlen(STANDALONE_STATE);
506 
507 	/* Walk the drive list to determine width of state column. */
508 	for (i = 0; i < list->ndrives; i++) {
509 		len = strlen(mpt_pdstate(list->drives[i]));
510 		if (len > state_len)
511 			state_len = len;
512 	}
513 
514 	/* List the drives. */
515 	printf("mpt%d Physical Drives:\n", mpt_unit);
516 	for (i = 0; i < list->ndrives; i++) {
517 		printf("%4u ", list->drives[i]->PhysDiskNum);
518 		print_pd(list->drives[i], state_len, 1);
519 		printf("\n");
520 	}
521 	mpt_free_pd_list(list);
522 	for (i = 0; i < nsdisks; i++) {
523 		printf("%4s ", sdisks[i].devname);
524 		print_standalone(&sdisks[i], state_len, 1);
525 		printf("\n");
526 	}
527 	free(sdisks);
528 
529 	close(fd);
530 
531 	return (0);
532 }
533 MPT_COMMAND(show, drives, show_drives);
534 
535 #ifdef DEBUG
536 static int
537 show_physdisks(int ac, char **av)
538 {
539 	CONFIG_PAGE_RAID_PHYS_DISK_0 *pinfo;
540 	U16 IOCStatus;
541 	int error, fd, i;
542 
543 	if (ac != 1) {
544 		warnx("show drives: extra arguments");
545 		return (EINVAL);
546 	}
547 
548 	fd = mpt_open(mpt_unit);
549 	if (fd < 0) {
550 		error = errno;
551 		warn("mpt_open");
552 		return (error);
553 	}
554 
555 	/* Try to find each possible phys disk page. */
556 	for (i = 0; i <= 0xff; i++) {
557 		pinfo = mpt_pd_info(fd, i, &IOCStatus);
558 		if (pinfo == NULL) {
559 			if ((IOCStatus & MPI_IOCSTATUS_MASK) !=
560 			    MPI_IOCSTATUS_CONFIG_INVALID_PAGE)
561 				warnx("mpt_pd_info(%d): %s", i,
562 				    mpt_ioc_status(IOCStatus));
563 			continue;
564 		}
565 		printf("%3u ", i);
566 		print_pd(pinfo, -1, 1);
567 		printf("\n");
568 	}
569 
570 	close(fd);
571 
572 	return (0);
573 }
574 MPT_COMMAND(show, pd, show_physdisks);
575 #endif
576