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>
36 #include <sys/endian.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/scsi/scsi_all.h>
56 #include <cam/scsi/scsi_da.h>
57 #include <cam/scsi/scsi_pass.h>
58 #include <cam/scsi/scsi_message.h>
59 #include <cam/scsi/smp_all.h>
60 #include <cam/ata/ata_all.h>
61 #include <cam/mmc/mmc_all.h>
63 #include "camcontrol.h"
65 #include "nvmecontrol_ext.h"
69 CAM_CMD_NONE = 0x00000000,
70 CAM_CMD_DEVLIST = 0x00000001,
71 CAM_CMD_TUR = 0x00000002,
72 CAM_CMD_INQUIRY = 0x00000003,
73 CAM_CMD_STARTSTOP = 0x00000004,
74 CAM_CMD_RESCAN = 0x00000005,
75 CAM_CMD_READ_DEFECTS = 0x00000006,
76 CAM_CMD_MODE_PAGE = 0x00000007,
77 CAM_CMD_SCSI_CMD = 0x00000008,
78 CAM_CMD_DEVTREE = 0x00000009,
79 CAM_CMD_USAGE = 0x0000000a,
80 CAM_CMD_DEBUG = 0x0000000b,
81 CAM_CMD_RESET = 0x0000000c,
82 CAM_CMD_FORMAT = 0x0000000d,
83 CAM_CMD_TAG = 0x0000000e,
84 CAM_CMD_RATE = 0x0000000f,
85 CAM_CMD_DETACH = 0x00000010,
86 CAM_CMD_REPORTLUNS = 0x00000011,
87 CAM_CMD_READCAP = 0x00000012,
88 CAM_CMD_IDENTIFY = 0x00000013,
89 CAM_CMD_IDLE = 0x00000014,
90 CAM_CMD_STANDBY = 0x00000015,
91 CAM_CMD_SLEEP = 0x00000016,
92 CAM_CMD_SMP_CMD = 0x00000017,
93 CAM_CMD_SMP_RG = 0x00000018,
94 CAM_CMD_SMP_PC = 0x00000019,
95 CAM_CMD_SMP_PHYLIST = 0x0000001a,
96 CAM_CMD_SMP_MANINFO = 0x0000001b,
97 CAM_CMD_DOWNLOAD_FW = 0x0000001c,
98 CAM_CMD_SECURITY = 0x0000001d,
99 CAM_CMD_HPA = 0x0000001e,
100 CAM_CMD_SANITIZE = 0x0000001f,
101 CAM_CMD_PERSIST = 0x00000020,
102 CAM_CMD_APM = 0x00000021,
103 CAM_CMD_AAM = 0x00000022,
104 CAM_CMD_ATTRIB = 0x00000023,
105 CAM_CMD_OPCODES = 0x00000024,
106 CAM_CMD_REPROBE = 0x00000025,
107 CAM_CMD_ZONE = 0x00000026,
108 CAM_CMD_EPC = 0x00000027,
109 CAM_CMD_TIMESTAMP = 0x00000028,
110 CAM_CMD_MMCSD_CMD = 0x00000029,
111 CAM_CMD_POWER_MODE = 0x0000002a,
112 CAM_CMD_DEVTYPE = 0x0000002b,
113 CAM_CMD_AMA = 0x0000002c,
117 CAM_ARG_NONE = 0x00000000,
118 CAM_ARG_VERBOSE = 0x00000001,
119 CAM_ARG_DEVICE = 0x00000002,
120 CAM_ARG_BUS = 0x00000004,
121 CAM_ARG_TARGET = 0x00000008,
122 CAM_ARG_LUN = 0x00000010,
123 CAM_ARG_EJECT = 0x00000020,
124 CAM_ARG_UNIT = 0x00000040,
125 CAM_ARG_FORMAT_BLOCK = 0x00000080,
126 CAM_ARG_FORMAT_BFI = 0x00000100,
127 CAM_ARG_FORMAT_PHYS = 0x00000200,
128 CAM_ARG_PLIST = 0x00000400,
129 CAM_ARG_GLIST = 0x00000800,
130 CAM_ARG_GET_SERIAL = 0x00001000,
131 CAM_ARG_GET_STDINQ = 0x00002000,
132 CAM_ARG_GET_XFERRATE = 0x00004000,
133 CAM_ARG_INQ_MASK = 0x00007000,
134 CAM_ARG_TIMEOUT = 0x00020000,
135 CAM_ARG_CMD_IN = 0x00040000,
136 CAM_ARG_CMD_OUT = 0x00080000,
137 CAM_ARG_ERR_RECOVER = 0x00200000,
138 CAM_ARG_RETRIES = 0x00400000,
139 CAM_ARG_START_UNIT = 0x00800000,
140 CAM_ARG_DEBUG_INFO = 0x01000000,
141 CAM_ARG_DEBUG_TRACE = 0x02000000,
142 CAM_ARG_DEBUG_SUBTRACE = 0x04000000,
143 CAM_ARG_DEBUG_CDB = 0x08000000,
144 CAM_ARG_DEBUG_XPT = 0x10000000,
145 CAM_ARG_DEBUG_PERIPH = 0x20000000,
146 CAM_ARG_DEBUG_PROBE = 0x40000000,
149 struct camcontrol_opts {
156 struct ata_set_max_pwd
159 u_int8_t password[32];
160 u_int16_t reserved2[239];
163 static struct scsi_nv task_attrs[] = {
164 { "simple", MSG_SIMPLE_Q_TAG },
165 { "head", MSG_HEAD_OF_Q_TAG },
166 { "ordered", MSG_ORDERED_Q_TAG },
167 { "iwr", MSG_IGN_WIDE_RESIDUE },
168 { "aca", MSG_ACA_TASK }
171 static const char scsicmd_opts[] = "a:c:dfi:o:r";
172 static const char readdefect_opts[] = "f:GPqsS:X";
173 static const char negotiate_opts[] = "acD:M:O:qR:T:UW:";
174 static const char smprg_opts[] = "l";
175 static const char smppc_opts[] = "a:A:d:lm:M:o:p:s:S:T:";
176 static const char smpphylist_opts[] = "lq";
179 static struct camcontrol_opts option_table[] = {
180 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL},
181 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"},
182 {"identify", CAM_CMD_IDENTIFY, CAM_ARG_NONE, NULL},
183 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL},
184 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL},
185 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL},
186 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL},
187 {"reportluns", CAM_CMD_REPORTLUNS, CAM_ARG_NONE, "clr:"},
188 {"readcapacity", CAM_CMD_READCAP, CAM_ARG_NONE, "bhHlNqs"},
189 {"reprobe", CAM_CMD_REPROBE, CAM_ARG_NONE, NULL},
190 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL},
191 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL},
192 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
193 {"mmcsdcmd", CAM_CMD_MMCSD_CMD, CAM_ARG_NONE, "c:a:F:f:Wb:l:41S:I"},
194 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts},
195 {"smpcmd", CAM_CMD_SMP_CMD, CAM_ARG_NONE, "r:R:"},
196 {"smprg", CAM_CMD_SMP_RG, CAM_ARG_NONE, smprg_opts},
197 {"smpreportgeneral", CAM_CMD_SMP_RG, CAM_ARG_NONE, smprg_opts},
198 {"smppc", CAM_CMD_SMP_PC, CAM_ARG_NONE, smppc_opts},
199 {"smpphycontrol", CAM_CMD_SMP_PC, CAM_ARG_NONE, smppc_opts},
200 {"smpplist", CAM_CMD_SMP_PHYLIST, CAM_ARG_NONE, smpphylist_opts},
201 {"smpphylist", CAM_CMD_SMP_PHYLIST, CAM_ARG_NONE, smpphylist_opts},
202 {"smpmaninfo", CAM_CMD_SMP_MANINFO, CAM_ARG_NONE, "l"},
203 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
204 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts},
205 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, "-b"},
206 {"devtype", CAM_CMD_DEVTYPE, CAM_ARG_NONE, ""},
207 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL},
208 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "6bdelm:DLP:"},
209 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"},
210 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
211 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts},
212 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXcp"},
213 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"},
214 {"sanitize", CAM_CMD_SANITIZE, CAM_ARG_NONE, "a:c:IP:qrUwy"},
215 {"idle", CAM_CMD_IDLE, CAM_ARG_NONE, "t:"},
216 {"standby", CAM_CMD_STANDBY, CAM_ARG_NONE, "t:"},
217 {"sleep", CAM_CMD_SLEEP, CAM_ARG_NONE, ""},
218 {"powermode", CAM_CMD_POWER_MODE, CAM_ARG_NONE, ""},
219 {"apm", CAM_CMD_APM, CAM_ARG_NONE, "l:"},
220 {"aam", CAM_CMD_AAM, CAM_ARG_NONE, "l:"},
221 {"fwdownload", CAM_CMD_DOWNLOAD_FW, CAM_ARG_NONE, "f:qsy"},
222 {"security", CAM_CMD_SECURITY, CAM_ARG_NONE, "d:e:fh:k:l:qs:T:U:y"},
223 {"hpa", CAM_CMD_HPA, CAM_ARG_NONE, "Pflp:qs:U:y"},
224 {"ama", CAM_CMD_AMA, CAM_ARG_NONE, "fqs:"},
225 {"persist", CAM_CMD_PERSIST, CAM_ARG_NONE, "ai:I:k:K:o:ps:ST:U"},
226 {"attrib", CAM_CMD_ATTRIB, CAM_ARG_NONE, "a:ce:F:p:r:s:T:w:V:"},
227 {"opcodes", CAM_CMD_OPCODES, CAM_ARG_NONE, "No:s:T"},
228 {"zone", CAM_CMD_ZONE, CAM_ARG_NONE, "ac:l:No:P:"},
229 {"epc", CAM_CMD_EPC, CAM_ARG_NONE, "c:dDeHp:Pr:sS:T:"},
230 {"timestamp", CAM_CMD_TIMESTAMP, CAM_ARG_NONE, "f:mrsUT:"},
231 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
232 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
233 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL},
238 struct device_match_result dev_match;
240 struct periph_match_result *periph_matches;
241 struct scsi_vpd_device_id *device_id;
243 STAILQ_ENTRY(cam_devitem) links;
247 STAILQ_HEAD(, cam_devitem) dev_queue;
251 static cam_cmdmask cmdlist;
252 static cam_argmask arglist;
254 static const char *devtype_names[] = {
264 camcontrol_optret getoption(struct camcontrol_opts *table, char *arg,
265 uint32_t *cmdnum, cam_argmask *argnum,
266 const char **subopt);
267 static int getdevlist(struct cam_device *device);
268 static int getdevtree(int argc, char **argv, char *combinedopt);
269 static int getdevtype(struct cam_device *device);
270 static int print_dev_scsi(struct device_match_result *dev_result, char *tmpstr);
271 static int print_dev_ata(struct device_match_result *dev_result, char *tmpstr);
272 static int print_dev_semb(struct device_match_result *dev_result, char *tmpstr);
273 static int print_dev_mmcsd(struct device_match_result *dev_result,
276 static int print_dev_nvme(struct device_match_result *dev_result, char *tmpstr);
278 static int testunitready(struct cam_device *device, int task_attr,
279 int retry_count, int timeout, int quiet);
280 static int scsistart(struct cam_device *device, int startstop, int loadeject,
281 int task_attr, int retry_count, int timeout);
282 static int scsiinquiry(struct cam_device *device, int task_attr,
283 int retry_count, int timeout);
284 static int scsiserial(struct cam_device *device, int task_attr,
285 int retry_count, int timeout);
286 static int parse_btl(char *tstr, path_id_t *bus, target_id_t *target,
287 lun_id_t *lun, cam_argmask *arglst);
288 static int reprobe(struct cam_device *device);
289 static int dorescan_or_reset(int argc, char **argv, int rescan);
290 static int rescan_or_reset_bus(path_id_t bus, int rescan);
291 static int scanlun_or_reset_dev(path_id_t bus, target_id_t target,
292 lun_id_t lun, int scan);
293 static int readdefects(struct cam_device *device, int argc, char **argv,
294 char *combinedopt, int task_attr, int retry_count,
296 static void modepage(struct cam_device *device, int argc, char **argv,
297 char *combinedopt, int task_attr, int retry_count,
299 static int scsicmd(struct cam_device *device, int argc, char **argv,
300 char *combinedopt, int task_attr, int retry_count,
302 static int smpcmd(struct cam_device *device, int argc, char **argv,
303 char *combinedopt, int retry_count, int timeout);
304 static int mmcsdcmd(struct cam_device *device, int argc, char **argv,
305 char *combinedopt, int retry_count, int timeout);
306 static int smpreportgeneral(struct cam_device *device, int argc, char **argv,
307 char *combinedopt, int retry_count, int timeout);
308 static int smpphycontrol(struct cam_device *device, int argc, char **argv,
309 char *combinedopt, int retry_count, int timeout);
310 static int smpmaninfo(struct cam_device *device, int argc, char **argv,
311 char *combinedopt, int retry_count, int timeout);
312 static int getdevid(struct cam_devitem *item);
313 static int buildbusdevlist(struct cam_devlist *devlist);
314 static void freebusdevlist(struct cam_devlist *devlist);
315 static struct cam_devitem *findsasdevice(struct cam_devlist *devlist,
317 static int smpphylist(struct cam_device *device, int argc, char **argv,
318 char *combinedopt, int retry_count, int timeout);
319 static int tagcontrol(struct cam_device *device, int argc, char **argv,
321 static void cts_print(struct cam_device *device,
322 struct ccb_trans_settings *cts);
323 static void cpi_print(struct ccb_pathinq *cpi);
324 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
325 static int get_cgd(struct cam_device *device, struct ccb_getdev *cgd);
326 static int get_print_cts(struct cam_device *device, int user_settings,
327 int quiet, struct ccb_trans_settings *cts);
328 static int ratecontrol(struct cam_device *device, int task_attr,
329 int retry_count, int timeout, int argc, char **argv,
331 static int scsiformat(struct cam_device *device, int argc, char **argv,
332 char *combinedopt, int task_attr, int retry_count,
334 static int sanitize(struct cam_device *device, int argc, char **argv,
335 char *combinedopt, int task_attr, int retry_count,
337 static int scsireportluns(struct cam_device *device, int argc, char **argv,
338 char *combinedopt, int task_attr, int retry_count,
340 static int scsireadcapacity(struct cam_device *device, int argc, char **argv,
341 char *combinedopt, int task_attr, int retry_count,
343 static int atapm(struct cam_device *device, int argc, char **argv,
344 char *combinedopt, int retry_count, int timeout);
345 static int atasecurity(struct cam_device *device, int retry_count, int timeout,
346 int argc, char **argv, char *combinedopt);
347 static int atahpa(struct cam_device *device, int retry_count, int timeout,
348 int argc, char **argv, char *combinedopt);
349 static int ataama(struct cam_device *device, int retry_count, int timeout,
350 int argc, char **argv, char *combinedopt);
351 static int scsiprintoneopcode(struct cam_device *device, int req_opcode,
352 int sa_set, int req_sa, uint8_t *buf,
354 static int scsiprintopcodes(struct cam_device *device, int td_req, uint8_t *buf,
356 static int scsiopcodes(struct cam_device *device, int argc, char **argv,
357 char *combinedopt, int task_attr, int retry_count,
358 int timeout, int verbose);
361 #define min(a,b) (((a)<(b))?(a):(b))
364 #define max(a,b) (((a)>(b))?(a):(b))
368 getoption(struct camcontrol_opts *table, char *arg, uint32_t *cmdnum,
369 cam_argmask *argnum, const char **subopt)
371 struct camcontrol_opts *opts;
374 for (opts = table; (opts != NULL) && (opts->optname != NULL);
376 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
377 *cmdnum = opts->cmdnum;
378 *argnum = opts->argnum;
379 *subopt = opts->subopt;
380 if (++num_matches > 1)
381 return (CC_OR_AMBIGUOUS);
386 return (CC_OR_FOUND);
388 return (CC_OR_NOT_FOUND);
392 getdevlist(struct cam_device *device)
398 ccb = cam_getccb(device);
400 ccb->ccb_h.func_code = XPT_GDEVLIST;
401 ccb->ccb_h.flags = CAM_DIR_NONE;
402 ccb->ccb_h.retry_count = 1;
404 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
405 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
406 if (cam_send_ccb(device, ccb) < 0) {
407 warn("error getting device list");
414 switch (ccb->cgdl.status) {
415 case CAM_GDEVLIST_MORE_DEVS:
416 strcpy(status, "MORE");
418 case CAM_GDEVLIST_LAST_DEVICE:
419 strcpy(status, "LAST");
421 case CAM_GDEVLIST_LIST_CHANGED:
422 strcpy(status, "CHANGED");
424 case CAM_GDEVLIST_ERROR:
425 strcpy(status, "ERROR");
430 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
431 ccb->cgdl.periph_name,
432 ccb->cgdl.unit_number,
433 ccb->cgdl.generation,
438 * If the list has changed, we need to start over from the
441 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
451 getdevtree(int argc, char **argv, char *combinedopt)
462 while ((c = getopt(argc, argv, combinedopt)) != -1) {
465 if ((arglist & CAM_ARG_VERBOSE) == 0)
473 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
474 warn("couldn't open %s", XPT_DEVICE);
478 bzero(&ccb, sizeof(union ccb));
480 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
481 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
482 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
484 ccb.ccb_h.func_code = XPT_DEV_MATCH;
485 bufsize = sizeof(struct dev_match_result) * 100;
486 ccb.cdm.match_buf_len = bufsize;
487 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
488 if (ccb.cdm.matches == NULL) {
489 warnx("can't malloc memory for matches");
493 ccb.cdm.num_matches = 0;
496 * We fetch all nodes, since we display most of them in the default
497 * case, and all in the verbose case.
499 ccb.cdm.num_patterns = 0;
500 ccb.cdm.pattern_buf_len = 0;
503 * We do the ioctl multiple times if necessary, in case there are
504 * more than 100 nodes in the EDT.
507 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
508 warn("error sending CAMIOCOMMAND ioctl");
513 if ((ccb.ccb_h.status != CAM_REQ_CMP)
514 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
515 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
516 warnx("got CAM error %#x, CDM error %d\n",
517 ccb.ccb_h.status, ccb.cdm.status);
522 for (i = 0; i < ccb.cdm.num_matches; i++) {
523 switch (ccb.cdm.matches[i].type) {
524 case DEV_MATCH_BUS: {
525 struct bus_match_result *bus_result;
528 * Only print the bus information if the
529 * user turns on the verbose flag.
531 if ((busonly == 0) &&
532 (arglist & CAM_ARG_VERBOSE) == 0)
536 &ccb.cdm.matches[i].result.bus_result;
539 fprintf(stdout, ")\n");
543 fprintf(stdout, "scbus%d on %s%d bus %d%s\n",
545 bus_result->dev_name,
546 bus_result->unit_number,
548 (busonly ? "" : ":"));
551 case DEV_MATCH_DEVICE: {
552 struct device_match_result *dev_result;
559 &ccb.cdm.matches[i].result.device_result;
561 if ((dev_result->flags
562 & DEV_RESULT_UNCONFIGURED)
563 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
569 if (dev_result->protocol == PROTO_SCSI) {
570 if (print_dev_scsi(dev_result,
575 } else if (dev_result->protocol == PROTO_ATA ||
576 dev_result->protocol == PROTO_SATAPM) {
577 if (print_dev_ata(dev_result,
582 } else if (dev_result->protocol == PROTO_MMCSD){
583 if (print_dev_mmcsd(dev_result,
588 } else if (dev_result->protocol == PROTO_SEMB) {
589 if (print_dev_semb(dev_result,
595 } else if (dev_result->protocol == PROTO_NVME) {
596 if (print_dev_nvme(dev_result,
603 sprintf(tmpstr, "<>");
606 fprintf(stdout, ")\n");
610 fprintf(stdout, "%-33s at scbus%d "
611 "target %d lun %jx (",
614 dev_result->target_id,
615 (uintmax_t)dev_result->target_lun);
621 case DEV_MATCH_PERIPH: {
622 struct periph_match_result *periph_result;
625 &ccb.cdm.matches[i].result.periph_result;
627 if (busonly || skip_device != 0)
631 fprintf(stdout, ",");
633 fprintf(stdout, "%s%d",
634 periph_result->periph_name,
635 periph_result->unit_number);
641 fprintf(stdout, "unknown match type\n");
646 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
647 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
650 fprintf(stdout, ")\n");
658 getdevtype(struct cam_device *cam_dev)
660 camcontrol_devtype dt;
664 * Get the device type and report it, request no I/O be done to do this.
666 error = get_device_type(cam_dev, -1, 0, 0, &dt);
667 if (error != 0 || (unsigned)dt > CC_DT_UNKNOWN) {
668 fprintf(stdout, "illegal\n");
671 fprintf(stdout, "%s\n", devtype_names[dt]);
676 print_dev_scsi(struct device_match_result *dev_result, char *tmpstr)
678 char vendor[16], product[48], revision[16];
680 cam_strvis(vendor, dev_result->inq_data.vendor,
681 sizeof(dev_result->inq_data.vendor), sizeof(vendor));
682 cam_strvis(product, dev_result->inq_data.product,
683 sizeof(dev_result->inq_data.product), sizeof(product));
684 cam_strvis(revision, dev_result->inq_data.revision,
685 sizeof(dev_result->inq_data.revision), sizeof(revision));
686 sprintf(tmpstr, "<%s %s %s>", vendor, product, revision);
692 print_dev_ata(struct device_match_result *dev_result, char *tmpstr)
694 char product[48], revision[16];
696 cam_strvis(product, dev_result->ident_data.model,
697 sizeof(dev_result->ident_data.model), sizeof(product));
698 cam_strvis(revision, dev_result->ident_data.revision,
699 sizeof(dev_result->ident_data.revision), sizeof(revision));
700 sprintf(tmpstr, "<%s %s>", product, revision);
706 print_dev_semb(struct device_match_result *dev_result, char *tmpstr)
708 struct sep_identify_data *sid;
709 char vendor[16], product[48], revision[16], fw[5];
711 sid = (struct sep_identify_data *)&dev_result->ident_data;
712 cam_strvis(vendor, sid->vendor_id,
713 sizeof(sid->vendor_id), sizeof(vendor));
714 cam_strvis(product, sid->product_id,
715 sizeof(sid->product_id), sizeof(product));
716 cam_strvis(revision, sid->product_rev,
717 sizeof(sid->product_rev), sizeof(revision));
718 cam_strvis(fw, sid->firmware_rev,
719 sizeof(sid->firmware_rev), sizeof(fw));
720 sprintf(tmpstr, "<%s %s %s %s>", vendor, product, revision, fw);
726 print_dev_mmcsd(struct device_match_result *dev_result, char *tmpstr)
729 struct ccb_dev_advinfo *advi;
730 struct cam_device *dev;
731 struct mmc_params mmc_ident_data;
733 dev = cam_open_btl(dev_result->path_id, dev_result->target_id,
734 dev_result->target_lun, O_RDWR, NULL);
736 warnx("%s", cam_errbuf);
740 ccb = cam_getccb(dev);
742 warnx("couldn't allocate CCB");
743 cam_close_device(dev);
748 advi->ccb_h.flags = CAM_DIR_IN;
749 advi->ccb_h.func_code = XPT_DEV_ADVINFO;
750 advi->flags = CDAI_FLAG_NONE;
751 advi->buftype = CDAI_TYPE_MMC_PARAMS;
752 advi->bufsiz = sizeof(struct mmc_params);
753 advi->buf = (uint8_t *)&mmc_ident_data;
755 if (cam_send_ccb(dev, ccb) < 0) {
756 warn("error sending XPT_DEV_ADVINFO CCB");
758 cam_close_device(dev);
762 if (strlen(mmc_ident_data.model) > 0) {
763 sprintf(tmpstr, "<%s>", mmc_ident_data.model);
765 sprintf(tmpstr, "<%s card>",
766 mmc_ident_data.card_features &
767 CARD_FEATURE_SDIO ? "SDIO" : "unknown");
771 cam_close_device(dev);
777 nvme_get_cdata(struct cam_device *dev, struct nvme_controller_data *cdata)
780 struct ccb_dev_advinfo *advi;
782 ccb = cam_getccb(dev);
784 warnx("couldn't allocate CCB");
785 cam_close_device(dev);
790 advi->ccb_h.flags = CAM_DIR_IN;
791 advi->ccb_h.func_code = XPT_DEV_ADVINFO;
792 advi->flags = CDAI_FLAG_NONE;
793 advi->buftype = CDAI_TYPE_NVME_CNTRL;
794 advi->bufsiz = sizeof(struct nvme_controller_data);
795 advi->buf = (uint8_t *)cdata;
797 if (cam_send_ccb(dev, ccb) < 0) {
798 warn("error sending XPT_DEV_ADVINFO CCB");
800 cam_close_device(dev);
803 if (advi->ccb_h.status != CAM_REQ_CMP) {
804 warnx("got CAM error %#x", advi->ccb_h.status);
806 cam_close_device(dev);
814 print_dev_nvme(struct device_match_result *dev_result, char *tmpstr)
816 struct cam_device *dev;
817 struct nvme_controller_data cdata;
818 char vendor[64], product[64];
820 dev = cam_open_btl(dev_result->path_id, dev_result->target_id,
821 dev_result->target_lun, O_RDWR, NULL);
823 warnx("%s", cam_errbuf);
827 if (nvme_get_cdata(dev, &cdata))
830 cam_strvis(vendor, cdata.mn, sizeof(cdata.mn), sizeof(vendor));
831 cam_strvis(product, cdata.fr, sizeof(cdata.fr), sizeof(product));
832 sprintf(tmpstr, "<%s %s>", vendor, product);
834 cam_close_device(dev);
840 testunitready(struct cam_device *device, int task_attr, int retry_count,
841 int timeout, int quiet)
846 ccb = cam_getccb(device);
848 scsi_test_unit_ready(&ccb->csio,
849 /* retries */ retry_count,
851 /* tag_action */ task_attr,
852 /* sense_len */ SSD_FULL_SIZE,
853 /* timeout */ timeout ? timeout : 5000);
855 /* Disable freezing the device queue */
856 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
858 if (arglist & CAM_ARG_ERR_RECOVER)
859 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
861 if (cam_send_ccb(device, ccb) < 0) {
863 warn("error sending TEST UNIT READY command");
868 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
870 fprintf(stdout, "Unit is ready\n");
873 fprintf(stdout, "Unit is not ready\n");
876 if (arglist & CAM_ARG_VERBOSE) {
877 cam_error_print(device, ccb, CAM_ESF_ALL,
878 CAM_EPF_ALL, stderr);
888 scsistart(struct cam_device *device, int startstop, int loadeject,
889 int task_attr, int retry_count, int timeout)
894 ccb = cam_getccb(device);
897 * If we're stopping, send an ordered tag so the drive in question
898 * will finish any previously queued writes before stopping. If
899 * the device isn't capable of tagged queueing, or if tagged
900 * queueing is turned off, the tag action is a no-op. We override
901 * the default simple tag, although this also has the effect of
902 * overriding the user's wishes if he wanted to specify a simple
906 && (task_attr == MSG_SIMPLE_Q_TAG))
907 task_attr = MSG_ORDERED_Q_TAG;
909 scsi_start_stop(&ccb->csio,
910 /* retries */ retry_count,
912 /* tag_action */ task_attr,
913 /* start/stop */ startstop,
914 /* load_eject */ loadeject,
916 /* sense_len */ SSD_FULL_SIZE,
917 /* timeout */ timeout ? timeout : 120000);
919 /* Disable freezing the device queue */
920 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
922 if (arglist & CAM_ARG_ERR_RECOVER)
923 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
925 if (cam_send_ccb(device, ccb) < 0) {
926 warn("error sending START STOP UNIT command");
931 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
933 fprintf(stdout, "Unit started successfully");
935 fprintf(stdout,", Media loaded\n");
937 fprintf(stdout,"\n");
939 fprintf(stdout, "Unit stopped successfully");
941 fprintf(stdout, ", Media ejected\n");
943 fprintf(stdout, "\n");
949 "Error received from start unit command\n");
952 "Error received from stop unit command\n");
954 if (arglist & CAM_ARG_VERBOSE) {
955 cam_error_print(device, ccb, CAM_ESF_ALL,
956 CAM_EPF_ALL, stderr);
966 scsidoinquiry(struct cam_device *device, int argc, char **argv,
967 char *combinedopt, int task_attr, int retry_count, int timeout)
972 while ((c = getopt(argc, argv, combinedopt)) != -1) {
975 arglist |= CAM_ARG_GET_STDINQ;
978 arglist |= CAM_ARG_GET_XFERRATE;
981 arglist |= CAM_ARG_GET_SERIAL;
989 * If the user didn't specify any inquiry options, he wants all of
992 if ((arglist & CAM_ARG_INQ_MASK) == 0)
993 arglist |= CAM_ARG_INQ_MASK;
995 if (arglist & CAM_ARG_GET_STDINQ)
996 error = scsiinquiry(device, task_attr, retry_count, timeout);
1001 if (arglist & CAM_ARG_GET_SERIAL)
1002 scsiserial(device, task_attr, retry_count, timeout);
1004 if (arglist & CAM_ARG_GET_XFERRATE)
1005 error = camxferrate(device);
1011 scsiinquiry(struct cam_device *device, int task_attr, int retry_count,
1015 struct scsi_inquiry_data *inq_buf;
1018 ccb = cam_getccb(device);
1021 warnx("couldn't allocate CCB");
1025 /* cam_getccb cleans up the header, caller has to zero the payload */
1026 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1028 inq_buf = (struct scsi_inquiry_data *)malloc(
1029 sizeof(struct scsi_inquiry_data));
1031 if (inq_buf == NULL) {
1033 warnx("can't malloc memory for inquiry\n");
1036 bzero(inq_buf, sizeof(*inq_buf));
1039 * Note that although the size of the inquiry buffer is the full
1040 * 256 bytes specified in the SCSI spec, we only tell the device
1041 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
1042 * two reasons for this:
1044 * - The SCSI spec says that when a length field is only 1 byte,
1045 * a value of 0 will be interpreted as 256. Therefore
1046 * scsi_inquiry() will convert an inq_len (which is passed in as
1047 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
1048 * to 0. Evidently, very few devices meet the spec in that
1049 * regard. Some devices, like many Seagate disks, take the 0 as
1050 * 0, and don't return any data. One Pioneer DVD-R drive
1051 * returns more data than the command asked for.
1053 * So, since there are numerous devices that just don't work
1054 * right with the full inquiry size, we don't send the full size.
1056 * - The second reason not to use the full inquiry data length is
1057 * that we don't need it here. The only reason we issue a
1058 * standard inquiry is to get the vendor name, device name,
1059 * and revision so scsi_print_inquiry() can print them.
1061 * If, at some point in the future, more inquiry data is needed for
1062 * some reason, this code should use a procedure similar to the
1063 * probe code. i.e., issue a short inquiry, and determine from
1064 * the additional length passed back from the device how much
1065 * inquiry data the device supports. Once the amount the device
1066 * supports is determined, issue an inquiry for that amount and no
1071 scsi_inquiry(&ccb->csio,
1072 /* retries */ retry_count,
1074 /* tag_action */ task_attr,
1075 /* inq_buf */ (u_int8_t *)inq_buf,
1076 /* inq_len */ SHORT_INQUIRY_LENGTH,
1079 /* sense_len */ SSD_FULL_SIZE,
1080 /* timeout */ timeout ? timeout : 5000);
1082 /* Disable freezing the device queue */
1083 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1085 if (arglist & CAM_ARG_ERR_RECOVER)
1086 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1088 if (cam_send_ccb(device, ccb) < 0) {
1089 warn("error sending INQUIRY command");
1094 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1097 if (arglist & CAM_ARG_VERBOSE) {
1098 cam_error_print(device, ccb, CAM_ESF_ALL,
1099 CAM_EPF_ALL, stderr);
1110 fprintf(stdout, "%s%d: ", device->device_name,
1111 device->dev_unit_num);
1112 scsi_print_inquiry(inq_buf);
1120 scsiserial(struct cam_device *device, int task_attr, int retry_count,
1124 struct scsi_vpd_unit_serial_number *serial_buf;
1125 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
1128 ccb = cam_getccb(device);
1131 warnx("couldn't allocate CCB");
1135 /* cam_getccb cleans up the header, caller has to zero the payload */
1136 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1138 serial_buf = (struct scsi_vpd_unit_serial_number *)
1139 malloc(sizeof(*serial_buf));
1141 if (serial_buf == NULL) {
1143 warnx("can't malloc memory for serial number");
1147 scsi_inquiry(&ccb->csio,
1148 /*retries*/ retry_count,
1150 /* tag_action */ task_attr,
1151 /* inq_buf */ (u_int8_t *)serial_buf,
1152 /* inq_len */ sizeof(*serial_buf),
1154 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
1155 /* sense_len */ SSD_FULL_SIZE,
1156 /* timeout */ timeout ? timeout : 5000);
1158 /* Disable freezing the device queue */
1159 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1161 if (arglist & CAM_ARG_ERR_RECOVER)
1162 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1164 if (cam_send_ccb(device, ccb) < 0) {
1165 warn("error sending INQUIRY command");
1171 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1174 if (arglist & CAM_ARG_VERBOSE) {
1175 cam_error_print(device, ccb, CAM_ESF_ALL,
1176 CAM_EPF_ALL, stderr);
1187 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
1188 serial_num[serial_buf->length] = '\0';
1190 if ((arglist & CAM_ARG_GET_STDINQ)
1191 || (arglist & CAM_ARG_GET_XFERRATE))
1192 fprintf(stdout, "%s%d: Serial Number ",
1193 device->device_name, device->dev_unit_num);
1195 fprintf(stdout, "%.60s\n", serial_num);
1203 camxferrate(struct cam_device *device)
1205 struct ccb_pathinq cpi;
1207 u_int32_t speed = 0;
1212 if ((retval = get_cpi(device, &cpi)) != 0)
1215 ccb = cam_getccb(device);
1218 warnx("couldn't allocate CCB");
1222 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cts);
1224 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1225 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
1227 if (((retval = cam_send_ccb(device, ccb)) < 0)
1228 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1229 const char error_string[] = "error getting transfer settings";
1234 warnx(error_string);
1236 if (arglist & CAM_ARG_VERBOSE)
1237 cam_error_print(device, ccb, CAM_ESF_ALL,
1238 CAM_EPF_ALL, stderr);
1242 goto xferrate_bailout;
1246 speed = cpi.base_transfer_speed;
1248 if (ccb->cts.transport == XPORT_SPI) {
1249 struct ccb_trans_settings_spi *spi =
1250 &ccb->cts.xport_specific.spi;
1252 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
1253 freq = scsi_calc_syncsrate(spi->sync_period);
1256 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
1257 speed *= (0x01 << spi->bus_width);
1259 } else if (ccb->cts.transport == XPORT_FC) {
1260 struct ccb_trans_settings_fc *fc =
1261 &ccb->cts.xport_specific.fc;
1263 if (fc->valid & CTS_FC_VALID_SPEED)
1264 speed = fc->bitrate;
1265 } else if (ccb->cts.transport == XPORT_SAS) {
1266 struct ccb_trans_settings_sas *sas =
1267 &ccb->cts.xport_specific.sas;
1269 if (sas->valid & CTS_SAS_VALID_SPEED)
1270 speed = sas->bitrate;
1271 } else if (ccb->cts.transport == XPORT_ATA) {
1272 struct ccb_trans_settings_pata *pata =
1273 &ccb->cts.xport_specific.ata;
1275 if (pata->valid & CTS_ATA_VALID_MODE)
1276 speed = ata_mode2speed(pata->mode);
1277 } else if (ccb->cts.transport == XPORT_SATA) {
1278 struct ccb_trans_settings_sata *sata =
1279 &ccb->cts.xport_specific.sata;
1281 if (sata->valid & CTS_SATA_VALID_REVISION)
1282 speed = ata_revision2speed(sata->revision);
1287 fprintf(stdout, "%s%d: %d.%03dMB/s transfers",
1288 device->device_name, device->dev_unit_num,
1291 fprintf(stdout, "%s%d: %dKB/s transfers",
1292 device->device_name, device->dev_unit_num,
1296 if (ccb->cts.transport == XPORT_SPI) {
1297 struct ccb_trans_settings_spi *spi =
1298 &ccb->cts.xport_specific.spi;
1300 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1301 && (spi->sync_offset != 0))
1302 fprintf(stdout, " (%d.%03dMHz, offset %d", freq / 1000,
1303 freq % 1000, spi->sync_offset);
1305 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1306 && (spi->bus_width > 0)) {
1307 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1308 && (spi->sync_offset != 0)) {
1309 fprintf(stdout, ", ");
1311 fprintf(stdout, " (");
1313 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
1314 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1315 && (spi->sync_offset != 0)) {
1316 fprintf(stdout, ")");
1318 } else if (ccb->cts.transport == XPORT_ATA) {
1319 struct ccb_trans_settings_pata *pata =
1320 &ccb->cts.xport_specific.ata;
1323 if (pata->valid & CTS_ATA_VALID_MODE)
1324 printf("%s, ", ata_mode2string(pata->mode));
1325 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
1326 printf("ATAPI %dbytes, ", pata->atapi);
1327 if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
1328 printf("PIO %dbytes", pata->bytecount);
1330 } else if (ccb->cts.transport == XPORT_SATA) {
1331 struct ccb_trans_settings_sata *sata =
1332 &ccb->cts.xport_specific.sata;
1335 if (sata->valid & CTS_SATA_VALID_REVISION)
1336 printf("SATA %d.x, ", sata->revision);
1339 if (sata->valid & CTS_SATA_VALID_MODE)
1340 printf("%s, ", ata_mode2string(sata->mode));
1341 if ((sata->valid & CTS_SATA_VALID_ATAPI) && sata->atapi != 0)
1342 printf("ATAPI %dbytes, ", sata->atapi);
1343 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1344 printf("PIO %dbytes", sata->bytecount);
1348 if (ccb->cts.protocol == PROTO_SCSI) {
1349 struct ccb_trans_settings_scsi *scsi =
1350 &ccb->cts.proto_specific.scsi;
1351 if (scsi->valid & CTS_SCSI_VALID_TQ) {
1352 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
1353 fprintf(stdout, ", Command Queueing Enabled");
1358 fprintf(stdout, "\n");
1368 atahpa_print(struct ata_params *parm, u_int64_t hpasize, int header)
1370 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1371 ((u_int32_t)parm->lba_size_2 << 16);
1373 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1374 ((u_int64_t)parm->lba_size48_2 << 16) |
1375 ((u_int64_t)parm->lba_size48_3 << 32) |
1376 ((u_int64_t)parm->lba_size48_4 << 48);
1380 "Support Enabled Value\n");
1383 printf("Host Protected Area (HPA) ");
1384 if (parm->support.command1 & ATA_SUPPORT_PROTECTED) {
1385 u_int64_t lba = lbasize48 ? lbasize48 : lbasize;
1386 printf("yes %s %ju/%ju\n", (hpasize > lba) ? "yes" : "no ",
1389 printf("HPA - Security ");
1390 if (parm->support.command2 & ATA_SUPPORT_MAXSECURITY)
1391 printf("yes %s\n", (parm->enabled.command2 &
1392 ATA_SUPPORT_MAXSECURITY) ? "yes" : "no ");
1401 ataama_print(struct ata_params *parm, u_int64_t nativesize, int header)
1403 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1404 ((u_int32_t)parm->lba_size_2 << 16);
1406 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1407 ((u_int64_t)parm->lba_size48_2 << 16) |
1408 ((u_int64_t)parm->lba_size48_3 << 32) |
1409 ((u_int64_t)parm->lba_size48_4 << 48);
1413 "Support Enabled Value\n");
1416 printf("Accessible Max Address Config ");
1417 if (parm->support2 & ATA_SUPPORT_AMAX_ADDR) {
1418 u_int64_t lba = lbasize48 ? lbasize48 : lbasize;
1419 printf("yes %s %ju/%ju\n",
1420 (nativesize > lba) ? "yes" : "no ", lba, nativesize);
1427 atasata(struct ata_params *parm)
1431 if (parm->satacapabilities != 0xffff &&
1432 parm->satacapabilities != 0x0000)
1439 atacapprint(struct ata_params *parm)
1442 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1443 ((u_int32_t)parm->lba_size_2 << 16);
1445 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1446 ((u_int64_t)parm->lba_size48_2 << 16) |
1447 ((u_int64_t)parm->lba_size48_3 << 32) |
1448 ((u_int64_t)parm->lba_size48_4 << 48);
1451 printf("protocol ");
1452 proto = (parm->config == ATA_PROTO_CFA) ? "CFA" :
1453 (parm->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
1454 if (ata_version(parm->version_major) == 0) {
1455 printf("%s", proto);
1456 } else if (ata_version(parm->version_major) <= 7) {
1457 printf("%s-%d", proto,
1458 ata_version(parm->version_major));
1459 } else if (ata_version(parm->version_major) == 8) {
1460 printf("%s8-ACS", proto);
1463 ata_version(parm->version_major) - 7, proto);
1465 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
1466 if (parm->satacapabilities & ATA_SATA_GEN3)
1467 printf(" SATA 3.x\n");
1468 else if (parm->satacapabilities & ATA_SATA_GEN2)
1469 printf(" SATA 2.x\n");
1470 else if (parm->satacapabilities & ATA_SATA_GEN1)
1471 printf(" SATA 1.x\n");
1477 printf("device model %.40s\n", parm->model);
1478 printf("firmware revision %.8s\n", parm->revision);
1479 printf("serial number %.20s\n", parm->serial);
1480 if (parm->enabled.extension & ATA_SUPPORT_64BITWWN) {
1481 printf("WWN %04x%04x%04x%04x\n",
1482 parm->wwn[0], parm->wwn[1], parm->wwn[2], parm->wwn[3]);
1484 printf("additional product id %.8s\n", parm->product_id);
1485 if (parm->enabled.extension & ATA_SUPPORT_MEDIASN) {
1486 printf("media serial number %.30s\n",
1487 parm->media_serial);
1490 printf("cylinders %d\n", parm->cylinders);
1491 printf("heads %d\n", parm->heads);
1492 printf("sectors/track %d\n", parm->sectors);
1493 printf("sector size logical %u, physical %lu, offset %lu\n",
1494 ata_logical_sector_size(parm),
1495 (unsigned long)ata_physical_sector_size(parm),
1496 (unsigned long)ata_logical_sector_offset(parm));
1498 if (parm->config == ATA_PROTO_CFA ||
1499 (parm->support.command2 & ATA_SUPPORT_CFA))
1500 printf("CFA supported\n");
1502 printf("LBA%ssupported ",
1503 parm->capabilities1 & ATA_SUPPORT_LBA ? " " : " not ");
1505 printf("%d sectors\n", lbasize);
1509 printf("LBA48%ssupported ",
1510 parm->support.command2 & ATA_SUPPORT_ADDRESS48 ? " " : " not ");
1512 printf("%ju sectors\n", (uintmax_t)lbasize48);
1516 printf("PIO supported PIO");
1517 switch (ata_max_pmode(parm)) {
1533 if ((parm->capabilities1 & ATA_SUPPORT_IORDY) == 0)
1534 printf(" w/o IORDY");
1537 printf("DMA%ssupported ",
1538 parm->capabilities1 & ATA_SUPPORT_DMA ? " " : " not ");
1539 if (parm->capabilities1 & ATA_SUPPORT_DMA) {
1540 if (parm->mwdmamodes & 0xff) {
1542 if (parm->mwdmamodes & 0x04)
1544 else if (parm->mwdmamodes & 0x02)
1546 else if (parm->mwdmamodes & 0x01)
1550 if ((parm->atavalid & ATA_FLAG_88) &&
1551 (parm->udmamodes & 0xff)) {
1553 if (parm->udmamodes & 0x40)
1555 else if (parm->udmamodes & 0x20)
1557 else if (parm->udmamodes & 0x10)
1559 else if (parm->udmamodes & 0x08)
1561 else if (parm->udmamodes & 0x04)
1563 else if (parm->udmamodes & 0x02)
1565 else if (parm->udmamodes & 0x01)
1572 if (parm->media_rotation_rate == 1) {
1573 printf("media RPM non-rotating\n");
1574 } else if (parm->media_rotation_rate >= 0x0401 &&
1575 parm->media_rotation_rate <= 0xFFFE) {
1576 printf("media RPM %d\n",
1577 parm->media_rotation_rate);
1580 printf("Zoned-Device Commands ");
1581 switch (parm->support3 & ATA_SUPPORT_ZONE_MASK) {
1582 case ATA_SUPPORT_ZONE_DEV_MANAGED:
1583 printf("device managed\n");
1585 case ATA_SUPPORT_ZONE_HOST_AWARE:
1586 printf("host aware\n");
1593 "Support Enabled Value Vendor\n");
1594 printf("read ahead %s %s\n",
1595 parm->support.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no",
1596 parm->enabled.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no");
1597 printf("write cache %s %s\n",
1598 parm->support.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no",
1599 parm->enabled.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no");
1600 printf("flush cache %s %s\n",
1601 parm->support.command2 & ATA_SUPPORT_FLUSHCACHE ? "yes" : "no",
1602 parm->enabled.command2 & ATA_SUPPORT_FLUSHCACHE ? "yes" : "no");
1603 printf("Native Command Queuing (NCQ) ");
1604 if (atasata(parm) && (parm->satacapabilities & ATA_SUPPORT_NCQ)) {
1605 printf("yes %d tags\n",
1606 ATA_QUEUE_LEN(parm->queue) + 1);
1607 printf("NCQ Priority Information %s\n",
1608 parm->satacapabilities & ATA_SUPPORT_NCQ_PRIO ?
1610 printf("NCQ Non-Data Command %s\n",
1611 parm->satacapabilities2 & ATA_SUPPORT_NCQ_NON_DATA ?
1613 printf("NCQ Streaming %s\n",
1614 parm->satacapabilities2 & ATA_SUPPORT_NCQ_STREAM ?
1616 printf("Receive & Send FPDMA Queued %s\n",
1617 parm->satacapabilities2 & ATA_SUPPORT_RCVSND_FPDMA_QUEUED ?
1619 printf("NCQ Autosense %s\n",
1620 parm->satasupport & ATA_SUPPORT_NCQ_AUTOSENSE ?
1625 printf("SMART %s %s\n",
1626 parm->support.command1 & ATA_SUPPORT_SMART ? "yes" : "no",
1627 parm->enabled.command1 & ATA_SUPPORT_SMART ? "yes" : "no");
1628 printf("security %s %s\n",
1629 parm->support.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no",
1630 parm->enabled.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no");
1631 printf("power management %s %s\n",
1632 parm->support.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no",
1633 parm->enabled.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no");
1634 printf("microcode download %s %s\n",
1635 parm->support.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no",
1636 parm->enabled.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no");
1637 printf("advanced power management %s %s",
1638 parm->support.command2 & ATA_SUPPORT_APM ? "yes" : "no",
1639 parm->enabled.command2 & ATA_SUPPORT_APM ? "yes" : "no");
1640 if (parm->support.command2 & ATA_SUPPORT_APM) {
1641 printf(" %d/0x%02X\n",
1642 parm->apm_value & 0xff, parm->apm_value & 0xff);
1645 printf("automatic acoustic management %s %s",
1646 parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
1647 parm->enabled.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no");
1648 if (parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC) {
1649 printf(" %d/0x%02X %d/0x%02X\n",
1650 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1651 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1652 ATA_ACOUSTIC_VENDOR(parm->acoustic),
1653 ATA_ACOUSTIC_VENDOR(parm->acoustic));
1656 printf("media status notification %s %s\n",
1657 parm->support.command2 & ATA_SUPPORT_NOTIFY ? "yes" : "no",
1658 parm->enabled.command2 & ATA_SUPPORT_NOTIFY ? "yes" : "no");
1659 printf("power-up in Standby %s %s\n",
1660 parm->support.command2 & ATA_SUPPORT_STANDBY ? "yes" : "no",
1661 parm->enabled.command2 & ATA_SUPPORT_STANDBY ? "yes" : "no");
1662 printf("write-read-verify %s %s",
1663 parm->support2 & ATA_SUPPORT_WRITEREADVERIFY ? "yes" : "no",
1664 parm->enabled2 & ATA_SUPPORT_WRITEREADVERIFY ? "yes" : "no");
1665 if (parm->support2 & ATA_SUPPORT_WRITEREADVERIFY) {
1666 printf(" %d/0x%x\n",
1667 parm->wrv_mode, parm->wrv_mode);
1670 printf("unload %s %s\n",
1671 parm->support.extension & ATA_SUPPORT_UNLOAD ? "yes" : "no",
1672 parm->enabled.extension & ATA_SUPPORT_UNLOAD ? "yes" : "no");
1673 printf("general purpose logging %s %s\n",
1674 parm->support.extension & ATA_SUPPORT_GENLOG ? "yes" : "no",
1675 parm->enabled.extension & ATA_SUPPORT_GENLOG ? "yes" : "no");
1676 printf("free-fall %s %s\n",
1677 parm->support2 & ATA_SUPPORT_FREEFALL ? "yes" : "no",
1678 parm->enabled2 & ATA_SUPPORT_FREEFALL ? "yes" : "no");
1679 printf("sense data reporting %s %s\n",
1680 parm->support2 & ATA_SUPPORT_SENSE_REPORT ? "yes" : "no",
1681 parm->enabled2 & ATA_SUPPORT_SENSE_REPORT ? "yes" : "no");
1682 printf("extended power conditions %s %s\n",
1683 parm->support2 & ATA_SUPPORT_EPC ? "yes" : "no",
1684 parm->enabled2 & ATA_SUPPORT_EPC ? "yes" : "no");
1685 printf("device statistics notification %s %s\n",
1686 parm->support2 & ATA_SUPPORT_DSN ? "yes" : "no",
1687 parm->enabled2 & ATA_SUPPORT_DSN ? "yes" : "no");
1688 printf("Data Set Management (DSM/TRIM) ");
1689 if (parm->support_dsm & ATA_SUPPORT_DSM_TRIM) {
1691 printf("DSM - max 512byte blocks ");
1692 if (parm->max_dsm_blocks == 0x00)
1693 printf("yes not specified\n");
1696 parm->max_dsm_blocks);
1698 printf("DSM - deterministic read ");
1699 if (parm->support3 & ATA_SUPPORT_DRAT) {
1700 if (parm->support3 & ATA_SUPPORT_RZAT)
1701 printf("yes zeroed\n");
1703 printf("yes any value\n");
1710 printf("Trusted Computing %s\n",
1711 ((parm->tcg & 0xc000) == 0x4000) && (parm->tcg & ATA_SUPPORT_TCG) ?
1713 printf("encrypts all user data %s\n",
1714 parm->support3 & ATA_ENCRYPTS_ALL_USER_DATA ? "yes" : "no");
1715 printf("Sanitize ");
1716 if (parm->multi & ATA_SUPPORT_SANITIZE) {
1717 printf("yes\t\t%s%s%s\n",
1718 parm->multi & ATA_SUPPORT_BLOCK_ERASE_EXT ? "block, " : "",
1719 parm->multi & ATA_SUPPORT_OVERWRITE_EXT ? "overwrite, " : "",
1720 parm->multi & ATA_SUPPORT_CRYPTO_SCRAMBLE_EXT ? "crypto" : "");
1721 printf("Sanitize - commands allowed %s\n",
1722 parm->multi & ATA_SUPPORT_SANITIZE_ALLOWED ? "yes" : "no");
1723 printf("Sanitize - antifreeze lock %s\n",
1724 parm->multi & ATA_SUPPORT_ANTIFREEZE_LOCK_EXT ? "yes" : "no");
1731 scsi_cam_pass_16_send(struct cam_device *device, union ccb *ccb)
1733 struct ata_pass_16 *ata_pass_16;
1734 struct ata_cmd ata_cmd;
1736 ata_pass_16 = (struct ata_pass_16 *)ccb->csio.cdb_io.cdb_bytes;
1737 ata_cmd.command = ata_pass_16->command;
1738 ata_cmd.control = ata_pass_16->control;
1739 ata_cmd.features = ata_pass_16->features;
1741 if (arglist & CAM_ARG_VERBOSE) {
1742 warnx("sending ATA %s via pass_16 with timeout of %u msecs",
1743 ata_op_string(&ata_cmd),
1744 ccb->csio.ccb_h.timeout);
1747 /* Disable freezing the device queue */
1748 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1750 if (arglist & CAM_ARG_ERR_RECOVER)
1751 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1753 if (cam_send_ccb(device, ccb) < 0) {
1754 warn("error sending ATA %s via pass_16", ata_op_string(&ata_cmd));
1759 * Consider any non-CAM_REQ_CMP status as error and report it here,
1760 * unless caller set AP_FLAG_CHK_COND, in which case it is reponsible.
1762 if (!(ata_pass_16->flags & AP_FLAG_CHK_COND) &&
1763 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1764 warnx("ATA %s via pass_16 failed", ata_op_string(&ata_cmd));
1765 if (arglist & CAM_ARG_VERBOSE) {
1766 cam_error_print(device, ccb, CAM_ESF_ALL,
1767 CAM_EPF_ALL, stderr);
1777 ata_cam_send(struct cam_device *device, union ccb *ccb)
1779 if (arglist & CAM_ARG_VERBOSE) {
1780 warnx("sending ATA %s with timeout of %u msecs",
1781 ata_op_string(&(ccb->ataio.cmd)),
1782 ccb->ataio.ccb_h.timeout);
1785 /* Disable freezing the device queue */
1786 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1788 if (arglist & CAM_ARG_ERR_RECOVER)
1789 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1791 if (cam_send_ccb(device, ccb) < 0) {
1792 warn("error sending ATA %s", ata_op_string(&(ccb->ataio.cmd)));
1797 * Consider any non-CAM_REQ_CMP status as error and report it here,
1798 * unless caller set AP_FLAG_CHK_COND, in which case it is reponsible.
1800 if (!(ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT) &&
1801 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1802 warnx("ATA %s failed", ata_op_string(&(ccb->ataio.cmd)));
1803 if (arglist & CAM_ARG_VERBOSE) {
1804 cam_error_print(device, ccb, CAM_ESF_ALL,
1805 CAM_EPF_ALL, stderr);
1814 ata_do_pass_16(struct cam_device *device, union ccb *ccb, int retries,
1815 u_int32_t flags, u_int8_t protocol, u_int8_t ata_flags,
1816 u_int8_t tag_action, u_int8_t command, u_int16_t features,
1817 u_int64_t lba, u_int16_t sector_count, u_int8_t *data_ptr,
1818 u_int16_t dxfer_len, int timeout)
1820 if (data_ptr != NULL) {
1821 if (flags & CAM_DIR_OUT)
1822 ata_flags |= AP_FLAG_TDIR_TO_DEV;
1824 ata_flags |= AP_FLAG_TDIR_FROM_DEV;
1826 ata_flags |= AP_FLAG_TLEN_NO_DATA;
1829 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1831 scsi_ata_pass_16(&ccb->csio,
1845 /*sense_len*/SSD_FULL_SIZE,
1848 return scsi_cam_pass_16_send(device, ccb);
1852 ata_try_pass_16(struct cam_device *device)
1854 struct ccb_pathinq cpi;
1856 if (get_cpi(device, &cpi) != 0) {
1857 warnx("couldn't get CPI");
1861 if (cpi.protocol == PROTO_SCSI) {
1862 /* possibly compatible with pass_16 */
1866 /* likely not compatible with pass_16 */
1871 ata_do_cmd(struct cam_device *device, union ccb *ccb, int retries,
1872 u_int32_t flags, u_int8_t protocol, u_int8_t ata_flags,
1873 u_int8_t tag_action, u_int8_t command, u_int16_t features,
1874 u_int64_t lba, u_int16_t sector_count, u_int8_t *data_ptr,
1875 u_int16_t dxfer_len, int timeout, int force48bit)
1879 retval = ata_try_pass_16(device);
1884 return (ata_do_pass_16(device, ccb, retries, flags, protocol,
1885 ata_flags, tag_action, command, features,
1886 lba, sector_count, data_ptr, dxfer_len,
1890 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->ataio);
1891 cam_fill_ataio(&ccb->ataio,
1900 if (force48bit || lba > ATA_MAX_28BIT_LBA)
1901 ata_48bit_cmd(&ccb->ataio, command, features, lba, sector_count);
1903 ata_28bit_cmd(&ccb->ataio, command, features, lba, sector_count);
1905 if (ata_flags & AP_FLAG_CHK_COND)
1906 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
1908 return ata_cam_send(device, ccb);
1912 dump_data(uint16_t *ptr, uint32_t len)
1916 for (i = 0; i < len / 2; i++) {
1918 printf(" %3d: ", i);
1919 printf("%04hx ", ptr[i]);
1928 atahpa_proc_resp(struct cam_device *device, union ccb *ccb, u_int64_t *hpasize)
1930 uint8_t error = 0, ata_device = 0, status = 0;
1935 retval = get_ata_status(device, ccb, &error, &count, &lba, &ata_device,
1938 if (arglist & CAM_ARG_VERBOSE) {
1939 cam_error_print(device, ccb, CAM_ESF_ALL,
1940 CAM_EPF_ALL, stderr);
1942 warnx("Can't get ATA command status");
1946 if (status & ATA_STATUS_ERROR) {
1947 if (arglist & CAM_ARG_VERBOSE) {
1948 cam_error_print(device, ccb, CAM_ESF_ALL,
1949 CAM_EPF_ALL, stderr);
1952 if (error & ATA_ERROR_ID_NOT_FOUND) {
1953 warnx("Max address has already been set since "
1954 "last power-on or hardware reset");
1955 } else if (hpasize == NULL)
1956 warnx("Command failed with ATA error");
1961 if (hpasize != NULL) {
1962 if (retval == 2 || retval == 6)
1971 ata_read_native_max(struct cam_device *device, int retry_count,
1972 u_int32_t timeout, union ccb *ccb,
1973 struct ata_params *parm, u_int64_t *hpasize)
1979 is48bit = parm->support.command2 & ATA_SUPPORT_ADDRESS48;
1980 protocol = AP_PROTO_NON_DATA;
1983 cmd = ATA_READ_NATIVE_MAX_ADDRESS48;
1984 protocol |= AP_EXTEND;
1986 cmd = ATA_READ_NATIVE_MAX_ADDRESS;
1989 error = ata_do_cmd(device,
1992 /*flags*/CAM_DIR_NONE,
1993 /*protocol*/protocol,
1994 /*ata_flags*/AP_FLAG_CHK_COND,
1995 /*tag_action*/MSG_SIMPLE_Q_TAG,
2002 timeout ? timeout : 5000,
2008 return atahpa_proc_resp(device, ccb, hpasize);
2012 atahpa_set_max(struct cam_device *device, int retry_count,
2013 u_int32_t timeout, union ccb *ccb,
2014 int is48bit, u_int64_t maxsize, int persist)
2020 protocol = AP_PROTO_NON_DATA;
2023 cmd = ATA_SET_MAX_ADDRESS48;
2024 protocol |= AP_EXTEND;
2026 cmd = ATA_SET_MAX_ADDRESS;
2029 /* lba's are zero indexed so the max lba is requested max - 1 */
2033 error = ata_do_cmd(device,
2036 /*flags*/CAM_DIR_NONE,
2037 /*protocol*/protocol,
2038 /*ata_flags*/AP_FLAG_CHK_COND,
2039 /*tag_action*/MSG_SIMPLE_Q_TAG,
2041 /*features*/ATA_HPA_FEAT_MAX_ADDR,
2043 /*sector_count*/persist,
2046 timeout ? timeout : 1000,
2052 return atahpa_proc_resp(device, ccb, NULL);
2056 atahpa_password(struct cam_device *device, int retry_count,
2057 u_int32_t timeout, union ccb *ccb,
2058 int is48bit, struct ata_set_max_pwd *pwd)
2063 protocol = AP_PROTO_PIO_OUT;
2064 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2066 return (ata_do_cmd(device,
2069 /*flags*/CAM_DIR_OUT,
2070 /*protocol*/protocol,
2071 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2072 AP_FLAG_TLEN_SECT_CNT,
2073 /*tag_action*/MSG_SIMPLE_Q_TAG,
2075 /*features*/ATA_HPA_FEAT_SET_PWD,
2077 /*sector_count*/sizeof(*pwd) / 512,
2078 /*data_ptr*/(u_int8_t*)pwd,
2079 /*dxfer_len*/sizeof(*pwd),
2080 timeout ? timeout : 1000,
2085 atahpa_lock(struct cam_device *device, int retry_count,
2086 u_int32_t timeout, union ccb *ccb, int is48bit)
2091 protocol = AP_PROTO_NON_DATA;
2092 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2094 return (ata_do_cmd(device,
2097 /*flags*/CAM_DIR_NONE,
2098 /*protocol*/protocol,
2100 /*tag_action*/MSG_SIMPLE_Q_TAG,
2102 /*features*/ATA_HPA_FEAT_LOCK,
2107 timeout ? timeout : 1000,
2112 atahpa_unlock(struct cam_device *device, int retry_count,
2113 u_int32_t timeout, union ccb *ccb,
2114 int is48bit, struct ata_set_max_pwd *pwd)
2119 protocol = AP_PROTO_PIO_OUT;
2120 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2122 return (ata_do_cmd(device,
2125 /*flags*/CAM_DIR_OUT,
2126 /*protocol*/protocol,
2127 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2128 AP_FLAG_TLEN_SECT_CNT,
2129 /*tag_action*/MSG_SIMPLE_Q_TAG,
2131 /*features*/ATA_HPA_FEAT_UNLOCK,
2133 /*sector_count*/sizeof(*pwd) / 512,
2134 /*data_ptr*/(u_int8_t*)pwd,
2135 /*dxfer_len*/sizeof(*pwd),
2136 timeout ? timeout : 1000,
2141 atahpa_freeze_lock(struct cam_device *device, int retry_count,
2142 u_int32_t timeout, union ccb *ccb, int is48bit)
2147 protocol = AP_PROTO_NON_DATA;
2148 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2150 return (ata_do_cmd(device,
2153 /*flags*/CAM_DIR_NONE,
2154 /*protocol*/protocol,
2156 /*tag_action*/MSG_SIMPLE_Q_TAG,
2158 /*features*/ATA_HPA_FEAT_FREEZE,
2163 timeout ? timeout : 1000,
2168 ata_get_native_max(struct cam_device *device, int retry_count,
2169 u_int32_t timeout, union ccb *ccb,
2170 u_int64_t *nativesize)
2174 error = ata_do_cmd(device,
2177 /*flags*/CAM_DIR_NONE,
2178 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2179 /*ata_flags*/AP_FLAG_CHK_COND,
2180 /*tag_action*/MSG_SIMPLE_Q_TAG,
2181 /*command*/ATA_AMAX_ADDR,
2182 /*features*/ATA_AMAX_ADDR_GET,
2187 timeout ? timeout : 30 * 1000,
2193 return atahpa_proc_resp(device, ccb, nativesize);
2197 ataama_set(struct cam_device *device, int retry_count,
2198 u_int32_t timeout, union ccb *ccb, u_int64_t maxsize)
2202 /* lba's are zero indexed so the max lba is requested max - 1 */
2206 error = ata_do_cmd(device,
2209 /*flags*/CAM_DIR_NONE,
2210 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2211 /*ata_flags*/AP_FLAG_CHK_COND,
2212 /*tag_action*/MSG_SIMPLE_Q_TAG,
2213 /*command*/ATA_AMAX_ADDR,
2214 /*features*/ATA_AMAX_ADDR_SET,
2219 timeout ? timeout : 30 * 1000,
2225 return atahpa_proc_resp(device, ccb, NULL);
2229 ataama_freeze(struct cam_device *device, int retry_count,
2230 u_int32_t timeout, union ccb *ccb)
2233 return (ata_do_cmd(device,
2236 /*flags*/CAM_DIR_NONE,
2237 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2239 /*tag_action*/MSG_SIMPLE_Q_TAG,
2240 /*command*/ATA_AMAX_ADDR,
2241 /*features*/ATA_AMAX_ADDR_FREEZE,
2246 timeout ? timeout : 30 * 1000,
2251 ata_do_identify(struct cam_device *device, int retry_count, int timeout,
2252 union ccb *ccb, struct ata_params** ident_bufp)
2254 struct ata_params *ident_buf;
2255 struct ccb_pathinq cpi;
2256 struct ccb_getdev cgd;
2259 u_int8_t command, retry_command;
2261 if (get_cpi(device, &cpi) != 0) {
2262 warnx("couldn't get CPI");
2266 /* Neither PROTO_ATAPI or PROTO_SATAPM are used in cpi.protocol */
2267 if (cpi.protocol == PROTO_ATA) {
2268 if (get_cgd(device, &cgd) != 0) {
2269 warnx("couldn't get CGD");
2273 command = (cgd.protocol == PROTO_ATA) ?
2274 ATA_ATA_IDENTIFY : ATA_ATAPI_IDENTIFY;
2277 /* We don't know which for sure so try both */
2278 command = ATA_ATA_IDENTIFY;
2279 retry_command = ATA_ATAPI_IDENTIFY;
2282 ptr = (uint16_t *)calloc(1, sizeof(struct ata_params));
2284 warnx("can't calloc memory for identify\n");
2289 error = ata_do_cmd(device,
2291 /*retries*/retry_count,
2292 /*flags*/CAM_DIR_IN,
2293 /*protocol*/AP_PROTO_PIO_IN,
2294 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2295 AP_FLAG_TLEN_SECT_CNT,
2296 /*tag_action*/MSG_SIMPLE_Q_TAG,
2300 /*sector_count*/sizeof(struct ata_params) / 512,
2301 /*data_ptr*/(u_int8_t *)ptr,
2302 /*dxfer_len*/sizeof(struct ata_params),
2303 /*timeout*/timeout ? timeout : 30 * 1000,
2307 if (retry_command != 0) {
2308 command = retry_command;
2316 ident_buf = (struct ata_params *)ptr;
2317 ata_param_fixup(ident_buf);
2320 for (i = 0; i < sizeof(struct ata_params) / 2; i++) {
2325 /* check for invalid (all zero) response */
2327 warnx("Invalid identify response detected");
2332 *ident_bufp = ident_buf;
2339 ataidentify(struct cam_device *device, int retry_count, int timeout)
2342 struct ata_params *ident_buf;
2343 u_int64_t hpasize = 0, nativesize = 0;
2345 if ((ccb = cam_getccb(device)) == NULL) {
2346 warnx("couldn't allocate CCB");
2350 if (ata_do_identify(device, retry_count, timeout, ccb, &ident_buf) != 0) {
2355 if (arglist & CAM_ARG_VERBOSE) {
2356 printf("%s%d: Raw identify data:\n",
2357 device->device_name, device->dev_unit_num);
2358 dump_data((uint16_t *)ident_buf, sizeof(struct ata_params));
2361 if (ident_buf->support.command1 & ATA_SUPPORT_PROTECTED) {
2362 ata_read_native_max(device, retry_count, timeout, ccb,
2363 ident_buf, &hpasize);
2365 if (ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR) {
2366 ata_get_native_max(device, retry_count, timeout, ccb,
2370 printf("%s%d: ", device->device_name, device->dev_unit_num);
2371 ata_print_ident(ident_buf);
2372 camxferrate(device);
2373 atacapprint(ident_buf);
2374 atahpa_print(ident_buf, hpasize, 0);
2375 ataama_print(ident_buf, nativesize, 0);
2385 nvmeidentify(struct cam_device *device, int retry_count __unused, int timeout __unused)
2387 struct nvme_controller_data cdata;
2389 if (nvme_get_cdata(device, &cdata))
2391 nvme_print_controller(&cdata);
2398 identify(struct cam_device *device, int retry_count, int timeout)
2401 struct ccb_pathinq cpi;
2403 if (get_cpi(device, &cpi) != 0) {
2404 warnx("couldn't get CPI");
2408 if (cpi.protocol == PROTO_NVME) {
2409 return (nvmeidentify(device, retry_count, timeout));
2412 return (ataidentify(device, retry_count, timeout));
2417 ATA_SECURITY_ACTION_PRINT,
2418 ATA_SECURITY_ACTION_FREEZE,
2419 ATA_SECURITY_ACTION_UNLOCK,
2420 ATA_SECURITY_ACTION_DISABLE,
2421 ATA_SECURITY_ACTION_ERASE,
2422 ATA_SECURITY_ACTION_ERASE_ENHANCED,
2423 ATA_SECURITY_ACTION_SET_PASSWORD
2427 atasecurity_print_time(u_int16_t tw)
2431 printf("unspecified");
2433 printf("> 508 min");
2435 printf("%i min", 2 * tw);
2439 atasecurity_erase_timeout_msecs(u_int16_t timeout)
2443 return 2 * 3600 * 1000; /* default: two hours */
2444 else if (timeout > 255)
2445 return (508 + 60) * 60 * 1000; /* spec says > 508 minutes */
2447 return ((2 * timeout) + 5) * 60 * 1000; /* add a 5min margin */
2452 atasecurity_notify(u_int8_t command, struct ata_security_password *pwd)
2456 bzero(&cmd, sizeof(cmd));
2457 cmd.command = command;
2458 printf("Issuing %s", ata_op_string(&cmd));
2461 /* pwd->password may not be null terminated */
2462 char pass[sizeof(pwd->password)+1];
2464 strlcpy(pass, pwd->password, sizeof(pass));
2465 printf(" password='%s', user='%s'",
2467 (pwd->ctrl & ATA_SECURITY_PASSWORD_MASTER) ?
2470 if (command == ATA_SECURITY_SET_PASSWORD) {
2471 printf(", mode='%s'",
2472 (pwd->ctrl & ATA_SECURITY_LEVEL_MAXIMUM) ?
2473 "maximum" : "high");
2481 atasecurity_freeze(struct cam_device *device, union ccb *ccb,
2482 int retry_count, u_int32_t timeout, int quiet)
2486 atasecurity_notify(ATA_SECURITY_FREEZE_LOCK, NULL);
2488 return ata_do_cmd(device,
2491 /*flags*/CAM_DIR_NONE,
2492 /*protocol*/AP_PROTO_NON_DATA,
2494 /*tag_action*/MSG_SIMPLE_Q_TAG,
2495 /*command*/ATA_SECURITY_FREEZE_LOCK,
2506 atasecurity_unlock(struct cam_device *device, union ccb *ccb,
2507 int retry_count, u_int32_t timeout,
2508 struct ata_security_password *pwd, int quiet)
2512 atasecurity_notify(ATA_SECURITY_UNLOCK, pwd);
2514 return ata_do_cmd(device,
2517 /*flags*/CAM_DIR_OUT,
2518 /*protocol*/AP_PROTO_PIO_OUT,
2519 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2520 AP_FLAG_TLEN_SECT_CNT,
2521 /*tag_action*/MSG_SIMPLE_Q_TAG,
2522 /*command*/ATA_SECURITY_UNLOCK,
2525 /*sector_count*/sizeof(*pwd) / 512,
2526 /*data_ptr*/(u_int8_t *)pwd,
2527 /*dxfer_len*/sizeof(*pwd),
2533 atasecurity_disable(struct cam_device *device, union ccb *ccb,
2534 int retry_count, u_int32_t timeout,
2535 struct ata_security_password *pwd, int quiet)
2539 atasecurity_notify(ATA_SECURITY_DISABLE_PASSWORD, pwd);
2540 return ata_do_cmd(device,
2543 /*flags*/CAM_DIR_OUT,
2544 /*protocol*/AP_PROTO_PIO_OUT,
2545 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2546 AP_FLAG_TLEN_SECT_CNT,
2547 /*tag_action*/MSG_SIMPLE_Q_TAG,
2548 /*command*/ATA_SECURITY_DISABLE_PASSWORD,
2551 /*sector_count*/sizeof(*pwd) / 512,
2552 /*data_ptr*/(u_int8_t *)pwd,
2553 /*dxfer_len*/sizeof(*pwd),
2560 atasecurity_erase_confirm(struct cam_device *device,
2561 struct ata_params* ident_buf)
2564 printf("\nYou are about to ERASE ALL DATA from the following"
2565 " device:\n%s%d,%s%d: ", device->device_name,
2566 device->dev_unit_num, device->given_dev_name,
2567 device->given_unit_number);
2568 ata_print_ident(ident_buf);
2572 printf("\nAre you SURE you want to ERASE ALL DATA? (yes/no) ");
2574 if (fgets(str, sizeof(str), stdin) != NULL) {
2575 if (strncasecmp(str, "yes", 3) == 0) {
2577 } else if (strncasecmp(str, "no", 2) == 0) {
2580 printf("Please answer \"yes\" or "
2591 atasecurity_erase(struct cam_device *device, union ccb *ccb,
2592 int retry_count, u_int32_t timeout,
2593 u_int32_t erase_timeout,
2594 struct ata_security_password *pwd, int quiet)
2599 atasecurity_notify(ATA_SECURITY_ERASE_PREPARE, NULL);
2601 error = ata_do_cmd(device,
2604 /*flags*/CAM_DIR_NONE,
2605 /*protocol*/AP_PROTO_NON_DATA,
2607 /*tag_action*/MSG_SIMPLE_Q_TAG,
2608 /*command*/ATA_SECURITY_ERASE_PREPARE,
2621 atasecurity_notify(ATA_SECURITY_ERASE_UNIT, pwd);
2623 error = ata_do_cmd(device,
2626 /*flags*/CAM_DIR_OUT,
2627 /*protocol*/AP_PROTO_PIO_OUT,
2628 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2629 AP_FLAG_TLEN_SECT_CNT,
2630 /*tag_action*/MSG_SIMPLE_Q_TAG,
2631 /*command*/ATA_SECURITY_ERASE_UNIT,
2634 /*sector_count*/sizeof(*pwd) / 512,
2635 /*data_ptr*/(u_int8_t *)pwd,
2636 /*dxfer_len*/sizeof(*pwd),
2637 /*timeout*/erase_timeout,
2640 if (error == 0 && quiet == 0)
2641 printf("\nErase Complete\n");
2647 atasecurity_set_password(struct cam_device *device, union ccb *ccb,
2648 int retry_count, u_int32_t timeout,
2649 struct ata_security_password *pwd, int quiet)
2653 atasecurity_notify(ATA_SECURITY_SET_PASSWORD, pwd);
2655 return ata_do_cmd(device,
2658 /*flags*/CAM_DIR_OUT,
2659 /*protocol*/AP_PROTO_PIO_OUT,
2660 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2661 AP_FLAG_TLEN_SECT_CNT,
2662 /*tag_action*/MSG_SIMPLE_Q_TAG,
2663 /*command*/ATA_SECURITY_SET_PASSWORD,
2666 /*sector_count*/sizeof(*pwd) / 512,
2667 /*data_ptr*/(u_int8_t *)pwd,
2668 /*dxfer_len*/sizeof(*pwd),
2674 atasecurity_print(struct ata_params *parm)
2677 printf("\nSecurity Option Value\n");
2678 if (arglist & CAM_ARG_VERBOSE) {
2679 printf("status %04x\n",
2680 parm->security_status);
2682 printf("supported %s\n",
2683 parm->security_status & ATA_SECURITY_SUPPORTED ? "yes" : "no");
2684 if (!(parm->security_status & ATA_SECURITY_SUPPORTED))
2686 printf("enabled %s\n",
2687 parm->security_status & ATA_SECURITY_ENABLED ? "yes" : "no");
2688 printf("drive locked %s\n",
2689 parm->security_status & ATA_SECURITY_LOCKED ? "yes" : "no");
2690 printf("security config frozen %s\n",
2691 parm->security_status & ATA_SECURITY_FROZEN ? "yes" : "no");
2692 printf("count expired %s\n",
2693 parm->security_status & ATA_SECURITY_COUNT_EXP ? "yes" : "no");
2694 printf("security level %s\n",
2695 parm->security_status & ATA_SECURITY_LEVEL ? "maximum" : "high");
2696 printf("enhanced erase supported %s\n",
2697 parm->security_status & ATA_SECURITY_ENH_SUPP ? "yes" : "no");
2698 printf("erase time ");
2699 atasecurity_print_time(parm->erase_time);
2701 printf("enhanced erase time ");
2702 atasecurity_print_time(parm->enhanced_erase_time);
2704 printf("master password rev %04x%s\n",
2705 parm->master_passwd_revision,
2706 parm->master_passwd_revision == 0x0000 ||
2707 parm->master_passwd_revision == 0xFFFF ? " (unsupported)" : "");
2711 * Validates and copies the password in optarg to the passed buffer.
2712 * If the password in optarg is the same length as the buffer then
2713 * the data will still be copied but no null termination will occur.
2716 ata_getpwd(u_int8_t *passwd, int max, char opt)
2720 len = strlen(optarg);
2722 warnx("-%c password is too long", opt);
2724 } else if (len == 0) {
2725 warnx("-%c password is missing", opt);
2727 } else if (optarg[0] == '-'){
2728 warnx("-%c password starts with '-' (generic arg?)", opt);
2730 } else if (strlen(passwd) != 0 && strcmp(passwd, optarg) != 0) {
2731 warnx("-%c password conflicts with existing password from -%c",
2736 /* Callers pass in a buffer which does NOT need to be terminated */
2737 strncpy(passwd, optarg, max);
2744 ATA_HPA_ACTION_PRINT,
2745 ATA_HPA_ACTION_SET_MAX,
2746 ATA_HPA_ACTION_SET_PWD,
2747 ATA_HPA_ACTION_LOCK,
2748 ATA_HPA_ACTION_UNLOCK,
2749 ATA_HPA_ACTION_FREEZE_LOCK
2753 atahpa_set_confirm(struct cam_device *device, struct ata_params* ident_buf,
2754 u_int64_t maxsize, int persist)
2756 printf("\nYou are about to configure HPA to limit the user accessible\n"
2757 "sectors to %ju %s on the device:\n%s%d,%s%d: ", maxsize,
2758 persist ? "persistently" : "temporarily",
2759 device->device_name, device->dev_unit_num,
2760 device->given_dev_name, device->given_unit_number);
2761 ata_print_ident(ident_buf);
2765 printf("\nAre you SURE you want to configure HPA? (yes/no) ");
2767 if (NULL != fgets(str, sizeof(str), stdin)) {
2768 if (0 == strncasecmp(str, "yes", 3)) {
2770 } else if (0 == strncasecmp(str, "no", 2)) {
2773 printf("Please answer \"yes\" or "
2784 atahpa(struct cam_device *device, int retry_count, int timeout,
2785 int argc, char **argv, char *combinedopt)
2788 struct ata_params *ident_buf;
2789 struct ccb_getdev cgd;
2790 struct ata_set_max_pwd pwd;
2791 int error, confirm, quiet, c, action, actions, persist;
2792 int security, is48bit, pwdsize;
2793 u_int64_t hpasize, maxsize;
2802 memset(&pwd, 0, sizeof(pwd));
2804 /* default action is to print hpa information */
2805 action = ATA_HPA_ACTION_PRINT;
2806 pwdsize = sizeof(pwd.password);
2808 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2811 action = ATA_HPA_ACTION_SET_MAX;
2812 maxsize = strtoumax(optarg, NULL, 0);
2817 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
2819 action = ATA_HPA_ACTION_SET_PWD;
2825 action = ATA_HPA_ACTION_LOCK;
2831 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
2833 action = ATA_HPA_ACTION_UNLOCK;
2839 action = ATA_HPA_ACTION_FREEZE_LOCK;
2859 warnx("too many hpa actions specified");
2863 if (get_cgd(device, &cgd) != 0) {
2864 warnx("couldn't get CGD");
2868 ccb = cam_getccb(device);
2870 warnx("couldn't allocate CCB");
2874 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
2881 printf("%s%d: ", device->device_name, device->dev_unit_num);
2882 ata_print_ident(ident_buf);
2883 camxferrate(device);
2886 if (action == ATA_HPA_ACTION_PRINT) {
2888 if (ident_buf->support.command1 & ATA_SUPPORT_PROTECTED)
2889 ata_read_native_max(device, retry_count, timeout, ccb,
2890 ident_buf, &hpasize);
2891 atahpa_print(ident_buf, hpasize, 1);
2898 if (!(ident_buf->support.command1 & ATA_SUPPORT_PROTECTED)) {
2899 warnx("HPA is not supported by this device");
2905 if (security && !(ident_buf->support.command2 & ATA_SUPPORT_MAXSECURITY)) {
2906 warnx("HPA Security is not supported by this device");
2912 is48bit = ident_buf->support.command2 & ATA_SUPPORT_ADDRESS48;
2915 * The ATA spec requires:
2916 * 1. Read native max addr is called directly before set max addr
2917 * 2. Read native max addr is NOT called before any other set max call
2920 case ATA_HPA_ACTION_SET_MAX:
2922 atahpa_set_confirm(device, ident_buf, maxsize,
2929 error = ata_read_native_max(device, retry_count, timeout,
2930 ccb, ident_buf, &hpasize);
2932 error = atahpa_set_max(device, retry_count, timeout,
2933 ccb, is48bit, maxsize, persist);
2936 /* redo identify to get new values */
2937 error = ata_do_identify(device,
2938 retry_count, timeout, ccb,
2940 atahpa_print(ident_buf, hpasize, 1);
2942 /* Hint CAM to reprobe the device. */
2948 case ATA_HPA_ACTION_SET_PWD:
2949 error = atahpa_password(device, retry_count, timeout,
2950 ccb, is48bit, &pwd);
2951 if (error == 0 && quiet == 0)
2952 printf("HPA password has been set\n");
2955 case ATA_HPA_ACTION_LOCK:
2956 error = atahpa_lock(device, retry_count, timeout,
2958 if (error == 0 && quiet == 0)
2959 printf("HPA has been locked\n");
2962 case ATA_HPA_ACTION_UNLOCK:
2963 error = atahpa_unlock(device, retry_count, timeout,
2964 ccb, is48bit, &pwd);
2965 if (error == 0 && quiet == 0)
2966 printf("HPA has been unlocked\n");
2969 case ATA_HPA_ACTION_FREEZE_LOCK:
2970 error = atahpa_freeze_lock(device, retry_count, timeout,
2972 if (error == 0 && quiet == 0)
2973 printf("HPA has been frozen\n");
2977 errx(1, "Option currently not supported");
2987 ATA_AMA_ACTION_PRINT,
2988 ATA_AMA_ACTION_SET_MAX,
2989 ATA_AMA_ACTION_FREEZE_LOCK
2993 ataama(struct cam_device *device, int retry_count, int timeout,
2994 int argc, char **argv, char *combinedopt)
2997 struct ata_params *ident_buf;
2998 struct ccb_getdev cgd;
2999 int error, quiet, c, action, actions;
3000 u_int64_t nativesize, maxsize;
3006 /* default action is to print AMA information */
3007 action = ATA_AMA_ACTION_PRINT;
3009 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3012 action = ATA_AMA_ACTION_SET_MAX;
3013 maxsize = strtoumax(optarg, NULL, 0);
3018 action = ATA_AMA_ACTION_FREEZE_LOCK;
3029 warnx("too many AMA actions specified");
3033 if (get_cgd(device, &cgd) != 0) {
3034 warnx("couldn't get CGD");
3038 ccb = cam_getccb(device);
3040 warnx("couldn't allocate CCB");
3044 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
3051 printf("%s%d: ", device->device_name, device->dev_unit_num);
3052 ata_print_ident(ident_buf);
3053 camxferrate(device);
3056 if (action == ATA_AMA_ACTION_PRINT) {
3058 if (ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR)
3059 ata_get_native_max(device, retry_count, timeout, ccb,
3061 ataama_print(ident_buf, nativesize, 1);
3068 if (!(ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR)) {
3069 warnx("Accessible Max Address is not supported by this device");
3076 case ATA_AMA_ACTION_SET_MAX:
3077 error = ata_get_native_max(device, retry_count, timeout, ccb,
3080 error = ataama_set(device, retry_count, timeout,
3084 /* redo identify to get new values */
3085 error = ata_do_identify(device,
3086 retry_count, timeout, ccb,
3088 ataama_print(ident_buf, nativesize, 1);
3090 /* Hint CAM to reprobe the device. */
3096 case ATA_AMA_ACTION_FREEZE_LOCK:
3097 error = ataama_freeze(device, retry_count, timeout,
3099 if (error == 0 && quiet == 0)
3100 printf("Accessible Max Address has been frozen\n");
3104 errx(1, "Option currently not supported");
3114 atasecurity(struct cam_device *device, int retry_count, int timeout,
3115 int argc, char **argv, char *combinedopt)
3118 struct ata_params *ident_buf;
3119 int error, confirm, quiet, c, action, actions, setpwd;
3120 int security_enabled, erase_timeout, pwdsize;
3121 struct ata_security_password pwd;
3129 memset(&pwd, 0, sizeof(pwd));
3131 /* default action is to print security information */
3132 action = ATA_SECURITY_ACTION_PRINT;
3134 /* user is master by default as its safer that way */
3135 pwd.ctrl |= ATA_SECURITY_PASSWORD_MASTER;
3136 pwdsize = sizeof(pwd.password);
3138 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3141 action = ATA_SECURITY_ACTION_FREEZE;
3146 if (strcasecmp(optarg, "user") == 0) {
3147 pwd.ctrl |= ATA_SECURITY_PASSWORD_USER;
3148 pwd.ctrl &= ~ATA_SECURITY_PASSWORD_MASTER;
3149 } else if (strcasecmp(optarg, "master") == 0) {
3150 pwd.ctrl |= ATA_SECURITY_PASSWORD_MASTER;
3151 pwd.ctrl &= ~ATA_SECURITY_PASSWORD_USER;
3153 warnx("-U argument '%s' is invalid (must be "
3154 "'user' or 'master')", optarg);
3160 if (strcasecmp(optarg, "high") == 0) {
3161 pwd.ctrl |= ATA_SECURITY_LEVEL_HIGH;
3162 pwd.ctrl &= ~ATA_SECURITY_LEVEL_MAXIMUM;
3163 } else if (strcasecmp(optarg, "maximum") == 0) {
3164 pwd.ctrl |= ATA_SECURITY_LEVEL_MAXIMUM;
3165 pwd.ctrl &= ~ATA_SECURITY_LEVEL_HIGH;
3167 warnx("-l argument '%s' is unknown (must be "
3168 "'high' or 'maximum')", optarg);
3174 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3176 action = ATA_SECURITY_ACTION_UNLOCK;
3181 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3183 action = ATA_SECURITY_ACTION_DISABLE;
3188 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3190 action = ATA_SECURITY_ACTION_ERASE;
3195 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3197 pwd.ctrl |= ATA_SECURITY_ERASE_ENHANCED;
3198 action = ATA_SECURITY_ACTION_ERASE_ENHANCED;
3203 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3206 if (action == ATA_SECURITY_ACTION_PRINT)
3207 action = ATA_SECURITY_ACTION_SET_PASSWORD;
3209 * Don't increment action as this can be combined
3210 * with other actions.
3223 erase_timeout = atoi(optarg) * 1000;
3229 warnx("too many security actions specified");
3233 if ((ccb = cam_getccb(device)) == NULL) {
3234 warnx("couldn't allocate CCB");
3238 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
3245 printf("%s%d: ", device->device_name, device->dev_unit_num);
3246 ata_print_ident(ident_buf);
3247 camxferrate(device);
3250 if (action == ATA_SECURITY_ACTION_PRINT) {
3251 atasecurity_print(ident_buf);
3257 if ((ident_buf->support.command1 & ATA_SUPPORT_SECURITY) == 0) {
3258 warnx("Security not supported");
3264 /* default timeout 15 seconds the same as linux hdparm */
3265 timeout = timeout ? timeout : 15 * 1000;
3267 security_enabled = ident_buf->security_status & ATA_SECURITY_ENABLED;
3269 /* first set the password if requested */
3271 /* confirm we can erase before setting the password if erasing */
3273 (action == ATA_SECURITY_ACTION_ERASE_ENHANCED ||
3274 action == ATA_SECURITY_ACTION_ERASE) &&
3275 atasecurity_erase_confirm(device, ident_buf) == 0) {
3281 if (pwd.ctrl & ATA_SECURITY_PASSWORD_MASTER) {
3282 pwd.revision = ident_buf->master_passwd_revision;
3283 if (pwd.revision != 0 && pwd.revision != 0xfff &&
3284 --pwd.revision == 0) {
3285 pwd.revision = 0xfffe;
3288 error = atasecurity_set_password(device, ccb, retry_count,
3289 timeout, &pwd, quiet);
3295 security_enabled = 1;
3299 case ATA_SECURITY_ACTION_FREEZE:
3300 error = atasecurity_freeze(device, ccb, retry_count,
3304 case ATA_SECURITY_ACTION_UNLOCK:
3305 if (security_enabled) {
3306 if (ident_buf->security_status & ATA_SECURITY_LOCKED) {
3307 error = atasecurity_unlock(device, ccb,
3308 retry_count, timeout, &pwd, quiet);
3310 warnx("Can't unlock, drive is not locked");
3314 warnx("Can't unlock, security is disabled");
3319 case ATA_SECURITY_ACTION_DISABLE:
3320 if (security_enabled) {
3321 /* First unlock the drive if its locked */
3322 if (ident_buf->security_status & ATA_SECURITY_LOCKED) {
3323 error = atasecurity_unlock(device, ccb,
3331 error = atasecurity_disable(device,
3339 warnx("Can't disable security (already disabled)");
3344 case ATA_SECURITY_ACTION_ERASE:
3345 if (security_enabled) {
3346 if (erase_timeout == 0) {
3347 erase_timeout = atasecurity_erase_timeout_msecs(
3348 ident_buf->erase_time);
3351 error = atasecurity_erase(device, ccb, retry_count,
3352 timeout, erase_timeout, &pwd, quiet);
3354 warnx("Can't secure erase (security is disabled)");
3359 case ATA_SECURITY_ACTION_ERASE_ENHANCED:
3360 if (security_enabled) {
3361 if (ident_buf->security_status & ATA_SECURITY_ENH_SUPP) {
3362 if (erase_timeout == 0) {
3364 atasecurity_erase_timeout_msecs(
3365 ident_buf->enhanced_erase_time);
3368 error = atasecurity_erase(device, ccb,
3369 retry_count, timeout,
3370 erase_timeout, &pwd,
3373 warnx("Enhanced erase is not supported");
3377 warnx("Can't secure erase (enhanced), "
3378 "(security is disabled)");
3391 * Convert periph name into a bus, target and lun.
3393 * Returns the number of parsed components, or 0.
3396 parse_btl_name(char *tstr, path_id_t *bus, target_id_t *target, lun_id_t *lun,
3397 cam_argmask *arglst)
3402 bzero(&ccb, sizeof(ccb));
3403 ccb.ccb_h.func_code = XPT_GDEVLIST;
3404 if (cam_get_device(tstr, ccb.cgdl.periph_name,
3405 sizeof(ccb.cgdl.periph_name), &ccb.cgdl.unit_number) == -1) {
3406 warnx("%s", cam_errbuf);
3411 * Attempt to get the passthrough device. This ioctl will
3412 * fail if the device name is null, if the device doesn't
3413 * exist, or if the passthrough driver isn't in the kernel.
3415 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
3416 warn("Unable to open %s", XPT_DEVICE);
3419 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
3420 warn("Unable to find bus:target:lun for device %s%d",
3421 ccb.cgdl.periph_name, ccb.cgdl.unit_number);
3426 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3427 const struct cam_status_entry *entry;
3429 entry = cam_fetch_status_entry(ccb.ccb_h.status);
3430 warnx("Unable to find bus:target_lun for device %s%d, "
3431 "CAM status: %s (%#x)",
3432 ccb.cgdl.periph_name, ccb.cgdl.unit_number,
3433 entry ? entry->status_text : "Unknown",
3439 * The kernel fills in the bus/target/lun. We don't
3440 * need the passthrough device name and unit number since
3441 * we aren't going to open it.
3443 *bus = ccb.ccb_h.path_id;
3444 *target = ccb.ccb_h.target_id;
3445 *lun = ccb.ccb_h.target_lun;
3446 *arglst |= CAM_ARG_BUS | CAM_ARG_TARGET | CAM_ARG_LUN;
3451 * Parse out a bus, or a bus, target and lun in the following
3457 * Returns the number of parsed components, or 0.
3460 parse_btl(char *tstr, path_id_t *bus, target_id_t *target, lun_id_t *lun,
3461 cam_argmask *arglst)
3466 *bus = CAM_BUS_WILDCARD;
3467 *target = CAM_TARGET_WILDCARD;
3468 *lun = CAM_LUN_WILDCARD;
3470 while (isspace(*tstr) && (*tstr != '\0'))
3473 if (strncasecmp(tstr, "all", strlen("all")) == 0) {
3474 arglist |= CAM_ARG_BUS;
3478 if (!isdigit(*tstr))
3479 return (parse_btl_name(tstr, bus, target, lun, arglst));
3481 tmpstr = strsep(&tstr, ":");
3482 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3483 *bus = strtol(tmpstr, &end, 0);
3486 *arglst |= CAM_ARG_BUS;
3488 tmpstr = strsep(&tstr, ":");
3489 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3490 *target = strtol(tmpstr, &end, 0);
3493 *arglst |= CAM_ARG_TARGET;
3495 tmpstr = strsep(&tstr, ":");
3496 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3497 *lun = strtoll(tmpstr, &end, 0);
3500 *arglst |= CAM_ARG_LUN;
3510 dorescan_or_reset(int argc, char **argv, int rescan)
3512 static const char must[] =
3513 "you must specify \"all\", a bus, a bus:target:lun or periph to %s";
3515 path_id_t bus = CAM_BUS_WILDCARD;
3516 target_id_t target = CAM_TARGET_WILDCARD;
3517 lun_id_t lun = CAM_LUN_WILDCARD;
3521 warnx(must, rescan? "rescan" : "reset");
3525 tstr = argv[optind];
3526 while (isspace(*tstr) && (*tstr != '\0'))
3528 if (strncasecmp(tstr, "all", strlen("all")) == 0)
3529 arglist |= CAM_ARG_BUS;
3531 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
3532 if (rv != 1 && rv != 3) {
3533 warnx(must, rescan ? "rescan" : "reset");
3538 if (arglist & CAM_ARG_LUN)
3539 error = scanlun_or_reset_dev(bus, target, lun, rescan);
3541 error = rescan_or_reset_bus(bus, rescan);
3547 rescan_or_reset_bus(path_id_t bus, int rescan)
3549 union ccb *ccb = NULL, *matchccb = NULL;
3550 int fd = -1, retval;
3555 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
3556 warnx("error opening transport layer device %s", XPT_DEVICE);
3557 warn("%s", XPT_DEVICE);
3561 ccb = malloc(sizeof(*ccb));
3563 warn("failed to allocate CCB");
3567 bzero(ccb, sizeof(*ccb));
3569 if (bus != CAM_BUS_WILDCARD) {
3570 ccb->ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
3571 ccb->ccb_h.path_id = bus;
3572 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
3573 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
3574 ccb->crcn.flags = CAM_FLAG_NONE;
3576 /* run this at a low priority */
3577 ccb->ccb_h.pinfo.priority = 5;
3579 if (ioctl(fd, CAMIOCOMMAND, ccb) == -1) {
3580 warn("CAMIOCOMMAND ioctl failed");
3585 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3586 fprintf(stdout, "%s of bus %d was successful\n",
3587 rescan ? "Re-scan" : "Reset", bus);
3589 fprintf(stdout, "%s of bus %d returned error %#x\n",
3590 rescan ? "Re-scan" : "Reset", bus,
3591 ccb->ccb_h.status & CAM_STATUS_MASK);
3600 * The right way to handle this is to modify the xpt so that it can
3601 * handle a wildcarded bus in a rescan or reset CCB. At the moment
3602 * that isn't implemented, so instead we enumerate the buses and
3603 * send the rescan or reset to those buses in the case where the
3604 * given bus is -1 (wildcard). We don't send a rescan or reset
3605 * to the xpt bus; sending a rescan to the xpt bus is effectively a
3606 * no-op, sending a rescan to the xpt bus would result in a status of
3609 matchccb = malloc(sizeof(*matchccb));
3610 if (matchccb == NULL) {
3611 warn("failed to allocate CCB");
3615 bzero(matchccb, sizeof(*matchccb));
3616 matchccb->ccb_h.func_code = XPT_DEV_MATCH;
3617 matchccb->ccb_h.path_id = CAM_BUS_WILDCARD;
3618 bufsize = sizeof(struct dev_match_result) * 20;
3619 matchccb->cdm.match_buf_len = bufsize;
3620 matchccb->cdm.matches=(struct dev_match_result *)malloc(bufsize);
3621 if (matchccb->cdm.matches == NULL) {
3622 warnx("can't malloc memory for matches");
3626 matchccb->cdm.num_matches = 0;
3628 matchccb->cdm.num_patterns = 1;
3629 matchccb->cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
3631 matchccb->cdm.patterns = (struct dev_match_pattern *)malloc(
3632 matchccb->cdm.pattern_buf_len);
3633 if (matchccb->cdm.patterns == NULL) {
3634 warnx("can't malloc memory for patterns");
3638 matchccb->cdm.patterns[0].type = DEV_MATCH_BUS;
3639 matchccb->cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
3644 if (ioctl(fd, CAMIOCOMMAND, matchccb) == -1) {
3645 warn("CAMIOCOMMAND ioctl failed");
3650 if ((matchccb->ccb_h.status != CAM_REQ_CMP)
3651 || ((matchccb->cdm.status != CAM_DEV_MATCH_LAST)
3652 && (matchccb->cdm.status != CAM_DEV_MATCH_MORE))) {
3653 warnx("got CAM error %#x, CDM error %d\n",
3654 matchccb->ccb_h.status, matchccb->cdm.status);
3659 for (i = 0; i < matchccb->cdm.num_matches; i++) {
3660 struct bus_match_result *bus_result;
3662 /* This shouldn't happen. */
3663 if (matchccb->cdm.matches[i].type != DEV_MATCH_BUS)
3666 bus_result =&matchccb->cdm.matches[i].result.bus_result;
3669 * We don't want to rescan or reset the xpt bus.
3672 if (bus_result->path_id == CAM_XPT_PATH_ID)
3675 ccb->ccb_h.func_code = rescan ? XPT_SCAN_BUS :
3677 ccb->ccb_h.path_id = bus_result->path_id;
3678 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
3679 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
3680 ccb->crcn.flags = CAM_FLAG_NONE;
3682 /* run this at a low priority */
3683 ccb->ccb_h.pinfo.priority = 5;
3685 if (ioctl(fd, CAMIOCOMMAND, ccb) == -1) {
3686 warn("CAMIOCOMMAND ioctl failed");
3691 if ((ccb->ccb_h.status & CAM_STATUS_MASK)==CAM_REQ_CMP){
3692 fprintf(stdout, "%s of bus %d was successful\n",
3693 rescan? "Re-scan" : "Reset",
3694 bus_result->path_id);
3697 * Don't bail out just yet, maybe the other
3698 * rescan or reset commands will complete
3701 fprintf(stderr, "%s of bus %d returned error "
3702 "%#x\n", rescan? "Re-scan" : "Reset",
3703 bus_result->path_id,
3704 ccb->ccb_h.status & CAM_STATUS_MASK);
3708 } while ((matchccb->ccb_h.status == CAM_REQ_CMP)
3709 && (matchccb->cdm.status == CAM_DEV_MATCH_MORE));
3716 if (matchccb != NULL) {
3717 free(matchccb->cdm.patterns);
3718 free(matchccb->cdm.matches);
3727 scanlun_or_reset_dev(path_id_t bus, target_id_t target, lun_id_t lun, int scan)
3730 struct cam_device *device;
3735 if (bus == CAM_BUS_WILDCARD) {
3736 warnx("invalid bus number %d", bus);
3740 if (target == CAM_TARGET_WILDCARD) {
3741 warnx("invalid target number %d", target);
3745 if (lun == CAM_LUN_WILDCARD) {
3746 warnx("invalid lun number %jx", (uintmax_t)lun);
3752 bzero(&ccb, sizeof(union ccb));
3755 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
3756 warnx("error opening transport layer device %s\n",
3758 warn("%s", XPT_DEVICE);
3762 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
3763 if (device == NULL) {
3764 warnx("%s", cam_errbuf);
3769 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
3770 ccb.ccb_h.path_id = bus;
3771 ccb.ccb_h.target_id = target;
3772 ccb.ccb_h.target_lun = lun;
3773 ccb.ccb_h.timeout = 5000;
3774 ccb.crcn.flags = CAM_FLAG_NONE;
3776 /* run this at a low priority */
3777 ccb.ccb_h.pinfo.priority = 5;
3780 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
3781 warn("CAMIOCOMMAND ioctl failed");
3786 if (cam_send_ccb(device, &ccb) < 0) {
3787 warn("error sending XPT_RESET_DEV CCB");
3788 cam_close_device(device);
3796 cam_close_device(device);
3799 * An error code of CAM_BDR_SENT is normal for a BDR request.
3801 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
3803 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
3804 fprintf(stdout, "%s of %d:%d:%jx was successful\n",
3805 scan? "Re-scan" : "Reset", bus, target, (uintmax_t)lun);
3808 fprintf(stdout, "%s of %d:%d:%jx returned error %#x\n",
3809 scan? "Re-scan" : "Reset", bus, target, (uintmax_t)lun,
3810 ccb.ccb_h.status & CAM_STATUS_MASK);
3816 static struct scsi_nv defect_list_type_map[] = {
3817 { "block", SRDD10_BLOCK_FORMAT },
3818 { "extbfi", SRDD10_EXT_BFI_FORMAT },
3819 { "extphys", SRDD10_EXT_PHYS_FORMAT },
3820 { "longblock", SRDD10_LONG_BLOCK_FORMAT },
3821 { "bfi", SRDD10_BYTES_FROM_INDEX_FORMAT },
3822 { "phys", SRDD10_PHYSICAL_SECTOR_FORMAT }
3826 readdefects(struct cam_device *device, int argc, char **argv,
3827 char *combinedopt, int task_attr, int retry_count, int timeout)
3829 union ccb *ccb = NULL;
3830 struct scsi_read_defect_data_hdr_10 *hdr10 = NULL;
3831 struct scsi_read_defect_data_hdr_12 *hdr12 = NULL;
3832 size_t hdr_size = 0, entry_size = 0;
3835 u_int8_t *defect_list = NULL;
3836 u_int8_t list_format = 0;
3837 int list_type_set = 0;
3838 u_int32_t dlist_length = 0;
3839 u_int32_t returned_length = 0, valid_len = 0;
3840 u_int32_t num_returned = 0, num_valid = 0;
3841 u_int32_t max_possible_size = 0, hdr_max = 0;
3842 u_int32_t starting_offset = 0;
3843 u_int8_t returned_format, returned_type;
3845 int summary = 0, quiet = 0;
3847 int lists_specified = 0;
3848 int get_length = 1, first_pass = 1;
3851 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3855 scsi_nv_status status;
3858 status = scsi_get_nv(defect_list_type_map,
3859 sizeof(defect_list_type_map) /
3860 sizeof(defect_list_type_map[0]), optarg,
3861 &entry_num, SCSI_NV_FLAG_IG_CASE);
3863 if (status == SCSI_NV_FOUND) {
3864 list_format = defect_list_type_map[
3868 warnx("%s: %s %s option %s", __func__,
3869 (status == SCSI_NV_AMBIGUOUS) ?
3870 "ambiguous" : "invalid", "defect list type",
3873 goto defect_bailout;
3878 arglist |= CAM_ARG_GLIST;
3881 arglist |= CAM_ARG_PLIST;
3892 starting_offset = strtoul(optarg, &endptr, 0);
3893 if (*endptr != '\0') {
3895 warnx("invalid starting offset %s", optarg);
3896 goto defect_bailout;
3908 if (list_type_set == 0) {
3910 warnx("no defect list format specified");
3911 goto defect_bailout;
3914 if (arglist & CAM_ARG_PLIST) {
3915 list_format |= SRDD10_PLIST;
3919 if (arglist & CAM_ARG_GLIST) {
3920 list_format |= SRDD10_GLIST;
3925 * This implies a summary, and was the previous behavior.
3927 if (lists_specified == 0)
3930 ccb = cam_getccb(device);
3935 * We start off asking for just the header to determine how much
3936 * defect data is available. Some Hitachi drives return an error
3937 * if you ask for more data than the drive has. Once we know the
3938 * length, we retry the command with the returned length.
3940 if (use_12byte == 0)
3941 dlist_length = sizeof(*hdr10);
3943 dlist_length = sizeof(*hdr12);
3946 if (defect_list != NULL) {
3950 defect_list = malloc(dlist_length);
3951 if (defect_list == NULL) {
3952 warnx("can't malloc memory for defect list");
3954 goto defect_bailout;
3958 bzero(defect_list, dlist_length);
3961 * cam_getccb() zeros the CCB header only. So we need to zero the
3962 * payload portion of the ccb.
3964 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
3966 scsi_read_defects(&ccb->csio,
3967 /*retries*/ retry_count,
3969 /*tag_action*/ task_attr,
3970 /*list_format*/ list_format,
3971 /*addr_desc_index*/ starting_offset,
3972 /*data_ptr*/ defect_list,
3973 /*dxfer_len*/ dlist_length,
3974 /*minimum_cmd_size*/ use_12byte ? 12 : 0,
3975 /*sense_len*/ SSD_FULL_SIZE,
3976 /*timeout*/ timeout ? timeout : 5000);
3978 /* Disable freezing the device queue */
3979 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3981 if (cam_send_ccb(device, ccb) < 0) {
3982 warn("error sending READ DEFECT DATA command");
3984 goto defect_bailout;
3987 valid_len = ccb->csio.dxfer_len - ccb->csio.resid;
3989 if (use_12byte == 0) {
3990 hdr10 = (struct scsi_read_defect_data_hdr_10 *)defect_list;
3991 hdr_size = sizeof(*hdr10);
3992 hdr_max = SRDDH10_MAX_LENGTH;
3994 if (valid_len >= hdr_size) {
3995 returned_length = scsi_2btoul(hdr10->length);
3996 returned_format = hdr10->format;
3998 returned_length = 0;
3999 returned_format = 0;
4002 hdr12 = (struct scsi_read_defect_data_hdr_12 *)defect_list;
4003 hdr_size = sizeof(*hdr12);
4004 hdr_max = SRDDH12_MAX_LENGTH;
4006 if (valid_len >= hdr_size) {
4007 returned_length = scsi_4btoul(hdr12->length);
4008 returned_format = hdr12->format;
4010 returned_length = 0;
4011 returned_format = 0;
4015 returned_type = returned_format & SRDDH10_DLIST_FORMAT_MASK;
4016 switch (returned_type) {
4017 case SRDD10_BLOCK_FORMAT:
4018 entry_size = sizeof(struct scsi_defect_desc_block);
4020 case SRDD10_LONG_BLOCK_FORMAT:
4021 entry_size = sizeof(struct scsi_defect_desc_long_block);
4023 case SRDD10_EXT_PHYS_FORMAT:
4024 case SRDD10_PHYSICAL_SECTOR_FORMAT:
4025 entry_size = sizeof(struct scsi_defect_desc_phys_sector);
4027 case SRDD10_EXT_BFI_FORMAT:
4028 case SRDD10_BYTES_FROM_INDEX_FORMAT:
4029 entry_size = sizeof(struct scsi_defect_desc_bytes_from_index);
4032 warnx("Unknown defect format 0x%x\n", returned_type);
4034 goto defect_bailout;
4038 max_possible_size = (hdr_max / entry_size) * entry_size;
4039 num_returned = returned_length / entry_size;
4040 num_valid = min(returned_length, valid_len - hdr_size);
4041 num_valid /= entry_size;
4043 if (get_length != 0) {
4046 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
4047 CAM_SCSI_STATUS_ERROR) {
4048 struct scsi_sense_data *sense;
4049 int error_code, sense_key, asc, ascq;
4051 sense = &ccb->csio.sense_data;
4052 scsi_extract_sense_len(sense, ccb->csio.sense_len -
4053 ccb->csio.sense_resid, &error_code, &sense_key,
4054 &asc, &ascq, /*show_errors*/ 1);
4057 * If the drive is reporting that it just doesn't
4058 * support the defect list format, go ahead and use
4059 * the length it reported. Otherwise, the length
4060 * may not be valid, so use the maximum.
4062 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4063 && (asc == 0x1c) && (ascq == 0x00)
4064 && (returned_length > 0)) {
4065 if ((use_12byte == 0)
4066 && (returned_length >= max_possible_size)) {
4071 dlist_length = returned_length + hdr_size;
4072 } else if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4073 && (asc == 0x1f) && (ascq == 0x00)
4074 && (returned_length > 0)) {
4075 /* Partial defect list transfer */
4077 * Hitachi drives return this error
4078 * along with a partial defect list if they
4079 * have more defects than the 10 byte
4080 * command can support. Retry with the 12
4083 if (use_12byte == 0) {
4088 dlist_length = returned_length + hdr_size;
4089 } else if ((sense_key == SSD_KEY_ILLEGAL_REQUEST)
4090 && (asc == 0x24) && (ascq == 0x00)) {
4091 /* Invalid field in CDB */
4093 * SBC-3 says that if the drive has more
4094 * defects than can be reported with the
4095 * 10 byte command, it should return this
4096 * error and no data. Retry with the 12
4099 if (use_12byte == 0) {
4104 dlist_length = returned_length + hdr_size;
4107 * If we got a SCSI error and no valid length,
4108 * just use the 10 byte maximum. The 12
4109 * byte maximum is too large.
4111 if (returned_length == 0)
4112 dlist_length = SRDD10_MAX_LENGTH;
4114 if ((use_12byte == 0)
4115 && (returned_length >=
4116 max_possible_size)) {
4121 dlist_length = returned_length +
4125 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
4128 warnx("Error reading defect header");
4129 if (arglist & CAM_ARG_VERBOSE)
4130 cam_error_print(device, ccb, CAM_ESF_ALL,
4131 CAM_EPF_ALL, stderr);
4132 goto defect_bailout;
4134 if ((use_12byte == 0)
4135 && (returned_length >= max_possible_size)) {
4140 dlist_length = returned_length + hdr_size;
4143 fprintf(stdout, "%u", num_returned);
4145 fprintf(stdout, " defect%s",
4146 (num_returned != 1) ? "s" : "");
4148 fprintf(stdout, "\n");
4150 goto defect_bailout;
4154 * We always limit the list length to the 10-byte maximum
4155 * length (0xffff). The reason is that some controllers
4156 * can't handle larger I/Os, and we can transfer the entire
4157 * 10 byte list in one shot. For drives that support the 12
4158 * byte read defects command, we'll step through the list
4159 * by specifying a starting offset. For drives that don't
4160 * support the 12 byte command's starting offset, we'll
4161 * just display the first 64K.
4163 dlist_length = min(dlist_length, SRDD10_MAX_LENGTH);
4169 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
4170 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
4171 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
4172 struct scsi_sense_data *sense;
4173 int error_code, sense_key, asc, ascq;
4175 sense = &ccb->csio.sense_data;
4176 scsi_extract_sense_len(sense, ccb->csio.sense_len -
4177 ccb->csio.sense_resid, &error_code, &sense_key, &asc,
4178 &ascq, /*show_errors*/ 1);
4181 * According to the SCSI spec, if the disk doesn't support
4182 * the requested format, it will generally return a sense
4183 * key of RECOVERED ERROR, and an additional sense code
4184 * of "DEFECT LIST NOT FOUND". HGST drives also return
4185 * Primary/Grown defect list not found errors. So just
4186 * check for an ASC of 0x1c.
4188 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4190 const char *format_str;
4192 format_str = scsi_nv_to_str(defect_list_type_map,
4193 sizeof(defect_list_type_map) /
4194 sizeof(defect_list_type_map[0]),
4195 list_format & SRDD10_DLIST_FORMAT_MASK);
4196 warnx("requested defect format %s not available",
4197 format_str ? format_str : "unknown");
4199 format_str = scsi_nv_to_str(defect_list_type_map,
4200 sizeof(defect_list_type_map) /
4201 sizeof(defect_list_type_map[0]), returned_type);
4202 if (format_str != NULL) {
4203 warnx("Device returned %s format",
4207 warnx("Device returned unknown defect"
4208 " data format %#x", returned_type);
4209 goto defect_bailout;
4213 warnx("Error returned from read defect data command");
4214 if (arglist & CAM_ARG_VERBOSE)
4215 cam_error_print(device, ccb, CAM_ESF_ALL,
4216 CAM_EPF_ALL, stderr);
4217 goto defect_bailout;
4219 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4221 warnx("Error returned from read defect data command");
4222 if (arglist & CAM_ARG_VERBOSE)
4223 cam_error_print(device, ccb, CAM_ESF_ALL,
4224 CAM_EPF_ALL, stderr);
4225 goto defect_bailout;
4228 if (first_pass != 0) {
4229 fprintf(stderr, "Got %d defect", num_returned);
4231 if ((lists_specified == 0) || (num_returned == 0)) {
4232 fprintf(stderr, "s.\n");
4233 goto defect_bailout;
4234 } else if (num_returned == 1)
4235 fprintf(stderr, ":\n");
4237 fprintf(stderr, "s:\n");
4243 * XXX KDM I should probably clean up the printout format for the
4246 switch (returned_type) {
4247 case SRDD10_PHYSICAL_SECTOR_FORMAT:
4248 case SRDD10_EXT_PHYS_FORMAT:
4250 struct scsi_defect_desc_phys_sector *dlist;
4252 dlist = (struct scsi_defect_desc_phys_sector *)
4253 (defect_list + hdr_size);
4255 for (i = 0; i < num_valid; i++) {
4258 sector = scsi_4btoul(dlist[i].sector);
4259 if (returned_type == SRDD10_EXT_PHYS_FORMAT) {
4260 mads = (sector & SDD_EXT_PHYS_MADS) ?
4262 sector &= ~SDD_EXT_PHYS_FLAG_MASK;
4264 if (hex_format == 0)
4265 fprintf(stdout, "%d:%d:%d%s",
4266 scsi_3btoul(dlist[i].cylinder),
4268 scsi_4btoul(dlist[i].sector),
4269 mads ? " - " : "\n");
4271 fprintf(stdout, "0x%x:0x%x:0x%x%s",
4272 scsi_3btoul(dlist[i].cylinder),
4274 scsi_4btoul(dlist[i].sector),
4275 mads ? " - " : "\n");
4278 if (num_valid < num_returned) {
4279 starting_offset += num_valid;
4284 case SRDD10_BYTES_FROM_INDEX_FORMAT:
4285 case SRDD10_EXT_BFI_FORMAT:
4287 struct scsi_defect_desc_bytes_from_index *dlist;
4289 dlist = (struct scsi_defect_desc_bytes_from_index *)
4290 (defect_list + hdr_size);
4292 for (i = 0; i < num_valid; i++) {
4295 bfi = scsi_4btoul(dlist[i].bytes_from_index);
4296 if (returned_type == SRDD10_EXT_BFI_FORMAT) {
4297 mads = (bfi & SDD_EXT_BFI_MADS) ? 1 : 0;
4298 bfi &= ~SDD_EXT_BFI_FLAG_MASK;
4300 if (hex_format == 0)
4301 fprintf(stdout, "%d:%d:%d%s",
4302 scsi_3btoul(dlist[i].cylinder),
4304 scsi_4btoul(dlist[i].bytes_from_index),
4305 mads ? " - " : "\n");
4307 fprintf(stdout, "0x%x:0x%x:0x%x%s",
4308 scsi_3btoul(dlist[i].cylinder),
4310 scsi_4btoul(dlist[i].bytes_from_index),
4311 mads ? " - " : "\n");
4315 if (num_valid < num_returned) {
4316 starting_offset += num_valid;
4321 case SRDDH10_BLOCK_FORMAT:
4323 struct scsi_defect_desc_block *dlist;
4325 dlist = (struct scsi_defect_desc_block *)
4326 (defect_list + hdr_size);
4328 for (i = 0; i < num_valid; i++) {
4329 if (hex_format == 0)
4330 fprintf(stdout, "%u\n",
4331 scsi_4btoul(dlist[i].address));
4333 fprintf(stdout, "0x%x\n",
4334 scsi_4btoul(dlist[i].address));
4337 if (num_valid < num_returned) {
4338 starting_offset += num_valid;
4344 case SRDD10_LONG_BLOCK_FORMAT:
4346 struct scsi_defect_desc_long_block *dlist;
4348 dlist = (struct scsi_defect_desc_long_block *)
4349 (defect_list + hdr_size);
4351 for (i = 0; i < num_valid; i++) {
4352 if (hex_format == 0)
4353 fprintf(stdout, "%ju\n",
4354 (uintmax_t)scsi_8btou64(
4357 fprintf(stdout, "0x%jx\n",
4358 (uintmax_t)scsi_8btou64(
4362 if (num_valid < num_returned) {
4363 starting_offset += num_valid;
4369 fprintf(stderr, "Unknown defect format 0x%x\n",
4376 if (defect_list != NULL)
4387 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
4391 ccb = cam_getccb(device);
4398 mode_sense(struct cam_device *device, int *cdb_len, int dbd, int llbaa, int pc,
4399 int page, int subpage, int task_attr, int retry_count, int timeout,
4400 u_int8_t *data, int datalen)
4403 int error_code, sense_key, asc, ascq;
4405 ccb = cam_getccb(device);
4407 errx(1, "mode_sense: couldn't allocate CCB");
4411 * MODE SENSE(6) can't handle more then 255 bytes. If there are more,
4412 * device must return error, so we should not get trucated data.
4414 if (*cdb_len == 6 && datalen > 255)
4417 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
4419 scsi_mode_sense_subpage(&ccb->csio,
4420 /* retries */ retry_count,
4422 /* tag_action */ task_attr,
4426 /* subpage */ subpage,
4427 /* param_buf */ data,
4428 /* param_len */ datalen,
4429 /* minimum_cmd_size */ *cdb_len,
4430 /* sense_len */ SSD_FULL_SIZE,
4431 /* timeout */ timeout ? timeout : 5000);
4432 if (llbaa && ccb->csio.cdb_len == 10) {
4433 struct scsi_mode_sense_10 *cdb =
4434 (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
4435 cdb->byte2 |= SMS10_LLBAA;
4438 /* Record what CDB size the above function really set. */
4439 *cdb_len = ccb->csio.cdb_len;
4441 if (arglist & CAM_ARG_ERR_RECOVER)
4442 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
4444 /* Disable freezing the device queue */
4445 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
4447 if (cam_send_ccb(device, ccb) < 0)
4448 err(1, "error sending mode sense command");
4450 /* In case of ILLEGEL REQUEST try to fall back to 6-byte command. */
4451 if (*cdb_len != 6 &&
4452 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID ||
4453 (scsi_extract_sense_ccb(ccb, &error_code, &sense_key, &asc, &ascq)
4454 && sense_key == SSD_KEY_ILLEGAL_REQUEST))) {
4459 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4460 if (arglist & CAM_ARG_VERBOSE) {
4461 cam_error_print(device, ccb, CAM_ESF_ALL,
4462 CAM_EPF_ALL, stderr);
4465 cam_close_device(device);
4466 errx(1, "mode sense command returned error");
4473 mode_select(struct cam_device *device, int cdb_len, int save_pages,
4474 int task_attr, int retry_count, int timeout, u_int8_t *data, int datalen)
4479 ccb = cam_getccb(device);
4482 errx(1, "mode_select: couldn't allocate CCB");
4484 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
4486 scsi_mode_select_len(&ccb->csio,
4487 /* retries */ retry_count,
4489 /* tag_action */ task_attr,
4490 /* scsi_page_fmt */ 1,
4491 /* save_pages */ save_pages,
4492 /* param_buf */ data,
4493 /* param_len */ datalen,
4494 /* minimum_cmd_size */ cdb_len,
4495 /* sense_len */ SSD_FULL_SIZE,
4496 /* timeout */ timeout ? timeout : 5000);
4498 if (arglist & CAM_ARG_ERR_RECOVER)
4499 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
4501 /* Disable freezing the device queue */
4502 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
4504 if (((retval = cam_send_ccb(device, ccb)) < 0)
4505 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
4506 if (arglist & CAM_ARG_VERBOSE) {
4507 cam_error_print(device, ccb, CAM_ESF_ALL,
4508 CAM_EPF_ALL, stderr);
4511 cam_close_device(device);
4514 err(1, "error sending mode select command");
4516 errx(1, "error sending mode select command");
4524 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
4525 int task_attr, int retry_count, int timeout)
4528 int c, page = -1, subpage = 0, pc = 0, llbaa = 0;
4529 int binary = 0, cdb_len = 10, dbd = 0, desc = 0, edit = 0, list = 0;
4531 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4549 str_subpage = optarg;
4550 strsep(&str_subpage, ",");
4551 page = strtol(optarg, NULL, 0);
4553 subpage = strtol(str_subpage, NULL, 0);
4554 if (page < 0 || page > 0x3f)
4555 errx(1, "invalid mode page %d", page);
4556 if (subpage < 0 || subpage > 0xff)
4557 errx(1, "invalid mode subpage %d", subpage);
4566 pc = strtol(optarg, NULL, 0);
4567 if ((pc < 0) || (pc > 3))
4568 errx(1, "invalid page control field %d", pc);
4575 if (desc && page == -1)
4576 page = SMS_ALL_PAGES_PAGE;
4578 if (page == -1 && list == 0)
4579 errx(1, "you must specify a mode page!");
4582 errx(1, "-d and -D are incompatible!");
4584 if (llbaa && cdb_len != 10)
4585 errx(1, "LLBAA bit is not present in MODE SENSE(6)!");
4588 mode_list(device, cdb_len, dbd, pc, list > 1, task_attr,
4589 retry_count, timeout);
4591 mode_edit(device, cdb_len, desc, dbd, llbaa, pc, page, subpage,
4592 edit, binary, task_attr, retry_count, timeout);
4597 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
4598 int task_attr, int retry_count, int timeout)
4601 u_int32_t flags = CAM_DIR_NONE;
4602 u_int8_t *data_ptr = NULL;
4604 u_int8_t atacmd[12];
4605 struct get_hook hook;
4606 int c, data_bytes = 0, valid_bytes;
4612 char *datastr = NULL, *tstr, *resstr = NULL;
4614 int fd_data = 0, fd_res = 0;
4617 ccb = cam_getccb(device);
4620 warnx("scsicmd: error allocating ccb");
4624 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
4626 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4630 while (isspace(*tstr) && (*tstr != '\0'))
4632 hook.argc = argc - optind;
4633 hook.argv = argv + optind;
4635 atacmd_len = buff_encode_visit(atacmd, sizeof(atacmd), tstr,
4638 * Increment optind by the number of arguments the
4639 * encoding routine processed. After each call to
4640 * getopt(3), optind points to the argument that
4641 * getopt should process _next_. In this case,
4642 * that means it points to the first command string
4643 * argument, if there is one. Once we increment
4644 * this, it should point to either the next command
4645 * line argument, or it should be past the end of
4652 while (isspace(*tstr) && (*tstr != '\0'))
4654 hook.argc = argc - optind;
4655 hook.argv = argv + optind;
4657 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
4660 * Increment optind by the number of arguments the
4661 * encoding routine processed. After each call to
4662 * getopt(3), optind points to the argument that
4663 * getopt should process _next_. In this case,
4664 * that means it points to the first command string
4665 * argument, if there is one. Once we increment
4666 * this, it should point to either the next command
4667 * line argument, or it should be past the end of
4679 if (arglist & CAM_ARG_CMD_OUT) {
4680 warnx("command must either be "
4681 "read or write, not both");
4683 goto scsicmd_bailout;
4685 arglist |= CAM_ARG_CMD_IN;
4687 data_bytes = strtol(optarg, NULL, 0);
4688 if (data_bytes <= 0) {
4689 warnx("invalid number of input bytes %d",
4692 goto scsicmd_bailout;
4694 hook.argc = argc - optind;
4695 hook.argv = argv + optind;
4698 datastr = cget(&hook, NULL);
4700 * If the user supplied "-" instead of a format, he
4701 * wants the data to be written to stdout.
4703 if ((datastr != NULL)
4704 && (datastr[0] == '-'))
4707 data_ptr = (u_int8_t *)malloc(data_bytes);
4708 if (data_ptr == NULL) {
4709 warnx("can't malloc memory for data_ptr");
4711 goto scsicmd_bailout;
4715 if (arglist & CAM_ARG_CMD_IN) {
4716 warnx("command must either be "
4717 "read or write, not both");
4719 goto scsicmd_bailout;
4721 arglist |= CAM_ARG_CMD_OUT;
4722 flags = CAM_DIR_OUT;
4723 data_bytes = strtol(optarg, NULL, 0);
4724 if (data_bytes <= 0) {
4725 warnx("invalid number of output bytes %d",
4728 goto scsicmd_bailout;
4730 hook.argc = argc - optind;
4731 hook.argv = argv + optind;
4733 datastr = cget(&hook, NULL);
4734 data_ptr = (u_int8_t *)malloc(data_bytes);
4735 if (data_ptr == NULL) {
4736 warnx("can't malloc memory for data_ptr");
4738 goto scsicmd_bailout;
4740 bzero(data_ptr, data_bytes);
4742 * If the user supplied "-" instead of a format, he
4743 * wants the data to be read from stdin.
4745 if ((datastr != NULL)
4746 && (datastr[0] == '-'))
4749 buff_encode_visit(data_ptr, data_bytes, datastr,
4755 hook.argc = argc - optind;
4756 hook.argv = argv + optind;
4758 resstr = cget(&hook, NULL);
4759 if ((resstr != NULL) && (resstr[0] == '-'))
4769 * If fd_data is set, and we're writing to the device, we need to
4770 * read the data the user wants written from stdin.
4772 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
4774 int amt_to_read = data_bytes;
4775 u_int8_t *buf_ptr = data_ptr;
4777 for (amt_read = 0; amt_to_read > 0;
4778 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
4779 if (amt_read == -1) {
4780 warn("error reading data from stdin");
4782 goto scsicmd_bailout;
4784 amt_to_read -= amt_read;
4785 buf_ptr += amt_read;
4789 if (arglist & CAM_ARG_ERR_RECOVER)
4790 flags |= CAM_PASS_ERR_RECOVER;
4792 /* Disable freezing the device queue */
4793 flags |= CAM_DEV_QFRZDIS;
4797 * This is taken from the SCSI-3 draft spec.
4798 * (T10/1157D revision 0.3)
4799 * The top 3 bits of an opcode are the group code.
4800 * The next 5 bits are the command code.
4801 * Group 0: six byte commands
4802 * Group 1: ten byte commands
4803 * Group 2: ten byte commands
4805 * Group 4: sixteen byte commands
4806 * Group 5: twelve byte commands
4807 * Group 6: vendor specific
4808 * Group 7: vendor specific
4810 switch((cdb[0] >> 5) & 0x7) {
4821 /* computed by buff_encode_visit */
4832 * We should probably use csio_build_visit or something like that
4833 * here, but it's easier to encode arguments as you go. The
4834 * alternative would be skipping the CDB argument and then encoding
4835 * it here, since we've got the data buffer argument by now.
4837 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
4839 cam_fill_csio(&ccb->csio,
4840 /*retries*/ retry_count,
4843 /*tag_action*/ task_attr,
4844 /*data_ptr*/ data_ptr,
4845 /*dxfer_len*/ data_bytes,
4846 /*sense_len*/ SSD_FULL_SIZE,
4847 /*cdb_len*/ cdb_len,
4848 /*timeout*/ timeout ? timeout : 5000);
4851 bcopy(atacmd, &ccb->ataio.cmd.command, atacmd_len);
4853 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
4855 ccb->ataio.cmd.flags |= CAM_ATAIO_DMA;
4857 ccb->ataio.cmd.flags |= CAM_ATAIO_FPDMA;
4859 cam_fill_ataio(&ccb->ataio,
4860 /*retries*/ retry_count,
4864 /*data_ptr*/ data_ptr,
4865 /*dxfer_len*/ data_bytes,
4866 /*timeout*/ timeout ? timeout : 5000);
4869 if (((retval = cam_send_ccb(device, ccb)) < 0)
4870 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
4871 const char warnstr[] = "error sending command";
4878 if (arglist & CAM_ARG_VERBOSE) {
4879 cam_error_print(device, ccb, CAM_ESF_ALL,
4880 CAM_EPF_ALL, stderr);
4884 goto scsicmd_bailout;
4887 if (atacmd_len && need_res) {
4889 buff_decode_visit(&ccb->ataio.res.status, 11, resstr,
4891 fprintf(stdout, "\n");
4894 "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
4895 ccb->ataio.res.status,
4896 ccb->ataio.res.error,
4897 ccb->ataio.res.lba_low,
4898 ccb->ataio.res.lba_mid,
4899 ccb->ataio.res.lba_high,
4900 ccb->ataio.res.device,
4901 ccb->ataio.res.lba_low_exp,
4902 ccb->ataio.res.lba_mid_exp,
4903 ccb->ataio.res.lba_high_exp,
4904 ccb->ataio.res.sector_count,
4905 ccb->ataio.res.sector_count_exp);
4911 valid_bytes = ccb->csio.dxfer_len - ccb->csio.resid;
4913 valid_bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
4914 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
4915 && (arglist & CAM_ARG_CMD_IN)
4916 && (valid_bytes > 0)) {
4918 buff_decode_visit(data_ptr, valid_bytes, datastr,
4920 fprintf(stdout, "\n");
4922 ssize_t amt_written;
4923 int amt_to_write = valid_bytes;
4924 u_int8_t *buf_ptr = data_ptr;
4926 for (amt_written = 0; (amt_to_write > 0) &&
4927 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
4928 amt_to_write -= amt_written;
4929 buf_ptr += amt_written;
4931 if (amt_written == -1) {
4932 warn("error writing data to stdout");
4934 goto scsicmd_bailout;
4935 } else if ((amt_written == 0)
4936 && (amt_to_write > 0)) {
4937 warnx("only wrote %u bytes out of %u",
4938 valid_bytes - amt_to_write, valid_bytes);
4945 if ((data_bytes > 0) && (data_ptr != NULL))
4954 camdebug(int argc, char **argv, char *combinedopt)
4957 path_id_t bus = CAM_BUS_WILDCARD;
4958 target_id_t target = CAM_TARGET_WILDCARD;
4959 lun_id_t lun = CAM_LUN_WILDCARD;
4964 bzero(&ccb, sizeof(union ccb));
4966 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4969 arglist |= CAM_ARG_DEBUG_INFO;
4970 ccb.cdbg.flags |= CAM_DEBUG_INFO;
4973 arglist |= CAM_ARG_DEBUG_PERIPH;
4974 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
4977 arglist |= CAM_ARG_DEBUG_SUBTRACE;
4978 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
4981 arglist |= CAM_ARG_DEBUG_TRACE;
4982 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
4985 arglist |= CAM_ARG_DEBUG_XPT;
4986 ccb.cdbg.flags |= CAM_DEBUG_XPT;
4989 arglist |= CAM_ARG_DEBUG_CDB;
4990 ccb.cdbg.flags |= CAM_DEBUG_CDB;
4993 arglist |= CAM_ARG_DEBUG_PROBE;
4994 ccb.cdbg.flags |= CAM_DEBUG_PROBE;
5005 warnx("you must specify \"off\", \"all\" or a bus,");
5006 warnx("bus:target, bus:target:lun or periph");
5011 while (isspace(*tstr) && (*tstr != '\0'))
5014 if (strncmp(tstr, "off", 3) == 0) {
5015 ccb.cdbg.flags = CAM_DEBUG_NONE;
5016 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
5017 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
5018 CAM_ARG_DEBUG_XPT|CAM_ARG_DEBUG_PROBE);
5020 rv = parse_btl(tstr, &bus, &target, &lun, &arglist);
5022 warnx("you must specify \"all\", \"off\", or a bus,");
5023 warnx("bus:target, bus:target:lun or periph to debug");
5028 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
5029 warnx("error opening transport layer device %s", XPT_DEVICE);
5030 warn("%s", XPT_DEVICE);
5034 ccb.ccb_h.func_code = XPT_DEBUG;
5035 ccb.ccb_h.path_id = bus;
5036 ccb.ccb_h.target_id = target;
5037 ccb.ccb_h.target_lun = lun;
5039 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
5040 warn("CAMIOCOMMAND ioctl failed");
5043 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
5044 CAM_FUNC_NOTAVAIL) {
5045 warnx("CAM debugging not available");
5046 warnx("you need to put options CAMDEBUG in"
5047 " your kernel config file!");
5049 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
5051 warnx("XPT_DEBUG CCB failed with status %#x",
5055 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
5057 "Debugging turned off\n");
5060 "Debugging enabled for "
5062 bus, target, (uintmax_t)lun);
5072 tagcontrol(struct cam_device *device, int argc, char **argv,
5082 ccb = cam_getccb(device);
5085 warnx("tagcontrol: error allocating ccb");
5089 while ((c = getopt(argc, argv, combinedopt)) != -1) {
5092 numtags = strtol(optarg, NULL, 0);
5094 warnx("tag count %d is < 0", numtags);
5096 goto tagcontrol_bailout;
5107 cam_path_string(device, pathstr, sizeof(pathstr));
5110 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->crs);
5111 ccb->ccb_h.func_code = XPT_REL_SIMQ;
5112 ccb->ccb_h.flags = CAM_DEV_QFREEZE;
5113 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
5114 ccb->crs.openings = numtags;
5117 if (cam_send_ccb(device, ccb) < 0) {
5118 warn("error sending XPT_REL_SIMQ CCB");
5120 goto tagcontrol_bailout;
5123 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5124 warnx("XPT_REL_SIMQ CCB failed");
5125 cam_error_print(device, ccb, CAM_ESF_ALL,
5126 CAM_EPF_ALL, stderr);
5128 goto tagcontrol_bailout;
5133 fprintf(stdout, "%stagged openings now %d\n",
5134 pathstr, ccb->crs.openings);
5137 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cgds);
5139 ccb->ccb_h.func_code = XPT_GDEV_STATS;
5141 if (cam_send_ccb(device, ccb) < 0) {
5142 warn("error sending XPT_GDEV_STATS CCB");
5144 goto tagcontrol_bailout;
5147 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5148 warnx("XPT_GDEV_STATS CCB failed");
5149 cam_error_print(device, ccb, CAM_ESF_ALL,
5150 CAM_EPF_ALL, stderr);
5152 goto tagcontrol_bailout;
5155 if (arglist & CAM_ARG_VERBOSE) {
5156 fprintf(stdout, "%s", pathstr);
5157 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
5158 fprintf(stdout, "%s", pathstr);
5159 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
5160 fprintf(stdout, "%s", pathstr);
5161 fprintf(stdout, "allocated %d\n", ccb->cgds.allocated);
5162 fprintf(stdout, "%s", pathstr);
5163 fprintf(stdout, "queued %d\n", ccb->cgds.queued);
5164 fprintf(stdout, "%s", pathstr);
5165 fprintf(stdout, "held %d\n", ccb->cgds.held);
5166 fprintf(stdout, "%s", pathstr);
5167 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
5168 fprintf(stdout, "%s", pathstr);
5169 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
5172 fprintf(stdout, "%s", pathstr);
5173 fprintf(stdout, "device openings: ");
5175 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
5176 ccb->cgds.dev_active);
5186 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
5190 cam_path_string(device, pathstr, sizeof(pathstr));
5192 if (cts->transport == XPORT_SPI) {
5193 struct ccb_trans_settings_spi *spi =
5194 &cts->xport_specific.spi;
5196 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
5198 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
5201 if (spi->sync_offset != 0) {
5204 freq = scsi_calc_syncsrate(spi->sync_period);
5205 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
5206 pathstr, freq / 1000, freq % 1000);
5210 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
5211 fprintf(stdout, "%soffset: %d\n", pathstr,
5215 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
5216 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
5217 (0x01 << spi->bus_width) * 8);
5220 if (spi->valid & CTS_SPI_VALID_DISC) {
5221 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
5222 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
5223 "enabled" : "disabled");
5226 if (cts->transport == XPORT_FC) {
5227 struct ccb_trans_settings_fc *fc =
5228 &cts->xport_specific.fc;
5230 if (fc->valid & CTS_FC_VALID_WWNN)
5231 fprintf(stdout, "%sWWNN: 0x%llx\n", pathstr,
5232 (long long) fc->wwnn);
5233 if (fc->valid & CTS_FC_VALID_WWPN)
5234 fprintf(stdout, "%sWWPN: 0x%llx\n", pathstr,
5235 (long long) fc->wwpn);
5236 if (fc->valid & CTS_FC_VALID_PORT)
5237 fprintf(stdout, "%sPortID: 0x%x\n", pathstr, fc->port);
5238 if (fc->valid & CTS_FC_VALID_SPEED)
5239 fprintf(stdout, "%stransfer speed: %d.%03dMB/s\n",
5240 pathstr, fc->bitrate / 1000, fc->bitrate % 1000);
5242 if (cts->transport == XPORT_SAS) {
5243 struct ccb_trans_settings_sas *sas =
5244 &cts->xport_specific.sas;
5246 if (sas->valid & CTS_SAS_VALID_SPEED)
5247 fprintf(stdout, "%stransfer speed: %d.%03dMB/s\n",
5248 pathstr, sas->bitrate / 1000, sas->bitrate % 1000);
5250 if (cts->transport == XPORT_ATA) {
5251 struct ccb_trans_settings_pata *pata =
5252 &cts->xport_specific.ata;
5254 if ((pata->valid & CTS_ATA_VALID_MODE) != 0) {
5255 fprintf(stdout, "%sATA mode: %s\n", pathstr,
5256 ata_mode2string(pata->mode));
5258 if ((pata->valid & CTS_ATA_VALID_ATAPI) != 0) {
5259 fprintf(stdout, "%sATAPI packet length: %d\n", pathstr,
5262 if ((pata->valid & CTS_ATA_VALID_BYTECOUNT) != 0) {
5263 fprintf(stdout, "%sPIO transaction length: %d\n",
5264 pathstr, pata->bytecount);
5267 if (cts->transport == XPORT_SATA) {
5268 struct ccb_trans_settings_sata *sata =
5269 &cts->xport_specific.sata;
5271 if ((sata->valid & CTS_SATA_VALID_REVISION) != 0) {
5272 fprintf(stdout, "%sSATA revision: %d.x\n", pathstr,
5275 if ((sata->valid & CTS_SATA_VALID_MODE) != 0) {
5276 fprintf(stdout, "%sATA mode: %s\n", pathstr,
5277 ata_mode2string(sata->mode));
5279 if ((sata->valid & CTS_SATA_VALID_ATAPI) != 0) {
5280 fprintf(stdout, "%sATAPI packet length: %d\n", pathstr,
5283 if ((sata->valid & CTS_SATA_VALID_BYTECOUNT) != 0) {
5284 fprintf(stdout, "%sPIO transaction length: %d\n",
5285 pathstr, sata->bytecount);
5287 if ((sata->valid & CTS_SATA_VALID_PM) != 0) {
5288 fprintf(stdout, "%sPMP presence: %d\n", pathstr,
5291 if ((sata->valid & CTS_SATA_VALID_TAGS) != 0) {
5292 fprintf(stdout, "%sNumber of tags: %d\n", pathstr,
5295 if ((sata->valid & CTS_SATA_VALID_CAPS) != 0) {
5296 fprintf(stdout, "%sSATA capabilities: %08x\n", pathstr,
5300 if (cts->protocol == PROTO_ATA) {
5301 struct ccb_trans_settings_ata *ata=
5302 &cts->proto_specific.ata;
5304 if (ata->valid & CTS_ATA_VALID_TQ) {
5305 fprintf(stdout, "%stagged queueing: %s\n", pathstr,
5306 (ata->flags & CTS_ATA_FLAGS_TAG_ENB) ?
5307 "enabled" : "disabled");
5310 if (cts->protocol == PROTO_SCSI) {
5311 struct ccb_trans_settings_scsi *scsi=
5312 &cts->proto_specific.scsi;
5314 if (scsi->valid & CTS_SCSI_VALID_TQ) {
5315 fprintf(stdout, "%stagged queueing: %s\n", pathstr,
5316 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
5317 "enabled" : "disabled");
5321 if (cts->protocol == PROTO_NVME) {
5322 struct ccb_trans_settings_nvme *nvmex =
5323 &cts->xport_specific.nvme;
5325 if (nvmex->valid & CTS_NVME_VALID_SPEC) {
5326 fprintf(stdout, "%sNVMe Spec: %d.%d\n", pathstr,
5327 NVME_MAJOR(nvmex->spec),
5328 NVME_MINOR(nvmex->spec));
5330 if (nvmex->valid & CTS_NVME_VALID_LINK) {
5331 fprintf(stdout, "%sPCIe lanes: %d (%d max)\n", pathstr,
5332 nvmex->lanes, nvmex->max_lanes);
5333 fprintf(stdout, "%sPCIe Generation: %d (%d max)\n", pathstr,
5334 nvmex->speed, nvmex->max_speed);
5341 * Get a path inquiry CCB for the specified device.
5344 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
5349 ccb = cam_getccb(device);
5351 warnx("get_cpi: couldn't allocate CCB");
5354 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cpi);
5355 ccb->ccb_h.func_code = XPT_PATH_INQ;
5356 if (cam_send_ccb(device, ccb) < 0) {
5357 warn("get_cpi: error sending Path Inquiry CCB");
5359 goto get_cpi_bailout;
5361 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5362 if (arglist & CAM_ARG_VERBOSE)
5363 cam_error_print(device, ccb, CAM_ESF_ALL,
5364 CAM_EPF_ALL, stderr);
5366 goto get_cpi_bailout;
5368 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
5376 * Get a get device CCB for the specified device.
5379 get_cgd(struct cam_device *device, struct ccb_getdev *cgd)
5384 ccb = cam_getccb(device);
5386 warnx("get_cgd: couldn't allocate CCB");
5389 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cgd);
5390 ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5391 if (cam_send_ccb(device, ccb) < 0) {
5392 warn("get_cgd: error sending Get type information CCB");
5394 goto get_cgd_bailout;
5396 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5397 if (arglist & CAM_ARG_VERBOSE)
5398 cam_error_print(device, ccb, CAM_ESF_ALL,
5399 CAM_EPF_ALL, stderr);
5401 goto get_cgd_bailout;
5403 bcopy(&ccb->cgd, cgd, sizeof(struct ccb_getdev));
5411 * Returns 1 if the device has the VPD page, 0 if it does not, and -1 on an
5415 dev_has_vpd_page(struct cam_device *dev, uint8_t page_id, int retry_count,
5416 int timeout, int verbosemode)
5418 union ccb *ccb = NULL;
5419 struct scsi_vpd_supported_page_list sup_pages;
5423 ccb = cam_getccb(dev);
5425 warn("Unable to allocate CCB");
5430 /* cam_getccb cleans up the header, caller has to zero the payload */
5431 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
5433 bzero(&sup_pages, sizeof(sup_pages));
5435 scsi_inquiry(&ccb->csio,
5436 /*retries*/ retry_count,
5438 /* tag_action */ MSG_SIMPLE_Q_TAG,
5439 /* inq_buf */ (u_int8_t *)&sup_pages,
5440 /* inq_len */ sizeof(sup_pages),
5442 /* page_code */ SVPD_SUPPORTED_PAGE_LIST,
5443 /* sense_len */ SSD_FULL_SIZE,
5444 /* timeout */ timeout ? timeout : 5000);
5446 /* Disable freezing the device queue */
5447 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
5449 if (retry_count != 0)
5450 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
5452 if (cam_send_ccb(dev, ccb) < 0) {
5459 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5460 if (verbosemode != 0)
5461 cam_error_print(dev, ccb, CAM_ESF_ALL,
5462 CAM_EPF_ALL, stderr);
5467 for (i = 0; i < sup_pages.length; i++) {
5468 if (sup_pages.list[i] == page_id) {
5481 * devtype is filled in with the type of device.
5482 * Returns 0 for success, non-zero for failure.
5485 get_device_type(struct cam_device *dev, int retry_count, int timeout,
5486 int verbosemode, camcontrol_devtype *devtype)
5488 struct ccb_getdev cgd;
5491 retval = get_cgd(dev, &cgd);
5495 switch (cgd.protocol) {
5501 *devtype = CC_DT_ATA;
5503 break; /*NOTREACHED*/
5505 *devtype = CC_DT_NVME;
5507 break; /*NOTREACHED*/
5509 *devtype = CC_DT_MMCSD;
5511 break; /*NOTREACHED*/
5513 *devtype = CC_DT_UNKNOWN;
5515 break; /*NOTREACHED*/
5518 if (retry_count == -1) {
5520 * For a retry count of -1, used only the cached data to avoid
5521 * I/O to the drive. Sending the identify command to the drive
5522 * can cause issues for SATL attachaed drives since identify is
5523 * not an NCQ command.
5525 if (cgd.ident_data.config != 0)
5526 *devtype = CC_DT_SATL;
5528 *devtype = CC_DT_SCSI;
5531 * Check for the ATA Information VPD page (0x89). If this is an
5532 * ATA device behind a SCSI to ATA translation layer (SATL),
5533 * this VPD page should be present.
5535 * If that VPD page isn't present, or we get an error back from
5536 * the INQUIRY command, we'll just treat it as a normal SCSI
5539 retval = dev_has_vpd_page(dev, SVPD_ATA_INFORMATION, retry_count,
5540 timeout, verbosemode);
5542 *devtype = CC_DT_SATL;
5544 *devtype = CC_DT_SCSI;
5553 build_ata_cmd(union ccb *ccb, uint32_t retry_count, uint32_t flags,
5554 uint8_t tag_action, uint8_t protocol, uint8_t ata_flags, uint16_t features,
5555 uint16_t sector_count, uint64_t lba, uint8_t command, uint32_t auxiliary,
5556 uint8_t *data_ptr, uint32_t dxfer_len, uint8_t *cdb_storage,
5557 size_t cdb_storage_len, uint8_t sense_len, uint32_t timeout,
5558 int is48bit, camcontrol_devtype devtype)
5562 if (devtype == CC_DT_ATA) {
5563 cam_fill_ataio(&ccb->ataio,
5564 /*retries*/ retry_count,
5567 /*tag_action*/ tag_action,
5568 /*data_ptr*/ data_ptr,
5569 /*dxfer_len*/ dxfer_len,
5570 /*timeout*/ timeout);
5571 if (is48bit || lba > ATA_MAX_28BIT_LBA)
5572 ata_48bit_cmd(&ccb->ataio, command, features, lba,
5575 ata_28bit_cmd(&ccb->ataio, command, features, lba,
5578 if (auxiliary != 0) {
5579 ccb->ataio.ata_flags |= ATA_FLAG_AUX;
5580 ccb->ataio.aux = auxiliary;
5583 if (ata_flags & AP_FLAG_CHK_COND)
5584 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
5586 if ((protocol & AP_PROTO_MASK) == AP_PROTO_DMA)
5587 ccb->ataio.cmd.flags |= CAM_ATAIO_DMA;
5588 else if ((protocol & AP_PROTO_MASK) == AP_PROTO_FPDMA)
5589 ccb->ataio.cmd.flags |= CAM_ATAIO_FPDMA;
5591 if (is48bit || lba > ATA_MAX_28BIT_LBA)
5592 protocol |= AP_EXTEND;
5594 retval = scsi_ata_pass(&ccb->csio,
5595 /*retries*/ retry_count,
5598 /*tag_action*/ tag_action,
5599 /*protocol*/ protocol,
5600 /*ata_flags*/ ata_flags,
5601 /*features*/ features,
5602 /*sector_count*/ sector_count,
5604 /*command*/ command,
5607 /*auxiliary*/ auxiliary,
5609 /*data_ptr*/ data_ptr,
5610 /*dxfer_len*/ dxfer_len,
5611 /*cdb_storage*/ cdb_storage,
5612 /*cdb_storage_len*/ cdb_storage_len,
5613 /*minimum_cmd_size*/ 0,
5614 /*sense_len*/ sense_len,
5615 /*timeout*/ timeout);
5622 * Returns: 0 -- success, 1 -- error, 2 -- lba truncated,
5623 * 4 -- count truncated, 6 -- lba and count truncated.
5626 get_ata_status(struct cam_device *dev, union ccb *ccb, uint8_t *error,
5627 uint16_t *count, uint64_t *lba, uint8_t *device, uint8_t *status)
5631 switch (ccb->ccb_h.func_code) {
5634 int error_code = 0, sense_key = 0, asc = 0, ascq = 0;
5638 * In this case, we have SCSI ATA PASS-THROUGH command, 12
5639 * or 16 byte, and need to see what
5641 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
5642 opcode = ccb->csio.cdb_io.cdb_ptr[0];
5644 opcode = ccb->csio.cdb_io.cdb_bytes[0];
5645 if ((opcode != ATA_PASS_12)
5646 && (opcode != ATA_PASS_16)) {
5647 warnx("%s: unsupported opcode %02x", __func__, opcode);
5651 retval = scsi_extract_sense_ccb(ccb, &error_code, &sense_key,
5653 /* Note: the _ccb() variant returns 0 for an error */
5657 sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
5658 switch (error_code) {
5659 case SSD_DESC_CURRENT_ERROR:
5660 case SSD_DESC_DEFERRED_ERROR: {
5661 struct scsi_sense_data_desc *sense;
5662 struct scsi_sense_ata_ret_desc *desc;
5665 sense = (struct scsi_sense_data_desc *)
5666 &ccb->csio.sense_data;
5668 desc_ptr = scsi_find_desc(sense, sense_len,
5670 if (desc_ptr == NULL) {
5671 cam_error_print(dev, ccb, CAM_ESF_ALL,
5672 CAM_EPF_ALL, stderr);
5675 desc = (struct scsi_sense_ata_ret_desc *)desc_ptr;
5677 *error = desc->error;
5678 *count = (desc->count_15_8 << 8) |
5680 *lba = ((uint64_t)desc->lba_47_40 << 40) |
5681 ((uint64_t)desc->lba_39_32 << 32) |
5682 ((uint64_t)desc->lba_31_24 << 24) |
5683 (desc->lba_23_16 << 16) |
5684 (desc->lba_15_8 << 8) |
5686 *device = desc->device;
5687 *status = desc->status;
5690 * If the extend bit isn't set, the result is for a
5691 * 12-byte ATA PASS-THROUGH command or a 16 or 32 byte
5692 * command without the extend bit set. This means
5693 * that the device is supposed to return 28-bit
5694 * status. The count field is only 8 bits, and the
5695 * LBA field is only 8 bits.
5697 if ((desc->flags & SSD_DESC_ATA_FLAG_EXTEND) == 0){
5703 case SSD_CURRENT_ERROR:
5704 case SSD_DEFERRED_ERROR: {
5708 * In my understanding of SAT-5 specification, saying:
5709 * "without interpreting the contents of the STATUS",
5710 * this should not happen if CK_COND was set, but it
5711 * does at least for some devices, so try to revert.
5713 if ((sense_key == SSD_KEY_ABORTED_COMMAND) &&
5714 (asc == 0) && (ascq == 0)) {
5715 *status = ATA_STATUS_ERROR;
5716 *error = ATA_ERROR_ABORT;
5723 if ((sense_key != SSD_KEY_RECOVERED_ERROR) ||
5724 (asc != 0x00) || (ascq != 0x1d))
5728 scsi_get_sense_info(&ccb->csio.sense_data, sense_len,
5729 SSD_DESC_INFO, &val, NULL);
5730 *error = (val >> 24) & 0xff;
5731 *status = (val >> 16) & 0xff;
5732 *device = (val >> 8) & 0xff;
5733 *count = val & 0xff;
5736 scsi_get_sense_info(&ccb->csio.sense_data, sense_len,
5737 SSD_DESC_COMMAND, &val, NULL);
5738 *lba = ((val >> 16) & 0xff) | (val & 0xff00) |
5739 ((val & 0xff) << 16);
5741 /* Report UPPER NONZERO bits as errors 2, 4 and 6. */
5742 return ((val >> 28) & 0x06);
5751 struct ata_res *res;
5753 /* Only some statuses return ATA result register set. */
5754 if (cam_ccb_status(ccb) != CAM_REQ_CMP &&
5755 cam_ccb_status(ccb) != CAM_ATA_STATUS_ERROR)
5758 res = &ccb->ataio.res;
5759 *error = res->error;
5760 *status = res->status;
5761 *device = res->device;
5762 *count = res->sector_count;
5763 *lba = (res->lba_high << 16) |
5764 (res->lba_mid << 8) |
5766 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
5767 *count |= (res->sector_count_exp << 8);
5768 *lba |= ((uint64_t)res->lba_low_exp << 24) |
5769 ((uint64_t)res->lba_mid_exp << 32) |
5770 ((uint64_t)res->lba_high_exp << 40);
5772 *lba |= (res->device & 0xf) << 24;
5783 cpi_print(struct ccb_pathinq *cpi)
5785 char adapter_str[1024];
5788 snprintf(adapter_str, sizeof(adapter_str),
5789 "%s%d:", cpi->dev_name, cpi->unit_number);
5791 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
5794 for (i = 1; i < UINT8_MAX; i = i << 1) {
5797 if ((i & cpi->hba_inquiry) == 0)
5800 fprintf(stdout, "%s supports ", adapter_str);
5804 str = "MDP message";
5807 str = "32 bit wide SCSI";
5810 str = "16 bit wide SCSI";
5813 str = "SDTR message";
5816 str = "linked CDBs";
5819 str = "tag queue messages";
5822 str = "soft reset alternative";
5825 str = "SATA Port Multiplier";
5828 str = "unknown PI bit set";
5831 fprintf(stdout, "%s\n", str);
5834 for (i = 1; i < UINT32_MAX; i = i << 1) {
5837 if ((i & cpi->hba_misc) == 0)
5840 fprintf(stdout, "%s ", adapter_str);
5844 str = "can understand ata_ext requests";
5847 str = "64bit extended LUNs supported";
5850 str = "bus scans from high ID to low ID";
5853 str = "removable devices not included in scan";
5855 case PIM_NOINITIATOR:
5856 str = "initiator role not supported";
5858 case PIM_NOBUSRESET:
5859 str = "user has disabled initial BUS RESET or"
5860 " controller is in target/mixed mode";
5863 str = "do not send 6-byte commands";
5866 str = "scan bus sequentially";
5869 str = "unmapped I/O supported";
5872 str = "does its own scanning";
5875 str = "unknown PIM bit set";
5878 fprintf(stdout, "%s\n", str);
5881 for (i = 1; i < UINT16_MAX; i = i << 1) {
5884 if ((i & cpi->target_sprt) == 0)
5887 fprintf(stdout, "%s supports ", adapter_str);
5890 str = "target mode processor mode";
5893 str = "target mode phase cog. mode";
5895 case PIT_DISCONNECT:
5896 str = "disconnects in target mode";
5899 str = "terminate I/O message in target mode";
5902 str = "group 6 commands in target mode";
5905 str = "group 7 commands in target mode";
5908 str = "unknown PIT bit set";
5912 fprintf(stdout, "%s\n", str);
5914 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
5916 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
5918 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
5920 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
5921 adapter_str, cpi->hpath_id);
5922 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
5924 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
5925 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
5926 fprintf(stdout, "%s HBA vendor ID: 0x%04x\n",
5927 adapter_str, cpi->hba_vendor);
5928 fprintf(stdout, "%s HBA device ID: 0x%04x\n",
5929 adapter_str, cpi->hba_device);
5930 fprintf(stdout, "%s HBA subvendor ID: 0x%04x\n",
5931 adapter_str, cpi->hba_subvendor);
5932 fprintf(stdout, "%s HBA subdevice ID: 0x%04x\n",
5933 adapter_str, cpi->hba_subdevice);
5934 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
5935 fprintf(stdout, "%s base transfer speed: ", adapter_str);
5936 if (cpi->base_transfer_speed > 1000)
5937 fprintf(stdout, "%d.%03dMB/sec\n",
5938 cpi->base_transfer_speed / 1000,
5939 cpi->base_transfer_speed % 1000);
5941 fprintf(stdout, "%dKB/sec\n",
5942 (cpi->base_transfer_speed % 1000) * 1000);
5943 fprintf(stdout, "%s maximum transfer size: %u bytes\n",
5944 adapter_str, cpi->maxio);
5948 get_print_cts(struct cam_device *device, int user_settings, int quiet,
5949 struct ccb_trans_settings *cts)
5955 ccb = cam_getccb(device);
5958 warnx("get_print_cts: error allocating ccb");
5962 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cts);
5964 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5966 if (user_settings == 0)
5967 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
5969 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
5971 if (cam_send_ccb(device, ccb) < 0) {
5972 warn("error sending XPT_GET_TRAN_SETTINGS CCB");
5974 goto get_print_cts_bailout;
5977 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5978 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
5979 if (arglist & CAM_ARG_VERBOSE)
5980 cam_error_print(device, ccb, CAM_ESF_ALL,
5981 CAM_EPF_ALL, stderr);
5983 goto get_print_cts_bailout;
5987 cts_print(device, &ccb->cts);
5990 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
5992 get_print_cts_bailout:
6000 ratecontrol(struct cam_device *device, int task_attr, int retry_count,
6001 int timeout, int argc, char **argv, char *combinedopt)
6005 int user_settings = 0;
6007 int disc_enable = -1, tag_enable = -1;
6010 double syncrate = -1;
6013 int change_settings = 0, send_tur = 0;
6014 struct ccb_pathinq cpi;
6016 ccb = cam_getccb(device);
6018 warnx("ratecontrol: error allocating ccb");
6021 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6030 if (strncasecmp(optarg, "enable", 6) == 0)
6032 else if (strncasecmp(optarg, "disable", 7) == 0)
6035 warnx("-D argument \"%s\" is unknown", optarg);
6037 goto ratecontrol_bailout;
6039 change_settings = 1;
6042 mode = ata_string2mode(optarg);
6044 warnx("unknown mode '%s'", optarg);
6046 goto ratecontrol_bailout;
6048 change_settings = 1;
6051 offset = strtol(optarg, NULL, 0);
6053 warnx("offset value %d is < 0", offset);
6055 goto ratecontrol_bailout;
6057 change_settings = 1;
6063 syncrate = atof(optarg);
6065 warnx("sync rate %f is < 0", syncrate);
6067 goto ratecontrol_bailout;
6069 change_settings = 1;
6072 if (strncasecmp(optarg, "enable", 6) == 0)
6074 else if (strncasecmp(optarg, "disable", 7) == 0)
6077 warnx("-T argument \"%s\" is unknown", optarg);
6079 goto ratecontrol_bailout;
6081 change_settings = 1;
6087 bus_width = strtol(optarg, NULL, 0);
6088 if (bus_width < 0) {
6089 warnx("bus width %d is < 0", bus_width);
6091 goto ratecontrol_bailout;
6093 change_settings = 1;
6100 * Grab path inquiry information, so we can determine whether
6101 * or not the initiator is capable of the things that the user
6104 if ((retval = get_cpi(device, &cpi)) != 0)
6105 goto ratecontrol_bailout;
6106 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cts);
6108 fprintf(stdout, "%s parameters:\n",
6109 user_settings ? "User" : "Current");
6111 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
6113 goto ratecontrol_bailout;
6115 if (arglist & CAM_ARG_VERBOSE)
6118 if (change_settings) {
6119 int didsettings = 0;
6120 struct ccb_trans_settings_spi *spi = NULL;
6121 struct ccb_trans_settings_pata *pata = NULL;
6122 struct ccb_trans_settings_sata *sata = NULL;
6123 struct ccb_trans_settings_ata *ata = NULL;
6124 struct ccb_trans_settings_scsi *scsi = NULL;
6126 if (ccb->cts.transport == XPORT_SPI)
6127 spi = &ccb->cts.xport_specific.spi;
6128 if (ccb->cts.transport == XPORT_ATA)
6129 pata = &ccb->cts.xport_specific.ata;
6130 if (ccb->cts.transport == XPORT_SATA)
6131 sata = &ccb->cts.xport_specific.sata;
6132 if (ccb->cts.protocol == PROTO_ATA)
6133 ata = &ccb->cts.proto_specific.ata;
6134 if (ccb->cts.protocol == PROTO_SCSI)
6135 scsi = &ccb->cts.proto_specific.scsi;
6136 ccb->cts.xport_specific.valid = 0;
6137 ccb->cts.proto_specific.valid = 0;
6138 if (spi && disc_enable != -1) {
6139 spi->valid |= CTS_SPI_VALID_DISC;
6140 if (disc_enable == 0)
6141 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6143 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
6146 if (tag_enable != -1) {
6147 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
6148 warnx("HBA does not support tagged queueing, "
6149 "so you cannot modify tag settings");
6151 goto ratecontrol_bailout;
6154 ata->valid |= CTS_SCSI_VALID_TQ;
6155 if (tag_enable == 0)
6156 ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
6158 ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
6161 scsi->valid |= CTS_SCSI_VALID_TQ;
6162 if (tag_enable == 0)
6163 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6165 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
6169 if (spi && offset != -1) {
6170 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6171 warnx("HBA is not capable of changing offset");
6173 goto ratecontrol_bailout;
6175 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
6176 spi->sync_offset = offset;
6179 if (spi && syncrate != -1) {
6180 int prelim_sync_period;
6182 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6183 warnx("HBA is not capable of changing "
6186 goto ratecontrol_bailout;
6188 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
6190 * The sync rate the user gives us is in MHz.
6191 * We need to translate it into KHz for this
6196 * Next, we calculate a "preliminary" sync period
6197 * in tenths of a nanosecond.
6200 prelim_sync_period = 0;
6202 prelim_sync_period = 10000000 / syncrate;
6204 scsi_calc_syncparam(prelim_sync_period);
6207 if (sata && syncrate != -1) {
6208 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6209 warnx("HBA is not capable of changing "
6212 goto ratecontrol_bailout;
6214 if (!user_settings) {
6215 warnx("You can modify only user rate "
6216 "settings for SATA");
6218 goto ratecontrol_bailout;
6220 sata->revision = ata_speed2revision(syncrate * 100);
6221 if (sata->revision < 0) {
6222 warnx("Invalid rate %f", syncrate);
6224 goto ratecontrol_bailout;
6226 sata->valid |= CTS_SATA_VALID_REVISION;
6229 if ((pata || sata) && mode != -1) {
6230 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6231 warnx("HBA is not capable of changing "
6234 goto ratecontrol_bailout;
6236 if (!user_settings) {
6237 warnx("You can modify only user mode "
6238 "settings for ATA/SATA");
6240 goto ratecontrol_bailout;
6244 pata->valid |= CTS_ATA_VALID_MODE;
6247 sata->valid |= CTS_SATA_VALID_MODE;
6252 * The bus_width argument goes like this:
6256 * Therefore, if you shift the number of bits given on the
6257 * command line right by 4, you should get the correct
6260 if (spi && bus_width != -1) {
6262 * We might as well validate things here with a
6263 * decipherable error message, rather than what
6264 * will probably be an indecipherable error message
6265 * by the time it gets back to us.
6267 if ((bus_width == 16)
6268 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
6269 warnx("HBA does not support 16 bit bus width");
6271 goto ratecontrol_bailout;
6272 } else if ((bus_width == 32)
6273 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
6274 warnx("HBA does not support 32 bit bus width");
6276 goto ratecontrol_bailout;
6277 } else if ((bus_width != 8)
6278 && (bus_width != 16)
6279 && (bus_width != 32)) {
6280 warnx("Invalid bus width %d", bus_width);
6282 goto ratecontrol_bailout;
6284 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
6285 spi->bus_width = bus_width >> 4;
6288 if (didsettings == 0) {
6289 goto ratecontrol_bailout;
6291 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6292 if (cam_send_ccb(device, ccb) < 0) {
6293 warn("error sending XPT_SET_TRAN_SETTINGS CCB");
6295 goto ratecontrol_bailout;
6297 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6298 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
6299 if (arglist & CAM_ARG_VERBOSE) {
6300 cam_error_print(device, ccb, CAM_ESF_ALL,
6301 CAM_EPF_ALL, stderr);
6304 goto ratecontrol_bailout;
6308 retval = testunitready(device, task_attr, retry_count, timeout,
6309 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
6311 * If the TUR didn't succeed, just bail.
6315 fprintf(stderr, "Test Unit Ready failed\n");
6316 goto ratecontrol_bailout;
6319 if ((change_settings || send_tur) && !quiet &&
6320 (ccb->cts.transport == XPORT_ATA ||
6321 ccb->cts.transport == XPORT_SATA || send_tur)) {
6322 fprintf(stdout, "New parameters:\n");
6323 retval = get_print_cts(device, user_settings, 0, NULL);
6326 ratecontrol_bailout:
6332 scsiformat(struct cam_device *device, int argc, char **argv,
6333 char *combinedopt, int task_attr, int retry_count, int timeout)
6337 int ycount = 0, quiet = 0;
6338 int error = 0, retval = 0;
6339 int use_timeout = 10800 * 1000;
6341 struct format_defect_list_header fh;
6342 u_int8_t *data_ptr = NULL;
6343 u_int32_t dxfer_len = 0;
6345 int num_warnings = 0;
6348 ccb = cam_getccb(device);
6351 warnx("scsiformat: error allocating ccb");
6355 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6357 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6377 if (quiet == 0 && ycount == 0) {
6378 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
6379 "following device:\n");
6381 error = scsidoinquiry(device, argc, argv, combinedopt,
6382 task_attr, retry_count, timeout);
6385 warnx("scsiformat: error sending inquiry");
6386 goto scsiformat_bailout;
6391 if (!get_confirmation()) {
6393 goto scsiformat_bailout;
6398 use_timeout = timeout;
6401 fprintf(stdout, "Current format timeout is %d seconds\n",
6402 use_timeout / 1000);
6406 * If the user hasn't disabled questions and didn't specify a
6407 * timeout on the command line, ask them if they want the current
6411 && (timeout == 0)) {
6413 int new_timeout = 0;
6415 fprintf(stdout, "Enter new timeout in seconds or press\n"
6416 "return to keep the current timeout [%d] ",
6417 use_timeout / 1000);
6419 if (fgets(str, sizeof(str), stdin) != NULL) {
6421 new_timeout = atoi(str);
6424 if (new_timeout != 0) {
6425 use_timeout = new_timeout * 1000;
6426 fprintf(stdout, "Using new timeout value %d\n",
6427 use_timeout / 1000);
6432 * Keep this outside the if block below to silence any unused
6433 * variable warnings.
6435 bzero(&fh, sizeof(fh));
6438 * If we're in immediate mode, we've got to include the format
6441 if (immediate != 0) {
6442 fh.byte2 = FU_DLH_IMMED;
6443 data_ptr = (u_int8_t *)&fh;
6444 dxfer_len = sizeof(fh);
6445 byte2 = FU_FMT_DATA;
6446 } else if (quiet == 0) {
6447 fprintf(stdout, "Formatting...");
6451 scsi_format_unit(&ccb->csio,
6452 /* retries */ retry_count,
6454 /* tag_action */ task_attr,
6457 /* data_ptr */ data_ptr,
6458 /* dxfer_len */ dxfer_len,
6459 /* sense_len */ SSD_FULL_SIZE,
6460 /* timeout */ use_timeout);
6462 /* Disable freezing the device queue */
6463 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6465 if (arglist & CAM_ARG_ERR_RECOVER)
6466 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
6468 if (((retval = cam_send_ccb(device, ccb)) < 0)
6469 || ((immediate == 0)
6470 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
6471 const char errstr[] = "error sending format command";
6478 if (arglist & CAM_ARG_VERBOSE) {
6479 cam_error_print(device, ccb, CAM_ESF_ALL,
6480 CAM_EPF_ALL, stderr);
6483 goto scsiformat_bailout;
6487 * If we ran in non-immediate mode, we already checked for errors
6488 * above and printed out any necessary information. If we're in
6489 * immediate mode, we need to loop through and get status
6490 * information periodically.
6492 if (immediate == 0) {
6494 fprintf(stdout, "Format Complete\n");
6496 goto scsiformat_bailout;
6503 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6506 * There's really no need to do error recovery or
6507 * retries here, since we're just going to sit in a
6508 * loop and wait for the device to finish formatting.
6510 scsi_test_unit_ready(&ccb->csio,
6513 /* tag_action */ task_attr,
6514 /* sense_len */ SSD_FULL_SIZE,
6515 /* timeout */ 5000);
6517 /* Disable freezing the device queue */
6518 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6520 retval = cam_send_ccb(device, ccb);
6523 * If we get an error from the ioctl, bail out. SCSI
6524 * errors are expected.
6527 warn("error sending TEST UNIT READY command");
6529 goto scsiformat_bailout;
6532 status = ccb->ccb_h.status & CAM_STATUS_MASK;
6534 if ((status != CAM_REQ_CMP)
6535 && (status == CAM_SCSI_STATUS_ERROR)
6536 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
6537 struct scsi_sense_data *sense;
6538 int error_code, sense_key, asc, ascq;
6540 sense = &ccb->csio.sense_data;
6541 scsi_extract_sense_len(sense, ccb->csio.sense_len -
6542 ccb->csio.sense_resid, &error_code, &sense_key,
6543 &asc, &ascq, /*show_errors*/ 1);
6546 * According to the SCSI-2 and SCSI-3 specs, a
6547 * drive that is in the middle of a format should
6548 * return NOT READY with an ASC of "logical unit
6549 * not ready, format in progress". The sense key
6550 * specific bytes will then be a progress indicator.
6552 if ((sense_key == SSD_KEY_NOT_READY)
6553 && (asc == 0x04) && (ascq == 0x04)) {
6556 if ((scsi_get_sks(sense, ccb->csio.sense_len -
6557 ccb->csio.sense_resid, sks) == 0)
6560 u_int64_t percentage;
6562 val = scsi_2btoul(&sks[1]);
6563 percentage = 10000ull * val;
6566 "\rFormatting: %ju.%02u %% "
6568 (uintmax_t)(percentage /
6570 (unsigned)((percentage /
6574 } else if ((quiet == 0)
6575 && (++num_warnings <= 1)) {
6576 warnx("Unexpected SCSI Sense Key "
6577 "Specific value returned "
6579 scsi_sense_print(device, &ccb->csio,
6581 warnx("Unable to print status "
6582 "information, but format will "
6584 warnx("will exit when format is "
6589 warnx("Unexpected SCSI error during format");
6590 cam_error_print(device, ccb, CAM_ESF_ALL,
6591 CAM_EPF_ALL, stderr);
6593 goto scsiformat_bailout;
6596 } else if (status != CAM_REQ_CMP) {
6597 warnx("Unexpected CAM status %#x", status);
6598 if (arglist & CAM_ARG_VERBOSE)
6599 cam_error_print(device, ccb, CAM_ESF_ALL,
6600 CAM_EPF_ALL, stderr);
6602 goto scsiformat_bailout;
6605 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
6608 fprintf(stdout, "\nFormat Complete\n");
6618 sanitize_wait_ata(struct cam_device *device, union ccb *ccb, int quiet,
6619 camcontrol_devtype devtype)
6622 uint8_t error = 0, ata_device = 0, status = 0;
6628 retval = build_ata_cmd(ccb,
6630 /*flags*/ CAM_DIR_NONE,
6631 /*tag_action*/ MSG_SIMPLE_Q_TAG,
6632 /*protocol*/ AP_PROTO_NON_DATA,
6633 /*ata_flags*/ AP_FLAG_CHK_COND,
6634 /*features*/ 0x00, /* SANITIZE STATUS EXT */
6637 /*command*/ ATA_SANITIZE,
6641 /*cdb_storage*/ NULL,
6642 /*cdb_storage_len*/ 0,
6643 /*sense_len*/ SSD_FULL_SIZE,
6646 /*devtype*/ devtype);
6648 warnx("%s: build_ata_cmd() failed, likely "
6649 "programmer error", __func__);
6653 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6654 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
6655 retval = cam_send_ccb(device, ccb);
6657 warn("error sending SANITIZE STATUS EXT command");
6661 retval = get_ata_status(device, ccb, &error, &count, &lba,
6662 &ata_device, &status);
6664 warnx("Can't get SANITIZE STATUS EXT status, "
6665 "sanitize may still run.");
6668 if (status & ATA_STATUS_ERROR) {
6669 if (error & ATA_ERROR_ABORT) {
6670 switch (lba & 0xff) {
6672 warnx("Reason not reported or sanitize failed.");
6675 warnx("Sanitize command unsuccessful. ");
6678 warnx("Unsupported sanitize device command. ");
6681 warnx("Device is in sanitize frozen state. ");
6684 warnx("Sanitize antifreeze lock is enabled. ");
6688 warnx("SANITIZE STATUS EXT failed, "
6689 "sanitize may still run.");
6692 if (count & 0x4000) {
6697 "Sanitizing: %u.%02u%% (%d/%d)\r",
6698 (perc / (0x10000 * 100)),
6699 ((perc / 0x10000) % 100),
6711 sanitize_wait_scsi(struct cam_device *device, union ccb *ccb, int task_attr, int quiet)
6713 int warnings = 0, retval;
6718 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6721 * There's really no need to do error recovery or
6722 * retries here, since we're just going to sit in a
6723 * loop and wait for the device to finish sanitizing.
6725 scsi_test_unit_ready(&ccb->csio,
6728 /* tag_action */ task_attr,
6729 /* sense_len */ SSD_FULL_SIZE,
6730 /* timeout */ 5000);
6732 /* Disable freezing the device queue */
6733 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6735 retval = cam_send_ccb(device, ccb);
6738 * If we get an error from the ioctl, bail out. SCSI
6739 * errors are expected.
6742 warn("error sending TEST UNIT READY command");
6746 status = ccb->ccb_h.status & CAM_STATUS_MASK;
6747 if ((status == CAM_SCSI_STATUS_ERROR) &&
6748 ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
6749 struct scsi_sense_data *sense;
6750 int error_code, sense_key, asc, ascq;
6752 sense = &ccb->csio.sense_data;
6753 scsi_extract_sense_len(sense, ccb->csio.sense_len -
6754 ccb->csio.sense_resid, &error_code, &sense_key,
6755 &asc, &ascq, /*show_errors*/ 1);
6758 * According to the SCSI-3 spec, a drive that is in the
6759 * middle of a sanitize should return NOT READY with an
6760 * ASC of "logical unit not ready, sanitize in
6761 * progress". The sense key specific bytes will then
6762 * be a progress indicator.
6764 if ((sense_key == SSD_KEY_NOT_READY)
6765 && (asc == 0x04) && (ascq == 0x1b)) {
6768 if ((scsi_get_sks(sense, ccb->csio.sense_len -
6769 ccb->csio.sense_resid, sks) == 0)
6771 val = scsi_2btoul(&sks[1]);
6774 "Sanitizing: %u.%02u%% (%d/%d)\r",
6775 (perc / (0x10000 * 100)),
6776 ((perc / 0x10000) % 100),
6779 } else if ((quiet == 0) && (++warnings <= 1)) {
6780 warnx("Unexpected SCSI Sense Key "
6781 "Specific value returned "
6782 "during sanitize:");
6783 scsi_sense_print(device, &ccb->csio,
6785 warnx("Unable to print status "
6786 "information, but sanitze will "
6788 warnx("will exit when sanitize is "
6793 warnx("Unexpected SCSI error during sanitize");
6794 cam_error_print(device, ccb, CAM_ESF_ALL,
6795 CAM_EPF_ALL, stderr);
6799 } else if (status != CAM_REQ_CMP && status != CAM_REQUEUE_REQ) {
6800 warnx("Unexpected CAM status %#x", status);
6801 if (arglist & CAM_ARG_VERBOSE)
6802 cam_error_print(device, ccb, CAM_ESF_ALL,
6803 CAM_EPF_ALL, stderr);
6806 } while ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
6811 sanitize(struct cam_device *device, int argc, char **argv,
6812 char *combinedopt, int task_attr, int retry_count, int timeout)
6815 u_int8_t action = 0;
6817 int ycount = 0, quiet = 0;
6825 const char *pattern = NULL;
6826 u_int8_t *data_ptr = NULL;
6827 u_int32_t dxfer_len = 0;
6829 uint16_t feature, count;
6832 camcontrol_devtype dt;
6835 * Get the device type, request no I/O be done to do this.
6837 error = get_device_type(device, -1, 0, 0, &dt);
6838 if (error != 0 || (unsigned)dt > CC_DT_UNKNOWN) {
6839 warnx("sanitize: can't get device type");
6843 ccb = cam_getccb(device);
6846 warnx("sanitize: error allocating ccb");
6850 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6852 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6855 if (strcasecmp(optarg, "overwrite") == 0)
6856 action = SSZ_SERVICE_ACTION_OVERWRITE;
6857 else if (strcasecmp(optarg, "block") == 0)
6858 action = SSZ_SERVICE_ACTION_BLOCK_ERASE;
6859 else if (strcasecmp(optarg, "crypto") == 0)
6860 action = SSZ_SERVICE_ACTION_CRYPTO_ERASE;
6861 else if (strcasecmp(optarg, "exitfailure") == 0)
6862 action = SSZ_SERVICE_ACTION_EXIT_MODE_FAILURE;
6864 warnx("invalid service operation \"%s\"",
6867 goto sanitize_bailout;
6871 passes = strtol(optarg, NULL, 0);
6872 if (passes < 1 || passes > 31) {
6873 warnx("invalid passes value %d", passes);
6875 goto sanitize_bailout;
6894 /* ATA supports only immediate commands. */
6895 if (dt == CC_DT_SCSI)
6908 warnx("an action is required");
6910 goto sanitize_bailout;
6911 } else if (action == SSZ_SERVICE_ACTION_OVERWRITE) {
6912 struct scsi_sanitize_parameter_list *pl;
6916 if (pattern == NULL) {
6917 warnx("overwrite action requires -P argument");
6919 goto sanitize_bailout;
6921 fd = open(pattern, O_RDONLY);
6923 warn("cannot open pattern file %s", pattern);
6925 goto sanitize_bailout;
6927 if (fstat(fd, &sb) < 0) {
6928 warn("cannot stat pattern file %s", pattern);
6930 goto sanitize_bailout;
6933 if (sz > SSZPL_MAX_PATTERN_LENGTH) {
6934 warnx("pattern file size exceeds maximum value %d",
6935 SSZPL_MAX_PATTERN_LENGTH);
6937 goto sanitize_bailout;
6939 dxfer_len = sizeof(*pl) + sz;
6940 data_ptr = calloc(1, dxfer_len);
6941 if (data_ptr == NULL) {
6942 warnx("cannot allocate parameter list buffer");
6944 goto sanitize_bailout;
6947 amt = read(fd, data_ptr + sizeof(*pl), sz);
6949 warn("cannot read pattern file");
6951 goto sanitize_bailout;
6952 } else if (amt != sz) {
6953 warnx("short pattern file read");
6955 goto sanitize_bailout;
6958 pl = (struct scsi_sanitize_parameter_list *)data_ptr;
6964 pl->byte1 |= SSZPL_INVERT;
6965 scsi_ulto2b(sz, pl->length);
6971 else if (invert != 0)
6973 else if (pattern != NULL)
6978 warnx("%s argument only valid with overwrite "
6981 goto sanitize_bailout;
6985 if (quiet == 0 && ycount == 0) {
6986 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
6987 "following device:\n");
6989 if (dt == CC_DT_SCSI) {
6990 error = scsidoinquiry(device, argc, argv, combinedopt,
6991 task_attr, retry_count, timeout);
6992 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
6993 struct ata_params *ident_buf;
6994 error = ata_do_identify(device, retry_count, timeout,
6997 printf("%s%d: ", device->device_name,
6998 device->dev_unit_num);
6999 ata_print_ident(ident_buf);
7006 warnx("sanitize: error sending inquiry");
7007 goto sanitize_bailout;
7012 if (!get_confirmation()) {
7014 goto sanitize_bailout;
7019 use_timeout = timeout;
7021 use_timeout = (immediate ? 10 : 10800) * 1000;
7023 if (immediate == 0 && quiet == 0) {
7024 fprintf(stdout, "Current sanitize timeout is %d seconds\n",
7025 use_timeout / 1000);
7029 * If the user hasn't disabled questions and didn't specify a
7030 * timeout on the command line, ask them if they want the current
7033 if (immediate == 0 && ycount == 0 && timeout == 0) {
7035 int new_timeout = 0;
7037 fprintf(stdout, "Enter new timeout in seconds or press\n"
7038 "return to keep the current timeout [%d] ",
7039 use_timeout / 1000);
7041 if (fgets(str, sizeof(str), stdin) != NULL) {
7043 new_timeout = atoi(str);
7046 if (new_timeout != 0) {
7047 use_timeout = new_timeout * 1000;
7048 fprintf(stdout, "Using new timeout value %d\n",
7049 use_timeout / 1000);
7053 if (dt == CC_DT_SCSI) {
7056 byte2 |= SSZ_UNRESTRICTED_EXIT;
7059 scsi_sanitize(&ccb->csio,
7060 /* retries */ retry_count,
7062 /* tag_action */ task_attr,
7065 /* data_ptr */ data_ptr,
7066 /* dxfer_len */ dxfer_len,
7067 /* sense_len */ SSD_FULL_SIZE,
7068 /* timeout */ use_timeout);
7070 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7071 if (arglist & CAM_ARG_ERR_RECOVER)
7072 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7073 if (cam_send_ccb(device, ccb) < 0) {
7074 warn("error sending sanitize command");
7076 goto sanitize_bailout;
7078 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
7079 if (action == SSZ_SERVICE_ACTION_OVERWRITE) {
7080 feature = 0x14; /* OVERWRITE EXT */
7081 lba = 0x4F5700000000 | scsi_4btoul(data_ptr + 4);
7082 count = (passes == 0) ? 1 : (passes >= 16) ? 0 : passes;
7084 count |= 0x80; /* INVERT PATTERN */
7086 count |= 0x10; /* FAILURE MODE */
7087 } else if (action == SSZ_SERVICE_ACTION_BLOCK_ERASE) {
7088 feature = 0x12; /* BLOCK ERASE EXT */
7089 lba = 0x0000426B4572;
7092 count |= 0x10; /* FAILURE MODE */
7093 } else if (action == SSZ_SERVICE_ACTION_CRYPTO_ERASE) {
7094 feature = 0x11; /* CRYPTO SCRAMBLE EXT */
7095 lba = 0x000043727970;
7098 count |= 0x10; /* FAILURE MODE */
7099 } else if (action == SSZ_SERVICE_ACTION_EXIT_MODE_FAILURE) {
7100 feature = 0x00; /* SANITIZE STATUS EXT */
7102 count = 1; /* CLEAR SANITIZE OPERATION FAILED */
7105 goto sanitize_bailout;
7108 error = ata_do_cmd(device,
7111 /*flags*/CAM_DIR_NONE,
7112 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
7114 /*tag_action*/MSG_SIMPLE_Q_TAG,
7115 /*command*/ATA_SANITIZE,
7116 /*features*/feature,
7118 /*sector_count*/count,
7121 /*timeout*/ use_timeout,
7125 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7126 struct scsi_sense_data *sense;
7127 int error_code, sense_key, asc, ascq;
7129 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
7130 CAM_SCSI_STATUS_ERROR) {
7131 sense = &ccb->csio.sense_data;
7132 scsi_extract_sense_len(sense, ccb->csio.sense_len -
7133 ccb->csio.sense_resid, &error_code, &sense_key,
7134 &asc, &ascq, /*show_errors*/ 1);
7136 if (sense_key == SSD_KEY_ILLEGAL_REQUEST &&
7137 asc == 0x20 && ascq == 0x00)
7138 warnx("sanitize is not supported by "
7141 warnx("error sanitizing this device");
7143 warnx("error sanitizing this device");
7145 if (arglist & CAM_ARG_VERBOSE) {
7146 cam_error_print(device, ccb, CAM_ESF_ALL,
7147 CAM_EPF_ALL, stderr);
7150 goto sanitize_bailout;
7154 * If we ran in non-immediate mode, we already checked for errors
7155 * above and printed out any necessary information. If we're in
7156 * immediate mode, we need to loop through and get status
7157 * information periodically.
7159 if (immediate == 0) {
7161 fprintf(stdout, "Sanitize Complete\n");
7163 goto sanitize_bailout;
7167 if (dt == CC_DT_SCSI) {
7168 error = sanitize_wait_scsi(device, ccb, task_attr, quiet);
7169 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
7170 error = sanitize_wait_ata(device, ccb, quiet, dt);
7173 if (error == 0 && quiet == 0)
7174 fprintf(stdout, "Sanitize Complete \n");
7179 if (data_ptr != NULL)
7187 scsireportluns(struct cam_device *device, int argc, char **argv,
7188 char *combinedopt, int task_attr, int retry_count, int timeout)
7191 int c, countonly, lunsonly;
7192 struct scsi_report_luns_data *lundata;
7194 uint8_t report_type;
7195 uint32_t list_len, i, j;
7200 report_type = RPL_REPORT_DEFAULT;
7201 ccb = cam_getccb(device);
7204 warnx("%s: error allocating ccb", __func__);
7208 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
7213 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7222 if (strcasecmp(optarg, "default") == 0)
7223 report_type = RPL_REPORT_DEFAULT;
7224 else if (strcasecmp(optarg, "wellknown") == 0)
7225 report_type = RPL_REPORT_WELLKNOWN;
7226 else if (strcasecmp(optarg, "all") == 0)
7227 report_type = RPL_REPORT_ALL;
7229 warnx("%s: invalid report type \"%s\"",
7240 if ((countonly != 0)
7241 && (lunsonly != 0)) {
7242 warnx("%s: you can only specify one of -c or -l", __func__);
7247 * According to SPC-4, the allocation length must be at least 16
7248 * bytes -- enough for the header and one LUN.
7250 alloc_len = sizeof(*lundata) + 8;
7254 lundata = malloc(alloc_len);
7256 if (lundata == NULL) {
7257 warn("%s: error mallocing %d bytes", __func__, alloc_len);
7262 scsi_report_luns(&ccb->csio,
7263 /*retries*/ retry_count,
7265 /*tag_action*/ task_attr,
7266 /*select_report*/ report_type,
7267 /*rpl_buf*/ lundata,
7268 /*alloc_len*/ alloc_len,
7269 /*sense_len*/ SSD_FULL_SIZE,
7270 /*timeout*/ timeout ? timeout : 5000);
7272 /* Disable freezing the device queue */
7273 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7275 if (arglist & CAM_ARG_ERR_RECOVER)
7276 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7278 if (cam_send_ccb(device, ccb) < 0) {
7279 warn("error sending REPORT LUNS command");
7284 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7285 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7291 list_len = scsi_4btoul(lundata->length);
7294 * If we need to list the LUNs, and our allocation
7295 * length was too short, reallocate and retry.
7297 if ((countonly == 0)
7298 && (list_len > (alloc_len - sizeof(*lundata)))) {
7299 alloc_len = list_len + sizeof(*lundata);
7305 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
7306 ((list_len / 8) > 1) ? "s" : "");
7311 for (i = 0; i < (list_len / 8); i++) {
7315 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
7317 fprintf(stdout, ",");
7318 switch (lundata->luns[i].lundata[j] &
7319 RPL_LUNDATA_ATYP_MASK) {
7320 case RPL_LUNDATA_ATYP_PERIPH:
7321 if ((lundata->luns[i].lundata[j] &
7322 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
7323 fprintf(stdout, "%d:",
7324 lundata->luns[i].lundata[j] &
7325 RPL_LUNDATA_PERIPH_BUS_MASK);
7327 && ((lundata->luns[i].lundata[j+2] &
7328 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
7331 fprintf(stdout, "%d",
7332 lundata->luns[i].lundata[j+1]);
7334 case RPL_LUNDATA_ATYP_FLAT: {
7336 tmplun[0] = lundata->luns[i].lundata[j] &
7337 RPL_LUNDATA_FLAT_LUN_MASK;
7338 tmplun[1] = lundata->luns[i].lundata[j+1];
7340 fprintf(stdout, "%d", scsi_2btoul(tmplun));
7344 case RPL_LUNDATA_ATYP_LUN:
7345 fprintf(stdout, "%d:%d:%d",
7346 (lundata->luns[i].lundata[j+1] &
7347 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
7348 lundata->luns[i].lundata[j] &
7349 RPL_LUNDATA_LUN_TARG_MASK,
7350 lundata->luns[i].lundata[j+1] &
7351 RPL_LUNDATA_LUN_LUN_MASK);
7353 case RPL_LUNDATA_ATYP_EXTLUN: {
7354 int field_len_code, eam_code;
7356 eam_code = lundata->luns[i].lundata[j] &
7357 RPL_LUNDATA_EXT_EAM_MASK;
7358 field_len_code = (lundata->luns[i].lundata[j] &
7359 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
7361 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
7362 && (field_len_code == 0x00)) {
7363 fprintf(stdout, "%d",
7364 lundata->luns[i].lundata[j+1]);
7365 } else if ((eam_code ==
7366 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
7367 && (field_len_code == 0x03)) {
7371 * This format takes up all 8 bytes.
7372 * If we aren't starting at offset 0,
7376 fprintf(stdout, "Invalid "
7379 "specified format", j);
7383 bzero(tmp_lun, sizeof(tmp_lun));
7384 bcopy(&lundata->luns[i].lundata[j+1],
7385 &tmp_lun[1], sizeof(tmp_lun) - 1);
7386 fprintf(stdout, "%#jx",
7387 (intmax_t)scsi_8btou64(tmp_lun));
7390 fprintf(stderr, "Unknown Extended LUN"
7391 "Address method %#x, length "
7392 "code %#x", eam_code,
7399 fprintf(stderr, "Unknown LUN address method "
7400 "%#x\n", lundata->luns[i].lundata[0] &
7401 RPL_LUNDATA_ATYP_MASK);
7405 * For the flat addressing method, there are no
7406 * other levels after it.
7411 fprintf(stdout, "\n");
7424 scsireadcapacity(struct cam_device *device, int argc, char **argv,
7425 char *combinedopt, int task_attr, int retry_count, int timeout)
7428 int blocksizeonly, humanize, numblocks, quiet, sizeonly, baseten, longonly;
7429 struct scsi_read_capacity_data rcap;
7430 struct scsi_read_capacity_data_long rcaplong;
7445 ccb = cam_getccb(device);
7448 warnx("%s: error allocating ccb", __func__);
7452 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
7454 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7484 if ((blocksizeonly != 0)
7485 && (numblocks != 0)) {
7486 warnx("%s: you can only specify one of -b or -N", __func__);
7491 if ((blocksizeonly != 0)
7492 && (sizeonly != 0)) {
7493 warnx("%s: you can only specify one of -b or -s", __func__);
7500 warnx("%s: you can only specify one of -h/-H or -q", __func__);
7506 && (blocksizeonly != 0)) {
7507 warnx("%s: you can only specify one of -h/-H or -b", __func__);
7515 scsi_read_capacity(&ccb->csio,
7516 /*retries*/ retry_count,
7518 /*tag_action*/ task_attr,
7521 /*timeout*/ timeout ? timeout : 5000);
7523 /* Disable freezing the device queue */
7524 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7526 if (arglist & CAM_ARG_ERR_RECOVER)
7527 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7529 if (cam_send_ccb(device, ccb) < 0) {
7530 warn("error sending READ CAPACITY command");
7535 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7536 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7541 maxsector = scsi_4btoul(rcap.addr);
7542 block_len = scsi_4btoul(rcap.length);
7545 * A last block of 2^32-1 means that the true capacity is over 2TB,
7546 * and we need to issue the long READ CAPACITY to get the real
7547 * capacity. Otherwise, we're all set.
7549 if (maxsector != 0xffffffff)
7553 scsi_read_capacity_16(&ccb->csio,
7554 /*retries*/ retry_count,
7556 /*tag_action*/ task_attr,
7560 /*rcap_buf*/ (uint8_t *)&rcaplong,
7561 /*rcap_buf_len*/ sizeof(rcaplong),
7562 /*sense_len*/ SSD_FULL_SIZE,
7563 /*timeout*/ timeout ? timeout : 5000);
7565 /* Disable freezing the device queue */
7566 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7568 if (arglist & CAM_ARG_ERR_RECOVER)
7569 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7571 if (cam_send_ccb(device, ccb) < 0) {
7572 warn("error sending READ CAPACITY (16) command");
7577 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7578 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7583 maxsector = scsi_8btou64(rcaplong.addr);
7584 block_len = scsi_4btoul(rcaplong.length);
7587 if (blocksizeonly == 0) {
7589 * Humanize implies !quiet, and also implies numblocks.
7591 if (humanize != 0) {
7596 tmpbytes = (maxsector + 1) * block_len;
7597 ret = humanize_number(tmpstr, sizeof(tmpstr),
7598 tmpbytes, "", HN_AUTOSCALE,
7601 HN_DIVISOR_1000 : 0));
7603 warnx("%s: humanize_number failed!", __func__);
7607 fprintf(stdout, "Device Size: %s%s", tmpstr,
7608 (sizeonly == 0) ? ", " : "\n");
7609 } else if (numblocks != 0) {
7610 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
7611 "Blocks: " : "", (uintmax_t)maxsector + 1,
7612 (sizeonly == 0) ? ", " : "\n");
7614 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
7615 "Last Block: " : "", (uintmax_t)maxsector,
7616 (sizeonly == 0) ? ", " : "\n");
7620 fprintf(stdout, "%s%u%s\n", (quiet == 0) ?
7621 "Block Length: " : "", block_len, (quiet == 0) ?
7630 smpcmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
7631 int retry_count, int timeout)
7635 uint8_t *smp_request = NULL, *smp_response = NULL;
7636 int request_size = 0, response_size = 0;
7637 int fd_request = 0, fd_response = 0;
7638 char *datastr = NULL;
7639 struct get_hook hook;
7644 * Note that at the moment we don't support sending SMP CCBs to
7645 * devices that aren't probed by CAM.
7647 ccb = cam_getccb(device);
7649 warnx("%s: error allocating CCB", __func__);
7653 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
7655 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7658 arglist |= CAM_ARG_CMD_IN;
7659 response_size = strtol(optarg, NULL, 0);
7660 if (response_size <= 0) {
7661 warnx("invalid number of response bytes %d",
7664 goto smpcmd_bailout;
7666 hook.argc = argc - optind;
7667 hook.argv = argv + optind;
7670 datastr = cget(&hook, NULL);
7672 * If the user supplied "-" instead of a format, he
7673 * wants the data to be written to stdout.
7675 if ((datastr != NULL)
7676 && (datastr[0] == '-'))
7679 smp_response = (u_int8_t *)malloc(response_size);
7680 if (smp_response == NULL) {
7681 warn("can't malloc memory for SMP response");
7683 goto smpcmd_bailout;
7687 arglist |= CAM_ARG_CMD_OUT;
7688 request_size = strtol(optarg, NULL, 0);
7689 if (request_size <= 0) {
7690 warnx("invalid number of request bytes %d",
7693 goto smpcmd_bailout;
7695 hook.argc = argc - optind;
7696 hook.argv = argv + optind;
7698 datastr = cget(&hook, NULL);
7699 smp_request = (u_int8_t *)malloc(request_size);
7700 if (smp_request == NULL) {
7701 warn("can't malloc memory for SMP request");
7703 goto smpcmd_bailout;
7705 bzero(smp_request, request_size);
7707 * If the user supplied "-" instead of a format, he
7708 * wants the data to be read from stdin.
7710 if ((datastr != NULL)
7711 && (datastr[0] == '-'))
7714 buff_encode_visit(smp_request, request_size,
7725 * If fd_data is set, and we're writing to the device, we need to
7726 * read the data the user wants written from stdin.
7728 if ((fd_request == 1) && (arglist & CAM_ARG_CMD_OUT)) {
7730 int amt_to_read = request_size;
7731 u_int8_t *buf_ptr = smp_request;
7733 for (amt_read = 0; amt_to_read > 0;
7734 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
7735 if (amt_read == -1) {
7736 warn("error reading data from stdin");
7738 goto smpcmd_bailout;
7740 amt_to_read -= amt_read;
7741 buf_ptr += amt_read;
7745 if (((arglist & CAM_ARG_CMD_IN) == 0)
7746 || ((arglist & CAM_ARG_CMD_OUT) == 0)) {
7747 warnx("%s: need both the request (-r) and response (-R) "
7748 "arguments", __func__);
7750 goto smpcmd_bailout;
7753 flags |= CAM_DEV_QFRZDIS;
7755 cam_fill_smpio(&ccb->smpio,
7756 /*retries*/ retry_count,
7759 /*smp_request*/ smp_request,
7760 /*smp_request_len*/ request_size,
7761 /*smp_response*/ smp_response,
7762 /*smp_response_len*/ response_size,
7763 /*timeout*/ timeout ? timeout : 5000);
7765 ccb->smpio.flags = SMP_FLAG_NONE;
7767 if (((retval = cam_send_ccb(device, ccb)) < 0)
7768 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7769 const char warnstr[] = "error sending command";
7776 if (arglist & CAM_ARG_VERBOSE) {
7777 cam_error_print(device, ccb, CAM_ESF_ALL,
7778 CAM_EPF_ALL, stderr);
7782 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
7783 && (response_size > 0)) {
7784 if (fd_response == 0) {
7785 buff_decode_visit(smp_response, response_size,
7786 datastr, arg_put, NULL);
7787 fprintf(stdout, "\n");
7789 ssize_t amt_written;
7790 int amt_to_write = response_size;
7791 u_int8_t *buf_ptr = smp_response;
7793 for (amt_written = 0; (amt_to_write > 0) &&
7794 (amt_written = write(STDOUT_FILENO, buf_ptr,
7795 amt_to_write)) > 0;){
7796 amt_to_write -= amt_written;
7797 buf_ptr += amt_written;
7799 if (amt_written == -1) {
7800 warn("error writing data to stdout");
7802 goto smpcmd_bailout;
7803 } else if ((amt_written == 0)
7804 && (amt_to_write > 0)) {
7805 warnx("only wrote %u bytes out of %u",
7806 response_size - amt_to_write,
7815 if (smp_request != NULL)
7818 if (smp_response != NULL)
7825 mmcsdcmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
7826 int retry_count, int timeout)
7830 int32_t mmc_opcode = 0, mmc_arg = 0;
7831 int32_t mmc_flags = -1;
7834 int is_bw_4 = 0, is_bw_1 = 0;
7835 int is_frequency = 0;
7836 int is_highspeed = 0, is_stdspeed = 0;
7837 int is_info_request = 0;
7839 uint8_t mmc_data_byte = 0;
7840 uint32_t mmc_frequency = 0;
7842 /* For IO_RW_EXTENDED command */
7843 uint8_t *mmc_data = NULL;
7844 struct mmc_data mmc_d;
7845 int mmc_data_len = 0;
7848 * Note that at the moment we don't support sending SMP CCBs to
7849 * devices that aren't probed by CAM.
7851 ccb = cam_getccb(device);
7853 warnx("%s: error allocating CCB", __func__);
7857 bzero(&(&ccb->ccb_h)[1],
7858 sizeof(union ccb) - sizeof(struct ccb_hdr));
7860 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7869 if (!strcmp(optarg, "high"))
7875 is_info_request = 1;
7879 mmc_frequency = strtol(optarg, NULL, 0);
7882 mmc_opcode = strtol(optarg, NULL, 0);
7883 if (mmc_opcode < 0) {
7884 warnx("invalid MMC opcode %d",
7887 goto mmccmd_bailout;
7891 mmc_arg = strtol(optarg, NULL, 0);
7893 warnx("invalid MMC arg %d",
7896 goto mmccmd_bailout;
7900 mmc_flags = strtol(optarg, NULL, 0);
7901 if (mmc_flags < 0) {
7902 warnx("invalid MMC flags %d",
7905 goto mmccmd_bailout;
7909 mmc_data_len = strtol(optarg, NULL, 0);
7910 if (mmc_data_len <= 0) {
7911 warnx("invalid MMC data len %d",
7914 goto mmccmd_bailout;
7921 mmc_data_byte = strtol(optarg, NULL, 0);
7927 flags |= CAM_DEV_QFRZDIS; /* masks are broken?! */
7929 /* If flags are left default, supply the right flags */
7931 switch (mmc_opcode) {
7932 case MMC_GO_IDLE_STATE:
7933 mmc_flags = MMC_RSP_NONE | MMC_CMD_BC;
7935 case IO_SEND_OP_COND:
7936 mmc_flags = MMC_RSP_R4;
7938 case SD_SEND_RELATIVE_ADDR:
7939 mmc_flags = MMC_RSP_R6 | MMC_CMD_BCR;
7941 case MMC_SELECT_CARD:
7942 mmc_flags = MMC_RSP_R1B | MMC_CMD_AC;
7943 mmc_arg = mmc_arg << 16;
7945 case SD_IO_RW_DIRECT:
7946 mmc_flags = MMC_RSP_R5 | MMC_CMD_AC;
7947 mmc_arg = SD_IO_RW_ADR(mmc_arg);
7949 mmc_arg |= SD_IO_RW_WR | SD_IO_RW_RAW | SD_IO_RW_DAT(mmc_data_byte);
7951 case SD_IO_RW_EXTENDED:
7952 mmc_flags = MMC_RSP_R5 | MMC_CMD_ADTC;
7953 mmc_arg = SD_IO_RW_ADR(mmc_arg);
7954 int len_arg = mmc_data_len;
7955 if (mmc_data_len == 512)
7959 mmc_arg |= SD_IOE_RW_LEN(len_arg) | SD_IO_RW_INCR;
7961 // mmc_arg |= SD_IOE_RW_BLK | SD_IOE_RW_LEN(len_arg) | SD_IO_RW_INCR;
7964 mmc_flags = MMC_RSP_R1;
7968 // Switch bus width instead of sending IO command
7969 if (is_bw_4 || is_bw_1) {
7970 struct ccb_trans_settings_mmc *cts;
7971 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
7972 ccb->ccb_h.flags = 0;
7973 cts = &ccb->cts.proto_specific.mmc;
7974 cts->ios.bus_width = is_bw_4 == 1 ? bus_width_4 : bus_width_1;
7975 cts->ios_valid = MMC_BW;
7976 if (((retval = cam_send_ccb(device, ccb)) < 0)
7977 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7978 warn("Error sending command");
7980 printf("Parameters set OK\n");
7987 struct ccb_trans_settings_mmc *cts;
7988 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
7989 ccb->ccb_h.flags = 0;
7990 cts = &ccb->cts.proto_specific.mmc;
7991 cts->ios.clock = mmc_frequency;
7992 cts->ios_valid = MMC_CLK;
7993 if (((retval = cam_send_ccb(device, ccb)) < 0)
7994 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7995 warn("Error sending command");
7997 printf("Parameters set OK\n");
8003 // Switch bus speed instead of sending IO command
8004 if (is_stdspeed || is_highspeed) {
8005 struct ccb_trans_settings_mmc *cts;
8006 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
8007 ccb->ccb_h.flags = 0;
8008 cts = &ccb->cts.proto_specific.mmc;
8009 cts->ios.timing = is_highspeed == 1 ? bus_timing_hs : bus_timing_normal;
8010 cts->ios_valid = MMC_BT;
8011 if (((retval = cam_send_ccb(device, ccb)) < 0)
8012 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8013 warn("Error sending command");
8015 printf("Speed set OK (HS: %d)\n", is_highspeed);
8021 // Get information about controller and its settings
8022 if (is_info_request) {
8023 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
8024 ccb->ccb_h.flags = 0;
8025 struct ccb_trans_settings_mmc *cts;
8026 cts = &ccb->cts.proto_specific.mmc;
8027 if (((retval = cam_send_ccb(device, ccb)) < 0)
8028 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8029 warn("Error sending command");
8032 printf("Host controller information\n");
8033 printf("Host OCR: 0x%x\n", cts->host_ocr);
8034 printf("Min frequency: %u KHz\n", cts->host_f_min / 1000);
8035 printf("Max frequency: %u MHz\n", cts->host_f_max / 1000000);
8036 printf("Supported bus width:\n");
8037 if (cts->host_caps & MMC_CAP_4_BIT_DATA)
8039 if (cts->host_caps & MMC_CAP_8_BIT_DATA)
8042 printf("Supported operating modes:\n");
8043 if (cts->host_caps & MMC_CAP_HSPEED)
8044 printf(" Can do High Speed transfers\n");
8045 if (cts->host_caps & MMC_CAP_UHS_SDR12)
8046 printf(" Can do UHS SDR12\n");
8047 if (cts->host_caps & MMC_CAP_UHS_SDR25)
8048 printf(" Can do UHS SDR25\n");
8049 if (cts->host_caps & MMC_CAP_UHS_SDR50)
8050 printf(" Can do UHS SDR50\n");
8051 if (cts->host_caps & MMC_CAP_UHS_SDR104)
8052 printf(" Can do UHS SDR104\n");
8053 if (cts->host_caps & MMC_CAP_UHS_DDR50)
8054 printf(" Can do UHS DDR50\n");
8055 if (cts->host_caps & MMC_CAP_MMC_DDR52_120)
8056 printf(" Can do eMMC DDR52 at 1.2V\n");
8057 if (cts->host_caps & MMC_CAP_MMC_DDR52_180)
8058 printf(" Can do eMMC DDR52 at 1.8V\n");
8059 if (cts->host_caps & MMC_CAP_MMC_HS200_120)
8060 printf(" Can do eMMC HS200 at 1.2V\n");
8061 if (cts->host_caps & MMC_CAP_MMC_HS200_180)
8062 printf(" Can do eMMC HS200 at 1.8V\n");
8063 if (cts->host_caps & MMC_CAP_MMC_HS400_120)
8064 printf(" Can do eMMC HS400 at 1.2V\n");
8065 if (cts->host_caps & MMC_CAP_MMC_HS400_180)
8066 printf(" Can do eMMC HS400 at 1.8V\n");
8068 printf("Supported VCCQ voltages:\n");
8069 if (cts->host_caps & MMC_CAP_SIGNALING_120)
8071 if (cts->host_caps & MMC_CAP_SIGNALING_180)
8073 if (cts->host_caps & MMC_CAP_SIGNALING_330)
8076 printf("Current settings:\n");
8077 printf(" Bus width: ");
8078 switch (cts->ios.bus_width) {
8089 printf(" Freq: %d.%03d MHz%s\n",
8090 cts->ios.clock / 1000000,
8091 (cts->ios.clock / 1000) % 1000,
8092 cts->ios.timing == bus_timing_hs ? " (high-speed timing)" : "");
8095 switch (cts->ios.vccq) {
8109 printf("CMD %d arg %d flags %02x\n", mmc_opcode, mmc_arg, mmc_flags);
8111 if (mmc_data_len > 0) {
8112 flags |= CAM_DIR_IN;
8113 mmc_data = malloc(mmc_data_len);
8114 memset(mmc_data, 0, mmc_data_len);
8115 memset(&mmc_d, 0, sizeof(mmc_d));
8116 mmc_d.len = mmc_data_len;
8117 mmc_d.data = mmc_data;
8118 mmc_d.flags = MMC_DATA_READ;
8119 } else flags |= CAM_DIR_NONE;
8121 cam_fill_mmcio(&ccb->mmcio,
8122 /*retries*/ retry_count,
8125 /*mmc_opcode*/ mmc_opcode,
8126 /*mmc_arg*/ mmc_arg,
8127 /*mmc_flags*/ mmc_flags,
8128 /*mmc_data*/ mmc_data_len > 0 ? &mmc_d : NULL,
8129 /*timeout*/ timeout ? timeout : 5000);
8131 if (((retval = cam_send_ccb(device, ccb)) < 0)
8132 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8133 const char warnstr[] = "error sending command";
8140 if (arglist & CAM_ARG_VERBOSE) {
8141 cam_error_print(device, ccb, CAM_ESF_ALL,
8142 CAM_EPF_ALL, stderr);
8146 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) {
8147 printf("MMCIO: error %d, %08x %08x %08x %08x\n",
8148 ccb->mmcio.cmd.error, ccb->mmcio.cmd.resp[0],
8149 ccb->mmcio.cmd.resp[1],
8150 ccb->mmcio.cmd.resp[2],
8151 ccb->mmcio.cmd.resp[3]);
8153 switch (mmc_opcode) {
8154 case SD_IO_RW_DIRECT:
8155 printf("IO_RW_DIRECT: resp byte %02x, cur state %d\n",
8156 SD_R5_DATA(ccb->mmcio.cmd.resp),
8157 (ccb->mmcio.cmd.resp[0] >> 12) & 0x3);
8159 case SD_IO_RW_EXTENDED:
8160 printf("IO_RW_EXTENDED: read %d bytes w/o error:\n", mmc_data_len);
8161 hexdump(mmc_data, mmc_data_len, NULL, 0);
8163 case SD_SEND_RELATIVE_ADDR:
8164 printf("SEND_RELATIVE_ADDR: published RCA %02x\n", ccb->mmcio.cmd.resp[0] >> 16);
8167 printf("No command-specific decoder for CMD %d\n", mmc_opcode);
8174 if (mmc_data_len > 0 && mmc_data != NULL)
8181 smpreportgeneral(struct cam_device *device, int argc, char **argv,
8182 char *combinedopt, int retry_count, int timeout)
8185 struct smp_report_general_request *request = NULL;
8186 struct smp_report_general_response *response = NULL;
8187 struct sbuf *sb = NULL;
8189 int c, long_response = 0;
8193 * Note that at the moment we don't support sending SMP CCBs to
8194 * devices that aren't probed by CAM.
8196 ccb = cam_getccb(device);
8198 warnx("%s: error allocating CCB", __func__);
8202 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8204 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8213 request = malloc(sizeof(*request));
8214 if (request == NULL) {
8215 warn("%s: unable to allocate %zd bytes", __func__,
8221 response = malloc(sizeof(*response));
8222 if (response == NULL) {
8223 warn("%s: unable to allocate %zd bytes", __func__,
8230 smp_report_general(&ccb->smpio,
8234 /*request_len*/ sizeof(*request),
8235 (uint8_t *)response,
8236 /*response_len*/ sizeof(*response),
8237 /*long_response*/ long_response,
8240 if (((retval = cam_send_ccb(device, ccb)) < 0)
8241 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8242 const char warnstr[] = "error sending command";
8249 if (arglist & CAM_ARG_VERBOSE) {
8250 cam_error_print(device, ccb, CAM_ESF_ALL,
8251 CAM_EPF_ALL, stderr);
8258 * If the device supports the long response bit, try again and see
8259 * if we can get all of the data.
8261 if ((response->long_response & SMP_RG_LONG_RESPONSE)
8262 && (long_response == 0)) {
8263 ccb->ccb_h.status = CAM_REQ_INPROG;
8264 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8270 * XXX KDM detect and decode SMP errors here.
8272 sb = sbuf_new_auto();
8274 warnx("%s: error allocating sbuf", __func__);
8278 smp_report_general_sbuf(response, sizeof(*response), sb);
8280 if (sbuf_finish(sb) != 0) {
8281 warnx("%s: sbuf_finish", __func__);
8285 printf("%s", sbuf_data(sb));
8291 if (request != NULL)
8294 if (response != NULL)
8303 static struct camcontrol_opts phy_ops[] = {
8304 {"nop", SMP_PC_PHY_OP_NOP, CAM_ARG_NONE, NULL},
8305 {"linkreset", SMP_PC_PHY_OP_LINK_RESET, CAM_ARG_NONE, NULL},
8306 {"hardreset", SMP_PC_PHY_OP_HARD_RESET, CAM_ARG_NONE, NULL},
8307 {"disable", SMP_PC_PHY_OP_DISABLE, CAM_ARG_NONE, NULL},
8308 {"clearerrlog", SMP_PC_PHY_OP_CLEAR_ERR_LOG, CAM_ARG_NONE, NULL},
8309 {"clearaffiliation", SMP_PC_PHY_OP_CLEAR_AFFILIATON, CAM_ARG_NONE,NULL},
8310 {"sataportsel", SMP_PC_PHY_OP_TRANS_SATA_PSS, CAM_ARG_NONE, NULL},
8311 {"clearitnl", SMP_PC_PHY_OP_CLEAR_STP_ITN_LS, CAM_ARG_NONE, NULL},
8312 {"setdevname", SMP_PC_PHY_OP_SET_ATT_DEV_NAME, CAM_ARG_NONE, NULL},
8317 smpphycontrol(struct cam_device *device, int argc, char **argv,
8318 char *combinedopt, int retry_count, int timeout)
8321 struct smp_phy_control_request *request = NULL;
8322 struct smp_phy_control_response *response = NULL;
8323 int long_response = 0;
8326 uint32_t phy_operation = SMP_PC_PHY_OP_NOP;
8328 uint64_t attached_dev_name = 0;
8329 int dev_name_set = 0;
8330 uint32_t min_plr = 0, max_plr = 0;
8331 uint32_t pp_timeout_val = 0;
8332 int slumber_partial = 0;
8333 int set_pp_timeout_val = 0;
8337 * Note that at the moment we don't support sending SMP CCBs to
8338 * devices that aren't probed by CAM.
8340 ccb = cam_getccb(device);
8342 warnx("%s: error allocating CCB", __func__);
8346 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8348 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8356 if (strcasecmp(optarg, "enable") == 0)
8358 else if (strcasecmp(optarg, "disable") == 0)
8361 warnx("%s: Invalid argument %s", __func__,
8368 slumber_partial |= enable <<
8369 SMP_PC_SAS_SLUMBER_SHIFT;
8372 slumber_partial |= enable <<
8373 SMP_PC_SAS_PARTIAL_SHIFT;
8376 slumber_partial |= enable <<
8377 SMP_PC_SATA_SLUMBER_SHIFT;
8380 slumber_partial |= enable <<
8381 SMP_PC_SATA_PARTIAL_SHIFT;
8384 warnx("%s: programmer error", __func__);
8387 break; /*NOTREACHED*/
8392 attached_dev_name = (uintmax_t)strtoumax(optarg,
8401 * We don't do extensive checking here, so this
8402 * will continue to work when new speeds come out.
8404 min_plr = strtoul(optarg, NULL, 0);
8406 || (min_plr > 0xf)) {
8407 warnx("%s: invalid link rate %x",
8415 * We don't do extensive checking here, so this
8416 * will continue to work when new speeds come out.
8418 max_plr = strtoul(optarg, NULL, 0);
8420 || (max_plr > 0xf)) {
8421 warnx("%s: invalid link rate %x",
8428 camcontrol_optret optreturn;
8429 cam_argmask argnums;
8432 if (phy_op_set != 0) {
8433 warnx("%s: only one phy operation argument "
8434 "(-o) allowed", __func__);
8442 * Allow the user to specify the phy operation
8443 * numerically, as well as with a name. This will
8444 * future-proof it a bit, so options that are added
8445 * in future specs can be used.
8447 if (isdigit(optarg[0])) {
8448 phy_operation = strtoul(optarg, NULL, 0);
8449 if ((phy_operation == 0)
8450 || (phy_operation > 0xff)) {
8451 warnx("%s: invalid phy operation %#x",
8452 __func__, phy_operation);
8458 optreturn = getoption(phy_ops, optarg, &phy_operation,
8461 if (optreturn == CC_OR_AMBIGUOUS) {
8462 warnx("%s: ambiguous option %s", __func__,
8467 } else if (optreturn == CC_OR_NOT_FOUND) {
8468 warnx("%s: option %s not found", __func__,
8480 pp_timeout_val = strtoul(optarg, NULL, 0);
8481 if (pp_timeout_val > 15) {
8482 warnx("%s: invalid partial pathway timeout "
8483 "value %u, need a value less than 16",
8484 __func__, pp_timeout_val);
8488 set_pp_timeout_val = 1;
8496 warnx("%s: a PHY (-p phy) argument is required",__func__);
8501 if (((dev_name_set != 0)
8502 && (phy_operation != SMP_PC_PHY_OP_SET_ATT_DEV_NAME))
8503 || ((phy_operation == SMP_PC_PHY_OP_SET_ATT_DEV_NAME)
8504 && (dev_name_set == 0))) {
8505 warnx("%s: -d name and -o setdevname arguments both "
8506 "required to set device name", __func__);
8511 request = malloc(sizeof(*request));
8512 if (request == NULL) {
8513 warn("%s: unable to allocate %zd bytes", __func__,
8519 response = malloc(sizeof(*response));
8520 if (response == NULL) {
8521 warn("%s: unable to allocate %zd bytes", __func__,
8527 smp_phy_control(&ccb->smpio,
8532 (uint8_t *)response,
8535 /*expected_exp_change_count*/ 0,
8538 (set_pp_timeout_val != 0) ? 1 : 0,
8546 if (((retval = cam_send_ccb(device, ccb)) < 0)
8547 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8548 const char warnstr[] = "error sending command";
8555 if (arglist & CAM_ARG_VERBOSE) {
8557 * Use CAM_EPF_NORMAL so we only get one line of
8558 * SMP command decoding.
8560 cam_error_print(device, ccb, CAM_ESF_ALL,
8561 CAM_EPF_NORMAL, stderr);
8567 /* XXX KDM print out something here for success? */
8572 if (request != NULL)
8575 if (response != NULL)
8582 smpmaninfo(struct cam_device *device, int argc, char **argv,
8583 char *combinedopt, int retry_count, int timeout)
8586 struct smp_report_manuf_info_request request;
8587 struct smp_report_manuf_info_response response;
8588 struct sbuf *sb = NULL;
8589 int long_response = 0;
8594 * Note that at the moment we don't support sending SMP CCBs to
8595 * devices that aren't probed by CAM.
8597 ccb = cam_getccb(device);
8599 warnx("%s: error allocating CCB", __func__);
8603 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8605 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8614 bzero(&request, sizeof(request));
8615 bzero(&response, sizeof(response));
8617 smp_report_manuf_info(&ccb->smpio,
8622 (uint8_t *)&response,
8627 if (((retval = cam_send_ccb(device, ccb)) < 0)
8628 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8629 const char warnstr[] = "error sending command";
8636 if (arglist & CAM_ARG_VERBOSE) {
8637 cam_error_print(device, ccb, CAM_ESF_ALL,
8638 CAM_EPF_ALL, stderr);
8644 sb = sbuf_new_auto();
8646 warnx("%s: error allocating sbuf", __func__);
8650 smp_report_manuf_info_sbuf(&response, sizeof(response), sb);
8652 if (sbuf_finish(sb) != 0) {
8653 warnx("%s: sbuf_finish", __func__);
8657 printf("%s", sbuf_data(sb));
8671 getdevid(struct cam_devitem *item)
8674 union ccb *ccb = NULL;
8676 struct cam_device *dev;
8678 dev = cam_open_btl(item->dev_match.path_id,
8679 item->dev_match.target_id,
8680 item->dev_match.target_lun, O_RDWR, NULL);
8683 warnx("%s", cam_errbuf);
8688 item->device_id_len = 0;
8690 ccb = cam_getccb(dev);
8692 warnx("%s: error allocating CCB", __func__);
8697 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cdai);
8700 * On the first try, we just probe for the size of the data, and
8701 * then allocate that much memory and try again.
8704 ccb->ccb_h.func_code = XPT_DEV_ADVINFO;
8705 ccb->ccb_h.flags = CAM_DIR_IN;
8706 ccb->cdai.flags = CDAI_FLAG_NONE;
8707 ccb->cdai.buftype = CDAI_TYPE_SCSI_DEVID;
8708 ccb->cdai.bufsiz = item->device_id_len;
8709 if (item->device_id_len != 0)
8710 ccb->cdai.buf = (uint8_t *)item->device_id;
8712 if (cam_send_ccb(dev, ccb) < 0) {
8713 warn("%s: error sending XPT_GDEV_ADVINFO CCB", __func__);
8718 if (ccb->ccb_h.status != CAM_REQ_CMP) {
8719 warnx("%s: CAM status %#x", __func__, ccb->ccb_h.status);
8724 if (item->device_id_len == 0) {
8726 * This is our first time through. Allocate the buffer,
8727 * and then go back to get the data.
8729 if (ccb->cdai.provsiz == 0) {
8730 warnx("%s: invalid .provsiz field returned with "
8731 "XPT_GDEV_ADVINFO CCB", __func__);
8735 item->device_id_len = ccb->cdai.provsiz;
8736 item->device_id = malloc(item->device_id_len);
8737 if (item->device_id == NULL) {
8738 warn("%s: unable to allocate %d bytes", __func__,
8739 item->device_id_len);
8743 ccb->ccb_h.status = CAM_REQ_INPROG;
8749 cam_close_device(dev);
8758 * XXX KDM merge this code with getdevtree()?
8761 buildbusdevlist(struct cam_devlist *devlist)
8764 int bufsize, fd = -1;
8765 struct dev_match_pattern *patterns;
8766 struct cam_devitem *item = NULL;
8767 int skip_device = 0;
8770 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
8771 warn("couldn't open %s", XPT_DEVICE);
8775 bzero(&ccb, sizeof(union ccb));
8777 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
8778 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
8779 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
8781 ccb.ccb_h.func_code = XPT_DEV_MATCH;
8782 bufsize = sizeof(struct dev_match_result) * 100;
8783 ccb.cdm.match_buf_len = bufsize;
8784 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
8785 if (ccb.cdm.matches == NULL) {
8786 warnx("can't malloc memory for matches");
8790 ccb.cdm.num_matches = 0;
8791 ccb.cdm.num_patterns = 2;
8792 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern) *
8793 ccb.cdm.num_patterns;
8795 patterns = (struct dev_match_pattern *)malloc(ccb.cdm.pattern_buf_len);
8796 if (patterns == NULL) {
8797 warnx("can't malloc memory for patterns");
8802 ccb.cdm.patterns = patterns;
8803 bzero(patterns, ccb.cdm.pattern_buf_len);
8805 patterns[0].type = DEV_MATCH_DEVICE;
8806 patterns[0].pattern.device_pattern.flags = DEV_MATCH_PATH;
8807 patterns[0].pattern.device_pattern.path_id = devlist->path_id;
8808 patterns[1].type = DEV_MATCH_PERIPH;
8809 patterns[1].pattern.periph_pattern.flags = PERIPH_MATCH_PATH;
8810 patterns[1].pattern.periph_pattern.path_id = devlist->path_id;
8813 * We do the ioctl multiple times if necessary, in case there are
8814 * more than 100 nodes in the EDT.
8819 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
8820 warn("error sending CAMIOCOMMAND ioctl");
8825 if ((ccb.ccb_h.status != CAM_REQ_CMP)
8826 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
8827 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
8828 warnx("got CAM error %#x, CDM error %d\n",
8829 ccb.ccb_h.status, ccb.cdm.status);
8834 for (i = 0; i < ccb.cdm.num_matches; i++) {
8835 switch (ccb.cdm.matches[i].type) {
8836 case DEV_MATCH_DEVICE: {
8837 struct device_match_result *dev_result;
8840 &ccb.cdm.matches[i].result.device_result;
8842 if (dev_result->flags &
8843 DEV_RESULT_UNCONFIGURED) {
8849 item = malloc(sizeof(*item));
8851 warn("%s: unable to allocate %zd bytes",
8852 __func__, sizeof(*item));
8856 bzero(item, sizeof(*item));
8857 bcopy(dev_result, &item->dev_match,
8858 sizeof(*dev_result));
8859 STAILQ_INSERT_TAIL(&devlist->dev_queue, item,
8862 if (getdevid(item) != 0) {
8868 case DEV_MATCH_PERIPH: {
8869 struct periph_match_result *periph_result;
8872 &ccb.cdm.matches[i].result.periph_result;
8874 if (skip_device != 0)
8876 item->num_periphs++;
8877 item->periph_matches = realloc(
8878 item->periph_matches,
8880 sizeof(struct periph_match_result));
8881 if (item->periph_matches == NULL) {
8882 warn("%s: error allocating periph "
8887 bcopy(periph_result, &item->periph_matches[
8888 item->num_periphs - 1],
8889 sizeof(*periph_result));
8893 fprintf(stderr, "%s: unexpected match "
8894 "type %d\n", __func__,
8895 ccb.cdm.matches[i].type);
8898 break; /*NOTREACHED*/
8901 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
8902 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
8910 free(ccb.cdm.matches);
8913 freebusdevlist(devlist);
8919 freebusdevlist(struct cam_devlist *devlist)
8921 struct cam_devitem *item, *item2;
8923 STAILQ_FOREACH_SAFE(item, &devlist->dev_queue, links, item2) {
8924 STAILQ_REMOVE(&devlist->dev_queue, item, cam_devitem,
8926 free(item->device_id);
8927 free(item->periph_matches);
8932 static struct cam_devitem *
8933 findsasdevice(struct cam_devlist *devlist, uint64_t sasaddr)
8935 struct cam_devitem *item;
8937 STAILQ_FOREACH(item, &devlist->dev_queue, links) {
8938 struct scsi_vpd_id_descriptor *idd;
8941 * XXX KDM look for LUN IDs as well?
8943 idd = scsi_get_devid(item->device_id,
8944 item->device_id_len,
8945 scsi_devid_is_sas_target);
8949 if (scsi_8btou64(idd->identifier) == sasaddr)
8957 smpphylist(struct cam_device *device, int argc, char **argv,
8958 char *combinedopt, int retry_count, int timeout)
8960 struct smp_report_general_request *rgrequest = NULL;
8961 struct smp_report_general_response *rgresponse = NULL;
8962 struct smp_discover_request *disrequest = NULL;
8963 struct smp_discover_response *disresponse = NULL;
8964 struct cam_devlist devlist;
8966 int long_response = 0;
8973 * Note that at the moment we don't support sending SMP CCBs to
8974 * devices that aren't probed by CAM.
8976 ccb = cam_getccb(device);
8978 warnx("%s: error allocating CCB", __func__);
8982 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8983 STAILQ_INIT(&devlist.dev_queue);
8985 rgrequest = malloc(sizeof(*rgrequest));
8986 if (rgrequest == NULL) {
8987 warn("%s: unable to allocate %zd bytes", __func__,
8988 sizeof(*rgrequest));
8993 rgresponse = malloc(sizeof(*rgresponse));
8994 if (rgresponse == NULL) {
8995 warn("%s: unable to allocate %zd bytes", __func__,
8996 sizeof(*rgresponse));
9001 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9014 smp_report_general(&ccb->smpio,
9018 /*request_len*/ sizeof(*rgrequest),
9019 (uint8_t *)rgresponse,
9020 /*response_len*/ sizeof(*rgresponse),
9021 /*long_response*/ long_response,
9024 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
9026 if (((retval = cam_send_ccb(device, ccb)) < 0)
9027 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
9028 const char warnstr[] = "error sending command";
9035 if (arglist & CAM_ARG_VERBOSE) {
9036 cam_error_print(device, ccb, CAM_ESF_ALL,
9037 CAM_EPF_ALL, stderr);
9043 num_phys = rgresponse->num_phys;
9045 if (num_phys == 0) {
9047 fprintf(stdout, "%s: No Phys reported\n", __func__);
9052 devlist.path_id = device->path_id;
9054 retval = buildbusdevlist(&devlist);
9059 fprintf(stdout, "%d PHYs:\n", num_phys);
9060 fprintf(stdout, "PHY Attached SAS Address\n");
9063 disrequest = malloc(sizeof(*disrequest));
9064 if (disrequest == NULL) {
9065 warn("%s: unable to allocate %zd bytes", __func__,
9066 sizeof(*disrequest));
9071 disresponse = malloc(sizeof(*disresponse));
9072 if (disresponse == NULL) {
9073 warn("%s: unable to allocate %zd bytes", __func__,
9074 sizeof(*disresponse));
9079 for (i = 0; i < num_phys; i++) {
9080 struct cam_devitem *item;
9081 struct device_match_result *dev_match;
9082 char vendor[16], product[48], revision[16];
9086 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
9088 ccb->ccb_h.status = CAM_REQ_INPROG;
9089 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
9091 smp_discover(&ccb->smpio,
9095 sizeof(*disrequest),
9096 (uint8_t *)disresponse,
9097 sizeof(*disresponse),
9099 /*ignore_zone_group*/ 0,
9103 if (((retval = cam_send_ccb(device, ccb)) < 0)
9104 || (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
9105 && (disresponse->function_result != SMP_FR_PHY_VACANT))) {
9106 const char warnstr[] = "error sending command";
9113 if (arglist & CAM_ARG_VERBOSE) {
9114 cam_error_print(device, ccb, CAM_ESF_ALL,
9115 CAM_EPF_ALL, stderr);
9121 if (disresponse->function_result == SMP_FR_PHY_VACANT) {
9123 fprintf(stdout, "%3d <vacant>\n", i);
9127 if (disresponse->attached_device == SMP_DIS_AD_TYPE_NONE) {
9130 item = findsasdevice(&devlist,
9131 scsi_8btou64(disresponse->attached_sas_address));
9135 || (item != NULL)) {
9136 fprintf(stdout, "%3d 0x%016jx", i,
9137 (uintmax_t)scsi_8btou64(
9138 disresponse->attached_sas_address));
9140 fprintf(stdout, "\n");
9143 } else if (quiet != 0)
9146 dev_match = &item->dev_match;
9148 if (dev_match->protocol == PROTO_SCSI) {
9149 cam_strvis(vendor, dev_match->inq_data.vendor,
9150 sizeof(dev_match->inq_data.vendor),
9152 cam_strvis(product, dev_match->inq_data.product,
9153 sizeof(dev_match->inq_data.product),
9155 cam_strvis(revision, dev_match->inq_data.revision,
9156 sizeof(dev_match->inq_data.revision),
9158 sprintf(tmpstr, "<%s %s %s>", vendor, product,
9160 } else if ((dev_match->protocol == PROTO_ATA)
9161 || (dev_match->protocol == PROTO_SATAPM)) {
9162 cam_strvis(product, dev_match->ident_data.model,
9163 sizeof(dev_match->ident_data.model),
9165 cam_strvis(revision, dev_match->ident_data.revision,
9166 sizeof(dev_match->ident_data.revision),
9168 sprintf(tmpstr, "<%s %s>", product, revision);
9170 sprintf(tmpstr, "<>");
9172 fprintf(stdout, " %-33s ", tmpstr);
9175 * If we have 0 periphs, that's a bug...
9177 if (item->num_periphs == 0) {
9178 fprintf(stdout, "\n");
9182 fprintf(stdout, "(");
9183 for (j = 0; j < item->num_periphs; j++) {
9185 fprintf(stdout, ",");
9187 fprintf(stdout, "%s%d",
9188 item->periph_matches[j].periph_name,
9189 item->periph_matches[j].unit_number);
9192 fprintf(stdout, ")\n");
9206 freebusdevlist(&devlist);
9212 atapm_proc_resp(struct cam_device *device, union ccb *ccb)
9214 uint8_t error = 0, ata_device = 0, status = 0;
9219 retval = get_ata_status(device, ccb, &error, &count, &lba, &ata_device,
9222 if (arglist & CAM_ARG_VERBOSE) {
9223 cam_error_print(device, ccb, CAM_ESF_ALL,
9224 CAM_EPF_ALL, stderr);
9226 warnx("Can't get ATA command status");
9230 if (status & ATA_STATUS_ERROR) {
9231 cam_error_print(device, ccb, CAM_ESF_ALL,
9232 CAM_EPF_ALL, stderr);
9236 printf("%s%d: ", device->device_name, device->dev_unit_num);
9239 printf("Standby mode\n");
9242 printf("Standby_y mode\n");
9245 printf("NV Cache Power Mode and the spindle is spun down or spinning down\n");
9248 printf("NV Cache Power Mode and the spindle is spun up or spinning up\n");
9251 printf("Idle mode\n");
9254 printf("Idle_a mode\n");
9257 printf("Idle_b mode\n");
9260 printf("Idle_c mode\n");
9263 printf("Active or Idle mode\n");
9266 printf("Unknown mode 0x%02x\n", count);
9274 atapm(struct cam_device *device, int argc, char **argv,
9275 char *combinedopt, int retry_count, int timeout)
9281 u_int8_t ata_flags = 0;
9284 ccb = cam_getccb(device);
9287 warnx("%s: error allocating ccb", __func__);
9291 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9300 if (strcmp(argv[1], "idle") == 0) {
9302 cmd = ATA_IDLE_IMMEDIATE;
9305 } else if (strcmp(argv[1], "standby") == 0) {
9307 cmd = ATA_STANDBY_IMMEDIATE;
9309 cmd = ATA_STANDBY_CMD;
9310 } else if (strcmp(argv[1], "powermode") == 0) {
9311 cmd = ATA_CHECK_POWER_MODE;
9312 ata_flags = AP_FLAG_CHK_COND;
9321 else if (t <= (240 * 5))
9323 else if (t <= (252 * 5))
9324 /* special encoding for 21 minutes */
9326 else if (t <= (11 * 30 * 60))
9327 sc = (t - 1) / (30 * 60) + 241;
9331 retval = ata_do_cmd(device,
9333 /*retries*/retry_count,
9334 /*flags*/CAM_DIR_NONE,
9335 /*protocol*/AP_PROTO_NON_DATA,
9336 /*ata_flags*/ata_flags,
9337 /*tag_action*/MSG_SIMPLE_Q_TAG,
9344 /*timeout*/timeout ? timeout : 30 * 1000,
9349 if (retval || cmd != ATA_CHECK_POWER_MODE)
9352 return (atapm_proc_resp(device, ccb));
9356 ataaxm(struct cam_device *device, int argc, char **argv,
9357 char *combinedopt, int retry_count, int timeout)
9365 ccb = cam_getccb(device);
9368 warnx("%s: error allocating ccb", __func__);
9372 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9382 if (strcmp(argv[1], "apm") == 0) {
9398 retval = ata_do_cmd(device,
9400 /*retries*/retry_count,
9401 /*flags*/CAM_DIR_NONE,
9402 /*protocol*/AP_PROTO_NON_DATA,
9404 /*tag_action*/MSG_SIMPLE_Q_TAG,
9405 /*command*/ATA_SETFEATURES,
9411 /*timeout*/timeout ? timeout : 30 * 1000,
9419 scsigetopcodes(struct cam_device *device, int opcode_set, int opcode,
9420 int show_sa_errors, int sa_set, int service_action,
9421 int timeout_desc, int task_attr, int retry_count, int timeout,
9422 int verbosemode, uint32_t *fill_len, uint8_t **data_ptr)
9424 union ccb *ccb = NULL;
9425 uint8_t *buf = NULL;
9426 uint32_t alloc_len = 0, num_opcodes;
9427 uint32_t valid_len = 0;
9428 uint32_t avail_len = 0;
9429 struct scsi_report_supported_opcodes_all *all_hdr;
9430 struct scsi_report_supported_opcodes_one *one;
9435 * Make it clear that we haven't yet allocated or filled anything.
9440 ccb = cam_getccb(device);
9442 warnx("couldn't allocate CCB");
9447 /* cam_getccb cleans up the header, caller has to zero the payload */
9448 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
9450 if (opcode_set != 0) {
9451 options |= RSO_OPTIONS_OC;
9453 alloc_len = sizeof(*one) + CAM_MAX_CDBLEN;
9456 alloc_len = sizeof(*all_hdr) + (num_opcodes *
9457 sizeof(struct scsi_report_supported_opcodes_descr));
9460 if (timeout_desc != 0) {
9461 options |= RSO_RCTD;
9462 alloc_len += num_opcodes *
9463 sizeof(struct scsi_report_supported_opcodes_timeout);
9467 options |= RSO_OPTIONS_OC_SA;
9468 if (show_sa_errors != 0)
9469 options &= ~RSO_OPTIONS_OC;
9478 buf = malloc(alloc_len);
9480 warn("Unable to allocate %u bytes", alloc_len);
9484 bzero(buf, alloc_len);
9486 scsi_report_supported_opcodes(&ccb->csio,
9487 /*retries*/ retry_count,
9489 /*tag_action*/ task_attr,
9490 /*options*/ options,
9491 /*req_opcode*/ opcode,
9492 /*req_service_action*/ service_action,
9494 /*dxfer_len*/ alloc_len,
9495 /*sense_len*/ SSD_FULL_SIZE,
9496 /*timeout*/ timeout ? timeout : 10000);
9498 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
9500 if (retry_count != 0)
9501 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
9503 if (cam_send_ccb(device, ccb) < 0) {
9504 warn("error sending REPORT SUPPORTED OPERATION CODES command");
9509 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
9510 if (verbosemode != 0)
9511 cam_error_print(device, ccb, CAM_ESF_ALL,
9512 CAM_EPF_ALL, stderr);
9517 valid_len = ccb->csio.dxfer_len - ccb->csio.resid;
9519 if (((options & RSO_OPTIONS_MASK) == RSO_OPTIONS_ALL)
9520 && (valid_len >= sizeof(*all_hdr))) {
9521 all_hdr = (struct scsi_report_supported_opcodes_all *)buf;
9522 avail_len = scsi_4btoul(all_hdr->length) + sizeof(*all_hdr);
9523 } else if (((options & RSO_OPTIONS_MASK) != RSO_OPTIONS_ALL)
9524 && (valid_len >= sizeof(*one))) {
9525 uint32_t cdb_length;
9527 one = (struct scsi_report_supported_opcodes_one *)buf;
9528 cdb_length = scsi_2btoul(one->cdb_length);
9529 avail_len = sizeof(*one) + cdb_length;
9530 if (one->support & RSO_ONE_CTDP) {
9531 struct scsi_report_supported_opcodes_timeout *td;
9533 td = (struct scsi_report_supported_opcodes_timeout *)
9535 if (valid_len >= (avail_len + sizeof(td->length))) {
9536 avail_len += scsi_2btoul(td->length) +
9539 avail_len += sizeof(*td);
9545 * avail_len could be zero if we didn't get enough data back from
9546 * thet target to determine
9548 if ((avail_len != 0)
9549 && (avail_len > valid_len)) {
9550 alloc_len = avail_len;
9554 *fill_len = valid_len;
9566 scsiprintoneopcode(struct cam_device *device, int req_opcode, int sa_set,
9567 int req_sa, uint8_t *buf, uint32_t valid_len)
9569 struct scsi_report_supported_opcodes_one *one;
9570 struct scsi_report_supported_opcodes_timeout *td;
9571 uint32_t cdb_len = 0, td_len = 0;
9572 const char *op_desc = NULL;
9576 one = (struct scsi_report_supported_opcodes_one *)buf;
9579 * If we don't have the full single opcode descriptor, no point in
9582 if (valid_len < __offsetof(struct scsi_report_supported_opcodes_one,
9584 warnx("Only %u bytes returned, not enough to verify support",
9590 op_desc = scsi_op_desc(req_opcode, &device->inq_data);
9592 printf("%s (0x%02x)", op_desc != NULL ? op_desc : "UNKNOWN",
9595 printf(", SA 0x%x", req_sa);
9598 switch (one->support & RSO_ONE_SUP_MASK) {
9599 case RSO_ONE_SUP_UNAVAIL:
9600 printf("No command support information currently available\n");
9602 case RSO_ONE_SUP_NOT_SUP:
9603 printf("Command not supported\n");
9606 break; /*NOTREACHED*/
9607 case RSO_ONE_SUP_AVAIL:
9608 printf("Command is supported, complies with a SCSI standard\n");
9610 case RSO_ONE_SUP_VENDOR:
9611 printf("Command is supported, vendor-specific "
9612 "implementation\n");
9615 printf("Unknown command support flags 0x%#x\n",
9616 one->support & RSO_ONE_SUP_MASK);
9621 * If we don't have the CDB length, it isn't exactly an error, the
9622 * command probably isn't supported.
9624 if (valid_len < __offsetof(struct scsi_report_supported_opcodes_one,
9628 cdb_len = scsi_2btoul(one->cdb_length);
9631 * If our valid data doesn't include the full reported length,
9632 * return. The caller should have detected this and adjusted his
9633 * allocation length to get all of the available data.
9635 if (valid_len < sizeof(*one) + cdb_len) {
9641 * If all we have is the opcode, there is no point in printing out
9649 printf("CDB usage bitmap:");
9650 for (i = 0; i < cdb_len; i++) {
9651 printf(" %02x", one->cdb_usage[i]);
9656 * If we don't have a timeout descriptor, we're done.
9658 if ((one->support & RSO_ONE_CTDP) == 0)
9662 * If we don't have enough valid length to include the timeout
9663 * descriptor length, we're done.
9665 if (valid_len < (sizeof(*one) + cdb_len + sizeof(td->length)))
9668 td = (struct scsi_report_supported_opcodes_timeout *)
9669 &buf[sizeof(*one) + cdb_len];
9670 td_len = scsi_2btoul(td->length);
9671 td_len += sizeof(td->length);
9674 * If we don't have the full timeout descriptor, we're done.
9676 if (td_len < sizeof(*td))
9680 * If we don't have enough valid length to contain the full timeout
9681 * descriptor, we're done.
9683 if (valid_len < (sizeof(*one) + cdb_len + td_len))
9686 printf("Timeout information:\n");
9687 printf("Command-specific: 0x%02x\n", td->cmd_specific);
9688 printf("Nominal timeout: %u seconds\n",
9689 scsi_4btoul(td->nominal_time));
9690 printf("Recommended timeout: %u seconds\n",
9691 scsi_4btoul(td->recommended_time));
9698 scsiprintopcodes(struct cam_device *device, int td_req, uint8_t *buf,
9701 struct scsi_report_supported_opcodes_all *hdr;
9702 struct scsi_report_supported_opcodes_descr *desc;
9703 uint32_t avail_len = 0, used_len = 0;
9707 if (valid_len < sizeof(*hdr)) {
9708 warnx("%s: not enough returned data (%u bytes) opcode list",
9709 __func__, valid_len);
9713 hdr = (struct scsi_report_supported_opcodes_all *)buf;
9714 avail_len = scsi_4btoul(hdr->length);
9715 avail_len += sizeof(hdr->length);
9717 * Take the lesser of the amount of data the drive claims is
9718 * available, and the amount of data the HBA says was returned.
9720 avail_len = MIN(avail_len, valid_len);
9722 used_len = sizeof(hdr->length);
9724 printf("%-6s %4s %8s ",
9725 "Opcode", "SA", "CDB len" );
9728 printf("%5s %6s %6s ", "CS", "Nom", "Rec");
9729 printf(" Description\n");
9731 while ((avail_len - used_len) > sizeof(*desc)) {
9732 struct scsi_report_supported_opcodes_timeout *td;
9734 const char *op_desc = NULL;
9736 cur_ptr = &buf[used_len];
9737 desc = (struct scsi_report_supported_opcodes_descr *)cur_ptr;
9739 op_desc = scsi_op_desc(desc->opcode, &device->inq_data);
9740 if (op_desc == NULL)
9741 op_desc = "UNKNOWN";
9743 printf("0x%02x %#4x %8u ", desc->opcode,
9744 scsi_2btoul(desc->service_action),
9745 scsi_2btoul(desc->cdb_length));
9747 used_len += sizeof(*desc);
9749 if ((desc->flags & RSO_CTDP) == 0) {
9750 printf(" %s\n", op_desc);
9755 * If we don't have enough space to fit a timeout
9756 * descriptor, then we're done.
9758 if (avail_len - used_len < sizeof(*td)) {
9759 used_len = avail_len;
9760 printf(" %s\n", op_desc);
9763 cur_ptr = &buf[used_len];
9764 td = (struct scsi_report_supported_opcodes_timeout *)cur_ptr;
9765 td_len = scsi_2btoul(td->length);
9766 td_len += sizeof(td->length);
9770 * If the given timeout descriptor length is less than what
9771 * we understand, skip it.
9773 if (td_len < sizeof(*td)) {
9774 printf(" %s\n", op_desc);
9778 printf(" 0x%02x %6u %6u %s\n", td->cmd_specific,
9779 scsi_4btoul(td->nominal_time),
9780 scsi_4btoul(td->recommended_time), op_desc);
9787 scsiopcodes(struct cam_device *device, int argc, char **argv,
9788 char *combinedopt, int task_attr, int retry_count, int timeout,
9792 uint32_t opcode = 0, service_action = 0;
9793 int td_set = 0, opcode_set = 0, sa_set = 0;
9794 int show_sa_errors = 1;
9795 uint32_t valid_len = 0;
9796 uint8_t *buf = NULL;
9800 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9806 opcode = strtoul(optarg, &endptr, 0);
9807 if (*endptr != '\0') {
9808 warnx("Invalid opcode \"%s\", must be a number",
9813 if (opcode > 0xff) {
9814 warnx("Invalid opcode 0x%#x, must be between"
9815 "0 and 0xff inclusive", opcode);
9822 service_action = strtoul(optarg, &endptr, 0);
9823 if (*endptr != '\0') {
9824 warnx("Invalid service action \"%s\", must "
9825 "be a number", optarg);
9829 if (service_action > 0xffff) {
9830 warnx("Invalid service action 0x%#x, must "
9831 "be between 0 and 0xffff inclusive",
9846 && (opcode_set == 0)) {
9847 warnx("You must specify an opcode with -o if a service "
9852 retval = scsigetopcodes(device, opcode_set, opcode, show_sa_errors,
9853 sa_set, service_action, td_set, task_attr,
9854 retry_count, timeout, verbosemode, &valid_len,
9859 if ((opcode_set != 0)
9861 retval = scsiprintoneopcode(device, opcode, sa_set,
9862 service_action, buf, valid_len);
9864 retval = scsiprintopcodes(device, td_set, buf, valid_len);
9875 reprobe(struct cam_device *device)
9880 ccb = cam_getccb(device);
9883 warnx("%s: error allocating ccb", __func__);
9887 CCB_CLEAR_ALL_EXCEPT_HDR(ccb);
9889 ccb->ccb_h.func_code = XPT_REPROBE_LUN;
9891 if (cam_send_ccb(device, ccb) < 0) {
9892 warn("error sending XPT_REPROBE_LUN CCB");
9897 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
9898 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
9910 usage(int printlong)
9913 fprintf(printlong ? stdout : stderr,
9914 "usage: camcontrol <command> [device id][generic args][command args]\n"
9915 " camcontrol devlist [-b] [-v]\n"
9916 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
9917 " camcontrol tur [dev_id][generic args]\n"
9918 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
9919 " camcontrol identify [dev_id][generic args] [-v]\n"
9920 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
9921 " camcontrol readcap [dev_id][generic args] [-b] [-h] [-H] [-N]\n"
9923 " camcontrol start [dev_id][generic args]\n"
9924 " camcontrol stop [dev_id][generic args]\n"
9925 " camcontrol load [dev_id][generic args]\n"
9926 " camcontrol eject [dev_id][generic args]\n"
9927 " camcontrol reprobe [dev_id][generic args]\n"
9928 " camcontrol rescan <all | bus[:target:lun] | dev_id>\n"
9929 " camcontrol reset <all | bus[:target:lun] | dev_id>\n"
9930 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
9931 " [-q][-s][-S offset][-X]\n"
9932 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
9933 " [-P pagectl][-e | -b][-d]\n"
9934 " camcontrol cmd [dev_id][generic args]\n"
9935 " <-a cmd [args] | -c cmd [args]>\n"
9936 " [-d] [-f] [-i len fmt|-o len fmt [args]] [-r fmt]\n"
9937 " camcontrol smpcmd [dev_id][generic args]\n"
9938 " <-r len fmt [args]> <-R len fmt [args]>\n"
9939 " camcontrol smprg [dev_id][generic args][-l]\n"
9940 " camcontrol smppc [dev_id][generic args] <-p phy> [-l]\n"
9941 " [-o operation][-d name][-m rate][-M rate]\n"
9942 " [-T pp_timeout][-a enable|disable]\n"
9943 " [-A enable|disable][-s enable|disable]\n"
9944 " [-S enable|disable]\n"
9945 " camcontrol smpphylist [dev_id][generic args][-l][-q]\n"
9946 " camcontrol smpmaninfo [dev_id][generic args][-l]\n"
9947 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
9948 " <all|dev_id|bus[:target[:lun]]|off>\n"
9949 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
9950 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
9951 " [-D <enable|disable>][-M mode][-O offset]\n"
9952 " [-q][-R syncrate][-v][-T <enable|disable>]\n"
9953 " [-U][-W bus_width]\n"
9954 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
9955 " camcontrol sanitize [dev_id][generic args]\n"
9956 " [-a overwrite|block|crypto|exitfailure]\n"
9957 " [-c passes][-I][-P pattern][-q][-U][-r][-w]\n"
9959 " camcontrol idle [dev_id][generic args][-t time]\n"
9960 " camcontrol standby [dev_id][generic args][-t time]\n"
9961 " camcontrol sleep [dev_id][generic args]\n"
9962 " camcontrol powermode [dev_id][generic args]\n"
9963 " camcontrol apm [dev_id][generic args][-l level]\n"
9964 " camcontrol aam [dev_id][generic args][-l level]\n"
9965 " camcontrol fwdownload [dev_id][generic args] <-f fw_image> [-q]\n"
9967 " camcontrol security [dev_id][generic args]\n"
9968 " <-d pwd | -e pwd | -f | -h pwd | -k pwd>\n"
9969 " [-l <high|maximum>] [-q] [-s pwd] [-T timeout]\n"
9970 " [-U <user|master>] [-y]\n"
9971 " camcontrol hpa [dev_id][generic args] [-f] [-l] [-P] [-p pwd]\n"
9972 " [-q] [-s max_sectors] [-U pwd] [-y]\n"
9973 " camcontrol ama [dev_id][generic args] [-f] [-q] [-s max_sectors]\n"
9974 " camcontrol persist [dev_id][generic args] <-i action|-o action>\n"
9975 " [-a][-I tid][-k key][-K sa_key][-p][-R rtp]\n"
9976 " [-s scope][-S][-T type][-U]\n"
9977 " camcontrol attrib [dev_id][generic args] <-r action|-w attr>\n"
9978 " [-a attr_num][-c][-e elem][-F form1,form1]\n"
9979 " [-p part][-s start][-T type][-V vol]\n"
9980 " camcontrol opcodes [dev_id][generic args][-o opcode][-s SA]\n"
9982 " camcontrol zone [dev_id][generic args]<-c cmd> [-a] [-l LBA]\n"
9983 " [-o rep_opts] [-P print_opts]\n"
9984 " camcontrol epc [dev_id][generic_args]<-c cmd> [-d] [-D] [-e]\n"
9985 " [-H] [-p power_cond] [-P] [-r rst_src] [-s]\n"
9986 " [-S power_src] [-T timer]\n"
9987 " camcontrol timestamp [dev_id][generic_args] <-r [-f format|-m|-U]>|\n"
9988 " <-s <-f format -T time | -U >>\n"
9989 " camcontrol devtype [dev_id]\n"
9990 " camcontrol mmcsdcmd [dev_id] [[-c mmc_opcode] [-a mmc_arg]\n"
9991 " [-f mmc_flags] [-l data_len]\n"
9992 " [-W [-b data_byte]]] |\n"
9993 " [-F frequency] |\n"
9996 " [-S high|normal]\n"
9998 " camcontrol help\n");
10002 "Specify one of the following options:\n"
10003 "devlist list all CAM devices\n"
10004 "periphlist list all CAM peripheral drivers attached to a device\n"
10005 "tur send a test unit ready to the named device\n"
10006 "inquiry send a SCSI inquiry command to the named device\n"
10007 "identify send a ATA identify command to the named device\n"
10008 "reportluns send a SCSI report luns command to the device\n"
10009 "readcap send a SCSI read capacity command to the device\n"
10010 "start send a Start Unit command to the device\n"
10011 "stop send a Stop Unit command to the device\n"
10012 "load send a Start Unit command to the device with the load bit set\n"
10013 "eject send a Stop Unit command to the device with the eject bit set\n"
10014 "reprobe update capacity information of the given device\n"
10015 "rescan rescan all buses, the given bus, bus:target:lun or device\n"
10016 "reset reset all buses, the given bus, bus:target:lun or device\n"
10017 "defects read the defect list of the specified device\n"
10018 "modepage display or edit (-e) the given mode page\n"
10019 "cmd send the given SCSI command, may need -i or -o as well\n"
10020 "smpcmd send the given SMP command, requires -o and -i\n"
10021 "smprg send the SMP Report General command\n"
10022 "smppc send the SMP PHY Control command, requires -p\n"
10023 "smpphylist display phys attached to a SAS expander\n"
10024 "smpmaninfo send the SMP Report Manufacturer Info command\n"
10025 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
10026 "tags report or set the number of transaction slots for a device\n"
10027 "negotiate report or set device negotiation parameters\n"
10028 "format send the SCSI FORMAT UNIT command to the named device\n"
10029 "sanitize send the SCSI SANITIZE command to the named device\n"
10030 "idle send the ATA IDLE command to the named device\n"
10031 "standby send the ATA STANDBY command to the named device\n"
10032 "sleep send the ATA SLEEP command to the named device\n"
10033 "powermode send the ATA CHECK POWER MODE command to the named device\n"
10034 "fwdownload program firmware of the named device with the given image\n"
10035 "security report or send ATA security commands to the named device\n"
10036 "persist send the SCSI PERSISTENT RESERVE IN or OUT commands\n"
10037 "attrib send the SCSI READ or WRITE ATTRIBUTE commands\n"
10038 "opcodes send the SCSI REPORT SUPPORTED OPCODES command\n"
10039 "zone manage Zoned Block (Shingled) devices\n"
10040 "epc send ATA Extended Power Conditions commands\n"
10041 "timestamp report or set the device's timestamp\n"
10042 "devtype report the type of device\n"
10043 "mmcsdcmd send the given MMC command, needs -c and -a as well\n"
10044 "help this message\n"
10045 "Device Identifiers:\n"
10046 "bus:target specify the bus and target, lun defaults to 0\n"
10047 "bus:target:lun specify the bus, target and lun\n"
10048 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
10049 "Generic arguments:\n"
10050 "-v be verbose, print out sense information\n"
10051 "-t timeout command timeout in seconds, overrides default timeout\n"
10052 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
10053 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
10054 "-E have the kernel attempt to perform SCSI error recovery\n"
10055 "-C count specify the SCSI command retry count (needs -E to work)\n"
10056 "-Q task_attr specify ordered, simple or head tag type for SCSI cmds\n"
10057 "modepage arguments:\n"
10058 "-l list all available mode pages\n"
10059 "-m page specify the mode page to view or edit\n"
10060 "-e edit the specified mode page\n"
10061 "-b force view to binary mode\n"
10062 "-d disable block descriptors for mode sense\n"
10063 "-P pgctl page control field 0-3\n"
10064 "defects arguments:\n"
10065 "-f format specify defect list format (block, bfi or phys)\n"
10066 "-G get the grown defect list\n"
10067 "-P get the permanent defect list\n"
10068 "inquiry arguments:\n"
10069 "-D get the standard inquiry data\n"
10070 "-S get the serial number\n"
10071 "-R get the transfer rate, etc.\n"
10072 "reportluns arguments:\n"
10073 "-c only report a count of available LUNs\n"
10074 "-l only print out luns, and not a count\n"
10075 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
10076 "readcap arguments\n"
10077 "-b only report the blocksize\n"
10078 "-h human readable device size, base 2\n"
10079 "-H human readable device size, base 10\n"
10080 "-N print the number of blocks instead of last block\n"
10081 "-q quiet, print numbers only\n"
10082 "-s only report the last block/device size\n"
10084 "-c cdb [args] specify the SCSI CDB\n"
10085 "-i len fmt specify input data and input data format\n"
10086 "-o len fmt [args] specify output data and output data fmt\n"
10087 "smpcmd arguments:\n"
10088 "-r len fmt [args] specify the SMP command to be sent\n"
10089 "-R len fmt [args] specify SMP response format\n"
10090 "smprg arguments:\n"
10091 "-l specify the long response format\n"
10092 "smppc arguments:\n"
10093 "-p phy specify the PHY to operate on\n"
10094 "-l specify the long request/response format\n"
10095 "-o operation specify the phy control operation\n"
10096 "-d name set the attached device name\n"
10097 "-m rate set the minimum physical link rate\n"
10098 "-M rate set the maximum physical link rate\n"
10099 "-T pp_timeout set the partial pathway timeout value\n"
10100 "-a enable|disable enable or disable SATA slumber\n"
10101 "-A enable|disable enable or disable SATA partial phy power\n"
10102 "-s enable|disable enable or disable SAS slumber\n"
10103 "-S enable|disable enable or disable SAS partial phy power\n"
10104 "smpphylist arguments:\n"
10105 "-l specify the long response format\n"
10106 "-q only print phys with attached devices\n"
10107 "smpmaninfo arguments:\n"
10108 "-l specify the long response format\n"
10109 "debug arguments:\n"
10110 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
10111 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
10112 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
10113 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
10114 "tags arguments:\n"
10115 "-N tags specify the number of tags to use for this device\n"
10116 "-q be quiet, don't report the number of tags\n"
10117 "-v report a number of tag-related parameters\n"
10118 "negotiate arguments:\n"
10119 "-a send a test unit ready after negotiation\n"
10120 "-c report/set current negotiation settings\n"
10121 "-D <arg> \"enable\" or \"disable\" disconnection\n"
10122 "-M mode set ATA mode\n"
10123 "-O offset set command delay offset\n"
10124 "-q be quiet, don't report anything\n"
10125 "-R syncrate synchronization rate in MHz\n"
10126 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
10127 "-U report/set user negotiation settings\n"
10128 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
10129 "-v also print a Path Inquiry CCB for the controller\n"
10130 "format arguments:\n"
10131 "-q be quiet, don't print status messages\n"
10132 "-r run in report only mode\n"
10133 "-w don't send immediate format command\n"
10134 "-y don't ask any questions\n"
10135 "sanitize arguments:\n"
10136 "-a operation operation mode: overwrite, block, crypto or exitfailure\n"
10137 "-c passes overwrite passes to perform (1 to 31)\n"
10138 "-I invert overwrite pattern after each pass\n"
10139 "-P pattern path to overwrite pattern file\n"
10140 "-q be quiet, don't print status messages\n"
10141 "-r run in report only mode\n"
10142 "-U run operation in unrestricted completion exit mode\n"
10143 "-w don't send immediate sanitize command\n"
10144 "-y don't ask any questions\n"
10145 "idle/standby arguments:\n"
10146 "-t <arg> number of seconds before respective state.\n"
10147 "fwdownload arguments:\n"
10148 "-f fw_image path to firmware image file\n"
10149 "-q don't print informational messages, only errors\n"
10150 "-s run in simulation mode\n"
10151 "-v print info for every firmware segment sent to device\n"
10152 "-y don't ask any questions\n"
10153 "security arguments:\n"
10154 "-d pwd disable security using the given password for the selected\n"
10156 "-e pwd erase the device using the given pwd for the selected user\n"
10157 "-f freeze the security configuration of the specified device\n"
10158 "-h pwd enhanced erase the device using the given pwd for the\n"
10160 "-k pwd unlock the device using the given pwd for the selected\n"
10162 "-l <high|maximum> specifies which security level to set: high or maximum\n"
10163 "-q be quiet, do not print any status messages\n"
10164 "-s pwd password the device (enable security) using the given\n"
10165 " pwd for the selected user\n"
10166 "-T timeout overrides the timeout (seconds) used for erase operation\n"
10167 "-U <user|master> specifies which user to set: user or master\n"
10168 "-y don't ask any questions\n"
10170 "-f freeze the HPA configuration of the device\n"
10171 "-l lock the HPA configuration of the device\n"
10172 "-P make the HPA max sectors persist\n"
10173 "-p pwd Set the HPA configuration password required for unlock\n"
10175 "-q be quiet, do not print any status messages\n"
10176 "-s sectors configures the maximum user accessible sectors of the\n"
10178 "-U pwd unlock the HPA configuration of the device\n"
10179 "-y don't ask any questions\n"
10181 "-f freeze the AMA configuration of the device\n"
10182 "-q be quiet, do not print any status messages\n"
10183 "-s sectors configures the maximum user accessible sectors of the\n"
10185 "persist arguments:\n"
10186 "-i action specify read_keys, read_reservation, report_cap, or\n"
10187 " read_full_status\n"
10188 "-o action specify register, register_ignore, reserve, release,\n"
10189 " clear, preempt, preempt_abort, register_move, replace_lost\n"
10190 "-a set the All Target Ports (ALL_TG_PT) bit\n"
10191 "-I tid specify a Transport ID, e.g.: sas,0x1234567812345678\n"
10192 "-k key specify the Reservation Key\n"
10193 "-K sa_key specify the Service Action Reservation Key\n"
10194 "-p set the Activate Persist Through Power Loss bit\n"
10195 "-R rtp specify the Relative Target Port\n"
10196 "-s scope specify the scope: lun, extent, element or a number\n"
10197 "-S specify Transport ID for register, requires -I\n"
10198 "-T res_type specify the reservation type: read_shared, wr_ex, rd_ex,\n"
10199 " ex_ac, wr_ex_ro, ex_ac_ro, wr_ex_ar, ex_ac_ar\n"
10200 "-U unregister the current initiator for register_move\n"
10201 "attrib arguments:\n"
10202 "-r action specify attr_values, attr_list, lv_list, part_list, or\n"
10204 "-w attr specify an attribute to write, one -w argument per attr\n"
10205 "-a attr_num only display this attribute number\n"
10206 "-c get cached attributes\n"
10207 "-e elem_addr request attributes for the given element in a changer\n"
10208 "-F form1,form2 output format, comma separated list: text_esc, text_raw,\n"
10209 " nonascii_esc, nonascii_trim, nonascii_raw, field_all,\n"
10210 " field_none, field_desc, field_num, field_size, field_rw\n"
10211 "-p partition request attributes for the given partition\n"
10212 "-s start_attr request attributes starting at the given number\n"
10213 "-T elem_type specify the element type (used with -e)\n"
10214 "-V logical_vol specify the logical volume ID\n"
10215 "opcodes arguments:\n"
10216 "-o opcode specify the individual opcode to list\n"
10217 "-s service_action specify the service action for the opcode\n"
10218 "-N do not return SCSI error for unsupported SA\n"
10219 "-T request nominal and recommended timeout values\n"
10220 "zone arguments:\n"
10221 "-c cmd required: rz, open, close, finish, or rwp\n"
10222 "-a apply the action to all zones\n"
10223 "-l LBA specify the zone starting LBA\n"
10224 "-o rep_opts report zones options: all, empty, imp_open, exp_open,\n"
10225 " closed, full, ro, offline, reset, nonseq, nonwp\n"
10226 "-P print_opt report zones printing: normal, summary, script\n"
10228 "-c cmd required: restore, goto, timer, state, enable, disable,\n"
10229 " source, status, list\n"
10230 "-d disable power mode (timer, state)\n"
10231 "-D delayed entry (goto)\n"
10232 "-e enable power mode (timer, state)\n"
10233 "-H hold power mode (goto)\n"
10234 "-p power_cond Idle_a, Idle_b, Idle_c, Standby_y, Standby_z (timer,\n"
10236 "-P only display power mode (status)\n"
10237 "-r rst_src restore settings from: default, saved (restore)\n"
10238 "-s save mode (timer, state, restore)\n"
10239 "-S power_src set power source: battery, nonbattery (source)\n"
10240 "-T timer set timer, seconds, .1 sec resolution (timer)\n"
10241 "timestamp arguments:\n"
10242 "-r report the timestamp of the device\n"
10243 "-f format report the timestamp of the device with the given\n"
10244 " strftime(3) format string\n"
10245 "-m report the timestamp of the device as milliseconds since\n"
10246 " January 1st, 1970\n"
10247 "-U report the time with UTC instead of the local time zone\n"
10248 "-s set the timestamp of the device\n"
10249 "-f format the format of the time string passed into strptime(3)\n"
10250 "-T time the time value passed into strptime(3)\n"
10251 "-U set the timestamp of the device to UTC time\n"
10252 "mmcsdcmd arguments:\n"
10253 "-c mmc_cmd MMC command to send to the card\n"
10254 "-a mmc_arg Argument for the MMC command\n"
10255 "-f mmc_flag Flags to set for the MMC command\n"
10256 "-l data_len Expect data_len bytes of data in reply and display them\n"
10257 "-W Fill the data buffer before invoking the MMC command\n"
10258 "-b data_byte One byte of data to fill the data buffer with\n"
10259 "-F frequency Operating frequency to set on the controller\n"
10260 "-4 Set bus width to 4 bit\n"
10261 "-1 Set bus width to 8 bit\n"
10262 "-S high | std Set high-speed or standard timing\n"
10263 "-I Display various card and host controller information\n"
10268 main(int argc, char **argv)
10271 char *device = NULL;
10273 struct cam_device *cam_dev = NULL;
10274 int timeout = 0, retry_count = 1;
10275 camcontrol_optret optreturn;
10277 const char *mainopt = "C:En:Q:t:u:v";
10278 const char *subopt = NULL;
10279 char combinedopt[256];
10280 int error = 0, optstart = 2;
10281 int task_attr = MSG_SIMPLE_Q_TAG;
10284 target_id_t target;
10287 cmdlist = CAM_CMD_NONE;
10288 arglist = CAM_ARG_NONE;
10296 * Get the base option.
10298 optreturn = getoption(option_table,argv[1], &cmdlist, &arglist,&subopt);
10300 if (optreturn == CC_OR_AMBIGUOUS) {
10301 warnx("ambiguous option %s", argv[1]);
10304 } else if (optreturn == CC_OR_NOT_FOUND) {
10305 warnx("option %s not found", argv[1]);
10311 * Ahh, getopt(3) is a pain.
10313 * This is a gross hack. There really aren't many other good
10314 * options (excuse the pun) for parsing options in a situation like
10315 * this. getopt is kinda braindead, so you end up having to run
10316 * through the options twice, and give each invocation of getopt
10317 * the option string for the other invocation.
10319 * You would think that you could just have two groups of options.
10320 * The first group would get parsed by the first invocation of
10321 * getopt, and the second group would get parsed by the second
10322 * invocation of getopt. It doesn't quite work out that way. When
10323 * the first invocation of getopt finishes, it leaves optind pointing
10324 * to the argument _after_ the first argument in the second group.
10325 * So when the second invocation of getopt comes around, it doesn't
10326 * recognize the first argument it gets and then bails out.
10328 * A nice alternative would be to have a flag for getopt that says
10329 * "just keep parsing arguments even when you encounter an unknown
10330 * argument", but there isn't one. So there's no real clean way to
10331 * easily parse two sets of arguments without having one invocation
10332 * of getopt know about the other.
10334 * Without this hack, the first invocation of getopt would work as
10335 * long as the generic arguments are first, but the second invocation
10336 * (in the subfunction) would fail in one of two ways. In the case
10337 * where you don't set optreset, it would fail because optind may be
10338 * pointing to the argument after the one it should be pointing at.
10339 * In the case where you do set optreset, and reset optind, it would
10340 * fail because getopt would run into the first set of options, which
10341 * it doesn't understand.
10343 * All of this would "sort of" work if you could somehow figure out
10344 * whether optind had been incremented one option too far. The
10345 * mechanics of that, however, are more daunting than just giving
10346 * both invocations all of the expect options for either invocation.
10348 * Needless to say, I wouldn't mind if someone invented a better
10349 * (non-GPL!) command line parsing interface than getopt. I
10350 * wouldn't mind if someone added more knobs to getopt to make it
10351 * work better. Who knows, I may talk myself into doing it someday,
10352 * if the standards weenies let me. As it is, it just leads to
10353 * hackery like this and causes people to avoid it in some cases.
10355 * KDM, September 8th, 1998
10357 if (subopt != NULL)
10358 sprintf(combinedopt, "%s%s", mainopt, subopt);
10360 sprintf(combinedopt, "%s", mainopt);
10363 * For these options we do not parse optional device arguments and
10364 * we do not open a passthrough device.
10366 if ((cmdlist == CAM_CMD_RESCAN)
10367 || (cmdlist == CAM_CMD_RESET)
10368 || (cmdlist == CAM_CMD_DEVTREE)
10369 || (cmdlist == CAM_CMD_USAGE)
10370 || (cmdlist == CAM_CMD_DEBUG))
10374 && (argc > 2 && argv[2][0] != '-')) {
10378 if (isdigit(argv[2][0])) {
10379 /* device specified as bus:target[:lun] */
10380 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
10382 errx(1, "numeric device specification must "
10383 "be either bus:target, or "
10385 /* default to 0 if lun was not specified */
10386 if ((arglist & CAM_ARG_LUN) == 0) {
10388 arglist |= CAM_ARG_LUN;
10392 if (cam_get_device(argv[2], name, sizeof name, &unit)
10394 errx(1, "%s", cam_errbuf);
10395 device = strdup(name);
10396 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
10401 * Start getopt processing at argv[2/3], since we've already
10402 * accepted argv[1..2] as the command name, and as a possible
10408 * Now we run through the argument list looking for generic
10409 * options, and ignoring options that possibly belong to
10412 while ((c = getopt(argc, argv, combinedopt))!= -1){
10415 retry_count = strtol(optarg, NULL, 0);
10416 if (retry_count < 0)
10417 errx(1, "retry count %d is < 0",
10419 arglist |= CAM_ARG_RETRIES;
10422 arglist |= CAM_ARG_ERR_RECOVER;
10425 arglist |= CAM_ARG_DEVICE;
10427 while (isspace(*tstr) && (*tstr != '\0'))
10429 device = (char *)strdup(tstr);
10433 int table_entry = 0;
10436 while (isspace(*tstr) && (*tstr != '\0'))
10438 if (isdigit(*tstr)) {
10439 task_attr = strtol(tstr, &endptr, 0);
10440 if (*endptr != '\0') {
10441 errx(1, "Invalid queue option "
10446 scsi_nv_status status;
10448 table_size = sizeof(task_attrs) /
10449 sizeof(task_attrs[0]);
10450 status = scsi_get_nv(task_attrs,
10451 table_size, tstr, &table_entry,
10452 SCSI_NV_FLAG_IG_CASE);
10453 if (status == SCSI_NV_FOUND)
10454 task_attr = task_attrs[
10455 table_entry].value;
10457 errx(1, "%s option %s",
10458 (status == SCSI_NV_AMBIGUOUS)?
10459 "ambiguous" : "invalid",
10466 timeout = strtol(optarg, NULL, 0);
10468 errx(1, "invalid timeout %d", timeout);
10469 /* Convert the timeout from seconds to ms */
10471 arglist |= CAM_ARG_TIMEOUT;
10474 arglist |= CAM_ARG_UNIT;
10475 unit = strtol(optarg, NULL, 0);
10478 arglist |= CAM_ARG_VERBOSE;
10486 * For most commands we'll want to open the passthrough device
10487 * associated with the specified device. In the case of the rescan
10488 * commands, we don't use a passthrough device at all, just the
10489 * transport layer device.
10491 if (devopen == 1) {
10492 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
10493 && (((arglist & CAM_ARG_DEVICE) == 0)
10494 || ((arglist & CAM_ARG_UNIT) == 0))) {
10495 errx(1, "subcommand \"%s\" requires a valid device "
10496 "identifier", argv[1]);
10499 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
10500 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
10501 cam_open_spec_device(device,unit,O_RDWR,NULL)))
10503 errx(1,"%s", cam_errbuf);
10507 * Reset optind to 2, and reset getopt, so these routines can parse
10508 * the arguments again.
10514 case CAM_CMD_DEVLIST:
10515 error = getdevlist(cam_dev);
10518 error = atahpa(cam_dev, retry_count, timeout,
10519 argc, argv, combinedopt);
10522 error = ataama(cam_dev, retry_count, timeout,
10523 argc, argv, combinedopt);
10525 case CAM_CMD_DEVTREE:
10526 error = getdevtree(argc, argv, combinedopt);
10528 case CAM_CMD_DEVTYPE:
10529 error = getdevtype(cam_dev);
10532 error = testunitready(cam_dev, task_attr, retry_count,
10535 case CAM_CMD_INQUIRY:
10536 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
10537 task_attr, retry_count, timeout);
10539 case CAM_CMD_IDENTIFY:
10540 error = identify(cam_dev, retry_count, timeout);
10542 case CAM_CMD_STARTSTOP:
10543 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
10544 arglist & CAM_ARG_EJECT, task_attr,
10545 retry_count, timeout);
10547 case CAM_CMD_RESCAN:
10548 error = dorescan_or_reset(argc, argv, 1);
10550 case CAM_CMD_RESET:
10551 error = dorescan_or_reset(argc, argv, 0);
10553 case CAM_CMD_READ_DEFECTS:
10554 error = readdefects(cam_dev, argc, argv, combinedopt,
10555 task_attr, retry_count, timeout);
10557 case CAM_CMD_MODE_PAGE:
10558 modepage(cam_dev, argc, argv, combinedopt,
10559 task_attr, retry_count, timeout);
10561 case CAM_CMD_SCSI_CMD:
10562 error = scsicmd(cam_dev, argc, argv, combinedopt,
10563 task_attr, retry_count, timeout);
10565 case CAM_CMD_MMCSD_CMD:
10566 error = mmcsdcmd(cam_dev, argc, argv, combinedopt,
10567 retry_count, timeout);
10569 case CAM_CMD_SMP_CMD:
10570 error = smpcmd(cam_dev, argc, argv, combinedopt,
10571 retry_count, timeout);
10573 case CAM_CMD_SMP_RG:
10574 error = smpreportgeneral(cam_dev, argc, argv,
10575 combinedopt, retry_count,
10578 case CAM_CMD_SMP_PC:
10579 error = smpphycontrol(cam_dev, argc, argv, combinedopt,
10580 retry_count, timeout);
10582 case CAM_CMD_SMP_PHYLIST:
10583 error = smpphylist(cam_dev, argc, argv, combinedopt,
10584 retry_count, timeout);
10586 case CAM_CMD_SMP_MANINFO:
10587 error = smpmaninfo(cam_dev, argc, argv, combinedopt,
10588 retry_count, timeout);
10590 case CAM_CMD_DEBUG:
10591 error = camdebug(argc, argv, combinedopt);
10594 error = tagcontrol(cam_dev, argc, argv, combinedopt);
10597 error = ratecontrol(cam_dev, task_attr, retry_count,
10598 timeout, argc, argv, combinedopt);
10600 case CAM_CMD_FORMAT:
10601 error = scsiformat(cam_dev, argc, argv,
10602 combinedopt, task_attr, retry_count,
10605 case CAM_CMD_REPORTLUNS:
10606 error = scsireportluns(cam_dev, argc, argv,
10607 combinedopt, task_attr,
10608 retry_count, timeout);
10610 case CAM_CMD_READCAP:
10611 error = scsireadcapacity(cam_dev, argc, argv,
10612 combinedopt, task_attr,
10613 retry_count, timeout);
10616 case CAM_CMD_STANDBY:
10617 case CAM_CMD_SLEEP:
10618 case CAM_CMD_POWER_MODE:
10619 error = atapm(cam_dev, argc, argv,
10620 combinedopt, retry_count, timeout);
10624 error = ataaxm(cam_dev, argc, argv,
10625 combinedopt, retry_count, timeout);
10627 case CAM_CMD_SECURITY:
10628 error = atasecurity(cam_dev, retry_count, timeout,
10629 argc, argv, combinedopt);
10631 case CAM_CMD_DOWNLOAD_FW:
10632 error = fwdownload(cam_dev, argc, argv, combinedopt,
10633 arglist & CAM_ARG_VERBOSE, task_attr, retry_count,
10636 case CAM_CMD_SANITIZE:
10637 error = sanitize(cam_dev, argc, argv, combinedopt, task_attr,
10638 retry_count, timeout);
10640 case CAM_CMD_PERSIST:
10641 error = scsipersist(cam_dev, argc, argv, combinedopt,
10642 task_attr, retry_count, timeout,
10643 arglist & CAM_ARG_VERBOSE,
10644 arglist & CAM_ARG_ERR_RECOVER);
10646 case CAM_CMD_ATTRIB:
10647 error = scsiattrib(cam_dev, argc, argv, combinedopt,
10648 task_attr, retry_count, timeout,
10649 arglist & CAM_ARG_VERBOSE,
10650 arglist & CAM_ARG_ERR_RECOVER);
10652 case CAM_CMD_OPCODES:
10653 error = scsiopcodes(cam_dev, argc, argv, combinedopt,
10654 task_attr, retry_count, timeout,
10655 arglist & CAM_ARG_VERBOSE);
10657 case CAM_CMD_REPROBE:
10658 error = reprobe(cam_dev);
10661 error = zone(cam_dev, argc, argv, combinedopt,
10662 task_attr, retry_count, timeout,
10663 arglist & CAM_ARG_VERBOSE);
10666 error = epc(cam_dev, argc, argv, combinedopt,
10667 retry_count, timeout, arglist & CAM_ARG_VERBOSE);
10669 case CAM_CMD_TIMESTAMP:
10670 error = timestamp(cam_dev, argc, argv, combinedopt,
10671 task_attr, retry_count, timeout,
10672 arglist & CAM_ARG_VERBOSE);
10674 case CAM_CMD_USAGE:
10683 if (cam_dev != NULL)
10684 cam_close_device(cam_dev);