xref: /freebsd/usr.sbin/mfiutil/mfiutil.c (revision 7c2ad1ee5c39a4b200a8f6f509526054149b570e)
1763fae79SScott Long /*-
2763fae79SScott Long  * Copyright (c) 2008, 2009 Yahoo!, Inc.
3763fae79SScott Long  * All rights reserved.
4763fae79SScott Long  *
5763fae79SScott Long  * Redistribution and use in source and binary forms, with or without
6763fae79SScott Long  * modification, are permitted provided that the following conditions
7763fae79SScott Long  * are met:
8763fae79SScott Long  * 1. Redistributions of source code must retain the above copyright
9763fae79SScott Long  *    notice, this list of conditions and the following disclaimer.
10763fae79SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
11763fae79SScott Long  *    notice, this list of conditions and the following disclaimer in the
12763fae79SScott Long  *    documentation and/or other materials provided with the distribution.
13763fae79SScott Long  * 3. The names of the authors may not be used to endorse or promote
14763fae79SScott Long  *    products derived from this software without specific prior written
15763fae79SScott Long  *    permission.
16763fae79SScott Long  *
17763fae79SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18763fae79SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19763fae79SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20763fae79SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21763fae79SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22763fae79SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23763fae79SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24763fae79SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25763fae79SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26763fae79SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27763fae79SScott Long  * SUCH DAMAGE.
28763fae79SScott Long  *
29763fae79SScott Long  * $FreeBSD$
30763fae79SScott Long  */
31763fae79SScott Long 
32763fae79SScott Long #include <sys/errno.h>
33763fae79SScott Long #include <err.h>
34763fae79SScott Long #include <stdio.h>
35763fae79SScott Long #include <stdlib.h>
36763fae79SScott Long #include <string.h>
37763fae79SScott Long #include <unistd.h>
38763fae79SScott Long #include "mfiutil.h"
39763fae79SScott Long 
40763fae79SScott Long SET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
41763fae79SScott Long 
42763fae79SScott Long MFI_TABLE(top, start);
43763fae79SScott Long MFI_TABLE(top, stop);
44763fae79SScott Long MFI_TABLE(top, abort);
45763fae79SScott Long 
46763fae79SScott Long int mfi_unit;
477bbae305SBjoern A. Zeeb u_int mfi_opts;
48186475e2SEd Schouten static int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
497bbae305SBjoern A. Zeeb 
50763fae79SScott Long static void
51763fae79SScott Long usage(void)
52763fae79SScott Long {
53763fae79SScott Long 
547bbae305SBjoern A. Zeeb 	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
55763fae79SScott Long 	fprintf(stderr, "Commands include:\n");
56763fae79SScott Long 	fprintf(stderr, "    version\n");
57763fae79SScott Long 	fprintf(stderr, "    show adapter              - display controller information\n");
58763fae79SScott Long 	fprintf(stderr, "    show battery              - display battery information\n");
59763fae79SScott Long 	fprintf(stderr, "    show config               - display RAID configuration\n");
60763fae79SScott Long 	fprintf(stderr, "    show drives               - list physical drives\n");
61763fae79SScott Long 	fprintf(stderr, "    show events               - display event log\n");
62763fae79SScott Long 	fprintf(stderr, "    show firmware             - list firmware images\n");
6306f1884fSSean Bruno 	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
6430d1616eSBjoern A. Zeeb 	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
65763fae79SScott Long 	fprintf(stderr, "    show volumes              - list logical volumes\n");
66763fae79SScott Long 	fprintf(stderr, "    show patrol               - display patrol read status\n");
6798be0dfeSJohn Baldwin 	fprintf(stderr, "    show progress             - display status of active operations\n");
68763fae79SScott Long 	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
69*7c2ad1eeSSean Bruno 	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
70763fae79SScott Long 	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
71*7c2ad1eeSSean Bruno 	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
72763fae79SScott Long 	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
73763fae79SScott Long 	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
74763fae79SScott Long 	fprintf(stderr, "    start rebuild <drive>\n");
75763fae79SScott Long 	fprintf(stderr, "    abort rebuild <drive>\n");
76763fae79SScott Long 	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
77763fae79SScott Long 	fprintf(stderr, "    cache <volume> [command [setting]]\n");
78763fae79SScott Long 	fprintf(stderr, "    name <volume> <name>\n");
79763fae79SScott Long 	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
80763fae79SScott Long 	fprintf(stderr, "    clear                     - clear volume configuration\n");
81763fae79SScott Long 	fprintf(stderr, "    create <type> [-v] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
82763fae79SScott Long 	fprintf(stderr, "    delete <volume>\n");
83763fae79SScott Long 	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
84763fae79SScott Long 	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
85763fae79SScott Long 	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
86763fae79SScott Long 	fprintf(stderr, "    start patrol              - start a patrol read\n");
87763fae79SScott Long 	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
8806f1884fSSean Bruno 	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
8906f1884fSSean Bruno 	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
9006f1884fSSean Bruno 	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
9106f1884fSSean Bruno 	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
9206f1884fSSean Bruno 	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
93763fae79SScott Long 	fprintf(stderr, "    flash <firmware>\n");
94dee3e845SMark Johnston 	fprintf(stderr, "    start learn               - start a BBU relearn\n");
95dee3e845SMark Johnston 	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
96763fae79SScott Long #ifdef DEBUG
97763fae79SScott Long 	fprintf(stderr, "    debug                     - debug 'show config'\n");
98763fae79SScott Long 	fprintf(stderr, "    dump                      - display 'saved' config\n");
99763fae79SScott Long #endif
100763fae79SScott Long 	exit(1);
101763fae79SScott Long }
102763fae79SScott Long 
103763fae79SScott Long static int
10441b8cbdaSEitan Adler version(int ac __unused, char **av __unused)
105763fae79SScott Long {
106763fae79SScott Long 
107*7c2ad1eeSSean Bruno 	printf("mfiutil version 1.0.15");
108763fae79SScott Long #ifdef DEBUG
109763fae79SScott Long 	printf(" (DEBUG)");
110763fae79SScott Long #endif
111763fae79SScott Long 	printf("\n");
112763fae79SScott Long 	return (0);
113763fae79SScott Long }
114763fae79SScott Long MFI_COMMAND(top, version, version);
115763fae79SScott Long 
116763fae79SScott Long int
117763fae79SScott Long main(int ac, char **av)
118763fae79SScott Long {
119763fae79SScott Long 	struct mfiutil_command **cmd;
120763fae79SScott Long 	int ch;
121763fae79SScott Long 
1227bbae305SBjoern A. Zeeb 	while ((ch = getopt(ac, av, "deu:")) != -1) {
123763fae79SScott Long 		switch (ch) {
1247bbae305SBjoern A. Zeeb 		case 'd':
1257bbae305SBjoern A. Zeeb 			mfi_opts |= MFI_DNAME_DEVICE_ID;
1267bbae305SBjoern A. Zeeb 			break;
1277bbae305SBjoern A. Zeeb 		case 'e':
1287bbae305SBjoern A. Zeeb 			mfi_opts |= MFI_DNAME_ES;
1297bbae305SBjoern A. Zeeb 			break;
130763fae79SScott Long 		case 'u':
131763fae79SScott Long 			mfi_unit = atoi(optarg);
132763fae79SScott Long 			break;
133763fae79SScott Long 		case '?':
134763fae79SScott Long 			usage();
135763fae79SScott Long 		}
136763fae79SScott Long 	}
137763fae79SScott Long 
138763fae79SScott Long 	av += optind;
139763fae79SScott Long 	ac -= optind;
140763fae79SScott Long 
141763fae79SScott Long 	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
142763fae79SScott Long 	if (ac == 0)
143763fae79SScott Long 		usage();
144763fae79SScott Long 
145763fae79SScott Long 	SET_FOREACH(cmd, MFI_DATASET(top)) {
146763fae79SScott Long 		if (strcmp((*cmd)->name, av[0]) == 0) {
1474ee8bfc5SRandi Harper 			if ((*cmd)->handler(ac, av))
1484ee8bfc5SRandi Harper 				return (1);
1494ee8bfc5SRandi Harper 			else
150763fae79SScott Long 				return (0);
151763fae79SScott Long 		}
152763fae79SScott Long 	}
153763fae79SScott Long 	warnx("Unknown command %s.", av[0]);
1544ee8bfc5SRandi Harper 	return (1);
155763fae79SScott Long }
156186475e2SEd Schouten 
157186475e2SEd Schouten void
158186475e2SEd Schouten scan_firmware(struct mfi_info_component *comp)
159186475e2SEd Schouten {
160186475e2SEd Schouten 	int len;
161186475e2SEd Schouten 
162186475e2SEd Schouten 	len = strlen(comp->name);
163186475e2SEd Schouten 	if (fw_name_width < len)
164186475e2SEd Schouten 		fw_name_width = len;
165186475e2SEd Schouten 	len = strlen(comp->version);
166186475e2SEd Schouten 	if (fw_version_width < len)
167186475e2SEd Schouten 		fw_version_width = len;
168186475e2SEd Schouten 	len = strlen(comp->build_date);
169186475e2SEd Schouten 	if (fw_date_width < len)
170186475e2SEd Schouten 		fw_date_width = len;
171186475e2SEd Schouten 	len = strlen(comp->build_time);
172186475e2SEd Schouten 	if (fw_time_width < len)
173186475e2SEd Schouten 		fw_time_width = len;
174186475e2SEd Schouten }
175186475e2SEd Schouten 
176186475e2SEd Schouten void
177186475e2SEd Schouten display_firmware(struct mfi_info_component *comp, const char *tag)
178186475e2SEd Schouten {
179186475e2SEd Schouten 
180186475e2SEd Schouten 	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
181186475e2SEd Schouten 	    fw_version_width, comp->version, fw_date_width, comp->build_date,
182186475e2SEd Schouten 	    fw_time_width, comp->build_time, tag);
183186475e2SEd Schouten }
184