xref: /freebsd/usr.sbin/mfiutil/mfiutil.c (revision 40f65a4df509c6b29b9e72407c7afefe28838844)
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/errno.h>
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include "mfiutil.h"
39 
40 SET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
41 
42 MFI_TABLE(top, start);
43 MFI_TABLE(top, stop);
44 MFI_TABLE(top, abort);
45 
46 int mfi_unit;
47 u_int mfi_opts;
48 static int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
49 
50 static void
51 usage(void)
52 {
53 
54 	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
55 	fprintf(stderr, "Commands include:\n");
56 	fprintf(stderr, "    version\n");
57 	fprintf(stderr, "    show adapter              - display controller information\n");
58 	fprintf(stderr, "    show battery              - display battery information\n");
59 	fprintf(stderr, "    show config               - display RAID configuration\n");
60 	fprintf(stderr, "    show drives               - list physical drives\n");
61 	fprintf(stderr, "    show events               - display event log\n");
62 	fprintf(stderr, "    show firmware             - list firmware images\n");
63 	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
64 	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
65 	fprintf(stderr, "    show volumes              - list logical volumes\n");
66 	fprintf(stderr, "    show patrol               - display patrol read status\n");
67 	fprintf(stderr, "    show progress             - display status of active operations\n");
68 	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
69 	fprintf(stderr, "    good <drive>              - mark a bad physical drive as good\n");
70 	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
71 	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
72 	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
73 	fprintf(stderr, "    start rebuild <drive>\n");
74 	fprintf(stderr, "    abort rebuild <drive>\n");
75 	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
76 	fprintf(stderr, "    cache <volume> [command [setting]]\n");
77 	fprintf(stderr, "    name <volume> <name>\n");
78 	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
79 	fprintf(stderr, "    clear                     - clear volume configuration\n");
80 	fprintf(stderr, "    create <type> [-v] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
81 	fprintf(stderr, "    delete <volume>\n");
82 	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
83 	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
84 	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
85 	fprintf(stderr, "    start patrol              - start a patrol read\n");
86 	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
87 	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
88 	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
89 	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
90 	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
91 	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
92 	fprintf(stderr, "    flash <firmware>\n");
93 	fprintf(stderr, "    start learn               - start a BBU relearn\n");
94 	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
95 #ifdef DEBUG
96 	fprintf(stderr, "    debug                     - debug 'show config'\n");
97 	fprintf(stderr, "    dump                      - display 'saved' config\n");
98 #endif
99 	exit(1);
100 }
101 
102 static int
103 version(int ac __unused, char **av __unused)
104 {
105 
106 	printf("mfiutil version 1.0.14");
107 #ifdef DEBUG
108 	printf(" (DEBUG)");
109 #endif
110 	printf("\n");
111 	return (0);
112 }
113 MFI_COMMAND(top, version, version);
114 
115 int
116 main(int ac, char **av)
117 {
118 	struct mfiutil_command **cmd;
119 	int ch;
120 
121 	while ((ch = getopt(ac, av, "deu:")) != -1) {
122 		switch (ch) {
123 		case 'd':
124 			mfi_opts |= MFI_DNAME_DEVICE_ID;
125 			break;
126 		case 'e':
127 			mfi_opts |= MFI_DNAME_ES;
128 			break;
129 		case 'u':
130 			mfi_unit = atoi(optarg);
131 			break;
132 		case '?':
133 			usage();
134 		}
135 	}
136 
137 	av += optind;
138 	ac -= optind;
139 
140 	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
141 	if (ac == 0)
142 		usage();
143 
144 	SET_FOREACH(cmd, MFI_DATASET(top)) {
145 		if (strcmp((*cmd)->name, av[0]) == 0) {
146 			if ((*cmd)->handler(ac, av))
147 				return (1);
148 			else
149 				return (0);
150 		}
151 	}
152 	warnx("Unknown command %s.", av[0]);
153 	return (1);
154 }
155 
156 void
157 scan_firmware(struct mfi_info_component *comp)
158 {
159 	int len;
160 
161 	len = strlen(comp->name);
162 	if (fw_name_width < len)
163 		fw_name_width = len;
164 	len = strlen(comp->version);
165 	if (fw_version_width < len)
166 		fw_version_width = len;
167 	len = strlen(comp->build_date);
168 	if (fw_date_width < len)
169 		fw_date_width = len;
170 	len = strlen(comp->build_time);
171 	if (fw_time_width < len)
172 		fw_time_width = len;
173 }
174 
175 void
176 display_firmware(struct mfi_info_component *comp, const char *tag)
177 {
178 
179 	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
180 	    fw_version_width, comp->version, fw_date_width, comp->build_date,
181 	    fw_time_width, comp->build_time, tag);
182 }
183