mfiutil.c revision 254906
1184610Salfred/*-
2184610Salfred * Copyright (c) 2008, 2009 Yahoo!, Inc.
3184610Salfred * All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred * 3. The names of the authors may not be used to endorse or promote
14184610Salfred *    products derived from this software without specific prior written
15184610Salfred *    permission.
16184610Salfred *
17184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27184610Salfred * SUCH DAMAGE.
28184610Salfred *
29184610Salfred * $FreeBSD: head/usr.sbin/mfiutil/mfiutil.c 254906 2013-08-26 12:05:38Z sbruno $
30184610Salfred */
31184610Salfred
32184610Salfred#include <sys/errno.h>
33184610Salfred#include <err.h>
34184610Salfred#include <stdio.h>
35184610Salfred#include <stdlib.h>
36184610Salfred#include <string.h>
37184610Salfred#include <unistd.h>
38184610Salfred#include "mfiutil.h"
39184610Salfred
40184610SalfredSET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
41184610Salfred
42184610SalfredMFI_TABLE(top, start);
43184610SalfredMFI_TABLE(top, stop);
44194677SthompsaMFI_TABLE(top, abort);
45194677Sthompsa
46194677Sthompsaint mfi_unit;
47194677Sthompsau_int mfi_opts;
48194677Sthompsastatic int fw_name_width, fw_version_width, fw_date_width, fw_time_width;
49194677Sthompsa
50194677Sthompsastatic void
51194677Sthompsausage(void)
52194677Sthompsa{
53194677Sthompsa
54194677Sthompsa	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
55194677Sthompsa	fprintf(stderr, "Commands include:\n");
56194677Sthompsa	fprintf(stderr, "    version\n");
57194677Sthompsa	fprintf(stderr, "    show adapter              - display controller information\n");
58194677Sthompsa	fprintf(stderr, "    show battery              - display battery information\n");
59194677Sthompsa	fprintf(stderr, "    show config               - display RAID configuration\n");
60194677Sthompsa	fprintf(stderr, "    show drives               - list physical drives\n");
61194677Sthompsa	fprintf(stderr, "    show events               - display event log\n");
62223989Shselasky	fprintf(stderr, "    show firmware             - list firmware images\n");
63223989Shselasky	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
64198152Sthompsa	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
65194677Sthompsa	fprintf(stderr, "    show volumes              - list logical volumes\n");
66188942Sthompsa	fprintf(stderr, "    show patrol               - display patrol read status\n");
67194677Sthompsa	fprintf(stderr, "    show progress             - display status of active operations\n");
68194677Sthompsa	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
69188942Sthompsa	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
70184610Salfred	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
71184610Salfred	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
72188942Sthompsa	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
73184610Salfred	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
74188942Sthompsa	fprintf(stderr, "    start rebuild <drive>\n");
75184610Salfred	fprintf(stderr, "    abort rebuild <drive>\n");
76184610Salfred	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
77184610Salfred	fprintf(stderr, "    cache <volume> [command [setting]]\n");
78184610Salfred	fprintf(stderr, "    name <volume> <name>\n");
79184610Salfred	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
80184610Salfred	fprintf(stderr, "    clear                     - clear volume configuration\n");
81184610Salfred	fprintf(stderr, "    create <type> [-v] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
82184610Salfred	fprintf(stderr, "    delete <volume>\n");
83184610Salfred	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
84184610Salfred	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
85184610Salfred	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
86184610Salfred	fprintf(stderr, "    start patrol              - start a patrol read\n");
87184610Salfred	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
88184610Salfred	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
89184610Salfred	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
90184610Salfred	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
91184610Salfred	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
92207077Sthompsa	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
93184610Salfred	fprintf(stderr, "    flash <firmware>\n");
94196489Salfred	fprintf(stderr, "    start learn               - start a BBU relearn\n");
95184610Salfred	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
96248085Smarius#ifdef DEBUG
97242775Shselasky	fprintf(stderr, "    debug                     - debug 'show config'\n");
98184610Salfred	fprintf(stderr, "    dump                      - display 'saved' config\n");
99242775Shselasky#endif
100242775Shselasky	exit(1);
101196489Salfred}
102199675Sthompsa
103184610Salfredstatic int
104184610Salfredversion(int ac __unused, char **av __unused)
105184610Salfred{
106184610Salfred
107184610Salfred	printf("mfiutil version 1.0.15");
108184610Salfred#ifdef DEBUG
109184610Salfred	printf(" (DEBUG)");
110184610Salfred#endif
111184610Salfred	printf("\n");
112223755Shselasky	return (0);
113184610Salfred}
114184610SalfredMFI_COMMAND(top, version, version);
115223755Shselasky
116184610Salfredint
117184610Salfredmain(int ac, char **av)
118184610Salfred{
119184610Salfred	struct mfiutil_command **cmd;
120184610Salfred	int ch;
121184610Salfred
122184610Salfred	while ((ch = getopt(ac, av, "deu:")) != -1) {
123184610Salfred		switch (ch) {
124223755Shselasky		case 'd':
125223755Shselasky			mfi_opts |= MFI_DNAME_DEVICE_ID;
126223755Shselasky			break;
127184610Salfred		case 'e':
128192925Sthompsa			mfi_opts |= MFI_DNAME_ES;
129184610Salfred			break;
130187259Sthompsa		case 'u':
131187259Sthompsa			mfi_unit = atoi(optarg);
132187259Sthompsa			break;
133192925Sthompsa		case '?':
134187259Sthompsa			usage();
135187259Sthompsa		}
136184610Salfred	}
137184610Salfred
138184610Salfred	av += optind;
139184610Salfred	ac -= optind;
140184610Salfred
141192925Sthompsa	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
142192925Sthompsa	if (ac == 0)
143223755Shselasky		usage();
144223755Shselasky
145223755Shselasky	SET_FOREACH(cmd, MFI_DATASET(top)) {
146223755Shselasky		if (strcmp((*cmd)->name, av[0]) == 0) {
147223755Shselasky			if ((*cmd)->handler(ac, av))
148223755Shselasky				return (1);
149223755Shselasky			else
150223755Shselasky				return (0);
151223755Shselasky		}
152223755Shselasky	}
153223755Shselasky	warnx("Unknown command %s.", av[0]);
154223755Shselasky	return (1);
155192984Sthompsa}
156184610Salfred
157184610Salfredvoid
158184610Salfredscan_firmware(struct mfi_info_component *comp)
159203896Sthompsa{
160192984Sthompsa	int len;
161192984Sthompsa
162192984Sthompsa	len = strlen(comp->name);
163184610Salfred	if (fw_name_width < len)
164184610Salfred		fw_name_width = len;
165184610Salfred	len = strlen(comp->version);
166184610Salfred	if (fw_version_width < len)
167184610Salfred		fw_version_width = len;
168184610Salfred	len = strlen(comp->build_date);
169184610Salfred	if (fw_date_width < len)
170184610Salfred		fw_date_width = len;
171184610Salfred	len = strlen(comp->build_time);
172184610Salfred	if (fw_time_width < len)
173223755Shselasky		fw_time_width = len;
174223755Shselasky}
175223755Shselasky
176223755Shselaskyvoid
177223755Shselaskydisplay_firmware(struct mfi_info_component *comp, const char *tag)
178184610Salfred{
179223755Shselasky
180223755Shselasky	printf("%-*s  %-*s  %-*s  %-*s  %s\n", fw_name_width, comp->name,
181223755Shselasky	    fw_version_width, comp->version, fw_date_width, comp->build_date,
182223755Shselasky	    fw_time_width, comp->build_time, tag);
183223755Shselasky}
184223755Shselasky