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"
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_argmask arglist;
253 static const char *devtype_names[] = {
263 camcontrol_optret getoption(struct camcontrol_opts *table, char *arg,
264 uint32_t *cmdnum, cam_argmask *argnum,
265 const char **subopt);
266 static int getdevlist(struct cam_device *device);
267 static int getdevtree(int argc, char **argv, char *combinedopt);
268 static int getdevtype(struct cam_device *device);
269 static int print_dev_scsi(struct device_match_result *dev_result, char *tmpstr);
270 static int print_dev_ata(struct device_match_result *dev_result, char *tmpstr);
271 static int print_dev_semb(struct device_match_result *dev_result, char *tmpstr);
272 static int print_dev_mmcsd(struct device_match_result *dev_result,
275 static int print_dev_nvme(struct device_match_result *dev_result, char *tmpstr);
277 static int testunitready(struct cam_device *device, int task_attr,
278 int retry_count, int timeout, int quiet);
279 static int scsistart(struct cam_device *device, int startstop, int loadeject,
280 int task_attr, int retry_count, int timeout);
281 static int scsiinquiry(struct cam_device *device, int task_attr,
282 int retry_count, int timeout);
283 static int scsiserial(struct cam_device *device, int task_attr,
284 int retry_count, int timeout);
285 static int parse_btl(char *tstr, path_id_t *bus, target_id_t *target,
286 lun_id_t *lun, cam_argmask *arglst);
287 static int reprobe(struct cam_device *device);
288 static int dorescan_or_reset(int argc, char **argv, int rescan);
289 static int rescan_or_reset_bus(path_id_t bus, int rescan);
290 static int scanlun_or_reset_dev(path_id_t bus, target_id_t target,
291 lun_id_t lun, int scan);
292 static int readdefects(struct cam_device *device, int argc, char **argv,
293 char *combinedopt, int task_attr, int retry_count,
295 static void modepage(struct cam_device *device, int argc, char **argv,
296 char *combinedopt, int task_attr, int retry_count,
298 static int scsicmd(struct cam_device *device, int argc, char **argv,
299 char *combinedopt, int task_attr, int retry_count,
301 static int smpcmd(struct cam_device *device, int argc, char **argv,
302 char *combinedopt, int retry_count, int timeout);
303 static int mmcsdcmd(struct cam_device *device, int argc, char **argv,
304 char *combinedopt, int retry_count, int timeout);
305 static int smpreportgeneral(struct cam_device *device, int argc, char **argv,
306 char *combinedopt, int retry_count, int timeout);
307 static int smpphycontrol(struct cam_device *device, int argc, char **argv,
308 char *combinedopt, int retry_count, int timeout);
309 static int smpmaninfo(struct cam_device *device, int argc, char **argv,
310 char *combinedopt, int retry_count, int timeout);
311 static int getdevid(struct cam_devitem *item);
312 static int buildbusdevlist(struct cam_devlist *devlist);
313 static void freebusdevlist(struct cam_devlist *devlist);
314 static struct cam_devitem *findsasdevice(struct cam_devlist *devlist,
316 static int smpphylist(struct cam_device *device, int argc, char **argv,
317 char *combinedopt, int retry_count, int timeout);
318 static int tagcontrol(struct cam_device *device, int argc, char **argv,
320 static void cts_print(struct cam_device *device,
321 struct ccb_trans_settings *cts);
322 static void cpi_print(struct ccb_pathinq *cpi);
323 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi);
324 static int get_cgd(struct cam_device *device, struct ccb_getdev *cgd);
325 static int get_print_cts(struct cam_device *device, int user_settings,
326 int quiet, struct ccb_trans_settings *cts);
327 static int ratecontrol(struct cam_device *device, int task_attr,
328 int retry_count, int timeout, int argc, char **argv,
330 static int scsiformat(struct cam_device *device, int argc, char **argv,
331 char *combinedopt, int task_attr, int retry_count,
333 static int sanitize(struct cam_device *device, int argc, char **argv,
334 char *combinedopt, int task_attr, int retry_count,
336 static int scsireportluns(struct cam_device *device, int argc, char **argv,
337 char *combinedopt, int task_attr, int retry_count,
339 static int scsireadcapacity(struct cam_device *device, int argc, char **argv,
340 char *combinedopt, int task_attr, int retry_count,
342 static int atapm(struct cam_device *device, int argc, char **argv,
343 char *combinedopt, int retry_count, int timeout);
344 static int atasecurity(struct cam_device *device, int retry_count, int timeout,
345 int argc, char **argv, char *combinedopt);
346 static int atahpa(struct cam_device *device, int retry_count, int timeout,
347 int argc, char **argv, char *combinedopt);
348 static int ataama(struct cam_device *device, int retry_count, int timeout,
349 int argc, char **argv, char *combinedopt);
350 static int scsiprintoneopcode(struct cam_device *device, int req_opcode,
351 int sa_set, int req_sa, uint8_t *buf,
353 static int scsiprintopcodes(struct cam_device *device, int td_req, uint8_t *buf,
355 static int scsiopcodes(struct cam_device *device, int argc, char **argv,
356 char *combinedopt, int task_attr, int retry_count,
357 int timeout, int verbose);
360 #define min(a,b) (((a)<(b))?(a):(b))
363 #define max(a,b) (((a)>(b))?(a):(b))
367 getoption(struct camcontrol_opts *table, char *arg, uint32_t *cmdnum,
368 cam_argmask *argnum, const char **subopt)
370 struct camcontrol_opts *opts;
373 for (opts = table; (opts != NULL) && (opts->optname != NULL);
375 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
376 *cmdnum = opts->cmdnum;
377 *argnum = opts->argnum;
378 *subopt = opts->subopt;
379 if (++num_matches > 1)
380 return (CC_OR_AMBIGUOUS);
385 return (CC_OR_FOUND);
387 return (CC_OR_NOT_FOUND);
391 getdevlist(struct cam_device *device)
397 ccb = cam_getccb(device);
399 ccb->ccb_h.func_code = XPT_GDEVLIST;
400 ccb->ccb_h.flags = CAM_DIR_NONE;
401 ccb->ccb_h.retry_count = 1;
403 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS;
404 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) {
405 if (cam_send_ccb(device, ccb) < 0) {
406 warn("error getting device list");
413 switch (ccb->cgdl.status) {
414 case CAM_GDEVLIST_MORE_DEVS:
415 strcpy(status, "MORE");
417 case CAM_GDEVLIST_LAST_DEVICE:
418 strcpy(status, "LAST");
420 case CAM_GDEVLIST_LIST_CHANGED:
421 strcpy(status, "CHANGED");
423 case CAM_GDEVLIST_ERROR:
424 strcpy(status, "ERROR");
429 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n",
430 ccb->cgdl.periph_name,
431 ccb->cgdl.unit_number,
432 ccb->cgdl.generation,
437 * If the list has changed, we need to start over from the
440 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED)
450 getdevtree(int argc, char **argv, char *combinedopt)
461 while ((c = getopt(argc, argv, combinedopt)) != -1) {
464 if ((arglist & CAM_ARG_VERBOSE) == 0)
472 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
473 warn("couldn't open %s", XPT_DEVICE);
477 bzero(&ccb, sizeof(union ccb));
479 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
480 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
481 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
483 ccb.ccb_h.func_code = XPT_DEV_MATCH;
484 bufsize = sizeof(struct dev_match_result) * 100;
485 ccb.cdm.match_buf_len = bufsize;
486 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
487 if (ccb.cdm.matches == NULL) {
488 warnx("can't malloc memory for matches");
492 ccb.cdm.num_matches = 0;
495 * We fetch all nodes, since we display most of them in the default
496 * case, and all in the verbose case.
498 ccb.cdm.num_patterns = 0;
499 ccb.cdm.pattern_buf_len = 0;
502 * We do the ioctl multiple times if necessary, in case there are
503 * more than 100 nodes in the EDT.
506 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
507 warn("error sending CAMIOCOMMAND ioctl");
512 if ((ccb.ccb_h.status != CAM_REQ_CMP)
513 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
514 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
515 warnx("got CAM error %#x, CDM error %d\n",
516 ccb.ccb_h.status, ccb.cdm.status);
521 for (i = 0; i < ccb.cdm.num_matches; i++) {
522 switch (ccb.cdm.matches[i].type) {
523 case DEV_MATCH_BUS: {
524 struct bus_match_result *bus_result;
527 * Only print the bus information if the
528 * user turns on the verbose flag.
530 if ((busonly == 0) &&
531 (arglist & CAM_ARG_VERBOSE) == 0)
535 &ccb.cdm.matches[i].result.bus_result;
538 fprintf(stdout, ")\n");
542 fprintf(stdout, "scbus%d on %s%d bus %d%s\n",
544 bus_result->dev_name,
545 bus_result->unit_number,
547 (busonly ? "" : ":"));
550 case DEV_MATCH_DEVICE: {
551 struct device_match_result *dev_result;
558 &ccb.cdm.matches[i].result.device_result;
560 if ((dev_result->flags
561 & DEV_RESULT_UNCONFIGURED)
562 && ((arglist & CAM_ARG_VERBOSE) == 0)) {
568 if (dev_result->protocol == PROTO_SCSI) {
569 if (print_dev_scsi(dev_result,
574 } else if (dev_result->protocol == PROTO_ATA ||
575 dev_result->protocol == PROTO_SATAPM) {
576 if (print_dev_ata(dev_result,
581 } else if (dev_result->protocol == PROTO_MMCSD){
582 if (print_dev_mmcsd(dev_result,
587 } else if (dev_result->protocol == PROTO_SEMB) {
588 if (print_dev_semb(dev_result,
594 } else if (dev_result->protocol == PROTO_NVME) {
595 if (print_dev_nvme(dev_result,
602 sprintf(tmpstr, "<>");
605 fprintf(stdout, ")\n");
609 fprintf(stdout, "%-33s at scbus%d "
610 "target %d lun %jx (",
613 dev_result->target_id,
614 (uintmax_t)dev_result->target_lun);
620 case DEV_MATCH_PERIPH: {
621 struct periph_match_result *periph_result;
624 &ccb.cdm.matches[i].result.periph_result;
626 if (busonly || skip_device != 0)
630 fprintf(stdout, ",");
632 fprintf(stdout, "%s%d",
633 periph_result->periph_name,
634 periph_result->unit_number);
640 fprintf(stdout, "unknown match type\n");
645 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
646 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
649 fprintf(stdout, ")\n");
657 getdevtype(struct cam_device *cam_dev)
659 camcontrol_devtype dt;
663 * Get the device type and report it, request no I/O be done to do this.
665 error = get_device_type(cam_dev, -1, 0, 0, &dt);
666 if (error != 0 || (unsigned)dt > CC_DT_UNKNOWN) {
667 fprintf(stdout, "illegal\n");
670 fprintf(stdout, "%s\n", devtype_names[dt]);
675 print_dev_scsi(struct device_match_result *dev_result, char *tmpstr)
677 char vendor[16], product[48], revision[16];
679 cam_strvis(vendor, dev_result->inq_data.vendor,
680 sizeof(dev_result->inq_data.vendor), sizeof(vendor));
681 cam_strvis(product, dev_result->inq_data.product,
682 sizeof(dev_result->inq_data.product), sizeof(product));
683 cam_strvis(revision, dev_result->inq_data.revision,
684 sizeof(dev_result->inq_data.revision), sizeof(revision));
685 sprintf(tmpstr, "<%s %s %s>", vendor, product, revision);
691 print_dev_ata(struct device_match_result *dev_result, char *tmpstr)
693 char product[48], revision[16];
695 cam_strvis(product, dev_result->ident_data.model,
696 sizeof(dev_result->ident_data.model), sizeof(product));
697 cam_strvis(revision, dev_result->ident_data.revision,
698 sizeof(dev_result->ident_data.revision), sizeof(revision));
699 sprintf(tmpstr, "<%s %s>", product, revision);
705 print_dev_semb(struct device_match_result *dev_result, char *tmpstr)
707 struct sep_identify_data *sid;
708 char vendor[16], product[48], revision[16], fw[5];
710 sid = (struct sep_identify_data *)&dev_result->ident_data;
711 cam_strvis(vendor, sid->vendor_id,
712 sizeof(sid->vendor_id), sizeof(vendor));
713 cam_strvis(product, sid->product_id,
714 sizeof(sid->product_id), sizeof(product));
715 cam_strvis(revision, sid->product_rev,
716 sizeof(sid->product_rev), sizeof(revision));
717 cam_strvis(fw, sid->firmware_rev,
718 sizeof(sid->firmware_rev), sizeof(fw));
719 sprintf(tmpstr, "<%s %s %s %s>", vendor, product, revision, fw);
725 print_dev_mmcsd(struct device_match_result *dev_result, char *tmpstr)
728 struct ccb_dev_advinfo *advi;
729 struct cam_device *dev;
730 struct mmc_params mmc_ident_data;
732 dev = cam_open_btl(dev_result->path_id, dev_result->target_id,
733 dev_result->target_lun, O_RDWR, NULL);
735 warnx("%s", cam_errbuf);
739 ccb = cam_getccb(dev);
741 warnx("couldn't allocate CCB");
742 cam_close_device(dev);
747 advi->ccb_h.flags = CAM_DIR_IN;
748 advi->ccb_h.func_code = XPT_DEV_ADVINFO;
749 advi->flags = CDAI_FLAG_NONE;
750 advi->buftype = CDAI_TYPE_MMC_PARAMS;
751 advi->bufsiz = sizeof(struct mmc_params);
752 advi->buf = (uint8_t *)&mmc_ident_data;
754 if (cam_send_ccb(dev, ccb) < 0) {
755 warn("error sending XPT_DEV_ADVINFO CCB");
757 cam_close_device(dev);
761 if (strlen(mmc_ident_data.model) > 0) {
762 sprintf(tmpstr, "<%s>", mmc_ident_data.model);
764 sprintf(tmpstr, "<%s card>",
765 mmc_ident_data.card_features &
766 CARD_FEATURE_SDIO ? "SDIO" : "unknown");
770 cam_close_device(dev);
776 nvme_get_cdata(struct cam_device *dev, struct nvme_controller_data *cdata)
779 struct ccb_dev_advinfo *advi;
781 ccb = cam_getccb(dev);
783 warnx("couldn't allocate CCB");
784 cam_close_device(dev);
789 advi->ccb_h.flags = CAM_DIR_IN;
790 advi->ccb_h.func_code = XPT_DEV_ADVINFO;
791 advi->flags = CDAI_FLAG_NONE;
792 advi->buftype = CDAI_TYPE_NVME_CNTRL;
793 advi->bufsiz = sizeof(struct nvme_controller_data);
794 advi->buf = (uint8_t *)cdata;
796 if (cam_send_ccb(dev, ccb) < 0) {
797 warn("error sending XPT_DEV_ADVINFO CCB");
799 cam_close_device(dev);
802 if (advi->ccb_h.status != CAM_REQ_CMP) {
803 warnx("got CAM error %#x", advi->ccb_h.status);
805 cam_close_device(dev);
813 print_dev_nvme(struct device_match_result *dev_result, char *tmpstr)
815 struct cam_device *dev;
816 struct nvme_controller_data cdata;
817 char vendor[64], product[64];
819 dev = cam_open_btl(dev_result->path_id, dev_result->target_id,
820 dev_result->target_lun, O_RDWR, NULL);
822 warnx("%s", cam_errbuf);
826 if (nvme_get_cdata(dev, &cdata))
829 cam_strvis(vendor, cdata.mn, sizeof(cdata.mn), sizeof(vendor));
830 cam_strvis(product, cdata.fr, sizeof(cdata.fr), sizeof(product));
831 sprintf(tmpstr, "<%s %s>", vendor, product);
833 cam_close_device(dev);
839 testunitready(struct cam_device *device, int task_attr, int retry_count,
840 int timeout, int quiet)
845 ccb = cam_getccb(device);
847 scsi_test_unit_ready(&ccb->csio,
848 /* retries */ retry_count,
850 /* tag_action */ task_attr,
851 /* sense_len */ SSD_FULL_SIZE,
852 /* timeout */ timeout ? timeout : 5000);
854 /* Disable freezing the device queue */
855 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
857 if (arglist & CAM_ARG_ERR_RECOVER)
858 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
860 if (cam_send_ccb(device, ccb) < 0) {
862 warn("error sending TEST UNIT READY command");
867 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
869 fprintf(stdout, "Unit is ready\n");
872 fprintf(stdout, "Unit is not ready\n");
875 if (arglist & CAM_ARG_VERBOSE) {
876 cam_error_print(device, ccb, CAM_ESF_ALL,
877 CAM_EPF_ALL, stderr);
887 scsistart(struct cam_device *device, int startstop, int loadeject,
888 int task_attr, int retry_count, int timeout)
893 ccb = cam_getccb(device);
896 * If we're stopping, send an ordered tag so the drive in question
897 * will finish any previously queued writes before stopping. If
898 * the device isn't capable of tagged queueing, or if tagged
899 * queueing is turned off, the tag action is a no-op. We override
900 * the default simple tag, although this also has the effect of
901 * overriding the user's wishes if he wanted to specify a simple
905 && (task_attr == MSG_SIMPLE_Q_TAG))
906 task_attr = MSG_ORDERED_Q_TAG;
908 scsi_start_stop(&ccb->csio,
909 /* retries */ retry_count,
911 /* tag_action */ task_attr,
912 /* start/stop */ startstop,
913 /* load_eject */ loadeject,
915 /* sense_len */ SSD_FULL_SIZE,
916 /* timeout */ timeout ? timeout : 120000);
918 /* Disable freezing the device queue */
919 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
921 if (arglist & CAM_ARG_ERR_RECOVER)
922 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
924 if (cam_send_ccb(device, ccb) < 0) {
925 warn("error sending START STOP UNIT command");
930 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
932 fprintf(stdout, "Unit started successfully");
934 fprintf(stdout,", Media loaded\n");
936 fprintf(stdout,"\n");
938 fprintf(stdout, "Unit stopped successfully");
940 fprintf(stdout, ", Media ejected\n");
942 fprintf(stdout, "\n");
948 "Error received from start unit command\n");
951 "Error received from stop unit command\n");
953 if (arglist & CAM_ARG_VERBOSE) {
954 cam_error_print(device, ccb, CAM_ESF_ALL,
955 CAM_EPF_ALL, stderr);
965 scsidoinquiry(struct cam_device *device, int argc, char **argv,
966 char *combinedopt, int task_attr, int retry_count, int timeout)
971 while ((c = getopt(argc, argv, combinedopt)) != -1) {
974 arglist |= CAM_ARG_GET_STDINQ;
977 arglist |= CAM_ARG_GET_XFERRATE;
980 arglist |= CAM_ARG_GET_SERIAL;
988 * If the user didn't specify any inquiry options, he wants all of
991 if ((arglist & CAM_ARG_INQ_MASK) == 0)
992 arglist |= CAM_ARG_INQ_MASK;
994 if (arglist & CAM_ARG_GET_STDINQ)
995 error = scsiinquiry(device, task_attr, retry_count, timeout);
1000 if (arglist & CAM_ARG_GET_SERIAL)
1001 scsiserial(device, task_attr, retry_count, timeout);
1003 if (arglist & CAM_ARG_GET_XFERRATE)
1004 error = camxferrate(device);
1010 scsiinquiry(struct cam_device *device, int task_attr, int retry_count,
1014 struct scsi_inquiry_data *inq_buf;
1017 ccb = cam_getccb(device);
1020 warnx("couldn't allocate CCB");
1024 inq_buf = (struct scsi_inquiry_data *)malloc(
1025 sizeof(struct scsi_inquiry_data));
1027 if (inq_buf == NULL) {
1029 warnx("can't malloc memory for inquiry\n");
1032 bzero(inq_buf, sizeof(*inq_buf));
1035 * Note that although the size of the inquiry buffer is the full
1036 * 256 bytes specified in the SCSI spec, we only tell the device
1037 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are
1038 * two reasons for this:
1040 * - The SCSI spec says that when a length field is only 1 byte,
1041 * a value of 0 will be interpreted as 256. Therefore
1042 * scsi_inquiry() will convert an inq_len (which is passed in as
1043 * a u_int32_t, but the field in the CDB is only 1 byte) of 256
1044 * to 0. Evidently, very few devices meet the spec in that
1045 * regard. Some devices, like many Seagate disks, take the 0 as
1046 * 0, and don't return any data. One Pioneer DVD-R drive
1047 * returns more data than the command asked for.
1049 * So, since there are numerous devices that just don't work
1050 * right with the full inquiry size, we don't send the full size.
1052 * - The second reason not to use the full inquiry data length is
1053 * that we don't need it here. The only reason we issue a
1054 * standard inquiry is to get the vendor name, device name,
1055 * and revision so scsi_print_inquiry() can print them.
1057 * If, at some point in the future, more inquiry data is needed for
1058 * some reason, this code should use a procedure similar to the
1059 * probe code. i.e., issue a short inquiry, and determine from
1060 * the additional length passed back from the device how much
1061 * inquiry data the device supports. Once the amount the device
1062 * supports is determined, issue an inquiry for that amount and no
1067 scsi_inquiry(&ccb->csio,
1068 /* retries */ retry_count,
1070 /* tag_action */ task_attr,
1071 /* inq_buf */ (u_int8_t *)inq_buf,
1072 /* inq_len */ SHORT_INQUIRY_LENGTH,
1075 /* sense_len */ SSD_FULL_SIZE,
1076 /* timeout */ timeout ? timeout : 5000);
1078 /* Disable freezing the device queue */
1079 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1081 if (arglist & CAM_ARG_ERR_RECOVER)
1082 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1084 if (cam_send_ccb(device, ccb) < 0) {
1085 warn("error sending INQUIRY command");
1090 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1093 if (arglist & CAM_ARG_VERBOSE) {
1094 cam_error_print(device, ccb, CAM_ESF_ALL,
1095 CAM_EPF_ALL, stderr);
1106 fprintf(stdout, "%s%d: ", device->device_name,
1107 device->dev_unit_num);
1108 scsi_print_inquiry(inq_buf);
1116 scsiserial(struct cam_device *device, int task_attr, int retry_count,
1120 struct scsi_vpd_unit_serial_number *serial_buf;
1121 char serial_num[SVPD_SERIAL_NUM_SIZE + 1];
1124 ccb = cam_getccb(device);
1127 warnx("couldn't allocate CCB");
1131 serial_buf = (struct scsi_vpd_unit_serial_number *)
1132 malloc(sizeof(*serial_buf));
1134 if (serial_buf == NULL) {
1136 warnx("can't malloc memory for serial number");
1140 scsi_inquiry(&ccb->csio,
1141 /*retries*/ retry_count,
1143 /* tag_action */ task_attr,
1144 /* inq_buf */ (u_int8_t *)serial_buf,
1145 /* inq_len */ sizeof(*serial_buf),
1147 /* page_code */ SVPD_UNIT_SERIAL_NUMBER,
1148 /* sense_len */ SSD_FULL_SIZE,
1149 /* timeout */ timeout ? timeout : 5000);
1151 /* Disable freezing the device queue */
1152 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1154 if (arglist & CAM_ARG_ERR_RECOVER)
1155 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1157 if (cam_send_ccb(device, ccb) < 0) {
1158 warn("error sending INQUIRY command");
1164 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1167 if (arglist & CAM_ARG_VERBOSE) {
1168 cam_error_print(device, ccb, CAM_ESF_ALL,
1169 CAM_EPF_ALL, stderr);
1180 bcopy(serial_buf->serial_num, serial_num, serial_buf->length);
1181 serial_num[serial_buf->length] = '\0';
1183 if ((arglist & CAM_ARG_GET_STDINQ)
1184 || (arglist & CAM_ARG_GET_XFERRATE))
1185 fprintf(stdout, "%s%d: Serial Number ",
1186 device->device_name, device->dev_unit_num);
1188 fprintf(stdout, "%.60s\n", serial_num);
1196 camxferrate(struct cam_device *device)
1198 struct ccb_pathinq cpi;
1200 u_int32_t speed = 0;
1205 if ((retval = get_cpi(device, &cpi)) != 0)
1208 ccb = cam_getccb(device);
1211 warnx("couldn't allocate CCB");
1215 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1216 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
1218 if (((retval = cam_send_ccb(device, ccb)) < 0)
1219 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
1220 const char error_string[] = "error getting transfer settings";
1225 warnx(error_string);
1227 if (arglist & CAM_ARG_VERBOSE)
1228 cam_error_print(device, ccb, CAM_ESF_ALL,
1229 CAM_EPF_ALL, stderr);
1233 goto xferrate_bailout;
1237 speed = cpi.base_transfer_speed;
1239 if (ccb->cts.transport == XPORT_SPI) {
1240 struct ccb_trans_settings_spi *spi =
1241 &ccb->cts.xport_specific.spi;
1243 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
1244 freq = scsi_calc_syncsrate(spi->sync_period);
1247 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
1248 speed *= (0x01 << spi->bus_width);
1250 } else if (ccb->cts.transport == XPORT_FC) {
1251 struct ccb_trans_settings_fc *fc =
1252 &ccb->cts.xport_specific.fc;
1254 if (fc->valid & CTS_FC_VALID_SPEED)
1255 speed = fc->bitrate;
1256 } else if (ccb->cts.transport == XPORT_SAS) {
1257 struct ccb_trans_settings_sas *sas =
1258 &ccb->cts.xport_specific.sas;
1260 if (sas->valid & CTS_SAS_VALID_SPEED)
1261 speed = sas->bitrate;
1262 } else if (ccb->cts.transport == XPORT_ATA) {
1263 struct ccb_trans_settings_pata *pata =
1264 &ccb->cts.xport_specific.ata;
1266 if (pata->valid & CTS_ATA_VALID_MODE)
1267 speed = ata_mode2speed(pata->mode);
1268 } else if (ccb->cts.transport == XPORT_SATA) {
1269 struct ccb_trans_settings_sata *sata =
1270 &ccb->cts.xport_specific.sata;
1272 if (sata->valid & CTS_SATA_VALID_REVISION)
1273 speed = ata_revision2speed(sata->revision);
1278 fprintf(stdout, "%s%d: %d.%03dMB/s transfers",
1279 device->device_name, device->dev_unit_num,
1282 fprintf(stdout, "%s%d: %dKB/s transfers",
1283 device->device_name, device->dev_unit_num,
1287 if (ccb->cts.transport == XPORT_SPI) {
1288 struct ccb_trans_settings_spi *spi =
1289 &ccb->cts.xport_specific.spi;
1291 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1292 && (spi->sync_offset != 0))
1293 fprintf(stdout, " (%d.%03dMHz, offset %d", freq / 1000,
1294 freq % 1000, spi->sync_offset);
1296 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1297 && (spi->bus_width > 0)) {
1298 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1299 && (spi->sync_offset != 0)) {
1300 fprintf(stdout, ", ");
1302 fprintf(stdout, " (");
1304 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width));
1305 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)
1306 && (spi->sync_offset != 0)) {
1307 fprintf(stdout, ")");
1309 } else if (ccb->cts.transport == XPORT_ATA) {
1310 struct ccb_trans_settings_pata *pata =
1311 &ccb->cts.xport_specific.ata;
1314 if (pata->valid & CTS_ATA_VALID_MODE)
1315 printf("%s, ", ata_mode2string(pata->mode));
1316 if ((pata->valid & CTS_ATA_VALID_ATAPI) && pata->atapi != 0)
1317 printf("ATAPI %dbytes, ", pata->atapi);
1318 if (pata->valid & CTS_ATA_VALID_BYTECOUNT)
1319 printf("PIO %dbytes", pata->bytecount);
1321 } else if (ccb->cts.transport == XPORT_SATA) {
1322 struct ccb_trans_settings_sata *sata =
1323 &ccb->cts.xport_specific.sata;
1326 if (sata->valid & CTS_SATA_VALID_REVISION)
1327 printf("SATA %d.x, ", sata->revision);
1330 if (sata->valid & CTS_SATA_VALID_MODE)
1331 printf("%s, ", ata_mode2string(sata->mode));
1332 if ((sata->valid & CTS_SATA_VALID_ATAPI) && sata->atapi != 0)
1333 printf("ATAPI %dbytes, ", sata->atapi);
1334 if (sata->valid & CTS_SATA_VALID_BYTECOUNT)
1335 printf("PIO %dbytes", sata->bytecount);
1339 if (ccb->cts.protocol == PROTO_SCSI) {
1340 struct ccb_trans_settings_scsi *scsi =
1341 &ccb->cts.proto_specific.scsi;
1342 if (scsi->valid & CTS_SCSI_VALID_TQ) {
1343 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) {
1344 fprintf(stdout, ", Command Queueing Enabled");
1349 fprintf(stdout, "\n");
1359 atahpa_print(struct ata_params *parm, u_int64_t hpasize, int header)
1361 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1362 ((u_int32_t)parm->lba_size_2 << 16);
1364 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1365 ((u_int64_t)parm->lba_size48_2 << 16) |
1366 ((u_int64_t)parm->lba_size48_3 << 32) |
1367 ((u_int64_t)parm->lba_size48_4 << 48);
1371 "Support Enabled Value\n");
1374 printf("Host Protected Area (HPA) ");
1375 if (parm->support.command1 & ATA_SUPPORT_PROTECTED) {
1376 u_int64_t lba = lbasize48 ? lbasize48 : lbasize;
1377 printf("yes %s %ju/%ju\n", (hpasize > lba) ? "yes" : "no ",
1380 printf("HPA - Security ");
1381 if (parm->support.command2 & ATA_SUPPORT_MAXSECURITY)
1382 printf("yes %s\n", (parm->enabled.command2 &
1383 ATA_SUPPORT_MAXSECURITY) ? "yes" : "no ");
1392 ataama_print(struct ata_params *parm, u_int64_t nativesize, int header)
1394 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1395 ((u_int32_t)parm->lba_size_2 << 16);
1397 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1398 ((u_int64_t)parm->lba_size48_2 << 16) |
1399 ((u_int64_t)parm->lba_size48_3 << 32) |
1400 ((u_int64_t)parm->lba_size48_4 << 48);
1404 "Support Enabled Value\n");
1407 printf("Accessible Max Address Config ");
1408 if (parm->support2 & ATA_SUPPORT_AMAX_ADDR) {
1409 u_int64_t lba = lbasize48 ? lbasize48 : lbasize;
1410 printf("yes %s %ju/%ju\n",
1411 (nativesize > lba) ? "yes" : "no ", lba, nativesize);
1418 atasata(struct ata_params *parm)
1422 if (parm->satacapabilities != 0xffff &&
1423 parm->satacapabilities != 0x0000)
1430 atacapprint(struct ata_params *parm)
1433 u_int32_t lbasize = (u_int32_t)parm->lba_size_1 |
1434 ((u_int32_t)parm->lba_size_2 << 16);
1436 u_int64_t lbasize48 = ((u_int64_t)parm->lba_size48_1) |
1437 ((u_int64_t)parm->lba_size48_2 << 16) |
1438 ((u_int64_t)parm->lba_size48_3 << 32) |
1439 ((u_int64_t)parm->lba_size48_4 << 48);
1442 printf("protocol ");
1443 proto = (parm->config == ATA_PROTO_CFA) ? "CFA" :
1444 (parm->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
1445 if (ata_version(parm->version_major) == 0) {
1446 printf("%s", proto);
1447 } else if (ata_version(parm->version_major) <= 7) {
1448 printf("%s-%d", proto,
1449 ata_version(parm->version_major));
1450 } else if (ata_version(parm->version_major) == 8) {
1451 printf("%s8-ACS", proto);
1454 ata_version(parm->version_major) - 7, proto);
1456 if (parm->satacapabilities && parm->satacapabilities != 0xffff) {
1457 if (parm->satacapabilities & ATA_SATA_GEN3)
1458 printf(" SATA 3.x\n");
1459 else if (parm->satacapabilities & ATA_SATA_GEN2)
1460 printf(" SATA 2.x\n");
1461 else if (parm->satacapabilities & ATA_SATA_GEN1)
1462 printf(" SATA 1.x\n");
1468 printf("device model %.40s\n", parm->model);
1469 printf("firmware revision %.8s\n", parm->revision);
1470 printf("serial number %.20s\n", parm->serial);
1471 if (parm->enabled.extension & ATA_SUPPORT_64BITWWN) {
1472 printf("WWN %04x%04x%04x%04x\n",
1473 parm->wwn[0], parm->wwn[1], parm->wwn[2], parm->wwn[3]);
1475 printf("additional product id %.8s\n", parm->product_id);
1476 if (parm->enabled.extension & ATA_SUPPORT_MEDIASN) {
1477 printf("media serial number %.30s\n",
1478 parm->media_serial);
1481 printf("cylinders %d\n", parm->cylinders);
1482 printf("heads %d\n", parm->heads);
1483 printf("sectors/track %d\n", parm->sectors);
1484 printf("sector size logical %u, physical %lu, offset %lu\n",
1485 ata_logical_sector_size(parm),
1486 (unsigned long)ata_physical_sector_size(parm),
1487 (unsigned long)ata_logical_sector_offset(parm));
1489 if (parm->config == ATA_PROTO_CFA ||
1490 (parm->support.command2 & ATA_SUPPORT_CFA))
1491 printf("CFA supported\n");
1493 printf("LBA%ssupported ",
1494 parm->capabilities1 & ATA_SUPPORT_LBA ? " " : " not ");
1496 printf("%d sectors\n", lbasize);
1500 printf("LBA48%ssupported ",
1501 parm->support.command2 & ATA_SUPPORT_ADDRESS48 ? " " : " not ");
1503 printf("%ju sectors\n", (uintmax_t)lbasize48);
1507 printf("PIO supported PIO");
1508 switch (ata_max_pmode(parm)) {
1524 if ((parm->capabilities1 & ATA_SUPPORT_IORDY) == 0)
1525 printf(" w/o IORDY");
1528 printf("DMA%ssupported ",
1529 parm->capabilities1 & ATA_SUPPORT_DMA ? " " : " not ");
1530 if (parm->capabilities1 & ATA_SUPPORT_DMA) {
1531 if (parm->mwdmamodes & 0xff) {
1533 if (parm->mwdmamodes & 0x04)
1535 else if (parm->mwdmamodes & 0x02)
1537 else if (parm->mwdmamodes & 0x01)
1541 if ((parm->atavalid & ATA_FLAG_88) &&
1542 (parm->udmamodes & 0xff)) {
1544 if (parm->udmamodes & 0x40)
1546 else if (parm->udmamodes & 0x20)
1548 else if (parm->udmamodes & 0x10)
1550 else if (parm->udmamodes & 0x08)
1552 else if (parm->udmamodes & 0x04)
1554 else if (parm->udmamodes & 0x02)
1556 else if (parm->udmamodes & 0x01)
1563 if (parm->media_rotation_rate == 1) {
1564 printf("media RPM non-rotating\n");
1565 } else if (parm->media_rotation_rate >= 0x0401 &&
1566 parm->media_rotation_rate <= 0xFFFE) {
1567 printf("media RPM %d\n",
1568 parm->media_rotation_rate);
1571 printf("Zoned-Device Commands ");
1572 switch (parm->support3 & ATA_SUPPORT_ZONE_MASK) {
1573 case ATA_SUPPORT_ZONE_DEV_MANAGED:
1574 printf("device managed\n");
1576 case ATA_SUPPORT_ZONE_HOST_AWARE:
1577 printf("host aware\n");
1584 "Support Enabled Value Vendor\n");
1585 printf("read ahead %s %s\n",
1586 parm->support.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no",
1587 parm->enabled.command1 & ATA_SUPPORT_LOOKAHEAD ? "yes" : "no");
1588 printf("write cache %s %s\n",
1589 parm->support.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no",
1590 parm->enabled.command1 & ATA_SUPPORT_WRITECACHE ? "yes" : "no");
1591 printf("flush cache %s %s\n",
1592 parm->support.command2 & ATA_SUPPORT_FLUSHCACHE ? "yes" : "no",
1593 parm->enabled.command2 & ATA_SUPPORT_FLUSHCACHE ? "yes" : "no");
1594 printf("Native Command Queuing (NCQ) ");
1595 if (atasata(parm) && (parm->satacapabilities & ATA_SUPPORT_NCQ)) {
1596 printf("yes %d tags\n",
1597 ATA_QUEUE_LEN(parm->queue) + 1);
1598 printf("NCQ Priority Information %s\n",
1599 parm->satacapabilities & ATA_SUPPORT_NCQ_PRIO ?
1601 printf("NCQ Non-Data Command %s\n",
1602 parm->satacapabilities2 & ATA_SUPPORT_NCQ_NON_DATA ?
1604 printf("NCQ Streaming %s\n",
1605 parm->satacapabilities2 & ATA_SUPPORT_NCQ_STREAM ?
1607 printf("Receive & Send FPDMA Queued %s\n",
1608 parm->satacapabilities2 & ATA_SUPPORT_RCVSND_FPDMA_QUEUED ?
1610 printf("NCQ Autosense %s\n",
1611 parm->satasupport & ATA_SUPPORT_NCQ_AUTOSENSE ?
1616 printf("SMART %s %s\n",
1617 parm->support.command1 & ATA_SUPPORT_SMART ? "yes" : "no",
1618 parm->enabled.command1 & ATA_SUPPORT_SMART ? "yes" : "no");
1619 printf("security %s %s\n",
1620 parm->support.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no",
1621 parm->enabled.command1 & ATA_SUPPORT_SECURITY ? "yes" : "no");
1622 printf("power management %s %s\n",
1623 parm->support.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no",
1624 parm->enabled.command1 & ATA_SUPPORT_POWERMGT ? "yes" : "no");
1625 printf("microcode download %s %s\n",
1626 parm->support.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no",
1627 parm->enabled.command2 & ATA_SUPPORT_MICROCODE ? "yes" : "no");
1628 printf("advanced power management %s %s",
1629 parm->support.command2 & ATA_SUPPORT_APM ? "yes" : "no",
1630 parm->enabled.command2 & ATA_SUPPORT_APM ? "yes" : "no");
1631 if (parm->support.command2 & ATA_SUPPORT_APM) {
1632 printf(" %d/0x%02X\n",
1633 parm->apm_value & 0xff, parm->apm_value & 0xff);
1636 printf("automatic acoustic management %s %s",
1637 parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no",
1638 parm->enabled.command2 & ATA_SUPPORT_AUTOACOUSTIC ? "yes" :"no");
1639 if (parm->support.command2 & ATA_SUPPORT_AUTOACOUSTIC) {
1640 printf(" %d/0x%02X %d/0x%02X\n",
1641 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1642 ATA_ACOUSTIC_CURRENT(parm->acoustic),
1643 ATA_ACOUSTIC_VENDOR(parm->acoustic),
1644 ATA_ACOUSTIC_VENDOR(parm->acoustic));
1647 printf("media status notification %s %s\n",
1648 parm->support.command2 & ATA_SUPPORT_NOTIFY ? "yes" : "no",
1649 parm->enabled.command2 & ATA_SUPPORT_NOTIFY ? "yes" : "no");
1650 printf("power-up in Standby %s %s\n",
1651 parm->support.command2 & ATA_SUPPORT_STANDBY ? "yes" : "no",
1652 parm->enabled.command2 & ATA_SUPPORT_STANDBY ? "yes" : "no");
1653 printf("write-read-verify %s %s",
1654 parm->support2 & ATA_SUPPORT_WRITEREADVERIFY ? "yes" : "no",
1655 parm->enabled2 & ATA_SUPPORT_WRITEREADVERIFY ? "yes" : "no");
1656 if (parm->support2 & ATA_SUPPORT_WRITEREADVERIFY) {
1657 printf(" %d/0x%x\n",
1658 parm->wrv_mode, parm->wrv_mode);
1661 printf("unload %s %s\n",
1662 parm->support.extension & ATA_SUPPORT_UNLOAD ? "yes" : "no",
1663 parm->enabled.extension & ATA_SUPPORT_UNLOAD ? "yes" : "no");
1664 printf("general purpose logging %s %s\n",
1665 parm->support.extension & ATA_SUPPORT_GENLOG ? "yes" : "no",
1666 parm->enabled.extension & ATA_SUPPORT_GENLOG ? "yes" : "no");
1667 printf("free-fall %s %s\n",
1668 parm->support2 & ATA_SUPPORT_FREEFALL ? "yes" : "no",
1669 parm->enabled2 & ATA_SUPPORT_FREEFALL ? "yes" : "no");
1670 printf("sense data reporting %s %s\n",
1671 parm->support2 & ATA_SUPPORT_SENSE_REPORT ? "yes" : "no",
1672 parm->enabled2 & ATA_SUPPORT_SENSE_REPORT ? "yes" : "no");
1673 printf("extended power conditions %s %s\n",
1674 parm->support2 & ATA_SUPPORT_EPC ? "yes" : "no",
1675 parm->enabled2 & ATA_SUPPORT_EPC ? "yes" : "no");
1676 printf("device statistics notification %s %s\n",
1677 parm->support2 & ATA_SUPPORT_DSN ? "yes" : "no",
1678 parm->enabled2 & ATA_SUPPORT_DSN ? "yes" : "no");
1679 printf("Data Set Management (DSM/TRIM) ");
1680 if (parm->support_dsm & ATA_SUPPORT_DSM_TRIM) {
1682 printf("DSM - max 512byte blocks ");
1683 if (parm->max_dsm_blocks == 0x00)
1684 printf("yes not specified\n");
1687 parm->max_dsm_blocks);
1689 printf("DSM - deterministic read ");
1690 if (parm->support3 & ATA_SUPPORT_DRAT) {
1691 if (parm->support3 & ATA_SUPPORT_RZAT)
1692 printf("yes zeroed\n");
1694 printf("yes any value\n");
1701 printf("Trusted Computing %s\n",
1702 ((parm->tcg & 0xc000) == 0x4000) && (parm->tcg & ATA_SUPPORT_TCG) ?
1704 printf("encrypts all user data %s\n",
1705 parm->support3 & ATA_ENCRYPTS_ALL_USER_DATA ? "yes" : "no");
1706 printf("Sanitize ");
1707 if (parm->multi & ATA_SUPPORT_SANITIZE) {
1708 printf("yes\t\t%s%s%s\n",
1709 parm->multi & ATA_SUPPORT_BLOCK_ERASE_EXT ? "block, " : "",
1710 parm->multi & ATA_SUPPORT_OVERWRITE_EXT ? "overwrite, " : "",
1711 parm->multi & ATA_SUPPORT_CRYPTO_SCRAMBLE_EXT ? "crypto" : "");
1712 printf("Sanitize - commands allowed %s\n",
1713 parm->multi & ATA_SUPPORT_SANITIZE_ALLOWED ? "yes" : "no");
1714 printf("Sanitize - antifreeze lock %s\n",
1715 parm->multi & ATA_SUPPORT_ANTIFREEZE_LOCK_EXT ? "yes" : "no");
1722 scsi_cam_pass_16_send(struct cam_device *device, union ccb *ccb)
1724 struct ata_pass_16 *ata_pass_16;
1725 struct ata_cmd ata_cmd;
1727 ata_pass_16 = (struct ata_pass_16 *)ccb->csio.cdb_io.cdb_bytes;
1728 ata_cmd.command = ata_pass_16->command;
1729 ata_cmd.control = ata_pass_16->control;
1730 ata_cmd.features = ata_pass_16->features;
1732 if (arglist & CAM_ARG_VERBOSE) {
1733 warnx("sending ATA %s via pass_16 with timeout of %u msecs",
1734 ata_op_string(&ata_cmd),
1735 ccb->csio.ccb_h.timeout);
1738 /* Disable freezing the device queue */
1739 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1741 if (arglist & CAM_ARG_ERR_RECOVER)
1742 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1744 if (cam_send_ccb(device, ccb) < 0) {
1745 warn("error sending ATA %s via pass_16", ata_op_string(&ata_cmd));
1750 * Consider any non-CAM_REQ_CMP status as error and report it here,
1751 * unless caller set AP_FLAG_CHK_COND, in which case it is reponsible.
1753 if (!(ata_pass_16->flags & AP_FLAG_CHK_COND) &&
1754 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1755 warnx("ATA %s via pass_16 failed", ata_op_string(&ata_cmd));
1756 if (arglist & CAM_ARG_VERBOSE) {
1757 cam_error_print(device, ccb, CAM_ESF_ALL,
1758 CAM_EPF_ALL, stderr);
1768 ata_cam_send(struct cam_device *device, union ccb *ccb)
1770 if (arglist & CAM_ARG_VERBOSE) {
1771 warnx("sending ATA %s with timeout of %u msecs",
1772 ata_op_string(&(ccb->ataio.cmd)),
1773 ccb->ataio.ccb_h.timeout);
1776 /* Disable freezing the device queue */
1777 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1779 if (arglist & CAM_ARG_ERR_RECOVER)
1780 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1782 if (cam_send_ccb(device, ccb) < 0) {
1783 warn("error sending ATA %s", ata_op_string(&(ccb->ataio.cmd)));
1788 * Consider any non-CAM_REQ_CMP status as error and report it here,
1789 * unless caller set AP_FLAG_CHK_COND, in which case it is reponsible.
1791 if (!(ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT) &&
1792 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1793 warnx("ATA %s failed", ata_op_string(&(ccb->ataio.cmd)));
1794 if (arglist & CAM_ARG_VERBOSE) {
1795 cam_error_print(device, ccb, CAM_ESF_ALL,
1796 CAM_EPF_ALL, stderr);
1805 ata_do_pass_16(struct cam_device *device, union ccb *ccb, int retries,
1806 u_int32_t flags, u_int8_t protocol, u_int8_t ata_flags,
1807 u_int8_t tag_action, u_int8_t command, u_int16_t features,
1808 u_int64_t lba, u_int16_t sector_count, u_int8_t *data_ptr,
1809 u_int16_t dxfer_len, int timeout)
1811 if (data_ptr != NULL) {
1812 if (flags & CAM_DIR_OUT)
1813 ata_flags |= AP_FLAG_TDIR_TO_DEV;
1815 ata_flags |= AP_FLAG_TDIR_FROM_DEV;
1817 ata_flags |= AP_FLAG_TLEN_NO_DATA;
1820 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1822 scsi_ata_pass_16(&ccb->csio,
1836 /*sense_len*/SSD_FULL_SIZE,
1839 return scsi_cam_pass_16_send(device, ccb);
1843 ata_try_pass_16(struct cam_device *device)
1845 struct ccb_pathinq cpi;
1847 if (get_cpi(device, &cpi) != 0) {
1848 warnx("couldn't get CPI");
1852 if (cpi.protocol == PROTO_SCSI) {
1853 /* possibly compatible with pass_16 */
1857 /* likely not compatible with pass_16 */
1862 ata_do_cmd(struct cam_device *device, union ccb *ccb, int retries,
1863 u_int32_t flags, u_int8_t protocol, u_int8_t ata_flags,
1864 u_int8_t tag_action, u_int8_t command, u_int16_t features,
1865 u_int64_t lba, u_int16_t sector_count, u_int8_t *data_ptr,
1866 u_int16_t dxfer_len, int timeout, int force48bit)
1870 retval = ata_try_pass_16(device);
1875 return (ata_do_pass_16(device, ccb, retries, flags, protocol,
1876 ata_flags, tag_action, command, features,
1877 lba, sector_count, data_ptr, dxfer_len,
1881 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->ataio);
1882 cam_fill_ataio(&ccb->ataio,
1891 if (force48bit || lba > ATA_MAX_28BIT_LBA)
1892 ata_48bit_cmd(&ccb->ataio, command, features, lba, sector_count);
1894 ata_28bit_cmd(&ccb->ataio, command, features, lba, sector_count);
1896 if (ata_flags & AP_FLAG_CHK_COND)
1897 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
1899 return ata_cam_send(device, ccb);
1903 dump_data(uint16_t *ptr, uint32_t len)
1907 for (i = 0; i < len / 2; i++) {
1909 printf(" %3d: ", i);
1910 printf("%04hx ", ptr[i]);
1919 atahpa_proc_resp(struct cam_device *device, union ccb *ccb, u_int64_t *hpasize)
1921 uint8_t error = 0, ata_device = 0, status = 0;
1926 retval = get_ata_status(device, ccb, &error, &count, &lba, &ata_device,
1929 if (arglist & CAM_ARG_VERBOSE) {
1930 cam_error_print(device, ccb, CAM_ESF_ALL,
1931 CAM_EPF_ALL, stderr);
1933 warnx("Can't get ATA command status");
1937 if (status & ATA_STATUS_ERROR) {
1938 if (arglist & CAM_ARG_VERBOSE) {
1939 cam_error_print(device, ccb, CAM_ESF_ALL,
1940 CAM_EPF_ALL, stderr);
1943 if (error & ATA_ERROR_ID_NOT_FOUND) {
1944 warnx("Max address has already been set since "
1945 "last power-on or hardware reset");
1946 } else if (hpasize == NULL)
1947 warnx("Command failed with ATA error");
1952 if (hpasize != NULL) {
1953 if (retval == 2 || retval == 6)
1962 ata_read_native_max(struct cam_device *device, int retry_count,
1963 u_int32_t timeout, union ccb *ccb,
1964 struct ata_params *parm, u_int64_t *hpasize)
1970 is48bit = parm->support.command2 & ATA_SUPPORT_ADDRESS48;
1971 protocol = AP_PROTO_NON_DATA;
1974 cmd = ATA_READ_NATIVE_MAX_ADDRESS48;
1975 protocol |= AP_EXTEND;
1977 cmd = ATA_READ_NATIVE_MAX_ADDRESS;
1980 error = ata_do_cmd(device,
1983 /*flags*/CAM_DIR_NONE,
1984 /*protocol*/protocol,
1985 /*ata_flags*/AP_FLAG_CHK_COND,
1986 /*tag_action*/MSG_SIMPLE_Q_TAG,
1993 timeout ? timeout : 5000,
1999 return atahpa_proc_resp(device, ccb, hpasize);
2003 atahpa_set_max(struct cam_device *device, int retry_count,
2004 u_int32_t timeout, union ccb *ccb,
2005 int is48bit, u_int64_t maxsize, int persist)
2011 protocol = AP_PROTO_NON_DATA;
2014 cmd = ATA_SET_MAX_ADDRESS48;
2015 protocol |= AP_EXTEND;
2017 cmd = ATA_SET_MAX_ADDRESS;
2020 /* lba's are zero indexed so the max lba is requested max - 1 */
2024 error = ata_do_cmd(device,
2027 /*flags*/CAM_DIR_NONE,
2028 /*protocol*/protocol,
2029 /*ata_flags*/AP_FLAG_CHK_COND,
2030 /*tag_action*/MSG_SIMPLE_Q_TAG,
2032 /*features*/ATA_HPA_FEAT_MAX_ADDR,
2034 /*sector_count*/persist,
2037 timeout ? timeout : 1000,
2043 return atahpa_proc_resp(device, ccb, NULL);
2047 atahpa_password(struct cam_device *device, int retry_count,
2048 u_int32_t timeout, union ccb *ccb,
2049 int is48bit, struct ata_set_max_pwd *pwd)
2054 protocol = AP_PROTO_PIO_OUT;
2055 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2057 return (ata_do_cmd(device,
2060 /*flags*/CAM_DIR_OUT,
2061 /*protocol*/protocol,
2062 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2063 AP_FLAG_TLEN_SECT_CNT,
2064 /*tag_action*/MSG_SIMPLE_Q_TAG,
2066 /*features*/ATA_HPA_FEAT_SET_PWD,
2068 /*sector_count*/sizeof(*pwd) / 512,
2069 /*data_ptr*/(u_int8_t*)pwd,
2070 /*dxfer_len*/sizeof(*pwd),
2071 timeout ? timeout : 1000,
2076 atahpa_lock(struct cam_device *device, int retry_count,
2077 u_int32_t timeout, union ccb *ccb, int is48bit)
2082 protocol = AP_PROTO_NON_DATA;
2083 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2085 return (ata_do_cmd(device,
2088 /*flags*/CAM_DIR_NONE,
2089 /*protocol*/protocol,
2091 /*tag_action*/MSG_SIMPLE_Q_TAG,
2093 /*features*/ATA_HPA_FEAT_LOCK,
2098 timeout ? timeout : 1000,
2103 atahpa_unlock(struct cam_device *device, int retry_count,
2104 u_int32_t timeout, union ccb *ccb,
2105 int is48bit, struct ata_set_max_pwd *pwd)
2110 protocol = AP_PROTO_PIO_OUT;
2111 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2113 return (ata_do_cmd(device,
2116 /*flags*/CAM_DIR_OUT,
2117 /*protocol*/protocol,
2118 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2119 AP_FLAG_TLEN_SECT_CNT,
2120 /*tag_action*/MSG_SIMPLE_Q_TAG,
2122 /*features*/ATA_HPA_FEAT_UNLOCK,
2124 /*sector_count*/sizeof(*pwd) / 512,
2125 /*data_ptr*/(u_int8_t*)pwd,
2126 /*dxfer_len*/sizeof(*pwd),
2127 timeout ? timeout : 1000,
2132 atahpa_freeze_lock(struct cam_device *device, int retry_count,
2133 u_int32_t timeout, union ccb *ccb, int is48bit)
2138 protocol = AP_PROTO_NON_DATA;
2139 cmd = (is48bit) ? ATA_SET_MAX_ADDRESS48 : ATA_SET_MAX_ADDRESS;
2141 return (ata_do_cmd(device,
2144 /*flags*/CAM_DIR_NONE,
2145 /*protocol*/protocol,
2147 /*tag_action*/MSG_SIMPLE_Q_TAG,
2149 /*features*/ATA_HPA_FEAT_FREEZE,
2154 timeout ? timeout : 1000,
2159 ata_get_native_max(struct cam_device *device, int retry_count,
2160 u_int32_t timeout, union ccb *ccb,
2161 u_int64_t *nativesize)
2165 error = ata_do_cmd(device,
2168 /*flags*/CAM_DIR_NONE,
2169 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2170 /*ata_flags*/AP_FLAG_CHK_COND,
2171 /*tag_action*/MSG_SIMPLE_Q_TAG,
2172 /*command*/ATA_AMAX_ADDR,
2173 /*features*/ATA_AMAX_ADDR_GET,
2178 timeout ? timeout : 30 * 1000,
2184 return atahpa_proc_resp(device, ccb, nativesize);
2188 ataama_set(struct cam_device *device, int retry_count,
2189 u_int32_t timeout, union ccb *ccb, u_int64_t maxsize)
2193 /* lba's are zero indexed so the max lba is requested max - 1 */
2197 error = ata_do_cmd(device,
2200 /*flags*/CAM_DIR_NONE,
2201 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2202 /*ata_flags*/AP_FLAG_CHK_COND,
2203 /*tag_action*/MSG_SIMPLE_Q_TAG,
2204 /*command*/ATA_AMAX_ADDR,
2205 /*features*/ATA_AMAX_ADDR_SET,
2210 timeout ? timeout : 30 * 1000,
2216 return atahpa_proc_resp(device, ccb, NULL);
2220 ataama_freeze(struct cam_device *device, int retry_count,
2221 u_int32_t timeout, union ccb *ccb)
2224 return (ata_do_cmd(device,
2227 /*flags*/CAM_DIR_NONE,
2228 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
2230 /*tag_action*/MSG_SIMPLE_Q_TAG,
2231 /*command*/ATA_AMAX_ADDR,
2232 /*features*/ATA_AMAX_ADDR_FREEZE,
2237 timeout ? timeout : 30 * 1000,
2242 ata_do_identify(struct cam_device *device, int retry_count, int timeout,
2243 union ccb *ccb, struct ata_params** ident_bufp)
2245 struct ata_params *ident_buf;
2246 struct ccb_pathinq cpi;
2247 struct ccb_getdev cgd;
2250 u_int8_t command, retry_command;
2252 if (get_cpi(device, &cpi) != 0) {
2253 warnx("couldn't get CPI");
2257 /* Neither PROTO_ATAPI or PROTO_SATAPM are used in cpi.protocol */
2258 if (cpi.protocol == PROTO_ATA) {
2259 if (get_cgd(device, &cgd) != 0) {
2260 warnx("couldn't get CGD");
2264 command = (cgd.protocol == PROTO_ATA) ?
2265 ATA_ATA_IDENTIFY : ATA_ATAPI_IDENTIFY;
2268 /* We don't know which for sure so try both */
2269 command = ATA_ATA_IDENTIFY;
2270 retry_command = ATA_ATAPI_IDENTIFY;
2273 ptr = (uint16_t *)calloc(1, sizeof(struct ata_params));
2275 warnx("can't calloc memory for identify\n");
2280 error = ata_do_cmd(device,
2282 /*retries*/retry_count,
2283 /*flags*/CAM_DIR_IN,
2284 /*protocol*/AP_PROTO_PIO_IN,
2285 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2286 AP_FLAG_TLEN_SECT_CNT,
2287 /*tag_action*/MSG_SIMPLE_Q_TAG,
2291 /*sector_count*/sizeof(struct ata_params) / 512,
2292 /*data_ptr*/(u_int8_t *)ptr,
2293 /*dxfer_len*/sizeof(struct ata_params),
2294 /*timeout*/timeout ? timeout : 30 * 1000,
2298 if (retry_command != 0) {
2299 command = retry_command;
2307 ident_buf = (struct ata_params *)ptr;
2308 ata_param_fixup(ident_buf);
2311 for (i = 0; i < sizeof(struct ata_params) / 2; i++) {
2316 /* check for invalid (all zero) response */
2318 warnx("Invalid identify response detected");
2323 *ident_bufp = ident_buf;
2330 ataidentify(struct cam_device *device, int retry_count, int timeout)
2333 struct ata_params *ident_buf;
2334 u_int64_t hpasize = 0, nativesize = 0;
2336 if ((ccb = cam_getccb(device)) == NULL) {
2337 warnx("couldn't allocate CCB");
2341 if (ata_do_identify(device, retry_count, timeout, ccb, &ident_buf) != 0) {
2346 if (arglist & CAM_ARG_VERBOSE) {
2347 printf("%s%d: Raw identify data:\n",
2348 device->device_name, device->dev_unit_num);
2349 dump_data((uint16_t *)ident_buf, sizeof(struct ata_params));
2352 if (ident_buf->support.command1 & ATA_SUPPORT_PROTECTED) {
2353 ata_read_native_max(device, retry_count, timeout, ccb,
2354 ident_buf, &hpasize);
2356 if (ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR) {
2357 ata_get_native_max(device, retry_count, timeout, ccb,
2361 printf("%s%d: ", device->device_name, device->dev_unit_num);
2362 ata_print_ident(ident_buf);
2363 camxferrate(device);
2364 atacapprint(ident_buf);
2365 atahpa_print(ident_buf, hpasize, 0);
2366 ataama_print(ident_buf, nativesize, 0);
2376 nvmeidentify(struct cam_device *device, int retry_count __unused, int timeout __unused)
2378 struct nvme_controller_data cdata;
2380 if (nvme_get_cdata(device, &cdata))
2382 nvme_print_controller(&cdata);
2389 identify(struct cam_device *device, int retry_count, int timeout)
2392 struct ccb_pathinq cpi;
2394 if (get_cpi(device, &cpi) != 0) {
2395 warnx("couldn't get CPI");
2399 if (cpi.protocol == PROTO_NVME) {
2400 return (nvmeidentify(device, retry_count, timeout));
2403 return (ataidentify(device, retry_count, timeout));
2408 ATA_SECURITY_ACTION_PRINT,
2409 ATA_SECURITY_ACTION_FREEZE,
2410 ATA_SECURITY_ACTION_UNLOCK,
2411 ATA_SECURITY_ACTION_DISABLE,
2412 ATA_SECURITY_ACTION_ERASE,
2413 ATA_SECURITY_ACTION_ERASE_ENHANCED,
2414 ATA_SECURITY_ACTION_SET_PASSWORD
2418 atasecurity_print_time(u_int16_t tw)
2422 printf("unspecified");
2424 printf("> 508 min");
2426 printf("%i min", 2 * tw);
2430 atasecurity_erase_timeout_msecs(u_int16_t timeout)
2434 return 2 * 3600 * 1000; /* default: two hours */
2435 else if (timeout > 255)
2436 return (508 + 60) * 60 * 1000; /* spec says > 508 minutes */
2438 return ((2 * timeout) + 5) * 60 * 1000; /* add a 5min margin */
2443 atasecurity_notify(u_int8_t command, struct ata_security_password *pwd)
2447 bzero(&cmd, sizeof(cmd));
2448 cmd.command = command;
2449 printf("Issuing %s", ata_op_string(&cmd));
2452 /* pwd->password may not be null terminated */
2453 char pass[sizeof(pwd->password)+1];
2455 strlcpy(pass, pwd->password, sizeof(pass));
2456 printf(" password='%s', user='%s'",
2458 (pwd->ctrl & ATA_SECURITY_PASSWORD_MASTER) ?
2461 if (command == ATA_SECURITY_SET_PASSWORD) {
2462 printf(", mode='%s'",
2463 (pwd->ctrl & ATA_SECURITY_LEVEL_MAXIMUM) ?
2464 "maximum" : "high");
2472 atasecurity_freeze(struct cam_device *device, union ccb *ccb,
2473 int retry_count, u_int32_t timeout, int quiet)
2477 atasecurity_notify(ATA_SECURITY_FREEZE_LOCK, NULL);
2479 return ata_do_cmd(device,
2482 /*flags*/CAM_DIR_NONE,
2483 /*protocol*/AP_PROTO_NON_DATA,
2485 /*tag_action*/MSG_SIMPLE_Q_TAG,
2486 /*command*/ATA_SECURITY_FREEZE_LOCK,
2497 atasecurity_unlock(struct cam_device *device, union ccb *ccb,
2498 int retry_count, u_int32_t timeout,
2499 struct ata_security_password *pwd, int quiet)
2503 atasecurity_notify(ATA_SECURITY_UNLOCK, pwd);
2505 return ata_do_cmd(device,
2508 /*flags*/CAM_DIR_OUT,
2509 /*protocol*/AP_PROTO_PIO_OUT,
2510 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2511 AP_FLAG_TLEN_SECT_CNT,
2512 /*tag_action*/MSG_SIMPLE_Q_TAG,
2513 /*command*/ATA_SECURITY_UNLOCK,
2516 /*sector_count*/sizeof(*pwd) / 512,
2517 /*data_ptr*/(u_int8_t *)pwd,
2518 /*dxfer_len*/sizeof(*pwd),
2524 atasecurity_disable(struct cam_device *device, union ccb *ccb,
2525 int retry_count, u_int32_t timeout,
2526 struct ata_security_password *pwd, int quiet)
2530 atasecurity_notify(ATA_SECURITY_DISABLE_PASSWORD, pwd);
2531 return ata_do_cmd(device,
2534 /*flags*/CAM_DIR_OUT,
2535 /*protocol*/AP_PROTO_PIO_OUT,
2536 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2537 AP_FLAG_TLEN_SECT_CNT,
2538 /*tag_action*/MSG_SIMPLE_Q_TAG,
2539 /*command*/ATA_SECURITY_DISABLE_PASSWORD,
2542 /*sector_count*/sizeof(*pwd) / 512,
2543 /*data_ptr*/(u_int8_t *)pwd,
2544 /*dxfer_len*/sizeof(*pwd),
2551 atasecurity_erase_confirm(struct cam_device *device,
2552 struct ata_params* ident_buf)
2555 printf("\nYou are about to ERASE ALL DATA from the following"
2556 " device:\n%s%d,%s%d: ", device->device_name,
2557 device->dev_unit_num, device->given_dev_name,
2558 device->given_unit_number);
2559 ata_print_ident(ident_buf);
2563 printf("\nAre you SURE you want to ERASE ALL DATA? (yes/no) ");
2565 if (fgets(str, sizeof(str), stdin) != NULL) {
2566 if (strncasecmp(str, "yes", 3) == 0) {
2568 } else if (strncasecmp(str, "no", 2) == 0) {
2571 printf("Please answer \"yes\" or "
2582 atasecurity_erase(struct cam_device *device, union ccb *ccb,
2583 int retry_count, u_int32_t timeout,
2584 u_int32_t erase_timeout,
2585 struct ata_security_password *pwd, int quiet)
2590 atasecurity_notify(ATA_SECURITY_ERASE_PREPARE, NULL);
2592 error = ata_do_cmd(device,
2595 /*flags*/CAM_DIR_NONE,
2596 /*protocol*/AP_PROTO_NON_DATA,
2598 /*tag_action*/MSG_SIMPLE_Q_TAG,
2599 /*command*/ATA_SECURITY_ERASE_PREPARE,
2612 atasecurity_notify(ATA_SECURITY_ERASE_UNIT, pwd);
2614 error = ata_do_cmd(device,
2617 /*flags*/CAM_DIR_OUT,
2618 /*protocol*/AP_PROTO_PIO_OUT,
2619 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2620 AP_FLAG_TLEN_SECT_CNT,
2621 /*tag_action*/MSG_SIMPLE_Q_TAG,
2622 /*command*/ATA_SECURITY_ERASE_UNIT,
2625 /*sector_count*/sizeof(*pwd) / 512,
2626 /*data_ptr*/(u_int8_t *)pwd,
2627 /*dxfer_len*/sizeof(*pwd),
2628 /*timeout*/erase_timeout,
2631 if (error == 0 && quiet == 0)
2632 printf("\nErase Complete\n");
2638 atasecurity_set_password(struct cam_device *device, union ccb *ccb,
2639 int retry_count, u_int32_t timeout,
2640 struct ata_security_password *pwd, int quiet)
2644 atasecurity_notify(ATA_SECURITY_SET_PASSWORD, pwd);
2646 return ata_do_cmd(device,
2649 /*flags*/CAM_DIR_OUT,
2650 /*protocol*/AP_PROTO_PIO_OUT,
2651 /*ata_flags*/AP_FLAG_BYT_BLOK_BLOCKS |
2652 AP_FLAG_TLEN_SECT_CNT,
2653 /*tag_action*/MSG_SIMPLE_Q_TAG,
2654 /*command*/ATA_SECURITY_SET_PASSWORD,
2657 /*sector_count*/sizeof(*pwd) / 512,
2658 /*data_ptr*/(u_int8_t *)pwd,
2659 /*dxfer_len*/sizeof(*pwd),
2665 atasecurity_print(struct ata_params *parm)
2668 printf("\nSecurity Option Value\n");
2669 if (arglist & CAM_ARG_VERBOSE) {
2670 printf("status %04x\n",
2671 parm->security_status);
2673 printf("supported %s\n",
2674 parm->security_status & ATA_SECURITY_SUPPORTED ? "yes" : "no");
2675 if (!(parm->security_status & ATA_SECURITY_SUPPORTED))
2677 printf("enabled %s\n",
2678 parm->security_status & ATA_SECURITY_ENABLED ? "yes" : "no");
2679 printf("drive locked %s\n",
2680 parm->security_status & ATA_SECURITY_LOCKED ? "yes" : "no");
2681 printf("security config frozen %s\n",
2682 parm->security_status & ATA_SECURITY_FROZEN ? "yes" : "no");
2683 printf("count expired %s\n",
2684 parm->security_status & ATA_SECURITY_COUNT_EXP ? "yes" : "no");
2685 printf("security level %s\n",
2686 parm->security_status & ATA_SECURITY_LEVEL ? "maximum" : "high");
2687 printf("enhanced erase supported %s\n",
2688 parm->security_status & ATA_SECURITY_ENH_SUPP ? "yes" : "no");
2689 printf("erase time ");
2690 atasecurity_print_time(parm->erase_time);
2692 printf("enhanced erase time ");
2693 atasecurity_print_time(parm->enhanced_erase_time);
2695 printf("master password rev %04x%s\n",
2696 parm->master_passwd_revision,
2697 parm->master_passwd_revision == 0x0000 ||
2698 parm->master_passwd_revision == 0xFFFF ? " (unsupported)" : "");
2702 * Validates and copies the password in optarg to the passed buffer.
2703 * If the password in optarg is the same length as the buffer then
2704 * the data will still be copied but no null termination will occur.
2707 ata_getpwd(u_int8_t *passwd, int max, char opt)
2711 len = strlen(optarg);
2713 warnx("-%c password is too long", opt);
2715 } else if (len == 0) {
2716 warnx("-%c password is missing", opt);
2718 } else if (optarg[0] == '-'){
2719 warnx("-%c password starts with '-' (generic arg?)", opt);
2721 } else if (strlen(passwd) != 0 && strcmp(passwd, optarg) != 0) {
2722 warnx("-%c password conflicts with existing password from -%c",
2727 /* Callers pass in a buffer which does NOT need to be terminated */
2728 strncpy(passwd, optarg, max);
2735 ATA_HPA_ACTION_PRINT,
2736 ATA_HPA_ACTION_SET_MAX,
2737 ATA_HPA_ACTION_SET_PWD,
2738 ATA_HPA_ACTION_LOCK,
2739 ATA_HPA_ACTION_UNLOCK,
2740 ATA_HPA_ACTION_FREEZE_LOCK
2744 atahpa_set_confirm(struct cam_device *device, struct ata_params* ident_buf,
2745 u_int64_t maxsize, int persist)
2747 printf("\nYou are about to configure HPA to limit the user accessible\n"
2748 "sectors to %ju %s on the device:\n%s%d,%s%d: ", maxsize,
2749 persist ? "persistently" : "temporarily",
2750 device->device_name, device->dev_unit_num,
2751 device->given_dev_name, device->given_unit_number);
2752 ata_print_ident(ident_buf);
2756 printf("\nAre you SURE you want to configure HPA? (yes/no) ");
2758 if (NULL != fgets(str, sizeof(str), stdin)) {
2759 if (0 == strncasecmp(str, "yes", 3)) {
2761 } else if (0 == strncasecmp(str, "no", 2)) {
2764 printf("Please answer \"yes\" or "
2775 atahpa(struct cam_device *device, int retry_count, int timeout,
2776 int argc, char **argv, char *combinedopt)
2779 struct ata_params *ident_buf;
2780 struct ccb_getdev cgd;
2781 struct ata_set_max_pwd pwd;
2782 int error, confirm, quiet, c, action, actions, persist;
2783 int security, is48bit, pwdsize;
2784 u_int64_t hpasize, maxsize;
2793 memset(&pwd, 0, sizeof(pwd));
2795 /* default action is to print hpa information */
2796 action = ATA_HPA_ACTION_PRINT;
2797 pwdsize = sizeof(pwd.password);
2799 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2802 action = ATA_HPA_ACTION_SET_MAX;
2803 maxsize = strtoumax(optarg, NULL, 0);
2808 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
2810 action = ATA_HPA_ACTION_SET_PWD;
2816 action = ATA_HPA_ACTION_LOCK;
2822 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
2824 action = ATA_HPA_ACTION_UNLOCK;
2830 action = ATA_HPA_ACTION_FREEZE_LOCK;
2850 warnx("too many hpa actions specified");
2854 if (get_cgd(device, &cgd) != 0) {
2855 warnx("couldn't get CGD");
2859 ccb = cam_getccb(device);
2861 warnx("couldn't allocate CCB");
2865 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
2872 printf("%s%d: ", device->device_name, device->dev_unit_num);
2873 ata_print_ident(ident_buf);
2874 camxferrate(device);
2877 if (action == ATA_HPA_ACTION_PRINT) {
2879 if (ident_buf->support.command1 & ATA_SUPPORT_PROTECTED)
2880 ata_read_native_max(device, retry_count, timeout, ccb,
2881 ident_buf, &hpasize);
2882 atahpa_print(ident_buf, hpasize, 1);
2889 if (!(ident_buf->support.command1 & ATA_SUPPORT_PROTECTED)) {
2890 warnx("HPA is not supported by this device");
2896 if (security && !(ident_buf->support.command2 & ATA_SUPPORT_MAXSECURITY)) {
2897 warnx("HPA Security is not supported by this device");
2903 is48bit = ident_buf->support.command2 & ATA_SUPPORT_ADDRESS48;
2906 * The ATA spec requires:
2907 * 1. Read native max addr is called directly before set max addr
2908 * 2. Read native max addr is NOT called before any other set max call
2911 case ATA_HPA_ACTION_SET_MAX:
2913 atahpa_set_confirm(device, ident_buf, maxsize,
2920 error = ata_read_native_max(device, retry_count, timeout,
2921 ccb, ident_buf, &hpasize);
2923 error = atahpa_set_max(device, retry_count, timeout,
2924 ccb, is48bit, maxsize, persist);
2927 /* redo identify to get new values */
2928 error = ata_do_identify(device,
2929 retry_count, timeout, ccb,
2931 atahpa_print(ident_buf, hpasize, 1);
2933 /* Hint CAM to reprobe the device. */
2939 case ATA_HPA_ACTION_SET_PWD:
2940 error = atahpa_password(device, retry_count, timeout,
2941 ccb, is48bit, &pwd);
2942 if (error == 0 && quiet == 0)
2943 printf("HPA password has been set\n");
2946 case ATA_HPA_ACTION_LOCK:
2947 error = atahpa_lock(device, retry_count, timeout,
2949 if (error == 0 && quiet == 0)
2950 printf("HPA has been locked\n");
2953 case ATA_HPA_ACTION_UNLOCK:
2954 error = atahpa_unlock(device, retry_count, timeout,
2955 ccb, is48bit, &pwd);
2956 if (error == 0 && quiet == 0)
2957 printf("HPA has been unlocked\n");
2960 case ATA_HPA_ACTION_FREEZE_LOCK:
2961 error = atahpa_freeze_lock(device, retry_count, timeout,
2963 if (error == 0 && quiet == 0)
2964 printf("HPA has been frozen\n");
2968 errx(1, "Option currently not supported");
2978 ATA_AMA_ACTION_PRINT,
2979 ATA_AMA_ACTION_SET_MAX,
2980 ATA_AMA_ACTION_FREEZE_LOCK
2984 ataama(struct cam_device *device, int retry_count, int timeout,
2985 int argc, char **argv, char *combinedopt)
2988 struct ata_params *ident_buf;
2989 struct ccb_getdev cgd;
2990 int error, quiet, c, action, actions;
2991 u_int64_t nativesize, maxsize;
2997 /* default action is to print AMA information */
2998 action = ATA_AMA_ACTION_PRINT;
3000 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3003 action = ATA_AMA_ACTION_SET_MAX;
3004 maxsize = strtoumax(optarg, NULL, 0);
3009 action = ATA_AMA_ACTION_FREEZE_LOCK;
3020 warnx("too many AMA actions specified");
3024 if (get_cgd(device, &cgd) != 0) {
3025 warnx("couldn't get CGD");
3029 ccb = cam_getccb(device);
3031 warnx("couldn't allocate CCB");
3035 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
3042 printf("%s%d: ", device->device_name, device->dev_unit_num);
3043 ata_print_ident(ident_buf);
3044 camxferrate(device);
3047 if (action == ATA_AMA_ACTION_PRINT) {
3049 if (ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR)
3050 ata_get_native_max(device, retry_count, timeout, ccb,
3052 ataama_print(ident_buf, nativesize, 1);
3059 if (!(ident_buf->support2 & ATA_SUPPORT_AMAX_ADDR)) {
3060 warnx("Accessible Max Address is not supported by this device");
3067 case ATA_AMA_ACTION_SET_MAX:
3068 error = ata_get_native_max(device, retry_count, timeout, ccb,
3071 error = ataama_set(device, retry_count, timeout,
3075 /* redo identify to get new values */
3076 error = ata_do_identify(device,
3077 retry_count, timeout, ccb,
3079 ataama_print(ident_buf, nativesize, 1);
3081 /* Hint CAM to reprobe the device. */
3087 case ATA_AMA_ACTION_FREEZE_LOCK:
3088 error = ataama_freeze(device, retry_count, timeout,
3090 if (error == 0 && quiet == 0)
3091 printf("Accessible Max Address has been frozen\n");
3095 errx(1, "Option currently not supported");
3105 atasecurity(struct cam_device *device, int retry_count, int timeout,
3106 int argc, char **argv, char *combinedopt)
3109 struct ata_params *ident_buf;
3110 int error, confirm, quiet, c, action, actions, setpwd;
3111 int security_enabled, erase_timeout, pwdsize;
3112 struct ata_security_password pwd;
3120 memset(&pwd, 0, sizeof(pwd));
3122 /* default action is to print security information */
3123 action = ATA_SECURITY_ACTION_PRINT;
3125 /* user is master by default as its safer that way */
3126 pwd.ctrl |= ATA_SECURITY_PASSWORD_MASTER;
3127 pwdsize = sizeof(pwd.password);
3129 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3132 action = ATA_SECURITY_ACTION_FREEZE;
3137 if (strcasecmp(optarg, "user") == 0) {
3138 pwd.ctrl |= ATA_SECURITY_PASSWORD_USER;
3139 pwd.ctrl &= ~ATA_SECURITY_PASSWORD_MASTER;
3140 } else if (strcasecmp(optarg, "master") == 0) {
3141 pwd.ctrl |= ATA_SECURITY_PASSWORD_MASTER;
3142 pwd.ctrl &= ~ATA_SECURITY_PASSWORD_USER;
3144 warnx("-U argument '%s' is invalid (must be "
3145 "'user' or 'master')", optarg);
3151 if (strcasecmp(optarg, "high") == 0) {
3152 pwd.ctrl |= ATA_SECURITY_LEVEL_HIGH;
3153 pwd.ctrl &= ~ATA_SECURITY_LEVEL_MAXIMUM;
3154 } else if (strcasecmp(optarg, "maximum") == 0) {
3155 pwd.ctrl |= ATA_SECURITY_LEVEL_MAXIMUM;
3156 pwd.ctrl &= ~ATA_SECURITY_LEVEL_HIGH;
3158 warnx("-l argument '%s' is unknown (must be "
3159 "'high' or 'maximum')", optarg);
3165 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3167 action = ATA_SECURITY_ACTION_UNLOCK;
3172 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3174 action = ATA_SECURITY_ACTION_DISABLE;
3179 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3181 action = ATA_SECURITY_ACTION_ERASE;
3186 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3188 pwd.ctrl |= ATA_SECURITY_ERASE_ENHANCED;
3189 action = ATA_SECURITY_ACTION_ERASE_ENHANCED;
3194 if (ata_getpwd(pwd.password, pwdsize, c) != 0)
3197 if (action == ATA_SECURITY_ACTION_PRINT)
3198 action = ATA_SECURITY_ACTION_SET_PASSWORD;
3200 * Don't increment action as this can be combined
3201 * with other actions.
3214 erase_timeout = atoi(optarg) * 1000;
3220 warnx("too many security actions specified");
3224 if ((ccb = cam_getccb(device)) == NULL) {
3225 warnx("couldn't allocate CCB");
3229 error = ata_do_identify(device, retry_count, timeout, ccb, &ident_buf);
3236 printf("%s%d: ", device->device_name, device->dev_unit_num);
3237 ata_print_ident(ident_buf);
3238 camxferrate(device);
3241 if (action == ATA_SECURITY_ACTION_PRINT) {
3242 atasecurity_print(ident_buf);
3248 if ((ident_buf->support.command1 & ATA_SUPPORT_SECURITY) == 0) {
3249 warnx("Security not supported");
3255 /* default timeout 15 seconds the same as linux hdparm */
3256 timeout = timeout ? timeout : 15 * 1000;
3258 security_enabled = ident_buf->security_status & ATA_SECURITY_ENABLED;
3260 /* first set the password if requested */
3262 /* confirm we can erase before setting the password if erasing */
3264 (action == ATA_SECURITY_ACTION_ERASE_ENHANCED ||
3265 action == ATA_SECURITY_ACTION_ERASE) &&
3266 atasecurity_erase_confirm(device, ident_buf) == 0) {
3272 if (pwd.ctrl & ATA_SECURITY_PASSWORD_MASTER) {
3273 pwd.revision = ident_buf->master_passwd_revision;
3274 if (pwd.revision != 0 && pwd.revision != 0xfff &&
3275 --pwd.revision == 0) {
3276 pwd.revision = 0xfffe;
3279 error = atasecurity_set_password(device, ccb, retry_count,
3280 timeout, &pwd, quiet);
3286 security_enabled = 1;
3290 case ATA_SECURITY_ACTION_FREEZE:
3291 error = atasecurity_freeze(device, ccb, retry_count,
3295 case ATA_SECURITY_ACTION_UNLOCK:
3296 if (security_enabled) {
3297 if (ident_buf->security_status & ATA_SECURITY_LOCKED) {
3298 error = atasecurity_unlock(device, ccb,
3299 retry_count, timeout, &pwd, quiet);
3301 warnx("Can't unlock, drive is not locked");
3305 warnx("Can't unlock, security is disabled");
3310 case ATA_SECURITY_ACTION_DISABLE:
3311 if (security_enabled) {
3312 /* First unlock the drive if its locked */
3313 if (ident_buf->security_status & ATA_SECURITY_LOCKED) {
3314 error = atasecurity_unlock(device, ccb,
3322 error = atasecurity_disable(device,
3330 warnx("Can't disable security (already disabled)");
3335 case ATA_SECURITY_ACTION_ERASE:
3336 if (security_enabled) {
3337 if (erase_timeout == 0) {
3338 erase_timeout = atasecurity_erase_timeout_msecs(
3339 ident_buf->erase_time);
3342 error = atasecurity_erase(device, ccb, retry_count,
3343 timeout, erase_timeout, &pwd, quiet);
3345 warnx("Can't secure erase (security is disabled)");
3350 case ATA_SECURITY_ACTION_ERASE_ENHANCED:
3351 if (security_enabled) {
3352 if (ident_buf->security_status & ATA_SECURITY_ENH_SUPP) {
3353 if (erase_timeout == 0) {
3355 atasecurity_erase_timeout_msecs(
3356 ident_buf->enhanced_erase_time);
3359 error = atasecurity_erase(device, ccb,
3360 retry_count, timeout,
3361 erase_timeout, &pwd,
3364 warnx("Enhanced erase is not supported");
3368 warnx("Can't secure erase (enhanced), "
3369 "(security is disabled)");
3382 * Convert periph name into a bus, target and lun.
3384 * Returns the number of parsed components, or 0.
3387 parse_btl_name(char *tstr, path_id_t *bus, target_id_t *target, lun_id_t *lun,
3388 cam_argmask *arglst)
3393 bzero(&ccb, sizeof(ccb));
3394 ccb.ccb_h.func_code = XPT_GDEVLIST;
3395 if (cam_get_device(tstr, ccb.cgdl.periph_name,
3396 sizeof(ccb.cgdl.periph_name), &ccb.cgdl.unit_number) == -1) {
3397 warnx("%s", cam_errbuf);
3402 * Attempt to get the passthrough device. This ioctl will
3403 * fail if the device name is null, if the device doesn't
3404 * exist, or if the passthrough driver isn't in the kernel.
3406 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
3407 warn("Unable to open %s", XPT_DEVICE);
3410 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) {
3411 warn("Unable to find bus:target:lun for device %s%d",
3412 ccb.cgdl.periph_name, ccb.cgdl.unit_number);
3417 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3418 const struct cam_status_entry *entry;
3420 entry = cam_fetch_status_entry(ccb.ccb_h.status);
3421 warnx("Unable to find bus:target_lun for device %s%d, "
3422 "CAM status: %s (%#x)",
3423 ccb.cgdl.periph_name, ccb.cgdl.unit_number,
3424 entry ? entry->status_text : "Unknown",
3430 * The kernel fills in the bus/target/lun. We don't
3431 * need the passthrough device name and unit number since
3432 * we aren't going to open it.
3434 *bus = ccb.ccb_h.path_id;
3435 *target = ccb.ccb_h.target_id;
3436 *lun = ccb.ccb_h.target_lun;
3437 *arglst |= CAM_ARG_BUS | CAM_ARG_TARGET | CAM_ARG_LUN;
3442 * Parse out a bus, or a bus, target and lun in the following
3448 * Returns the number of parsed components, or 0.
3451 parse_btl(char *tstr, path_id_t *bus, target_id_t *target, lun_id_t *lun,
3452 cam_argmask *arglst)
3457 *bus = CAM_BUS_WILDCARD;
3458 *target = CAM_TARGET_WILDCARD;
3459 *lun = CAM_LUN_WILDCARD;
3461 while (isspace(*tstr) && (*tstr != '\0'))
3464 if (strncasecmp(tstr, "all", strlen("all")) == 0) {
3465 arglist |= CAM_ARG_BUS;
3469 if (!isdigit(*tstr))
3470 return (parse_btl_name(tstr, bus, target, lun, arglst));
3472 tmpstr = strsep(&tstr, ":");
3473 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3474 *bus = strtol(tmpstr, &end, 0);
3477 *arglst |= CAM_ARG_BUS;
3479 tmpstr = strsep(&tstr, ":");
3480 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3481 *target = strtol(tmpstr, &end, 0);
3484 *arglst |= CAM_ARG_TARGET;
3486 tmpstr = strsep(&tstr, ":");
3487 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
3488 *lun = strtoll(tmpstr, &end, 0);
3491 *arglst |= CAM_ARG_LUN;
3501 dorescan_or_reset(int argc, char **argv, int rescan)
3503 static const char must[] =
3504 "you must specify \"all\", a bus, a bus:target:lun or periph to %s";
3506 path_id_t bus = CAM_BUS_WILDCARD;
3507 target_id_t target = CAM_TARGET_WILDCARD;
3508 lun_id_t lun = CAM_LUN_WILDCARD;
3512 warnx(must, rescan? "rescan" : "reset");
3516 tstr = argv[optind];
3517 while (isspace(*tstr) && (*tstr != '\0'))
3519 if (strncasecmp(tstr, "all", strlen("all")) == 0)
3520 arglist |= CAM_ARG_BUS;
3522 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist);
3523 if (rv != 1 && rv != 3) {
3524 warnx(must, rescan ? "rescan" : "reset");
3529 if (arglist & CAM_ARG_LUN)
3530 error = scanlun_or_reset_dev(bus, target, lun, rescan);
3532 error = rescan_or_reset_bus(bus, rescan);
3538 rescan_or_reset_bus(path_id_t bus, int rescan)
3540 union ccb *ccb = NULL, *matchccb = NULL;
3541 int fd = -1, retval;
3546 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
3547 warnx("error opening transport layer device %s", XPT_DEVICE);
3548 warn("%s", XPT_DEVICE);
3552 ccb = malloc(sizeof(*ccb));
3554 warn("failed to allocate CCB");
3558 bzero(ccb, sizeof(*ccb));
3560 if (bus != CAM_BUS_WILDCARD) {
3561 ccb->ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS;
3562 ccb->ccb_h.path_id = bus;
3563 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
3564 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
3565 ccb->crcn.flags = CAM_FLAG_NONE;
3567 /* run this at a low priority */
3568 ccb->ccb_h.pinfo.priority = 5;
3570 if (ioctl(fd, CAMIOCOMMAND, ccb) == -1) {
3571 warn("CAMIOCOMMAND ioctl failed");
3576 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
3577 fprintf(stdout, "%s of bus %d was successful\n",
3578 rescan ? "Re-scan" : "Reset", bus);
3580 fprintf(stdout, "%s of bus %d returned error %#x\n",
3581 rescan ? "Re-scan" : "Reset", bus,
3582 ccb->ccb_h.status & CAM_STATUS_MASK);
3591 * The right way to handle this is to modify the xpt so that it can
3592 * handle a wildcarded bus in a rescan or reset CCB. At the moment
3593 * that isn't implemented, so instead we enumerate the buses and
3594 * send the rescan or reset to those buses in the case where the
3595 * given bus is -1 (wildcard). We don't send a rescan or reset
3596 * to the xpt bus; sending a rescan to the xpt bus is effectively a
3597 * no-op, sending a rescan to the xpt bus would result in a status of
3600 matchccb = malloc(sizeof(*matchccb));
3601 if (matchccb == NULL) {
3602 warn("failed to allocate CCB");
3606 bzero(matchccb, sizeof(*matchccb));
3607 matchccb->ccb_h.func_code = XPT_DEV_MATCH;
3608 matchccb->ccb_h.path_id = CAM_BUS_WILDCARD;
3609 bufsize = sizeof(struct dev_match_result) * 20;
3610 matchccb->cdm.match_buf_len = bufsize;
3611 matchccb->cdm.matches=(struct dev_match_result *)malloc(bufsize);
3612 if (matchccb->cdm.matches == NULL) {
3613 warnx("can't malloc memory for matches");
3617 matchccb->cdm.num_matches = 0;
3619 matchccb->cdm.num_patterns = 1;
3620 matchccb->cdm.pattern_buf_len = sizeof(struct dev_match_pattern);
3622 matchccb->cdm.patterns = (struct dev_match_pattern *)malloc(
3623 matchccb->cdm.pattern_buf_len);
3624 if (matchccb->cdm.patterns == NULL) {
3625 warnx("can't malloc memory for patterns");
3629 matchccb->cdm.patterns[0].type = DEV_MATCH_BUS;
3630 matchccb->cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY;
3635 if (ioctl(fd, CAMIOCOMMAND, matchccb) == -1) {
3636 warn("CAMIOCOMMAND ioctl failed");
3641 if ((matchccb->ccb_h.status != CAM_REQ_CMP)
3642 || ((matchccb->cdm.status != CAM_DEV_MATCH_LAST)
3643 && (matchccb->cdm.status != CAM_DEV_MATCH_MORE))) {
3644 warnx("got CAM error %#x, CDM error %d\n",
3645 matchccb->ccb_h.status, matchccb->cdm.status);
3650 for (i = 0; i < matchccb->cdm.num_matches; i++) {
3651 struct bus_match_result *bus_result;
3653 /* This shouldn't happen. */
3654 if (matchccb->cdm.matches[i].type != DEV_MATCH_BUS)
3657 bus_result =&matchccb->cdm.matches[i].result.bus_result;
3660 * We don't want to rescan or reset the xpt bus.
3663 if (bus_result->path_id == CAM_XPT_PATH_ID)
3666 ccb->ccb_h.func_code = rescan ? XPT_SCAN_BUS :
3668 ccb->ccb_h.path_id = bus_result->path_id;
3669 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
3670 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
3671 ccb->crcn.flags = CAM_FLAG_NONE;
3673 /* run this at a low priority */
3674 ccb->ccb_h.pinfo.priority = 5;
3676 if (ioctl(fd, CAMIOCOMMAND, ccb) == -1) {
3677 warn("CAMIOCOMMAND ioctl failed");
3682 if ((ccb->ccb_h.status & CAM_STATUS_MASK)==CAM_REQ_CMP){
3683 fprintf(stdout, "%s of bus %d was successful\n",
3684 rescan? "Re-scan" : "Reset",
3685 bus_result->path_id);
3688 * Don't bail out just yet, maybe the other
3689 * rescan or reset commands will complete
3692 fprintf(stderr, "%s of bus %d returned error "
3693 "%#x\n", rescan? "Re-scan" : "Reset",
3694 bus_result->path_id,
3695 ccb->ccb_h.status & CAM_STATUS_MASK);
3699 } while ((matchccb->ccb_h.status == CAM_REQ_CMP)
3700 && (matchccb->cdm.status == CAM_DEV_MATCH_MORE));
3707 if (matchccb != NULL) {
3708 free(matchccb->cdm.patterns);
3709 free(matchccb->cdm.matches);
3718 scanlun_or_reset_dev(path_id_t bus, target_id_t target, lun_id_t lun, int scan)
3721 struct cam_device *device;
3726 if (bus == CAM_BUS_WILDCARD) {
3727 warnx("invalid bus number %d", bus);
3731 if (target == CAM_TARGET_WILDCARD) {
3732 warnx("invalid target number %d", target);
3736 if (lun == CAM_LUN_WILDCARD) {
3737 warnx("invalid lun number %jx", (uintmax_t)lun);
3743 bzero(&ccb, sizeof(union ccb));
3746 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
3747 warnx("error opening transport layer device %s\n",
3749 warn("%s", XPT_DEVICE);
3753 device = cam_open_btl(bus, target, lun, O_RDWR, NULL);
3754 if (device == NULL) {
3755 warnx("%s", cam_errbuf);
3760 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV;
3761 ccb.ccb_h.path_id = bus;
3762 ccb.ccb_h.target_id = target;
3763 ccb.ccb_h.target_lun = lun;
3764 ccb.ccb_h.timeout = 5000;
3765 ccb.crcn.flags = CAM_FLAG_NONE;
3767 /* run this at a low priority */
3768 ccb.ccb_h.pinfo.priority = 5;
3771 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) {
3772 warn("CAMIOCOMMAND ioctl failed");
3777 if (cam_send_ccb(device, &ccb) < 0) {
3778 warn("error sending XPT_RESET_DEV CCB");
3779 cam_close_device(device);
3787 cam_close_device(device);
3790 * An error code of CAM_BDR_SENT is normal for a BDR request.
3792 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
3794 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) {
3795 fprintf(stdout, "%s of %d:%d:%jx was successful\n",
3796 scan? "Re-scan" : "Reset", bus, target, (uintmax_t)lun);
3799 fprintf(stdout, "%s of %d:%d:%jx returned error %#x\n",
3800 scan? "Re-scan" : "Reset", bus, target, (uintmax_t)lun,
3801 ccb.ccb_h.status & CAM_STATUS_MASK);
3807 static struct scsi_nv defect_list_type_map[] = {
3808 { "block", SRDD10_BLOCK_FORMAT },
3809 { "extbfi", SRDD10_EXT_BFI_FORMAT },
3810 { "extphys", SRDD10_EXT_PHYS_FORMAT },
3811 { "longblock", SRDD10_LONG_BLOCK_FORMAT },
3812 { "bfi", SRDD10_BYTES_FROM_INDEX_FORMAT },
3813 { "phys", SRDD10_PHYSICAL_SECTOR_FORMAT }
3817 readdefects(struct cam_device *device, int argc, char **argv,
3818 char *combinedopt, int task_attr, int retry_count, int timeout)
3820 union ccb *ccb = NULL;
3821 struct scsi_read_defect_data_hdr_10 *hdr10 = NULL;
3822 struct scsi_read_defect_data_hdr_12 *hdr12 = NULL;
3823 size_t hdr_size = 0, entry_size = 0;
3826 u_int8_t *defect_list = NULL;
3827 u_int8_t list_format = 0;
3828 int list_type_set = 0;
3829 u_int32_t dlist_length = 0;
3830 u_int32_t returned_length = 0, valid_len = 0;
3831 u_int32_t num_returned = 0, num_valid = 0;
3832 u_int32_t max_possible_size = 0, hdr_max = 0;
3833 u_int32_t starting_offset = 0;
3834 u_int8_t returned_format, returned_type;
3836 int summary = 0, quiet = 0;
3838 int lists_specified = 0;
3839 int get_length = 1, first_pass = 1;
3842 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3846 scsi_nv_status status;
3849 status = scsi_get_nv(defect_list_type_map,
3850 sizeof(defect_list_type_map) /
3851 sizeof(defect_list_type_map[0]), optarg,
3852 &entry_num, SCSI_NV_FLAG_IG_CASE);
3854 if (status == SCSI_NV_FOUND) {
3855 list_format = defect_list_type_map[
3859 warnx("%s: %s %s option %s", __func__,
3860 (status == SCSI_NV_AMBIGUOUS) ?
3861 "ambiguous" : "invalid", "defect list type",
3864 goto defect_bailout;
3869 arglist |= CAM_ARG_GLIST;
3872 arglist |= CAM_ARG_PLIST;
3883 starting_offset = strtoul(optarg, &endptr, 0);
3884 if (*endptr != '\0') {
3886 warnx("invalid starting offset %s", optarg);
3887 goto defect_bailout;
3899 if (list_type_set == 0) {
3901 warnx("no defect list format specified");
3902 goto defect_bailout;
3905 if (arglist & CAM_ARG_PLIST) {
3906 list_format |= SRDD10_PLIST;
3910 if (arglist & CAM_ARG_GLIST) {
3911 list_format |= SRDD10_GLIST;
3916 * This implies a summary, and was the previous behavior.
3918 if (lists_specified == 0)
3921 ccb = cam_getccb(device);
3926 * We start off asking for just the header to determine how much
3927 * defect data is available. Some Hitachi drives return an error
3928 * if you ask for more data than the drive has. Once we know the
3929 * length, we retry the command with the returned length.
3931 if (use_12byte == 0)
3932 dlist_length = sizeof(*hdr10);
3934 dlist_length = sizeof(*hdr12);
3937 if (defect_list != NULL) {
3941 defect_list = malloc(dlist_length);
3942 if (defect_list == NULL) {
3943 warnx("can't malloc memory for defect list");
3945 goto defect_bailout;
3949 bzero(defect_list, dlist_length);
3952 * cam_getccb() zeros the CCB header only. So we need to zero the
3953 * payload portion of the ccb.
3955 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
3957 scsi_read_defects(&ccb->csio,
3958 /*retries*/ retry_count,
3960 /*tag_action*/ task_attr,
3961 /*list_format*/ list_format,
3962 /*addr_desc_index*/ starting_offset,
3963 /*data_ptr*/ defect_list,
3964 /*dxfer_len*/ dlist_length,
3965 /*minimum_cmd_size*/ use_12byte ? 12 : 0,
3966 /*sense_len*/ SSD_FULL_SIZE,
3967 /*timeout*/ timeout ? timeout : 5000);
3969 /* Disable freezing the device queue */
3970 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
3972 if (cam_send_ccb(device, ccb) < 0) {
3973 warn("error sending READ DEFECT DATA command");
3975 goto defect_bailout;
3978 valid_len = ccb->csio.dxfer_len - ccb->csio.resid;
3980 if (use_12byte == 0) {
3981 hdr10 = (struct scsi_read_defect_data_hdr_10 *)defect_list;
3982 hdr_size = sizeof(*hdr10);
3983 hdr_max = SRDDH10_MAX_LENGTH;
3985 if (valid_len >= hdr_size) {
3986 returned_length = scsi_2btoul(hdr10->length);
3987 returned_format = hdr10->format;
3989 returned_length = 0;
3990 returned_format = 0;
3993 hdr12 = (struct scsi_read_defect_data_hdr_12 *)defect_list;
3994 hdr_size = sizeof(*hdr12);
3995 hdr_max = SRDDH12_MAX_LENGTH;
3997 if (valid_len >= hdr_size) {
3998 returned_length = scsi_4btoul(hdr12->length);
3999 returned_format = hdr12->format;
4001 returned_length = 0;
4002 returned_format = 0;
4006 returned_type = returned_format & SRDDH10_DLIST_FORMAT_MASK;
4007 switch (returned_type) {
4008 case SRDD10_BLOCK_FORMAT:
4009 entry_size = sizeof(struct scsi_defect_desc_block);
4011 case SRDD10_LONG_BLOCK_FORMAT:
4012 entry_size = sizeof(struct scsi_defect_desc_long_block);
4014 case SRDD10_EXT_PHYS_FORMAT:
4015 case SRDD10_PHYSICAL_SECTOR_FORMAT:
4016 entry_size = sizeof(struct scsi_defect_desc_phys_sector);
4018 case SRDD10_EXT_BFI_FORMAT:
4019 case SRDD10_BYTES_FROM_INDEX_FORMAT:
4020 entry_size = sizeof(struct scsi_defect_desc_bytes_from_index);
4023 warnx("Unknown defect format 0x%x\n", returned_type);
4025 goto defect_bailout;
4029 max_possible_size = (hdr_max / entry_size) * entry_size;
4030 num_returned = returned_length / entry_size;
4031 num_valid = min(returned_length, valid_len - hdr_size);
4032 num_valid /= entry_size;
4034 if (get_length != 0) {
4037 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
4038 CAM_SCSI_STATUS_ERROR) {
4039 struct scsi_sense_data *sense;
4040 int error_code, sense_key, asc, ascq;
4042 sense = &ccb->csio.sense_data;
4043 scsi_extract_sense_len(sense, ccb->csio.sense_len -
4044 ccb->csio.sense_resid, &error_code, &sense_key,
4045 &asc, &ascq, /*show_errors*/ 1);
4048 * If the drive is reporting that it just doesn't
4049 * support the defect list format, go ahead and use
4050 * the length it reported. Otherwise, the length
4051 * may not be valid, so use the maximum.
4053 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4054 && (asc == 0x1c) && (ascq == 0x00)
4055 && (returned_length > 0)) {
4056 if ((use_12byte == 0)
4057 && (returned_length >= max_possible_size)) {
4062 dlist_length = returned_length + hdr_size;
4063 } else if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4064 && (asc == 0x1f) && (ascq == 0x00)
4065 && (returned_length > 0)) {
4066 /* Partial defect list transfer */
4068 * Hitachi drives return this error
4069 * along with a partial defect list if they
4070 * have more defects than the 10 byte
4071 * command can support. Retry with the 12
4074 if (use_12byte == 0) {
4079 dlist_length = returned_length + hdr_size;
4080 } else if ((sense_key == SSD_KEY_ILLEGAL_REQUEST)
4081 && (asc == 0x24) && (ascq == 0x00)) {
4082 /* Invalid field in CDB */
4084 * SBC-3 says that if the drive has more
4085 * defects than can be reported with the
4086 * 10 byte command, it should return this
4087 * error and no data. Retry with the 12
4090 if (use_12byte == 0) {
4095 dlist_length = returned_length + hdr_size;
4098 * If we got a SCSI error and no valid length,
4099 * just use the 10 byte maximum. The 12
4100 * byte maximum is too large.
4102 if (returned_length == 0)
4103 dlist_length = SRDD10_MAX_LENGTH;
4105 if ((use_12byte == 0)
4106 && (returned_length >=
4107 max_possible_size)) {
4112 dlist_length = returned_length +
4116 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
4119 warnx("Error reading defect header");
4120 if (arglist & CAM_ARG_VERBOSE)
4121 cam_error_print(device, ccb, CAM_ESF_ALL,
4122 CAM_EPF_ALL, stderr);
4123 goto defect_bailout;
4125 if ((use_12byte == 0)
4126 && (returned_length >= max_possible_size)) {
4131 dlist_length = returned_length + hdr_size;
4134 fprintf(stdout, "%u", num_returned);
4136 fprintf(stdout, " defect%s",
4137 (num_returned != 1) ? "s" : "");
4139 fprintf(stdout, "\n");
4141 goto defect_bailout;
4145 * We always limit the list length to the 10-byte maximum
4146 * length (0xffff). The reason is that some controllers
4147 * can't handle larger I/Os, and we can transfer the entire
4148 * 10 byte list in one shot. For drives that support the 12
4149 * byte read defects command, we'll step through the list
4150 * by specifying a starting offset. For drives that don't
4151 * support the 12 byte command's starting offset, we'll
4152 * just display the first 64K.
4154 dlist_length = min(dlist_length, SRDD10_MAX_LENGTH);
4160 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)
4161 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
4162 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
4163 struct scsi_sense_data *sense;
4164 int error_code, sense_key, asc, ascq;
4166 sense = &ccb->csio.sense_data;
4167 scsi_extract_sense_len(sense, ccb->csio.sense_len -
4168 ccb->csio.sense_resid, &error_code, &sense_key, &asc,
4169 &ascq, /*show_errors*/ 1);
4172 * According to the SCSI spec, if the disk doesn't support
4173 * the requested format, it will generally return a sense
4174 * key of RECOVERED ERROR, and an additional sense code
4175 * of "DEFECT LIST NOT FOUND". HGST drives also return
4176 * Primary/Grown defect list not found errors. So just
4177 * check for an ASC of 0x1c.
4179 if ((sense_key == SSD_KEY_RECOVERED_ERROR)
4181 const char *format_str;
4183 format_str = scsi_nv_to_str(defect_list_type_map,
4184 sizeof(defect_list_type_map) /
4185 sizeof(defect_list_type_map[0]),
4186 list_format & SRDD10_DLIST_FORMAT_MASK);
4187 warnx("requested defect format %s not available",
4188 format_str ? format_str : "unknown");
4190 format_str = scsi_nv_to_str(defect_list_type_map,
4191 sizeof(defect_list_type_map) /
4192 sizeof(defect_list_type_map[0]), returned_type);
4193 if (format_str != NULL) {
4194 warnx("Device returned %s format",
4198 warnx("Device returned unknown defect"
4199 " data format %#x", returned_type);
4200 goto defect_bailout;
4204 warnx("Error returned from read defect data command");
4205 if (arglist & CAM_ARG_VERBOSE)
4206 cam_error_print(device, ccb, CAM_ESF_ALL,
4207 CAM_EPF_ALL, stderr);
4208 goto defect_bailout;
4210 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4212 warnx("Error returned from read defect data command");
4213 if (arglist & CAM_ARG_VERBOSE)
4214 cam_error_print(device, ccb, CAM_ESF_ALL,
4215 CAM_EPF_ALL, stderr);
4216 goto defect_bailout;
4219 if (first_pass != 0) {
4220 fprintf(stderr, "Got %d defect", num_returned);
4222 if ((lists_specified == 0) || (num_returned == 0)) {
4223 fprintf(stderr, "s.\n");
4224 goto defect_bailout;
4225 } else if (num_returned == 1)
4226 fprintf(stderr, ":\n");
4228 fprintf(stderr, "s:\n");
4234 * XXX KDM I should probably clean up the printout format for the
4237 switch (returned_type) {
4238 case SRDD10_PHYSICAL_SECTOR_FORMAT:
4239 case SRDD10_EXT_PHYS_FORMAT:
4241 struct scsi_defect_desc_phys_sector *dlist;
4243 dlist = (struct scsi_defect_desc_phys_sector *)
4244 (defect_list + hdr_size);
4246 for (i = 0; i < num_valid; i++) {
4249 sector = scsi_4btoul(dlist[i].sector);
4250 if (returned_type == SRDD10_EXT_PHYS_FORMAT) {
4251 mads = (sector & SDD_EXT_PHYS_MADS) ?
4253 sector &= ~SDD_EXT_PHYS_FLAG_MASK;
4255 if (hex_format == 0)
4256 fprintf(stdout, "%d:%d:%d%s",
4257 scsi_3btoul(dlist[i].cylinder),
4259 scsi_4btoul(dlist[i].sector),
4260 mads ? " - " : "\n");
4262 fprintf(stdout, "0x%x:0x%x:0x%x%s",
4263 scsi_3btoul(dlist[i].cylinder),
4265 scsi_4btoul(dlist[i].sector),
4266 mads ? " - " : "\n");
4269 if (num_valid < num_returned) {
4270 starting_offset += num_valid;
4275 case SRDD10_BYTES_FROM_INDEX_FORMAT:
4276 case SRDD10_EXT_BFI_FORMAT:
4278 struct scsi_defect_desc_bytes_from_index *dlist;
4280 dlist = (struct scsi_defect_desc_bytes_from_index *)
4281 (defect_list + hdr_size);
4283 for (i = 0; i < num_valid; i++) {
4286 bfi = scsi_4btoul(dlist[i].bytes_from_index);
4287 if (returned_type == SRDD10_EXT_BFI_FORMAT) {
4288 mads = (bfi & SDD_EXT_BFI_MADS) ? 1 : 0;
4289 bfi &= ~SDD_EXT_BFI_FLAG_MASK;
4291 if (hex_format == 0)
4292 fprintf(stdout, "%d:%d:%d%s",
4293 scsi_3btoul(dlist[i].cylinder),
4295 scsi_4btoul(dlist[i].bytes_from_index),
4296 mads ? " - " : "\n");
4298 fprintf(stdout, "0x%x:0x%x:0x%x%s",
4299 scsi_3btoul(dlist[i].cylinder),
4301 scsi_4btoul(dlist[i].bytes_from_index),
4302 mads ? " - " : "\n");
4306 if (num_valid < num_returned) {
4307 starting_offset += num_valid;
4312 case SRDDH10_BLOCK_FORMAT:
4314 struct scsi_defect_desc_block *dlist;
4316 dlist = (struct scsi_defect_desc_block *)
4317 (defect_list + hdr_size);
4319 for (i = 0; i < num_valid; i++) {
4320 if (hex_format == 0)
4321 fprintf(stdout, "%u\n",
4322 scsi_4btoul(dlist[i].address));
4324 fprintf(stdout, "0x%x\n",
4325 scsi_4btoul(dlist[i].address));
4328 if (num_valid < num_returned) {
4329 starting_offset += num_valid;
4335 case SRDD10_LONG_BLOCK_FORMAT:
4337 struct scsi_defect_desc_long_block *dlist;
4339 dlist = (struct scsi_defect_desc_long_block *)
4340 (defect_list + hdr_size);
4342 for (i = 0; i < num_valid; i++) {
4343 if (hex_format == 0)
4344 fprintf(stdout, "%ju\n",
4345 (uintmax_t)scsi_8btou64(
4348 fprintf(stdout, "0x%jx\n",
4349 (uintmax_t)scsi_8btou64(
4353 if (num_valid < num_returned) {
4354 starting_offset += num_valid;
4360 fprintf(stderr, "Unknown defect format 0x%x\n",
4367 if (defect_list != NULL)
4378 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks)
4382 ccb = cam_getccb(device);
4389 mode_sense(struct cam_device *device, int *cdb_len, int dbd, int llbaa, int pc,
4390 int page, int subpage, int task_attr, int retry_count, int timeout,
4391 u_int8_t *data, int datalen)
4394 int error_code, sense_key, asc, ascq;
4396 ccb = cam_getccb(device);
4398 errx(1, "mode_sense: couldn't allocate CCB");
4402 * MODE SENSE(6) can't handle more then 255 bytes. If there are more,
4403 * device must return error, so we should not get trucated data.
4405 if (*cdb_len == 6 && datalen > 255)
4408 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
4410 scsi_mode_sense_subpage(&ccb->csio,
4411 /* retries */ retry_count,
4413 /* tag_action */ task_attr,
4417 /* subpage */ subpage,
4418 /* param_buf */ data,
4419 /* param_len */ datalen,
4420 /* minimum_cmd_size */ *cdb_len,
4421 /* sense_len */ SSD_FULL_SIZE,
4422 /* timeout */ timeout ? timeout : 5000);
4423 if (llbaa && ccb->csio.cdb_len == 10) {
4424 struct scsi_mode_sense_10 *cdb =
4425 (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
4426 cdb->byte2 |= SMS10_LLBAA;
4429 /* Record what CDB size the above function really set. */
4430 *cdb_len = ccb->csio.cdb_len;
4432 if (arglist & CAM_ARG_ERR_RECOVER)
4433 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
4435 /* Disable freezing the device queue */
4436 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
4438 if (cam_send_ccb(device, ccb) < 0)
4439 err(1, "error sending mode sense command");
4441 /* In case of ILLEGEL REQUEST try to fall back to 6-byte command. */
4442 if (*cdb_len != 6 &&
4443 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID ||
4444 (scsi_extract_sense_ccb(ccb, &error_code, &sense_key, &asc, &ascq)
4445 && sense_key == SSD_KEY_ILLEGAL_REQUEST))) {
4450 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4451 if (arglist & CAM_ARG_VERBOSE) {
4452 cam_error_print(device, ccb, CAM_ESF_ALL,
4453 CAM_EPF_ALL, stderr);
4456 cam_close_device(device);
4457 errx(1, "mode sense command returned error");
4464 mode_select(struct cam_device *device, int cdb_len, int save_pages,
4465 int task_attr, int retry_count, int timeout, u_int8_t *data, int datalen)
4470 ccb = cam_getccb(device);
4473 errx(1, "mode_select: couldn't allocate CCB");
4475 scsi_mode_select_len(&ccb->csio,
4476 /* retries */ retry_count,
4478 /* tag_action */ task_attr,
4479 /* scsi_page_fmt */ 1,
4480 /* save_pages */ save_pages,
4481 /* param_buf */ data,
4482 /* param_len */ datalen,
4483 /* minimum_cmd_size */ cdb_len,
4484 /* sense_len */ SSD_FULL_SIZE,
4485 /* timeout */ timeout ? timeout : 5000);
4487 if (arglist & CAM_ARG_ERR_RECOVER)
4488 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
4490 /* Disable freezing the device queue */
4491 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
4493 if (((retval = cam_send_ccb(device, ccb)) < 0)
4494 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
4495 if (arglist & CAM_ARG_VERBOSE) {
4496 cam_error_print(device, ccb, CAM_ESF_ALL,
4497 CAM_EPF_ALL, stderr);
4500 cam_close_device(device);
4503 err(1, "error sending mode select command");
4505 errx(1, "error sending mode select command");
4513 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt,
4514 int task_attr, int retry_count, int timeout)
4517 int c, page = -1, subpage = 0, pc = 0, llbaa = 0;
4518 int binary = 0, cdb_len = 10, dbd = 0, desc = 0, edit = 0, list = 0;
4520 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4538 str_subpage = optarg;
4539 strsep(&str_subpage, ",");
4540 page = strtol(optarg, NULL, 0);
4542 subpage = strtol(str_subpage, NULL, 0);
4543 if (page < 0 || page > 0x3f)
4544 errx(1, "invalid mode page %d", page);
4545 if (subpage < 0 || subpage > 0xff)
4546 errx(1, "invalid mode subpage %d", subpage);
4555 pc = strtol(optarg, NULL, 0);
4556 if ((pc < 0) || (pc > 3))
4557 errx(1, "invalid page control field %d", pc);
4564 if (desc && page == -1)
4565 page = SMS_ALL_PAGES_PAGE;
4567 if (page == -1 && list == 0)
4568 errx(1, "you must specify a mode page!");
4571 errx(1, "-d and -D are incompatible!");
4573 if (llbaa && cdb_len != 10)
4574 errx(1, "LLBAA bit is not present in MODE SENSE(6)!");
4577 mode_list(device, cdb_len, dbd, pc, list > 1, task_attr,
4578 retry_count, timeout);
4580 mode_edit(device, cdb_len, desc, dbd, llbaa, pc, page, subpage,
4581 edit, binary, task_attr, retry_count, timeout);
4586 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
4587 int task_attr, int retry_count, int timeout)
4590 u_int32_t flags = CAM_DIR_NONE;
4591 u_int8_t *data_ptr = NULL;
4593 u_int8_t atacmd[12];
4594 struct get_hook hook;
4595 int c, data_bytes = 0, valid_bytes;
4601 char *datastr = NULL, *tstr, *resstr = NULL;
4603 int fd_data = 0, fd_res = 0;
4606 ccb = cam_getccb(device);
4609 warnx("scsicmd: error allocating ccb");
4613 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4617 while (isspace(*tstr) && (*tstr != '\0'))
4619 hook.argc = argc - optind;
4620 hook.argv = argv + optind;
4622 atacmd_len = buff_encode_visit(atacmd, sizeof(atacmd), tstr,
4625 * Increment optind by the number of arguments the
4626 * encoding routine processed. After each call to
4627 * getopt(3), optind points to the argument that
4628 * getopt should process _next_. In this case,
4629 * that means it points to the first command string
4630 * argument, if there is one. Once we increment
4631 * this, it should point to either the next command
4632 * line argument, or it should be past the end of
4639 while (isspace(*tstr) && (*tstr != '\0'))
4641 hook.argc = argc - optind;
4642 hook.argv = argv + optind;
4644 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr,
4647 * Increment optind by the number of arguments the
4648 * encoding routine processed. After each call to
4649 * getopt(3), optind points to the argument that
4650 * getopt should process _next_. In this case,
4651 * that means it points to the first command string
4652 * argument, if there is one. Once we increment
4653 * this, it should point to either the next command
4654 * line argument, or it should be past the end of
4666 if (arglist & CAM_ARG_CMD_OUT) {
4667 warnx("command must either be "
4668 "read or write, not both");
4670 goto scsicmd_bailout;
4672 arglist |= CAM_ARG_CMD_IN;
4674 data_bytes = strtol(optarg, NULL, 0);
4675 if (data_bytes <= 0) {
4676 warnx("invalid number of input bytes %d",
4679 goto scsicmd_bailout;
4681 hook.argc = argc - optind;
4682 hook.argv = argv + optind;
4685 datastr = cget(&hook, NULL);
4687 * If the user supplied "-" instead of a format, he
4688 * wants the data to be written to stdout.
4690 if ((datastr != NULL)
4691 && (datastr[0] == '-'))
4694 data_ptr = (u_int8_t *)malloc(data_bytes);
4695 if (data_ptr == NULL) {
4696 warnx("can't malloc memory for data_ptr");
4698 goto scsicmd_bailout;
4702 if (arglist & CAM_ARG_CMD_IN) {
4703 warnx("command must either be "
4704 "read or write, not both");
4706 goto scsicmd_bailout;
4708 arglist |= CAM_ARG_CMD_OUT;
4709 flags = CAM_DIR_OUT;
4710 data_bytes = strtol(optarg, NULL, 0);
4711 if (data_bytes <= 0) {
4712 warnx("invalid number of output bytes %d",
4715 goto scsicmd_bailout;
4717 hook.argc = argc - optind;
4718 hook.argv = argv + optind;
4720 datastr = cget(&hook, NULL);
4721 data_ptr = (u_int8_t *)malloc(data_bytes);
4722 if (data_ptr == NULL) {
4723 warnx("can't malloc memory for data_ptr");
4725 goto scsicmd_bailout;
4727 bzero(data_ptr, data_bytes);
4729 * If the user supplied "-" instead of a format, he
4730 * wants the data to be read from stdin.
4732 if ((datastr != NULL)
4733 && (datastr[0] == '-'))
4736 buff_encode_visit(data_ptr, data_bytes, datastr,
4742 hook.argc = argc - optind;
4743 hook.argv = argv + optind;
4745 resstr = cget(&hook, NULL);
4746 if ((resstr != NULL) && (resstr[0] == '-'))
4756 * If fd_data is set, and we're writing to the device, we need to
4757 * read the data the user wants written from stdin.
4759 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) {
4761 int amt_to_read = data_bytes;
4762 u_int8_t *buf_ptr = data_ptr;
4764 for (amt_read = 0; amt_to_read > 0;
4765 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
4766 if (amt_read == -1) {
4767 warn("error reading data from stdin");
4769 goto scsicmd_bailout;
4771 amt_to_read -= amt_read;
4772 buf_ptr += amt_read;
4776 if (arglist & CAM_ARG_ERR_RECOVER)
4777 flags |= CAM_PASS_ERR_RECOVER;
4779 /* Disable freezing the device queue */
4780 flags |= CAM_DEV_QFRZDIS;
4784 * This is taken from the SCSI-3 draft spec.
4785 * (T10/1157D revision 0.3)
4786 * The top 3 bits of an opcode are the group code.
4787 * The next 5 bits are the command code.
4788 * Group 0: six byte commands
4789 * Group 1: ten byte commands
4790 * Group 2: ten byte commands
4792 * Group 4: sixteen byte commands
4793 * Group 5: twelve byte commands
4794 * Group 6: vendor specific
4795 * Group 7: vendor specific
4797 switch((cdb[0] >> 5) & 0x7) {
4808 /* computed by buff_encode_visit */
4819 * We should probably use csio_build_visit or something like that
4820 * here, but it's easier to encode arguments as you go. The
4821 * alternative would be skipping the CDB argument and then encoding
4822 * it here, since we've got the data buffer argument by now.
4824 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len);
4826 cam_fill_csio(&ccb->csio,
4827 /*retries*/ retry_count,
4830 /*tag_action*/ task_attr,
4831 /*data_ptr*/ data_ptr,
4832 /*dxfer_len*/ data_bytes,
4833 /*sense_len*/ SSD_FULL_SIZE,
4834 /*cdb_len*/ cdb_len,
4835 /*timeout*/ timeout ? timeout : 5000);
4838 bcopy(atacmd, &ccb->ataio.cmd.command, atacmd_len);
4840 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
4842 ccb->ataio.cmd.flags |= CAM_ATAIO_DMA;
4844 ccb->ataio.cmd.flags |= CAM_ATAIO_FPDMA;
4846 cam_fill_ataio(&ccb->ataio,
4847 /*retries*/ retry_count,
4851 /*data_ptr*/ data_ptr,
4852 /*dxfer_len*/ data_bytes,
4853 /*timeout*/ timeout ? timeout : 5000);
4856 if (((retval = cam_send_ccb(device, ccb)) < 0)
4857 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
4858 const char warnstr[] = "error sending command";
4865 if (arglist & CAM_ARG_VERBOSE) {
4866 cam_error_print(device, ccb, CAM_ESF_ALL,
4867 CAM_EPF_ALL, stderr);
4871 goto scsicmd_bailout;
4874 if (atacmd_len && need_res) {
4876 buff_decode_visit(&ccb->ataio.res.status, 11, resstr,
4878 fprintf(stdout, "\n");
4881 "%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
4882 ccb->ataio.res.status,
4883 ccb->ataio.res.error,
4884 ccb->ataio.res.lba_low,
4885 ccb->ataio.res.lba_mid,
4886 ccb->ataio.res.lba_high,
4887 ccb->ataio.res.device,
4888 ccb->ataio.res.lba_low_exp,
4889 ccb->ataio.res.lba_mid_exp,
4890 ccb->ataio.res.lba_high_exp,
4891 ccb->ataio.res.sector_count,
4892 ccb->ataio.res.sector_count_exp);
4898 valid_bytes = ccb->csio.dxfer_len - ccb->csio.resid;
4900 valid_bytes = ccb->ataio.dxfer_len - ccb->ataio.resid;
4901 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
4902 && (arglist & CAM_ARG_CMD_IN)
4903 && (valid_bytes > 0)) {
4905 buff_decode_visit(data_ptr, valid_bytes, datastr,
4907 fprintf(stdout, "\n");
4909 ssize_t amt_written;
4910 int amt_to_write = valid_bytes;
4911 u_int8_t *buf_ptr = data_ptr;
4913 for (amt_written = 0; (amt_to_write > 0) &&
4914 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){
4915 amt_to_write -= amt_written;
4916 buf_ptr += amt_written;
4918 if (amt_written == -1) {
4919 warn("error writing data to stdout");
4921 goto scsicmd_bailout;
4922 } else if ((amt_written == 0)
4923 && (amt_to_write > 0)) {
4924 warnx("only wrote %u bytes out of %u",
4925 valid_bytes - amt_to_write, valid_bytes);
4932 if ((data_bytes > 0) && (data_ptr != NULL))
4941 camdebug(int argc, char **argv, char *combinedopt)
4944 path_id_t bus = CAM_BUS_WILDCARD;
4945 target_id_t target = CAM_TARGET_WILDCARD;
4946 lun_id_t lun = CAM_LUN_WILDCARD;
4951 bzero(&ccb, sizeof(union ccb));
4953 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4956 arglist |= CAM_ARG_DEBUG_INFO;
4957 ccb.cdbg.flags |= CAM_DEBUG_INFO;
4960 arglist |= CAM_ARG_DEBUG_PERIPH;
4961 ccb.cdbg.flags |= CAM_DEBUG_PERIPH;
4964 arglist |= CAM_ARG_DEBUG_SUBTRACE;
4965 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE;
4968 arglist |= CAM_ARG_DEBUG_TRACE;
4969 ccb.cdbg.flags |= CAM_DEBUG_TRACE;
4972 arglist |= CAM_ARG_DEBUG_XPT;
4973 ccb.cdbg.flags |= CAM_DEBUG_XPT;
4976 arglist |= CAM_ARG_DEBUG_CDB;
4977 ccb.cdbg.flags |= CAM_DEBUG_CDB;
4980 arglist |= CAM_ARG_DEBUG_PROBE;
4981 ccb.cdbg.flags |= CAM_DEBUG_PROBE;
4992 warnx("you must specify \"off\", \"all\" or a bus,");
4993 warnx("bus:target, bus:target:lun or periph");
4998 while (isspace(*tstr) && (*tstr != '\0'))
5001 if (strncmp(tstr, "off", 3) == 0) {
5002 ccb.cdbg.flags = CAM_DEBUG_NONE;
5003 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH|
5004 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE|
5005 CAM_ARG_DEBUG_XPT|CAM_ARG_DEBUG_PROBE);
5007 rv = parse_btl(tstr, &bus, &target, &lun, &arglist);
5009 warnx("you must specify \"all\", \"off\", or a bus,");
5010 warnx("bus:target, bus:target:lun or periph to debug");
5015 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
5016 warnx("error opening transport layer device %s", XPT_DEVICE);
5017 warn("%s", XPT_DEVICE);
5021 ccb.ccb_h.func_code = XPT_DEBUG;
5022 ccb.ccb_h.path_id = bus;
5023 ccb.ccb_h.target_id = target;
5024 ccb.ccb_h.target_lun = lun;
5026 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
5027 warn("CAMIOCOMMAND ioctl failed");
5030 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==
5031 CAM_FUNC_NOTAVAIL) {
5032 warnx("CAM debugging not available");
5033 warnx("you need to put options CAMDEBUG in"
5034 " your kernel config file!");
5036 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) !=
5038 warnx("XPT_DEBUG CCB failed with status %#x",
5042 if (ccb.cdbg.flags == CAM_DEBUG_NONE) {
5044 "Debugging turned off\n");
5047 "Debugging enabled for "
5049 bus, target, (uintmax_t)lun);
5059 tagcontrol(struct cam_device *device, int argc, char **argv,
5069 ccb = cam_getccb(device);
5072 warnx("tagcontrol: error allocating ccb");
5076 while ((c = getopt(argc, argv, combinedopt)) != -1) {
5079 numtags = strtol(optarg, NULL, 0);
5081 warnx("tag count %d is < 0", numtags);
5083 goto tagcontrol_bailout;
5094 cam_path_string(device, pathstr, sizeof(pathstr));
5097 ccb->ccb_h.func_code = XPT_REL_SIMQ;
5098 ccb->ccb_h.flags = CAM_DEV_QFREEZE;
5099 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS;
5100 ccb->crs.openings = numtags;
5103 if (cam_send_ccb(device, ccb) < 0) {
5104 warn("error sending XPT_REL_SIMQ CCB");
5106 goto tagcontrol_bailout;
5109 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5110 warnx("XPT_REL_SIMQ CCB failed");
5111 cam_error_print(device, ccb, CAM_ESF_ALL,
5112 CAM_EPF_ALL, stderr);
5114 goto tagcontrol_bailout;
5119 fprintf(stdout, "%stagged openings now %d\n",
5120 pathstr, ccb->crs.openings);
5123 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cgds);
5125 ccb->ccb_h.func_code = XPT_GDEV_STATS;
5127 if (cam_send_ccb(device, ccb) < 0) {
5128 warn("error sending XPT_GDEV_STATS CCB");
5130 goto tagcontrol_bailout;
5133 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5134 warnx("XPT_GDEV_STATS CCB failed");
5135 cam_error_print(device, ccb, CAM_ESF_ALL,
5136 CAM_EPF_ALL, stderr);
5138 goto tagcontrol_bailout;
5141 if (arglist & CAM_ARG_VERBOSE) {
5142 fprintf(stdout, "%s", pathstr);
5143 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings);
5144 fprintf(stdout, "%s", pathstr);
5145 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active);
5146 fprintf(stdout, "%s", pathstr);
5147 fprintf(stdout, "allocated %d\n", ccb->cgds.allocated);
5148 fprintf(stdout, "%s", pathstr);
5149 fprintf(stdout, "queued %d\n", ccb->cgds.queued);
5150 fprintf(stdout, "%s", pathstr);
5151 fprintf(stdout, "held %d\n", ccb->cgds.held);
5152 fprintf(stdout, "%s", pathstr);
5153 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags);
5154 fprintf(stdout, "%s", pathstr);
5155 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags);
5158 fprintf(stdout, "%s", pathstr);
5159 fprintf(stdout, "device openings: ");
5161 fprintf(stdout, "%d\n", ccb->cgds.dev_openings +
5162 ccb->cgds.dev_active);
5172 cts_print(struct cam_device *device, struct ccb_trans_settings *cts)
5176 cam_path_string(device, pathstr, sizeof(pathstr));
5178 if (cts->transport == XPORT_SPI) {
5179 struct ccb_trans_settings_spi *spi =
5180 &cts->xport_specific.spi;
5182 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) {
5184 fprintf(stdout, "%ssync parameter: %d\n", pathstr,
5187 if (spi->sync_offset != 0) {
5190 freq = scsi_calc_syncsrate(spi->sync_period);
5191 fprintf(stdout, "%sfrequency: %d.%03dMHz\n",
5192 pathstr, freq / 1000, freq % 1000);
5196 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
5197 fprintf(stdout, "%soffset: %d\n", pathstr,
5201 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) {
5202 fprintf(stdout, "%sbus width: %d bits\n", pathstr,
5203 (0x01 << spi->bus_width) * 8);
5206 if (spi->valid & CTS_SPI_VALID_DISC) {
5207 fprintf(stdout, "%sdisconnection is %s\n", pathstr,
5208 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ?
5209 "enabled" : "disabled");
5212 if (cts->transport == XPORT_FC) {
5213 struct ccb_trans_settings_fc *fc =
5214 &cts->xport_specific.fc;
5216 if (fc->valid & CTS_FC_VALID_WWNN)
5217 fprintf(stdout, "%sWWNN: 0x%llx\n", pathstr,
5218 (long long) fc->wwnn);
5219 if (fc->valid & CTS_FC_VALID_WWPN)
5220 fprintf(stdout, "%sWWPN: 0x%llx\n", pathstr,
5221 (long long) fc->wwpn);
5222 if (fc->valid & CTS_FC_VALID_PORT)
5223 fprintf(stdout, "%sPortID: 0x%x\n", pathstr, fc->port);
5224 if (fc->valid & CTS_FC_VALID_SPEED)
5225 fprintf(stdout, "%stransfer speed: %d.%03dMB/s\n",
5226 pathstr, fc->bitrate / 1000, fc->bitrate % 1000);
5228 if (cts->transport == XPORT_SAS) {
5229 struct ccb_trans_settings_sas *sas =
5230 &cts->xport_specific.sas;
5232 if (sas->valid & CTS_SAS_VALID_SPEED)
5233 fprintf(stdout, "%stransfer speed: %d.%03dMB/s\n",
5234 pathstr, sas->bitrate / 1000, sas->bitrate % 1000);
5236 if (cts->transport == XPORT_ATA) {
5237 struct ccb_trans_settings_pata *pata =
5238 &cts->xport_specific.ata;
5240 if ((pata->valid & CTS_ATA_VALID_MODE) != 0) {
5241 fprintf(stdout, "%sATA mode: %s\n", pathstr,
5242 ata_mode2string(pata->mode));
5244 if ((pata->valid & CTS_ATA_VALID_ATAPI) != 0) {
5245 fprintf(stdout, "%sATAPI packet length: %d\n", pathstr,
5248 if ((pata->valid & CTS_ATA_VALID_BYTECOUNT) != 0) {
5249 fprintf(stdout, "%sPIO transaction length: %d\n",
5250 pathstr, pata->bytecount);
5253 if (cts->transport == XPORT_SATA) {
5254 struct ccb_trans_settings_sata *sata =
5255 &cts->xport_specific.sata;
5257 if ((sata->valid & CTS_SATA_VALID_REVISION) != 0) {
5258 fprintf(stdout, "%sSATA revision: %d.x\n", pathstr,
5261 if ((sata->valid & CTS_SATA_VALID_MODE) != 0) {
5262 fprintf(stdout, "%sATA mode: %s\n", pathstr,
5263 ata_mode2string(sata->mode));
5265 if ((sata->valid & CTS_SATA_VALID_ATAPI) != 0) {
5266 fprintf(stdout, "%sATAPI packet length: %d\n", pathstr,
5269 if ((sata->valid & CTS_SATA_VALID_BYTECOUNT) != 0) {
5270 fprintf(stdout, "%sPIO transaction length: %d\n",
5271 pathstr, sata->bytecount);
5273 if ((sata->valid & CTS_SATA_VALID_PM) != 0) {
5274 fprintf(stdout, "%sPMP presence: %d\n", pathstr,
5277 if ((sata->valid & CTS_SATA_VALID_TAGS) != 0) {
5278 fprintf(stdout, "%sNumber of tags: %d\n", pathstr,
5281 if ((sata->valid & CTS_SATA_VALID_CAPS) != 0) {
5282 fprintf(stdout, "%sSATA capabilities: %08x\n", pathstr,
5286 if (cts->protocol == PROTO_ATA) {
5287 struct ccb_trans_settings_ata *ata=
5288 &cts->proto_specific.ata;
5290 if (ata->valid & CTS_ATA_VALID_TQ) {
5291 fprintf(stdout, "%stagged queueing: %s\n", pathstr,
5292 (ata->flags & CTS_ATA_FLAGS_TAG_ENB) ?
5293 "enabled" : "disabled");
5296 if (cts->protocol == PROTO_SCSI) {
5297 struct ccb_trans_settings_scsi *scsi=
5298 &cts->proto_specific.scsi;
5300 if (scsi->valid & CTS_SCSI_VALID_TQ) {
5301 fprintf(stdout, "%stagged queueing: %s\n", pathstr,
5302 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ?
5303 "enabled" : "disabled");
5307 if (cts->protocol == PROTO_NVME) {
5308 struct ccb_trans_settings_nvme *nvmex =
5309 &cts->xport_specific.nvme;
5311 if (nvmex->valid & CTS_NVME_VALID_SPEC) {
5312 fprintf(stdout, "%sNVMe Spec: %d.%d\n", pathstr,
5313 NVME_MAJOR(nvmex->spec),
5314 NVME_MINOR(nvmex->spec));
5316 if (nvmex->valid & CTS_NVME_VALID_LINK) {
5317 fprintf(stdout, "%sPCIe lanes: %d (%d max)\n", pathstr,
5318 nvmex->lanes, nvmex->max_lanes);
5319 fprintf(stdout, "%sPCIe Generation: %d (%d max)\n", pathstr,
5320 nvmex->speed, nvmex->max_speed);
5327 * Get a path inquiry CCB for the specified device.
5330 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi)
5335 ccb = cam_getccb(device);
5337 warnx("get_cpi: couldn't allocate CCB");
5340 ccb->ccb_h.func_code = XPT_PATH_INQ;
5341 if (cam_send_ccb(device, ccb) < 0) {
5342 warn("get_cpi: error sending Path Inquiry CCB");
5344 goto get_cpi_bailout;
5346 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5347 if (arglist & CAM_ARG_VERBOSE)
5348 cam_error_print(device, ccb, CAM_ESF_ALL,
5349 CAM_EPF_ALL, stderr);
5351 goto get_cpi_bailout;
5353 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq));
5361 * Get a get device CCB for the specified device.
5364 get_cgd(struct cam_device *device, struct ccb_getdev *cgd)
5369 ccb = cam_getccb(device);
5371 warnx("get_cgd: couldn't allocate CCB");
5374 ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5375 if (cam_send_ccb(device, ccb) < 0) {
5376 warn("get_cgd: error sending Get type information CCB");
5378 goto get_cgd_bailout;
5380 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5381 if (arglist & CAM_ARG_VERBOSE)
5382 cam_error_print(device, ccb, CAM_ESF_ALL,
5383 CAM_EPF_ALL, stderr);
5385 goto get_cgd_bailout;
5387 bcopy(&ccb->cgd, cgd, sizeof(struct ccb_getdev));
5395 * Returns 1 if the device has the VPD page, 0 if it does not, and -1 on an
5399 dev_has_vpd_page(struct cam_device *dev, uint8_t page_id, int retry_count,
5400 int timeout, int verbosemode)
5402 union ccb *ccb = NULL;
5403 struct scsi_vpd_supported_page_list sup_pages;
5407 ccb = cam_getccb(dev);
5409 warn("Unable to allocate CCB");
5414 bzero(&sup_pages, sizeof(sup_pages));
5416 scsi_inquiry(&ccb->csio,
5417 /*retries*/ retry_count,
5419 /* tag_action */ MSG_SIMPLE_Q_TAG,
5420 /* inq_buf */ (u_int8_t *)&sup_pages,
5421 /* inq_len */ sizeof(sup_pages),
5423 /* page_code */ SVPD_SUPPORTED_PAGE_LIST,
5424 /* sense_len */ SSD_FULL_SIZE,
5425 /* timeout */ timeout ? timeout : 5000);
5427 /* Disable freezing the device queue */
5428 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
5430 if (retry_count != 0)
5431 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
5433 if (cam_send_ccb(dev, ccb) < 0) {
5440 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5441 if (verbosemode != 0)
5442 cam_error_print(dev, ccb, CAM_ESF_ALL,
5443 CAM_EPF_ALL, stderr);
5448 for (i = 0; i < sup_pages.length; i++) {
5449 if (sup_pages.list[i] == page_id) {
5462 * devtype is filled in with the type of device.
5463 * Returns 0 for success, non-zero for failure.
5466 get_device_type(struct cam_device *dev, int retry_count, int timeout,
5467 int verbosemode, camcontrol_devtype *devtype)
5469 struct ccb_getdev cgd;
5472 retval = get_cgd(dev, &cgd);
5476 switch (cgd.protocol) {
5482 *devtype = CC_DT_ATA;
5484 break; /*NOTREACHED*/
5486 *devtype = CC_DT_NVME;
5488 break; /*NOTREACHED*/
5490 *devtype = CC_DT_MMCSD;
5492 break; /*NOTREACHED*/
5494 *devtype = CC_DT_UNKNOWN;
5496 break; /*NOTREACHED*/
5499 if (retry_count == -1) {
5501 * For a retry count of -1, used only the cached data to avoid
5502 * I/O to the drive. Sending the identify command to the drive
5503 * can cause issues for SATL attachaed drives since identify is
5504 * not an NCQ command. We check for the strings that windows
5505 * displays since those will not be NULs (they are supposed
5506 * to be space padded). We could check other bits, but anything
5507 * non-zero implies SATL.
5509 if (cgd.ident_data.serial[0] != 0 ||
5510 cgd.ident_data.revision[0] != 0 ||
5511 cgd.ident_data.model[0] != 0)
5512 *devtype = CC_DT_SATL;
5514 *devtype = CC_DT_SCSI;
5517 * Check for the ATA Information VPD page (0x89). If this is an
5518 * ATA device behind a SCSI to ATA translation layer (SATL),
5519 * this VPD page should be present.
5521 * If that VPD page isn't present, or we get an error back from
5522 * the INQUIRY command, we'll just treat it as a normal SCSI
5525 retval = dev_has_vpd_page(dev, SVPD_ATA_INFORMATION, retry_count,
5526 timeout, verbosemode);
5528 *devtype = CC_DT_SATL;
5530 *devtype = CC_DT_SCSI;
5539 build_ata_cmd(union ccb *ccb, uint32_t retry_count, uint32_t flags,
5540 uint8_t tag_action, uint8_t protocol, uint8_t ata_flags, uint16_t features,
5541 uint16_t sector_count, uint64_t lba, uint8_t command, uint32_t auxiliary,
5542 uint8_t *data_ptr, uint32_t dxfer_len, uint8_t *cdb_storage,
5543 size_t cdb_storage_len, uint8_t sense_len, uint32_t timeout,
5544 int is48bit, camcontrol_devtype devtype)
5548 if (devtype == CC_DT_ATA) {
5549 cam_fill_ataio(&ccb->ataio,
5550 /*retries*/ retry_count,
5553 /*tag_action*/ tag_action,
5554 /*data_ptr*/ data_ptr,
5555 /*dxfer_len*/ dxfer_len,
5556 /*timeout*/ timeout);
5557 if (is48bit || lba > ATA_MAX_28BIT_LBA)
5558 ata_48bit_cmd(&ccb->ataio, command, features, lba,
5561 ata_28bit_cmd(&ccb->ataio, command, features, lba,
5564 if (auxiliary != 0) {
5565 ccb->ataio.ata_flags |= ATA_FLAG_AUX;
5566 ccb->ataio.aux = auxiliary;
5569 if (ata_flags & AP_FLAG_CHK_COND)
5570 ccb->ataio.cmd.flags |= CAM_ATAIO_NEEDRESULT;
5572 if ((protocol & AP_PROTO_MASK) == AP_PROTO_DMA)
5573 ccb->ataio.cmd.flags |= CAM_ATAIO_DMA;
5574 else if ((protocol & AP_PROTO_MASK) == AP_PROTO_FPDMA)
5575 ccb->ataio.cmd.flags |= CAM_ATAIO_FPDMA;
5577 if (is48bit || lba > ATA_MAX_28BIT_LBA)
5578 protocol |= AP_EXTEND;
5580 retval = scsi_ata_pass(&ccb->csio,
5581 /*retries*/ retry_count,
5584 /*tag_action*/ tag_action,
5585 /*protocol*/ protocol,
5586 /*ata_flags*/ ata_flags,
5587 /*features*/ features,
5588 /*sector_count*/ sector_count,
5590 /*command*/ command,
5593 /*auxiliary*/ auxiliary,
5595 /*data_ptr*/ data_ptr,
5596 /*dxfer_len*/ dxfer_len,
5597 /*cdb_storage*/ cdb_storage,
5598 /*cdb_storage_len*/ cdb_storage_len,
5599 /*minimum_cmd_size*/ 0,
5600 /*sense_len*/ sense_len,
5601 /*timeout*/ timeout);
5608 * Returns: 0 -- success, 1 -- error, 2 -- lba truncated,
5609 * 4 -- count truncated, 6 -- lba and count truncated.
5612 get_ata_status(struct cam_device *dev, union ccb *ccb, uint8_t *error,
5613 uint16_t *count, uint64_t *lba, uint8_t *device, uint8_t *status)
5617 switch (ccb->ccb_h.func_code) {
5620 int error_code = 0, sense_key = 0, asc = 0, ascq = 0;
5624 * In this case, we have SCSI ATA PASS-THROUGH command, 12
5625 * or 16 byte, and need to see what
5627 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
5628 opcode = ccb->csio.cdb_io.cdb_ptr[0];
5630 opcode = ccb->csio.cdb_io.cdb_bytes[0];
5631 if ((opcode != ATA_PASS_12)
5632 && (opcode != ATA_PASS_16)) {
5633 warnx("%s: unsupported opcode %02x", __func__, opcode);
5637 retval = scsi_extract_sense_ccb(ccb, &error_code, &sense_key,
5639 /* Note: the _ccb() variant returns 0 for an error */
5643 sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
5644 switch (error_code) {
5645 case SSD_DESC_CURRENT_ERROR:
5646 case SSD_DESC_DEFERRED_ERROR: {
5647 struct scsi_sense_data_desc *sense;
5648 struct scsi_sense_ata_ret_desc *desc;
5651 sense = (struct scsi_sense_data_desc *)
5652 &ccb->csio.sense_data;
5654 desc_ptr = scsi_find_desc(sense, sense_len,
5656 if (desc_ptr == NULL) {
5657 cam_error_print(dev, ccb, CAM_ESF_ALL,
5658 CAM_EPF_ALL, stderr);
5661 desc = (struct scsi_sense_ata_ret_desc *)desc_ptr;
5663 *error = desc->error;
5664 *count = (desc->count_15_8 << 8) |
5666 *lba = ((uint64_t)desc->lba_47_40 << 40) |
5667 ((uint64_t)desc->lba_39_32 << 32) |
5668 ((uint64_t)desc->lba_31_24 << 24) |
5669 (desc->lba_23_16 << 16) |
5670 (desc->lba_15_8 << 8) |
5672 *device = desc->device;
5673 *status = desc->status;
5676 * If the extend bit isn't set, the result is for a
5677 * 12-byte ATA PASS-THROUGH command or a 16 or 32 byte
5678 * command without the extend bit set. This means
5679 * that the device is supposed to return 28-bit
5680 * status. The count field is only 8 bits, and the
5681 * LBA field is only 8 bits.
5683 if ((desc->flags & SSD_DESC_ATA_FLAG_EXTEND) == 0){
5689 case SSD_CURRENT_ERROR:
5690 case SSD_DEFERRED_ERROR: {
5694 * In my understanding of SAT-5 specification, saying:
5695 * "without interpreting the contents of the STATUS",
5696 * this should not happen if CK_COND was set, but it
5697 * does at least for some devices, so try to revert.
5699 if ((sense_key == SSD_KEY_ABORTED_COMMAND) &&
5700 (asc == 0) && (ascq == 0)) {
5701 *status = ATA_STATUS_ERROR;
5702 *error = ATA_ERROR_ABORT;
5709 if ((sense_key != SSD_KEY_RECOVERED_ERROR) ||
5710 (asc != 0x00) || (ascq != 0x1d))
5714 scsi_get_sense_info(&ccb->csio.sense_data, sense_len,
5715 SSD_DESC_INFO, &val, NULL);
5716 *error = (val >> 24) & 0xff;
5717 *status = (val >> 16) & 0xff;
5718 *device = (val >> 8) & 0xff;
5719 *count = val & 0xff;
5722 scsi_get_sense_info(&ccb->csio.sense_data, sense_len,
5723 SSD_DESC_COMMAND, &val, NULL);
5724 *lba = ((val >> 16) & 0xff) | (val & 0xff00) |
5725 ((val & 0xff) << 16);
5727 /* Report UPPER NONZERO bits as errors 2, 4 and 6. */
5728 return ((val >> 28) & 0x06);
5737 struct ata_res *res;
5739 /* Only some statuses return ATA result register set. */
5740 if (cam_ccb_status(ccb) != CAM_REQ_CMP &&
5741 cam_ccb_status(ccb) != CAM_ATA_STATUS_ERROR)
5744 res = &ccb->ataio.res;
5745 *error = res->error;
5746 *status = res->status;
5747 *device = res->device;
5748 *count = res->sector_count;
5749 *lba = (res->lba_high << 16) |
5750 (res->lba_mid << 8) |
5752 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
5753 *count |= (res->sector_count_exp << 8);
5754 *lba |= ((uint64_t)res->lba_low_exp << 24) |
5755 ((uint64_t)res->lba_mid_exp << 32) |
5756 ((uint64_t)res->lba_high_exp << 40);
5758 *lba |= (res->device & 0xf) << 24;
5769 cpi_print(struct ccb_pathinq *cpi)
5771 char adapter_str[1024];
5774 snprintf(adapter_str, sizeof(adapter_str),
5775 "%s%d:", cpi->dev_name, cpi->unit_number);
5777 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str,
5780 for (i = 1; i < UINT8_MAX; i = i << 1) {
5783 if ((i & cpi->hba_inquiry) == 0)
5786 fprintf(stdout, "%s supports ", adapter_str);
5790 str = "MDP message";
5793 str = "32 bit wide SCSI";
5796 str = "16 bit wide SCSI";
5799 str = "SDTR message";
5802 str = "linked CDBs";
5805 str = "tag queue messages";
5808 str = "soft reset alternative";
5811 str = "SATA Port Multiplier";
5814 str = "unknown PI bit set";
5817 fprintf(stdout, "%s\n", str);
5820 for (i = 1; i < UINT32_MAX; i = i << 1) {
5823 if ((i & cpi->hba_misc) == 0)
5826 fprintf(stdout, "%s ", adapter_str);
5830 str = "can understand ata_ext requests";
5833 str = "64bit extended LUNs supported";
5836 str = "bus scans from high ID to low ID";
5839 str = "removable devices not included in scan";
5841 case PIM_NOINITIATOR:
5842 str = "initiator role not supported";
5844 case PIM_NOBUSRESET:
5845 str = "user has disabled initial BUS RESET or"
5846 " controller is in target/mixed mode";
5849 str = "do not send 6-byte commands";
5852 str = "scan bus sequentially";
5855 str = "unmapped I/O supported";
5858 str = "does its own scanning";
5861 str = "unknown PIM bit set";
5864 fprintf(stdout, "%s\n", str);
5867 for (i = 1; i < UINT16_MAX; i = i << 1) {
5870 if ((i & cpi->target_sprt) == 0)
5873 fprintf(stdout, "%s supports ", adapter_str);
5876 str = "target mode processor mode";
5879 str = "target mode phase cog. mode";
5881 case PIT_DISCONNECT:
5882 str = "disconnects in target mode";
5885 str = "terminate I/O message in target mode";
5888 str = "group 6 commands in target mode";
5891 str = "group 7 commands in target mode";
5894 str = "unknown PIT bit set";
5898 fprintf(stdout, "%s\n", str);
5900 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str,
5902 fprintf(stdout, "%s maximum target: %d\n", adapter_str,
5904 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str,
5906 fprintf(stdout, "%s highest path ID in subsystem: %d\n",
5907 adapter_str, cpi->hpath_id);
5908 fprintf(stdout, "%s initiator ID: %d\n", adapter_str,
5910 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid);
5911 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid);
5912 fprintf(stdout, "%s HBA vendor ID: 0x%04x\n",
5913 adapter_str, cpi->hba_vendor);
5914 fprintf(stdout, "%s HBA device ID: 0x%04x\n",
5915 adapter_str, cpi->hba_device);
5916 fprintf(stdout, "%s HBA subvendor ID: 0x%04x\n",
5917 adapter_str, cpi->hba_subvendor);
5918 fprintf(stdout, "%s HBA subdevice ID: 0x%04x\n",
5919 adapter_str, cpi->hba_subdevice);
5920 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id);
5921 fprintf(stdout, "%s base transfer speed: ", adapter_str);
5922 if (cpi->base_transfer_speed > 1000)
5923 fprintf(stdout, "%d.%03dMB/sec\n",
5924 cpi->base_transfer_speed / 1000,
5925 cpi->base_transfer_speed % 1000);
5927 fprintf(stdout, "%dKB/sec\n",
5928 (cpi->base_transfer_speed % 1000) * 1000);
5929 fprintf(stdout, "%s maximum transfer size: %u bytes\n",
5930 adapter_str, cpi->maxio);
5934 get_print_cts(struct cam_device *device, int user_settings, int quiet,
5935 struct ccb_trans_settings *cts)
5941 ccb = cam_getccb(device);
5944 warnx("get_print_cts: error allocating ccb");
5948 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5950 if (user_settings == 0)
5951 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS;
5953 ccb->cts.type = CTS_TYPE_USER_SETTINGS;
5955 if (cam_send_ccb(device, ccb) < 0) {
5956 warn("error sending XPT_GET_TRAN_SETTINGS CCB");
5958 goto get_print_cts_bailout;
5961 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5962 warnx("XPT_GET_TRANS_SETTINGS CCB failed");
5963 if (arglist & CAM_ARG_VERBOSE)
5964 cam_error_print(device, ccb, CAM_ESF_ALL,
5965 CAM_EPF_ALL, stderr);
5967 goto get_print_cts_bailout;
5971 cts_print(device, &ccb->cts);
5974 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings));
5976 get_print_cts_bailout:
5984 ratecontrol(struct cam_device *device, int task_attr, int retry_count,
5985 int timeout, int argc, char **argv, char *combinedopt)
5989 int user_settings = 0;
5991 int disc_enable = -1, tag_enable = -1;
5994 double syncrate = -1;
5997 int change_settings = 0, send_tur = 0;
5998 struct ccb_pathinq cpi;
6000 ccb = cam_getccb(device);
6002 warnx("ratecontrol: error allocating ccb");
6005 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6014 if (strncasecmp(optarg, "enable", 6) == 0)
6016 else if (strncasecmp(optarg, "disable", 7) == 0)
6019 warnx("-D argument \"%s\" is unknown", optarg);
6021 goto ratecontrol_bailout;
6023 change_settings = 1;
6026 mode = ata_string2mode(optarg);
6028 warnx("unknown mode '%s'", optarg);
6030 goto ratecontrol_bailout;
6032 change_settings = 1;
6035 offset = strtol(optarg, NULL, 0);
6037 warnx("offset value %d is < 0", offset);
6039 goto ratecontrol_bailout;
6041 change_settings = 1;
6047 syncrate = atof(optarg);
6049 warnx("sync rate %f is < 0", syncrate);
6051 goto ratecontrol_bailout;
6053 change_settings = 1;
6056 if (strncasecmp(optarg, "enable", 6) == 0)
6058 else if (strncasecmp(optarg, "disable", 7) == 0)
6061 warnx("-T argument \"%s\" is unknown", optarg);
6063 goto ratecontrol_bailout;
6065 change_settings = 1;
6071 bus_width = strtol(optarg, NULL, 0);
6072 if (bus_width < 0) {
6073 warnx("bus width %d is < 0", bus_width);
6075 goto ratecontrol_bailout;
6077 change_settings = 1;
6084 * Grab path inquiry information, so we can determine whether
6085 * or not the initiator is capable of the things that the user
6088 if ((retval = get_cpi(device, &cpi)) != 0)
6089 goto ratecontrol_bailout;
6091 fprintf(stdout, "%s parameters:\n",
6092 user_settings ? "User" : "Current");
6094 retval = get_print_cts(device, user_settings, quiet, &ccb->cts);
6096 goto ratecontrol_bailout;
6098 if (arglist & CAM_ARG_VERBOSE)
6101 if (change_settings) {
6102 int didsettings = 0;
6103 struct ccb_trans_settings_spi *spi = NULL;
6104 struct ccb_trans_settings_pata *pata = NULL;
6105 struct ccb_trans_settings_sata *sata = NULL;
6106 struct ccb_trans_settings_ata *ata = NULL;
6107 struct ccb_trans_settings_scsi *scsi = NULL;
6109 if (ccb->cts.transport == XPORT_SPI)
6110 spi = &ccb->cts.xport_specific.spi;
6111 if (ccb->cts.transport == XPORT_ATA)
6112 pata = &ccb->cts.xport_specific.ata;
6113 if (ccb->cts.transport == XPORT_SATA)
6114 sata = &ccb->cts.xport_specific.sata;
6115 if (ccb->cts.protocol == PROTO_ATA)
6116 ata = &ccb->cts.proto_specific.ata;
6117 if (ccb->cts.protocol == PROTO_SCSI)
6118 scsi = &ccb->cts.proto_specific.scsi;
6119 ccb->cts.xport_specific.valid = 0;
6120 ccb->cts.proto_specific.valid = 0;
6121 if (spi && disc_enable != -1) {
6122 spi->valid |= CTS_SPI_VALID_DISC;
6123 if (disc_enable == 0)
6124 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6126 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
6129 if (tag_enable != -1) {
6130 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) {
6131 warnx("HBA does not support tagged queueing, "
6132 "so you cannot modify tag settings");
6134 goto ratecontrol_bailout;
6137 ata->valid |= CTS_SCSI_VALID_TQ;
6138 if (tag_enable == 0)
6139 ata->flags &= ~CTS_ATA_FLAGS_TAG_ENB;
6141 ata->flags |= CTS_ATA_FLAGS_TAG_ENB;
6144 scsi->valid |= CTS_SCSI_VALID_TQ;
6145 if (tag_enable == 0)
6146 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6148 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
6152 if (spi && offset != -1) {
6153 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6154 warnx("HBA is not capable of changing offset");
6156 goto ratecontrol_bailout;
6158 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
6159 spi->sync_offset = offset;
6162 if (spi && syncrate != -1) {
6163 int prelim_sync_period;
6165 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6166 warnx("HBA is not capable of changing "
6169 goto ratecontrol_bailout;
6171 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
6173 * The sync rate the user gives us is in MHz.
6174 * We need to translate it into KHz for this
6179 * Next, we calculate a "preliminary" sync period
6180 * in tenths of a nanosecond.
6183 prelim_sync_period = 0;
6185 prelim_sync_period = 10000000 / syncrate;
6187 scsi_calc_syncparam(prelim_sync_period);
6190 if (sata && syncrate != -1) {
6191 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6192 warnx("HBA is not capable of changing "
6195 goto ratecontrol_bailout;
6197 if (!user_settings) {
6198 warnx("You can modify only user rate "
6199 "settings for SATA");
6201 goto ratecontrol_bailout;
6203 sata->revision = ata_speed2revision(syncrate * 100);
6204 if (sata->revision < 0) {
6205 warnx("Invalid rate %f", syncrate);
6207 goto ratecontrol_bailout;
6209 sata->valid |= CTS_SATA_VALID_REVISION;
6212 if ((pata || sata) && mode != -1) {
6213 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) {
6214 warnx("HBA is not capable of changing "
6217 goto ratecontrol_bailout;
6219 if (!user_settings) {
6220 warnx("You can modify only user mode "
6221 "settings for ATA/SATA");
6223 goto ratecontrol_bailout;
6227 pata->valid |= CTS_ATA_VALID_MODE;
6230 sata->valid |= CTS_SATA_VALID_MODE;
6235 * The bus_width argument goes like this:
6239 * Therefore, if you shift the number of bits given on the
6240 * command line right by 4, you should get the correct
6243 if (spi && bus_width != -1) {
6245 * We might as well validate things here with a
6246 * decipherable error message, rather than what
6247 * will probably be an indecipherable error message
6248 * by the time it gets back to us.
6250 if ((bus_width == 16)
6251 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) {
6252 warnx("HBA does not support 16 bit bus width");
6254 goto ratecontrol_bailout;
6255 } else if ((bus_width == 32)
6256 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) {
6257 warnx("HBA does not support 32 bit bus width");
6259 goto ratecontrol_bailout;
6260 } else if ((bus_width != 8)
6261 && (bus_width != 16)
6262 && (bus_width != 32)) {
6263 warnx("Invalid bus width %d", bus_width);
6265 goto ratecontrol_bailout;
6267 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
6268 spi->bus_width = bus_width >> 4;
6271 if (didsettings == 0) {
6272 goto ratecontrol_bailout;
6274 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6275 if (cam_send_ccb(device, ccb) < 0) {
6276 warn("error sending XPT_SET_TRAN_SETTINGS CCB");
6278 goto ratecontrol_bailout;
6280 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
6281 warnx("XPT_SET_TRANS_SETTINGS CCB failed");
6282 if (arglist & CAM_ARG_VERBOSE) {
6283 cam_error_print(device, ccb, CAM_ESF_ALL,
6284 CAM_EPF_ALL, stderr);
6287 goto ratecontrol_bailout;
6291 retval = testunitready(device, task_attr, retry_count, timeout,
6292 (arglist & CAM_ARG_VERBOSE) ? 0 : 1);
6294 * If the TUR didn't succeed, just bail.
6298 fprintf(stderr, "Test Unit Ready failed\n");
6299 goto ratecontrol_bailout;
6302 if ((change_settings || send_tur) && !quiet &&
6303 (ccb->cts.transport == XPORT_ATA ||
6304 ccb->cts.transport == XPORT_SATA || send_tur)) {
6305 fprintf(stdout, "New parameters:\n");
6306 retval = get_print_cts(device, user_settings, 0, NULL);
6309 ratecontrol_bailout:
6315 scsiformat(struct cam_device *device, int argc, char **argv,
6316 char *combinedopt, int task_attr, int retry_count, int timeout)
6320 int ycount = 0, quiet = 0;
6321 int error = 0, retval = 0;
6322 int use_timeout = 10800 * 1000;
6324 struct format_defect_list_header fh;
6325 u_int8_t *data_ptr = NULL;
6326 u_int32_t dxfer_len = 0;
6328 int num_warnings = 0;
6331 ccb = cam_getccb(device);
6334 warnx("scsiformat: error allocating ccb");
6338 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6358 if (quiet == 0 && ycount == 0) {
6359 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
6360 "following device:\n");
6362 error = scsidoinquiry(device, argc, argv, combinedopt,
6363 task_attr, retry_count, timeout);
6366 warnx("scsiformat: error sending inquiry");
6367 goto scsiformat_bailout;
6372 if (!get_confirmation()) {
6374 goto scsiformat_bailout;
6379 use_timeout = timeout;
6382 fprintf(stdout, "Current format timeout is %d seconds\n",
6383 use_timeout / 1000);
6387 * If the user hasn't disabled questions and didn't specify a
6388 * timeout on the command line, ask them if they want the current
6392 && (timeout == 0)) {
6394 int new_timeout = 0;
6396 fprintf(stdout, "Enter new timeout in seconds or press\n"
6397 "return to keep the current timeout [%d] ",
6398 use_timeout / 1000);
6400 if (fgets(str, sizeof(str), stdin) != NULL) {
6402 new_timeout = atoi(str);
6405 if (new_timeout != 0) {
6406 use_timeout = new_timeout * 1000;
6407 fprintf(stdout, "Using new timeout value %d\n",
6408 use_timeout / 1000);
6413 * Keep this outside the if block below to silence any unused
6414 * variable warnings.
6416 bzero(&fh, sizeof(fh));
6419 * If we're in immediate mode, we've got to include the format
6422 if (immediate != 0) {
6423 fh.byte2 = FU_DLH_IMMED;
6424 data_ptr = (u_int8_t *)&fh;
6425 dxfer_len = sizeof(fh);
6426 byte2 = FU_FMT_DATA;
6427 } else if (quiet == 0) {
6428 fprintf(stdout, "Formatting...");
6432 scsi_format_unit(&ccb->csio,
6433 /* retries */ retry_count,
6435 /* tag_action */ task_attr,
6438 /* data_ptr */ data_ptr,
6439 /* dxfer_len */ dxfer_len,
6440 /* sense_len */ SSD_FULL_SIZE,
6441 /* timeout */ use_timeout);
6443 /* Disable freezing the device queue */
6444 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6446 if (arglist & CAM_ARG_ERR_RECOVER)
6447 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
6449 if (((retval = cam_send_ccb(device, ccb)) < 0)
6450 || ((immediate == 0)
6451 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) {
6452 const char errstr[] = "error sending format command";
6459 if (arglist & CAM_ARG_VERBOSE) {
6460 cam_error_print(device, ccb, CAM_ESF_ALL,
6461 CAM_EPF_ALL, stderr);
6464 goto scsiformat_bailout;
6468 * If we ran in non-immediate mode, we already checked for errors
6469 * above and printed out any necessary information. If we're in
6470 * immediate mode, we need to loop through and get status
6471 * information periodically.
6473 if (immediate == 0) {
6475 fprintf(stdout, "Format Complete\n");
6477 goto scsiformat_bailout;
6484 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6487 * There's really no need to do error recovery or
6488 * retries here, since we're just going to sit in a
6489 * loop and wait for the device to finish formatting.
6491 scsi_test_unit_ready(&ccb->csio,
6494 /* tag_action */ task_attr,
6495 /* sense_len */ SSD_FULL_SIZE,
6496 /* timeout */ 5000);
6498 /* Disable freezing the device queue */
6499 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6501 retval = cam_send_ccb(device, ccb);
6504 * If we get an error from the ioctl, bail out. SCSI
6505 * errors are expected.
6508 warn("error sending TEST UNIT READY command");
6510 goto scsiformat_bailout;
6513 status = ccb->ccb_h.status & CAM_STATUS_MASK;
6515 if ((status != CAM_REQ_CMP)
6516 && (status == CAM_SCSI_STATUS_ERROR)
6517 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
6518 struct scsi_sense_data *sense;
6519 int error_code, sense_key, asc, ascq;
6521 sense = &ccb->csio.sense_data;
6522 scsi_extract_sense_len(sense, ccb->csio.sense_len -
6523 ccb->csio.sense_resid, &error_code, &sense_key,
6524 &asc, &ascq, /*show_errors*/ 1);
6527 * According to the SCSI-2 and SCSI-3 specs, a
6528 * drive that is in the middle of a format should
6529 * return NOT READY with an ASC of "logical unit
6530 * not ready, format in progress". The sense key
6531 * specific bytes will then be a progress indicator.
6533 if ((sense_key == SSD_KEY_NOT_READY)
6534 && (asc == 0x04) && (ascq == 0x04)) {
6537 if ((scsi_get_sks(sense, ccb->csio.sense_len -
6538 ccb->csio.sense_resid, sks) == 0)
6541 u_int64_t percentage;
6543 val = scsi_2btoul(&sks[1]);
6544 percentage = 10000ull * val;
6547 "\rFormatting: %ju.%02u %% "
6549 (uintmax_t)(percentage /
6551 (unsigned)((percentage /
6555 } else if ((quiet == 0)
6556 && (++num_warnings <= 1)) {
6557 warnx("Unexpected SCSI Sense Key "
6558 "Specific value returned "
6560 scsi_sense_print(device, &ccb->csio,
6562 warnx("Unable to print status "
6563 "information, but format will "
6565 warnx("will exit when format is "
6570 warnx("Unexpected SCSI error during format");
6571 cam_error_print(device, ccb, CAM_ESF_ALL,
6572 CAM_EPF_ALL, stderr);
6574 goto scsiformat_bailout;
6577 } else if (status != CAM_REQ_CMP) {
6578 warnx("Unexpected CAM status %#x", status);
6579 if (arglist & CAM_ARG_VERBOSE)
6580 cam_error_print(device, ccb, CAM_ESF_ALL,
6581 CAM_EPF_ALL, stderr);
6583 goto scsiformat_bailout;
6586 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
6589 fprintf(stdout, "\nFormat Complete\n");
6599 sanitize_wait_ata(struct cam_device *device, union ccb *ccb, int quiet,
6600 camcontrol_devtype devtype)
6603 uint8_t error = 0, ata_device = 0, status = 0;
6609 retval = build_ata_cmd(ccb,
6611 /*flags*/ CAM_DIR_NONE,
6612 /*tag_action*/ MSG_SIMPLE_Q_TAG,
6613 /*protocol*/ AP_PROTO_NON_DATA,
6614 /*ata_flags*/ AP_FLAG_CHK_COND,
6615 /*features*/ 0x00, /* SANITIZE STATUS EXT */
6618 /*command*/ ATA_SANITIZE,
6622 /*cdb_storage*/ NULL,
6623 /*cdb_storage_len*/ 0,
6624 /*sense_len*/ SSD_FULL_SIZE,
6627 /*devtype*/ devtype);
6629 warnx("%s: build_ata_cmd() failed, likely "
6630 "programmer error", __func__);
6634 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6635 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
6636 retval = cam_send_ccb(device, ccb);
6638 warn("error sending SANITIZE STATUS EXT command");
6642 retval = get_ata_status(device, ccb, &error, &count, &lba,
6643 &ata_device, &status);
6645 warnx("Can't get SANITIZE STATUS EXT status, "
6646 "sanitize may still run.");
6649 if (status & ATA_STATUS_ERROR) {
6650 if (error & ATA_ERROR_ABORT) {
6651 switch (lba & 0xff) {
6653 warnx("Reason not reported or sanitize failed.");
6656 warnx("Sanitize command unsuccessful. ");
6659 warnx("Unsupported sanitize device command. ");
6662 warnx("Device is in sanitize frozen state. ");
6665 warnx("Sanitize antifreeze lock is enabled. ");
6669 warnx("SANITIZE STATUS EXT failed, "
6670 "sanitize may still run.");
6673 if (count & 0x4000) {
6678 "Sanitizing: %u.%02u%% (%d/%d)\r",
6679 (perc / (0x10000 * 100)),
6680 ((perc / 0x10000) % 100),
6692 sanitize_wait_scsi(struct cam_device *device, union ccb *ccb, int task_attr, int quiet)
6694 int warnings = 0, retval;
6699 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
6702 * There's really no need to do error recovery or
6703 * retries here, since we're just going to sit in a
6704 * loop and wait for the device to finish sanitizing.
6706 scsi_test_unit_ready(&ccb->csio,
6709 /* tag_action */ task_attr,
6710 /* sense_len */ SSD_FULL_SIZE,
6711 /* timeout */ 5000);
6713 /* Disable freezing the device queue */
6714 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
6716 retval = cam_send_ccb(device, ccb);
6719 * If we get an error from the ioctl, bail out. SCSI
6720 * errors are expected.
6723 warn("error sending TEST UNIT READY command");
6727 status = ccb->ccb_h.status & CAM_STATUS_MASK;
6728 if ((status == CAM_SCSI_STATUS_ERROR) &&
6729 ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) {
6730 struct scsi_sense_data *sense;
6731 int error_code, sense_key, asc, ascq;
6733 sense = &ccb->csio.sense_data;
6734 scsi_extract_sense_len(sense, ccb->csio.sense_len -
6735 ccb->csio.sense_resid, &error_code, &sense_key,
6736 &asc, &ascq, /*show_errors*/ 1);
6739 * According to the SCSI-3 spec, a drive that is in the
6740 * middle of a sanitize should return NOT READY with an
6741 * ASC of "logical unit not ready, sanitize in
6742 * progress". The sense key specific bytes will then
6743 * be a progress indicator.
6745 if ((sense_key == SSD_KEY_NOT_READY)
6746 && (asc == 0x04) && (ascq == 0x1b)) {
6749 if ((scsi_get_sks(sense, ccb->csio.sense_len -
6750 ccb->csio.sense_resid, sks) == 0)
6752 val = scsi_2btoul(&sks[1]);
6755 "Sanitizing: %u.%02u%% (%d/%d)\r",
6756 (perc / (0x10000 * 100)),
6757 ((perc / 0x10000) % 100),
6760 } else if ((quiet == 0) && (++warnings <= 1)) {
6761 warnx("Unexpected SCSI Sense Key "
6762 "Specific value returned "
6763 "during sanitize:");
6764 scsi_sense_print(device, &ccb->csio,
6766 warnx("Unable to print status "
6767 "information, but sanitze will "
6769 warnx("will exit when sanitize is "
6774 warnx("Unexpected SCSI error during sanitize");
6775 cam_error_print(device, ccb, CAM_ESF_ALL,
6776 CAM_EPF_ALL, stderr);
6780 } else if (status != CAM_REQ_CMP && status != CAM_REQUEUE_REQ) {
6781 warnx("Unexpected CAM status %#x", status);
6782 if (arglist & CAM_ARG_VERBOSE)
6783 cam_error_print(device, ccb, CAM_ESF_ALL,
6784 CAM_EPF_ALL, stderr);
6787 } while ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP);
6792 sanitize(struct cam_device *device, int argc, char **argv,
6793 char *combinedopt, int task_attr, int retry_count, int timeout)
6796 u_int8_t action = 0;
6798 int ycount = 0, quiet = 0;
6806 const char *pattern = NULL;
6807 u_int8_t *data_ptr = NULL;
6808 u_int32_t dxfer_len = 0;
6810 uint16_t feature, count;
6813 camcontrol_devtype dt;
6816 * Get the device type, request no I/O be done to do this.
6818 error = get_device_type(device, -1, 0, 0, &dt);
6819 if (error != 0 || (unsigned)dt > CC_DT_UNKNOWN) {
6820 warnx("sanitize: can't get device type");
6824 ccb = cam_getccb(device);
6827 warnx("sanitize: error allocating ccb");
6831 while ((c = getopt(argc, argv, combinedopt)) != -1) {
6834 if (strcasecmp(optarg, "overwrite") == 0)
6835 action = SSZ_SERVICE_ACTION_OVERWRITE;
6836 else if (strcasecmp(optarg, "block") == 0)
6837 action = SSZ_SERVICE_ACTION_BLOCK_ERASE;
6838 else if (strcasecmp(optarg, "crypto") == 0)
6839 action = SSZ_SERVICE_ACTION_CRYPTO_ERASE;
6840 else if (strcasecmp(optarg, "exitfailure") == 0)
6841 action = SSZ_SERVICE_ACTION_EXIT_MODE_FAILURE;
6843 warnx("invalid service operation \"%s\"",
6846 goto sanitize_bailout;
6850 passes = strtol(optarg, NULL, 0);
6851 if (passes < 1 || passes > 31) {
6852 warnx("invalid passes value %d", passes);
6854 goto sanitize_bailout;
6873 /* ATA supports only immediate commands. */
6874 if (dt == CC_DT_SCSI)
6887 warnx("an action is required");
6889 goto sanitize_bailout;
6890 } else if (action == SSZ_SERVICE_ACTION_OVERWRITE) {
6891 struct scsi_sanitize_parameter_list *pl;
6895 if (pattern == NULL) {
6896 warnx("overwrite action requires -P argument");
6898 goto sanitize_bailout;
6900 fd = open(pattern, O_RDONLY);
6902 warn("cannot open pattern file %s", pattern);
6904 goto sanitize_bailout;
6906 if (fstat(fd, &sb) < 0) {
6907 warn("cannot stat pattern file %s", pattern);
6909 goto sanitize_bailout;
6912 if (sz > SSZPL_MAX_PATTERN_LENGTH) {
6913 warnx("pattern file size exceeds maximum value %d",
6914 SSZPL_MAX_PATTERN_LENGTH);
6916 goto sanitize_bailout;
6918 dxfer_len = sizeof(*pl) + sz;
6919 data_ptr = calloc(1, dxfer_len);
6920 if (data_ptr == NULL) {
6921 warnx("cannot allocate parameter list buffer");
6923 goto sanitize_bailout;
6926 amt = read(fd, data_ptr + sizeof(*pl), sz);
6928 warn("cannot read pattern file");
6930 goto sanitize_bailout;
6931 } else if (amt != sz) {
6932 warnx("short pattern file read");
6934 goto sanitize_bailout;
6937 pl = (struct scsi_sanitize_parameter_list *)data_ptr;
6943 pl->byte1 |= SSZPL_INVERT;
6944 scsi_ulto2b(sz, pl->length);
6950 else if (invert != 0)
6952 else if (pattern != NULL)
6957 warnx("%s argument only valid with overwrite "
6960 goto sanitize_bailout;
6964 if (quiet == 0 && ycount == 0) {
6965 fprintf(stdout, "You are about to REMOVE ALL DATA from the "
6966 "following device:\n");
6968 if (dt == CC_DT_SCSI) {
6969 error = scsidoinquiry(device, argc, argv, combinedopt,
6970 task_attr, retry_count, timeout);
6971 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
6972 struct ata_params *ident_buf;
6973 error = ata_do_identify(device, retry_count, timeout,
6976 printf("%s%d: ", device->device_name,
6977 device->dev_unit_num);
6978 ata_print_ident(ident_buf);
6985 warnx("sanitize: error sending inquiry");
6986 goto sanitize_bailout;
6991 if (!get_confirmation()) {
6993 goto sanitize_bailout;
6998 use_timeout = timeout;
7000 use_timeout = (immediate ? 10 : 10800) * 1000;
7002 if (immediate == 0 && quiet == 0) {
7003 fprintf(stdout, "Current sanitize timeout is %d seconds\n",
7004 use_timeout / 1000);
7008 * If the user hasn't disabled questions and didn't specify a
7009 * timeout on the command line, ask them if they want the current
7012 if (immediate == 0 && ycount == 0 && timeout == 0) {
7014 int new_timeout = 0;
7016 fprintf(stdout, "Enter new timeout in seconds or press\n"
7017 "return to keep the current timeout [%d] ",
7018 use_timeout / 1000);
7020 if (fgets(str, sizeof(str), stdin) != NULL) {
7022 new_timeout = atoi(str);
7025 if (new_timeout != 0) {
7026 use_timeout = new_timeout * 1000;
7027 fprintf(stdout, "Using new timeout value %d\n",
7028 use_timeout / 1000);
7032 if (dt == CC_DT_SCSI) {
7035 byte2 |= SSZ_UNRESTRICTED_EXIT;
7038 scsi_sanitize(&ccb->csio,
7039 /* retries */ retry_count,
7041 /* tag_action */ task_attr,
7044 /* data_ptr */ data_ptr,
7045 /* dxfer_len */ dxfer_len,
7046 /* sense_len */ SSD_FULL_SIZE,
7047 /* timeout */ use_timeout);
7049 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7050 if (arglist & CAM_ARG_ERR_RECOVER)
7051 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7052 if (cam_send_ccb(device, ccb) < 0) {
7053 warn("error sending sanitize command");
7055 goto sanitize_bailout;
7057 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
7058 if (action == SSZ_SERVICE_ACTION_OVERWRITE) {
7059 feature = 0x14; /* OVERWRITE EXT */
7060 lba = 0x4F5700000000 | scsi_4btoul(data_ptr + 4);
7061 count = (passes == 0) ? 1 : (passes >= 16) ? 0 : passes;
7063 count |= 0x80; /* INVERT PATTERN */
7065 count |= 0x10; /* FAILURE MODE */
7066 } else if (action == SSZ_SERVICE_ACTION_BLOCK_ERASE) {
7067 feature = 0x12; /* BLOCK ERASE EXT */
7068 lba = 0x0000426B4572;
7071 count |= 0x10; /* FAILURE MODE */
7072 } else if (action == SSZ_SERVICE_ACTION_CRYPTO_ERASE) {
7073 feature = 0x11; /* CRYPTO SCRAMBLE EXT */
7074 lba = 0x000043727970;
7077 count |= 0x10; /* FAILURE MODE */
7078 } else if (action == SSZ_SERVICE_ACTION_EXIT_MODE_FAILURE) {
7079 feature = 0x00; /* SANITIZE STATUS EXT */
7081 count = 1; /* CLEAR SANITIZE OPERATION FAILED */
7084 goto sanitize_bailout;
7087 error = ata_do_cmd(device,
7090 /*flags*/CAM_DIR_NONE,
7091 /*protocol*/AP_PROTO_NON_DATA | AP_EXTEND,
7093 /*tag_action*/MSG_SIMPLE_Q_TAG,
7094 /*command*/ATA_SANITIZE,
7095 /*features*/feature,
7097 /*sector_count*/count,
7100 /*timeout*/ use_timeout,
7104 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7105 struct scsi_sense_data *sense;
7106 int error_code, sense_key, asc, ascq;
7108 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
7109 CAM_SCSI_STATUS_ERROR) {
7110 sense = &ccb->csio.sense_data;
7111 scsi_extract_sense_len(sense, ccb->csio.sense_len -
7112 ccb->csio.sense_resid, &error_code, &sense_key,
7113 &asc, &ascq, /*show_errors*/ 1);
7115 if (sense_key == SSD_KEY_ILLEGAL_REQUEST &&
7116 asc == 0x20 && ascq == 0x00)
7117 warnx("sanitize is not supported by "
7120 warnx("error sanitizing this device");
7122 warnx("error sanitizing this device");
7124 if (arglist & CAM_ARG_VERBOSE) {
7125 cam_error_print(device, ccb, CAM_ESF_ALL,
7126 CAM_EPF_ALL, stderr);
7129 goto sanitize_bailout;
7133 * If we ran in non-immediate mode, we already checked for errors
7134 * above and printed out any necessary information. If we're in
7135 * immediate mode, we need to loop through and get status
7136 * information periodically.
7138 if (immediate == 0) {
7140 fprintf(stdout, "Sanitize Complete\n");
7142 goto sanitize_bailout;
7146 if (dt == CC_DT_SCSI) {
7147 error = sanitize_wait_scsi(device, ccb, task_attr, quiet);
7148 } else if (dt == CC_DT_ATA || dt == CC_DT_SATL) {
7149 error = sanitize_wait_ata(device, ccb, quiet, dt);
7152 if (error == 0 && quiet == 0)
7153 fprintf(stdout, "Sanitize Complete \n");
7158 if (data_ptr != NULL)
7166 scsireportluns(struct cam_device *device, int argc, char **argv,
7167 char *combinedopt, int task_attr, int retry_count, int timeout)
7170 int c, countonly, lunsonly;
7171 struct scsi_report_luns_data *lundata;
7173 uint8_t report_type;
7174 uint32_t list_len, i, j;
7179 report_type = RPL_REPORT_DEFAULT;
7180 ccb = cam_getccb(device);
7183 warnx("%s: error allocating ccb", __func__);
7190 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7199 if (strcasecmp(optarg, "default") == 0)
7200 report_type = RPL_REPORT_DEFAULT;
7201 else if (strcasecmp(optarg, "wellknown") == 0)
7202 report_type = RPL_REPORT_WELLKNOWN;
7203 else if (strcasecmp(optarg, "all") == 0)
7204 report_type = RPL_REPORT_ALL;
7206 warnx("%s: invalid report type \"%s\"",
7217 if ((countonly != 0)
7218 && (lunsonly != 0)) {
7219 warnx("%s: you can only specify one of -c or -l", __func__);
7224 * According to SPC-4, the allocation length must be at least 16
7225 * bytes -- enough for the header and one LUN.
7227 alloc_len = sizeof(*lundata) + 8;
7231 lundata = malloc(alloc_len);
7233 if (lundata == NULL) {
7234 warn("%s: error mallocing %d bytes", __func__, alloc_len);
7239 scsi_report_luns(&ccb->csio,
7240 /*retries*/ retry_count,
7242 /*tag_action*/ task_attr,
7243 /*select_report*/ report_type,
7244 /*rpl_buf*/ lundata,
7245 /*alloc_len*/ alloc_len,
7246 /*sense_len*/ SSD_FULL_SIZE,
7247 /*timeout*/ timeout ? timeout : 5000);
7249 /* Disable freezing the device queue */
7250 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7252 if (arglist & CAM_ARG_ERR_RECOVER)
7253 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7255 if (cam_send_ccb(device, ccb) < 0) {
7256 warn("error sending REPORT LUNS command");
7261 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7262 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7268 list_len = scsi_4btoul(lundata->length);
7271 * If we need to list the LUNs, and our allocation
7272 * length was too short, reallocate and retry.
7274 if ((countonly == 0)
7275 && (list_len > (alloc_len - sizeof(*lundata)))) {
7276 alloc_len = list_len + sizeof(*lundata);
7282 fprintf(stdout, "%u LUN%s found\n", list_len / 8,
7283 ((list_len / 8) > 1) ? "s" : "");
7288 for (i = 0; i < (list_len / 8); i++) {
7292 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) {
7294 fprintf(stdout, ",");
7295 switch (lundata->luns[i].lundata[j] &
7296 RPL_LUNDATA_ATYP_MASK) {
7297 case RPL_LUNDATA_ATYP_PERIPH:
7298 if ((lundata->luns[i].lundata[j] &
7299 RPL_LUNDATA_PERIPH_BUS_MASK) != 0)
7300 fprintf(stdout, "%d:",
7301 lundata->luns[i].lundata[j] &
7302 RPL_LUNDATA_PERIPH_BUS_MASK);
7304 && ((lundata->luns[i].lundata[j+2] &
7305 RPL_LUNDATA_PERIPH_BUS_MASK) == 0))
7308 fprintf(stdout, "%d",
7309 lundata->luns[i].lundata[j+1]);
7311 case RPL_LUNDATA_ATYP_FLAT: {
7313 tmplun[0] = lundata->luns[i].lundata[j] &
7314 RPL_LUNDATA_FLAT_LUN_MASK;
7315 tmplun[1] = lundata->luns[i].lundata[j+1];
7317 fprintf(stdout, "%d", scsi_2btoul(tmplun));
7321 case RPL_LUNDATA_ATYP_LUN:
7322 fprintf(stdout, "%d:%d:%d",
7323 (lundata->luns[i].lundata[j+1] &
7324 RPL_LUNDATA_LUN_BUS_MASK) >> 5,
7325 lundata->luns[i].lundata[j] &
7326 RPL_LUNDATA_LUN_TARG_MASK,
7327 lundata->luns[i].lundata[j+1] &
7328 RPL_LUNDATA_LUN_LUN_MASK);
7330 case RPL_LUNDATA_ATYP_EXTLUN: {
7331 int field_len_code, eam_code;
7333 eam_code = lundata->luns[i].lundata[j] &
7334 RPL_LUNDATA_EXT_EAM_MASK;
7335 field_len_code = (lundata->luns[i].lundata[j] &
7336 RPL_LUNDATA_EXT_LEN_MASK) >> 4;
7338 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK)
7339 && (field_len_code == 0x00)) {
7340 fprintf(stdout, "%d",
7341 lundata->luns[i].lundata[j+1]);
7342 } else if ((eam_code ==
7343 RPL_LUNDATA_EXT_EAM_NOT_SPEC)
7344 && (field_len_code == 0x03)) {
7348 * This format takes up all 8 bytes.
7349 * If we aren't starting at offset 0,
7353 fprintf(stdout, "Invalid "
7356 "specified format", j);
7360 bzero(tmp_lun, sizeof(tmp_lun));
7361 bcopy(&lundata->luns[i].lundata[j+1],
7362 &tmp_lun[1], sizeof(tmp_lun) - 1);
7363 fprintf(stdout, "%#jx",
7364 (intmax_t)scsi_8btou64(tmp_lun));
7367 fprintf(stderr, "Unknown Extended LUN"
7368 "Address method %#x, length "
7369 "code %#x", eam_code,
7376 fprintf(stderr, "Unknown LUN address method "
7377 "%#x\n", lundata->luns[i].lundata[0] &
7378 RPL_LUNDATA_ATYP_MASK);
7382 * For the flat addressing method, there are no
7383 * other levels after it.
7388 fprintf(stdout, "\n");
7401 scsireadcapacity(struct cam_device *device, int argc, char **argv,
7402 char *combinedopt, int task_attr, int retry_count, int timeout)
7405 int blocksizeonly, humanize, numblocks, quiet, sizeonly, baseten, longonly;
7406 struct scsi_read_capacity_data rcap;
7407 struct scsi_read_capacity_data_long rcaplong;
7422 ccb = cam_getccb(device);
7425 warnx("%s: error allocating ccb", __func__);
7429 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7459 if ((blocksizeonly != 0)
7460 && (numblocks != 0)) {
7461 warnx("%s: you can only specify one of -b or -N", __func__);
7466 if ((blocksizeonly != 0)
7467 && (sizeonly != 0)) {
7468 warnx("%s: you can only specify one of -b or -s", __func__);
7475 warnx("%s: you can only specify one of -h/-H or -q", __func__);
7481 && (blocksizeonly != 0)) {
7482 warnx("%s: you can only specify one of -h/-H or -b", __func__);
7490 scsi_read_capacity(&ccb->csio,
7491 /*retries*/ retry_count,
7493 /*tag_action*/ task_attr,
7496 /*timeout*/ timeout ? timeout : 5000);
7498 /* Disable freezing the device queue */
7499 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7501 if (arglist & CAM_ARG_ERR_RECOVER)
7502 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7504 if (cam_send_ccb(device, ccb) < 0) {
7505 warn("error sending READ CAPACITY command");
7510 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7511 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7516 maxsector = scsi_4btoul(rcap.addr);
7517 block_len = scsi_4btoul(rcap.length);
7520 * A last block of 2^32-1 means that the true capacity is over 2TB,
7521 * and we need to issue the long READ CAPACITY to get the real
7522 * capacity. Otherwise, we're all set.
7524 if (maxsector != 0xffffffff)
7528 scsi_read_capacity_16(&ccb->csio,
7529 /*retries*/ retry_count,
7531 /*tag_action*/ task_attr,
7535 /*rcap_buf*/ (uint8_t *)&rcaplong,
7536 /*rcap_buf_len*/ sizeof(rcaplong),
7537 /*sense_len*/ SSD_FULL_SIZE,
7538 /*timeout*/ timeout ? timeout : 5000);
7540 /* Disable freezing the device queue */
7541 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
7543 if (arglist & CAM_ARG_ERR_RECOVER)
7544 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
7546 if (cam_send_ccb(device, ccb) < 0) {
7547 warn("error sending READ CAPACITY (16) command");
7552 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
7553 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
7558 maxsector = scsi_8btou64(rcaplong.addr);
7559 block_len = scsi_4btoul(rcaplong.length);
7562 if (blocksizeonly == 0) {
7564 * Humanize implies !quiet, and also implies numblocks.
7566 if (humanize != 0) {
7571 tmpbytes = (maxsector + 1) * block_len;
7572 ret = humanize_number(tmpstr, sizeof(tmpstr),
7573 tmpbytes, "", HN_AUTOSCALE,
7576 HN_DIVISOR_1000 : 0));
7578 warnx("%s: humanize_number failed!", __func__);
7582 fprintf(stdout, "Device Size: %s%s", tmpstr,
7583 (sizeonly == 0) ? ", " : "\n");
7584 } else if (numblocks != 0) {
7585 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
7586 "Blocks: " : "", (uintmax_t)maxsector + 1,
7587 (sizeonly == 0) ? ", " : "\n");
7589 fprintf(stdout, "%s%ju%s", (quiet == 0) ?
7590 "Last Block: " : "", (uintmax_t)maxsector,
7591 (sizeonly == 0) ? ", " : "\n");
7595 fprintf(stdout, "%s%u%s\n", (quiet == 0) ?
7596 "Block Length: " : "", block_len, (quiet == 0) ?
7605 smpcmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
7606 int retry_count, int timeout)
7610 uint8_t *smp_request = NULL, *smp_response = NULL;
7611 int request_size = 0, response_size = 0;
7612 int fd_request = 0, fd_response = 0;
7613 char *datastr = NULL;
7614 struct get_hook hook;
7619 * Note that at the moment we don't support sending SMP CCBs to
7620 * devices that aren't probed by CAM.
7622 ccb = cam_getccb(device);
7624 warnx("%s: error allocating CCB", __func__);
7628 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7631 arglist |= CAM_ARG_CMD_IN;
7632 response_size = strtol(optarg, NULL, 0);
7633 if (response_size <= 0) {
7634 warnx("invalid number of response bytes %d",
7637 goto smpcmd_bailout;
7639 hook.argc = argc - optind;
7640 hook.argv = argv + optind;
7643 datastr = cget(&hook, NULL);
7645 * If the user supplied "-" instead of a format, he
7646 * wants the data to be written to stdout.
7648 if ((datastr != NULL)
7649 && (datastr[0] == '-'))
7652 smp_response = (u_int8_t *)malloc(response_size);
7653 if (smp_response == NULL) {
7654 warn("can't malloc memory for SMP response");
7656 goto smpcmd_bailout;
7660 arglist |= CAM_ARG_CMD_OUT;
7661 request_size = strtol(optarg, NULL, 0);
7662 if (request_size <= 0) {
7663 warnx("invalid number of request bytes %d",
7666 goto smpcmd_bailout;
7668 hook.argc = argc - optind;
7669 hook.argv = argv + optind;
7671 datastr = cget(&hook, NULL);
7672 smp_request = (u_int8_t *)malloc(request_size);
7673 if (smp_request == NULL) {
7674 warn("can't malloc memory for SMP request");
7676 goto smpcmd_bailout;
7678 bzero(smp_request, request_size);
7680 * If the user supplied "-" instead of a format, he
7681 * wants the data to be read from stdin.
7683 if ((datastr != NULL)
7684 && (datastr[0] == '-'))
7687 buff_encode_visit(smp_request, request_size,
7698 * If fd_data is set, and we're writing to the device, we need to
7699 * read the data the user wants written from stdin.
7701 if ((fd_request == 1) && (arglist & CAM_ARG_CMD_OUT)) {
7703 int amt_to_read = request_size;
7704 u_int8_t *buf_ptr = smp_request;
7706 for (amt_read = 0; amt_to_read > 0;
7707 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
7708 if (amt_read == -1) {
7709 warn("error reading data from stdin");
7711 goto smpcmd_bailout;
7713 amt_to_read -= amt_read;
7714 buf_ptr += amt_read;
7718 if (((arglist & CAM_ARG_CMD_IN) == 0)
7719 || ((arglist & CAM_ARG_CMD_OUT) == 0)) {
7720 warnx("%s: need both the request (-r) and response (-R) "
7721 "arguments", __func__);
7723 goto smpcmd_bailout;
7726 flags |= CAM_DEV_QFRZDIS;
7728 cam_fill_smpio(&ccb->smpio,
7729 /*retries*/ retry_count,
7732 /*smp_request*/ smp_request,
7733 /*smp_request_len*/ request_size,
7734 /*smp_response*/ smp_response,
7735 /*smp_response_len*/ response_size,
7736 /*timeout*/ timeout ? timeout : 5000);
7738 ccb->smpio.flags = SMP_FLAG_NONE;
7740 if (((retval = cam_send_ccb(device, ccb)) < 0)
7741 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7742 const char warnstr[] = "error sending command";
7749 if (arglist & CAM_ARG_VERBOSE) {
7750 cam_error_print(device, ccb, CAM_ESF_ALL,
7751 CAM_EPF_ALL, stderr);
7755 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
7756 && (response_size > 0)) {
7757 if (fd_response == 0) {
7758 buff_decode_visit(smp_response, response_size,
7759 datastr, arg_put, NULL);
7760 fprintf(stdout, "\n");
7762 ssize_t amt_written;
7763 int amt_to_write = response_size;
7764 u_int8_t *buf_ptr = smp_response;
7766 for (amt_written = 0; (amt_to_write > 0) &&
7767 (amt_written = write(STDOUT_FILENO, buf_ptr,
7768 amt_to_write)) > 0;){
7769 amt_to_write -= amt_written;
7770 buf_ptr += amt_written;
7772 if (amt_written == -1) {
7773 warn("error writing data to stdout");
7775 goto smpcmd_bailout;
7776 } else if ((amt_written == 0)
7777 && (amt_to_write > 0)) {
7778 warnx("only wrote %u bytes out of %u",
7779 response_size - amt_to_write,
7788 if (smp_request != NULL)
7791 if (smp_response != NULL)
7798 mmcsdcmd(struct cam_device *device, int argc, char **argv, char *combinedopt,
7799 int retry_count, int timeout)
7803 int32_t mmc_opcode = 0, mmc_arg = 0;
7804 int32_t mmc_flags = -1;
7807 int is_bw_4 = 0, is_bw_1 = 0;
7808 int is_frequency = 0;
7809 int is_highspeed = 0, is_stdspeed = 0;
7810 int is_info_request = 0;
7812 uint8_t mmc_data_byte = 0;
7813 uint32_t mmc_frequency = 0;
7815 /* For IO_RW_EXTENDED command */
7816 uint8_t *mmc_data = NULL;
7817 struct mmc_data mmc_d;
7818 int mmc_data_len = 0;
7821 * Note that at the moment we don't support sending SMP CCBs to
7822 * devices that aren't probed by CAM.
7824 ccb = cam_getccb(device);
7826 warnx("%s: error allocating CCB", __func__);
7830 bzero(&(&ccb->ccb_h)[1],
7831 sizeof(union ccb) - sizeof(struct ccb_hdr));
7833 while ((c = getopt(argc, argv, combinedopt)) != -1) {
7842 if (!strcmp(optarg, "high"))
7848 is_info_request = 1;
7852 mmc_frequency = strtol(optarg, NULL, 0);
7855 mmc_opcode = strtol(optarg, NULL, 0);
7856 if (mmc_opcode < 0) {
7857 warnx("invalid MMC opcode %d",
7860 goto mmccmd_bailout;
7864 mmc_arg = strtol(optarg, NULL, 0);
7866 warnx("invalid MMC arg %d",
7869 goto mmccmd_bailout;
7873 mmc_flags = strtol(optarg, NULL, 0);
7874 if (mmc_flags < 0) {
7875 warnx("invalid MMC flags %d",
7878 goto mmccmd_bailout;
7882 mmc_data_len = strtol(optarg, NULL, 0);
7883 if (mmc_data_len <= 0) {
7884 warnx("invalid MMC data len %d",
7887 goto mmccmd_bailout;
7894 mmc_data_byte = strtol(optarg, NULL, 0);
7900 flags |= CAM_DEV_QFRZDIS; /* masks are broken?! */
7902 /* If flags are left default, supply the right flags */
7904 switch (mmc_opcode) {
7905 case MMC_GO_IDLE_STATE:
7906 mmc_flags = MMC_RSP_NONE | MMC_CMD_BC;
7908 case IO_SEND_OP_COND:
7909 mmc_flags = MMC_RSP_R4;
7911 case SD_SEND_RELATIVE_ADDR:
7912 mmc_flags = MMC_RSP_R6 | MMC_CMD_BCR;
7914 case MMC_SELECT_CARD:
7915 mmc_flags = MMC_RSP_R1B | MMC_CMD_AC;
7916 mmc_arg = mmc_arg << 16;
7918 case SD_IO_RW_DIRECT:
7919 mmc_flags = MMC_RSP_R5 | MMC_CMD_AC;
7920 mmc_arg = SD_IO_RW_ADR(mmc_arg);
7922 mmc_arg |= SD_IO_RW_WR | SD_IO_RW_RAW | SD_IO_RW_DAT(mmc_data_byte);
7924 case SD_IO_RW_EXTENDED:
7925 mmc_flags = MMC_RSP_R5 | MMC_CMD_ADTC;
7926 mmc_arg = SD_IO_RW_ADR(mmc_arg);
7927 int len_arg = mmc_data_len;
7928 if (mmc_data_len == 512)
7932 mmc_arg |= SD_IOE_RW_LEN(len_arg) | SD_IO_RW_INCR;
7934 // mmc_arg |= SD_IOE_RW_BLK | SD_IOE_RW_LEN(len_arg) | SD_IO_RW_INCR;
7937 mmc_flags = MMC_RSP_R1;
7941 // Switch bus width instead of sending IO command
7942 if (is_bw_4 || is_bw_1) {
7943 struct ccb_trans_settings_mmc *cts;
7944 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
7945 ccb->ccb_h.flags = 0;
7946 cts = &ccb->cts.proto_specific.mmc;
7947 cts->ios.bus_width = is_bw_4 == 1 ? bus_width_4 : bus_width_1;
7948 cts->ios_valid = MMC_BW;
7949 if (((retval = cam_send_ccb(device, ccb)) < 0)
7950 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7951 warn("Error sending command");
7953 printf("Parameters set OK\n");
7960 struct ccb_trans_settings_mmc *cts;
7961 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
7962 ccb->ccb_h.flags = 0;
7963 cts = &ccb->cts.proto_specific.mmc;
7964 cts->ios.clock = mmc_frequency;
7965 cts->ios_valid = MMC_CLK;
7966 if (((retval = cam_send_ccb(device, ccb)) < 0)
7967 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7968 warn("Error sending command");
7970 printf("Parameters set OK\n");
7976 // Switch bus speed instead of sending IO command
7977 if (is_stdspeed || is_highspeed) {
7978 struct ccb_trans_settings_mmc *cts;
7979 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
7980 ccb->ccb_h.flags = 0;
7981 cts = &ccb->cts.proto_specific.mmc;
7982 cts->ios.timing = is_highspeed == 1 ? bus_timing_hs : bus_timing_normal;
7983 cts->ios_valid = MMC_BT;
7984 if (((retval = cam_send_ccb(device, ccb)) < 0)
7985 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
7986 warn("Error sending command");
7988 printf("Speed set OK (HS: %d)\n", is_highspeed);
7994 // Get information about controller and its settings
7995 if (is_info_request) {
7996 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
7997 ccb->ccb_h.flags = 0;
7998 struct ccb_trans_settings_mmc *cts;
7999 cts = &ccb->cts.proto_specific.mmc;
8000 if (((retval = cam_send_ccb(device, ccb)) < 0)
8001 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8002 warn("Error sending command");
8005 printf("Host controller information\n");
8006 printf("Host OCR: 0x%x\n", cts->host_ocr);
8007 printf("Min frequency: %u KHz\n", cts->host_f_min / 1000);
8008 printf("Max frequency: %u MHz\n", cts->host_f_max / 1000000);
8009 printf("Supported bus width:\n");
8010 if (cts->host_caps & MMC_CAP_4_BIT_DATA)
8012 if (cts->host_caps & MMC_CAP_8_BIT_DATA)
8015 printf("Supported operating modes:\n");
8016 if (cts->host_caps & MMC_CAP_HSPEED)
8017 printf(" Can do High Speed transfers\n");
8018 if (cts->host_caps & MMC_CAP_UHS_SDR12)
8019 printf(" Can do UHS SDR12\n");
8020 if (cts->host_caps & MMC_CAP_UHS_SDR25)
8021 printf(" Can do UHS SDR25\n");
8022 if (cts->host_caps & MMC_CAP_UHS_SDR50)
8023 printf(" Can do UHS SDR50\n");
8024 if (cts->host_caps & MMC_CAP_UHS_SDR104)
8025 printf(" Can do UHS SDR104\n");
8026 if (cts->host_caps & MMC_CAP_UHS_DDR50)
8027 printf(" Can do UHS DDR50\n");
8028 if (cts->host_caps & MMC_CAP_MMC_DDR52_120)
8029 printf(" Can do eMMC DDR52 at 1.2V\n");
8030 if (cts->host_caps & MMC_CAP_MMC_DDR52_180)
8031 printf(" Can do eMMC DDR52 at 1.8V\n");
8032 if (cts->host_caps & MMC_CAP_MMC_HS200_120)
8033 printf(" Can do eMMC HS200 at 1.2V\n");
8034 if (cts->host_caps & MMC_CAP_MMC_HS200_180)
8035 printf(" Can do eMMC HS200 at 1.8V\n");
8036 if (cts->host_caps & MMC_CAP_MMC_HS400_120)
8037 printf(" Can do eMMC HS400 at 1.2V\n");
8038 if (cts->host_caps & MMC_CAP_MMC_HS400_180)
8039 printf(" Can do eMMC HS400 at 1.8V\n");
8041 printf("Supported VCCQ voltages:\n");
8042 if (cts->host_caps & MMC_CAP_SIGNALING_120)
8044 if (cts->host_caps & MMC_CAP_SIGNALING_180)
8046 if (cts->host_caps & MMC_CAP_SIGNALING_330)
8049 printf("Current settings:\n");
8050 printf(" Bus width: ");
8051 switch (cts->ios.bus_width) {
8062 printf(" Freq: %d.%03d MHz%s\n",
8063 cts->ios.clock / 1000000,
8064 (cts->ios.clock / 1000) % 1000,
8065 cts->ios.timing == bus_timing_hs ? " (high-speed timing)" : "");
8068 switch (cts->ios.vccq) {
8082 printf("CMD %d arg %d flags %02x\n", mmc_opcode, mmc_arg, mmc_flags);
8084 if (mmc_data_len > 0) {
8085 flags |= CAM_DIR_IN;
8086 mmc_data = malloc(mmc_data_len);
8087 memset(mmc_data, 0, mmc_data_len);
8088 memset(&mmc_d, 0, sizeof(mmc_d));
8089 mmc_d.len = mmc_data_len;
8090 mmc_d.data = mmc_data;
8091 mmc_d.flags = MMC_DATA_READ;
8092 } else flags |= CAM_DIR_NONE;
8094 cam_fill_mmcio(&ccb->mmcio,
8095 /*retries*/ retry_count,
8098 /*mmc_opcode*/ mmc_opcode,
8099 /*mmc_arg*/ mmc_arg,
8100 /*mmc_flags*/ mmc_flags,
8101 /*mmc_data*/ mmc_data_len > 0 ? &mmc_d : NULL,
8102 /*timeout*/ timeout ? timeout : 5000);
8104 if (((retval = cam_send_ccb(device, ccb)) < 0)
8105 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8106 const char warnstr[] = "error sending command";
8113 if (arglist & CAM_ARG_VERBOSE) {
8114 cam_error_print(device, ccb, CAM_ESF_ALL,
8115 CAM_EPF_ALL, stderr);
8119 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) {
8120 printf("MMCIO: error %d, %08x %08x %08x %08x\n",
8121 ccb->mmcio.cmd.error, ccb->mmcio.cmd.resp[0],
8122 ccb->mmcio.cmd.resp[1],
8123 ccb->mmcio.cmd.resp[2],
8124 ccb->mmcio.cmd.resp[3]);
8126 switch (mmc_opcode) {
8127 case SD_IO_RW_DIRECT:
8128 printf("IO_RW_DIRECT: resp byte %02x, cur state %d\n",
8129 SD_R5_DATA(ccb->mmcio.cmd.resp),
8130 (ccb->mmcio.cmd.resp[0] >> 12) & 0x3);
8132 case SD_IO_RW_EXTENDED:
8133 printf("IO_RW_EXTENDED: read %d bytes w/o error:\n", mmc_data_len);
8134 hexdump(mmc_data, mmc_data_len, NULL, 0);
8136 case SD_SEND_RELATIVE_ADDR:
8137 printf("SEND_RELATIVE_ADDR: published RCA %02x\n", ccb->mmcio.cmd.resp[0] >> 16);
8140 printf("No command-specific decoder for CMD %d\n", mmc_opcode);
8147 if (mmc_data_len > 0 && mmc_data != NULL)
8154 smpreportgeneral(struct cam_device *device, int argc, char **argv,
8155 char *combinedopt, int retry_count, int timeout)
8158 struct smp_report_general_request *request = NULL;
8159 struct smp_report_general_response *response = NULL;
8160 struct sbuf *sb = NULL;
8162 int c, long_response = 0;
8166 * Note that at the moment we don't support sending SMP CCBs to
8167 * devices that aren't probed by CAM.
8169 ccb = cam_getccb(device);
8171 warnx("%s: error allocating CCB", __func__);
8175 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8184 request = malloc(sizeof(*request));
8185 if (request == NULL) {
8186 warn("%s: unable to allocate %zd bytes", __func__,
8192 response = malloc(sizeof(*response));
8193 if (response == NULL) {
8194 warn("%s: unable to allocate %zd bytes", __func__,
8201 smp_report_general(&ccb->smpio,
8205 /*request_len*/ sizeof(*request),
8206 (uint8_t *)response,
8207 /*response_len*/ sizeof(*response),
8208 /*long_response*/ long_response,
8211 if (((retval = cam_send_ccb(device, ccb)) < 0)
8212 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8213 const char warnstr[] = "error sending command";
8220 if (arglist & CAM_ARG_VERBOSE) {
8221 cam_error_print(device, ccb, CAM_ESF_ALL,
8222 CAM_EPF_ALL, stderr);
8229 * If the device supports the long response bit, try again and see
8230 * if we can get all of the data.
8232 if ((response->long_response & SMP_RG_LONG_RESPONSE)
8233 && (long_response == 0)) {
8234 ccb->ccb_h.status = CAM_REQ_INPROG;
8235 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
8241 * XXX KDM detect and decode SMP errors here.
8243 sb = sbuf_new_auto();
8245 warnx("%s: error allocating sbuf", __func__);
8249 smp_report_general_sbuf(response, sizeof(*response), sb);
8251 if (sbuf_finish(sb) != 0) {
8252 warnx("%s: sbuf_finish", __func__);
8256 printf("%s", sbuf_data(sb));
8262 if (request != NULL)
8265 if (response != NULL)
8274 static struct camcontrol_opts phy_ops[] = {
8275 {"nop", SMP_PC_PHY_OP_NOP, CAM_ARG_NONE, NULL},
8276 {"linkreset", SMP_PC_PHY_OP_LINK_RESET, CAM_ARG_NONE, NULL},
8277 {"hardreset", SMP_PC_PHY_OP_HARD_RESET, CAM_ARG_NONE, NULL},
8278 {"disable", SMP_PC_PHY_OP_DISABLE, CAM_ARG_NONE, NULL},
8279 {"clearerrlog", SMP_PC_PHY_OP_CLEAR_ERR_LOG, CAM_ARG_NONE, NULL},
8280 {"clearaffiliation", SMP_PC_PHY_OP_CLEAR_AFFILIATON, CAM_ARG_NONE,NULL},
8281 {"sataportsel", SMP_PC_PHY_OP_TRANS_SATA_PSS, CAM_ARG_NONE, NULL},
8282 {"clearitnl", SMP_PC_PHY_OP_CLEAR_STP_ITN_LS, CAM_ARG_NONE, NULL},
8283 {"setdevname", SMP_PC_PHY_OP_SET_ATT_DEV_NAME, CAM_ARG_NONE, NULL},
8288 smpphycontrol(struct cam_device *device, int argc, char **argv,
8289 char *combinedopt, int retry_count, int timeout)
8292 struct smp_phy_control_request *request = NULL;
8293 struct smp_phy_control_response *response = NULL;
8294 int long_response = 0;
8297 uint32_t phy_operation = SMP_PC_PHY_OP_NOP;
8299 uint64_t attached_dev_name = 0;
8300 int dev_name_set = 0;
8301 uint32_t min_plr = 0, max_plr = 0;
8302 uint32_t pp_timeout_val = 0;
8303 int slumber_partial = 0;
8304 int set_pp_timeout_val = 0;
8308 * Note that at the moment we don't support sending SMP CCBs to
8309 * devices that aren't probed by CAM.
8311 ccb = cam_getccb(device);
8313 warnx("%s: error allocating CCB", __func__);
8317 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8325 if (strcasecmp(optarg, "enable") == 0)
8327 else if (strcasecmp(optarg, "disable") == 0)
8330 warnx("%s: Invalid argument %s", __func__,
8337 slumber_partial |= enable <<
8338 SMP_PC_SAS_SLUMBER_SHIFT;
8341 slumber_partial |= enable <<
8342 SMP_PC_SAS_PARTIAL_SHIFT;
8345 slumber_partial |= enable <<
8346 SMP_PC_SATA_SLUMBER_SHIFT;
8349 slumber_partial |= enable <<
8350 SMP_PC_SATA_PARTIAL_SHIFT;
8353 warnx("%s: programmer error", __func__);
8356 break; /*NOTREACHED*/
8361 attached_dev_name = (uintmax_t)strtoumax(optarg,
8370 * We don't do extensive checking here, so this
8371 * will continue to work when new speeds come out.
8373 min_plr = strtoul(optarg, NULL, 0);
8375 || (min_plr > 0xf)) {
8376 warnx("%s: invalid link rate %x",
8384 * We don't do extensive checking here, so this
8385 * will continue to work when new speeds come out.
8387 max_plr = strtoul(optarg, NULL, 0);
8389 || (max_plr > 0xf)) {
8390 warnx("%s: invalid link rate %x",
8397 camcontrol_optret optreturn;
8398 cam_argmask argnums;
8401 if (phy_op_set != 0) {
8402 warnx("%s: only one phy operation argument "
8403 "(-o) allowed", __func__);
8411 * Allow the user to specify the phy operation
8412 * numerically, as well as with a name. This will
8413 * future-proof it a bit, so options that are added
8414 * in future specs can be used.
8416 if (isdigit(optarg[0])) {
8417 phy_operation = strtoul(optarg, NULL, 0);
8418 if ((phy_operation == 0)
8419 || (phy_operation > 0xff)) {
8420 warnx("%s: invalid phy operation %#x",
8421 __func__, phy_operation);
8427 optreturn = getoption(phy_ops, optarg, &phy_operation,
8430 if (optreturn == CC_OR_AMBIGUOUS) {
8431 warnx("%s: ambiguous option %s", __func__,
8436 } else if (optreturn == CC_OR_NOT_FOUND) {
8437 warnx("%s: option %s not found", __func__,
8449 pp_timeout_val = strtoul(optarg, NULL, 0);
8450 if (pp_timeout_val > 15) {
8451 warnx("%s: invalid partial pathway timeout "
8452 "value %u, need a value less than 16",
8453 __func__, pp_timeout_val);
8457 set_pp_timeout_val = 1;
8465 warnx("%s: a PHY (-p phy) argument is required",__func__);
8470 if (((dev_name_set != 0)
8471 && (phy_operation != SMP_PC_PHY_OP_SET_ATT_DEV_NAME))
8472 || ((phy_operation == SMP_PC_PHY_OP_SET_ATT_DEV_NAME)
8473 && (dev_name_set == 0))) {
8474 warnx("%s: -d name and -o setdevname arguments both "
8475 "required to set device name", __func__);
8480 request = malloc(sizeof(*request));
8481 if (request == NULL) {
8482 warn("%s: unable to allocate %zd bytes", __func__,
8488 response = malloc(sizeof(*response));
8489 if (response == NULL) {
8490 warn("%s: unable to allocate %zd bytes", __func__,
8496 smp_phy_control(&ccb->smpio,
8501 (uint8_t *)response,
8504 /*expected_exp_change_count*/ 0,
8507 (set_pp_timeout_val != 0) ? 1 : 0,
8515 if (((retval = cam_send_ccb(device, ccb)) < 0)
8516 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8517 const char warnstr[] = "error sending command";
8524 if (arglist & CAM_ARG_VERBOSE) {
8526 * Use CAM_EPF_NORMAL so we only get one line of
8527 * SMP command decoding.
8529 cam_error_print(device, ccb, CAM_ESF_ALL,
8530 CAM_EPF_NORMAL, stderr);
8536 /* XXX KDM print out something here for success? */
8541 if (request != NULL)
8544 if (response != NULL)
8551 smpmaninfo(struct cam_device *device, int argc, char **argv,
8552 char *combinedopt, int retry_count, int timeout)
8555 struct smp_report_manuf_info_request request;
8556 struct smp_report_manuf_info_response response;
8557 struct sbuf *sb = NULL;
8558 int long_response = 0;
8563 * Note that at the moment we don't support sending SMP CCBs to
8564 * devices that aren't probed by CAM.
8566 ccb = cam_getccb(device);
8568 warnx("%s: error allocating CCB", __func__);
8572 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8581 bzero(&request, sizeof(request));
8582 bzero(&response, sizeof(response));
8584 smp_report_manuf_info(&ccb->smpio,
8589 (uint8_t *)&response,
8594 if (((retval = cam_send_ccb(device, ccb)) < 0)
8595 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8596 const char warnstr[] = "error sending command";
8603 if (arglist & CAM_ARG_VERBOSE) {
8604 cam_error_print(device, ccb, CAM_ESF_ALL,
8605 CAM_EPF_ALL, stderr);
8611 sb = sbuf_new_auto();
8613 warnx("%s: error allocating sbuf", __func__);
8617 smp_report_manuf_info_sbuf(&response, sizeof(response), sb);
8619 if (sbuf_finish(sb) != 0) {
8620 warnx("%s: sbuf_finish", __func__);
8624 printf("%s", sbuf_data(sb));
8638 getdevid(struct cam_devitem *item)
8641 union ccb *ccb = NULL;
8643 struct cam_device *dev;
8645 dev = cam_open_btl(item->dev_match.path_id,
8646 item->dev_match.target_id,
8647 item->dev_match.target_lun, O_RDWR, NULL);
8650 warnx("%s", cam_errbuf);
8655 item->device_id_len = 0;
8657 ccb = cam_getccb(dev);
8659 warnx("%s: error allocating CCB", __func__);
8665 * On the first try, we just probe for the size of the data, and
8666 * then allocate that much memory and try again.
8669 ccb->ccb_h.func_code = XPT_DEV_ADVINFO;
8670 ccb->ccb_h.flags = CAM_DIR_IN;
8671 ccb->cdai.flags = CDAI_FLAG_NONE;
8672 ccb->cdai.buftype = CDAI_TYPE_SCSI_DEVID;
8673 ccb->cdai.bufsiz = item->device_id_len;
8674 if (item->device_id_len != 0)
8675 ccb->cdai.buf = (uint8_t *)item->device_id;
8677 if (cam_send_ccb(dev, ccb) < 0) {
8678 warn("%s: error sending XPT_GDEV_ADVINFO CCB", __func__);
8683 if (ccb->ccb_h.status != CAM_REQ_CMP) {
8684 warnx("%s: CAM status %#x", __func__, ccb->ccb_h.status);
8689 if (item->device_id_len == 0) {
8691 * This is our first time through. Allocate the buffer,
8692 * and then go back to get the data.
8694 if (ccb->cdai.provsiz == 0) {
8695 warnx("%s: invalid .provsiz field returned with "
8696 "XPT_GDEV_ADVINFO CCB", __func__);
8700 item->device_id_len = ccb->cdai.provsiz;
8701 item->device_id = malloc(item->device_id_len);
8702 if (item->device_id == NULL) {
8703 warn("%s: unable to allocate %d bytes", __func__,
8704 item->device_id_len);
8708 ccb->ccb_h.status = CAM_REQ_INPROG;
8714 cam_close_device(dev);
8723 * XXX KDM merge this code with getdevtree()?
8726 buildbusdevlist(struct cam_devlist *devlist)
8729 int bufsize, fd = -1;
8730 struct dev_match_pattern *patterns;
8731 struct cam_devitem *item = NULL;
8732 int skip_device = 0;
8735 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) {
8736 warn("couldn't open %s", XPT_DEVICE);
8740 bzero(&ccb, sizeof(union ccb));
8742 ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
8743 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
8744 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
8746 ccb.ccb_h.func_code = XPT_DEV_MATCH;
8747 bufsize = sizeof(struct dev_match_result) * 100;
8748 ccb.cdm.match_buf_len = bufsize;
8749 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
8750 if (ccb.cdm.matches == NULL) {
8751 warnx("can't malloc memory for matches");
8755 ccb.cdm.num_matches = 0;
8756 ccb.cdm.num_patterns = 2;
8757 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern) *
8758 ccb.cdm.num_patterns;
8760 patterns = (struct dev_match_pattern *)malloc(ccb.cdm.pattern_buf_len);
8761 if (patterns == NULL) {
8762 warnx("can't malloc memory for patterns");
8767 ccb.cdm.patterns = patterns;
8768 bzero(patterns, ccb.cdm.pattern_buf_len);
8770 patterns[0].type = DEV_MATCH_DEVICE;
8771 patterns[0].pattern.device_pattern.flags = DEV_MATCH_PATH;
8772 patterns[0].pattern.device_pattern.path_id = devlist->path_id;
8773 patterns[1].type = DEV_MATCH_PERIPH;
8774 patterns[1].pattern.periph_pattern.flags = PERIPH_MATCH_PATH;
8775 patterns[1].pattern.periph_pattern.path_id = devlist->path_id;
8778 * We do the ioctl multiple times if necessary, in case there are
8779 * more than 100 nodes in the EDT.
8784 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
8785 warn("error sending CAMIOCOMMAND ioctl");
8790 if ((ccb.ccb_h.status != CAM_REQ_CMP)
8791 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
8792 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
8793 warnx("got CAM error %#x, CDM error %d\n",
8794 ccb.ccb_h.status, ccb.cdm.status);
8799 for (i = 0; i < ccb.cdm.num_matches; i++) {
8800 switch (ccb.cdm.matches[i].type) {
8801 case DEV_MATCH_DEVICE: {
8802 struct device_match_result *dev_result;
8805 &ccb.cdm.matches[i].result.device_result;
8807 if (dev_result->flags &
8808 DEV_RESULT_UNCONFIGURED) {
8814 item = malloc(sizeof(*item));
8816 warn("%s: unable to allocate %zd bytes",
8817 __func__, sizeof(*item));
8821 bzero(item, sizeof(*item));
8822 bcopy(dev_result, &item->dev_match,
8823 sizeof(*dev_result));
8824 STAILQ_INSERT_TAIL(&devlist->dev_queue, item,
8827 if (getdevid(item) != 0) {
8833 case DEV_MATCH_PERIPH: {
8834 struct periph_match_result *periph_result;
8837 &ccb.cdm.matches[i].result.periph_result;
8839 if (skip_device != 0)
8841 item->num_periphs++;
8842 item->periph_matches = realloc(
8843 item->periph_matches,
8845 sizeof(struct periph_match_result));
8846 if (item->periph_matches == NULL) {
8847 warn("%s: error allocating periph "
8852 bcopy(periph_result, &item->periph_matches[
8853 item->num_periphs - 1],
8854 sizeof(*periph_result));
8858 fprintf(stderr, "%s: unexpected match "
8859 "type %d\n", __func__,
8860 ccb.cdm.matches[i].type);
8863 break; /*NOTREACHED*/
8866 } while ((ccb.ccb_h.status == CAM_REQ_CMP)
8867 && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
8875 free(ccb.cdm.matches);
8878 freebusdevlist(devlist);
8884 freebusdevlist(struct cam_devlist *devlist)
8886 struct cam_devitem *item, *item2;
8888 STAILQ_FOREACH_SAFE(item, &devlist->dev_queue, links, item2) {
8889 STAILQ_REMOVE(&devlist->dev_queue, item, cam_devitem,
8891 free(item->device_id);
8892 free(item->periph_matches);
8897 static struct cam_devitem *
8898 findsasdevice(struct cam_devlist *devlist, uint64_t sasaddr)
8900 struct cam_devitem *item;
8902 STAILQ_FOREACH(item, &devlist->dev_queue, links) {
8903 struct scsi_vpd_id_descriptor *idd;
8906 * XXX KDM look for LUN IDs as well?
8908 idd = scsi_get_devid(item->device_id,
8909 item->device_id_len,
8910 scsi_devid_is_sas_target);
8914 if (scsi_8btou64(idd->identifier) == sasaddr)
8922 smpphylist(struct cam_device *device, int argc, char **argv,
8923 char *combinedopt, int retry_count, int timeout)
8925 struct smp_report_general_request *rgrequest = NULL;
8926 struct smp_report_general_response *rgresponse = NULL;
8927 struct smp_discover_request *disrequest = NULL;
8928 struct smp_discover_response *disresponse = NULL;
8929 struct cam_devlist devlist;
8931 int long_response = 0;
8938 * Note that at the moment we don't support sending SMP CCBs to
8939 * devices that aren't probed by CAM.
8941 ccb = cam_getccb(device);
8943 warnx("%s: error allocating CCB", __func__);
8947 STAILQ_INIT(&devlist.dev_queue);
8949 rgrequest = malloc(sizeof(*rgrequest));
8950 if (rgrequest == NULL) {
8951 warn("%s: unable to allocate %zd bytes", __func__,
8952 sizeof(*rgrequest));
8957 rgresponse = malloc(sizeof(*rgresponse));
8958 if (rgresponse == NULL) {
8959 warn("%s: unable to allocate %zd bytes", __func__,
8960 sizeof(*rgresponse));
8965 while ((c = getopt(argc, argv, combinedopt)) != -1) {
8978 smp_report_general(&ccb->smpio,
8982 /*request_len*/ sizeof(*rgrequest),
8983 (uint8_t *)rgresponse,
8984 /*response_len*/ sizeof(*rgresponse),
8985 /*long_response*/ long_response,
8988 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
8990 if (((retval = cam_send_ccb(device, ccb)) < 0)
8991 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) {
8992 const char warnstr[] = "error sending command";
8999 if (arglist & CAM_ARG_VERBOSE) {
9000 cam_error_print(device, ccb, CAM_ESF_ALL,
9001 CAM_EPF_ALL, stderr);
9007 num_phys = rgresponse->num_phys;
9009 if (num_phys == 0) {
9011 fprintf(stdout, "%s: No Phys reported\n", __func__);
9016 devlist.path_id = device->path_id;
9018 retval = buildbusdevlist(&devlist);
9023 fprintf(stdout, "%d PHYs:\n", num_phys);
9024 fprintf(stdout, "PHY Attached SAS Address\n");
9027 disrequest = malloc(sizeof(*disrequest));
9028 if (disrequest == NULL) {
9029 warn("%s: unable to allocate %zd bytes", __func__,
9030 sizeof(*disrequest));
9035 disresponse = malloc(sizeof(*disresponse));
9036 if (disresponse == NULL) {
9037 warn("%s: unable to allocate %zd bytes", __func__,
9038 sizeof(*disresponse));
9043 for (i = 0; i < num_phys; i++) {
9044 struct cam_devitem *item;
9045 struct device_match_result *dev_match;
9046 char vendor[16], product[48], revision[16];
9050 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->smpio);
9052 ccb->ccb_h.status = CAM_REQ_INPROG;
9053 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
9055 smp_discover(&ccb->smpio,
9059 sizeof(*disrequest),
9060 (uint8_t *)disresponse,
9061 sizeof(*disresponse),
9063 /*ignore_zone_group*/ 0,
9067 if (((retval = cam_send_ccb(device, ccb)) < 0)
9068 || (((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
9069 && (disresponse->function_result != SMP_FR_PHY_VACANT))) {
9070 const char warnstr[] = "error sending command";
9077 if (arglist & CAM_ARG_VERBOSE) {
9078 cam_error_print(device, ccb, CAM_ESF_ALL,
9079 CAM_EPF_ALL, stderr);
9085 if (disresponse->function_result == SMP_FR_PHY_VACANT) {
9087 fprintf(stdout, "%3d <vacant>\n", i);
9091 if (disresponse->attached_device == SMP_DIS_AD_TYPE_NONE) {
9094 item = findsasdevice(&devlist,
9095 scsi_8btou64(disresponse->attached_sas_address));
9099 || (item != NULL)) {
9100 fprintf(stdout, "%3d 0x%016jx", i,
9101 (uintmax_t)scsi_8btou64(
9102 disresponse->attached_sas_address));
9104 fprintf(stdout, "\n");
9107 } else if (quiet != 0)
9110 dev_match = &item->dev_match;
9112 if (dev_match->protocol == PROTO_SCSI) {
9113 cam_strvis(vendor, dev_match->inq_data.vendor,
9114 sizeof(dev_match->inq_data.vendor),
9116 cam_strvis(product, dev_match->inq_data.product,
9117 sizeof(dev_match->inq_data.product),
9119 cam_strvis(revision, dev_match->inq_data.revision,
9120 sizeof(dev_match->inq_data.revision),
9122 sprintf(tmpstr, "<%s %s %s>", vendor, product,
9124 } else if ((dev_match->protocol == PROTO_ATA)
9125 || (dev_match->protocol == PROTO_SATAPM)) {
9126 cam_strvis(product, dev_match->ident_data.model,
9127 sizeof(dev_match->ident_data.model),
9129 cam_strvis(revision, dev_match->ident_data.revision,
9130 sizeof(dev_match->ident_data.revision),
9132 sprintf(tmpstr, "<%s %s>", product, revision);
9134 sprintf(tmpstr, "<>");
9136 fprintf(stdout, " %-33s ", tmpstr);
9139 * If we have 0 periphs, that's a bug...
9141 if (item->num_periphs == 0) {
9142 fprintf(stdout, "\n");
9146 fprintf(stdout, "(");
9147 for (j = 0; j < item->num_periphs; j++) {
9149 fprintf(stdout, ",");
9151 fprintf(stdout, "%s%d",
9152 item->periph_matches[j].periph_name,
9153 item->periph_matches[j].unit_number);
9156 fprintf(stdout, ")\n");
9170 freebusdevlist(&devlist);
9176 atapm_proc_resp(struct cam_device *device, union ccb *ccb)
9178 uint8_t error = 0, ata_device = 0, status = 0;
9183 retval = get_ata_status(device, ccb, &error, &count, &lba, &ata_device,
9186 if (arglist & CAM_ARG_VERBOSE) {
9187 cam_error_print(device, ccb, CAM_ESF_ALL,
9188 CAM_EPF_ALL, stderr);
9190 warnx("Can't get ATA command status");
9194 if (status & ATA_STATUS_ERROR) {
9195 cam_error_print(device, ccb, CAM_ESF_ALL,
9196 CAM_EPF_ALL, stderr);
9200 printf("%s%d: ", device->device_name, device->dev_unit_num);
9203 printf("Standby mode\n");
9206 printf("Standby_y mode\n");
9209 printf("NV Cache Power Mode and the spindle is spun down or spinning down\n");
9212 printf("NV Cache Power Mode and the spindle is spun up or spinning up\n");
9215 printf("Idle mode\n");
9218 printf("Idle_a mode\n");
9221 printf("Idle_b mode\n");
9224 printf("Idle_c mode\n");
9227 printf("Active or Idle mode\n");
9230 printf("Unknown mode 0x%02x\n", count);
9238 atapm(struct cam_device *device, int argc, char **argv,
9239 char *combinedopt, int retry_count, int timeout)
9245 u_int8_t ata_flags = 0;
9248 ccb = cam_getccb(device);
9251 warnx("%s: error allocating ccb", __func__);
9255 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9264 if (strcmp(argv[1], "idle") == 0) {
9266 cmd = ATA_IDLE_IMMEDIATE;
9269 } else if (strcmp(argv[1], "standby") == 0) {
9271 cmd = ATA_STANDBY_IMMEDIATE;
9273 cmd = ATA_STANDBY_CMD;
9274 } else if (strcmp(argv[1], "powermode") == 0) {
9275 cmd = ATA_CHECK_POWER_MODE;
9276 ata_flags = AP_FLAG_CHK_COND;
9285 else if (t <= (240 * 5))
9287 else if (t <= (252 * 5))
9288 /* special encoding for 21 minutes */
9290 else if (t <= (11 * 30 * 60))
9291 sc = (t - 1) / (30 * 60) + 241;
9295 retval = ata_do_cmd(device,
9297 /*retries*/retry_count,
9298 /*flags*/CAM_DIR_NONE,
9299 /*protocol*/AP_PROTO_NON_DATA,
9300 /*ata_flags*/ata_flags,
9301 /*tag_action*/MSG_SIMPLE_Q_TAG,
9308 /*timeout*/timeout ? timeout : 30 * 1000,
9313 if (retval || cmd != ATA_CHECK_POWER_MODE)
9316 return (atapm_proc_resp(device, ccb));
9320 ataaxm(struct cam_device *device, int argc, char **argv,
9321 char *combinedopt, int retry_count, int timeout)
9329 ccb = cam_getccb(device);
9332 warnx("%s: error allocating ccb", __func__);
9336 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9346 if (strcmp(argv[1], "apm") == 0) {
9362 retval = ata_do_cmd(device,
9364 /*retries*/retry_count,
9365 /*flags*/CAM_DIR_NONE,
9366 /*protocol*/AP_PROTO_NON_DATA,
9368 /*tag_action*/MSG_SIMPLE_Q_TAG,
9369 /*command*/ATA_SETFEATURES,
9375 /*timeout*/timeout ? timeout : 30 * 1000,
9383 scsigetopcodes(struct cam_device *device, int opcode_set, int opcode,
9384 int show_sa_errors, int sa_set, int service_action,
9385 int timeout_desc, int task_attr, int retry_count, int timeout,
9386 int verbosemode, uint32_t *fill_len, uint8_t **data_ptr)
9388 union ccb *ccb = NULL;
9389 uint8_t *buf = NULL;
9390 uint32_t alloc_len = 0, num_opcodes;
9391 uint32_t valid_len = 0;
9392 uint32_t avail_len = 0;
9393 struct scsi_report_supported_opcodes_all *all_hdr;
9394 struct scsi_report_supported_opcodes_one *one;
9399 * Make it clear that we haven't yet allocated or filled anything.
9404 ccb = cam_getccb(device);
9406 warnx("couldn't allocate CCB");
9411 if (opcode_set != 0) {
9412 options |= RSO_OPTIONS_OC;
9414 alloc_len = sizeof(*one) + CAM_MAX_CDBLEN;
9417 alloc_len = sizeof(*all_hdr) + (num_opcodes *
9418 sizeof(struct scsi_report_supported_opcodes_descr));
9421 if (timeout_desc != 0) {
9422 options |= RSO_RCTD;
9423 alloc_len += num_opcodes *
9424 sizeof(struct scsi_report_supported_opcodes_timeout);
9428 options |= RSO_OPTIONS_OC_SA;
9429 if (show_sa_errors != 0)
9430 options &= ~RSO_OPTIONS_OC;
9439 buf = malloc(alloc_len);
9441 warn("Unable to allocate %u bytes", alloc_len);
9445 bzero(buf, alloc_len);
9447 scsi_report_supported_opcodes(&ccb->csio,
9448 /*retries*/ retry_count,
9450 /*tag_action*/ task_attr,
9451 /*options*/ options,
9452 /*req_opcode*/ opcode,
9453 /*req_service_action*/ service_action,
9455 /*dxfer_len*/ alloc_len,
9456 /*sense_len*/ SSD_FULL_SIZE,
9457 /*timeout*/ timeout ? timeout : 10000);
9459 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
9461 if (retry_count != 0)
9462 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
9464 if (cam_send_ccb(device, ccb) < 0) {
9465 warn("error sending REPORT SUPPORTED OPERATION CODES command");
9470 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
9471 if (verbosemode != 0)
9472 cam_error_print(device, ccb, CAM_ESF_ALL,
9473 CAM_EPF_ALL, stderr);
9478 valid_len = ccb->csio.dxfer_len - ccb->csio.resid;
9480 if (((options & RSO_OPTIONS_MASK) == RSO_OPTIONS_ALL)
9481 && (valid_len >= sizeof(*all_hdr))) {
9482 all_hdr = (struct scsi_report_supported_opcodes_all *)buf;
9483 avail_len = scsi_4btoul(all_hdr->length) + sizeof(*all_hdr);
9484 } else if (((options & RSO_OPTIONS_MASK) != RSO_OPTIONS_ALL)
9485 && (valid_len >= sizeof(*one))) {
9486 uint32_t cdb_length;
9488 one = (struct scsi_report_supported_opcodes_one *)buf;
9489 cdb_length = scsi_2btoul(one->cdb_length);
9490 avail_len = sizeof(*one) + cdb_length;
9491 if (one->support & RSO_ONE_CTDP) {
9492 struct scsi_report_supported_opcodes_timeout *td;
9494 td = (struct scsi_report_supported_opcodes_timeout *)
9496 if (valid_len >= (avail_len + sizeof(td->length))) {
9497 avail_len += scsi_2btoul(td->length) +
9500 avail_len += sizeof(*td);
9506 * avail_len could be zero if we didn't get enough data back from
9507 * thet target to determine
9509 if ((avail_len != 0)
9510 && (avail_len > valid_len)) {
9511 alloc_len = avail_len;
9515 *fill_len = valid_len;
9527 scsiprintoneopcode(struct cam_device *device, int req_opcode, int sa_set,
9528 int req_sa, uint8_t *buf, uint32_t valid_len)
9530 struct scsi_report_supported_opcodes_one *one;
9531 struct scsi_report_supported_opcodes_timeout *td;
9532 uint32_t cdb_len = 0, td_len = 0;
9533 const char *op_desc = NULL;
9537 one = (struct scsi_report_supported_opcodes_one *)buf;
9540 * If we don't have the full single opcode descriptor, no point in
9543 if (valid_len < __offsetof(struct scsi_report_supported_opcodes_one,
9545 warnx("Only %u bytes returned, not enough to verify support",
9551 op_desc = scsi_op_desc(req_opcode, &device->inq_data);
9553 printf("%s (0x%02x)", op_desc != NULL ? op_desc : "UNKNOWN",
9556 printf(", SA 0x%x", req_sa);
9559 switch (one->support & RSO_ONE_SUP_MASK) {
9560 case RSO_ONE_SUP_UNAVAIL:
9561 printf("No command support information currently available\n");
9563 case RSO_ONE_SUP_NOT_SUP:
9564 printf("Command not supported\n");
9567 break; /*NOTREACHED*/
9568 case RSO_ONE_SUP_AVAIL:
9569 printf("Command is supported, complies with a SCSI standard\n");
9571 case RSO_ONE_SUP_VENDOR:
9572 printf("Command is supported, vendor-specific "
9573 "implementation\n");
9576 printf("Unknown command support flags 0x%#x\n",
9577 one->support & RSO_ONE_SUP_MASK);
9582 * If we don't have the CDB length, it isn't exactly an error, the
9583 * command probably isn't supported.
9585 if (valid_len < __offsetof(struct scsi_report_supported_opcodes_one,
9589 cdb_len = scsi_2btoul(one->cdb_length);
9592 * If our valid data doesn't include the full reported length,
9593 * return. The caller should have detected this and adjusted his
9594 * allocation length to get all of the available data.
9596 if (valid_len < sizeof(*one) + cdb_len) {
9602 * If all we have is the opcode, there is no point in printing out
9610 printf("CDB usage bitmap:");
9611 for (i = 0; i < cdb_len; i++) {
9612 printf(" %02x", one->cdb_usage[i]);
9617 * If we don't have a timeout descriptor, we're done.
9619 if ((one->support & RSO_ONE_CTDP) == 0)
9623 * If we don't have enough valid length to include the timeout
9624 * descriptor length, we're done.
9626 if (valid_len < (sizeof(*one) + cdb_len + sizeof(td->length)))
9629 td = (struct scsi_report_supported_opcodes_timeout *)
9630 &buf[sizeof(*one) + cdb_len];
9631 td_len = scsi_2btoul(td->length);
9632 td_len += sizeof(td->length);
9635 * If we don't have the full timeout descriptor, we're done.
9637 if (td_len < sizeof(*td))
9641 * If we don't have enough valid length to contain the full timeout
9642 * descriptor, we're done.
9644 if (valid_len < (sizeof(*one) + cdb_len + td_len))
9647 printf("Timeout information:\n");
9648 printf("Command-specific: 0x%02x\n", td->cmd_specific);
9649 printf("Nominal timeout: %u seconds\n",
9650 scsi_4btoul(td->nominal_time));
9651 printf("Recommended timeout: %u seconds\n",
9652 scsi_4btoul(td->recommended_time));
9659 scsiprintopcodes(struct cam_device *device, int td_req, uint8_t *buf,
9662 struct scsi_report_supported_opcodes_all *hdr;
9663 struct scsi_report_supported_opcodes_descr *desc;
9664 uint32_t avail_len = 0, used_len = 0;
9668 if (valid_len < sizeof(*hdr)) {
9669 warnx("%s: not enough returned data (%u bytes) opcode list",
9670 __func__, valid_len);
9674 hdr = (struct scsi_report_supported_opcodes_all *)buf;
9675 avail_len = scsi_4btoul(hdr->length);
9676 avail_len += sizeof(hdr->length);
9678 * Take the lesser of the amount of data the drive claims is
9679 * available, and the amount of data the HBA says was returned.
9681 avail_len = MIN(avail_len, valid_len);
9683 used_len = sizeof(hdr->length);
9685 printf("%-6s %4s %8s ",
9686 "Opcode", "SA", "CDB len" );
9689 printf("%5s %6s %6s ", "CS", "Nom", "Rec");
9690 printf(" Description\n");
9692 while ((avail_len - used_len) > sizeof(*desc)) {
9693 struct scsi_report_supported_opcodes_timeout *td;
9695 const char *op_desc = NULL;
9697 cur_ptr = &buf[used_len];
9698 desc = (struct scsi_report_supported_opcodes_descr *)cur_ptr;
9700 op_desc = scsi_op_desc(desc->opcode, &device->inq_data);
9701 if (op_desc == NULL)
9702 op_desc = "UNKNOWN";
9704 printf("0x%02x %#4x %8u ", desc->opcode,
9705 scsi_2btoul(desc->service_action),
9706 scsi_2btoul(desc->cdb_length));
9708 used_len += sizeof(*desc);
9710 if ((desc->flags & RSO_CTDP) == 0) {
9711 printf(" %s\n", op_desc);
9716 * If we don't have enough space to fit a timeout
9717 * descriptor, then we're done.
9719 if (avail_len - used_len < sizeof(*td)) {
9720 used_len = avail_len;
9721 printf(" %s\n", op_desc);
9724 cur_ptr = &buf[used_len];
9725 td = (struct scsi_report_supported_opcodes_timeout *)cur_ptr;
9726 td_len = scsi_2btoul(td->length);
9727 td_len += sizeof(td->length);
9731 * If the given timeout descriptor length is less than what
9732 * we understand, skip it.
9734 if (td_len < sizeof(*td)) {
9735 printf(" %s\n", op_desc);
9739 printf(" 0x%02x %6u %6u %s\n", td->cmd_specific,
9740 scsi_4btoul(td->nominal_time),
9741 scsi_4btoul(td->recommended_time), op_desc);
9748 scsiopcodes(struct cam_device *device, int argc, char **argv,
9749 char *combinedopt, int task_attr, int retry_count, int timeout,
9753 uint32_t opcode = 0, service_action = 0;
9754 int td_set = 0, opcode_set = 0, sa_set = 0;
9755 int show_sa_errors = 1;
9756 uint32_t valid_len = 0;
9757 uint8_t *buf = NULL;
9761 while ((c = getopt(argc, argv, combinedopt)) != -1) {
9767 opcode = strtoul(optarg, &endptr, 0);
9768 if (*endptr != '\0') {
9769 warnx("Invalid opcode \"%s\", must be a number",
9774 if (opcode > 0xff) {
9775 warnx("Invalid opcode 0x%#x, must be between"
9776 "0 and 0xff inclusive", opcode);
9783 service_action = strtoul(optarg, &endptr, 0);
9784 if (*endptr != '\0') {
9785 warnx("Invalid service action \"%s\", must "
9786 "be a number", optarg);
9790 if (service_action > 0xffff) {
9791 warnx("Invalid service action 0x%#x, must "
9792 "be between 0 and 0xffff inclusive",
9807 && (opcode_set == 0)) {
9808 warnx("You must specify an opcode with -o if a service "
9813 retval = scsigetopcodes(device, opcode_set, opcode, show_sa_errors,
9814 sa_set, service_action, td_set, task_attr,
9815 retry_count, timeout, verbosemode, &valid_len,
9820 if ((opcode_set != 0)
9822 retval = scsiprintoneopcode(device, opcode, sa_set,
9823 service_action, buf, valid_len);
9825 retval = scsiprintopcodes(device, td_set, buf, valid_len);
9836 reprobe(struct cam_device *device)
9841 ccb = cam_getccb(device);
9844 warnx("%s: error allocating ccb", __func__);
9848 ccb->ccb_h.func_code = XPT_REPROBE_LUN;
9850 if (cam_send_ccb(device, ccb) < 0) {
9851 warn("error sending XPT_REPROBE_LUN CCB");
9856 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
9857 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
9869 usage(int printlong)
9872 fprintf(printlong ? stdout : stderr,
9873 "usage: camcontrol <command> [device id][generic args][command args]\n"
9874 " camcontrol devlist [-b] [-v]\n"
9875 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n"
9876 " camcontrol tur [dev_id][generic args]\n"
9877 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n"
9878 " camcontrol identify [dev_id][generic args] [-v]\n"
9879 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n"
9880 " camcontrol readcap [dev_id][generic args] [-b] [-h] [-H] [-N]\n"
9882 " camcontrol start [dev_id][generic args]\n"
9883 " camcontrol stop [dev_id][generic args]\n"
9884 " camcontrol load [dev_id][generic args]\n"
9885 " camcontrol eject [dev_id][generic args]\n"
9886 " camcontrol reprobe [dev_id][generic args]\n"
9887 " camcontrol rescan <all | bus[:target:lun] | dev_id>\n"
9888 " camcontrol reset <all | bus[:target:lun] | dev_id>\n"
9889 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n"
9890 " [-q][-s][-S offset][-X]\n"
9891 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n"
9892 " [-P pagectl][-e | -b][-d]\n"
9893 " camcontrol cmd [dev_id][generic args]\n"
9894 " <-a cmd [args] | -c cmd [args]>\n"
9895 " [-d] [-f] [-i len fmt|-o len fmt [args]] [-r fmt]\n"
9896 " camcontrol smpcmd [dev_id][generic args]\n"
9897 " <-r len fmt [args]> <-R len fmt [args]>\n"
9898 " camcontrol smprg [dev_id][generic args][-l]\n"
9899 " camcontrol smppc [dev_id][generic args] <-p phy> [-l]\n"
9900 " [-o operation][-d name][-m rate][-M rate]\n"
9901 " [-T pp_timeout][-a enable|disable]\n"
9902 " [-A enable|disable][-s enable|disable]\n"
9903 " [-S enable|disable]\n"
9904 " camcontrol smpphylist [dev_id][generic args][-l][-q]\n"
9905 " camcontrol smpmaninfo [dev_id][generic args][-l]\n"
9906 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n"
9907 " <all|dev_id|bus[:target[:lun]]|off>\n"
9908 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n"
9909 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n"
9910 " [-D <enable|disable>][-M mode][-O offset]\n"
9911 " [-q][-R syncrate][-v][-T <enable|disable>]\n"
9912 " [-U][-W bus_width]\n"
9913 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n"
9914 " camcontrol sanitize [dev_id][generic args]\n"
9915 " [-a overwrite|block|crypto|exitfailure]\n"
9916 " [-c passes][-I][-P pattern][-q][-U][-r][-w]\n"
9918 " camcontrol idle [dev_id][generic args][-t time]\n"
9919 " camcontrol standby [dev_id][generic args][-t time]\n"
9920 " camcontrol sleep [dev_id][generic args]\n"
9921 " camcontrol powermode [dev_id][generic args]\n"
9922 " camcontrol apm [dev_id][generic args][-l level]\n"
9923 " camcontrol aam [dev_id][generic args][-l level]\n"
9924 " camcontrol fwdownload [dev_id][generic args] <-f fw_image> [-q]\n"
9926 " camcontrol security [dev_id][generic args]\n"
9927 " <-d pwd | -e pwd | -f | -h pwd | -k pwd>\n"
9928 " [-l <high|maximum>] [-q] [-s pwd] [-T timeout]\n"
9929 " [-U <user|master>] [-y]\n"
9930 " camcontrol hpa [dev_id][generic args] [-f] [-l] [-P] [-p pwd]\n"
9931 " [-q] [-s max_sectors] [-U pwd] [-y]\n"
9932 " camcontrol ama [dev_id][generic args] [-f] [-q] [-s max_sectors]\n"
9933 " camcontrol persist [dev_id][generic args] <-i action|-o action>\n"
9934 " [-a][-I tid][-k key][-K sa_key][-p][-R rtp]\n"
9935 " [-s scope][-S][-T type][-U]\n"
9936 " camcontrol attrib [dev_id][generic args] <-r action|-w attr>\n"
9937 " [-a attr_num][-c][-e elem][-F form1,form1]\n"
9938 " [-p part][-s start][-T type][-V vol]\n"
9939 " camcontrol opcodes [dev_id][generic args][-o opcode][-s SA]\n"
9941 " camcontrol zone [dev_id][generic args]<-c cmd> [-a] [-l LBA]\n"
9942 " [-o rep_opts] [-P print_opts]\n"
9943 " camcontrol epc [dev_id][generic_args]<-c cmd> [-d] [-D] [-e]\n"
9944 " [-H] [-p power_cond] [-P] [-r rst_src] [-s]\n"
9945 " [-S power_src] [-T timer]\n"
9946 " camcontrol timestamp [dev_id][generic_args] <-r [-f format|-m|-U]>|\n"
9947 " <-s <-f format -T time | -U >>\n"
9948 " camcontrol devtype [dev_id]\n"
9949 " camcontrol mmcsdcmd [dev_id] [[-c mmc_opcode] [-a mmc_arg]\n"
9950 " [-f mmc_flags] [-l data_len]\n"
9951 " [-W [-b data_byte]]] |\n"
9952 " [-F frequency] |\n"
9955 " [-S high|normal]\n"
9957 " camcontrol help\n");
9961 "Specify one of the following options:\n"
9962 "devlist list all CAM devices\n"
9963 "periphlist list all CAM peripheral drivers attached to a device\n"
9964 "tur send a test unit ready to the named device\n"
9965 "inquiry send a SCSI inquiry command to the named device\n"
9966 "identify send a ATA identify command to the named device\n"
9967 "reportluns send a SCSI report luns command to the device\n"
9968 "readcap send a SCSI read capacity command to the device\n"
9969 "start send a Start Unit command to the device\n"
9970 "stop send a Stop Unit command to the device\n"
9971 "load send a Start Unit command to the device with the load bit set\n"
9972 "eject send a Stop Unit command to the device with the eject bit set\n"
9973 "reprobe update capacity information of the given device\n"
9974 "rescan rescan all buses, the given bus, bus:target:lun or device\n"
9975 "reset reset all buses, the given bus, bus:target:lun or device\n"
9976 "defects read the defect list of the specified device\n"
9977 "modepage display or edit (-e) the given mode page\n"
9978 "cmd send the given SCSI command, may need -i or -o as well\n"
9979 "smpcmd send the given SMP command, requires -o and -i\n"
9980 "smprg send the SMP Report General command\n"
9981 "smppc send the SMP PHY Control command, requires -p\n"
9982 "smpphylist display phys attached to a SAS expander\n"
9983 "smpmaninfo send the SMP Report Manufacturer Info command\n"
9984 "debug turn debugging on/off for a bus, target, or lun, or all devices\n"
9985 "tags report or set the number of transaction slots for a device\n"
9986 "negotiate report or set device negotiation parameters\n"
9987 "format send the SCSI FORMAT UNIT command to the named device\n"
9988 "sanitize send the SCSI SANITIZE command to the named device\n"
9989 "idle send the ATA IDLE command to the named device\n"
9990 "standby send the ATA STANDBY command to the named device\n"
9991 "sleep send the ATA SLEEP command to the named device\n"
9992 "powermode send the ATA CHECK POWER MODE command to the named device\n"
9993 "fwdownload program firmware of the named device with the given image\n"
9994 "security report or send ATA security commands to the named device\n"
9995 "persist send the SCSI PERSISTENT RESERVE IN or OUT commands\n"
9996 "attrib send the SCSI READ or WRITE ATTRIBUTE commands\n"
9997 "opcodes send the SCSI REPORT SUPPORTED OPCODES command\n"
9998 "zone manage Zoned Block (Shingled) devices\n"
9999 "epc send ATA Extended Power Conditions commands\n"
10000 "timestamp report or set the device's timestamp\n"
10001 "devtype report the type of device\n"
10002 "mmcsdcmd send the given MMC command, needs -c and -a as well\n"
10003 "help this message\n"
10004 "Device Identifiers:\n"
10005 "bus:target specify the bus and target, lun defaults to 0\n"
10006 "bus:target:lun specify the bus, target and lun\n"
10007 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n"
10008 "Generic arguments:\n"
10009 "-v be verbose, print out sense information\n"
10010 "-t timeout command timeout in seconds, overrides default timeout\n"
10011 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n"
10012 "-u unit specify unit number, e.g. \"0\", \"5\"\n"
10013 "-E have the kernel attempt to perform SCSI error recovery\n"
10014 "-C count specify the SCSI command retry count (needs -E to work)\n"
10015 "-Q task_attr specify ordered, simple or head tag type for SCSI cmds\n"
10016 "modepage arguments:\n"
10017 "-l list all available mode pages\n"
10018 "-m page specify the mode page to view or edit\n"
10019 "-e edit the specified mode page\n"
10020 "-b force view to binary mode\n"
10021 "-d disable block descriptors for mode sense\n"
10022 "-P pgctl page control field 0-3\n"
10023 "defects arguments:\n"
10024 "-f format specify defect list format (block, bfi or phys)\n"
10025 "-G get the grown defect list\n"
10026 "-P get the permanent defect list\n"
10027 "inquiry arguments:\n"
10028 "-D get the standard inquiry data\n"
10029 "-S get the serial number\n"
10030 "-R get the transfer rate, etc.\n"
10031 "reportluns arguments:\n"
10032 "-c only report a count of available LUNs\n"
10033 "-l only print out luns, and not a count\n"
10034 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n"
10035 "readcap arguments\n"
10036 "-b only report the blocksize\n"
10037 "-h human readable device size, base 2\n"
10038 "-H human readable device size, base 10\n"
10039 "-N print the number of blocks instead of last block\n"
10040 "-q quiet, print numbers only\n"
10041 "-s only report the last block/device size\n"
10043 "-c cdb [args] specify the SCSI CDB\n"
10044 "-i len fmt specify input data and input data format\n"
10045 "-o len fmt [args] specify output data and output data fmt\n"
10046 "smpcmd arguments:\n"
10047 "-r len fmt [args] specify the SMP command to be sent\n"
10048 "-R len fmt [args] specify SMP response format\n"
10049 "smprg arguments:\n"
10050 "-l specify the long response format\n"
10051 "smppc arguments:\n"
10052 "-p phy specify the PHY to operate on\n"
10053 "-l specify the long request/response format\n"
10054 "-o operation specify the phy control operation\n"
10055 "-d name set the attached device name\n"
10056 "-m rate set the minimum physical link rate\n"
10057 "-M rate set the maximum physical link rate\n"
10058 "-T pp_timeout set the partial pathway timeout value\n"
10059 "-a enable|disable enable or disable SATA slumber\n"
10060 "-A enable|disable enable or disable SATA partial phy power\n"
10061 "-s enable|disable enable or disable SAS slumber\n"
10062 "-S enable|disable enable or disable SAS partial phy power\n"
10063 "smpphylist arguments:\n"
10064 "-l specify the long response format\n"
10065 "-q only print phys with attached devices\n"
10066 "smpmaninfo arguments:\n"
10067 "-l specify the long response format\n"
10068 "debug arguments:\n"
10069 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n"
10070 "-T CAM_DEBUG_TRACE -- routine flow tracking\n"
10071 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n"
10072 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n"
10073 "tags arguments:\n"
10074 "-N tags specify the number of tags to use for this device\n"
10075 "-q be quiet, don't report the number of tags\n"
10076 "-v report a number of tag-related parameters\n"
10077 "negotiate arguments:\n"
10078 "-a send a test unit ready after negotiation\n"
10079 "-c report/set current negotiation settings\n"
10080 "-D <arg> \"enable\" or \"disable\" disconnection\n"
10081 "-M mode set ATA mode\n"
10082 "-O offset set command delay offset\n"
10083 "-q be quiet, don't report anything\n"
10084 "-R syncrate synchronization rate in MHz\n"
10085 "-T <arg> \"enable\" or \"disable\" tagged queueing\n"
10086 "-U report/set user negotiation settings\n"
10087 "-W bus_width set the bus width in bits (8, 16 or 32)\n"
10088 "-v also print a Path Inquiry CCB for the controller\n"
10089 "format arguments:\n"
10090 "-q be quiet, don't print status messages\n"
10091 "-r run in report only mode\n"
10092 "-w don't send immediate format command\n"
10093 "-y don't ask any questions\n"
10094 "sanitize arguments:\n"
10095 "-a operation operation mode: overwrite, block, crypto or exitfailure\n"
10096 "-c passes overwrite passes to perform (1 to 31)\n"
10097 "-I invert overwrite pattern after each pass\n"
10098 "-P pattern path to overwrite pattern file\n"
10099 "-q be quiet, don't print status messages\n"
10100 "-r run in report only mode\n"
10101 "-U run operation in unrestricted completion exit mode\n"
10102 "-w don't send immediate sanitize command\n"
10103 "-y don't ask any questions\n"
10104 "idle/standby arguments:\n"
10105 "-t <arg> number of seconds before respective state.\n"
10106 "fwdownload arguments:\n"
10107 "-f fw_image path to firmware image file\n"
10108 "-q don't print informational messages, only errors\n"
10109 "-s run in simulation mode\n"
10110 "-v print info for every firmware segment sent to device\n"
10111 "-y don't ask any questions\n"
10112 "security arguments:\n"
10113 "-d pwd disable security using the given password for the selected\n"
10115 "-e pwd erase the device using the given pwd for the selected user\n"
10116 "-f freeze the security configuration of the specified device\n"
10117 "-h pwd enhanced erase the device using the given pwd for the\n"
10119 "-k pwd unlock the device using the given pwd for the selected\n"
10121 "-l <high|maximum> specifies which security level to set: high or maximum\n"
10122 "-q be quiet, do not print any status messages\n"
10123 "-s pwd password the device (enable security) using the given\n"
10124 " pwd for the selected user\n"
10125 "-T timeout overrides the timeout (seconds) used for erase operation\n"
10126 "-U <user|master> specifies which user to set: user or master\n"
10127 "-y don't ask any questions\n"
10129 "-f freeze the HPA configuration of the device\n"
10130 "-l lock the HPA configuration of the device\n"
10131 "-P make the HPA max sectors persist\n"
10132 "-p pwd Set the HPA configuration password required for unlock\n"
10134 "-q be quiet, do not print any status messages\n"
10135 "-s sectors configures the maximum user accessible sectors of the\n"
10137 "-U pwd unlock the HPA configuration of the device\n"
10138 "-y don't ask any questions\n"
10140 "-f freeze the AMA configuration of the device\n"
10141 "-q be quiet, do not print any status messages\n"
10142 "-s sectors configures the maximum user accessible sectors of the\n"
10144 "persist arguments:\n"
10145 "-i action specify read_keys, read_reservation, report_cap, or\n"
10146 " read_full_status\n"
10147 "-o action specify register, register_ignore, reserve, release,\n"
10148 " clear, preempt, preempt_abort, register_move, replace_lost\n"
10149 "-a set the All Target Ports (ALL_TG_PT) bit\n"
10150 "-I tid specify a Transport ID, e.g.: sas,0x1234567812345678\n"
10151 "-k key specify the Reservation Key\n"
10152 "-K sa_key specify the Service Action Reservation Key\n"
10153 "-p set the Activate Persist Through Power Loss bit\n"
10154 "-R rtp specify the Relative Target Port\n"
10155 "-s scope specify the scope: lun, extent, element or a number\n"
10156 "-S specify Transport ID for register, requires -I\n"
10157 "-T res_type specify the reservation type: read_shared, wr_ex, rd_ex,\n"
10158 " ex_ac, wr_ex_ro, ex_ac_ro, wr_ex_ar, ex_ac_ar\n"
10159 "-U unregister the current initiator for register_move\n"
10160 "attrib arguments:\n"
10161 "-r action specify attr_values, attr_list, lv_list, part_list, or\n"
10163 "-w attr specify an attribute to write, one -w argument per attr\n"
10164 "-a attr_num only display this attribute number\n"
10165 "-c get cached attributes\n"
10166 "-e elem_addr request attributes for the given element in a changer\n"
10167 "-F form1,form2 output format, comma separated list: text_esc, text_raw,\n"
10168 " nonascii_esc, nonascii_trim, nonascii_raw, field_all,\n"
10169 " field_none, field_desc, field_num, field_size, field_rw\n"
10170 "-p partition request attributes for the given partition\n"
10171 "-s start_attr request attributes starting at the given number\n"
10172 "-T elem_type specify the element type (used with -e)\n"
10173 "-V logical_vol specify the logical volume ID\n"
10174 "opcodes arguments:\n"
10175 "-o opcode specify the individual opcode to list\n"
10176 "-s service_action specify the service action for the opcode\n"
10177 "-N do not return SCSI error for unsupported SA\n"
10178 "-T request nominal and recommended timeout values\n"
10179 "zone arguments:\n"
10180 "-c cmd required: rz, open, close, finish, or rwp\n"
10181 "-a apply the action to all zones\n"
10182 "-l LBA specify the zone starting LBA\n"
10183 "-o rep_opts report zones options: all, empty, imp_open, exp_open,\n"
10184 " closed, full, ro, offline, reset, nonseq, nonwp\n"
10185 "-P print_opt report zones printing: normal, summary, script\n"
10187 "-c cmd required: restore, goto, timer, state, enable, disable,\n"
10188 " source, status, list\n"
10189 "-d disable power mode (timer, state)\n"
10190 "-D delayed entry (goto)\n"
10191 "-e enable power mode (timer, state)\n"
10192 "-H hold power mode (goto)\n"
10193 "-p power_cond Idle_a, Idle_b, Idle_c, Standby_y, Standby_z (timer,\n"
10195 "-P only display power mode (status)\n"
10196 "-r rst_src restore settings from: default, saved (restore)\n"
10197 "-s save mode (timer, state, restore)\n"
10198 "-S power_src set power source: battery, nonbattery (source)\n"
10199 "-T timer set timer, seconds, .1 sec resolution (timer)\n"
10200 "timestamp arguments:\n"
10201 "-r report the timestamp of the device\n"
10202 "-f format report the timestamp of the device with the given\n"
10203 " strftime(3) format string\n"
10204 "-m report the timestamp of the device as milliseconds since\n"
10205 " January 1st, 1970\n"
10206 "-U report the time with UTC instead of the local time zone\n"
10207 "-s set the timestamp of the device\n"
10208 "-f format the format of the time string passed into strptime(3)\n"
10209 "-T time the time value passed into strptime(3)\n"
10210 "-U set the timestamp of the device to UTC time\n"
10211 "mmcsdcmd arguments:\n"
10212 "-c mmc_cmd MMC command to send to the card\n"
10213 "-a mmc_arg Argument for the MMC command\n"
10214 "-f mmc_flag Flags to set for the MMC command\n"
10215 "-l data_len Expect data_len bytes of data in reply and display them\n"
10216 "-W Fill the data buffer before invoking the MMC command\n"
10217 "-b data_byte One byte of data to fill the data buffer with\n"
10218 "-F frequency Operating frequency to set on the controller\n"
10219 "-4 Set bus width to 4 bit\n"
10220 "-1 Set bus width to 8 bit\n"
10221 "-S high | std Set high-speed or standard timing\n"
10222 "-I Display various card and host controller information\n"
10227 main(int argc, char **argv)
10230 char *device = NULL;
10232 struct cam_device *cam_dev = NULL;
10233 int timeout = 0, retry_count = 1;
10234 camcontrol_optret optreturn;
10236 const char *mainopt = "C:En:Q:t:u:v";
10237 const char *subopt = NULL;
10238 char combinedopt[256];
10239 int error = 0, optstart = 2;
10240 int task_attr = MSG_SIMPLE_Q_TAG;
10244 target_id_t target;
10247 cmdlist = CAM_CMD_NONE;
10248 arglist = CAM_ARG_NONE;
10256 * Get the base option.
10258 optreturn = getoption(option_table,argv[1], &cmdlist, &arglist,&subopt);
10260 if (optreturn == CC_OR_AMBIGUOUS) {
10261 warnx("ambiguous option %s", argv[1]);
10264 } else if (optreturn == CC_OR_NOT_FOUND) {
10265 warnx("option %s not found", argv[1]);
10271 * Ahh, getopt(3) is a pain.
10273 * This is a gross hack. There really aren't many other good
10274 * options (excuse the pun) for parsing options in a situation like
10275 * this. getopt is kinda braindead, so you end up having to run
10276 * through the options twice, and give each invocation of getopt
10277 * the option string for the other invocation.
10279 * You would think that you could just have two groups of options.
10280 * The first group would get parsed by the first invocation of
10281 * getopt, and the second group would get parsed by the second
10282 * invocation of getopt. It doesn't quite work out that way. When
10283 * the first invocation of getopt finishes, it leaves optind pointing
10284 * to the argument _after_ the first argument in the second group.
10285 * So when the second invocation of getopt comes around, it doesn't
10286 * recognize the first argument it gets and then bails out.
10288 * A nice alternative would be to have a flag for getopt that says
10289 * "just keep parsing arguments even when you encounter an unknown
10290 * argument", but there isn't one. So there's no real clean way to
10291 * easily parse two sets of arguments without having one invocation
10292 * of getopt know about the other.
10294 * Without this hack, the first invocation of getopt would work as
10295 * long as the generic arguments are first, but the second invocation
10296 * (in the subfunction) would fail in one of two ways. In the case
10297 * where you don't set optreset, it would fail because optind may be
10298 * pointing to the argument after the one it should be pointing at.
10299 * In the case where you do set optreset, and reset optind, it would
10300 * fail because getopt would run into the first set of options, which
10301 * it doesn't understand.
10303 * All of this would "sort of" work if you could somehow figure out
10304 * whether optind had been incremented one option too far. The
10305 * mechanics of that, however, are more daunting than just giving
10306 * both invocations all of the expect options for either invocation.
10308 * Needless to say, I wouldn't mind if someone invented a better
10309 * (non-GPL!) command line parsing interface than getopt. I
10310 * wouldn't mind if someone added more knobs to getopt to make it
10311 * work better. Who knows, I may talk myself into doing it someday,
10312 * if the standards weenies let me. As it is, it just leads to
10313 * hackery like this and causes people to avoid it in some cases.
10315 * KDM, September 8th, 1998
10317 if (subopt != NULL)
10318 sprintf(combinedopt, "%s%s", mainopt, subopt);
10320 sprintf(combinedopt, "%s", mainopt);
10323 * For these options we do not parse optional device arguments and
10324 * we do not open a passthrough device.
10326 if ((cmdlist == CAM_CMD_RESCAN)
10327 || (cmdlist == CAM_CMD_RESET)
10328 || (cmdlist == CAM_CMD_DEVTREE)
10329 || (cmdlist == CAM_CMD_USAGE)
10330 || (cmdlist == CAM_CMD_DEBUG))
10334 && (argc > 2 && argv[2][0] != '-')) {
10338 if (isdigit(argv[2][0])) {
10339 /* device specified as bus:target[:lun] */
10340 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist);
10342 errx(1, "numeric device specification must "
10343 "be either bus:target, or "
10345 /* default to 0 if lun was not specified */
10346 if ((arglist & CAM_ARG_LUN) == 0) {
10348 arglist |= CAM_ARG_LUN;
10352 if (cam_get_device(argv[2], name, sizeof name, &unit)
10354 errx(1, "%s", cam_errbuf);
10355 device = strdup(name);
10356 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT;
10361 * Start getopt processing at argv[2/3], since we've already
10362 * accepted argv[1..2] as the command name, and as a possible
10368 * Now we run through the argument list looking for generic
10369 * options, and ignoring options that possibly belong to
10372 while ((c = getopt(argc, argv, combinedopt))!= -1){
10375 retry_count = strtol(optarg, NULL, 0);
10376 if (retry_count < 0)
10377 errx(1, "retry count %d is < 0",
10379 arglist |= CAM_ARG_RETRIES;
10382 arglist |= CAM_ARG_ERR_RECOVER;
10385 arglist |= CAM_ARG_DEVICE;
10387 while (isspace(*tstr) && (*tstr != '\0'))
10389 device = (char *)strdup(tstr);
10393 int table_entry = 0;
10396 while (isspace(*tstr) && (*tstr != '\0'))
10398 if (isdigit(*tstr)) {
10399 task_attr = strtol(tstr, &endptr, 0);
10400 if (*endptr != '\0') {
10401 errx(1, "Invalid queue option "
10406 scsi_nv_status status;
10408 table_size = sizeof(task_attrs) /
10409 sizeof(task_attrs[0]);
10410 status = scsi_get_nv(task_attrs,
10411 table_size, tstr, &table_entry,
10412 SCSI_NV_FLAG_IG_CASE);
10413 if (status == SCSI_NV_FOUND)
10414 task_attr = task_attrs[
10415 table_entry].value;
10417 errx(1, "%s option %s",
10418 (status == SCSI_NV_AMBIGUOUS)?
10419 "ambiguous" : "invalid",
10426 timeout = strtol(optarg, NULL, 0);
10428 errx(1, "invalid timeout %d", timeout);
10429 /* Convert the timeout from seconds to ms */
10431 arglist |= CAM_ARG_TIMEOUT;
10434 arglist |= CAM_ARG_UNIT;
10435 unit = strtol(optarg, NULL, 0);
10438 arglist |= CAM_ARG_VERBOSE;
10446 * For most commands we'll want to open the passthrough device
10447 * associated with the specified device. In the case of the rescan
10448 * commands, we don't use a passthrough device at all, just the
10449 * transport layer device.
10451 if (devopen == 1) {
10452 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0)
10453 && (((arglist & CAM_ARG_DEVICE) == 0)
10454 || ((arglist & CAM_ARG_UNIT) == 0))) {
10455 errx(1, "subcommand \"%s\" requires a valid device "
10456 "identifier", argv[1]);
10459 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))?
10460 cam_open_btl(bus, target, lun, O_RDWR, NULL) :
10461 cam_open_spec_device(device,unit,O_RDWR,NULL)))
10463 errx(1,"%s", cam_errbuf);
10467 * Reset optind to 2, and reset getopt, so these routines can parse
10468 * the arguments again.
10474 case CAM_CMD_DEVLIST:
10475 error = getdevlist(cam_dev);
10478 error = atahpa(cam_dev, retry_count, timeout,
10479 argc, argv, combinedopt);
10482 error = ataama(cam_dev, retry_count, timeout,
10483 argc, argv, combinedopt);
10485 case CAM_CMD_DEVTREE:
10486 error = getdevtree(argc, argv, combinedopt);
10488 case CAM_CMD_DEVTYPE:
10489 error = getdevtype(cam_dev);
10492 error = testunitready(cam_dev, task_attr, retry_count,
10495 case CAM_CMD_INQUIRY:
10496 error = scsidoinquiry(cam_dev, argc, argv, combinedopt,
10497 task_attr, retry_count, timeout);
10499 case CAM_CMD_IDENTIFY:
10500 error = identify(cam_dev, retry_count, timeout);
10502 case CAM_CMD_STARTSTOP:
10503 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT,
10504 arglist & CAM_ARG_EJECT, task_attr,
10505 retry_count, timeout);
10507 case CAM_CMD_RESCAN:
10508 error = dorescan_or_reset(argc, argv, 1);
10510 case CAM_CMD_RESET:
10511 error = dorescan_or_reset(argc, argv, 0);
10513 case CAM_CMD_READ_DEFECTS:
10514 error = readdefects(cam_dev, argc, argv, combinedopt,
10515 task_attr, retry_count, timeout);
10517 case CAM_CMD_MODE_PAGE:
10518 modepage(cam_dev, argc, argv, combinedopt,
10519 task_attr, retry_count, timeout);
10521 case CAM_CMD_SCSI_CMD:
10522 error = scsicmd(cam_dev, argc, argv, combinedopt,
10523 task_attr, retry_count, timeout);
10525 case CAM_CMD_MMCSD_CMD:
10526 error = mmcsdcmd(cam_dev, argc, argv, combinedopt,
10527 retry_count, timeout);
10529 case CAM_CMD_SMP_CMD:
10530 error = smpcmd(cam_dev, argc, argv, combinedopt,
10531 retry_count, timeout);
10533 case CAM_CMD_SMP_RG:
10534 error = smpreportgeneral(cam_dev, argc, argv,
10535 combinedopt, retry_count,
10538 case CAM_CMD_SMP_PC:
10539 error = smpphycontrol(cam_dev, argc, argv, combinedopt,
10540 retry_count, timeout);
10542 case CAM_CMD_SMP_PHYLIST:
10543 error = smpphylist(cam_dev, argc, argv, combinedopt,
10544 retry_count, timeout);
10546 case CAM_CMD_SMP_MANINFO:
10547 error = smpmaninfo(cam_dev, argc, argv, combinedopt,
10548 retry_count, timeout);
10550 case CAM_CMD_DEBUG:
10551 error = camdebug(argc, argv, combinedopt);
10554 error = tagcontrol(cam_dev, argc, argv, combinedopt);
10557 error = ratecontrol(cam_dev, task_attr, retry_count,
10558 timeout, argc, argv, combinedopt);
10560 case CAM_CMD_FORMAT:
10561 error = scsiformat(cam_dev, argc, argv,
10562 combinedopt, task_attr, retry_count,
10565 case CAM_CMD_REPORTLUNS:
10566 error = scsireportluns(cam_dev, argc, argv,
10567 combinedopt, task_attr,
10568 retry_count, timeout);
10570 case CAM_CMD_READCAP:
10571 error = scsireadcapacity(cam_dev, argc, argv,
10572 combinedopt, task_attr,
10573 retry_count, timeout);
10576 case CAM_CMD_STANDBY:
10577 case CAM_CMD_SLEEP:
10578 case CAM_CMD_POWER_MODE:
10579 error = atapm(cam_dev, argc, argv,
10580 combinedopt, retry_count, timeout);
10584 error = ataaxm(cam_dev, argc, argv,
10585 combinedopt, retry_count, timeout);
10587 case CAM_CMD_SECURITY:
10588 error = atasecurity(cam_dev, retry_count, timeout,
10589 argc, argv, combinedopt);
10591 case CAM_CMD_DOWNLOAD_FW:
10592 error = fwdownload(cam_dev, argc, argv, combinedopt,
10593 arglist & CAM_ARG_VERBOSE, task_attr, retry_count,
10596 case CAM_CMD_SANITIZE:
10597 error = sanitize(cam_dev, argc, argv, combinedopt, task_attr,
10598 retry_count, timeout);
10600 case CAM_CMD_PERSIST:
10601 error = scsipersist(cam_dev, argc, argv, combinedopt,
10602 task_attr, retry_count, timeout,
10603 arglist & CAM_ARG_VERBOSE,
10604 arglist & CAM_ARG_ERR_RECOVER);
10606 case CAM_CMD_ATTRIB:
10607 error = scsiattrib(cam_dev, argc, argv, combinedopt,
10608 task_attr, retry_count, timeout,
10609 arglist & CAM_ARG_VERBOSE,
10610 arglist & CAM_ARG_ERR_RECOVER);
10612 case CAM_CMD_OPCODES:
10613 error = scsiopcodes(cam_dev, argc, argv, combinedopt,
10614 task_attr, retry_count, timeout,
10615 arglist & CAM_ARG_VERBOSE);
10617 case CAM_CMD_REPROBE:
10618 error = reprobe(cam_dev);
10621 error = zone(cam_dev, argc, argv, combinedopt,
10622 task_attr, retry_count, timeout,
10623 arglist & CAM_ARG_VERBOSE);
10626 error = epc(cam_dev, argc, argv, combinedopt,
10627 retry_count, timeout, arglist & CAM_ARG_VERBOSE);
10629 case CAM_CMD_TIMESTAMP:
10630 error = timestamp(cam_dev, argc, argv, combinedopt,
10631 task_attr, retry_count, timeout,
10632 arglist & CAM_ARG_VERBOSE);
10634 case CAM_CMD_USAGE:
10643 if (cam_dev != NULL)
10644 cam_close_device(cam_dev);