scsictl.c revision 1.37 1 1.37 jakllsch /* $NetBSD: scsictl.c,v 1.37 2013/01/12 02:52:59 jakllsch Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.18 thorpej * Copyright (c) 1998, 2002 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 *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej /*
34 1.1 thorpej * scsictl(8) - a program to manipulate SCSI devices and busses.
35 1.1 thorpej */
36 1.20 agc #include <sys/cdefs.h>
37 1.20 agc
38 1.20 agc #ifndef lint
39 1.37 jakllsch __RCSID("$NetBSD: scsictl.c,v 1.37 2013/01/12 02:52:59 jakllsch Exp $");
40 1.20 agc #endif
41 1.20 agc
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.1 thorpej #include <sys/ioctl.h>
45 1.1 thorpej #include <sys/scsiio.h>
46 1.1 thorpej #include <err.h>
47 1.1 thorpej #include <errno.h>
48 1.1 thorpej #include <fcntl.h>
49 1.25 ginsbach #include <limits.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.27 thorpej #include <dev/scsipi/scsi_spc.h>
57 1.1 thorpej #include <dev/scsipi/scsipi_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.26 xtraeme void (*cmd_func)(int, char *[]);
67 1.1 thorpej };
68 1.1 thorpej
69 1.33 joerg __dead static void usage(void);
70 1.1 thorpej
71 1.35 jakllsch static int fd; /* file descriptor for device */
72 1.35 jakllsch const char *dvname; /* device name */
73 1.35 jakllsch static char dvname_store[MAXPATHLEN]; /* for opendisk(3) */
74 1.35 jakllsch static const char *cmdname; /* command user issued */
75 1.35 jakllsch static struct scsi_addr dvaddr; /* SCSI device's address */
76 1.35 jakllsch
77 1.35 jakllsch static void device_defects(int, char *[]);
78 1.35 jakllsch static void device_format(int, char *[]);
79 1.35 jakllsch static void device_identify(int, char *[]);
80 1.35 jakllsch static void device_reassign(int, char *[]);
81 1.35 jakllsch static void device_release(int, char *[]);
82 1.35 jakllsch static void device_reserve(int, char *[]);
83 1.35 jakllsch static void device_reset(int, char *[]);
84 1.35 jakllsch static void device_debug(int, char *[]);
85 1.35 jakllsch static void device_prevent(int, char *[]);
86 1.35 jakllsch static void device_allow(int, char *[]);
87 1.35 jakllsch static void device_start(int, char *[]);
88 1.35 jakllsch static void device_stop(int, char *[]);
89 1.35 jakllsch static void device_tur(int, char *[]);
90 1.35 jakllsch static void device_getcache(int, char *[]);
91 1.35 jakllsch static void device_setcache(int, char *[]);
92 1.35 jakllsch static void device_flushcache(int, char *[]);
93 1.35 jakllsch static void device_setspeed(int, char *[]);
94 1.1 thorpej
95 1.35 jakllsch static struct command device_commands[] = {
96 1.25 ginsbach { "defects", "[primary] [grown] [block|byte|physical]",
97 1.25 ginsbach device_defects },
98 1.16 mjacob { "format", "[blocksize [immediate]]", device_format },
99 1.7 mjl { "identify", "", device_identify },
100 1.7 mjl { "reassign", "blkno [blkno [...]]", device_reassign },
101 1.16 mjacob { "release", "", device_release },
102 1.16 mjacob { "reserve", "", device_reserve },
103 1.7 mjl { "reset", "", device_reset },
104 1.19 petrov { "debug", "level", device_debug },
105 1.22 mycroft { "prevent", "", device_prevent },
106 1.22 mycroft { "allow", "", device_allow },
107 1.16 mjacob { "start", "", device_start },
108 1.16 mjacob { "stop", "", device_stop },
109 1.16 mjacob { "tur", "", device_tur },
110 1.18 thorpej { "getcache", "", device_getcache },
111 1.18 thorpej { "setcache", "none|r|w|rw [save]", device_setcache },
112 1.21 mycroft { "flushcache", "", device_flushcache },
113 1.29 bouyer { "setspeed", "[speed]", device_setspeed },
114 1.7 mjl { NULL, NULL, NULL },
115 1.1 thorpej };
116 1.1 thorpej
117 1.35 jakllsch static void bus_reset(int, char *[]);
118 1.35 jakllsch static void bus_scan(int, char *[]);
119 1.35 jakllsch static void bus_detach(int, char *[]);
120 1.1 thorpej
121 1.35 jakllsch static struct command bus_commands[] = {
122 1.7 mjl { "reset", "", bus_reset },
123 1.7 mjl { "scan", "target lun", bus_scan },
124 1.14 bouyer { "detach", "target lun", bus_detach },
125 1.6 hubertf { NULL, NULL, NULL },
126 1.1 thorpej };
127 1.1 thorpej
128 1.1 thorpej int
129 1.26 xtraeme main(int argc, char *argv[])
130 1.1 thorpej {
131 1.1 thorpej struct command *commands;
132 1.1 thorpej int i;
133 1.1 thorpej
134 1.1 thorpej /* Must have at least: device command */
135 1.1 thorpej if (argc < 3)
136 1.1 thorpej usage();
137 1.1 thorpej
138 1.1 thorpej /* Skip program name, get and skip device name and command. */
139 1.1 thorpej dvname = argv[1];
140 1.1 thorpej cmdname = argv[2];
141 1.1 thorpej argv += 3;
142 1.1 thorpej argc -= 3;
143 1.1 thorpej
144 1.1 thorpej /*
145 1.1 thorpej * Open the device and determine if it's a scsibus or an actual
146 1.1 thorpej * device. Devices respond to the SCIOCIDENTIFY ioctl.
147 1.1 thorpej */
148 1.1 thorpej fd = opendisk(dvname, O_RDWR, dvname_store, sizeof(dvname_store), 0);
149 1.1 thorpej if (fd == -1) {
150 1.1 thorpej if (errno == ENOENT) {
151 1.1 thorpej /*
152 1.1 thorpej * Device doesn't exist. Probably trying to open
153 1.1 thorpej * a device which doesn't use disk semantics for
154 1.3 thorpej * device name. Try again, specifying "cooked",
155 1.3 thorpej * which leaves off the "r" in front of the device's
156 1.3 thorpej * name.
157 1.1 thorpej */
158 1.3 thorpej fd = opendisk(dvname, O_RDWR, dvname_store,
159 1.3 thorpej sizeof(dvname_store), 1);
160 1.1 thorpej if (fd == -1)
161 1.1 thorpej err(1, "%s", dvname);
162 1.5 jwise } else
163 1.5 jwise err(1, "%s", dvname);
164 1.1 thorpej }
165 1.3 thorpej
166 1.3 thorpej /*
167 1.3 thorpej * Point the dvname at the actual device name that opendisk() opened.
168 1.3 thorpej */
169 1.3 thorpej dvname = dvname_store;
170 1.1 thorpej
171 1.1 thorpej if (ioctl(fd, SCIOCIDENTIFY, &dvaddr) < 0)
172 1.1 thorpej commands = bus_commands;
173 1.1 thorpej else
174 1.1 thorpej commands = device_commands;
175 1.1 thorpej
176 1.1 thorpej /* Look up and call the command. */
177 1.1 thorpej for (i = 0; commands[i].cmd_name != NULL; i++)
178 1.1 thorpej if (strcmp(cmdname, commands[i].cmd_name) == 0)
179 1.1 thorpej break;
180 1.1 thorpej if (commands[i].cmd_name == NULL)
181 1.12 ad errx(1, "unknown %s command: %s",
182 1.1 thorpej commands == bus_commands ? "bus" : "device", cmdname);
183 1.1 thorpej
184 1.1 thorpej (*commands[i].cmd_func)(argc, argv);
185 1.1 thorpej exit(0);
186 1.1 thorpej }
187 1.1 thorpej
188 1.33 joerg static void
189 1.26 xtraeme usage(void)
190 1.1 thorpej {
191 1.6 hubertf int i;
192 1.1 thorpej
193 1.23 jmmv fprintf(stderr, "usage: %s device command [arg [...]]\n",
194 1.11 cgd getprogname());
195 1.7 mjl
196 1.7 mjl fprintf(stderr, " Commands pertaining to scsi devices:\n");
197 1.6 hubertf for (i=0; device_commands[i].cmd_name != NULL; i++)
198 1.7 mjl fprintf(stderr, "\t%s %s\n", device_commands[i].cmd_name,
199 1.6 hubertf device_commands[i].arg_names);
200 1.7 mjl fprintf(stderr, " Commands pertaining to scsi busses:\n");
201 1.6 hubertf for (i=0; bus_commands[i].cmd_name != NULL; i++)
202 1.7 mjl fprintf(stderr, "\t%s %s\n", bus_commands[i].cmd_name,
203 1.6 hubertf bus_commands[i].arg_names);
204 1.8 ad fprintf(stderr, " Use `any' or `all' to wildcard target or lun\n");
205 1.6 hubertf
206 1.1 thorpej exit(1);
207 1.1 thorpej }
208 1.1 thorpej
209 1.1 thorpej /*
210 1.1 thorpej * DEVICE COMMANDS
211 1.1 thorpej */
212 1.4 thorpej
213 1.4 thorpej /*
214 1.25 ginsbach * device_read_defect:
215 1.25 ginsbach *
216 1.25 ginsbach * Read primary and/or growth defect list in physical or block
217 1.25 ginsbach * format from a direct access device.
218 1.25 ginsbach *
219 1.25 ginsbach * XXX Does not handle very large defect lists. Needs SCSI3 12
220 1.25 ginsbach * byte READ DEFECT DATA command.
221 1.25 ginsbach */
222 1.25 ginsbach
223 1.35 jakllsch static void print_bf_dd(union scsi_defect_descriptor *);
224 1.35 jakllsch static void print_bfif_dd(union scsi_defect_descriptor *);
225 1.35 jakllsch static void print_psf_dd(union scsi_defect_descriptor *);
226 1.25 ginsbach
227 1.35 jakllsch static void
228 1.26 xtraeme device_defects(int argc, char *argv[])
229 1.25 ginsbach {
230 1.25 ginsbach struct scsi_read_defect_data cmd;
231 1.25 ginsbach struct scsi_read_defect_data_data *data;
232 1.25 ginsbach size_t dlen;
233 1.25 ginsbach int i, dlfmt = -1;
234 1.25 ginsbach int defects;
235 1.25 ginsbach char msg[256];
236 1.26 xtraeme void (*pfunc)(union scsi_defect_descriptor *);
237 1.25 ginsbach #define RDD_P_G_MASK 0x18
238 1.25 ginsbach #define RDD_DLF_MASK 0x7
239 1.25 ginsbach
240 1.25 ginsbach dlen = USHRT_MAX; /* XXX - this may not be enough room
241 1.25 ginsbach * for all of the defects.
242 1.25 ginsbach */
243 1.25 ginsbach data = malloc(dlen);
244 1.25 ginsbach if (data == NULL)
245 1.25 ginsbach errx(1, "unable to allocate defect list");
246 1.25 ginsbach memset(data, 0, dlen);
247 1.25 ginsbach memset(&cmd, 0, sizeof(cmd));
248 1.28 lukem defects = 0;
249 1.28 lukem pfunc = NULL;
250 1.25 ginsbach
251 1.25 ginsbach /* determine which defect list(s) to read. */
252 1.25 ginsbach for (i = 0; i < argc; i++) {
253 1.25 ginsbach if (strncmp("primary", argv[i], 7) == 0) {
254 1.25 ginsbach cmd.flags |= RDD_PRIMARY;
255 1.25 ginsbach continue;
256 1.25 ginsbach }
257 1.25 ginsbach if (strncmp("grown", argv[i], 5) == 0) {
258 1.25 ginsbach cmd.flags |= RDD_GROWN;
259 1.25 ginsbach continue;
260 1.25 ginsbach }
261 1.25 ginsbach break;
262 1.25 ginsbach }
263 1.25 ginsbach
264 1.25 ginsbach /* no defect list sepecified, assume both. */
265 1.25 ginsbach if ((cmd.flags & (RDD_PRIMARY|RDD_GROWN)) == 0)
266 1.25 ginsbach cmd.flags |= (RDD_PRIMARY|RDD_GROWN);
267 1.25 ginsbach
268 1.25 ginsbach /* list format option. */
269 1.25 ginsbach if (i < argc) {
270 1.25 ginsbach if (strncmp("block", argv[i], 5) == 0) {
271 1.25 ginsbach cmd.flags |= RDD_BF;
272 1.25 ginsbach dlfmt = RDD_BF;
273 1.25 ginsbach }
274 1.25 ginsbach else if (strncmp("byte", argv[i], 4) == 0) {
275 1.25 ginsbach cmd.flags |= RDD_BFIF;
276 1.25 ginsbach dlfmt = RDD_BFIF;
277 1.25 ginsbach }
278 1.25 ginsbach else if (strncmp("physical", argv[i], 4) == 0) {
279 1.25 ginsbach cmd.flags |= RDD_PSF;
280 1.25 ginsbach dlfmt = RDD_PSF;
281 1.25 ginsbach }
282 1.25 ginsbach else {
283 1.25 ginsbach usage();
284 1.25 ginsbach }
285 1.25 ginsbach }
286 1.25 ginsbach
287 1.25 ginsbach /*
288 1.25 ginsbach * no list format specified; since block format not
289 1.25 ginsbach * recommended use physical sector format as default.
290 1.25 ginsbach */
291 1.25 ginsbach if (dlfmt < 0) {
292 1.25 ginsbach cmd.flags |= RDD_PSF;
293 1.25 ginsbach dlfmt = RDD_PSF;
294 1.25 ginsbach }
295 1.25 ginsbach
296 1.25 ginsbach cmd.opcode = SCSI_READ_DEFECT_DATA;
297 1.25 ginsbach _lto2b(dlen, &cmd.length[0]);
298 1.25 ginsbach
299 1.25 ginsbach scsi_command(fd, &cmd, sizeof(cmd), data, dlen, 30000, SCCMD_READ);
300 1.25 ginsbach
301 1.25 ginsbach msg[0] = '\0';
302 1.25 ginsbach
303 1.25 ginsbach /* is the defect list in the format asked for? */
304 1.25 ginsbach if ((data->flags & RDD_DLF_MASK) != dlfmt) {
305 1.25 ginsbach strcpy(msg, "\n\tnotice:"
306 1.25 ginsbach "requested defect list format not supported by device\n\n");
307 1.25 ginsbach dlfmt = (data->flags & RDD_DLF_MASK);
308 1.25 ginsbach }
309 1.25 ginsbach
310 1.25 ginsbach if (data->flags & RDD_PRIMARY)
311 1.25 ginsbach strcat(msg, "primary");
312 1.25 ginsbach
313 1.25 ginsbach if (data->flags & RDD_GROWN) {
314 1.25 ginsbach if (data->flags & RDD_PRIMARY)
315 1.25 ginsbach strcat(msg, " and ");
316 1.25 ginsbach strcat(msg, "grown");
317 1.25 ginsbach }
318 1.25 ginsbach
319 1.25 ginsbach strcat(msg, " defects");
320 1.25 ginsbach
321 1.25 ginsbach if ((data->flags & RDD_P_G_MASK) == 0)
322 1.25 ginsbach strcat(msg, ": none reported\n");
323 1.25 ginsbach
324 1.25 ginsbach
325 1.25 ginsbach printf("%s: scsibus%d target %d lun %d %s",
326 1.25 ginsbach dvname, dvaddr.addr.scsi.scbus, dvaddr.addr.scsi.target,
327 1.25 ginsbach dvaddr.addr.scsi.lun, msg);
328 1.25 ginsbach
329 1.25 ginsbach /* device did not return either defect list. */
330 1.25 ginsbach if ((data->flags & RDD_P_G_MASK) == 0)
331 1.25 ginsbach return;
332 1.25 ginsbach
333 1.25 ginsbach switch (dlfmt) {
334 1.25 ginsbach case RDD_BF:
335 1.25 ginsbach defects = _2btol(data->length) /
336 1.25 ginsbach sizeof(struct scsi_defect_descriptor_bf);
337 1.25 ginsbach pfunc = print_bf_dd;
338 1.25 ginsbach strcpy(msg, "block address\n"
339 1.25 ginsbach "-------------\n");
340 1.25 ginsbach break;
341 1.25 ginsbach case RDD_BFIF:
342 1.25 ginsbach defects = _2btol(data->length) /
343 1.25 ginsbach sizeof(struct scsi_defect_descriptor_bfif);
344 1.25 ginsbach pfunc = print_bfif_dd;
345 1.25 ginsbach strcpy(msg, " bytes from\n"
346 1.25 ginsbach "cylinder head index\n"
347 1.25 ginsbach "-------- ---- ----------\n");
348 1.25 ginsbach break;
349 1.25 ginsbach case RDD_PSF:
350 1.25 ginsbach defects = _2btol(data->length) /
351 1.25 ginsbach sizeof(struct scsi_defect_descriptor_psf);
352 1.25 ginsbach pfunc = print_psf_dd;
353 1.25 ginsbach strcpy(msg, "cylinder head sector\n"
354 1.25 ginsbach "-------- ---- ----------\n");
355 1.25 ginsbach break;
356 1.25 ginsbach }
357 1.25 ginsbach
358 1.25 ginsbach /* device did not return any defects. */
359 1.25 ginsbach if (defects == 0) {
360 1.25 ginsbach printf(": none\n");
361 1.25 ginsbach return;
362 1.25 ginsbach }
363 1.25 ginsbach
364 1.25 ginsbach printf(": %d\n", defects);
365 1.25 ginsbach
366 1.25 ginsbach /* print heading. */
367 1.25 ginsbach printf("%s", msg);
368 1.25 ginsbach
369 1.25 ginsbach /* print defect list. */
370 1.25 ginsbach for (i = 0 ; i < defects; i++) {
371 1.25 ginsbach pfunc(&data->defect_descriptor[i]);
372 1.25 ginsbach }
373 1.25 ginsbach
374 1.25 ginsbach free(data);
375 1.25 ginsbach return;
376 1.25 ginsbach }
377 1.25 ginsbach
378 1.25 ginsbach /*
379 1.25 ginsbach * print_bf_dd:
380 1.25 ginsbach *
381 1.25 ginsbach * Print a block format defect descriptor.
382 1.25 ginsbach */
383 1.35 jakllsch static void
384 1.26 xtraeme print_bf_dd(union scsi_defect_descriptor *dd)
385 1.25 ginsbach {
386 1.25 ginsbach u_int32_t block;
387 1.25 ginsbach
388 1.25 ginsbach block = _4btol(dd->bf.block_address);
389 1.25 ginsbach
390 1.25 ginsbach printf("%13u\n", block);
391 1.25 ginsbach }
392 1.25 ginsbach
393 1.25 ginsbach #define DEFECTIVE_TRACK 0xffffffff
394 1.25 ginsbach
395 1.25 ginsbach /*
396 1.25 ginsbach * print_bfif_dd:
397 1.25 ginsbach *
398 1.25 ginsbach * Print a bytes from index format defect descriptor.
399 1.25 ginsbach */
400 1.35 jakllsch static void
401 1.26 xtraeme print_bfif_dd(union scsi_defect_descriptor *dd)
402 1.25 ginsbach {
403 1.25 ginsbach u_int32_t cylinder;
404 1.25 ginsbach u_int32_t head;
405 1.25 ginsbach u_int32_t bytes_from_index;
406 1.25 ginsbach
407 1.25 ginsbach cylinder = _3btol(dd->bfif.cylinder);
408 1.25 ginsbach head = dd->bfif.head;
409 1.25 ginsbach bytes_from_index = _4btol(dd->bfif.bytes_from_index);
410 1.25 ginsbach
411 1.25 ginsbach printf("%8u %4u ", cylinder, head);
412 1.25 ginsbach
413 1.25 ginsbach if (bytes_from_index == DEFECTIVE_TRACK)
414 1.25 ginsbach printf("entire track defective\n");
415 1.25 ginsbach else
416 1.25 ginsbach printf("%10u\n", bytes_from_index);
417 1.25 ginsbach }
418 1.25 ginsbach
419 1.25 ginsbach /*
420 1.25 ginsbach * print_psf_dd:
421 1.25 ginsbach *
422 1.25 ginsbach * Print a physical sector format defect descriptor.
423 1.25 ginsbach */
424 1.35 jakllsch static void
425 1.26 xtraeme print_psf_dd(union scsi_defect_descriptor *dd)
426 1.25 ginsbach {
427 1.25 ginsbach u_int32_t cylinder;
428 1.25 ginsbach u_int32_t head;
429 1.25 ginsbach u_int32_t sector;
430 1.25 ginsbach
431 1.25 ginsbach cylinder = _3btol(dd->psf.cylinder);
432 1.25 ginsbach head = dd->psf.head;
433 1.25 ginsbach sector = _4btol(dd->psf.sector);
434 1.25 ginsbach
435 1.25 ginsbach printf("%8u %4u ", cylinder, head);
436 1.25 ginsbach
437 1.25 ginsbach if (sector == DEFECTIVE_TRACK)
438 1.25 ginsbach printf("entire track defective\n");
439 1.25 ginsbach else
440 1.25 ginsbach printf("%10u\n", sector);
441 1.25 ginsbach }
442 1.25 ginsbach
443 1.25 ginsbach /*
444 1.4 thorpej * device_format:
445 1.4 thorpej *
446 1.4 thorpej * Format a direct access device.
447 1.4 thorpej */
448 1.35 jakllsch static void
449 1.26 xtraeme device_format(int argc, char *argv[])
450 1.4 thorpej {
451 1.16 mjacob u_int32_t blksize;
452 1.16 mjacob int i, j, immediate;
453 1.16 mjacob #define PC (65536/10)
454 1.16 mjacob static int complete[] = {
455 1.16 mjacob PC*1, PC*2, PC*3, PC*4, PC*5, PC*6, PC*7, PC*8, PC*9, 65536
456 1.16 mjacob };
457 1.16 mjacob char *cp, buffer[64];
458 1.27 thorpej struct scsi_sense_data sense;
459 1.4 thorpej struct scsi_format_unit cmd;
460 1.4 thorpej struct {
461 1.16 mjacob struct scsi_format_unit_defect_list_header header;
462 1.16 mjacob /* optional initialization pattern */
463 1.16 mjacob /* optional defect list */
464 1.16 mjacob } dfl;
465 1.16 mjacob struct {
466 1.27 thorpej struct scsi_mode_parameter_header_6 header;
467 1.27 thorpej struct scsi_general_block_descriptor blk_desc;
468 1.4 thorpej struct page_disk_format format_page;
469 1.16 mjacob } mode_page;
470 1.16 mjacob struct {
471 1.27 thorpej struct scsi_mode_parameter_header_6 header;
472 1.27 thorpej struct scsi_general_block_descriptor blk_desc;
473 1.16 mjacob } data_select;
474 1.16 mjacob
475 1.4 thorpej
476 1.16 mjacob /* Blocksize is an optional argument. */
477 1.16 mjacob if (argc > 2)
478 1.6 hubertf usage();
479 1.4 thorpej
480 1.4 thorpej /*
481 1.16 mjacob * Loop doing Request Sense to clear any pending Unit Attention.
482 1.16 mjacob *
483 1.16 mjacob * Multiple conditions may exist on the drive which are returned
484 1.16 mjacob * in priority order.
485 1.16 mjacob */
486 1.16 mjacob for (i = 0; i < 8; i++) {
487 1.16 mjacob scsi_request_sense(fd, &sense, sizeof (sense));
488 1.27 thorpej if ((j = SSD_SENSE_KEY(sense.flags)) == SKEY_NO_SENSE)
489 1.16 mjacob break;
490 1.16 mjacob }
491 1.16 mjacob /*
492 1.16 mjacob * Make sure we cleared any pending Unit Attention
493 1.16 mjacob */
494 1.16 mjacob if (j != SKEY_NO_SENSE) {
495 1.16 mjacob cp = scsi_decode_sense((const unsigned char *) &sense, 2,
496 1.16 mjacob buffer, sizeof (buffer));
497 1.17 grant errx(1, "failed to clean Unit Attention: %s", cp);
498 1.16 mjacob }
499 1.16 mjacob
500 1.16 mjacob /*
501 1.4 thorpej * Get the DISK FORMAT mode page. SCSI-2 recommends specifying the
502 1.4 thorpej * interleave read from this page in the FORMAT UNIT command.
503 1.4 thorpej */
504 1.16 mjacob scsi_mode_sense(fd, 0x03, 0x00, &mode_page, sizeof(mode_page));
505 1.16 mjacob
506 1.16 mjacob j = (mode_page.format_page.bytes_s[0] << 8) |
507 1.16 mjacob (mode_page.format_page.bytes_s[1]);
508 1.16 mjacob
509 1.16 mjacob if (j != DEV_BSIZE)
510 1.32 joerg printf("current disk sector size: %d\n", j);
511 1.4 thorpej
512 1.4 thorpej memset(&cmd, 0, sizeof(cmd));
513 1.4 thorpej
514 1.4 thorpej cmd.opcode = SCSI_FORMAT_UNIT;
515 1.16 mjacob memcpy(cmd.interleave, mode_page.format_page.interleave,
516 1.4 thorpej sizeof(cmd.interleave));
517 1.4 thorpej
518 1.16 mjacob /*
519 1.16 mjacob * The blocksize on the device is only changed if the user
520 1.16 mjacob * specified a new blocksize. If not specified the blocksize
521 1.16 mjacob * used for the device will be the Default value in the device.
522 1.16 mjacob * We don't specify the number of blocks since the format
523 1.16 mjacob * command will always reformat the entire drive. Also by
524 1.16 mjacob * not specifying a block count the drive will reset the
525 1.16 mjacob * block count to the maximum available after the format
526 1.16 mjacob * completes if the blocksize was changed in the format.
527 1.16 mjacob * Finally, the new disk geometry will not but updated on
528 1.16 mjacob * the drive in permanent storage until _AFTER_ the format
529 1.16 mjacob * completes successfully.
530 1.16 mjacob */
531 1.16 mjacob if (argc > 0) {
532 1.16 mjacob blksize = strtoul(argv[0], &cp, 10);
533 1.16 mjacob if (*cp != '\0')
534 1.17 grant errx(1, "invalid block size: %s", argv[0]);
535 1.16 mjacob
536 1.16 mjacob memset(&data_select, 0, sizeof(data_select));
537 1.16 mjacob
538 1.27 thorpej data_select.header.blk_desc_len =
539 1.27 thorpej sizeof(struct scsi_general_block_descriptor);
540 1.16 mjacob /*
541 1.16 mjacob * blklen in desc is 3 bytes with a leading reserved byte
542 1.16 mjacob */
543 1.16 mjacob _lto4b(blksize, &data_select.blk_desc.reserved);
544 1.16 mjacob
545 1.16 mjacob /*
546 1.16 mjacob * Issue Mode Select to modify the device blocksize to be
547 1.16 mjacob * used on the Format. The modified device geometry will
548 1.16 mjacob * be stored as Current and Saved Page 3 parameters when
549 1.16 mjacob * the Format completes.
550 1.16 mjacob */
551 1.16 mjacob scsi_mode_select(fd, 0, &data_select, sizeof(data_select));
552 1.16 mjacob
553 1.16 mjacob /*
554 1.16 mjacob * Since user specified a specific block size make sure it
555 1.16 mjacob * gets stored in the device when the format completes.
556 1.16 mjacob *
557 1.16 mjacob * Also scrub the defect list back to the manufacturers
558 1.16 mjacob * original.
559 1.16 mjacob */
560 1.16 mjacob cmd.flags = SFU_CMPLST | SFU_FMTDATA;
561 1.16 mjacob }
562 1.16 mjacob
563 1.16 mjacob memset(&dfl, 0, sizeof(dfl));
564 1.4 thorpej
565 1.16 mjacob if (argc > 1 && strncmp(argv[1], "imm", 3) == 0) {
566 1.16 mjacob /*
567 1.16 mjacob * Signal target for an immediate return from Format.
568 1.16 mjacob *
569 1.16 mjacob * We'll poll for completion status.
570 1.16 mjacob */
571 1.16 mjacob dfl.header.flags = DLH_IMMED;
572 1.16 mjacob immediate = 1;
573 1.16 mjacob } else {
574 1.16 mjacob immediate = 0;
575 1.16 mjacob }
576 1.16 mjacob
577 1.16 mjacob scsi_command(fd, &cmd, sizeof(cmd), &dfl, sizeof(dfl),
578 1.37 jakllsch 8 * 60 * 60 * 1000, SCCMD_WRITE);
579 1.16 mjacob
580 1.16 mjacob /*
581 1.16 mjacob * Poll device for completion of Format
582 1.16 mjacob */
583 1.16 mjacob if (immediate) {
584 1.16 mjacob i = 0;
585 1.16 mjacob printf("formatting.");
586 1.16 mjacob fflush(stdout);
587 1.16 mjacob do {
588 1.16 mjacob scsireq_t req;
589 1.27 thorpej struct scsi_test_unit_ready tcmd;
590 1.16 mjacob
591 1.36 jakllsch memset(&tcmd, 0, sizeof(tcmd));
592 1.27 thorpej tcmd.opcode = SCSI_TEST_UNIT_READY;
593 1.16 mjacob
594 1.16 mjacob memset(&req, 0, sizeof(req));
595 1.16 mjacob memcpy(req.cmd, &tcmd, 6);
596 1.16 mjacob req.cmdlen = 6;
597 1.16 mjacob req.timeout = 10000;
598 1.16 mjacob req.senselen = SENSEBUFLEN;
599 1.16 mjacob
600 1.16 mjacob if (ioctl(fd, SCIOCCOMMAND, &req) == -1) {
601 1.16 mjacob err(1, "SCIOCCOMMAND");
602 1.16 mjacob }
603 1.16 mjacob
604 1.16 mjacob if (req.retsts == SCCMD_OK) {
605 1.16 mjacob break;
606 1.16 mjacob } else if (req.retsts == SCCMD_TIMEOUT) {
607 1.16 mjacob fprintf(stderr, "%s: SCSI command timed out",
608 1.16 mjacob dvname);
609 1.16 mjacob break;
610 1.16 mjacob } else if (req.retsts == SCCMD_BUSY) {
611 1.16 mjacob fprintf(stderr, "%s: device is busy",
612 1.16 mjacob dvname);
613 1.16 mjacob break;
614 1.16 mjacob } else if (req.retsts != SCCMD_SENSE) {
615 1.16 mjacob fprintf(stderr,
616 1.16 mjacob "%s: device had unknown status %x", dvname,
617 1.16 mjacob req.retsts);
618 1.16 mjacob break;
619 1.16 mjacob }
620 1.30 christos memcpy(&sense, req.sense, sizeof(sense));
621 1.27 thorpej if (sense.sks.sks_bytes[0] & SSD_SKSV) {
622 1.27 thorpej j = (sense.sks.sks_bytes[1] << 8) |
623 1.27 thorpej (sense.sks.sks_bytes[2]);
624 1.16 mjacob if (j >= complete[i]) {
625 1.16 mjacob printf(".%d0%%.", ++i);
626 1.16 mjacob fflush(stdout);
627 1.16 mjacob }
628 1.16 mjacob }
629 1.16 mjacob sleep(10);
630 1.27 thorpej } while (SSD_SENSE_KEY(sense.flags) == SKEY_NOT_READY);
631 1.16 mjacob printf(".100%%..done.\n");
632 1.16 mjacob }
633 1.4 thorpej return;
634 1.4 thorpej }
635 1.1 thorpej
636 1.1 thorpej /*
637 1.1 thorpej * device_identify:
638 1.1 thorpej *
639 1.1 thorpej * Display the identity of the device, including it's SCSI bus,
640 1.1 thorpej * target, lun, and it's vendor/product/revision information.
641 1.1 thorpej */
642 1.35 jakllsch static void
643 1.26 xtraeme device_identify(int argc, char *argv[])
644 1.1 thorpej {
645 1.1 thorpej struct scsipi_inquiry_data inqbuf;
646 1.1 thorpej struct scsipi_inquiry cmd;
647 1.1 thorpej
648 1.1 thorpej /* x4 in case every character is escaped, +1 for NUL. */
649 1.1 thorpej char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
650 1.1 thorpej product[(sizeof(inqbuf.product) * 4) + 1],
651 1.1 thorpej revision[(sizeof(inqbuf.revision) * 4) + 1];
652 1.1 thorpej
653 1.1 thorpej /* No arguments. */
654 1.1 thorpej if (argc != 0)
655 1.6 hubertf usage();
656 1.1 thorpej
657 1.1 thorpej memset(&cmd, 0, sizeof(cmd));
658 1.1 thorpej memset(&inqbuf, 0, sizeof(inqbuf));
659 1.1 thorpej
660 1.1 thorpej cmd.opcode = INQUIRY;
661 1.1 thorpej cmd.length = sizeof(inqbuf);
662 1.1 thorpej
663 1.1 thorpej scsi_command(fd, &cmd, sizeof(cmd), &inqbuf, sizeof(inqbuf),
664 1.1 thorpej 10000, SCCMD_READ);
665 1.1 thorpej
666 1.1 thorpej scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
667 1.1 thorpej sizeof(inqbuf.vendor));
668 1.1 thorpej scsi_strvis(product, sizeof(product), inqbuf.product,
669 1.1 thorpej sizeof(inqbuf.product));
670 1.1 thorpej scsi_strvis(revision, sizeof(revision), inqbuf.revision,
671 1.1 thorpej sizeof(inqbuf.revision));
672 1.1 thorpej
673 1.1 thorpej printf("%s: scsibus%d target %d lun %d <%s, %s, %s>\n",
674 1.1 thorpej dvname, dvaddr.addr.scsi.scbus, dvaddr.addr.scsi.target,
675 1.1 thorpej dvaddr.addr.scsi.lun, vendor, product, revision);
676 1.1 thorpej
677 1.1 thorpej return;
678 1.1 thorpej }
679 1.1 thorpej
680 1.1 thorpej /*
681 1.1 thorpej * device_reassign:
682 1.1 thorpej *
683 1.1 thorpej * Reassign bad blocks on a direct access device.
684 1.1 thorpej */
685 1.35 jakllsch static void
686 1.26 xtraeme device_reassign(int argc, char *argv[])
687 1.1 thorpej {
688 1.1 thorpej struct scsi_reassign_blocks cmd;
689 1.1 thorpej struct scsi_reassign_blocks_data *data;
690 1.1 thorpej size_t dlen;
691 1.1 thorpej u_int32_t blkno;
692 1.1 thorpej int i;
693 1.1 thorpej char *cp;
694 1.1 thorpej
695 1.1 thorpej /* We get a list of block numbers. */
696 1.1 thorpej if (argc < 1)
697 1.6 hubertf usage();
698 1.1 thorpej
699 1.1 thorpej /*
700 1.1 thorpej * Allocate the reassign blocks descriptor. The 4 comes from the
701 1.1 thorpej * size of the block address in the defect descriptor.
702 1.1 thorpej */
703 1.1 thorpej dlen = sizeof(struct scsi_reassign_blocks_data) + ((argc - 1) * 4);
704 1.1 thorpej data = malloc(dlen);
705 1.1 thorpej if (data == NULL)
706 1.1 thorpej errx(1, "unable to allocate defect descriptor");
707 1.1 thorpej memset(data, 0, dlen);
708 1.1 thorpej
709 1.1 thorpej cmd.opcode = SCSI_REASSIGN_BLOCKS;
710 1.9 mycroft cmd.byte2 = 0;
711 1.9 mycroft cmd.unused[0] = 0;
712 1.9 mycroft cmd.unused[1] = 0;
713 1.9 mycroft cmd.unused[2] = 0;
714 1.9 mycroft cmd.control = 0;
715 1.1 thorpej
716 1.1 thorpej /* Defect descriptor length. */
717 1.9 mycroft _lto2b(argc * 4, data->length);
718 1.1 thorpej
719 1.1 thorpej /* Build the defect descriptor list. */
720 1.1 thorpej for (i = 0; i < argc; i++) {
721 1.1 thorpej blkno = strtoul(argv[i], &cp, 10);
722 1.1 thorpej if (*cp != '\0')
723 1.12 ad errx(1, "invalid block number: %s", argv[i]);
724 1.9 mycroft _lto4b(blkno, data->defect_descriptor[i].dlbaddr);
725 1.1 thorpej }
726 1.1 thorpej
727 1.1 thorpej scsi_command(fd, &cmd, sizeof(cmd), data, dlen, 30000, SCCMD_WRITE);
728 1.1 thorpej
729 1.1 thorpej free(data);
730 1.1 thorpej return;
731 1.1 thorpej }
732 1.1 thorpej
733 1.1 thorpej /*
734 1.16 mjacob * device_release:
735 1.16 mjacob *
736 1.29 bouyer * Issue a RELEASE command to a SCSI device.
737 1.16 mjacob */
738 1.16 mjacob #ifndef SCSI_RELEASE
739 1.16 mjacob #define SCSI_RELEASE 0x17
740 1.16 mjacob #endif
741 1.35 jakllsch static void
742 1.26 xtraeme device_release(int argc, char *argv[])
743 1.16 mjacob {
744 1.27 thorpej struct scsi_test_unit_ready cmd; /* close enough */
745 1.16 mjacob
746 1.16 mjacob /* No arguments. */
747 1.16 mjacob if (argc != 0)
748 1.16 mjacob usage();
749 1.16 mjacob
750 1.16 mjacob memset(&cmd, 0, sizeof(cmd));
751 1.16 mjacob
752 1.16 mjacob cmd.opcode = SCSI_RELEASE;
753 1.16 mjacob
754 1.16 mjacob scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
755 1.16 mjacob
756 1.16 mjacob return;
757 1.16 mjacob }
758 1.16 mjacob
759 1.16 mjacob
760 1.16 mjacob
761 1.16 mjacob /*
762 1.16 mjacob * device_reserve:
763 1.16 mjacob *
764 1.29 bouyer * Issue a RESERVE command to a SCSI device.
765 1.16 mjacob */
766 1.16 mjacob #ifndef SCSI_RESERVE
767 1.16 mjacob #define SCSI_RESERVE 0x16
768 1.16 mjacob #endif
769 1.35 jakllsch static void
770 1.26 xtraeme device_reserve(int argc, char *argv[])
771 1.16 mjacob {
772 1.27 thorpej struct scsi_test_unit_ready cmd; /* close enough */
773 1.16 mjacob
774 1.16 mjacob /* No arguments. */
775 1.16 mjacob if (argc != 0)
776 1.16 mjacob usage();
777 1.16 mjacob
778 1.16 mjacob memset(&cmd, 0, sizeof(cmd));
779 1.16 mjacob
780 1.16 mjacob cmd.opcode = SCSI_RESERVE;
781 1.16 mjacob
782 1.16 mjacob scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
783 1.16 mjacob
784 1.16 mjacob return;
785 1.16 mjacob }
786 1.16 mjacob
787 1.16 mjacob /*
788 1.1 thorpej * device_reset:
789 1.1 thorpej *
790 1.1 thorpej * Issue a reset to a SCSI device.
791 1.1 thorpej */
792 1.35 jakllsch static void
793 1.26 xtraeme device_reset(int argc, char *argv[])
794 1.1 thorpej {
795 1.1 thorpej
796 1.1 thorpej /* No arguments. */
797 1.1 thorpej if (argc != 0)
798 1.6 hubertf usage();
799 1.1 thorpej
800 1.1 thorpej if (ioctl(fd, SCIOCRESET, NULL) != 0)
801 1.1 thorpej err(1, "SCIOCRESET");
802 1.19 petrov
803 1.19 petrov return;
804 1.19 petrov }
805 1.19 petrov
806 1.19 petrov /*
807 1.19 petrov * device_debug:
808 1.19 petrov *
809 1.19 petrov * Set debug level to a SCSI device.
810 1.19 petrov * scsipi will print anything iff SCSIPI_DEBUG set in config.
811 1.19 petrov */
812 1.35 jakllsch static void
813 1.26 xtraeme device_debug(int argc, char *argv[])
814 1.19 petrov {
815 1.19 petrov int lvl;
816 1.19 petrov
817 1.19 petrov if (argc < 1)
818 1.19 petrov usage();
819 1.19 petrov
820 1.19 petrov lvl = atoi(argv[0]);
821 1.19 petrov
822 1.19 petrov if (ioctl(fd, SCIOCDEBUG, &lvl) != 0)
823 1.19 petrov err(1, "SCIOCDEBUG");
824 1.1 thorpej
825 1.1 thorpej return;
826 1.1 thorpej }
827 1.1 thorpej
828 1.1 thorpej /*
829 1.18 thorpej * device_getcache:
830 1.18 thorpej *
831 1.18 thorpej * Get the caching parameters for a SCSI disk.
832 1.1 thorpej */
833 1.35 jakllsch static void
834 1.26 xtraeme device_getcache(int argc, char *argv[])
835 1.18 thorpej {
836 1.18 thorpej struct {
837 1.27 thorpej struct scsi_mode_parameter_header_6 header;
838 1.27 thorpej struct scsi_general_block_descriptor blk_desc;
839 1.18 thorpej struct page_caching caching_params;
840 1.18 thorpej } data;
841 1.18 thorpej
842 1.18 thorpej /* No arguments. */
843 1.18 thorpej if (argc != 0)
844 1.18 thorpej usage();
845 1.18 thorpej
846 1.18 thorpej scsi_mode_sense(fd, 0x08, 0x00, &data, sizeof(data));
847 1.18 thorpej
848 1.18 thorpej if ((data.caching_params.flags & (CACHING_RCD|CACHING_WCE)) ==
849 1.18 thorpej CACHING_RCD)
850 1.18 thorpej printf("%s: no caches enabled\n", dvname);
851 1.18 thorpej else {
852 1.18 thorpej printf("%s: read cache %senabled\n", dvname,
853 1.18 thorpej (data.caching_params.flags & CACHING_RCD) ? "not " : "");
854 1.18 thorpej printf("%s: write-back cache %senabled\n", dvname,
855 1.18 thorpej (data.caching_params.flags & CACHING_WCE) ? "" : "not ");
856 1.18 thorpej }
857 1.18 thorpej printf("%s: caching parameters are %ssavable\n", dvname,
858 1.18 thorpej (data.caching_params.pg_code & PGCODE_PS) ? "" : "not ");
859 1.18 thorpej }
860 1.1 thorpej
861 1.1 thorpej /*
862 1.18 thorpej * device_setcache:
863 1.1 thorpej *
864 1.18 thorpej * Set cache enables for a SCSI disk.
865 1.1 thorpej */
866 1.35 jakllsch static void
867 1.26 xtraeme device_setcache(int argc, char *argv[])
868 1.1 thorpej {
869 1.18 thorpej struct {
870 1.27 thorpej struct scsi_mode_parameter_header_6 header;
871 1.27 thorpej struct scsi_general_block_descriptor blk_desc;
872 1.18 thorpej struct page_caching caching_params;
873 1.18 thorpej } data;
874 1.18 thorpej int dlen;
875 1.18 thorpej u_int8_t flags, byte2;
876 1.18 thorpej
877 1.18 thorpej if (argc > 2 || argc == 0)
878 1.18 thorpej usage();
879 1.18 thorpej
880 1.28 lukem flags = 0;
881 1.28 lukem byte2 = 0;
882 1.18 thorpej if (strcmp(argv[0], "none") == 0)
883 1.18 thorpej flags = CACHING_RCD;
884 1.18 thorpej else if (strcmp(argv[0], "r") == 0)
885 1.18 thorpej flags = 0;
886 1.18 thorpej else if (strcmp(argv[0], "w") == 0)
887 1.18 thorpej flags = CACHING_RCD|CACHING_WCE;
888 1.18 thorpej else if (strcmp(argv[0], "rw") == 0)
889 1.18 thorpej flags = CACHING_WCE;
890 1.18 thorpej else
891 1.18 thorpej usage();
892 1.18 thorpej
893 1.18 thorpej if (argc == 2) {
894 1.18 thorpej if (strcmp(argv[1], "save") == 0)
895 1.18 thorpej byte2 = SMS_SP;
896 1.18 thorpej else
897 1.18 thorpej usage();
898 1.18 thorpej }
899 1.18 thorpej
900 1.18 thorpej scsi_mode_sense(fd, 0x08, 0x00, &data, sizeof(data));
901 1.18 thorpej
902 1.18 thorpej data.caching_params.pg_code &= PGCODE_MASK;
903 1.18 thorpej data.caching_params.flags =
904 1.18 thorpej (data.caching_params.flags & ~(CACHING_RCD|CACHING_WCE)) | flags;
905 1.1 thorpej
906 1.18 thorpej data.caching_params.cache_segment_size[0] = 0;
907 1.18 thorpej data.caching_params.cache_segment_size[1] = 0;
908 1.18 thorpej
909 1.18 thorpej data.header.data_length = 0;
910 1.1 thorpej
911 1.18 thorpej dlen = sizeof(data.header) + sizeof(data.blk_desc) + 2 +
912 1.18 thorpej data.caching_params.pg_length;
913 1.1 thorpej
914 1.18 thorpej scsi_mode_select(fd, byte2, &data, dlen);
915 1.21 mycroft }
916 1.21 mycroft
917 1.21 mycroft /*
918 1.21 mycroft * device_flushcache:
919 1.21 mycroft *
920 1.29 bouyer * Issue a FLUSH CACHE command to a SCSI device.
921 1.21 mycroft */
922 1.21 mycroft #ifndef SCSI_FLUSHCACHE
923 1.21 mycroft #define SCSI_FLUSHCACHE 0x35
924 1.21 mycroft #endif
925 1.35 jakllsch static void
926 1.26 xtraeme device_flushcache(int argc, char *argv[])
927 1.21 mycroft {
928 1.27 thorpej struct scsi_test_unit_ready cmd; /* close enough */
929 1.21 mycroft
930 1.21 mycroft /* No arguments. */
931 1.21 mycroft if (argc != 0)
932 1.21 mycroft usage();
933 1.21 mycroft
934 1.21 mycroft memset(&cmd, 0, sizeof(cmd));
935 1.21 mycroft
936 1.21 mycroft cmd.opcode = SCSI_FLUSHCACHE;
937 1.22 mycroft
938 1.22 mycroft scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
939 1.22 mycroft
940 1.22 mycroft return;
941 1.22 mycroft }
942 1.22 mycroft
943 1.22 mycroft /*
944 1.29 bouyer * device_setspeed:
945 1.29 bouyer *
946 1.29 bouyer * Set rotation speed to a CD/DVD drive.
947 1.29 bouyer */
948 1.35 jakllsch static void
949 1.29 bouyer device_setspeed(int argc, char *argv[])
950 1.29 bouyer {
951 1.29 bouyer u_char cmd[11];
952 1.29 bouyer u_char pd[28];
953 1.29 bouyer u_int32_t speed;
954 1.29 bouyer
955 1.29 bouyer if (argc != 1)
956 1.29 bouyer usage();
957 1.29 bouyer
958 1.29 bouyer speed = atoi(argv[0]) * 177;
959 1.29 bouyer
960 1.29 bouyer memset(&pd, 0, sizeof(pd));
961 1.29 bouyer if (speed == 0)
962 1.29 bouyer pd[0] = 4; /* restore drive defaults */
963 1.29 bouyer pd[8] = 0xff;
964 1.29 bouyer pd[9] = 0xff;
965 1.29 bouyer pd[10] = 0xff;
966 1.29 bouyer pd[11] = 0xff;
967 1.29 bouyer pd[12] = pd[20] = (speed >> 24) & 0xff;
968 1.29 bouyer pd[13] = pd[21] = (speed >> 16) & 0xff;
969 1.29 bouyer pd[14] = pd[22] = (speed >> 8) & 0xff;
970 1.29 bouyer pd[15] = pd[23] = speed & 0xff;
971 1.29 bouyer pd[18] = pd[26] = 1000 >> 8;
972 1.29 bouyer pd[19] = pd[27] = 1000 & 0xff;
973 1.29 bouyer
974 1.29 bouyer memset(&cmd, 0, sizeof(cmd));
975 1.29 bouyer cmd[0] = 0xb6;
976 1.29 bouyer cmd[10] = sizeof(pd);
977 1.29 bouyer
978 1.29 bouyer scsi_command(fd, &cmd, sizeof(cmd), pd, sizeof(pd), 10000, SCCMD_WRITE);
979 1.29 bouyer
980 1.29 bouyer return;
981 1.29 bouyer }
982 1.29 bouyer
983 1.29 bouyer /*
984 1.22 mycroft * device_prevent:
985 1.22 mycroft *
986 1.22 mycroft * Issue a prevent to a SCSI device.
987 1.22 mycroft */
988 1.35 jakllsch static void
989 1.26 xtraeme device_prevent(int argc, char *argv[])
990 1.22 mycroft {
991 1.27 thorpej struct scsi_prevent_allow_medium_removal cmd;
992 1.22 mycroft
993 1.22 mycroft /* No arguments. */
994 1.22 mycroft if (argc != 0)
995 1.22 mycroft usage();
996 1.22 mycroft
997 1.22 mycroft memset(&cmd, 0, sizeof(cmd));
998 1.22 mycroft
999 1.27 thorpej cmd.opcode = SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL;
1000 1.27 thorpej cmd.how = SPAMR_PREVENT_DT; /* XXX SMAMR_PREVENT_ALL? */
1001 1.22 mycroft
1002 1.22 mycroft scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
1003 1.22 mycroft
1004 1.22 mycroft return;
1005 1.22 mycroft }
1006 1.22 mycroft
1007 1.22 mycroft /*
1008 1.22 mycroft * device_allow:
1009 1.22 mycroft *
1010 1.22 mycroft * Issue a stop to a SCSI device.
1011 1.22 mycroft */
1012 1.35 jakllsch static void
1013 1.26 xtraeme device_allow(int argc, char *argv[])
1014 1.22 mycroft {
1015 1.27 thorpej struct scsi_prevent_allow_medium_removal cmd;
1016 1.22 mycroft
1017 1.22 mycroft /* No arguments. */
1018 1.22 mycroft if (argc != 0)
1019 1.22 mycroft usage();
1020 1.22 mycroft
1021 1.22 mycroft memset(&cmd, 0, sizeof(cmd));
1022 1.22 mycroft
1023 1.27 thorpej cmd.opcode = SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL;
1024 1.27 thorpej cmd.how = SPAMR_ALLOW;
1025 1.21 mycroft
1026 1.21 mycroft scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
1027 1.21 mycroft
1028 1.21 mycroft return;
1029 1.1 thorpej }
1030 1.16 mjacob
1031 1.16 mjacob /*
1032 1.16 mjacob * device_start:
1033 1.16 mjacob *
1034 1.16 mjacob * Issue a start to a SCSI device.
1035 1.16 mjacob */
1036 1.35 jakllsch static void
1037 1.26 xtraeme device_start(int argc, char *argv[])
1038 1.16 mjacob {
1039 1.16 mjacob struct scsipi_start_stop cmd;
1040 1.16 mjacob
1041 1.16 mjacob /* No arguments. */
1042 1.16 mjacob if (argc != 0)
1043 1.16 mjacob usage();
1044 1.16 mjacob
1045 1.16 mjacob memset(&cmd, 0, sizeof(cmd));
1046 1.16 mjacob
1047 1.16 mjacob cmd.opcode = START_STOP;
1048 1.16 mjacob cmd.how = SSS_START;
1049 1.16 mjacob
1050 1.24 fair scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 30000, 0);
1051 1.16 mjacob
1052 1.16 mjacob return;
1053 1.16 mjacob }
1054 1.16 mjacob
1055 1.16 mjacob /*
1056 1.16 mjacob * device_stop:
1057 1.16 mjacob *
1058 1.16 mjacob * Issue a stop to a SCSI device.
1059 1.16 mjacob */
1060 1.35 jakllsch static void
1061 1.26 xtraeme device_stop(int argc, char *argv[])
1062 1.16 mjacob {
1063 1.16 mjacob struct scsipi_start_stop cmd;
1064 1.16 mjacob
1065 1.16 mjacob /* No arguments. */
1066 1.16 mjacob if (argc != 0)
1067 1.16 mjacob usage();
1068 1.16 mjacob
1069 1.16 mjacob memset(&cmd, 0, sizeof(cmd));
1070 1.16 mjacob
1071 1.16 mjacob cmd.opcode = START_STOP;
1072 1.16 mjacob cmd.how = SSS_STOP;
1073 1.16 mjacob
1074 1.24 fair scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 30000, 0);
1075 1.16 mjacob
1076 1.16 mjacob return;
1077 1.16 mjacob }
1078 1.16 mjacob
1079 1.16 mjacob /*
1080 1.16 mjacob * device_tur:
1081 1.16 mjacob *
1082 1.29 bouyer * Issue a TEST UNIT READY to a SCSI device.
1083 1.16 mjacob */
1084 1.35 jakllsch static void
1085 1.26 xtraeme device_tur(int argc, char *argv[])
1086 1.16 mjacob {
1087 1.27 thorpej struct scsi_test_unit_ready cmd;
1088 1.16 mjacob
1089 1.16 mjacob /* No arguments. */
1090 1.16 mjacob if (argc != 0)
1091 1.16 mjacob usage();
1092 1.16 mjacob
1093 1.16 mjacob memset(&cmd, 0, sizeof(cmd));
1094 1.16 mjacob
1095 1.27 thorpej cmd.opcode = SCSI_TEST_UNIT_READY;
1096 1.16 mjacob
1097 1.16 mjacob scsi_command(fd, &cmd, sizeof(cmd), NULL, 0, 10000, 0);
1098 1.16 mjacob
1099 1.16 mjacob return;
1100 1.16 mjacob }
1101 1.16 mjacob
1102 1.18 thorpej /*
1103 1.18 thorpej * BUS COMMANDS
1104 1.18 thorpej */
1105 1.18 thorpej
1106 1.18 thorpej /*
1107 1.18 thorpej * bus_reset:
1108 1.18 thorpej *
1109 1.18 thorpej * Issue a reset to a SCSI bus.
1110 1.18 thorpej */
1111 1.35 jakllsch static void
1112 1.26 xtraeme bus_reset(int argc, char *argv[])
1113 1.18 thorpej {
1114 1.18 thorpej
1115 1.18 thorpej /* No arguments. */
1116 1.18 thorpej if (argc != 0)
1117 1.18 thorpej usage();
1118 1.16 mjacob
1119 1.18 thorpej if (ioctl(fd, SCBUSIORESET, NULL) != 0)
1120 1.18 thorpej err(1, "SCBUSIORESET");
1121 1.18 thorpej
1122 1.18 thorpej return;
1123 1.18 thorpej }
1124 1.1 thorpej
1125 1.1 thorpej /*
1126 1.1 thorpej * bus_scan:
1127 1.1 thorpej *
1128 1.1 thorpej * Rescan a SCSI bus for new devices.
1129 1.1 thorpej */
1130 1.35 jakllsch static void
1131 1.26 xtraeme bus_scan(int argc, char *argv[])
1132 1.1 thorpej {
1133 1.1 thorpej struct scbusioscan_args args;
1134 1.1 thorpej char *cp;
1135 1.1 thorpej
1136 1.1 thorpej /* Must have two args: target lun */
1137 1.1 thorpej if (argc != 2)
1138 1.6 hubertf usage();
1139 1.1 thorpej
1140 1.8 ad if (strcmp(argv[0], "any") == 0 || strcmp(argv[0], "all") == 0)
1141 1.1 thorpej args.sa_target = -1;
1142 1.1 thorpej else {
1143 1.1 thorpej args.sa_target = strtol(argv[0], &cp, 10);
1144 1.1 thorpej if (*cp != '\0' || args.sa_target < 0)
1145 1.12 ad errx(1, "invalid target: %s", argv[0]);
1146 1.1 thorpej }
1147 1.1 thorpej
1148 1.8 ad if (strcmp(argv[1], "any") == 0 || strcmp(argv[1], "all") == 0)
1149 1.1 thorpej args.sa_lun = -1;
1150 1.1 thorpej else {
1151 1.1 thorpej args.sa_lun = strtol(argv[1], &cp, 10);
1152 1.1 thorpej if (*cp != '\0' || args.sa_lun < 0)
1153 1.12 ad errx(1, "invalid lun: %s", argv[1]);
1154 1.1 thorpej }
1155 1.1 thorpej
1156 1.1 thorpej if (ioctl(fd, SCBUSIOSCAN, &args) != 0)
1157 1.1 thorpej err(1, "SCBUSIOSCAN");
1158 1.14 bouyer
1159 1.14 bouyer return;
1160 1.14 bouyer }
1161 1.14 bouyer
1162 1.14 bouyer /*
1163 1.14 bouyer * bus_detach:
1164 1.14 bouyer *
1165 1.14 bouyer * detach SCSI devices from a bus.
1166 1.14 bouyer */
1167 1.35 jakllsch static void
1168 1.26 xtraeme bus_detach(int argc, char *argv[])
1169 1.14 bouyer {
1170 1.14 bouyer struct scbusiodetach_args args;
1171 1.14 bouyer char *cp;
1172 1.14 bouyer
1173 1.14 bouyer /* Must have two args: target lun */
1174 1.14 bouyer if (argc != 2)
1175 1.14 bouyer usage();
1176 1.14 bouyer
1177 1.14 bouyer if (strcmp(argv[0], "any") == 0 || strcmp(argv[0], "all") == 0)
1178 1.14 bouyer args.sa_target = -1;
1179 1.14 bouyer else {
1180 1.14 bouyer args.sa_target = strtol(argv[0], &cp, 10);
1181 1.14 bouyer if (*cp != '\0' || args.sa_target < 0)
1182 1.17 grant errx(1, "invalid target: %s", argv[0]);
1183 1.14 bouyer }
1184 1.14 bouyer
1185 1.14 bouyer if (strcmp(argv[1], "any") == 0 || strcmp(argv[1], "all") == 0)
1186 1.14 bouyer args.sa_lun = -1;
1187 1.14 bouyer else {
1188 1.14 bouyer args.sa_lun = strtol(argv[1], &cp, 10);
1189 1.14 bouyer if (*cp != '\0' || args.sa_lun < 0)
1190 1.17 grant errx(1, "invalid lun: %s", argv[1]);
1191 1.14 bouyer }
1192 1.14 bouyer
1193 1.14 bouyer if (ioctl(fd, SCBUSIODETACH, &args) != 0)
1194 1.14 bouyer err(1, "SCBUSIODETACH");
1195 1.1 thorpej
1196 1.1 thorpej return;
1197 1.1 thorpej }
1198