1196200Sscottl/*-
2196200Sscottl * Copyright (c) 2008, 2009 Yahoo!, Inc.
3196200Sscottl * All rights reserved.
4196200Sscottl *
5196200Sscottl * Redistribution and use in source and binary forms, with or without
6196200Sscottl * modification, are permitted provided that the following conditions
7196200Sscottl * are met:
8196200Sscottl * 1. Redistributions of source code must retain the above copyright
9196200Sscottl *    notice, this list of conditions and the following disclaimer.
10196200Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11196200Sscottl *    notice, this list of conditions and the following disclaimer in the
12196200Sscottl *    documentation and/or other materials provided with the distribution.
13196200Sscottl * 3. The names of the authors may not be used to endorse or promote
14196200Sscottl *    products derived from this software without specific prior written
15196200Sscottl *    permission.
16196200Sscottl *
17196200Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18196200Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19196200Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20196200Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21196200Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22196200Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23196200Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24196200Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25196200Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26196200Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27196200Sscottl * SUCH DAMAGE.
28196200Sscottl *
29196200Sscottl * $FreeBSD$
30196200Sscottl */
31196200Sscottl
32196200Sscottl#include <sys/errno.h>
33196200Sscottl#include <err.h>
34196200Sscottl#include <stdio.h>
35196200Sscottl#include <stdlib.h>
36196200Sscottl#include <string.h>
37196200Sscottl#include <unistd.h>
38196200Sscottl#include "mfiutil.h"
39196200Sscottl
40196200SscottlSET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
41196200Sscottl
42196200SscottlMFI_TABLE(top, start);
43196200SscottlMFI_TABLE(top, stop);
44196200SscottlMFI_TABLE(top, abort);
45196200Sscottl
46196200Sscottlint mfi_unit;
47196200Sscottl
48223345Sbzu_int mfi_opts;
49223345Sbz
50196200Sscottlstatic void
51196200Sscottlusage(void)
52196200Sscottl{
53196200Sscottl
54223345Sbz	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
55196200Sscottl	fprintf(stderr, "Commands include:\n");
56196200Sscottl	fprintf(stderr, "    version\n");
57196200Sscottl	fprintf(stderr, "    show adapter              - display controller information\n");
58196200Sscottl	fprintf(stderr, "    show battery              - display battery information\n");
59196200Sscottl	fprintf(stderr, "    show config               - display RAID configuration\n");
60196200Sscottl	fprintf(stderr, "    show drives               - list physical drives\n");
61196200Sscottl	fprintf(stderr, "    show events               - display event log\n");
62196200Sscottl	fprintf(stderr, "    show firmware             - list firmware images\n");
63253244Ssbruno	fprintf(stderr, "    show foreign              - display detected foreign volumes\n");
64223267Sbz	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
65196200Sscottl	fprintf(stderr, "    show volumes              - list logical volumes\n");
66196200Sscottl	fprintf(stderr, "    show patrol               - display patrol read status\n");
67221208Sjhb	fprintf(stderr, "    show progress             - display status of active operations\n");
68196200Sscottl	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
69255429Ssbruno	fprintf(stderr, "    good <drive>              - set a failed/SYSPD drive as UNCONFIGURED\n");
70196200Sscottl	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
71255429Ssbruno	fprintf(stderr, "    syspd <drive>             - set drive into use as SYSPD JBOD\n");
72196200Sscottl	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
73196200Sscottl	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
74196200Sscottl	fprintf(stderr, "    start rebuild <drive>\n");
75196200Sscottl	fprintf(stderr, "    abort rebuild <drive>\n");
76196200Sscottl	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
77196200Sscottl	fprintf(stderr, "    cache <volume> [command [setting]]\n");
78196200Sscottl	fprintf(stderr, "    name <volume> <name>\n");
79196200Sscottl	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
80196200Sscottl	fprintf(stderr, "    clear                     - clear volume configuration\n");
81196200Sscottl	fprintf(stderr, "    create <type> [-v] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
82196200Sscottl	fprintf(stderr, "    delete <volume>\n");
83196200Sscottl	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
84196200Sscottl	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
85196200Sscottl	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
86196200Sscottl	fprintf(stderr, "    start patrol              - start a patrol read\n");
87196200Sscottl	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
88253244Ssbruno	fprintf(stderr, "    foreign scan              - scan for foreign configurations\n");
89253244Ssbruno	fprintf(stderr, "    foreign clear [volume]    - clear foreign configurations (default all)\n");
90253244Ssbruno	fprintf(stderr, "    foreign diag [volume]     - diagnostic display foreign configurations (default all)\n");
91253244Ssbruno	fprintf(stderr, "    foreign preview [volume]  - preview foreign configurations (default all)\n");
92253244Ssbruno	fprintf(stderr, "    foreign import [volume]   - import foreign configurations (default all)\n");
93196200Sscottl	fprintf(stderr, "    flash <firmware>\n");
94250482Smarkj	fprintf(stderr, "    start learn               - start a BBU relearn\n");
95250482Smarkj	fprintf(stderr, "    bbu <setting> <value>     - set BBU properties\n");
96196200Sscottl#ifdef DEBUG
97196200Sscottl	fprintf(stderr, "    debug                     - debug 'show config'\n");
98196200Sscottl	fprintf(stderr, "    dump                      - display 'saved' config\n");
99196200Sscottl#endif
100196200Sscottl	exit(1);
101196200Sscottl}
102196200Sscottl
103196200Sscottlstatic int
104237589Seadlerversion(int ac __unused, char **av __unused)
105196200Sscottl{
106196200Sscottl
107255429Ssbruno	printf("mfiutil version 1.0.15");
108196200Sscottl#ifdef DEBUG
109196200Sscottl	printf(" (DEBUG)");
110196200Sscottl#endif
111196200Sscottl	printf("\n");
112196200Sscottl	return (0);
113196200Sscottl}
114196200SscottlMFI_COMMAND(top, version, version);
115196200Sscottl
116196200Sscottlint
117196200Sscottlmain(int ac, char **av)
118196200Sscottl{
119196200Sscottl	struct mfiutil_command **cmd;
120196200Sscottl	int ch;
121196200Sscottl
122223345Sbz	while ((ch = getopt(ac, av, "deu:")) != -1) {
123196200Sscottl		switch (ch) {
124223345Sbz		case 'd':
125223345Sbz			mfi_opts |= MFI_DNAME_DEVICE_ID;
126223345Sbz			break;
127223345Sbz		case 'e':
128223345Sbz			mfi_opts |= MFI_DNAME_ES;
129223345Sbz			break;
130196200Sscottl		case 'u':
131196200Sscottl			mfi_unit = atoi(optarg);
132196200Sscottl			break;
133196200Sscottl		case '?':
134196200Sscottl			usage();
135196200Sscottl		}
136196200Sscottl	}
137196200Sscottl
138196200Sscottl	av += optind;
139196200Sscottl	ac -= optind;
140196200Sscottl
141196200Sscottl	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
142196200Sscottl	if (ac == 0)
143196200Sscottl		usage();
144196200Sscottl
145196200Sscottl	SET_FOREACH(cmd, MFI_DATASET(top)) {
146196200Sscottl		if (strcmp((*cmd)->name, av[0]) == 0) {
147213672Srandi			if ((*cmd)->handler(ac, av))
148213672Srandi				return (1);
149213672Srandi			else
150213672Srandi				return (0);
151196200Sscottl		}
152196200Sscottl	}
153196200Sscottl	warnx("Unknown command %s.", av[0]);
154213672Srandi	return (1);
155196200Sscottl}
156