2 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2005 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 $
29 * $DragonFly: src/sbin/camcontrol/camcontrol.c,v 1.8 2007/11/29 01:57:29 pavalos Exp $
32 #include <sys/ioctl.h>
33 #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>
50 #include "camcontrol.h"
53 CAM_CMD_NONE = 0x00000000,
54 CAM_CMD_DEVLIST = 0x00000001,
55 CAM_CMD_TUR = 0x00000002,
56 CAM_CMD_INQUIRY = 0x00000003,
57 CAM_CMD_STARTSTOP = 0x00000004,
58 CAM_CMD_RESCAN = 0x00000005,
59 CAM_CMD_READ_DEFECTS = 0x00000006,
60 CAM_CMD_MODE_PAGE = 0x00000007,
61 CAM_CMD_SCSI_CMD = 0x00000008,
62 CAM_CMD_DEVTREE = 0x00000009,
63 CAM_CMD_USAGE = 0x0000000a,
64 CAM_CMD_DEBUG = 0x0000000b,
65 CAM_CMD_RESET = 0x0000000c,
66 CAM_CMD_FORMAT = 0x0000000d,
67 CAM_CMD_TAG = 0x0000000e,
68 CAM_CMD_RATE = 0x0000000f,
69 CAM_CMD_DETACH = 0x00000010,
73 CAM_ARG_NONE = 0x00000000,
74 CAM_ARG_VERBOSE = 0x00000001,
75 CAM_ARG_DEVICE = 0x00000002,
76 CAM_ARG_BUS = 0x00000004,
77 CAM_ARG_TARGET = 0x00000008,
78 CAM_ARG_LUN = 0x00000010,
79 CAM_ARG_EJECT = 0x00000020,
80 CAM_ARG_UNIT = 0x00000040,
81 CAM_ARG_FORMAT_BLOCK = 0x00000080,
82 CAM_ARG_FORMAT_BFI = 0x00000100,
83 CAM_ARG_FORMAT_PHYS = 0x00000200,
84 CAM_ARG_PLIST = 0x00000400,
85 CAM_ARG_GLIST = 0x00000800,
86 CAM_ARG_GET_SERIAL = 0x00001000,
87 CAM_ARG_GET_STDINQ = 0x00002000,
88 CAM_ARG_GET_XFERRATE = 0x00004000,
89 CAM_ARG_INQ_MASK = 0x00007000,
90 CAM_ARG_MODE_EDIT = 0x00008000,
91 CAM_ARG_PAGE_CNTL = 0x00010000,
92 CAM_ARG_TIMEOUT = 0x00020000,
93 CAM_ARG_CMD_IN = 0x00040000,
94 CAM_ARG_CMD_OUT = 0x00080000,
95 CAM_ARG_DBD = 0x00100000,
96 CAM_ARG_ERR_RECOVER = 0x00200000,
97 CAM_ARG_RETRIES = 0x00400000,
98 CAM_ARG_START_UNIT = 0x00800000,
99 CAM_ARG_DEBUG_INFO = 0x01000000,
100 CAM_ARG_DEBUG_TRACE = 0x02000000,
101 CAM_ARG_DEBUG_SUBTRACE = 0x04000000,
102 CAM_ARG_DEBUG_CDB = 0x08000000,
103 CAM_ARG_DEBUG_XPT = 0x10000000,
104 CAM_ARG_DEBUG_PERIPH = 0x20000000,
107 struct camcontrol_opts {
115 static const char scsicmd_opts[] = "c:i:o:";
116 static const char readdefect_opts[] = "f:GP";
117 static const char negotiate_opts[] = "acD:O:qR:T:UW:";
120 struct camcontrol_opts option_table[] = {
122 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
123 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
124 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
125 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
126 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
127 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
128 #endif /* MINIMALISTIC */
129 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
130 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
132 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
133 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
134 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
135 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
136 #endif /* MINIMALISTIC */
137 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
139 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
140 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
141 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
142 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
143 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
144 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
145 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
146 #endif /* MINIMALISTIC */
147 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
148 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
149 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
161 int bus, target, lun;
164 camcontrol_optret getoption(char *, cam_cmdmask *, cam_argmask *,
167 static int getdevlist(struct cam_device *);
168 static int getdevtree(void);
169 static int testunitready(struct cam_device *, int, int, int);
170 static int scsistart(struct cam_device *, int, int, int, int);
171 static int scsidoinquiry(struct cam_device *, int, char **, char *, int,
173 static int scsiinquiry(struct cam_device *, int, int);
174 static int scsiserial(struct cam_device *, int, int);
175 static int scsixferrate(struct cam_device *);
176 #endif /* MINIMALISTIC */
177 static int parse_btl(char *, int *, int *, int *, cam_argmask *);
178 static int dorescan_or_reset(int, char **, int);
179 static int rescan_or_reset_bus(int, int);
180 static int scanlun_or_reset_dev(int, int, int, int);
182 static int readdefects(struct cam_device *, int, char **, char *, int,
184 static void modepage(struct cam_device *, int, char **, char *, int, int);
185 static int scsicmd(struct cam_device *, int, char **, char *, int, int);
186 static int tagcontrol(struct cam_device *, int, char **, char *);
187 static void cts_print(struct cam_device *device,
188 struct ccb_trans_settings *);
189 static void cpi_print(struct ccb_pathinq *);
190 static int get_cpi(struct cam_device *, struct ccb_pathinq *);
191 static int get_print_cts(struct cam_device *, int, int,
192 struct ccb_trans_settings *);
193 static int ratecontrol(struct cam_device *, int, int, int, char **,
195 static int scsiformat(struct cam_device *, int, char **, char *, int, int);
196 #endif /* MINIMALISTIC */
200 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
203 struct camcontrol_opts *opts;
206 for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
208 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
209 *cmdnum = opts->cmdnum;
210 *argnum = opts->argnum;
211 *subopt = opts->subopt;
212 if (++num_matches > 1)
213 return(CC_OR_AMBIGUOUS);
220 return(CC_OR_NOT_FOUND);
225 getdevlist(struct cam_device *device)
231 ccb = cam_getccb(device);
233 ccb->ccb_h.func_code = XPT_GDEVLIST;
234 ccb->ccb_h.flags = CAM_DIR_NONE;
235 ccb->ccb_h.retry_count = 1;
237 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
238 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
239 if (cam_send_ccb(device, ccb) < 0) {
240 perror("error getting device list");
247 switch (ccb->cgdl.status) {
248 case CAM_GDEVLIST_MORE_DEVS:
249 strcpy(status, "MORE");
251 case CAM_GDEVLIST_LAST_DEVICE:
252 strcpy(status, "LAST");
254 case CAM_GDEVLIST_LIST_CHANGED:
255 strcpy(status, "CHANGED");
257 case CAM_GDEVLIST_ERROR:
258 strcpy(status, "ERROR");
263 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
264 ccb->cgdl.periph_name,
265 ccb->cgdl.unit_number,
266 ccb->cgdl.generation,
271 * If the list has changed, we need to start over from the
274 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
282 #endif /* MINIMALISTIC */
294 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
295 warn("couldn't open %s", XPT_DEVICE);
299 bzero(&ccb, sizeof(union ccb));
301 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
302 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
303 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
305 ccb.ccb_h.func_code = XPT_DEV_MATCH;
306 bufsize = sizeof(struct dev_match_result) * 100;
307 ccb.cdm.match_buf_len = bufsize;
308 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
309 if (ccb.cdm.matches == NULL) {
310 warnx("can't malloc memory for matches");
314 ccb.cdm.num_matches = 0;
317 * We fetch all nodes, since we display most of them in the default
318 * case, and all in the verbose case.
320 ccb.cdm.num_patterns = 0;
321 ccb.cdm.pattern_buf_len = 0;
324 * We do the ioctl multiple times if necessary, in case there are
325 * more than 100 nodes in the EDT.
328 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
329 warn("error sending CAMIOCOMMAND ioctl");
334 if ((ccb.ccb_h.status != CAM_REQ_CMP)
335 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
336 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
337 warnx("got CAM error %#x, CDM error %d\n",
338 ccb.ccb_h.status, ccb.cdm.status);
343 for (i = 0; i < ccb.cdm.num_matches; i++) {
344 switch (ccb.cdm.matches[i].type) {
345 case DEV_MATCH_BUS: {
346 struct bus_match_result *bus_result;
349 * Only print the bus information if the
350 * user turns on the verbose flag.
352 if ((arglist & CAM_ARG_VERBOSE) == 0)
356 &ccb.cdm.matches[i].result.bus_result;
359 fprintf(stdout, ")\n");
363 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
365 bus_result->dev_name,
366 bus_result->unit_number,
370 case DEV_MATCH_DEVICE: {
371 struct device_match_result *dev_result;
372 char vendor[16], product[48], revision[16];
376 &ccb.cdm.matches[i].result.device_result;
378 if ((dev_result->flags
379 & DEV_RESULT_UNCONFIGURED)
380 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
386 cam_strvis(vendor, dev_result->inq_data.vendor,
387 sizeof(dev_result->inq_data.vendor),
390 dev_result->inq_data.product,
391 sizeof(dev_result->inq_data.product),
394 dev_result->inq_data.revision,
395 sizeof(dev_result->inq_data.revision),
397 sprintf(tmpstr, "<%s %s %s>", vendor, product,
400 fprintf(stdout, ")\n");
404 fprintf(stdout, "%-33s at scbus%d "
405 "target %d lun %d (",
408 dev_result->target_id,
409 dev_result->target_lun);
415 case DEV_MATCH_PERIPH: {
416 struct periph_match_result *periph_result;
419 &ccb.cdm.matches[i].result.periph_result;
421 if (skip_device != 0)
425 fprintf(stdout, ",");
427 fprintf(stdout, "%s%d",
428 periph_result->periph_name,
429 periph_result->unit_number);
435 fprintf(stdout, "unknown match type\n");
440 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
441 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
444 fprintf(stdout, ")\n");
453 testunitready(struct cam_device *device, int retry_count, int timeout,
459 ccb = cam_getccb(device);
461 scsi_test_unit_ready(&ccb->csio,
462 /* retries */ retry_count,
464 /* tag_action */ MSG_SIMPLE_Q_TAG,
465 /* sense_len */ SSD_FULL_SIZE,
466 /* timeout */ timeout ? timeout : 5000);
468 /* Disable freezing the device queue */
469 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
471 if (arglist & CAM_ARG_ERR_RECOVER)
472 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
474 if (cam_send_ccb(device, ccb) < 0) {
476 perror("error sending test unit ready");
478 if (arglist & CAM_ARG_VERBOSE) {
479 cam_error_print(device, ccb, CAM_ESF_ALL,
480 CAM_EPF_ALL, stderr);
487 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
489 fprintf(stdout, "Unit is ready\n");
492 fprintf(stdout, "Unit is not ready\n");
495 if (arglist & CAM_ARG_VERBOSE) {
496 cam_error_print(device, ccb, CAM_ESF_ALL,
497 CAM_EPF_ALL, stderr);
507 scsistart(struct cam_device *device, int startstop, int loadeject,
508 int retry_count, int timeout)
513 ccb = cam_getccb(device);
516 * If we're stopping, send an ordered tag so the drive in question
517 * will finish any previously queued writes before stopping. If
518 * the device isn't capable of tagged queueing, or if tagged
519 * queueing is turned off, the tag action is a no-op.
521 scsi_start_stop(&ccb->csio,
522 /* retries */ retry_count,
524 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
526 /* start/stop */ startstop,
527 /* load_eject */ loadeject,
529 /* sense_len */ SSD_FULL_SIZE,
530 /* timeout */ timeout ? timeout : 120000);
532 /* Disable freezing the device queue */
533 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
535 if (arglist & CAM_ARG_ERR_RECOVER)
536 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
538 if (cam_send_ccb(device, ccb) < 0) {
539 perror("error sending start unit");
541 if (arglist & CAM_ARG_VERBOSE) {
542 cam_error_print(device, ccb, CAM_ESF_ALL,
543 CAM_EPF_ALL, stderr);
550 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
552 fprintf(stdout, "Unit started successfully");
554 fprintf(stdout,", Media loaded\n");
556 fprintf(stdout,"\n");
558 fprintf(stdout, "Unit stopped successfully");
560 fprintf(stdout, ", Media ejected\n");
562 fprintf(stdout, "\n");
568 "Error received from start unit command\n");
571 "Error received from stop unit command\n");
573 if (arglist & CAM_ARG_VERBOSE) {
574 cam_error_print(device, ccb, CAM_ESF_ALL,
575 CAM_EPF_ALL, stderr);
585 scsidoinquiry(struct cam_device *device, int argc, char **argv,
586 char *combinedopt, int retry_count, int timeout)
591 while ((c = getopt(argc, argv, combinedopt)) != -1) {
594 arglist |= CAM_ARG_GET_STDINQ;
597 arglist |= CAM_ARG_GET_XFERRATE;
600 arglist |= CAM_ARG_GET_SERIAL;
608 * If the user didn't specify any inquiry options, he wants all of
611 if ((arglist & CAM_ARG_INQ_MASK) == 0)
612 arglist |= CAM_ARG_INQ_MASK;
614 if (arglist & CAM_ARG_GET_STDINQ)
615 error = scsiinquiry(device, retry_count, timeout);
620 if (arglist & CAM_ARG_GET_SERIAL)
621 scsiserial(device, retry_count, timeout);
626 if (arglist & CAM_ARG_GET_XFERRATE)
627 error = scsixferrate(device);
633 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
636 struct scsi_inquiry_data *inq_buf;
639 ccb = cam_getccb(device);
642 warnx("couldn't allocate CCB");
646 /* cam_getccb cleans up the header, caller has to zero the payload */
647 bzero(&(&ccb->ccb_h)[1],
648 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
650 inq_buf = (struct scsi_inquiry_data *)malloc(
651 sizeof(struct scsi_inquiry_data));
653 if (inq_buf == NULL) {
655 warnx("can't malloc memory for inquiry\n");
658 bzero(inq_buf, sizeof(*inq_buf));
661 * Note that although the size of the inquiry buffer is the full
662 * 256 bytes specified in the SCSI spec, we only tell the device
663 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
664 * two reasons for this:
666 * - The SCSI spec says that when a length field is only 1 byte,
667 * a value of 0 will be interpreted as 256. Therefore
668 * scsi_inquiry() will convert an inq_len (which is passed in as
669 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
670 * to 0. Evidently, very few devices meet the spec in that
671 * regard. Some devices, like many Seagate disks, take the 0 as
672 * 0, and don't return any data. One Pioneer DVD-R drive
673 * returns more data than the command asked for.
675 * So, since there are numerous devices that just don't work
676 * right with the full inquiry size, we don't send the full size.
678 * - The second reason not to use the full inquiry data length is
679 * that we don't need it here. The only reason we issue a
680 * standard inquiry is to get the vendor name, device name,
681 * and revision so scsi_print_inquiry() can print them.
683 * If, at some point in the future, more inquiry data is needed for
684 * some reason, this code should use a procedure similar to the
685 * probe code. i.e., issue a short inquiry, and determine from
686 * the additional length passed back from the device how much
687 * inquiry data the device supports. Once the amount the device
688 * supports is determined, issue an inquiry for that amount and no
693 scsi_inquiry(&ccb->csio,
694 /* retries */ retry_count,
696 /* tag_action */ MSG_SIMPLE_Q_TAG,
697 /* inq_buf */ (u_int8_t *)inq_buf,
698 /* inq_len */ SHORT_INQUIRY_LENGTH,
701 /* sense_len */ SSD_FULL_SIZE,
702 /* timeout */ timeout ? timeout : 5000);
704 /* Disable freezing the device queue */
705 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
707 if (arglist & CAM_ARG_ERR_RECOVER)
708 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
710 if (cam_send_ccb(device, ccb) < 0) {
711 perror("error sending SCSI inquiry");
713 if (arglist & CAM_ARG_VERBOSE) {
714 cam_error_print(device, ccb, CAM_ESF_ALL,
715 CAM_EPF_ALL, stderr);
722 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
725 if (arglist & CAM_ARG_VERBOSE) {
726 cam_error_print(device, ccb, CAM_ESF_ALL,
727 CAM_EPF_ALL, stderr);
738 fprintf(stdout, "%s%d: ", device->device_name,
739 device->dev_unit_num);
740 scsi_print_inquiry(inq_buf);
748 scsiserial(struct cam_device *device, int retry_count, int timeout)
751 struct scsi_vpd_unit_serial_number *serial_buf;
752 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
755 ccb = cam_getccb(device);
758 warnx("couldn't allocate CCB");
762 /* cam_getccb cleans up the header, caller has to zero the payload */
763 bzero(&(&ccb->ccb_h)[1],
764 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
766 serial_buf = (struct scsi_vpd_unit_serial_number *)
767 malloc(sizeof(*serial_buf));
769 if (serial_buf == NULL) {
771 warnx("can't malloc memory for serial number");
775 scsi_inquiry(&ccb->csio,
776 /*retries*/ retry_count,
778 /* tag_action */ MSG_SIMPLE_Q_TAG,
779 /* inq_buf */ (u_int8_t *)serial_buf,
780 /* inq_len */ sizeof(*serial_buf),
782 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
783 /* sense_len */ SSD_FULL_SIZE,
784 /* timeout */ timeout ? timeout : 5000);
786 /* Disable freezing the device queue */
787 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
789 if (arglist & CAM_ARG_ERR_RECOVER)
790 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
792 if (cam_send_ccb(device, ccb) < 0) {
793 warn("error getting serial number");
795 if (arglist & CAM_ARG_VERBOSE) {
796 cam_error_print(device, ccb, CAM_ESF_ALL,
797 CAM_EPF_ALL, stderr);
805 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
808 if (arglist & CAM_ARG_VERBOSE) {
809 cam_error_print(device, ccb, CAM_ESF_ALL,
810 CAM_EPF_ALL, stderr);
821 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
822 serial_num[serial_buf->length] = '\0';
824 if ((arglist & CAM_ARG_GET_STDINQ)
825 || (arglist & CAM_ARG_GET_XFERRATE))
826 fprintf(stdout, "%s%d: Serial Number ",
827 device->device_name, device->dev_unit_num);
829 fprintf(stdout, "%.60s\n", serial_num);
837 scsixferrate(struct cam_device *device)
845 ccb = cam_getccb(device);
848 warnx("couldn't allocate CCB");
852 bzero(&(&ccb->ccb_h)[1],
853 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
855 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
856 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
858 if (((retval = cam_send_ccb(device, ccb)) < 0)
859 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
860 const char error_string[] = "error getting transfer settings";
867 if (arglist & CAM_ARG_VERBOSE)
868 cam_error_print(device, ccb, CAM_ESF_ALL,
869 CAM_EPF_ALL, stderr);
873 goto xferrate_bailout;
877 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
878 && (ccb->cts.sync_offset != 0)) {
879 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
882 struct ccb_pathinq cpi;
884 retval = get_cpi(device, &cpi);
887 goto xferrate_bailout;
889 speed = cpi.base_transfer_speed;
893 fprintf(stdout, "%s%d: ", device->device_name,
894 device->dev_unit_num);
896 if ((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
897 speed *= (0x01 << device->bus_width);
902 fprintf(stdout, "%d.%03dMB/s transfers ",
905 fprintf(stdout, "%dKB/s transfers ",
908 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
909 && (ccb->cts.sync_offset != 0))
910 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
911 freq % 1000, ccb->cts.sync_offset);
913 if (((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
914 && (ccb->cts.bus_width > 0)) {
915 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
916 && (ccb->cts.sync_offset != 0)) {
917 fprintf(stdout, ", ");
919 fprintf(stdout, " (");
921 fprintf(stdout, "%dbit)", 8 * (0x01 << ccb->cts.bus_width));
922 } else if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
923 && (ccb->cts.sync_offset != 0)) {
924 fprintf(stdout, ")");
927 if (((ccb->cts.valid & CCB_TRANS_TQ_VALID) != 0)
928 && (ccb->cts.flags & CCB_TRANS_TAG_ENB))
929 fprintf(stdout, ", Tagged Queueing Enabled");
931 fprintf(stdout, "\n");
939 #endif /* MINIMALISTIC */
942 * Parse out a bus, or a bus, target and lun in the following
948 * Returns the number of parsed components, or 0.
951 parse_btl(char *tstr, int *mybus, int *mytarget, int *mylun,
952 cam_argmask *myarglist)
957 while (isspace(*tstr) && (*tstr != '\0'))
960 tmpstr = (char *)strtok(tstr, ":");
961 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
962 *mybus = strtol(tmpstr, NULL, 0);
963 *myarglist |= CAM_ARG_BUS;
965 tmpstr = (char *)strtok(NULL, ":");
966 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
967 *mytarget = strtol(tmpstr, NULL, 0);
968 *myarglist |= CAM_ARG_TARGET;
970 tmpstr = (char *)strtok(NULL, ":");
971 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
972 *mylun = strtol(tmpstr, NULL, 0);
973 *myarglist |= CAM_ARG_LUN;
983 dorescan_or_reset(int argc, char **argv, int rescan)
985 static const char must[] =
986 "you must specify \"all\", a bus, or a bus:target:lun to %s";
988 int mybus = -1, mytarget = -1, mylun = -1;
992 warnx(must, rescan? "rescan" : "reset");
997 while (isspace(*tstr) && (*tstr != '\0'))
999 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1000 arglist |= CAM_ARG_BUS;
1002 rv = parse_btl(argv[optind], &mybus, &mytarget, &mylun,
1004 if (rv != 1 && rv != 3) {
1005 warnx(must, rescan? "rescan" : "reset");
1010 if ((arglist & CAM_ARG_BUS)
1011 && (arglist & CAM_ARG_TARGET)
1012 && (arglist & CAM_ARG_LUN))
1013 error = scanlun_or_reset_dev(mybus, mytarget, mylun, rescan);
1015 error = rescan_or_reset_bus(mybus, rescan);
1021 rescan_or_reset_bus(int mybus, int rescan)
1023 union ccb ccb, matchccb;
1029 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1030 warnx("error opening transport layer device %s", XPT_DEVICE);
1031 warn("%s", XPT_DEVICE);
1036 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1037 ccb.ccb_h.path_id = mybus;
1038 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1039 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1040 ccb.crcn.flags = CAM_FLAG_NONE;
1042 /* run this at a low priority */
1043 ccb.ccb_h.pinfo.priority = 5;
1045 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1046 warn("CAMIOCOMMAND ioctl failed");
1051 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1052 fprintf(stdout, "%s of bus %d was successful\n",
1053 rescan ? "Re-scan" : "Reset", mybus);
1055 fprintf(stdout, "%s of bus %d returned error %#x\n",
1056 rescan ? "Re-scan" : "Reset", mybus,
1057 ccb.ccb_h.status & CAM_STATUS_MASK);
1068 * The right way to handle this is to modify the xpt so that it can
1069 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1070 * that isn't implemented, so instead we enumerate the busses and
1071 * send the rescan or reset to those busses in the case where the
1072 * given bus is -1 (wildcard). We don't send a rescan or reset
1073 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1074 * no-op, sending a rescan to the xpt bus would result in a status of
1077 bzero(&(&matchccb.ccb_h)[1],
1078 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1079 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1080 bufsize = sizeof(struct dev_match_result) * 20;
1081 matchccb.cdm.match_buf_len = bufsize;
1082 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1083 if (matchccb.cdm.matches == NULL) {
1084 warnx("can't malloc memory for matches");
1088 matchccb.cdm.num_matches = 0;
1090 matchccb.cdm.num_patterns = 1;
1091 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1093 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1094 matchccb.cdm.pattern_buf_len);
1095 if (matchccb.cdm.patterns == NULL) {
1096 warnx("can't malloc memory for patterns");
1100 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1101 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1106 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1107 warn("CAMIOCOMMAND ioctl failed");
1112 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1113 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1114 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1115 warnx("got CAM error %#x, CDM error %d\n",
1116 matchccb.ccb_h.status, matchccb.cdm.status);
1121 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1122 struct bus_match_result *bus_result;
1124 /* This shouldn't happen. */
1125 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1128 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1131 * We don't want to rescan or reset the xpt bus.
1134 if ((int)bus_result->path_id == -1)
1137 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1139 ccb.ccb_h.path_id = bus_result->path_id;
1140 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1141 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1142 ccb.crcn.flags = CAM_FLAG_NONE;
1144 /* run this at a low priority */
1145 ccb.ccb_h.pinfo.priority = 5;
1147 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1148 warn("CAMIOCOMMAND ioctl failed");
1153 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1154 fprintf(stdout, "%s of bus %d was successful\n",
1155 rescan? "Re-scan" : "Reset",
1156 bus_result->path_id);
1159 * Don't bail out just yet, maybe the other
1160 * rescan or reset commands will complete
1163 fprintf(stderr, "%s of bus %d returned error "
1164 "%#x\n", rescan? "Re-scan" : "Reset",
1165 bus_result->path_id,
1166 ccb.ccb_h.status & CAM_STATUS_MASK);
1170 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1171 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1178 if (matchccb.cdm.patterns != NULL)
1179 free(matchccb.cdm.patterns);
1180 if (matchccb.cdm.matches != NULL)
1181 free(matchccb.cdm.matches);
1187 scanlun_or_reset_dev(int mybus, int mytarget, int mylun, int scan)
1190 struct cam_device *device;
1196 warnx("invalid bus number %d", mybus);
1201 warnx("invalid target number %d", mytarget);
1206 warnx("invalid lun number %d", mylun);
1212 bzero(&ccb, sizeof(union ccb));
1215 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1216 warnx("error opening transport layer device %s\n",
1218 warn("%s", XPT_DEVICE);
1222 device = cam_open_btl(mybus, mytarget, mylun, O_RDWR, NULL);
1223 if (device == NULL) {
1224 warnx("%s", cam_errbuf);
1229 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1230 ccb.ccb_h.path_id = mybus;
1231 ccb.ccb_h.target_id = mytarget;
1232 ccb.ccb_h.target_lun = mylun;
1233 ccb.ccb_h.timeout = 5000;
1234 ccb.crcn.flags = CAM_FLAG_NONE;
1236 /* run this at a low priority */
1237 ccb.ccb_h.pinfo.priority = 5;
1240 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1241 warn("CAMIOCOMMAND ioctl failed");
1246 if (cam_send_ccb(device, &ccb) < 0) {
1247 warn("error sending XPT_RESET_DEV CCB");
1248 cam_close_device(device);
1256 cam_close_device(device);
1259 * An error code of CAM_BDR_SENT is normal for a BDR request.
1261 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1263 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1264 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1265 scan? "Re-scan" : "Reset", mybus, mytarget, mylun);
1268 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1269 scan? "Re-scan" : "Reset", mybus, mytarget, mylun,
1270 ccb.ccb_h.status & CAM_STATUS_MASK);
1275 #ifndef MINIMALISTIC
1277 readdefects(struct cam_device *device, int argc, char **argv,
1278 char *combinedopt, int retry_count, int timeout)
1280 union ccb *ccb = NULL;
1281 struct scsi_read_defect_data_10 *rdd_cdb;
1282 u_int8_t *defect_list = NULL;
1283 u_int32_t dlist_length = 65000;
1284 u_int32_t returned_length = 0;
1285 u_int32_t num_returned = 0;
1286 u_int8_t returned_format;
1289 int lists_specified = 0;
1291 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1297 while (isspace(*tstr) && (*tstr != '\0'))
1299 if (strcmp(tstr, "block") == 0)
1300 arglist |= CAM_ARG_FORMAT_BLOCK;
1301 else if (strcmp(tstr, "bfi") == 0)
1302 arglist |= CAM_ARG_FORMAT_BFI;
1303 else if (strcmp(tstr, "phys") == 0)
1304 arglist |= CAM_ARG_FORMAT_PHYS;
1307 warnx("invalid defect format %s", tstr);
1308 goto defect_bailout;
1313 arglist |= CAM_ARG_GLIST;
1316 arglist |= CAM_ARG_PLIST;
1323 ccb = cam_getccb(device);
1326 * Hopefully 65000 bytes is enough to hold the defect list. If it
1327 * isn't, the disk is probably dead already. We'd have to go with
1328 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1331 defect_list = malloc(dlist_length);
1332 if (defect_list == NULL) {
1333 warnx("can't malloc memory for defect list");
1335 goto defect_bailout;
1338 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1341 * cam_getccb() zeros the CCB header only. So we need to zero the
1342 * payload portion of the ccb.
1344 bzero(&(&ccb->ccb_h)[1],
1345 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1347 cam_fill_csio(&ccb->csio,
1348 /*retries*/ retry_count,
1350 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1351 CAM_PASS_ERR_RECOVER : 0),
1352 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1353 /*data_ptr*/ defect_list,
1354 /*dxfer_len*/ dlist_length,
1355 /*sense_len*/ SSD_FULL_SIZE,
1356 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1357 /*timeout*/ timeout ? timeout : 5000);
1359 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1360 if (arglist & CAM_ARG_FORMAT_BLOCK)
1361 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1362 else if (arglist & CAM_ARG_FORMAT_BFI)
1363 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1364 else if (arglist & CAM_ARG_FORMAT_PHYS)
1365 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1368 warnx("no defect list format specified");
1369 goto defect_bailout;
1371 if (arglist & CAM_ARG_PLIST) {
1372 rdd_cdb->format |= SRDD10_PLIST;
1376 if (arglist & CAM_ARG_GLIST) {
1377 rdd_cdb->format |= SRDD10_GLIST;
1381 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1383 /* Disable freezing the device queue */
1384 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1386 if (cam_send_ccb(device, ccb) < 0) {
1387 perror("error reading defect list");
1389 if (arglist & CAM_ARG_VERBOSE) {
1390 cam_error_print(device, ccb, CAM_ESF_ALL,
1391 CAM_EPF_ALL, stderr);
1395 goto defect_bailout;
1398 returned_length = scsi_2btoul(((struct
1399 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1401 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1402 defect_list)->format;
1404 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1405 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1406 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1407 struct scsi_sense_data *sense;
1408 int error_code, sense_key, asc, ascq;
1410 sense = &ccb->csio.sense_data;
1411 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1414 * According to the SCSI spec, if the disk doesn't support
1415 * the requested format, it will generally return a sense
1416 * key of RECOVERED ERROR, and an additional sense code
1417 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1418 * also check to make sure that the returned length is
1419 * greater than 0, and then print out whatever format the
1422 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1423 && (asc == 0x1c) && (ascq == 0x00)
1424 && (returned_length > 0)) {
1425 warnx("requested defect format not available");
1426 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1427 case SRDD10_BLOCK_FORMAT:
1428 warnx("Device returned block format");
1430 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1431 warnx("Device returned bytes from index"
1434 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1435 warnx("Device returned physical sector format");
1439 warnx("Device returned unknown defect"
1440 " data format %#x", returned_format);
1441 goto defect_bailout;
1442 break; /* NOTREACHED */
1446 warnx("Error returned from read defect data command");
1447 if (arglist & CAM_ARG_VERBOSE)
1448 cam_error_print(device, ccb, CAM_ESF_ALL,
1449 CAM_EPF_ALL, stderr);
1450 goto defect_bailout;
1452 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1454 warnx("Error returned from read defect data command");
1455 if (arglist & CAM_ARG_VERBOSE)
1456 cam_error_print(device, ccb, CAM_ESF_ALL,
1457 CAM_EPF_ALL, stderr);
1458 goto defect_bailout;
1462 * XXX KDM I should probably clean up the printout format for the
1465 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1466 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1468 struct scsi_defect_desc_phys_sector *dlist;
1470 dlist = (struct scsi_defect_desc_phys_sector *)
1472 sizeof(struct scsi_read_defect_data_hdr_10));
1474 num_returned = returned_length /
1475 sizeof(struct scsi_defect_desc_phys_sector);
1477 fprintf(stderr, "Got %d defect", num_returned);
1479 if ((lists_specified == 0) || (num_returned == 0)) {
1480 fprintf(stderr, "s.\n");
1482 } else if (num_returned == 1)
1483 fprintf(stderr, ":\n");
1485 fprintf(stderr, "s:\n");
1487 for (i = 0; i < num_returned; i++) {
1488 fprintf(stdout, "%d:%d:%d\n",
1489 scsi_3btoul(dlist[i].cylinder),
1491 scsi_4btoul(dlist[i].sector));
1495 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1497 struct scsi_defect_desc_bytes_from_index *dlist;
1499 dlist = (struct scsi_defect_desc_bytes_from_index *)
1501 sizeof(struct scsi_read_defect_data_hdr_10));
1503 num_returned = returned_length /
1504 sizeof(struct scsi_defect_desc_bytes_from_index);
1506 fprintf(stderr, "Got %d defect", num_returned);
1508 if ((lists_specified == 0) || (num_returned == 0)) {
1509 fprintf(stderr, "s.\n");
1511 } else if (num_returned == 1)
1512 fprintf(stderr, ":\n");
1514 fprintf(stderr, "s:\n");
1516 for (i = 0; i < num_returned; i++) {
1517 fprintf(stdout, "%d:%d:%d\n",
1518 scsi_3btoul(dlist[i].cylinder),
1520 scsi_4btoul(dlist[i].bytes_from_index));
1524 case SRDDH10_BLOCK_FORMAT:
1526 struct scsi_defect_desc_block *dlist;
1528 dlist = (struct scsi_defect_desc_block *)(defect_list +
1529 sizeof(struct scsi_read_defect_data_hdr_10));
1531 num_returned = returned_length /
1532 sizeof(struct scsi_defect_desc_block);
1534 fprintf(stderr, "Got %d defect", num_returned);
1536 if ((lists_specified == 0) || (num_returned == 0)) {
1537 fprintf(stderr, "s.\n");
1539 } else if (num_returned == 1)
1540 fprintf(stderr, ":\n");
1542 fprintf(stderr, "s:\n");
1544 for (i = 0; i < num_returned; i++)
1545 fprintf(stdout, "%u\n",
1546 scsi_4btoul(dlist[i].address));
1550 fprintf(stderr, "Unknown defect format %d\n",
1551 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1557 if (defect_list != NULL)
1565 #endif /* MINIMALISTIC */
1569 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1573 ccb = cam_getccb(device);
1579 #ifndef MINIMALISTIC
1581 mode_sense(struct cam_device *device, int mode_page, int page_control,
1582 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1587 ccb = cam_getccb(device);
1590 errx(1, "mode_sense: couldn't allocate CCB");
1592 bzero(&(&ccb->ccb_h)[1],
1593 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1595 scsi_mode_sense(&ccb->csio,
1596 /* retries */ retry_count,
1598 /* tag_action */ MSG_SIMPLE_Q_TAG,
1600 /* page_code */ page_control << 6,
1601 /* page */ mode_page,
1602 /* param_buf */ data,
1603 /* param_len */ datalen,
1604 /* sense_len */ SSD_FULL_SIZE,
1605 /* timeout */ timeout ? timeout : 5000);
1607 if (arglist & CAM_ARG_ERR_RECOVER)
1608 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1610 /* Disable freezing the device queue */
1611 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1613 if (((retval = cam_send_ccb(device, ccb)) < 0)
1614 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1615 if (arglist & CAM_ARG_VERBOSE) {
1616 cam_error_print(device, ccb, CAM_ESF_ALL,
1617 CAM_EPF_ALL, stderr);
1620 cam_close_device(device);
1622 err(1, "error sending mode sense command");
1624 errx(1, "error sending mode sense command");
1631 mode_select(struct cam_device *device, int save_pages, int retry_count,
1632 int timeout, u_int8_t *data, int datalen)
1637 ccb = cam_getccb(device);
1640 errx(1, "mode_select: couldn't allocate CCB");
1642 bzero(&(&ccb->ccb_h)[1],
1643 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1645 scsi_mode_select(&ccb->csio,
1646 /* retries */ retry_count,
1648 /* tag_action */ MSG_SIMPLE_Q_TAG,
1649 /* scsi_page_fmt */ 1,
1650 /* save_pages */ save_pages,
1651 /* param_buf */ data,
1652 /* param_len */ datalen,
1653 /* sense_len */ SSD_FULL_SIZE,
1654 /* timeout */ timeout ? timeout : 5000);
1656 if (arglist & CAM_ARG_ERR_RECOVER)
1657 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1659 /* Disable freezing the device queue */
1660 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1662 if (((retval = cam_send_ccb(device, ccb)) < 0)
1663 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1664 if (arglist & CAM_ARG_VERBOSE) {
1665 cam_error_print(device, ccb, CAM_ESF_ALL,
1666 CAM_EPF_ALL, stderr);
1669 cam_close_device(device);
1672 err(1, "error sending mode select command");
1674 errx(1, "error sending mode select command");
1682 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1683 int retry_count, int timeout)
1685 int c, mode_page = -1, page_control = 0;
1686 int binary = 0, list = 0;
1688 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1694 arglist |= CAM_ARG_DBD;
1697 arglist |= CAM_ARG_MODE_EDIT;
1703 mode_page = strtol(optarg, NULL, 0);
1705 errx(1, "invalid mode page %d", mode_page);
1708 page_control = strtol(optarg, NULL, 0);
1709 if ((page_control < 0) || (page_control > 3))
1710 errx(1, "invalid page control field %d",
1712 arglist |= CAM_ARG_PAGE_CNTL;
1719 if (mode_page == -1 && list == 0)
1720 errx(1, "you must specify a mode page!");
1723 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1724 retry_count, timeout);
1726 mode_edit(device, mode_page, page_control,
1727 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1728 retry_count, timeout);
1733 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1734 int retry_count, int timeout)
1737 u_int32_t flags = CAM_DIR_NONE;
1738 u_int8_t *data_ptr = NULL;
1740 struct get_hook hook;
1741 int c, data_bytes = 0;
1743 char *datastr = NULL, *tstr;
1748 ccb = cam_getccb(device);
1751 warnx("scsicmd: error allocating ccb");
1755 bzero(&(&ccb->ccb_h)[1],
1756 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1758 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1762 while (isspace(*tstr) && (*tstr != '\0'))
1764 hook.argc = argc - optind;
1765 hook.argv = argv + optind;
1767 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1770 * Increment optind by the number of arguments the
1771 * encoding routine processed. After each call to
1772 * getopt(3), optind points to the argument that
1773 * getopt should process _next_. In this case,
1774 * that means it points to the first command string
1775 * argument, if there is one. Once we increment
1776 * this, it should point to either the next command
1777 * line argument, or it should be past the end of
1783 if (arglist & CAM_ARG_CMD_OUT) {
1784 warnx("command must either be "
1785 "read or write, not both");
1787 goto scsicmd_bailout;
1789 arglist |= CAM_ARG_CMD_IN;
1791 data_bytes = strtol(optarg, NULL, 0);
1792 if (data_bytes <= 0) {
1793 warnx("invalid number of input bytes %d",
1796 goto scsicmd_bailout;
1798 hook.argc = argc - optind;
1799 hook.argv = argv + optind;
1802 datastr = cget(&hook, NULL);
1804 * If the user supplied "-" instead of a format, he
1805 * wants the data to be written to stdout.
1807 if ((datastr != NULL)
1808 && (datastr[0] == '-'))
1811 data_ptr = (u_int8_t *)malloc(data_bytes);
1812 if (data_ptr == NULL) {
1813 warnx("can't malloc memory for data_ptr");
1815 goto scsicmd_bailout;
1819 if (arglist & CAM_ARG_CMD_IN) {
1820 warnx("command must either be "
1821 "read or write, not both");
1823 goto scsicmd_bailout;
1825 arglist |= CAM_ARG_CMD_OUT;
1826 flags = CAM_DIR_OUT;
1827 data_bytes = strtol(optarg, NULL, 0);
1828 if (data_bytes <= 0) {
1829 warnx("invalid number of output bytes %d",
1832 goto scsicmd_bailout;
1834 hook.argc = argc - optind;
1835 hook.argv = argv + optind;
1837 datastr = cget(&hook, NULL);
1838 data_ptr = (u_int8_t *)malloc(data_bytes);
1839 if (data_ptr == NULL) {
1840 warnx("can't malloc memory for data_ptr");
1842 goto scsicmd_bailout;
1845 * If the user supplied "-" instead of a format, he
1846 * wants the data to be read from stdin.
1848 if ((datastr != NULL)
1849 && (datastr[0] == '-'))
1852 buff_encode_visit(data_ptr, data_bytes, datastr,
1862 * If fd_data is set, and we're writing to the device, we need to
1863 * read the data the user wants written from stdin.
1865 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1867 int amt_to_read = data_bytes;
1868 u_int8_t *buf_ptr = data_ptr;
1870 for (amt_read = 0; amt_to_read > 0;
1871 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1872 if (amt_read == -1) {
1873 warn("error reading data from stdin");
1875 goto scsicmd_bailout;
1877 amt_to_read -= amt_read;
1878 buf_ptr += amt_read;
1882 if (arglist & CAM_ARG_ERR_RECOVER)
1883 flags |= CAM_PASS_ERR_RECOVER;
1885 /* Disable freezing the device queue */
1886 flags |= CAM_DEV_QFRZDIS;
1889 * This is taken from the SCSI-3 draft spec.
1890 * (T10/1157D revision 0.3)
1891 * The top 3 bits of an opcode are the group code. The next 5 bits
1892 * are the command code.
1893 * Group 0: six byte commands
1894 * Group 1: ten byte commands
1895 * Group 2: ten byte commands
1897 * Group 4: sixteen byte commands
1898 * Group 5: twelve byte commands
1899 * Group 6: vendor specific
1900 * Group 7: vendor specific
1902 switch((cdb[0] >> 5) & 0x7) {
1913 /* computed by buff_encode_visit */
1924 * We should probably use csio_build_visit or something like that
1925 * here, but it's easier to encode arguments as you go. The
1926 * alternative would be skipping the CDB argument and then encoding
1927 * it here, since we've got the data buffer argument by now.
1929 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1931 cam_fill_csio(&ccb->csio,
1932 /*retries*/ retry_count,
1935 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1936 /*data_ptr*/ data_ptr,
1937 /*dxfer_len*/ data_bytes,
1938 /*sense_len*/ SSD_FULL_SIZE,
1939 /*cdb_len*/ cdb_len,
1940 /*timeout*/ timeout ? timeout : 5000);
1942 if (((retval = cam_send_ccb(device, ccb)) < 0)
1943 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1945 warn("error sending command");
1947 warnx("error sending command");
1949 if (arglist & CAM_ARG_VERBOSE) {
1950 cam_error_print(device, ccb, CAM_ESF_ALL,
1951 CAM_EPF_ALL, stderr);
1955 goto scsicmd_bailout;
1959 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1960 && (arglist & CAM_ARG_CMD_IN)
1961 && (data_bytes > 0)) {
1963 buff_decode_visit(data_ptr, data_bytes, datastr,
1965 fprintf(stdout, "\n");
1967 ssize_t amt_written;
1968 int amt_to_write = data_bytes;
1969 u_int8_t *buf_ptr = data_ptr;
1971 for (amt_written = 0; (amt_to_write > 0) &&
1972 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
1973 amt_to_write -= amt_written;
1974 buf_ptr += amt_written;
1976 if (amt_written == -1) {
1977 warn("error writing data to stdout");
1979 goto scsicmd_bailout;
1980 } else if ((amt_written == 0)
1981 && (amt_to_write > 0)) {
1982 warnx("only wrote %u bytes out of %u",
1983 data_bytes - amt_to_write, data_bytes);
1990 if ((data_bytes > 0) && (data_ptr != NULL))
1999 camdebug(int argc, char **argv, char *combinedopt)
2002 int mybus = -1, mytarget = -1, mylun = -1;
2003 char *tstr, *tmpstr = NULL;
2007 bzero(&ccb, sizeof(union ccb));
2009 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2012 arglist |= CAM_ARG_DEBUG_INFO;
2013 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2016 arglist |= CAM_ARG_DEBUG_PERIPH;
2017 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2020 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2021 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2024 arglist |= CAM_ARG_DEBUG_TRACE;
2025 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2028 arglist |= CAM_ARG_DEBUG_XPT;
2029 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2032 arglist |= CAM_ARG_DEBUG_CDB;
2033 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2040 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2041 warnx("error opening transport layer device %s", XPT_DEVICE);
2042 warn("%s", XPT_DEVICE);
2049 warnx("you must specify \"off\", \"all\" or a bus,");
2050 warnx("bus:target, or bus:target:lun");
2057 while (isspace(*tstr) && (*tstr != '\0'))
2060 if (strncmp(tstr, "off", 3) == 0) {
2061 ccb.cdbg.flags = CAM_DEBUG_NONE;
2062 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2063 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2065 } else if (strncmp(tstr, "all", 3) != 0) {
2066 tmpstr = (char *)strtok(tstr, ":");
2067 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2068 mybus = strtol(tmpstr, NULL, 0);
2069 arglist |= CAM_ARG_BUS;
2070 tmpstr = (char *)strtok(NULL, ":");
2071 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2072 mytarget = strtol(tmpstr, NULL, 0);
2073 arglist |= CAM_ARG_TARGET;
2074 tmpstr = (char *)strtok(NULL, ":");
2075 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2076 mylun = strtol(tmpstr, NULL, 0);
2077 arglist |= CAM_ARG_LUN;
2082 warnx("you must specify \"all\", \"off\", or a bus,");
2083 warnx("bus:target, or bus:target:lun to debug");
2089 ccb.ccb_h.func_code = XPT_DEBUG;
2090 ccb.ccb_h.path_id = mybus;
2091 ccb.ccb_h.target_id = mytarget;
2092 ccb.ccb_h.target_lun = mylun;
2094 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2095 warn("CAMIOCOMMAND ioctl failed");
2100 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2101 CAM_FUNC_NOTAVAIL) {
2102 warnx("CAM debugging not available");
2103 warnx("you need to put options CAMDEBUG in"
2104 " your kernel config file!");
2106 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2108 warnx("XPT_DEBUG CCB failed with status %#x",
2112 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2114 "Debugging turned off\n");
2117 "Debugging enabled for "
2119 mybus, mytarget, mylun);
2130 tagcontrol(struct cam_device *device, int argc, char **argv,
2140 ccb = cam_getccb(device);
2143 warnx("tagcontrol: error allocating ccb");
2147 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2150 numtags = strtol(optarg, NULL, 0);
2152 warnx("tag count %d is < 0", numtags);
2154 goto tagcontrol_bailout;
2165 cam_path_string(device, pathstr, sizeof(pathstr));
2168 bzero(&(&ccb->ccb_h)[1],
2169 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2170 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2171 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2172 ccb->crs.openings = numtags;
2175 if (cam_send_ccb(device, ccb) < 0) {
2176 perror("error sending XPT_REL_SIMQ CCB");
2178 goto tagcontrol_bailout;
2181 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2182 warnx("XPT_REL_SIMQ CCB failed");
2183 cam_error_print(device, ccb, CAM_ESF_ALL,
2184 CAM_EPF_ALL, stderr);
2186 goto tagcontrol_bailout;
2191 fprintf(stdout, "%stagged openings now %d\n",
2192 pathstr, ccb->crs.openings);
2195 bzero(&(&ccb->ccb_h)[1],
2196 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2198 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2200 if (cam_send_ccb(device, ccb) < 0) {
2201 perror("error sending XPT_GDEV_STATS CCB");
2203 goto tagcontrol_bailout;
2206 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2207 warnx("XPT_GDEV_STATS CCB failed");
2208 cam_error_print(device, ccb, CAM_ESF_ALL,
2209 CAM_EPF_ALL, stderr);
2211 goto tagcontrol_bailout;
2214 if (arglist & CAM_ARG_VERBOSE) {
2215 fprintf(stdout, "%s", pathstr);
2216 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2217 fprintf(stdout, "%s", pathstr);
2218 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2219 fprintf(stdout, "%s", pathstr);
2220 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2221 fprintf(stdout, "%s", pathstr);
2222 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2223 fprintf(stdout, "%s", pathstr);
2224 fprintf(stdout, "held %d\n", ccb->cgds.held);
2225 fprintf(stdout, "%s", pathstr);
2226 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2227 fprintf(stdout, "%s", pathstr);
2228 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2231 fprintf(stdout, "%s", pathstr);
2232 fprintf(stdout, "device openings: ");
2234 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2235 ccb->cgds.dev_active);
2245 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2249 cam_path_string(device, pathstr, sizeof(pathstr));
2251 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
2253 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2256 if (cts->sync_offset != 0) {
2259 freq = scsi_calc_syncsrate(cts->sync_period);
2260 fprintf(stdout, "%sfrequency: %d.%03dMHz\n", pathstr,
2261 freq / 1000, freq % 1000);
2265 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID)
2266 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset);
2268 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID)
2269 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2270 (0x01 << cts->bus_width) * 8);
2272 if (cts->valid & CCB_TRANS_DISC_VALID)
2273 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2274 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" :
2277 if (cts->valid & CCB_TRANS_TQ_VALID)
2278 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2279 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" :
2285 * Get a path inquiry CCB for the specified device.
2288 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2293 ccb = cam_getccb(device);
2296 warnx("get_cpi: couldn't allocate CCB");
2300 bzero(&(&ccb->ccb_h)[1],
2301 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2303 ccb->ccb_h.func_code = XPT_PATH_INQ;
2305 if (cam_send_ccb(device, ccb) < 0) {
2306 warn("get_cpi: error sending Path Inquiry CCB");
2308 if (arglist & CAM_ARG_VERBOSE)
2309 cam_error_print(device, ccb, CAM_ESF_ALL,
2310 CAM_EPF_ALL, stderr);
2314 goto get_cpi_bailout;
2317 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2319 if (arglist & CAM_ARG_VERBOSE)
2320 cam_error_print(device, ccb, CAM_ESF_ALL,
2321 CAM_EPF_ALL, stderr);
2325 goto get_cpi_bailout;
2328 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2338 cpi_print(struct ccb_pathinq *cpi)
2340 char adapter_str[1024];
2343 snprintf(adapter_str, sizeof(adapter_str),
2344 "%s%d:", cpi->dev_name, cpi->unit_number);
2346 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2349 for (i = 1; i < 0xff; i = i << 1) {
2352 if ((i & cpi->hba_inquiry) == 0)
2355 fprintf(stdout, "%s supports ", adapter_str);
2359 str = "MDP message";
2362 str = "32 bit wide SCSI";
2365 str = "16 bit wide SCSI";
2368 str = "SDTR message";
2371 str = "linked CDBs";
2374 str = "tag queue messages";
2377 str = "soft reset alternative";
2380 str = "unknown PI bit set";
2383 fprintf(stdout, "%s\n", str);
2386 for (i = 1; i < 0xff; i = i << 1) {
2389 if ((i & cpi->hba_misc) == 0)
2392 fprintf(stdout, "%s ", adapter_str);
2396 str = "bus scans from high ID to low ID";
2399 str = "removable devices not included in scan";
2401 case PIM_NOINITIATOR:
2402 str = "initiator role not supported";
2404 case PIM_NOBUSRESET:
2405 str = "user has disabled initial BUS RESET or"
2406 " controller is in target/mixed mode";
2409 str = "unknown PIM bit set";
2412 fprintf(stdout, "%s\n", str);
2415 for (i = 1; i < 0xff; i = i << 1) {
2418 if ((i & cpi->target_sprt) == 0)
2421 fprintf(stdout, "%s supports ", adapter_str);
2424 str = "target mode processor mode";
2427 str = "target mode phase cog. mode";
2429 case PIT_DISCONNECT:
2430 str = "disconnects in target mode";
2433 str = "terminate I/O message in target mode";
2436 str = "group 6 commands in target mode";
2439 str = "group 7 commands in target mode";
2442 str = "unknown PIT bit set";
2446 fprintf(stdout, "%s\n", str);
2448 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2450 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2452 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2454 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2455 adapter_str, cpi->hpath_id);
2456 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2458 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2459 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2460 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2461 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2462 if (cpi->base_transfer_speed > 1000)
2463 fprintf(stdout, "%d.%03dMB/sec\n",
2464 cpi->base_transfer_speed / 1000,
2465 cpi->base_transfer_speed % 1000);
2467 fprintf(stdout, "%dKB/sec\n",
2468 (cpi->base_transfer_speed % 1000) * 1000);
2472 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2473 struct ccb_trans_settings *cts)
2479 ccb = cam_getccb(device);
2482 warnx("get_print_cts: error allocating ccb");
2486 bzero(&(&ccb->ccb_h)[1],
2487 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2489 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2491 if (user_settings == 0)
2492 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2494 ccb->cts.flags = CCB_TRANS_USER_SETTINGS;
2496 if (cam_send_ccb(device, ccb) < 0) {
2497 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2498 if (arglist & CAM_ARG_VERBOSE)
2499 cam_error_print(device, ccb, CAM_ESF_ALL,
2500 CAM_EPF_ALL, stderr);
2502 goto get_print_cts_bailout;
2505 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2506 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2507 if (arglist & CAM_ARG_VERBOSE)
2508 cam_error_print(device, ccb, CAM_ESF_ALL,
2509 CAM_EPF_ALL, stderr);
2511 goto get_print_cts_bailout;
2515 cts_print(device, &ccb->cts);
2518 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2520 get_print_cts_bailout:
2528 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2529 int argc, char **argv, char *combinedopt)
2533 int user_settings = 0;
2535 int disc_enable = -1, tag_enable = -1;
2537 double syncrate = -1;
2540 int change_settings = 0, send_tur = 0;
2541 struct ccb_pathinq cpi;
2543 ccb = cam_getccb(device);
2546 warnx("ratecontrol: error allocating ccb");
2550 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2559 if (strncasecmp(optarg, "enable", 6) == 0)
2561 else if (strncasecmp(optarg, "disable", 7) == 0)
2564 warnx("-D argument \"%s\" is unknown", optarg);
2566 goto ratecontrol_bailout;
2568 change_settings = 1;
2571 offset = strtol(optarg, NULL, 0);
2573 warnx("offset value %d is < 0", offset);
2575 goto ratecontrol_bailout;
2577 change_settings = 1;
2583 syncrate = atof(optarg);
2586 warnx("sync rate %f is < 0", syncrate);
2588 goto ratecontrol_bailout;
2590 change_settings = 1;
2593 if (strncasecmp(optarg, "enable", 6) == 0)
2595 else if (strncasecmp(optarg, "disable", 7) == 0)
2598 warnx("-T argument \"%s\" is unknown", optarg);
2600 goto ratecontrol_bailout;
2602 change_settings = 1;
2608 bus_width = strtol(optarg, NULL, 0);
2609 if (bus_width < 0) {
2610 warnx("bus width %d is < 0", bus_width);
2612 goto ratecontrol_bailout;
2614 change_settings = 1;
2621 bzero(&(&ccb->ccb_h)[1],
2622 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2625 * Grab path inquiry information, so we can determine whether
2626 * or not the initiator is capable of the things that the user
2629 ccb->ccb_h.func_code = XPT_PATH_INQ;
2631 if (cam_send_ccb(device, ccb) < 0) {
2632 perror("error sending XPT_PATH_INQ CCB");
2633 if (arglist & CAM_ARG_VERBOSE) {
2634 cam_error_print(device, ccb, CAM_ESF_ALL,
2635 CAM_EPF_ALL, stderr);
2638 goto ratecontrol_bailout;
2641 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2642 warnx("XPT_PATH_INQ CCB failed");
2643 if (arglist & CAM_ARG_VERBOSE) {
2644 cam_error_print(device, ccb, CAM_ESF_ALL,
2645 CAM_EPF_ALL, stderr);
2648 goto ratecontrol_bailout;
2651 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2653 bzero(&(&ccb->ccb_h)[1],
2654 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2657 fprintf(stdout, "Current Parameters:\n");
2659 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2662 goto ratecontrol_bailout;
2664 if (arglist & CAM_ARG_VERBOSE)
2667 if (change_settings) {
2668 if (disc_enable != -1) {
2669 ccb->cts.valid |= CCB_TRANS_DISC_VALID;
2670 if (disc_enable == 0)
2671 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB;
2673 ccb->cts.flags |= CCB_TRANS_DISC_ENB;
2675 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID;
2677 if (tag_enable != -1) {
2678 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2679 warnx("HBA does not support tagged queueing, "
2680 "so you cannot modify tag settings");
2682 goto ratecontrol_bailout;
2685 ccb->cts.valid |= CCB_TRANS_TQ_VALID;
2687 if (tag_enable == 0)
2688 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB;
2690 ccb->cts.flags |= CCB_TRANS_TAG_ENB;
2692 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID;
2695 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2696 warnx("HBA at %s%d is not cable of changing "
2697 "offset", cpi.dev_name,
2700 goto ratecontrol_bailout;
2702 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID;
2703 ccb->cts.sync_offset = offset;
2705 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID;
2707 if (syncrate != -1) {
2708 int prelim_sync_period;
2711 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2712 warnx("HBA at %s%d is not cable of changing "
2713 "transfer rates", cpi.dev_name,
2716 goto ratecontrol_bailout;
2719 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID;
2722 * The sync rate the user gives us is in MHz.
2723 * We need to translate it into KHz for this
2729 * Next, we calculate a "preliminary" sync period
2730 * in tenths of a nanosecond.
2733 prelim_sync_period = 0;
2735 prelim_sync_period = 10000000 / syncrate;
2737 ccb->cts.sync_period =
2738 scsi_calc_syncparam(prelim_sync_period);
2740 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
2742 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID;
2745 * The bus_width argument goes like this:
2749 * Therefore, if you shift the number of bits given on the
2750 * command line right by 4, you should get the correct
2753 if (bus_width != -1) {
2756 * We might as well validate things here with a
2757 * decipherable error message, rather than what
2758 * will probably be an indecipherable error message
2759 * by the time it gets back to us.
2761 if ((bus_width == 16)
2762 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2763 warnx("HBA does not support 16 bit bus width");
2765 goto ratecontrol_bailout;
2766 } else if ((bus_width == 32)
2767 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2768 warnx("HBA does not support 32 bit bus width");
2770 goto ratecontrol_bailout;
2771 } else if ((bus_width != 8)
2772 && (bus_width != 16)
2773 && (bus_width != 32)) {
2774 warnx("Invalid bus width %d", bus_width);
2776 goto ratecontrol_bailout;
2779 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2780 ccb->cts.bus_width = bus_width >> 4;
2782 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID;
2784 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2786 if (cam_send_ccb(device, ccb) < 0) {
2787 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2788 if (arglist & CAM_ARG_VERBOSE) {
2789 cam_error_print(device, ccb, CAM_ESF_ALL,
2790 CAM_EPF_ALL, stderr);
2793 goto ratecontrol_bailout;
2796 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2797 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2798 if (arglist & CAM_ARG_VERBOSE) {
2799 cam_error_print(device, ccb, CAM_ESF_ALL,
2800 CAM_EPF_ALL, stderr);
2803 goto ratecontrol_bailout;
2808 retval = testunitready(device, retry_count, timeout,
2809 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2812 * If the TUR didn't succeed, just bail.
2816 fprintf(stderr, "Test Unit Ready failed\n");
2817 goto ratecontrol_bailout;
2821 * If the user wants things quiet, there's no sense in
2822 * getting the transfer settings, if we're not going
2826 goto ratecontrol_bailout;
2828 fprintf(stdout, "New Parameters:\n");
2829 retval = get_print_cts(device, user_settings, 0, NULL);
2832 ratecontrol_bailout:
2839 scsiformat(struct cam_device *device, int argc, char **argv,
2840 char *combinedopt, int retry_count, int timeout)
2844 int ycount = 0, quiet = 0;
2845 int error = 0, response = 0, retval = 0;
2846 int use_timeout = 10800 * 1000;
2848 struct format_defect_list_header fh;
2849 u_int8_t *data_ptr = NULL;
2850 u_int32_t dxfer_len = 0;
2852 int num_warnings = 0;
2855 ccb = cam_getccb(device);
2858 warnx("scsiformat: error allocating ccb");
2862 bzero(&(&ccb->ccb_h)[1],
2863 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2865 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2886 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2887 "following device:\n");
2889 error = scsidoinquiry(device, argc, argv, combinedopt,
2890 retry_count, timeout);
2893 warnx("scsiformat: error sending inquiry");
2894 goto scsiformat_bailout;
2903 fprintf(stdout, "Are you SURE you want to do "
2906 if (fgets(str, sizeof(str), stdin) != NULL) {
2908 if (strncasecmp(str, "yes", 3) == 0)
2910 else if (strncasecmp(str, "no", 2) == 0)
2913 fprintf(stdout, "Please answer"
2914 " \"yes\" or \"no\"\n");
2917 } while (response == 0);
2919 if (response == -1) {
2921 goto scsiformat_bailout;
2926 use_timeout = timeout;
2929 fprintf(stdout, "Current format timeout is %d seconds\n",
2930 use_timeout / 1000);
2934 * If the user hasn't disabled questions and didn't specify a
2935 * timeout on the command line, ask them if they want the current
2939 && (timeout == 0)) {
2941 int new_timeout = 0;
2943 fprintf(stdout, "Enter new timeout in seconds or press\n"
2944 "return to keep the current timeout [%d] ",
2945 use_timeout / 1000);
2947 if (fgets(str, sizeof(str), stdin) != NULL) {
2949 new_timeout = atoi(str);
2952 if (new_timeout != 0) {
2953 use_timeout = new_timeout * 1000;
2954 fprintf(stdout, "Using new timeout value %d\n",
2955 use_timeout / 1000);
2960 * Keep this outside the if block below to silence any unused
2961 * variable warnings.
2963 bzero(&fh, sizeof(fh));
2966 * If we're in immediate mode, we've got to include the format
2969 if (immediate != 0) {
2970 fh.byte2 = FU_DLH_IMMED;
2971 data_ptr = (u_int8_t *)&fh;
2972 dxfer_len = sizeof(fh);
2973 byte2 = FU_FMT_DATA;
2974 } else if (quiet == 0) {
2975 fprintf(stdout, "Formatting...");
2979 scsi_format_unit(&ccb->csio,
2980 /* retries */ retry_count,
2982 /* tag_action */ MSG_SIMPLE_Q_TAG,
2985 /* data_ptr */ data_ptr,
2986 /* dxfer_len */ dxfer_len,
2987 /* sense_len */ SSD_FULL_SIZE,
2988 /* timeout */ use_timeout);
2990 /* Disable freezing the device queue */
2991 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
2993 if (arglist & CAM_ARG_ERR_RECOVER)
2994 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
2996 if (((retval = cam_send_ccb(device, ccb)) < 0)
2997 || ((immediate == 0)
2998 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
2999 const char errstr[] = "error sending format command";
3006 if (arglist & CAM_ARG_VERBOSE) {
3007 cam_error_print(device, ccb, CAM_ESF_ALL,
3008 CAM_EPF_ALL, stderr);
3011 goto scsiformat_bailout;
3015 * If we ran in non-immediate mode, we already checked for errors
3016 * above and printed out any necessary information. If we're in
3017 * immediate mode, we need to loop through and get status
3018 * information periodically.
3020 if (immediate == 0) {
3022 fprintf(stdout, "Format Complete\n");
3024 goto scsiformat_bailout;
3031 bzero(&(&ccb->ccb_h)[1],
3032 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3035 * There's really no need to do error recovery or
3036 * retries here, since we're just going to sit in a
3037 * loop and wait for the device to finish formatting.
3039 scsi_test_unit_ready(&ccb->csio,
3042 /* tag_action */ MSG_SIMPLE_Q_TAG,
3043 /* sense_len */ SSD_FULL_SIZE,
3044 /* timeout */ 5000);
3046 /* Disable freezing the device queue */
3047 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3049 retval = cam_send_ccb(device, ccb);
3052 * If we get an error from the ioctl, bail out. SCSI
3053 * errors are expected.
3056 warn("error sending CAMIOCOMMAND ioctl");
3057 if (arglist & CAM_ARG_VERBOSE) {
3058 cam_error_print(device, ccb, CAM_ESF_ALL,
3059 CAM_EPF_ALL, stderr);
3062 goto scsiformat_bailout;
3065 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3067 if ((status != CAM_REQ_CMP)
3068 && (status == CAM_SCSI_STATUS_ERROR)
3069 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3070 struct scsi_sense_data *sense;
3071 int error_code, sense_key, asc, ascq;
3073 sense = &ccb->csio.sense_data;
3074 scsi_extract_sense(sense, &error_code, &sense_key,
3078 * According to the SCSI-2 and SCSI-3 specs, a
3079 * drive that is in the middle of a format should
3080 * return NOT READY with an ASC of "logical unit
3081 * not ready, format in progress". The sense key
3082 * specific bytes will then be a progress indicator.
3084 if ((sense_key == SSD_KEY_NOT_READY)
3085 && (asc == 0x04) && (ascq == 0x04)) {
3086 if ((sense->extra_len >= 10)
3087 && ((sense->sense_key_spec[0] &
3088 SSD_SCS_VALID) != 0)
3091 u_int64_t percentage;
3094 &sense->sense_key_spec[1]);
3095 percentage = 10000 * val;
3098 "\rFormatting: %qd.%02qd %% "
3100 percentage / (0x10000 * 100),
3101 (percentage / 0x10000) % 100,
3104 } else if ((quiet == 0)
3105 && (++num_warnings <= 1)) {
3106 warnx("Unexpected SCSI Sense Key "
3107 "Specific value returned "
3109 scsi_sense_print(device, &ccb->csio,
3111 warnx("Unable to print status "
3112 "information, but format will "
3114 warnx("will exit when format is "
3119 warnx("Unexpected SCSI error during format");
3120 cam_error_print(device, ccb, CAM_ESF_ALL,
3121 CAM_EPF_ALL, stderr);
3123 goto scsiformat_bailout;
3126 } else if (status != CAM_REQ_CMP) {
3127 warnx("Unexpected CAM status %#x", status);
3128 if (arglist & CAM_ARG_VERBOSE)
3129 cam_error_print(device, ccb, CAM_ESF_ALL,
3130 CAM_EPF_ALL, stderr);
3132 goto scsiformat_bailout;
3135 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3138 fprintf(stdout, "\nFormat Complete\n");
3146 #endif /* MINIMALISTIC */
3151 fprintf(verbose ? stdout : stderr,
3152 "usage: camcontrol <command> [device id][generic args][command args]\n"
3153 " camcontrol devlist [-v]\n"
3154 #ifndef MINIMALISTIC
3155 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3156 " camcontrol tur [dev_id][generic args]\n"
3157 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3158 " camcontrol start [dev_id][generic args]\n"
3159 " camcontrol stop [dev_id][generic args]\n"
3160 " camcontrol load [dev_id][generic args]\n"
3161 " camcontrol eject [dev_id][generic args]\n"
3162 #endif /* MINIMALISTIC */
3163 " camcontrol rescan <all | bus[:target:lun]>\n"
3164 " camcontrol reset <all | bus[:target:lun]>\n"
3165 #ifndef MINIMALISTIC
3166 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3167 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3168 " [-P pagectl][-e | -b][-d]\n"
3169 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3170 " [-i len fmt|-o len fmt [args]]\n"
3171 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3172 " <all|bus[:target[:lun]]|off>\n"
3173 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3174 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3175 " [-D <enable|disable>][-O offset][-q]\n"
3176 " [-R syncrate][-v][-T <enable|disable>]\n"
3177 " [-U][-W bus_width]\n"
3178 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
3179 #endif /* MINIMALISTIC */
3180 " camcontrol help\n");
3183 #ifndef MINIMALISTIC
3185 "Specify one of the following options:\n"
3186 "devlist list all CAM devices\n"
3187 "periphlist list all CAM peripheral drivers attached to a device\n"
3188 "tur send a test unit ready to the named device\n"
3189 "inquiry send a SCSI inquiry command to the named device\n"
3190 "start send a Start Unit command to the device\n"
3191 "stop send a Stop Unit command to the device\n"
3192 "load send a Start Unit command to the device with the load bit set\n"
3193 "eject send a Stop Unit command to the device with the eject bit set\n"
3194 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3195 "reset reset all busses, the given bus, or bus:target:lun\n"
3196 "defects read the defect list of the specified device\n"
3197 "modepage display or edit (-e) the given mode page\n"
3198 "cmd send the given scsi command, may need -i or -o as well\n"
3199 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3200 "tags report or set the number of transaction slots for a device\n"
3201 "negotiate report or set device negotiation parameters\n"
3202 "format send the SCSI FORMAT UNIT command to the named device\n"
3203 "help this message\n"
3204 "Device Identifiers:\n"
3205 "bus:target specify the bus and target, lun defaults to 0\n"
3206 "bus:target:lun specify the bus, target and lun\n"
3207 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3208 "Generic arguments:\n"
3209 "-v be verbose, print out sense information\n"
3210 "-t timeout command timeout in seconds, overrides default timeout\n"
3211 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3212 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3213 "-E have the kernel attempt to perform SCSI error recovery\n"
3214 "-C count specify the SCSI command retry count (needs -E to work)\n"
3215 "modepage arguments:\n"
3216 "-l list all available mode pages\n"
3217 "-m page specify the mode page to view or edit\n"
3218 "-e edit the specified mode page\n"
3219 "-b force view to binary mode\n"
3220 "-d disable block descriptors for mode sense\n"
3221 "-P pgctl page control field 0-3\n"
3222 "defects arguments:\n"
3223 "-f format specify defect list format (block, bfi or phys)\n"
3224 "-G get the grown defect list\n"
3225 "-P get the permanant defect list\n"
3226 "inquiry arguments:\n"
3227 "-D get the standard inquiry data\n"
3228 "-S get the serial number\n"
3229 "-R get the transfer rate, etc.\n"
3231 "-c cdb [args] specify the SCSI CDB\n"
3232 "-i len fmt specify input data and input data format\n"
3233 "-o len fmt [args] specify output data and output data fmt\n"
3234 "debug arguments:\n"
3235 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3236 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3237 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3238 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3240 "-N tags specify the number of tags to use for this device\n"
3241 "-q be quiet, don't report the number of tags\n"
3242 "-v report a number of tag-related parameters\n"
3243 "negotiate arguments:\n"
3244 "-a send a test unit ready after negotiation\n"
3245 "-c report/set current negotiation settings\n"
3246 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3247 "-O offset set command delay offset\n"
3248 "-q be quiet, don't report anything\n"
3249 "-R syncrate synchronization rate in MHz\n"
3250 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3251 "-U report/set user negotiation settings\n"
3252 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3253 "-v also print a Path Inquiry CCB for the controller\n"
3254 "format arguments:\n"
3255 "-q be quiet, don't print status messages\n"
3256 "-r run in report only mode\n"
3257 "-w don't send immediate format command\n"
3258 "-y don't ask any questions\n");
3259 #endif /* MINIMALISTIC */
3263 main(int argc, char **argv)
3266 char *device = NULL;
3268 struct cam_device *cam_dev = NULL;
3269 int timeout = 0, retry_count = 1;
3270 camcontrol_optret optreturn;
3272 const char *mainopt = "C:En:t:u:v";
3273 const char *subopt = NULL;
3274 char combinedopt[256];
3275 int error = 0, optstart = 2;
3278 cmdlist = CAM_CMD_NONE;
3279 arglist = CAM_ARG_NONE;
3287 * Get the base option.
3289 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3291 if (optreturn == CC_OR_AMBIGUOUS) {
3292 warnx("ambiguous option %s", argv[1]);
3295 } else if (optreturn == CC_OR_NOT_FOUND) {
3296 warnx("option %s not found", argv[1]);
3302 * Ahh, getopt(3) is a pain.
3304 * This is a gross hack. There really aren't many other good
3305 * options (excuse the pun) for parsing options in a situation like
3306 * this. getopt is kinda braindead, so you end up having to run
3307 * through the options twice, and give each invocation of getopt
3308 * the option string for the other invocation.
3310 * You would think that you could just have two groups of options.
3311 * The first group would get parsed by the first invocation of
3312 * getopt, and the second group would get parsed by the second
3313 * invocation of getopt. It doesn't quite work out that way. When
3314 * the first invocation of getopt finishes, it leaves optind pointing
3315 * to the argument _after_ the first argument in the second group.
3316 * So when the second invocation of getopt comes around, it doesn't
3317 * recognize the first argument it gets and then bails out.
3319 * A nice alternative would be to have a flag for getopt that says
3320 * "just keep parsing arguments even when you encounter an unknown
3321 * argument", but there isn't one. So there's no real clean way to
3322 * easily parse two sets of arguments without having one invocation
3323 * of getopt know about the other.
3325 * Without this hack, the first invocation of getopt would work as
3326 * long as the generic arguments are first, but the second invocation
3327 * (in the subfunction) would fail in one of two ways. In the case
3328 * where you don't set optreset, it would fail because optind may be
3329 * pointing to the argument after the one it should be pointing at.
3330 * In the case where you do set optreset, and reset optind, it would
3331 * fail because getopt would run into the first set of options, which
3332 * it doesn't understand.
3334 * All of this would "sort of" work if you could somehow figure out
3335 * whether optind had been incremented one option too far. The
3336 * mechanics of that, however, are more daunting than just giving
3337 * both invocations all of the expect options for either invocation.
3339 * Needless to say, I wouldn't mind if someone invented a better
3340 * (non-GPL!) command line parsing interface than getopt. I
3341 * wouldn't mind if someone added more knobs to getopt to make it
3342 * work better. Who knows, I may talk myself into doing it someday,
3343 * if the standards weenies let me. As it is, it just leads to
3344 * hackery like this and causes people to avoid it in some cases.
3346 * KDM, September 8th, 1998
3349 sprintf(combinedopt, "%s%s", mainopt, subopt);
3351 sprintf(combinedopt, "%s", mainopt);
3354 * For these options we do not parse optional device arguments and
3355 * we do not open a passthrough device.
3357 if ((cmdlist == CAM_CMD_RESCAN)
3358 || (cmdlist == CAM_CMD_RESET)
3359 || (cmdlist == CAM_CMD_DEVTREE)
3360 || (cmdlist == CAM_CMD_USAGE)
3361 || (cmdlist == CAM_CMD_DEBUG))
3364 #ifndef MINIMALISTIC
3366 && (argc > 2 && argv[2][0] != '-')) {
3371 * First catch people who try to do things like:
3372 * camcontrol tur /dev/da0
3373 * camcontrol doesn't take device nodes as arguments.
3375 if (argv[2][0] == '/') {
3376 warnx("%s is not a valid device identifier", argv[2]);
3377 errx(1, "please read the camcontrol(8) man page");
3378 } else if (isdigit(argv[2][0])) {
3379 /* device specified as bus:target[:lun] */
3380 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3382 errx(1, "numeric device specification must "
3383 "be either bus:target, or "
3385 /* default to 0 if lun was not specified */
3386 if ((arglist & CAM_ARG_LUN) == 0) {
3388 arglist |= CAM_ARG_LUN;
3392 if (cam_get_device(argv[2], name, sizeof name, &unit)
3394 errx(1, "%s", cam_errbuf);
3395 device = strdup(name);
3396 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3400 #endif /* MINIMALISTIC */
3402 * Start getopt processing at argv[2/3], since we've already
3403 * accepted argv[1..2] as the command name, and as a possible
3409 * Now we run through the argument list looking for generic
3410 * options, and ignoring options that possibly belong to
3413 while ((c = getopt(argc, argv, combinedopt))!= -1){
3416 retry_count = strtol(optarg, NULL, 0);
3417 if (retry_count < 0)
3418 errx(1, "retry count %d is < 0",
3420 arglist |= CAM_ARG_RETRIES;
3423 arglist |= CAM_ARG_ERR_RECOVER;
3426 arglist |= CAM_ARG_DEVICE;
3428 while (isspace(*tstr) && (*tstr != '\0'))
3430 device = (char *)strdup(tstr);
3433 timeout = strtol(optarg, NULL, 0);
3435 errx(1, "invalid timeout %d", timeout);
3436 /* Convert the timeout from seconds to ms */
3438 arglist |= CAM_ARG_TIMEOUT;
3441 arglist |= CAM_ARG_UNIT;
3442 unit = strtol(optarg, NULL, 0);
3445 arglist |= CAM_ARG_VERBOSE;
3452 #ifndef MINIMALISTIC
3454 * For most commands we'll want to open the passthrough device
3455 * associated with the specified device. In the case of the rescan
3456 * commands, we don't use a passthrough device at all, just the
3457 * transport layer device.
3460 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3461 && (((arglist & CAM_ARG_DEVICE) == 0)
3462 || ((arglist & CAM_ARG_UNIT) == 0))) {
3463 errx(1, "subcommand \"%s\" requires a valid device "
3464 "identifier", argv[1]);
3467 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3468 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3469 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3471 errx(1,"%s", cam_errbuf);
3473 #endif /* MINIMALISTIC */
3476 * Reset optind to 2, and reset getopt, so these routines can parse
3477 * the arguments again.
3483 #ifndef MINIMALISTIC
3484 case CAM_CMD_DEVLIST:
3485 error = getdevlist(cam_dev);
3487 #endif /* MINIMALISTIC */
3488 case CAM_CMD_DEVTREE:
3489 error = getdevtree();
3491 #ifndef MINIMALISTIC
3493 error = testunitready(cam_dev, retry_count, timeout, 0);
3495 case CAM_CMD_INQUIRY:
3496 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3497 retry_count, timeout);
3499 case CAM_CMD_STARTSTOP:
3500 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3501 arglist & CAM_ARG_EJECT, retry_count,
3504 #endif /* MINIMALISTIC */
3505 case CAM_CMD_RESCAN:
3506 error = dorescan_or_reset(argc, argv, 1);
3509 error = dorescan_or_reset(argc, argv, 0);
3511 #ifndef MINIMALISTIC
3512 case CAM_CMD_READ_DEFECTS:
3513 error = readdefects(cam_dev, argc, argv, combinedopt,
3514 retry_count, timeout);
3516 case CAM_CMD_MODE_PAGE:
3517 modepage(cam_dev, argc, argv, combinedopt,
3518 retry_count, timeout);
3520 case CAM_CMD_SCSI_CMD:
3521 error = scsicmd(cam_dev, argc, argv, combinedopt,
3522 retry_count, timeout);
3525 error = camdebug(argc, argv, combinedopt);
3528 error = tagcontrol(cam_dev, argc, argv, combinedopt);
3531 error = ratecontrol(cam_dev, retry_count, timeout,
3532 argc, argv, combinedopt);
3534 case CAM_CMD_FORMAT:
3535 error = scsiformat(cam_dev, argc, argv,
3536 combinedopt, retry_count, timeout);
3538 #endif /* MINIMALISTIC */
3548 if (cam_dev != NULL)
3549 cam_close_device(cam_dev);