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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/ioctl.h>
33 #include <sys/stdint.h>
34 #include <sys/types.h>
35 #include <sys/endian.h>
47 #include <cam/cam_debug.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/scsi/scsi_all.h>
50 #include <cam/scsi/scsi_da.h>
51 #include <cam/scsi/scsi_pass.h>
52 #include <cam/scsi/scsi_message.h>
53 #include <cam/ata/ata_all.h>
55 #include "camcontrol.h"
58 CAM_CMD_NONE = 0x00000000,
59 CAM_CMD_DEVLIST = 0x00000001,
60 CAM_CMD_TUR = 0x00000002,
61 CAM_CMD_INQUIRY = 0x00000003,
62 CAM_CMD_STARTSTOP = 0x00000004,
63 CAM_CMD_RESCAN = 0x00000005,
64 CAM_CMD_READ_DEFECTS = 0x00000006,
65 CAM_CMD_MODE_PAGE = 0x00000007,
66 CAM_CMD_SCSI_CMD = 0x00000008,
67 CAM_CMD_DEVTREE = 0x00000009,
68 CAM_CMD_USAGE = 0x0000000a,
69 CAM_CMD_DEBUG = 0x0000000b,
70 CAM_CMD_RESET = 0x0000000c,
71 CAM_CMD_FORMAT = 0x0000000d,
72 CAM_CMD_TAG = 0x0000000e,
73 CAM_CMD_RATE = 0x0000000f,
74 CAM_CMD_DETACH = 0x00000010,
75 CAM_CMD_REPORTLUNS = 0x00000011,
76 CAM_CMD_READCAP = 0x00000012,
77 CAM_CMD_IDENTIFY = 0x00000013
81 CAM_ARG_NONE = 0x00000000,
82 CAM_ARG_VERBOSE = 0x00000001,
83 CAM_ARG_DEVICE = 0x00000002,
84 CAM_ARG_BUS = 0x00000004,
85 CAM_ARG_TARGET = 0x00000008,
86 CAM_ARG_LUN = 0x00000010,
87 CAM_ARG_EJECT = 0x00000020,
88 CAM_ARG_UNIT = 0x00000040,
89 CAM_ARG_FORMAT_BLOCK = 0x00000080,
90 CAM_ARG_FORMAT_BFI = 0x00000100,
91 CAM_ARG_FORMAT_PHYS = 0x00000200,
92 CAM_ARG_PLIST = 0x00000400,
93 CAM_ARG_GLIST = 0x00000800,
94 CAM_ARG_GET_SERIAL = 0x00001000,
95 CAM_ARG_GET_STDINQ = 0x00002000,
96 CAM_ARG_GET_XFERRATE = 0x00004000,
97 CAM_ARG_INQ_MASK = 0x00007000,
98 CAM_ARG_MODE_EDIT = 0x00008000,
99 CAM_ARG_PAGE_CNTL = 0x00010000,
100 CAM_ARG_TIMEOUT = 0x00020000,
101 CAM_ARG_CMD_IN = 0x00040000,
102 CAM_ARG_CMD_OUT = 0x00080000,
103 CAM_ARG_DBD = 0x00100000,
104 CAM_ARG_ERR_RECOVER = 0x00200000,
105 CAM_ARG_RETRIES = 0x00400000,
106 CAM_ARG_START_UNIT = 0x00800000,
107 CAM_ARG_DEBUG_INFO = 0x01000000,
108 CAM_ARG_DEBUG_TRACE = 0x02000000,
109 CAM_ARG_DEBUG_SUBTRACE = 0x04000000,
110 CAM_ARG_DEBUG_CDB = 0x08000000,
111 CAM_ARG_DEBUG_XPT = 0x10000000,
112 CAM_ARG_DEBUG_PERIPH = 0x20000000,
115 struct camcontrol_opts {
123 static const char scsicmd_opts[] = "a:c:i:o:r";
124 static const char readdefect_opts[] = "f:GP";
125 static const char negotiate_opts[] = "acD:O:qR:T:UW:";
128 struct camcontrol_opts option_table[] = {
130 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
131 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
132 {"identify", CAM_CMD_IDENTIFY, CAM_ARG_NONE, NULL},
133 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
134 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
135 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
136 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
137 {"reportluns", CAM_CMD_REPORTLUNS, CAM_ARG_NONE, "clr:"},
138 {"readcapacity", CAM_CMD_READCAP, CAM_ARG_NONE, "bhHNqs"},
139 #endif /* MINIMALISTIC */
140 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
141 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
143 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
144 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
145 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
146 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
147 #endif /* MINIMALISTIC */
148 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL},
150 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
151 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"},
152 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
153 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
154 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
155 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"},
156 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
157 #endif /* MINIMALISTIC */
158 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
159 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
160 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
174 camcontrol_optret getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
175 const char **subopt);
177 static int getdevlist(struct cam_device *device);
178 #endif /* MINIMALISTIC */
179 static int getdevtree(void);
181 static int testunitready(struct cam_device *device, int retry_count,
182 int timeout, int quiet);
183 static int scsistart(struct cam_device *device, int startstop, int loadeject,
184 int retry_count, int timeout);
185 static int scsidoinquiry(struct cam_device *device, int argc, char **argv,
186 char *combinedopt, int retry_count, int timeout);
187 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout);
188 static int scsiserial(struct cam_device *device, int retry_count, int timeout);
189 static int scsixferrate(struct cam_device *device);
190 #endif /* MINIMALISTIC */
191 static int parse_btl(char *tstr, int *bus, int *target, int *lun,
192 cam_argmask *arglst);
193 static int dorescan_or_reset(int argc, char **argv, int rescan);
194 static int rescan_or_reset_bus(int bus, int rescan);
195 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan);
197 static int readdefects(struct cam_device *device, int argc, char **argv,
198 char *combinedopt, int retry_count, int timeout);
199 static void modepage(struct cam_device *device, int argc, char **argv,
200 char *combinedopt, int retry_count, int timeout);
201 static int scsicmd(struct cam_device *device, int argc, char **argv,
202 char *combinedopt, int retry_count, int timeout);
203 static int tagcontrol(struct cam_device *device, int argc, char **argv,
205 static void cts_print(struct cam_device *device,
206 struct ccb_trans_settings *cts);
207 static void cpi_print(struct ccb_pathinq *cpi);
208 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
209 static int get_cgd(struct cam_device *device, struct ccb_getdev *cgd);
210 static int get_print_cts(struct cam_device *device, int user_settings,
211 int quiet, struct ccb_trans_settings *cts);
212 static int ratecontrol(struct cam_device *device, int retry_count,
213 int timeout, int argc, char **argv, char *combinedopt);
214 static int scsiformat(struct cam_device *device, int argc, char **argv,
215 char *combinedopt, int retry_count, int timeout);
216 static int scsireportluns(struct cam_device *device, int argc, char **argv,
217 char *combinedopt, int retry_count, int timeout);
218 static int scsireadcapacity(struct cam_device *device, int argc, char **argv,
219 char *combinedopt, int retry_count, int timeout);
220 #endif /* MINIMALISTIC */
223 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum,
226 struct camcontrol_opts *opts;
229 for (opts = option_table; (opts != NULL) && (opts->optname != NULL);
231 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
232 *cmdnum = opts->cmdnum;
233 *argnum = opts->argnum;
234 *subopt = opts->subopt;
235 if (++num_matches > 1)
236 return(CC_OR_AMBIGUOUS);
243 return(CC_OR_NOT_FOUND);
248 getdevlist(struct cam_device *device)
254 ccb = cam_getccb(device);
256 ccb->ccb_h.func_code = XPT_GDEVLIST;
257 ccb->ccb_h.flags = CAM_DIR_NONE;
258 ccb->ccb_h.retry_count = 1;
260 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
261 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
262 if (cam_send_ccb(device, ccb) < 0) {
263 perror("error getting device list");
270 switch (ccb->cgdl.status) {
271 case CAM_GDEVLIST_MORE_DEVS:
272 strcpy(status, "MORE");
274 case CAM_GDEVLIST_LAST_DEVICE:
275 strcpy(status, "LAST");
277 case CAM_GDEVLIST_LIST_CHANGED:
278 strcpy(status, "CHANGED");
280 case CAM_GDEVLIST_ERROR:
281 strcpy(status, "ERROR");
286 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
287 ccb->cgdl.periph_name,
288 ccb->cgdl.unit_number,
289 ccb->cgdl.generation,
294 * If the list has changed, we need to start over from the
297 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
305 #endif /* MINIMALISTIC */
317 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
318 warn("couldn't open %s", XPT_DEVICE);
322 bzero(&ccb, sizeof(union ccb));
324 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
325 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
326 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
328 ccb.ccb_h.func_code = XPT_DEV_MATCH;
329 bufsize = sizeof(struct dev_match_result) * 100;
330 ccb.cdm.match_buf_len = bufsize;
331 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
332 if (ccb.cdm.matches == NULL) {
333 warnx("can't malloc memory for matches");
337 ccb.cdm.num_matches = 0;
340 * We fetch all nodes, since we display most of them in the default
341 * case, and all in the verbose case.
343 ccb.cdm.num_patterns = 0;
344 ccb.cdm.pattern_buf_len = 0;
347 * We do the ioctl multiple times if necessary, in case there are
348 * more than 100 nodes in the EDT.
351 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
352 warn("error sending CAMIOCOMMAND ioctl");
357 if ((ccb.ccb_h.status != CAM_REQ_CMP)
358 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
359 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
360 warnx("got CAM error %#x, CDM error %d\n",
361 ccb.ccb_h.status, ccb.cdm.status);
366 for (i = 0; i < ccb.cdm.num_matches; i++) {
367 switch (ccb.cdm.matches[i].type) {
368 case DEV_MATCH_BUS: {
369 struct bus_match_result *bus_result;
372 * Only print the bus information if the
373 * user turns on the verbose flag.
375 if ((arglist & CAM_ARG_VERBOSE) == 0)
379 &ccb.cdm.matches[i].result.bus_result;
382 fprintf(stdout, ")\n");
386 fprintf(stdout, "scbus%d on %s%d bus %d:\n",
388 bus_result->dev_name,
389 bus_result->unit_number,
393 case DEV_MATCH_DEVICE: {
394 struct device_match_result *dev_result;
395 char vendor[16], product[48], revision[16];
399 &ccb.cdm.matches[i].result.device_result;
401 if ((dev_result->flags
402 & DEV_RESULT_UNCONFIGURED)
403 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
409 if (dev_result->protocol == PROTO_SCSI) {
410 cam_strvis(vendor, dev_result->inq_data.vendor,
411 sizeof(dev_result->inq_data.vendor),
414 dev_result->inq_data.product,
415 sizeof(dev_result->inq_data.product),
418 dev_result->inq_data.revision,
419 sizeof(dev_result->inq_data.revision),
421 sprintf(tmpstr, "<%s %s %s>", vendor, product,
423 } else if (dev_result->protocol == PROTO_ATA ||
424 dev_result->protocol == PROTO_SATAPM) {
426 dev_result->ident_data.model,
427 sizeof(dev_result->ident_data.model),
430 dev_result->ident_data.revision,
431 sizeof(dev_result->ident_data.revision),
433 sprintf(tmpstr, "<%s %s>", product,
436 sprintf(tmpstr, "<>");
439 fprintf(stdout, ")\n");
443 fprintf(stdout, "%-33s at scbus%d "
444 "target %d lun %d (",
447 dev_result->target_id,
448 dev_result->target_lun);
454 case DEV_MATCH_PERIPH: {
455 struct periph_match_result *periph_result;
458 &ccb.cdm.matches[i].result.periph_result;
460 if (skip_device != 0)
464 fprintf(stdout, ",");
466 fprintf(stdout, "%s%d",
467 periph_result->periph_name,
468 periph_result->unit_number);
474 fprintf(stdout, "unknown match type\n");
479 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
480 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
483 fprintf(stdout, ")\n");
492 testunitready(struct cam_device *device, int retry_count, int timeout,
498 ccb = cam_getccb(device);
500 scsi_test_unit_ready(&ccb->csio,
501 /* retries */ retry_count,
503 /* tag_action */ MSG_SIMPLE_Q_TAG,
504 /* sense_len */ SSD_FULL_SIZE,
505 /* timeout */ timeout ? timeout : 5000);
507 /* Disable freezing the device queue */
508 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
510 if (arglist & CAM_ARG_ERR_RECOVER)
511 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
513 if (cam_send_ccb(device, ccb) < 0) {
515 perror("error sending test unit ready");
517 if (arglist & CAM_ARG_VERBOSE) {
518 cam_error_print(device, ccb, CAM_ESF_ALL,
519 CAM_EPF_ALL, stderr);
526 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
528 fprintf(stdout, "Unit is ready\n");
531 fprintf(stdout, "Unit is not ready\n");
534 if (arglist & CAM_ARG_VERBOSE) {
535 cam_error_print(device, ccb, CAM_ESF_ALL,
536 CAM_EPF_ALL, stderr);
546 scsistart(struct cam_device *device, int startstop, int loadeject,
547 int retry_count, int timeout)
552 ccb = cam_getccb(device);
555 * If we're stopping, send an ordered tag so the drive in question
556 * will finish any previously queued writes before stopping. If
557 * the device isn't capable of tagged queueing, or if tagged
558 * queueing is turned off, the tag action is a no-op.
560 scsi_start_stop(&ccb->csio,
561 /* retries */ retry_count,
563 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG :
565 /* start/stop */ startstop,
566 /* load_eject */ loadeject,
568 /* sense_len */ SSD_FULL_SIZE,
569 /* timeout */ timeout ? timeout : 120000);
571 /* Disable freezing the device queue */
572 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
574 if (arglist & CAM_ARG_ERR_RECOVER)
575 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
577 if (cam_send_ccb(device, ccb) < 0) {
578 perror("error sending start unit");
580 if (arglist & CAM_ARG_VERBOSE) {
581 cam_error_print(device, ccb, CAM_ESF_ALL,
582 CAM_EPF_ALL, stderr);
589 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
591 fprintf(stdout, "Unit started successfully");
593 fprintf(stdout,", Media loaded\n");
595 fprintf(stdout,"\n");
597 fprintf(stdout, "Unit stopped successfully");
599 fprintf(stdout, ", Media ejected\n");
601 fprintf(stdout, "\n");
607 "Error received from start unit command\n");
610 "Error received from stop unit command\n");
612 if (arglist & CAM_ARG_VERBOSE) {
613 cam_error_print(device, ccb, CAM_ESF_ALL,
614 CAM_EPF_ALL, stderr);
624 scsidoinquiry(struct cam_device *device, int argc, char **argv,
625 char *combinedopt, int retry_count, int timeout)
630 while ((c = getopt(argc, argv, combinedopt)) != -1) {
633 arglist |= CAM_ARG_GET_STDINQ;
636 arglist |= CAM_ARG_GET_XFERRATE;
639 arglist |= CAM_ARG_GET_SERIAL;
647 * If the user didn't specify any inquiry options, he wants all of
650 if ((arglist & CAM_ARG_INQ_MASK) == 0)
651 arglist |= CAM_ARG_INQ_MASK;
653 if (arglist & CAM_ARG_GET_STDINQ)
654 error = scsiinquiry(device, retry_count, timeout);
659 if (arglist & CAM_ARG_GET_SERIAL)
660 scsiserial(device, retry_count, timeout);
665 if (arglist & CAM_ARG_GET_XFERRATE)
666 error = scsixferrate(device);
672 scsiinquiry(struct cam_device *device, int retry_count, int timeout)
675 struct scsi_inquiry_data *inq_buf;
678 ccb = cam_getccb(device);
681 warnx("couldn't allocate CCB");
685 /* cam_getccb cleans up the header, caller has to zero the payload */
686 bzero(&(&ccb->ccb_h)[1],
687 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
689 inq_buf = (struct scsi_inquiry_data *)malloc(
690 sizeof(struct scsi_inquiry_data));
692 if (inq_buf == NULL) {
694 warnx("can't malloc memory for inquiry\n");
697 bzero(inq_buf, sizeof(*inq_buf));
700 * Note that although the size of the inquiry buffer is the full
701 * 256 bytes specified in the SCSI spec, we only tell the device
702 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
703 * two reasons for this:
705 * - The SCSI spec says that when a length field is only 1 byte,
706 * a value of 0 will be interpreted as 256. Therefore
707 * scsi_inquiry() will convert an inq_len (which is passed in as
708 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
709 * to 0. Evidently, very few devices meet the spec in that
710 * regard. Some devices, like many Seagate disks, take the 0 as
711 * 0, and don't return any data. One Pioneer DVD-R drive
712 * returns more data than the command asked for.
714 * So, since there are numerous devices that just don't work
715 * right with the full inquiry size, we don't send the full size.
717 * - The second reason not to use the full inquiry data length is
718 * that we don't need it here. The only reason we issue a
719 * standard inquiry is to get the vendor name, device name,
720 * and revision so scsi_print_inquiry() can print them.
722 * If, at some point in the future, more inquiry data is needed for
723 * some reason, this code should use a procedure similar to the
724 * probe code. i.e., issue a short inquiry, and determine from
725 * the additional length passed back from the device how much
726 * inquiry data the device supports. Once the amount the device
727 * supports is determined, issue an inquiry for that amount and no
732 scsi_inquiry(&ccb->csio,
733 /* retries */ retry_count,
735 /* tag_action */ MSG_SIMPLE_Q_TAG,
736 /* inq_buf */ (u_int8_t *)inq_buf,
737 /* inq_len */ SHORT_INQUIRY_LENGTH,
740 /* sense_len */ SSD_FULL_SIZE,
741 /* timeout */ timeout ? timeout : 5000);
743 /* Disable freezing the device queue */
744 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
746 if (arglist & CAM_ARG_ERR_RECOVER)
747 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
749 if (cam_send_ccb(device, ccb) < 0) {
750 perror("error sending SCSI inquiry");
752 if (arglist & CAM_ARG_VERBOSE) {
753 cam_error_print(device, ccb, CAM_ESF_ALL,
754 CAM_EPF_ALL, stderr);
761 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
764 if (arglist & CAM_ARG_VERBOSE) {
765 cam_error_print(device, ccb, CAM_ESF_ALL,
766 CAM_EPF_ALL, stderr);
777 fprintf(stdout, "%s%d: ", device->device_name,
778 device->dev_unit_num);
779 scsi_print_inquiry(inq_buf);
787 scsiserial(struct cam_device *device, int retry_count, int timeout)
790 struct scsi_vpd_unit_serial_number *serial_buf;
791 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
794 ccb = cam_getccb(device);
797 warnx("couldn't allocate CCB");
801 /* cam_getccb cleans up the header, caller has to zero the payload */
802 bzero(&(&ccb->ccb_h)[1],
803 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
805 serial_buf = (struct scsi_vpd_unit_serial_number *)
806 malloc(sizeof(*serial_buf));
808 if (serial_buf == NULL) {
810 warnx("can't malloc memory for serial number");
814 scsi_inquiry(&ccb->csio,
815 /*retries*/ retry_count,
817 /* tag_action */ MSG_SIMPLE_Q_TAG,
818 /* inq_buf */ (u_int8_t *)serial_buf,
819 /* inq_len */ sizeof(*serial_buf),
821 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
822 /* sense_len */ SSD_FULL_SIZE,
823 /* timeout */ timeout ? timeout : 5000);
825 /* Disable freezing the device queue */
826 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
828 if (arglist & CAM_ARG_ERR_RECOVER)
829 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
831 if (cam_send_ccb(device, ccb) < 0) {
832 warn("error getting serial number");
834 if (arglist & CAM_ARG_VERBOSE) {
835 cam_error_print(device, ccb, CAM_ESF_ALL,
836 CAM_EPF_ALL, stderr);
844 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
847 if (arglist & CAM_ARG_VERBOSE) {
848 cam_error_print(device, ccb, CAM_ESF_ALL,
849 CAM_EPF_ALL, stderr);
860 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
861 serial_num[serial_buf->length] = '\0';
863 if ((arglist & CAM_ARG_GET_STDINQ)
864 || (arglist & CAM_ARG_GET_XFERRATE))
865 fprintf(stdout, "%s%d: Serial Number ",
866 device->device_name, device->dev_unit_num);
868 fprintf(stdout, "%.60s\n", serial_num);
876 scsixferrate(struct cam_device *device)
884 ccb = cam_getccb(device);
887 warnx("couldn't allocate CCB");
891 bzero(&(&ccb->ccb_h)[1],
892 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
894 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
895 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
897 if (((retval = cam_send_ccb(device, ccb)) < 0)
898 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
899 const char error_string[] = "error getting transfer settings";
906 if (arglist & CAM_ARG_VERBOSE)
907 cam_error_print(device, ccb, CAM_ESF_ALL,
908 CAM_EPF_ALL, stderr);
912 goto xferrate_bailout;
916 if (ccb->cts.transport == XPORT_SPI) {
917 struct ccb_trans_settings_spi *spi =
918 &ccb->cts.xport_specific.spi;
920 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
921 freq = scsi_calc_syncsrate(spi->sync_period);
924 struct ccb_pathinq cpi;
926 retval = get_cpi(device, &cpi);
928 speed = cpi.base_transfer_speed;
933 fprintf(stdout, "%s%d: ", device->device_name,
934 device->dev_unit_num);
936 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
937 speed *= (0x01 << spi->bus_width);
943 fprintf(stdout, "%d.%03dMB/s transfers ",
946 fprintf(stdout, "%dKB/s transfers ",
949 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
950 && (spi->sync_offset != 0))
951 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000,
952 freq % 1000, spi->sync_offset);
954 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
955 && (spi->bus_width > 0)) {
956 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
957 && (spi->sync_offset != 0)) {
958 fprintf(stdout, ", ");
960 fprintf(stdout, " (");
962 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
963 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
964 && (spi->sync_offset != 0)) {
965 fprintf(stdout, ")");
968 struct ccb_pathinq cpi;
970 retval = get_cpi(device, &cpi);
973 goto xferrate_bailout;
975 speed = cpi.base_transfer_speed;
981 fprintf(stdout, "%d.%03dMB/s transfers ",
984 fprintf(stdout, "%dKB/s transfers ",
988 if (ccb->cts.protocol == PROTO_SCSI) {
989 struct ccb_trans_settings_scsi *scsi =
990 &ccb->cts.proto_specific.scsi;
991 if (scsi->valid & CTS_SCSI_VALID_TQ) {
992 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
993 fprintf(stdout, ", Command Queueing Enabled");
998 fprintf(stdout, "\n");
1008 atacapprint(struct ata_params *parm)
1010 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1011 ((u_int32_t)parm->lba_size_2 << 16);
1013 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1014 ((u_int64_t)parm->lba_size48_2 << 16) |
1015 ((u_int64_t)parm->lba_size48_3 << 32) |
1016 ((u_int64_t)parm->lba_size48_4 << 48);
1019 printf("protocol ");
1020 printf("ATA/ATAPI-%d", ata_version(parm->version_major));
1021 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
1022 if (parm->satacapabilities & ATA_SATA_GEN2)
1023 printf(" SATA 2.x\n");
1024 else if (parm->satacapabilities & ATA_SATA_GEN1)
1025 printf(" SATA 1.x\n");
1027 printf(" SATA x.x\n");
1031 printf("device model %.40s\n", parm->model);
1032 printf("serial number %.20s\n", parm->serial);
1033 printf("firmware revision %.8s\n", parm->revision);
1035 printf("cylinders %d\n", parm->cylinders);
1036 printf("heads %d\n", parm->heads);
1037 printf("sectors/track %d\n", parm->sectors);
1039 if (parm->config == ATA_PROTO_CFA ||
1040 (parm->support.command2 & ATA_SUPPORT_CFA))
1041 printf("CFA supported\n");
1043 printf("LBA%ssupported ",
1044 parm->capabilities1 & ATA_SUPPORT_LBA ? " " : " not ");
1046 printf("%d sectors\n", lbasize);
1050 printf("LBA48%ssupported ",
1051 parm->support.command2 & ATA_SUPPORT_ADDRESS48 ? " " : " not ");
1053 printf("%ju sectors\n", (uintmax_t)lbasize48);
1057 printf("PIO supported PIO");
1058 if (parm->atavalid & ATA_FLAG_64_70) {
1059 if (parm->apiomodes & 0x02)
1061 else if (parm->apiomodes & 0x01)
1063 } else if (parm->mwdmamodes & 0x04)
1065 else if (parm->mwdmamodes & 0x02)
1067 else if (parm->mwdmamodes & 0x01)
1069 else if ((parm->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1071 else if ((parm->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1077 printf("DMA%ssupported ",
1078 parm->capabilities1 & ATA_SUPPORT_DMA ? " " : " not ");
1079 if (parm->capabilities1 & ATA_SUPPORT_DMA) {
1080 if (parm->mwdmamodes & 0xff) {
1082 if (parm->mwdmamodes & 0x04)
1084 else if (parm->mwdmamodes & 0x02)
1086 else if (parm->mwdmamodes & 0x01)
1090 if ((parm->atavalid & ATA_FLAG_88) &&
1091 (parm->udmamodes & 0xff)) {
1093 if (parm->udmamodes & 0x40)
1095 else if (parm->udmamodes & 0x20)
1097 else if (parm->udmamodes & 0x10)
1099 else if (parm->udmamodes & 0x08)
1101 else if (parm->udmamodes & 0x04)
1103 else if (parm->udmamodes & 0x02)
1105 else if (parm->udmamodes & 0x01)
1112 printf("overlap%ssupported\n",
1113 parm->capabilities1 & ATA_SUPPORT_OVERLAP ? " " : " not ");
1116 "Support Enable Value Vendor\n");
1118 printf("write cache %s %s\n",
1119 parm->support.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no",
1120 parm->enabled.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no");
1122 printf("read ahead %s %s\n",
1123 parm->support.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no",
1124 parm->enabled.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no");
1126 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
1127 printf("Native Command Queuing (NCQ) %s "
1129 parm->satacapabilities & ATA_SUPPORT_NCQ ?
1131 (parm->satacapabilities & ATA_SUPPORT_NCQ) ?
1132 ATA_QUEUE_LEN(parm->queue) : 0,
1133 (parm->satacapabilities & ATA_SUPPORT_NCQ) ?
1134 ATA_QUEUE_LEN(parm->queue) : 0);
1136 printf("Tagged Command Queuing (TCQ) %s %s %d/0x%02X\n",
1137 parm->support.command2 & ATA_SUPPORT_QUEUED ? "yes" : "no",
1138 parm->enabled.command2 & ATA_SUPPORT_QUEUED ? "yes" : "no",
1139 ATA_QUEUE_LEN(parm->queue), ATA_QUEUE_LEN(parm->queue));
1141 printf("SMART %s %s\n",
1142 parm->support.command1 & ATA_SUPPORT_SMART ? "yes" : "no",
1143 parm->enabled.command1 & ATA_SUPPORT_SMART ? "yes" : "no");
1145 printf("microcode download %s %s\n",
1146 parm->support.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no",
1147 parm->enabled.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no");
1149 printf("security %s %s\n",
1150 parm->support.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no",
1151 parm->enabled.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no");
1153 printf("power management %s %s\n",
1154 parm->support.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no",
1155 parm->enabled.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no");
1157 printf("advanced power management %s %s %d/0x%02X\n",
1158 parm->support.command2 & ATA_SUPPORT_APM ? "yes" : "no",
1159 parm->enabled.command2 & ATA_SUPPORT_APM ? "yes" : "no",
1160 parm->apm_value, parm->apm_value);
1162 printf("automatic acoustic management %s %s "
1163 "%d/0x%02X %d/0x%02X\n",
1164 parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
1165 parm->enabled.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
1166 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1167 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1168 ATA_ACOUSTIC_VENDOR(parm->acoustic),
1169 ATA_ACOUSTIC_VENDOR(parm->acoustic));
1174 ataidentify(struct cam_device *device, int retry_count, int timeout)
1177 struct ata_params *ident_buf;
1178 struct ccb_getdev cgd;
1182 if (get_cgd(device, &cgd) != 0) {
1183 warnx("couldn't get CGD");
1186 ccb = cam_getccb(device);
1189 warnx("couldn't allocate CCB");
1193 /* cam_getccb cleans up the header, caller has to zero the payload */
1194 bzero(&(&ccb->ccb_h)[1],
1195 sizeof(struct ccb_ataio) - sizeof(struct ccb_hdr));
1197 ptr = (uint16_t *)malloc(sizeof(struct ata_params));
1201 warnx("can't malloc memory for identify\n");
1204 bzero(ptr, sizeof(struct ata_params));
1206 cam_fill_ataio(&ccb->ataio,
1209 /*flags*/CAM_DIR_IN,
1211 /*data_ptr*/(u_int8_t *)ptr,
1212 /*dxfer_len*/sizeof(struct ata_params),
1213 timeout ? timeout : 30 * 1000);
1214 if (cgd.protocol == PROTO_ATA)
1215 ata_28bit_cmd(&ccb->ataio, ATA_ATA_IDENTIFY, 0, 0, 0);
1217 ata_28bit_cmd(&ccb->ataio, ATA_ATAPI_IDENTIFY, 0, 0, 0);
1219 /* Disable freezing the device queue */
1220 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1222 if (arglist & CAM_ARG_ERR_RECOVER)
1223 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1225 if (cam_send_ccb(device, ccb) < 0) {
1226 perror("error sending ATA identify");
1228 if (arglist & CAM_ARG_VERBOSE) {
1229 cam_error_print(device, ccb, CAM_ESF_ALL,
1230 CAM_EPF_ALL, stderr);
1238 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1241 if (arglist & CAM_ARG_VERBOSE) {
1242 cam_error_print(device, ccb, CAM_ESF_ALL,
1243 CAM_EPF_ALL, stderr);
1254 for (i = 0; i < sizeof(struct ata_params) / 2; i++)
1255 ptr[i] = le16toh(ptr[i]);
1256 ident_buf = (struct ata_params *)ptr;
1258 if (strncmp(ident_buf->model, "FX", 2) &&
1259 strncmp(ident_buf->model, "NEC", 3) &&
1260 strncmp(ident_buf->model, "Pioneer", 7) &&
1261 strncmp(ident_buf->model, "SHARP", 5)) {
1262 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
1263 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
1264 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
1266 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
1267 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
1268 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
1269 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
1270 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
1271 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
1273 fprintf(stdout, "%s%d: ", device->device_name,
1274 device->dev_unit_num);
1275 ata_print_ident(ident_buf);
1276 atacapprint(ident_buf);
1282 #endif /* MINIMALISTIC */
1285 * Parse out a bus, or a bus, target and lun in the following
1291 * Returns the number of parsed components, or 0.
1294 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglst)
1299 while (isspace(*tstr) && (*tstr != '\0'))
1302 tmpstr = (char *)strtok(tstr, ":");
1303 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1304 *bus = strtol(tmpstr, NULL, 0);
1305 *arglst |= CAM_ARG_BUS;
1307 tmpstr = (char *)strtok(NULL, ":");
1308 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1309 *target = strtol(tmpstr, NULL, 0);
1310 *arglst |= CAM_ARG_TARGET;
1312 tmpstr = (char *)strtok(NULL, ":");
1313 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
1314 *lun = strtol(tmpstr, NULL, 0);
1315 *arglst |= CAM_ARG_LUN;
1325 dorescan_or_reset(int argc, char **argv, int rescan)
1327 static const char must[] =
1328 "you must specify \"all\", a bus, or a bus:target:lun to %s";
1330 int bus = -1, target = -1, lun = -1;
1334 warnx(must, rescan? "rescan" : "reset");
1338 tstr = argv[optind];
1339 while (isspace(*tstr) && (*tstr != '\0'))
1341 if (strncasecmp(tstr, "all", strlen("all")) == 0)
1342 arglist |= CAM_ARG_BUS;
1344 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
1345 if (rv != 1 && rv != 3) {
1346 warnx(must, rescan? "rescan" : "reset");
1351 if ((arglist & CAM_ARG_BUS)
1352 && (arglist & CAM_ARG_TARGET)
1353 && (arglist & CAM_ARG_LUN))
1354 error = scanlun_or_reset_dev(bus, target, lun, rescan);
1356 error = rescan_or_reset_bus(bus, rescan);
1362 rescan_or_reset_bus(int bus, int rescan)
1364 union ccb ccb, matchccb;
1370 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1371 warnx("error opening transport layer device %s", XPT_DEVICE);
1372 warn("%s", XPT_DEVICE);
1377 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
1378 ccb.ccb_h.path_id = bus;
1379 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1380 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1381 ccb.crcn.flags = CAM_FLAG_NONE;
1383 /* run this at a low priority */
1384 ccb.ccb_h.pinfo.priority = 5;
1386 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1387 warn("CAMIOCOMMAND ioctl failed");
1392 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1393 fprintf(stdout, "%s of bus %d was successful\n",
1394 rescan ? "Re-scan" : "Reset", bus);
1396 fprintf(stdout, "%s of bus %d returned error %#x\n",
1397 rescan ? "Re-scan" : "Reset", bus,
1398 ccb.ccb_h.status & CAM_STATUS_MASK);
1409 * The right way to handle this is to modify the xpt so that it can
1410 * handle a wildcarded bus in a rescan or reset CCB. At the moment
1411 * that isn't implemented, so instead we enumerate the busses and
1412 * send the rescan or reset to those busses in the case where the
1413 * given bus is -1 (wildcard). We don't send a rescan or reset
1414 * to the xpt bus; sending a rescan to the xpt bus is effectively a
1415 * no-op, sending a rescan to the xpt bus would result in a status of
1418 bzero(&(&matchccb.ccb_h)[1],
1419 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr));
1420 matchccb.ccb_h.func_code = XPT_DEV_MATCH;
1421 bufsize = sizeof(struct dev_match_result) * 20;
1422 matchccb.cdm.match_buf_len = bufsize;
1423 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize);
1424 if (matchccb.cdm.matches == NULL) {
1425 warnx("can't malloc memory for matches");
1429 matchccb.cdm.num_matches = 0;
1431 matchccb.cdm.num_patterns = 1;
1432 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
1434 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc(
1435 matchccb.cdm.pattern_buf_len);
1436 if (matchccb.cdm.patterns == NULL) {
1437 warnx("can't malloc memory for patterns");
1441 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS;
1442 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
1447 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) {
1448 warn("CAMIOCOMMAND ioctl failed");
1453 if ((matchccb.ccb_h.status != CAM_REQ_CMP)
1454 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST)
1455 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) {
1456 warnx("got CAM error %#x, CDM error %d\n",
1457 matchccb.ccb_h.status, matchccb.cdm.status);
1462 for (i = 0; i < matchccb.cdm.num_matches; i++) {
1463 struct bus_match_result *bus_result;
1465 /* This shouldn't happen. */
1466 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS)
1469 bus_result = &matchccb.cdm.matches[i].result.bus_result;
1472 * We don't want to rescan or reset the xpt bus.
1475 if ((int)bus_result->path_id == -1)
1478 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS :
1480 ccb.ccb_h.path_id = bus_result->path_id;
1481 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
1482 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
1483 ccb.crcn.flags = CAM_FLAG_NONE;
1485 /* run this at a low priority */
1486 ccb.ccb_h.pinfo.priority = 5;
1488 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
1489 warn("CAMIOCOMMAND ioctl failed");
1494 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){
1495 fprintf(stdout, "%s of bus %d was successful\n",
1496 rescan? "Re-scan" : "Reset",
1497 bus_result->path_id);
1500 * Don't bail out just yet, maybe the other
1501 * rescan or reset commands will complete
1504 fprintf(stderr, "%s of bus %d returned error "
1505 "%#x\n", rescan? "Re-scan" : "Reset",
1506 bus_result->path_id,
1507 ccb.ccb_h.status & CAM_STATUS_MASK);
1511 } while ((matchccb.ccb_h.status == CAM_REQ_CMP)
1512 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE));
1519 if (matchccb.cdm.patterns != NULL)
1520 free(matchccb.cdm.patterns);
1521 if (matchccb.cdm.matches != NULL)
1522 free(matchccb.cdm.matches);
1528 scanlun_or_reset_dev(int bus, int target, int lun, int scan)
1531 struct cam_device *device;
1537 warnx("invalid bus number %d", bus);
1542 warnx("invalid target number %d", target);
1547 warnx("invalid lun number %d", lun);
1553 bzero(&ccb, sizeof(union ccb));
1556 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
1557 warnx("error opening transport layer device %s\n",
1559 warn("%s", XPT_DEVICE);
1563 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
1564 if (device == NULL) {
1565 warnx("%s", cam_errbuf);
1570 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
1571 ccb.ccb_h.path_id = bus;
1572 ccb.ccb_h.target_id = target;
1573 ccb.ccb_h.target_lun = lun;
1574 ccb.ccb_h.timeout = 5000;
1575 ccb.crcn.flags = CAM_FLAG_NONE;
1577 /* run this at a low priority */
1578 ccb.ccb_h.pinfo.priority = 5;
1581 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
1582 warn("CAMIOCOMMAND ioctl failed");
1587 if (cam_send_ccb(device, &ccb) < 0) {
1588 warn("error sending XPT_RESET_DEV CCB");
1589 cam_close_device(device);
1597 cam_close_device(device);
1600 * An error code of CAM_BDR_SENT is normal for a BDR request.
1602 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1604 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
1605 fprintf(stdout, "%s of %d:%d:%d was successful\n",
1606 scan? "Re-scan" : "Reset", bus, target, lun);
1609 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n",
1610 scan? "Re-scan" : "Reset", bus, target, lun,
1611 ccb.ccb_h.status & CAM_STATUS_MASK);
1616 #ifndef MINIMALISTIC
1618 readdefects(struct cam_device *device, int argc, char **argv,
1619 char *combinedopt, int retry_count, int timeout)
1621 union ccb *ccb = NULL;
1622 struct scsi_read_defect_data_10 *rdd_cdb;
1623 u_int8_t *defect_list = NULL;
1624 u_int32_t dlist_length = 65000;
1625 u_int32_t returned_length = 0;
1626 u_int32_t num_returned = 0;
1627 u_int8_t returned_format;
1630 int lists_specified = 0;
1632 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1638 while (isspace(*tstr) && (*tstr != '\0'))
1640 if (strcmp(tstr, "block") == 0)
1641 arglist |= CAM_ARG_FORMAT_BLOCK;
1642 else if (strcmp(tstr, "bfi") == 0)
1643 arglist |= CAM_ARG_FORMAT_BFI;
1644 else if (strcmp(tstr, "phys") == 0)
1645 arglist |= CAM_ARG_FORMAT_PHYS;
1648 warnx("invalid defect format %s", tstr);
1649 goto defect_bailout;
1654 arglist |= CAM_ARG_GLIST;
1657 arglist |= CAM_ARG_PLIST;
1664 ccb = cam_getccb(device);
1667 * Hopefully 65000 bytes is enough to hold the defect list. If it
1668 * isn't, the disk is probably dead already. We'd have to go with
1669 * 12 byte command (i.e. alloc_length is 32 bits instead of 16)
1672 defect_list = malloc(dlist_length);
1673 if (defect_list == NULL) {
1674 warnx("can't malloc memory for defect list");
1676 goto defect_bailout;
1679 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes;
1682 * cam_getccb() zeros the CCB header only. So we need to zero the
1683 * payload portion of the ccb.
1685 bzero(&(&ccb->ccb_h)[1],
1686 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1688 cam_fill_csio(&ccb->csio,
1689 /*retries*/ retry_count,
1691 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ?
1692 CAM_PASS_ERR_RECOVER : 0),
1693 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1694 /*data_ptr*/ defect_list,
1695 /*dxfer_len*/ dlist_length,
1696 /*sense_len*/ SSD_FULL_SIZE,
1697 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10),
1698 /*timeout*/ timeout ? timeout : 5000);
1700 rdd_cdb->opcode = READ_DEFECT_DATA_10;
1701 if (arglist & CAM_ARG_FORMAT_BLOCK)
1702 rdd_cdb->format = SRDD10_BLOCK_FORMAT;
1703 else if (arglist & CAM_ARG_FORMAT_BFI)
1704 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT;
1705 else if (arglist & CAM_ARG_FORMAT_PHYS)
1706 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT;
1709 warnx("no defect list format specified");
1710 goto defect_bailout;
1712 if (arglist & CAM_ARG_PLIST) {
1713 rdd_cdb->format |= SRDD10_PLIST;
1717 if (arglist & CAM_ARG_GLIST) {
1718 rdd_cdb->format |= SRDD10_GLIST;
1722 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length);
1724 /* Disable freezing the device queue */
1725 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1727 if (cam_send_ccb(device, ccb) < 0) {
1728 perror("error reading defect list");
1730 if (arglist & CAM_ARG_VERBOSE) {
1731 cam_error_print(device, ccb, CAM_ESF_ALL,
1732 CAM_EPF_ALL, stderr);
1736 goto defect_bailout;
1739 returned_length = scsi_2btoul(((struct
1740 scsi_read_defect_data_hdr_10 *)defect_list)->length);
1742 returned_format = ((struct scsi_read_defect_data_hdr_10 *)
1743 defect_list)->format;
1745 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
1746 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1747 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
1748 struct scsi_sense_data *sense;
1749 int error_code, sense_key, asc, ascq;
1751 sense = &ccb->csio.sense_data;
1752 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
1755 * According to the SCSI spec, if the disk doesn't support
1756 * the requested format, it will generally return a sense
1757 * key of RECOVERED ERROR, and an additional sense code
1758 * of "DEFECT LIST NOT FOUND". So, we check for that, and
1759 * also check to make sure that the returned length is
1760 * greater than 0, and then print out whatever format the
1763 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
1764 && (asc == 0x1c) && (ascq == 0x00)
1765 && (returned_length > 0)) {
1766 warnx("requested defect format not available");
1767 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) {
1768 case SRDD10_BLOCK_FORMAT:
1769 warnx("Device returned block format");
1771 case SRDD10_BYTES_FROM_INDEX_FORMAT:
1772 warnx("Device returned bytes from index"
1775 case SRDD10_PHYSICAL_SECTOR_FORMAT:
1776 warnx("Device returned physical sector format");
1780 warnx("Device returned unknown defect"
1781 " data format %#x", returned_format);
1782 goto defect_bailout;
1783 break; /* NOTREACHED */
1787 warnx("Error returned from read defect data command");
1788 if (arglist & CAM_ARG_VERBOSE)
1789 cam_error_print(device, ccb, CAM_ESF_ALL,
1790 CAM_EPF_ALL, stderr);
1791 goto defect_bailout;
1793 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1795 warnx("Error returned from read defect data command");
1796 if (arglist & CAM_ARG_VERBOSE)
1797 cam_error_print(device, ccb, CAM_ESF_ALL,
1798 CAM_EPF_ALL, stderr);
1799 goto defect_bailout;
1803 * XXX KDM I should probably clean up the printout format for the
1806 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){
1807 case SRDDH10_PHYSICAL_SECTOR_FORMAT:
1809 struct scsi_defect_desc_phys_sector *dlist;
1811 dlist = (struct scsi_defect_desc_phys_sector *)
1813 sizeof(struct scsi_read_defect_data_hdr_10));
1815 num_returned = returned_length /
1816 sizeof(struct scsi_defect_desc_phys_sector);
1818 fprintf(stderr, "Got %d defect", num_returned);
1820 if ((lists_specified == 0) || (num_returned == 0)) {
1821 fprintf(stderr, "s.\n");
1823 } else if (num_returned == 1)
1824 fprintf(stderr, ":\n");
1826 fprintf(stderr, "s:\n");
1828 for (i = 0; i < num_returned; i++) {
1829 fprintf(stdout, "%d:%d:%d\n",
1830 scsi_3btoul(dlist[i].cylinder),
1832 scsi_4btoul(dlist[i].sector));
1836 case SRDDH10_BYTES_FROM_INDEX_FORMAT:
1838 struct scsi_defect_desc_bytes_from_index *dlist;
1840 dlist = (struct scsi_defect_desc_bytes_from_index *)
1842 sizeof(struct scsi_read_defect_data_hdr_10));
1844 num_returned = returned_length /
1845 sizeof(struct scsi_defect_desc_bytes_from_index);
1847 fprintf(stderr, "Got %d defect", num_returned);
1849 if ((lists_specified == 0) || (num_returned == 0)) {
1850 fprintf(stderr, "s.\n");
1852 } else if (num_returned == 1)
1853 fprintf(stderr, ":\n");
1855 fprintf(stderr, "s:\n");
1857 for (i = 0; i < num_returned; i++) {
1858 fprintf(stdout, "%d:%d:%d\n",
1859 scsi_3btoul(dlist[i].cylinder),
1861 scsi_4btoul(dlist[i].bytes_from_index));
1865 case SRDDH10_BLOCK_FORMAT:
1867 struct scsi_defect_desc_block *dlist;
1869 dlist = (struct scsi_defect_desc_block *)(defect_list +
1870 sizeof(struct scsi_read_defect_data_hdr_10));
1872 num_returned = returned_length /
1873 sizeof(struct scsi_defect_desc_block);
1875 fprintf(stderr, "Got %d defect", num_returned);
1877 if ((lists_specified == 0) || (num_returned == 0)) {
1878 fprintf(stderr, "s.\n");
1880 } else if (num_returned == 1)
1881 fprintf(stderr, ":\n");
1883 fprintf(stderr, "s:\n");
1885 for (i = 0; i < num_returned; i++)
1886 fprintf(stdout, "%u\n",
1887 scsi_4btoul(dlist[i].address));
1891 fprintf(stderr, "Unknown defect format %d\n",
1892 returned_format & SRDDH10_DLIST_FORMAT_MASK);
1898 if (defect_list != NULL)
1906 #endif /* MINIMALISTIC */
1910 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
1914 ccb = cam_getccb(device);
1920 #ifndef MINIMALISTIC
1922 mode_sense(struct cam_device *device, int mode_page, int page_control,
1923 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen)
1928 ccb = cam_getccb(device);
1931 errx(1, "mode_sense: couldn't allocate CCB");
1933 bzero(&(&ccb->ccb_h)[1],
1934 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1936 scsi_mode_sense(&ccb->csio,
1937 /* retries */ retry_count,
1939 /* tag_action */ MSG_SIMPLE_Q_TAG,
1941 /* page_code */ page_control << 6,
1942 /* page */ mode_page,
1943 /* param_buf */ data,
1944 /* param_len */ datalen,
1945 /* sense_len */ SSD_FULL_SIZE,
1946 /* timeout */ timeout ? timeout : 5000);
1948 if (arglist & CAM_ARG_ERR_RECOVER)
1949 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1951 /* Disable freezing the device queue */
1952 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1954 if (((retval = cam_send_ccb(device, ccb)) < 0)
1955 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1956 if (arglist & CAM_ARG_VERBOSE) {
1957 cam_error_print(device, ccb, CAM_ESF_ALL,
1958 CAM_EPF_ALL, stderr);
1961 cam_close_device(device);
1963 err(1, "error sending mode sense command");
1965 errx(1, "error sending mode sense command");
1972 mode_select(struct cam_device *device, int save_pages, int retry_count,
1973 int timeout, u_int8_t *data, int datalen)
1978 ccb = cam_getccb(device);
1981 errx(1, "mode_select: couldn't allocate CCB");
1983 bzero(&(&ccb->ccb_h)[1],
1984 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
1986 scsi_mode_select(&ccb->csio,
1987 /* retries */ retry_count,
1989 /* tag_action */ MSG_SIMPLE_Q_TAG,
1990 /* scsi_page_fmt */ 1,
1991 /* save_pages */ save_pages,
1992 /* param_buf */ data,
1993 /* param_len */ datalen,
1994 /* sense_len */ SSD_FULL_SIZE,
1995 /* timeout */ timeout ? timeout : 5000);
1997 if (arglist & CAM_ARG_ERR_RECOVER)
1998 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
2000 /* Disable freezing the device queue */
2001 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
2003 if (((retval = cam_send_ccb(device, ccb)) < 0)
2004 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
2005 if (arglist & CAM_ARG_VERBOSE) {
2006 cam_error_print(device, ccb, CAM_ESF_ALL,
2007 CAM_EPF_ALL, stderr);
2010 cam_close_device(device);
2013 err(1, "error sending mode select command");
2015 errx(1, "error sending mode select command");
2023 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
2024 int retry_count, int timeout)
2026 int c, mode_page = -1, page_control = 0;
2027 int binary = 0, list = 0;
2029 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2035 arglist |= CAM_ARG_DBD;
2038 arglist |= CAM_ARG_MODE_EDIT;
2044 mode_page = strtol(optarg, NULL, 0);
2046 errx(1, "invalid mode page %d", mode_page);
2049 page_control = strtol(optarg, NULL, 0);
2050 if ((page_control < 0) || (page_control > 3))
2051 errx(1, "invalid page control field %d",
2053 arglist |= CAM_ARG_PAGE_CNTL;
2060 if (mode_page == -1 && list == 0)
2061 errx(1, "you must specify a mode page!");
2064 mode_list(device, page_control, arglist & CAM_ARG_DBD,
2065 retry_count, timeout);
2067 mode_edit(device, mode_page, page_control,
2068 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary,
2069 retry_count, timeout);
2074 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
2075 int retry_count, int timeout)
2078 u_int32_t flags = CAM_DIR_NONE;
2079 u_int8_t *data_ptr = NULL;
2081 u_int8_t atacmd[12];
2082 struct get_hook hook;
2083 int c, data_bytes = 0;
2087 char *datastr = NULL, *tstr, *resstr = NULL;
2089 int fd_data = 0, fd_res = 0;
2092 ccb = cam_getccb(device);
2095 warnx("scsicmd: error allocating ccb");
2099 bzero(&(&ccb->ccb_h)[1],
2100 sizeof(union ccb) - sizeof(struct ccb_hdr));
2102 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2106 while (isspace(*tstr) && (*tstr != '\0'))
2108 hook.argc = argc - optind;
2109 hook.argv = argv + optind;
2111 atacmd_len = buff_encode_visit(atacmd, sizeof(atacmd), tstr,
2114 * Increment optind by the number of arguments the
2115 * encoding routine processed. After each call to
2116 * getopt(3), optind points to the argument that
2117 * getopt should process _next_. In this case,
2118 * that means it points to the first command string
2119 * argument, if there is one. Once we increment
2120 * this, it should point to either the next command
2121 * line argument, or it should be past the end of
2128 while (isspace(*tstr) && (*tstr != '\0'))
2130 hook.argc = argc - optind;
2131 hook.argv = argv + optind;
2133 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
2136 * Increment optind by the number of arguments the
2137 * encoding routine processed. After each call to
2138 * getopt(3), optind points to the argument that
2139 * getopt should process _next_. In this case,
2140 * that means it points to the first command string
2141 * argument, if there is one. Once we increment
2142 * this, it should point to either the next command
2143 * line argument, or it should be past the end of
2149 if (arglist & CAM_ARG_CMD_OUT) {
2150 warnx("command must either be "
2151 "read or write, not both");
2153 goto scsicmd_bailout;
2155 arglist |= CAM_ARG_CMD_IN;
2157 data_bytes = strtol(optarg, NULL, 0);
2158 if (data_bytes <= 0) {
2159 warnx("invalid number of input bytes %d",
2162 goto scsicmd_bailout;
2164 hook.argc = argc - optind;
2165 hook.argv = argv + optind;
2168 datastr = cget(&hook, NULL);
2170 * If the user supplied "-" instead of a format, he
2171 * wants the data to be written to stdout.
2173 if ((datastr != NULL)
2174 && (datastr[0] == '-'))
2177 data_ptr = (u_int8_t *)malloc(data_bytes);
2178 if (data_ptr == NULL) {
2179 warnx("can't malloc memory for data_ptr");
2181 goto scsicmd_bailout;
2185 if (arglist & CAM_ARG_CMD_IN) {
2186 warnx("command must either be "
2187 "read or write, not both");
2189 goto scsicmd_bailout;
2191 arglist |= CAM_ARG_CMD_OUT;
2192 flags = CAM_DIR_OUT;
2193 data_bytes = strtol(optarg, NULL, 0);
2194 if (data_bytes <= 0) {
2195 warnx("invalid number of output bytes %d",
2198 goto scsicmd_bailout;
2200 hook.argc = argc - optind;
2201 hook.argv = argv + optind;
2203 datastr = cget(&hook, NULL);
2204 data_ptr = (u_int8_t *)malloc(data_bytes);
2205 if (data_ptr == NULL) {
2206 warnx("can't malloc memory for data_ptr");
2208 goto scsicmd_bailout;
2211 * If the user supplied "-" instead of a format, he
2212 * wants the data to be read from stdin.
2214 if ((datastr != NULL)
2215 && (datastr[0] == '-'))
2218 buff_encode_visit(data_ptr, data_bytes, datastr,
2224 hook.argc = argc - optind;
2225 hook.argv = argv + optind;
2227 resstr = cget(&hook, NULL);
2228 if ((resstr != NULL) && (resstr[0] == '-'))
2238 * If fd_data is set, and we're writing to the device, we need to
2239 * read the data the user wants written from stdin.
2241 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
2243 int amt_to_read = data_bytes;
2244 u_int8_t *buf_ptr = data_ptr;
2246 for (amt_read = 0; amt_to_read > 0;
2247 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
2248 if (amt_read == -1) {
2249 warn("error reading data from stdin");
2251 goto scsicmd_bailout;
2253 amt_to_read -= amt_read;
2254 buf_ptr += amt_read;
2258 if (arglist & CAM_ARG_ERR_RECOVER)
2259 flags |= CAM_PASS_ERR_RECOVER;
2261 /* Disable freezing the device queue */
2262 flags |= CAM_DEV_QFRZDIS;
2266 * This is taken from the SCSI-3 draft spec.
2267 * (T10/1157D revision 0.3)
2268 * The top 3 bits of an opcode are the group code.
2269 * The next 5 bits are the command code.
2270 * Group 0: six byte commands
2271 * Group 1: ten byte commands
2272 * Group 2: ten byte commands
2274 * Group 4: sixteen byte commands
2275 * Group 5: twelve byte commands
2276 * Group 6: vendor specific
2277 * Group 7: vendor specific
2279 switch((cdb[0] >> 5) & 0x7) {
2290 /* computed by buff_encode_visit */
2301 * We should probably use csio_build_visit or something like that
2302 * here, but it's easier to encode arguments as you go. The
2303 * alternative would be skipping the CDB argument and then encoding
2304 * it here, since we've got the data buffer argument by now.
2306 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
2308 cam_fill_csio(&ccb->csio,
2309 /*retries*/ retry_count,
2312 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2313 /*data_ptr*/ data_ptr,
2314 /*dxfer_len*/ data_bytes,
2315 /*sense_len*/ SSD_FULL_SIZE,
2316 /*cdb_len*/ cdb_len,
2317 /*timeout*/ timeout ? timeout : 5000);
2320 bcopy(atacmd, &ccb->ataio.cmd.command, atacmd_len);
2322 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
2324 cam_fill_ataio(&ccb->ataio,
2325 /*retries*/ retry_count,
2329 /*data_ptr*/ data_ptr,
2330 /*dxfer_len*/ data_bytes,
2331 /*timeout*/ timeout ? timeout : 5000);
2334 if (((retval = cam_send_ccb(device, ccb)) < 0)
2335 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
2337 warn("error sending command");
2339 warnx("error sending command");
2341 if (arglist & CAM_ARG_VERBOSE) {
2342 cam_error_print(device, ccb, CAM_ESF_ALL,
2343 CAM_EPF_ALL, stderr);
2347 goto scsicmd_bailout;
2350 if (atacmd_len && need_res) {
2352 buff_decode_visit(&ccb->ataio.res.status, 11, resstr,
2354 fprintf(stdout, "\n");
2357 "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
2358 ccb->ataio.res.status,
2359 ccb->ataio.res.error,
2360 ccb->ataio.res.lba_low,
2361 ccb->ataio.res.lba_mid,
2362 ccb->ataio.res.lba_high,
2363 ccb->ataio.res.device,
2364 ccb->ataio.res.lba_low_exp,
2365 ccb->ataio.res.lba_mid_exp,
2366 ccb->ataio.res.lba_high_exp,
2367 ccb->ataio.res.sector_count,
2368 ccb->ataio.res.sector_count_exp);
2373 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
2374 && (arglist & CAM_ARG_CMD_IN)
2375 && (data_bytes > 0)) {
2377 buff_decode_visit(data_ptr, data_bytes, datastr,
2379 fprintf(stdout, "\n");
2381 ssize_t amt_written;
2382 int amt_to_write = data_bytes;
2383 u_int8_t *buf_ptr = data_ptr;
2385 for (amt_written = 0; (amt_to_write > 0) &&
2386 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
2387 amt_to_write -= amt_written;
2388 buf_ptr += amt_written;
2390 if (amt_written == -1) {
2391 warn("error writing data to stdout");
2393 goto scsicmd_bailout;
2394 } else if ((amt_written == 0)
2395 && (amt_to_write > 0)) {
2396 warnx("only wrote %u bytes out of %u",
2397 data_bytes - amt_to_write, data_bytes);
2404 if ((data_bytes > 0) && (data_ptr != NULL))
2413 camdebug(int argc, char **argv, char *combinedopt)
2416 int bus = -1, target = -1, lun = -1;
2417 char *tstr, *tmpstr = NULL;
2421 bzero(&ccb, sizeof(union ccb));
2423 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2426 arglist |= CAM_ARG_DEBUG_INFO;
2427 ccb.cdbg.flags |= CAM_DEBUG_INFO;
2430 arglist |= CAM_ARG_DEBUG_PERIPH;
2431 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
2434 arglist |= CAM_ARG_DEBUG_SUBTRACE;
2435 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
2438 arglist |= CAM_ARG_DEBUG_TRACE;
2439 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
2442 arglist |= CAM_ARG_DEBUG_XPT;
2443 ccb.cdbg.flags |= CAM_DEBUG_XPT;
2446 arglist |= CAM_ARG_DEBUG_CDB;
2447 ccb.cdbg.flags |= CAM_DEBUG_CDB;
2454 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
2455 warnx("error opening transport layer device %s", XPT_DEVICE);
2456 warn("%s", XPT_DEVICE);
2463 warnx("you must specify \"off\", \"all\" or a bus,");
2464 warnx("bus:target, or bus:target:lun");
2471 while (isspace(*tstr) && (*tstr != '\0'))
2474 if (strncmp(tstr, "off", 3) == 0) {
2475 ccb.cdbg.flags = CAM_DEBUG_NONE;
2476 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
2477 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
2479 } else if (strncmp(tstr, "all", 3) != 0) {
2480 tmpstr = (char *)strtok(tstr, ":");
2481 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2482 bus = strtol(tmpstr, NULL, 0);
2483 arglist |= CAM_ARG_BUS;
2484 tmpstr = (char *)strtok(NULL, ":");
2485 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2486 target = strtol(tmpstr, NULL, 0);
2487 arglist |= CAM_ARG_TARGET;
2488 tmpstr = (char *)strtok(NULL, ":");
2489 if ((tmpstr != NULL) && (*tmpstr != '\0')){
2490 lun = strtol(tmpstr, NULL, 0);
2491 arglist |= CAM_ARG_LUN;
2496 warnx("you must specify \"all\", \"off\", or a bus,");
2497 warnx("bus:target, or bus:target:lun to debug");
2503 ccb.ccb_h.func_code = XPT_DEBUG;
2504 ccb.ccb_h.path_id = bus;
2505 ccb.ccb_h.target_id = target;
2506 ccb.ccb_h.target_lun = lun;
2508 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
2509 warn("CAMIOCOMMAND ioctl failed");
2514 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2515 CAM_FUNC_NOTAVAIL) {
2516 warnx("CAM debugging not available");
2517 warnx("you need to put options CAMDEBUG in"
2518 " your kernel config file!");
2520 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
2522 warnx("XPT_DEBUG CCB failed with status %#x",
2526 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
2528 "Debugging turned off\n");
2531 "Debugging enabled for "
2544 tagcontrol(struct cam_device *device, int argc, char **argv,
2554 ccb = cam_getccb(device);
2557 warnx("tagcontrol: error allocating ccb");
2561 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2564 numtags = strtol(optarg, NULL, 0);
2566 warnx("tag count %d is < 0", numtags);
2568 goto tagcontrol_bailout;
2579 cam_path_string(device, pathstr, sizeof(pathstr));
2582 bzero(&(&ccb->ccb_h)[1],
2583 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr));
2584 ccb->ccb_h.func_code = XPT_REL_SIMQ;
2585 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
2586 ccb->crs.openings = numtags;
2589 if (cam_send_ccb(device, ccb) < 0) {
2590 perror("error sending XPT_REL_SIMQ CCB");
2592 goto tagcontrol_bailout;
2595 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2596 warnx("XPT_REL_SIMQ CCB failed");
2597 cam_error_print(device, ccb, CAM_ESF_ALL,
2598 CAM_EPF_ALL, stderr);
2600 goto tagcontrol_bailout;
2605 fprintf(stdout, "%stagged openings now %d\n",
2606 pathstr, ccb->crs.openings);
2609 bzero(&(&ccb->ccb_h)[1],
2610 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr));
2612 ccb->ccb_h.func_code = XPT_GDEV_STATS;
2614 if (cam_send_ccb(device, ccb) < 0) {
2615 perror("error sending XPT_GDEV_STATS CCB");
2617 goto tagcontrol_bailout;
2620 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2621 warnx("XPT_GDEV_STATS CCB failed");
2622 cam_error_print(device, ccb, CAM_ESF_ALL,
2623 CAM_EPF_ALL, stderr);
2625 goto tagcontrol_bailout;
2628 if (arglist & CAM_ARG_VERBOSE) {
2629 fprintf(stdout, "%s", pathstr);
2630 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
2631 fprintf(stdout, "%s", pathstr);
2632 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
2633 fprintf(stdout, "%s", pathstr);
2634 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings);
2635 fprintf(stdout, "%s", pathstr);
2636 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued);
2637 fprintf(stdout, "%s", pathstr);
2638 fprintf(stdout, "held %d\n", ccb->cgds.held);
2639 fprintf(stdout, "%s", pathstr);
2640 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
2641 fprintf(stdout, "%s", pathstr);
2642 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
2645 fprintf(stdout, "%s", pathstr);
2646 fprintf(stdout, "device openings: ");
2648 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
2649 ccb->cgds.dev_active);
2659 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
2663 cam_path_string(device, pathstr, sizeof(pathstr));
2665 if (cts->transport == XPORT_SPI) {
2666 struct ccb_trans_settings_spi *spi =
2667 &cts->xport_specific.spi;
2669 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
2671 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
2674 if (spi->sync_offset != 0) {
2677 freq = scsi_calc_syncsrate(spi->sync_period);
2678 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
2679 pathstr, freq / 1000, freq % 1000);
2683 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
2684 fprintf(stdout, "%soffset: %d\n", pathstr,
2688 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
2689 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
2690 (0x01 << spi->bus_width) * 8);
2693 if (spi->valid & CTS_SPI_VALID_DISC) {
2694 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
2695 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
2696 "enabled" : "disabled");
2700 if (cts->protocol == PROTO_SCSI) {
2701 struct ccb_trans_settings_scsi *scsi=
2702 &cts->proto_specific.scsi;
2704 if (scsi->valid & CTS_SCSI_VALID_TQ) {
2705 fprintf(stdout, "%stagged queueing is %s\n", pathstr,
2706 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
2707 "enabled" : "disabled");
2714 * Get a path inquiry CCB for the specified device.
2717 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
2722 ccb = cam_getccb(device);
2724 warnx("get_cpi: couldn't allocate CCB");
2727 bzero(&(&ccb->ccb_h)[1],
2728 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2729 ccb->ccb_h.func_code = XPT_PATH_INQ;
2730 if (cam_send_ccb(device, ccb) < 0) {
2731 warn("get_cpi: error sending Path Inquiry CCB");
2732 if (arglist & CAM_ARG_VERBOSE)
2733 cam_error_print(device, ccb, CAM_ESF_ALL,
2734 CAM_EPF_ALL, stderr);
2736 goto get_cpi_bailout;
2738 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2739 if (arglist & CAM_ARG_VERBOSE)
2740 cam_error_print(device, ccb, CAM_ESF_ALL,
2741 CAM_EPF_ALL, stderr);
2743 goto get_cpi_bailout;
2745 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
2753 * Get a get device CCB for the specified device.
2756 get_cgd(struct cam_device *device, struct ccb_getdev *cgd)
2761 ccb = cam_getccb(device);
2763 warnx("get_cgd: couldn't allocate CCB");
2766 bzero(&(&ccb->ccb_h)[1],
2767 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
2768 ccb->ccb_h.func_code = XPT_GDEV_TYPE;
2769 if (cam_send_ccb(device, ccb) < 0) {
2770 warn("get_cgd: error sending Path Inquiry CCB");
2771 if (arglist & CAM_ARG_VERBOSE)
2772 cam_error_print(device, ccb, CAM_ESF_ALL,
2773 CAM_EPF_ALL, stderr);
2775 goto get_cgd_bailout;
2777 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2778 if (arglist & CAM_ARG_VERBOSE)
2779 cam_error_print(device, ccb, CAM_ESF_ALL,
2780 CAM_EPF_ALL, stderr);
2782 goto get_cgd_bailout;
2784 bcopy(&ccb->cgd, cgd, sizeof(struct ccb_getdev));
2792 cpi_print(struct ccb_pathinq *cpi)
2794 char adapter_str[1024];
2797 snprintf(adapter_str, sizeof(adapter_str),
2798 "%s%d:", cpi->dev_name, cpi->unit_number);
2800 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
2803 for (i = 1; i < 0xff; i = i << 1) {
2806 if ((i & cpi->hba_inquiry) == 0)
2809 fprintf(stdout, "%s supports ", adapter_str);
2813 str = "MDP message";
2816 str = "32 bit wide SCSI";
2819 str = "16 bit wide SCSI";
2822 str = "SDTR message";
2825 str = "linked CDBs";
2828 str = "tag queue messages";
2831 str = "soft reset alternative";
2834 str = "SATA Port Multiplier";
2837 str = "unknown PI bit set";
2840 fprintf(stdout, "%s\n", str);
2843 for (i = 1; i < 0xff; i = i << 1) {
2846 if ((i & cpi->hba_misc) == 0)
2849 fprintf(stdout, "%s ", adapter_str);
2853 str = "bus scans from high ID to low ID";
2856 str = "removable devices not included in scan";
2858 case PIM_NOINITIATOR:
2859 str = "initiator role not supported";
2861 case PIM_NOBUSRESET:
2862 str = "user has disabled initial BUS RESET or"
2863 " controller is in target/mixed mode";
2866 str = "do not send 6-byte commands";
2869 str = "scan bus sequentially";
2872 str = "unknown PIM bit set";
2875 fprintf(stdout, "%s\n", str);
2878 for (i = 1; i < 0xff; i = i << 1) {
2881 if ((i & cpi->target_sprt) == 0)
2884 fprintf(stdout, "%s supports ", adapter_str);
2887 str = "target mode processor mode";
2890 str = "target mode phase cog. mode";
2892 case PIT_DISCONNECT:
2893 str = "disconnects in target mode";
2896 str = "terminate I/O message in target mode";
2899 str = "group 6 commands in target mode";
2902 str = "group 7 commands in target mode";
2905 str = "unknown PIT bit set";
2909 fprintf(stdout, "%s\n", str);
2911 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
2913 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
2915 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
2917 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
2918 adapter_str, cpi->hpath_id);
2919 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
2921 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
2922 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
2923 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
2924 fprintf(stdout, "%s base transfer speed: ", adapter_str);
2925 if (cpi->base_transfer_speed > 1000)
2926 fprintf(stdout, "%d.%03dMB/sec\n",
2927 cpi->base_transfer_speed / 1000,
2928 cpi->base_transfer_speed % 1000);
2930 fprintf(stdout, "%dKB/sec\n",
2931 (cpi->base_transfer_speed % 1000) * 1000);
2935 get_print_cts(struct cam_device *device, int user_settings, int quiet,
2936 struct ccb_trans_settings *cts)
2942 ccb = cam_getccb(device);
2945 warnx("get_print_cts: error allocating ccb");
2949 bzero(&(&ccb->ccb_h)[1],
2950 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
2952 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2954 if (user_settings == 0)
2955 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
2957 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
2959 if (cam_send_ccb(device, ccb) < 0) {
2960 perror("error sending XPT_GET_TRAN_SETTINGS CCB");
2961 if (arglist & CAM_ARG_VERBOSE)
2962 cam_error_print(device, ccb, CAM_ESF_ALL,
2963 CAM_EPF_ALL, stderr);
2965 goto get_print_cts_bailout;
2968 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2969 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
2970 if (arglist & CAM_ARG_VERBOSE)
2971 cam_error_print(device, ccb, CAM_ESF_ALL,
2972 CAM_EPF_ALL, stderr);
2974 goto get_print_cts_bailout;
2978 cts_print(device, &ccb->cts);
2981 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
2983 get_print_cts_bailout:
2991 ratecontrol(struct cam_device *device, int retry_count, int timeout,
2992 int argc, char **argv, char *combinedopt)
2996 int user_settings = 0;
2998 int disc_enable = -1, tag_enable = -1;
3000 double syncrate = -1;
3003 int change_settings = 0, send_tur = 0;
3004 struct ccb_pathinq cpi;
3006 ccb = cam_getccb(device);
3009 warnx("ratecontrol: error allocating ccb");
3013 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3022 if (strncasecmp(optarg, "enable", 6) == 0)
3024 else if (strncasecmp(optarg, "disable", 7) == 0)
3027 warnx("-D argument \"%s\" is unknown", optarg);
3029 goto ratecontrol_bailout;
3031 change_settings = 1;
3034 offset = strtol(optarg, NULL, 0);
3036 warnx("offset value %d is < 0", offset);
3038 goto ratecontrol_bailout;
3040 change_settings = 1;
3046 syncrate = atof(optarg);
3049 warnx("sync rate %f is < 0", syncrate);
3051 goto ratecontrol_bailout;
3053 change_settings = 1;
3056 if (strncasecmp(optarg, "enable", 6) == 0)
3058 else if (strncasecmp(optarg, "disable", 7) == 0)
3061 warnx("-T argument \"%s\" is unknown", optarg);
3063 goto ratecontrol_bailout;
3065 change_settings = 1;
3071 bus_width = strtol(optarg, NULL, 0);
3072 if (bus_width < 0) {
3073 warnx("bus width %d is < 0", bus_width);
3075 goto ratecontrol_bailout;
3077 change_settings = 1;
3084 bzero(&(&ccb->ccb_h)[1],
3085 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr));
3088 * Grab path inquiry information, so we can determine whether
3089 * or not the initiator is capable of the things that the user
3092 ccb->ccb_h.func_code = XPT_PATH_INQ;
3094 if (cam_send_ccb(device, ccb) < 0) {
3095 perror("error sending XPT_PATH_INQ CCB");
3096 if (arglist & CAM_ARG_VERBOSE) {
3097 cam_error_print(device, ccb, CAM_ESF_ALL,
3098 CAM_EPF_ALL, stderr);
3101 goto ratecontrol_bailout;
3104 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3105 warnx("XPT_PATH_INQ CCB failed");
3106 if (arglist & CAM_ARG_VERBOSE) {
3107 cam_error_print(device, ccb, CAM_ESF_ALL,
3108 CAM_EPF_ALL, stderr);
3111 goto ratecontrol_bailout;
3114 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq));
3116 bzero(&(&ccb->ccb_h)[1],
3117 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr));
3120 fprintf(stdout, "Current Parameters:\n");
3122 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
3125 goto ratecontrol_bailout;
3127 if (arglist & CAM_ARG_VERBOSE)
3130 if (change_settings) {
3131 int didsettings = 0;
3132 struct ccb_trans_settings_spi *spi = NULL;
3133 struct ccb_trans_settings_scsi *scsi = NULL;
3135 if (ccb->cts.transport == XPORT_SPI) {
3136 spi = &ccb->cts.xport_specific.spi;
3139 if (ccb->cts.protocol == PROTO_SCSI) {
3140 scsi = &ccb->cts.proto_specific.scsi;
3143 if (spi && disc_enable != -1) {
3144 spi->valid |= CTS_SPI_VALID_DISC;
3145 if (disc_enable == 0)
3146 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
3148 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3151 if (scsi && tag_enable != -1) {
3152 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
3153 warnx("HBA does not support tagged queueing, "
3154 "so you cannot modify tag settings");
3156 goto ratecontrol_bailout;
3159 scsi->valid |= CTS_SCSI_VALID_TQ;
3161 if (tag_enable == 0)
3162 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
3164 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3168 if (spi && offset != -1) {
3169 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
3170 warnx("HBA at %s%d is not cable of changing "
3171 "offset", cpi.dev_name,
3174 goto ratecontrol_bailout;
3176 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3177 spi->sync_offset = offset;
3181 if (spi && syncrate != -1) {
3182 int prelim_sync_period;
3185 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
3186 warnx("HBA at %s%d is not cable of changing "
3187 "transfer rates", cpi.dev_name,
3190 goto ratecontrol_bailout;
3193 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3196 * The sync rate the user gives us is in MHz.
3197 * We need to translate it into KHz for this
3203 * Next, we calculate a "preliminary" sync period
3204 * in tenths of a nanosecond.
3207 prelim_sync_period = 0;
3209 prelim_sync_period = 10000000 / syncrate;
3212 scsi_calc_syncparam(prelim_sync_period);
3214 freq = scsi_calc_syncsrate(spi->sync_period);
3219 * The bus_width argument goes like this:
3223 * Therefore, if you shift the number of bits given on the
3224 * command line right by 4, you should get the correct
3227 if (spi && bus_width != -1) {
3230 * We might as well validate things here with a
3231 * decipherable error message, rather than what
3232 * will probably be an indecipherable error message
3233 * by the time it gets back to us.
3235 if ((bus_width == 16)
3236 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
3237 warnx("HBA does not support 16 bit bus width");
3239 goto ratecontrol_bailout;
3240 } else if ((bus_width == 32)
3241 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
3242 warnx("HBA does not support 32 bit bus width");
3244 goto ratecontrol_bailout;
3245 } else if ((bus_width != 8)
3246 && (bus_width != 16)
3247 && (bus_width != 32)) {
3248 warnx("Invalid bus width %d", bus_width);
3250 goto ratecontrol_bailout;
3253 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3254 spi->bus_width = bus_width >> 4;
3258 if (didsettings == 0) {
3259 goto ratecontrol_bailout;
3261 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
3263 if (cam_send_ccb(device, ccb) < 0) {
3264 perror("error sending XPT_SET_TRAN_SETTINGS CCB");
3265 if (arglist & CAM_ARG_VERBOSE) {
3266 cam_error_print(device, ccb, CAM_ESF_ALL,
3267 CAM_EPF_ALL, stderr);
3270 goto ratecontrol_bailout;
3273 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3274 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
3275 if (arglist & CAM_ARG_VERBOSE) {
3276 cam_error_print(device, ccb, CAM_ESF_ALL,
3277 CAM_EPF_ALL, stderr);
3280 goto ratecontrol_bailout;
3285 retval = testunitready(device, retry_count, timeout,
3286 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
3289 * If the TUR didn't succeed, just bail.
3293 fprintf(stderr, "Test Unit Ready failed\n");
3294 goto ratecontrol_bailout;
3298 * If the user wants things quiet, there's no sense in
3299 * getting the transfer settings, if we're not going
3303 goto ratecontrol_bailout;
3305 fprintf(stdout, "New Parameters:\n");
3306 retval = get_print_cts(device, user_settings, 0, NULL);
3309 ratecontrol_bailout:
3316 scsiformat(struct cam_device *device, int argc, char **argv,
3317 char *combinedopt, int retry_count, int timeout)
3321 int ycount = 0, quiet = 0;
3322 int error = 0, response = 0, retval = 0;
3323 int use_timeout = 10800 * 1000;
3325 struct format_defect_list_header fh;
3326 u_int8_t *data_ptr = NULL;
3327 u_int32_t dxfer_len = 0;
3329 int num_warnings = 0;
3332 ccb = cam_getccb(device);
3335 warnx("scsiformat: error allocating ccb");
3339 bzero(&(&ccb->ccb_h)[1],
3340 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3342 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3363 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
3364 "following device:\n");
3366 error = scsidoinquiry(device, argc, argv, combinedopt,
3367 retry_count, timeout);
3370 warnx("scsiformat: error sending inquiry");
3371 goto scsiformat_bailout;
3380 fprintf(stdout, "Are you SURE you want to do "
3383 if (fgets(str, sizeof(str), stdin) != NULL) {
3385 if (strncasecmp(str, "yes", 3) == 0)
3387 else if (strncasecmp(str, "no", 2) == 0)
3390 fprintf(stdout, "Please answer"
3391 " \"yes\" or \"no\"\n");
3394 } while (response == 0);
3396 if (response == -1) {
3398 goto scsiformat_bailout;
3403 use_timeout = timeout;
3406 fprintf(stdout, "Current format timeout is %d seconds\n",
3407 use_timeout / 1000);
3411 * If the user hasn't disabled questions and didn't specify a
3412 * timeout on the command line, ask them if they want the current
3416 && (timeout == 0)) {
3418 int new_timeout = 0;
3420 fprintf(stdout, "Enter new timeout in seconds or press\n"
3421 "return to keep the current timeout [%d] ",
3422 use_timeout / 1000);
3424 if (fgets(str, sizeof(str), stdin) != NULL) {
3426 new_timeout = atoi(str);
3429 if (new_timeout != 0) {
3430 use_timeout = new_timeout * 1000;
3431 fprintf(stdout, "Using new timeout value %d\n",
3432 use_timeout / 1000);
3437 * Keep this outside the if block below to silence any unused
3438 * variable warnings.
3440 bzero(&fh, sizeof(fh));
3443 * If we're in immediate mode, we've got to include the format
3446 if (immediate != 0) {
3447 fh.byte2 = FU_DLH_IMMED;
3448 data_ptr = (u_int8_t *)&fh;
3449 dxfer_len = sizeof(fh);
3450 byte2 = FU_FMT_DATA;
3451 } else if (quiet == 0) {
3452 fprintf(stdout, "Formatting...");
3456 scsi_format_unit(&ccb->csio,
3457 /* retries */ retry_count,
3459 /* tag_action */ MSG_SIMPLE_Q_TAG,
3462 /* data_ptr */ data_ptr,
3463 /* dxfer_len */ dxfer_len,
3464 /* sense_len */ SSD_FULL_SIZE,
3465 /* timeout */ use_timeout);
3467 /* Disable freezing the device queue */
3468 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3470 if (arglist & CAM_ARG_ERR_RECOVER)
3471 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3473 if (((retval = cam_send_ccb(device, ccb)) < 0)
3474 || ((immediate == 0)
3475 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
3476 const char errstr[] = "error sending format command";
3483 if (arglist & CAM_ARG_VERBOSE) {
3484 cam_error_print(device, ccb, CAM_ESF_ALL,
3485 CAM_EPF_ALL, stderr);
3488 goto scsiformat_bailout;
3492 * If we ran in non-immediate mode, we already checked for errors
3493 * above and printed out any necessary information. If we're in
3494 * immediate mode, we need to loop through and get status
3495 * information periodically.
3497 if (immediate == 0) {
3499 fprintf(stdout, "Format Complete\n");
3501 goto scsiformat_bailout;
3508 bzero(&(&ccb->ccb_h)[1],
3509 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3512 * There's really no need to do error recovery or
3513 * retries here, since we're just going to sit in a
3514 * loop and wait for the device to finish formatting.
3516 scsi_test_unit_ready(&ccb->csio,
3519 /* tag_action */ MSG_SIMPLE_Q_TAG,
3520 /* sense_len */ SSD_FULL_SIZE,
3521 /* timeout */ 5000);
3523 /* Disable freezing the device queue */
3524 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3526 retval = cam_send_ccb(device, ccb);
3529 * If we get an error from the ioctl, bail out. SCSI
3530 * errors are expected.
3533 warn("error sending CAMIOCOMMAND ioctl");
3534 if (arglist & CAM_ARG_VERBOSE) {
3535 cam_error_print(device, ccb, CAM_ESF_ALL,
3536 CAM_EPF_ALL, stderr);
3539 goto scsiformat_bailout;
3542 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3544 if ((status != CAM_REQ_CMP)
3545 && (status == CAM_SCSI_STATUS_ERROR)
3546 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
3547 struct scsi_sense_data *sense;
3548 int error_code, sense_key, asc, ascq;
3550 sense = &ccb->csio.sense_data;
3551 scsi_extract_sense(sense, &error_code, &sense_key,
3555 * According to the SCSI-2 and SCSI-3 specs, a
3556 * drive that is in the middle of a format should
3557 * return NOT READY with an ASC of "logical unit
3558 * not ready, format in progress". The sense key
3559 * specific bytes will then be a progress indicator.
3561 if ((sense_key == SSD_KEY_NOT_READY)
3562 && (asc == 0x04) && (ascq == 0x04)) {
3563 if ((sense->extra_len >= 10)
3564 && ((sense->sense_key_spec[0] &
3565 SSD_SCS_VALID) != 0)
3568 u_int64_t percentage;
3571 &sense->sense_key_spec[1]);
3572 percentage = 10000 * val;
3575 "\rFormatting: %ju.%02u %% "
3577 (uintmax_t)(percentage /
3579 (unsigned)((percentage /
3583 } else if ((quiet == 0)
3584 && (++num_warnings <= 1)) {
3585 warnx("Unexpected SCSI Sense Key "
3586 "Specific value returned "
3588 scsi_sense_print(device, &ccb->csio,
3590 warnx("Unable to print status "
3591 "information, but format will "
3593 warnx("will exit when format is "
3598 warnx("Unexpected SCSI error during format");
3599 cam_error_print(device, ccb, CAM_ESF_ALL,
3600 CAM_EPF_ALL, stderr);
3602 goto scsiformat_bailout;
3605 } else if (status != CAM_REQ_CMP) {
3606 warnx("Unexpected CAM status %#x", status);
3607 if (arglist & CAM_ARG_VERBOSE)
3608 cam_error_print(device, ccb, CAM_ESF_ALL,
3609 CAM_EPF_ALL, stderr);
3611 goto scsiformat_bailout;
3614 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
3617 fprintf(stdout, "\nFormat Complete\n");
3627 scsireportluns(struct cam_device *device, int argc, char **argv,
3628 char *combinedopt, int retry_count, int timeout)
3631 int c, countonly, lunsonly;
3632 struct scsi_report_luns_data *lundata;
3634 uint8_t report_type;
3635 uint32_t list_len, i, j;
3640 report_type = RPL_REPORT_DEFAULT;
3641 ccb = cam_getccb(device);
3644 warnx("%s: error allocating ccb", __func__);
3648 bzero(&(&ccb->ccb_h)[1],
3649 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3654 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3663 if (strcasecmp(optarg, "default") == 0)
3664 report_type = RPL_REPORT_DEFAULT;
3665 else if (strcasecmp(optarg, "wellknown") == 0)
3666 report_type = RPL_REPORT_WELLKNOWN;
3667 else if (strcasecmp(optarg, "all") == 0)
3668 report_type = RPL_REPORT_ALL;
3670 warnx("%s: invalid report type \"%s\"",
3681 if ((countonly != 0)
3682 && (lunsonly != 0)) {
3683 warnx("%s: you can only specify one of -c or -l", __func__);
3688 * According to SPC-4, the allocation length must be at least 16
3689 * bytes -- enough for the header and one LUN.
3691 alloc_len = sizeof(*lundata) + 8;
3695 lundata = malloc(alloc_len);
3697 if (lundata == NULL) {
3698 warn("%s: error mallocing %d bytes", __func__, alloc_len);
3703 scsi_report_luns(&ccb->csio,
3704 /*retries*/ retry_count,
3706 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3707 /*select_report*/ report_type,
3708 /*rpl_buf*/ lundata,
3709 /*alloc_len*/ alloc_len,
3710 /*sense_len*/ SSD_FULL_SIZE,
3711 /*timeout*/ timeout ? timeout : 5000);
3713 /* Disable freezing the device queue */
3714 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3716 if (arglist & CAM_ARG_ERR_RECOVER)
3717 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3719 if (cam_send_ccb(device, ccb) < 0) {
3720 warn("error sending REPORT LUNS command");
3722 if (arglist & CAM_ARG_VERBOSE)
3723 cam_error_print(device, ccb, CAM_ESF_ALL,
3724 CAM_EPF_ALL, stderr);
3730 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3731 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3737 list_len = scsi_4btoul(lundata->length);
3740 * If we need to list the LUNs, and our allocation
3741 * length was too short, reallocate and retry.
3743 if ((countonly == 0)
3744 && (list_len > (alloc_len - sizeof(*lundata)))) {
3745 alloc_len = list_len + sizeof(*lundata);
3751 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
3752 ((list_len / 8) > 1) ? "s" : "");
3757 for (i = 0; i < (list_len / 8); i++) {
3761 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
3763 fprintf(stdout, ",");
3764 switch (lundata->luns[i].lundata[j] &
3765 RPL_LUNDATA_ATYP_MASK) {
3766 case RPL_LUNDATA_ATYP_PERIPH:
3767 if ((lundata->luns[i].lundata[j] &
3768 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
3769 fprintf(stdout, "%d:",
3770 lundata->luns[i].lundata[j] &
3771 RPL_LUNDATA_PERIPH_BUS_MASK);
3773 && ((lundata->luns[i].lundata[j+2] &
3774 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
3777 fprintf(stdout, "%d",
3778 lundata->luns[i].lundata[j+1]);
3780 case RPL_LUNDATA_ATYP_FLAT: {
3782 tmplun[0] = lundata->luns[i].lundata[j] &
3783 RPL_LUNDATA_FLAT_LUN_MASK;
3784 tmplun[1] = lundata->luns[i].lundata[j+1];
3786 fprintf(stdout, "%d", scsi_2btoul(tmplun));
3790 case RPL_LUNDATA_ATYP_LUN:
3791 fprintf(stdout, "%d:%d:%d",
3792 (lundata->luns[i].lundata[j+1] &
3793 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
3794 lundata->luns[i].lundata[j] &
3795 RPL_LUNDATA_LUN_TARG_MASK,
3796 lundata->luns[i].lundata[j+1] &
3797 RPL_LUNDATA_LUN_LUN_MASK);
3799 case RPL_LUNDATA_ATYP_EXTLUN: {
3800 int field_len, field_len_code, eam_code;
3802 eam_code = lundata->luns[i].lundata[j] &
3803 RPL_LUNDATA_EXT_EAM_MASK;
3804 field_len_code = (lundata->luns[i].lundata[j] &
3805 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
3806 field_len = field_len_code * 2;
3808 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
3809 && (field_len_code == 0x00)) {
3810 fprintf(stdout, "%d",
3811 lundata->luns[i].lundata[j+1]);
3812 } else if ((eam_code ==
3813 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
3814 && (field_len_code == 0x03)) {
3818 * This format takes up all 8 bytes.
3819 * If we aren't starting at offset 0,
3823 fprintf(stdout, "Invalid "
3826 "specified format", j);
3830 bzero(tmp_lun, sizeof(tmp_lun));
3831 bcopy(&lundata->luns[i].lundata[j+1],
3832 &tmp_lun[1], sizeof(tmp_lun) - 1);
3833 fprintf(stdout, "%#jx",
3834 (intmax_t)scsi_8btou64(tmp_lun));
3837 fprintf(stderr, "Unknown Extended LUN"
3838 "Address method %#x, length "
3839 "code %#x", eam_code,
3846 fprintf(stderr, "Unknown LUN address method "
3847 "%#x\n", lundata->luns[i].lundata[0] &
3848 RPL_LUNDATA_ATYP_MASK);
3852 * For the flat addressing method, there are no
3853 * other levels after it.
3858 fprintf(stdout, "\n");
3871 scsireadcapacity(struct cam_device *device, int argc, char **argv,
3872 char *combinedopt, int retry_count, int timeout)
3875 int blocksizeonly, humanize, numblocks, quiet, sizeonly, baseten;
3876 struct scsi_read_capacity_data rcap;
3877 struct scsi_read_capacity_data_long rcaplong;
3891 ccb = cam_getccb(device);
3894 warnx("%s: error allocating ccb", __func__);
3898 bzero(&(&ccb->ccb_h)[1],
3899 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
3901 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3928 if ((blocksizeonly != 0)
3929 && (numblocks != 0)) {
3930 warnx("%s: you can only specify one of -b or -N", __func__);
3935 if ((blocksizeonly != 0)
3936 && (sizeonly != 0)) {
3937 warnx("%s: you can only specify one of -b or -s", __func__);
3944 warnx("%s: you can only specify one of -h/-H or -q", __func__);
3950 && (blocksizeonly != 0)) {
3951 warnx("%s: you can only specify one of -h/-H or -b", __func__);
3956 scsi_read_capacity(&ccb->csio,
3957 /*retries*/ retry_count,
3959 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3962 /*timeout*/ timeout ? timeout : 5000);
3964 /* Disable freezing the device queue */
3965 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3967 if (arglist & CAM_ARG_ERR_RECOVER)
3968 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
3970 if (cam_send_ccb(device, ccb) < 0) {
3971 warn("error sending READ CAPACITY command");
3973 if (arglist & CAM_ARG_VERBOSE)
3974 cam_error_print(device, ccb, CAM_ESF_ALL,
3975 CAM_EPF_ALL, stderr);
3981 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3982 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
3987 maxsector = scsi_4btoul(rcap.addr);
3988 block_len = scsi_4btoul(rcap.length);
3991 * A last block of 2^32-1 means that the true capacity is over 2TB,
3992 * and we need to issue the long READ CAPACITY to get the real
3993 * capacity. Otherwise, we're all set.
3995 if (maxsector != 0xffffffff)
3998 scsi_read_capacity_16(&ccb->csio,
3999 /*retries*/ retry_count,
4001 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4006 /*sense_len*/ SSD_FULL_SIZE,
4007 /*timeout*/ timeout ? timeout : 5000);
4009 /* Disable freezing the device queue */
4010 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
4012 if (arglist & CAM_ARG_ERR_RECOVER)
4013 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
4015 if (cam_send_ccb(device, ccb) < 0) {
4016 warn("error sending READ CAPACITY (16) command");
4018 if (arglist & CAM_ARG_VERBOSE)
4019 cam_error_print(device, ccb, CAM_ESF_ALL,
4020 CAM_EPF_ALL, stderr);
4026 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4027 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
4032 maxsector = scsi_8btou64(rcaplong.addr);
4033 block_len = scsi_4btoul(rcaplong.length);
4036 if (blocksizeonly == 0) {
4038 * Humanize implies !quiet, and also implies numblocks.
4040 if (humanize != 0) {
4045 tmpbytes = (maxsector + 1) * block_len;
4046 ret = humanize_number(tmpstr, sizeof(tmpstr),
4047 tmpbytes, "", HN_AUTOSCALE,
4050 HN_DIVISOR_1000 : 0));
4052 warnx("%s: humanize_number failed!", __func__);
4056 fprintf(stdout, "Device Size: %s%s", tmpstr,
4057 (sizeonly == 0) ? ", " : "\n");
4058 } else if (numblocks != 0) {
4059 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
4060 "Blocks: " : "", (uintmax_t)maxsector + 1,
4061 (sizeonly == 0) ? ", " : "\n");
4063 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
4064 "Last Block: " : "", (uintmax_t)maxsector,
4065 (sizeonly == 0) ? ", " : "\n");
4069 fprintf(stdout, "%s%u%s\n", (quiet == 0) ?
4070 "Block Length: " : "", block_len, (quiet == 0) ?
4078 #endif /* MINIMALISTIC */
4083 fprintf(verbose ? stdout : stderr,
4084 "usage: camcontrol <command> [device id][generic args][command args]\n"
4085 " camcontrol devlist [-v]\n"
4086 #ifndef MINIMALISTIC
4087 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
4088 " camcontrol tur [dev_id][generic args]\n"
4089 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
4090 " camcontrol identify [dev_id][generic args]\n"
4091 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
4092 " camcontrol readcap [dev_id][generic args] [-b] [-h] [-H] [-N]\n"
4094 " camcontrol start [dev_id][generic args]\n"
4095 " camcontrol stop [dev_id][generic args]\n"
4096 " camcontrol load [dev_id][generic args]\n"
4097 " camcontrol eject [dev_id][generic args]\n"
4098 #endif /* MINIMALISTIC */
4099 " camcontrol rescan <all | bus[:target:lun]>\n"
4100 " camcontrol reset <all | bus[:target:lun]>\n"
4101 #ifndef MINIMALISTIC
4102 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
4103 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
4104 " [-P pagectl][-e | -b][-d]\n"
4105 " camcontrol cmd [dev_id][generic args]\n"
4106 " <-a cmd [args] | -c cmd [args]>\n"
4107 " [-i len fmt|-o len fmt [args]] [-r fmt]\n"
4108 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
4109 " <all|bus[:target[:lun]]|off>\n"
4110 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
4111 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
4112 " [-D <enable|disable>][-O offset][-q]\n"
4113 " [-R syncrate][-v][-T <enable|disable>]\n"
4114 " [-U][-W bus_width]\n"
4115 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
4116 #endif /* MINIMALISTIC */
4117 " camcontrol help\n");
4120 #ifndef MINIMALISTIC
4122 "Specify one of the following options:\n"
4123 "devlist list all CAM devices\n"
4124 "periphlist list all CAM peripheral drivers attached to a device\n"
4125 "tur send a test unit ready to the named device\n"
4126 "inquiry send a SCSI inquiry command to the named device\n"
4127 "identify send a ATA identify command to the named device\n"
4128 "reportluns send a SCSI report luns command to the device\n"
4129 "readcap send a SCSI read capacity command to the device\n"
4130 "start send a Start Unit command to the device\n"
4131 "stop send a Stop Unit command to the device\n"
4132 "load send a Start Unit command to the device with the load bit set\n"
4133 "eject send a Stop Unit command to the device with the eject bit set\n"
4134 "rescan rescan all busses, the given bus, or bus:target:lun\n"
4135 "reset reset all busses, the given bus, or bus:target:lun\n"
4136 "defects read the defect list of the specified device\n"
4137 "modepage display or edit (-e) the given mode page\n"
4138 "cmd send the given scsi command, may need -i or -o as well\n"
4139 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
4140 "tags report or set the number of transaction slots for a device\n"
4141 "negotiate report or set device negotiation parameters\n"
4142 "format send the SCSI FORMAT UNIT command to the named device\n"
4143 "help this message\n"
4144 "Device Identifiers:\n"
4145 "bus:target specify the bus and target, lun defaults to 0\n"
4146 "bus:target:lun specify the bus, target and lun\n"
4147 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
4148 "Generic arguments:\n"
4149 "-v be verbose, print out sense information\n"
4150 "-t timeout command timeout in seconds, overrides default timeout\n"
4151 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
4152 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
4153 "-E have the kernel attempt to perform SCSI error recovery\n"
4154 "-C count specify the SCSI command retry count (needs -E to work)\n"
4155 "modepage arguments:\n"
4156 "-l list all available mode pages\n"
4157 "-m page specify the mode page to view or edit\n"
4158 "-e edit the specified mode page\n"
4159 "-b force view to binary mode\n"
4160 "-d disable block descriptors for mode sense\n"
4161 "-P pgctl page control field 0-3\n"
4162 "defects arguments:\n"
4163 "-f format specify defect list format (block, bfi or phys)\n"
4164 "-G get the grown defect list\n"
4165 "-P get the permanant defect list\n"
4166 "inquiry arguments:\n"
4167 "-D get the standard inquiry data\n"
4168 "-S get the serial number\n"
4169 "-R get the transfer rate, etc.\n"
4170 "reportluns arguments:\n"
4171 "-c only report a count of available LUNs\n"
4172 "-l only print out luns, and not a count\n"
4173 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
4174 "readcap arguments\n"
4175 "-b only report the blocksize\n"
4176 "-h human readable device size, base 2\n"
4177 "-H human readable device size, base 10\n"
4178 "-N print the number of blocks instead of last block\n"
4179 "-q quiet, print numbers only\n"
4180 "-s only report the last block/device size\n"
4182 "-c cdb [args] specify the SCSI CDB\n"
4183 "-i len fmt specify input data and input data format\n"
4184 "-o len fmt [args] specify output data and output data fmt\n"
4185 "debug arguments:\n"
4186 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
4187 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
4188 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
4189 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
4191 "-N tags specify the number of tags to use for this device\n"
4192 "-q be quiet, don't report the number of tags\n"
4193 "-v report a number of tag-related parameters\n"
4194 "negotiate arguments:\n"
4195 "-a send a test unit ready after negotiation\n"
4196 "-c report/set current negotiation settings\n"
4197 "-D <arg> \"enable\" or \"disable\" disconnection\n"
4198 "-O offset set command delay offset\n"
4199 "-q be quiet, don't report anything\n"
4200 "-R syncrate synchronization rate in MHz\n"
4201 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
4202 "-U report/set user negotiation settings\n"
4203 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
4204 "-v also print a Path Inquiry CCB for the controller\n"
4205 "format arguments:\n"
4206 "-q be quiet, don't print status messages\n"
4207 "-r run in report only mode\n"
4208 "-w don't send immediate format command\n"
4209 "-y don't ask any questions\n");
4210 #endif /* MINIMALISTIC */
4214 main(int argc, char **argv)
4217 char *device = NULL;
4219 struct cam_device *cam_dev = NULL;
4220 int timeout = 0, retry_count = 1;
4221 camcontrol_optret optreturn;
4223 const char *mainopt = "C:En:t:u:v";
4224 const char *subopt = NULL;
4225 char combinedopt[256];
4226 int error = 0, optstart = 2;
4228 #ifndef MINIMALISTIC
4229 int bus, target, lun;
4230 #endif /* MINIMALISTIC */
4232 cmdlist = CAM_CMD_NONE;
4233 arglist = CAM_ARG_NONE;
4241 * Get the base option.
4243 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt);
4245 if (optreturn == CC_OR_AMBIGUOUS) {
4246 warnx("ambiguous option %s", argv[1]);
4249 } else if (optreturn == CC_OR_NOT_FOUND) {
4250 warnx("option %s not found", argv[1]);
4256 * Ahh, getopt(3) is a pain.
4258 * This is a gross hack. There really aren't many other good
4259 * options (excuse the pun) for parsing options in a situation like
4260 * this. getopt is kinda braindead, so you end up having to run
4261 * through the options twice, and give each invocation of getopt
4262 * the option string for the other invocation.
4264 * You would think that you could just have two groups of options.
4265 * The first group would get parsed by the first invocation of
4266 * getopt, and the second group would get parsed by the second
4267 * invocation of getopt. It doesn't quite work out that way. When
4268 * the first invocation of getopt finishes, it leaves optind pointing
4269 * to the argument _after_ the first argument in the second group.
4270 * So when the second invocation of getopt comes around, it doesn't
4271 * recognize the first argument it gets and then bails out.
4273 * A nice alternative would be to have a flag for getopt that says
4274 * "just keep parsing arguments even when you encounter an unknown
4275 * argument", but there isn't one. So there's no real clean way to
4276 * easily parse two sets of arguments without having one invocation
4277 * of getopt know about the other.
4279 * Without this hack, the first invocation of getopt would work as
4280 * long as the generic arguments are first, but the second invocation
4281 * (in the subfunction) would fail in one of two ways. In the case
4282 * where you don't set optreset, it would fail because optind may be
4283 * pointing to the argument after the one it should be pointing at.
4284 * In the case where you do set optreset, and reset optind, it would
4285 * fail because getopt would run into the first set of options, which
4286 * it doesn't understand.
4288 * All of this would "sort of" work if you could somehow figure out
4289 * whether optind had been incremented one option too far. The
4290 * mechanics of that, however, are more daunting than just giving
4291 * both invocations all of the expect options for either invocation.
4293 * Needless to say, I wouldn't mind if someone invented a better
4294 * (non-GPL!) command line parsing interface than getopt. I
4295 * wouldn't mind if someone added more knobs to getopt to make it
4296 * work better. Who knows, I may talk myself into doing it someday,
4297 * if the standards weenies let me. As it is, it just leads to
4298 * hackery like this and causes people to avoid it in some cases.
4300 * KDM, September 8th, 1998
4303 sprintf(combinedopt, "%s%s", mainopt, subopt);
4305 sprintf(combinedopt, "%s", mainopt);
4308 * For these options we do not parse optional device arguments and
4309 * we do not open a passthrough device.
4311 if ((cmdlist == CAM_CMD_RESCAN)
4312 || (cmdlist == CAM_CMD_RESET)
4313 || (cmdlist == CAM_CMD_DEVTREE)
4314 || (cmdlist == CAM_CMD_USAGE)
4315 || (cmdlist == CAM_CMD_DEBUG))
4318 #ifndef MINIMALISTIC
4320 && (argc > 2 && argv[2][0] != '-')) {
4325 * First catch people who try to do things like:
4326 * camcontrol tur /dev/da0
4327 * camcontrol doesn't take device nodes as arguments.
4329 if (argv[2][0] == '/') {
4330 warnx("%s is not a valid device identifier", argv[2]);
4331 errx(1, "please read the camcontrol(8) man page");
4332 } else if (isdigit(argv[2][0])) {
4333 /* device specified as bus:target[:lun] */
4334 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
4336 errx(1, "numeric device specification must "
4337 "be either bus:target, or "
4339 /* default to 0 if lun was not specified */
4340 if ((arglist & CAM_ARG_LUN) == 0) {
4342 arglist |= CAM_ARG_LUN;
4346 if (cam_get_device(argv[2], name, sizeof name, &unit)
4348 errx(1, "%s", cam_errbuf);
4349 device = strdup(name);
4350 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
4354 #endif /* MINIMALISTIC */
4356 * Start getopt processing at argv[2/3], since we've already
4357 * accepted argv[1..2] as the command name, and as a possible
4363 * Now we run through the argument list looking for generic
4364 * options, and ignoring options that possibly belong to
4367 while ((c = getopt(argc, argv, combinedopt))!= -1){
4370 retry_count = strtol(optarg, NULL, 0);
4371 if (retry_count < 0)
4372 errx(1, "retry count %d is < 0",
4374 arglist |= CAM_ARG_RETRIES;
4377 arglist |= CAM_ARG_ERR_RECOVER;
4380 arglist |= CAM_ARG_DEVICE;
4382 while (isspace(*tstr) && (*tstr != '\0'))
4384 device = (char *)strdup(tstr);
4387 timeout = strtol(optarg, NULL, 0);
4389 errx(1, "invalid timeout %d", timeout);
4390 /* Convert the timeout from seconds to ms */
4392 arglist |= CAM_ARG_TIMEOUT;
4395 arglist |= CAM_ARG_UNIT;
4396 unit = strtol(optarg, NULL, 0);
4399 arglist |= CAM_ARG_VERBOSE;
4406 #ifndef MINIMALISTIC
4408 * For most commands we'll want to open the passthrough device
4409 * associated with the specified device. In the case of the rescan
4410 * commands, we don't use a passthrough device at all, just the
4411 * transport layer device.
4414 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
4415 && (((arglist & CAM_ARG_DEVICE) == 0)
4416 || ((arglist & CAM_ARG_UNIT) == 0))) {
4417 errx(1, "subcommand \"%s\" requires a valid device "
4418 "identifier", argv[1]);
4421 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
4422 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
4423 cam_open_spec_device(device,unit,O_RDWR,NULL)))
4425 errx(1,"%s", cam_errbuf);
4427 #endif /* MINIMALISTIC */
4430 * Reset optind to 2, and reset getopt, so these routines can parse
4431 * the arguments again.
4437 #ifndef MINIMALISTIC
4438 case CAM_CMD_DEVLIST:
4439 error = getdevlist(cam_dev);
4441 #endif /* MINIMALISTIC */
4442 case CAM_CMD_DEVTREE:
4443 error = getdevtree();
4445 #ifndef MINIMALISTIC
4447 error = testunitready(cam_dev, retry_count, timeout, 0);
4449 case CAM_CMD_INQUIRY:
4450 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
4451 retry_count, timeout);
4453 case CAM_CMD_IDENTIFY:
4454 error = ataidentify(cam_dev, retry_count, timeout);
4456 case CAM_CMD_STARTSTOP:
4457 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
4458 arglist & CAM_ARG_EJECT, retry_count,
4461 #endif /* MINIMALISTIC */
4462 case CAM_CMD_RESCAN:
4463 error = dorescan_or_reset(argc, argv, 1);
4466 error = dorescan_or_reset(argc, argv, 0);
4468 #ifndef MINIMALISTIC
4469 case CAM_CMD_READ_DEFECTS:
4470 error = readdefects(cam_dev, argc, argv, combinedopt,
4471 retry_count, timeout);
4473 case CAM_CMD_MODE_PAGE:
4474 modepage(cam_dev, argc, argv, combinedopt,
4475 retry_count, timeout);
4477 case CAM_CMD_SCSI_CMD:
4478 error = scsicmd(cam_dev, argc, argv, combinedopt,
4479 retry_count, timeout);
4482 error = camdebug(argc, argv, combinedopt);
4485 error = tagcontrol(cam_dev, argc, argv, combinedopt);
4488 error = ratecontrol(cam_dev, retry_count, timeout,
4489 argc, argv, combinedopt);
4491 case CAM_CMD_FORMAT:
4492 error = scsiformat(cam_dev, argc, argv,
4493 combinedopt, retry_count, timeout);
4495 case CAM_CMD_REPORTLUNS:
4496 error = scsireportluns(cam_dev, argc, argv,
4497 combinedopt, retry_count,
4500 case CAM_CMD_READCAP:
4501 error = scsireadcapacity(cam_dev, argc, argv,
4502 combinedopt, retry_count,
4505 #endif /* MINIMALISTIC */
4515 if (cam_dev != NULL)
4516 cam_close_device(cam_dev);