xref: /netbsd/usr.sbin/mlxctl/main.c (revision c4a72b64)
1 /*	$NetBSD: main.c,v 1.5 2002/08/26 17:04: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.5 2002/08/26 17:04: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 struct	mlx_cinfo ci;
65 int	mlxfd = -1;
66 int	verbosity;
67 
68 struct cmd {
69 	const char	*label;
70 	int	flags;
71 	int	(*func)(char **);
72 };
73 #define	CMD_DISKS	0x01
74 #define	CMD_NOARGS	0x02
75 
76 static const struct cmd cmdtab[] = {
77 	{ "check",	CMD_DISKS,	cmd_check },
78 	{ "config",	CMD_NOARGS,	cmd_config },
79 	{ "cstatus",	CMD_NOARGS,	cmd_cstatus },
80 	{ "detach",	CMD_DISKS,	cmd_detach },
81 	{ "rebuild",	0,		cmd_rebuild },
82 	{ "rescan",	CMD_NOARGS,	cmd_rescan },
83 	{ "status",	CMD_DISKS,	cmd_status },
84 };
85 
86 int
87 main(int argc, char **argv)
88 {
89 	const struct cmd *cmd, *maxcmd;
90 	const char *cmdname, *dv;
91 	int ch, i, rv, all;
92 
93 	all = 0;
94 	dv = "/dev/mlx0";
95 	mlx_disk_init();
96 
97 	while ((ch = getopt(argc, argv, "af:v")) != -1) {
98 		switch (ch) {
99 		case 'a':
100 			all = 1;
101 			break;
102 
103 		case 'f':
104 			dv = optarg;
105 			break;
106 
107 		case 'v':
108 			verbosity++;
109 			break;
110 
111 		default:
112 			usage();
113 			/* NOTREACHED */
114 		}
115 	}
116 
117 	if ((cmdname = argv[optind++]) == NULL)
118 		usage();
119 
120 	for (i = 0; dv[i] != '\0'; i++)
121 		if (dv[i] == '/')
122 			mlxname = &dv[i + 1];
123 
124 	if ((mlxfd = open(dv, O_RDWR)) < 0)
125 		err(EXIT_FAILURE, "%s", dv);
126 
127 	cmd = &cmdtab[0];
128 	maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])];
129 	while (cmd < maxcmd) {
130 		if (strcmp(cmdname, cmd->label) == 0)
131 			break;
132 		cmd++;
133 	}
134 	if (cmd == maxcmd)
135 		usage();
136 
137 	if (ioctl(mlxfd, MLX_GET_CINFO, &ci))
138 		err(EXIT_FAILURE, "ioctl(MLX_GET_CINFO)");
139 
140 	if ((cmd->flags & CMD_DISKS) != 0) {
141 		if (all)
142 			mlx_disk_add_all();
143 
144 		while (argv[optind] != NULL)
145 			mlx_disk_add(argv[optind++]);
146 		if (mlx_disk_empty())
147 			errx(EXIT_FAILURE,
148 			    "no logical drives attached to this controller");
149 	} else if ((cmd->flags & CMD_NOARGS) != 0)
150 		if (argv[optind] != NULL)
151 			usage();
152 
153 	rv = (*cmd->func)(&argv[optind]);
154 	close(mlxfd);
155 	exit(rv);
156 	/* NOTREACHED */
157 }
158 
159 void
160 usage(void)
161 {
162 
163 	(void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n",
164 	    getprogname());
165 	exit(EXIT_FAILURE);
166 	/* NOTREACHED */
167 }
168