cmds.c revision 1.1 1 /* $NetBSD: cmds.c,v 1.1 2001/02/04 17:30:37 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001 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 /*-
40 * Copyright (c) 1999 Michael Smith
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * from FreeBSD: command.c,v 1.2 2000/04/11 23:04:17 msmith Exp
65 */
66
67 #ifndef lint
68 #include <sys/cdefs.h>
69 __RCSID("$NetBSD: cmds.c,v 1.1 2001/02/04 17:30:37 ad Exp $");
70 #endif /* not lint */
71
72 #include <sys/types.h>
73 #include <sys/ioctl.h>
74 #include <sys/queue.h>
75 #include <sys/endian.h>
76
77 #include <dev/ic/mlxreg.h>
78 #include <dev/ic/mlxio.h>
79
80 #include <err.h>
81 #include <fcntl.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <ctype.h>
86 #include <unistd.h>
87 #include <getopt.h>
88
89 #include "extern.h"
90
91 static void cmd_status0(struct mlx_disk *);
92 static void cmd_check0(struct mlx_disk *);
93 static void cmd_detach0(struct mlx_disk *);
94
95 static struct mlx_rebuild_status rs;
96 static int rstatus;
97
98 struct {
99 int hwid;
100 const char *name;
101 } static const mlx_ctlr_names[] = {
102 { 0x01, "960P/PD" },
103 { 0x02, "960PL" },
104 { 0x10, "960PG" },
105 { 0x11, "960PJ" },
106 { 0x12, "960PR" },
107 { 0x13, "960PT" },
108 { 0x14, "960PTL0" },
109 { 0x15, "960PRL" } ,
110 { 0x16, "960PTL1" },
111 { 0x20, "1100PVX" },
112 { -1, NULL },
113 };
114
115 /*
116 * Status output
117 */
118 static void
119 cmd_status0(struct mlx_disk *md)
120 {
121 const char *statusfmt;
122 int result;
123
124 result = md->hwunit;
125 if (ioctl(mlxfd, MLXD_STATUS, &result) < 0)
126 err(EXIT_FAILURE, "ioctl(MLXD_STATUS)");
127
128 switch(result) {
129 case MLX_SYSD_ONLINE:
130 statusfmt = "%s: online\n";
131 break;
132
133 case MLX_SYSD_CRITICAL:
134 statusfmt = "%s: critical\n";
135 if (!rstatus)
136 rstatus = 1;
137 break;
138
139 case MLX_SYSD_OFFLINE:
140 statusfmt = "%s: offline\n";
141 rstatus = 2;
142 break;
143
144 default:
145 statusfmt = "%s: unknown status 0x%02x\n";
146 break;
147 }
148
149 printf(statusfmt, md->name, result);
150
151 /* Rebuild/check in progress on this drive? */
152 if (rs.rs_drive == md->hwunit &&
153 rs.rs_code != MLX_REBUILDSTAT_IDLE) {
154 switch(rs.rs_code) {
155 case MLX_REBUILDSTAT_REBUILDCHECK:
156 printf(" [consistency check");
157 break;
158
159 case MLX_REBUILDSTAT_ADDCAPACITY:
160 printf(" [add capacity");
161 break;
162
163 case MLX_REBUILDSTAT_ADDCAPACITYINIT:
164 printf(" [add capacity init");
165 break;
166
167 default:
168 printf(" [unknown operation");
169 break;
170 }
171
172 printf(": %d/%d, %d%% complete]\n", rs.rs_remaining, rs.rs_size,
173 ((rs.rs_size - rs.rs_remaining) / (rs.rs_size / 100)));
174 }
175 }
176
177 int
178 cmd_status(char **argv)
179 {
180
181 if (ioctl(mlxfd, MLX_REBUILDSTAT, &rs) < 0)
182 err(EXIT_FAILURE, "ioctl(MLX_REBUILDSTAT)");
183
184 mlx_disk_iterate(cmd_status0);
185 return (rstatus);
186 }
187
188 /*
189 * Display controller status.
190 */
191 int
192 cmd_cstatus(char **argv)
193 {
194 struct mlx_enquiry2 enq;
195 struct mlx_phys_drv pd;
196 static char buf[80];
197 const char *model;
198 int i, channel, target;
199
200 mlx_enquiry(&enq);
201
202 for (i = 0; i < sizeof(mlx_ctlr_names) / sizeof(mlx_ctlr_names[0]); i++)
203 if (enq.me_hardware_id[0] == mlx_ctlr_names[i].hwid) {
204 model = mlx_ctlr_names[i].name;
205 break;
206 }
207
208 if (i == sizeof(mlx_ctlr_names) / sizeof(mlx_ctlr_names[0])) {
209 sprintf(buf, " model 0x%x", enq.me_hardware_id[0]);
210 model = buf;
211 }
212
213 printf("DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
214 model, enq.me_actual_channels,
215 enq.me_actual_channels > 1 ? "s" : "",
216 enq.me_firmware_id[0], enq.me_firmware_id[1],
217 enq.me_firmware_id[2], enq.me_firmware_id[3],
218 le32toh(enq.me_mem_size) / (1024 * 1024));
219
220 if (verbosity > 0) {
221 printf(" Hardware ID\t\t\t0x%08x\n",
222 le32toh(*(u_int32_t *)enq.me_hardware_id));
223 printf(" Firmware ID\t\t\t0x%08x\n",
224 le32toh(*(u_int32_t *)enq.me_firmware_id));
225 printf(" Configured/Actual channels\t%d/%d\n",
226 enq.me_configured_channels, enq.me_actual_channels);
227 printf(" Max Targets\t\t\t%d\n", enq.me_max_targets);
228 printf(" Max Tags\t\t\t%d\n", enq.me_max_tags);
229 printf(" Max System Drives\t\t%d\n", enq.me_max_sys_drives);
230 printf(" Max Arms\t\t\t%d\n", enq.me_max_arms);
231 printf(" Max Spans\t\t\t%d\n", enq.me_max_spans);
232 printf(" DRAM/cache/flash/NVRAM size\t%d/%d/%d/%d\n",
233 le32toh(enq.me_mem_size), le32toh(enq.me_cache_size),
234 le32toh(enq.me_flash_size), le32toh(enq.me_nvram_size));
235 printf(" DRAM type\t\t\t%d\n", le16toh(enq.me_mem_type));
236 printf(" Clock Speed\t\t\t%dns\n",
237 le16toh(enq.me_clock_speed));
238 printf(" Hardware Speed\t\t%dns\n",
239 le16toh(enq.me_hardware_speed));
240 printf(" Max Commands\t\t\t%d\n",
241 le16toh(enq.me_max_commands));
242 printf(" Max SG Entries\t\t%d\n", le16toh(enq.me_max_sg));
243 printf(" Max DP\t\t\t%d\n", le16toh(enq.me_max_dp));
244 printf(" Max IOD\t\t\t%d\n", le16toh(enq.me_max_iod));
245 printf(" Max Comb\t\t\t%d\n", le16toh(enq.me_max_comb));
246 printf(" Latency\t\t\t%ds\n", enq.me_latency);
247 printf(" SCSI Timeout\t\t\t%ds\n", enq.me_scsi_timeout);
248 printf(" Min Free Lines\t\t%d\n",
249 le16toh(enq.me_min_freelines));
250 printf(" Rate Constant\t\t\t%d\n", enq.me_rate_const);
251 printf(" MAXBLK\t\t\t%d\n", le16toh(enq.me_maxblk));
252 printf(" Blocking Factor\t\t%d sectors\n",
253 le16toh(enq.me_blocking_factor));
254 printf(" Cache Line Size\t\t%d blocks\n",
255 le16toh(enq.me_cacheline));
256 printf(" SCSI Capability\t\t%s%dMHz, %d bit\n",
257 enq.me_scsi_cap & (1<<4) ? "differential " : "",
258 (1 << ((enq.me_scsi_cap >> 2) & 3)) * 10,
259 8 << (enq.me_scsi_cap & 0x3));
260 printf(" Firmware Build Number\t\t%d\n",
261 le16toh(enq.me_firmware_build));
262 printf(" Fault Management Type\t\t%d\n",
263 enq.me_fault_mgmt_type);
264 #if 0
265 printf(" Features\t\t\t%b\n", enq.me_firmware_features,
266 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
267 #endif
268 }
269
270 fflush(stdout);
271
272 /*
273 * Fetch and print physical drive data.
274 */
275 for (channel = 0; channel < enq.me_configured_channels; channel++) {
276 for (target = 0; target < enq.me_max_targets; target++)
277 if (mlx_get_device_state(channel, target, &pd) == 0 &&
278 (pd.pd_flags1 & MLX_PHYS_DRV_PRESENT) != 0)
279 mlx_print_phys_drv(&pd, channel, target, " ");
280 }
281
282 return (0);
283 }
284
285 /*
286 * Recscan for new or changed system drives.
287 */
288 int
289 cmd_rescan(char **argv)
290 {
291
292 if (ioctl(mlxfd, MLX_RESCAN_DRIVES) < 0)
293 err(EXIT_FAILURE, "rescan failed");
294 return (0);
295 }
296
297 /*
298 * Detach one or more system drives from a controller.
299 */
300 static void
301 cmd_detach0(struct mlx_disk *md)
302 {
303
304 if (ioctl(mlxfd, MLXD_DETACH, &md->hwunit) < 0)
305 warn("can't detach %s", md->name);
306 }
307
308 int
309 cmd_detach(char **argv)
310 {
311
312 mlx_disk_iterate(cmd_detach0);
313 return (0);
314 }
315
316 /*
317 * Initiate a consistency check on a system drive.
318 */
319 static void
320 cmd_check0(struct mlx_disk *md)
321 {
322 int result;
323
324 if (ioctl(mlxfd, MLXD_CHECKASYNC, &result) == 0)
325 return;
326
327 switch (result) {
328 case 0x0002:
329 warnx("one or more of the SCSI disks on which %s", md->name);
330 warnx("depends is DEAD.", md->name);
331 break;
332
333 case 0x0105:
334 warnx("drive %s is invalid, or not a drive which ", md->name);
335 warnx("can be checked.");
336 break;
337
338 case 0x0106:
339 warnx("drive rebuild or consistency check is already ");
340 warnx("in progress on this controller.");
341 break;
342
343 default:
344 err(EXIT_FAILURE, "ioctl(MLXD_CHECKASYNC)");
345 /* NOTREACHED */
346 }
347 }
348
349 int
350 cmd_check(char **argv)
351 {
352
353 mlx_disk_iterate(cmd_check0);
354 return (0);
355 }
356
357 /*
358 * Initiate a physical drive rebuild.
359 */
360 int
361 cmd_rebuild(char **argv)
362 {
363 struct mlx_rebuild_request rb;
364 char *p;
365
366 if (argv[0] == NULL || argv[1] != NULL)
367 usage();
368
369 rb.rr_channel = strtol(*argv, &p, 0);
370 if (p[0] != ':' || p[1] == '\0')
371 usage();
372
373 rb.rr_target = strtol(*argv, &p, 0);
374 if (p[0] != '\0')
375 usage();
376
377 if (ioctl(mlxfd, MLX_REBUILDASYNC, &rb) == 0)
378 return (0);
379
380 switch (rb.rr_status) {
381 case 0x0002:
382 warnx("the drive at %d:%d is already ONLINE", rb.rr_channel,
383 rb.rr_target);
384 break;
385
386 case 0x0004:
387 warnx("drive failed during rebuild");
388 break;
389
390 case 0x0105:
391 warnx("there is no drive at %d:%d", rb.rr_channel,
392 rb.rr_target);
393 break;
394
395 case 0x0106:
396 warnx("drive rebuild or consistency check is already in ");
397 warnx("progress on this controller");
398 break;
399
400 default:
401 err(EXIT_FAILURE, "ioctl(MLXD_CHECKASYNC)");
402 /* NOTREACHED */
403 }
404
405 return(0);
406 }
407