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