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