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