2 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/ioctl.h>
33 #include <sys/stdint.h>
34 #include <sys/types.h>
45 #include <cam/cam_debug.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/scsi/scsi_all.h>
48 #include <cam/scsi/scsi_da.h>
49 #include <cam/scsi/scsi_pass.h>
50 #include <cam/scsi/scsi_message.h>
52 #include "camcontrol.h"
55 CAM_CMD_NONE = 0x00000000,
56 CAM_CMD_DEVLIST = 0x00000001,
57 CAM_CMD_TUR = 0x00000002,
58 CAM_CMD_INQUIRY = 0x00000003,
59 CAM_CMD_STARTSTOP = 0x00000004,
60 CAM_CMD_RESCAN = 0x00000005,
61 CAM_CMD_READ_DEFECTS = 0x00000006,
62 CAM_CMD_MODE_PAGE = 0x00000007,
63 CAM_CMD_SCSI_CMD = 0x00000008,
64 CAM_CMD_DEVTREE = 0x00000009,
65 CAM_CMD_USAGE = 0x0000000a,
66 CAM_CMD_DEBUG = 0x0000000b,
67 CAM_CMD_RESET = 0x0000000c,
68 CAM_CMD_FORMAT = 0x0000000d,
69 CAM_CMD_TAG = 0x0000000e,
70 CAM_CMD_RATE = 0x0000000f,
71 CAM_CMD_DETACH = 0x00000010,
72 CAM_CMD_REPORTLUNS = 0x00000011
76 CAM_ARG_NONE = 0x00000000,
77 CAM_ARG_VERBOSE = 0x00000001,
78 CAM_ARG_DEVICE = 0x00000002,
79 CAM_ARG_BUS = 0x00000004,
80 CAM_ARG_TARGET = 0x00000008,
81 CAM_ARG_LUN = 0x00000010,
82 CAM_ARG_EJECT = 0x00000020,
83 CAM_ARG_UNIT = 0x00000040,
84 CAM_ARG_FORMAT_BLOCK = 0x00000080,
85 CAM_ARG_FORMAT_BFI = 0x00000100,
86 CAM_ARG_FORMAT_PHYS = 0x00000200,
87 CAM_ARG_PLIST = 0x00000400,
88 CAM_ARG_GLIST = 0x00000800,
89 CAM_ARG_GET_SERIAL = 0x00001000,
90 CAM_ARG_GET_STDINQ = 0x00002000,
91 CAM_ARG_GET_XFERRATE = 0x00004000,
92 CAM_ARG_INQ_MASK = 0x00007000,
93 CAM_ARG_MODE_EDIT = 0x00008000,
94 CAM_ARG_PAGE_CNTL = 0x00010000,
95 CAM_ARG_TIMEOUT = 0x00020000,
96 CAM_ARG_CMD_IN = 0x00040000,
97 CAM_ARG_CMD_OUT = 0x00080000,
98 CAM_ARG_DBD = 0x00100000,
99 CAM_ARG_ERR_RECOVER = 0x00200000,
100 CAM_ARG_RETRIES = 0x00400000,
101 CAM_ARG_START_UNIT = 0x00800000,
102 CAM_ARG_DEBUG_INFO = 0x01000000,
103 CAM_ARG_DEBUG_TRACE = 0x02000000,
104 CAM_ARG_DEBUG_SUBTRACE = 0x04000000,
105 CAM_ARG_DEBUG_CDB = 0x08000000,
106 CAM_ARG_DEBUG_XPT = 0x10000000,
107 CAM_ARG_DEBUG_PERIPH = 0x20000000,
110 struct camcontrol_opts {
118 static const char scsicmd_opts[] = "c:i:o:";
119 static const char readdefect_opts[] = "f:GP";
120 static const char negotiate_opts[] = "acD:O:qR:T:UW:";
123 struct camcontrol_opts option_table[] = {
125 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
126 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
127 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
128 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
129 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
130 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
131 {"reportluns", CAM_CMD_REPORTLUNS, CAM_ARG_NONE, "clr:"},
132 #endif /* MINIMALISTIC */
133 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
134 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
136 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
137 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
138 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
139 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
140 #endif /* MINIMALISTIC */
141 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
143 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
144 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
145 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
146 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
147 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
148 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
149 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
150 #endif /* MINIMALISTIC */
151 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
152 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
153 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
167 camcontrol_optret getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
168 const char **subopt);
170 static int getdevlist(struct cam_device *device);
171 #endif /* MINIMALISTIC */
172 static int getdevtree(void);
174 static int testunitready(struct cam_device *device, int retry_count,
175 int timeout, int quiet);
176 static int scsistart(struct cam_device *device, int startstop, int loadeject,
177 int retry_count, int timeout);
178 static int scsidoinquiry(struct cam_device *device, int argc, char **argv,
179 char *combinedopt, int retry_count, int timeout);
180 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout);
181 static int scsiserial(struct cam_device *device, int retry_count, int timeout);
182 static int scsixferrate(struct cam_device *device);
183 #endif /* MINIMALISTIC */
184 static int parse_btl(char *tstr, int *bus, int *target, int *lun,
185 cam_argmask *arglst);
186 static int dorescan_or_reset(int argc, char **argv, int rescan);
187 static int rescan_or_reset_bus(int bus, int rescan);
188 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan);
190 static int readdefects(struct cam_device *device, int argc, char **argv,
191 char *combinedopt, int retry_count, int timeout);
192 static void modepage(struct cam_device *device, int argc, char **argv,
193 char *combinedopt, int retry_count, int timeout);
194 static int scsicmd(struct cam_device *device, int argc, char **argv,
195 char *combinedopt, int retry_count, int timeout);
196 static int tagcontrol(struct cam_device *device, int argc, char **argv,
198 static void cts_print(struct cam_device *device,
199 struct ccb_trans_settings *cts);
200 static void cpi_print(struct ccb_pathinq *cpi);
201 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
202 static int get_print_cts(struct cam_device *device, int user_settings,
203 int quiet, struct ccb_trans_settings *cts);
204 static int ratecontrol(struct cam_device *device, int retry_count,
205 int timeout, int argc, char **argv, char *combinedopt);
206 static int scsiformat(struct cam_device *device, int argc, char **argv,
207 char *combinedopt, int retry_count, int timeout);
208 static int scsireportluns(struct cam_device *device, int argc, char **argv,
209 char *combinedopt, int retry_count, int timeout);
210 #endif /* MINIMALISTIC */
213 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
216 struct camcontrol_opts *opts;
219 for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
221 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
222 *cmdnum = opts->cmdnum;
223 *argnum = opts->argnum;
224 *subopt = opts->subopt;
225 if (++num_matches > 1)
226 return(CC_OR_AMBIGUOUS);
233 return(CC_OR_NOT_FOUND);
238 getdevlist(struct cam_device *device)
244 ccb = cam_getccb(device);
246 ccb->ccb_h.func_code = XPT_GDEVLIST;
247 ccb->ccb_h.flags = CAM_DIR_NONE;
248 ccb->ccb_h.retry_count = 1;
250 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
251 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
252 if (cam_send_ccb(device, ccb) < 0) {
253 perror("error getting device list");
260 switch (ccb->cgdl.status) {
261 case CAM_GDEVLIST_MORE_DEVS:
262 strcpy(status, "MORE");
264 case CAM_GDEVLIST_LAST_DEVICE:
265 strcpy(status, "LAST");
267 case CAM_GDEVLIST_LIST_CHANGED:
268 strcpy(status, "CHANGED");
270 case CAM_GDEVLIST_ERROR:
271 strcpy(status, "ERROR");
276 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
277 ccb->cgdl.periph_name,
278 ccb->cgdl.unit_number,
279 ccb->cgdl.generation,
284 * If the list has changed, we need to start over from the
287 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
295 #endif /* MINIMALISTIC */
307 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
308 warn("couldn't open %s", XPT_DEVICE);
312 bzero(&ccb, sizeof(union ccb));
314 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
315 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
316 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
318 ccb.ccb_h.func_code = XPT_DEV_MATCH;
319 bufsize = sizeof(struct dev_match_result) * 100;
320 ccb.cdm.match_buf_len = bufsize;
321 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
322 if (ccb.cdm.matches == NULL) {
323 warnx("can't malloc memory for matches");
327 ccb.cdm.num_matches = 0;
330 * We fetch all nodes, since we display most of them in the default
331 * case, and all in the verbose case.
333 ccb.cdm.num_patterns = 0;
334 ccb.cdm.pattern_buf_len = 0;
337 * We do the ioctl multiple times if necessary, in case there are
338 * more than 100 nodes in the EDT.
341 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
342 warn("error sending CAMIOCOMMAND ioctl");
347 if ((ccb.ccb_h.status != CAM_REQ_CMP)
348 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
349 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
350 warnx("got CAM error %#x, CDM error %d\n",
351 ccb.ccb_h.status, ccb.cdm.status);
356 for (i = 0; i < ccb.cdm.num_matches; i++) {
357 switch (ccb.cdm.matches[i].type) {
358 case DEV_MATCH_BUS: {
359 struct bus_match_result *bus_result;
362 * Only print the bus information if the
363 * user turns on the verbose flag.
365 if ((arglist & CAM_ARG_VERBOSE) == 0)
369 &ccb.cdm.matches[i].result.bus_result;
372 fprintf(stdout, ")\n");
376 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
378 bus_result->dev_name,
379 bus_result->unit_number,
383 case DEV_MATCH_DEVICE: {
384 struct device_match_result *dev_result;
385 char vendor[16], product[48], revision[16];
389 &ccb.cdm.matches[i].result.device_result;
391 if ((dev_result->flags
392 & DEV_RESULT_UNCONFIGURED)
393 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
399 cam_strvis(vendor, dev_result->inq_data.vendor,
400 sizeof(dev_result->inq_data.vendor),
403 dev_result->inq_data.product,
404 sizeof(dev_result->inq_data.product),
407 dev_result->inq_data.revision,
408 sizeof(dev_result->inq_data.revision),
410 sprintf(tmpstr, "<%s %s %s>", vendor, product,
413 fprintf(stdout, ")\n");
417 fprintf(stdout, "%-33s at scbus%d "
418 "target %d lun %d (",
421 dev_result->target_id,
422 dev_result->target_lun);
428 case DEV_MATCH_PERIPH: {
429 struct periph_match_result *periph_result;
432 &ccb.cdm.matches[i].result.periph_result;
434 if (skip_device != 0)
438 fprintf(stdout, ",");
440 fprintf(stdout, "%s%d",
441 periph_result->periph_name,
442 periph_result->unit_number);
448 fprintf(stdout, "unknown match type\n");
453 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
454 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
457 fprintf(stdout, ")\n");
466 testunitready(struct cam_device *device, int retry_count, int timeout,
472 ccb = cam_getccb(device);
474 scsi_test_unit_ready(&ccb->csio,
475 /* retries */ retry_count,
477 /* tag_action */ MSG_SIMPLE_Q_TAG,
478 /* sense_len */ SSD_FULL_SIZE,
479 /* timeout */ timeout ? timeout : 5000);
481 /* Disable freezing the device queue */
482 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
484 if (arglist & CAM_ARG_ERR_RECOVER)
485 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
487 if (cam_send_ccb(device, ccb) < 0) {
489 perror("error sending test unit ready");
491 if (arglist & CAM_ARG_VERBOSE) {
492 cam_error_print(device, ccb, CAM_ESF_ALL,
493 CAM_EPF_ALL, stderr);
500 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
502 fprintf(stdout, "Unit is ready\n");
505 fprintf(stdout, "Unit is not ready\n");
508 if (arglist & CAM_ARG_VERBOSE) {
509 cam_error_print(device, ccb, CAM_ESF_ALL,
510 CAM_EPF_ALL, stderr);
520 scsistart(struct cam_device *device, int startstop, int loadeject,
521 int retry_count, int timeout)
526 ccb = cam_getccb(device);
529 * If we're stopping, send an ordered tag so the drive in question
530 * will finish any previously queued writes before stopping. If
531 * the device isn't capable of tagged queueing, or if tagged
532 * queueing is turned off, the tag action is a no-op.
534 scsi_start_stop(&ccb->csio,
535 /* retries */ retry_count,
537 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
539 /* start/stop */ startstop,
540 /* load_eject */ loadeject,
542 /* sense_len */ SSD_FULL_SIZE,
543 /* timeout */ timeout ? timeout : 120000);
545 /* Disable freezing the device queue */
546 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
548 if (arglist & CAM_ARG_ERR_RECOVER)
549 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
551 if (cam_send_ccb(device, ccb) < 0) {
552 perror("error sending start unit");
554 if (arglist & CAM_ARG_VERBOSE) {
555 cam_error_print(device, ccb, CAM_ESF_ALL,
556 CAM_EPF_ALL, stderr);
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 cam_error_print(device, ccb, CAM_ESF_ALL,
588 CAM_EPF_ALL, stderr);
598 scsidoinquiry(struct cam_device *device, int argc, char **argv,
599 char *combinedopt, int retry_count, int timeout)
604 while ((c = getopt(argc, argv, combinedopt)) != -1) {
607 arglist |= CAM_ARG_GET_STDINQ;
610 arglist |= CAM_ARG_GET_XFERRATE;
613 arglist |= CAM_ARG_GET_SERIAL;
621 * If the user didn't specify any inquiry options, he wants all of
624 if ((arglist & CAM_ARG_INQ_MASK) == 0)
625 arglist |= CAM_ARG_INQ_MASK;
627 if (arglist & CAM_ARG_GET_STDINQ)
628 error = scsiinquiry(device, retry_count, timeout);
633 if (arglist & CAM_ARG_GET_SERIAL)
634 scsiserial(device, retry_count, timeout);
639 if (arglist & CAM_ARG_GET_XFERRATE)
640 error = scsixferrate(device);
646 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
649 struct scsi_inquiry_data *inq_buf;
652 ccb = cam_getccb(device);
655 warnx("couldn't allocate CCB");
659 /* cam_getccb cleans up the header, caller has to zero the payload */
660 bzero(&(&ccb->ccb_h)[1],
661 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
663 inq_buf = (struct scsi_inquiry_data *)malloc(
664 sizeof(struct scsi_inquiry_data));
666 if (inq_buf == NULL) {
668 warnx("can't malloc memory for inquiry\n");
671 bzero(inq_buf, sizeof(*inq_buf));
674 * Note that although the size of the inquiry buffer is the full
675 * 256 bytes specified in the SCSI spec, we only tell the device
676 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
677 * two reasons for this:
679 * - The SCSI spec says that when a length field is only 1 byte,
680 * a value of 0 will be interpreted as 256. Therefore
681 * scsi_inquiry() will convert an inq_len (which is passed in as
682 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
683 * to 0. Evidently, very few devices meet the spec in that
684 * regard. Some devices, like many Seagate disks, take the 0 as
685 * 0, and don't return any data. One Pioneer DVD-R drive
686 * returns more data than the command asked for.
688 * So, since there are numerous devices that just don't work
689 * right with the full inquiry size, we don't send the full size.
691 * - The second reason not to use the full inquiry data length is
692 * that we don't need it here. The only reason we issue a
693 * standard inquiry is to get the vendor name, device name,
694 * and revision so scsi_print_inquiry() can print them.
696 * If, at some point in the future, more inquiry data is needed for
697 * some reason, this code should use a procedure similar to the
698 * probe code. i.e., issue a short inquiry, and determine from
699 * the additional length passed back from the device how much
700 * inquiry data the device supports. Once the amount the device
701 * supports is determined, issue an inquiry for that amount and no
706 scsi_inquiry(&ccb->csio,
707 /* retries */ retry_count,
709 /* tag_action */ MSG_SIMPLE_Q_TAG,
710 /* inq_buf */ (u_int8_t *)inq_buf,
711 /* inq_len */ SHORT_INQUIRY_LENGTH,
714 /* sense_len */ SSD_FULL_SIZE,
715 /* timeout */ timeout ? timeout : 5000);
717 /* Disable freezing the device queue */
718 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
720 if (arglist & CAM_ARG_ERR_RECOVER)
721 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
723 if (cam_send_ccb(device, ccb) < 0) {
724 perror("error sending SCSI inquiry");
726 if (arglist & CAM_ARG_VERBOSE) {
727 cam_error_print(device, ccb, CAM_ESF_ALL,
728 CAM_EPF_ALL, stderr);
735 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
738 if (arglist & CAM_ARG_VERBOSE) {
739 cam_error_print(device, ccb, CAM_ESF_ALL,
740 CAM_EPF_ALL, stderr);
751 fprintf(stdout, "%s%d: ", device->device_name,
752 device->dev_unit_num);
753 scsi_print_inquiry(inq_buf);
761 scsiserial(struct cam_device *device, int retry_count, int timeout)
764 struct scsi_vpd_unit_serial_number *serial_buf;
765 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
768 ccb = cam_getccb(device);
771 warnx("couldn't allocate CCB");
775 /* cam_getccb cleans up the header, caller has to zero the payload */
776 bzero(&(&ccb->ccb_h)[1],
777 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
779 serial_buf = (struct scsi_vpd_unit_serial_number *)
780 malloc(sizeof(*serial_buf));
782 if (serial_buf == NULL) {
784 warnx("can't malloc memory for serial number");
788 scsi_inquiry(&ccb->csio,
789 /*retries*/ retry_count,
791 /* tag_action */ MSG_SIMPLE_Q_TAG,
792 /* inq_buf */ (u_int8_t *)serial_buf,
793 /* inq_len */ sizeof(*serial_buf),
795 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
796 /* sense_len */ SSD_FULL_SIZE,
797 /* timeout */ timeout ? timeout : 5000);
799 /* Disable freezing the device queue */
800 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
802 if (arglist & CAM_ARG_ERR_RECOVER)
803 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
805 if (cam_send_ccb(device, ccb) < 0) {
806 warn("error getting serial number");
808 if (arglist & CAM_ARG_VERBOSE) {
809 cam_error_print(device, ccb, CAM_ESF_ALL,
810 CAM_EPF_ALL, stderr);
818 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
821 if (arglist & CAM_ARG_VERBOSE) {
822 cam_error_print(device, ccb, CAM_ESF_ALL,
823 CAM_EPF_ALL, stderr);
834 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
835 serial_num[serial_buf->length] = '\0';
837 if ((arglist & CAM_ARG_GET_STDINQ)
838 || (arglist & CAM_ARG_GET_XFERRATE))
839 fprintf(stdout, "%s%d: Serial Number ",
840 device->device_name, device->dev_unit_num);
842 fprintf(stdout, "%.60s\n", serial_num);
850 scsixferrate(struct cam_device *device)
858 ccb = cam_getccb(device);
861 warnx("couldn't allocate CCB");
865 bzero(&(&ccb->ccb_h)[1],
866 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
868 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
869 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
871 if (((retval = cam_send_ccb(device, ccb)) < 0)
872 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
873 const char error_string[] = "error getting transfer settings";
880 if (arglist & CAM_ARG_VERBOSE)
881 cam_error_print(device, ccb, CAM_ESF_ALL,
882 CAM_EPF_ALL, stderr);
886 goto xferrate_bailout;
890 if (ccb->cts.transport == XPORT_SPI) {
891 struct ccb_trans_settings_spi *spi =
892 &ccb->cts.xport_specific.spi;
894 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
895 freq = scsi_calc_syncsrate(spi->sync_period);
899 fprintf(stdout, "%s%d: ", device->device_name,
900 device->dev_unit_num);
902 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
903 speed *= (0x01 << spi->bus_width);
909 fprintf(stdout, "%d.%03dMB/s transfers ",
912 fprintf(stdout, "%dKB/s transfers ",
915 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
916 && (spi->sync_offset != 0))
917 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
918 freq % 1000, spi->sync_offset);
920 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
921 && (spi->bus_width > 0)) {
922 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
923 && (spi->sync_offset != 0)) {
924 fprintf(stdout, ", ");
926 fprintf(stdout, " (");
928 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
929 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
930 && (spi->sync_offset != 0)) {
931 fprintf(stdout, ")");
934 struct ccb_pathinq cpi;
936 retval = get_cpi(device, &cpi);
939 goto xferrate_bailout;
941 speed = cpi.base_transfer_speed;
947 fprintf(stdout, "%d.%03dMB/s transfers ",
950 fprintf(stdout, "%dKB/s transfers ",
954 if (ccb->cts.protocol == PROTO_SCSI) {
955 struct ccb_trans_settings_scsi *scsi =
956 &ccb->cts.proto_specific.scsi;
957 if (scsi->valid & CTS_SCSI_VALID_TQ) {
958 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
959 fprintf(stdout, ", Command Queueing Enabled");
964 fprintf(stdout, "\n");
972 #endif /* MINIMALISTIC */
975 * Parse out a bus, or a bus, target and lun in the following
981 * Returns the number of parsed components, or 0.
984 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglst)
989 while (isspace(*tstr) && (*tstr != '\0'))
992 tmpstr = (char *)strtok(tstr, ":");
993 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
994 *bus = strtol(tmpstr, NULL, 0);
995 *arglst |= CAM_ARG_BUS;
997 tmpstr = (char *)strtok(NULL, ":");
998 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
999 *target = strtol(tmpstr, NULL, 0);
1000 *arglst |= CAM_ARG_TARGET;
1002 tmpstr = (char *)strtok(NULL, ":");
1003 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1004 *lun = strtol(tmpstr, NULL, 0);
1005 *arglst |= CAM_ARG_LUN;
1015 dorescan_or_reset(int argc, char **argv, int rescan)
1017 static const char must[] =
1018 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1020 int bus = -1, target = -1, lun = -1;
1024 warnx(must, rescan? "rescan" : "reset");
1028 tstr = argv[optind];
1029 while (isspace(*tstr) && (*tstr != '\0'))
1031 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1032 arglist |= CAM_ARG_BUS;
1034 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
1035 if (rv != 1 && rv != 3) {
1036 warnx(must, rescan? "rescan" : "reset");
1041 if ((arglist & CAM_ARG_BUS)
1042 && (arglist & CAM_ARG_TARGET)
1043 && (arglist & CAM_ARG_LUN))
1044 error = scanlun_or_reset_dev(bus, target, lun, rescan);
1046 error = rescan_or_reset_bus(bus, rescan);
1052 rescan_or_reset_bus(int bus, int rescan)
1054 union ccb ccb, matchccb;
1060 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1061 warnx("error opening transport layer device %s", XPT_DEVICE);
1062 warn("%s", XPT_DEVICE);
1067 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1068 ccb.ccb_h.path_id = bus;
1069 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1070 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1071 ccb.crcn.flags = CAM_FLAG_NONE;
1073 /* run this at a low priority */
1074 ccb.ccb_h.pinfo.priority = 5;
1076 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1077 warn("CAMIOCOMMAND ioctl failed");
1082 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1083 fprintf(stdout, "%s of bus %d was successful\n",
1084 rescan ? "Re-scan" : "Reset", bus);
1086 fprintf(stdout, "%s of bus %d returned error %#x\n",
1087 rescan ? "Re-scan" : "Reset", bus,
1088 ccb.ccb_h.status & CAM_STATUS_MASK);
1099 * The right way to handle this is to modify the xpt so that it can
1100 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1101 * that isn't implemented, so instead we enumerate the busses and
1102 * send the rescan or reset to those busses in the case where the
1103 * given bus is -1 (wildcard). We don't send a rescan or reset
1104 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1105 * no-op, sending a rescan to the xpt bus would result in a status of
1108 bzero(&(&matchccb.ccb_h)[1],
1109 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1110 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1111 bufsize = sizeof(struct dev_match_result) * 20;
1112 matchccb.cdm.match_buf_len = bufsize;
1113 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1114 if (matchccb.cdm.matches == NULL) {
1115 warnx("can't malloc memory for matches");
1119 matchccb.cdm.num_matches = 0;
1121 matchccb.cdm.num_patterns = 1;
1122 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1124 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1125 matchccb.cdm.pattern_buf_len);
1126 if (matchccb.cdm.patterns == NULL) {
1127 warnx("can't malloc memory for patterns");
1131 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1132 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1137 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1138 warn("CAMIOCOMMAND ioctl failed");
1143 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1144 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1145 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1146 warnx("got CAM error %#x, CDM error %d\n",
1147 matchccb.ccb_h.status, matchccb.cdm.status);
1152 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1153 struct bus_match_result *bus_result;
1155 /* This shouldn't happen. */
1156 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1159 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1162 * We don't want to rescan or reset the xpt bus.
1165 if ((int)bus_result->path_id == -1)
1168 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1170 ccb.ccb_h.path_id = bus_result->path_id;
1171 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1172 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1173 ccb.crcn.flags = CAM_FLAG_NONE;
1175 /* run this at a low priority */
1176 ccb.ccb_h.pinfo.priority = 5;
1178 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1179 warn("CAMIOCOMMAND ioctl failed");
1184 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1185 fprintf(stdout, "%s of bus %d was successful\n",
1186 rescan? "Re-scan" : "Reset",
1187 bus_result->path_id);
1190 * Don't bail out just yet, maybe the other
1191 * rescan or reset commands will complete
1194 fprintf(stderr, "%s of bus %d returned error "
1195 "%#x\n", rescan? "Re-scan" : "Reset",
1196 bus_result->path_id,
1197 ccb.ccb_h.status & CAM_STATUS_MASK);
1201 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1202 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1209 if (matchccb.cdm.patterns != NULL)
1210 free(matchccb.cdm.patterns);
1211 if (matchccb.cdm.matches != NULL)
1212 free(matchccb.cdm.matches);
1218 scanlun_or_reset_dev(int bus, int target, int lun, int scan)
1221 struct cam_device *device;
1227 warnx("invalid bus number %d", bus);
1232 warnx("invalid target number %d", target);
1237 warnx("invalid lun number %d", lun);
1243 bzero(&ccb, sizeof(union ccb));
1246 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1247 warnx("error opening transport layer device %s\n",
1249 warn("%s", XPT_DEVICE);
1253 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
1254 if (device == NULL) {
1255 warnx("%s", cam_errbuf);
1260 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1261 ccb.ccb_h.path_id = bus;
1262 ccb.ccb_h.target_id = target;
1263 ccb.ccb_h.target_lun = lun;
1264 ccb.ccb_h.timeout = 5000;
1265 ccb.crcn.flags = CAM_FLAG_NONE;
1267 /* run this at a low priority */
1268 ccb.ccb_h.pinfo.priority = 5;
1271 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1272 warn("CAMIOCOMMAND ioctl failed");
1277 if (cam_send_ccb(device, &ccb) < 0) {
1278 warn("error sending XPT_RESET_DEV CCB");
1279 cam_close_device(device);
1287 cam_close_device(device);
1290 * An error code of CAM_BDR_SENT is normal for a BDR request.
1292 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1294 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1295 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1296 scan? "Re-scan" : "Reset", bus, target, lun);
1299 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1300 scan? "Re-scan" : "Reset", bus, target, lun,
1301 ccb.ccb_h.status & CAM_STATUS_MASK);
1306 #ifndef MINIMALISTIC
1308 readdefects(struct cam_device *device, int argc, char **argv,
1309 char *combinedopt, int retry_count, int timeout)
1311 union ccb *ccb = NULL;
1312 struct scsi_read_defect_data_10 *rdd_cdb;
1313 u_int8_t *defect_list = NULL;
1314 u_int32_t dlist_length = 65000;
1315 u_int32_t returned_length = 0;
1316 u_int32_t num_returned = 0;
1317 u_int8_t returned_format;
1320 int lists_specified = 0;
1322 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1328 while (isspace(*tstr) && (*tstr != '\0'))
1330 if (strcmp(tstr, "block") == 0)
1331 arglist |= CAM_ARG_FORMAT_BLOCK;
1332 else if (strcmp(tstr, "bfi") == 0)
1333 arglist |= CAM_ARG_FORMAT_BFI;
1334 else if (strcmp(tstr, "phys") == 0)
1335 arglist |= CAM_ARG_FORMAT_PHYS;
1338 warnx("invalid defect format %s", tstr);
1339 goto defect_bailout;
1344 arglist |= CAM_ARG_GLIST;
1347 arglist |= CAM_ARG_PLIST;
1354 ccb = cam_getccb(device);
1357 * Hopefully 65000 bytes is enough to hold the defect list. If it
1358 * isn't, the disk is probably dead already. We'd have to go with
1359 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1362 defect_list = malloc(dlist_length);
1363 if (defect_list == NULL) {
1364 warnx("can't malloc memory for defect list");
1366 goto defect_bailout;
1369 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1372 * cam_getccb() zeros the CCB header only. So we need to zero the
1373 * payload portion of the ccb.
1375 bzero(&(&ccb->ccb_h)[1],
1376 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1378 cam_fill_csio(&ccb->csio,
1379 /*retries*/ retry_count,
1381 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1382 CAM_PASS_ERR_RECOVER : 0),
1383 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1384 /*data_ptr*/ defect_list,
1385 /*dxfer_len*/ dlist_length,
1386 /*sense_len*/ SSD_FULL_SIZE,
1387 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1388 /*timeout*/ timeout ? timeout : 5000);
1390 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1391 if (arglist & CAM_ARG_FORMAT_BLOCK)
1392 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1393 else if (arglist & CAM_ARG_FORMAT_BFI)
1394 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1395 else if (arglist & CAM_ARG_FORMAT_PHYS)
1396 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1399 warnx("no defect list format specified");
1400 goto defect_bailout;
1402 if (arglist & CAM_ARG_PLIST) {
1403 rdd_cdb->format |= SRDD10_PLIST;
1407 if (arglist & CAM_ARG_GLIST) {
1408 rdd_cdb->format |= SRDD10_GLIST;
1412 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1414 /* Disable freezing the device queue */
1415 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1417 if (cam_send_ccb(device, ccb) < 0) {
1418 perror("error reading defect list");
1420 if (arglist & CAM_ARG_VERBOSE) {
1421 cam_error_print(device, ccb, CAM_ESF_ALL,
1422 CAM_EPF_ALL, stderr);
1426 goto defect_bailout;
1429 returned_length = scsi_2btoul(((struct
1430 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1432 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1433 defect_list)->format;
1435 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1436 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1437 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1438 struct scsi_sense_data *sense;
1439 int error_code, sense_key, asc, ascq;
1441 sense = &ccb->csio.sense_data;
1442 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1445 * According to the SCSI spec, if the disk doesn't support
1446 * the requested format, it will generally return a sense
1447 * key of RECOVERED ERROR, and an additional sense code
1448 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1449 * also check to make sure that the returned length is
1450 * greater than 0, and then print out whatever format the
1453 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1454 && (asc == 0x1c) && (ascq == 0x00)
1455 && (returned_length > 0)) {
1456 warnx("requested defect format not available");
1457 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1458 case SRDD10_BLOCK_FORMAT:
1459 warnx("Device returned block format");
1461 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1462 warnx("Device returned bytes from index"
1465 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1466 warnx("Device returned physical sector format");
1470 warnx("Device returned unknown defect"
1471 " data format %#x", returned_format);
1472 goto defect_bailout;
1473 break; /* NOTREACHED */
1477 warnx("Error returned from read defect data command");
1478 if (arglist & CAM_ARG_VERBOSE)
1479 cam_error_print(device, ccb, CAM_ESF_ALL,
1480 CAM_EPF_ALL, stderr);
1481 goto defect_bailout;
1483 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1485 warnx("Error returned from read defect data command");
1486 if (arglist & CAM_ARG_VERBOSE)
1487 cam_error_print(device, ccb, CAM_ESF_ALL,
1488 CAM_EPF_ALL, stderr);
1489 goto defect_bailout;
1493 * XXX KDM I should probably clean up the printout format for the
1496 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1497 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1499 struct scsi_defect_desc_phys_sector *dlist;
1501 dlist = (struct scsi_defect_desc_phys_sector *)
1503 sizeof(struct scsi_read_defect_data_hdr_10));
1505 num_returned = returned_length /
1506 sizeof(struct scsi_defect_desc_phys_sector);
1508 fprintf(stderr, "Got %d defect", num_returned);
1510 if ((lists_specified == 0) || (num_returned == 0)) {
1511 fprintf(stderr, "s.\n");
1513 } else if (num_returned == 1)
1514 fprintf(stderr, ":\n");
1516 fprintf(stderr, "s:\n");
1518 for (i = 0; i < num_returned; i++) {
1519 fprintf(stdout, "%d:%d:%d\n",
1520 scsi_3btoul(dlist[i].cylinder),
1522 scsi_4btoul(dlist[i].sector));
1526 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1528 struct scsi_defect_desc_bytes_from_index *dlist;
1530 dlist = (struct scsi_defect_desc_bytes_from_index *)
1532 sizeof(struct scsi_read_defect_data_hdr_10));
1534 num_returned = returned_length /
1535 sizeof(struct scsi_defect_desc_bytes_from_index);
1537 fprintf(stderr, "Got %d defect", num_returned);
1539 if ((lists_specified == 0) || (num_returned == 0)) {
1540 fprintf(stderr, "s.\n");
1542 } else if (num_returned == 1)
1543 fprintf(stderr, ":\n");
1545 fprintf(stderr, "s:\n");
1547 for (i = 0; i < num_returned; i++) {
1548 fprintf(stdout, "%d:%d:%d\n",
1549 scsi_3btoul(dlist[i].cylinder),
1551 scsi_4btoul(dlist[i].bytes_from_index));
1555 case SRDDH10_BLOCK_FORMAT:
1557 struct scsi_defect_desc_block *dlist;
1559 dlist = (struct scsi_defect_desc_block *)(defect_list +
1560 sizeof(struct scsi_read_defect_data_hdr_10));
1562 num_returned = returned_length /
1563 sizeof(struct scsi_defect_desc_block);
1565 fprintf(stderr, "Got %d defect", num_returned);
1567 if ((lists_specified == 0) || (num_returned == 0)) {
1568 fprintf(stderr, "s.\n");
1570 } else if (num_returned == 1)
1571 fprintf(stderr, ":\n");
1573 fprintf(stderr, "s:\n");
1575 for (i = 0; i < num_returned; i++)
1576 fprintf(stdout, "%u\n",
1577 scsi_4btoul(dlist[i].address));
1581 fprintf(stderr, "Unknown defect format %d\n",
1582 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1588 if (defect_list != NULL)
1596 #endif /* MINIMALISTIC */
1600 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1604 ccb = cam_getccb(device);
1610 #ifndef MINIMALISTIC
1612 mode_sense(struct cam_device *device, int mode_page, int page_control,
1613 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1618 ccb = cam_getccb(device);
1621 errx(1, "mode_sense: couldn't allocate CCB");
1623 bzero(&(&ccb->ccb_h)[1],
1624 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1626 scsi_mode_sense(&ccb->csio,
1627 /* retries */ retry_count,
1629 /* tag_action */ MSG_SIMPLE_Q_TAG,
1631 /* page_code */ page_control << 6,
1632 /* page */ mode_page,
1633 /* param_buf */ data,
1634 /* param_len */ datalen,
1635 /* sense_len */ SSD_FULL_SIZE,
1636 /* timeout */ timeout ? timeout : 5000);
1638 if (arglist & CAM_ARG_ERR_RECOVER)
1639 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1641 /* Disable freezing the device queue */
1642 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1644 if (((retval = cam_send_ccb(device, ccb)) < 0)
1645 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1646 if (arglist & CAM_ARG_VERBOSE) {
1647 cam_error_print(device, ccb, CAM_ESF_ALL,
1648 CAM_EPF_ALL, stderr);
1651 cam_close_device(device);
1653 err(1, "error sending mode sense command");
1655 errx(1, "error sending mode sense command");
1662 mode_select(struct cam_device *device, int save_pages, int retry_count,
1663 int timeout, u_int8_t *data, int datalen)
1668 ccb = cam_getccb(device);
1671 errx(1, "mode_select: couldn't allocate CCB");
1673 bzero(&(&ccb->ccb_h)[1],
1674 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1676 scsi_mode_select(&ccb->csio,
1677 /* retries */ retry_count,
1679 /* tag_action */ MSG_SIMPLE_Q_TAG,
1680 /* scsi_page_fmt */ 1,
1681 /* save_pages */ save_pages,
1682 /* param_buf */ data,
1683 /* param_len */ datalen,
1684 /* sense_len */ SSD_FULL_SIZE,
1685 /* timeout */ timeout ? timeout : 5000);
1687 if (arglist & CAM_ARG_ERR_RECOVER)
1688 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1690 /* Disable freezing the device queue */
1691 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1693 if (((retval = cam_send_ccb(device, ccb)) < 0)
1694 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1695 if (arglist & CAM_ARG_VERBOSE) {
1696 cam_error_print(device, ccb, CAM_ESF_ALL,
1697 CAM_EPF_ALL, stderr);
1700 cam_close_device(device);
1703 err(1, "error sending mode select command");
1705 errx(1, "error sending mode select command");
1713 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1714 int retry_count, int timeout)
1716 int c, mode_page = -1, page_control = 0;
1717 int binary = 0, list = 0;
1719 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1725 arglist |= CAM_ARG_DBD;
1728 arglist |= CAM_ARG_MODE_EDIT;
1734 mode_page = strtol(optarg, NULL, 0);
1736 errx(1, "invalid mode page %d", mode_page);
1739 page_control = strtol(optarg, NULL, 0);
1740 if ((page_control < 0) || (page_control > 3))
1741 errx(1, "invalid page control field %d",
1743 arglist |= CAM_ARG_PAGE_CNTL;
1750 if (mode_page == -1 && list == 0)
1751 errx(1, "you must specify a mode page!");
1754 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1755 retry_count, timeout);
1757 mode_edit(device, mode_page, page_control,
1758 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1759 retry_count, timeout);
1764 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1765 int retry_count, int timeout)
1768 u_int32_t flags = CAM_DIR_NONE;
1769 u_int8_t *data_ptr = NULL;
1771 struct get_hook hook;
1772 int c, data_bytes = 0;
1774 char *datastr = NULL, *tstr;
1779 ccb = cam_getccb(device);
1782 warnx("scsicmd: error allocating ccb");
1786 bzero(&(&ccb->ccb_h)[1],
1787 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1789 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1793 while (isspace(*tstr) && (*tstr != '\0'))
1795 hook.argc = argc - optind;
1796 hook.argv = argv + optind;
1798 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1801 * Increment optind by the number of arguments the
1802 * encoding routine processed. After each call to
1803 * getopt(3), optind points to the argument that
1804 * getopt should process _next_. In this case,
1805 * that means it points to the first command string
1806 * argument, if there is one. Once we increment
1807 * this, it should point to either the next command
1808 * line argument, or it should be past the end of
1814 if (arglist & CAM_ARG_CMD_OUT) {
1815 warnx("command must either be "
1816 "read or write, not both");
1818 goto scsicmd_bailout;
1820 arglist |= CAM_ARG_CMD_IN;
1822 data_bytes = strtol(optarg, NULL, 0);
1823 if (data_bytes <= 0) {
1824 warnx("invalid number of input bytes %d",
1827 goto scsicmd_bailout;
1829 hook.argc = argc - optind;
1830 hook.argv = argv + optind;
1833 datastr = cget(&hook, NULL);
1835 * If the user supplied "-" instead of a format, he
1836 * wants the data to be written to stdout.
1838 if ((datastr != NULL)
1839 && (datastr[0] == '-'))
1842 data_ptr = (u_int8_t *)malloc(data_bytes);
1843 if (data_ptr == NULL) {
1844 warnx("can't malloc memory for data_ptr");
1846 goto scsicmd_bailout;
1850 if (arglist & CAM_ARG_CMD_IN) {
1851 warnx("command must either be "
1852 "read or write, not both");
1854 goto scsicmd_bailout;
1856 arglist |= CAM_ARG_CMD_OUT;
1857 flags = CAM_DIR_OUT;
1858 data_bytes = strtol(optarg, NULL, 0);
1859 if (data_bytes <= 0) {
1860 warnx("invalid number of output bytes %d",
1863 goto scsicmd_bailout;
1865 hook.argc = argc - optind;
1866 hook.argv = argv + optind;
1868 datastr = cget(&hook, NULL);
1869 data_ptr = (u_int8_t *)malloc(data_bytes);
1870 if (data_ptr == NULL) {
1871 warnx("can't malloc memory for data_ptr");
1873 goto scsicmd_bailout;
1876 * If the user supplied "-" instead of a format, he
1877 * wants the data to be read from stdin.
1879 if ((datastr != NULL)
1880 && (datastr[0] == '-'))
1883 buff_encode_visit(data_ptr, data_bytes, datastr,
1893 * If fd_data is set, and we're writing to the device, we need to
1894 * read the data the user wants written from stdin.
1896 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1898 int amt_to_read = data_bytes;
1899 u_int8_t *buf_ptr = data_ptr;
1901 for (amt_read = 0; amt_to_read > 0;
1902 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1903 if (amt_read == -1) {
1904 warn("error reading data from stdin");
1906 goto scsicmd_bailout;
1908 amt_to_read -= amt_read;
1909 buf_ptr += amt_read;
1913 if (arglist & CAM_ARG_ERR_RECOVER)
1914 flags |= CAM_PASS_ERR_RECOVER;
1916 /* Disable freezing the device queue */
1917 flags |= CAM_DEV_QFRZDIS;
1920 * This is taken from the SCSI-3 draft spec.
1921 * (T10/1157D revision 0.3)
1922 * The top 3 bits of an opcode are the group code. The next 5 bits
1923 * are the command code.
1924 * Group 0: six byte commands
1925 * Group 1: ten byte commands
1926 * Group 2: ten byte commands
1928 * Group 4: sixteen byte commands
1929 * Group 5: twelve byte commands
1930 * Group 6: vendor specific
1931 * Group 7: vendor specific
1933 switch((cdb[0] >> 5) & 0x7) {
1944 /* computed by buff_encode_visit */
1955 * We should probably use csio_build_visit or something like that
1956 * here, but it's easier to encode arguments as you go. The
1957 * alternative would be skipping the CDB argument and then encoding
1958 * it here, since we've got the data buffer argument by now.
1960 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1962 cam_fill_csio(&ccb->csio,
1963 /*retries*/ retry_count,
1966 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1967 /*data_ptr*/ data_ptr,
1968 /*dxfer_len*/ data_bytes,
1969 /*sense_len*/ SSD_FULL_SIZE,
1970 /*cdb_len*/ cdb_len,
1971 /*timeout*/ timeout ? timeout : 5000);
1973 if (((retval = cam_send_ccb(device, ccb)) < 0)
1974 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1976 warn("error sending command");
1978 warnx("error sending command");
1980 if (arglist & CAM_ARG_VERBOSE) {
1981 cam_error_print(device, ccb, CAM_ESF_ALL,
1982 CAM_EPF_ALL, stderr);
1986 goto scsicmd_bailout;
1990 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1991 && (arglist & CAM_ARG_CMD_IN)
1992 && (data_bytes > 0)) {
1994 buff_decode_visit(data_ptr, data_bytes, datastr,
1996 fprintf(stdout, "\n");
1998 ssize_t amt_written;
1999 int amt_to_write = data_bytes;
2000 u_int8_t *buf_ptr = data_ptr;
2002 for (amt_written = 0; (amt_to_write > 0) &&
2003 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
2004 amt_to_write -= amt_written;
2005 buf_ptr += amt_written;
2007 if (amt_written == -1) {
2008 warn("error writing data to stdout");
2010 goto scsicmd_bailout;
2011 } else if ((amt_written == 0)
2012 && (amt_to_write > 0)) {
2013 warnx("only wrote %u bytes out of %u",
2014 data_bytes - amt_to_write, data_bytes);
2021 if ((data_bytes > 0) && (data_ptr != NULL))
2030 camdebug(int argc, char **argv, char *combinedopt)
2033 int bus = -1, target = -1, lun = -1;
2034 char *tstr, *tmpstr = NULL;
2038 bzero(&ccb, sizeof(union ccb));
2040 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2043 arglist |= CAM_ARG_DEBUG_INFO;
2044 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2047 arglist |= CAM_ARG_DEBUG_PERIPH;
2048 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2051 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2052 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2055 arglist |= CAM_ARG_DEBUG_TRACE;
2056 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2059 arglist |= CAM_ARG_DEBUG_XPT;
2060 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2063 arglist |= CAM_ARG_DEBUG_CDB;
2064 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2071 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2072 warnx("error opening transport layer device %s", XPT_DEVICE);
2073 warn("%s", XPT_DEVICE);
2080 warnx("you must specify \"off\", \"all\" or a bus,");
2081 warnx("bus:target, or bus:target:lun");
2088 while (isspace(*tstr) && (*tstr != '\0'))
2091 if (strncmp(tstr, "off", 3) == 0) {
2092 ccb.cdbg.flags = CAM_DEBUG_NONE;
2093 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2094 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2096 } else if (strncmp(tstr, "all", 3) != 0) {
2097 tmpstr = (char *)strtok(tstr, ":");
2098 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2099 bus = strtol(tmpstr, NULL, 0);
2100 arglist |= CAM_ARG_BUS;
2101 tmpstr = (char *)strtok(NULL, ":");
2102 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2103 target = strtol(tmpstr, NULL, 0);
2104 arglist |= CAM_ARG_TARGET;
2105 tmpstr = (char *)strtok(NULL, ":");
2106 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2107 lun = strtol(tmpstr, NULL, 0);
2108 arglist |= CAM_ARG_LUN;
2113 warnx("you must specify \"all\", \"off\", or a bus,");
2114 warnx("bus:target, or bus:target:lun to debug");
2120 ccb.ccb_h.func_code = XPT_DEBUG;
2121 ccb.ccb_h.path_id = bus;
2122 ccb.ccb_h.target_id = target;
2123 ccb.ccb_h.target_lun = lun;
2125 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2126 warn("CAMIOCOMMAND ioctl failed");
2131 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2132 CAM_FUNC_NOTAVAIL) {
2133 warnx("CAM debugging not available");
2134 warnx("you need to put options CAMDEBUG in"
2135 " your kernel config file!");
2137 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2139 warnx("XPT_DEBUG CCB failed with status %#x",
2143 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2145 "Debugging turned off\n");
2148 "Debugging enabled for "
2161 tagcontrol(struct cam_device *device, int argc, char **argv,
2171 ccb = cam_getccb(device);
2174 warnx("tagcontrol: error allocating ccb");
2178 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2181 numtags = strtol(optarg, NULL, 0);
2183 warnx("tag count %d is < 0", numtags);
2185 goto tagcontrol_bailout;
2196 cam_path_string(device, pathstr, sizeof(pathstr));
2199 bzero(&(&ccb->ccb_h)[1],
2200 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2201 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2202 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2203 ccb->crs.openings = numtags;
2206 if (cam_send_ccb(device, ccb) < 0) {
2207 perror("error sending XPT_REL_SIMQ CCB");
2209 goto tagcontrol_bailout;
2212 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2213 warnx("XPT_REL_SIMQ CCB failed");
2214 cam_error_print(device, ccb, CAM_ESF_ALL,
2215 CAM_EPF_ALL, stderr);
2217 goto tagcontrol_bailout;
2222 fprintf(stdout, "%stagged openings now %d\n",
2223 pathstr, ccb->crs.openings);
2226 bzero(&(&ccb->ccb_h)[1],
2227 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2229 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2231 if (cam_send_ccb(device, ccb) < 0) {
2232 perror("error sending XPT_GDEV_STATS CCB");
2234 goto tagcontrol_bailout;
2237 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2238 warnx("XPT_GDEV_STATS CCB failed");
2239 cam_error_print(device, ccb, CAM_ESF_ALL,
2240 CAM_EPF_ALL, stderr);
2242 goto tagcontrol_bailout;
2245 if (arglist & CAM_ARG_VERBOSE) {
2246 fprintf(stdout, "%s", pathstr);
2247 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2248 fprintf(stdout, "%s", pathstr);
2249 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2250 fprintf(stdout, "%s", pathstr);
2251 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2252 fprintf(stdout, "%s", pathstr);
2253 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2254 fprintf(stdout, "%s", pathstr);
2255 fprintf(stdout, "held %d\n", ccb->cgds.held);
2256 fprintf(stdout, "%s", pathstr);
2257 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2258 fprintf(stdout, "%s", pathstr);
2259 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2262 fprintf(stdout, "%s", pathstr);
2263 fprintf(stdout, "device openings: ");
2265 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2266 ccb->cgds.dev_active);
2276 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2280 cam_path_string(device, pathstr, sizeof(pathstr));
2282 if (cts->transport == XPORT_SPI) {
2283 struct ccb_trans_settings_spi *spi =
2284 &cts->xport_specific.spi;
2286 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
2288 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2291 if (spi->sync_offset != 0) {
2294 freq = scsi_calc_syncsrate(spi->sync_period);
2295 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
2296 pathstr, freq / 1000, freq % 1000);
2300 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
2301 fprintf(stdout, "%soffset: %d\n", pathstr,
2305 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
2306 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2307 (0x01 << spi->bus_width) * 8);
2310 if (spi->valid & CTS_SPI_VALID_DISC) {
2311 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2312 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
2313 "enabled" : "disabled");
2317 if (cts->protocol == PROTO_SCSI) {
2318 struct ccb_trans_settings_scsi *scsi=
2319 &cts->proto_specific.scsi;
2321 if (scsi->valid & CTS_SCSI_VALID_TQ) {
2322 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2323 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
2324 "enabled" : "disabled");
2331 * Get a path inquiry CCB for the specified device.
2334 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2339 ccb = cam_getccb(device);
2342 warnx("get_cpi: couldn't allocate CCB");
2346 bzero(&(&ccb->ccb_h)[1],
2347 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2349 ccb->ccb_h.func_code = XPT_PATH_INQ;
2351 if (cam_send_ccb(device, ccb) < 0) {
2352 warn("get_cpi: error sending Path Inquiry CCB");
2354 if (arglist & CAM_ARG_VERBOSE)
2355 cam_error_print(device, ccb, CAM_ESF_ALL,
2356 CAM_EPF_ALL, stderr);
2360 goto get_cpi_bailout;
2363 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2365 if (arglist & CAM_ARG_VERBOSE)
2366 cam_error_print(device, ccb, CAM_ESF_ALL,
2367 CAM_EPF_ALL, stderr);
2371 goto get_cpi_bailout;
2374 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2384 cpi_print(struct ccb_pathinq *cpi)
2386 char adapter_str[1024];
2389 snprintf(adapter_str, sizeof(adapter_str),
2390 "%s%d:", cpi->dev_name, cpi->unit_number);
2392 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2395 for (i = 1; i < 0xff; i = i << 1) {
2398 if ((i & cpi->hba_inquiry) == 0)
2401 fprintf(stdout, "%s supports ", adapter_str);
2405 str = "MDP message";
2408 str = "32 bit wide SCSI";
2411 str = "16 bit wide SCSI";
2414 str = "SDTR message";
2417 str = "linked CDBs";
2420 str = "tag queue messages";
2423 str = "soft reset alternative";
2426 str = "unknown PI bit set";
2429 fprintf(stdout, "%s\n", str);
2432 for (i = 1; i < 0xff; i = i << 1) {
2435 if ((i & cpi->hba_misc) == 0)
2438 fprintf(stdout, "%s ", adapter_str);
2442 str = "bus scans from high ID to low ID";
2445 str = "removable devices not included in scan";
2447 case PIM_NOINITIATOR:
2448 str = "initiator role not supported";
2450 case PIM_NOBUSRESET:
2451 str = "user has disabled initial BUS RESET or"
2452 " controller is in target/mixed mode";
2455 str = "unknown PIM bit set";
2458 fprintf(stdout, "%s\n", str);
2461 for (i = 1; i < 0xff; i = i << 1) {
2464 if ((i & cpi->target_sprt) == 0)
2467 fprintf(stdout, "%s supports ", adapter_str);
2470 str = "target mode processor mode";
2473 str = "target mode phase cog. mode";
2475 case PIT_DISCONNECT:
2476 str = "disconnects in target mode";
2479 str = "terminate I/O message in target mode";
2482 str = "group 6 commands in target mode";
2485 str = "group 7 commands in target mode";
2488 str = "unknown PIT bit set";
2492 fprintf(stdout, "%s\n", str);
2494 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2496 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2498 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2500 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2501 adapter_str, cpi->hpath_id);
2502 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2504 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2505 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2506 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2507 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2508 if (cpi->base_transfer_speed > 1000)
2509 fprintf(stdout, "%d.%03dMB/sec\n",
2510 cpi->base_transfer_speed / 1000,
2511 cpi->base_transfer_speed % 1000);
2513 fprintf(stdout, "%dKB/sec\n",
2514 (cpi->base_transfer_speed % 1000) * 1000);
2518 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2519 struct ccb_trans_settings *cts)
2525 ccb = cam_getccb(device);
2528 warnx("get_print_cts: error allocating ccb");
2532 bzero(&(&ccb->ccb_h)[1],
2533 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2535 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2537 if (user_settings == 0)
2538 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
2540 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
2542 if (cam_send_ccb(device, ccb) < 0) {
2543 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2544 if (arglist & CAM_ARG_VERBOSE)
2545 cam_error_print(device, ccb, CAM_ESF_ALL,
2546 CAM_EPF_ALL, stderr);
2548 goto get_print_cts_bailout;
2551 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2552 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2553 if (arglist & CAM_ARG_VERBOSE)
2554 cam_error_print(device, ccb, CAM_ESF_ALL,
2555 CAM_EPF_ALL, stderr);
2557 goto get_print_cts_bailout;
2561 cts_print(device, &ccb->cts);
2564 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2566 get_print_cts_bailout:
2574 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2575 int argc, char **argv, char *combinedopt)
2579 int user_settings = 0;
2581 int disc_enable = -1, tag_enable = -1;
2583 double syncrate = -1;
2586 int change_settings = 0, send_tur = 0;
2587 struct ccb_pathinq cpi;
2589 ccb = cam_getccb(device);
2592 warnx("ratecontrol: error allocating ccb");
2596 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2605 if (strncasecmp(optarg, "enable", 6) == 0)
2607 else if (strncasecmp(optarg, "disable", 7) == 0)
2610 warnx("-D argument \"%s\" is unknown", optarg);
2612 goto ratecontrol_bailout;
2614 change_settings = 1;
2617 offset = strtol(optarg, NULL, 0);
2619 warnx("offset value %d is < 0", offset);
2621 goto ratecontrol_bailout;
2623 change_settings = 1;
2629 syncrate = atof(optarg);
2632 warnx("sync rate %f is < 0", syncrate);
2634 goto ratecontrol_bailout;
2636 change_settings = 1;
2639 if (strncasecmp(optarg, "enable", 6) == 0)
2641 else if (strncasecmp(optarg, "disable", 7) == 0)
2644 warnx("-T argument \"%s\" is unknown", optarg);
2646 goto ratecontrol_bailout;
2648 change_settings = 1;
2654 bus_width = strtol(optarg, NULL, 0);
2655 if (bus_width < 0) {
2656 warnx("bus width %d is < 0", bus_width);
2658 goto ratecontrol_bailout;
2660 change_settings = 1;
2667 bzero(&(&ccb->ccb_h)[1],
2668 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2671 * Grab path inquiry information, so we can determine whether
2672 * or not the initiator is capable of the things that the user
2675 ccb->ccb_h.func_code = XPT_PATH_INQ;
2677 if (cam_send_ccb(device, ccb) < 0) {
2678 perror("error sending XPT_PATH_INQ CCB");
2679 if (arglist & CAM_ARG_VERBOSE) {
2680 cam_error_print(device, ccb, CAM_ESF_ALL,
2681 CAM_EPF_ALL, stderr);
2684 goto ratecontrol_bailout;
2687 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2688 warnx("XPT_PATH_INQ CCB failed");
2689 if (arglist & CAM_ARG_VERBOSE) {
2690 cam_error_print(device, ccb, CAM_ESF_ALL,
2691 CAM_EPF_ALL, stderr);
2694 goto ratecontrol_bailout;
2697 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2699 bzero(&(&ccb->ccb_h)[1],
2700 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2703 fprintf(stdout, "Current Parameters:\n");
2705 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2708 goto ratecontrol_bailout;
2710 if (arglist & CAM_ARG_VERBOSE)
2713 if (change_settings) {
2714 int didsettings = 0;
2715 struct ccb_trans_settings_spi *spi = NULL;
2716 struct ccb_trans_settings_scsi *scsi = NULL;
2718 if (ccb->cts.transport == XPORT_SPI) {
2719 spi = &ccb->cts.xport_specific.spi;
2722 if (ccb->cts.protocol == PROTO_SCSI) {
2723 scsi = &ccb->cts.proto_specific.scsi;
2726 if (spi && disc_enable != -1) {
2727 spi->valid |= CTS_SPI_VALID_DISC;
2728 if (disc_enable == 0)
2729 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2731 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2734 if (scsi && tag_enable != -1) {
2735 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2736 warnx("HBA does not support tagged queueing, "
2737 "so you cannot modify tag settings");
2739 goto ratecontrol_bailout;
2742 scsi->valid |= CTS_SCSI_VALID_TQ;
2744 if (tag_enable == 0)
2745 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2747 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2751 if (spi && offset != -1) {
2752 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2753 warnx("HBA at %s%d is not cable of changing "
2754 "offset", cpi.dev_name,
2757 goto ratecontrol_bailout;
2759 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2760 spi->sync_offset = offset;
2764 if (spi && syncrate != -1) {
2765 int prelim_sync_period;
2768 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2769 warnx("HBA at %s%d is not cable of changing "
2770 "transfer rates", cpi.dev_name,
2773 goto ratecontrol_bailout;
2776 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2779 * The sync rate the user gives us is in MHz.
2780 * We need to translate it into KHz for this
2786 * Next, we calculate a "preliminary" sync period
2787 * in tenths of a nanosecond.
2790 prelim_sync_period = 0;
2792 prelim_sync_period = 10000000 / syncrate;
2795 scsi_calc_syncparam(prelim_sync_period);
2797 freq = scsi_calc_syncsrate(spi->sync_period);
2802 * The bus_width argument goes like this:
2806 * Therefore, if you shift the number of bits given on the
2807 * command line right by 4, you should get the correct
2810 if (spi && bus_width != -1) {
2813 * We might as well validate things here with a
2814 * decipherable error message, rather than what
2815 * will probably be an indecipherable error message
2816 * by the time it gets back to us.
2818 if ((bus_width == 16)
2819 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2820 warnx("HBA does not support 16 bit bus width");
2822 goto ratecontrol_bailout;
2823 } else if ((bus_width == 32)
2824 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2825 warnx("HBA does not support 32 bit bus width");
2827 goto ratecontrol_bailout;
2828 } else if ((bus_width != 8)
2829 && (bus_width != 16)
2830 && (bus_width != 32)) {
2831 warnx("Invalid bus width %d", bus_width);
2833 goto ratecontrol_bailout;
2836 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2837 spi->bus_width = bus_width >> 4;
2841 if (didsettings == 0) {
2842 goto ratecontrol_bailout;
2844 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2846 if (cam_send_ccb(device, ccb) < 0) {
2847 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2848 if (arglist & CAM_ARG_VERBOSE) {
2849 cam_error_print(device, ccb, CAM_ESF_ALL,
2850 CAM_EPF_ALL, stderr);
2853 goto ratecontrol_bailout;
2856 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2857 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2858 if (arglist & CAM_ARG_VERBOSE) {
2859 cam_error_print(device, ccb, CAM_ESF_ALL,
2860 CAM_EPF_ALL, stderr);
2863 goto ratecontrol_bailout;
2868 retval = testunitready(device, retry_count, timeout,
2869 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2872 * If the TUR didn't succeed, just bail.
2876 fprintf(stderr, "Test Unit Ready failed\n");
2877 goto ratecontrol_bailout;
2881 * If the user wants things quiet, there's no sense in
2882 * getting the transfer settings, if we're not going
2886 goto ratecontrol_bailout;
2888 fprintf(stdout, "New Parameters:\n");
2889 retval = get_print_cts(device, user_settings, 0, NULL);
2892 ratecontrol_bailout:
2899 scsiformat(struct cam_device *device, int argc, char **argv,
2900 char *combinedopt, int retry_count, int timeout)
2904 int ycount = 0, quiet = 0;
2905 int error = 0, response = 0, retval = 0;
2906 int use_timeout = 10800 * 1000;
2908 struct format_defect_list_header fh;
2909 u_int8_t *data_ptr = NULL;
2910 u_int32_t dxfer_len = 0;
2912 int num_warnings = 0;
2915 ccb = cam_getccb(device);
2918 warnx("scsiformat: error allocating ccb");
2922 bzero(&(&ccb->ccb_h)[1],
2923 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2925 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2946 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2947 "following device:\n");
2949 error = scsidoinquiry(device, argc, argv, combinedopt,
2950 retry_count, timeout);
2953 warnx("scsiformat: error sending inquiry");
2954 goto scsiformat_bailout;
2963 fprintf(stdout, "Are you SURE you want to do "
2966 if (fgets(str, sizeof(str), stdin) != NULL) {
2968 if (strncasecmp(str, "yes", 3) == 0)
2970 else if (strncasecmp(str, "no", 2) == 0)
2973 fprintf(stdout, "Please answer"
2974 " \"yes\" or \"no\"\n");
2977 } while (response == 0);
2979 if (response == -1) {
2981 goto scsiformat_bailout;
2986 use_timeout = timeout;
2989 fprintf(stdout, "Current format timeout is %d seconds\n",
2990 use_timeout / 1000);
2994 * If the user hasn't disabled questions and didn't specify a
2995 * timeout on the command line, ask them if they want the current
2999 && (timeout == 0)) {
3001 int new_timeout = 0;
3003 fprintf(stdout, "Enter new timeout in seconds or press\n"
3004 "return to keep the current timeout [%d] ",
3005 use_timeout / 1000);
3007 if (fgets(str, sizeof(str), stdin) != NULL) {
3009 new_timeout = atoi(str);
3012 if (new_timeout != 0) {
3013 use_timeout = new_timeout * 1000;
3014 fprintf(stdout, "Using new timeout value %d\n",
3015 use_timeout / 1000);
3020 * Keep this outside the if block below to silence any unused
3021 * variable warnings.
3023 bzero(&fh, sizeof(fh));
3026 * If we're in immediate mode, we've got to include the format
3029 if (immediate != 0) {
3030 fh.byte2 = FU_DLH_IMMED;
3031 data_ptr = (u_int8_t *)&fh;
3032 dxfer_len = sizeof(fh);
3033 byte2 = FU_FMT_DATA;
3034 } else if (quiet == 0) {
3035 fprintf(stdout, "Formatting...");
3039 scsi_format_unit(&ccb->csio,
3040 /* retries */ retry_count,
3042 /* tag_action */ MSG_SIMPLE_Q_TAG,
3045 /* data_ptr */ data_ptr,
3046 /* dxfer_len */ dxfer_len,
3047 /* sense_len */ SSD_FULL_SIZE,
3048 /* timeout */ use_timeout);
3050 /* Disable freezing the device queue */
3051 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3053 if (arglist & CAM_ARG_ERR_RECOVER)
3054 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3056 if (((retval = cam_send_ccb(device, ccb)) < 0)
3057 || ((immediate == 0)
3058 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3059 const char errstr[] = "error sending format command";
3066 if (arglist & CAM_ARG_VERBOSE) {
3067 cam_error_print(device, ccb, CAM_ESF_ALL,
3068 CAM_EPF_ALL, stderr);
3071 goto scsiformat_bailout;
3075 * If we ran in non-immediate mode, we already checked for errors
3076 * above and printed out any necessary information. If we're in
3077 * immediate mode, we need to loop through and get status
3078 * information periodically.
3080 if (immediate == 0) {
3082 fprintf(stdout, "Format Complete\n");
3084 goto scsiformat_bailout;
3091 bzero(&(&ccb->ccb_h)[1],
3092 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3095 * There's really no need to do error recovery or
3096 * retries here, since we're just going to sit in a
3097 * loop and wait for the device to finish formatting.
3099 scsi_test_unit_ready(&ccb->csio,
3102 /* tag_action */ MSG_SIMPLE_Q_TAG,
3103 /* sense_len */ SSD_FULL_SIZE,
3104 /* timeout */ 5000);
3106 /* Disable freezing the device queue */
3107 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3109 retval = cam_send_ccb(device, ccb);
3112 * If we get an error from the ioctl, bail out. SCSI
3113 * errors are expected.
3116 warn("error sending CAMIOCOMMAND ioctl");
3117 if (arglist & CAM_ARG_VERBOSE) {
3118 cam_error_print(device, ccb, CAM_ESF_ALL,
3119 CAM_EPF_ALL, stderr);
3122 goto scsiformat_bailout;
3125 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3127 if ((status != CAM_REQ_CMP)
3128 && (status == CAM_SCSI_STATUS_ERROR)
3129 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3130 struct scsi_sense_data *sense;
3131 int error_code, sense_key, asc, ascq;
3133 sense = &ccb->csio.sense_data;
3134 scsi_extract_sense(sense, &error_code, &sense_key,
3138 * According to the SCSI-2 and SCSI-3 specs, a
3139 * drive that is in the middle of a format should
3140 * return NOT READY with an ASC of "logical unit
3141 * not ready, format in progress". The sense key
3142 * specific bytes will then be a progress indicator.
3144 if ((sense_key == SSD_KEY_NOT_READY)
3145 && (asc == 0x04) && (ascq == 0x04)) {
3146 if ((sense->extra_len >= 10)
3147 && ((sense->sense_key_spec[0] &
3148 SSD_SCS_VALID) != 0)
3151 u_int64_t percentage;
3154 &sense->sense_key_spec[1]);
3155 percentage = 10000 * val;
3158 "\rFormatting: %ju.%02u %% "
3160 (uintmax_t)(percentage /
3162 (unsigned)((percentage /
3166 } else if ((quiet == 0)
3167 && (++num_warnings <= 1)) {
3168 warnx("Unexpected SCSI Sense Key "
3169 "Specific value returned "
3171 scsi_sense_print(device, &ccb->csio,
3173 warnx("Unable to print status "
3174 "information, but format will "
3176 warnx("will exit when format is "
3181 warnx("Unexpected SCSI error during format");
3182 cam_error_print(device, ccb, CAM_ESF_ALL,
3183 CAM_EPF_ALL, stderr);
3185 goto scsiformat_bailout;
3188 } else if (status != CAM_REQ_CMP) {
3189 warnx("Unexpected CAM status %#x", status);
3190 if (arglist & CAM_ARG_VERBOSE)
3191 cam_error_print(device, ccb, CAM_ESF_ALL,
3192 CAM_EPF_ALL, stderr);
3194 goto scsiformat_bailout;
3197 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3200 fprintf(stdout, "\nFormat Complete\n");
3210 scsireportluns(struct cam_device *device, int argc, char **argv,
3211 char *combinedopt, int retry_count, int timeout)
3214 int c, countonly, lunsonly;
3215 struct scsi_report_luns_data *lundata;
3217 uint8_t report_type;
3218 uint32_t list_len, i, j;
3223 report_type = RPL_REPORT_DEFAULT;
3224 ccb = cam_getccb(device);
3227 warnx("%s: error allocating ccb", __func__);
3231 bzero(&(&ccb->ccb_h)[1],
3232 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3237 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3246 if (strcasecmp(optarg, "default") == 0)
3247 report_type = RPL_REPORT_DEFAULT;
3248 else if (strcasecmp(optarg, "wellknown") == 0)
3249 report_type = RPL_REPORT_WELLKNOWN;
3250 else if (strcasecmp(optarg, "all") == 0)
3251 report_type = RPL_REPORT_ALL;
3253 warnx("%s: invalid report type \"%s\"",
3264 if ((countonly != 0)
3265 && (lunsonly != 0)) {
3266 warnx("%s: you can only specify one of -c or -l", __func__);
3271 * According to SPC-4, the allocation length must be at least 16
3272 * bytes -- enough for the header and one LUN.
3274 alloc_len = sizeof(*lundata) + 8;
3278 lundata = malloc(alloc_len);
3280 if (lundata == NULL) {
3281 warn("%s: error mallocing %d bytes", __func__, alloc_len);
3286 scsi_report_luns(&ccb->csio,
3287 /*retries*/ retry_count,
3289 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3290 /*select_report*/ report_type,
3291 /*rpl_buf*/ lundata,
3292 /*alloc_len*/ alloc_len,
3293 /*sense_len*/ SSD_FULL_SIZE,
3294 /*timeout*/ timeout ? timeout : 5000);
3296 /* Disable freezing the device queue */
3297 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3299 if (arglist & CAM_ARG_ERR_RECOVER)
3300 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3302 if (cam_send_ccb(device, ccb) < 0) {
3303 warn("error sending REPORT LUNS command");
3305 if (arglist & CAM_ARG_VERBOSE)
3306 cam_error_print(device, ccb, CAM_ESF_ALL,
3307 CAM_EPF_ALL, stderr);
3313 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3314 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3320 list_len = scsi_4btoul(lundata->length);
3323 * If we need to list the LUNs, and our allocation
3324 * length was too short, reallocate and retry.
3326 if ((countonly == 0)
3327 && (list_len > (alloc_len - sizeof(*lundata)))) {
3328 alloc_len = list_len + sizeof(*lundata);
3334 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
3335 ((list_len / 8) > 1) ? "s" : "");
3340 for (i = 0; i < (list_len / 8); i++) {
3344 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
3346 fprintf(stdout, ",");
3347 switch (lundata->luns[i].lundata[j] &
3348 RPL_LUNDATA_ATYP_MASK) {
3349 case RPL_LUNDATA_ATYP_PERIPH:
3350 if ((lundata->luns[i].lundata[j] &
3351 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
3352 fprintf(stdout, "%d:",
3353 lundata->luns[i].lundata[j] &
3354 RPL_LUNDATA_PERIPH_BUS_MASK);
3356 && ((lundata->luns[i].lundata[j+2] &
3357 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
3360 fprintf(stdout, "%d",
3361 lundata->luns[i].lundata[j+1]);
3363 case RPL_LUNDATA_ATYP_FLAT: {
3365 tmplun[0] = lundata->luns[i].lundata[j] &
3366 RPL_LUNDATA_FLAT_LUN_MASK;
3367 tmplun[1] = lundata->luns[i].lundata[j+1];
3369 fprintf(stdout, "%d", scsi_2btoul(tmplun));
3373 case RPL_LUNDATA_ATYP_LUN:
3374 fprintf(stdout, "%d:%d:%d",
3375 (lundata->luns[i].lundata[j+1] &
3376 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
3377 lundata->luns[i].lundata[j] &
3378 RPL_LUNDATA_LUN_TARG_MASK,
3379 lundata->luns[i].lundata[j+1] &
3380 RPL_LUNDATA_LUN_LUN_MASK);
3382 case RPL_LUNDATA_ATYP_EXTLUN: {
3383 int field_len, field_len_code, eam_code;
3385 eam_code = lundata->luns[i].lundata[j] &
3386 RPL_LUNDATA_EXT_EAM_MASK;
3387 field_len_code = (lundata->luns[i].lundata[j] &
3388 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
3389 field_len = field_len_code * 2;
3391 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
3392 && (field_len_code == 0x00)) {
3393 fprintf(stdout, "%d",
3394 lundata->luns[i].lundata[j+1]);
3395 } else if ((eam_code ==
3396 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
3397 && (field_len_code == 0x03)) {
3401 * This format takes up all 8 bytes.
3402 * If we aren't starting at offset 0,
3406 fprintf(stdout, "Invalid "
3409 "specified format", j);
3413 bzero(tmp_lun, sizeof(tmp_lun));
3414 bcopy(&lundata->luns[i].lundata[j+1],
3415 &tmp_lun[1], sizeof(tmp_lun) - 1);
3416 fprintf(stdout, "%#jx",
3417 (intmax_t)scsi_8btou64(tmp_lun));
3420 fprintf(stderr, "Unknown Extended LUN"
3421 "Address method %#x, length "
3422 "code %#x", eam_code,
3429 fprintf(stderr, "Unknown LUN address method "
3430 "%#x\n", lundata->luns[i].lundata[0] &
3431 RPL_LUNDATA_ATYP_MASK);
3435 * For the flat addressing method, there are no
3436 * other levels after it.
3441 fprintf(stdout, "\n");
3453 #endif /* MINIMALISTIC */
3458 fprintf(verbose ? stdout : stderr,
3459 "usage: camcontrol <command> [device id][generic args][command args]\n"
3460 " camcontrol devlist [-v]\n"
3461 #ifndef MINIMALISTIC
3462 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3463 " camcontrol tur [dev_id][generic args]\n"
3464 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3465 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
3466 " camcontrol start [dev_id][generic args]\n"
3467 " camcontrol stop [dev_id][generic args]\n"
3468 " camcontrol load [dev_id][generic args]\n"
3469 " camcontrol eject [dev_id][generic args]\n"
3470 #endif /* MINIMALISTIC */
3471 " camcontrol rescan <all | bus[:target:lun]>\n"
3472 " camcontrol reset <all | bus[:target:lun]>\n"
3473 #ifndef MINIMALISTIC
3474 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3475 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3476 " [-P pagectl][-e | -b][-d]\n"
3477 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3478 " [-i len fmt|-o len fmt [args]]\n"
3479 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3480 " <all|bus[:target[:lun]]|off>\n"
3481 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3482 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3483 " [-D <enable|disable>][-O offset][-q]\n"
3484 " [-R syncrate][-v][-T <enable|disable>]\n"
3485 " [-U][-W bus_width]\n"
3486 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
3487 #endif /* MINIMALISTIC */
3488 " camcontrol help\n");
3491 #ifndef MINIMALISTIC
3493 "Specify one of the following options:\n"
3494 "devlist list all CAM devices\n"
3495 "periphlist list all CAM peripheral drivers attached to a device\n"
3496 "tur send a test unit ready to the named device\n"
3497 "inquiry send a SCSI inquiry command to the named device\n"
3498 "reportluns send a SCSI report luns command to the device\n"
3499 "start send a Start Unit command to the device\n"
3500 "stop send a Stop Unit command to the device\n"
3501 "load send a Start Unit command to the device with the load bit set\n"
3502 "eject send a Stop Unit command to the device with the eject bit set\n"
3503 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3504 "reset reset all busses, the given bus, or bus:target:lun\n"
3505 "defects read the defect list of the specified device\n"
3506 "modepage display or edit (-e) the given mode page\n"
3507 "cmd send the given scsi command, may need -i or -o as well\n"
3508 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3509 "tags report or set the number of transaction slots for a device\n"
3510 "negotiate report or set device negotiation parameters\n"
3511 "format send the SCSI FORMAT UNIT command to the named device\n"
3512 "help this message\n"
3513 "Device Identifiers:\n"
3514 "bus:target specify the bus and target, lun defaults to 0\n"
3515 "bus:target:lun specify the bus, target and lun\n"
3516 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3517 "Generic arguments:\n"
3518 "-v be verbose, print out sense information\n"
3519 "-t timeout command timeout in seconds, overrides default timeout\n"
3520 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3521 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3522 "-E have the kernel attempt to perform SCSI error recovery\n"
3523 "-C count specify the SCSI command retry count (needs -E to work)\n"
3524 "modepage arguments:\n"
3525 "-l list all available mode pages\n"
3526 "-m page specify the mode page to view or edit\n"
3527 "-e edit the specified mode page\n"
3528 "-b force view to binary mode\n"
3529 "-d disable block descriptors for mode sense\n"
3530 "-P pgctl page control field 0-3\n"
3531 "defects arguments:\n"
3532 "-f format specify defect list format (block, bfi or phys)\n"
3533 "-G get the grown defect list\n"
3534 "-P get the permanant defect list\n"
3535 "inquiry arguments:\n"
3536 "-D get the standard inquiry data\n"
3537 "-S get the serial number\n"
3538 "-R get the transfer rate, etc.\n"
3539 "reportluns arguments:\n"
3540 "-c only report a count of available LUNs\n"
3541 "-l only print out luns, and not a count\n"
3542 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
3544 "-c cdb [args] specify the SCSI CDB\n"
3545 "-i len fmt specify input data and input data format\n"
3546 "-o len fmt [args] specify output data and output data fmt\n"
3547 "debug arguments:\n"
3548 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3549 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3550 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3551 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3553 "-N tags specify the number of tags to use for this device\n"
3554 "-q be quiet, don't report the number of tags\n"
3555 "-v report a number of tag-related parameters\n"
3556 "negotiate arguments:\n"
3557 "-a send a test unit ready after negotiation\n"
3558 "-c report/set current negotiation settings\n"
3559 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3560 "-O offset set command delay offset\n"
3561 "-q be quiet, don't report anything\n"
3562 "-R syncrate synchronization rate in MHz\n"
3563 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3564 "-U report/set user negotiation settings\n"
3565 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3566 "-v also print a Path Inquiry CCB for the controller\n"
3567 "format arguments:\n"
3568 "-q be quiet, don't print status messages\n"
3569 "-r run in report only mode\n"
3570 "-w don't send immediate format command\n"
3571 "-y don't ask any questions\n");
3572 #endif /* MINIMALISTIC */
3576 main(int argc, char **argv)
3579 char *device = NULL;
3581 struct cam_device *cam_dev = NULL;
3582 int timeout = 0, retry_count = 1;
3583 camcontrol_optret optreturn;
3585 const char *mainopt = "C:En:t:u:v";
3586 const char *subopt = NULL;
3587 char combinedopt[256];
3588 int error = 0, optstart = 2;
3590 #ifndef MINIMALISTIC
3591 int bus, target, lun;
3592 #endif /* MINIMALISTIC */
3594 cmdlist = CAM_CMD_NONE;
3595 arglist = CAM_ARG_NONE;
3603 * Get the base option.
3605 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3607 if (optreturn == CC_OR_AMBIGUOUS) {
3608 warnx("ambiguous option %s", argv[1]);
3611 } else if (optreturn == CC_OR_NOT_FOUND) {
3612 warnx("option %s not found", argv[1]);
3618 * Ahh, getopt(3) is a pain.
3620 * This is a gross hack. There really aren't many other good
3621 * options (excuse the pun) for parsing options in a situation like
3622 * this. getopt is kinda braindead, so you end up having to run
3623 * through the options twice, and give each invocation of getopt
3624 * the option string for the other invocation.
3626 * You would think that you could just have two groups of options.
3627 * The first group would get parsed by the first invocation of
3628 * getopt, and the second group would get parsed by the second
3629 * invocation of getopt. It doesn't quite work out that way. When
3630 * the first invocation of getopt finishes, it leaves optind pointing
3631 * to the argument _after_ the first argument in the second group.
3632 * So when the second invocation of getopt comes around, it doesn't
3633 * recognize the first argument it gets and then bails out.
3635 * A nice alternative would be to have a flag for getopt that says
3636 * "just keep parsing arguments even when you encounter an unknown
3637 * argument", but there isn't one. So there's no real clean way to
3638 * easily parse two sets of arguments without having one invocation
3639 * of getopt know about the other.
3641 * Without this hack, the first invocation of getopt would work as
3642 * long as the generic arguments are first, but the second invocation
3643 * (in the subfunction) would fail in one of two ways. In the case
3644 * where you don't set optreset, it would fail because optind may be
3645 * pointing to the argument after the one it should be pointing at.
3646 * In the case where you do set optreset, and reset optind, it would
3647 * fail because getopt would run into the first set of options, which
3648 * it doesn't understand.
3650 * All of this would "sort of" work if you could somehow figure out
3651 * whether optind had been incremented one option too far. The
3652 * mechanics of that, however, are more daunting than just giving
3653 * both invocations all of the expect options for either invocation.
3655 * Needless to say, I wouldn't mind if someone invented a better
3656 * (non-GPL!) command line parsing interface than getopt. I
3657 * wouldn't mind if someone added more knobs to getopt to make it
3658 * work better. Who knows, I may talk myself into doing it someday,
3659 * if the standards weenies let me. As it is, it just leads to
3660 * hackery like this and causes people to avoid it in some cases.
3662 * KDM, September 8th, 1998
3665 sprintf(combinedopt, "%s%s", mainopt, subopt);
3667 sprintf(combinedopt, "%s", mainopt);
3670 * For these options we do not parse optional device arguments and
3671 * we do not open a passthrough device.
3673 if ((cmdlist == CAM_CMD_RESCAN)
3674 || (cmdlist == CAM_CMD_RESET)
3675 || (cmdlist == CAM_CMD_DEVTREE)
3676 || (cmdlist == CAM_CMD_USAGE)
3677 || (cmdlist == CAM_CMD_DEBUG))
3680 #ifndef MINIMALISTIC
3682 && (argc > 2 && argv[2][0] != '-')) {
3687 * First catch people who try to do things like:
3688 * camcontrol tur /dev/da0
3689 * camcontrol doesn't take device nodes as arguments.
3691 if (argv[2][0] == '/') {
3692 warnx("%s is not a valid device identifier", argv[2]);
3693 errx(1, "please read the camcontrol(8) man page");
3694 } else if (isdigit(argv[2][0])) {
3695 /* device specified as bus:target[:lun] */
3696 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3698 errx(1, "numeric device specification must "
3699 "be either bus:target, or "
3701 /* default to 0 if lun was not specified */
3702 if ((arglist & CAM_ARG_LUN) == 0) {
3704 arglist |= CAM_ARG_LUN;
3708 if (cam_get_device(argv[2], name, sizeof name, &unit)
3710 errx(1, "%s", cam_errbuf);
3711 device = strdup(name);
3712 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3716 #endif /* MINIMALISTIC */
3718 * Start getopt processing at argv[2/3], since we've already
3719 * accepted argv[1..2] as the command name, and as a possible
3725 * Now we run through the argument list looking for generic
3726 * options, and ignoring options that possibly belong to
3729 while ((c = getopt(argc, argv, combinedopt))!= -1){
3732 retry_count = strtol(optarg, NULL, 0);
3733 if (retry_count < 0)
3734 errx(1, "retry count %d is < 0",
3736 arglist |= CAM_ARG_RETRIES;
3739 arglist |= CAM_ARG_ERR_RECOVER;
3742 arglist |= CAM_ARG_DEVICE;
3744 while (isspace(*tstr) && (*tstr != '\0'))
3746 device = (char *)strdup(tstr);
3749 timeout = strtol(optarg, NULL, 0);
3751 errx(1, "invalid timeout %d", timeout);
3752 /* Convert the timeout from seconds to ms */
3754 arglist |= CAM_ARG_TIMEOUT;
3757 arglist |= CAM_ARG_UNIT;
3758 unit = strtol(optarg, NULL, 0);
3761 arglist |= CAM_ARG_VERBOSE;
3768 #ifndef MINIMALISTIC
3770 * For most commands we'll want to open the passthrough device
3771 * associated with the specified device. In the case of the rescan
3772 * commands, we don't use a passthrough device at all, just the
3773 * transport layer device.
3776 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3777 && (((arglist & CAM_ARG_DEVICE) == 0)
3778 || ((arglist & CAM_ARG_UNIT) == 0))) {
3779 errx(1, "subcommand \"%s\" requires a valid device "
3780 "identifier", argv[1]);
3783 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3784 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3785 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3787 errx(1,"%s", cam_errbuf);
3789 #endif /* MINIMALISTIC */
3792 * Reset optind to 2, and reset getopt, so these routines can parse
3793 * the arguments again.
3799 #ifndef MINIMALISTIC
3800 case CAM_CMD_DEVLIST:
3801 error = getdevlist(cam_dev);
3803 #endif /* MINIMALISTIC */
3804 case CAM_CMD_DEVTREE:
3805 error = getdevtree();
3807 #ifndef MINIMALISTIC
3809 error = testunitready(cam_dev, retry_count, timeout, 0);
3811 case CAM_CMD_INQUIRY:
3812 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3813 retry_count, timeout);
3815 case CAM_CMD_STARTSTOP:
3816 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3817 arglist & CAM_ARG_EJECT, retry_count,
3820 #endif /* MINIMALISTIC */
3821 case CAM_CMD_RESCAN:
3822 error = dorescan_or_reset(argc, argv, 1);
3825 error = dorescan_or_reset(argc, argv, 0);
3827 #ifndef MINIMALISTIC
3828 case CAM_CMD_READ_DEFECTS:
3829 error = readdefects(cam_dev, argc, argv, combinedopt,
3830 retry_count, timeout);
3832 case CAM_CMD_MODE_PAGE:
3833 modepage(cam_dev, argc, argv, combinedopt,
3834 retry_count, timeout);
3836 case CAM_CMD_SCSI_CMD:
3837 error = scsicmd(cam_dev, argc, argv, combinedopt,
3838 retry_count, timeout);
3841 error = camdebug(argc, argv, combinedopt);
3844 error = tagcontrol(cam_dev, argc, argv, combinedopt);
3847 error = ratecontrol(cam_dev, retry_count, timeout,
3848 argc, argv, combinedopt);
3850 case CAM_CMD_FORMAT:
3851 error = scsiformat(cam_dev, argc, argv,
3852 combinedopt, retry_count, timeout);
3854 case CAM_CMD_REPORTLUNS:
3855 error = scsireportluns(cam_dev, argc, argv,
3856 combinedopt, retry_count,
3859 #endif /* MINIMALISTIC */
3869 if (cam_dev != NULL)
3870 cam_close_device(cam_dev);