xref: /netbsd/usr.sbin/mlxctl/main.c (revision bf9ec67e)
1 /*	$NetBSD: main.c,v 1.4 2002/04/07 20:02:18 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: main.c,v 1.4 2002/04/07 20:02:18 ad Exp $");
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/queue.h>
47 
48 #include <dev/ic/mlxreg.h>
49 #include <dev/ic/mlxio.h>
50 
51 #include <err.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <unistd.h>
58 
59 #include "extern.h"
60 
61 const char	*mlxname;
62 const char	*memf;
63 const char	*nlistf;
64 int	mlxfd = -1;
65 int	verbosity;
66 
67 struct cmd {
68 	const char	*label;
69 	int	flags;
70 	int	(*func)(char **);
71 };
72 #define	CMD_DISKS	0x01
73 #define	CMD_NOARGS	0x02
74 
75 static const struct cmd cmdtab[] = {
76 	{ "check",	CMD_DISKS,	cmd_check },
77 	{ "config",	CMD_NOARGS,	cmd_config },
78 	{ "cstatus",	CMD_NOARGS,	cmd_cstatus },
79 	{ "detach",	CMD_DISKS,	cmd_detach },
80 	{ "rebuild",	0,		cmd_rebuild },
81 	{ "rescan",	CMD_NOARGS,	cmd_rescan },
82 	{ "status",	CMD_DISKS,	cmd_status },
83 };
84 
85 int
86 main(int argc, char **argv)
87 {
88 	const struct cmd *cmd, *maxcmd;
89 	const char *cmdname, *dv;
90 	int ch, i, rv, all;
91 
92 	all = 0;
93 	dv = "/dev/mlx0";
94 	mlx_disk_init();
95 
96 	while ((ch = getopt(argc, argv, "af:v")) != -1) {
97 		switch (ch) {
98 		case 'a':
99 			all = 1;
100 			break;
101 
102 		case 'f':
103 			dv = optarg;
104 			break;
105 
106 		case 'v':
107 			verbosity++;
108 			break;
109 
110 		default:
111 			usage();
112 			/* NOTREACHED */
113 		}
114 	}
115 
116 	if ((cmdname = argv[optind++]) == NULL)
117 		usage();
118 
119 	for (i = 0; dv[i] != '\0'; i++)
120 		if (dv[i] == '/')
121 			mlxname = &dv[i + 1];
122 
123 	if ((mlxfd = open(dv, O_RDWR)) < 0)
124 		err(EXIT_FAILURE, "%s", dv);
125 
126 	cmd = &cmdtab[0];
127 	maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])];
128 	while (cmd < maxcmd) {
129 		if (strcmp(cmdname, cmd->label) == 0)
130 			break;
131 		cmd++;
132 	}
133 	if (cmd == maxcmd)
134 		usage();
135 
136 	if ((cmd->flags & CMD_DISKS) != 0) {
137 		if (all)
138 			mlx_disk_add_all();
139 
140 		while (argv[optind] != NULL)
141 			mlx_disk_add(argv[optind++]);
142 		if (mlx_disk_empty())
143 			errx(EXIT_FAILURE,
144 			    "no logical drives attached to this controller");
145 	} else if ((cmd->flags & CMD_NOARGS) != 0)
146 		if (argv[optind] != NULL)
147 			usage();
148 
149 	rv = (*cmd->func)(&argv[optind]);
150 	close(mlxfd);
151 	exit(rv);
152 	/* NOTREACHED */
153 }
154 
155 void
156 usage(void)
157 {
158 
159 	(void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n",
160 	    getprogname());
161 	exit(EXIT_FAILURE);
162 	/* NOTREACHED */
163 }
164