xref: /netbsd-src/usr.sbin/mlxctl/main.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: main.c,v 1.7 2008/04/28 20:24:17 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: main.c,v 1.7 2008/04/28 20:24:17 martin Exp $");
35 #endif /* not lint */
36 
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/queue.h>
40 
41 #include <dev/ic/mlxreg.h>
42 #include <dev/ic/mlxio.h>
43 
44 #include <err.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "extern.h"
52 
53 const char	*mlxname;
54 const char	*memf;
55 const char	*nlistf;
56 struct	mlx_cinfo ci;
57 int	mlxfd = -1;
58 int	verbosity;
59 
60 struct cmd {
61 	const char	*label;
62 	int	flags;
63 	int	(*func)(char **);
64 };
65 #define	CMD_DISKS	0x01
66 #define	CMD_NOARGS	0x02
67 
68 static const struct cmd cmdtab[] = {
69 	{ "check",	CMD_DISKS,	cmd_check },
70 	{ "config",	CMD_NOARGS,	cmd_config },
71 	{ "cstatus",	CMD_NOARGS,	cmd_cstatus },
72 	{ "detach",	CMD_DISKS,	cmd_detach },
73 	{ "rebuild",	0,		cmd_rebuild },
74 	{ "rescan",	CMD_NOARGS,	cmd_rescan },
75 	{ "status",	CMD_DISKS,	cmd_status },
76 };
77 
78 int
main(int argc,char ** argv)79 main(int argc, char **argv)
80 {
81 	const struct cmd *cmd, *maxcmd;
82 	const char *cmdname, *dv;
83 	int ch, i, rv, all;
84 
85 	all = 0;
86 	dv = "/dev/mlx0";
87 	mlx_disk_init();
88 
89 	while ((ch = getopt(argc, argv, "af:v")) != -1) {
90 		switch (ch) {
91 		case 'a':
92 			all = 1;
93 			break;
94 
95 		case 'f':
96 			dv = optarg;
97 			break;
98 
99 		case 'v':
100 			verbosity++;
101 			break;
102 
103 		default:
104 			usage();
105 			/* NOTREACHED */
106 		}
107 	}
108 
109 	if ((cmdname = argv[optind++]) == NULL)
110 		usage();
111 
112 	for (i = 0; dv[i] != '\0'; i++)
113 		if (dv[i] == '/')
114 			mlxname = &dv[i + 1];
115 
116 	if ((mlxfd = open(dv, O_RDWR)) < 0)
117 		err(EXIT_FAILURE, "%s", dv);
118 
119 	cmd = &cmdtab[0];
120 	maxcmd = &cmdtab[sizeof(cmdtab) / sizeof(cmdtab[0])];
121 	while (cmd < maxcmd) {
122 		if (strcmp(cmdname, cmd->label) == 0)
123 			break;
124 		cmd++;
125 	}
126 	if (cmd == maxcmd)
127 		usage();
128 
129 	if (ioctl(mlxfd, MLX_GET_CINFO, &ci))
130 		err(EXIT_FAILURE, "ioctl(MLX_GET_CINFO)");
131 
132 	if ((cmd->flags & CMD_DISKS) != 0) {
133 		if (all)
134 			mlx_disk_add_all();
135 
136 		while (argv[optind] != NULL)
137 			mlx_disk_add(argv[optind++]);
138 		if (mlx_disk_empty())
139 			errx(EXIT_FAILURE,
140 			    "no logical drives attached to this controller");
141 	} else if ((cmd->flags & CMD_NOARGS) != 0)
142 		if (argv[optind] != NULL)
143 			usage();
144 
145 	rv = (*cmd->func)(&argv[optind]);
146 	close(mlxfd);
147 	exit(rv);
148 	/* NOTREACHED */
149 }
150 
151 void
usage(void)152 usage(void)
153 {
154 
155 	(void)fprintf(stderr, "usage: %s [-f dev] [-av] command [...]\n",
156 	    getprogname());
157 	exit(EXIT_FAILURE);
158 	/* NOTREACHED */
159 }
160