scsictl.c revision 1.6 1 /* $NetBSD: scsictl.c,v 1.6 1999/07/30 02:29:04 hubertf Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * scsictl(8) - a program to manipulate SCSI devices and busses.
42 */
43
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/scsiio.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <util.h>
55
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsi_all.h>
58 #include <dev/scsipi/scsi_disk.h>
59 #include <dev/scsipi/scsipiconf.h>
60
61 #include "extern.h"
62
63 struct command {
64 const char *cmd_name;
65 const char *arg_names;
66 void (*cmd_func) __P((int, char *[]));
67 };
68
69 int main __P((int, char *[]));
70 void usage __P((void));
71
72 int fd; /* file descriptor for device */
73 const char *dvname; /* device name */
74 char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
75 const char *cmdname; /* command user issued */
76 const char *argnames; /* helpstring: expected arguments */
77 struct scsi_addr dvaddr; /* SCSI device's address */
78
79 extern const char *__progname; /* from crt0.o */
80
81 void device_format __P((int, char *[]));
82 void device_identify __P((int, char *[]));
83 void device_reassign __P((int, char *[]));
84 void device_reset __P((int, char *[]));
85
86 struct command device_commands[] = {
87 { "format", " device", device_format },
88 { "identify", " device", device_identify },
89 { "reassign", " device blkno [blkno [...]]", device_reassign },
90 { "reset", " device", device_reset },
91 { NULL, NULL, NULL },
92 };
93
94 void bus_reset __P((int, char *[]));
95 void bus_scan __P((int, char *[]));
96
97 struct command bus_commands[] = {
98 { "reset", " bus", bus_reset },
99 { "scan", " bus target lun", bus_scan },
100 { NULL, NULL, NULL },
101 };
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 struct command *commands;
109 int i;
110
111 /* Must have at least: device command */
112 if (argc < 3)
113 usage();
114
115 /* Skip program name, get and skip device name and command. */
116 dvname = argv[1];
117 cmdname = argv[2];
118 argv += 3;
119 argc -= 3;
120
121 /*
122 * Open the device and determine if it's a scsibus or an actual
123 * device. Devices respond to the SCIOCIDENTIFY ioctl.
124 */
125 fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
126 if (fd == -1) {
127 if (errno == ENOENT) {
128 /*
129 * Device doesn't exist. Probably trying to open
130 * a device which doesn't use disk semantics for
131 * device name. Try again, specifying "cooked",
132 * which leaves off the "r" in front of the device's
133 * name.
134 */
135 fd = opendisk(dvname, O_RDWR, dvname_store,
136 sizeof(dvname_store), 1);
137 if (fd == -1)
138 err(1, "%s", dvname);
139 } else
140 err(1, "%s", dvname);
141 }
142
143 /*
144 * Point the dvname at the actual device name that opendisk() opened.
145 */
146 dvname = dvname_store;
147
148 if (ioctl(fd, SCIOCIDENTIFY, &dvaddr) < 0)
149 commands = bus_commands;
150 else
151 commands = device_commands;
152
153 /* Look up and call the command. */
154 for (i = 0; commands[i].cmd_name != NULL; i++)
155 if (strcmp(cmdname, commands[i].cmd_name) == 0)
156 break;
157 if (commands[i].cmd_name == NULL)
158 errx(1, "unknown %s command: %s\n",
159 commands == bus_commands ? "bus" : "device", cmdname);
160
161 argnames = commands[i].arg_names;
162
163 (*commands[i].cmd_func)(argc, argv);
164 exit(0);
165 }
166
167 void
168 usage()
169 {
170 int i;
171
172 fprintf(stderr, "Usage: %s device command [arg [...]]\n",
173 __progname);
174 fprintf(stderr, "Where command (and args) are:\n");
175
176 for (i=0; device_commands[i].cmd_name != NULL; i++)
177 fprintf(stderr, "\t%s%s\n", device_commands[i].cmd_name,
178 device_commands[i].arg_names);
179 for (i=0; bus_commands[i].cmd_name != NULL; i++)
180 fprintf(stderr, "\t%s%s\n", bus_commands[i].cmd_name,
181 bus_commands[i].arg_names);
182 fprintf(stderr, "Use `any' to wildcard target or lun\n");
183
184 exit(1);
185 }
186
187 /*
188 * DEVICE COMMANDS
189 */
190
191 /*
192 * device_format:
193 *
194 * Format a direct access device.
195 *
196 * XXX Does not handle defect list management or geometry settings.
197 */
198 void
199 device_format(argc, argv)
200 int argc;
201 char *argv[];
202 {
203 struct scsi_format_unit cmd;
204 struct {
205 struct scsi_mode_header header;
206 struct scsi_blk_desc blk_desc;
207 struct page_disk_format format_page;
208 } data;
209
210 /* No arguments. */
211 if (argc != 0)
212 usage();
213
214 /*
215 * Get the DISK FORMAT mode page. SCSI-2 recommends specifying the
216 * interleave read from this page in the FORMAT UNIT command.
217 */
218 scsi_mode_sense(fd, 0x03, 0x00, &data, sizeof(data));
219
220 memset(&cmd, 0, sizeof(cmd));
221
222 cmd.opcode = SCSI_FORMAT_UNIT;
223 memcpy(cmd.interleave, data.format_page.interleave,
224 sizeof(cmd.interleave));
225
226 scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 60000, 0);
227
228 return;
229 }
230
231 /*
232 * device_identify:
233 *
234 * Display the identity of the device, including it's SCSI bus,
235 * target, lun, and it's vendor/product/revision information.
236 */
237 void
238 device_identify(argc, argv)
239 int argc;
240 char *argv[];
241 {
242 struct scsipi_inquiry_data inqbuf;
243 struct scsipi_inquiry cmd;
244
245 /* x4 in case every character is escaped, +1 for NUL. */
246 char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
247 product[(sizeof(inqbuf.product) * 4) + 1],
248 revision[(sizeof(inqbuf.revision) * 4) + 1];
249
250 /* No arguments. */
251 if (argc != 0)
252 usage();
253
254 memset(&cmd, 0, sizeof(cmd));
255 memset(&inqbuf, 0, sizeof(inqbuf));
256
257 cmd.opcode = INQUIRY;
258 cmd.length = sizeof(inqbuf);
259
260 scsi_command(fd, &cmd, sizeof(cmd), &inqbuf, sizeof(inqbuf),
261 10000, SCCMD_READ);
262
263 scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
264 sizeof(inqbuf.vendor));
265 scsi_strvis(product, sizeof(product), inqbuf.product,
266 sizeof(inqbuf.product));
267 scsi_strvis(revision, sizeof(revision), inqbuf.revision,
268 sizeof(inqbuf.revision));
269
270 printf("%s: scsibus%d target %d lun %d <%s, %s, %s>\n",
271 dvname, dvaddr.addr.scsi.scbus, dvaddr.addr.scsi.target,
272 dvaddr.addr.scsi.lun, vendor, product, revision);
273
274 return;
275 }
276
277 /*
278 * device_reassign:
279 *
280 * Reassign bad blocks on a direct access device.
281 */
282 void
283 device_reassign(argc, argv)
284 int argc;
285 char *argv[];
286 {
287 struct scsi_reassign_blocks cmd;
288 struct scsi_reassign_blocks_data *data;
289 size_t dlen;
290 u_int32_t blkno;
291 int i;
292 char *cp;
293
294 /* We get a list of block numbers. */
295 if (argc < 1)
296 usage();
297
298 /*
299 * Allocate the reassign blocks descriptor. The 4 comes from the
300 * size of the block address in the defect descriptor.
301 */
302 dlen = sizeof(struct scsi_reassign_blocks_data) + ((argc - 1) * 4);
303 data = malloc(dlen);
304 if (data == NULL)
305 errx(1, "unable to allocate defect descriptor");
306 memset(data, 0, dlen);
307
308 cmd.opcode = SCSI_REASSIGN_BLOCKS;
309
310 /* Defect descriptor length. */
311 _lto2l(argc * 4, data->length);
312
313 /* Build the defect descriptor list. */
314 for (i = 0; i < argc; i++) {
315 blkno = strtoul(argv[i], &cp, 10);
316 if (*cp != '\0')
317 errx(1, "invalid block number: %s\n", argv[i]);
318 _lto4l(blkno, data->defect_descriptor[i].dlbaddr);
319 }
320
321 scsi_command(fd, &cmd, sizeof(cmd), data, dlen, 30000, SCCMD_WRITE);
322
323 free(data);
324 return;
325 }
326
327 /*
328 * device_reset:
329 *
330 * Issue a reset to a SCSI device.
331 */
332 void
333 device_reset(argc, argv)
334 int argc;
335 char *argv[];
336 {
337
338 /* No arguments. */
339 if (argc != 0)
340 usage();
341
342 if (ioctl(fd, SCIOCRESET, NULL) != 0)
343 err(1, "SCIOCRESET");
344
345 return;
346 }
347
348 /*
349 * BUS COMMANDS
350 */
351
352 /*
353 * bus_reset:
354 *
355 * Issue a reset to a SCSI bus.
356 */
357 void
358 bus_reset(argc, argv)
359 int argc;
360 char *argv[];
361 {
362
363 /* No arguments. */
364 if (argc != 0)
365 usage();
366
367 if (ioctl(fd, SCBUSIORESET, NULL) != 0)
368 err(1, "SCBUSIORESET");
369
370 return;
371 }
372
373 /*
374 * bus_scan:
375 *
376 * Rescan a SCSI bus for new devices.
377 */
378 void
379 bus_scan(argc, argv)
380 int argc;
381 char *argv[];
382 {
383 struct scbusioscan_args args;
384 char *cp;
385
386 /* Must have two args: target lun */
387 if (argc != 2)
388 usage();
389
390 if (strcmp(argv[0], "any") == 0)
391 args.sa_target = -1;
392 else {
393 args.sa_target = strtol(argv[0], &cp, 10);
394 if (*cp != '\0' || args.sa_target < 0)
395 errx(1, "invalid target: %s\n", argv[0]);
396 }
397
398 if (strcmp(argv[1], "any") == 0)
399 args.sa_lun = -1;
400 else {
401 args.sa_lun = strtol(argv[1], &cp, 10);
402 if (*cp != '\0' || args.sa_lun < 0)
403 errx(1, "invalid lun: %s\n", argv[1]);
404 }
405
406 if (ioctl(fd, SCBUSIOSCAN, &args) != 0)
407 err(1, "SCBUSIOSCAN");
408
409 return;
410 }
411