xref: /freebsd/usr.sbin/mfiutil/mfiutil.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1763fae79SScott Long /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni  *
4763fae79SScott Long  * Copyright (c) 2008, 2009 Yahoo!, Inc.
5763fae79SScott Long  * All rights reserved.
6763fae79SScott Long  *
7763fae79SScott Long  * Redistribution and use in source and binary forms, with or without
8763fae79SScott Long  * modification, are permitted provided that the following conditions
9763fae79SScott Long  * are met:
10763fae79SScott Long  * 1. Redistributions of source code must retain the above copyright
11763fae79SScott Long  *    notice, this list of conditions and the following disclaimer.
12763fae79SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
13763fae79SScott Long  *    notice, this list of conditions and the following disclaimer in the
14763fae79SScott Long  *    documentation and/or other materials provided with the distribution.
15763fae79SScott Long  * 3. The names of the authors may not be used to endorse or promote
16763fae79SScott Long  *    products derived from this software without specific prior written
17763fae79SScott Long  *    permission.
18763fae79SScott Long  *
19763fae79SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20763fae79SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21763fae79SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22763fae79SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23763fae79SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24763fae79SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25763fae79SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26763fae79SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27763fae79SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28763fae79SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29763fae79SScott Long  * SUCH DAMAGE.
30763fae79SScott Long  */
31763fae79SScott Long 
32*7e0f8b79SDoug Ambrisko #include <sys/param.h>
33*7e0f8b79SDoug Ambrisko 
34763fae79SScott Long #include <err.h>
35*7e0f8b79SDoug Ambrisko #include <errno.h>
36763fae79SScott Long #include <stdio.h>
37763fae79SScott Long #include <stdlib.h>
38763fae79SScott Long #include <string.h>
39763fae79SScott Long #include <unistd.h>
40*7e0f8b79SDoug Ambrisko #include <paths.h>
41763fae79SScott Long #include "mfiutil.h"
42763fae79SScott Long 
43763fae79SScott Long SET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
44763fae79SScott Long 
45763fae79SScott Long MFI_TABLE(top, start);
46763fae79SScott Long MFI_TABLE(top, stop);
47763fae79SScott Long MFI_TABLE(top, abort);
48763fae79SScott Long 
49*7e0f8b79SDoug Ambrisko char *mfi_device = NULL;
507bbae305SBjoern A. Zeeb u_int mfi_opts;
51186475e2SEd Schouten static int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
527bbae305SBjoern A. Zeeb 
53763fae79SScott Long static void
usage(void)54763fae79SScott Long usage(void)
55763fae79SScott Long {
56763fae79SScott Long 
57*7e0f8b79SDoug Ambrisko 	fprintf(stderr, "usage: %s [-de] [-D device] [-u unit] [-t type] <command> ...\n\n", getprogname());
58763fae79SScott Long 	fprintf(stderr, "Commands include:\n");
59763fae79SScott Long 	fprintf(stderr, "    version\n");
60763fae79SScott Long 	fprintf(stderr, "    show adapter              - display controller information\n");
61763fae79SScott Long 	fprintf(stderr, "    show battery              - display battery information\n");
62763fae79SScott Long 	fprintf(stderr, "    show config               - display RAID configuration\n");
63763fae79SScott Long 	fprintf(stderr, "    show drives               - list physical drives\n");
64763fae79SScott Long 	fprintf(stderr, "    show events               - display event log\n");
65763fae79SScott Long 	fprintf(stderr, "    show firmware             - list firmware images\n");
6606f1884fSSean Bruno 	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
6730d1616eSBjoern A. Zeeb 	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
68763fae79SScott Long 	fprintf(stderr, "    show volumes              - list logical volumes\n");
69763fae79SScott Long 	fprintf(stderr, "    show patrol               - display patrol read status\n");
7098be0dfeSJohn Baldwin 	fprintf(stderr, "    show progress             - display status of active operations\n");
71763fae79SScott Long 	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
727c2ad1eeSSean Bruno 	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
73763fae79SScott Long 	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
747c2ad1eeSSean Bruno 	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
75763fae79SScott Long 	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
76763fae79SScott Long 	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
77763fae79SScott Long 	fprintf(stderr, "    start rebuild <drive>\n");
78763fae79SScott Long 	fprintf(stderr, "    abort rebuild <drive>\n");
79763fae79SScott Long 	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
80763fae79SScott Long 	fprintf(stderr, "    cache <volume> [command [setting]]\n");
81763fae79SScott Long 	fprintf(stderr, "    name <volume> <name>\n");
82763fae79SScott Long 	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
83763fae79SScott Long 	fprintf(stderr, "    clear                     - clear volume configuration\n");
8443a3230aSSean Bruno 	fprintf(stderr, "    create <type> [-v] [-s stripe_size] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
85763fae79SScott Long 	fprintf(stderr, "    delete <volume>\n");
86763fae79SScott Long 	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
87763fae79SScott Long 	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
88763fae79SScott Long 	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
89763fae79SScott Long 	fprintf(stderr, "    start patrol              - start a patrol read\n");
90763fae79SScott Long 	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
9106f1884fSSean Bruno 	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
9206f1884fSSean Bruno 	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
9306f1884fSSean Bruno 	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
9406f1884fSSean Bruno 	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
9506f1884fSSean Bruno 	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
96763fae79SScott Long 	fprintf(stderr, "    flash <firmware>\n");
97dee3e845SMark Johnston 	fprintf(stderr, "    start learn               - start a BBU relearn\n");
98dee3e845SMark Johnston 	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
99ee7a2acaSSean Bruno 	fprintf(stderr, "    ctrlprop rebuild [rate]   - get/set the volume rebuild rate\n");
100ee7a2acaSSean Bruno 	fprintf(stderr, "    ctrlprop alarm [0/1]      - enable/disable controller alarms\n");
101763fae79SScott Long #ifdef DEBUG
102763fae79SScott Long 	fprintf(stderr, "    debug                     - debug 'show config'\n");
103763fae79SScott Long 	fprintf(stderr, "    dump                      - display 'saved' config\n");
104763fae79SScott Long #endif
105763fae79SScott Long 	exit(1);
106763fae79SScott Long }
107763fae79SScott Long 
108763fae79SScott Long static int
version(int ac __unused,char ** av __unused)10941b8cbdaSEitan Adler version(int ac __unused, char **av __unused)
110763fae79SScott Long {
111763fae79SScott Long 
1127c2ad1eeSSean Bruno 	printf("mfiutil version 1.0.15");
113763fae79SScott Long #ifdef DEBUG
114763fae79SScott Long 	printf(" (DEBUG)");
115763fae79SScott Long #endif
116763fae79SScott Long 	printf("\n");
117763fae79SScott Long 	return (0);
118763fae79SScott Long }
119763fae79SScott Long MFI_COMMAND(top, version, version);
120763fae79SScott Long 
121763fae79SScott Long int
main(int ac,char ** av)122763fae79SScott Long main(int ac, char **av)
123763fae79SScott Long {
124763fae79SScott Long 	struct mfiutil_command **cmd;
125*7e0f8b79SDoug Ambrisko 	int ch, mfi_unit;
126*7e0f8b79SDoug Ambrisko 	const char *pn, *mfi_type;
127*7e0f8b79SDoug Ambrisko 	char *temp;
128763fae79SScott Long 
129*7e0f8b79SDoug Ambrisko 	mfi_unit = 0;
130*7e0f8b79SDoug Ambrisko 
131*7e0f8b79SDoug Ambrisko 	pn = getprogname();
132*7e0f8b79SDoug Ambrisko 
133*7e0f8b79SDoug Ambrisko 	if (strcmp(pn, "mrsasutil") == 0)
134*7e0f8b79SDoug Ambrisko 		mfi_type = MRSAS_TYPE;
135*7e0f8b79SDoug Ambrisko 	else
136*7e0f8b79SDoug Ambrisko 		mfi_type = MFI_TYPE;
137*7e0f8b79SDoug Ambrisko 
138*7e0f8b79SDoug Ambrisko 	while ((ch = getopt(ac, av, "D:det:u:")) != -1) {
139763fae79SScott Long 		switch (ch) {
140*7e0f8b79SDoug Ambrisko 		case 'D':
141*7e0f8b79SDoug Ambrisko 			mfi_device = optarg;
142*7e0f8b79SDoug Ambrisko 			break;
1437bbae305SBjoern A. Zeeb 		case 'd':
1447bbae305SBjoern A. Zeeb 			mfi_opts |= MFI_DNAME_DEVICE_ID;
1457bbae305SBjoern A. Zeeb 			break;
1467bbae305SBjoern A. Zeeb 		case 'e':
1477bbae305SBjoern A. Zeeb 			mfi_opts |= MFI_DNAME_ES;
1487bbae305SBjoern A. Zeeb 			break;
149763fae79SScott Long 		case 'u':
150763fae79SScott Long 			mfi_unit = atoi(optarg);
151763fae79SScott Long 			break;
152*7e0f8b79SDoug Ambrisko 		case 't':
153*7e0f8b79SDoug Ambrisko 			mfi_type = optarg;
154*7e0f8b79SDoug Ambrisko 			break;
155763fae79SScott Long 		case '?':
156763fae79SScott Long 			usage();
157763fae79SScott Long 		}
158763fae79SScott Long 	}
159763fae79SScott Long 
160*7e0f8b79SDoug Ambrisko 	if (mfi_device == NULL) {
161*7e0f8b79SDoug Ambrisko 		if (asprintf(&mfi_device, "%s%s%d", _PATH_DEV, mfi_type,
162*7e0f8b79SDoug Ambrisko 		    mfi_unit) < 0)
163*7e0f8b79SDoug Ambrisko 			errx(1, "Can't allocate memory for device name\n");
164*7e0f8b79SDoug Ambrisko 	} else {
165*7e0f8b79SDoug Ambrisko 		if (strncmp(mfi_device, _PATH_DEV, strlen(_PATH_DEV)) != 0) {
166*7e0f8b79SDoug Ambrisko 			if (asprintf(&temp, "%s%s%d", _PATH_DEV, mfi_type,
167*7e0f8b79SDoug Ambrisko 			    mfi_unit) < 0)
168*7e0f8b79SDoug Ambrisko 				errx(1, "Can't allocate memory for device name\n");
169*7e0f8b79SDoug Ambrisko 			mfi_device = temp;
170*7e0f8b79SDoug Ambrisko 		}
171*7e0f8b79SDoug Ambrisko 	}
172*7e0f8b79SDoug Ambrisko 
173763fae79SScott Long 	av += optind;
174763fae79SScott Long 	ac -= optind;
175763fae79SScott Long 
176763fae79SScott Long 	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
177763fae79SScott Long 	if (ac == 0)
178763fae79SScott Long 		usage();
179763fae79SScott Long 
180763fae79SScott Long 	SET_FOREACH(cmd, MFI_DATASET(top)) {
181763fae79SScott Long 		if (strcmp((*cmd)->name, av[0]) == 0) {
1824ee8bfc5SRandi Harper 			if ((*cmd)->handler(ac, av))
1834ee8bfc5SRandi Harper 				return (1);
1844ee8bfc5SRandi Harper 			else
185763fae79SScott Long 				return (0);
186763fae79SScott Long 		}
187763fae79SScott Long 	}
188763fae79SScott Long 	warnx("Unknown command %s.", av[0]);
1894ee8bfc5SRandi Harper 	return (1);
190763fae79SScott Long }
191186475e2SEd Schouten 
192186475e2SEd Schouten void
scan_firmware(struct mfi_info_component * comp)193186475e2SEd Schouten scan_firmware(struct mfi_info_component *comp)
194186475e2SEd Schouten {
195186475e2SEd Schouten 	int len;
196186475e2SEd Schouten 
197186475e2SEd Schouten 	len = strlen(comp->name);
198186475e2SEd Schouten 	if (fw_name_width < len)
199186475e2SEd Schouten 		fw_name_width = len;
200186475e2SEd Schouten 	len = strlen(comp->version);
201186475e2SEd Schouten 	if (fw_version_width < len)
202186475e2SEd Schouten 		fw_version_width = len;
203186475e2SEd Schouten 	len = strlen(comp->build_date);
204186475e2SEd Schouten 	if (fw_date_width < len)
205186475e2SEd Schouten 		fw_date_width = len;
206186475e2SEd Schouten 	len = strlen(comp->build_time);
207186475e2SEd Schouten 	if (fw_time_width < len)
208186475e2SEd Schouten 		fw_time_width = len;
209186475e2SEd Schouten }
210186475e2SEd Schouten 
211186475e2SEd Schouten void
display_firmware(struct mfi_info_component * comp,const char * tag)212186475e2SEd Schouten display_firmware(struct mfi_info_component *comp, const char *tag)
213186475e2SEd Schouten {
214186475e2SEd Schouten 
215186475e2SEd Schouten 	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
216186475e2SEd Schouten 	    fw_version_width, comp->version, fw_date_width, comp->build_date,
217186475e2SEd Schouten 	    fw_time_width, comp->build_time, tag);
218186475e2SEd Schouten }
219