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 $
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
42 #include <cam/cam_debug.h>
43 #include <cam/cam_ccb.h>
44 #include <cam/scsi/scsi_all.h>
45 #include <cam/scsi/scsi_da.h>
46 #include <cam/scsi/scsi_pass.h>
47 #include <cam/scsi/scsi_message.h>
49 #include "camcontrol.h"
52 CAM_CMD_NONE = 0x00000000,
53 CAM_CMD_DEVLIST = 0x00000001,
54 CAM_CMD_TUR = 0x00000002,
55 CAM_CMD_INQUIRY = 0x00000003,
56 CAM_CMD_STARTSTOP = 0x00000004,
57 CAM_CMD_RESCAN = 0x00000005,
58 CAM_CMD_READ_DEFECTS = 0x00000006,
59 CAM_CMD_MODE_PAGE = 0x00000007,
60 CAM_CMD_SCSI_CMD = 0x00000008,
61 CAM_CMD_DEVTREE = 0x00000009,
62 CAM_CMD_USAGE = 0x0000000a,
63 CAM_CMD_DEBUG = 0x0000000b,
64 CAM_CMD_RESET = 0x0000000c,
65 CAM_CMD_FORMAT = 0x0000000d,
66 CAM_CMD_TAG = 0x0000000e,
67 CAM_CMD_RATE = 0x0000000f,
68 CAM_CMD_DETACH = 0x00000010,
72 CAM_ARG_NONE = 0x00000000,
73 CAM_ARG_VERBOSE = 0x00000001,
74 CAM_ARG_DEVICE = 0x00000002,
75 CAM_ARG_BUS = 0x00000004,
76 CAM_ARG_TARGET = 0x00000008,
77 CAM_ARG_LUN = 0x00000010,
78 CAM_ARG_EJECT = 0x00000020,
79 CAM_ARG_UNIT = 0x00000040,
80 CAM_ARG_FORMAT_BLOCK = 0x00000080,
81 CAM_ARG_FORMAT_BFI = 0x00000100,
82 CAM_ARG_FORMAT_PHYS = 0x00000200,
83 CAM_ARG_PLIST = 0x00000400,
84 CAM_ARG_GLIST = 0x00000800,
85 CAM_ARG_GET_SERIAL = 0x00001000,
86 CAM_ARG_GET_STDINQ = 0x00002000,
87 CAM_ARG_GET_XFERRATE = 0x00004000,
88 CAM_ARG_INQ_MASK = 0x00007000,
89 CAM_ARG_MODE_EDIT = 0x00008000,
90 CAM_ARG_PAGE_CNTL = 0x00010000,
91 CAM_ARG_TIMEOUT = 0x00020000,
92 CAM_ARG_CMD_IN = 0x00040000,
93 CAM_ARG_CMD_OUT = 0x00080000,
94 CAM_ARG_DBD = 0x00100000,
95 CAM_ARG_ERR_RECOVER = 0x00200000,
96 CAM_ARG_RETRIES = 0x00400000,
97 CAM_ARG_START_UNIT = 0x00800000,
98 CAM_ARG_DEBUG_INFO = 0x01000000,
99 CAM_ARG_DEBUG_TRACE = 0x02000000,
100 CAM_ARG_DEBUG_SUBTRACE = 0x04000000,
101 CAM_ARG_DEBUG_CDB = 0x08000000,
102 CAM_ARG_DEBUG_XPT = 0x10000000,
103 CAM_ARG_DEBUG_PERIPH = 0x20000000,
106 struct camcontrol_opts {
114 static const char scsicmd_opts[] = "c:i:o:";
115 static const char readdefect_opts[] = "f:GP";
116 static const char negotiate_opts[] = "acD:O:qR:T:UW:";
119 struct camcontrol_opts option_table[] = {
121 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
122 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
123 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
124 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
125 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
126 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
127 #endif /* MINIMALISTIC */
128 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
129 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
131 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
132 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
133 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
134 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
135 #endif /* MINIMALISTIC */
136 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
138 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
139 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
140 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
141 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
142 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
143 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
144 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qwy"},
145 #endif /* MINIMALISTIC */
146 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
147 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
148 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
160 int bus, target, lun;
163 camcontrol_optret getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
166 static int getdevlist(struct cam_device *device);
167 static int getdevtree(void);
168 static int testunitready(struct cam_device *device, int retry_count,
169 int timeout, int quiet);
170 static int scsistart(struct cam_device *device, int startstop, int loadeject,
171 int retry_count, int timeout);
172 static int scsidoinquiry(struct cam_device *device, int argc, char **argv,
173 char *combinedopt, int retry_count, int timeout);
174 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout);
175 static int scsiserial(struct cam_device *device, int retry_count, int timeout);
176 static int scsixferrate(struct cam_device *device);
177 #endif /* MINIMALISTIC */
178 static int parse_btl(char *tstr, int *bus, int *target, int *lun,
179 cam_argmask *arglist);
180 static int dorescan_or_reset(int argc, char **argv, int rescan);
181 static int rescan_or_reset_bus(int bus, int rescan);
182 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan);
184 static int readdefects(struct cam_device *device, int argc, char **argv,
185 char *combinedopt, int retry_count, int timeout);
186 static void modepage(struct cam_device *device, int argc, char **argv,
187 char *combinedopt, int retry_count, int timeout);
188 static int scsicmd(struct cam_device *device, int argc, char **argv,
189 char *combinedopt, int retry_count, int timeout);
190 static int tagcontrol(struct cam_device *device, int argc, char **argv,
192 static void cts_print(struct cam_device *device,
193 struct ccb_trans_settings *cts);
194 static void cpi_print(struct ccb_pathinq *cpi);
195 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
196 static int get_print_cts(struct cam_device *device, int user_settings,
197 int quiet, struct ccb_trans_settings *cts);
198 static int ratecontrol(struct cam_device *device, int retry_count,
199 int timeout, int argc, char **argv, char *combinedopt);
200 static int scsiformat(struct cam_device *device, int argc, char **argv,
201 char *combinedopt, int retry_count, int timeout);
202 #endif /* MINIMALISTIC */
205 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum, char **subopt)
207 struct camcontrol_opts *opts;
210 for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
212 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
213 *cmdnum = opts->cmdnum;
214 *argnum = opts->argnum;
215 *subopt = (char *)opts->subopt;
216 if (++num_matches > 1)
217 return(CC_OR_AMBIGUOUS);
224 return(CC_OR_NOT_FOUND);
229 getdevlist(struct cam_device *device)
235 ccb = cam_getccb(device);
237 ccb->ccb_h.func_code = XPT_GDEVLIST;
238 ccb->ccb_h.flags = CAM_DIR_NONE;
239 ccb->ccb_h.retry_count = 1;
241 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
242 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
243 if (cam_send_ccb(device, ccb) < 0) {
244 perror("error getting device list");
251 switch (ccb->cgdl.status) {
252 case CAM_GDEVLIST_MORE_DEVS:
253 strcpy(status, "MORE");
255 case CAM_GDEVLIST_LAST_DEVICE:
256 strcpy(status, "LAST");
258 case CAM_GDEVLIST_LIST_CHANGED:
259 strcpy(status, "CHANGED");
261 case CAM_GDEVLIST_ERROR:
262 strcpy(status, "ERROR");
267 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
268 ccb->cgdl.periph_name,
269 ccb->cgdl.unit_number,
270 ccb->cgdl.generation,
275 * If the list has changed, we need to start over from the
278 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
286 #endif /* MINIMALISTIC */
298 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
299 warn("couldn't open %s", XPT_DEVICE);
303 bzero(&(&ccb.ccb_h)[1],
304 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
306 ccb.ccb_h.func_code = XPT_DEV_MATCH;
307 bufsize = sizeof(struct dev_match_result) * 100;
308 ccb.cdm.match_buf_len = bufsize;
309 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
310 if (ccb.cdm.matches == NULL) {
311 warnx("can't malloc memory for matches");
315 ccb.cdm.num_matches = 0;
318 * We fetch all nodes, since we display most of them in the default
319 * case, and all in the verbose case.
321 ccb.cdm.num_patterns = 0;
322 ccb.cdm.pattern_buf_len = 0;
325 * We do the ioctl multiple times if necessary, in case there are
326 * more than 100 nodes in the EDT.
329 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
330 warn("error sending CAMIOCOMMAND ioctl");
335 if ((ccb.ccb_h.status != CAM_REQ_CMP)
336 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
337 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
338 warnx("got CAM error %#x, CDM error %d\n",
339 ccb.ccb_h.status, ccb.cdm.status);
344 for (i = 0; i < ccb.cdm.num_matches; i++) {
345 switch (ccb.cdm.matches[i].type) {
346 case DEV_MATCH_BUS: {
347 struct bus_match_result *bus_result;
350 * Only print the bus information if the
351 * user turns on the verbose flag.
353 if ((arglist & CAM_ARG_VERBOSE) == 0)
357 &ccb.cdm.matches[i].result.bus_result;
360 fprintf(stdout, ")\n");
364 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
366 bus_result->dev_name,
367 bus_result->unit_number,
371 case DEV_MATCH_DEVICE: {
372 struct device_match_result *dev_result;
373 char vendor[16], product[48], revision[16];
377 &ccb.cdm.matches[i].result.device_result;
379 if ((dev_result->flags
380 & DEV_RESULT_UNCONFIGURED)
381 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
387 cam_strvis(vendor, dev_result->inq_data.vendor,
388 sizeof(dev_result->inq_data.vendor),
391 dev_result->inq_data.product,
392 sizeof(dev_result->inq_data.product),
395 dev_result->inq_data.revision,
396 sizeof(dev_result->inq_data.revision),
398 sprintf(tmpstr, "<%s %s %s>", vendor, product,
401 fprintf(stdout, ")\n");
405 fprintf(stdout, "%-33s at scbus%d "
406 "target %d lun %d (",
409 dev_result->target_id,
410 dev_result->target_lun);
416 case DEV_MATCH_PERIPH: {
417 struct periph_match_result *periph_result;
420 &ccb.cdm.matches[i].result.periph_result;
422 if (skip_device != 0)
426 fprintf(stdout, ",");
428 fprintf(stdout, "%s%d",
429 periph_result->periph_name,
430 periph_result->unit_number);
436 fprintf(stdout, "unknown match type\n");
441 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
442 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
445 fprintf(stdout, ")\n");
454 testunitready(struct cam_device *device, int retry_count, int timeout,
460 ccb = cam_getccb(device);
462 scsi_test_unit_ready(&ccb->csio,
463 /* retries */ retry_count,
465 /* tag_action */ MSG_SIMPLE_Q_TAG,
466 /* sense_len */ SSD_FULL_SIZE,
467 /* timeout */ timeout ? timeout : 5000);
469 /* Disable freezing the device queue */
470 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
472 if (arglist & CAM_ARG_ERR_RECOVER)
473 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
475 if (cam_send_ccb(device, ccb) < 0) {
477 perror("error sending test unit ready");
479 if (arglist & CAM_ARG_VERBOSE) {
480 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
481 CAM_SCSI_STATUS_ERROR)
482 scsi_sense_print(device, &ccb->csio, stderr);
484 fprintf(stderr, "CAM status is %#x\n",
492 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
494 fprintf(stdout, "Unit is ready\n");
497 fprintf(stdout, "Unit is not ready\n");
500 if (arglist & CAM_ARG_VERBOSE) {
501 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
502 CAM_SCSI_STATUS_ERROR)
503 scsi_sense_print(device, &ccb->csio, stderr);
505 fprintf(stderr, "CAM status is %#x\n",
516 scsistart(struct cam_device *device, int startstop, int loadeject,
517 int retry_count, int timeout)
522 ccb = cam_getccb(device);
525 * If we're stopping, send an ordered tag so the drive in question
526 * will finish any previously queued writes before stopping. If
527 * the device isn't capable of tagged queueing, or if tagged
528 * queueing is turned off, the tag action is a no-op.
530 scsi_start_stop(&ccb->csio,
531 /* retries */ retry_count,
533 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
535 /* start/stop */ startstop,
536 /* load_eject */ loadeject,
538 /* sense_len */ SSD_FULL_SIZE,
539 /* timeout */ timeout ? timeout : 120000);
541 /* Disable freezing the device queue */
542 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
544 if (arglist & CAM_ARG_ERR_RECOVER)
545 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
547 if (cam_send_ccb(device, ccb) < 0) {
548 perror("error sending start unit");
550 if (arglist & CAM_ARG_VERBOSE) {
551 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
552 CAM_SCSI_STATUS_ERROR)
553 scsi_sense_print(device, &ccb->csio, stderr);
555 fprintf(stderr, "CAM status is %#x\n",
563 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
565 fprintf(stdout, "Unit started successfully");
567 fprintf(stdout,", Media loaded\n");
569 fprintf(stdout,"\n");
571 fprintf(stdout, "Unit stopped successfully");
573 fprintf(stdout, ", Media ejected\n");
575 fprintf(stdout, "\n");
581 "Error received from start unit command\n");
584 "Error received from stop unit command\n");
586 if (arglist & CAM_ARG_VERBOSE) {
587 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
588 CAM_SCSI_STATUS_ERROR)
589 scsi_sense_print(device, &ccb->csio, stderr);
591 fprintf(stderr, "CAM status is %#x\n",
602 scsidoinquiry(struct cam_device *device, int argc, char **argv,
603 char *combinedopt, int retry_count, int timeout)
608 while ((c = getopt(argc, argv, combinedopt)) != -1) {
611 arglist |= CAM_ARG_GET_STDINQ;
614 arglist |= CAM_ARG_GET_XFERRATE;
617 arglist |= CAM_ARG_GET_SERIAL;
625 * If the user didn't specify any inquiry options, he wants all of
628 if ((arglist & CAM_ARG_INQ_MASK) == 0)
629 arglist |= CAM_ARG_INQ_MASK;
631 if (arglist & CAM_ARG_GET_STDINQ)
632 error = scsiinquiry(device, retry_count, timeout);
637 if (arglist & CAM_ARG_GET_SERIAL)
638 scsiserial(device, retry_count, timeout);
643 if (arglist & CAM_ARG_GET_XFERRATE)
644 error = scsixferrate(device);
650 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
653 struct scsi_inquiry_data *inq_buf;
656 ccb = cam_getccb(device);
659 warnx("couldn't allocate CCB");
663 /* cam_getccb cleans up the header, caller has to zero the payload */
664 bzero(&(&ccb->ccb_h)[1],
665 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
667 inq_buf = (struct scsi_inquiry_data *)malloc(
668 sizeof(struct scsi_inquiry_data));
670 if (inq_buf == NULL) {
672 warnx("can't malloc memory for inquiry\n");
675 bzero(inq_buf, sizeof(*inq_buf));
678 * Note that although the size of the inquiry buffer is the full
679 * 256 bytes specified in the SCSI spec, we only tell the device
680 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
681 * two reasons for this:
683 * - The SCSI spec says that when a length field is only 1 byte,
684 * a value of 0 will be interpreted as 256. Therefore
685 * scsi_inquiry() will convert an inq_len (which is passed in as
686 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
687 * to 0. Evidently, very few devices meet the spec in that
688 * regard. Some devices, like many Seagate disks, take the 0 as
689 * 0, and don't return any data. One Pioneer DVD-R drive
690 * returns more data than the command asked for.
692 * So, since there are numerous devices that just don't work
693 * right with the full inquiry size, we don't send the full size.
695 * - The second reason not to use the full inquiry data length is
696 * that we don't need it here. The only reason we issue a
697 * standard inquiry is to get the vendor name, device name,
698 * and revision so scsi_print_inquiry() can print them.
700 * If, at some point in the future, more inquiry data is needed for
701 * some reason, this code should use a procedure similar to the
702 * probe code. i.e., issue a short inquiry, and determine from
703 * the additional length passed back from the device how much
704 * inquiry data the device supports. Once the amount the device
705 * supports is determined, issue an inquiry for that amount and no
710 scsi_inquiry(&ccb->csio,
711 /* retries */ retry_count,
713 /* tag_action */ MSG_SIMPLE_Q_TAG,
714 /* inq_buf */ (u_int8_t *)inq_buf,
715 /* inq_len */ SHORT_INQUIRY_LENGTH,
718 /* sense_len */ SSD_FULL_SIZE,
719 /* timeout */ timeout ? timeout : 5000);
721 /* Disable freezing the device queue */
722 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
724 if (arglist & CAM_ARG_ERR_RECOVER)
725 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
727 if (cam_send_ccb(device, ccb) < 0) {
728 perror("error sending SCSI inquiry");
730 if (arglist & CAM_ARG_VERBOSE) {
731 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
732 CAM_SCSI_STATUS_ERROR)
733 scsi_sense_print(device, &ccb->csio, stderr);
735 fprintf(stderr, "CAM status is %#x\n",
743 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
746 if (arglist & CAM_ARG_VERBOSE) {
747 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
748 CAM_SCSI_STATUS_ERROR)
749 scsi_sense_print(device, &ccb->csio, stderr);
751 fprintf(stderr, "CAM status is %#x\n",
763 fprintf(stdout, "%s%d: ", device->device_name,
764 device->dev_unit_num);
765 scsi_print_inquiry(inq_buf);
773 scsiserial(struct cam_device *device, int retry_count, int timeout)
776 struct scsi_vpd_unit_serial_number *serial_buf;
777 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
780 ccb = cam_getccb(device);
783 warnx("couldn't allocate CCB");
787 /* cam_getccb cleans up the header, caller has to zero the payload */
788 bzero(&(&ccb->ccb_h)[1],
789 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
791 serial_buf = (struct scsi_vpd_unit_serial_number *)
792 malloc(sizeof(*serial_buf));
794 if (serial_buf == NULL) {
796 warnx("can't malloc memory for serial number");
800 scsi_inquiry(&ccb->csio,
801 /*retries*/ retry_count,
803 /* tag_action */ MSG_SIMPLE_Q_TAG,
804 /* inq_buf */ (u_int8_t *)serial_buf,
805 /* inq_len */ sizeof(*serial_buf),
807 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
808 /* sense_len */ SSD_FULL_SIZE,
809 /* timeout */ timeout ? timeout : 5000);
811 /* Disable freezing the device queue */
812 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
814 if (arglist & CAM_ARG_ERR_RECOVER)
815 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
817 if (cam_send_ccb(device, ccb) < 0) {
818 warn("error getting serial number");
820 if (arglist & CAM_ARG_VERBOSE) {
821 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
822 CAM_SCSI_STATUS_ERROR)
823 scsi_sense_print(device, &ccb->csio, stderr);
825 fprintf(stderr, "CAM status is %#x\n",
834 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
837 if (arglist & CAM_ARG_VERBOSE) {
838 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
839 CAM_SCSI_STATUS_ERROR)
840 scsi_sense_print(device, &ccb->csio, stderr);
842 fprintf(stderr, "CAM status is %#x\n",
854 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
855 serial_num[serial_buf->length] = '\0';
857 if ((arglist & CAM_ARG_GET_STDINQ)
858 || (arglist & CAM_ARG_GET_XFERRATE))
859 fprintf(stdout, "%s%d: Serial Number ",
860 device->device_name, device->dev_unit_num);
862 fprintf(stdout, "%.60s\n", serial_num);
870 scsixferrate(struct cam_device *device)
878 ccb = cam_getccb(device);
881 warnx("couldn't allocate CCB");
885 bzero(&(&ccb->ccb_h)[1],
886 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
888 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
889 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
891 if (((retval = cam_send_ccb(device, ccb)) < 0)
892 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
893 const char error_string[] = "error getting transfer settings";
901 * If there is an error, it won't be a SCSI error since
902 * this isn't a SCSI CCB.
904 if (arglist & CAM_ARG_VERBOSE)
905 fprintf(stderr, "CAM status is %#x\n",
910 goto xferrate_bailout;
914 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
915 && (ccb->cts.sync_offset != 0)) {
916 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
919 struct ccb_pathinq cpi;
921 retval = get_cpi(device, &cpi);
924 goto xferrate_bailout;
926 speed = cpi.base_transfer_speed;
930 fprintf(stdout, "%s%d: ", device->device_name,
931 device->dev_unit_num);
933 if ((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
934 speed *= (0x01 << device->bus_width);
939 fprintf(stdout, "%d.%03dMB/s transfers ",
942 fprintf(stdout, "%dKB/s transfers ",
945 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
946 && (ccb->cts.sync_offset != 0))
947 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
948 freq % 1000, ccb->cts.sync_offset);
950 if (((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
951 && (ccb->cts.bus_width > 0)) {
952 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
953 && (ccb->cts.sync_offset != 0)) {
954 fprintf(stdout, ", ");
956 fprintf(stdout, " (");
958 fprintf(stdout, "%dbit)", 8 * (0x01 << ccb->cts.bus_width));
959 } else if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
960 && (ccb->cts.sync_offset != 0)) {
961 fprintf(stdout, ")");
964 if (((ccb->cts.valid & CCB_TRANS_TQ_VALID) != 0)
965 && (ccb->cts.flags & CCB_TRANS_TAG_ENB))
966 fprintf(stdout, ", Tagged Queueing Enabled");
968 fprintf(stdout, "\n");
976 #endif /* MINIMALISTIC */
979 * Parse out a bus, or a bus, target and lun in the following
985 * Returns the number of parsed components, or 0.
988 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglist)
993 while (isspace(*tstr) && (*tstr != '\0'))
996 tmpstr = (char *)strtok(tstr, ":");
997 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
998 *bus = strtol(tmpstr, NULL, 0);
999 *arglist |= CAM_ARG_BUS;
1001 tmpstr = (char *)strtok(NULL, ":");
1002 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1003 *target = strtol(tmpstr, NULL, 0);
1004 *arglist |= CAM_ARG_TARGET;
1006 tmpstr = (char *)strtok(NULL, ":");
1007 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1008 *lun = strtol(tmpstr, NULL, 0);
1009 *arglist |= CAM_ARG_LUN;
1019 dorescan_or_reset(int argc, char **argv, int rescan)
1021 static const char must[] =
1022 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1024 int bus = -1, target = -1, lun = -1;
1028 warnx(must, rescan? "rescan" : "reset");
1032 tstr = argv[optind];
1033 while (isspace(*tstr) && (*tstr != '\0'))
1035 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1036 arglist |= CAM_ARG_BUS;
1038 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
1039 if (rv != 1 && rv != 3) {
1040 warnx(must, rescan? "rescan" : "reset");
1045 if ((arglist & CAM_ARG_BUS)
1046 && (arglist & CAM_ARG_TARGET)
1047 && (arglist & CAM_ARG_LUN))
1048 error = scanlun_or_reset_dev(bus, target, lun, rescan);
1050 error = rescan_or_reset_bus(bus, rescan);
1056 rescan_or_reset_bus(int bus, int rescan)
1058 union ccb ccb, matchccb;
1064 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1065 warnx("error opening tranport layer device %s", XPT_DEVICE);
1066 warn("%s", XPT_DEVICE);
1071 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1072 ccb.ccb_h.path_id = bus;
1073 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1074 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1075 ccb.crcn.flags = CAM_FLAG_NONE;
1077 /* run this at a low priority */
1078 ccb.ccb_h.pinfo.priority = 5;
1080 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1081 warn("CAMIOCOMMAND ioctl failed");
1086 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1087 fprintf(stdout, "%s of bus %d was successful\n",
1088 rescan ? "Re-scan" : "Reset", bus);
1090 fprintf(stdout, "%s of bus %d returned error %#x\n",
1091 rescan ? "Re-scan" : "Reset", bus,
1092 ccb.ccb_h.status & CAM_STATUS_MASK);
1103 * The right way to handle this is to modify the xpt so that it can
1104 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1105 * that isn't implemented, so instead we enumerate the busses and
1106 * send the rescan or reset to those busses in the case where the
1107 * given bus is -1 (wildcard). We don't send a rescan or reset
1108 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1109 * no-op, sending a rescan to the xpt bus would result in a status of
1112 bzero(&(&matchccb.ccb_h)[1],
1113 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1114 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1115 bufsize = sizeof(struct dev_match_result) * 20;
1116 matchccb.cdm.match_buf_len = bufsize;
1117 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1118 if (matchccb.cdm.matches == NULL) {
1119 warnx("can't malloc memory for matches");
1123 matchccb.cdm.num_matches = 0;
1125 matchccb.cdm.num_patterns = 1;
1126 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1128 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1129 matchccb.cdm.pattern_buf_len);
1130 if (matchccb.cdm.patterns == NULL) {
1131 warnx("can't malloc memory for patterns");
1135 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1136 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1141 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1142 warn("CAMIOCOMMAND ioctl failed");
1147 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1148 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1149 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1150 warnx("got CAM error %#x, CDM error %d\n",
1151 matchccb.ccb_h.status, matchccb.cdm.status);
1156 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1157 struct bus_match_result *bus_result;
1159 /* This shouldn't happen. */
1160 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1163 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1166 * We don't want to rescan or reset the xpt bus.
1169 if ((int)bus_result->path_id == -1)
1172 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1174 ccb.ccb_h.path_id = bus_result->path_id;
1175 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1176 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1177 ccb.crcn.flags = CAM_FLAG_NONE;
1179 /* run this at a low priority */
1180 ccb.ccb_h.pinfo.priority = 5;
1182 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1183 warn("CAMIOCOMMAND ioctl failed");
1188 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1189 fprintf(stdout, "%s of bus %d was successful\n",
1190 rescan? "Re-scan" : "Reset",
1191 bus_result->path_id);
1194 * Don't bail out just yet, maybe the other
1195 * rescan or reset commands will complete
1198 fprintf(stderr, "%s of bus %d returned error "
1199 "%#x\n", rescan? "Re-scan" : "Reset",
1200 bus_result->path_id,
1201 ccb.ccb_h.status & CAM_STATUS_MASK);
1205 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1206 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1213 if (matchccb.cdm.patterns != NULL)
1214 free(matchccb.cdm.patterns);
1215 if (matchccb.cdm.matches != NULL)
1216 free(matchccb.cdm.matches);
1222 scanlun_or_reset_dev(int bus, int target, int lun, int scan)
1225 struct cam_device *device;
1231 warnx("invalid bus number %d", bus);
1236 warnx("invalid target number %d", target);
1241 warnx("invalid lun number %d", lun);
1247 bzero(&ccb, sizeof(union ccb));
1250 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1251 warnx("error opening tranport layer device %s\n",
1253 warn("%s", XPT_DEVICE);
1257 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
1258 if (device == NULL) {
1259 warnx("%s", cam_errbuf);
1264 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1265 ccb.ccb_h.path_id = bus;
1266 ccb.ccb_h.target_id = target;
1267 ccb.ccb_h.target_lun = lun;
1268 ccb.ccb_h.timeout = 5000;
1269 ccb.crcn.flags = CAM_FLAG_NONE;
1271 /* run this at a low priority */
1272 ccb.ccb_h.pinfo.priority = 5;
1275 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1276 warn("CAMIOCOMMAND ioctl failed");
1281 if (cam_send_ccb(device, &ccb) < 0) {
1282 warn("error sending XPT_RESET_DEV CCB");
1283 cam_close_device(device);
1291 cam_close_device(device);
1294 * An error code of CAM_BDR_SENT is normal for a BDR request.
1296 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1298 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1299 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1300 scan? "Re-scan" : "Reset", bus, target, lun);
1303 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1304 scan? "Re-scan" : "Reset", bus, target, lun,
1305 ccb.ccb_h.status & CAM_STATUS_MASK);
1310 #ifndef MINIMALISTIC
1312 readdefects(struct cam_device *device, int argc, char **argv,
1313 char *combinedopt, int retry_count, int timeout)
1315 union ccb *ccb = NULL;
1316 struct scsi_read_defect_data_10 *rdd_cdb;
1317 u_int8_t *defect_list = NULL;
1318 u_int32_t dlist_length = 65000;
1319 u_int32_t returned_length = 0;
1320 u_int32_t num_returned = 0;
1321 u_int8_t returned_format;
1324 int lists_specified = 0;
1326 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1332 while (isspace(*tstr) && (*tstr != '\0'))
1334 if (strcmp(tstr, "block") == 0)
1335 arglist |= CAM_ARG_FORMAT_BLOCK;
1336 else if (strcmp(tstr, "bfi") == 0)
1337 arglist |= CAM_ARG_FORMAT_BFI;
1338 else if (strcmp(tstr, "phys") == 0)
1339 arglist |= CAM_ARG_FORMAT_PHYS;
1342 warnx("invalid defect format %s", tstr);
1343 goto defect_bailout;
1348 arglist |= CAM_ARG_GLIST;
1351 arglist |= CAM_ARG_PLIST;
1358 ccb = cam_getccb(device);
1361 * Hopefully 65000 bytes is enough to hold the defect list. If it
1362 * isn't, the disk is probably dead already. We'd have to go with
1363 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1366 defect_list = malloc(dlist_length);
1367 if (defect_list == NULL) {
1368 warnx("can't malloc memory for defect list");
1370 goto defect_bailout;
1373 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1376 * cam_getccb() zeros the CCB header only. So we need to zero the
1377 * payload portion of the ccb.
1379 bzero(&(&ccb->ccb_h)[1],
1380 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1382 cam_fill_csio(&ccb->csio,
1383 /*retries*/ retry_count,
1385 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1386 CAM_PASS_ERR_RECOVER : 0),
1387 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1388 /*data_ptr*/ defect_list,
1389 /*dxfer_len*/ dlist_length,
1390 /*sense_len*/ SSD_FULL_SIZE,
1391 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1392 /*timeout*/ timeout ? timeout : 5000);
1394 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1395 if (arglist & CAM_ARG_FORMAT_BLOCK)
1396 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1397 else if (arglist & CAM_ARG_FORMAT_BFI)
1398 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1399 else if (arglist & CAM_ARG_FORMAT_PHYS)
1400 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1403 warnx("no defect list format specified");
1404 goto defect_bailout;
1406 if (arglist & CAM_ARG_PLIST) {
1407 rdd_cdb->format |= SRDD10_PLIST;
1411 if (arglist & CAM_ARG_GLIST) {
1412 rdd_cdb->format |= SRDD10_GLIST;
1416 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1418 /* Disable freezing the device queue */
1419 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1421 if (cam_send_ccb(device, ccb) < 0) {
1422 perror("error reading defect list");
1424 if (arglist & CAM_ARG_VERBOSE) {
1425 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1426 CAM_SCSI_STATUS_ERROR)
1427 scsi_sense_print(device, &ccb->csio, stderr);
1429 fprintf(stderr, "CAM status is %#x\n",
1434 goto defect_bailout;
1437 if (arglist & CAM_ARG_VERBOSE)
1438 scsi_sense_print(device, &ccb->csio, stderr);
1440 returned_length = scsi_2btoul(((struct
1441 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1443 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1444 defect_list)->format;
1446 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1447 struct scsi_sense_data *sense;
1448 int error_code, sense_key, asc, ascq;
1450 sense = &ccb->csio.sense_data;
1451 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1454 * According to the SCSI spec, if the disk doesn't support
1455 * the requested format, it will generally return a sense
1456 * key of RECOVERED ERROR, and an additional sense code
1457 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1458 * also check to make sure that the returned length is
1459 * greater than 0, and then print out whatever format the
1462 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1463 && (asc == 0x1c) && (ascq == 0x00)
1464 && (returned_length > 0)) {
1465 warnx("requested defect format not available");
1466 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1467 case SRDD10_BLOCK_FORMAT:
1468 warnx("Device returned block format");
1470 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1471 warnx("Device returned bytes from index"
1474 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1475 warnx("Device returned physical sector format");
1479 warnx("Device returned unknown defect"
1480 " data format %#x", returned_format);
1481 goto defect_bailout;
1482 break; /* NOTREACHED */
1486 warnx("Error returned from read defect data command");
1487 goto defect_bailout;
1492 * XXX KDM I should probably clean up the printout format for the
1495 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1496 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1498 struct scsi_defect_desc_phys_sector *dlist;
1500 dlist = (struct scsi_defect_desc_phys_sector *)
1502 sizeof(struct scsi_read_defect_data_hdr_10));
1504 num_returned = returned_length /
1505 sizeof(struct scsi_defect_desc_phys_sector);
1507 fprintf(stderr, "Got %d defect", num_returned);
1509 if ((lists_specified == 0) || (num_returned == 0)) {
1510 fprintf(stderr, "s.\n");
1512 } else if (num_returned == 1)
1513 fprintf(stderr, ":\n");
1515 fprintf(stderr, "s:\n");
1517 for (i = 0; i < num_returned; i++) {
1518 fprintf(stdout, "%d:%d:%d\n",
1519 scsi_3btoul(dlist[i].cylinder),
1521 scsi_4btoul(dlist[i].sector));
1525 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1527 struct scsi_defect_desc_bytes_from_index *dlist;
1529 dlist = (struct scsi_defect_desc_bytes_from_index *)
1531 sizeof(struct scsi_read_defect_data_hdr_10));
1533 num_returned = returned_length /
1534 sizeof(struct scsi_defect_desc_bytes_from_index);
1536 fprintf(stderr, "Got %d defect", num_returned);
1538 if ((lists_specified == 0) || (num_returned == 0)) {
1539 fprintf(stderr, "s.\n");
1541 } else if (num_returned == 1)
1542 fprintf(stderr, ":\n");
1544 fprintf(stderr, "s:\n");
1546 for (i = 0; i < num_returned; i++) {
1547 fprintf(stdout, "%d:%d:%d\n",
1548 scsi_3btoul(dlist[i].cylinder),
1550 scsi_4btoul(dlist[i].bytes_from_index));
1554 case SRDDH10_BLOCK_FORMAT:
1556 struct scsi_defect_desc_block *dlist;
1558 dlist = (struct scsi_defect_desc_block *)(defect_list +
1559 sizeof(struct scsi_read_defect_data_hdr_10));
1561 num_returned = returned_length /
1562 sizeof(struct scsi_defect_desc_block);
1564 fprintf(stderr, "Got %d defect", num_returned);
1566 if ((lists_specified == 0) || (num_returned == 0)) {
1567 fprintf(stderr, "s.\n");
1569 } else if (num_returned == 1)
1570 fprintf(stderr, ":\n");
1572 fprintf(stderr, "s:\n");
1574 for (i = 0; i < num_returned; i++)
1575 fprintf(stdout, "%u\n",
1576 scsi_4btoul(dlist[i].address));
1580 fprintf(stderr, "Unknown defect format %d\n",
1581 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1587 if (defect_list != NULL)
1595 #endif /* MINIMALISTIC */
1599 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1603 ccb = cam_getccb(device);
1609 #ifndef MINIMALISTIC
1611 mode_sense(struct cam_device *device, int mode_page, int page_control,
1612 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1617 ccb = cam_getccb(device);
1620 errx(1, "mode_sense: couldn't allocate CCB");
1622 bzero(&(&ccb->ccb_h)[1],
1623 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1625 scsi_mode_sense(&ccb->csio,
1626 /* retries */ retry_count,
1628 /* tag_action */ MSG_SIMPLE_Q_TAG,
1630 /* page_code */ page_control << 6,
1631 /* page */ mode_page,
1632 /* param_buf */ data,
1633 /* param_len */ datalen,
1634 /* sense_len */ SSD_FULL_SIZE,
1635 /* timeout */ timeout ? timeout : 5000);
1637 if (arglist & CAM_ARG_ERR_RECOVER)
1638 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1640 /* Disable freezing the device queue */
1641 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1643 if (((retval = cam_send_ccb(device, ccb)) < 0)
1644 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1645 if (arglist & CAM_ARG_VERBOSE) {
1646 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1647 CAM_SCSI_STATUS_ERROR)
1648 scsi_sense_print(device, &ccb->csio, stderr);
1650 fprintf(stderr, "CAM status is %#x\n",
1654 cam_close_device(device);
1656 err(1, "error sending mode sense command");
1658 errx(1, "error sending mode sense command");
1665 mode_select(struct cam_device *device, int save_pages, int retry_count,
1666 int timeout, u_int8_t *data, int datalen)
1671 ccb = cam_getccb(device);
1674 errx(1, "mode_select: couldn't allocate CCB");
1676 bzero(&(&ccb->ccb_h)[1],
1677 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1679 scsi_mode_select(&ccb->csio,
1680 /* retries */ retry_count,
1682 /* tag_action */ MSG_SIMPLE_Q_TAG,
1683 /* scsi_page_fmt */ 1,
1684 /* save_pages */ save_pages,
1685 /* param_buf */ data,
1686 /* param_len */ datalen,
1687 /* sense_len */ SSD_FULL_SIZE,
1688 /* timeout */ timeout ? timeout : 5000);
1690 if (arglist & CAM_ARG_ERR_RECOVER)
1691 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1693 /* Disable freezing the device queue */
1694 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1696 if (((retval = cam_send_ccb(device, ccb)) < 0)
1697 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1698 if (arglist & CAM_ARG_VERBOSE) {
1699 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1700 CAM_SCSI_STATUS_ERROR)
1701 scsi_sense_print(device, &ccb->csio, stderr);
1703 fprintf(stderr, "CAM status is %#x\n",
1707 cam_close_device(device);
1710 err(1, "error sending mode select command");
1712 errx(1, "error sending mode select command");
1720 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1721 int retry_count, int timeout)
1723 int c, mode_page = -1, page_control = 0;
1724 int binary = 0, list = 0;
1726 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1732 arglist |= CAM_ARG_DBD;
1735 arglist |= CAM_ARG_MODE_EDIT;
1741 mode_page = strtol(optarg, NULL, 0);
1743 errx(1, "invalid mode page %d", mode_page);
1746 page_control = strtol(optarg, NULL, 0);
1747 if ((page_control < 0) || (page_control > 3))
1748 errx(1, "invalid page control field %d",
1750 arglist |= CAM_ARG_PAGE_CNTL;
1757 if (mode_page == -1 && list == 0)
1758 errx(1, "you must specify a mode page!");
1761 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1762 retry_count, timeout);
1764 mode_edit(device, mode_page, page_control,
1765 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1766 retry_count, timeout);
1771 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1772 int retry_count, int timeout)
1775 u_int32_t flags = CAM_DIR_NONE;
1776 u_int8_t *data_ptr = NULL;
1778 struct get_hook hook;
1779 int c, data_bytes = 0;
1781 char *datastr = NULL, *tstr;
1786 ccb = cam_getccb(device);
1789 warnx("scsicmd: error allocating ccb");
1793 bzero(&(&ccb->ccb_h)[1],
1794 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1796 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1800 while (isspace(*tstr) && (*tstr != '\0'))
1802 hook.argc = argc - optind;
1803 hook.argv = argv + optind;
1805 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1808 * Increment optind by the number of arguments the
1809 * encoding routine processed. After each call to
1810 * getopt(3), optind points to the argument that
1811 * getopt should process _next_. In this case,
1812 * that means it points to the first command string
1813 * argument, if there is one. Once we increment
1814 * this, it should point to either the next command
1815 * line argument, or it should be past the end of
1821 if (arglist & CAM_ARG_CMD_OUT) {
1822 warnx("command must either be "
1823 "read or write, not both");
1825 goto scsicmd_bailout;
1827 arglist |= CAM_ARG_CMD_IN;
1829 data_bytes = strtol(optarg, NULL, 0);
1830 if (data_bytes <= 0) {
1831 warnx("invalid number of input bytes %d",
1834 goto scsicmd_bailout;
1836 hook.argc = argc - optind;
1837 hook.argv = argv + optind;
1840 datastr = cget(&hook, NULL);
1842 * If the user supplied "-" instead of a format, he
1843 * wants the data to be written to stdout.
1845 if ((datastr != NULL)
1846 && (datastr[0] == '-'))
1849 data_ptr = (u_int8_t *)malloc(data_bytes);
1850 if (data_ptr == NULL) {
1851 warnx("can't malloc memory for data_ptr");
1853 goto scsicmd_bailout;
1857 if (arglist & CAM_ARG_CMD_IN) {
1858 warnx("command must either be "
1859 "read or write, not both");
1861 goto scsicmd_bailout;
1863 arglist |= CAM_ARG_CMD_OUT;
1864 flags = CAM_DIR_OUT;
1865 data_bytes = strtol(optarg, NULL, 0);
1866 if (data_bytes <= 0) {
1867 warnx("invalid number of output bytes %d",
1870 goto scsicmd_bailout;
1872 hook.argc = argc - optind;
1873 hook.argv = argv + optind;
1875 datastr = cget(&hook, NULL);
1876 data_ptr = (u_int8_t *)malloc(data_bytes);
1877 if (data_ptr == NULL) {
1878 warnx("can't malloc memory for data_ptr");
1880 goto scsicmd_bailout;
1883 * If the user supplied "-" instead of a format, he
1884 * wants the data to be read from stdin.
1886 if ((datastr != NULL)
1887 && (datastr[0] == '-'))
1890 buff_encode_visit(data_ptr, data_bytes, datastr,
1900 * If fd_data is set, and we're writing to the device, we need to
1901 * read the data the user wants written from stdin.
1903 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1905 int amt_to_read = data_bytes;
1906 u_int8_t *buf_ptr = data_ptr;
1908 for (amt_read = 0; amt_to_read > 0;
1909 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1910 if (amt_read == -1) {
1911 warn("error reading data from stdin");
1913 goto scsicmd_bailout;
1915 amt_to_read -= amt_read;
1916 buf_ptr += amt_read;
1920 if (arglist & CAM_ARG_ERR_RECOVER)
1921 flags |= CAM_PASS_ERR_RECOVER;
1923 /* Disable freezing the device queue */
1924 flags |= CAM_DEV_QFRZDIS;
1927 * This is taken from the SCSI-3 draft spec.
1928 * (T10/1157D revision 0.3)
1929 * The top 3 bits of an opcode are the group code. The next 5 bits
1930 * are the command code.
1931 * Group 0: six byte commands
1932 * Group 1: ten byte commands
1933 * Group 2: ten byte commands
1935 * Group 4: sixteen byte commands
1936 * Group 5: twelve byte commands
1937 * Group 6: vendor specific
1938 * Group 7: vendor specific
1940 switch((cdb[0] >> 5) & 0x7) {
1951 /* computed by buff_encode_visit */
1962 * We should probably use csio_build_visit or something like that
1963 * here, but it's easier to encode arguments as you go. The
1964 * alternative would be skipping the CDB argument and then encoding
1965 * it here, since we've got the data buffer argument by now.
1967 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1969 cam_fill_csio(&ccb->csio,
1970 /*retries*/ retry_count,
1973 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1974 /*data_ptr*/ data_ptr,
1975 /*dxfer_len*/ data_bytes,
1976 /*sense_len*/ SSD_FULL_SIZE,
1977 /*cdb_len*/ cdb_len,
1978 /*timeout*/ timeout ? timeout : 5000);
1980 if (((retval = cam_send_ccb(device, ccb)) < 0)
1981 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1983 warn("error sending command");
1985 warnx("error sending command");
1987 if (arglist & CAM_ARG_VERBOSE) {
1988 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1989 CAM_SCSI_STATUS_ERROR)
1990 scsi_sense_print(device, &ccb->csio, stderr);
1992 fprintf(stderr, "CAM status is %#x\n",
1997 goto scsicmd_bailout;
2001 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
2002 && (arglist & CAM_ARG_CMD_IN)
2003 && (data_bytes > 0)) {
2005 buff_decode_visit(data_ptr, data_bytes, datastr,
2007 fprintf(stdout, "\n");
2009 ssize_t amt_written;
2010 int amt_to_write = data_bytes;
2011 u_int8_t *buf_ptr = data_ptr;
2013 for (amt_written = 0; (amt_to_write > 0) &&
2014 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
2015 amt_to_write -= amt_written;
2016 buf_ptr += amt_written;
2018 if (amt_written == -1) {
2019 warn("error writing data to stdout");
2021 goto scsicmd_bailout;
2022 } else if ((amt_written == 0)
2023 && (amt_to_write > 0)) {
2024 warnx("only wrote %u bytes out of %u",
2025 data_bytes - amt_to_write, data_bytes);
2032 if ((data_bytes > 0) && (data_ptr != NULL))
2041 camdebug(int argc, char **argv, char *combinedopt)
2044 int bus = -1, target = -1, lun = -1;
2045 char *tstr, *tmpstr = NULL;
2049 bzero(&ccb, sizeof(union ccb));
2051 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2054 arglist |= CAM_ARG_DEBUG_INFO;
2055 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2058 arglist |= CAM_ARG_DEBUG_PERIPH;
2059 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2062 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2063 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2066 arglist |= CAM_ARG_DEBUG_TRACE;
2067 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2070 arglist |= CAM_ARG_DEBUG_XPT;
2071 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2074 arglist |= CAM_ARG_DEBUG_CDB;
2075 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2082 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2083 warnx("error opening transport layer device %s", XPT_DEVICE);
2084 warn("%s", XPT_DEVICE);
2091 warnx("you must specify \"off\", \"all\" or a bus,");
2092 warnx("bus:target, or bus:target:lun");
2099 while (isspace(*tstr) && (*tstr != '\0'))
2102 if (strncmp(tstr, "off", 3) == 0) {
2103 ccb.cdbg.flags = CAM_DEBUG_NONE;
2104 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2105 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2107 } else if (strncmp(tstr, "all", 3) != 0) {
2108 tmpstr = (char *)strtok(tstr, ":");
2109 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2110 bus = strtol(tmpstr, NULL, 0);
2111 arglist |= CAM_ARG_BUS;
2112 tmpstr = (char *)strtok(NULL, ":");
2113 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2114 target = strtol(tmpstr, NULL, 0);
2115 arglist |= CAM_ARG_TARGET;
2116 tmpstr = (char *)strtok(NULL, ":");
2117 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2118 lun = strtol(tmpstr, NULL, 0);
2119 arglist |= CAM_ARG_LUN;
2124 warnx("you must specify \"all\", \"off\", or a bus,");
2125 warnx("bus:target, or bus:target:lun to debug");
2131 ccb.ccb_h.func_code = XPT_DEBUG;
2132 ccb.ccb_h.path_id = bus;
2133 ccb.ccb_h.target_id = target;
2134 ccb.ccb_h.target_lun = lun;
2136 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2137 warn("CAMIOCOMMAND ioctl failed");
2142 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2143 CAM_FUNC_NOTAVAIL) {
2144 warnx("CAM debugging not available");
2145 warnx("you need to put options CAMDEBUG in"
2146 " your kernel config file!");
2148 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2150 warnx("XPT_DEBUG CCB failed with status %#x",
2154 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2156 "Debugging turned off\n");
2159 "Debugging enabled for "
2172 tagcontrol(struct cam_device *device, int argc, char **argv,
2182 ccb = cam_getccb(device);
2185 warnx("tagcontrol: error allocating ccb");
2189 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2192 numtags = strtol(optarg, NULL, 0);
2194 warnx("tag count %d is < 0", numtags);
2196 goto tagcontrol_bailout;
2207 cam_path_string(device, pathstr, sizeof(pathstr));
2210 bzero(&(&ccb->ccb_h)[1],
2211 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2212 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2213 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2214 ccb->crs.openings = numtags;
2217 if (cam_send_ccb(device, ccb) < 0) {
2218 perror("error sending XPT_REL_SIMQ CCB");
2220 goto tagcontrol_bailout;
2223 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2224 warnx("XPT_REL_SIMQ CCB failed, status %#x",
2227 goto tagcontrol_bailout;
2232 fprintf(stdout, "%stagged openings now %d\n",
2233 pathstr, ccb->crs.openings);
2236 bzero(&(&ccb->ccb_h)[1],
2237 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2239 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2241 if (cam_send_ccb(device, ccb) < 0) {
2242 perror("error sending XPT_GDEV_STATS CCB");
2244 goto tagcontrol_bailout;
2247 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2248 warnx("XPT_GDEV_STATS CCB failed, status %#x",
2251 goto tagcontrol_bailout;
2254 if (arglist & CAM_ARG_VERBOSE) {
2255 fprintf(stdout, "%s", pathstr);
2256 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2257 fprintf(stdout, "%s", pathstr);
2258 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2259 fprintf(stdout, "%s", pathstr);
2260 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2261 fprintf(stdout, "%s", pathstr);
2262 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2263 fprintf(stdout, "%s", pathstr);
2264 fprintf(stdout, "held %d\n", ccb->cgds.held);
2265 fprintf(stdout, "%s", pathstr);
2266 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2267 fprintf(stdout, "%s", pathstr);
2268 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2271 fprintf(stdout, "%s", pathstr);
2272 fprintf(stdout, "device openings: ");
2274 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2275 ccb->cgds.dev_active);
2285 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2289 cam_path_string(device, pathstr, sizeof(pathstr));
2291 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
2293 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2296 if (cts->sync_offset != 0) {
2299 freq = scsi_calc_syncsrate(cts->sync_period);
2300 fprintf(stdout, "%sfrequency: %d.%03dMHz\n", pathstr,
2301 freq / 1000, freq % 1000);
2305 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID)
2306 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset);
2308 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID)
2309 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2310 (0x01 << cts->bus_width) * 8);
2312 if (cts->valid & CCB_TRANS_DISC_VALID)
2313 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2314 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" :
2317 if (cts->valid & CCB_TRANS_TQ_VALID)
2318 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2319 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" :
2325 * Get a path inquiry CCB for the specified device.
2328 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2333 ccb = cam_getccb(device);
2336 warnx("get_cpi: couldn't allocate CCB");
2340 bzero(&(&ccb->ccb_h)[1],
2341 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2343 ccb->ccb_h.func_code = XPT_PATH_INQ;
2345 if (cam_send_ccb(device, ccb) < 0) {
2346 warn("get_cpi: error sending Path Inquiry CCB");
2348 if (arglist & CAM_ARG_VERBOSE)
2349 fprintf(stderr, "CAM status is %#x\n",
2354 goto get_cpi_bailout;
2357 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2359 if (arglist & CAM_ARG_VERBOSE)
2360 fprintf(stderr, "get_cpi: CAM status is %#x\n",
2365 goto get_cpi_bailout;
2368 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2378 cpi_print(struct ccb_pathinq *cpi)
2380 char adapter_str[1024];
2383 snprintf(adapter_str, sizeof(adapter_str),
2384 "%s%d:", cpi->dev_name, cpi->unit_number);
2386 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2389 for (i = 1; i < 0xff; i = i << 1) {
2392 if ((i & cpi->hba_inquiry) == 0)
2395 fprintf(stdout, "%s supports ", adapter_str);
2399 str = "MDP message";
2402 str = "32 bit wide SCSI";
2405 str = "16 bit wide SCSI";
2408 str = "SDTR message";
2411 str = "linked CDBs";
2414 str = "tag queue messages";
2417 str = "soft reset alternative";
2420 str = "unknown PI bit set";
2423 fprintf(stdout, "%s\n", str);
2426 for (i = 1; i < 0xff; i = i << 1) {
2429 if ((i & cpi->hba_misc) == 0)
2432 fprintf(stdout, "%s ", adapter_str);
2436 str = "bus scans from high ID to low ID";
2439 str = "removable devices not included in scan";
2441 case PIM_NOINITIATOR:
2442 str = "initiator role not supported";
2444 case PIM_NOBUSRESET:
2445 str = "user has disabled initial BUS RESET or"
2446 " controller is in target/mixed mode";
2449 str = "unknown PIM bit set";
2452 fprintf(stdout, "%s\n", str);
2455 for (i = 1; i < 0xff; i = i << 1) {
2458 if ((i & cpi->target_sprt) == 0)
2461 fprintf(stdout, "%s supports ", adapter_str);
2464 str = "target mode processor mode";
2467 str = "target mode phase cog. mode";
2469 case PIT_DISCONNECT:
2470 str = "disconnects in target mode";
2473 str = "terminate I/O message in target mode";
2476 str = "group 6 commands in target mode";
2479 str = "group 7 commands in target mode";
2482 str = "unknown PIT bit set";
2486 fprintf(stdout, "%s\n", str);
2488 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2490 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2492 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2494 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2495 adapter_str, cpi->hpath_id);
2496 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2498 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2499 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2500 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2501 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2502 if (cpi->base_transfer_speed > 1000)
2503 fprintf(stdout, "%d.%03dMB/sec\n",
2504 cpi->base_transfer_speed / 1000,
2505 cpi->base_transfer_speed % 1000);
2507 fprintf(stdout, "%dKB/sec\n",
2508 (cpi->base_transfer_speed % 1000) * 1000);
2512 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2513 struct ccb_trans_settings *cts)
2519 ccb = cam_getccb(device);
2522 warnx("get_print_cts: error allocating ccb");
2526 bzero(&(&ccb->ccb_h)[1],
2527 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2529 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2531 if (user_settings == 0)
2532 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2534 ccb->cts.flags = CCB_TRANS_USER_SETTINGS;
2536 if (cam_send_ccb(device, ccb) < 0) {
2537 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2539 goto get_print_cts_bailout;
2542 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2543 warnx("XPT_GET_TRANS_SETTINGS CCB failed, status %#x",
2546 goto get_print_cts_bailout;
2550 cts_print(device, &ccb->cts);
2553 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2555 get_print_cts_bailout:
2563 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2564 int argc, char **argv, char *combinedopt)
2568 int user_settings = 0;
2570 int disc_enable = -1, tag_enable = -1;
2572 double syncrate = -1;
2575 int change_settings = 0, send_tur = 0;
2576 struct ccb_pathinq cpi;
2578 ccb = cam_getccb(device);
2581 warnx("ratecontrol: error allocating ccb");
2585 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2594 if (strncasecmp(optarg, "enable", 6) == 0)
2596 else if (strncasecmp(optarg, "disable", 7) == 0)
2599 warnx("-D argument \"%s\" is unknown", optarg);
2601 goto ratecontrol_bailout;
2603 change_settings = 1;
2606 offset = strtol(optarg, NULL, 0);
2608 warnx("offset value %d is < 0", offset);
2610 goto ratecontrol_bailout;
2612 change_settings = 1;
2618 syncrate = atof(optarg);
2621 warnx("sync rate %f is < 0", syncrate);
2623 goto ratecontrol_bailout;
2625 change_settings = 1;
2628 if (strncasecmp(optarg, "enable", 6) == 0)
2630 else if (strncasecmp(optarg, "disable", 7) == 0)
2633 warnx("-T argument \"%s\" is unknown", optarg);
2635 goto ratecontrol_bailout;
2637 change_settings = 1;
2643 bus_width = strtol(optarg, NULL, 0);
2644 if (bus_width < 0) {
2645 warnx("bus width %d is < 0", bus_width);
2647 goto ratecontrol_bailout;
2649 change_settings = 1;
2656 bzero(&(&ccb->ccb_h)[1],
2657 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2660 * Grab path inquiry information, so we can determine whether
2661 * or not the initiator is capable of the things that the user
2664 ccb->ccb_h.func_code = XPT_PATH_INQ;
2666 if (cam_send_ccb(device, ccb) < 0) {
2667 perror("error sending XPT_PATH_INQ CCB");
2669 goto ratecontrol_bailout;
2672 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2673 warnx("XPT_PATH_INQ CCB failed, status %#x",
2676 goto ratecontrol_bailout;
2679 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2681 bzero(&(&ccb->ccb_h)[1],
2682 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2685 fprintf(stdout, "Current Parameters:\n");
2687 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2690 goto ratecontrol_bailout;
2692 if (arglist & CAM_ARG_VERBOSE)
2695 if (change_settings) {
2696 if (disc_enable != -1) {
2697 ccb->cts.valid |= CCB_TRANS_DISC_VALID;
2698 if (disc_enable == 0)
2699 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB;
2701 ccb->cts.flags |= CCB_TRANS_DISC_ENB;
2703 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID;
2705 if (tag_enable != -1) {
2706 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2707 warnx("HBA does not support tagged queueing, "
2708 "so you cannot modify tag settings");
2710 goto ratecontrol_bailout;
2713 ccb->cts.valid |= CCB_TRANS_TQ_VALID;
2715 if (tag_enable == 0)
2716 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB;
2718 ccb->cts.flags |= CCB_TRANS_TAG_ENB;
2720 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID;
2723 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2724 warnx("HBA at %s%d is not cable of changing "
2725 "offset", cpi.dev_name,
2728 goto ratecontrol_bailout;
2730 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID;
2731 ccb->cts.sync_offset = offset;
2733 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID;
2735 if (syncrate != -1) {
2736 int prelim_sync_period;
2739 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2740 warnx("HBA at %s%d is not cable of changing "
2741 "transfer rates", cpi.dev_name,
2744 goto ratecontrol_bailout;
2747 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID;
2750 * The sync rate the user gives us is in MHz.
2751 * We need to translate it into KHz for this
2757 * Next, we calculate a "preliminary" sync period
2758 * in tenths of a nanosecond.
2761 prelim_sync_period = 0;
2763 prelim_sync_period = 10000000 / syncrate;
2765 ccb->cts.sync_period =
2766 scsi_calc_syncparam(prelim_sync_period);
2768 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
2770 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID;
2773 * The bus_width argument goes like this:
2777 * Therefore, if you shift the number of bits given on the
2778 * command line right by 4, you should get the correct
2781 if (bus_width != -1) {
2784 * We might as well validate things here with a
2785 * decipherable error message, rather than what
2786 * will probably be an indecipherable error message
2787 * by the time it gets back to us.
2789 if ((bus_width == 16)
2790 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2791 warnx("HBA does not support 16 bit bus width");
2793 goto ratecontrol_bailout;
2794 } else if ((bus_width == 32)
2795 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2796 warnx("HBA does not support 32 bit bus width");
2798 goto ratecontrol_bailout;
2799 } else if ((bus_width != 8)
2800 && (bus_width != 16)
2801 && (bus_width != 32)) {
2802 warnx("Invalid bus width %d", bus_width);
2804 goto ratecontrol_bailout;
2807 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2808 ccb->cts.bus_width = bus_width >> 4;
2810 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID;
2812 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2814 if (cam_send_ccb(device, ccb) < 0) {
2815 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2817 goto ratecontrol_bailout;
2820 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2821 warnx("XPT_SET_TRANS_SETTINGS CCB failed, status %#x",
2824 goto ratecontrol_bailout;
2829 retval = testunitready(device, retry_count, timeout,
2830 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2833 * If the TUR didn't succeed, just bail.
2837 fprintf(stderr, "Test Unit Ready failed\n");
2838 goto ratecontrol_bailout;
2842 * If the user wants things quiet, there's no sense in
2843 * getting the transfer settings, if we're not going
2847 goto ratecontrol_bailout;
2849 fprintf(stdout, "New Parameters:\n");
2850 retval = get_print_cts(device, user_settings, 0, NULL);
2853 ratecontrol_bailout:
2860 scsiformat(struct cam_device *device, int argc, char **argv,
2861 char *combinedopt, int retry_count, int timeout)
2865 int ycount = 0, quiet = 0;
2866 int error = 0, response = 0, retval = 0;
2867 int use_timeout = 10800 * 1000;
2869 struct format_defect_list_header fh;
2870 u_int8_t *data_ptr = NULL;
2871 u_int32_t dxfer_len = 0;
2873 int num_warnings = 0;
2875 ccb = cam_getccb(device);
2878 warnx("scsiformat: error allocating ccb");
2882 bzero(&(&ccb->ccb_h)[1],
2883 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2885 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2900 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2901 "following device:\n");
2903 error = scsidoinquiry(device, argc, argv, combinedopt,
2904 retry_count, timeout);
2907 warnx("scsiformat: error sending inquiry");
2908 goto scsiformat_bailout;
2917 fprintf(stdout, "Are you SURE you want to do "
2920 if (fgets(str, sizeof(str), stdin) != NULL) {
2922 if (strncasecmp(str, "yes", 3) == 0)
2924 else if (strncasecmp(str, "no", 2) == 0)
2927 fprintf(stdout, "Please answer"
2928 " \"yes\" or \"no\"\n");
2931 } while (response == 0);
2933 if (response == -1) {
2935 goto scsiformat_bailout;
2940 use_timeout = timeout;
2943 fprintf(stdout, "Current format timeout is %d seconds\n",
2944 use_timeout / 1000);
2948 * If the user hasn't disabled questions and didn't specify a
2949 * timeout on the command line, ask them if they want the current
2953 && (timeout == 0)) {
2955 int new_timeout = 0;
2957 fprintf(stdout, "Enter new timeout in seconds or press\n"
2958 "return to keep the current timeout [%d] ",
2959 use_timeout / 1000);
2961 if (fgets(str, sizeof(str), stdin) != NULL) {
2963 new_timeout = atoi(str);
2966 if (new_timeout != 0) {
2967 use_timeout = new_timeout * 1000;
2968 fprintf(stdout, "Using new timeout value %d\n",
2969 use_timeout / 1000);
2974 * Keep this outside the if block below to silence any unused
2975 * variable warnings.
2977 bzero(&fh, sizeof(fh));
2980 * If we're in immediate mode, we've got to include the format
2983 if (immediate != 0) {
2984 fh.byte2 = FU_DLH_IMMED;
2985 data_ptr = (u_int8_t *)&fh;
2986 dxfer_len = sizeof(fh);
2987 byte2 = FU_FMT_DATA;
2988 } else if (quiet == 0) {
2989 fprintf(stdout, "Formatting...");
2993 scsi_format_unit(&ccb->csio,
2994 /* retries */ retry_count,
2996 /* tag_action */ MSG_SIMPLE_Q_TAG,
2999 /* data_ptr */ data_ptr,
3000 /* dxfer_len */ dxfer_len,
3001 /* sense_len */ SSD_FULL_SIZE,
3002 /* timeout */ use_timeout);
3004 /* Disable freezing the device queue */
3005 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3007 if (arglist & CAM_ARG_ERR_RECOVER)
3008 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3010 if (((retval = cam_send_ccb(device, ccb)) < 0)
3011 || ((immediate == 0)
3012 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3013 const char errstr[] = "error sending format command";
3020 if (arglist & CAM_ARG_VERBOSE) {
3021 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3022 CAM_SCSI_STATUS_ERROR)
3023 scsi_sense_print(device, &ccb->csio, stderr);
3025 fprintf(stderr, "CAM status is %#x\n",
3029 goto scsiformat_bailout;
3033 * If we ran in non-immediate mode, we already checked for errors
3034 * above and printed out any necessary information. If we're in
3035 * immediate mode, we need to loop through and get status
3036 * information periodically.
3038 if (immediate == 0) {
3040 fprintf(stdout, "Format Complete\n");
3042 goto scsiformat_bailout;
3048 bzero(&(&ccb->ccb_h)[1],
3049 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3052 * There's really no need to do error recovery or
3053 * retries here, since we're just going to sit in a
3054 * loop and wait for the device to finish formatting.
3056 scsi_test_unit_ready(&ccb->csio,
3059 /* tag_action */ MSG_SIMPLE_Q_TAG,
3060 /* sense_len */ SSD_FULL_SIZE,
3061 /* timeout */ 5000);
3063 /* Disable freezing the device queue */
3064 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3066 retval = cam_send_ccb(device, ccb);
3069 * If we get an error from the ioctl, bail out. SCSI
3070 * errors are expected.
3073 warn("error sending CAMIOCOMMAND ioctl");
3074 if (arglist & CAM_ARG_VERBOSE) {
3075 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
3076 CAM_SCSI_STATUS_ERROR)
3077 scsi_sense_print(device, &ccb->csio,
3080 fprintf(stderr, "CAM status is %#x\n",
3084 goto scsiformat_bailout;
3087 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3089 if ((status != CAM_REQ_CMP)
3090 && (status == CAM_SCSI_STATUS_ERROR)) {
3091 struct scsi_sense_data *sense;
3092 int error_code, sense_key, asc, ascq;
3094 sense = &ccb->csio.sense_data;
3095 scsi_extract_sense(sense, &error_code, &sense_key,
3099 * According to the SCSI-2 and SCSI-3 specs, a
3100 * drive that is in the middle of a format should
3101 * return NOT READY with an ASC of "logical unit
3102 * not ready, format in progress". The sense key
3103 * specific bytes will then be a progress indicator.
3105 if ((sense_key == SSD_KEY_NOT_READY)
3106 && (asc == 0x04) && (ascq == 0x04)) {
3107 if ((sense->extra_len >= 10)
3108 && ((sense->sense_key_spec[0] &
3109 SSD_SCS_VALID) != 0)
3112 u_int64_t percentage;
3115 &sense->sense_key_spec[1]);
3116 percentage = 10000 * val;
3119 "\rFormatting: %qd.%02qd %% "
3121 percentage / (0x10000 * 100),
3122 (percentage / 0x10000) % 100,
3125 } else if ((quiet == 0)
3126 && (++num_warnings <= 1)) {
3127 warnx("Unexpected SCSI Sense Key "
3128 "Specific value returned "
3130 scsi_sense_print(device, &ccb->csio,
3132 warnx("Unable to print status "
3133 "information, but format will "
3135 warnx("will exit when format is "
3140 warnx("Unexpected SCSI error during format");
3141 scsi_sense_print(device, &ccb->csio, stderr);
3143 goto scsiformat_bailout;
3146 } else if (status != CAM_REQ_CMP) {
3147 warnx("Unexpected CAM status %#x", status);
3149 goto scsiformat_bailout;
3152 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3155 fprintf(stdout, "\nFormat Complete\n");
3163 #endif /* MINIMALISTIC */
3168 fprintf(verbose ? stdout : stderr,
3169 "usage: camcontrol <command> [device id][generic args][command args]\n"
3170 " camcontrol devlist [-v]\n"
3171 #ifndef MINIMALISTIC
3172 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3173 " camcontrol tur [dev_id][generic args]\n"
3174 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3175 " camcontrol start [dev_id][generic args]\n"
3176 " camcontrol stop [dev_id][generic args]\n"
3177 " camcontrol load [dev_id][generic args]\n"
3178 " camcontrol eject [dev_id][generic args]\n"
3179 #endif /* MINIMALISTIC */
3180 " camcontrol rescan <all | bus[:target:lun]>\n"
3181 " camcontrol reset <all | bus[:target:lun]>\n"
3182 #ifndef MINIMALISTIC
3183 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3184 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3185 " [-P pagectl][-e | -b][-d]\n"
3186 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3187 " [-i len fmt|-o len fmt [args]]\n"
3188 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3189 " <all|bus[:target[:lun]]|off>\n"
3190 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3191 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3192 " [-D <enable|disable>][-O offset][-q]\n"
3193 " [-R syncrate][-v][-T <enable|disable>]\n"
3194 " [-U][-W bus_width]\n"
3195 " camcontrol format [dev_id][generic args][-q][-w][-y]\n"
3196 #endif /* MINIMALISTIC */
3197 " camcontrol help\n");
3200 #ifndef MINIMALISTIC
3202 "Specify one of the following options:\n"
3203 "devlist list all CAM devices\n"
3204 "periphlist list all CAM peripheral drivers attached to a device\n"
3205 "tur send a test unit ready to the named device\n"
3206 "inquiry send a SCSI inquiry command to the named device\n"
3207 "start send a Start Unit command to the device\n"
3208 "stop send a Stop Unit command to the device\n"
3209 "load send a Start Unit command to the device with the load bit set\n"
3210 "eject send a Stop Unit command to the device with the eject bit set\n"
3211 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3212 "reset reset all busses, the given bus, or bus:target:lun\n"
3213 "defects read the defect list of the specified device\n"
3214 "modepage display or edit (-e) the given mode page\n"
3215 "cmd send the given scsi command, may need -i or -o as well\n"
3216 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3217 "tags report or set the number of transaction slots for a device\n"
3218 "negotiate report or set device negotiation parameters\n"
3219 "format send the SCSI FORMAT UNIT command to the named device\n"
3220 "help this message\n"
3221 "Device Identifiers:\n"
3222 "bus:target specify the bus and target, lun defaults to 0\n"
3223 "bus:target:lun specify the bus, target and lun\n"
3224 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3225 "Generic arguments:\n"
3226 "-v be verbose, print out sense information\n"
3227 "-t timeout command timeout in seconds, overrides default timeout\n"
3228 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3229 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3230 "-E have the kernel attempt to perform SCSI error recovery\n"
3231 "-C count specify the SCSI command retry count (needs -E to work)\n"
3232 "modepage arguments:\n"
3233 "-l list all available mode pages\n"
3234 "-m page specify the mode page to view or edit\n"
3235 "-e edit the specified mode page\n"
3236 "-b force view to binary mode\n"
3237 "-d disable block descriptors for mode sense\n"
3238 "-P pgctl page control field 0-3\n"
3239 "defects arguments:\n"
3240 "-f format specify defect list format (block, bfi or phys)\n"
3241 "-G get the grown defect list\n"
3242 "-P get the permanant defect list\n"
3243 "inquiry arguments:\n"
3244 "-D get the standard inquiry data\n"
3245 "-S get the serial number\n"
3246 "-R get the transfer rate, etc.\n"
3248 "-c cdb [args] specify the SCSI CDB\n"
3249 "-i len fmt specify input data and input data format\n"
3250 "-o len fmt [args] specify output data and output data fmt\n"
3251 "debug arguments:\n"
3252 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3253 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3254 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3255 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3257 "-N tags specify the number of tags to use for this device\n"
3258 "-q be quiet, don't report the number of tags\n"
3259 "-v report a number of tag-related parameters\n"
3260 "negotiate arguments:\n"
3261 "-a send a test unit ready after negotiation\n"
3262 "-c report/set current negotiation settings\n"
3263 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3264 "-O offset set command delay offset\n"
3265 "-q be quiet, don't report anything\n"
3266 "-R syncrate synchronization rate in MHz\n"
3267 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3268 "-U report/set user negotiation settings\n"
3269 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3270 "-v also print a Path Inquiry CCB for the controller\n"
3271 "format arguments:\n"
3272 "-q be quiet, don't print status messages\n"
3273 "-w don't send immediate format command\n"
3274 "-y don't ask any questions\n");
3275 #endif /* MINIMALISTIC */
3279 main(int argc, char **argv)
3282 char *device = NULL;
3284 struct cam_device *cam_dev = NULL;
3285 int timeout = 0, retry_count = 1;
3286 camcontrol_optret optreturn;
3288 char *mainopt = "C:En:t:u:v";
3289 char *subopt = NULL;
3290 char combinedopt[256];
3291 int error = 0, optstart = 2;
3294 cmdlist = CAM_CMD_NONE;
3295 arglist = CAM_ARG_NONE;
3303 * Get the base option.
3305 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3307 if (optreturn == CC_OR_AMBIGUOUS) {
3308 warnx("ambiguous option %s", argv[1]);
3311 } else if (optreturn == CC_OR_NOT_FOUND) {
3312 warnx("option %s not found", argv[1]);
3318 * Ahh, getopt(3) is a pain.
3320 * This is a gross hack. There really aren't many other good
3321 * options (excuse the pun) for parsing options in a situation like
3322 * this. getopt is kinda braindead, so you end up having to run
3323 * through the options twice, and give each invocation of getopt
3324 * the option string for the other invocation.
3326 * You would think that you could just have two groups of options.
3327 * The first group would get parsed by the first invocation of
3328 * getopt, and the second group would get parsed by the second
3329 * invocation of getopt. It doesn't quite work out that way. When
3330 * the first invocation of getopt finishes, it leaves optind pointing
3331 * to the argument _after_ the first argument in the second group.
3332 * So when the second invocation of getopt comes around, it doesn't
3333 * recognize the first argument it gets and then bails out.
3335 * A nice alternative would be to have a flag for getopt that says
3336 * "just keep parsing arguments even when you encounter an unknown
3337 * argument", but there isn't one. So there's no real clean way to
3338 * easily parse two sets of arguments without having one invocation
3339 * of getopt know about the other.
3341 * Without this hack, the first invocation of getopt would work as
3342 * long as the generic arguments are first, but the second invocation
3343 * (in the subfunction) would fail in one of two ways. In the case
3344 * where you don't set optreset, it would fail because optind may be
3345 * pointing to the argument after the one it should be pointing at.
3346 * In the case where you do set optreset, and reset optind, it would
3347 * fail because getopt would run into the first set of options, which
3348 * it doesn't understand.
3350 * All of this would "sort of" work if you could somehow figure out
3351 * whether optind had been incremented one option too far. The
3352 * mechanics of that, however, are more daunting than just giving
3353 * both invocations all of the expect options for either invocation.
3355 * Needless to say, I wouldn't mind if someone invented a better
3356 * (non-GPL!) command line parsing interface than getopt. I
3357 * wouldn't mind if someone added more knobs to getopt to make it
3358 * work better. Who knows, I may talk myself into doing it someday,
3359 * if the standards weenies let me. As it is, it just leads to
3360 * hackery like this and causes people to avoid it in some cases.
3362 * KDM, September 8th, 1998
3365 sprintf(combinedopt, "%s%s", mainopt, subopt);
3367 sprintf(combinedopt, "%s", mainopt);
3370 * For these options we do not parse optional device arguments and
3371 * we do not open a passthrough device.
3373 if ((cmdlist == CAM_CMD_RESCAN)
3374 || (cmdlist == CAM_CMD_RESET)
3375 || (cmdlist == CAM_CMD_DEVTREE)
3376 || (cmdlist == CAM_CMD_USAGE)
3377 || (cmdlist == CAM_CMD_DEBUG))
3380 #ifndef MINIMALISTIC
3382 && (argc > 2 && argv[2][0] != '-')) {
3387 * First catch people who try to do things like:
3388 * camcontrol tur /dev/da0
3389 * camcontrol doesn't take device nodes as arguments.
3391 if (argv[2][0] == '/') {
3392 warnx("%s is not a valid device identifier", argv[2]);
3393 errx(1, "please read the camcontrol(8) man page");
3394 } else if (isdigit(argv[2][0])) {
3395 /* device specified as bus:target[:lun] */
3396 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3398 errx(1, "numeric device specification must "
3399 "be either bus:target, or "
3403 if (cam_get_device(argv[2], name, sizeof name, &unit)
3405 errx(1, "%s", cam_errbuf);
3406 device = strdup(name);
3407 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3411 #endif /* MINIMALISTIC */
3413 * Start getopt processing at argv[2/3], since we've already
3414 * accepted argv[1..2] as the command name, and as a possible
3420 * Now we run through the argument list looking for generic
3421 * options, and ignoring options that possibly belong to
3424 while ((c = getopt(argc, argv, combinedopt))!= -1){
3427 retry_count = strtol(optarg, NULL, 0);
3428 if (retry_count < 0)
3429 errx(1, "retry count %d is < 0",
3431 arglist |= CAM_ARG_RETRIES;
3434 arglist |= CAM_ARG_ERR_RECOVER;
3437 arglist |= CAM_ARG_DEVICE;
3439 while (isspace(*tstr) && (*tstr != '\0'))
3441 device = (char *)strdup(tstr);
3444 timeout = strtol(optarg, NULL, 0);
3446 errx(1, "invalid timeout %d", timeout);
3447 /* Convert the timeout from seconds to ms */
3449 arglist |= CAM_ARG_TIMEOUT;
3452 arglist |= CAM_ARG_UNIT;
3453 unit = strtol(optarg, NULL, 0);
3456 arglist |= CAM_ARG_VERBOSE;
3463 #ifndef MINIMALISTIC
3465 * For most commands we'll want to open the passthrough device
3466 * associated with the specified device. In the case of the rescan
3467 * commands, we don't use a passthrough device at all, just the
3468 * transport layer device.
3471 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3472 && (((arglist & CAM_ARG_DEVICE) == 0)
3473 || ((arglist & CAM_ARG_UNIT) == 0))) {
3474 errx(1, "subcommand \"%s\" requires a valid device "
3475 "identifier", argv[1]);
3478 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3479 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3480 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3482 errx(1,"%s", cam_errbuf);
3484 #endif /* MINIMALISTIC */
3487 * Reset optind to 2, and reset getopt, so these routines can parse
3488 * the arguments again.
3494 #ifndef MINIMALISTIC
3495 case CAM_CMD_DEVLIST:
3496 error = getdevlist(cam_dev);
3498 #endif /* MINIMALISTIC */
3499 case CAM_CMD_DEVTREE:
3500 error = getdevtree();
3502 #ifndef MINIMALISTIC
3504 error = testunitready(cam_dev, retry_count, timeout, 0);
3506 case CAM_CMD_INQUIRY:
3507 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3508 retry_count, timeout);
3510 case CAM_CMD_STARTSTOP:
3511 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3512 arglist & CAM_ARG_EJECT, retry_count,
3515 #endif /* MINIMALISTIC */
3516 case CAM_CMD_RESCAN:
3517 error = dorescan_or_reset(argc, argv, 1);
3520 error = dorescan_or_reset(argc, argv, 0);
3522 #ifndef MINIMALISTIC
3523 case CAM_CMD_READ_DEFECTS:
3524 error = readdefects(cam_dev, argc, argv, combinedopt,
3525 retry_count, timeout);
3527 case CAM_CMD_MODE_PAGE:
3528 modepage(cam_dev, argc, argv, combinedopt,
3529 retry_count, timeout);
3531 case CAM_CMD_SCSI_CMD:
3532 error = scsicmd(cam_dev, argc, argv, combinedopt,
3533 retry_count, timeout);
3536 error = camdebug(argc, argv, combinedopt);
3539 error = tagcontrol(cam_dev, argc, argv, combinedopt);
3542 error = ratecontrol(cam_dev, retry_count, timeout,
3543 argc, argv, combinedopt);
3545 case CAM_CMD_FORMAT:
3546 error = scsiformat(cam_dev, argc, argv,
3547 combinedopt, retry_count, timeout);
3549 #endif /* MINIMALISTIC */
3559 if (cam_dev != NULL)
3560 cam_close_device(cam_dev);