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.flags = CCB_TRANS_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.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
891 && (ccb->cts.sync_offset != 0)) {
892 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
895 struct ccb_pathinq cpi;
897 retval = get_cpi(device, &cpi);
900 goto xferrate_bailout;
902 speed = cpi.base_transfer_speed;
906 fprintf(stdout, "%s%d: ", device->device_name,
907 device->dev_unit_num);
909 if ((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
910 speed *= (0x01 << device->bus_width);
915 fprintf(stdout, "%d.%03dMB/s transfers ",
918 fprintf(stdout, "%dKB/s transfers ",
921 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
922 && (ccb->cts.sync_offset != 0))
923 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
924 freq % 1000, ccb->cts.sync_offset);
926 if (((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
927 && (ccb->cts.bus_width > 0)) {
928 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
929 && (ccb->cts.sync_offset != 0)) {
930 fprintf(stdout, ", ");
932 fprintf(stdout, " (");
934 fprintf(stdout, "%dbit)", 8 * (0x01 << ccb->cts.bus_width));
935 } else if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0)
936 && (ccb->cts.sync_offset != 0)) {
937 fprintf(stdout, ")");
940 if (((ccb->cts.valid & CCB_TRANS_TQ_VALID) != 0)
941 && (ccb->cts.flags & CCB_TRANS_TAG_ENB))
942 fprintf(stdout, ", Tagged Queueing Enabled");
944 fprintf(stdout, "\n");
952 #endif /* MINIMALISTIC */
955 * Parse out a bus, or a bus, target and lun in the following
961 * Returns the number of parsed components, or 0.
964 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglst)
969 while (isspace(*tstr) && (*tstr != '\0'))
972 tmpstr = (char *)strtok(tstr, ":");
973 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
974 *bus = strtol(tmpstr, NULL, 0);
975 *arglst |= CAM_ARG_BUS;
977 tmpstr = (char *)strtok(NULL, ":");
978 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
979 *target = strtol(tmpstr, NULL, 0);
980 *arglst |= CAM_ARG_TARGET;
982 tmpstr = (char *)strtok(NULL, ":");
983 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
984 *lun = strtol(tmpstr, NULL, 0);
985 *arglst |= CAM_ARG_LUN;
995 dorescan_or_reset(int argc, char **argv, int rescan)
997 static const char must[] =
998 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1000 int bus = -1, target = -1, lun = -1;
1004 warnx(must, rescan? "rescan" : "reset");
1008 tstr = argv[optind];
1009 while (isspace(*tstr) && (*tstr != '\0'))
1011 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1012 arglist |= CAM_ARG_BUS;
1014 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
1015 if (rv != 1 && rv != 3) {
1016 warnx(must, rescan? "rescan" : "reset");
1021 if ((arglist & CAM_ARG_BUS)
1022 && (arglist & CAM_ARG_TARGET)
1023 && (arglist & CAM_ARG_LUN))
1024 error = scanlun_or_reset_dev(bus, target, lun, rescan);
1026 error = rescan_or_reset_bus(bus, rescan);
1032 rescan_or_reset_bus(int bus, int rescan)
1034 union ccb ccb, matchccb;
1040 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1041 warnx("error opening tranport layer device %s", XPT_DEVICE);
1042 warn("%s", XPT_DEVICE);
1047 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1048 ccb.ccb_h.path_id = bus;
1049 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1050 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1051 ccb.crcn.flags = CAM_FLAG_NONE;
1053 /* run this at a low priority */
1054 ccb.ccb_h.pinfo.priority = 5;
1056 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1057 warn("CAMIOCOMMAND ioctl failed");
1062 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1063 fprintf(stdout, "%s of bus %d was successful\n",
1064 rescan ? "Re-scan" : "Reset", bus);
1066 fprintf(stdout, "%s of bus %d returned error %#x\n",
1067 rescan ? "Re-scan" : "Reset", bus,
1068 ccb.ccb_h.status & CAM_STATUS_MASK);
1079 * The right way to handle this is to modify the xpt so that it can
1080 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1081 * that isn't implemented, so instead we enumerate the busses and
1082 * send the rescan or reset to those busses in the case where the
1083 * given bus is -1 (wildcard). We don't send a rescan or reset
1084 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1085 * no-op, sending a rescan to the xpt bus would result in a status of
1088 bzero(&(&matchccb.ccb_h)[1],
1089 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1090 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1091 bufsize = sizeof(struct dev_match_result) * 20;
1092 matchccb.cdm.match_buf_len = bufsize;
1093 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1094 if (matchccb.cdm.matches == NULL) {
1095 warnx("can't malloc memory for matches");
1099 matchccb.cdm.num_matches = 0;
1101 matchccb.cdm.num_patterns = 1;
1102 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1104 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1105 matchccb.cdm.pattern_buf_len);
1106 if (matchccb.cdm.patterns == NULL) {
1107 warnx("can't malloc memory for patterns");
1111 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1112 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1117 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1118 warn("CAMIOCOMMAND ioctl failed");
1123 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1124 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1125 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1126 warnx("got CAM error %#x, CDM error %d\n",
1127 matchccb.ccb_h.status, matchccb.cdm.status);
1132 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1133 struct bus_match_result *bus_result;
1135 /* This shouldn't happen. */
1136 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1139 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1142 * We don't want to rescan or reset the xpt bus.
1145 if ((int)bus_result->path_id == -1)
1148 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1150 ccb.ccb_h.path_id = bus_result->path_id;
1151 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1152 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1153 ccb.crcn.flags = CAM_FLAG_NONE;
1155 /* run this at a low priority */
1156 ccb.ccb_h.pinfo.priority = 5;
1158 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1159 warn("CAMIOCOMMAND ioctl failed");
1164 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1165 fprintf(stdout, "%s of bus %d was successful\n",
1166 rescan? "Re-scan" : "Reset",
1167 bus_result->path_id);
1170 * Don't bail out just yet, maybe the other
1171 * rescan or reset commands will complete
1174 fprintf(stderr, "%s of bus %d returned error "
1175 "%#x\n", rescan? "Re-scan" : "Reset",
1176 bus_result->path_id,
1177 ccb.ccb_h.status & CAM_STATUS_MASK);
1181 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1182 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1189 if (matchccb.cdm.patterns != NULL)
1190 free(matchccb.cdm.patterns);
1191 if (matchccb.cdm.matches != NULL)
1192 free(matchccb.cdm.matches);
1198 scanlun_or_reset_dev(int bus, int target, int lun, int scan)
1201 struct cam_device *device;
1207 warnx("invalid bus number %d", bus);
1212 warnx("invalid target number %d", target);
1217 warnx("invalid lun number %d", lun);
1223 bzero(&ccb, sizeof(union ccb));
1226 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1227 warnx("error opening tranport layer device %s\n",
1229 warn("%s", XPT_DEVICE);
1233 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
1234 if (device == NULL) {
1235 warnx("%s", cam_errbuf);
1240 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1241 ccb.ccb_h.path_id = bus;
1242 ccb.ccb_h.target_id = target;
1243 ccb.ccb_h.target_lun = lun;
1244 ccb.ccb_h.timeout = 5000;
1245 ccb.crcn.flags = CAM_FLAG_NONE;
1247 /* run this at a low priority */
1248 ccb.ccb_h.pinfo.priority = 5;
1251 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1252 warn("CAMIOCOMMAND ioctl failed");
1257 if (cam_send_ccb(device, &ccb) < 0) {
1258 warn("error sending XPT_RESET_DEV CCB");
1259 cam_close_device(device);
1267 cam_close_device(device);
1270 * An error code of CAM_BDR_SENT is normal for a BDR request.
1272 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1274 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1275 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1276 scan? "Re-scan" : "Reset", bus, target, lun);
1279 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1280 scan? "Re-scan" : "Reset", bus, target, lun,
1281 ccb.ccb_h.status & CAM_STATUS_MASK);
1286 #ifndef MINIMALISTIC
1288 readdefects(struct cam_device *device, int argc, char **argv,
1289 char *combinedopt, int retry_count, int timeout)
1291 union ccb *ccb = NULL;
1292 struct scsi_read_defect_data_10 *rdd_cdb;
1293 u_int8_t *defect_list = NULL;
1294 u_int32_t dlist_length = 65000;
1295 u_int32_t returned_length = 0;
1296 u_int32_t num_returned = 0;
1297 u_int8_t returned_format;
1300 int lists_specified = 0;
1302 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1308 while (isspace(*tstr) && (*tstr != '\0'))
1310 if (strcmp(tstr, "block") == 0)
1311 arglist |= CAM_ARG_FORMAT_BLOCK;
1312 else if (strcmp(tstr, "bfi") == 0)
1313 arglist |= CAM_ARG_FORMAT_BFI;
1314 else if (strcmp(tstr, "phys") == 0)
1315 arglist |= CAM_ARG_FORMAT_PHYS;
1318 warnx("invalid defect format %s", tstr);
1319 goto defect_bailout;
1324 arglist |= CAM_ARG_GLIST;
1327 arglist |= CAM_ARG_PLIST;
1334 ccb = cam_getccb(device);
1337 * Hopefully 65000 bytes is enough to hold the defect list. If it
1338 * isn't, the disk is probably dead already. We'd have to go with
1339 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1342 defect_list = malloc(dlist_length);
1343 if (defect_list == NULL) {
1344 warnx("can't malloc memory for defect list");
1346 goto defect_bailout;
1349 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1352 * cam_getccb() zeros the CCB header only. So we need to zero the
1353 * payload portion of the ccb.
1355 bzero(&(&ccb->ccb_h)[1],
1356 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1358 cam_fill_csio(&ccb->csio,
1359 /*retries*/ retry_count,
1361 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1362 CAM_PASS_ERR_RECOVER : 0),
1363 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1364 /*data_ptr*/ defect_list,
1365 /*dxfer_len*/ dlist_length,
1366 /*sense_len*/ SSD_FULL_SIZE,
1367 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1368 /*timeout*/ timeout ? timeout : 5000);
1370 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1371 if (arglist & CAM_ARG_FORMAT_BLOCK)
1372 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1373 else if (arglist & CAM_ARG_FORMAT_BFI)
1374 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1375 else if (arglist & CAM_ARG_FORMAT_PHYS)
1376 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1379 warnx("no defect list format specified");
1380 goto defect_bailout;
1382 if (arglist & CAM_ARG_PLIST) {
1383 rdd_cdb->format |= SRDD10_PLIST;
1387 if (arglist & CAM_ARG_GLIST) {
1388 rdd_cdb->format |= SRDD10_GLIST;
1392 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1394 /* Disable freezing the device queue */
1395 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1397 if (cam_send_ccb(device, ccb) < 0) {
1398 perror("error reading defect list");
1400 if (arglist & CAM_ARG_VERBOSE) {
1401 cam_error_print(device, ccb, CAM_ESF_ALL,
1402 CAM_EPF_ALL, stderr);
1406 goto defect_bailout;
1409 returned_length = scsi_2btoul(((struct
1410 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1412 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1413 defect_list)->format;
1415 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1416 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1417 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1418 struct scsi_sense_data *sense;
1419 int error_code, sense_key, asc, ascq;
1421 sense = &ccb->csio.sense_data;
1422 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1425 * According to the SCSI spec, if the disk doesn't support
1426 * the requested format, it will generally return a sense
1427 * key of RECOVERED ERROR, and an additional sense code
1428 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1429 * also check to make sure that the returned length is
1430 * greater than 0, and then print out whatever format the
1433 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1434 && (asc == 0x1c) && (ascq == 0x00)
1435 && (returned_length > 0)) {
1436 warnx("requested defect format not available");
1437 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1438 case SRDD10_BLOCK_FORMAT:
1439 warnx("Device returned block format");
1441 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1442 warnx("Device returned bytes from index"
1445 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1446 warnx("Device returned physical sector format");
1450 warnx("Device returned unknown defect"
1451 " data format %#x", returned_format);
1452 goto defect_bailout;
1453 break; /* NOTREACHED */
1457 warnx("Error returned from read defect data command");
1458 if (arglist & CAM_ARG_VERBOSE)
1459 cam_error_print(device, ccb, CAM_ESF_ALL,
1460 CAM_EPF_ALL, stderr);
1461 goto defect_bailout;
1463 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1465 warnx("Error returned from read defect data command");
1466 if (arglist & CAM_ARG_VERBOSE)
1467 cam_error_print(device, ccb, CAM_ESF_ALL,
1468 CAM_EPF_ALL, stderr);
1469 goto defect_bailout;
1473 * XXX KDM I should probably clean up the printout format for the
1476 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1477 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1479 struct scsi_defect_desc_phys_sector *dlist;
1481 dlist = (struct scsi_defect_desc_phys_sector *)
1483 sizeof(struct scsi_read_defect_data_hdr_10));
1485 num_returned = returned_length /
1486 sizeof(struct scsi_defect_desc_phys_sector);
1488 fprintf(stderr, "Got %d defect", num_returned);
1490 if ((lists_specified == 0) || (num_returned == 0)) {
1491 fprintf(stderr, "s.\n");
1493 } else if (num_returned == 1)
1494 fprintf(stderr, ":\n");
1496 fprintf(stderr, "s:\n");
1498 for (i = 0; i < num_returned; i++) {
1499 fprintf(stdout, "%d:%d:%d\n",
1500 scsi_3btoul(dlist[i].cylinder),
1502 scsi_4btoul(dlist[i].sector));
1506 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1508 struct scsi_defect_desc_bytes_from_index *dlist;
1510 dlist = (struct scsi_defect_desc_bytes_from_index *)
1512 sizeof(struct scsi_read_defect_data_hdr_10));
1514 num_returned = returned_length /
1515 sizeof(struct scsi_defect_desc_bytes_from_index);
1517 fprintf(stderr, "Got %d defect", num_returned);
1519 if ((lists_specified == 0) || (num_returned == 0)) {
1520 fprintf(stderr, "s.\n");
1522 } else if (num_returned == 1)
1523 fprintf(stderr, ":\n");
1525 fprintf(stderr, "s:\n");
1527 for (i = 0; i < num_returned; i++) {
1528 fprintf(stdout, "%d:%d:%d\n",
1529 scsi_3btoul(dlist[i].cylinder),
1531 scsi_4btoul(dlist[i].bytes_from_index));
1535 case SRDDH10_BLOCK_FORMAT:
1537 struct scsi_defect_desc_block *dlist;
1539 dlist = (struct scsi_defect_desc_block *)(defect_list +
1540 sizeof(struct scsi_read_defect_data_hdr_10));
1542 num_returned = returned_length /
1543 sizeof(struct scsi_defect_desc_block);
1545 fprintf(stderr, "Got %d defect", num_returned);
1547 if ((lists_specified == 0) || (num_returned == 0)) {
1548 fprintf(stderr, "s.\n");
1550 } else if (num_returned == 1)
1551 fprintf(stderr, ":\n");
1553 fprintf(stderr, "s:\n");
1555 for (i = 0; i < num_returned; i++)
1556 fprintf(stdout, "%u\n",
1557 scsi_4btoul(dlist[i].address));
1561 fprintf(stderr, "Unknown defect format %d\n",
1562 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1568 if (defect_list != NULL)
1576 #endif /* MINIMALISTIC */
1580 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1584 ccb = cam_getccb(device);
1590 #ifndef MINIMALISTIC
1592 mode_sense(struct cam_device *device, int mode_page, int page_control,
1593 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1598 ccb = cam_getccb(device);
1601 errx(1, "mode_sense: couldn't allocate CCB");
1603 bzero(&(&ccb->ccb_h)[1],
1604 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1606 scsi_mode_sense(&ccb->csio,
1607 /* retries */ retry_count,
1609 /* tag_action */ MSG_SIMPLE_Q_TAG,
1611 /* page_code */ page_control << 6,
1612 /* page */ mode_page,
1613 /* param_buf */ data,
1614 /* param_len */ datalen,
1615 /* sense_len */ SSD_FULL_SIZE,
1616 /* timeout */ timeout ? timeout : 5000);
1618 if (arglist & CAM_ARG_ERR_RECOVER)
1619 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1621 /* Disable freezing the device queue */
1622 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1624 if (((retval = cam_send_ccb(device, ccb)) < 0)
1625 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1626 if (arglist & CAM_ARG_VERBOSE) {
1627 cam_error_print(device, ccb, CAM_ESF_ALL,
1628 CAM_EPF_ALL, stderr);
1631 cam_close_device(device);
1633 err(1, "error sending mode sense command");
1635 errx(1, "error sending mode sense command");
1642 mode_select(struct cam_device *device, int save_pages, int retry_count,
1643 int timeout, u_int8_t *data, int datalen)
1648 ccb = cam_getccb(device);
1651 errx(1, "mode_select: couldn't allocate CCB");
1653 bzero(&(&ccb->ccb_h)[1],
1654 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1656 scsi_mode_select(&ccb->csio,
1657 /* retries */ retry_count,
1659 /* tag_action */ MSG_SIMPLE_Q_TAG,
1660 /* scsi_page_fmt */ 1,
1661 /* save_pages */ save_pages,
1662 /* param_buf */ data,
1663 /* param_len */ datalen,
1664 /* sense_len */ SSD_FULL_SIZE,
1665 /* timeout */ timeout ? timeout : 5000);
1667 if (arglist & CAM_ARG_ERR_RECOVER)
1668 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1670 /* Disable freezing the device queue */
1671 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1673 if (((retval = cam_send_ccb(device, ccb)) < 0)
1674 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1675 if (arglist & CAM_ARG_VERBOSE) {
1676 cam_error_print(device, ccb, CAM_ESF_ALL,
1677 CAM_EPF_ALL, stderr);
1680 cam_close_device(device);
1683 err(1, "error sending mode select command");
1685 errx(1, "error sending mode select command");
1693 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1694 int retry_count, int timeout)
1696 int c, mode_page = -1, page_control = 0;
1697 int binary = 0, list = 0;
1699 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1705 arglist |= CAM_ARG_DBD;
1708 arglist |= CAM_ARG_MODE_EDIT;
1714 mode_page = strtol(optarg, NULL, 0);
1716 errx(1, "invalid mode page %d", mode_page);
1719 page_control = strtol(optarg, NULL, 0);
1720 if ((page_control < 0) || (page_control > 3))
1721 errx(1, "invalid page control field %d",
1723 arglist |= CAM_ARG_PAGE_CNTL;
1730 if (mode_page == -1 && list == 0)
1731 errx(1, "you must specify a mode page!");
1734 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1735 retry_count, timeout);
1737 mode_edit(device, mode_page, page_control,
1738 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1739 retry_count, timeout);
1744 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1745 int retry_count, int timeout)
1748 u_int32_t flags = CAM_DIR_NONE;
1749 u_int8_t *data_ptr = NULL;
1751 struct get_hook hook;
1752 int c, data_bytes = 0;
1754 char *datastr = NULL, *tstr;
1759 ccb = cam_getccb(device);
1762 warnx("scsicmd: error allocating ccb");
1766 bzero(&(&ccb->ccb_h)[1],
1767 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1769 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1773 while (isspace(*tstr) && (*tstr != '\0'))
1775 hook.argc = argc - optind;
1776 hook.argv = argv + optind;
1778 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1781 * Increment optind by the number of arguments the
1782 * encoding routine processed. After each call to
1783 * getopt(3), optind points to the argument that
1784 * getopt should process _next_. In this case,
1785 * that means it points to the first command string
1786 * argument, if there is one. Once we increment
1787 * this, it should point to either the next command
1788 * line argument, or it should be past the end of
1794 if (arglist & CAM_ARG_CMD_OUT) {
1795 warnx("command must either be "
1796 "read or write, not both");
1798 goto scsicmd_bailout;
1800 arglist |= CAM_ARG_CMD_IN;
1802 data_bytes = strtol(optarg, NULL, 0);
1803 if (data_bytes <= 0) {
1804 warnx("invalid number of input bytes %d",
1807 goto scsicmd_bailout;
1809 hook.argc = argc - optind;
1810 hook.argv = argv + optind;
1813 datastr = cget(&hook, NULL);
1815 * If the user supplied "-" instead of a format, he
1816 * wants the data to be written to stdout.
1818 if ((datastr != NULL)
1819 && (datastr[0] == '-'))
1822 data_ptr = (u_int8_t *)malloc(data_bytes);
1823 if (data_ptr == NULL) {
1824 warnx("can't malloc memory for data_ptr");
1826 goto scsicmd_bailout;
1830 if (arglist & CAM_ARG_CMD_IN) {
1831 warnx("command must either be "
1832 "read or write, not both");
1834 goto scsicmd_bailout;
1836 arglist |= CAM_ARG_CMD_OUT;
1837 flags = CAM_DIR_OUT;
1838 data_bytes = strtol(optarg, NULL, 0);
1839 if (data_bytes <= 0) {
1840 warnx("invalid number of output bytes %d",
1843 goto scsicmd_bailout;
1845 hook.argc = argc - optind;
1846 hook.argv = argv + optind;
1848 datastr = cget(&hook, NULL);
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;
1856 * If the user supplied "-" instead of a format, he
1857 * wants the data to be read from stdin.
1859 if ((datastr != NULL)
1860 && (datastr[0] == '-'))
1863 buff_encode_visit(data_ptr, data_bytes, datastr,
1873 * If fd_data is set, and we're writing to the device, we need to
1874 * read the data the user wants written from stdin.
1876 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1878 int amt_to_read = data_bytes;
1879 u_int8_t *buf_ptr = data_ptr;
1881 for (amt_read = 0; amt_to_read > 0;
1882 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1883 if (amt_read == -1) {
1884 warn("error reading data from stdin");
1886 goto scsicmd_bailout;
1888 amt_to_read -= amt_read;
1889 buf_ptr += amt_read;
1893 if (arglist & CAM_ARG_ERR_RECOVER)
1894 flags |= CAM_PASS_ERR_RECOVER;
1896 /* Disable freezing the device queue */
1897 flags |= CAM_DEV_QFRZDIS;
1900 * This is taken from the SCSI-3 draft spec.
1901 * (T10/1157D revision 0.3)
1902 * The top 3 bits of an opcode are the group code. The next 5 bits
1903 * are the command code.
1904 * Group 0: six byte commands
1905 * Group 1: ten byte commands
1906 * Group 2: ten byte commands
1908 * Group 4: sixteen byte commands
1909 * Group 5: twelve byte commands
1910 * Group 6: vendor specific
1911 * Group 7: vendor specific
1913 switch((cdb[0] >> 5) & 0x7) {
1924 /* computed by buff_encode_visit */
1935 * We should probably use csio_build_visit or something like that
1936 * here, but it's easier to encode arguments as you go. The
1937 * alternative would be skipping the CDB argument and then encoding
1938 * it here, since we've got the data buffer argument by now.
1940 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1942 cam_fill_csio(&ccb->csio,
1943 /*retries*/ retry_count,
1946 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1947 /*data_ptr*/ data_ptr,
1948 /*dxfer_len*/ data_bytes,
1949 /*sense_len*/ SSD_FULL_SIZE,
1950 /*cdb_len*/ cdb_len,
1951 /*timeout*/ timeout ? timeout : 5000);
1953 if (((retval = cam_send_ccb(device, ccb)) < 0)
1954 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1956 warn("error sending command");
1958 warnx("error sending command");
1960 if (arglist & CAM_ARG_VERBOSE) {
1961 cam_error_print(device, ccb, CAM_ESF_ALL,
1962 CAM_EPF_ALL, stderr);
1966 goto scsicmd_bailout;
1970 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1971 && (arglist & CAM_ARG_CMD_IN)
1972 && (data_bytes > 0)) {
1974 buff_decode_visit(data_ptr, data_bytes, datastr,
1976 fprintf(stdout, "\n");
1978 ssize_t amt_written;
1979 int amt_to_write = data_bytes;
1980 u_int8_t *buf_ptr = data_ptr;
1982 for (amt_written = 0; (amt_to_write > 0) &&
1983 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
1984 amt_to_write -= amt_written;
1985 buf_ptr += amt_written;
1987 if (amt_written == -1) {
1988 warn("error writing data to stdout");
1990 goto scsicmd_bailout;
1991 } else if ((amt_written == 0)
1992 && (amt_to_write > 0)) {
1993 warnx("only wrote %u bytes out of %u",
1994 data_bytes - amt_to_write, data_bytes);
2001 if ((data_bytes > 0) && (data_ptr != NULL))
2010 camdebug(int argc, char **argv, char *combinedopt)
2013 int bus = -1, target = -1, lun = -1;
2014 char *tstr, *tmpstr = NULL;
2018 bzero(&ccb, sizeof(union ccb));
2020 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2023 arglist |= CAM_ARG_DEBUG_INFO;
2024 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2027 arglist |= CAM_ARG_DEBUG_PERIPH;
2028 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2031 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2032 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2035 arglist |= CAM_ARG_DEBUG_TRACE;
2036 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2039 arglist |= CAM_ARG_DEBUG_XPT;
2040 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2043 arglist |= CAM_ARG_DEBUG_CDB;
2044 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2051 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2052 warnx("error opening transport layer device %s", XPT_DEVICE);
2053 warn("%s", XPT_DEVICE);
2060 warnx("you must specify \"off\", \"all\" or a bus,");
2061 warnx("bus:target, or bus:target:lun");
2068 while (isspace(*tstr) && (*tstr != '\0'))
2071 if (strncmp(tstr, "off", 3) == 0) {
2072 ccb.cdbg.flags = CAM_DEBUG_NONE;
2073 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2074 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2076 } else if (strncmp(tstr, "all", 3) != 0) {
2077 tmpstr = (char *)strtok(tstr, ":");
2078 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2079 bus = strtol(tmpstr, NULL, 0);
2080 arglist |= CAM_ARG_BUS;
2081 tmpstr = (char *)strtok(NULL, ":");
2082 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2083 target = strtol(tmpstr, NULL, 0);
2084 arglist |= CAM_ARG_TARGET;
2085 tmpstr = (char *)strtok(NULL, ":");
2086 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2087 lun = strtol(tmpstr, NULL, 0);
2088 arglist |= CAM_ARG_LUN;
2093 warnx("you must specify \"all\", \"off\", or a bus,");
2094 warnx("bus:target, or bus:target:lun to debug");
2100 ccb.ccb_h.func_code = XPT_DEBUG;
2101 ccb.ccb_h.path_id = bus;
2102 ccb.ccb_h.target_id = target;
2103 ccb.ccb_h.target_lun = lun;
2105 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2106 warn("CAMIOCOMMAND ioctl failed");
2111 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2112 CAM_FUNC_NOTAVAIL) {
2113 warnx("CAM debugging not available");
2114 warnx("you need to put options CAMDEBUG in"
2115 " your kernel config file!");
2117 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2119 warnx("XPT_DEBUG CCB failed with status %#x",
2123 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2125 "Debugging turned off\n");
2128 "Debugging enabled for "
2141 tagcontrol(struct cam_device *device, int argc, char **argv,
2151 ccb = cam_getccb(device);
2154 warnx("tagcontrol: error allocating ccb");
2158 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2161 numtags = strtol(optarg, NULL, 0);
2163 warnx("tag count %d is < 0", numtags);
2165 goto tagcontrol_bailout;
2176 cam_path_string(device, pathstr, sizeof(pathstr));
2179 bzero(&(&ccb->ccb_h)[1],
2180 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2181 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2182 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2183 ccb->crs.openings = numtags;
2186 if (cam_send_ccb(device, ccb) < 0) {
2187 perror("error sending XPT_REL_SIMQ CCB");
2189 goto tagcontrol_bailout;
2192 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2193 warnx("XPT_REL_SIMQ CCB failed");
2194 cam_error_print(device, ccb, CAM_ESF_ALL,
2195 CAM_EPF_ALL, stderr);
2197 goto tagcontrol_bailout;
2202 fprintf(stdout, "%stagged openings now %d\n",
2203 pathstr, ccb->crs.openings);
2206 bzero(&(&ccb->ccb_h)[1],
2207 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2209 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2211 if (cam_send_ccb(device, ccb) < 0) {
2212 perror("error sending XPT_GDEV_STATS CCB");
2214 goto tagcontrol_bailout;
2217 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2218 warnx("XPT_GDEV_STATS CCB failed");
2219 cam_error_print(device, ccb, CAM_ESF_ALL,
2220 CAM_EPF_ALL, stderr);
2222 goto tagcontrol_bailout;
2225 if (arglist & CAM_ARG_VERBOSE) {
2226 fprintf(stdout, "%s", pathstr);
2227 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2228 fprintf(stdout, "%s", pathstr);
2229 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2230 fprintf(stdout, "%s", pathstr);
2231 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2232 fprintf(stdout, "%s", pathstr);
2233 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2234 fprintf(stdout, "%s", pathstr);
2235 fprintf(stdout, "held %d\n", ccb->cgds.held);
2236 fprintf(stdout, "%s", pathstr);
2237 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2238 fprintf(stdout, "%s", pathstr);
2239 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2242 fprintf(stdout, "%s", pathstr);
2243 fprintf(stdout, "device openings: ");
2245 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2246 ccb->cgds.dev_active);
2256 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2260 cam_path_string(device, pathstr, sizeof(pathstr));
2262 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) {
2264 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2267 if (cts->sync_offset != 0) {
2270 freq = scsi_calc_syncsrate(cts->sync_period);
2271 fprintf(stdout, "%sfrequency: %d.%03dMHz\n", pathstr,
2272 freq / 1000, freq % 1000);
2276 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID)
2277 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset);
2279 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID)
2280 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2281 (0x01 << cts->bus_width) * 8);
2283 if (cts->valid & CCB_TRANS_DISC_VALID)
2284 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2285 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" :
2288 if (cts->valid & CCB_TRANS_TQ_VALID)
2289 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2290 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" :
2296 * Get a path inquiry CCB for the specified device.
2299 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2304 ccb = cam_getccb(device);
2307 warnx("get_cpi: couldn't allocate CCB");
2311 bzero(&(&ccb->ccb_h)[1],
2312 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2314 ccb->ccb_h.func_code = XPT_PATH_INQ;
2316 if (cam_send_ccb(device, ccb) < 0) {
2317 warn("get_cpi: error sending Path Inquiry CCB");
2319 if (arglist & CAM_ARG_VERBOSE)
2320 cam_error_print(device, ccb, CAM_ESF_ALL,
2321 CAM_EPF_ALL, stderr);
2325 goto get_cpi_bailout;
2328 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2330 if (arglist & CAM_ARG_VERBOSE)
2331 cam_error_print(device, ccb, CAM_ESF_ALL,
2332 CAM_EPF_ALL, stderr);
2336 goto get_cpi_bailout;
2339 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2349 cpi_print(struct ccb_pathinq *cpi)
2351 char adapter_str[1024];
2354 snprintf(adapter_str, sizeof(adapter_str),
2355 "%s%d:", cpi->dev_name, cpi->unit_number);
2357 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2360 for (i = 1; i < 0xff; i = i << 1) {
2363 if ((i & cpi->hba_inquiry) == 0)
2366 fprintf(stdout, "%s supports ", adapter_str);
2370 str = "MDP message";
2373 str = "32 bit wide SCSI";
2376 str = "16 bit wide SCSI";
2379 str = "SDTR message";
2382 str = "linked CDBs";
2385 str = "tag queue messages";
2388 str = "soft reset alternative";
2391 str = "unknown PI bit set";
2394 fprintf(stdout, "%s\n", str);
2397 for (i = 1; i < 0xff; i = i << 1) {
2400 if ((i & cpi->hba_misc) == 0)
2403 fprintf(stdout, "%s ", adapter_str);
2407 str = "bus scans from high ID to low ID";
2410 str = "removable devices not included in scan";
2412 case PIM_NOINITIATOR:
2413 str = "initiator role not supported";
2415 case PIM_NOBUSRESET:
2416 str = "user has disabled initial BUS RESET or"
2417 " controller is in target/mixed mode";
2420 str = "unknown PIM bit set";
2423 fprintf(stdout, "%s\n", str);
2426 for (i = 1; i < 0xff; i = i << 1) {
2429 if ((i & cpi->target_sprt) == 0)
2432 fprintf(stdout, "%s supports ", adapter_str);
2435 str = "target mode processor mode";
2438 str = "target mode phase cog. mode";
2440 case PIT_DISCONNECT:
2441 str = "disconnects in target mode";
2444 str = "terminate I/O message in target mode";
2447 str = "group 6 commands in target mode";
2450 str = "group 7 commands in target mode";
2453 str = "unknown PIT bit set";
2457 fprintf(stdout, "%s\n", str);
2459 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2461 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2463 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2465 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2466 adapter_str, cpi->hpath_id);
2467 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2469 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2470 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2471 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2472 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2473 if (cpi->base_transfer_speed > 1000)
2474 fprintf(stdout, "%d.%03dMB/sec\n",
2475 cpi->base_transfer_speed / 1000,
2476 cpi->base_transfer_speed % 1000);
2478 fprintf(stdout, "%dKB/sec\n",
2479 (cpi->base_transfer_speed % 1000) * 1000);
2483 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2484 struct ccb_trans_settings *cts)
2490 ccb = cam_getccb(device);
2493 warnx("get_print_cts: error allocating ccb");
2497 bzero(&(&ccb->ccb_h)[1],
2498 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2500 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2502 if (user_settings == 0)
2503 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS;
2505 ccb->cts.flags = CCB_TRANS_USER_SETTINGS;
2507 if (cam_send_ccb(device, ccb) < 0) {
2508 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2509 if (arglist & CAM_ARG_VERBOSE)
2510 cam_error_print(device, ccb, CAM_ESF_ALL,
2511 CAM_EPF_ALL, stderr);
2513 goto get_print_cts_bailout;
2516 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2517 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2518 if (arglist & CAM_ARG_VERBOSE)
2519 cam_error_print(device, ccb, CAM_ESF_ALL,
2520 CAM_EPF_ALL, stderr);
2522 goto get_print_cts_bailout;
2526 cts_print(device, &ccb->cts);
2529 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2531 get_print_cts_bailout:
2539 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2540 int argc, char **argv, char *combinedopt)
2544 int user_settings = 0;
2546 int disc_enable = -1, tag_enable = -1;
2548 double syncrate = -1;
2551 int change_settings = 0, send_tur = 0;
2552 struct ccb_pathinq cpi;
2554 ccb = cam_getccb(device);
2557 warnx("ratecontrol: error allocating ccb");
2561 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2570 if (strncasecmp(optarg, "enable", 6) == 0)
2572 else if (strncasecmp(optarg, "disable", 7) == 0)
2575 warnx("-D argument \"%s\" is unknown", optarg);
2577 goto ratecontrol_bailout;
2579 change_settings = 1;
2582 offset = strtol(optarg, NULL, 0);
2584 warnx("offset value %d is < 0", offset);
2586 goto ratecontrol_bailout;
2588 change_settings = 1;
2594 syncrate = atof(optarg);
2597 warnx("sync rate %f is < 0", syncrate);
2599 goto ratecontrol_bailout;
2601 change_settings = 1;
2604 if (strncasecmp(optarg, "enable", 6) == 0)
2606 else if (strncasecmp(optarg, "disable", 7) == 0)
2609 warnx("-T argument \"%s\" is unknown", optarg);
2611 goto ratecontrol_bailout;
2613 change_settings = 1;
2619 bus_width = strtol(optarg, NULL, 0);
2620 if (bus_width < 0) {
2621 warnx("bus width %d is < 0", bus_width);
2623 goto ratecontrol_bailout;
2625 change_settings = 1;
2632 bzero(&(&ccb->ccb_h)[1],
2633 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2636 * Grab path inquiry information, so we can determine whether
2637 * or not the initiator is capable of the things that the user
2640 ccb->ccb_h.func_code = XPT_PATH_INQ;
2642 if (cam_send_ccb(device, ccb) < 0) {
2643 perror("error sending XPT_PATH_INQ CCB");
2644 if (arglist & CAM_ARG_VERBOSE) {
2645 cam_error_print(device, ccb, CAM_ESF_ALL,
2646 CAM_EPF_ALL, stderr);
2649 goto ratecontrol_bailout;
2652 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2653 warnx("XPT_PATH_INQ CCB failed");
2654 if (arglist & CAM_ARG_VERBOSE) {
2655 cam_error_print(device, ccb, CAM_ESF_ALL,
2656 CAM_EPF_ALL, stderr);
2659 goto ratecontrol_bailout;
2662 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2664 bzero(&(&ccb->ccb_h)[1],
2665 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2668 fprintf(stdout, "Current Parameters:\n");
2670 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2673 goto ratecontrol_bailout;
2675 if (arglist & CAM_ARG_VERBOSE)
2678 if (change_settings) {
2679 if (disc_enable != -1) {
2680 ccb->cts.valid |= CCB_TRANS_DISC_VALID;
2681 if (disc_enable == 0)
2682 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB;
2684 ccb->cts.flags |= CCB_TRANS_DISC_ENB;
2686 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID;
2688 if (tag_enable != -1) {
2689 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2690 warnx("HBA does not support tagged queueing, "
2691 "so you cannot modify tag settings");
2693 goto ratecontrol_bailout;
2696 ccb->cts.valid |= CCB_TRANS_TQ_VALID;
2698 if (tag_enable == 0)
2699 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB;
2701 ccb->cts.flags |= CCB_TRANS_TAG_ENB;
2703 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID;
2706 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2707 warnx("HBA at %s%d is not cable of changing "
2708 "offset", cpi.dev_name,
2711 goto ratecontrol_bailout;
2713 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID;
2714 ccb->cts.sync_offset = offset;
2716 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID;
2718 if (syncrate != -1) {
2719 int prelim_sync_period;
2722 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2723 warnx("HBA at %s%d is not cable of changing "
2724 "transfer rates", cpi.dev_name,
2727 goto ratecontrol_bailout;
2730 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID;
2733 * The sync rate the user gives us is in MHz.
2734 * We need to translate it into KHz for this
2740 * Next, we calculate a "preliminary" sync period
2741 * in tenths of a nanosecond.
2744 prelim_sync_period = 0;
2746 prelim_sync_period = 10000000 / syncrate;
2748 ccb->cts.sync_period =
2749 scsi_calc_syncparam(prelim_sync_period);
2751 freq = scsi_calc_syncsrate(ccb->cts.sync_period);
2753 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID;
2756 * The bus_width argument goes like this:
2760 * Therefore, if you shift the number of bits given on the
2761 * command line right by 4, you should get the correct
2764 if (bus_width != -1) {
2767 * We might as well validate things here with a
2768 * decipherable error message, rather than what
2769 * will probably be an indecipherable error message
2770 * by the time it gets back to us.
2772 if ((bus_width == 16)
2773 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2774 warnx("HBA does not support 16 bit bus width");
2776 goto ratecontrol_bailout;
2777 } else if ((bus_width == 32)
2778 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2779 warnx("HBA does not support 32 bit bus width");
2781 goto ratecontrol_bailout;
2782 } else if ((bus_width != 8)
2783 && (bus_width != 16)
2784 && (bus_width != 32)) {
2785 warnx("Invalid bus width %d", bus_width);
2787 goto ratecontrol_bailout;
2790 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID;
2791 ccb->cts.bus_width = bus_width >> 4;
2793 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID;
2795 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2797 if (cam_send_ccb(device, ccb) < 0) {
2798 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2799 if (arglist & CAM_ARG_VERBOSE) {
2800 cam_error_print(device, ccb, CAM_ESF_ALL,
2801 CAM_EPF_ALL, stderr);
2804 goto ratecontrol_bailout;
2807 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2808 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2809 if (arglist & CAM_ARG_VERBOSE) {
2810 cam_error_print(device, ccb, CAM_ESF_ALL,
2811 CAM_EPF_ALL, stderr);
2814 goto ratecontrol_bailout;
2819 retval = testunitready(device, retry_count, timeout,
2820 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2823 * If the TUR didn't succeed, just bail.
2827 fprintf(stderr, "Test Unit Ready failed\n");
2828 goto ratecontrol_bailout;
2832 * If the user wants things quiet, there's no sense in
2833 * getting the transfer settings, if we're not going
2837 goto ratecontrol_bailout;
2839 fprintf(stdout, "New Parameters:\n");
2840 retval = get_print_cts(device, user_settings, 0, NULL);
2843 ratecontrol_bailout:
2850 scsiformat(struct cam_device *device, int argc, char **argv,
2851 char *combinedopt, int retry_count, int timeout)
2855 int ycount = 0, quiet = 0;
2856 int error = 0, response = 0, retval = 0;
2857 int use_timeout = 10800 * 1000;
2859 struct format_defect_list_header fh;
2860 u_int8_t *data_ptr = NULL;
2861 u_int32_t dxfer_len = 0;
2863 int num_warnings = 0;
2866 ccb = cam_getccb(device);
2869 warnx("scsiformat: error allocating ccb");
2873 bzero(&(&ccb->ccb_h)[1],
2874 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2876 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2897 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2898 "following device:\n");
2900 error = scsidoinquiry(device, argc, argv, combinedopt,
2901 retry_count, timeout);
2904 warnx("scsiformat: error sending inquiry");
2905 goto scsiformat_bailout;
2914 fprintf(stdout, "Are you SURE you want to do "
2917 if (fgets(str, sizeof(str), stdin) != NULL) {
2919 if (strncasecmp(str, "yes", 3) == 0)
2921 else if (strncasecmp(str, "no", 2) == 0)
2924 fprintf(stdout, "Please answer"
2925 " \"yes\" or \"no\"\n");
2928 } while (response == 0);
2930 if (response == -1) {
2932 goto scsiformat_bailout;
2937 use_timeout = timeout;
2940 fprintf(stdout, "Current format timeout is %d seconds\n",
2941 use_timeout / 1000);
2945 * If the user hasn't disabled questions and didn't specify a
2946 * timeout on the command line, ask them if they want the current
2950 && (timeout == 0)) {
2952 int new_timeout = 0;
2954 fprintf(stdout, "Enter new timeout in seconds or press\n"
2955 "return to keep the current timeout [%d] ",
2956 use_timeout / 1000);
2958 if (fgets(str, sizeof(str), stdin) != NULL) {
2960 new_timeout = atoi(str);
2963 if (new_timeout != 0) {
2964 use_timeout = new_timeout * 1000;
2965 fprintf(stdout, "Using new timeout value %d\n",
2966 use_timeout / 1000);
2971 * Keep this outside the if block below to silence any unused
2972 * variable warnings.
2974 bzero(&fh, sizeof(fh));
2977 * If we're in immediate mode, we've got to include the format
2980 if (immediate != 0) {
2981 fh.byte2 = FU_DLH_IMMED;
2982 data_ptr = (u_int8_t *)&fh;
2983 dxfer_len = sizeof(fh);
2984 byte2 = FU_FMT_DATA;
2985 } else if (quiet == 0) {
2986 fprintf(stdout, "Formatting...");
2990 scsi_format_unit(&ccb->csio,
2991 /* retries */ retry_count,
2993 /* tag_action */ MSG_SIMPLE_Q_TAG,
2996 /* data_ptr */ data_ptr,
2997 /* dxfer_len */ dxfer_len,
2998 /* sense_len */ SSD_FULL_SIZE,
2999 /* timeout */ use_timeout);
3001 /* Disable freezing the device queue */
3002 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3004 if (arglist & CAM_ARG_ERR_RECOVER)
3005 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3007 if (((retval = cam_send_ccb(device, ccb)) < 0)
3008 || ((immediate == 0)
3009 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3010 const char errstr[] = "error sending format command";
3017 if (arglist & CAM_ARG_VERBOSE) {
3018 cam_error_print(device, ccb, CAM_ESF_ALL,
3019 CAM_EPF_ALL, stderr);
3022 goto scsiformat_bailout;
3026 * If we ran in non-immediate mode, we already checked for errors
3027 * above and printed out any necessary information. If we're in
3028 * immediate mode, we need to loop through and get status
3029 * information periodically.
3031 if (immediate == 0) {
3033 fprintf(stdout, "Format Complete\n");
3035 goto scsiformat_bailout;
3042 bzero(&(&ccb->ccb_h)[1],
3043 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3046 * There's really no need to do error recovery or
3047 * retries here, since we're just going to sit in a
3048 * loop and wait for the device to finish formatting.
3050 scsi_test_unit_ready(&ccb->csio,
3053 /* tag_action */ MSG_SIMPLE_Q_TAG,
3054 /* sense_len */ SSD_FULL_SIZE,
3055 /* timeout */ 5000);
3057 /* Disable freezing the device queue */
3058 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3060 retval = cam_send_ccb(device, ccb);
3063 * If we get an error from the ioctl, bail out. SCSI
3064 * errors are expected.
3067 warn("error sending CAMIOCOMMAND ioctl");
3068 if (arglist & CAM_ARG_VERBOSE) {
3069 cam_error_print(device, ccb, CAM_ESF_ALL,
3070 CAM_EPF_ALL, stderr);
3073 goto scsiformat_bailout;
3076 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3078 if ((status != CAM_REQ_CMP)
3079 && (status == CAM_SCSI_STATUS_ERROR)
3080 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3081 struct scsi_sense_data *sense;
3082 int error_code, sense_key, asc, ascq;
3084 sense = &ccb->csio.sense_data;
3085 scsi_extract_sense(sense, &error_code, &sense_key,
3089 * According to the SCSI-2 and SCSI-3 specs, a
3090 * drive that is in the middle of a format should
3091 * return NOT READY with an ASC of "logical unit
3092 * not ready, format in progress". The sense key
3093 * specific bytes will then be a progress indicator.
3095 if ((sense_key == SSD_KEY_NOT_READY)
3096 && (asc == 0x04) && (ascq == 0x04)) {
3097 if ((sense->extra_len >= 10)
3098 && ((sense->sense_key_spec[0] &
3099 SSD_SCS_VALID) != 0)
3102 u_int64_t percentage;
3105 &sense->sense_key_spec[1]);
3106 percentage = 10000 * val;
3109 "\rFormatting: %ju.%02u %% "
3111 (uintmax_t)(percentage /
3113 (unsigned)((percentage /
3117 } else if ((quiet == 0)
3118 && (++num_warnings <= 1)) {
3119 warnx("Unexpected SCSI Sense Key "
3120 "Specific value returned "
3122 scsi_sense_print(device, &ccb->csio,
3124 warnx("Unable to print status "
3125 "information, but format will "
3127 warnx("will exit when format is "
3132 warnx("Unexpected SCSI error during format");
3133 cam_error_print(device, ccb, CAM_ESF_ALL,
3134 CAM_EPF_ALL, stderr);
3136 goto scsiformat_bailout;
3139 } else if (status != CAM_REQ_CMP) {
3140 warnx("Unexpected CAM status %#x", status);
3141 if (arglist & CAM_ARG_VERBOSE)
3142 cam_error_print(device, ccb, CAM_ESF_ALL,
3143 CAM_EPF_ALL, stderr);
3145 goto scsiformat_bailout;
3148 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3151 fprintf(stdout, "\nFormat Complete\n");
3161 scsireportluns(struct cam_device *device, int argc, char **argv,
3162 char *combinedopt, int retry_count, int timeout)
3165 int c, countonly, lunsonly;
3166 struct scsi_report_luns_data *lundata;
3168 uint8_t report_type;
3169 uint32_t list_len, i, j;
3174 report_type = RPL_REPORT_DEFAULT;
3175 ccb = cam_getccb(device);
3178 warnx("%s: error allocating ccb", __func__);
3182 bzero(&(&ccb->ccb_h)[1],
3183 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3188 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3197 if (strcasecmp(optarg, "default") == 0)
3198 report_type = RPL_REPORT_DEFAULT;
3199 else if (strcasecmp(optarg, "wellknown") == 0)
3200 report_type = RPL_REPORT_WELLKNOWN;
3201 else if (strcasecmp(optarg, "all") == 0)
3202 report_type = RPL_REPORT_ALL;
3204 warnx("%s: invalid report type \"%s\"",
3215 if ((countonly != 0)
3216 && (lunsonly != 0)) {
3217 warnx("%s: you can only specify one of -c or -l", __func__);
3222 * According to SPC-4, the allocation length must be at least 16
3223 * bytes -- enough for the header and one LUN.
3225 alloc_len = sizeof(*lundata) + 8;
3229 lundata = malloc(alloc_len);
3231 if (lundata == NULL) {
3232 warn("%s: error mallocing %d bytes", __func__, alloc_len);
3237 scsi_report_luns(&ccb->csio,
3238 /*retries*/ retry_count,
3240 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3241 /*select_report*/ report_type,
3242 /*rpl_buf*/ lundata,
3243 /*alloc_len*/ alloc_len,
3244 /*sense_len*/ SSD_FULL_SIZE,
3245 /*timeout*/ timeout ? timeout : 5000);
3247 /* Disable freezing the device queue */
3248 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3250 if (arglist & CAM_ARG_ERR_RECOVER)
3251 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3253 if (cam_send_ccb(device, ccb) < 0) {
3254 warn("error sending REPORT LUNS command");
3256 if (arglist & CAM_ARG_VERBOSE)
3257 cam_error_print(device, ccb, CAM_ESF_ALL,
3258 CAM_EPF_ALL, stderr);
3264 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3265 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3271 list_len = scsi_4btoul(lundata->length);
3274 * If we need to list the LUNs, and our allocation
3275 * length was too short, reallocate and retry.
3277 if ((countonly == 0)
3278 && (list_len > (alloc_len - sizeof(*lundata)))) {
3279 alloc_len = list_len + sizeof(*lundata);
3285 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
3286 ((list_len / 8) > 1) ? "s" : "");
3291 for (i = 0; i < (list_len / 8); i++) {
3295 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
3297 fprintf(stdout, ",");
3298 switch (lundata->luns[i].lundata[j] &
3299 RPL_LUNDATA_ATYP_MASK) {
3300 case RPL_LUNDATA_ATYP_PERIPH:
3301 if ((lundata->luns[i].lundata[j] &
3302 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
3303 fprintf(stdout, "%d:",
3304 lundata->luns[i].lundata[j] &
3305 RPL_LUNDATA_PERIPH_BUS_MASK);
3307 && ((lundata->luns[i].lundata[j+2] &
3308 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
3311 fprintf(stdout, "%d",
3312 lundata->luns[i].lundata[j+1]);
3314 case RPL_LUNDATA_ATYP_FLAT: {
3316 tmplun[0] = lundata->luns[i].lundata[j] &
3317 RPL_LUNDATA_FLAT_LUN_MASK;
3318 tmplun[1] = lundata->luns[i].lundata[j+1];
3320 fprintf(stdout, "%d", scsi_2btoul(tmplun));
3324 case RPL_LUNDATA_ATYP_LUN:
3325 fprintf(stdout, "%d:%d:%d",
3326 (lundata->luns[i].lundata[j+1] &
3327 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
3328 lundata->luns[i].lundata[j] &
3329 RPL_LUNDATA_LUN_TARG_MASK,
3330 lundata->luns[i].lundata[j+1] &
3331 RPL_LUNDATA_LUN_LUN_MASK);
3333 case RPL_LUNDATA_ATYP_EXTLUN: {
3334 int field_len, field_len_code, eam_code;
3336 eam_code = lundata->luns[i].lundata[j] &
3337 RPL_LUNDATA_EXT_EAM_MASK;
3338 field_len_code = (lundata->luns[i].lundata[j] &
3339 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
3340 field_len = field_len_code * 2;
3342 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
3343 && (field_len_code == 0x00)) {
3344 fprintf(stdout, "%d",
3345 lundata->luns[i].lundata[j+1]);
3346 } else if ((eam_code ==
3347 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
3348 && (field_len_code == 0x03)) {
3352 * This format takes up all 8 bytes.
3353 * If we aren't starting at offset 0,
3357 fprintf(stdout, "Invalid "
3360 "specified format", j);
3364 bzero(tmp_lun, sizeof(tmp_lun));
3365 bcopy(&lundata->luns[i].lundata[j+1],
3366 &tmp_lun[1], sizeof(tmp_lun) - 1);
3367 fprintf(stdout, "%#jx",
3368 (intmax_t)scsi_8btou64(tmp_lun));
3371 fprintf(stderr, "Unknown Extended LUN"
3372 "Address method %#x, length "
3373 "code %#x", eam_code,
3380 fprintf(stderr, "Unknown LUN address method "
3381 "%#x\n", lundata->luns[i].lundata[0] &
3382 RPL_LUNDATA_ATYP_MASK);
3386 * For the flat addressing method, there are no
3387 * other levels after it.
3392 fprintf(stdout, "\n");
3404 #endif /* MINIMALISTIC */
3409 fprintf(verbose ? stdout : stderr,
3410 "usage: camcontrol <command> [device id][generic args][command args]\n"
3411 " camcontrol devlist [-v]\n"
3412 #ifndef MINIMALISTIC
3413 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3414 " camcontrol tur [dev_id][generic args]\n"
3415 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3416 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
3417 " camcontrol start [dev_id][generic args]\n"
3418 " camcontrol stop [dev_id][generic args]\n"
3419 " camcontrol load [dev_id][generic args]\n"
3420 " camcontrol eject [dev_id][generic args]\n"
3421 #endif /* MINIMALISTIC */
3422 " camcontrol rescan <all | bus[:target:lun]>\n"
3423 " camcontrol reset <all | bus[:target:lun]>\n"
3424 #ifndef MINIMALISTIC
3425 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3426 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3427 " [-P pagectl][-e | -b][-d]\n"
3428 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3429 " [-i len fmt|-o len fmt [args]]\n"
3430 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3431 " <all|bus[:target[:lun]]|off>\n"
3432 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3433 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3434 " [-D <enable|disable>][-O offset][-q]\n"
3435 " [-R syncrate][-v][-T <enable|disable>]\n"
3436 " [-U][-W bus_width]\n"
3437 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
3438 #endif /* MINIMALISTIC */
3439 " camcontrol help\n");
3442 #ifndef MINIMALISTIC
3444 "Specify one of the following options:\n"
3445 "devlist list all CAM devices\n"
3446 "periphlist list all CAM peripheral drivers attached to a device\n"
3447 "tur send a test unit ready to the named device\n"
3448 "inquiry send a SCSI inquiry command to the named device\n"
3449 "reportluns send a SCSI report luns command to the device\n"
3450 "start send a Start Unit command to the device\n"
3451 "stop send a Stop Unit command to the device\n"
3452 "load send a Start Unit command to the device with the load bit set\n"
3453 "eject send a Stop Unit command to the device with the eject bit set\n"
3454 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3455 "reset reset all busses, the given bus, or bus:target:lun\n"
3456 "defects read the defect list of the specified device\n"
3457 "modepage display or edit (-e) the given mode page\n"
3458 "cmd send the given scsi command, may need -i or -o as well\n"
3459 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3460 "tags report or set the number of transaction slots for a device\n"
3461 "negotiate report or set device negotiation parameters\n"
3462 "format send the SCSI FORMAT UNIT command to the named device\n"
3463 "help this message\n"
3464 "Device Identifiers:\n"
3465 "bus:target specify the bus and target, lun defaults to 0\n"
3466 "bus:target:lun specify the bus, target and lun\n"
3467 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3468 "Generic arguments:\n"
3469 "-v be verbose, print out sense information\n"
3470 "-t timeout command timeout in seconds, overrides default timeout\n"
3471 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3472 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3473 "-E have the kernel attempt to perform SCSI error recovery\n"
3474 "-C count specify the SCSI command retry count (needs -E to work)\n"
3475 "modepage arguments:\n"
3476 "-l list all available mode pages\n"
3477 "-m page specify the mode page to view or edit\n"
3478 "-e edit the specified mode page\n"
3479 "-b force view to binary mode\n"
3480 "-d disable block descriptors for mode sense\n"
3481 "-P pgctl page control field 0-3\n"
3482 "defects arguments:\n"
3483 "-f format specify defect list format (block, bfi or phys)\n"
3484 "-G get the grown defect list\n"
3485 "-P get the permanant defect list\n"
3486 "inquiry arguments:\n"
3487 "-D get the standard inquiry data\n"
3488 "-S get the serial number\n"
3489 "-R get the transfer rate, etc.\n"
3490 "reportluns arguments:\n"
3491 "-c only report a count of available LUNs\n"
3492 "-l only print out luns, and not a count\n"
3493 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
3495 "-c cdb [args] specify the SCSI CDB\n"
3496 "-i len fmt specify input data and input data format\n"
3497 "-o len fmt [args] specify output data and output data fmt\n"
3498 "debug arguments:\n"
3499 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3500 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3501 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3502 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3504 "-N tags specify the number of tags to use for this device\n"
3505 "-q be quiet, don't report the number of tags\n"
3506 "-v report a number of tag-related parameters\n"
3507 "negotiate arguments:\n"
3508 "-a send a test unit ready after negotiation\n"
3509 "-c report/set current negotiation settings\n"
3510 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3511 "-O offset set command delay offset\n"
3512 "-q be quiet, don't report anything\n"
3513 "-R syncrate synchronization rate in MHz\n"
3514 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3515 "-U report/set user negotiation settings\n"
3516 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3517 "-v also print a Path Inquiry CCB for the controller\n"
3518 "format arguments:\n"
3519 "-q be quiet, don't print status messages\n"
3520 "-r run in report only mode\n"
3521 "-w don't send immediate format command\n"
3522 "-y don't ask any questions\n");
3523 #endif /* MINIMALISTIC */
3527 main(int argc, char **argv)
3530 char *device = NULL;
3532 struct cam_device *cam_dev = NULL;
3533 int timeout = 0, retry_count = 1;
3534 camcontrol_optret optreturn;
3536 const char *mainopt = "C:En:t:u:v";
3537 const char *subopt = NULL;
3538 char combinedopt[256];
3539 int error = 0, optstart = 2;
3541 #ifndef MINIMALISTIC
3542 int bus, target, lun;
3543 #endif /* MINIMALISTIC */
3545 cmdlist = CAM_CMD_NONE;
3546 arglist = CAM_ARG_NONE;
3554 * Get the base option.
3556 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3558 if (optreturn == CC_OR_AMBIGUOUS) {
3559 warnx("ambiguous option %s", argv[1]);
3562 } else if (optreturn == CC_OR_NOT_FOUND) {
3563 warnx("option %s not found", argv[1]);
3569 * Ahh, getopt(3) is a pain.
3571 * This is a gross hack. There really aren't many other good
3572 * options (excuse the pun) for parsing options in a situation like
3573 * this. getopt is kinda braindead, so you end up having to run
3574 * through the options twice, and give each invocation of getopt
3575 * the option string for the other invocation.
3577 * You would think that you could just have two groups of options.
3578 * The first group would get parsed by the first invocation of
3579 * getopt, and the second group would get parsed by the second
3580 * invocation of getopt. It doesn't quite work out that way. When
3581 * the first invocation of getopt finishes, it leaves optind pointing
3582 * to the argument _after_ the first argument in the second group.
3583 * So when the second invocation of getopt comes around, it doesn't
3584 * recognize the first argument it gets and then bails out.
3586 * A nice alternative would be to have a flag for getopt that says
3587 * "just keep parsing arguments even when you encounter an unknown
3588 * argument", but there isn't one. So there's no real clean way to
3589 * easily parse two sets of arguments without having one invocation
3590 * of getopt know about the other.
3592 * Without this hack, the first invocation of getopt would work as
3593 * long as the generic arguments are first, but the second invocation
3594 * (in the subfunction) would fail in one of two ways. In the case
3595 * where you don't set optreset, it would fail because optind may be
3596 * pointing to the argument after the one it should be pointing at.
3597 * In the case where you do set optreset, and reset optind, it would
3598 * fail because getopt would run into the first set of options, which
3599 * it doesn't understand.
3601 * All of this would "sort of" work if you could somehow figure out
3602 * whether optind had been incremented one option too far. The
3603 * mechanics of that, however, are more daunting than just giving
3604 * both invocations all of the expect options for either invocation.
3606 * Needless to say, I wouldn't mind if someone invented a better
3607 * (non-GPL!) command line parsing interface than getopt. I
3608 * wouldn't mind if someone added more knobs to getopt to make it
3609 * work better. Who knows, I may talk myself into doing it someday,
3610 * if the standards weenies let me. As it is, it just leads to
3611 * hackery like this and causes people to avoid it in some cases.
3613 * KDM, September 8th, 1998
3616 sprintf(combinedopt, "%s%s", mainopt, subopt);
3618 sprintf(combinedopt, "%s", mainopt);
3621 * For these options we do not parse optional device arguments and
3622 * we do not open a passthrough device.
3624 if ((cmdlist == CAM_CMD_RESCAN)
3625 || (cmdlist == CAM_CMD_RESET)
3626 || (cmdlist == CAM_CMD_DEVTREE)
3627 || (cmdlist == CAM_CMD_USAGE)
3628 || (cmdlist == CAM_CMD_DEBUG))
3631 #ifndef MINIMALISTIC
3633 && (argc > 2 && argv[2][0] != '-')) {
3638 * First catch people who try to do things like:
3639 * camcontrol tur /dev/da0
3640 * camcontrol doesn't take device nodes as arguments.
3642 if (argv[2][0] == '/') {
3643 warnx("%s is not a valid device identifier", argv[2]);
3644 errx(1, "please read the camcontrol(8) man page");
3645 } else if (isdigit(argv[2][0])) {
3646 /* device specified as bus:target[:lun] */
3647 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3649 errx(1, "numeric device specification must "
3650 "be either bus:target, or "
3652 /* default to 0 if lun was not specified */
3653 if ((arglist & CAM_ARG_LUN) == 0) {
3655 arglist |= CAM_ARG_LUN;
3659 if (cam_get_device(argv[2], name, sizeof name, &unit)
3661 errx(1, "%s", cam_errbuf);
3662 device = strdup(name);
3663 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3667 #endif /* MINIMALISTIC */
3669 * Start getopt processing at argv[2/3], since we've already
3670 * accepted argv[1..2] as the command name, and as a possible
3676 * Now we run through the argument list looking for generic
3677 * options, and ignoring options that possibly belong to
3680 while ((c = getopt(argc, argv, combinedopt))!= -1){
3683 retry_count = strtol(optarg, NULL, 0);
3684 if (retry_count < 0)
3685 errx(1, "retry count %d is < 0",
3687 arglist |= CAM_ARG_RETRIES;
3690 arglist |= CAM_ARG_ERR_RECOVER;
3693 arglist |= CAM_ARG_DEVICE;
3695 while (isspace(*tstr) && (*tstr != '\0'))
3697 device = (char *)strdup(tstr);
3700 timeout = strtol(optarg, NULL, 0);
3702 errx(1, "invalid timeout %d", timeout);
3703 /* Convert the timeout from seconds to ms */
3705 arglist |= CAM_ARG_TIMEOUT;
3708 arglist |= CAM_ARG_UNIT;
3709 unit = strtol(optarg, NULL, 0);
3712 arglist |= CAM_ARG_VERBOSE;
3719 #ifndef MINIMALISTIC
3721 * For most commands we'll want to open the passthrough device
3722 * associated with the specified device. In the case of the rescan
3723 * commands, we don't use a passthrough device at all, just the
3724 * transport layer device.
3727 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3728 && (((arglist & CAM_ARG_DEVICE) == 0)
3729 || ((arglist & CAM_ARG_UNIT) == 0))) {
3730 errx(1, "subcommand \"%s\" requires a valid device "
3731 "identifier", argv[1]);
3734 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3735 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3736 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3738 errx(1,"%s", cam_errbuf);
3740 #endif /* MINIMALISTIC */
3743 * Reset optind to 2, and reset getopt, so these routines can parse
3744 * the arguments again.
3750 #ifndef MINIMALISTIC
3751 case CAM_CMD_DEVLIST:
3752 error = getdevlist(cam_dev);
3754 #endif /* MINIMALISTIC */
3755 case CAM_CMD_DEVTREE:
3756 error = getdevtree();
3758 #ifndef MINIMALISTIC
3760 error = testunitready(cam_dev, retry_count, timeout, 0);
3762 case CAM_CMD_INQUIRY:
3763 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
3764 retry_count, timeout);
3766 case CAM_CMD_STARTSTOP:
3767 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
3768 arglist & CAM_ARG_EJECT, retry_count,
3771 #endif /* MINIMALISTIC */
3772 case CAM_CMD_RESCAN:
3773 error = dorescan_or_reset(argc, argv, 1);
3776 error = dorescan_or_reset(argc, argv, 0);
3778 #ifndef MINIMALISTIC
3779 case CAM_CMD_READ_DEFECTS:
3780 error = readdefects(cam_dev, argc, argv, combinedopt,
3781 retry_count, timeout);
3783 case CAM_CMD_MODE_PAGE:
3784 modepage(cam_dev, argc, argv, combinedopt,
3785 retry_count, timeout);
3787 case CAM_CMD_SCSI_CMD:
3788 error = scsicmd(cam_dev, argc, argv, combinedopt,
3789 retry_count, timeout);
3792 error = camdebug(argc, argv, combinedopt);
3795 error = tagcontrol(cam_dev, argc, argv, combinedopt);
3798 error = ratecontrol(cam_dev, retry_count, timeout,
3799 argc, argv, combinedopt);
3801 case CAM_CMD_FORMAT:
3802 error = scsiformat(cam_dev, argc, argv,
3803 combinedopt, retry_count, timeout);
3805 case CAM_CMD_REPORTLUNS:
3806 error = scsireportluns(cam_dev, argc, argv,
3807 combinedopt, retry_count,
3810 #endif /* MINIMALISTIC */
3820 if (cam_dev != NULL)
3821 cam_close_device(cam_dev);