2 * Copyright (c) 1997-2007 Kenneth D. Merry
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sbin/camcontrol/camcontrol.c,v 1.21.2.13 2003/01/08 17:55:02 njl Exp $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
43 #include <cam/cam_debug.h>
44 #include <cam/cam_ccb.h>
45 #include <cam/scsi/scsi_all.h>
46 #include <cam/scsi/scsi_da.h>
47 #include <cam/scsi/scsi_pass.h>
48 #include <cam/scsi/scsi_message.h>
51 #include "camcontrol.h"
54 CAM_CMD_NONE
= 0x00000000,
55 CAM_CMD_DEVLIST
= 0x00000001,
56 CAM_CMD_TUR
= 0x00000002,
57 CAM_CMD_INQUIRY
= 0x00000003,
58 CAM_CMD_STARTSTOP
= 0x00000004,
59 CAM_CMD_RESCAN
= 0x00000005,
60 CAM_CMD_READ_DEFECTS
= 0x00000006,
61 CAM_CMD_MODE_PAGE
= 0x00000007,
62 CAM_CMD_SCSI_CMD
= 0x00000008,
63 CAM_CMD_DEVTREE
= 0x00000009,
64 CAM_CMD_USAGE
= 0x0000000a,
65 CAM_CMD_DEBUG
= 0x0000000b,
66 CAM_CMD_RESET
= 0x0000000c,
67 CAM_CMD_FORMAT
= 0x0000000d,
68 CAM_CMD_TAG
= 0x0000000e,
69 CAM_CMD_RATE
= 0x0000000f,
70 CAM_CMD_DETACH
= 0x00000010,
71 CAM_CMD_REPORTLUNS
= 0x00000011,
72 CAM_CMD_READCAP
= 0x00000012,
73 CAM_CMD_IDENTIFY
= 0x00000013,
74 CAM_CMD_IDLE
= 0x00000014,
75 CAM_CMD_STANDBY
= 0x00000015,
76 CAM_CMD_SLEEP
= 0x00000016,
77 CAM_CMD_SMP_CMD
= 0x00000017,
78 CAM_CMD_SMP_RG
= 0x00000018,
79 CAM_CMD_SMP_PC
= 0x00000019,
80 CAM_CMD_SMP_PHYLIST
= 0x0000001a,
81 CAM_CMD_SMP_MANINFO
= 0x0000001b,
82 CAM_CMD_DOWNLOAD_FW
= 0x0000001c,
83 CAM_CMD_SECURITY
= 0x0000001d,
84 CAM_CMD_HPA
= 0x0000001e,
85 CAM_CMD_SANITIZE
= 0x0000001f,
86 CAM_CMD_PERSIST
= 0x00000020
90 CAM_ARG_NONE
= 0x00000000,
91 CAM_ARG_VERBOSE
= 0x00000001,
92 CAM_ARG_DEVICE
= 0x00000002,
93 CAM_ARG_BUS
= 0x00000004,
94 CAM_ARG_TARGET
= 0x00000008,
95 CAM_ARG_LUN
= 0x00000010,
96 CAM_ARG_EJECT
= 0x00000020,
97 CAM_ARG_UNIT
= 0x00000040,
98 CAM_ARG_FORMAT_BLOCK
= 0x00000080,
99 CAM_ARG_FORMAT_BFI
= 0x00000100,
100 CAM_ARG_FORMAT_PHYS
= 0x00000200,
101 CAM_ARG_PLIST
= 0x00000400,
102 CAM_ARG_GLIST
= 0x00000800,
103 CAM_ARG_GET_SERIAL
= 0x00001000,
104 CAM_ARG_GET_STDINQ
= 0x00002000,
105 CAM_ARG_GET_XFERRATE
= 0x00004000,
106 CAM_ARG_INQ_MASK
= 0x00007000,
107 CAM_ARG_MODE_EDIT
= 0x00008000,
108 CAM_ARG_PAGE_CNTL
= 0x00010000,
109 CAM_ARG_TIMEOUT
= 0x00020000,
110 CAM_ARG_CMD_IN
= 0x00040000,
111 CAM_ARG_CMD_OUT
= 0x00080000,
112 CAM_ARG_DBD
= 0x00100000,
113 CAM_ARG_ERR_RECOVER
= 0x00200000,
114 CAM_ARG_RETRIES
= 0x00400000,
115 CAM_ARG_START_UNIT
= 0x00800000,
116 CAM_ARG_DEBUG_INFO
= 0x01000000,
117 CAM_ARG_DEBUG_TRACE
= 0x02000000,
118 CAM_ARG_DEBUG_SUBTRACE
= 0x04000000,
119 CAM_ARG_DEBUG_CDB
= 0x08000000,
120 CAM_ARG_DEBUG_XPT
= 0x10000000,
121 CAM_ARG_DEBUG_PERIPH
= 0x20000000,
124 struct camcontrol_opts
{
132 static const char scsicmd_opts
[] = "c:i:o:";
133 static const char readdefect_opts
[] = "f:GP";
134 static const char negotiate_opts
[] = "acD:O:qR:T:UW:";
137 struct camcontrol_opts option_table
[] = {
139 {"tur", CAM_CMD_TUR
, CAM_ARG_NONE
, NULL
},
140 {"inquiry", CAM_CMD_INQUIRY
, CAM_ARG_NONE
, "DSR"},
141 {"start", CAM_CMD_STARTSTOP
, CAM_ARG_START_UNIT
, NULL
},
142 {"stop", CAM_CMD_STARTSTOP
, CAM_ARG_NONE
, NULL
},
143 {"load", CAM_CMD_STARTSTOP
, CAM_ARG_START_UNIT
| CAM_ARG_EJECT
, NULL
},
144 {"eject", CAM_CMD_STARTSTOP
, CAM_ARG_EJECT
, NULL
},
145 {"reportluns", CAM_CMD_REPORTLUNS
, CAM_ARG_NONE
, "clr:"},
146 {"readcapacity", CAM_CMD_READCAP
, CAM_ARG_NONE
, "bhHNqs"},
147 #endif /* MINIMALISTIC */
148 {"rescan", CAM_CMD_RESCAN
, CAM_ARG_NONE
, NULL
},
149 {"reset", CAM_CMD_RESET
, CAM_ARG_NONE
, NULL
},
151 {"cmd", CAM_CMD_SCSI_CMD
, CAM_ARG_NONE
, scsicmd_opts
},
152 {"command", CAM_CMD_SCSI_CMD
, CAM_ARG_NONE
, scsicmd_opts
},
153 {"defects", CAM_CMD_READ_DEFECTS
, CAM_ARG_NONE
, readdefect_opts
},
154 {"defectlist", CAM_CMD_READ_DEFECTS
, CAM_ARG_NONE
, readdefect_opts
},
155 #endif /* MINIMALISTIC */
156 {"devlist", CAM_CMD_DEVTREE
, CAM_ARG_NONE
, "-b"},
158 {"periphlist", CAM_CMD_DEVLIST
, CAM_ARG_NONE
, NULL
},
159 {"modepage", CAM_CMD_MODE_PAGE
, CAM_ARG_NONE
, "bdelm:P:"},
160 {"tags", CAM_CMD_TAG
, CAM_ARG_NONE
, "N:q"},
161 {"negotiate", CAM_CMD_RATE
, CAM_ARG_NONE
, negotiate_opts
},
162 {"rate", CAM_CMD_RATE
, CAM_ARG_NONE
, negotiate_opts
},
163 {"debug", CAM_CMD_DEBUG
, CAM_ARG_NONE
, "IPTSXc"},
164 {"format", CAM_CMD_FORMAT
, CAM_ARG_NONE
, "qrwy"},
166 {"sanitize", CAM_CMD_SANITIZE
, CAM_ARG_NONE
, "a:c:IP:qrUwy"},
168 {"idle", CAM_CMD_IDLE
, CAM_ARG_NONE
, "t:"},
169 {"standby", CAM_CMD_STANDBY
, CAM_ARG_NONE
, "t:"},
170 {"sleep", CAM_CMD_SLEEP
, CAM_ARG_NONE
, ""},
172 {"fwdownload", CAM_CMD_DOWNLOAD_FW
, CAM_ARG_NONE
, "f:ys"},
173 {"security", CAM_CMD_SECURITY
, CAM_ARG_NONE
, "d:e:fh:k:l:qs:T:U:y"},
174 {"hpa", CAM_CMD_HPA
, CAM_ARG_NONE
, "Pflp:qs:U:y"},
175 {"persist", CAM_CMD_PERSIST
, CAM_ARG_NONE
, "ai:I:k:K:o:ps:ST:U"},
177 #endif /* MINIMALISTIC */
178 {"help", CAM_CMD_USAGE
, CAM_ARG_NONE
, NULL
},
179 {"-?", CAM_CMD_USAGE
, CAM_ARG_NONE
, NULL
},
180 {"-h", CAM_CMD_USAGE
, CAM_ARG_NONE
, NULL
},
192 int bus
, target
, lun
;
195 camcontrol_optret
getoption(char *, cam_cmdmask
*, cam_argmask
*,
198 static int getdevlist(struct cam_device
*);
199 static int getdevtree(int, char **, char *);
200 static int testunitready(struct cam_device
*, int, int, int);
201 static int scsistart(struct cam_device
*, int, int, int, int);
202 static int scsidoinquiry(struct cam_device
*, int, char **, char *,
204 static int scsiinquiry(struct cam_device
*, int, int);
205 static int scsiserial(struct cam_device
*, int, int);
206 static int scsixferrate(struct cam_device
*);
207 #endif /* MINIMALISTIC */
208 static int parse_btl(char *, int *, int *, int *, cam_argmask
*);
209 static int dorescan_or_reset(int, char **, int);
210 static int rescan_or_reset_bus(int, int);
211 static int scanlun_or_reset_dev(int, int, int, int);
213 static int readdefects(struct cam_device
*, int, char **, char *,
215 static void modepage(struct cam_device
*, int, char **, char *, int, int);
216 static int scsicmd(struct cam_device
*, int, char **, char *, int, int);
217 static int tagcontrol(struct cam_device
*, int, char **, char *);
218 static void cts_print(struct cam_device
*device
,
219 struct ccb_trans_settings
*);
220 static void cpi_print(struct ccb_pathinq
*);
221 static int get_cpi(struct cam_device
*, struct ccb_pathinq
*);
222 static int get_print_cts(struct cam_device
*, int, int,
223 struct ccb_trans_settings
*);
224 static int ratecontrol(struct cam_device
*, int, int, int, char **,
226 static int scsiformat(struct cam_device
*, int, char **, char *, int, int);
228 static int scsisanitize(struct cam_device
*device
, int argc
, char **argv
,
229 char *combinedopt
, int retry_count
,
232 static int scsireportluns(struct cam_device
*device
, int argc
, char **argv
,
233 char *combinedopt
, int retry_count
,
235 static int scsireadcapacity(struct cam_device
*device
, int argc
,
236 char **argv
, char *combinedopt
,
237 int retry_count
, int timeout
);
238 static int atapm(struct cam_device
*device
, int argc
, char **argv
,
239 char *combinedopt
, int retry_count
,
242 static int atasecurity(struct cam_device
*device
, int retry_count
,
243 int timeout
, int argc
, char **argv
,
245 static int atahpa(struct cam_device
*device
, int retry_count
,
246 int timeout
, int argc
, char **argv
,
249 #endif /* MINIMALISTIC */
253 getoption(char *arg
, cam_cmdmask
*cmdnum
, cam_argmask
*argnum
,
256 struct camcontrol_opts
*opts
;
259 for (opts
= option_table
; (opts
!= NULL
) && (opts
->optname
!= NULL
);
261 if (strncmp(opts
->optname
, arg
, strlen(arg
)) == 0) {
262 *cmdnum
= opts
->cmdnum
;
263 *argnum
= opts
->argnum
;
264 *subopt
= opts
->subopt
;
265 if (++num_matches
> 1)
266 return(CC_OR_AMBIGUOUS
);
273 return(CC_OR_NOT_FOUND
);
278 getdevlist(struct cam_device
*device
)
284 ccb
= cam_getccb(device
);
286 ccb
->ccb_h
.func_code
= XPT_GDEVLIST
;
287 ccb
->ccb_h
.flags
= CAM_DIR_NONE
;
288 ccb
->ccb_h
.retry_count
= 1;
290 ccb
->cgdl
.status
= CAM_GDEVLIST_MORE_DEVS
;
291 while (ccb
->cgdl
.status
== CAM_GDEVLIST_MORE_DEVS
) {
292 if (cam_send_ccb(device
, ccb
) < 0) {
293 perror("error getting device list");
300 switch (ccb
->cgdl
.status
) {
301 case CAM_GDEVLIST_MORE_DEVS
:
302 strcpy(status
, "MORE");
304 case CAM_GDEVLIST_LAST_DEVICE
:
305 strcpy(status
, "LAST");
307 case CAM_GDEVLIST_LIST_CHANGED
:
308 strcpy(status
, "CHANGED");
310 case CAM_GDEVLIST_ERROR
:
311 strcpy(status
, "ERROR");
316 fprintf(stdout
, "%s%d: generation: %d index: %d status: %s\n",
317 ccb
->cgdl
.periph_name
,
318 ccb
->cgdl
.unit_number
,
319 ccb
->cgdl
.generation
,
324 * If the list has changed, we need to start over from the
327 if (ccb
->cgdl
.status
== CAM_GDEVLIST_LIST_CHANGED
)
335 #endif /* MINIMALISTIC */
338 getdevtree(int argc
, char **argv
, char *combinedopt
)
349 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
352 if ((arglist
& CAM_ARG_VERBOSE
) == 0)
360 if ((fd
= open(XPT_DEVICE
, O_RDWR
)) == -1) {
361 warn("couldn't open %s", XPT_DEVICE
);
365 bzero(&ccb
, sizeof(union ccb
));
367 ccb
.ccb_h
.path_id
= CAM_XPT_PATH_ID
;
368 ccb
.ccb_h
.target_id
= CAM_TARGET_WILDCARD
;
369 ccb
.ccb_h
.target_lun
= CAM_LUN_WILDCARD
;
371 ccb
.ccb_h
.func_code
= XPT_DEV_MATCH
;
372 bufsize
= sizeof(struct dev_match_result
) * 100;
373 ccb
.cdm
.match_buf_len
= bufsize
;
374 ccb
.cdm
.matches
= (struct dev_match_result
*)malloc(bufsize
);
375 if (ccb
.cdm
.matches
== NULL
) {
376 warnx("can't malloc memory for matches");
380 ccb
.cdm
.num_matches
= 0;
383 * We fetch all nodes, since we display most of them in the default
384 * case, and all in the verbose case.
386 ccb
.cdm
.num_patterns
= 0;
387 ccb
.cdm
.pattern_buf_len
= 0;
390 * We do the ioctl multiple times if necessary, in case there are
391 * more than 100 nodes in the EDT.
394 if (ioctl(fd
, CAMIOCOMMAND
, &ccb
) == -1) {
395 warn("error sending CAMIOCOMMAND ioctl");
400 if ((ccb
.ccb_h
.status
!= CAM_REQ_CMP
)
401 || ((ccb
.cdm
.status
!= CAM_DEV_MATCH_LAST
)
402 && (ccb
.cdm
.status
!= CAM_DEV_MATCH_MORE
))) {
403 warnx("got CAM error %#x, CDM error %d\n",
404 ccb
.ccb_h
.status
, ccb
.cdm
.status
);
409 for (i
= 0; i
< ccb
.cdm
.num_matches
; i
++) {
410 switch (ccb
.cdm
.matches
[i
].type
) {
411 case DEV_MATCH_BUS
: {
412 struct bus_match_result
*bus_result
;
415 * Only print the bus information if the
416 * user turns on the verbose flag.
418 if ((busonly
== 0) &&
419 (arglist
& CAM_ARG_VERBOSE
) == 0)
423 &ccb
.cdm
.matches
[i
].result
.bus_result
;
426 fprintf(stdout
, ")\n");
430 fprintf(stdout
, "scbus%d on %s%d bus %d%s\n",
432 bus_result
->dev_name
,
433 bus_result
->unit_number
,
435 (busonly
? "" : ":"));
438 case DEV_MATCH_DEVICE
: {
439 struct device_match_result
*dev_result
;
440 char vendor
[16], product
[48], revision
[16];
447 &ccb
.cdm
.matches
[i
].result
.device_result
;
449 if ((dev_result
->flags
450 & DEV_RESULT_UNCONFIGURED
)
451 && ((arglist
& CAM_ARG_VERBOSE
) == 0)) {
457 cam_strvis(vendor
, dev_result
->inq_data
.vendor
,
458 sizeof(dev_result
->inq_data
.vendor
),
461 dev_result
->inq_data
.product
,
462 sizeof(dev_result
->inq_data
.product
),
465 dev_result
->inq_data
.revision
,
466 sizeof(dev_result
->inq_data
.revision
),
468 sprintf(tmpstr
, "<%s %s %s>", vendor
, product
,
471 fprintf(stdout
, ")\n");
475 fprintf(stdout
, "%-33s at scbus%d "
476 "target %d lun %d (",
479 dev_result
->target_id
,
480 dev_result
->target_lun
);
486 case DEV_MATCH_PERIPH
: {
487 struct periph_match_result
*periph_result
;
490 &ccb
.cdm
.matches
[i
].result
.periph_result
;
492 if (busonly
|| skip_device
!= 0)
496 fprintf(stdout
, ",");
498 fprintf(stdout
, "%s%d",
499 periph_result
->periph_name
,
500 periph_result
->unit_number
);
506 fprintf(stdout
, "unknown match type\n");
511 } while ((ccb
.ccb_h
.status
== CAM_REQ_CMP
)
512 && (ccb
.cdm
.status
== CAM_DEV_MATCH_MORE
));
515 fprintf(stdout
, ")\n");
524 testunitready(struct cam_device
*device
, int retry_count
, int timeout
,
530 ccb
= cam_getccb(device
);
532 scsi_test_unit_ready(&ccb
->csio
,
533 /* retries */ retry_count
,
535 /* tag_action */ MSG_SIMPLE_Q_TAG
,
536 /* sense_len */ SSD_FULL_SIZE
,
537 /* timeout */ timeout
? timeout
: 5000);
539 /* Disable freezing the device queue */
540 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
542 if (arglist
& CAM_ARG_ERR_RECOVER
)
543 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
545 if (cam_send_ccb(device
, ccb
) < 0) {
547 perror("error sending test unit ready");
549 if (arglist
& CAM_ARG_VERBOSE
) {
550 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
551 CAM_EPF_ALL
, stderr
);
558 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
560 fprintf(stdout
, "Unit is ready\n");
563 fprintf(stdout
, "Unit is not ready\n");
566 if (arglist
& CAM_ARG_VERBOSE
) {
567 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
568 CAM_EPF_ALL
, stderr
);
578 scsistart(struct cam_device
*device
, int startstop
, int loadeject
,
579 int retry_count
, int timeout
)
584 ccb
= cam_getccb(device
);
587 * If we're stopping, send an ordered tag so the drive in question
588 * will finish any previously queued writes before stopping. If
589 * the device isn't capable of tagged queueing, or if tagged
590 * queueing is turned off, the tag action is a no-op.
592 scsi_start_stop(&ccb
->csio
,
593 /* retries */ retry_count
,
595 /* tag_action */ startstop
? MSG_SIMPLE_Q_TAG
:
597 /* start/stop */ startstop
,
598 /* load_eject */ loadeject
,
600 /* sense_len */ SSD_FULL_SIZE
,
601 /* timeout */ timeout
? timeout
: 120000);
603 /* Disable freezing the device queue */
604 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
606 if (arglist
& CAM_ARG_ERR_RECOVER
)
607 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
609 if (cam_send_ccb(device
, ccb
) < 0) {
610 perror("error sending start unit");
612 if (arglist
& CAM_ARG_VERBOSE
) {
613 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
614 CAM_EPF_ALL
, stderr
);
621 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
)
623 fprintf(stdout
, "Unit started successfully");
625 fprintf(stdout
,", Media loaded\n");
627 fprintf(stdout
,"\n");
629 fprintf(stdout
, "Unit stopped successfully");
631 fprintf(stdout
, ", Media ejected\n");
633 fprintf(stdout
, "\n");
639 "Error received from start unit command\n");
642 "Error received from stop unit command\n");
644 if (arglist
& CAM_ARG_VERBOSE
) {
645 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
646 CAM_EPF_ALL
, stderr
);
656 scsidoinquiry(struct cam_device
*device
, int argc
, char **argv
,
657 char *combinedopt
, int retry_count
, int timeout
)
662 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
665 arglist
|= CAM_ARG_GET_STDINQ
;
668 arglist
|= CAM_ARG_GET_XFERRATE
;
671 arglist
|= CAM_ARG_GET_SERIAL
;
679 * If the user didn't specify any inquiry options, he wants all of
682 if ((arglist
& CAM_ARG_INQ_MASK
) == 0)
683 arglist
|= CAM_ARG_INQ_MASK
;
685 if (arglist
& CAM_ARG_GET_STDINQ
)
686 error
= scsiinquiry(device
, retry_count
, timeout
);
691 if (arglist
& CAM_ARG_GET_SERIAL
)
692 scsiserial(device
, retry_count
, timeout
);
697 if (arglist
& CAM_ARG_GET_XFERRATE
)
698 error
= scsixferrate(device
);
704 scsiinquiry(struct cam_device
*device
, int retry_count
, int timeout
)
707 struct scsi_inquiry_data
*inq_buf
;
710 ccb
= cam_getccb(device
);
713 warnx("couldn't allocate CCB");
717 /* cam_getccb cleans up the header, caller has to zero the payload */
718 bzero(&(&ccb
->ccb_h
)[1],
719 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
721 inq_buf
= (struct scsi_inquiry_data
*)malloc(
722 sizeof(struct scsi_inquiry_data
));
724 if (inq_buf
== NULL
) {
726 warnx("can't malloc memory for inquiry\n");
729 bzero(inq_buf
, sizeof(*inq_buf
));
732 * Note that although the size of the inquiry buffer is the full
733 * 256 bytes specified in the SCSI spec, we only tell the device
734 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
735 * two reasons for this:
737 * - The SCSI spec says that when a length field is only 1 byte,
738 * a value of 0 will be interpreted as 256. Therefore
739 * scsi_inquiry() will convert an inq_len (which is passed in as
740 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
741 * to 0. Evidently, very few devices meet the spec in that
742 * regard. Some devices, like many Seagate disks, take the 0 as
743 * 0, and don't return any data. One Pioneer DVD-R drive
744 * returns more data than the command asked for.
746 * So, since there are numerous devices that just don't work
747 * right with the full inquiry size, we don't send the full size.
749 * - The second reason not to use the full inquiry data length is
750 * that we don't need it here. The only reason we issue a
751 * standard inquiry is to get the vendor name, device name,
752 * and revision so scsi_print_inquiry() can print them.
754 * If, at some point in the future, more inquiry data is needed for
755 * some reason, this code should use a procedure similar to the
756 * probe code. i.e., issue a short inquiry, and determine from
757 * the additional length passed back from the device how much
758 * inquiry data the device supports. Once the amount the device
759 * supports is determined, issue an inquiry for that amount and no
764 scsi_inquiry(&ccb
->csio
,
765 /* retries */ retry_count
,
767 /* tag_action */ MSG_SIMPLE_Q_TAG
,
768 /* inq_buf */ (u_int8_t
*)inq_buf
,
769 /* inq_len */ SHORT_INQUIRY_LENGTH
,
772 /* sense_len */ SSD_FULL_SIZE
,
773 /* timeout */ timeout
? timeout
: 5000);
775 /* Disable freezing the device queue */
776 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
778 if (arglist
& CAM_ARG_ERR_RECOVER
)
779 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
781 if (cam_send_ccb(device
, ccb
) < 0) {
782 perror("error sending SCSI inquiry");
784 if (arglist
& CAM_ARG_VERBOSE
) {
785 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
786 CAM_EPF_ALL
, stderr
);
793 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
796 if (arglist
& CAM_ARG_VERBOSE
) {
797 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
798 CAM_EPF_ALL
, stderr
);
809 fprintf(stdout
, "%s%d: ", device
->device_name
,
810 device
->dev_unit_num
);
811 scsi_print_inquiry(inq_buf
);
819 scsiserial(struct cam_device
*device
, int retry_count
, int timeout
)
822 struct scsi_vpd_unit_serial_number
*serial_buf
;
823 char serial_num
[SVPD_SERIAL_NUM_SIZE
+ 1];
826 ccb
= cam_getccb(device
);
829 warnx("couldn't allocate CCB");
833 /* cam_getccb cleans up the header, caller has to zero the payload */
834 bzero(&(&ccb
->ccb_h
)[1],
835 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
837 serial_buf
= (struct scsi_vpd_unit_serial_number
*)
838 malloc(sizeof(*serial_buf
));
840 if (serial_buf
== NULL
) {
842 warnx("can't malloc memory for serial number");
846 scsi_inquiry(&ccb
->csio
,
847 /*retries*/ retry_count
,
849 /* tag_action */ MSG_SIMPLE_Q_TAG
,
850 /* inq_buf */ (u_int8_t
*)serial_buf
,
851 /* inq_len */ sizeof(*serial_buf
),
853 /* page_code */ SVPD_UNIT_SERIAL_NUMBER
,
854 /* sense_len */ SSD_FULL_SIZE
,
855 /* timeout */ timeout
? timeout
: 5000);
857 /* Disable freezing the device queue */
858 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
860 if (arglist
& CAM_ARG_ERR_RECOVER
)
861 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
863 if (cam_send_ccb(device
, ccb
) < 0) {
864 warn("error getting serial number");
866 if (arglist
& CAM_ARG_VERBOSE
) {
867 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
868 CAM_EPF_ALL
, stderr
);
876 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
879 if (arglist
& CAM_ARG_VERBOSE
) {
880 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
881 CAM_EPF_ALL
, stderr
);
892 bcopy(serial_buf
->serial_num
, serial_num
, serial_buf
->length
);
893 serial_num
[serial_buf
->length
] = '\0';
895 if ((arglist
& CAM_ARG_GET_STDINQ
)
896 || (arglist
& CAM_ARG_GET_XFERRATE
))
897 fprintf(stdout
, "%s%d: Serial Number ",
898 device
->device_name
, device
->dev_unit_num
);
900 fprintf(stdout
, "%.60s\n", serial_num
);
908 scsixferrate(struct cam_device
*device
)
916 ccb
= cam_getccb(device
);
919 warnx("couldn't allocate CCB");
923 bzero(&(&ccb
->ccb_h
)[1],
924 sizeof(struct ccb_trans_settings
) - sizeof(struct ccb_hdr
));
926 ccb
->ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
927 ccb
->cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
929 if (((retval
= cam_send_ccb(device
, ccb
)) < 0)
930 || ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
)) {
931 const char error_string
[] = "error getting transfer settings";
938 if (arglist
& CAM_ARG_VERBOSE
)
939 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
940 CAM_EPF_ALL
, stderr
);
944 goto xferrate_bailout
;
948 if (ccb
->cts
.transport
== XPORT_SPI
) {
949 struct ccb_trans_settings_spi
*spi
=
950 &ccb
->cts
.xport_specific
.spi
;
952 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0) {
953 freq
= scsi_calc_syncsrate(spi
->sync_period
);
957 fprintf(stdout
, "%s%d: ", device
->device_name
,
958 device
->dev_unit_num
);
960 if ((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0) {
961 speed
*= (0x01 << spi
->bus_width
);
967 fprintf(stdout
, "%d.%03dMB/s transfers ",
970 fprintf(stdout
, "%dKB/s transfers ",
973 if (((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)
974 && (spi
->sync_offset
!= 0))
975 fprintf(stdout
, "(%d.%03dMHz, offset %d", freq
/ 1000,
976 freq
% 1000, spi
->sync_offset
);
978 if (((spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) != 0)
979 && (spi
->bus_width
> 0)) {
980 if (((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)
981 && (spi
->sync_offset
!= 0)) {
982 fprintf(stdout
, ", ");
984 fprintf(stdout
, " (");
986 fprintf(stdout
, "%dbit)", 8 * (0x01 << spi
->bus_width
));
987 } else if (((spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) != 0)
988 && (spi
->sync_offset
!= 0)) {
989 fprintf(stdout
, ")");
992 struct ccb_pathinq cpi
;
994 retval
= get_cpi(device
, &cpi
);
997 goto xferrate_bailout
;
999 speed
= cpi
.base_transfer_speed
;
1005 fprintf(stdout
, "%d.%03dMB/s transfers ",
1008 fprintf(stdout
, "%dKB/s transfers ",
1012 if (ccb
->cts
.protocol
== PROTO_SCSI
) {
1013 struct ccb_trans_settings_scsi
*scsi
=
1014 &ccb
->cts
.proto_specific
.scsi
;
1015 if (scsi
->valid
& CTS_SCSI_VALID_TQ
) {
1016 if (scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) {
1017 fprintf(stdout
, ", Command Queueing Enabled");
1022 fprintf(stdout
, "\n");
1030 #endif /* MINIMALISTIC */
1033 * Parse out a bus, or a bus, target and lun in the following
1039 * Returns the number of parsed components, or 0.
1042 parse_btl(char *tstr
, int *mybus
, int *mytarget
, int *mylun
,
1043 cam_argmask
*myarglist
)
1048 while (isspace(*tstr
) && (*tstr
!= '\0'))
1051 tmpstr
= (char *)strtok(tstr
, ":");
1052 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')) {
1053 *mybus
= strtol(tmpstr
, NULL
, 0);
1054 *myarglist
|= CAM_ARG_BUS
;
1056 tmpstr
= (char *)strtok(NULL
, ":");
1057 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')) {
1058 *mytarget
= strtol(tmpstr
, NULL
, 0);
1059 *myarglist
|= CAM_ARG_TARGET
;
1061 tmpstr
= (char *)strtok(NULL
, ":");
1062 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')) {
1063 *mylun
= strtol(tmpstr
, NULL
, 0);
1064 *myarglist
|= CAM_ARG_LUN
;
1074 dorescan_or_reset(int argc
, char **argv
, int rescan
)
1076 static const char must
[] =
1077 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1079 int mybus
= -1, mytarget
= -1, mylun
= -1;
1083 warnx(must
, rescan
? "rescan" : "reset");
1087 tstr
= argv
[optind
];
1088 while (isspace(*tstr
) && (*tstr
!= '\0'))
1090 if (strncasecmp(tstr
, "all", strlen("all")) == 0)
1091 arglist
|= CAM_ARG_BUS
;
1093 rv
= parse_btl(argv
[optind
], &mybus
, &mytarget
, &mylun
,
1095 if (rv
!= 1 && rv
!= 3) {
1096 warnx(must
, rescan
? "rescan" : "reset");
1101 if ((arglist
& CAM_ARG_BUS
)
1102 && (arglist
& CAM_ARG_TARGET
)
1103 && (arglist
& CAM_ARG_LUN
))
1104 error
= scanlun_or_reset_dev(mybus
, mytarget
, mylun
, rescan
);
1106 error
= rescan_or_reset_bus(mybus
, rescan
);
1112 rescan_or_reset_bus(int mybus
, int rescan
)
1114 union ccb ccb
, matchccb
;
1120 if ((fd
= open(XPT_DEVICE
, O_RDWR
)) < 0) {
1121 warnx("error opening transport layer device %s", XPT_DEVICE
);
1122 warn("%s", XPT_DEVICE
);
1127 ccb
.ccb_h
.func_code
= rescan
? XPT_SCAN_BUS
: XPT_RESET_BUS
;
1128 ccb
.ccb_h
.path_id
= mybus
;
1129 ccb
.ccb_h
.target_id
= CAM_TARGET_WILDCARD
;
1130 ccb
.ccb_h
.target_lun
= CAM_LUN_WILDCARD
;
1131 ccb
.crcn
.flags
= CAM_FLAG_NONE
;
1133 /* run this at a low priority */
1134 ccb
.ccb_h
.pinfo
.priority
= 5;
1136 if (ioctl(fd
, CAMIOCOMMAND
, &ccb
) == -1) {
1137 warn("CAMIOCOMMAND ioctl failed");
1142 if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
1143 fprintf(stdout
, "%s of bus %d was successful\n",
1144 rescan
? "Re-scan" : "Reset", mybus
);
1146 fprintf(stdout
, "%s of bus %d returned error %#x\n",
1147 rescan
? "Re-scan" : "Reset", mybus
,
1148 ccb
.ccb_h
.status
& CAM_STATUS_MASK
);
1159 * The right way to handle this is to modify the xpt so that it can
1160 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1161 * that isn't implemented, so instead we enumerate the busses and
1162 * send the rescan or reset to those busses in the case where the
1163 * given bus is -1 (wildcard). We don't send a rescan or reset
1164 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1165 * no-op, sending a rescan to the xpt bus would result in a status of
1168 bzero(&(&matchccb
.ccb_h
)[1],
1169 sizeof(struct ccb_dev_match
) - sizeof(struct ccb_hdr
));
1170 matchccb
.ccb_h
.func_code
= XPT_DEV_MATCH
;
1171 bufsize
= sizeof(struct dev_match_result
) * 20;
1172 matchccb
.cdm
.match_buf_len
= bufsize
;
1173 matchccb
.cdm
.matches
=(struct dev_match_result
*)malloc(bufsize
);
1174 if (matchccb
.cdm
.matches
== NULL
) {
1175 warnx("can't malloc memory for matches");
1179 matchccb
.cdm
.num_matches
= 0;
1181 matchccb
.cdm
.num_patterns
= 1;
1182 matchccb
.cdm
.pattern_buf_len
= sizeof(struct dev_match_pattern
);
1184 matchccb
.cdm
.patterns
= (struct dev_match_pattern
*)malloc(
1185 matchccb
.cdm
.pattern_buf_len
);
1186 if (matchccb
.cdm
.patterns
== NULL
) {
1187 warnx("can't malloc memory for patterns");
1191 matchccb
.cdm
.patterns
[0].type
= DEV_MATCH_BUS
;
1192 matchccb
.cdm
.patterns
[0].pattern
.bus_pattern
.flags
= BUS_MATCH_ANY
;
1197 if (ioctl(fd
, CAMIOCOMMAND
, &matchccb
) == -1) {
1198 warn("CAMIOCOMMAND ioctl failed");
1203 if ((matchccb
.ccb_h
.status
!= CAM_REQ_CMP
)
1204 || ((matchccb
.cdm
.status
!= CAM_DEV_MATCH_LAST
)
1205 && (matchccb
.cdm
.status
!= CAM_DEV_MATCH_MORE
))) {
1206 warnx("got CAM error %#x, CDM error %d\n",
1207 matchccb
.ccb_h
.status
, matchccb
.cdm
.status
);
1212 for (i
= 0; i
< matchccb
.cdm
.num_matches
; i
++) {
1213 struct bus_match_result
*bus_result
;
1215 /* This shouldn't happen. */
1216 if (matchccb
.cdm
.matches
[i
].type
!= DEV_MATCH_BUS
)
1219 bus_result
= &matchccb
.cdm
.matches
[i
].result
.bus_result
;
1222 * We don't want to rescan or reset the xpt bus.
1225 if ((int)bus_result
->path_id
== -1)
1228 ccb
.ccb_h
.func_code
= rescan
? XPT_SCAN_BUS
:
1230 ccb
.ccb_h
.path_id
= bus_result
->path_id
;
1231 ccb
.ccb_h
.target_id
= CAM_TARGET_WILDCARD
;
1232 ccb
.ccb_h
.target_lun
= CAM_LUN_WILDCARD
;
1233 ccb
.crcn
.flags
= CAM_FLAG_NONE
;
1235 /* run this at a low priority */
1236 ccb
.ccb_h
.pinfo
.priority
= 5;
1238 if (ioctl(fd
, CAMIOCOMMAND
, &ccb
) == -1) {
1239 warn("CAMIOCOMMAND ioctl failed");
1244 if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) ==CAM_REQ_CMP
){
1245 fprintf(stdout
, "%s of bus %d was successful\n",
1246 rescan
? "Re-scan" : "Reset",
1247 bus_result
->path_id
);
1250 * Don't bail out just yet, maybe the other
1251 * rescan or reset commands will complete
1254 fprintf(stderr
, "%s of bus %d returned error "
1255 "%#x\n", rescan
? "Re-scan" : "Reset",
1256 bus_result
->path_id
,
1257 ccb
.ccb_h
.status
& CAM_STATUS_MASK
);
1261 } while ((matchccb
.ccb_h
.status
== CAM_REQ_CMP
)
1262 && (matchccb
.cdm
.status
== CAM_DEV_MATCH_MORE
));
1269 if (matchccb
.cdm
.patterns
!= NULL
)
1270 free(matchccb
.cdm
.patterns
);
1271 if (matchccb
.cdm
.matches
!= NULL
)
1272 free(matchccb
.cdm
.matches
);
1278 scanlun_or_reset_dev(int mybus
, int mytarget
, int mylun
, int scan
)
1281 struct cam_device
*device
;
1287 warnx("invalid bus number %d", mybus
);
1292 warnx("invalid target number %d", mytarget
);
1297 warnx("invalid lun number %d", mylun
);
1303 bzero(&ccb
, sizeof(union ccb
));
1306 if ((fd
= open(XPT_DEVICE
, O_RDWR
)) < 0) {
1307 warnx("error opening transport layer device %s\n",
1309 warn("%s", XPT_DEVICE
);
1313 device
= cam_open_btl(mybus
, mytarget
, mylun
, O_RDWR
, NULL
);
1314 if (device
== NULL
) {
1315 warnx("%s", cam_errbuf
);
1320 ccb
.ccb_h
.func_code
= (scan
)? XPT_SCAN_LUN
: XPT_RESET_DEV
;
1321 ccb
.ccb_h
.path_id
= mybus
;
1322 ccb
.ccb_h
.target_id
= mytarget
;
1323 ccb
.ccb_h
.target_lun
= mylun
;
1324 ccb
.ccb_h
.timeout
= 5000;
1325 ccb
.crcn
.flags
= CAM_FLAG_NONE
;
1327 /* run this at a low priority */
1328 ccb
.ccb_h
.pinfo
.priority
= 5;
1331 if (ioctl(fd
, CAMIOCOMMAND
, &ccb
) < 0) {
1332 warn("CAMIOCOMMAND ioctl failed");
1337 if (cam_send_ccb(device
, &ccb
) < 0) {
1338 warn("error sending XPT_RESET_DEV CCB");
1339 cam_close_device(device
);
1347 cam_close_device(device
);
1350 * An error code of CAM_BDR_SENT is normal for a BDR request.
1352 if (((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
)
1354 && ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) == CAM_BDR_SENT
))) {
1355 fprintf(stdout
, "%s of %d:%d:%d was successful\n",
1356 scan
? "Re-scan" : "Reset", mybus
, mytarget
, mylun
);
1359 fprintf(stdout
, "%s of %d:%d:%d returned error %#x\n",
1360 scan
? "Re-scan" : "Reset", mybus
, mytarget
, mylun
,
1361 ccb
.ccb_h
.status
& CAM_STATUS_MASK
);
1366 #ifndef MINIMALISTIC
1368 readdefects(struct cam_device
*device
, int argc
, char **argv
,
1369 char *combinedopt
, int retry_count
, int timeout
)
1371 union ccb
*ccb
= NULL
;
1372 struct scsi_read_defect_data_10
*rdd_cdb
;
1373 u_int8_t
*defect_list
= NULL
;
1374 u_int32_t dlist_length
= 65000;
1375 u_int32_t returned_length
= 0;
1376 u_int32_t num_returned
= 0;
1377 u_int8_t returned_format
;
1380 int lists_specified
= 0;
1382 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
1388 while (isspace(*tstr
) && (*tstr
!= '\0'))
1390 if (strcmp(tstr
, "block") == 0)
1391 arglist
|= CAM_ARG_FORMAT_BLOCK
;
1392 else if (strcmp(tstr
, "bfi") == 0)
1393 arglist
|= CAM_ARG_FORMAT_BFI
;
1394 else if (strcmp(tstr
, "phys") == 0)
1395 arglist
|= CAM_ARG_FORMAT_PHYS
;
1398 warnx("invalid defect format %s", tstr
);
1399 goto defect_bailout
;
1404 arglist
|= CAM_ARG_GLIST
;
1407 arglist
|= CAM_ARG_PLIST
;
1414 ccb
= cam_getccb(device
);
1417 * Hopefully 65000 bytes is enough to hold the defect list. If it
1418 * isn't, the disk is probably dead already. We'd have to go with
1419 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1422 defect_list
= malloc(dlist_length
);
1423 if (defect_list
== NULL
) {
1424 warnx("can't malloc memory for defect list");
1426 goto defect_bailout
;
1429 rdd_cdb
=(struct scsi_read_defect_data_10
*)&ccb
->csio
.cdb_io
.cdb_bytes
;
1432 * cam_getccb() zeros the CCB header only. So we need to zero the
1433 * payload portion of the ccb.
1435 bzero(&(&ccb
->ccb_h
)[1],
1436 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
1438 cam_fill_csio(&ccb
->csio
,
1439 /*retries*/ retry_count
,
1441 /*flags*/ CAM_DIR_IN
| ((arglist
& CAM_ARG_ERR_RECOVER
) ?
1442 CAM_PASS_ERR_RECOVER
: 0),
1443 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
1444 /*data_ptr*/ defect_list
,
1445 /*dxfer_len*/ dlist_length
,
1446 /*sense_len*/ SSD_FULL_SIZE
,
1447 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10
),
1448 /*timeout*/ timeout
? timeout
: 5000);
1450 rdd_cdb
->opcode
= READ_DEFECT_DATA_10
;
1451 if (arglist
& CAM_ARG_FORMAT_BLOCK
)
1452 rdd_cdb
->format
= SRDD10_BLOCK_FORMAT
;
1453 else if (arglist
& CAM_ARG_FORMAT_BFI
)
1454 rdd_cdb
->format
= SRDD10_BYTES_FROM_INDEX_FORMAT
;
1455 else if (arglist
& CAM_ARG_FORMAT_PHYS
)
1456 rdd_cdb
->format
= SRDD10_PHYSICAL_SECTOR_FORMAT
;
1459 warnx("no defect list format specified");
1460 goto defect_bailout
;
1462 if (arglist
& CAM_ARG_PLIST
) {
1463 rdd_cdb
->format
|= SRDD10_PLIST
;
1467 if (arglist
& CAM_ARG_GLIST
) {
1468 rdd_cdb
->format
|= SRDD10_GLIST
;
1472 scsi_ulto2b(dlist_length
, rdd_cdb
->alloc_length
);
1474 /* Disable freezing the device queue */
1475 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
1477 if (cam_send_ccb(device
, ccb
) < 0) {
1478 perror("error reading defect list");
1480 if (arglist
& CAM_ARG_VERBOSE
) {
1481 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
1482 CAM_EPF_ALL
, stderr
);
1486 goto defect_bailout
;
1489 returned_length
= scsi_2btoul(((struct
1490 scsi_read_defect_data_hdr_10
*)defect_list
)->length
);
1492 returned_format
= ((struct scsi_read_defect_data_hdr_10
*)
1493 defect_list
)->format
;
1495 if (((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_SCSI_STATUS_ERROR
)
1496 && (ccb
->csio
.scsi_status
== SCSI_STATUS_CHECK_COND
)
1497 && ((ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
) != 0)) {
1498 struct scsi_sense_data
*sense
;
1499 int error_code
, sense_key
, asc
, ascq
;
1501 sense
= &ccb
->csio
.sense_data
;
1502 scsi_extract_sense(sense
, &error_code
, &sense_key
, &asc
, &ascq
);
1505 * According to the SCSI spec, if the disk doesn't support
1506 * the requested format, it will generally return a sense
1507 * key of RECOVERED ERROR, and an additional sense code
1508 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1509 * also check to make sure that the returned length is
1510 * greater than 0, and then print out whatever format the
1513 if ((sense_key
== SSD_KEY_RECOVERED_ERROR
)
1514 && (asc
== 0x1c) && (ascq
== 0x00)
1515 && (returned_length
> 0)) {
1516 warnx("requested defect format not available");
1517 switch(returned_format
& SRDDH10_DLIST_FORMAT_MASK
) {
1518 case SRDD10_BLOCK_FORMAT
:
1519 warnx("Device returned block format");
1521 case SRDD10_BYTES_FROM_INDEX_FORMAT
:
1522 warnx("Device returned bytes from index"
1525 case SRDD10_PHYSICAL_SECTOR_FORMAT
:
1526 warnx("Device returned physical sector format");
1530 warnx("Device returned unknown defect"
1531 " data format %#x", returned_format
);
1532 goto defect_bailout
;
1533 break; /* NOTREACHED */
1537 warnx("Error returned from read defect data command");
1538 if (arglist
& CAM_ARG_VERBOSE
)
1539 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
1540 CAM_EPF_ALL
, stderr
);
1541 goto defect_bailout
;
1543 } else if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
1545 warnx("Error returned from read defect data command");
1546 if (arglist
& CAM_ARG_VERBOSE
)
1547 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
1548 CAM_EPF_ALL
, stderr
);
1549 goto defect_bailout
;
1553 * XXX KDM I should probably clean up the printout format for the
1556 switch (returned_format
& SRDDH10_DLIST_FORMAT_MASK
){
1557 case SRDDH10_PHYSICAL_SECTOR_FORMAT
:
1559 struct scsi_defect_desc_phys_sector
*dlist
;
1561 dlist
= (struct scsi_defect_desc_phys_sector
*)
1563 sizeof(struct scsi_read_defect_data_hdr_10
));
1565 num_returned
= returned_length
/
1566 sizeof(struct scsi_defect_desc_phys_sector
);
1568 fprintf(stderr
, "Got %d defect", num_returned
);
1570 if ((lists_specified
== 0) || (num_returned
== 0)) {
1571 fprintf(stderr
, "s.\n");
1573 } else if (num_returned
== 1)
1574 fprintf(stderr
, ":\n");
1576 fprintf(stderr
, "s:\n");
1578 for (i
= 0; i
< num_returned
; i
++) {
1579 fprintf(stdout
, "%d:%d:%d\n",
1580 scsi_3btoul(dlist
[i
].cylinder
),
1582 scsi_4btoul(dlist
[i
].sector
));
1586 case SRDDH10_BYTES_FROM_INDEX_FORMAT
:
1588 struct scsi_defect_desc_bytes_from_index
*dlist
;
1590 dlist
= (struct scsi_defect_desc_bytes_from_index
*)
1592 sizeof(struct scsi_read_defect_data_hdr_10
));
1594 num_returned
= returned_length
/
1595 sizeof(struct scsi_defect_desc_bytes_from_index
);
1597 fprintf(stderr
, "Got %d defect", num_returned
);
1599 if ((lists_specified
== 0) || (num_returned
== 0)) {
1600 fprintf(stderr
, "s.\n");
1602 } else if (num_returned
== 1)
1603 fprintf(stderr
, ":\n");
1605 fprintf(stderr
, "s:\n");
1607 for (i
= 0; i
< num_returned
; i
++) {
1608 fprintf(stdout
, "%d:%d:%d\n",
1609 scsi_3btoul(dlist
[i
].cylinder
),
1611 scsi_4btoul(dlist
[i
].bytes_from_index
));
1615 case SRDDH10_BLOCK_FORMAT
:
1617 struct scsi_defect_desc_block
*dlist
;
1619 dlist
= (struct scsi_defect_desc_block
*)(defect_list
+
1620 sizeof(struct scsi_read_defect_data_hdr_10
));
1622 num_returned
= returned_length
/
1623 sizeof(struct scsi_defect_desc_block
);
1625 fprintf(stderr
, "Got %d defect", num_returned
);
1627 if ((lists_specified
== 0) || (num_returned
== 0)) {
1628 fprintf(stderr
, "s.\n");
1630 } else if (num_returned
== 1)
1631 fprintf(stderr
, ":\n");
1633 fprintf(stderr
, "s:\n");
1635 for (i
= 0; i
< num_returned
; i
++)
1636 fprintf(stdout
, "%u\n",
1637 scsi_4btoul(dlist
[i
].address
));
1641 fprintf(stderr
, "Unknown defect format %d\n",
1642 returned_format
& SRDDH10_DLIST_FORMAT_MASK
);
1648 if (defect_list
!= NULL
)
1656 #endif /* MINIMALISTIC */
1660 reassignblocks(struct cam_device
*device
, u_int32_t
*blocks
, int num_blocks
)
1664 ccb
= cam_getccb(device
);
1670 #ifndef MINIMALISTIC
1672 mode_sense(struct cam_device
*device
, int mode_page
, int page_control
,
1673 int dbd
, int retry_count
, int timeout
, u_int8_t
*data
, int datalen
)
1678 ccb
= cam_getccb(device
);
1681 errx(1, "mode_sense: couldn't allocate CCB");
1683 bzero(&(&ccb
->ccb_h
)[1],
1684 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
1686 scsi_mode_sense(&ccb
->csio
,
1687 /* retries */ retry_count
,
1689 /* tag_action */ MSG_SIMPLE_Q_TAG
,
1691 /* page_code */ page_control
<< 6,
1692 /* page */ mode_page
,
1693 /* param_buf */ data
,
1694 /* param_len */ datalen
,
1695 /* sense_len */ SSD_FULL_SIZE
,
1696 /* timeout */ timeout
? timeout
: 5000);
1698 if (arglist
& CAM_ARG_ERR_RECOVER
)
1699 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
1701 /* Disable freezing the device queue */
1702 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
1704 if (((retval
= cam_send_ccb(device
, ccb
)) < 0)
1705 || ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
)) {
1706 if (arglist
& CAM_ARG_VERBOSE
) {
1707 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
1708 CAM_EPF_ALL
, stderr
);
1711 cam_close_device(device
);
1713 err(1, "error sending mode sense command");
1715 errx(1, "error sending mode sense command");
1722 mode_select(struct cam_device
*device
, int save_pages
, int retry_count
,
1723 int timeout
, u_int8_t
*data
, int datalen
)
1728 ccb
= cam_getccb(device
);
1731 errx(1, "mode_select: couldn't allocate CCB");
1733 bzero(&(&ccb
->ccb_h
)[1],
1734 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
1736 scsi_mode_select(&ccb
->csio
,
1737 /* retries */ retry_count
,
1739 /* tag_action */ MSG_SIMPLE_Q_TAG
,
1740 /* scsi_page_fmt */ 1,
1741 /* save_pages */ save_pages
,
1742 /* param_buf */ data
,
1743 /* param_len */ datalen
,
1744 /* sense_len */ SSD_FULL_SIZE
,
1745 /* timeout */ timeout
? timeout
: 5000);
1747 if (arglist
& CAM_ARG_ERR_RECOVER
)
1748 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
1750 /* Disable freezing the device queue */
1751 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
1753 if (((retval
= cam_send_ccb(device
, ccb
)) < 0)
1754 || ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
)) {
1755 if (arglist
& CAM_ARG_VERBOSE
) {
1756 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
1757 CAM_EPF_ALL
, stderr
);
1760 cam_close_device(device
);
1763 err(1, "error sending mode select command");
1765 errx(1, "error sending mode select command");
1773 modepage(struct cam_device
*device
, int argc
, char **argv
, char *combinedopt
,
1774 int retry_count
, int timeout
)
1776 int c
, mode_page
= -1, page_control
= 0;
1777 int binary
= 0, list
= 0;
1779 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
1785 arglist
|= CAM_ARG_DBD
;
1788 arglist
|= CAM_ARG_MODE_EDIT
;
1794 mode_page
= strtol(optarg
, NULL
, 0);
1796 errx(1, "invalid mode page %d", mode_page
);
1799 page_control
= strtol(optarg
, NULL
, 0);
1800 if ((page_control
< 0) || (page_control
> 3))
1801 errx(1, "invalid page control field %d",
1803 arglist
|= CAM_ARG_PAGE_CNTL
;
1810 if (mode_page
== -1 && list
== 0)
1811 errx(1, "you must specify a mode page!");
1814 mode_list(device
, page_control
, arglist
& CAM_ARG_DBD
,
1815 retry_count
, timeout
);
1817 mode_edit(device
, mode_page
, page_control
,
1818 arglist
& CAM_ARG_DBD
, arglist
& CAM_ARG_MODE_EDIT
, binary
,
1819 retry_count
, timeout
);
1824 scsicmd(struct cam_device
*device
, int argc
, char **argv
, char *combinedopt
,
1825 int retry_count
, int timeout
)
1828 u_int32_t flags
= CAM_DIR_NONE
;
1829 u_int8_t
*data_ptr
= NULL
;
1831 struct get_hook hook
;
1832 int c
, data_bytes
= 0;
1834 char *datastr
= NULL
, *tstr
;
1839 ccb
= cam_getccb(device
);
1842 warnx("scsicmd: error allocating ccb");
1846 bzero(&(&ccb
->ccb_h
)[1],
1847 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
1849 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
1853 while (isspace(*tstr
) && (*tstr
!= '\0'))
1855 hook
.argc
= argc
- optind
;
1856 hook
.argv
= argv
+ optind
;
1858 cdb_len
= buff_encode_visit(cdb
, sizeof(cdb
), tstr
,
1861 * Increment optind by the number of arguments the
1862 * encoding routine processed. After each call to
1863 * getopt(3), optind points to the argument that
1864 * getopt should process _next_. In this case,
1865 * that means it points to the first command string
1866 * argument, if there is one. Once we increment
1867 * this, it should point to either the next command
1868 * line argument, or it should be past the end of
1874 if (arglist
& CAM_ARG_CMD_OUT
) {
1875 warnx("command must either be "
1876 "read or write, not both");
1878 goto scsicmd_bailout
;
1880 arglist
|= CAM_ARG_CMD_IN
;
1882 data_bytes
= strtol(optarg
, NULL
, 0);
1883 if (data_bytes
<= 0) {
1884 warnx("invalid number of input bytes %d",
1887 goto scsicmd_bailout
;
1889 hook
.argc
= argc
- optind
;
1890 hook
.argv
= argv
+ optind
;
1893 datastr
= cget(&hook
, NULL
);
1895 * If the user supplied "-" instead of a format, he
1896 * wants the data to be written to stdout.
1898 if ((datastr
!= NULL
)
1899 && (datastr
[0] == '-'))
1902 data_ptr
= (u_int8_t
*)malloc(data_bytes
);
1903 if (data_ptr
== NULL
) {
1904 warnx("can't malloc memory for data_ptr");
1906 goto scsicmd_bailout
;
1910 if (arglist
& CAM_ARG_CMD_IN
) {
1911 warnx("command must either be "
1912 "read or write, not both");
1914 goto scsicmd_bailout
;
1916 arglist
|= CAM_ARG_CMD_OUT
;
1917 flags
= CAM_DIR_OUT
;
1918 data_bytes
= strtol(optarg
, NULL
, 0);
1919 if (data_bytes
<= 0) {
1920 warnx("invalid number of output bytes %d",
1923 goto scsicmd_bailout
;
1925 hook
.argc
= argc
- optind
;
1926 hook
.argv
= argv
+ optind
;
1928 datastr
= cget(&hook
, NULL
);
1929 data_ptr
= (u_int8_t
*)malloc(data_bytes
);
1930 if (data_ptr
== NULL
) {
1931 warnx("can't malloc memory for data_ptr");
1933 goto scsicmd_bailout
;
1936 * If the user supplied "-" instead of a format, he
1937 * wants the data to be read from stdin.
1939 if ((datastr
!= NULL
)
1940 && (datastr
[0] == '-'))
1943 buff_encode_visit(data_ptr
, data_bytes
, datastr
,
1953 * If fd_data is set, and we're writing to the device, we need to
1954 * read the data the user wants written from stdin.
1956 if ((fd_data
== 1) && (arglist
& CAM_ARG_CMD_OUT
)) {
1958 int amt_to_read
= data_bytes
;
1959 u_int8_t
*buf_ptr
= data_ptr
;
1961 for (amt_read
= 0; amt_to_read
> 0;
1962 amt_read
= read(STDIN_FILENO
, buf_ptr
, amt_to_read
)) {
1963 if (amt_read
== -1) {
1964 warn("error reading data from stdin");
1966 goto scsicmd_bailout
;
1968 amt_to_read
-= amt_read
;
1969 buf_ptr
+= amt_read
;
1973 if (arglist
& CAM_ARG_ERR_RECOVER
)
1974 flags
|= CAM_PASS_ERR_RECOVER
;
1976 /* Disable freezing the device queue */
1977 flags
|= CAM_DEV_QFRZDIS
;
1980 * This is taken from the SCSI-3 draft spec.
1981 * (T10/1157D revision 0.3)
1982 * The top 3 bits of an opcode are the group code. The next 5 bits
1983 * are the command code.
1984 * Group 0: six byte commands
1985 * Group 1: ten byte commands
1986 * Group 2: ten byte commands
1988 * Group 4: sixteen byte commands
1989 * Group 5: twelve byte commands
1990 * Group 6: vendor specific
1991 * Group 7: vendor specific
1993 switch((cdb
[0] >> 5) & 0x7) {
2004 /* computed by buff_encode_visit */
2015 * We should probably use csio_build_visit or something like that
2016 * here, but it's easier to encode arguments as you go. The
2017 * alternative would be skipping the CDB argument and then encoding
2018 * it here, since we've got the data buffer argument by now.
2020 bcopy(cdb
, &ccb
->csio
.cdb_io
.cdb_bytes
, cdb_len
);
2022 cam_fill_csio(&ccb
->csio
,
2023 /*retries*/ retry_count
,
2026 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
2027 /*data_ptr*/ data_ptr
,
2028 /*dxfer_len*/ data_bytes
,
2029 /*sense_len*/ SSD_FULL_SIZE
,
2030 /*cdb_len*/ cdb_len
,
2031 /*timeout*/ timeout
? timeout
: 5000);
2033 if (((retval
= cam_send_ccb(device
, ccb
)) < 0)
2034 || ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
)) {
2036 warn("error sending command");
2038 warnx("error sending command");
2040 if (arglist
& CAM_ARG_VERBOSE
) {
2041 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2042 CAM_EPF_ALL
, stderr
);
2046 goto scsicmd_bailout
;
2050 if (((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
)
2051 && (arglist
& CAM_ARG_CMD_IN
)
2052 && (data_bytes
> 0)) {
2054 buff_decode_visit(data_ptr
, data_bytes
, datastr
,
2056 fprintf(stdout
, "\n");
2058 ssize_t amt_written
;
2059 int amt_to_write
= data_bytes
;
2060 u_int8_t
*buf_ptr
= data_ptr
;
2062 for (amt_written
= 0; (amt_to_write
> 0) &&
2063 (amt_written
=write(1, buf_ptr
,amt_to_write
))> 0;){
2064 amt_to_write
-= amt_written
;
2065 buf_ptr
+= amt_written
;
2067 if (amt_written
== -1) {
2068 warn("error writing data to stdout");
2070 goto scsicmd_bailout
;
2071 } else if ((amt_written
== 0)
2072 && (amt_to_write
> 0)) {
2073 warnx("only wrote %u bytes out of %u",
2074 data_bytes
- amt_to_write
, data_bytes
);
2081 if ((data_bytes
> 0) && (data_ptr
!= NULL
))
2090 camdebug(int argc
, char **argv
, char *combinedopt
)
2093 int mybus
= -1, mytarget
= -1, mylun
= -1;
2094 char *tstr
, *tmpstr
= NULL
;
2098 bzero(&ccb
, sizeof(union ccb
));
2100 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
2103 arglist
|= CAM_ARG_DEBUG_INFO
;
2104 ccb
.cdbg
.flags
|= CAM_DEBUG_INFO
;
2107 arglist
|= CAM_ARG_DEBUG_PERIPH
;
2108 ccb
.cdbg
.flags
|= CAM_DEBUG_PERIPH
;
2111 arglist
|= CAM_ARG_DEBUG_SUBTRACE
;
2112 ccb
.cdbg
.flags
|= CAM_DEBUG_SUBTRACE
;
2115 arglist
|= CAM_ARG_DEBUG_TRACE
;
2116 ccb
.cdbg
.flags
|= CAM_DEBUG_TRACE
;
2119 arglist
|= CAM_ARG_DEBUG_XPT
;
2120 ccb
.cdbg
.flags
|= CAM_DEBUG_XPT
;
2123 arglist
|= CAM_ARG_DEBUG_CDB
;
2124 ccb
.cdbg
.flags
|= CAM_DEBUG_CDB
;
2131 if ((fd
= open(XPT_DEVICE
, O_RDWR
)) < 0) {
2132 warnx("error opening transport layer device %s", XPT_DEVICE
);
2133 warn("%s", XPT_DEVICE
);
2140 warnx("you must specify \"off\", \"all\" or a bus,");
2141 warnx("bus:target, or bus:target:lun");
2148 while (isspace(*tstr
) && (*tstr
!= '\0'))
2151 if (strncmp(tstr
, "off", 3) == 0) {
2152 ccb
.cdbg
.flags
= CAM_DEBUG_NONE
;
2153 arglist
&= ~(CAM_ARG_DEBUG_INFO
|CAM_ARG_DEBUG_PERIPH
|
2154 CAM_ARG_DEBUG_TRACE
|CAM_ARG_DEBUG_SUBTRACE
|
2156 } else if (strncmp(tstr
, "all", 3) != 0) {
2157 tmpstr
= (char *)strtok(tstr
, ":");
2158 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')){
2159 mybus
= strtol(tmpstr
, NULL
, 0);
2160 arglist
|= CAM_ARG_BUS
;
2161 tmpstr
= (char *)strtok(NULL
, ":");
2162 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')){
2163 mytarget
= strtol(tmpstr
, NULL
, 0);
2164 arglist
|= CAM_ARG_TARGET
;
2165 tmpstr
= (char *)strtok(NULL
, ":");
2166 if ((tmpstr
!= NULL
) && (*tmpstr
!= '\0')){
2167 mylun
= strtol(tmpstr
, NULL
, 0);
2168 arglist
|= CAM_ARG_LUN
;
2173 warnx("you must specify \"all\", \"off\", or a bus,");
2174 warnx("bus:target, or bus:target:lun to debug");
2180 ccb
.ccb_h
.func_code
= XPT_DEBUG
;
2181 ccb
.ccb_h
.path_id
= mybus
;
2182 ccb
.ccb_h
.target_id
= mytarget
;
2183 ccb
.ccb_h
.target_lun
= mylun
;
2185 if (ioctl(fd
, CAMIOCOMMAND
, &ccb
) == -1) {
2186 warn("CAMIOCOMMAND ioctl failed");
2191 if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) ==
2192 CAM_FUNC_NOTAVAIL
) {
2193 warnx("CAM debugging not available");
2194 warnx("you need to put options CAMDEBUG in"
2195 " your kernel config file!");
2197 } else if ((ccb
.ccb_h
.status
& CAM_STATUS_MASK
) !=
2199 warnx("XPT_DEBUG CCB failed with status %#x",
2203 if (ccb
.cdbg
.flags
== CAM_DEBUG_NONE
) {
2205 "Debugging turned off\n");
2208 "Debugging enabled for "
2210 mybus
, mytarget
, mylun
);
2221 tagcontrol(struct cam_device
*device
, int argc
, char **argv
,
2231 ccb
= cam_getccb(device
);
2234 warnx("tagcontrol: error allocating ccb");
2238 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
2241 numtags
= strtol(optarg
, NULL
, 0);
2243 warnx("tag count %d is < 0", numtags
);
2245 goto tagcontrol_bailout
;
2256 cam_path_string(device
, pathstr
, sizeof(pathstr
));
2259 bzero(&(&ccb
->ccb_h
)[1],
2260 sizeof(struct ccb_relsim
) - sizeof(struct ccb_hdr
));
2261 ccb
->ccb_h
.func_code
= XPT_REL_SIMQ
;
2262 ccb
->crs
.release_flags
= RELSIM_ADJUST_OPENINGS
;
2263 ccb
->crs
.openings
= numtags
;
2266 if (cam_send_ccb(device
, ccb
) < 0) {
2267 perror("error sending XPT_REL_SIMQ CCB");
2269 goto tagcontrol_bailout
;
2272 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2273 warnx("XPT_REL_SIMQ CCB failed");
2274 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2275 CAM_EPF_ALL
, stderr
);
2277 goto tagcontrol_bailout
;
2282 fprintf(stdout
, "%stagged openings now %d\n",
2283 pathstr
, ccb
->crs
.openings
);
2286 bzero(&(&ccb
->ccb_h
)[1],
2287 sizeof(struct ccb_getdevstats
) - sizeof(struct ccb_hdr
));
2289 ccb
->ccb_h
.func_code
= XPT_GDEV_STATS
;
2291 if (cam_send_ccb(device
, ccb
) < 0) {
2292 perror("error sending XPT_GDEV_STATS CCB");
2294 goto tagcontrol_bailout
;
2297 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2298 warnx("XPT_GDEV_STATS CCB failed");
2299 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2300 CAM_EPF_ALL
, stderr
);
2302 goto tagcontrol_bailout
;
2305 if (arglist
& CAM_ARG_VERBOSE
) {
2306 fprintf(stdout
, "%s", pathstr
);
2307 fprintf(stdout
, "dev_openings %d\n", ccb
->cgds
.dev_openings
);
2308 fprintf(stdout
, "%s", pathstr
);
2309 fprintf(stdout
, "dev_active %d\n", ccb
->cgds
.dev_active
);
2310 fprintf(stdout
, "%s", pathstr
);
2311 fprintf(stdout
, "devq_openings %d\n", ccb
->cgds
.devq_openings
);
2312 fprintf(stdout
, "%s", pathstr
);
2313 fprintf(stdout
, "devq_queued %d\n", ccb
->cgds
.devq_queued
);
2314 fprintf(stdout
, "%s", pathstr
);
2315 fprintf(stdout
, "held %d\n", ccb
->cgds
.held
);
2316 fprintf(stdout
, "%s", pathstr
);
2317 fprintf(stdout
, "mintags %d\n", ccb
->cgds
.mintags
);
2318 fprintf(stdout
, "%s", pathstr
);
2319 fprintf(stdout
, "maxtags %d\n", ccb
->cgds
.maxtags
);
2322 fprintf(stdout
, "%s", pathstr
);
2323 fprintf(stdout
, "device openings: ");
2325 fprintf(stdout
, "%d\n", ccb
->cgds
.dev_openings
+
2326 ccb
->cgds
.dev_active
);
2336 cts_print(struct cam_device
*device
, struct ccb_trans_settings
*cts
)
2340 cam_path_string(device
, pathstr
, sizeof(pathstr
));
2342 if (cts
->transport
== XPORT_SPI
) {
2343 struct ccb_trans_settings_spi
*spi
=
2344 &cts
->xport_specific
.spi
;
2346 if ((spi
->valid
& CTS_SPI_VALID_SYNC_RATE
) != 0) {
2348 fprintf(stdout
, "%ssync parameter: %d\n", pathstr
,
2351 if (spi
->sync_offset
!= 0) {
2354 freq
= scsi_calc_syncsrate(spi
->sync_period
);
2355 fprintf(stdout
, "%sfrequency: %d.%03dMHz\n",
2356 pathstr
, freq
/ 1000, freq
% 1000);
2360 if (spi
->valid
& CTS_SPI_VALID_SYNC_OFFSET
) {
2361 fprintf(stdout
, "%soffset: %d\n", pathstr
,
2365 if (spi
->valid
& CTS_SPI_VALID_BUS_WIDTH
) {
2366 fprintf(stdout
, "%sbus width: %d bits\n", pathstr
,
2367 (0x01 << spi
->bus_width
) * 8);
2370 if (spi
->valid
& CTS_SPI_VALID_DISC
) {
2371 fprintf(stdout
, "%sdisconnection is %s\n", pathstr
,
2372 (spi
->flags
& CTS_SPI_FLAGS_DISC_ENB
) ?
2373 "enabled" : "disabled");
2377 if (cts
->protocol
== PROTO_SCSI
) {
2378 struct ccb_trans_settings_scsi
*scsi
=
2379 &cts
->proto_specific
.scsi
;
2381 if (scsi
->valid
& CTS_SCSI_VALID_TQ
) {
2382 fprintf(stdout
, "%stagged queueing is %s\n", pathstr
,
2383 (scsi
->flags
& CTS_SCSI_FLAGS_TAG_ENB
) ?
2384 "enabled" : "disabled");
2391 * Get a path inquiry CCB for the specified device.
2394 get_cpi(struct cam_device
*device
, struct ccb_pathinq
*cpi
)
2399 ccb
= cam_getccb(device
);
2402 warnx("get_cpi: couldn't allocate CCB");
2406 bzero(&(&ccb
->ccb_h
)[1],
2407 sizeof(struct ccb_pathinq
) - sizeof(struct ccb_hdr
));
2409 ccb
->ccb_h
.func_code
= XPT_PATH_INQ
;
2411 if (cam_send_ccb(device
, ccb
) < 0) {
2412 warn("get_cpi: error sending Path Inquiry CCB");
2414 if (arglist
& CAM_ARG_VERBOSE
)
2415 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2416 CAM_EPF_ALL
, stderr
);
2420 goto get_cpi_bailout
;
2423 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2425 if (arglist
& CAM_ARG_VERBOSE
)
2426 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2427 CAM_EPF_ALL
, stderr
);
2431 goto get_cpi_bailout
;
2434 bcopy(&ccb
->cpi
, cpi
, sizeof(struct ccb_pathinq
));
2444 cpi_print(struct ccb_pathinq
*cpi
)
2446 char adapter_str
[1024];
2449 snprintf(adapter_str
, sizeof(adapter_str
),
2450 "%s%d:", cpi
->dev_name
, cpi
->unit_number
);
2452 fprintf(stdout
, "%s SIM/HBA version: %d\n", adapter_str
,
2455 for (i
= 1; i
< 0xff; i
= i
<< 1) {
2458 if ((i
& cpi
->hba_inquiry
) == 0)
2461 fprintf(stdout
, "%s supports ", adapter_str
);
2465 str
= "MDP message";
2468 str
= "32 bit wide SCSI";
2471 str
= "16 bit wide SCSI";
2474 str
= "SDTR message";
2477 str
= "linked CDBs";
2480 str
= "tag queue messages";
2483 str
= "soft reset alternative";
2486 str
= "unknown PI bit set";
2489 fprintf(stdout
, "%s\n", str
);
2492 for (i
= 1; i
< 0xff; i
= i
<< 1) {
2495 if ((i
& cpi
->hba_misc
) == 0)
2498 fprintf(stdout
, "%s ", adapter_str
);
2502 str
= "bus scans from high ID to low ID";
2505 str
= "removable devices not included in scan";
2507 case PIM_NOINITIATOR
:
2508 str
= "initiator role not supported";
2510 case PIM_NOBUSRESET
:
2511 str
= "user has disabled initial BUS RESET or"
2512 " controller is in target/mixed mode";
2515 str
= "unknown PIM bit set";
2518 fprintf(stdout
, "%s\n", str
);
2521 for (i
= 1; i
< 0xff; i
= i
<< 1) {
2524 if ((i
& cpi
->target_sprt
) == 0)
2527 fprintf(stdout
, "%s supports ", adapter_str
);
2530 str
= "target mode processor mode";
2533 str
= "target mode phase cog. mode";
2535 case PIT_DISCONNECT
:
2536 str
= "disconnects in target mode";
2539 str
= "terminate I/O message in target mode";
2542 str
= "group 6 commands in target mode";
2545 str
= "group 7 commands in target mode";
2548 str
= "unknown PIT bit set";
2552 fprintf(stdout
, "%s\n", str
);
2554 fprintf(stdout
, "%s HBA engine count: %d\n", adapter_str
,
2556 fprintf(stdout
, "%s maximum target: %d\n", adapter_str
,
2558 fprintf(stdout
, "%s maximum LUN: %d\n", adapter_str
,
2560 fprintf(stdout
, "%s highest path ID in subsystem: %d\n",
2561 adapter_str
, cpi
->hpath_id
);
2562 fprintf(stdout
, "%s initiator ID: %d\n", adapter_str
,
2564 fprintf(stdout
, "%s SIM vendor: %s\n", adapter_str
, cpi
->sim_vid
);
2565 fprintf(stdout
, "%s HBA vendor: %s\n", adapter_str
, cpi
->hba_vid
);
2566 fprintf(stdout
, "%s bus ID: %d\n", adapter_str
, cpi
->bus_id
);
2567 fprintf(stdout
, "%s base transfer speed: ", adapter_str
);
2568 if (cpi
->base_transfer_speed
> 1000)
2569 fprintf(stdout
, "%d.%03dMB/sec\n",
2570 cpi
->base_transfer_speed
/ 1000,
2571 cpi
->base_transfer_speed
% 1000);
2573 fprintf(stdout
, "%dKB/sec\n",
2574 (cpi
->base_transfer_speed
% 1000) * 1000);
2578 get_print_cts(struct cam_device
*device
, int user_settings
, int quiet
,
2579 struct ccb_trans_settings
*cts
)
2585 ccb
= cam_getccb(device
);
2588 warnx("get_print_cts: error allocating ccb");
2592 bzero(&(&ccb
->ccb_h
)[1],
2593 sizeof(struct ccb_trans_settings
) - sizeof(struct ccb_hdr
));
2595 ccb
->ccb_h
.func_code
= XPT_GET_TRAN_SETTINGS
;
2597 if (user_settings
== 0)
2598 ccb
->cts
.type
= CTS_TYPE_CURRENT_SETTINGS
;
2600 ccb
->cts
.type
= CTS_TYPE_USER_SETTINGS
;
2602 if (cam_send_ccb(device
, ccb
) < 0) {
2603 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2604 if (arglist
& CAM_ARG_VERBOSE
)
2605 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2606 CAM_EPF_ALL
, stderr
);
2608 goto get_print_cts_bailout
;
2611 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2612 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2613 if (arglist
& CAM_ARG_VERBOSE
)
2614 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2615 CAM_EPF_ALL
, stderr
);
2617 goto get_print_cts_bailout
;
2621 cts_print(device
, &ccb
->cts
);
2624 bcopy(&ccb
->cts
, cts
, sizeof(struct ccb_trans_settings
));
2626 get_print_cts_bailout
:
2634 ratecontrol(struct cam_device
*device
, int retry_count
, int timeout
,
2635 int argc
, char **argv
, char *combinedopt
)
2639 int user_settings
= 0;
2641 int disc_enable
= -1, tag_enable
= -1;
2643 double syncrate
= -1;
2646 int change_settings
= 0, send_tur
= 0;
2647 struct ccb_pathinq cpi
;
2649 ccb
= cam_getccb(device
);
2652 warnx("ratecontrol: error allocating ccb");
2656 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
2665 if (strncasecmp(optarg
, "enable", 6) == 0)
2667 else if (strncasecmp(optarg
, "disable", 7) == 0)
2670 warnx("-D argument \"%s\" is unknown", optarg
);
2672 goto ratecontrol_bailout
;
2674 change_settings
= 1;
2677 offset
= strtol(optarg
, NULL
, 0);
2679 warnx("offset value %d is < 0", offset
);
2681 goto ratecontrol_bailout
;
2683 change_settings
= 1;
2689 syncrate
= atof(optarg
);
2692 warnx("sync rate %f is < 0", syncrate
);
2694 goto ratecontrol_bailout
;
2696 change_settings
= 1;
2699 if (strncasecmp(optarg
, "enable", 6) == 0)
2701 else if (strncasecmp(optarg
, "disable", 7) == 0)
2704 warnx("-T argument \"%s\" is unknown", optarg
);
2706 goto ratecontrol_bailout
;
2708 change_settings
= 1;
2714 bus_width
= strtol(optarg
, NULL
, 0);
2715 if (bus_width
< 0) {
2716 warnx("bus width %d is < 0", bus_width
);
2718 goto ratecontrol_bailout
;
2720 change_settings
= 1;
2727 bzero(&(&ccb
->ccb_h
)[1],
2728 sizeof(struct ccb_pathinq
) - sizeof(struct ccb_hdr
));
2731 * Grab path inquiry information, so we can determine whether
2732 * or not the initiator is capable of the things that the user
2735 ccb
->ccb_h
.func_code
= XPT_PATH_INQ
;
2737 if (cam_send_ccb(device
, ccb
) < 0) {
2738 perror("error sending XPT_PATH_INQ CCB");
2739 if (arglist
& CAM_ARG_VERBOSE
) {
2740 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2741 CAM_EPF_ALL
, stderr
);
2744 goto ratecontrol_bailout
;
2747 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2748 warnx("XPT_PATH_INQ CCB failed");
2749 if (arglist
& CAM_ARG_VERBOSE
) {
2750 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2751 CAM_EPF_ALL
, stderr
);
2754 goto ratecontrol_bailout
;
2757 bcopy(&ccb
->cpi
, &cpi
, sizeof(struct ccb_pathinq
));
2759 bzero(&(&ccb
->ccb_h
)[1],
2760 sizeof(struct ccb_trans_settings
) - sizeof(struct ccb_hdr
));
2763 fprintf(stdout
, "Current Parameters:\n");
2765 retval
= get_print_cts(device
, user_settings
, quiet
, &ccb
->cts
);
2768 goto ratecontrol_bailout
;
2770 if (arglist
& CAM_ARG_VERBOSE
)
2773 if (change_settings
) {
2774 int didsettings
= 0;
2775 struct ccb_trans_settings_spi
*spi
= NULL
;
2776 struct ccb_trans_settings_scsi
*scsi
= NULL
;
2778 if (ccb
->cts
.transport
== XPORT_SPI
) {
2779 spi
= &ccb
->cts
.xport_specific
.spi
;
2782 if (ccb
->cts
.protocol
== PROTO_SCSI
) {
2783 scsi
= &ccb
->cts
.proto_specific
.scsi
;
2786 if (spi
&& disc_enable
!= -1) {
2787 spi
->valid
|= CTS_SPI_VALID_DISC
;
2788 if (disc_enable
== 0)
2789 spi
->flags
&= ~CTS_SPI_FLAGS_DISC_ENB
;
2791 spi
->flags
|= CTS_SPI_FLAGS_DISC_ENB
;
2794 if (scsi
&& tag_enable
!= -1) {
2795 if ((cpi
.hba_inquiry
& PI_TAG_ABLE
) == 0) {
2796 warnx("HBA does not support tagged queueing, "
2797 "so you cannot modify tag settings");
2799 goto ratecontrol_bailout
;
2802 scsi
->valid
|= CTS_SCSI_VALID_TQ
;
2804 if (tag_enable
== 0)
2805 scsi
->flags
&= ~CTS_SCSI_FLAGS_TAG_ENB
;
2807 scsi
->flags
|= CTS_SCSI_FLAGS_TAG_ENB
;
2811 if (spi
&& offset
!= -1) {
2812 if ((cpi
.hba_inquiry
& PI_SDTR_ABLE
) == 0) {
2813 warnx("HBA at %s%d is not cable of changing "
2814 "offset", cpi
.dev_name
,
2817 goto ratecontrol_bailout
;
2819 spi
->valid
|= CTS_SPI_VALID_SYNC_OFFSET
;
2820 spi
->sync_offset
= offset
;
2824 if (spi
&& syncrate
!= -1) {
2825 int prelim_sync_period
;
2827 if ((cpi
.hba_inquiry
& PI_SDTR_ABLE
) == 0) {
2828 warnx("HBA at %s%d is not cable of changing "
2829 "transfer rates", cpi
.dev_name
,
2832 goto ratecontrol_bailout
;
2835 spi
->valid
|= CTS_SPI_VALID_SYNC_RATE
;
2838 * The sync rate the user gives us is in MHz.
2839 * We need to translate it into KHz for this
2845 * Next, we calculate a "preliminary" sync period
2846 * in tenths of a nanosecond.
2849 prelim_sync_period
= 0;
2851 prelim_sync_period
= 10000000 / syncrate
;
2854 scsi_calc_syncparam(prelim_sync_period
);
2860 * The bus_width argument goes like this:
2864 * Therefore, if you shift the number of bits given on the
2865 * command line right by 4, you should get the correct
2868 if (spi
&& bus_width
!= -1) {
2871 * We might as well validate things here with a
2872 * decipherable error message, rather than what
2873 * will probably be an indecipherable error message
2874 * by the time it gets back to us.
2876 if ((bus_width
== 16)
2877 && ((cpi
.hba_inquiry
& PI_WIDE_16
) == 0)) {
2878 warnx("HBA does not support 16 bit bus width");
2880 goto ratecontrol_bailout
;
2881 } else if ((bus_width
== 32)
2882 && ((cpi
.hba_inquiry
& PI_WIDE_32
) == 0)) {
2883 warnx("HBA does not support 32 bit bus width");
2885 goto ratecontrol_bailout
;
2886 } else if ((bus_width
!= 8)
2887 && (bus_width
!= 16)
2888 && (bus_width
!= 32)) {
2889 warnx("Invalid bus width %d", bus_width
);
2891 goto ratecontrol_bailout
;
2894 spi
->valid
|= CTS_SPI_VALID_BUS_WIDTH
;
2895 spi
->bus_width
= bus_width
>> 4;
2899 if (didsettings
== 0) {
2900 goto ratecontrol_bailout
;
2902 ccb
->ccb_h
.func_code
= XPT_SET_TRAN_SETTINGS
;
2904 if (cam_send_ccb(device
, ccb
) < 0) {
2905 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2906 if (arglist
& CAM_ARG_VERBOSE
) {
2907 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2908 CAM_EPF_ALL
, stderr
);
2911 goto ratecontrol_bailout
;
2914 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
2915 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2916 if (arglist
& CAM_ARG_VERBOSE
) {
2917 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
2918 CAM_EPF_ALL
, stderr
);
2921 goto ratecontrol_bailout
;
2926 retval
= testunitready(device
, retry_count
, timeout
,
2927 (arglist
& CAM_ARG_VERBOSE
) ? 0 : 1);
2930 * If the TUR didn't succeed, just bail.
2934 fprintf(stderr
, "Test Unit Ready failed\n");
2935 goto ratecontrol_bailout
;
2939 * If the user wants things quiet, there's no sense in
2940 * getting the transfer settings, if we're not going
2944 goto ratecontrol_bailout
;
2946 fprintf(stdout
, "New Parameters:\n");
2947 retval
= get_print_cts(device
, user_settings
, 0, NULL
);
2950 ratecontrol_bailout
:
2957 scsiformat(struct cam_device
*device
, int argc
, char **argv
,
2958 char *combinedopt
, int retry_count
, int timeout
)
2962 int ycount
= 0, quiet
= 0;
2963 int error
= 0, response
= 0, retval
= 0;
2964 int use_timeout
= 10800 * 1000;
2966 struct format_defect_list_header fh
;
2967 u_int8_t
*data_ptr
= NULL
;
2968 u_int32_t dxfer_len
= 0;
2970 int num_warnings
= 0;
2973 ccb
= cam_getccb(device
);
2976 warnx("scsiformat: error allocating ccb");
2980 bzero(&(&ccb
->ccb_h
)[1],
2981 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
2983 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
3004 fprintf(stdout
, "You are about to REMOVE ALL DATA from the "
3005 "following device:\n");
3007 error
= scsidoinquiry(device
, argc
, argv
, combinedopt
,
3008 retry_count
, timeout
);
3011 warnx("scsiformat: error sending inquiry");
3012 goto scsiformat_bailout
;
3021 fprintf(stdout
, "Are you SURE you want to do "
3024 if (fgets(str
, sizeof(str
), stdin
) != NULL
) {
3026 if (strncasecmp(str
, "yes", 3) == 0)
3028 else if (strncasecmp(str
, "no", 2) == 0)
3031 fprintf(stdout
, "Please answer"
3032 " \"yes\" or \"no\"\n");
3035 } while (response
== 0);
3037 if (response
== -1) {
3039 goto scsiformat_bailout
;
3044 use_timeout
= timeout
;
3047 fprintf(stdout
, "Current format timeout is %d seconds\n",
3048 use_timeout
/ 1000);
3052 * If the user hasn't disabled questions and didn't specify a
3053 * timeout on the command line, ask them if they want the current
3057 && (timeout
== 0)) {
3059 int new_timeout
= 0;
3061 fprintf(stdout
, "Enter new timeout in seconds or press\n"
3062 "return to keep the current timeout [%d] ",
3063 use_timeout
/ 1000);
3065 if (fgets(str
, sizeof(str
), stdin
) != NULL
) {
3067 new_timeout
= atoi(str
);
3070 if (new_timeout
!= 0) {
3071 use_timeout
= new_timeout
* 1000;
3072 fprintf(stdout
, "Using new timeout value %d\n",
3073 use_timeout
/ 1000);
3078 * Keep this outside the if block below to silence any unused
3079 * variable warnings.
3081 bzero(&fh
, sizeof(fh
));
3084 * If we're in immediate mode, we've got to include the format
3087 if (immediate
!= 0) {
3088 fh
.byte2
= FU_DLH_IMMED
;
3089 data_ptr
= (u_int8_t
*)&fh
;
3090 dxfer_len
= sizeof(fh
);
3091 byte2
= FU_FMT_DATA
;
3092 } else if (quiet
== 0) {
3093 fprintf(stdout
, "Formatting...");
3097 scsi_format_unit(&ccb
->csio
,
3098 /* retries */ retry_count
,
3100 /* tag_action */ MSG_SIMPLE_Q_TAG
,
3103 /* data_ptr */ data_ptr
,
3104 /* dxfer_len */ dxfer_len
,
3105 /* sense_len */ SSD_FULL_SIZE
,
3106 /* timeout */ use_timeout
);
3108 /* Disable freezing the device queue */
3109 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
3111 if (arglist
& CAM_ARG_ERR_RECOVER
)
3112 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
3114 if (((retval
= cam_send_ccb(device
, ccb
)) < 0)
3115 || ((immediate
== 0)
3116 && ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
))) {
3117 const char errstr
[] = "error sending format command";
3124 if (arglist
& CAM_ARG_VERBOSE
) {
3125 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3126 CAM_EPF_ALL
, stderr
);
3129 goto scsiformat_bailout
;
3133 * If we ran in non-immediate mode, we already checked for errors
3134 * above and printed out any necessary information. If we're in
3135 * immediate mode, we need to loop through and get status
3136 * information periodically.
3138 if (immediate
== 0) {
3140 fprintf(stdout
, "Format Complete\n");
3142 goto scsiformat_bailout
;
3149 bzero(&(&ccb
->ccb_h
)[1],
3150 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
3153 * There's really no need to do error recovery or
3154 * retries here, since we're just going to sit in a
3155 * loop and wait for the device to finish formatting.
3157 scsi_test_unit_ready(&ccb
->csio
,
3160 /* tag_action */ MSG_SIMPLE_Q_TAG
,
3161 /* sense_len */ SSD_FULL_SIZE
,
3162 /* timeout */ 5000);
3164 /* Disable freezing the device queue */
3165 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
3167 retval
= cam_send_ccb(device
, ccb
);
3170 * If we get an error from the ioctl, bail out. SCSI
3171 * errors are expected.
3174 warn("error sending CAMIOCOMMAND ioctl");
3175 if (arglist
& CAM_ARG_VERBOSE
) {
3176 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3177 CAM_EPF_ALL
, stderr
);
3180 goto scsiformat_bailout
;
3183 status
= ccb
->ccb_h
.status
& CAM_STATUS_MASK
;
3185 if ((status
!= CAM_REQ_CMP
)
3186 && (status
== CAM_SCSI_STATUS_ERROR
)
3187 && ((ccb
->ccb_h
.status
& CAM_AUTOSNS_VALID
) != 0)) {
3188 struct scsi_sense_data
*sense
;
3189 int error_code
, sense_key
, asc
, ascq
;
3191 sense
= &ccb
->csio
.sense_data
;
3192 scsi_extract_sense(sense
, &error_code
, &sense_key
,
3196 * According to the SCSI-2 and SCSI-3 specs, a
3197 * drive that is in the middle of a format should
3198 * return NOT READY with an ASC of "logical unit
3199 * not ready, format in progress". The sense key
3200 * specific bytes will then be a progress indicator.
3202 if ((sense_key
== SSD_KEY_NOT_READY
)
3203 && (asc
== 0x04) && (ascq
== 0x04)) {
3204 if ((sense
->extra_len
>= 10)
3205 && ((sense
->sense_key_spec
[0] &
3206 SSD_SCS_VALID
) != 0)
3209 u_int64_t percentage
;
3212 &sense
->sense_key_spec
[1]);
3213 percentage
= 10000 * val
;
3216 "\rFormatting: %jd.%02jd %% "
3218 (intmax_t)percentage
/ (0x10000 * 100),
3219 (intmax_t)(percentage
/ 0x10000) % 100,
3222 } else if ((quiet
== 0)
3223 && (++num_warnings
<= 1)) {
3224 warnx("Unexpected SCSI Sense Key "
3225 "Specific value returned "
3227 scsi_sense_print(device
, &ccb
->csio
,
3229 warnx("Unable to print status "
3230 "information, but format will "
3232 warnx("will exit when format is "
3237 warnx("Unexpected SCSI error during format");
3238 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3239 CAM_EPF_ALL
, stderr
);
3241 goto scsiformat_bailout
;
3244 } else if (status
!= CAM_REQ_CMP
) {
3245 warnx("Unexpected CAM status %#x", status
);
3246 if (arglist
& CAM_ARG_VERBOSE
)
3247 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3248 CAM_EPF_ALL
, stderr
);
3250 goto scsiformat_bailout
;
3253 } while((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
);
3256 fprintf(stdout
, "\nFormat Complete\n");
3266 scsireportluns(struct cam_device
*device
, int argc
, char **argv
,
3267 char *combinedopt
, int retry_count
, int timeout
)
3270 int c
, countonly
, lunsonly
;
3271 struct scsi_report_luns_data
*lundata
;
3273 uint8_t report_type
;
3274 uint32_t list_len
, i
, j
;
3279 report_type
= RPL_REPORT_DEFAULT
;
3280 ccb
= cam_getccb(device
);
3283 warnx("%s: error allocating ccb", __func__
);
3287 bzero(&(&ccb
->ccb_h
)[1],
3288 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
3293 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
3302 if (strcasecmp(optarg
, "default") == 0)
3303 report_type
= RPL_REPORT_DEFAULT
;
3304 else if (strcasecmp(optarg
, "wellknown") == 0)
3305 report_type
= RPL_REPORT_WELLKNOWN
;
3306 else if (strcasecmp(optarg
, "all") == 0)
3307 report_type
= RPL_REPORT_ALL
;
3309 warnx("%s: invalid report type \"%s\"",
3320 if ((countonly
!= 0)
3321 && (lunsonly
!= 0)) {
3322 warnx("%s: you can only specify one of -c or -l", __func__
);
3327 * According to SPC-4, the allocation length must be at least 16
3328 * bytes -- enough for the header and one LUN.
3330 alloc_len
= sizeof(*lundata
) + 8;
3334 lundata
= malloc(alloc_len
);
3336 if (lundata
== NULL
) {
3337 warn("%s: error mallocing %d bytes", __func__
, alloc_len
);
3342 scsi_report_luns(&ccb
->csio
,
3343 /*retries*/ retry_count
,
3345 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
3346 /*select_report*/ report_type
,
3347 /*rpl_buf*/ lundata
,
3348 /*alloc_len*/ alloc_len
,
3349 /*sense_len*/ SSD_FULL_SIZE
,
3350 /*timeout*/ timeout
? timeout
: 5000);
3352 /* Disable freezing the device queue */
3353 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
3355 if (arglist
& CAM_ARG_ERR_RECOVER
)
3356 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
3358 if (cam_send_ccb(device
, ccb
) < 0) {
3359 warn("error sending REPORT LUNS command");
3361 if (arglist
& CAM_ARG_VERBOSE
)
3362 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3363 CAM_EPF_ALL
, stderr
);
3369 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
3370 cam_error_print(device
, ccb
, CAM_ESF_ALL
, CAM_EPF_ALL
, stderr
);
3376 list_len
= scsi_4btoul(lundata
->length
);
3379 * If we need to list the LUNs, and our allocation
3380 * length was too short, reallocate and retry.
3382 if ((countonly
== 0)
3383 && (list_len
> (alloc_len
- sizeof(*lundata
)))) {
3384 alloc_len
= list_len
+ sizeof(*lundata
);
3390 fprintf(stdout
, "%u LUN%s found\n", list_len
/ 8,
3391 ((list_len
/ 8) > 1) ? "s" : "");
3396 for (i
= 0; i
< (list_len
/ 8); i
++) {
3400 for (j
= 0; j
< sizeof(lundata
->luns
[i
].lundata
); j
+= 2) {
3402 fprintf(stdout
, ",");
3403 switch (lundata
->luns
[i
].lundata
[j
] &
3404 RPL_LUNDATA_ATYP_MASK
) {
3405 case RPL_LUNDATA_ATYP_PERIPH
:
3406 if ((lundata
->luns
[i
].lundata
[j
] &
3407 RPL_LUNDATA_PERIPH_BUS_MASK
) != 0)
3408 fprintf(stdout
, "%d:",
3409 lundata
->luns
[i
].lundata
[j
] &
3410 RPL_LUNDATA_PERIPH_BUS_MASK
);
3412 && ((lundata
->luns
[i
].lundata
[j
+2] &
3413 RPL_LUNDATA_PERIPH_BUS_MASK
) == 0))
3416 fprintf(stdout
, "%d",
3417 lundata
->luns
[i
].lundata
[j
+1]);
3419 case RPL_LUNDATA_ATYP_FLAT
: {
3421 tmplun
[0] = lundata
->luns
[i
].lundata
[j
] &
3422 RPL_LUNDATA_FLAT_LUN_MASK
;
3423 tmplun
[1] = lundata
->luns
[i
].lundata
[j
+1];
3425 fprintf(stdout
, "%d", scsi_2btoul(tmplun
));
3429 case RPL_LUNDATA_ATYP_LUN
:
3430 fprintf(stdout
, "%d:%d:%d",
3431 (lundata
->luns
[i
].lundata
[j
+1] &
3432 RPL_LUNDATA_LUN_BUS_MASK
) >> 5,
3433 lundata
->luns
[i
].lundata
[j
] &
3434 RPL_LUNDATA_LUN_TARG_MASK
,
3435 lundata
->luns
[i
].lundata
[j
+1] &
3436 RPL_LUNDATA_LUN_LUN_MASK
);
3438 case RPL_LUNDATA_ATYP_EXTLUN
: {
3439 int field_len_code
, eam_code
;
3441 eam_code
= lundata
->luns
[i
].lundata
[j
] &
3442 RPL_LUNDATA_EXT_EAM_MASK
;
3443 field_len_code
= (lundata
->luns
[i
].lundata
[j
] &
3444 RPL_LUNDATA_EXT_LEN_MASK
) >> 4;
3446 if ((eam_code
== RPL_LUNDATA_EXT_EAM_WK
)
3447 && (field_len_code
== 0x00)) {
3448 fprintf(stdout
, "%d",
3449 lundata
->luns
[i
].lundata
[j
+1]);
3450 } else if ((eam_code
==
3451 RPL_LUNDATA_EXT_EAM_NOT_SPEC
)
3452 && (field_len_code
== 0x03)) {
3456 * This format takes up all 8 bytes.
3457 * If we aren't starting at offset 0,
3461 fprintf(stdout
, "Invalid "
3464 "specified format", j
);
3468 bzero(tmp_lun
, sizeof(tmp_lun
));
3469 bcopy(&lundata
->luns
[i
].lundata
[j
+1],
3470 &tmp_lun
[1], sizeof(tmp_lun
) - 1);
3471 fprintf(stdout
, "%#jx",
3472 (intmax_t)scsi_8btou64(tmp_lun
));
3475 fprintf(stderr
, "Unknown Extended LUN"
3476 "Address method %#x, length "
3477 "code %#x", eam_code
,
3484 fprintf(stderr
, "Unknown LUN address method "
3485 "%#x\n", lundata
->luns
[i
].lundata
[0] &
3486 RPL_LUNDATA_ATYP_MASK
);
3490 * For the flat addressing method, there are no
3491 * other levels after it.
3496 fprintf(stdout
, "\n");
3509 scsireadcapacity(struct cam_device
*device
, int argc
, char **argv
,
3510 char *combinedopt
, int retry_count
, int timeout
)
3513 int blocksizeonly
, humanize
, numblocks
, quiet
, sizeonly
, baseten
;
3514 struct scsi_read_capacity_data rcap
;
3515 struct scsi_read_capacity_data_16 rcaplong
;
3529 ccb
= cam_getccb(device
);
3532 warnx("%s: error allocating ccb", __func__
);
3536 bzero(&(&ccb
->ccb_h
)[1],
3537 sizeof(struct ccb_scsiio
) - sizeof(struct ccb_hdr
));
3539 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
3566 if ((blocksizeonly
!= 0)
3567 && (numblocks
!= 0)) {
3568 warnx("%s: you can only specify one of -b or -N", __func__
);
3573 if ((blocksizeonly
!= 0)
3574 && (sizeonly
!= 0)) {
3575 warnx("%s: you can only specify one of -b or -s", __func__
);
3582 warnx("%s: you can only specify one of -h/-H or -q", __func__
);
3588 && (blocksizeonly
!= 0)) {
3589 warnx("%s: you can only specify one of -h/-H or -b", __func__
);
3594 scsi_read_capacity(&ccb
->csio
,
3595 /*retries*/ retry_count
,
3597 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
3600 /*timeout*/ timeout
? timeout
: 5000);
3602 /* Disable freezing the device queue */
3603 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
3605 if (arglist
& CAM_ARG_ERR_RECOVER
)
3606 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
3608 if (cam_send_ccb(device
, ccb
) < 0) {
3609 warn("error sending READ CAPACITY command");
3611 if (arglist
& CAM_ARG_VERBOSE
)
3612 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3613 CAM_EPF_ALL
, stderr
);
3619 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
3620 cam_error_print(device
, ccb
, CAM_ESF_ALL
, CAM_EPF_ALL
, stderr
);
3625 maxsector
= scsi_4btoul(rcap
.addr
);
3626 block_len
= scsi_4btoul(rcap
.length
);
3629 * A last block of 2^32-1 means that the true capacity is over 2TB,
3630 * and we need to issue the long READ CAPACITY to get the real
3631 * capacity. Otherwise, we're all set.
3633 if (maxsector
!= 0xffffffff)
3636 scsi_read_capacity_16(&ccb
->csio
,
3637 /*retries*/ retry_count
,
3639 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
3644 /*sense_len*/ SSD_FULL_SIZE
,
3645 /*timeout*/ timeout
? timeout
: 5000);
3647 /* Disable freezing the device queue */
3648 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
3650 if (arglist
& CAM_ARG_ERR_RECOVER
)
3651 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
3653 if (cam_send_ccb(device
, ccb
) < 0) {
3654 warn("error sending READ CAPACITY (16) command");
3656 if (arglist
& CAM_ARG_VERBOSE
)
3657 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
3658 CAM_EPF_ALL
, stderr
);
3664 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
3665 cam_error_print(device
, ccb
, CAM_ESF_ALL
, CAM_EPF_ALL
, stderr
);
3670 maxsector
= scsi_8btou64(rcaplong
.addr
);
3671 block_len
= scsi_4btoul(rcaplong
.length
);
3674 if (blocksizeonly
== 0) {
3676 * Humanize implies !quiet, and also implies numblocks.
3678 if (humanize
!= 0) {
3683 tmpbytes
= (maxsector
+ 1) * block_len
;
3684 ret
= humanize_number(tmpstr
, sizeof(tmpstr
),
3685 tmpbytes
, "", HN_AUTOSCALE
,
3688 HN_DIVISOR_1000
: 0));
3690 warnx("%s: humanize_number failed!", __func__
);
3694 fprintf(stdout
, "Device Size: %s%s", tmpstr
,
3695 (sizeonly
== 0) ? ", " : "\n");
3696 } else if (numblocks
!= 0) {
3697 fprintf(stdout
, "%s%ju%s", (quiet
== 0) ?
3698 "Blocks: " : "", (uintmax_t)maxsector
+ 1,
3699 (sizeonly
== 0) ? ", " : "\n");
3701 fprintf(stdout
, "%s%ju%s", (quiet
== 0) ?
3702 "Last Block: " : "", (uintmax_t)maxsector
,
3703 (sizeonly
== 0) ? ", " : "\n");
3707 fprintf(stdout
, "%s%u%s\n", (quiet
== 0) ?
3708 "Block Length: " : "", block_len
, (quiet
== 0) ?
3716 #endif /* MINIMALISTIC */
3721 fprintf(verbose
? stdout
: stderr
,
3722 "usage: camcontrol <command> [device id][generic args][command args]\n"
3723 " camcontrol devlist [-b][-v]\n"
3724 #ifndef MINIMALISTIC
3725 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3726 " camcontrol tur [dev_id][generic args]\n"
3727 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3728 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
3729 " camcontrol readcap [dev_id][generic args] [-b] [-h] [-H] [-N]\n"
3731 " camcontrol start [dev_id][generic args]\n"
3732 " camcontrol stop [dev_id][generic args]\n"
3733 " camcontrol load [dev_id][generic args]\n"
3734 " camcontrol eject [dev_id][generic args]\n"
3735 #endif /* MINIMALISTIC */
3736 " camcontrol rescan <all | bus[:target:lun]>\n"
3737 " camcontrol reset <all | bus[:target:lun]>\n"
3738 #ifndef MINIMALISTIC
3739 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3740 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3741 " [-P pagectl][-e | -b][-d]\n"
3742 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3743 " [-i len fmt|-o len fmt [args]]\n"
3744 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3745 " <all|bus[:target[:lun]]|off>\n"
3746 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3747 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3748 " [-D <enable|disable>][-O offset][-q]\n"
3749 " [-R syncrate][-v][-T <enable|disable>]\n"
3750 " [-U][-W bus_width]\n"
3751 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
3752 #endif /* MINIMALISTIC */
3753 " camcontrol help\n");
3756 #ifndef MINIMALISTIC
3758 "Specify one of the following options:\n"
3759 "devlist list all CAM devices\n"
3760 "periphlist list all CAM peripheral drivers attached to a device\n"
3761 "tur send a test unit ready to the named device\n"
3762 "inquiry send a SCSI inquiry command to the named device\n"
3763 "reportluns send a SCSI report luns command to the device\n"
3764 "readcap send a SCSI read capacity command to the device\n"
3765 "start send a Start Unit command to the device\n"
3766 "stop send a Stop Unit command to the device\n"
3767 "load send a Start Unit command to the device with the load bit set\n"
3768 "eject send a Stop Unit command to the device with the eject bit set\n"
3769 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3770 "reset reset all busses, the given bus, or bus:target:lun\n"
3771 "defects read the defect list of the specified device\n"
3772 "modepage display or edit (-e) the given mode page\n"
3773 "cmd send the given scsi command, may need -i or -o as well\n"
3774 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3775 "tags report or set the number of transaction slots for a device\n"
3776 "negotiate report or set device negotiation parameters\n"
3777 "format send the SCSI FORMAT UNIT command to the named device\n"
3778 "help this message\n"
3779 "Device Identifiers:\n"
3780 "bus:target specify the bus and target, lun defaults to 0\n"
3781 "bus:target:lun specify the bus, target and lun\n"
3782 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3783 "Generic arguments:\n"
3784 "-v be verbose, print out sense information\n"
3785 "-t timeout command timeout in seconds, overrides default timeout\n"
3786 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3787 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3788 "-E have the kernel attempt to perform SCSI error recovery\n"
3789 "-C count specify the SCSI command retry count (needs -E to work)\n"
3790 "modepage arguments:\n"
3791 "-l list all available mode pages\n"
3792 "-m page specify the mode page to view or edit\n"
3793 "-e edit the specified mode page\n"
3794 "-b force view to binary mode\n"
3795 "-d disable block descriptors for mode sense\n"
3796 "-P pgctl page control field 0-3\n"
3797 "defects arguments:\n"
3798 "-f format specify defect list format (block, bfi or phys)\n"
3799 "-G get the grown defect list\n"
3800 "-P get the permanent defect list\n"
3801 "inquiry arguments:\n"
3802 "-D get the standard inquiry data\n"
3803 "-S get the serial number\n"
3804 "-R get the transfer rate, etc.\n"
3805 "reportluns arguments:\n"
3806 "-c only report a count of available LUNs\n"
3807 "-l only print out luns, and not a count\n"
3808 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
3809 "readcap arguments\n"
3810 "-b only report the blocksize\n"
3811 "-h human readable device size, base 2\n"
3812 "-H human readable device size, base 10\n"
3813 "-N print the number of blocks instead of last block\n"
3814 "-q quiet, print numbers only\n"
3815 "-s only report the last block/device size\n"
3817 "-c cdb [args] specify the SCSI CDB\n"
3818 "-i len fmt specify input data and input data format\n"
3819 "-o len fmt [args] specify output data and output data fmt\n"
3820 "debug arguments:\n"
3821 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3822 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3823 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3824 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3826 "-N tags specify the number of tags to use for this device\n"
3827 "-q be quiet, don't report the number of tags\n"
3828 "-v report a number of tag-related parameters\n"
3829 "negotiate arguments:\n"
3830 "-a send a test unit ready after negotiation\n"
3831 "-c report/set current negotiation settings\n"
3832 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3833 "-O offset set command delay offset\n"
3834 "-q be quiet, don't report anything\n"
3835 "-R syncrate synchronization rate in MHz\n"
3836 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3837 "-U report/set user negotiation settings\n"
3838 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3839 "-v also print a Path Inquiry CCB for the controller\n"
3840 "format arguments:\n"
3841 "-q be quiet, don't print status messages\n"
3842 "-r run in report only mode\n"
3843 "-w don't send immediate format command\n"
3844 "-y don't ask any questions\n");
3845 #endif /* MINIMALISTIC */
3849 main(int argc
, char **argv
)
3852 char *device
= NULL
;
3854 struct cam_device
*cam_dev
= NULL
;
3855 int timeout
= 0, retry_count
= 1;
3856 camcontrol_optret optreturn
;
3858 const char *mainopt
= "C:En:t:u:v";
3859 const char *subopt
= NULL
;
3860 char combinedopt
[256];
3861 int error
= 0, optstart
= 2;
3864 cmdlist
= CAM_CMD_NONE
;
3865 arglist
= CAM_ARG_NONE
;
3873 * Get the base option.
3875 optreturn
= getoption(argv
[1], &cmdlist
, &arglist
, &subopt
);
3877 if (optreturn
== CC_OR_AMBIGUOUS
) {
3878 warnx("ambiguous option %s", argv
[1]);
3881 } else if (optreturn
== CC_OR_NOT_FOUND
) {
3882 warnx("option %s not found", argv
[1]);
3888 * Ahh, getopt(3) is a pain.
3890 * This is a gross hack. There really aren't many other good
3891 * options (excuse the pun) for parsing options in a situation like
3892 * this. getopt is kinda braindead, so you end up having to run
3893 * through the options twice, and give each invocation of getopt
3894 * the option string for the other invocation.
3896 * You would think that you could just have two groups of options.
3897 * The first group would get parsed by the first invocation of
3898 * getopt, and the second group would get parsed by the second
3899 * invocation of getopt. It doesn't quite work out that way. When
3900 * the first invocation of getopt finishes, it leaves optind pointing
3901 * to the argument _after_ the first argument in the second group.
3902 * So when the second invocation of getopt comes around, it doesn't
3903 * recognize the first argument it gets and then bails out.
3905 * A nice alternative would be to have a flag for getopt that says
3906 * "just keep parsing arguments even when you encounter an unknown
3907 * argument", but there isn't one. So there's no real clean way to
3908 * easily parse two sets of arguments without having one invocation
3909 * of getopt know about the other.
3911 * Without this hack, the first invocation of getopt would work as
3912 * long as the generic arguments are first, but the second invocation
3913 * (in the subfunction) would fail in one of two ways. In the case
3914 * where you don't set optreset, it would fail because optind may be
3915 * pointing to the argument after the one it should be pointing at.
3916 * In the case where you do set optreset, and reset optind, it would
3917 * fail because getopt would run into the first set of options, which
3918 * it doesn't understand.
3920 * All of this would "sort of" work if you could somehow figure out
3921 * whether optind had been incremented one option too far. The
3922 * mechanics of that, however, are more daunting than just giving
3923 * both invocations all of the expect options for either invocation.
3925 * Needless to say, I wouldn't mind if someone invented a better
3926 * (non-GPL!) command line parsing interface than getopt. I
3927 * wouldn't mind if someone added more knobs to getopt to make it
3928 * work better. Who knows, I may talk myself into doing it someday,
3929 * if the standards weenies let me. As it is, it just leads to
3930 * hackery like this and causes people to avoid it in some cases.
3932 * KDM, September 8th, 1998
3935 sprintf(combinedopt
, "%s%s", mainopt
, subopt
);
3937 sprintf(combinedopt
, "%s", mainopt
);
3940 * For these options we do not parse optional device arguments and
3941 * we do not open a passthrough device.
3943 if ((cmdlist
== CAM_CMD_RESCAN
)
3944 || (cmdlist
== CAM_CMD_RESET
)
3945 || (cmdlist
== CAM_CMD_DEVTREE
)
3946 || (cmdlist
== CAM_CMD_USAGE
)
3947 || (cmdlist
== CAM_CMD_DEBUG
))
3950 #ifndef MINIMALISTIC
3952 && (argc
> 2 && argv
[2][0] != '-')) {
3957 * First catch people who try to do things like:
3958 * camcontrol tur /dev/da0
3959 * camcontrol doesn't take device nodes as arguments.
3961 if (argv
[2][0] == '/') {
3962 warnx("%s is not a valid device identifier", argv
[2]);
3963 errx(1, "please read the camcontrol(8) man page");
3964 } else if (isdigit(argv
[2][0])) {
3965 /* device specified as bus:target[:lun] */
3966 rv
= parse_btl(argv
[2], &bus
, &target
, &lun
, &arglist
);
3968 errx(1, "numeric device specification must "
3969 "be either bus:target, or "
3971 /* default to 0 if lun was not specified */
3972 if ((arglist
& CAM_ARG_LUN
) == 0) {
3974 arglist
|= CAM_ARG_LUN
;
3978 if (cam_get_device(argv
[2], name
, sizeof name
, &unit
)
3980 errx(1, "%s", cam_errbuf
);
3981 device
= strdup(name
);
3982 arglist
|= CAM_ARG_DEVICE
| CAM_ARG_UNIT
;
3986 #endif /* MINIMALISTIC */
3988 * Start getopt processing at argv[2/3], since we've already
3989 * accepted argv[1..2] as the command name, and as a possible
3995 * Now we run through the argument list looking for generic
3996 * options, and ignoring options that possibly belong to
3999 while ((c
= getopt(argc
, argv
, combinedopt
))!= -1){
4002 retry_count
= strtol(optarg
, NULL
, 0);
4003 if (retry_count
< 0)
4004 errx(1, "retry count %d is < 0",
4006 arglist
|= CAM_ARG_RETRIES
;
4009 arglist
|= CAM_ARG_ERR_RECOVER
;
4012 arglist
|= CAM_ARG_DEVICE
;
4014 while (isspace(*tstr
) && (*tstr
!= '\0'))
4016 device
= (char *)strdup(tstr
);
4019 timeout
= strtol(optarg
, NULL
, 0);
4021 errx(1, "invalid timeout %d", timeout
);
4022 /* Convert the timeout from seconds to ms */
4024 arglist
|= CAM_ARG_TIMEOUT
;
4027 arglist
|= CAM_ARG_UNIT
;
4028 unit
= strtol(optarg
, NULL
, 0);
4031 arglist
|= CAM_ARG_VERBOSE
;
4038 #ifndef MINIMALISTIC
4040 * For most commands we'll want to open the passthrough device
4041 * associated with the specified device. In the case of the rescan
4042 * commands, we don't use a passthrough device at all, just the
4043 * transport layer device.
4046 if (((arglist
& (CAM_ARG_BUS
|CAM_ARG_TARGET
)) == 0)
4047 && (((arglist
& CAM_ARG_DEVICE
) == 0)
4048 || ((arglist
& CAM_ARG_UNIT
) == 0))) {
4049 errx(1, "subcommand \"%s\" requires a valid device "
4050 "identifier", argv
[1]);
4053 if ((cam_dev
= ((arglist
& (CAM_ARG_BUS
| CAM_ARG_TARGET
))?
4054 cam_open_btl(bus
, target
, lun
, O_RDWR
, NULL
) :
4055 cam_open_spec_device(device
,unit
,O_RDWR
,NULL
)))
4057 errx(1,"%s", cam_errbuf
);
4059 #endif /* MINIMALISTIC */
4062 * Reset optind to 2, and reset getopt, so these routines can parse
4063 * the arguments again.
4069 #ifndef MINIMALISTIC
4070 case CAM_CMD_DEVLIST
:
4071 error
= getdevlist(cam_dev
);
4073 #endif /* MINIMALISTIC */
4074 case CAM_CMD_DEVTREE
:
4075 error
= getdevtree(argc
, argv
, combinedopt
);
4077 #ifndef MINIMALISTIC
4079 error
= testunitready(cam_dev
, retry_count
, timeout
, 0);
4081 case CAM_CMD_INQUIRY
:
4082 error
= scsidoinquiry(cam_dev
, argc
, argv
, combinedopt
,
4083 retry_count
, timeout
);
4085 case CAM_CMD_STARTSTOP
:
4086 error
= scsistart(cam_dev
, arglist
& CAM_ARG_START_UNIT
,
4087 arglist
& CAM_ARG_EJECT
, retry_count
,
4090 #endif /* MINIMALISTIC */
4091 case CAM_CMD_RESCAN
:
4092 error
= dorescan_or_reset(argc
, argv
, 1);
4095 error
= dorescan_or_reset(argc
, argv
, 0);
4097 #ifndef MINIMALISTIC
4098 case CAM_CMD_READ_DEFECTS
:
4099 error
= readdefects(cam_dev
, argc
, argv
, combinedopt
,
4100 retry_count
, timeout
);
4102 case CAM_CMD_MODE_PAGE
:
4103 modepage(cam_dev
, argc
, argv
, combinedopt
,
4104 retry_count
, timeout
);
4106 case CAM_CMD_SCSI_CMD
:
4107 error
= scsicmd(cam_dev
, argc
, argv
, combinedopt
,
4108 retry_count
, timeout
);
4111 error
= camdebug(argc
, argv
, combinedopt
);
4114 error
= tagcontrol(cam_dev
, argc
, argv
, combinedopt
);
4117 error
= ratecontrol(cam_dev
, retry_count
, timeout
,
4118 argc
, argv
, combinedopt
);
4120 case CAM_CMD_FORMAT
:
4121 error
= scsiformat(cam_dev
, argc
, argv
,
4122 combinedopt
, retry_count
, timeout
);
4124 case CAM_CMD_REPORTLUNS
:
4125 error
= scsireportluns(cam_dev
, argc
, argv
,
4126 combinedopt
, retry_count
,
4129 case CAM_CMD_READCAP
:
4130 error
= scsireadcapacity(cam_dev
, argc
, argv
,
4131 combinedopt
, retry_count
,
4135 case CAM_CMD_STANDBY
:
4137 error
= atapm(cam_dev
, argc
, argv
,
4138 combinedopt
, retry_count
, timeout
);
4141 case CAM_CMD_SECURITY
:
4142 error
= atasecurity(cam_dev
, retry_count
, timeout
,
4143 argc
, argv
, combinedopt
);
4145 case CAM_CMD_DOWNLOAD_FW
:
4146 error
= fwdownload(cam_dev
, argc
, argv
, combinedopt
,
4147 arglist
& CAM_ARG_VERBOSE
, retry_count
, timeout
,
4148 get_disk_type(cam_dev
));
4152 case CAM_CMD_SANITIZE
:
4153 error
= scsisanitize(cam_dev
, argc
, argv
,
4154 combinedopt
, retry_count
, timeout
);
4156 case CAM_CMD_PERSIST
:
4157 error
= scsipersist(cam_dev
, argc
, argv
, combinedopt
,
4158 retry_count
, timeout
, arglist
& CAM_ARG_VERBOSE
,
4159 arglist
& CAM_ARG_ERR_RECOVER
);
4162 #endif /* MINIMALISTIC */
4172 if (cam_dev
!= NULL
)
4173 cam_close_device(cam_dev
);
4179 atapm(struct cam_device
*device
, int argc
, char **argv
,
4180 char *combinedopt
, int retry_count
, int timeout
)
4187 struct ata_pass_12
*pass12
;
4189 ccb
= cam_getccb(device
);
4192 warnx("%s: error allocating ccb", __func__
);
4196 while ((c
= getopt(argc
, argv
, combinedopt
)) != -1) {
4205 if (strcmp(argv
[1], "idle") == 0) {
4207 cmd
= ATA_IDLE_IMMEDIATE
;
4210 } else if (strcmp(argv
[1], "standby") == 0) {
4212 cmd
= ATA_STANDBY_IMMEDIATE
;
4214 cmd
= ATA_STANDBY_CMD
;
4222 else if (t
<= (240 * 5))
4224 else if (t
<= (252 * 5))
4225 /* special encoding for 21 minutes */
4227 else if (t
<= (11 * 30 * 60))
4228 sc
= (t
- 1) / (30 * 60) + 241;
4232 cam_fill_csio(&ccb
->csio
,
4233 /*retries*/ retry_count
,
4235 /*flags*/ CAM_DIR_NONE
|
4236 ((arglist
& CAM_ARG_ERR_RECOVER
) ?
4237 CAM_PASS_ERR_RECOVER
: 0),
4238 /*tag_action*/ MSG_SIMPLE_Q_TAG
,
4241 /*sense_len*/ SSD_FULL_SIZE
,
4242 /*cdb_len*/ sizeof(*pass12
),
4243 /*timeout*/ timeout
? timeout
: 30 * 1000);
4245 pass12
= (struct ata_pass_12
*)&ccb
->csio
.cdb_io
.cdb_bytes
;
4246 bzero(pass12
, sizeof(*pass12
));
4247 pass12
->opcode
= ATA_PASS_12
;
4248 pass12
->command
= cmd
;
4249 pass12
->sector_count
= sc
;
4251 /* Disable freezing the device queue */
4252 ccb
->ccb_h
.flags
|= CAM_DEV_QFRZDIS
;
4254 if (arglist
& CAM_ARG_ERR_RECOVER
)
4255 ccb
->ccb_h
.flags
|= CAM_PASS_ERR_RECOVER
;
4257 if (cam_send_ccb(device
, ccb
) < 0) {
4258 warn("error sending command");
4260 if (arglist
& CAM_ARG_VERBOSE
)
4261 cam_error_print(device
, ccb
, CAM_ESF_ALL
,
4262 CAM_EPF_ALL
, stderr
);
4268 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) != CAM_REQ_CMP
) {
4269 cam_error_print(device
, ccb
, CAM_ESF_ALL
, CAM_EPF_ALL
, stderr
);