2 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 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.5 2007/11/18 17:53:01 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, "qwy"},
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.ccb_h)[1],
300 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
302 ccb.ccb_h.func_code = XPT_DEV_MATCH;
303 bufsize = sizeof(struct dev_match_result) * 100;
304 ccb.cdm.match_buf_len = bufsize;
305 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
306 if (ccb.cdm.matches == NULL) {
307 warnx("can't malloc memory for matches");
311 ccb.cdm.num_matches = 0;
314 * We fetch all nodes, since we display most of them in the default
315 * case, and all in the verbose case.
317 ccb.cdm.num_patterns = 0;
318 ccb.cdm.pattern_buf_len = 0;
321 * We do the ioctl multiple times if necessary, in case there are
322 * more than 100 nodes in the EDT.
325 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
326 warn("error sending CAMIOCOMMAND ioctl");
331 if ((ccb.ccb_h.status != CAM_REQ_CMP)
332 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
333 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
334 warnx("got CAM error %#x, CDM error %d\n",
335 ccb.ccb_h.status, ccb.cdm.status);
340 for (i = 0; i < ccb.cdm.num_matches; i++) {
341 switch (ccb.cdm.matches[i].type) {
342 case DEV_MATCH_BUS: {
343 struct bus_match_result *bus_result;
346 * Only print the bus information if the
347 * user turns on the verbose flag.
349 if ((arglist & CAM_ARG_VERBOSE) == 0)
353 &ccb.cdm.matches[i].result.bus_result;
356 fprintf(stdout, ")\n");
360 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
362 bus_result->dev_name,
363 bus_result->unit_number,
367 case DEV_MATCH_DEVICE: {
368 struct device_match_result *dev_result;
369 char vendor[16], product[48], revision[16];
373 &ccb.cdm.matches[i].result.device_result;
375 if ((dev_result->flags
376 & DEV_RESULT_UNCONFIGURED)
377 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
383 cam_strvis(vendor, dev_result->inq_data.vendor,
384 sizeof(dev_result->inq_data.vendor),
387 dev_result->inq_data.product,
388 sizeof(dev_result->inq_data.product),
391 dev_result->inq_data.revision,
392 sizeof(dev_result->inq_data.revision),
394 sprintf(tmpstr, "<%s %s %s>", vendor, product,
397 fprintf(stdout, ")\n");
401 fprintf(stdout, "%-33s at scbus%d "
402 "target %d lun %d (",
405 dev_result->target_id,
406 dev_result->target_lun);
412 case DEV_MATCH_PERIPH: {
413 struct periph_match_result *periph_result;
416 &ccb.cdm.matches[i].result.periph_result;
418 if (skip_device != 0)
422 fprintf(stdout, ",");
424 fprintf(stdout, "%s%d",
425 periph_result->periph_name,
426 periph_result->unit_number);
432 fprintf(stdout, "unknown match type\n");
437 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
438 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
441 fprintf(stdout, ")\n");
450 testunitready(struct cam_device *device, int retry_count, int timeout,
456 ccb = cam_getccb(device);
458 scsi_test_unit_ready(&ccb->csio,
459 /* retries */ retry_count,
461 /* tag_action */ MSG_SIMPLE_Q_TAG,
462 /* sense_len */ SSD_FULL_SIZE,
463 /* timeout */ timeout ? timeout : 5000);
465 /* Disable freezing the device queue */
466 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
468 if (arglist & CAM_ARG_ERR_RECOVER)
469 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
471 if (cam_send_ccb(device, ccb) < 0) {
473 perror("error sending test unit ready");
475 if (arglist & CAM_ARG_VERBOSE) {
476 cam_error_print(device, ccb, CAM_ESF_ALL,
477 CAM_EPF_ALL, stderr);
484 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
486 fprintf(stdout, "Unit is ready\n");
489 fprintf(stdout, "Unit is not ready\n");
492 if (arglist & CAM_ARG_VERBOSE) {
493 cam_error_print(device, ccb, CAM_ESF_ALL,
494 CAM_EPF_ALL, stderr);
504 scsistart(struct cam_device *device, int startstop, int loadeject,
505 int retry_count, int timeout)
510 ccb = cam_getccb(device);
513 * If we're stopping, send an ordered tag so the drive in question
514 * will finish any previously queued writes before stopping. If
515 * the device isn't capable of tagged queueing, or if tagged
516 * queueing is turned off, the tag action is a no-op.
518 scsi_start_stop(&ccb->csio,
519 /* retries */ retry_count,
521 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
523 /* start/stop */ startstop,
524 /* load_eject */ loadeject,
526 /* sense_len */ SSD_FULL_SIZE,
527 /* timeout */ timeout ? timeout : 120000);
529 /* Disable freezing the device queue */
530 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
532 if (arglist & CAM_ARG_ERR_RECOVER)
533 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
535 if (cam_send_ccb(device, ccb) < 0) {
536 perror("error sending start unit");
538 if (arglist & CAM_ARG_VERBOSE) {
539 cam_error_print(device, ccb, CAM_ESF_ALL,
540 CAM_EPF_ALL, stderr);
547 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
549 fprintf(stdout, "Unit started successfully");
551 fprintf(stdout,", Media loaded\n");
553 fprintf(stdout,"\n");
555 fprintf(stdout, "Unit stopped successfully");
557 fprintf(stdout, ", Media ejected\n");
559 fprintf(stdout, "\n");
565 "Error received from start unit command\n");
568 "Error received from stop unit command\n");
570 if (arglist & CAM_ARG_VERBOSE) {
571 cam_error_print(device, ccb, CAM_ESF_ALL,
572 CAM_EPF_ALL, stderr);
582 scsidoinquiry(struct cam_device *device, int argc, char **argv,
583 char *combinedopt, int retry_count, int timeout)
588 while ((c = getopt(argc, argv, combinedopt)) != -1) {
591 arglist |= CAM_ARG_GET_STDINQ;
594 arglist |= CAM_ARG_GET_XFERRATE;
597 arglist |= CAM_ARG_GET_SERIAL;
605 * If the user didn't specify any inquiry options, he wants all of
608 if ((arglist & CAM_ARG_INQ_MASK) == 0)
609 arglist |= CAM_ARG_INQ_MASK;
611 if (arglist & CAM_ARG_GET_STDINQ)
612 error = scsiinquiry(device, retry_count, timeout);
617 if (arglist & CAM_ARG_GET_SERIAL)
618 scsiserial(device, retry_count, timeout);
623 if (arglist & CAM_ARG_GET_XFERRATE)
624 error = scsixferrate(device);
630 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
633 struct scsi_inquiry_data *inq_buf;
636 ccb = cam_getccb(device);
639 warnx("couldn't allocate CCB");
643 /* cam_getccb cleans up the header, caller has to zero the payload */
644 bzero(&(&ccb->ccb_h)[1],
645 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
647 inq_buf = (struct scsi_inquiry_data *)malloc(
648 sizeof(struct scsi_inquiry_data));
650 if (inq_buf == NULL) {
652 warnx("can't malloc memory for inquiry\n");
655 bzero(inq_buf, sizeof(*inq_buf));
658 * Note that although the size of the inquiry buffer is the full
659 * 256 bytes specified in the SCSI spec, we only tell the device
660 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
661 * two reasons for this:
663 * - The SCSI spec says that when a length field is only 1 byte,
664 * a value of 0 will be interpreted as 256. Therefore
665 * scsi_inquiry() will convert an inq_len (which is passed in as
666 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
667 * to 0. Evidently, very few devices meet the spec in that
668 * regard. Some devices, like many Seagate disks, take the 0 as
669 * 0, and don't return any data. One Pioneer DVD-R drive
670 * returns more data than the command asked for.
672 * So, since there are numerous devices that just don't work
673 * right with the full inquiry size, we don't send the full size.
675 * - The second reason not to use the full inquiry data length is
676 * that we don't need it here. The only reason we issue a
677 * standard inquiry is to get the vendor name, device name,
678 * and revision so scsi_print_inquiry() can print them.
680 * If, at some point in the future, more inquiry data is needed for
681 * some reason, this code should use a procedure similar to the
682 * probe code. i.e., issue a short inquiry, and determine from
683 * the additional length passed back from the device how much
684 * inquiry data the device supports. Once the amount the device
685 * supports is determined, issue an inquiry for that amount and no
690 scsi_inquiry(&ccb->csio,
691 /* retries */ retry_count,
693 /* tag_action */ MSG_SIMPLE_Q_TAG,
694 /* inq_buf */ (u_int8_t *)inq_buf,
695 /* inq_len */ SHORT_INQUIRY_LENGTH,
698 /* sense_len */ SSD_FULL_SIZE,
699 /* timeout */ timeout ? timeout : 5000);
701 /* Disable freezing the device queue */
702 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
704 if (arglist & CAM_ARG_ERR_RECOVER)
705 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
707 if (cam_send_ccb(device, ccb) < 0) {
708 perror("error sending SCSI inquiry");
710 if (arglist & CAM_ARG_VERBOSE) {
711 cam_error_print(device, ccb, CAM_ESF_ALL,
712 CAM_EPF_ALL, stderr);
719 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
722 if (arglist & CAM_ARG_VERBOSE) {
723 cam_error_print(device, ccb, CAM_ESF_ALL,
724 CAM_EPF_ALL, stderr);
735 fprintf(stdout, "%s%d: ", device->device_name,
736 device->dev_unit_num);
737 scsi_print_inquiry(inq_buf);
745 scsiserial(struct cam_device *device, int retry_count, int timeout)
748 struct scsi_vpd_unit_serial_number *serial_buf;
749 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
752 ccb = cam_getccb(device);
755 warnx("couldn't allocate CCB");
759 /* cam_getccb cleans up the header, caller has to zero the payload */
760 bzero(&(&ccb->ccb_h)[1],
761 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
763 serial_buf = (struct scsi_vpd_unit_serial_number *)
764 malloc(sizeof(*serial_buf));
766 if (serial_buf == NULL) {
768 warnx("can't malloc memory for serial number");
772 scsi_inquiry(&ccb->csio,
773 /*retries*/ retry_count,
775 /* tag_action */ MSG_SIMPLE_Q_TAG,
776 /* inq_buf */ (u_int8_t *)serial_buf,
777 /* inq_len */ sizeof(*serial_buf),
779 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
780 /* sense_len */ SSD_FULL_SIZE,
781 /* timeout */ timeout ? timeout : 5000);
783 /* Disable freezing the device queue */
784 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
786 if (arglist & CAM_ARG_ERR_RECOVER)
787 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
789 if (cam_send_ccb(device, ccb) < 0) {
790 warn("error getting serial number");
792 if (arglist & CAM_ARG_VERBOSE) {
793 cam_error_print(device, ccb, CAM_ESF_ALL,
794 CAM_EPF_ALL, stderr);
802 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
805 if (arglist & CAM_ARG_VERBOSE) {
806 cam_error_print(device, ccb, CAM_ESF_ALL,
807 CAM_EPF_ALL, stderr);
818 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
819 serial_num[serial_buf->length] = '\0';
821 if ((arglist & CAM_ARG_GET_STDINQ)
822 || (arglist & CAM_ARG_GET_XFERRATE))
823 fprintf(stdout, "%s%d: Serial Number ",
824 device->device_name, device->dev_unit_num);
826 fprintf(stdout, "%.60s\n", serial_num);
834 scsixferrate(struct cam_device *device)
842 ccb = cam_getccb(device);
845 warnx("couldn't allocate CCB");
849 bzero(&(&ccb->ccb_h)[1],
850 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
852 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
853 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
855 if (((retval = cam_send_ccb(device, ccb)) < 0)
856 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
857 const char error_string[] = "error getting transfer settings";
864 if (arglist & CAM_ARG_VERBOSE)
865 cam_error_print(device, ccb, CAM_ESF_ALL,
866 CAM_EPF_ALL, stderr);
870 goto xferrate_bailout;
874 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
875 && (ccb->cts.sync_offset != 0)) {
876 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
879 struct ccb_pathinq cpi;
881 retval = get_cpi(device, &cpi);
884 goto xferrate_bailout;
886 speed = cpi.base_transfer_speed;
890 fprintf(stdout, "%s%d: ", device->device_name,
891 device->dev_unit_num);
893 if ((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
894 speed *= (0x01 << device->bus_width);
899 fprintf(stdout, "%d.%03dMB/s transfers ",
902 fprintf(stdout, "%dKB/s transfers ",
905 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
906 && (ccb->cts.sync_offset != 0))
907 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
908 freq % 1000, ccb->cts.sync_offset);
910 if (((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
911 && (ccb->cts.bus_width > 0)) {
912 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
913 && (ccb->cts.sync_offset != 0)) {
914 fprintf(stdout, ", ");
916 fprintf(stdout, " (");
918 fprintf(stdout, "%dbit)", 8 * (0x01 << ccb->cts.bus_width));
919 } else if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
920 && (ccb->cts.sync_offset != 0)) {
921 fprintf(stdout, ")");
924 if (((ccb->cts.valid & CCB_TRANS_TQ_VALID) != 0)
925 && (ccb->cts.flags & CCB_TRANS_TAG_ENB))
926 fprintf(stdout, ", Tagged Queueing Enabled");
928 fprintf(stdout, "\n");
936 #endif /* MINIMALISTIC */
939 * Parse out a bus, or a bus, target and lun in the following
945 * Returns the number of parsed components, or 0.
948 parse_btl(char *tstr, int *mybus, int *mytarget, int *mylun,
949 cam_argmask *myarglist)
954 while (isspace(*tstr) && (*tstr != '\0'))
957 tmpstr = (char *)strtok(tstr, ":");
958 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
959 *mybus = strtol(tmpstr, NULL, 0);
960 *myarglist |= CAM_ARG_BUS;
962 tmpstr = (char *)strtok(NULL, ":");
963 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
964 *mytarget = strtol(tmpstr, NULL, 0);
965 *myarglist |= CAM_ARG_TARGET;
967 tmpstr = (char *)strtok(NULL, ":");
968 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
969 *mylun = strtol(tmpstr, NULL, 0);
970 *myarglist |= CAM_ARG_LUN;
980 dorescan_or_reset(int argc, char **argv, int rescan)
982 static const char must[] =
983 "you must specify \"all\", a bus, or a bus:target:lun to %s";
985 int mybus = -1, mytarget = -1, mylun = -1;
989 warnx(must, rescan? "rescan" : "reset");
994 while (isspace(*tstr) && (*tstr != '\0'))
996 if (strncasecmp(tstr, "all", strlen("all")) == 0)
997 arglist |= CAM_ARG_BUS;
999 rv = parse_btl(argv[optind], &mybus, &mytarget, &mylun,
1001 if (rv != 1 && rv != 3) {
1002 warnx(must, rescan? "rescan" : "reset");
1007 if ((arglist & CAM_ARG_BUS)
1008 && (arglist & CAM_ARG_TARGET)
1009 && (arglist & CAM_ARG_LUN))
1010 error = scanlun_or_reset_dev(mybus, mytarget, mylun, rescan);
1012 error = rescan_or_reset_bus(mybus, rescan);
1018 rescan_or_reset_bus(int mybus, int rescan)
1020 union ccb ccb, matchccb;
1026 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1027 warnx("error opening transport layer device %s", XPT_DEVICE);
1028 warn("%s", XPT_DEVICE);
1033 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1034 ccb.ccb_h.path_id = mybus;
1035 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1036 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1037 ccb.crcn.flags = CAM_FLAG_NONE;
1039 /* run this at a low priority */
1040 ccb.ccb_h.pinfo.priority = 5;
1042 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1043 warn("CAMIOCOMMAND ioctl failed");
1048 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1049 fprintf(stdout, "%s of bus %d was successful\n",
1050 rescan ? "Re-scan" : "Reset", mybus);
1052 fprintf(stdout, "%s of bus %d returned error %#x\n",
1053 rescan ? "Re-scan" : "Reset", mybus,
1054 ccb.ccb_h.status & CAM_STATUS_MASK);
1065 * The right way to handle this is to modify the xpt so that it can
1066 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1067 * that isn't implemented, so instead we enumerate the busses and
1068 * send the rescan or reset to those busses in the case where the
1069 * given bus is -1 (wildcard). We don't send a rescan or reset
1070 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1071 * no-op, sending a rescan to the xpt bus would result in a status of
1074 bzero(&(&matchccb.ccb_h)[1],
1075 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1076 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1077 bufsize = sizeof(struct dev_match_result) * 20;
1078 matchccb.cdm.match_buf_len = bufsize;
1079 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1080 if (matchccb.cdm.matches == NULL) {
1081 warnx("can't malloc memory for matches");
1085 matchccb.cdm.num_matches = 0;
1087 matchccb.cdm.num_patterns = 1;
1088 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1090 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1091 matchccb.cdm.pattern_buf_len);
1092 if (matchccb.cdm.patterns == NULL) {
1093 warnx("can't malloc memory for patterns");
1097 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1098 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1103 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1104 warn("CAMIOCOMMAND ioctl failed");
1109 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1110 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1111 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1112 warnx("got CAM error %#x, CDM error %d\n",
1113 matchccb.ccb_h.status, matchccb.cdm.status);
1118 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1119 struct bus_match_result *bus_result;
1121 /* This shouldn't happen. */
1122 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1125 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1128 * We don't want to rescan or reset the xpt bus.
1131 if ((int)bus_result->path_id == -1)
1134 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1136 ccb.ccb_h.path_id = bus_result->path_id;
1137 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1138 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1139 ccb.crcn.flags = CAM_FLAG_NONE;
1141 /* run this at a low priority */
1142 ccb.ccb_h.pinfo.priority = 5;
1144 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1145 warn("CAMIOCOMMAND ioctl failed");
1150 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1151 fprintf(stdout, "%s of bus %d was successful\n",
1152 rescan? "Re-scan" : "Reset",
1153 bus_result->path_id);
1156 * Don't bail out just yet, maybe the other
1157 * rescan or reset commands will complete
1160 fprintf(stderr, "%s of bus %d returned error "
1161 "%#x\n", rescan? "Re-scan" : "Reset",
1162 bus_result->path_id,
1163 ccb.ccb_h.status & CAM_STATUS_MASK);
1167 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1168 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1175 if (matchccb.cdm.patterns != NULL)
1176 free(matchccb.cdm.patterns);
1177 if (matchccb.cdm.matches != NULL)
1178 free(matchccb.cdm.matches);
1184 scanlun_or_reset_dev(int mybus, int mytarget, int mylun, int scan)
1187 struct cam_device *device;
1193 warnx("invalid bus number %d", mybus);
1198 warnx("invalid target number %d", mytarget);
1203 warnx("invalid lun number %d", mylun);
1209 bzero(&ccb, sizeof(union ccb));
1212 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1213 warnx("error opening transport layer device %s\n",
1215 warn("%s", XPT_DEVICE);
1219 device = cam_open_btl(mybus, mytarget, mylun, O_RDWR, NULL);
1220 if (device == NULL) {
1221 warnx("%s", cam_errbuf);
1226 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1227 ccb.ccb_h.path_id = mybus;
1228 ccb.ccb_h.target_id = mytarget;
1229 ccb.ccb_h.target_lun = mylun;
1230 ccb.ccb_h.timeout = 5000;
1231 ccb.crcn.flags = CAM_FLAG_NONE;
1233 /* run this at a low priority */
1234 ccb.ccb_h.pinfo.priority = 5;
1237 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1238 warn("CAMIOCOMMAND ioctl failed");
1243 if (cam_send_ccb(device, &ccb) < 0) {
1244 warn("error sending XPT_RESET_DEV CCB");
1245 cam_close_device(device);
1253 cam_close_device(device);
1256 * An error code of CAM_BDR_SENT is normal for a BDR request.
1258 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1260 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1261 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1262 scan? "Re-scan" : "Reset", mybus, mytarget, mylun);
1265 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1266 scan? "Re-scan" : "Reset", mybus, mytarget, mylun,
1267 ccb.ccb_h.status & CAM_STATUS_MASK);
1272 #ifndef MINIMALISTIC
1274 readdefects(struct cam_device *device, int argc, char **argv,
1275 char *combinedopt, int retry_count, int timeout)
1277 union ccb *ccb = NULL;
1278 struct scsi_read_defect_data_10 *rdd_cdb;
1279 u_int8_t *defect_list = NULL;
1280 u_int32_t dlist_length = 65000;
1281 u_int32_t returned_length = 0;
1282 u_int32_t num_returned = 0;
1283 u_int8_t returned_format;
1286 int lists_specified = 0;
1288 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1294 while (isspace(*tstr) && (*tstr != '\0'))
1296 if (strcmp(tstr, "block") == 0)
1297 arglist |= CAM_ARG_FORMAT_BLOCK;
1298 else if (strcmp(tstr, "bfi") == 0)
1299 arglist |= CAM_ARG_FORMAT_BFI;
1300 else if (strcmp(tstr, "phys") == 0)
1301 arglist |= CAM_ARG_FORMAT_PHYS;
1304 warnx("invalid defect format %s", tstr);
1305 goto defect_bailout;
1310 arglist |= CAM_ARG_GLIST;
1313 arglist |= CAM_ARG_PLIST;
1320 ccb = cam_getccb(device);
1323 * Hopefully 65000 bytes is enough to hold the defect list. If it
1324 * isn't, the disk is probably dead already. We'd have to go with
1325 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1328 defect_list = malloc(dlist_length);
1329 if (defect_list == NULL) {
1330 warnx("can't malloc memory for defect list");
1332 goto defect_bailout;
1335 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1338 * cam_getccb() zeros the CCB header only. So we need to zero the
1339 * payload portion of the ccb.
1341 bzero(&(&ccb->ccb_h)[1],
1342 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1344 cam_fill_csio(&ccb->csio,
1345 /*retries*/ retry_count,
1347 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1348 CAM_PASS_ERR_RECOVER : 0),
1349 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1350 /*data_ptr*/ defect_list,
1351 /*dxfer_len*/ dlist_length,
1352 /*sense_len*/ SSD_FULL_SIZE,
1353 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1354 /*timeout*/ timeout ? timeout : 5000);
1356 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1357 if (arglist & CAM_ARG_FORMAT_BLOCK)
1358 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1359 else if (arglist & CAM_ARG_FORMAT_BFI)
1360 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1361 else if (arglist & CAM_ARG_FORMAT_PHYS)
1362 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1365 warnx("no defect list format specified");
1366 goto defect_bailout;
1368 if (arglist & CAM_ARG_PLIST) {
1369 rdd_cdb->format |= SRDD10_PLIST;
1373 if (arglist & CAM_ARG_GLIST) {
1374 rdd_cdb->format |= SRDD10_GLIST;
1378 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1380 /* Disable freezing the device queue */
1381 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1383 if (cam_send_ccb(device, ccb) < 0) {
1384 perror("error reading defect list");
1386 if (arglist & CAM_ARG_VERBOSE) {
1387 cam_error_print(device, ccb, CAM_ESF_ALL,
1388 CAM_EPF_ALL, stderr);
1392 goto defect_bailout;
1395 returned_length = scsi_2btoul(((struct
1396 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1398 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1399 defect_list)->format;
1401 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1402 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1403 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1404 struct scsi_sense_data *sense;
1405 int error_code, sense_key, asc, ascq;
1407 sense = &ccb->csio.sense_data;
1408 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1411 * According to the SCSI spec, if the disk doesn't support
1412 * the requested format, it will generally return a sense
1413 * key of RECOVERED ERROR, and an additional sense code
1414 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1415 * also check to make sure that the returned length is
1416 * greater than 0, and then print out whatever format the
1419 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1420 && (asc == 0x1c) && (ascq == 0x00)
1421 && (returned_length > 0)) {
1422 warnx("requested defect format not available");
1423 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1424 case SRDD10_BLOCK_FORMAT:
1425 warnx("Device returned block format");
1427 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1428 warnx("Device returned bytes from index"
1431 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1432 warnx("Device returned physical sector format");
1436 warnx("Device returned unknown defect"
1437 " data format %#x", returned_format);
1438 goto defect_bailout;
1439 break; /* NOTREACHED */
1443 warnx("Error returned from read defect data command");
1444 if (arglist & CAM_ARG_VERBOSE)
1445 cam_error_print(device, ccb, CAM_ESF_ALL,
1446 CAM_EPF_ALL, stderr);
1447 goto defect_bailout;
1451 warnx("Error returned from read defect data command");
1452 if (arglist & CAM_ARG_VERBOSE)
1453 cam_error_print(device, ccb, CAM_ESF_ALL,
1454 CAM_EPF_ALL, stderr);
1455 goto defect_bailout;
1459 * XXX KDM I should probably clean up the printout format for the
1462 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1463 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1465 struct scsi_defect_desc_phys_sector *dlist;
1467 dlist = (struct scsi_defect_desc_phys_sector *)
1469 sizeof(struct scsi_read_defect_data_hdr_10));
1471 num_returned = returned_length /
1472 sizeof(struct scsi_defect_desc_phys_sector);
1474 fprintf(stderr, "Got %d defect", num_returned);
1476 if ((lists_specified == 0) || (num_returned == 0)) {
1477 fprintf(stderr, "s.\n");
1479 } else if (num_returned == 1)
1480 fprintf(stderr, ":\n");
1482 fprintf(stderr, "s:\n");
1484 for (i = 0; i < num_returned; i++) {
1485 fprintf(stdout, "%d:%d:%d\n",
1486 scsi_3btoul(dlist[i].cylinder),
1488 scsi_4btoul(dlist[i].sector));
1492 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1494 struct scsi_defect_desc_bytes_from_index *dlist;
1496 dlist = (struct scsi_defect_desc_bytes_from_index *)
1498 sizeof(struct scsi_read_defect_data_hdr_10));
1500 num_returned = returned_length /
1501 sizeof(struct scsi_defect_desc_bytes_from_index);
1503 fprintf(stderr, "Got %d defect", num_returned);
1505 if ((lists_specified == 0) || (num_returned == 0)) {
1506 fprintf(stderr, "s.\n");
1508 } else if (num_returned == 1)
1509 fprintf(stderr, ":\n");
1511 fprintf(stderr, "s:\n");
1513 for (i = 0; i < num_returned; i++) {
1514 fprintf(stdout, "%d:%d:%d\n",
1515 scsi_3btoul(dlist[i].cylinder),
1517 scsi_4btoul(dlist[i].bytes_from_index));
1521 case SRDDH10_BLOCK_FORMAT:
1523 struct scsi_defect_desc_block *dlist;
1525 dlist = (struct scsi_defect_desc_block *)(defect_list +
1526 sizeof(struct scsi_read_defect_data_hdr_10));
1528 num_returned = returned_length /
1529 sizeof(struct scsi_defect_desc_block);
1531 fprintf(stderr, "Got %d defect", num_returned);
1533 if ((lists_specified == 0) || (num_returned == 0)) {
1534 fprintf(stderr, "s.\n");
1536 } else if (num_returned == 1)
1537 fprintf(stderr, ":\n");
1539 fprintf(stderr, "s:\n");
1541 for (i = 0; i < num_returned; i++)
1542 fprintf(stdout, "%u\n",
1543 scsi_4btoul(dlist[i].address));
1547 fprintf(stderr, "Unknown defect format %d\n",
1548 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1554 if (defect_list != NULL)
1562 #endif /* MINIMALISTIC */
1566 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1570 ccb = cam_getccb(device);
1576 #ifndef MINIMALISTIC
1578 mode_sense(struct cam_device *device, int mode_page, int page_control,
1579 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1584 ccb = cam_getccb(device);
1587 errx(1, "mode_sense: couldn't allocate CCB");
1589 bzero(&(&ccb->ccb_h)[1],
1590 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1592 scsi_mode_sense(&ccb->csio,
1593 /* retries */ retry_count,
1595 /* tag_action */ MSG_SIMPLE_Q_TAG,
1597 /* page_code */ page_control << 6,
1598 /* page */ mode_page,
1599 /* param_buf */ data,
1600 /* param_len */ datalen,
1601 /* sense_len */ SSD_FULL_SIZE,
1602 /* timeout */ timeout ? timeout : 5000);
1604 if (arglist & CAM_ARG_ERR_RECOVER)
1605 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1607 /* Disable freezing the device queue */
1608 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1610 if (((retval = cam_send_ccb(device, ccb)) < 0)
1611 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1612 if (arglist & CAM_ARG_VERBOSE) {
1613 cam_error_print(device, ccb, CAM_ESF_ALL,
1614 CAM_EPF_ALL, stderr);
1617 cam_close_device(device);
1619 err(1, "error sending mode sense command");
1621 errx(1, "error sending mode sense command");
1628 mode_select(struct cam_device *device, int save_pages, int retry_count,
1629 int timeout, u_int8_t *data, int datalen)
1634 ccb = cam_getccb(device);
1637 errx(1, "mode_select: couldn't allocate CCB");
1639 bzero(&(&ccb->ccb_h)[1],
1640 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1642 scsi_mode_select(&ccb->csio,
1643 /* retries */ retry_count,
1645 /* tag_action */ MSG_SIMPLE_Q_TAG,
1646 /* scsi_page_fmt */ 1,
1647 /* save_pages */ save_pages,
1648 /* param_buf */ data,
1649 /* param_len */ datalen,
1650 /* sense_len */ SSD_FULL_SIZE,
1651 /* timeout */ timeout ? timeout : 5000);
1653 if (arglist & CAM_ARG_ERR_RECOVER)
1654 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1656 /* Disable freezing the device queue */
1657 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1659 if (((retval = cam_send_ccb(device, ccb)) < 0)
1660 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1661 if (arglist & CAM_ARG_VERBOSE) {
1662 cam_error_print(device, ccb, CAM_ESF_ALL,
1663 CAM_EPF_ALL, stderr);
1666 cam_close_device(device);
1669 err(1, "error sending mode select command");
1671 errx(1, "error sending mode select command");
1679 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1680 int retry_count, int timeout)
1682 int c, mode_page = -1, page_control = 0;
1683 int binary = 0, list = 0;
1685 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1691 arglist |= CAM_ARG_DBD;
1694 arglist |= CAM_ARG_MODE_EDIT;
1700 mode_page = strtol(optarg, NULL, 0);
1702 errx(1, "invalid mode page %d", mode_page);
1705 page_control = strtol(optarg, NULL, 0);
1706 if ((page_control < 0) || (page_control > 3))
1707 errx(1, "invalid page control field %d",
1709 arglist |= CAM_ARG_PAGE_CNTL;
1716 if (mode_page == -1 && list == 0)
1717 errx(1, "you must specify a mode page!");
1720 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1721 retry_count, timeout);
1723 mode_edit(device, mode_page, page_control,
1724 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1725 retry_count, timeout);
1730 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1731 int retry_count, int timeout)
1734 u_int32_t flags = CAM_DIR_NONE;
1735 u_int8_t *data_ptr = NULL;
1737 struct get_hook hook;
1738 int c, data_bytes = 0;
1740 char *datastr = NULL, *tstr;
1745 ccb = cam_getccb(device);
1748 warnx("scsicmd: error allocating ccb");
1752 bzero(&(&ccb->ccb_h)[1],
1753 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1755 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1759 while (isspace(*tstr) && (*tstr != '\0'))
1761 hook.argc = argc - optind;
1762 hook.argv = argv + optind;
1764 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1767 * Increment optind by the number of arguments the
1768 * encoding routine processed. After each call to
1769 * getopt(3), optind points to the argument that
1770 * getopt should process _next_. In this case,
1771 * that means it points to the first command string
1772 * argument, if there is one. Once we increment
1773 * this, it should point to either the next command
1774 * line argument, or it should be past the end of
1780 if (arglist & CAM_ARG_CMD_OUT) {
1781 warnx("command must either be "
1782 "read or write, not both");
1784 goto scsicmd_bailout;
1786 arglist |= CAM_ARG_CMD_IN;
1788 data_bytes = strtol(optarg, NULL, 0);
1789 if (data_bytes <= 0) {
1790 warnx("invalid number of input bytes %d",
1793 goto scsicmd_bailout;
1795 hook.argc = argc - optind;
1796 hook.argv = argv + optind;
1799 datastr = cget(&hook, NULL);
1801 * If the user supplied "-" instead of a format, he
1802 * wants the data to be written to stdout.
1804 if ((datastr != NULL)
1805 && (datastr[0] == '-'))
1808 data_ptr = (u_int8_t *)malloc(data_bytes);
1809 if (data_ptr == NULL) {
1810 warnx("can't malloc memory for data_ptr");
1812 goto scsicmd_bailout;
1816 if (arglist & CAM_ARG_CMD_IN) {
1817 warnx("command must either be "
1818 "read or write, not both");
1820 goto scsicmd_bailout;
1822 arglist |= CAM_ARG_CMD_OUT;
1823 flags = CAM_DIR_OUT;
1824 data_bytes = strtol(optarg, NULL, 0);
1825 if (data_bytes <= 0) {
1826 warnx("invalid number of output bytes %d",
1829 goto scsicmd_bailout;
1831 hook.argc = argc - optind;
1832 hook.argv = argv + optind;
1834 datastr = cget(&hook, NULL);
1835 data_ptr = (u_int8_t *)malloc(data_bytes);
1836 if (data_ptr == NULL) {
1837 warnx("can't malloc memory for data_ptr");
1839 goto scsicmd_bailout;
1842 * If the user supplied "-" instead of a format, he
1843 * wants the data to be read from stdin.
1845 if ((datastr != NULL)
1846 && (datastr[0] == '-'))
1849 buff_encode_visit(data_ptr, data_bytes, datastr,
1859 * If fd_data is set, and we're writing to the device, we need to
1860 * read the data the user wants written from stdin.
1862 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1864 int amt_to_read = data_bytes;
1865 u_int8_t *buf_ptr = data_ptr;
1867 for (amt_read = 0; amt_to_read > 0;
1868 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1869 if (amt_read == -1) {
1870 warn("error reading data from stdin");
1872 goto scsicmd_bailout;
1874 amt_to_read -= amt_read;
1875 buf_ptr += amt_read;
1879 if (arglist & CAM_ARG_ERR_RECOVER)
1880 flags |= CAM_PASS_ERR_RECOVER;
1882 /* Disable freezing the device queue */
1883 flags |= CAM_DEV_QFRZDIS;
1886 * This is taken from the SCSI-3 draft spec.
1887 * (T10/1157D revision 0.3)
1888 * The top 3 bits of an opcode are the group code. The next 5 bits
1889 * are the command code.
1890 * Group 0: six byte commands
1891 * Group 1: ten byte commands
1892 * Group 2: ten byte commands
1894 * Group 4: sixteen byte commands
1895 * Group 5: twelve byte commands
1896 * Group 6: vendor specific
1897 * Group 7: vendor specific
1899 switch((cdb[0] >> 5) & 0x7) {
1910 /* computed by buff_encode_visit */
1921 * We should probably use csio_build_visit or something like that
1922 * here, but it's easier to encode arguments as you go. The
1923 * alternative would be skipping the CDB argument and then encoding
1924 * it here, since we've got the data buffer argument by now.
1926 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1928 cam_fill_csio(&ccb->csio,
1929 /*retries*/ retry_count,
1932 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1933 /*data_ptr*/ data_ptr,
1934 /*dxfer_len*/ data_bytes,
1935 /*sense_len*/ SSD_FULL_SIZE,
1936 /*cdb_len*/ cdb_len,
1937 /*timeout*/ timeout ? timeout : 5000);
1939 if (((retval = cam_send_ccb(device, ccb)) < 0)
1940 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1942 warn("error sending command");
1944 warnx("error sending command");
1946 if (arglist & CAM_ARG_VERBOSE) {
1947 cam_error_print(device, ccb, CAM_ESF_ALL,
1948 CAM_EPF_ALL, stderr);
1952 goto scsicmd_bailout;
1956 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1957 && (arglist & CAM_ARG_CMD_IN)
1958 && (data_bytes > 0)) {
1960 buff_decode_visit(data_ptr, data_bytes, datastr,
1962 fprintf(stdout, "\n");
1964 ssize_t amt_written;
1965 int amt_to_write = data_bytes;
1966 u_int8_t *buf_ptr = data_ptr;
1968 for (amt_written = 0; (amt_to_write > 0) &&
1969 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
1970 amt_to_write -= amt_written;
1971 buf_ptr += amt_written;
1973 if (amt_written == -1) {
1974 warn("error writing data to stdout");
1976 goto scsicmd_bailout;
1977 } else if ((amt_written == 0)
1978 && (amt_to_write > 0)) {
1979 warnx("only wrote %u bytes out of %u",
1980 data_bytes - amt_to_write, data_bytes);
1987 if ((data_bytes > 0) && (data_ptr != NULL))
1996 camdebug(int argc, char **argv, char *combinedopt)
1999 int mybus = -1, mytarget = -1, mylun = -1;
2000 char *tstr, *tmpstr = NULL;
2004 bzero(&ccb, sizeof(union ccb));
2006 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2009 arglist |= CAM_ARG_DEBUG_INFO;
2010 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2013 arglist |= CAM_ARG_DEBUG_PERIPH;
2014 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2017 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2018 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2021 arglist |= CAM_ARG_DEBUG_TRACE;
2022 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2025 arglist |= CAM_ARG_DEBUG_XPT;
2026 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2029 arglist |= CAM_ARG_DEBUG_CDB;
2030 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2037 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2038 warnx("error opening transport layer device %s", XPT_DEVICE);
2039 warn("%s", XPT_DEVICE);
2046 warnx("you must specify \"off\", \"all\" or a bus,");
2047 warnx("bus:target, or bus:target:lun");
2054 while (isspace(*tstr) && (*tstr != '\0'))
2057 if (strncmp(tstr, "off", 3) == 0) {
2058 ccb.cdbg.flags = CAM_DEBUG_NONE;
2059 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2060 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2062 } else if (strncmp(tstr, "all", 3) != 0) {
2063 tmpstr = (char *)strtok(tstr, ":");
2064 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2065 mybus = strtol(tmpstr, NULL, 0);
2066 arglist |= CAM_ARG_BUS;
2067 tmpstr = (char *)strtok(NULL, ":");
2068 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2069 mytarget = strtol(tmpstr, NULL, 0);
2070 arglist |= CAM_ARG_TARGET;
2071 tmpstr = (char *)strtok(NULL, ":");
2072 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2073 mylun = strtol(tmpstr, NULL, 0);
2074 arglist |= CAM_ARG_LUN;
2079 warnx("you must specify \"all\", \"off\", or a bus,");
2080 warnx("bus:target, or bus:target:lun to debug");
2086 ccb.ccb_h.func_code = XPT_DEBUG;
2087 ccb.ccb_h.path_id = mybus;
2088 ccb.ccb_h.target_id = mytarget;
2089 ccb.ccb_h.target_lun = mylun;
2091 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2092 warn("CAMIOCOMMAND ioctl failed");
2097 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2098 CAM_FUNC_NOTAVAIL) {
2099 warnx("CAM debugging not available");
2100 warnx("you need to put options CAMDEBUG in"
2101 " your kernel config file!");
2103 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2105 warnx("XPT_DEBUG CCB failed with status %#x",
2109 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2111 "Debugging turned off\n");
2114 "Debugging enabled for "
2116 mybus, mytarget, mylun);
2127 tagcontrol(struct cam_device *device, int argc, char **argv,
2137 ccb = cam_getccb(device);
2140 warnx("tagcontrol: error allocating ccb");
2144 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2147 numtags = strtol(optarg, NULL, 0);
2149 warnx("tag count %d is < 0", numtags);
2151 goto tagcontrol_bailout;
2162 cam_path_string(device, pathstr, sizeof(pathstr));
2165 bzero(&(&ccb->ccb_h)[1],
2166 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2167 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2168 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2169 ccb->crs.openings = numtags;
2172 if (cam_send_ccb(device, ccb) < 0) {
2173 perror("error sending XPT_REL_SIMQ CCB");
2175 goto tagcontrol_bailout;
2178 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2179 warnx("XPT_REL_SIMQ CCB failed");
2180 cam_error_print(device, ccb, CAM_ESF_ALL,
2181 CAM_EPF_ALL, stderr);
2183 goto tagcontrol_bailout;
2188 fprintf(stdout, "%stagged openings now %d\n",
2189 pathstr, ccb->crs.openings);
2192 bzero(&(&ccb->ccb_h)[1],
2193 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2195 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2197 if (cam_send_ccb(device, ccb) < 0) {
2198 perror("error sending XPT_GDEV_STATS CCB");
2200 goto tagcontrol_bailout;
2203 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2204 warnx("XPT_GDEV_STATS CCB failed");
2205 cam_error_print(device, ccb, CAM_ESF_ALL,
2206 CAM_EPF_ALL, stderr);
2208 goto tagcontrol_bailout;
2211 if (arglist & CAM_ARG_VERBOSE) {
2212 fprintf(stdout, "%s", pathstr);
2213 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2214 fprintf(stdout, "%s", pathstr);
2215 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2216 fprintf(stdout, "%s", pathstr);
2217 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2218 fprintf(stdout, "%s", pathstr);
2219 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2220 fprintf(stdout, "%s", pathstr);
2221 fprintf(stdout, "held %d\n", ccb->cgds.held);
2222 fprintf(stdout, "%s", pathstr);
2223 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2224 fprintf(stdout, "%s", pathstr);
2225 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2228 fprintf(stdout, "%s", pathstr);
2229 fprintf(stdout, "device openings: ");
2231 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2232 ccb->cgds.dev_active);
2242 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2246 cam_path_string(device, pathstr, sizeof(pathstr));
2248 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
2250 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2253 if (cts->sync_offset != 0) {
2256 freq = scsi_calc_syncsrate(cts->sync_period);
2257 fprintf(stdout, "%sfrequency: %d.%03dMHz\n", pathstr,
2258 freq / 1000, freq % 1000);
2262 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID)
2263 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset);
2265 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID)
2266 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2267 (0x01 << cts->bus_width) * 8);
2269 if (cts->valid & CCB_TRANS_DISC_VALID)
2270 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2271 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" :
2274 if (cts->valid & CCB_TRANS_TQ_VALID)
2275 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2276 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" :
2282 * Get a path inquiry CCB for the specified device.
2285 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2290 ccb = cam_getccb(device);
2293 warnx("get_cpi: couldn't allocate CCB");
2297 bzero(&(&ccb->ccb_h)[1],
2298 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2300 ccb->ccb_h.func_code = XPT_PATH_INQ;
2302 if (cam_send_ccb(device, ccb) < 0) {
2303 warn("get_cpi: error sending Path Inquiry CCB");
2305 if (arglist & CAM_ARG_VERBOSE)
2306 cam_error_print(device, ccb, CAM_ESF_ALL,
2307 CAM_EPF_ALL, stderr);
2311 goto get_cpi_bailout;
2314 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2316 if (arglist & CAM_ARG_VERBOSE)
2317 cam_error_print(device, ccb, CAM_ESF_ALL,
2318 CAM_EPF_ALL, stderr);
2322 goto get_cpi_bailout;
2325 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2335 cpi_print(struct ccb_pathinq *cpi)
2337 char adapter_str[1024];
2340 snprintf(adapter_str, sizeof(adapter_str),
2341 "%s%d:", cpi->dev_name, cpi->unit_number);
2343 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2346 for (i = 1; i < 0xff; i = i << 1) {
2349 if ((i & cpi->hba_inquiry) == 0)
2352 fprintf(stdout, "%s supports ", adapter_str);
2356 str = "MDP message";
2359 str = "32 bit wide SCSI";
2362 str = "16 bit wide SCSI";
2365 str = "SDTR message";
2368 str = "linked CDBs";
2371 str = "tag queue messages";
2374 str = "soft reset alternative";
2377 str = "unknown PI bit set";
2380 fprintf(stdout, "%s\n", str);
2383 for (i = 1; i < 0xff; i = i << 1) {
2386 if ((i & cpi->hba_misc) == 0)
2389 fprintf(stdout, "%s ", adapter_str);
2393 str = "bus scans from high ID to low ID";
2396 str = "removable devices not included in scan";
2398 case PIM_NOINITIATOR:
2399 str = "initiator role not supported";
2401 case PIM_NOBUSRESET:
2402 str = "user has disabled initial BUS RESET or"
2403 " controller is in target/mixed mode";
2406 str = "unknown PIM bit set";
2409 fprintf(stdout, "%s\n", str);
2412 for (i = 1; i < 0xff; i = i << 1) {
2415 if ((i & cpi->target_sprt) == 0)
2418 fprintf(stdout, "%s supports ", adapter_str);
2421 str = "target mode processor mode";
2424 str = "target mode phase cog. mode";
2426 case PIT_DISCONNECT:
2427 str = "disconnects in target mode";
2430 str = "terminate I/O message in target mode";
2433 str = "group 6 commands in target mode";
2436 str = "group 7 commands in target mode";
2439 str = "unknown PIT bit set";
2443 fprintf(stdout, "%s\n", str);
2445 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2447 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2449 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2451 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2452 adapter_str, cpi->hpath_id);
2453 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2455 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2456 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2457 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2458 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2459 if (cpi->base_transfer_speed > 1000)
2460 fprintf(stdout, "%d.%03dMB/sec\n",
2461 cpi->base_transfer_speed / 1000,
2462 cpi->base_transfer_speed % 1000);
2464 fprintf(stdout, "%dKB/sec\n",
2465 (cpi->base_transfer_speed % 1000) * 1000);
2469 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2470 struct ccb_trans_settings *cts)
2476 ccb = cam_getccb(device);
2479 warnx("get_print_cts: error allocating ccb");
2483 bzero(&(&ccb->ccb_h)[1],
2484 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2486 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2488 if (user_settings == 0)
2489 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2491 ccb->cts.flags = CCB_TRANS_USER_SETTINGS;
2493 if (cam_send_ccb(device, ccb) < 0) {
2494 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2495 if (arglist & CAM_ARG_VERBOSE)
2496 cam_error_print(device, ccb, CAM_ESF_ALL,
2497 CAM_EPF_ALL, stderr);
2499 goto get_print_cts_bailout;
2502 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2503 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2504 if (arglist & CAM_ARG_VERBOSE)
2505 cam_error_print(device, ccb, CAM_ESF_ALL,
2506 CAM_EPF_ALL, stderr);
2508 goto get_print_cts_bailout;
2512 cts_print(device, &ccb->cts);
2515 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2517 get_print_cts_bailout:
2525 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2526 int argc, char **argv, char *combinedopt)
2530 int user_settings = 0;
2532 int disc_enable = -1, tag_enable = -1;
2534 double syncrate = -1;
2537 int change_settings = 0, send_tur = 0;
2538 struct ccb_pathinq cpi;
2540 ccb = cam_getccb(device);
2543 warnx("ratecontrol: error allocating ccb");
2547 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2556 if (strncasecmp(optarg, "enable", 6) == 0)
2558 else if (strncasecmp(optarg, "disable", 7) == 0)
2561 warnx("-D argument \"%s\" is unknown", optarg);
2563 goto ratecontrol_bailout;
2565 change_settings = 1;
2568 offset = strtol(optarg, NULL, 0);
2570 warnx("offset value %d is < 0", offset);
2572 goto ratecontrol_bailout;
2574 change_settings = 1;
2580 syncrate = atof(optarg);
2583 warnx("sync rate %f is < 0", syncrate);
2585 goto ratecontrol_bailout;
2587 change_settings = 1;
2590 if (strncasecmp(optarg, "enable", 6) == 0)
2592 else if (strncasecmp(optarg, "disable", 7) == 0)
2595 warnx("-T argument \"%s\" is unknown", optarg);
2597 goto ratecontrol_bailout;
2599 change_settings = 1;
2605 bus_width = strtol(optarg, NULL, 0);
2606 if (bus_width < 0) {
2607 warnx("bus width %d is < 0", bus_width);
2609 goto ratecontrol_bailout;
2611 change_settings = 1;
2618 bzero(&(&ccb->ccb_h)[1],
2619 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2622 * Grab path inquiry information, so we can determine whether
2623 * or not the initiator is capable of the things that the user
2626 ccb->ccb_h.func_code = XPT_PATH_INQ;
2628 if (cam_send_ccb(device, ccb) < 0) {
2629 perror("error sending XPT_PATH_INQ CCB");
2630 if (arglist & CAM_ARG_VERBOSE) {
2631 cam_error_print(device, ccb, CAM_ESF_ALL,
2632 CAM_EPF_ALL, stderr);
2635 goto ratecontrol_bailout;
2638 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2639 warnx("XPT_PATH_INQ CCB failed");
2640 if (arglist & CAM_ARG_VERBOSE) {
2641 cam_error_print(device, ccb, CAM_ESF_ALL,
2642 CAM_EPF_ALL, stderr);
2645 goto ratecontrol_bailout;
2648 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2650 bzero(&(&ccb->ccb_h)[1],
2651 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2654 fprintf(stdout, "Current Parameters:\n");
2656 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2659 goto ratecontrol_bailout;
2661 if (arglist & CAM_ARG_VERBOSE)
2664 if (change_settings) {
2665 if (disc_enable != -1) {
2666 ccb->cts.valid |= CCB_TRANS_DISC_VALID;
2667 if (disc_enable == 0)
2668 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB;
2670 ccb->cts.flags |= CCB_TRANS_DISC_ENB;
2672 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID;
2674 if (tag_enable != -1) {
2675 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2676 warnx("HBA does not support tagged queueing, "
2677 "so you cannot modify tag settings");
2679 goto ratecontrol_bailout;
2682 ccb->cts.valid |= CCB_TRANS_TQ_VALID;
2684 if (tag_enable == 0)
2685 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB;
2687 ccb->cts.flags |= CCB_TRANS_TAG_ENB;
2689 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID;
2692 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2693 warnx("HBA at %s%d is not cable of changing "
2694 "offset", cpi.dev_name,
2697 goto ratecontrol_bailout;
2699 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID;
2700 ccb->cts.sync_offset = offset;
2702 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID;
2704 if (syncrate != -1) {
2705 int prelim_sync_period;
2708 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2709 warnx("HBA at %s%d is not cable of changing "
2710 "transfer rates", cpi.dev_name,
2713 goto ratecontrol_bailout;
2716 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID;
2719 * The sync rate the user gives us is in MHz.
2720 * We need to translate it into KHz for this
2726 * Next, we calculate a "preliminary" sync period
2727 * in tenths of a nanosecond.
2730 prelim_sync_period = 0;
2732 prelim_sync_period = 10000000 / syncrate;
2734 ccb->cts.sync_period =
2735 scsi_calc_syncparam(prelim_sync_period);
2737 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
2739 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID;
2742 * The bus_width argument goes like this:
2746 * Therefore, if you shift the number of bits given on the
2747 * command line right by 4, you should get the correct
2750 if (bus_width != -1) {
2753 * We might as well validate things here with a
2754 * decipherable error message, rather than what
2755 * will probably be an indecipherable error message
2756 * by the time it gets back to us.
2758 if ((bus_width == 16)
2759 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2760 warnx("HBA does not support 16 bit bus width");
2762 goto ratecontrol_bailout;
2763 } else if ((bus_width == 32)
2764 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2765 warnx("HBA does not support 32 bit bus width");
2767 goto ratecontrol_bailout;
2768 } else if ((bus_width != 8)
2769 && (bus_width != 16)
2770 && (bus_width != 32)) {
2771 warnx("Invalid bus width %d", bus_width);
2773 goto ratecontrol_bailout;
2776 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2777 ccb->cts.bus_width = bus_width >> 4;
2779 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID;
2781 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2783 if (cam_send_ccb(device, ccb) < 0) {
2784 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2785 if (arglist & CAM_ARG_VERBOSE) {
2786 cam_error_print(device, ccb, CAM_ESF_ALL,
2787 CAM_EPF_ALL, stderr);
2790 goto ratecontrol_bailout;
2793 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2794 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2795 if (arglist & CAM_ARG_VERBOSE) {
2796 cam_error_print(device, ccb, CAM_ESF_ALL,
2797 CAM_EPF_ALL, stderr);
2800 goto ratecontrol_bailout;
2805 retval = testunitready(device, retry_count, timeout,
2806 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2809 * If the TUR didn't succeed, just bail.
2813 fprintf(stderr, "Test Unit Ready failed\n");
2814 goto ratecontrol_bailout;
2818 * If the user wants things quiet, there's no sense in
2819 * getting the transfer settings, if we're not going
2823 goto ratecontrol_bailout;
2825 fprintf(stdout, "New Parameters:\n");
2826 retval = get_print_cts(device, user_settings, 0, NULL);
2829 ratecontrol_bailout:
2836 scsiformat(struct cam_device *device, int argc, char **argv,
2837 char *combinedopt, int retry_count, int timeout)
2841 int ycount = 0, quiet = 0;
2842 int error = 0, response = 0, retval = 0;
2843 int use_timeout = 10800 * 1000;
2845 struct format_defect_list_header fh;
2846 u_int8_t *data_ptr = NULL;
2847 u_int32_t dxfer_len = 0;
2849 int num_warnings = 0;
2851 ccb = cam_getccb(device);
2854 warnx("scsiformat: error allocating ccb");
2858 bzero(&(&ccb->ccb_h)[1],
2859 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2861 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2876 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2877 "following device:\n");
2879 error = scsidoinquiry(device, argc, argv, combinedopt,
2880 retry_count, timeout);
2883 warnx("scsiformat: error sending inquiry");
2884 goto scsiformat_bailout;
2893 fprintf(stdout, "Are you SURE you want to do "
2896 if (fgets(str, sizeof(str), stdin) != NULL) {
2898 if (strncasecmp(str, "yes", 3) == 0)
2900 else if (strncasecmp(str, "no", 2) == 0)
2903 fprintf(stdout, "Please answer"
2904 " \"yes\" or \"no\"\n");
2907 } while (response == 0);
2909 if (response == -1) {
2911 goto scsiformat_bailout;
2916 use_timeout = timeout;
2919 fprintf(stdout, "Current format timeout is %d seconds\n",
2920 use_timeout / 1000);
2924 * If the user hasn't disabled questions and didn't specify a
2925 * timeout on the command line, ask them if they want the current
2929 && (timeout == 0)) {
2931 int new_timeout = 0;
2933 fprintf(stdout, "Enter new timeout in seconds or press\n"
2934 "return to keep the current timeout [%d] ",
2935 use_timeout / 1000);
2937 if (fgets(str, sizeof(str), stdin) != NULL) {
2939 new_timeout = atoi(str);
2942 if (new_timeout != 0) {
2943 use_timeout = new_timeout * 1000;
2944 fprintf(stdout, "Using new timeout value %d\n",
2945 use_timeout / 1000);
2950 * Keep this outside the if block below to silence any unused
2951 * variable warnings.
2953 bzero(&fh, sizeof(fh));
2956 * If we're in immediate mode, we've got to include the format
2959 if (immediate != 0) {
2960 fh.byte2 = FU_DLH_IMMED;
2961 data_ptr = (u_int8_t *)&fh;
2962 dxfer_len = sizeof(fh);
2963 byte2 = FU_FMT_DATA;
2964 } else if (quiet == 0) {
2965 fprintf(stdout, "Formatting...");
2969 scsi_format_unit(&ccb->csio,
2970 /* retries */ retry_count,
2972 /* tag_action */ MSG_SIMPLE_Q_TAG,
2975 /* data_ptr */ data_ptr,
2976 /* dxfer_len */ dxfer_len,
2977 /* sense_len */ SSD_FULL_SIZE,
2978 /* timeout */ use_timeout);
2980 /* Disable freezing the device queue */
2981 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
2983 if (arglist & CAM_ARG_ERR_RECOVER)
2984 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
2986 if (((retval = cam_send_ccb(device, ccb)) < 0)
2987 || ((immediate == 0)
2988 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
2989 const char errstr[] = "error sending format command";
2996 if (arglist & CAM_ARG_VERBOSE) {
2997 cam_error_print(device, ccb, CAM_ESF_ALL,
2998 CAM_EPF_ALL, stderr);
3001 goto scsiformat_bailout;
3005 * If we ran in non-immediate mode, we already checked for errors
3006 * above and printed out any necessary information. If we're in
3007 * immediate mode, we need to loop through and get status
3008 * information periodically.
3010 if (immediate == 0) {
3012 fprintf(stdout, "Format Complete\n");
3014 goto scsiformat_bailout;
3020 bzero(&(&ccb->ccb_h)[1],
3021 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3024 * There's really no need to do error recovery or
3025 * retries here, since we're just going to sit in a
3026 * loop and wait for the device to finish formatting.
3028 scsi_test_unit_ready(&ccb->csio,
3031 /* tag_action */ MSG_SIMPLE_Q_TAG,
3032 /* sense_len */ SSD_FULL_SIZE,
3033 /* timeout */ 5000);
3035 /* Disable freezing the device queue */
3036 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3038 retval = cam_send_ccb(device, ccb);
3041 * If we get an error from the ioctl, bail out. SCSI
3042 * errors are expected.
3045 warn("error sending CAMIOCOMMAND ioctl");
3046 if (arglist & CAM_ARG_VERBOSE) {
3047 cam_error_print(device, ccb, CAM_ESF_ALL,
3048 CAM_EPF_ALL, stderr);
3051 goto scsiformat_bailout;
3054 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3056 if ((status != CAM_REQ_CMP)
3057 && (status == CAM_SCSI_STATUS_ERROR)
3058 && ((status & CAM_AUTOSNS_VALID) != 0)) {
3059 struct scsi_sense_data *sense;
3060 int error_code, sense_key, asc, ascq;
3062 sense = &ccb->csio.sense_data;
3063 scsi_extract_sense(sense, &error_code, &sense_key,
3067 * According to the SCSI-2 and SCSI-3 specs, a
3068 * drive that is in the middle of a format should
3069 * return NOT READY with an ASC of "logical unit
3070 * not ready, format in progress". The sense key
3071 * specific bytes will then be a progress indicator.
3073 if ((sense_key == SSD_KEY_NOT_READY)
3074 && (asc == 0x04) && (ascq == 0x04)) {
3075 if ((sense->extra_len >= 10)
3076 && ((sense->sense_key_spec[0] &
3077 SSD_SCS_VALID) != 0)
3080 u_int64_t percentage;
3083 &sense->sense_key_spec[1]);
3084 percentage = 10000 * val;
3087 "\rFormatting: %qd.%02qd %% "
3089 percentage / (0x10000 * 100),
3090 (percentage / 0x10000) % 100,
3093 } else if ((quiet == 0)
3094 && (++num_warnings <= 1)) {
3095 warnx("Unexpected SCSI Sense Key "
3096 "Specific value returned "
3098 scsi_sense_print(device, &ccb->csio,
3100 warnx("Unable to print status "
3101 "information, but format will "
3103 warnx("will exit when format is "
3108 warnx("Unexpected SCSI error during format");
3109 cam_error_print(device, ccb, CAM_ESF_ALL,
3110 CAM_EPF_ALL, stderr);
3112 goto scsiformat_bailout;
3115 } else if (status != CAM_REQ_CMP) {
3116 warnx("Unexpected CAM status %#x", status);
3117 if (arglist & CAM_ARG_VERBOSE)
3118 cam_error_print(device, ccb, CAM_ESF_ALL,
3119 CAM_EPF_ALL, stderr);
3121 goto scsiformat_bailout;
3124 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3127 fprintf(stdout, "\nFormat Complete\n");
3135 #endif /* MINIMALISTIC */
3140 fprintf(verbose ? stdout : stderr,
3141 "usage: camcontrol <command> [device id][generic args][command args]\n"
3142 " camcontrol devlist [-v]\n"
3143 #ifndef MINIMALISTIC
3144 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3145 " camcontrol tur [dev_id][generic args]\n"
3146 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3147 " camcontrol start [dev_id][generic args]\n"
3148 " camcontrol stop [dev_id][generic args]\n"
3149 " camcontrol load [dev_id][generic args]\n"
3150 " camcontrol eject [dev_id][generic args]\n"
3151 #endif /* MINIMALISTIC */
3152 " camcontrol rescan <all | bus[:target:lun]>\n"
3153 " camcontrol reset <all | bus[:target:lun]>\n"
3154 #ifndef MINIMALISTIC
3155 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3156 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3157 " [-P pagectl][-e | -b][-d]\n"
3158 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3159 " [-i len fmt|-o len fmt [args]]\n"
3160 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3161 " <all|bus[:target[:lun]]|off>\n"
3162 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3163 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3164 " [-D <enable|disable>][-O offset][-q]\n"
3165 " [-R syncrate][-v][-T <enable|disable>]\n"
3166 " [-U][-W bus_width]\n"
3167 " camcontrol format [dev_id][generic args][-q][-w][-y]\n"
3168 #endif /* MINIMALISTIC */
3169 " camcontrol help\n");
3172 #ifndef MINIMALISTIC
3174 "Specify one of the following options:\n"
3175 "devlist list all CAM devices\n"
3176 "periphlist list all CAM peripheral drivers attached to a device\n"
3177 "tur send a test unit ready to the named device\n"
3178 "inquiry send a SCSI inquiry command to the named device\n"
3179 "start send a Start Unit command to the device\n"
3180 "stop send a Stop Unit command to the device\n"
3181 "load send a Start Unit command to the device with the load bit set\n"
3182 "eject send a Stop Unit command to the device with the eject bit set\n"
3183 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3184 "reset reset all busses, the given bus, or bus:target:lun\n"
3185 "defects read the defect list of the specified device\n"
3186 "modepage display or edit (-e) the given mode page\n"
3187 "cmd send the given scsi command, may need -i or -o as well\n"
3188 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3189 "tags report or set the number of transaction slots for a device\n"
3190 "negotiate report or set device negotiation parameters\n"
3191 "format send the SCSI FORMAT UNIT command to the named device\n"
3192 "help this message\n"
3193 "Device Identifiers:\n"
3194 "bus:target specify the bus and target, lun defaults to 0\n"
3195 "bus:target:lun specify the bus, target and lun\n"
3196 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3197 "Generic arguments:\n"
3198 "-v be verbose, print out sense information\n"
3199 "-t timeout command timeout in seconds, overrides default timeout\n"
3200 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3201 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3202 "-E have the kernel attempt to perform SCSI error recovery\n"
3203 "-C count specify the SCSI command retry count (needs -E to work)\n"
3204 "modepage arguments:\n"
3205 "-l list all available mode pages\n"
3206 "-m page specify the mode page to view or edit\n"
3207 "-e edit the specified mode page\n"
3208 "-b force view to binary mode\n"
3209 "-d disable block descriptors for mode sense\n"
3210 "-P pgctl page control field 0-3\n"
3211 "defects arguments:\n"
3212 "-f format specify defect list format (block, bfi or phys)\n"
3213 "-G get the grown defect list\n"
3214 "-P get the permanant defect list\n"
3215 "inquiry arguments:\n"
3216 "-D get the standard inquiry data\n"
3217 "-S get the serial number\n"
3218 "-R get the transfer rate, etc.\n"
3220 "-c cdb [args] specify the SCSI CDB\n"
3221 "-i len fmt specify input data and input data format\n"
3222 "-o len fmt [args] specify output data and output data fmt\n"
3223 "debug arguments:\n"
3224 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3225 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3226 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3227 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3229 "-N tags specify the number of tags to use for this device\n"
3230 "-q be quiet, don't report the number of tags\n"
3231 "-v report a number of tag-related parameters\n"
3232 "negotiate arguments:\n"
3233 "-a send a test unit ready after negotiation\n"
3234 "-c report/set current negotiation settings\n"
3235 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3236 "-O offset set command delay offset\n"
3237 "-q be quiet, don't report anything\n"
3238 "-R syncrate synchronization rate in MHz\n"
3239 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3240 "-U report/set user negotiation settings\n"
3241 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3242 "-v also print a Path Inquiry CCB for the controller\n"
3243 "format arguments:\n"
3244 "-q be quiet, don't print status messages\n"
3245 "-w don't send immediate format command\n"
3246 "-y don't ask any questions\n");
3247 #endif /* MINIMALISTIC */
3251 main(int argc, char **argv)
3254 char *device = NULL;
3256 struct cam_device *cam_dev = NULL;
3257 int timeout = 0, retry_count = 1;
3258 camcontrol_optret optreturn;
3260 const char *mainopt = "C:En:t:u:v";
3261 const char *subopt = NULL;
3262 char combinedopt[256];
3263 int error = 0, optstart = 2;
3266 cmdlist = CAM_CMD_NONE;
3267 arglist = CAM_ARG_NONE;
3275 * Get the base option.
3277 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3279 if (optreturn == CC_OR_AMBIGUOUS) {
3280 warnx("ambiguous option %s", argv[1]);
3283 } else if (optreturn == CC_OR_NOT_FOUND) {
3284 warnx("option %s not found", argv[1]);
3290 * Ahh, getopt(3) is a pain.
3292 * This is a gross hack. There really aren't many other good
3293 * options (excuse the pun) for parsing options in a situation like
3294 * this. getopt is kinda braindead, so you end up having to run
3295 * through the options twice, and give each invocation of getopt
3296 * the option string for the other invocation.
3298 * You would think that you could just have two groups of options.
3299 * The first group would get parsed by the first invocation of
3300 * getopt, and the second group would get parsed by the second
3301 * invocation of getopt. It doesn't quite work out that way. When
3302 * the first invocation of getopt finishes, it leaves optind pointing
3303 * to the argument _after_ the first argument in the second group.
3304 * So when the second invocation of getopt comes around, it doesn't
3305 * recognize the first argument it gets and then bails out.
3307 * A nice alternative would be to have a flag for getopt that says
3308 * "just keep parsing arguments even when you encounter an unknown
3309 * argument", but there isn't one. So there's no real clean way to
3310 * easily parse two sets of arguments without having one invocation
3311 * of getopt know about the other.
3313 * Without this hack, the first invocation of getopt would work as
3314 * long as the generic arguments are first, but the second invocation
3315 * (in the subfunction) would fail in one of two ways. In the case
3316 * where you don't set optreset, it would fail because optind may be
3317 * pointing to the argument after the one it should be pointing at.
3318 * In the case where you do set optreset, and reset optind, it would
3319 * fail because getopt would run into the first set of options, which
3320 * it doesn't understand.
3322 * All of this would "sort of" work if you could somehow figure out
3323 * whether optind had been incremented one option too far. The
3324 * mechanics of that, however, are more daunting than just giving
3325 * both invocations all of the expect options for either invocation.
3327 * Needless to say, I wouldn't mind if someone invented a better
3328 * (non-GPL!) command line parsing interface than getopt. I
3329 * wouldn't mind if someone added more knobs to getopt to make it
3330 * work better. Who knows, I may talk myself into doing it someday,
3331 * if the standards weenies let me. As it is, it just leads to
3332 * hackery like this and causes people to avoid it in some cases.
3334 * KDM, September 8th, 1998
3337 sprintf(combinedopt, "%s%s", mainopt, subopt);
3339 sprintf(combinedopt, "%s", mainopt);
3342 * For these options we do not parse optional device arguments and
3343 * we do not open a passthrough device.
3345 if ((cmdlist == CAM_CMD_RESCAN)
3346 || (cmdlist == CAM_CMD_RESET)
3347 || (cmdlist == CAM_CMD_DEVTREE)
3348 || (cmdlist == CAM_CMD_USAGE)
3349 || (cmdlist == CAM_CMD_DEBUG))
3352 #ifndef MINIMALISTIC
3354 && (argc > 2 && argv[2][0] != '-')) {
3359 * First catch people who try to do things like:
3360 * camcontrol tur /dev/da0
3361 * camcontrol doesn't take device nodes as arguments.
3363 if (argv[2][0] == '/') {
3364 warnx("%s is not a valid device identifier", argv[2]);
3365 errx(1, "please read the camcontrol(8) man page");
3366 } else if (isdigit(argv[2][0])) {
3367 /* device specified as bus:target[:lun] */
3368 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3370 errx(1, "numeric device specification must "
3371 "be either bus:target, or "
3375 if (cam_get_device(argv[2], name, sizeof name, &unit)
3377 errx(1, "%s", cam_errbuf);
3378 device = strdup(name);
3379 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3383 #endif /* MINIMALISTIC */
3385 * Start getopt processing at argv[2/3], since we've already
3386 * accepted argv[1..2] as the command name, and as a possible
3392 * Now we run through the argument list looking for generic
3393 * options, and ignoring options that possibly belong to
3396 while ((c = getopt(argc, argv, combinedopt))!= -1){
3399 retry_count = strtol(optarg, NULL, 0);
3400 if (retry_count < 0)
3401 errx(1, "retry count %d is < 0",
3403 arglist |= CAM_ARG_RETRIES;
3406 arglist |= CAM_ARG_ERR_RECOVER;
3409 arglist |= CAM_ARG_DEVICE;
3411 while (isspace(*tstr) && (*tstr != '\0'))
3413 device = (char *)strdup(tstr);
3416 timeout = strtol(optarg, NULL, 0);
3418 errx(1, "invalid timeout %d", timeout);
3419 /* Convert the timeout from seconds to ms */
3421 arglist |= CAM_ARG_TIMEOUT;
3424 arglist |= CAM_ARG_UNIT;
3425 unit = strtol(optarg, NULL, 0);
3428 arglist |= CAM_ARG_VERBOSE;
3435 #ifndef MINIMALISTIC
3437 * For most commands we'll want to open the passthrough device
3438 * associated with the specified device. In the case of the rescan
3439 * commands, we don't use a passthrough device at all, just the
3440 * transport layer device.
3443 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3444 && (((arglist & CAM_ARG_DEVICE) == 0)
3445 || ((arglist & CAM_ARG_UNIT) == 0))) {
3446 errx(1, "subcommand \"%s\" requires a valid device "
3447 "identifier", argv[1]);
3450 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3451 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3452 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3454 errx(1,"%s", cam_errbuf);
3456 #endif /* MINIMALISTIC */
3459 * Reset optind to 2, and reset getopt, so these routines can parse
3460 * the arguments again.
3466 #ifndef MINIMALISTIC
3467 case CAM_CMD_DEVLIST:
3468 error = getdevlist(cam_dev);
3470 #endif /* MINIMALISTIC */
3471 case CAM_CMD_DEVTREE:
3472 error = getdevtree();
3474 #ifndef MINIMALISTIC
3476 error = testunitready(cam_dev, retry_count, timeout, 0);
3478 case CAM_CMD_INQUIRY:
3479 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3480 retry_count, timeout);
3482 case CAM_CMD_STARTSTOP:
3483 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3484 arglist & CAM_ARG_EJECT, retry_count,
3487 #endif /* MINIMALISTIC */
3488 case CAM_CMD_RESCAN:
3489 error = dorescan_or_reset(argc, argv, 1);
3492 error = dorescan_or_reset(argc, argv, 0);
3494 #ifndef MINIMALISTIC
3495 case CAM_CMD_READ_DEFECTS:
3496 error = readdefects(cam_dev, argc, argv, combinedopt,
3497 retry_count, timeout);
3499 case CAM_CMD_MODE_PAGE:
3500 modepage(cam_dev, argc, argv, combinedopt,
3501 retry_count, timeout);
3503 case CAM_CMD_SCSI_CMD:
3504 error = scsicmd(cam_dev, argc, argv, combinedopt,
3505 retry_count, timeout);
3508 error = camdebug(argc, argv, combinedopt);
3511 error = tagcontrol(cam_dev, argc, argv, combinedopt);
3514 error = ratecontrol(cam_dev, retry_count, timeout,
3515 argc, argv, combinedopt);
3517 case CAM_CMD_FORMAT:
3518 error = scsiformat(cam_dev, argc, argv,
3519 combinedopt, retry_count, timeout);
3521 #endif /* MINIMALISTIC */
3531 if (cam_dev != NULL)
3532 cam_close_device(cam_dev);