2 * Copyright (c) 1997-2007 Kenneth D. Merry
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sbin/camcontrol/camcontrol.c,v 1.21.2.13 2003/01/08 17:55:02 njl Exp $
29 * $DragonFly: src/sbin/camcontrol/camcontrol.c,v 1.13 2008/04/20 13:44:24 swildner Exp $
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
44 #include <cam/cam_debug.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_da.h>
48 #include <cam/scsi/scsi_pass.h>
49 #include <cam/scsi/scsi_message.h>
51 #include "camcontrol.h"
54 CAM_CMD_NONE = 0x00000000,
55 CAM_CMD_DEVLIST = 0x00000001,
56 CAM_CMD_TUR = 0x00000002,
57 CAM_CMD_INQUIRY = 0x00000003,
58 CAM_CMD_STARTSTOP = 0x00000004,
59 CAM_CMD_RESCAN = 0x00000005,
60 CAM_CMD_READ_DEFECTS = 0x00000006,
61 CAM_CMD_MODE_PAGE = 0x00000007,
62 CAM_CMD_SCSI_CMD = 0x00000008,
63 CAM_CMD_DEVTREE = 0x00000009,
64 CAM_CMD_USAGE = 0x0000000a,
65 CAM_CMD_DEBUG = 0x0000000b,
66 CAM_CMD_RESET = 0x0000000c,
67 CAM_CMD_FORMAT = 0x0000000d,
68 CAM_CMD_TAG = 0x0000000e,
69 CAM_CMD_RATE = 0x0000000f,
70 CAM_CMD_DETACH = 0x00000010,
71 CAM_CMD_REPORTLUNS = 0x00000011,
72 CAM_CMD_READCAP = 0x00000012
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 {"readcapacity", CAM_CMD_READCAP, CAM_ARG_NONE, "bhHNqs"},
133 #endif /* MINIMALISTIC */
134 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
135 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
137 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
138 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
139 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
140 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
141 #endif /* MINIMALISTIC */
142 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
144 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
145 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
146 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
147 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
148 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
149 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
150 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
151 #endif /* MINIMALISTIC */
152 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
153 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
154 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
166 int bus, target, lun;
169 camcontrol_optret getoption(char *, cam_cmdmask *, cam_argmask *,
172 static int getdevlist(struct cam_device *);
173 static int getdevtree(void);
174 static int testunitready(struct cam_device *, int, int, int);
175 static int scsistart(struct cam_device *, int, int, int, int);
176 static int scsidoinquiry(struct cam_device *, int, char **, char *, int,
178 static int scsiinquiry(struct cam_device *, int, int);
179 static int scsiserial(struct cam_device *, int, int);
180 static int scsixferrate(struct cam_device *);
181 #endif /* MINIMALISTIC */
182 static int parse_btl(char *, int *, int *, int *, cam_argmask *);
183 static int dorescan_or_reset(int, char **, int);
184 static int rescan_or_reset_bus(int, int);
185 static int scanlun_or_reset_dev(int, int, int, int);
187 static int readdefects(struct cam_device *, int, char **, char *, int,
189 static void modepage(struct cam_device *, int, char **, char *, int, int);
190 static int scsicmd(struct cam_device *, int, char **, char *, int, int);
191 static int tagcontrol(struct cam_device *, int, char **, char *);
192 static void cts_print(struct cam_device *device,
193 struct ccb_trans_settings *);
194 static void cpi_print(struct ccb_pathinq *);
195 static int get_cpi(struct cam_device *, struct ccb_pathinq *);
196 static int get_print_cts(struct cam_device *, int, int,
197 struct ccb_trans_settings *);
198 static int ratecontrol(struct cam_device *, int, int, int, char **,
200 static int scsiformat(struct cam_device *, int, char **, char *, int, int);
201 static int scsireportluns(struct cam_device *device, int argc, char **argv,
202 char *combinedopt, int retry_count, int timeout);
203 static int scsireadcapacity(struct cam_device *device, int argc,
204 char **argv, char *combinedopt,
205 int retry_count, int timeout);
206 #endif /* MINIMALISTIC */
210 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
213 struct camcontrol_opts *opts;
216 for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
218 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
219 *cmdnum = opts->cmdnum;
220 *argnum = opts->argnum;
221 *subopt = opts->subopt;
222 if (++num_matches > 1)
223 return(CC_OR_AMBIGUOUS);
230 return(CC_OR_NOT_FOUND);
235 getdevlist(struct cam_device *device)
241 ccb = cam_getccb(device);
243 ccb->ccb_h.func_code = XPT_GDEVLIST;
244 ccb->ccb_h.flags = CAM_DIR_NONE;
245 ccb->ccb_h.retry_count = 1;
247 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
248 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
249 if (cam_send_ccb(device, ccb) < 0) {
250 perror("error getting device list");
257 switch (ccb->cgdl.status) {
258 case CAM_GDEVLIST_MORE_DEVS:
259 strcpy(status, "MORE");
261 case CAM_GDEVLIST_LAST_DEVICE:
262 strcpy(status, "LAST");
264 case CAM_GDEVLIST_LIST_CHANGED:
265 strcpy(status, "CHANGED");
267 case CAM_GDEVLIST_ERROR:
268 strcpy(status, "ERROR");
273 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
274 ccb->cgdl.periph_name,
275 ccb->cgdl.unit_number,
276 ccb->cgdl.generation,
281 * If the list has changed, we need to start over from the
284 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
292 #endif /* MINIMALISTIC */
304 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
305 warn("couldn't open %s", XPT_DEVICE);
309 bzero(&ccb, sizeof(union ccb));
311 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
312 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
313 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
315 ccb.ccb_h.func_code = XPT_DEV_MATCH;
316 bufsize = sizeof(struct dev_match_result) * 100;
317 ccb.cdm.match_buf_len = bufsize;
318 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
319 if (ccb.cdm.matches == NULL) {
320 warnx("can't malloc memory for matches");
324 ccb.cdm.num_matches = 0;
327 * We fetch all nodes, since we display most of them in the default
328 * case, and all in the verbose case.
330 ccb.cdm.num_patterns = 0;
331 ccb.cdm.pattern_buf_len = 0;
334 * We do the ioctl multiple times if necessary, in case there are
335 * more than 100 nodes in the EDT.
338 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
339 warn("error sending CAMIOCOMMAND ioctl");
344 if ((ccb.ccb_h.status != CAM_REQ_CMP)
345 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
346 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
347 warnx("got CAM error %#x, CDM error %d\n",
348 ccb.ccb_h.status, ccb.cdm.status);
353 for (i = 0; i < ccb.cdm.num_matches; i++) {
354 switch (ccb.cdm.matches[i].type) {
355 case DEV_MATCH_BUS: {
356 struct bus_match_result *bus_result;
359 * Only print the bus information if the
360 * user turns on the verbose flag.
362 if ((arglist & CAM_ARG_VERBOSE) == 0)
366 &ccb.cdm.matches[i].result.bus_result;
369 fprintf(stdout, ")\n");
373 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
375 bus_result->dev_name,
376 bus_result->unit_number,
380 case DEV_MATCH_DEVICE: {
381 struct device_match_result *dev_result;
382 char vendor[16], product[48], revision[16];
386 &ccb.cdm.matches[i].result.device_result;
388 if ((dev_result->flags
389 & DEV_RESULT_UNCONFIGURED)
390 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
396 cam_strvis(vendor, dev_result->inq_data.vendor,
397 sizeof(dev_result->inq_data.vendor),
400 dev_result->inq_data.product,
401 sizeof(dev_result->inq_data.product),
404 dev_result->inq_data.revision,
405 sizeof(dev_result->inq_data.revision),
407 sprintf(tmpstr, "<%s %s %s>", vendor, product,
410 fprintf(stdout, ")\n");
414 fprintf(stdout, "%-33s at scbus%d "
415 "target %d lun %d (",
418 dev_result->target_id,
419 dev_result->target_lun);
425 case DEV_MATCH_PERIPH: {
426 struct periph_match_result *periph_result;
429 &ccb.cdm.matches[i].result.periph_result;
431 if (skip_device != 0)
435 fprintf(stdout, ",");
437 fprintf(stdout, "%s%d",
438 periph_result->periph_name,
439 periph_result->unit_number);
445 fprintf(stdout, "unknown match type\n");
450 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
451 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
454 fprintf(stdout, ")\n");
463 testunitready(struct cam_device *device, int retry_count, int timeout,
469 ccb = cam_getccb(device);
471 scsi_test_unit_ready(&ccb->csio,
472 /* retries */ retry_count,
474 /* tag_action */ MSG_SIMPLE_Q_TAG,
475 /* sense_len */ SSD_FULL_SIZE,
476 /* timeout */ timeout ? timeout : 5000);
478 /* Disable freezing the device queue */
479 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
481 if (arglist & CAM_ARG_ERR_RECOVER)
482 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
484 if (cam_send_ccb(device, ccb) < 0) {
486 perror("error sending test unit ready");
488 if (arglist & CAM_ARG_VERBOSE) {
489 cam_error_print(device, ccb, CAM_ESF_ALL,
490 CAM_EPF_ALL, stderr);
497 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
499 fprintf(stdout, "Unit is ready\n");
502 fprintf(stdout, "Unit is not ready\n");
505 if (arglist & CAM_ARG_VERBOSE) {
506 cam_error_print(device, ccb, CAM_ESF_ALL,
507 CAM_EPF_ALL, stderr);
517 scsistart(struct cam_device *device, int startstop, int loadeject,
518 int retry_count, int timeout)
523 ccb = cam_getccb(device);
526 * If we're stopping, send an ordered tag so the drive in question
527 * will finish any previously queued writes before stopping. If
528 * the device isn't capable of tagged queueing, or if tagged
529 * queueing is turned off, the tag action is a no-op.
531 scsi_start_stop(&ccb->csio,
532 /* retries */ retry_count,
534 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
536 /* start/stop */ startstop,
537 /* load_eject */ loadeject,
539 /* sense_len */ SSD_FULL_SIZE,
540 /* timeout */ timeout ? timeout : 120000);
542 /* Disable freezing the device queue */
543 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
545 if (arglist & CAM_ARG_ERR_RECOVER)
546 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
548 if (cam_send_ccb(device, ccb) < 0) {
549 perror("error sending start unit");
551 if (arglist & CAM_ARG_VERBOSE) {
552 cam_error_print(device, ccb, CAM_ESF_ALL,
553 CAM_EPF_ALL, stderr);
560 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
562 fprintf(stdout, "Unit started successfully");
564 fprintf(stdout,", Media loaded\n");
566 fprintf(stdout,"\n");
568 fprintf(stdout, "Unit stopped successfully");
570 fprintf(stdout, ", Media ejected\n");
572 fprintf(stdout, "\n");
578 "Error received from start unit command\n");
581 "Error received from stop unit command\n");
583 if (arglist & CAM_ARG_VERBOSE) {
584 cam_error_print(device, ccb, CAM_ESF_ALL,
585 CAM_EPF_ALL, stderr);
595 scsidoinquiry(struct cam_device *device, int argc, char **argv,
596 char *combinedopt, int retry_count, int timeout)
601 while ((c = getopt(argc, argv, combinedopt)) != -1) {
604 arglist |= CAM_ARG_GET_STDINQ;
607 arglist |= CAM_ARG_GET_XFERRATE;
610 arglist |= CAM_ARG_GET_SERIAL;
618 * If the user didn't specify any inquiry options, he wants all of
621 if ((arglist & CAM_ARG_INQ_MASK) == 0)
622 arglist |= CAM_ARG_INQ_MASK;
624 if (arglist & CAM_ARG_GET_STDINQ)
625 error = scsiinquiry(device, retry_count, timeout);
630 if (arglist & CAM_ARG_GET_SERIAL)
631 scsiserial(device, retry_count, timeout);
636 if (arglist & CAM_ARG_GET_XFERRATE)
637 error = scsixferrate(device);
643 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
646 struct scsi_inquiry_data *inq_buf;
649 ccb = cam_getccb(device);
652 warnx("couldn't allocate CCB");
656 /* cam_getccb cleans up the header, caller has to zero the payload */
657 bzero(&(&ccb->ccb_h)[1],
658 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
660 inq_buf = (struct scsi_inquiry_data *)malloc(
661 sizeof(struct scsi_inquiry_data));
663 if (inq_buf == NULL) {
665 warnx("can't malloc memory for inquiry\n");
668 bzero(inq_buf, sizeof(*inq_buf));
671 * Note that although the size of the inquiry buffer is the full
672 * 256 bytes specified in the SCSI spec, we only tell the device
673 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
674 * two reasons for this:
676 * - The SCSI spec says that when a length field is only 1 byte,
677 * a value of 0 will be interpreted as 256. Therefore
678 * scsi_inquiry() will convert an inq_len (which is passed in as
679 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
680 * to 0. Evidently, very few devices meet the spec in that
681 * regard. Some devices, like many Seagate disks, take the 0 as
682 * 0, and don't return any data. One Pioneer DVD-R drive
683 * returns more data than the command asked for.
685 * So, since there are numerous devices that just don't work
686 * right with the full inquiry size, we don't send the full size.
688 * - The second reason not to use the full inquiry data length is
689 * that we don't need it here. The only reason we issue a
690 * standard inquiry is to get the vendor name, device name,
691 * and revision so scsi_print_inquiry() can print them.
693 * If, at some point in the future, more inquiry data is needed for
694 * some reason, this code should use a procedure similar to the
695 * probe code. i.e., issue a short inquiry, and determine from
696 * the additional length passed back from the device how much
697 * inquiry data the device supports. Once the amount the device
698 * supports is determined, issue an inquiry for that amount and no
703 scsi_inquiry(&ccb->csio,
704 /* retries */ retry_count,
706 /* tag_action */ MSG_SIMPLE_Q_TAG,
707 /* inq_buf */ (u_int8_t *)inq_buf,
708 /* inq_len */ SHORT_INQUIRY_LENGTH,
711 /* sense_len */ SSD_FULL_SIZE,
712 /* timeout */ timeout ? timeout : 5000);
714 /* Disable freezing the device queue */
715 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
717 if (arglist & CAM_ARG_ERR_RECOVER)
718 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
720 if (cam_send_ccb(device, ccb) < 0) {
721 perror("error sending SCSI inquiry");
723 if (arglist & CAM_ARG_VERBOSE) {
724 cam_error_print(device, ccb, CAM_ESF_ALL,
725 CAM_EPF_ALL, stderr);
732 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
735 if (arglist & CAM_ARG_VERBOSE) {
736 cam_error_print(device, ccb, CAM_ESF_ALL,
737 CAM_EPF_ALL, stderr);
748 fprintf(stdout, "%s%d: ", device->device_name,
749 device->dev_unit_num);
750 scsi_print_inquiry(inq_buf);
758 scsiserial(struct cam_device *device, int retry_count, int timeout)
761 struct scsi_vpd_unit_serial_number *serial_buf;
762 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
765 ccb = cam_getccb(device);
768 warnx("couldn't allocate CCB");
772 /* cam_getccb cleans up the header, caller has to zero the payload */
773 bzero(&(&ccb->ccb_h)[1],
774 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
776 serial_buf = (struct scsi_vpd_unit_serial_number *)
777 malloc(sizeof(*serial_buf));
779 if (serial_buf == NULL) {
781 warnx("can't malloc memory for serial number");
785 scsi_inquiry(&ccb->csio,
786 /*retries*/ retry_count,
788 /* tag_action */ MSG_SIMPLE_Q_TAG,
789 /* inq_buf */ (u_int8_t *)serial_buf,
790 /* inq_len */ sizeof(*serial_buf),
792 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
793 /* sense_len */ SSD_FULL_SIZE,
794 /* timeout */ timeout ? timeout : 5000);
796 /* Disable freezing the device queue */
797 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
799 if (arglist & CAM_ARG_ERR_RECOVER)
800 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
802 if (cam_send_ccb(device, ccb) < 0) {
803 warn("error getting serial number");
805 if (arglist & CAM_ARG_VERBOSE) {
806 cam_error_print(device, ccb, CAM_ESF_ALL,
807 CAM_EPF_ALL, stderr);
815 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
818 if (arglist & CAM_ARG_VERBOSE) {
819 cam_error_print(device, ccb, CAM_ESF_ALL,
820 CAM_EPF_ALL, stderr);
831 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
832 serial_num[serial_buf->length] = '\0';
834 if ((arglist & CAM_ARG_GET_STDINQ)
835 || (arglist & CAM_ARG_GET_XFERRATE))
836 fprintf(stdout, "%s%d: Serial Number ",
837 device->device_name, device->dev_unit_num);
839 fprintf(stdout, "%.60s\n", serial_num);
847 scsixferrate(struct cam_device *device)
855 ccb = cam_getccb(device);
858 warnx("couldn't allocate CCB");
862 bzero(&(&ccb->ccb_h)[1],
863 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
865 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
866 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
868 if (((retval = cam_send_ccb(device, ccb)) < 0)
869 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
870 const char error_string[] = "error getting transfer settings";
877 if (arglist & CAM_ARG_VERBOSE)
878 cam_error_print(device, ccb, CAM_ESF_ALL,
879 CAM_EPF_ALL, stderr);
883 goto xferrate_bailout;
887 if (ccb->cts.transport == XPORT_SPI) {
888 struct ccb_trans_settings_spi *spi =
889 &ccb->cts.xport_specific.spi;
891 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
892 freq = scsi_calc_syncsrate(spi->sync_period);
896 fprintf(stdout, "%s%d: ", device->device_name,
897 device->dev_unit_num);
899 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
900 speed *= (0x01 << spi->bus_width);
906 fprintf(stdout, "%d.%03dMB/s transfers ",
909 fprintf(stdout, "%dKB/s transfers ",
912 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
913 && (spi->sync_offset != 0))
914 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
915 freq % 1000, spi->sync_offset);
917 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
918 && (spi->bus_width > 0)) {
919 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
920 && (spi->sync_offset != 0)) {
921 fprintf(stdout, ", ");
923 fprintf(stdout, " (");
925 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
926 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
927 && (spi->sync_offset != 0)) {
928 fprintf(stdout, ")");
931 struct ccb_pathinq cpi;
933 retval = get_cpi(device, &cpi);
936 goto xferrate_bailout;
938 speed = cpi.base_transfer_speed;
944 fprintf(stdout, "%d.%03dMB/s transfers ",
947 fprintf(stdout, "%dKB/s transfers ",
951 if (ccb->cts.protocol == PROTO_SCSI) {
952 struct ccb_trans_settings_scsi *scsi =
953 &ccb->cts.proto_specific.scsi;
954 if (scsi->valid & CTS_SCSI_VALID_TQ) {
955 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
956 fprintf(stdout, ", Command Queueing Enabled");
961 fprintf(stdout, "\n");
969 #endif /* MINIMALISTIC */
972 * Parse out a bus, or a bus, target and lun in the following
978 * Returns the number of parsed components, or 0.
981 parse_btl(char *tstr, int *mybus, int *mytarget, int *mylun,
982 cam_argmask *myarglist)
987 while (isspace(*tstr) && (*tstr != '\0'))
990 tmpstr = (char *)strtok(tstr, ":");
991 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
992 *mybus = strtol(tmpstr, NULL, 0);
993 *myarglist |= CAM_ARG_BUS;
995 tmpstr = (char *)strtok(NULL, ":");
996 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
997 *mytarget = strtol(tmpstr, NULL, 0);
998 *myarglist |= CAM_ARG_TARGET;
1000 tmpstr = (char *)strtok(NULL, ":");
1001 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1002 *mylun = strtol(tmpstr, NULL, 0);
1003 *myarglist |= CAM_ARG_LUN;
1013 dorescan_or_reset(int argc, char **argv, int rescan)
1015 static const char must[] =
1016 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1018 int mybus = -1, mytarget = -1, mylun = -1;
1022 warnx(must, rescan? "rescan" : "reset");
1026 tstr = argv[optind];
1027 while (isspace(*tstr) && (*tstr != '\0'))
1029 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1030 arglist |= CAM_ARG_BUS;
1032 rv = parse_btl(argv[optind], &mybus, &mytarget, &mylun,
1034 if (rv != 1 && rv != 3) {
1035 warnx(must, rescan? "rescan" : "reset");
1040 if ((arglist & CAM_ARG_BUS)
1041 && (arglist & CAM_ARG_TARGET)
1042 && (arglist & CAM_ARG_LUN))
1043 error = scanlun_or_reset_dev(mybus, mytarget, mylun, rescan);
1045 error = rescan_or_reset_bus(mybus, rescan);
1051 rescan_or_reset_bus(int mybus, int rescan)
1053 union ccb ccb, matchccb;
1059 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1060 warnx("error opening transport layer device %s", XPT_DEVICE);
1061 warn("%s", XPT_DEVICE);
1066 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1067 ccb.ccb_h.path_id = mybus;
1068 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1069 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1070 ccb.crcn.flags = CAM_FLAG_NONE;
1072 /* run this at a low priority */
1073 ccb.ccb_h.pinfo.priority = 5;
1075 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1076 warn("CAMIOCOMMAND ioctl failed");
1081 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1082 fprintf(stdout, "%s of bus %d was successful\n",
1083 rescan ? "Re-scan" : "Reset", mybus);
1085 fprintf(stdout, "%s of bus %d returned error %#x\n",
1086 rescan ? "Re-scan" : "Reset", mybus,
1087 ccb.ccb_h.status & CAM_STATUS_MASK);
1098 * The right way to handle this is to modify the xpt so that it can
1099 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1100 * that isn't implemented, so instead we enumerate the busses and
1101 * send the rescan or reset to those busses in the case where the
1102 * given bus is -1 (wildcard). We don't send a rescan or reset
1103 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1104 * no-op, sending a rescan to the xpt bus would result in a status of
1107 bzero(&(&matchccb.ccb_h)[1],
1108 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1109 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1110 bufsize = sizeof(struct dev_match_result) * 20;
1111 matchccb.cdm.match_buf_len = bufsize;
1112 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1113 if (matchccb.cdm.matches == NULL) {
1114 warnx("can't malloc memory for matches");
1118 matchccb.cdm.num_matches = 0;
1120 matchccb.cdm.num_patterns = 1;
1121 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1123 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1124 matchccb.cdm.pattern_buf_len);
1125 if (matchccb.cdm.patterns == NULL) {
1126 warnx("can't malloc memory for patterns");
1130 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1131 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1136 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1137 warn("CAMIOCOMMAND ioctl failed");
1142 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1143 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1144 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1145 warnx("got CAM error %#x, CDM error %d\n",
1146 matchccb.ccb_h.status, matchccb.cdm.status);
1151 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1152 struct bus_match_result *bus_result;
1154 /* This shouldn't happen. */
1155 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1158 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1161 * We don't want to rescan or reset the xpt bus.
1164 if ((int)bus_result->path_id == -1)
1167 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1169 ccb.ccb_h.path_id = bus_result->path_id;
1170 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1171 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1172 ccb.crcn.flags = CAM_FLAG_NONE;
1174 /* run this at a low priority */
1175 ccb.ccb_h.pinfo.priority = 5;
1177 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1178 warn("CAMIOCOMMAND ioctl failed");
1183 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1184 fprintf(stdout, "%s of bus %d was successful\n",
1185 rescan? "Re-scan" : "Reset",
1186 bus_result->path_id);
1189 * Don't bail out just yet, maybe the other
1190 * rescan or reset commands will complete
1193 fprintf(stderr, "%s of bus %d returned error "
1194 "%#x\n", rescan? "Re-scan" : "Reset",
1195 bus_result->path_id,
1196 ccb.ccb_h.status & CAM_STATUS_MASK);
1200 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1201 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1208 if (matchccb.cdm.patterns != NULL)
1209 free(matchccb.cdm.patterns);
1210 if (matchccb.cdm.matches != NULL)
1211 free(matchccb.cdm.matches);
1217 scanlun_or_reset_dev(int mybus, int mytarget, int mylun, int scan)
1220 struct cam_device *device;
1226 warnx("invalid bus number %d", mybus);
1231 warnx("invalid target number %d", mytarget);
1236 warnx("invalid lun number %d", mylun);
1242 bzero(&ccb, sizeof(union ccb));
1245 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1246 warnx("error opening transport layer device %s\n",
1248 warn("%s", XPT_DEVICE);
1252 device = cam_open_btl(mybus, mytarget, mylun, O_RDWR, NULL);
1253 if (device == NULL) {
1254 warnx("%s", cam_errbuf);
1259 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1260 ccb.ccb_h.path_id = mybus;
1261 ccb.ccb_h.target_id = mytarget;
1262 ccb.ccb_h.target_lun = mylun;
1263 ccb.ccb_h.timeout = 5000;
1264 ccb.crcn.flags = CAM_FLAG_NONE;
1266 /* run this at a low priority */
1267 ccb.ccb_h.pinfo.priority = 5;
1270 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1271 warn("CAMIOCOMMAND ioctl failed");
1276 if (cam_send_ccb(device, &ccb) < 0) {
1277 warn("error sending XPT_RESET_DEV CCB");
1278 cam_close_device(device);
1286 cam_close_device(device);
1289 * An error code of CAM_BDR_SENT is normal for a BDR request.
1291 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1293 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1294 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1295 scan? "Re-scan" : "Reset", mybus, mytarget, mylun);
1298 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1299 scan? "Re-scan" : "Reset", mybus, mytarget, mylun,
1300 ccb.ccb_h.status & CAM_STATUS_MASK);
1305 #ifndef MINIMALISTIC
1307 readdefects(struct cam_device *device, int argc, char **argv,
1308 char *combinedopt, int retry_count, int timeout)
1310 union ccb *ccb = NULL;
1311 struct scsi_read_defect_data_10 *rdd_cdb;
1312 u_int8_t *defect_list = NULL;
1313 u_int32_t dlist_length = 65000;
1314 u_int32_t returned_length = 0;
1315 u_int32_t num_returned = 0;
1316 u_int8_t returned_format;
1319 int lists_specified = 0;
1321 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1327 while (isspace(*tstr) && (*tstr != '\0'))
1329 if (strcmp(tstr, "block") == 0)
1330 arglist |= CAM_ARG_FORMAT_BLOCK;
1331 else if (strcmp(tstr, "bfi") == 0)
1332 arglist |= CAM_ARG_FORMAT_BFI;
1333 else if (strcmp(tstr, "phys") == 0)
1334 arglist |= CAM_ARG_FORMAT_PHYS;
1337 warnx("invalid defect format %s", tstr);
1338 goto defect_bailout;
1343 arglist |= CAM_ARG_GLIST;
1346 arglist |= CAM_ARG_PLIST;
1353 ccb = cam_getccb(device);
1356 * Hopefully 65000 bytes is enough to hold the defect list. If it
1357 * isn't, the disk is probably dead already. We'd have to go with
1358 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1361 defect_list = malloc(dlist_length);
1362 if (defect_list == NULL) {
1363 warnx("can't malloc memory for defect list");
1365 goto defect_bailout;
1368 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1371 * cam_getccb() zeros the CCB header only. So we need to zero the
1372 * payload portion of the ccb.
1374 bzero(&(&ccb->ccb_h)[1],
1375 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1377 cam_fill_csio(&ccb->csio,
1378 /*retries*/ retry_count,
1380 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1381 CAM_PASS_ERR_RECOVER : 0),
1382 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1383 /*data_ptr*/ defect_list,
1384 /*dxfer_len*/ dlist_length,
1385 /*sense_len*/ SSD_FULL_SIZE,
1386 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1387 /*timeout*/ timeout ? timeout : 5000);
1389 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1390 if (arglist & CAM_ARG_FORMAT_BLOCK)
1391 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1392 else if (arglist & CAM_ARG_FORMAT_BFI)
1393 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1394 else if (arglist & CAM_ARG_FORMAT_PHYS)
1395 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1398 warnx("no defect list format specified");
1399 goto defect_bailout;
1401 if (arglist & CAM_ARG_PLIST) {
1402 rdd_cdb->format |= SRDD10_PLIST;
1406 if (arglist & CAM_ARG_GLIST) {
1407 rdd_cdb->format |= SRDD10_GLIST;
1411 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1413 /* Disable freezing the device queue */
1414 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1416 if (cam_send_ccb(device, ccb) < 0) {
1417 perror("error reading defect list");
1419 if (arglist & CAM_ARG_VERBOSE) {
1420 cam_error_print(device, ccb, CAM_ESF_ALL,
1421 CAM_EPF_ALL, stderr);
1425 goto defect_bailout;
1428 returned_length = scsi_2btoul(((struct
1429 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1431 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1432 defect_list)->format;
1434 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1435 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1436 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1437 struct scsi_sense_data *sense;
1438 int error_code, sense_key, asc, ascq;
1440 sense = &ccb->csio.sense_data;
1441 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1444 * According to the SCSI spec, if the disk doesn't support
1445 * the requested format, it will generally return a sense
1446 * key of RECOVERED ERROR, and an additional sense code
1447 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1448 * also check to make sure that the returned length is
1449 * greater than 0, and then print out whatever format the
1452 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1453 && (asc == 0x1c) && (ascq == 0x00)
1454 && (returned_length > 0)) {
1455 warnx("requested defect format not available");
1456 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1457 case SRDD10_BLOCK_FORMAT:
1458 warnx("Device returned block format");
1460 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1461 warnx("Device returned bytes from index"
1464 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1465 warnx("Device returned physical sector format");
1469 warnx("Device returned unknown defect"
1470 " data format %#x", returned_format);
1471 goto defect_bailout;
1472 break; /* NOTREACHED */
1476 warnx("Error returned from read defect data command");
1477 if (arglist & CAM_ARG_VERBOSE)
1478 cam_error_print(device, ccb, CAM_ESF_ALL,
1479 CAM_EPF_ALL, stderr);
1480 goto defect_bailout;
1482 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1484 warnx("Error returned from read defect data command");
1485 if (arglist & CAM_ARG_VERBOSE)
1486 cam_error_print(device, ccb, CAM_ESF_ALL,
1487 CAM_EPF_ALL, stderr);
1488 goto defect_bailout;
1492 * XXX KDM I should probably clean up the printout format for the
1495 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1496 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1498 struct scsi_defect_desc_phys_sector *dlist;
1500 dlist = (struct scsi_defect_desc_phys_sector *)
1502 sizeof(struct scsi_read_defect_data_hdr_10));
1504 num_returned = returned_length /
1505 sizeof(struct scsi_defect_desc_phys_sector);
1507 fprintf(stderr, "Got %d defect", num_returned);
1509 if ((lists_specified == 0) || (num_returned == 0)) {
1510 fprintf(stderr, "s.\n");
1512 } else if (num_returned == 1)
1513 fprintf(stderr, ":\n");
1515 fprintf(stderr, "s:\n");
1517 for (i = 0; i < num_returned; i++) {
1518 fprintf(stdout, "%d:%d:%d\n",
1519 scsi_3btoul(dlist[i].cylinder),
1521 scsi_4btoul(dlist[i].sector));
1525 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1527 struct scsi_defect_desc_bytes_from_index *dlist;
1529 dlist = (struct scsi_defect_desc_bytes_from_index *)
1531 sizeof(struct scsi_read_defect_data_hdr_10));
1533 num_returned = returned_length /
1534 sizeof(struct scsi_defect_desc_bytes_from_index);
1536 fprintf(stderr, "Got %d defect", num_returned);
1538 if ((lists_specified == 0) || (num_returned == 0)) {
1539 fprintf(stderr, "s.\n");
1541 } else if (num_returned == 1)
1542 fprintf(stderr, ":\n");
1544 fprintf(stderr, "s:\n");
1546 for (i = 0; i < num_returned; i++) {
1547 fprintf(stdout, "%d:%d:%d\n",
1548 scsi_3btoul(dlist[i].cylinder),
1550 scsi_4btoul(dlist[i].bytes_from_index));
1554 case SRDDH10_BLOCK_FORMAT:
1556 struct scsi_defect_desc_block *dlist;
1558 dlist = (struct scsi_defect_desc_block *)(defect_list +
1559 sizeof(struct scsi_read_defect_data_hdr_10));
1561 num_returned = returned_length /
1562 sizeof(struct scsi_defect_desc_block);
1564 fprintf(stderr, "Got %d defect", num_returned);
1566 if ((lists_specified == 0) || (num_returned == 0)) {
1567 fprintf(stderr, "s.\n");
1569 } else if (num_returned == 1)
1570 fprintf(stderr, ":\n");
1572 fprintf(stderr, "s:\n");
1574 for (i = 0; i < num_returned; i++)
1575 fprintf(stdout, "%u\n",
1576 scsi_4btoul(dlist[i].address));
1580 fprintf(stderr, "Unknown defect format %d\n",
1581 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1587 if (defect_list != NULL)
1595 #endif /* MINIMALISTIC */
1599 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1603 ccb = cam_getccb(device);
1609 #ifndef MINIMALISTIC
1611 mode_sense(struct cam_device *device, int mode_page, int page_control,
1612 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1617 ccb = cam_getccb(device);
1620 errx(1, "mode_sense: couldn't allocate CCB");
1622 bzero(&(&ccb->ccb_h)[1],
1623 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1625 scsi_mode_sense(&ccb->csio,
1626 /* retries */ retry_count,
1628 /* tag_action */ MSG_SIMPLE_Q_TAG,
1630 /* page_code */ page_control << 6,
1631 /* page */ mode_page,
1632 /* param_buf */ data,
1633 /* param_len */ datalen,
1634 /* sense_len */ SSD_FULL_SIZE,
1635 /* timeout */ timeout ? timeout : 5000);
1637 if (arglist & CAM_ARG_ERR_RECOVER)
1638 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1640 /* Disable freezing the device queue */
1641 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1643 if (((retval = cam_send_ccb(device, ccb)) < 0)
1644 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1645 if (arglist & CAM_ARG_VERBOSE) {
1646 cam_error_print(device, ccb, CAM_ESF_ALL,
1647 CAM_EPF_ALL, stderr);
1650 cam_close_device(device);
1652 err(1, "error sending mode sense command");
1654 errx(1, "error sending mode sense command");
1661 mode_select(struct cam_device *device, int save_pages, int retry_count,
1662 int timeout, u_int8_t *data, int datalen)
1667 ccb = cam_getccb(device);
1670 errx(1, "mode_select: couldn't allocate CCB");
1672 bzero(&(&ccb->ccb_h)[1],
1673 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1675 scsi_mode_select(&ccb->csio,
1676 /* retries */ retry_count,
1678 /* tag_action */ MSG_SIMPLE_Q_TAG,
1679 /* scsi_page_fmt */ 1,
1680 /* save_pages */ save_pages,
1681 /* param_buf */ data,
1682 /* param_len */ datalen,
1683 /* sense_len */ SSD_FULL_SIZE,
1684 /* timeout */ timeout ? timeout : 5000);
1686 if (arglist & CAM_ARG_ERR_RECOVER)
1687 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1689 /* Disable freezing the device queue */
1690 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1692 if (((retval = cam_send_ccb(device, ccb)) < 0)
1693 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1694 if (arglist & CAM_ARG_VERBOSE) {
1695 cam_error_print(device, ccb, CAM_ESF_ALL,
1696 CAM_EPF_ALL, stderr);
1699 cam_close_device(device);
1702 err(1, "error sending mode select command");
1704 errx(1, "error sending mode select command");
1712 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
1713 int retry_count, int timeout)
1715 int c, mode_page = -1, page_control = 0;
1716 int binary = 0, list = 0;
1718 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1724 arglist |= CAM_ARG_DBD;
1727 arglist |= CAM_ARG_MODE_EDIT;
1733 mode_page = strtol(optarg, NULL, 0);
1735 errx(1, "invalid mode page %d", mode_page);
1738 page_control = strtol(optarg, NULL, 0);
1739 if ((page_control < 0) || (page_control > 3))
1740 errx(1, "invalid page control field %d",
1742 arglist |= CAM_ARG_PAGE_CNTL;
1749 if (mode_page == -1 && list == 0)
1750 errx(1, "you must specify a mode page!");
1753 mode_list(device, page_control, arglist & CAM_ARG_DBD,
1754 retry_count, timeout);
1756 mode_edit(device, mode_page, page_control,
1757 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
1758 retry_count, timeout);
1763 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
1764 int retry_count, int timeout)
1767 u_int32_t flags = CAM_DIR_NONE;
1768 u_int8_t *data_ptr = NULL;
1770 struct get_hook hook;
1771 int c, data_bytes = 0;
1773 char *datastr = NULL, *tstr;
1778 ccb = cam_getccb(device);
1781 warnx("scsicmd: error allocating ccb");
1785 bzero(&(&ccb->ccb_h)[1],
1786 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1788 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1792 while (isspace(*tstr) && (*tstr != '\0'))
1794 hook.argc = argc - optind;
1795 hook.argv = argv + optind;
1797 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
1800 * Increment optind by the number of arguments the
1801 * encoding routine processed. After each call to
1802 * getopt(3), optind points to the argument that
1803 * getopt should process _next_. In this case,
1804 * that means it points to the first command string
1805 * argument, if there is one. Once we increment
1806 * this, it should point to either the next command
1807 * line argument, or it should be past the end of
1813 if (arglist & CAM_ARG_CMD_OUT) {
1814 warnx("command must either be "
1815 "read or write, not both");
1817 goto scsicmd_bailout;
1819 arglist |= CAM_ARG_CMD_IN;
1821 data_bytes = strtol(optarg, NULL, 0);
1822 if (data_bytes <= 0) {
1823 warnx("invalid number of input bytes %d",
1826 goto scsicmd_bailout;
1828 hook.argc = argc - optind;
1829 hook.argv = argv + optind;
1832 datastr = cget(&hook, NULL);
1834 * If the user supplied "-" instead of a format, he
1835 * wants the data to be written to stdout.
1837 if ((datastr != NULL)
1838 && (datastr[0] == '-'))
1841 data_ptr = (u_int8_t *)malloc(data_bytes);
1842 if (data_ptr == NULL) {
1843 warnx("can't malloc memory for data_ptr");
1845 goto scsicmd_bailout;
1849 if (arglist & CAM_ARG_CMD_IN) {
1850 warnx("command must either be "
1851 "read or write, not both");
1853 goto scsicmd_bailout;
1855 arglist |= CAM_ARG_CMD_OUT;
1856 flags = CAM_DIR_OUT;
1857 data_bytes = strtol(optarg, NULL, 0);
1858 if (data_bytes <= 0) {
1859 warnx("invalid number of output bytes %d",
1862 goto scsicmd_bailout;
1864 hook.argc = argc - optind;
1865 hook.argv = argv + optind;
1867 datastr = cget(&hook, NULL);
1868 data_ptr = (u_int8_t *)malloc(data_bytes);
1869 if (data_ptr == NULL) {
1870 warnx("can't malloc memory for data_ptr");
1872 goto scsicmd_bailout;
1875 * If the user supplied "-" instead of a format, he
1876 * wants the data to be read from stdin.
1878 if ((datastr != NULL)
1879 && (datastr[0] == '-'))
1882 buff_encode_visit(data_ptr, data_bytes, datastr,
1892 * If fd_data is set, and we're writing to the device, we need to
1893 * read the data the user wants written from stdin.
1895 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
1897 int amt_to_read = data_bytes;
1898 u_int8_t *buf_ptr = data_ptr;
1900 for (amt_read = 0; amt_to_read > 0;
1901 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1902 if (amt_read == -1) {
1903 warn("error reading data from stdin");
1905 goto scsicmd_bailout;
1907 amt_to_read -= amt_read;
1908 buf_ptr += amt_read;
1912 if (arglist & CAM_ARG_ERR_RECOVER)
1913 flags |= CAM_PASS_ERR_RECOVER;
1915 /* Disable freezing the device queue */
1916 flags |= CAM_DEV_QFRZDIS;
1919 * This is taken from the SCSI-3 draft spec.
1920 * (T10/1157D revision 0.3)
1921 * The top 3 bits of an opcode are the group code. The next 5 bits
1922 * are the command code.
1923 * Group 0: six byte commands
1924 * Group 1: ten byte commands
1925 * Group 2: ten byte commands
1927 * Group 4: sixteen byte commands
1928 * Group 5: twelve byte commands
1929 * Group 6: vendor specific
1930 * Group 7: vendor specific
1932 switch((cdb[0] >> 5) & 0x7) {
1943 /* computed by buff_encode_visit */
1954 * We should probably use csio_build_visit or something like that
1955 * here, but it's easier to encode arguments as you go. The
1956 * alternative would be skipping the CDB argument and then encoding
1957 * it here, since we've got the data buffer argument by now.
1959 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
1961 cam_fill_csio(&ccb->csio,
1962 /*retries*/ retry_count,
1965 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1966 /*data_ptr*/ data_ptr,
1967 /*dxfer_len*/ data_bytes,
1968 /*sense_len*/ SSD_FULL_SIZE,
1969 /*cdb_len*/ cdb_len,
1970 /*timeout*/ timeout ? timeout : 5000);
1972 if (((retval = cam_send_ccb(device, ccb)) < 0)
1973 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1975 warn("error sending command");
1977 warnx("error sending command");
1979 if (arglist & CAM_ARG_VERBOSE) {
1980 cam_error_print(device, ccb, CAM_ESF_ALL,
1981 CAM_EPF_ALL, stderr);
1985 goto scsicmd_bailout;
1989 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1990 && (arglist & CAM_ARG_CMD_IN)
1991 && (data_bytes > 0)) {
1993 buff_decode_visit(data_ptr, data_bytes, datastr,
1995 fprintf(stdout, "\n");
1997 ssize_t amt_written;
1998 int amt_to_write = data_bytes;
1999 u_int8_t *buf_ptr = data_ptr;
2001 for (amt_written = 0; (amt_to_write > 0) &&
2002 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
2003 amt_to_write -= amt_written;
2004 buf_ptr += amt_written;
2006 if (amt_written == -1) {
2007 warn("error writing data to stdout");
2009 goto scsicmd_bailout;
2010 } else if ((amt_written == 0)
2011 && (amt_to_write > 0)) {
2012 warnx("only wrote %u bytes out of %u",
2013 data_bytes - amt_to_write, data_bytes);
2020 if ((data_bytes > 0) && (data_ptr != NULL))
2029 camdebug(int argc, char **argv, char *combinedopt)
2032 int mybus = -1, mytarget = -1, mylun = -1;
2033 char *tstr, *tmpstr = NULL;
2037 bzero(&ccb, sizeof(union ccb));
2039 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2042 arglist |= CAM_ARG_DEBUG_INFO;
2043 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2046 arglist |= CAM_ARG_DEBUG_PERIPH;
2047 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2050 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2051 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2054 arglist |= CAM_ARG_DEBUG_TRACE;
2055 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2058 arglist |= CAM_ARG_DEBUG_XPT;
2059 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2062 arglist |= CAM_ARG_DEBUG_CDB;
2063 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2070 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2071 warnx("error opening transport layer device %s", XPT_DEVICE);
2072 warn("%s", XPT_DEVICE);
2079 warnx("you must specify \"off\", \"all\" or a bus,");
2080 warnx("bus:target, or bus:target:lun");
2087 while (isspace(*tstr) && (*tstr != '\0'))
2090 if (strncmp(tstr, "off", 3) == 0) {
2091 ccb.cdbg.flags = CAM_DEBUG_NONE;
2092 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2093 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2095 } else if (strncmp(tstr, "all", 3) != 0) {
2096 tmpstr = (char *)strtok(tstr, ":");
2097 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2098 mybus = strtol(tmpstr, NULL, 0);
2099 arglist |= CAM_ARG_BUS;
2100 tmpstr = (char *)strtok(NULL, ":");
2101 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2102 mytarget = strtol(tmpstr, NULL, 0);
2103 arglist |= CAM_ARG_TARGET;
2104 tmpstr = (char *)strtok(NULL, ":");
2105 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2106 mylun = strtol(tmpstr, NULL, 0);
2107 arglist |= CAM_ARG_LUN;
2112 warnx("you must specify \"all\", \"off\", or a bus,");
2113 warnx("bus:target, or bus:target:lun to debug");
2119 ccb.ccb_h.func_code = XPT_DEBUG;
2120 ccb.ccb_h.path_id = mybus;
2121 ccb.ccb_h.target_id = mytarget;
2122 ccb.ccb_h.target_lun = mylun;
2124 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2125 warn("CAMIOCOMMAND ioctl failed");
2130 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2131 CAM_FUNC_NOTAVAIL) {
2132 warnx("CAM debugging not available");
2133 warnx("you need to put options CAMDEBUG in"
2134 " your kernel config file!");
2136 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2138 warnx("XPT_DEBUG CCB failed with status %#x",
2142 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2144 "Debugging turned off\n");
2147 "Debugging enabled for "
2149 mybus, mytarget, mylun);
2160 tagcontrol(struct cam_device *device, int argc, char **argv,
2170 ccb = cam_getccb(device);
2173 warnx("tagcontrol: error allocating ccb");
2177 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2180 numtags = strtol(optarg, NULL, 0);
2182 warnx("tag count %d is < 0", numtags);
2184 goto tagcontrol_bailout;
2195 cam_path_string(device, pathstr, sizeof(pathstr));
2198 bzero(&(&ccb->ccb_h)[1],
2199 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2200 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2201 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2202 ccb->crs.openings = numtags;
2205 if (cam_send_ccb(device, ccb) < 0) {
2206 perror("error sending XPT_REL_SIMQ CCB");
2208 goto tagcontrol_bailout;
2211 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2212 warnx("XPT_REL_SIMQ CCB failed");
2213 cam_error_print(device, ccb, CAM_ESF_ALL,
2214 CAM_EPF_ALL, stderr);
2216 goto tagcontrol_bailout;
2221 fprintf(stdout, "%stagged openings now %d\n",
2222 pathstr, ccb->crs.openings);
2225 bzero(&(&ccb->ccb_h)[1],
2226 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2228 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2230 if (cam_send_ccb(device, ccb) < 0) {
2231 perror("error sending XPT_GDEV_STATS CCB");
2233 goto tagcontrol_bailout;
2236 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2237 warnx("XPT_GDEV_STATS CCB failed");
2238 cam_error_print(device, ccb, CAM_ESF_ALL,
2239 CAM_EPF_ALL, stderr);
2241 goto tagcontrol_bailout;
2244 if (arglist & CAM_ARG_VERBOSE) {
2245 fprintf(stdout, "%s", pathstr);
2246 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2247 fprintf(stdout, "%s", pathstr);
2248 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2249 fprintf(stdout, "%s", pathstr);
2250 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2251 fprintf(stdout, "%s", pathstr);
2252 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2253 fprintf(stdout, "%s", pathstr);
2254 fprintf(stdout, "held %d\n", ccb->cgds.held);
2255 fprintf(stdout, "%s", pathstr);
2256 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2257 fprintf(stdout, "%s", pathstr);
2258 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2261 fprintf(stdout, "%s", pathstr);
2262 fprintf(stdout, "device openings: ");
2264 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2265 ccb->cgds.dev_active);
2275 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2279 cam_path_string(device, pathstr, sizeof(pathstr));
2281 if (cts->transport == XPORT_SPI) {
2282 struct ccb_trans_settings_spi *spi =
2283 &cts->xport_specific.spi;
2285 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
2287 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2290 if (spi->sync_offset != 0) {
2293 freq = scsi_calc_syncsrate(spi->sync_period);
2294 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
2295 pathstr, freq / 1000, freq % 1000);
2299 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
2300 fprintf(stdout, "%soffset: %d\n", pathstr,
2304 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
2305 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2306 (0x01 << spi->bus_width) * 8);
2309 if (spi->valid & CTS_SPI_VALID_DISC) {
2310 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2311 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
2312 "enabled" : "disabled");
2316 if (cts->protocol == PROTO_SCSI) {
2317 struct ccb_trans_settings_scsi *scsi=
2318 &cts->proto_specific.scsi;
2320 if (scsi->valid & CTS_SCSI_VALID_TQ) {
2321 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2322 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
2323 "enabled" : "disabled");
2330 * Get a path inquiry CCB for the specified device.
2333 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2338 ccb = cam_getccb(device);
2341 warnx("get_cpi: couldn't allocate CCB");
2345 bzero(&(&ccb->ccb_h)[1],
2346 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2348 ccb->ccb_h.func_code = XPT_PATH_INQ;
2350 if (cam_send_ccb(device, ccb) < 0) {
2351 warn("get_cpi: error sending Path Inquiry CCB");
2353 if (arglist & CAM_ARG_VERBOSE)
2354 cam_error_print(device, ccb, CAM_ESF_ALL,
2355 CAM_EPF_ALL, stderr);
2359 goto get_cpi_bailout;
2362 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2364 if (arglist & CAM_ARG_VERBOSE)
2365 cam_error_print(device, ccb, CAM_ESF_ALL,
2366 CAM_EPF_ALL, stderr);
2370 goto get_cpi_bailout;
2373 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2383 cpi_print(struct ccb_pathinq *cpi)
2385 char adapter_str[1024];
2388 snprintf(adapter_str, sizeof(adapter_str),
2389 "%s%d:", cpi->dev_name, cpi->unit_number);
2391 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2394 for (i = 1; i < 0xff; i = i << 1) {
2397 if ((i & cpi->hba_inquiry) == 0)
2400 fprintf(stdout, "%s supports ", adapter_str);
2404 str = "MDP message";
2407 str = "32 bit wide SCSI";
2410 str = "16 bit wide SCSI";
2413 str = "SDTR message";
2416 str = "linked CDBs";
2419 str = "tag queue messages";
2422 str = "soft reset alternative";
2425 str = "unknown PI bit set";
2428 fprintf(stdout, "%s\n", str);
2431 for (i = 1; i < 0xff; i = i << 1) {
2434 if ((i & cpi->hba_misc) == 0)
2437 fprintf(stdout, "%s ", adapter_str);
2441 str = "bus scans from high ID to low ID";
2444 str = "removable devices not included in scan";
2446 case PIM_NOINITIATOR:
2447 str = "initiator role not supported";
2449 case PIM_NOBUSRESET:
2450 str = "user has disabled initial BUS RESET or"
2451 " controller is in target/mixed mode";
2454 str = "unknown PIM bit set";
2457 fprintf(stdout, "%s\n", str);
2460 for (i = 1; i < 0xff; i = i << 1) {
2463 if ((i & cpi->target_sprt) == 0)
2466 fprintf(stdout, "%s supports ", adapter_str);
2469 str = "target mode processor mode";
2472 str = "target mode phase cog. mode";
2474 case PIT_DISCONNECT:
2475 str = "disconnects in target mode";
2478 str = "terminate I/O message in target mode";
2481 str = "group 6 commands in target mode";
2484 str = "group 7 commands in target mode";
2487 str = "unknown PIT bit set";
2491 fprintf(stdout, "%s\n", str);
2493 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2495 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2497 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2499 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2500 adapter_str, cpi->hpath_id);
2501 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2503 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2504 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2505 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2506 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2507 if (cpi->base_transfer_speed > 1000)
2508 fprintf(stdout, "%d.%03dMB/sec\n",
2509 cpi->base_transfer_speed / 1000,
2510 cpi->base_transfer_speed % 1000);
2512 fprintf(stdout, "%dKB/sec\n",
2513 (cpi->base_transfer_speed % 1000) * 1000);
2517 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2518 struct ccb_trans_settings *cts)
2524 ccb = cam_getccb(device);
2527 warnx("get_print_cts: error allocating ccb");
2531 bzero(&(&ccb->ccb_h)[1],
2532 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2534 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2536 if (user_settings == 0)
2537 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
2539 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
2541 if (cam_send_ccb(device, ccb) < 0) {
2542 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2543 if (arglist & CAM_ARG_VERBOSE)
2544 cam_error_print(device, ccb, CAM_ESF_ALL,
2545 CAM_EPF_ALL, stderr);
2547 goto get_print_cts_bailout;
2550 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2551 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2552 if (arglist & CAM_ARG_VERBOSE)
2553 cam_error_print(device, ccb, CAM_ESF_ALL,
2554 CAM_EPF_ALL, stderr);
2556 goto get_print_cts_bailout;
2560 cts_print(device, &ccb->cts);
2563 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2565 get_print_cts_bailout:
2573 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2574 int argc, char **argv, char *combinedopt)
2578 int user_settings = 0;
2580 int disc_enable = -1, tag_enable = -1;
2582 double syncrate = -1;
2585 int change_settings = 0, send_tur = 0;
2586 struct ccb_pathinq cpi;
2588 ccb = cam_getccb(device);
2591 warnx("ratecontrol: error allocating ccb");
2595 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2604 if (strncasecmp(optarg, "enable", 6) == 0)
2606 else if (strncasecmp(optarg, "disable", 7) == 0)
2609 warnx("-D argument \"%s\" is unknown", optarg);
2611 goto ratecontrol_bailout;
2613 change_settings = 1;
2616 offset = strtol(optarg, NULL, 0);
2618 warnx("offset value %d is < 0", offset);
2620 goto ratecontrol_bailout;
2622 change_settings = 1;
2628 syncrate = atof(optarg);
2631 warnx("sync rate %f is < 0", syncrate);
2633 goto ratecontrol_bailout;
2635 change_settings = 1;
2638 if (strncasecmp(optarg, "enable", 6) == 0)
2640 else if (strncasecmp(optarg, "disable", 7) == 0)
2643 warnx("-T argument \"%s\" is unknown", optarg);
2645 goto ratecontrol_bailout;
2647 change_settings = 1;
2653 bus_width = strtol(optarg, NULL, 0);
2654 if (bus_width < 0) {
2655 warnx("bus width %d is < 0", bus_width);
2657 goto ratecontrol_bailout;
2659 change_settings = 1;
2666 bzero(&(&ccb->ccb_h)[1],
2667 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2670 * Grab path inquiry information, so we can determine whether
2671 * or not the initiator is capable of the things that the user
2674 ccb->ccb_h.func_code = XPT_PATH_INQ;
2676 if (cam_send_ccb(device, ccb) < 0) {
2677 perror("error sending XPT_PATH_INQ CCB");
2678 if (arglist & CAM_ARG_VERBOSE) {
2679 cam_error_print(device, ccb, CAM_ESF_ALL,
2680 CAM_EPF_ALL, stderr);
2683 goto ratecontrol_bailout;
2686 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2687 warnx("XPT_PATH_INQ CCB failed");
2688 if (arglist & CAM_ARG_VERBOSE) {
2689 cam_error_print(device, ccb, CAM_ESF_ALL,
2690 CAM_EPF_ALL, stderr);
2693 goto ratecontrol_bailout;
2696 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
2698 bzero(&(&ccb->ccb_h)[1],
2699 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2702 fprintf(stdout, "Current Parameters:\n");
2704 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
2707 goto ratecontrol_bailout;
2709 if (arglist & CAM_ARG_VERBOSE)
2712 if (change_settings) {
2713 int didsettings = 0;
2714 struct ccb_trans_settings_spi *spi = NULL;
2715 struct ccb_trans_settings_scsi *scsi = NULL;
2717 if (ccb->cts.transport == XPORT_SPI) {
2718 spi = &ccb->cts.xport_specific.spi;
2721 if (ccb->cts.protocol == PROTO_SCSI) {
2722 scsi = &ccb->cts.proto_specific.scsi;
2725 if (spi && disc_enable != -1) {
2726 spi->valid |= CTS_SPI_VALID_DISC;
2727 if (disc_enable == 0)
2728 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2730 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
2733 if (scsi && tag_enable != -1) {
2734 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
2735 warnx("HBA does not support tagged queueing, "
2736 "so you cannot modify tag settings");
2738 goto ratecontrol_bailout;
2741 scsi->valid |= CTS_SCSI_VALID_TQ;
2743 if (tag_enable == 0)
2744 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2746 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
2750 if (spi && offset != -1) {
2751 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2752 warnx("HBA at %s%d is not cable of changing "
2753 "offset", cpi.dev_name,
2756 goto ratecontrol_bailout;
2758 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2759 spi->sync_offset = offset;
2763 if (spi && syncrate != -1) {
2764 int prelim_sync_period;
2767 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
2768 warnx("HBA at %s%d is not cable of changing "
2769 "transfer rates", cpi.dev_name,
2772 goto ratecontrol_bailout;
2775 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2778 * The sync rate the user gives us is in MHz.
2779 * We need to translate it into KHz for this
2785 * Next, we calculate a "preliminary" sync period
2786 * in tenths of a nanosecond.
2789 prelim_sync_period = 0;
2791 prelim_sync_period = 10000000 / syncrate;
2794 scsi_calc_syncparam(prelim_sync_period);
2796 freq = scsi_calc_syncsrate(spi->sync_period);
2801 * The bus_width argument goes like this:
2805 * Therefore, if you shift the number of bits given on the
2806 * command line right by 4, you should get the correct
2809 if (spi && bus_width != -1) {
2812 * We might as well validate things here with a
2813 * decipherable error message, rather than what
2814 * will probably be an indecipherable error message
2815 * by the time it gets back to us.
2817 if ((bus_width == 16)
2818 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
2819 warnx("HBA does not support 16 bit bus width");
2821 goto ratecontrol_bailout;
2822 } else if ((bus_width == 32)
2823 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
2824 warnx("HBA does not support 32 bit bus width");
2826 goto ratecontrol_bailout;
2827 } else if ((bus_width != 8)
2828 && (bus_width != 16)
2829 && (bus_width != 32)) {
2830 warnx("Invalid bus width %d", bus_width);
2832 goto ratecontrol_bailout;
2835 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2836 spi->bus_width = bus_width >> 4;
2840 if (didsettings == 0) {
2841 goto ratecontrol_bailout;
2843 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2845 if (cam_send_ccb(device, ccb) < 0) {
2846 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
2847 if (arglist & CAM_ARG_VERBOSE) {
2848 cam_error_print(device, ccb, CAM_ESF_ALL,
2849 CAM_EPF_ALL, stderr);
2852 goto ratecontrol_bailout;
2855 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2856 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
2857 if (arglist & CAM_ARG_VERBOSE) {
2858 cam_error_print(device, ccb, CAM_ESF_ALL,
2859 CAM_EPF_ALL, stderr);
2862 goto ratecontrol_bailout;
2867 retval = testunitready(device, retry_count, timeout,
2868 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
2871 * If the TUR didn't succeed, just bail.
2875 fprintf(stderr, "Test Unit Ready failed\n");
2876 goto ratecontrol_bailout;
2880 * If the user wants things quiet, there's no sense in
2881 * getting the transfer settings, if we're not going
2885 goto ratecontrol_bailout;
2887 fprintf(stdout, "New Parameters:\n");
2888 retval = get_print_cts(device, user_settings, 0, NULL);
2891 ratecontrol_bailout:
2898 scsiformat(struct cam_device *device, int argc, char **argv,
2899 char *combinedopt, int retry_count, int timeout)
2903 int ycount = 0, quiet = 0;
2904 int error = 0, response = 0, retval = 0;
2905 int use_timeout = 10800 * 1000;
2907 struct format_defect_list_header fh;
2908 u_int8_t *data_ptr = NULL;
2909 u_int32_t dxfer_len = 0;
2911 int num_warnings = 0;
2914 ccb = cam_getccb(device);
2917 warnx("scsiformat: error allocating ccb");
2921 bzero(&(&ccb->ccb_h)[1],
2922 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
2924 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2945 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
2946 "following device:\n");
2948 error = scsidoinquiry(device, argc, argv, combinedopt,
2949 retry_count, timeout);
2952 warnx("scsiformat: error sending inquiry");
2953 goto scsiformat_bailout;
2962 fprintf(stdout, "Are you SURE you want to do "
2965 if (fgets(str, sizeof(str), stdin) != NULL) {
2967 if (strncasecmp(str, "yes", 3) == 0)
2969 else if (strncasecmp(str, "no", 2) == 0)
2972 fprintf(stdout, "Please answer"
2973 " \"yes\" or \"no\"\n");
2976 } while (response == 0);
2978 if (response == -1) {
2980 goto scsiformat_bailout;
2985 use_timeout = timeout;
2988 fprintf(stdout, "Current format timeout is %d seconds\n",
2989 use_timeout / 1000);
2993 * If the user hasn't disabled questions and didn't specify a
2994 * timeout on the command line, ask them if they want the current
2998 && (timeout == 0)) {
3000 int new_timeout = 0;
3002 fprintf(stdout, "Enter new timeout in seconds or press\n"
3003 "return to keep the current timeout [%d] ",
3004 use_timeout / 1000);
3006 if (fgets(str, sizeof(str), stdin) != NULL) {
3008 new_timeout = atoi(str);
3011 if (new_timeout != 0) {
3012 use_timeout = new_timeout * 1000;
3013 fprintf(stdout, "Using new timeout value %d\n",
3014 use_timeout / 1000);
3019 * Keep this outside the if block below to silence any unused
3020 * variable warnings.
3022 bzero(&fh, sizeof(fh));
3025 * If we're in immediate mode, we've got to include the format
3028 if (immediate != 0) {
3029 fh.byte2 = FU_DLH_IMMED;
3030 data_ptr = (u_int8_t *)&fh;
3031 dxfer_len = sizeof(fh);
3032 byte2 = FU_FMT_DATA;
3033 } else if (quiet == 0) {
3034 fprintf(stdout, "Formatting...");
3038 scsi_format_unit(&ccb->csio,
3039 /* retries */ retry_count,
3041 /* tag_action */ MSG_SIMPLE_Q_TAG,
3044 /* data_ptr */ data_ptr,
3045 /* dxfer_len */ dxfer_len,
3046 /* sense_len */ SSD_FULL_SIZE,
3047 /* timeout */ use_timeout);
3049 /* Disable freezing the device queue */
3050 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3052 if (arglist & CAM_ARG_ERR_RECOVER)
3053 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3055 if (((retval = cam_send_ccb(device, ccb)) < 0)
3056 || ((immediate == 0)
3057 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3058 const char errstr[] = "error sending format command";
3065 if (arglist & CAM_ARG_VERBOSE) {
3066 cam_error_print(device, ccb, CAM_ESF_ALL,
3067 CAM_EPF_ALL, stderr);
3070 goto scsiformat_bailout;
3074 * If we ran in non-immediate mode, we already checked for errors
3075 * above and printed out any necessary information. If we're in
3076 * immediate mode, we need to loop through and get status
3077 * information periodically.
3079 if (immediate == 0) {
3081 fprintf(stdout, "Format Complete\n");
3083 goto scsiformat_bailout;
3090 bzero(&(&ccb->ccb_h)[1],
3091 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3094 * There's really no need to do error recovery or
3095 * retries here, since we're just going to sit in a
3096 * loop and wait for the device to finish formatting.
3098 scsi_test_unit_ready(&ccb->csio,
3101 /* tag_action */ MSG_SIMPLE_Q_TAG,
3102 /* sense_len */ SSD_FULL_SIZE,
3103 /* timeout */ 5000);
3105 /* Disable freezing the device queue */
3106 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3108 retval = cam_send_ccb(device, ccb);
3111 * If we get an error from the ioctl, bail out. SCSI
3112 * errors are expected.
3115 warn("error sending CAMIOCOMMAND ioctl");
3116 if (arglist & CAM_ARG_VERBOSE) {
3117 cam_error_print(device, ccb, CAM_ESF_ALL,
3118 CAM_EPF_ALL, stderr);
3121 goto scsiformat_bailout;
3124 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3126 if ((status != CAM_REQ_CMP)
3127 && (status == CAM_SCSI_STATUS_ERROR)
3128 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3129 struct scsi_sense_data *sense;
3130 int error_code, sense_key, asc, ascq;
3132 sense = &ccb->csio.sense_data;
3133 scsi_extract_sense(sense, &error_code, &sense_key,
3137 * According to the SCSI-2 and SCSI-3 specs, a
3138 * drive that is in the middle of a format should
3139 * return NOT READY with an ASC of "logical unit
3140 * not ready, format in progress". The sense key
3141 * specific bytes will then be a progress indicator.
3143 if ((sense_key == SSD_KEY_NOT_READY)
3144 && (asc == 0x04) && (ascq == 0x04)) {
3145 if ((sense->extra_len >= 10)
3146 && ((sense->sense_key_spec[0] &
3147 SSD_SCS_VALID) != 0)
3150 u_int64_t percentage;
3153 &sense->sense_key_spec[1]);
3154 percentage = 10000 * val;
3157 "\rFormatting: %jd.%02jd %% "
3159 (intmax_t)percentage / (0x10000 * 100),
3160 (intmax_t)(percentage / 0x10000) % 100,
3163 } else if ((quiet == 0)
3164 && (++num_warnings <= 1)) {
3165 warnx("Unexpected SCSI Sense Key "
3166 "Specific value returned "
3168 scsi_sense_print(device, &ccb->csio,
3170 warnx("Unable to print status "
3171 "information, but format will "
3173 warnx("will exit when format is "
3178 warnx("Unexpected SCSI error during format");
3179 cam_error_print(device, ccb, CAM_ESF_ALL,
3180 CAM_EPF_ALL, stderr);
3182 goto scsiformat_bailout;
3185 } else if (status != CAM_REQ_CMP) {
3186 warnx("Unexpected CAM status %#x", status);
3187 if (arglist & CAM_ARG_VERBOSE)
3188 cam_error_print(device, ccb, CAM_ESF_ALL,
3189 CAM_EPF_ALL, stderr);
3191 goto scsiformat_bailout;
3194 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3197 fprintf(stdout, "\nFormat Complete\n");
3207 scsireportluns(struct cam_device *device, int argc, char **argv,
3208 char *combinedopt, int retry_count, int timeout)
3211 int c, countonly, lunsonly;
3212 struct scsi_report_luns_data *lundata;
3214 uint8_t report_type;
3215 uint32_t list_len, i, j;
3220 report_type = RPL_REPORT_DEFAULT;
3221 ccb = cam_getccb(device);
3224 warnx("%s: error allocating ccb", __func__);
3228 bzero(&(&ccb->ccb_h)[1],
3229 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3234 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3243 if (strcasecmp(optarg, "default") == 0)
3244 report_type = RPL_REPORT_DEFAULT;
3245 else if (strcasecmp(optarg, "wellknown") == 0)
3246 report_type = RPL_REPORT_WELLKNOWN;
3247 else if (strcasecmp(optarg, "all") == 0)
3248 report_type = RPL_REPORT_ALL;
3250 warnx("%s: invalid report type \"%s\"",
3261 if ((countonly != 0)
3262 && (lunsonly != 0)) {
3263 warnx("%s: you can only specify one of -c or -l", __func__);
3268 * According to SPC-4, the allocation length must be at least 16
3269 * bytes -- enough for the header and one LUN.
3271 alloc_len = sizeof(*lundata) + 8;
3275 lundata = malloc(alloc_len);
3277 if (lundata == NULL) {
3278 warn("%s: error mallocing %d bytes", __func__, alloc_len);
3283 scsi_report_luns(&ccb->csio,
3284 /*retries*/ retry_count,
3286 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3287 /*select_report*/ report_type,
3288 /*rpl_buf*/ lundata,
3289 /*alloc_len*/ alloc_len,
3290 /*sense_len*/ SSD_FULL_SIZE,
3291 /*timeout*/ timeout ? timeout : 5000);
3293 /* Disable freezing the device queue */
3294 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3296 if (arglist & CAM_ARG_ERR_RECOVER)
3297 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3299 if (cam_send_ccb(device, ccb) < 0) {
3300 warn("error sending REPORT LUNS command");
3302 if (arglist & CAM_ARG_VERBOSE)
3303 cam_error_print(device, ccb, CAM_ESF_ALL,
3304 CAM_EPF_ALL, stderr);
3310 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3311 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3317 list_len = scsi_4btoul(lundata->length);
3320 * If we need to list the LUNs, and our allocation
3321 * length was too short, reallocate and retry.
3323 if ((countonly == 0)
3324 && (list_len > (alloc_len - sizeof(*lundata)))) {
3325 alloc_len = list_len + sizeof(*lundata);
3331 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
3332 ((list_len / 8) > 1) ? "s" : "");
3337 for (i = 0; i < (list_len / 8); i++) {
3341 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
3343 fprintf(stdout, ",");
3344 switch (lundata->luns[i].lundata[j] &
3345 RPL_LUNDATA_ATYP_MASK) {
3346 case RPL_LUNDATA_ATYP_PERIPH:
3347 if ((lundata->luns[i].lundata[j] &
3348 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
3349 fprintf(stdout, "%d:",
3350 lundata->luns[i].lundata[j] &
3351 RPL_LUNDATA_PERIPH_BUS_MASK);
3353 && ((lundata->luns[i].lundata[j+2] &
3354 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
3357 fprintf(stdout, "%d",
3358 lundata->luns[i].lundata[j+1]);
3360 case RPL_LUNDATA_ATYP_FLAT: {
3362 tmplun[0] = lundata->luns[i].lundata[j] &
3363 RPL_LUNDATA_FLAT_LUN_MASK;
3364 tmplun[1] = lundata->luns[i].lundata[j+1];
3366 fprintf(stdout, "%d", scsi_2btoul(tmplun));
3370 case RPL_LUNDATA_ATYP_LUN:
3371 fprintf(stdout, "%d:%d:%d",
3372 (lundata->luns[i].lundata[j+1] &
3373 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
3374 lundata->luns[i].lundata[j] &
3375 RPL_LUNDATA_LUN_TARG_MASK,
3376 lundata->luns[i].lundata[j+1] &
3377 RPL_LUNDATA_LUN_LUN_MASK);
3379 case RPL_LUNDATA_ATYP_EXTLUN: {
3380 int field_len, field_len_code, eam_code;
3382 eam_code = lundata->luns[i].lundata[j] &
3383 RPL_LUNDATA_EXT_EAM_MASK;
3384 field_len_code = (lundata->luns[i].lundata[j] &
3385 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
3386 field_len = field_len_code * 2;
3388 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
3389 && (field_len_code == 0x00)) {
3390 fprintf(stdout, "%d",
3391 lundata->luns[i].lundata[j+1]);
3392 } else if ((eam_code ==
3393 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
3394 && (field_len_code == 0x03)) {
3398 * This format takes up all 8 bytes.
3399 * If we aren't starting at offset 0,
3403 fprintf(stdout, "Invalid "
3406 "specified format", j);
3410 bzero(tmp_lun, sizeof(tmp_lun));
3411 bcopy(&lundata->luns[i].lundata[j+1],
3412 &tmp_lun[1], sizeof(tmp_lun) - 1);
3413 fprintf(stdout, "%#jx",
3414 (intmax_t)scsi_8btou64(tmp_lun));
3417 fprintf(stderr, "Unknown Extended LUN"
3418 "Address method %#x, length "
3419 "code %#x", eam_code,
3426 fprintf(stderr, "Unknown LUN address method "
3427 "%#x\n", lundata->luns[i].lundata[0] &
3428 RPL_LUNDATA_ATYP_MASK);
3432 * For the flat addressing method, there are no
3433 * other levels after it.
3438 fprintf(stdout, "\n");
3451 scsireadcapacity(struct cam_device *device, int argc, char **argv,
3452 char *combinedopt, int retry_count, int timeout)
3455 int blocksizeonly, humanize, numblocks, quiet, sizeonly, baseten;
3456 struct scsi_read_capacity_data rcap;
3457 struct scsi_read_capacity_data_16 rcaplong;
3471 ccb = cam_getccb(device);
3474 warnx("%s: error allocating ccb", __func__);
3478 bzero(&(&ccb->ccb_h)[1],
3479 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3481 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3508 if ((blocksizeonly != 0)
3509 && (numblocks != 0)) {
3510 warnx("%s: you can only specify one of -b or -N", __func__);
3515 if ((blocksizeonly != 0)
3516 && (sizeonly != 0)) {
3517 warnx("%s: you can only specify one of -b or -s", __func__);
3524 warnx("%s: you can only specify one of -h/-H or -q", __func__);
3530 && (blocksizeonly != 0)) {
3531 warnx("%s: you can only specify one of -h/-H or -b", __func__);
3536 scsi_read_capacity(&ccb->csio,
3537 /*retries*/ retry_count,
3539 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3542 /*timeout*/ timeout ? timeout : 5000);
3544 /* Disable freezing the device queue */
3545 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3547 if (arglist & CAM_ARG_ERR_RECOVER)
3548 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3550 if (cam_send_ccb(device, ccb) < 0) {
3551 warn("error sending READ CAPACITY command");
3553 if (arglist & CAM_ARG_VERBOSE)
3554 cam_error_print(device, ccb, CAM_ESF_ALL,
3555 CAM_EPF_ALL, stderr);
3561 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3562 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3567 maxsector = scsi_4btoul(rcap.addr);
3568 block_len = scsi_4btoul(rcap.length);
3571 * A last block of 2^32-1 means that the true capacity is over 2TB,
3572 * and we need to issue the long READ CAPACITY to get the real
3573 * capacity. Otherwise, we're all set.
3575 if (maxsector != 0xffffffff)
3578 scsi_read_capacity_16(&ccb->csio,
3579 /*retries*/ retry_count,
3581 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3586 /*sense_len*/ SSD_FULL_SIZE,
3587 /*timeout*/ timeout ? timeout : 5000);
3589 /* Disable freezing the device queue */
3590 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3592 if (arglist & CAM_ARG_ERR_RECOVER)
3593 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3595 if (cam_send_ccb(device, ccb) < 0) {
3596 warn("error sending READ CAPACITY (16) command");
3598 if (arglist & CAM_ARG_VERBOSE)
3599 cam_error_print(device, ccb, CAM_ESF_ALL,
3600 CAM_EPF_ALL, stderr);
3606 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3607 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3612 maxsector = scsi_8btou64(rcaplong.addr);
3613 block_len = scsi_4btoul(rcaplong.length);
3616 if (blocksizeonly == 0) {
3618 * Humanize implies !quiet, and also implies numblocks.
3620 if (humanize != 0) {
3625 tmpbytes = (maxsector + 1) * block_len;
3626 ret = humanize_number(tmpstr, sizeof(tmpstr),
3627 tmpbytes, "", HN_AUTOSCALE,
3630 HN_DIVISOR_1000 : 0));
3632 warnx("%s: humanize_number failed!", __func__);
3636 fprintf(stdout, "Device Size: %s%s", tmpstr,
3637 (sizeonly == 0) ? ", " : "\n");
3638 } else if (numblocks != 0) {
3639 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
3640 "Blocks: " : "", (uintmax_t)maxsector + 1,
3641 (sizeonly == 0) ? ", " : "\n");
3643 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
3644 "Last Block: " : "", (uintmax_t)maxsector,
3645 (sizeonly == 0) ? ", " : "\n");
3649 fprintf(stdout, "%s%u%s\n", (quiet == 0) ?
3650 "Block Length: " : "", block_len, (quiet == 0) ?
3658 #endif /* MINIMALISTIC */
3663 fprintf(verbose ? stdout : stderr,
3664 "usage: camcontrol <command> [device id][generic args][command args]\n"
3665 " camcontrol devlist [-v]\n"
3666 #ifndef MINIMALISTIC
3667 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
3668 " camcontrol tur [dev_id][generic args]\n"
3669 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
3670 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
3671 " camcontrol readcap [dev_id][generic args] [-b] [-h] [-H] [-N]\n"
3673 " camcontrol start [dev_id][generic args]\n"
3674 " camcontrol stop [dev_id][generic args]\n"
3675 " camcontrol load [dev_id][generic args]\n"
3676 " camcontrol eject [dev_id][generic args]\n"
3677 #endif /* MINIMALISTIC */
3678 " camcontrol rescan <all | bus[:target:lun]>\n"
3679 " camcontrol reset <all | bus[:target:lun]>\n"
3680 #ifndef MINIMALISTIC
3681 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
3682 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
3683 " [-P pagectl][-e | -b][-d]\n"
3684 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n"
3685 " [-i len fmt|-o len fmt [args]]\n"
3686 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
3687 " <all|bus[:target[:lun]]|off>\n"
3688 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
3689 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
3690 " [-D <enable|disable>][-O offset][-q]\n"
3691 " [-R syncrate][-v][-T <enable|disable>]\n"
3692 " [-U][-W bus_width]\n"
3693 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
3694 #endif /* MINIMALISTIC */
3695 " camcontrol help\n");
3698 #ifndef MINIMALISTIC
3700 "Specify one of the following options:\n"
3701 "devlist list all CAM devices\n"
3702 "periphlist list all CAM peripheral drivers attached to a device\n"
3703 "tur send a test unit ready to the named device\n"
3704 "inquiry send a SCSI inquiry command to the named device\n"
3705 "reportluns send a SCSI report luns command to the device\n"
3706 "readcap send a SCSI read capacity command to the device\n"
3707 "start send a Start Unit command to the device\n"
3708 "stop send a Stop Unit command to the device\n"
3709 "load send a Start Unit command to the device with the load bit set\n"
3710 "eject send a Stop Unit command to the device with the eject bit set\n"
3711 "rescan rescan all busses, the given bus, or bus:target:lun\n"
3712 "reset reset all busses, the given bus, or bus:target:lun\n"
3713 "defects read the defect list of the specified device\n"
3714 "modepage display or edit (-e) the given mode page\n"
3715 "cmd send the given scsi command, may need -i or -o as well\n"
3716 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
3717 "tags report or set the number of transaction slots for a device\n"
3718 "negotiate report or set device negotiation parameters\n"
3719 "format send the SCSI FORMAT UNIT command to the named device\n"
3720 "help this message\n"
3721 "Device Identifiers:\n"
3722 "bus:target specify the bus and target, lun defaults to 0\n"
3723 "bus:target:lun specify the bus, target and lun\n"
3724 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
3725 "Generic arguments:\n"
3726 "-v be verbose, print out sense information\n"
3727 "-t timeout command timeout in seconds, overrides default timeout\n"
3728 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
3729 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
3730 "-E have the kernel attempt to perform SCSI error recovery\n"
3731 "-C count specify the SCSI command retry count (needs -E to work)\n"
3732 "modepage arguments:\n"
3733 "-l list all available mode pages\n"
3734 "-m page specify the mode page to view or edit\n"
3735 "-e edit the specified mode page\n"
3736 "-b force view to binary mode\n"
3737 "-d disable block descriptors for mode sense\n"
3738 "-P pgctl page control field 0-3\n"
3739 "defects arguments:\n"
3740 "-f format specify defect list format (block, bfi or phys)\n"
3741 "-G get the grown defect list\n"
3742 "-P get the permanent defect list\n"
3743 "inquiry arguments:\n"
3744 "-D get the standard inquiry data\n"
3745 "-S get the serial number\n"
3746 "-R get the transfer rate, etc.\n"
3747 "reportluns arguments:\n"
3748 "-c only report a count of available LUNs\n"
3749 "-l only print out luns, and not a count\n"
3750 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
3751 "readcap arguments\n"
3752 "-b only report the blocksize\n"
3753 "-h human readable device size, base 2\n"
3754 "-H human readable device size, base 10\n"
3755 "-N print the number of blocks instead of last block\n"
3756 "-q quiet, print numbers only\n"
3757 "-s only report the last block/device size\n"
3759 "-c cdb [args] specify the SCSI CDB\n"
3760 "-i len fmt specify input data and input data format\n"
3761 "-o len fmt [args] specify output data and output data fmt\n"
3762 "debug arguments:\n"
3763 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
3764 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
3765 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
3766 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
3768 "-N tags specify the number of tags to use for this device\n"
3769 "-q be quiet, don't report the number of tags\n"
3770 "-v report a number of tag-related parameters\n"
3771 "negotiate arguments:\n"
3772 "-a send a test unit ready after negotiation\n"
3773 "-c report/set current negotiation settings\n"
3774 "-D <arg> \"enable\" or \"disable\" disconnection\n"
3775 "-O offset set command delay offset\n"
3776 "-q be quiet, don't report anything\n"
3777 "-R syncrate synchronization rate in MHz\n"
3778 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
3779 "-U report/set user negotiation settings\n"
3780 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
3781 "-v also print a Path Inquiry CCB for the controller\n"
3782 "format arguments:\n"
3783 "-q be quiet, don't print status messages\n"
3784 "-r run in report only mode\n"
3785 "-w don't send immediate format command\n"
3786 "-y don't ask any questions\n");
3787 #endif /* MINIMALISTIC */
3791 main(int argc, char **argv)
3794 char *device = NULL;
3796 struct cam_device *cam_dev = NULL;
3797 int timeout = 0, retry_count = 1;
3798 camcontrol_optret optreturn;
3800 const char *mainopt = "C:En:t:u:v";
3801 const char *subopt = NULL;
3802 char combinedopt[256];
3803 int error = 0, optstart = 2;
3806 cmdlist = CAM_CMD_NONE;
3807 arglist = CAM_ARG_NONE;
3815 * Get the base option.
3817 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
3819 if (optreturn == CC_OR_AMBIGUOUS) {
3820 warnx("ambiguous option %s", argv[1]);
3823 } else if (optreturn == CC_OR_NOT_FOUND) {
3824 warnx("option %s not found", argv[1]);
3830 * Ahh, getopt(3) is a pain.
3832 * This is a gross hack. There really aren't many other good
3833 * options (excuse the pun) for parsing options in a situation like
3834 * this. getopt is kinda braindead, so you end up having to run
3835 * through the options twice, and give each invocation of getopt
3836 * the option string for the other invocation.
3838 * You would think that you could just have two groups of options.
3839 * The first group would get parsed by the first invocation of
3840 * getopt, and the second group would get parsed by the second
3841 * invocation of getopt. It doesn't quite work out that way. When
3842 * the first invocation of getopt finishes, it leaves optind pointing
3843 * to the argument _after_ the first argument in the second group.
3844 * So when the second invocation of getopt comes around, it doesn't
3845 * recognize the first argument it gets and then bails out.
3847 * A nice alternative would be to have a flag for getopt that says
3848 * "just keep parsing arguments even when you encounter an unknown
3849 * argument", but there isn't one. So there's no real clean way to
3850 * easily parse two sets of arguments without having one invocation
3851 * of getopt know about the other.
3853 * Without this hack, the first invocation of getopt would work as
3854 * long as the generic arguments are first, but the second invocation
3855 * (in the subfunction) would fail in one of two ways. In the case
3856 * where you don't set optreset, it would fail because optind may be
3857 * pointing to the argument after the one it should be pointing at.
3858 * In the case where you do set optreset, and reset optind, it would
3859 * fail because getopt would run into the first set of options, which
3860 * it doesn't understand.
3862 * All of this would "sort of" work if you could somehow figure out
3863 * whether optind had been incremented one option too far. The
3864 * mechanics of that, however, are more daunting than just giving
3865 * both invocations all of the expect options for either invocation.
3867 * Needless to say, I wouldn't mind if someone invented a better
3868 * (non-GPL!) command line parsing interface than getopt. I
3869 * wouldn't mind if someone added more knobs to getopt to make it
3870 * work better. Who knows, I may talk myself into doing it someday,
3871 * if the standards weenies let me. As it is, it just leads to
3872 * hackery like this and causes people to avoid it in some cases.
3874 * KDM, September 8th, 1998
3877 sprintf(combinedopt, "%s%s", mainopt, subopt);
3879 sprintf(combinedopt, "%s", mainopt);
3882 * For these options we do not parse optional device arguments and
3883 * we do not open a passthrough device.
3885 if ((cmdlist == CAM_CMD_RESCAN)
3886 || (cmdlist == CAM_CMD_RESET)
3887 || (cmdlist == CAM_CMD_DEVTREE)
3888 || (cmdlist == CAM_CMD_USAGE)
3889 || (cmdlist == CAM_CMD_DEBUG))
3892 #ifndef MINIMALISTIC
3894 && (argc > 2 && argv[2][0] != '-')) {
3899 * First catch people who try to do things like:
3900 * camcontrol tur /dev/da0
3901 * camcontrol doesn't take device nodes as arguments.
3903 if (argv[2][0] == '/') {
3904 warnx("%s is not a valid device identifier", argv[2]);
3905 errx(1, "please read the camcontrol(8) man page");
3906 } else if (isdigit(argv[2][0])) {
3907 /* device specified as bus:target[:lun] */
3908 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
3910 errx(1, "numeric device specification must "
3911 "be either bus:target, or "
3913 /* default to 0 if lun was not specified */
3914 if ((arglist & CAM_ARG_LUN) == 0) {
3916 arglist |= CAM_ARG_LUN;
3920 if (cam_get_device(argv[2], name, sizeof name, &unit)
3922 errx(1, "%s", cam_errbuf);
3923 device = strdup(name);
3924 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
3928 #endif /* MINIMALISTIC */
3930 * Start getopt processing at argv[2/3], since we've already
3931 * accepted argv[1..2] as the command name, and as a possible
3937 * Now we run through the argument list looking for generic
3938 * options, and ignoring options that possibly belong to
3941 while ((c = getopt(argc, argv, combinedopt))!= -1){
3944 retry_count = strtol(optarg, NULL, 0);
3945 if (retry_count < 0)
3946 errx(1, "retry count %d is < 0",
3948 arglist |= CAM_ARG_RETRIES;
3951 arglist |= CAM_ARG_ERR_RECOVER;
3954 arglist |= CAM_ARG_DEVICE;
3956 while (isspace(*tstr) && (*tstr != '\0'))
3958 device = (char *)strdup(tstr);
3961 timeout = strtol(optarg, NULL, 0);
3963 errx(1, "invalid timeout %d", timeout);
3964 /* Convert the timeout from seconds to ms */
3966 arglist |= CAM_ARG_TIMEOUT;
3969 arglist |= CAM_ARG_UNIT;
3970 unit = strtol(optarg, NULL, 0);
3973 arglist |= CAM_ARG_VERBOSE;
3980 #ifndef MINIMALISTIC
3982 * For most commands we'll want to open the passthrough device
3983 * associated with the specified device. In the case of the rescan
3984 * commands, we don't use a passthrough device at all, just the
3985 * transport layer device.
3988 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
3989 && (((arglist & CAM_ARG_DEVICE) == 0)
3990 || ((arglist & CAM_ARG_UNIT) == 0))) {
3991 errx(1, "subcommand \"%s\" requires a valid device "
3992 "identifier", argv[1]);
3995 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
3996 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
3997 cam_open_spec_device(device,unit,O_RDWR,NULL)))
3999 errx(1,"%s", cam_errbuf);
4001 #endif /* MINIMALISTIC */
4004 * Reset optind to 2, and reset getopt, so these routines can parse
4005 * the arguments again.
4011 #ifndef MINIMALISTIC
4012 case CAM_CMD_DEVLIST:
4013 error = getdevlist(cam_dev);
4015 #endif /* MINIMALISTIC */
4016 case CAM_CMD_DEVTREE:
4017 error = getdevtree();
4019 #ifndef MINIMALISTIC
4021 error = testunitready(cam_dev, retry_count, timeout, 0);
4023 case CAM_CMD_INQUIRY:
4024 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
4025 retry_count, timeout);
4027 case CAM_CMD_STARTSTOP:
4028 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
4029 arglist & CAM_ARG_EJECT, retry_count,
4032 #endif /* MINIMALISTIC */
4033 case CAM_CMD_RESCAN:
4034 error = dorescan_or_reset(argc, argv, 1);
4037 error = dorescan_or_reset(argc, argv, 0);
4039 #ifndef MINIMALISTIC
4040 case CAM_CMD_READ_DEFECTS:
4041 error = readdefects(cam_dev, argc, argv, combinedopt,
4042 retry_count, timeout);
4044 case CAM_CMD_MODE_PAGE:
4045 modepage(cam_dev, argc, argv, combinedopt,
4046 retry_count, timeout);
4048 case CAM_CMD_SCSI_CMD:
4049 error = scsicmd(cam_dev, argc, argv, combinedopt,
4050 retry_count, timeout);
4053 error = camdebug(argc, argv, combinedopt);
4056 error = tagcontrol(cam_dev, argc, argv, combinedopt);
4059 error = ratecontrol(cam_dev, retry_count, timeout,
4060 argc, argv, combinedopt);
4062 case CAM_CMD_FORMAT:
4063 error = scsiformat(cam_dev, argc, argv,
4064 combinedopt, retry_count, timeout);
4066 case CAM_CMD_REPORTLUNS:
4067 error = scsireportluns(cam_dev, argc, argv,
4068 combinedopt, retry_count,
4071 case CAM_CMD_READCAP:
4072 error = scsireadcapacity(cam_dev, argc, argv,
4073 combinedopt, retry_count,
4076 #endif /* MINIMALISTIC */
4086 if (cam_dev != NULL)
4087 cam_close_device(cam_dev);