1 /* 2 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006 Kenneth D. Merry 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/ioctl.h> 33 #include <sys/stdint.h> 34 #include <sys/types.h> 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <fcntl.h> 41 #include <ctype.h> 42 #include <err.h> 43 44 #include <cam/cam.h> 45 #include <cam/cam_debug.h> 46 #include <cam/cam_ccb.h> 47 #include <cam/scsi/scsi_all.h> 48 #include <cam/scsi/scsi_da.h> 49 #include <cam/scsi/scsi_pass.h> 50 #include <cam/scsi/scsi_message.h> 51 #include <camlib.h> 52 #include "camcontrol.h" 53 54 typedef enum { 55 CAM_CMD_NONE = 0x00000000, 56 CAM_CMD_DEVLIST = 0x00000001, 57 CAM_CMD_TUR = 0x00000002, 58 CAM_CMD_INQUIRY = 0x00000003, 59 CAM_CMD_STARTSTOP = 0x00000004, 60 CAM_CMD_RESCAN = 0x00000005, 61 CAM_CMD_READ_DEFECTS = 0x00000006, 62 CAM_CMD_MODE_PAGE = 0x00000007, 63 CAM_CMD_SCSI_CMD = 0x00000008, 64 CAM_CMD_DEVTREE = 0x00000009, 65 CAM_CMD_USAGE = 0x0000000a, 66 CAM_CMD_DEBUG = 0x0000000b, 67 CAM_CMD_RESET = 0x0000000c, 68 CAM_CMD_FORMAT = 0x0000000d, 69 CAM_CMD_TAG = 0x0000000e, 70 CAM_CMD_RATE = 0x0000000f, 71 CAM_CMD_DETACH = 0x00000010, 72 CAM_CMD_REPORTLUNS = 0x00000011 73 } cam_cmdmask; 74 75 typedef enum { 76 CAM_ARG_NONE = 0x00000000, 77 CAM_ARG_VERBOSE = 0x00000001, 78 CAM_ARG_DEVICE = 0x00000002, 79 CAM_ARG_BUS = 0x00000004, 80 CAM_ARG_TARGET = 0x00000008, 81 CAM_ARG_LUN = 0x00000010, 82 CAM_ARG_EJECT = 0x00000020, 83 CAM_ARG_UNIT = 0x00000040, 84 CAM_ARG_FORMAT_BLOCK = 0x00000080, 85 CAM_ARG_FORMAT_BFI = 0x00000100, 86 CAM_ARG_FORMAT_PHYS = 0x00000200, 87 CAM_ARG_PLIST = 0x00000400, 88 CAM_ARG_GLIST = 0x00000800, 89 CAM_ARG_GET_SERIAL = 0x00001000, 90 CAM_ARG_GET_STDINQ = 0x00002000, 91 CAM_ARG_GET_XFERRATE = 0x00004000, 92 CAM_ARG_INQ_MASK = 0x00007000, 93 CAM_ARG_MODE_EDIT = 0x00008000, 94 CAM_ARG_PAGE_CNTL = 0x00010000, 95 CAM_ARG_TIMEOUT = 0x00020000, 96 CAM_ARG_CMD_IN = 0x00040000, 97 CAM_ARG_CMD_OUT = 0x00080000, 98 CAM_ARG_DBD = 0x00100000, 99 CAM_ARG_ERR_RECOVER = 0x00200000, 100 CAM_ARG_RETRIES = 0x00400000, 101 CAM_ARG_START_UNIT = 0x00800000, 102 CAM_ARG_DEBUG_INFO = 0x01000000, 103 CAM_ARG_DEBUG_TRACE = 0x02000000, 104 CAM_ARG_DEBUG_SUBTRACE = 0x04000000, 105 CAM_ARG_DEBUG_CDB = 0x08000000, 106 CAM_ARG_DEBUG_XPT = 0x10000000, 107 CAM_ARG_DEBUG_PERIPH = 0x20000000, 108 } cam_argmask; 109 110 struct camcontrol_opts { 111 const char *optname; 112 cam_cmdmask cmdnum; 113 cam_argmask argnum; 114 const char *subopt; 115 }; 116 117 #ifndef MINIMALISTIC 118 static const char scsicmd_opts[] = "c:i:o:"; 119 static const char readdefect_opts[] = "f:GP"; 120 static const char negotiate_opts[] = "acD:O:qR:T:UW:"; 121 #endif 122 123 struct camcontrol_opts option_table[] = { 124 #ifndef MINIMALISTIC 125 {"tur", CAM_CMD_TUR, CAM_ARG_NONE, NULL}, 126 {"inquiry", CAM_CMD_INQUIRY, CAM_ARG_NONE, "DSR"}, 127 {"start", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT, NULL}, 128 {"stop", CAM_CMD_STARTSTOP, CAM_ARG_NONE, NULL}, 129 {"load", CAM_CMD_STARTSTOP, CAM_ARG_START_UNIT | CAM_ARG_EJECT, NULL}, 130 {"eject", CAM_CMD_STARTSTOP, CAM_ARG_EJECT, NULL}, 131 {"reportluns", CAM_CMD_REPORTLUNS, CAM_ARG_NONE, "clr:"}, 132 #endif /* MINIMALISTIC */ 133 {"rescan", CAM_CMD_RESCAN, CAM_ARG_NONE, NULL}, 134 {"reset", CAM_CMD_RESET, CAM_ARG_NONE, NULL}, 135 #ifndef MINIMALISTIC 136 {"cmd", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts}, 137 {"command", CAM_CMD_SCSI_CMD, CAM_ARG_NONE, scsicmd_opts}, 138 {"defects", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts}, 139 {"defectlist", CAM_CMD_READ_DEFECTS, CAM_ARG_NONE, readdefect_opts}, 140 #endif /* MINIMALISTIC */ 141 {"devlist", CAM_CMD_DEVTREE, CAM_ARG_NONE, NULL}, 142 #ifndef MINIMALISTIC 143 {"periphlist", CAM_CMD_DEVLIST, CAM_ARG_NONE, NULL}, 144 {"modepage", CAM_CMD_MODE_PAGE, CAM_ARG_NONE, "bdelm:P:"}, 145 {"tags", CAM_CMD_TAG, CAM_ARG_NONE, "N:q"}, 146 {"negotiate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts}, 147 {"rate", CAM_CMD_RATE, CAM_ARG_NONE, negotiate_opts}, 148 {"debug", CAM_CMD_DEBUG, CAM_ARG_NONE, "IPTSXc"}, 149 {"format", CAM_CMD_FORMAT, CAM_ARG_NONE, "qrwy"}, 150 #endif /* MINIMALISTIC */ 151 {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL}, 152 {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL}, 153 {"-h", CAM_CMD_USAGE, CAM_ARG_NONE, NULL}, 154 {NULL, 0, 0, NULL} 155 }; 156 157 typedef enum { 158 CC_OR_NOT_FOUND, 159 CC_OR_AMBIGUOUS, 160 CC_OR_FOUND 161 } camcontrol_optret; 162 163 cam_cmdmask cmdlist; 164 cam_argmask arglist; 165 166 167 camcontrol_optret getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum, 168 const char **subopt); 169 #ifndef MINIMALISTIC 170 static int getdevlist(struct cam_device *device); 171 #endif /* MINIMALISTIC */ 172 static int getdevtree(void); 173 #ifndef MINIMALISTIC 174 static int testunitready(struct cam_device *device, int retry_count, 175 int timeout, int quiet); 176 static int scsistart(struct cam_device *device, int startstop, int loadeject, 177 int retry_count, int timeout); 178 static int scsidoinquiry(struct cam_device *device, int argc, char **argv, 179 char *combinedopt, int retry_count, int timeout); 180 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout); 181 static int scsiserial(struct cam_device *device, int retry_count, int timeout); 182 static int scsixferrate(struct cam_device *device); 183 #endif /* MINIMALISTIC */ 184 static int parse_btl(char *tstr, int *bus, int *target, int *lun, 185 cam_argmask *arglst); 186 static int dorescan_or_reset(int argc, char **argv, int rescan); 187 static int rescan_or_reset_bus(int bus, int rescan); 188 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan); 189 #ifndef MINIMALISTIC 190 static int readdefects(struct cam_device *device, int argc, char **argv, 191 char *combinedopt, int retry_count, int timeout); 192 static void modepage(struct cam_device *device, int argc, char **argv, 193 char *combinedopt, int retry_count, int timeout); 194 static int scsicmd(struct cam_device *device, int argc, char **argv, 195 char *combinedopt, int retry_count, int timeout); 196 static int tagcontrol(struct cam_device *device, int argc, char **argv, 197 char *combinedopt); 198 static void cts_print(struct cam_device *device, 199 struct ccb_trans_settings *cts); 200 static void cpi_print(struct ccb_pathinq *cpi); 201 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi); 202 static int get_print_cts(struct cam_device *device, int user_settings, 203 int quiet, struct ccb_trans_settings *cts); 204 static int ratecontrol(struct cam_device *device, int retry_count, 205 int timeout, int argc, char **argv, char *combinedopt); 206 static int scsiformat(struct cam_device *device, int argc, char **argv, 207 char *combinedopt, int retry_count, int timeout); 208 static int scsireportluns(struct cam_device *device, int argc, char **argv, 209 char *combinedopt, int retry_count, int timeout); 210 #endif /* MINIMALISTIC */ 211 212 camcontrol_optret 213 getoption(char *arg, cam_cmdmask *cmdnum, cam_argmask *argnum, 214 const char **subopt) 215 { 216 struct camcontrol_opts *opts; 217 int num_matches = 0; 218 219 for (opts = option_table; (opts != NULL) && (opts->optname != NULL); 220 opts++) { 221 if (strncmp(opts->optname, arg, strlen(arg)) == 0) { 222 *cmdnum = opts->cmdnum; 223 *argnum = opts->argnum; 224 *subopt = opts->subopt; 225 if (++num_matches > 1) 226 return(CC_OR_AMBIGUOUS); 227 } 228 } 229 230 if (num_matches > 0) 231 return(CC_OR_FOUND); 232 else 233 return(CC_OR_NOT_FOUND); 234 } 235 236 #ifndef MINIMALISTIC 237 static int 238 getdevlist(struct cam_device *device) 239 { 240 union ccb *ccb; 241 char status[32]; 242 int error = 0; 243 244 ccb = cam_getccb(device); 245 246 ccb->ccb_h.func_code = XPT_GDEVLIST; 247 ccb->ccb_h.flags = CAM_DIR_NONE; 248 ccb->ccb_h.retry_count = 1; 249 ccb->cgdl.index = 0; 250 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS; 251 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) { 252 if (cam_send_ccb(device, ccb) < 0) { 253 perror("error getting device list"); 254 cam_freeccb(ccb); 255 return(1); 256 } 257 258 status[0] = '\0'; 259 260 switch (ccb->cgdl.status) { 261 case CAM_GDEVLIST_MORE_DEVS: 262 strcpy(status, "MORE"); 263 break; 264 case CAM_GDEVLIST_LAST_DEVICE: 265 strcpy(status, "LAST"); 266 break; 267 case CAM_GDEVLIST_LIST_CHANGED: 268 strcpy(status, "CHANGED"); 269 break; 270 case CAM_GDEVLIST_ERROR: 271 strcpy(status, "ERROR"); 272 error = 1; 273 break; 274 } 275 276 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n", 277 ccb->cgdl.periph_name, 278 ccb->cgdl.unit_number, 279 ccb->cgdl.generation, 280 ccb->cgdl.index, 281 status); 282 283 /* 284 * If the list has changed, we need to start over from the 285 * beginning. 286 */ 287 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED) 288 ccb->cgdl.index = 0; 289 } 290 291 cam_freeccb(ccb); 292 293 return(error); 294 } 295 #endif /* MINIMALISTIC */ 296 297 static int 298 getdevtree(void) 299 { 300 union ccb ccb; 301 int bufsize, fd; 302 unsigned int i; 303 int need_close = 0; 304 int error = 0; 305 int skip_device = 0; 306 307 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) { 308 warn("couldn't open %s", XPT_DEVICE); 309 return(1); 310 } 311 312 bzero(&ccb, sizeof(union ccb)); 313 314 ccb.ccb_h.path_id = CAM_XPT_PATH_ID; 315 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 316 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 317 318 ccb.ccb_h.func_code = XPT_DEV_MATCH; 319 bufsize = sizeof(struct dev_match_result) * 100; 320 ccb.cdm.match_buf_len = bufsize; 321 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize); 322 if (ccb.cdm.matches == NULL) { 323 warnx("can't malloc memory for matches"); 324 close(fd); 325 return(1); 326 } 327 ccb.cdm.num_matches = 0; 328 329 /* 330 * We fetch all nodes, since we display most of them in the default 331 * case, and all in the verbose case. 332 */ 333 ccb.cdm.num_patterns = 0; 334 ccb.cdm.pattern_buf_len = 0; 335 336 /* 337 * We do the ioctl multiple times if necessary, in case there are 338 * more than 100 nodes in the EDT. 339 */ 340 do { 341 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 342 warn("error sending CAMIOCOMMAND ioctl"); 343 error = 1; 344 break; 345 } 346 347 if ((ccb.ccb_h.status != CAM_REQ_CMP) 348 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST) 349 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) { 350 warnx("got CAM error %#x, CDM error %d\n", 351 ccb.ccb_h.status, ccb.cdm.status); 352 error = 1; 353 break; 354 } 355 356 for (i = 0; i < ccb.cdm.num_matches; i++) { 357 switch (ccb.cdm.matches[i].type) { 358 case DEV_MATCH_BUS: { 359 struct bus_match_result *bus_result; 360 361 /* 362 * Only print the bus information if the 363 * user turns on the verbose flag. 364 */ 365 if ((arglist & CAM_ARG_VERBOSE) == 0) 366 break; 367 368 bus_result = 369 &ccb.cdm.matches[i].result.bus_result; 370 371 if (need_close) { 372 fprintf(stdout, ")\n"); 373 need_close = 0; 374 } 375 376 fprintf(stdout, "scbus%d on %s%d bus %d:\n", 377 bus_result->path_id, 378 bus_result->dev_name, 379 bus_result->unit_number, 380 bus_result->bus_id); 381 break; 382 } 383 case DEV_MATCH_DEVICE: { 384 struct device_match_result *dev_result; 385 char vendor[16], product[48], revision[16]; 386 char tmpstr[256]; 387 388 dev_result = 389 &ccb.cdm.matches[i].result.device_result; 390 391 if ((dev_result->flags 392 & DEV_RESULT_UNCONFIGURED) 393 && ((arglist & CAM_ARG_VERBOSE) == 0)) { 394 skip_device = 1; 395 break; 396 } else 397 skip_device = 0; 398 399 cam_strvis(vendor, dev_result->inq_data.vendor, 400 sizeof(dev_result->inq_data.vendor), 401 sizeof(vendor)); 402 cam_strvis(product, 403 dev_result->inq_data.product, 404 sizeof(dev_result->inq_data.product), 405 sizeof(product)); 406 cam_strvis(revision, 407 dev_result->inq_data.revision, 408 sizeof(dev_result->inq_data.revision), 409 sizeof(revision)); 410 sprintf(tmpstr, "<%s %s %s>", vendor, product, 411 revision); 412 if (need_close) { 413 fprintf(stdout, ")\n"); 414 need_close = 0; 415 } 416 417 fprintf(stdout, "%-33s at scbus%d " 418 "target %d lun %d (", 419 tmpstr, 420 dev_result->path_id, 421 dev_result->target_id, 422 dev_result->target_lun); 423 424 need_close = 1; 425 426 break; 427 } 428 case DEV_MATCH_PERIPH: { 429 struct periph_match_result *periph_result; 430 431 periph_result = 432 &ccb.cdm.matches[i].result.periph_result; 433 434 if (skip_device != 0) 435 break; 436 437 if (need_close > 1) 438 fprintf(stdout, ","); 439 440 fprintf(stdout, "%s%d", 441 periph_result->periph_name, 442 periph_result->unit_number); 443 444 need_close++; 445 break; 446 } 447 default: 448 fprintf(stdout, "unknown match type\n"); 449 break; 450 } 451 } 452 453 } while ((ccb.ccb_h.status == CAM_REQ_CMP) 454 && (ccb.cdm.status == CAM_DEV_MATCH_MORE)); 455 456 if (need_close) 457 fprintf(stdout, ")\n"); 458 459 close(fd); 460 461 return(error); 462 } 463 464 #ifndef MINIMALISTIC 465 static int 466 testunitready(struct cam_device *device, int retry_count, int timeout, 467 int quiet) 468 { 469 int error = 0; 470 union ccb *ccb; 471 472 ccb = cam_getccb(device); 473 474 scsi_test_unit_ready(&ccb->csio, 475 /* retries */ retry_count, 476 /* cbfcnp */ NULL, 477 /* tag_action */ MSG_SIMPLE_Q_TAG, 478 /* sense_len */ SSD_FULL_SIZE, 479 /* timeout */ timeout ? timeout : 5000); 480 481 /* Disable freezing the device queue */ 482 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 483 484 if (arglist & CAM_ARG_ERR_RECOVER) 485 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 486 487 if (cam_send_ccb(device, ccb) < 0) { 488 if (quiet == 0) 489 perror("error sending test unit ready"); 490 491 if (arglist & CAM_ARG_VERBOSE) { 492 cam_error_print(device, ccb, CAM_ESF_ALL, 493 CAM_EPF_ALL, stderr); 494 } 495 496 cam_freeccb(ccb); 497 return(1); 498 } 499 500 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 501 if (quiet == 0) 502 fprintf(stdout, "Unit is ready\n"); 503 } else { 504 if (quiet == 0) 505 fprintf(stdout, "Unit is not ready\n"); 506 error = 1; 507 508 if (arglist & CAM_ARG_VERBOSE) { 509 cam_error_print(device, ccb, CAM_ESF_ALL, 510 CAM_EPF_ALL, stderr); 511 } 512 } 513 514 cam_freeccb(ccb); 515 516 return(error); 517 } 518 519 static int 520 scsistart(struct cam_device *device, int startstop, int loadeject, 521 int retry_count, int timeout) 522 { 523 union ccb *ccb; 524 int error = 0; 525 526 ccb = cam_getccb(device); 527 528 /* 529 * If we're stopping, send an ordered tag so the drive in question 530 * will finish any previously queued writes before stopping. If 531 * the device isn't capable of tagged queueing, or if tagged 532 * queueing is turned off, the tag action is a no-op. 533 */ 534 scsi_start_stop(&ccb->csio, 535 /* retries */ retry_count, 536 /* cbfcnp */ NULL, 537 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG : 538 MSG_ORDERED_Q_TAG, 539 /* start/stop */ startstop, 540 /* load_eject */ loadeject, 541 /* immediate */ 0, 542 /* sense_len */ SSD_FULL_SIZE, 543 /* timeout */ timeout ? timeout : 120000); 544 545 /* Disable freezing the device queue */ 546 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 547 548 if (arglist & CAM_ARG_ERR_RECOVER) 549 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 550 551 if (cam_send_ccb(device, ccb) < 0) { 552 perror("error sending start unit"); 553 554 if (arglist & CAM_ARG_VERBOSE) { 555 cam_error_print(device, ccb, CAM_ESF_ALL, 556 CAM_EPF_ALL, stderr); 557 } 558 559 cam_freeccb(ccb); 560 return(1); 561 } 562 563 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 564 if (startstop) { 565 fprintf(stdout, "Unit started successfully"); 566 if (loadeject) 567 fprintf(stdout,", Media loaded\n"); 568 else 569 fprintf(stdout,"\n"); 570 } else { 571 fprintf(stdout, "Unit stopped successfully"); 572 if (loadeject) 573 fprintf(stdout, ", Media ejected\n"); 574 else 575 fprintf(stdout, "\n"); 576 } 577 else { 578 error = 1; 579 if (startstop) 580 fprintf(stdout, 581 "Error received from start unit command\n"); 582 else 583 fprintf(stdout, 584 "Error received from stop unit command\n"); 585 586 if (arglist & CAM_ARG_VERBOSE) { 587 cam_error_print(device, ccb, CAM_ESF_ALL, 588 CAM_EPF_ALL, stderr); 589 } 590 } 591 592 cam_freeccb(ccb); 593 594 return(error); 595 } 596 597 static int 598 scsidoinquiry(struct cam_device *device, int argc, char **argv, 599 char *combinedopt, int retry_count, int timeout) 600 { 601 int c; 602 int error = 0; 603 604 while ((c = getopt(argc, argv, combinedopt)) != -1) { 605 switch(c) { 606 case 'D': 607 arglist |= CAM_ARG_GET_STDINQ; 608 break; 609 case 'R': 610 arglist |= CAM_ARG_GET_XFERRATE; 611 break; 612 case 'S': 613 arglist |= CAM_ARG_GET_SERIAL; 614 break; 615 default: 616 break; 617 } 618 } 619 620 /* 621 * If the user didn't specify any inquiry options, he wants all of 622 * them. 623 */ 624 if ((arglist & CAM_ARG_INQ_MASK) == 0) 625 arglist |= CAM_ARG_INQ_MASK; 626 627 if (arglist & CAM_ARG_GET_STDINQ) 628 error = scsiinquiry(device, retry_count, timeout); 629 630 if (error != 0) 631 return(error); 632 633 if (arglist & CAM_ARG_GET_SERIAL) 634 scsiserial(device, retry_count, timeout); 635 636 if (error != 0) 637 return(error); 638 639 if (arglist & CAM_ARG_GET_XFERRATE) 640 error = scsixferrate(device); 641 642 return(error); 643 } 644 645 static int 646 scsiinquiry(struct cam_device *device, int retry_count, int timeout) 647 { 648 union ccb *ccb; 649 struct scsi_inquiry_data *inq_buf; 650 int error = 0; 651 652 ccb = cam_getccb(device); 653 654 if (ccb == NULL) { 655 warnx("couldn't allocate CCB"); 656 return(1); 657 } 658 659 /* cam_getccb cleans up the header, caller has to zero the payload */ 660 bzero(&(&ccb->ccb_h)[1], 661 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 662 663 inq_buf = (struct scsi_inquiry_data *)malloc( 664 sizeof(struct scsi_inquiry_data)); 665 666 if (inq_buf == NULL) { 667 cam_freeccb(ccb); 668 warnx("can't malloc memory for inquiry\n"); 669 return(1); 670 } 671 bzero(inq_buf, sizeof(*inq_buf)); 672 673 /* 674 * Note that although the size of the inquiry buffer is the full 675 * 256 bytes specified in the SCSI spec, we only tell the device 676 * that we have allocated SHORT_INQUIRY_LENGTH bytes. There are 677 * two reasons for this: 678 * 679 * - The SCSI spec says that when a length field is only 1 byte, 680 * a value of 0 will be interpreted as 256. Therefore 681 * scsi_inquiry() will convert an inq_len (which is passed in as 682 * a u_int32_t, but the field in the CDB is only 1 byte) of 256 683 * to 0. Evidently, very few devices meet the spec in that 684 * regard. Some devices, like many Seagate disks, take the 0 as 685 * 0, and don't return any data. One Pioneer DVD-R drive 686 * returns more data than the command asked for. 687 * 688 * So, since there are numerous devices that just don't work 689 * right with the full inquiry size, we don't send the full size. 690 * 691 * - The second reason not to use the full inquiry data length is 692 * that we don't need it here. The only reason we issue a 693 * standard inquiry is to get the vendor name, device name, 694 * and revision so scsi_print_inquiry() can print them. 695 * 696 * If, at some point in the future, more inquiry data is needed for 697 * some reason, this code should use a procedure similar to the 698 * probe code. i.e., issue a short inquiry, and determine from 699 * the additional length passed back from the device how much 700 * inquiry data the device supports. Once the amount the device 701 * supports is determined, issue an inquiry for that amount and no 702 * more. 703 * 704 * KDM, 2/18/2000 705 */ 706 scsi_inquiry(&ccb->csio, 707 /* retries */ retry_count, 708 /* cbfcnp */ NULL, 709 /* tag_action */ MSG_SIMPLE_Q_TAG, 710 /* inq_buf */ (u_int8_t *)inq_buf, 711 /* inq_len */ SHORT_INQUIRY_LENGTH, 712 /* evpd */ 0, 713 /* page_code */ 0, 714 /* sense_len */ SSD_FULL_SIZE, 715 /* timeout */ timeout ? timeout : 5000); 716 717 /* Disable freezing the device queue */ 718 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 719 720 if (arglist & CAM_ARG_ERR_RECOVER) 721 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 722 723 if (cam_send_ccb(device, ccb) < 0) { 724 perror("error sending SCSI inquiry"); 725 726 if (arglist & CAM_ARG_VERBOSE) { 727 cam_error_print(device, ccb, CAM_ESF_ALL, 728 CAM_EPF_ALL, stderr); 729 } 730 731 cam_freeccb(ccb); 732 return(1); 733 } 734 735 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 736 error = 1; 737 738 if (arglist & CAM_ARG_VERBOSE) { 739 cam_error_print(device, ccb, CAM_ESF_ALL, 740 CAM_EPF_ALL, stderr); 741 } 742 } 743 744 cam_freeccb(ccb); 745 746 if (error != 0) { 747 free(inq_buf); 748 return(error); 749 } 750 751 fprintf(stdout, "%s%d: ", device->device_name, 752 device->dev_unit_num); 753 scsi_print_inquiry(inq_buf); 754 755 free(inq_buf); 756 757 return(0); 758 } 759 760 static int 761 scsiserial(struct cam_device *device, int retry_count, int timeout) 762 { 763 union ccb *ccb; 764 struct scsi_vpd_unit_serial_number *serial_buf; 765 char serial_num[SVPD_SERIAL_NUM_SIZE + 1]; 766 int error = 0; 767 768 ccb = cam_getccb(device); 769 770 if (ccb == NULL) { 771 warnx("couldn't allocate CCB"); 772 return(1); 773 } 774 775 /* cam_getccb cleans up the header, caller has to zero the payload */ 776 bzero(&(&ccb->ccb_h)[1], 777 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 778 779 serial_buf = (struct scsi_vpd_unit_serial_number *) 780 malloc(sizeof(*serial_buf)); 781 782 if (serial_buf == NULL) { 783 cam_freeccb(ccb); 784 warnx("can't malloc memory for serial number"); 785 return(1); 786 } 787 788 scsi_inquiry(&ccb->csio, 789 /*retries*/ retry_count, 790 /*cbfcnp*/ NULL, 791 /* tag_action */ MSG_SIMPLE_Q_TAG, 792 /* inq_buf */ (u_int8_t *)serial_buf, 793 /* inq_len */ sizeof(*serial_buf), 794 /* evpd */ 1, 795 /* page_code */ SVPD_UNIT_SERIAL_NUMBER, 796 /* sense_len */ SSD_FULL_SIZE, 797 /* timeout */ timeout ? timeout : 5000); 798 799 /* Disable freezing the device queue */ 800 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 801 802 if (arglist & CAM_ARG_ERR_RECOVER) 803 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 804 805 if (cam_send_ccb(device, ccb) < 0) { 806 warn("error getting serial number"); 807 808 if (arglist & CAM_ARG_VERBOSE) { 809 cam_error_print(device, ccb, CAM_ESF_ALL, 810 CAM_EPF_ALL, stderr); 811 } 812 813 cam_freeccb(ccb); 814 free(serial_buf); 815 return(1); 816 } 817 818 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 819 error = 1; 820 821 if (arglist & CAM_ARG_VERBOSE) { 822 cam_error_print(device, ccb, CAM_ESF_ALL, 823 CAM_EPF_ALL, stderr); 824 } 825 } 826 827 cam_freeccb(ccb); 828 829 if (error != 0) { 830 free(serial_buf); 831 return(error); 832 } 833 834 bcopy(serial_buf->serial_num, serial_num, serial_buf->length); 835 serial_num[serial_buf->length] = '\0'; 836 837 if ((arglist & CAM_ARG_GET_STDINQ) 838 || (arglist & CAM_ARG_GET_XFERRATE)) 839 fprintf(stdout, "%s%d: Serial Number ", 840 device->device_name, device->dev_unit_num); 841 842 fprintf(stdout, "%.60s\n", serial_num); 843 844 free(serial_buf); 845 846 return(0); 847 } 848 849 static int 850 scsixferrate(struct cam_device *device) 851 { 852 u_int32_t freq = 0; 853 u_int32_t speed = 0; 854 union ccb *ccb; 855 u_int mb; 856 int retval = 0; 857 858 ccb = cam_getccb(device); 859 860 if (ccb == NULL) { 861 warnx("couldn't allocate CCB"); 862 return(1); 863 } 864 865 bzero(&(&ccb->ccb_h)[1], 866 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 867 868 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 869 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS; 870 871 if (((retval = cam_send_ccb(device, ccb)) < 0) 872 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 873 const char error_string[] = "error getting transfer settings"; 874 875 if (retval < 0) 876 warn(error_string); 877 else 878 warnx(error_string); 879 880 if (arglist & CAM_ARG_VERBOSE) 881 cam_error_print(device, ccb, CAM_ESF_ALL, 882 CAM_EPF_ALL, stderr); 883 884 retval = 1; 885 886 goto xferrate_bailout; 887 888 } 889 890 if (ccb->cts.transport == XPORT_SPI) { 891 struct ccb_trans_settings_spi *spi = 892 &ccb->cts.xport_specific.spi; 893 894 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) { 895 freq = scsi_calc_syncsrate(spi->sync_period); 896 speed = freq; 897 } 898 899 fprintf(stdout, "%s%d: ", device->device_name, 900 device->dev_unit_num); 901 902 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) { 903 speed *= (0x01 << spi->bus_width); 904 } 905 906 mb = speed / 1000; 907 908 if (mb > 0) 909 fprintf(stdout, "%d.%03dMB/s transfers ", 910 mb, speed % 1000); 911 else 912 fprintf(stdout, "%dKB/s transfers ", 913 speed); 914 915 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) 916 && (spi->sync_offset != 0)) 917 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000, 918 freq % 1000, spi->sync_offset); 919 920 if (((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) 921 && (spi->bus_width > 0)) { 922 if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) 923 && (spi->sync_offset != 0)) { 924 fprintf(stdout, ", "); 925 } else { 926 fprintf(stdout, " ("); 927 } 928 fprintf(stdout, "%dbit)", 8 * (0x01 << spi->bus_width)); 929 } else if (((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0) 930 && (spi->sync_offset != 0)) { 931 fprintf(stdout, ")"); 932 } 933 } else { 934 struct ccb_pathinq cpi; 935 936 retval = get_cpi(device, &cpi); 937 938 if (retval != 0) 939 goto xferrate_bailout; 940 941 speed = cpi.base_transfer_speed; 942 freq = 0; 943 944 mb = speed / 1000; 945 946 if (mb > 0) 947 fprintf(stdout, "%d.%03dMB/s transfers ", 948 mb, speed % 1000); 949 else 950 fprintf(stdout, "%dKB/s transfers ", 951 speed); 952 } 953 954 if (ccb->cts.protocol == PROTO_SCSI) { 955 struct ccb_trans_settings_scsi *scsi = 956 &ccb->cts.proto_specific.scsi; 957 if (scsi->valid & CTS_SCSI_VALID_TQ) { 958 if (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) 959 fprintf(stdout, ", Command Queueing Enabled"); 960 else 961 fprintf(stdout, ", Command Queueing Supported"); 962 } 963 } 964 965 fprintf(stdout, "\n"); 966 967 xferrate_bailout: 968 969 cam_freeccb(ccb); 970 971 return(retval); 972 } 973 #endif /* MINIMALISTIC */ 974 975 /* 976 * Parse out a bus, or a bus, target and lun in the following 977 * format: 978 * bus 979 * bus:target 980 * bus:target:lun 981 * 982 * Returns the number of parsed components, or 0. 983 */ 984 static int 985 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglst) 986 { 987 char *tmpstr; 988 int convs = 0; 989 990 while (isspace(*tstr) && (*tstr != '\0')) 991 tstr++; 992 993 tmpstr = (char *)strtok(tstr, ":"); 994 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 995 *bus = strtol(tmpstr, NULL, 0); 996 *arglst |= CAM_ARG_BUS; 997 convs++; 998 tmpstr = (char *)strtok(NULL, ":"); 999 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 1000 *target = strtol(tmpstr, NULL, 0); 1001 *arglst |= CAM_ARG_TARGET; 1002 convs++; 1003 tmpstr = (char *)strtok(NULL, ":"); 1004 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 1005 *lun = strtol(tmpstr, NULL, 0); 1006 *arglst |= CAM_ARG_LUN; 1007 convs++; 1008 } 1009 } 1010 } 1011 1012 return convs; 1013 } 1014 1015 static int 1016 dorescan_or_reset(int argc, char **argv, int rescan) 1017 { 1018 static const char must[] = 1019 "you must specify \"all\", a bus, or a bus:target:lun to %s"; 1020 int rv, error = 0; 1021 int bus = -1, target = -1, lun = -1; 1022 char *tstr; 1023 1024 if (argc < 3) { 1025 warnx(must, rescan? "rescan" : "reset"); 1026 return(1); 1027 } 1028 1029 tstr = argv[optind]; 1030 while (isspace(*tstr) && (*tstr != '\0')) 1031 tstr++; 1032 if (strncasecmp(tstr, "all", strlen("all")) == 0) 1033 arglist |= CAM_ARG_BUS; 1034 else { 1035 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist); 1036 if (rv != 1 && rv != 3) { 1037 warnx(must, rescan? "rescan" : "reset"); 1038 return(1); 1039 } 1040 } 1041 1042 if ((arglist & CAM_ARG_BUS) 1043 && (arglist & CAM_ARG_TARGET) 1044 && (arglist & CAM_ARG_LUN)) 1045 error = scanlun_or_reset_dev(bus, target, lun, rescan); 1046 else 1047 error = rescan_or_reset_bus(bus, rescan); 1048 1049 return(error); 1050 } 1051 1052 static int 1053 rescan_or_reset_bus(int bus, int rescan) 1054 { 1055 union ccb ccb, matchccb; 1056 int fd, retval; 1057 int bufsize; 1058 1059 retval = 0; 1060 1061 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1062 warnx("error opening tranport layer device %s", XPT_DEVICE); 1063 warn("%s", XPT_DEVICE); 1064 return(1); 1065 } 1066 1067 if (bus != -1) { 1068 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : XPT_RESET_BUS; 1069 ccb.ccb_h.path_id = bus; 1070 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 1071 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 1072 ccb.crcn.flags = CAM_FLAG_NONE; 1073 1074 /* run this at a low priority */ 1075 ccb.ccb_h.pinfo.priority = 5; 1076 1077 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1078 warn("CAMIOCOMMAND ioctl failed"); 1079 close(fd); 1080 return(1); 1081 } 1082 1083 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1084 fprintf(stdout, "%s of bus %d was successful\n", 1085 rescan ? "Re-scan" : "Reset", bus); 1086 } else { 1087 fprintf(stdout, "%s of bus %d returned error %#x\n", 1088 rescan ? "Re-scan" : "Reset", bus, 1089 ccb.ccb_h.status & CAM_STATUS_MASK); 1090 retval = 1; 1091 } 1092 1093 close(fd); 1094 return(retval); 1095 1096 } 1097 1098 1099 /* 1100 * The right way to handle this is to modify the xpt so that it can 1101 * handle a wildcarded bus in a rescan or reset CCB. At the moment 1102 * that isn't implemented, so instead we enumerate the busses and 1103 * send the rescan or reset to those busses in the case where the 1104 * given bus is -1 (wildcard). We don't send a rescan or reset 1105 * to the xpt bus; sending a rescan to the xpt bus is effectively a 1106 * no-op, sending a rescan to the xpt bus would result in a status of 1107 * CAM_REQ_INVALID. 1108 */ 1109 bzero(&(&matchccb.ccb_h)[1], 1110 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr)); 1111 matchccb.ccb_h.func_code = XPT_DEV_MATCH; 1112 bufsize = sizeof(struct dev_match_result) * 20; 1113 matchccb.cdm.match_buf_len = bufsize; 1114 matchccb.cdm.matches=(struct dev_match_result *)malloc(bufsize); 1115 if (matchccb.cdm.matches == NULL) { 1116 warnx("can't malloc memory for matches"); 1117 retval = 1; 1118 goto bailout; 1119 } 1120 matchccb.cdm.num_matches = 0; 1121 1122 matchccb.cdm.num_patterns = 1; 1123 matchccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern); 1124 1125 matchccb.cdm.patterns = (struct dev_match_pattern *)malloc( 1126 matchccb.cdm.pattern_buf_len); 1127 if (matchccb.cdm.patterns == NULL) { 1128 warnx("can't malloc memory for patterns"); 1129 retval = 1; 1130 goto bailout; 1131 } 1132 matchccb.cdm.patterns[0].type = DEV_MATCH_BUS; 1133 matchccb.cdm.patterns[0].pattern.bus_pattern.flags = BUS_MATCH_ANY; 1134 1135 do { 1136 unsigned int i; 1137 1138 if (ioctl(fd, CAMIOCOMMAND, &matchccb) == -1) { 1139 warn("CAMIOCOMMAND ioctl failed"); 1140 retval = 1; 1141 goto bailout; 1142 } 1143 1144 if ((matchccb.ccb_h.status != CAM_REQ_CMP) 1145 || ((matchccb.cdm.status != CAM_DEV_MATCH_LAST) 1146 && (matchccb.cdm.status != CAM_DEV_MATCH_MORE))) { 1147 warnx("got CAM error %#x, CDM error %d\n", 1148 matchccb.ccb_h.status, matchccb.cdm.status); 1149 retval = 1; 1150 goto bailout; 1151 } 1152 1153 for (i = 0; i < matchccb.cdm.num_matches; i++) { 1154 struct bus_match_result *bus_result; 1155 1156 /* This shouldn't happen. */ 1157 if (matchccb.cdm.matches[i].type != DEV_MATCH_BUS) 1158 continue; 1159 1160 bus_result = &matchccb.cdm.matches[i].result.bus_result; 1161 1162 /* 1163 * We don't want to rescan or reset the xpt bus. 1164 * See above. 1165 */ 1166 if ((int)bus_result->path_id == -1) 1167 continue; 1168 1169 ccb.ccb_h.func_code = rescan ? XPT_SCAN_BUS : 1170 XPT_RESET_BUS; 1171 ccb.ccb_h.path_id = bus_result->path_id; 1172 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 1173 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 1174 ccb.crcn.flags = CAM_FLAG_NONE; 1175 1176 /* run this at a low priority */ 1177 ccb.ccb_h.pinfo.priority = 5; 1178 1179 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1180 warn("CAMIOCOMMAND ioctl failed"); 1181 retval = 1; 1182 goto bailout; 1183 } 1184 1185 if ((ccb.ccb_h.status & CAM_STATUS_MASK) ==CAM_REQ_CMP){ 1186 fprintf(stdout, "%s of bus %d was successful\n", 1187 rescan? "Re-scan" : "Reset", 1188 bus_result->path_id); 1189 } else { 1190 /* 1191 * Don't bail out just yet, maybe the other 1192 * rescan or reset commands will complete 1193 * successfully. 1194 */ 1195 fprintf(stderr, "%s of bus %d returned error " 1196 "%#x\n", rescan? "Re-scan" : "Reset", 1197 bus_result->path_id, 1198 ccb.ccb_h.status & CAM_STATUS_MASK); 1199 retval = 1; 1200 } 1201 } 1202 } while ((matchccb.ccb_h.status == CAM_REQ_CMP) 1203 && (matchccb.cdm.status == CAM_DEV_MATCH_MORE)); 1204 1205 bailout: 1206 1207 if (fd != -1) 1208 close(fd); 1209 1210 if (matchccb.cdm.patterns != NULL) 1211 free(matchccb.cdm.patterns); 1212 if (matchccb.cdm.matches != NULL) 1213 free(matchccb.cdm.matches); 1214 1215 return(retval); 1216 } 1217 1218 static int 1219 scanlun_or_reset_dev(int bus, int target, int lun, int scan) 1220 { 1221 union ccb ccb; 1222 struct cam_device *device; 1223 int fd; 1224 1225 device = NULL; 1226 1227 if (bus < 0) { 1228 warnx("invalid bus number %d", bus); 1229 return(1); 1230 } 1231 1232 if (target < 0) { 1233 warnx("invalid target number %d", target); 1234 return(1); 1235 } 1236 1237 if (lun < 0) { 1238 warnx("invalid lun number %d", lun); 1239 return(1); 1240 } 1241 1242 fd = -1; 1243 1244 bzero(&ccb, sizeof(union ccb)); 1245 1246 if (scan) { 1247 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1248 warnx("error opening tranport layer device %s\n", 1249 XPT_DEVICE); 1250 warn("%s", XPT_DEVICE); 1251 return(1); 1252 } 1253 } else { 1254 device = cam_open_btl(bus, target, lun, O_RDWR, NULL); 1255 if (device == NULL) { 1256 warnx("%s", cam_errbuf); 1257 return(1); 1258 } 1259 } 1260 1261 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV; 1262 ccb.ccb_h.path_id = bus; 1263 ccb.ccb_h.target_id = target; 1264 ccb.ccb_h.target_lun = lun; 1265 ccb.ccb_h.timeout = 5000; 1266 ccb.crcn.flags = CAM_FLAG_NONE; 1267 1268 /* run this at a low priority */ 1269 ccb.ccb_h.pinfo.priority = 5; 1270 1271 if (scan) { 1272 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) { 1273 warn("CAMIOCOMMAND ioctl failed"); 1274 close(fd); 1275 return(1); 1276 } 1277 } else { 1278 if (cam_send_ccb(device, &ccb) < 0) { 1279 warn("error sending XPT_RESET_DEV CCB"); 1280 cam_close_device(device); 1281 return(1); 1282 } 1283 } 1284 1285 if (scan) 1286 close(fd); 1287 else 1288 cam_close_device(device); 1289 1290 /* 1291 * An error code of CAM_BDR_SENT is normal for a BDR request. 1292 */ 1293 if (((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1294 || ((!scan) 1295 && ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_BDR_SENT))) { 1296 fprintf(stdout, "%s of %d:%d:%d was successful\n", 1297 scan? "Re-scan" : "Reset", bus, target, lun); 1298 return(0); 1299 } else { 1300 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n", 1301 scan? "Re-scan" : "Reset", bus, target, lun, 1302 ccb.ccb_h.status & CAM_STATUS_MASK); 1303 return(1); 1304 } 1305 } 1306 1307 #ifndef MINIMALISTIC 1308 static int 1309 readdefects(struct cam_device *device, int argc, char **argv, 1310 char *combinedopt, int retry_count, int timeout) 1311 { 1312 union ccb *ccb = NULL; 1313 struct scsi_read_defect_data_10 *rdd_cdb; 1314 u_int8_t *defect_list = NULL; 1315 u_int32_t dlist_length = 65000; 1316 u_int32_t returned_length = 0; 1317 u_int32_t num_returned = 0; 1318 u_int8_t returned_format; 1319 unsigned int i; 1320 int c, error = 0; 1321 int lists_specified = 0; 1322 1323 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1324 switch(c){ 1325 case 'f': 1326 { 1327 char *tstr; 1328 tstr = optarg; 1329 while (isspace(*tstr) && (*tstr != '\0')) 1330 tstr++; 1331 if (strcmp(tstr, "block") == 0) 1332 arglist |= CAM_ARG_FORMAT_BLOCK; 1333 else if (strcmp(tstr, "bfi") == 0) 1334 arglist |= CAM_ARG_FORMAT_BFI; 1335 else if (strcmp(tstr, "phys") == 0) 1336 arglist |= CAM_ARG_FORMAT_PHYS; 1337 else { 1338 error = 1; 1339 warnx("invalid defect format %s", tstr); 1340 goto defect_bailout; 1341 } 1342 break; 1343 } 1344 case 'G': 1345 arglist |= CAM_ARG_GLIST; 1346 break; 1347 case 'P': 1348 arglist |= CAM_ARG_PLIST; 1349 break; 1350 default: 1351 break; 1352 } 1353 } 1354 1355 ccb = cam_getccb(device); 1356 1357 /* 1358 * Hopefully 65000 bytes is enough to hold the defect list. If it 1359 * isn't, the disk is probably dead already. We'd have to go with 1360 * 12 byte command (i.e. alloc_length is 32 bits instead of 16) 1361 * to hold them all. 1362 */ 1363 defect_list = malloc(dlist_length); 1364 if (defect_list == NULL) { 1365 warnx("can't malloc memory for defect list"); 1366 error = 1; 1367 goto defect_bailout; 1368 } 1369 1370 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes; 1371 1372 /* 1373 * cam_getccb() zeros the CCB header only. So we need to zero the 1374 * payload portion of the ccb. 1375 */ 1376 bzero(&(&ccb->ccb_h)[1], 1377 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1378 1379 cam_fill_csio(&ccb->csio, 1380 /*retries*/ retry_count, 1381 /*cbfcnp*/ NULL, 1382 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ? 1383 CAM_PASS_ERR_RECOVER : 0), 1384 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1385 /*data_ptr*/ defect_list, 1386 /*dxfer_len*/ dlist_length, 1387 /*sense_len*/ SSD_FULL_SIZE, 1388 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10), 1389 /*timeout*/ timeout ? timeout : 5000); 1390 1391 rdd_cdb->opcode = READ_DEFECT_DATA_10; 1392 if (arglist & CAM_ARG_FORMAT_BLOCK) 1393 rdd_cdb->format = SRDD10_BLOCK_FORMAT; 1394 else if (arglist & CAM_ARG_FORMAT_BFI) 1395 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT; 1396 else if (arglist & CAM_ARG_FORMAT_PHYS) 1397 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT; 1398 else { 1399 error = 1; 1400 warnx("no defect list format specified"); 1401 goto defect_bailout; 1402 } 1403 if (arglist & CAM_ARG_PLIST) { 1404 rdd_cdb->format |= SRDD10_PLIST; 1405 lists_specified++; 1406 } 1407 1408 if (arglist & CAM_ARG_GLIST) { 1409 rdd_cdb->format |= SRDD10_GLIST; 1410 lists_specified++; 1411 } 1412 1413 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length); 1414 1415 /* Disable freezing the device queue */ 1416 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1417 1418 if (cam_send_ccb(device, ccb) < 0) { 1419 perror("error reading defect list"); 1420 1421 if (arglist & CAM_ARG_VERBOSE) { 1422 cam_error_print(device, ccb, CAM_ESF_ALL, 1423 CAM_EPF_ALL, stderr); 1424 } 1425 1426 error = 1; 1427 goto defect_bailout; 1428 } 1429 1430 returned_length = scsi_2btoul(((struct 1431 scsi_read_defect_data_hdr_10 *)defect_list)->length); 1432 1433 returned_format = ((struct scsi_read_defect_data_hdr_10 *) 1434 defect_list)->format; 1435 1436 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) 1437 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 1438 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) { 1439 struct scsi_sense_data *sense; 1440 int error_code, sense_key, asc, ascq; 1441 1442 sense = &ccb->csio.sense_data; 1443 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq); 1444 1445 /* 1446 * According to the SCSI spec, if the disk doesn't support 1447 * the requested format, it will generally return a sense 1448 * key of RECOVERED ERROR, and an additional sense code 1449 * of "DEFECT LIST NOT FOUND". So, we check for that, and 1450 * also check to make sure that the returned length is 1451 * greater than 0, and then print out whatever format the 1452 * disk gave us. 1453 */ 1454 if ((sense_key == SSD_KEY_RECOVERED_ERROR) 1455 && (asc == 0x1c) && (ascq == 0x00) 1456 && (returned_length > 0)) { 1457 warnx("requested defect format not available"); 1458 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) { 1459 case SRDD10_BLOCK_FORMAT: 1460 warnx("Device returned block format"); 1461 break; 1462 case SRDD10_BYTES_FROM_INDEX_FORMAT: 1463 warnx("Device returned bytes from index" 1464 " format"); 1465 break; 1466 case SRDD10_PHYSICAL_SECTOR_FORMAT: 1467 warnx("Device returned physical sector format"); 1468 break; 1469 default: 1470 error = 1; 1471 warnx("Device returned unknown defect" 1472 " data format %#x", returned_format); 1473 goto defect_bailout; 1474 break; /* NOTREACHED */ 1475 } 1476 } else { 1477 error = 1; 1478 warnx("Error returned from read defect data command"); 1479 if (arglist & CAM_ARG_VERBOSE) 1480 cam_error_print(device, ccb, CAM_ESF_ALL, 1481 CAM_EPF_ALL, stderr); 1482 goto defect_bailout; 1483 } 1484 } else if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1485 error = 1; 1486 warnx("Error returned from read defect data command"); 1487 if (arglist & CAM_ARG_VERBOSE) 1488 cam_error_print(device, ccb, CAM_ESF_ALL, 1489 CAM_EPF_ALL, stderr); 1490 goto defect_bailout; 1491 } 1492 1493 /* 1494 * XXX KDM I should probably clean up the printout format for the 1495 * disk defects. 1496 */ 1497 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){ 1498 case SRDDH10_PHYSICAL_SECTOR_FORMAT: 1499 { 1500 struct scsi_defect_desc_phys_sector *dlist; 1501 1502 dlist = (struct scsi_defect_desc_phys_sector *) 1503 (defect_list + 1504 sizeof(struct scsi_read_defect_data_hdr_10)); 1505 1506 num_returned = returned_length / 1507 sizeof(struct scsi_defect_desc_phys_sector); 1508 1509 fprintf(stderr, "Got %d defect", num_returned); 1510 1511 if ((lists_specified == 0) || (num_returned == 0)) { 1512 fprintf(stderr, "s.\n"); 1513 break; 1514 } else if (num_returned == 1) 1515 fprintf(stderr, ":\n"); 1516 else 1517 fprintf(stderr, "s:\n"); 1518 1519 for (i = 0; i < num_returned; i++) { 1520 fprintf(stdout, "%d:%d:%d\n", 1521 scsi_3btoul(dlist[i].cylinder), 1522 dlist[i].head, 1523 scsi_4btoul(dlist[i].sector)); 1524 } 1525 break; 1526 } 1527 case SRDDH10_BYTES_FROM_INDEX_FORMAT: 1528 { 1529 struct scsi_defect_desc_bytes_from_index *dlist; 1530 1531 dlist = (struct scsi_defect_desc_bytes_from_index *) 1532 (defect_list + 1533 sizeof(struct scsi_read_defect_data_hdr_10)); 1534 1535 num_returned = returned_length / 1536 sizeof(struct scsi_defect_desc_bytes_from_index); 1537 1538 fprintf(stderr, "Got %d defect", num_returned); 1539 1540 if ((lists_specified == 0) || (num_returned == 0)) { 1541 fprintf(stderr, "s.\n"); 1542 break; 1543 } else if (num_returned == 1) 1544 fprintf(stderr, ":\n"); 1545 else 1546 fprintf(stderr, "s:\n"); 1547 1548 for (i = 0; i < num_returned; i++) { 1549 fprintf(stdout, "%d:%d:%d\n", 1550 scsi_3btoul(dlist[i].cylinder), 1551 dlist[i].head, 1552 scsi_4btoul(dlist[i].bytes_from_index)); 1553 } 1554 break; 1555 } 1556 case SRDDH10_BLOCK_FORMAT: 1557 { 1558 struct scsi_defect_desc_block *dlist; 1559 1560 dlist = (struct scsi_defect_desc_block *)(defect_list + 1561 sizeof(struct scsi_read_defect_data_hdr_10)); 1562 1563 num_returned = returned_length / 1564 sizeof(struct scsi_defect_desc_block); 1565 1566 fprintf(stderr, "Got %d defect", num_returned); 1567 1568 if ((lists_specified == 0) || (num_returned == 0)) { 1569 fprintf(stderr, "s.\n"); 1570 break; 1571 } else if (num_returned == 1) 1572 fprintf(stderr, ":\n"); 1573 else 1574 fprintf(stderr, "s:\n"); 1575 1576 for (i = 0; i < num_returned; i++) 1577 fprintf(stdout, "%u\n", 1578 scsi_4btoul(dlist[i].address)); 1579 break; 1580 } 1581 default: 1582 fprintf(stderr, "Unknown defect format %d\n", 1583 returned_format & SRDDH10_DLIST_FORMAT_MASK); 1584 error = 1; 1585 break; 1586 } 1587 defect_bailout: 1588 1589 if (defect_list != NULL) 1590 free(defect_list); 1591 1592 if (ccb != NULL) 1593 cam_freeccb(ccb); 1594 1595 return(error); 1596 } 1597 #endif /* MINIMALISTIC */ 1598 1599 #if 0 1600 void 1601 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks) 1602 { 1603 union ccb *ccb; 1604 1605 ccb = cam_getccb(device); 1606 1607 cam_freeccb(ccb); 1608 } 1609 #endif 1610 1611 #ifndef MINIMALISTIC 1612 void 1613 mode_sense(struct cam_device *device, int mode_page, int page_control, 1614 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen) 1615 { 1616 union ccb *ccb; 1617 int retval; 1618 1619 ccb = cam_getccb(device); 1620 1621 if (ccb == NULL) 1622 errx(1, "mode_sense: couldn't allocate CCB"); 1623 1624 bzero(&(&ccb->ccb_h)[1], 1625 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1626 1627 scsi_mode_sense(&ccb->csio, 1628 /* retries */ retry_count, 1629 /* cbfcnp */ NULL, 1630 /* tag_action */ MSG_SIMPLE_Q_TAG, 1631 /* dbd */ dbd, 1632 /* page_code */ page_control << 6, 1633 /* page */ mode_page, 1634 /* param_buf */ data, 1635 /* param_len */ datalen, 1636 /* sense_len */ SSD_FULL_SIZE, 1637 /* timeout */ timeout ? timeout : 5000); 1638 1639 if (arglist & CAM_ARG_ERR_RECOVER) 1640 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1641 1642 /* Disable freezing the device queue */ 1643 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1644 1645 if (((retval = cam_send_ccb(device, ccb)) < 0) 1646 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1647 if (arglist & CAM_ARG_VERBOSE) { 1648 cam_error_print(device, ccb, CAM_ESF_ALL, 1649 CAM_EPF_ALL, stderr); 1650 } 1651 cam_freeccb(ccb); 1652 cam_close_device(device); 1653 if (retval < 0) 1654 err(1, "error sending mode sense command"); 1655 else 1656 errx(1, "error sending mode sense command"); 1657 } 1658 1659 cam_freeccb(ccb); 1660 } 1661 1662 void 1663 mode_select(struct cam_device *device, int save_pages, int retry_count, 1664 int timeout, u_int8_t *data, int datalen) 1665 { 1666 union ccb *ccb; 1667 int retval; 1668 1669 ccb = cam_getccb(device); 1670 1671 if (ccb == NULL) 1672 errx(1, "mode_select: couldn't allocate CCB"); 1673 1674 bzero(&(&ccb->ccb_h)[1], 1675 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1676 1677 scsi_mode_select(&ccb->csio, 1678 /* retries */ retry_count, 1679 /* cbfcnp */ NULL, 1680 /* tag_action */ MSG_SIMPLE_Q_TAG, 1681 /* scsi_page_fmt */ 1, 1682 /* save_pages */ save_pages, 1683 /* param_buf */ data, 1684 /* param_len */ datalen, 1685 /* sense_len */ SSD_FULL_SIZE, 1686 /* timeout */ timeout ? timeout : 5000); 1687 1688 if (arglist & CAM_ARG_ERR_RECOVER) 1689 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1690 1691 /* Disable freezing the device queue */ 1692 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1693 1694 if (((retval = cam_send_ccb(device, ccb)) < 0) 1695 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1696 if (arglist & CAM_ARG_VERBOSE) { 1697 cam_error_print(device, ccb, CAM_ESF_ALL, 1698 CAM_EPF_ALL, stderr); 1699 } 1700 cam_freeccb(ccb); 1701 cam_close_device(device); 1702 1703 if (retval < 0) 1704 err(1, "error sending mode select command"); 1705 else 1706 errx(1, "error sending mode select command"); 1707 1708 } 1709 1710 cam_freeccb(ccb); 1711 } 1712 1713 void 1714 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt, 1715 int retry_count, int timeout) 1716 { 1717 int c, mode_page = -1, page_control = 0; 1718 int binary = 0, list = 0; 1719 1720 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1721 switch(c) { 1722 case 'b': 1723 binary = 1; 1724 break; 1725 case 'd': 1726 arglist |= CAM_ARG_DBD; 1727 break; 1728 case 'e': 1729 arglist |= CAM_ARG_MODE_EDIT; 1730 break; 1731 case 'l': 1732 list = 1; 1733 break; 1734 case 'm': 1735 mode_page = strtol(optarg, NULL, 0); 1736 if (mode_page < 0) 1737 errx(1, "invalid mode page %d", mode_page); 1738 break; 1739 case 'P': 1740 page_control = strtol(optarg, NULL, 0); 1741 if ((page_control < 0) || (page_control > 3)) 1742 errx(1, "invalid page control field %d", 1743 page_control); 1744 arglist |= CAM_ARG_PAGE_CNTL; 1745 break; 1746 default: 1747 break; 1748 } 1749 } 1750 1751 if (mode_page == -1 && list == 0) 1752 errx(1, "you must specify a mode page!"); 1753 1754 if (list) { 1755 mode_list(device, page_control, arglist & CAM_ARG_DBD, 1756 retry_count, timeout); 1757 } else { 1758 mode_edit(device, mode_page, page_control, 1759 arglist & CAM_ARG_DBD, arglist & CAM_ARG_MODE_EDIT, binary, 1760 retry_count, timeout); 1761 } 1762 } 1763 1764 static int 1765 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt, 1766 int retry_count, int timeout) 1767 { 1768 union ccb *ccb; 1769 u_int32_t flags = CAM_DIR_NONE; 1770 u_int8_t *data_ptr = NULL; 1771 u_int8_t cdb[20]; 1772 struct get_hook hook; 1773 int c, data_bytes = 0; 1774 int cdb_len = 0; 1775 char *datastr = NULL, *tstr; 1776 int error = 0; 1777 int fd_data = 0; 1778 int retval; 1779 1780 ccb = cam_getccb(device); 1781 1782 if (ccb == NULL) { 1783 warnx("scsicmd: error allocating ccb"); 1784 return(1); 1785 } 1786 1787 bzero(&(&ccb->ccb_h)[1], 1788 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1789 1790 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1791 switch(c) { 1792 case 'c': 1793 tstr = optarg; 1794 while (isspace(*tstr) && (*tstr != '\0')) 1795 tstr++; 1796 hook.argc = argc - optind; 1797 hook.argv = argv + optind; 1798 hook.got = 0; 1799 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr, 1800 iget, &hook); 1801 /* 1802 * Increment optind by the number of arguments the 1803 * encoding routine processed. After each call to 1804 * getopt(3), optind points to the argument that 1805 * getopt should process _next_. In this case, 1806 * that means it points to the first command string 1807 * argument, if there is one. Once we increment 1808 * this, it should point to either the next command 1809 * line argument, or it should be past the end of 1810 * the list. 1811 */ 1812 optind += hook.got; 1813 break; 1814 case 'i': 1815 if (arglist & CAM_ARG_CMD_OUT) { 1816 warnx("command must either be " 1817 "read or write, not both"); 1818 error = 1; 1819 goto scsicmd_bailout; 1820 } 1821 arglist |= CAM_ARG_CMD_IN; 1822 flags = CAM_DIR_IN; 1823 data_bytes = strtol(optarg, NULL, 0); 1824 if (data_bytes <= 0) { 1825 warnx("invalid number of input bytes %d", 1826 data_bytes); 1827 error = 1; 1828 goto scsicmd_bailout; 1829 } 1830 hook.argc = argc - optind; 1831 hook.argv = argv + optind; 1832 hook.got = 0; 1833 optind++; 1834 datastr = cget(&hook, NULL); 1835 /* 1836 * If the user supplied "-" instead of a format, he 1837 * wants the data to be written to stdout. 1838 */ 1839 if ((datastr != NULL) 1840 && (datastr[0] == '-')) 1841 fd_data = 1; 1842 1843 data_ptr = (u_int8_t *)malloc(data_bytes); 1844 if (data_ptr == NULL) { 1845 warnx("can't malloc memory for data_ptr"); 1846 error = 1; 1847 goto scsicmd_bailout; 1848 } 1849 break; 1850 case 'o': 1851 if (arglist & CAM_ARG_CMD_IN) { 1852 warnx("command must either be " 1853 "read or write, not both"); 1854 error = 1; 1855 goto scsicmd_bailout; 1856 } 1857 arglist |= CAM_ARG_CMD_OUT; 1858 flags = CAM_DIR_OUT; 1859 data_bytes = strtol(optarg, NULL, 0); 1860 if (data_bytes <= 0) { 1861 warnx("invalid number of output bytes %d", 1862 data_bytes); 1863 error = 1; 1864 goto scsicmd_bailout; 1865 } 1866 hook.argc = argc - optind; 1867 hook.argv = argv + optind; 1868 hook.got = 0; 1869 datastr = cget(&hook, NULL); 1870 data_ptr = (u_int8_t *)malloc(data_bytes); 1871 if (data_ptr == NULL) { 1872 warnx("can't malloc memory for data_ptr"); 1873 error = 1; 1874 goto scsicmd_bailout; 1875 } 1876 /* 1877 * If the user supplied "-" instead of a format, he 1878 * wants the data to be read from stdin. 1879 */ 1880 if ((datastr != NULL) 1881 && (datastr[0] == '-')) 1882 fd_data = 1; 1883 else 1884 buff_encode_visit(data_ptr, data_bytes, datastr, 1885 iget, &hook); 1886 optind += hook.got; 1887 break; 1888 default: 1889 break; 1890 } 1891 } 1892 1893 /* 1894 * If fd_data is set, and we're writing to the device, we need to 1895 * read the data the user wants written from stdin. 1896 */ 1897 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) { 1898 ssize_t amt_read; 1899 int amt_to_read = data_bytes; 1900 u_int8_t *buf_ptr = data_ptr; 1901 1902 for (amt_read = 0; amt_to_read > 0; 1903 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) { 1904 if (amt_read == -1) { 1905 warn("error reading data from stdin"); 1906 error = 1; 1907 goto scsicmd_bailout; 1908 } 1909 amt_to_read -= amt_read; 1910 buf_ptr += amt_read; 1911 } 1912 } 1913 1914 if (arglist & CAM_ARG_ERR_RECOVER) 1915 flags |= CAM_PASS_ERR_RECOVER; 1916 1917 /* Disable freezing the device queue */ 1918 flags |= CAM_DEV_QFRZDIS; 1919 1920 /* 1921 * This is taken from the SCSI-3 draft spec. 1922 * (T10/1157D revision 0.3) 1923 * The top 3 bits of an opcode are the group code. The next 5 bits 1924 * are the command code. 1925 * Group 0: six byte commands 1926 * Group 1: ten byte commands 1927 * Group 2: ten byte commands 1928 * Group 3: reserved 1929 * Group 4: sixteen byte commands 1930 * Group 5: twelve byte commands 1931 * Group 6: vendor specific 1932 * Group 7: vendor specific 1933 */ 1934 switch((cdb[0] >> 5) & 0x7) { 1935 case 0: 1936 cdb_len = 6; 1937 break; 1938 case 1: 1939 case 2: 1940 cdb_len = 10; 1941 break; 1942 case 3: 1943 case 6: 1944 case 7: 1945 /* computed by buff_encode_visit */ 1946 break; 1947 case 4: 1948 cdb_len = 16; 1949 break; 1950 case 5: 1951 cdb_len = 12; 1952 break; 1953 } 1954 1955 /* 1956 * We should probably use csio_build_visit or something like that 1957 * here, but it's easier to encode arguments as you go. The 1958 * alternative would be skipping the CDB argument and then encoding 1959 * it here, since we've got the data buffer argument by now. 1960 */ 1961 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len); 1962 1963 cam_fill_csio(&ccb->csio, 1964 /*retries*/ retry_count, 1965 /*cbfcnp*/ NULL, 1966 /*flags*/ flags, 1967 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1968 /*data_ptr*/ data_ptr, 1969 /*dxfer_len*/ data_bytes, 1970 /*sense_len*/ SSD_FULL_SIZE, 1971 /*cdb_len*/ cdb_len, 1972 /*timeout*/ timeout ? timeout : 5000); 1973 1974 if (((retval = cam_send_ccb(device, ccb)) < 0) 1975 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1976 if (retval < 0) 1977 warn("error sending command"); 1978 else 1979 warnx("error sending command"); 1980 1981 if (arglist & CAM_ARG_VERBOSE) { 1982 cam_error_print(device, ccb, CAM_ESF_ALL, 1983 CAM_EPF_ALL, stderr); 1984 } 1985 1986 error = 1; 1987 goto scsicmd_bailout; 1988 } 1989 1990 1991 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1992 && (arglist & CAM_ARG_CMD_IN) 1993 && (data_bytes > 0)) { 1994 if (fd_data == 0) { 1995 buff_decode_visit(data_ptr, data_bytes, datastr, 1996 arg_put, NULL); 1997 fprintf(stdout, "\n"); 1998 } else { 1999 ssize_t amt_written; 2000 int amt_to_write = data_bytes; 2001 u_int8_t *buf_ptr = data_ptr; 2002 2003 for (amt_written = 0; (amt_to_write > 0) && 2004 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){ 2005 amt_to_write -= amt_written; 2006 buf_ptr += amt_written; 2007 } 2008 if (amt_written == -1) { 2009 warn("error writing data to stdout"); 2010 error = 1; 2011 goto scsicmd_bailout; 2012 } else if ((amt_written == 0) 2013 && (amt_to_write > 0)) { 2014 warnx("only wrote %u bytes out of %u", 2015 data_bytes - amt_to_write, data_bytes); 2016 } 2017 } 2018 } 2019 2020 scsicmd_bailout: 2021 2022 if ((data_bytes > 0) && (data_ptr != NULL)) 2023 free(data_ptr); 2024 2025 cam_freeccb(ccb); 2026 2027 return(error); 2028 } 2029 2030 static int 2031 camdebug(int argc, char **argv, char *combinedopt) 2032 { 2033 int c, fd; 2034 int bus = -1, target = -1, lun = -1; 2035 char *tstr, *tmpstr = NULL; 2036 union ccb ccb; 2037 int error = 0; 2038 2039 bzero(&ccb, sizeof(union ccb)); 2040 2041 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2042 switch(c) { 2043 case 'I': 2044 arglist |= CAM_ARG_DEBUG_INFO; 2045 ccb.cdbg.flags |= CAM_DEBUG_INFO; 2046 break; 2047 case 'P': 2048 arglist |= CAM_ARG_DEBUG_PERIPH; 2049 ccb.cdbg.flags |= CAM_DEBUG_PERIPH; 2050 break; 2051 case 'S': 2052 arglist |= CAM_ARG_DEBUG_SUBTRACE; 2053 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE; 2054 break; 2055 case 'T': 2056 arglist |= CAM_ARG_DEBUG_TRACE; 2057 ccb.cdbg.flags |= CAM_DEBUG_TRACE; 2058 break; 2059 case 'X': 2060 arglist |= CAM_ARG_DEBUG_XPT; 2061 ccb.cdbg.flags |= CAM_DEBUG_XPT; 2062 break; 2063 case 'c': 2064 arglist |= CAM_ARG_DEBUG_CDB; 2065 ccb.cdbg.flags |= CAM_DEBUG_CDB; 2066 break; 2067 default: 2068 break; 2069 } 2070 } 2071 2072 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 2073 warnx("error opening transport layer device %s", XPT_DEVICE); 2074 warn("%s", XPT_DEVICE); 2075 return(1); 2076 } 2077 argc -= optind; 2078 argv += optind; 2079 2080 if (argc <= 0) { 2081 warnx("you must specify \"off\", \"all\" or a bus,"); 2082 warnx("bus:target, or bus:target:lun"); 2083 close(fd); 2084 return(1); 2085 } 2086 2087 tstr = *argv; 2088 2089 while (isspace(*tstr) && (*tstr != '\0')) 2090 tstr++; 2091 2092 if (strncmp(tstr, "off", 3) == 0) { 2093 ccb.cdbg.flags = CAM_DEBUG_NONE; 2094 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_PERIPH| 2095 CAM_ARG_DEBUG_TRACE|CAM_ARG_DEBUG_SUBTRACE| 2096 CAM_ARG_DEBUG_XPT); 2097 } else if (strncmp(tstr, "all", 3) != 0) { 2098 tmpstr = (char *)strtok(tstr, ":"); 2099 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 2100 bus = strtol(tmpstr, NULL, 0); 2101 arglist |= CAM_ARG_BUS; 2102 tmpstr = (char *)strtok(NULL, ":"); 2103 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 2104 target = strtol(tmpstr, NULL, 0); 2105 arglist |= CAM_ARG_TARGET; 2106 tmpstr = (char *)strtok(NULL, ":"); 2107 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 2108 lun = strtol(tmpstr, NULL, 0); 2109 arglist |= CAM_ARG_LUN; 2110 } 2111 } 2112 } else { 2113 error = 1; 2114 warnx("you must specify \"all\", \"off\", or a bus,"); 2115 warnx("bus:target, or bus:target:lun to debug"); 2116 } 2117 } 2118 2119 if (error == 0) { 2120 2121 ccb.ccb_h.func_code = XPT_DEBUG; 2122 ccb.ccb_h.path_id = bus; 2123 ccb.ccb_h.target_id = target; 2124 ccb.ccb_h.target_lun = lun; 2125 2126 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 2127 warn("CAMIOCOMMAND ioctl failed"); 2128 error = 1; 2129 } 2130 2131 if (error == 0) { 2132 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == 2133 CAM_FUNC_NOTAVAIL) { 2134 warnx("CAM debugging not available"); 2135 warnx("you need to put options CAMDEBUG in" 2136 " your kernel config file!"); 2137 error = 1; 2138 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) != 2139 CAM_REQ_CMP) { 2140 warnx("XPT_DEBUG CCB failed with status %#x", 2141 ccb.ccb_h.status); 2142 error = 1; 2143 } else { 2144 if (ccb.cdbg.flags == CAM_DEBUG_NONE) { 2145 fprintf(stderr, 2146 "Debugging turned off\n"); 2147 } else { 2148 fprintf(stderr, 2149 "Debugging enabled for " 2150 "%d:%d:%d\n", 2151 bus, target, lun); 2152 } 2153 } 2154 } 2155 close(fd); 2156 } 2157 2158 return(error); 2159 } 2160 2161 static int 2162 tagcontrol(struct cam_device *device, int argc, char **argv, 2163 char *combinedopt) 2164 { 2165 int c; 2166 union ccb *ccb; 2167 int numtags = -1; 2168 int retval = 0; 2169 int quiet = 0; 2170 char pathstr[1024]; 2171 2172 ccb = cam_getccb(device); 2173 2174 if (ccb == NULL) { 2175 warnx("tagcontrol: error allocating ccb"); 2176 return(1); 2177 } 2178 2179 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2180 switch(c) { 2181 case 'N': 2182 numtags = strtol(optarg, NULL, 0); 2183 if (numtags < 0) { 2184 warnx("tag count %d is < 0", numtags); 2185 retval = 1; 2186 goto tagcontrol_bailout; 2187 } 2188 break; 2189 case 'q': 2190 quiet++; 2191 break; 2192 default: 2193 break; 2194 } 2195 } 2196 2197 cam_path_string(device, pathstr, sizeof(pathstr)); 2198 2199 if (numtags >= 0) { 2200 bzero(&(&ccb->ccb_h)[1], 2201 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr)); 2202 ccb->ccb_h.func_code = XPT_REL_SIMQ; 2203 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS; 2204 ccb->crs.openings = numtags; 2205 2206 2207 if (cam_send_ccb(device, ccb) < 0) { 2208 perror("error sending XPT_REL_SIMQ CCB"); 2209 retval = 1; 2210 goto tagcontrol_bailout; 2211 } 2212 2213 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2214 warnx("XPT_REL_SIMQ CCB failed"); 2215 cam_error_print(device, ccb, CAM_ESF_ALL, 2216 CAM_EPF_ALL, stderr); 2217 retval = 1; 2218 goto tagcontrol_bailout; 2219 } 2220 2221 2222 if (quiet == 0) 2223 fprintf(stdout, "%stagged openings now %d\n", 2224 pathstr, ccb->crs.openings); 2225 } 2226 2227 bzero(&(&ccb->ccb_h)[1], 2228 sizeof(struct ccb_getdevstats) - sizeof(struct ccb_hdr)); 2229 2230 ccb->ccb_h.func_code = XPT_GDEV_STATS; 2231 2232 if (cam_send_ccb(device, ccb) < 0) { 2233 perror("error sending XPT_GDEV_STATS CCB"); 2234 retval = 1; 2235 goto tagcontrol_bailout; 2236 } 2237 2238 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2239 warnx("XPT_GDEV_STATS CCB failed"); 2240 cam_error_print(device, ccb, CAM_ESF_ALL, 2241 CAM_EPF_ALL, stderr); 2242 retval = 1; 2243 goto tagcontrol_bailout; 2244 } 2245 2246 if (arglist & CAM_ARG_VERBOSE) { 2247 fprintf(stdout, "%s", pathstr); 2248 fprintf(stdout, "dev_openings %d\n", ccb->cgds.dev_openings); 2249 fprintf(stdout, "%s", pathstr); 2250 fprintf(stdout, "dev_active %d\n", ccb->cgds.dev_active); 2251 fprintf(stdout, "%s", pathstr); 2252 fprintf(stdout, "devq_openings %d\n", ccb->cgds.devq_openings); 2253 fprintf(stdout, "%s", pathstr); 2254 fprintf(stdout, "devq_queued %d\n", ccb->cgds.devq_queued); 2255 fprintf(stdout, "%s", pathstr); 2256 fprintf(stdout, "held %d\n", ccb->cgds.held); 2257 fprintf(stdout, "%s", pathstr); 2258 fprintf(stdout, "mintags %d\n", ccb->cgds.mintags); 2259 fprintf(stdout, "%s", pathstr); 2260 fprintf(stdout, "maxtags %d\n", ccb->cgds.maxtags); 2261 } else { 2262 if (quiet == 0) { 2263 fprintf(stdout, "%s", pathstr); 2264 fprintf(stdout, "device openings: "); 2265 } 2266 fprintf(stdout, "%d\n", ccb->cgds.dev_openings + 2267 ccb->cgds.dev_active); 2268 } 2269 2270 tagcontrol_bailout: 2271 2272 cam_freeccb(ccb); 2273 return(retval); 2274 } 2275 2276 static void 2277 cts_print(struct cam_device *device, struct ccb_trans_settings *cts) 2278 { 2279 char pathstr[1024]; 2280 2281 cam_path_string(device, pathstr, sizeof(pathstr)); 2282 2283 if (cts->transport == XPORT_SPI) { 2284 struct ccb_trans_settings_spi *spi = 2285 &cts->xport_specific.spi; 2286 2287 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0) { 2288 2289 fprintf(stdout, "%ssync parameter: %d\n", pathstr, 2290 spi->sync_period); 2291 2292 if (spi->sync_offset != 0) { 2293 u_int freq; 2294 2295 freq = scsi_calc_syncsrate(spi->sync_period); 2296 fprintf(stdout, "%sfrequency: %d.%03dMHz\n", 2297 pathstr, freq / 1000, freq % 1000); 2298 } 2299 } 2300 2301 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) { 2302 fprintf(stdout, "%soffset: %d\n", pathstr, 2303 spi->sync_offset); 2304 } 2305 2306 if (spi->valid & CTS_SPI_VALID_BUS_WIDTH) { 2307 fprintf(stdout, "%sbus width: %d bits\n", pathstr, 2308 (0x01 << spi->bus_width) * 8); 2309 } 2310 2311 if (spi->valid & CTS_SPI_VALID_DISC) { 2312 fprintf(stdout, "%sdisconnection is %s\n", pathstr, 2313 (spi->flags & CTS_SPI_FLAGS_DISC_ENB) ? 2314 "enabled" : "disabled"); 2315 } 2316 } 2317 2318 if (cts->protocol == PROTO_SCSI) { 2319 struct ccb_trans_settings_scsi *scsi= 2320 &cts->proto_specific.scsi; 2321 2322 if (scsi->valid & CTS_SCSI_VALID_TQ) { 2323 fprintf(stdout, "%stagged queueing is %s\n", pathstr, 2324 (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) ? 2325 "enabled" : "disabled"); 2326 } 2327 } 2328 2329 } 2330 2331 /* 2332 * Get a path inquiry CCB for the specified device. 2333 */ 2334 static int 2335 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi) 2336 { 2337 union ccb *ccb; 2338 int retval = 0; 2339 2340 ccb = cam_getccb(device); 2341 2342 if (ccb == NULL) { 2343 warnx("get_cpi: couldn't allocate CCB"); 2344 return(1); 2345 } 2346 2347 bzero(&(&ccb->ccb_h)[1], 2348 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2349 2350 ccb->ccb_h.func_code = XPT_PATH_INQ; 2351 2352 if (cam_send_ccb(device, ccb) < 0) { 2353 warn("get_cpi: error sending Path Inquiry CCB"); 2354 2355 if (arglist & CAM_ARG_VERBOSE) 2356 cam_error_print(device, ccb, CAM_ESF_ALL, 2357 CAM_EPF_ALL, stderr); 2358 2359 retval = 1; 2360 2361 goto get_cpi_bailout; 2362 } 2363 2364 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2365 2366 if (arglist & CAM_ARG_VERBOSE) 2367 cam_error_print(device, ccb, CAM_ESF_ALL, 2368 CAM_EPF_ALL, stderr); 2369 2370 retval = 1; 2371 2372 goto get_cpi_bailout; 2373 } 2374 2375 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq)); 2376 2377 get_cpi_bailout: 2378 2379 cam_freeccb(ccb); 2380 2381 return(retval); 2382 } 2383 2384 static void 2385 cpi_print(struct ccb_pathinq *cpi) 2386 { 2387 char adapter_str[1024]; 2388 int i; 2389 2390 snprintf(adapter_str, sizeof(adapter_str), 2391 "%s%d:", cpi->dev_name, cpi->unit_number); 2392 2393 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str, 2394 cpi->version_num); 2395 2396 for (i = 1; i < 0xff; i = i << 1) { 2397 const char *str; 2398 2399 if ((i & cpi->hba_inquiry) == 0) 2400 continue; 2401 2402 fprintf(stdout, "%s supports ", adapter_str); 2403 2404 switch(i) { 2405 case PI_MDP_ABLE: 2406 str = "MDP message"; 2407 break; 2408 case PI_WIDE_32: 2409 str = "32 bit wide SCSI"; 2410 break; 2411 case PI_WIDE_16: 2412 str = "16 bit wide SCSI"; 2413 break; 2414 case PI_SDTR_ABLE: 2415 str = "SDTR message"; 2416 break; 2417 case PI_LINKED_CDB: 2418 str = "linked CDBs"; 2419 break; 2420 case PI_TAG_ABLE: 2421 str = "tag queue messages"; 2422 break; 2423 case PI_SOFT_RST: 2424 str = "soft reset alternative"; 2425 break; 2426 default: 2427 str = "unknown PI bit set"; 2428 break; 2429 } 2430 fprintf(stdout, "%s\n", str); 2431 } 2432 2433 for (i = 1; i < 0xff; i = i << 1) { 2434 const char *str; 2435 2436 if ((i & cpi->hba_misc) == 0) 2437 continue; 2438 2439 fprintf(stdout, "%s ", adapter_str); 2440 2441 switch(i) { 2442 case PIM_SCANHILO: 2443 str = "bus scans from high ID to low ID"; 2444 break; 2445 case PIM_NOREMOVE: 2446 str = "removable devices not included in scan"; 2447 break; 2448 case PIM_NOINITIATOR: 2449 str = "initiator role not supported"; 2450 break; 2451 case PIM_NOBUSRESET: 2452 str = "user has disabled initial BUS RESET or" 2453 " controller is in target/mixed mode"; 2454 break; 2455 default: 2456 str = "unknown PIM bit set"; 2457 break; 2458 } 2459 fprintf(stdout, "%s\n", str); 2460 } 2461 2462 for (i = 1; i < 0xff; i = i << 1) { 2463 const char *str; 2464 2465 if ((i & cpi->target_sprt) == 0) 2466 continue; 2467 2468 fprintf(stdout, "%s supports ", adapter_str); 2469 switch(i) { 2470 case PIT_PROCESSOR: 2471 str = "target mode processor mode"; 2472 break; 2473 case PIT_PHASE: 2474 str = "target mode phase cog. mode"; 2475 break; 2476 case PIT_DISCONNECT: 2477 str = "disconnects in target mode"; 2478 break; 2479 case PIT_TERM_IO: 2480 str = "terminate I/O message in target mode"; 2481 break; 2482 case PIT_GRP_6: 2483 str = "group 6 commands in target mode"; 2484 break; 2485 case PIT_GRP_7: 2486 str = "group 7 commands in target mode"; 2487 break; 2488 default: 2489 str = "unknown PIT bit set"; 2490 break; 2491 } 2492 2493 fprintf(stdout, "%s\n", str); 2494 } 2495 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str, 2496 cpi->hba_eng_cnt); 2497 fprintf(stdout, "%s maximum target: %d\n", adapter_str, 2498 cpi->max_target); 2499 fprintf(stdout, "%s maximum LUN: %d\n", adapter_str, 2500 cpi->max_lun); 2501 fprintf(stdout, "%s highest path ID in subsystem: %d\n", 2502 adapter_str, cpi->hpath_id); 2503 fprintf(stdout, "%s initiator ID: %d\n", adapter_str, 2504 cpi->initiator_id); 2505 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid); 2506 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid); 2507 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id); 2508 fprintf(stdout, "%s base transfer speed: ", adapter_str); 2509 if (cpi->base_transfer_speed > 1000) 2510 fprintf(stdout, "%d.%03dMB/sec\n", 2511 cpi->base_transfer_speed / 1000, 2512 cpi->base_transfer_speed % 1000); 2513 else 2514 fprintf(stdout, "%dKB/sec\n", 2515 (cpi->base_transfer_speed % 1000) * 1000); 2516 } 2517 2518 static int 2519 get_print_cts(struct cam_device *device, int user_settings, int quiet, 2520 struct ccb_trans_settings *cts) 2521 { 2522 int retval; 2523 union ccb *ccb; 2524 2525 retval = 0; 2526 ccb = cam_getccb(device); 2527 2528 if (ccb == NULL) { 2529 warnx("get_print_cts: error allocating ccb"); 2530 return(1); 2531 } 2532 2533 bzero(&(&ccb->ccb_h)[1], 2534 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2535 2536 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 2537 2538 if (user_settings == 0) 2539 ccb->cts.type = CTS_TYPE_CURRENT_SETTINGS; 2540 else 2541 ccb->cts.type = CTS_TYPE_USER_SETTINGS; 2542 2543 if (cam_send_ccb(device, ccb) < 0) { 2544 perror("error sending XPT_GET_TRAN_SETTINGS CCB"); 2545 if (arglist & CAM_ARG_VERBOSE) 2546 cam_error_print(device, ccb, CAM_ESF_ALL, 2547 CAM_EPF_ALL, stderr); 2548 retval = 1; 2549 goto get_print_cts_bailout; 2550 } 2551 2552 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2553 warnx("XPT_GET_TRANS_SETTINGS CCB failed"); 2554 if (arglist & CAM_ARG_VERBOSE) 2555 cam_error_print(device, ccb, CAM_ESF_ALL, 2556 CAM_EPF_ALL, stderr); 2557 retval = 1; 2558 goto get_print_cts_bailout; 2559 } 2560 2561 if (quiet == 0) 2562 cts_print(device, &ccb->cts); 2563 2564 if (cts != NULL) 2565 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings)); 2566 2567 get_print_cts_bailout: 2568 2569 cam_freeccb(ccb); 2570 2571 return(retval); 2572 } 2573 2574 static int 2575 ratecontrol(struct cam_device *device, int retry_count, int timeout, 2576 int argc, char **argv, char *combinedopt) 2577 { 2578 int c; 2579 union ccb *ccb; 2580 int user_settings = 0; 2581 int retval = 0; 2582 int disc_enable = -1, tag_enable = -1; 2583 int offset = -1; 2584 double syncrate = -1; 2585 int bus_width = -1; 2586 int quiet = 0; 2587 int change_settings = 0, send_tur = 0; 2588 struct ccb_pathinq cpi; 2589 2590 ccb = cam_getccb(device); 2591 2592 if (ccb == NULL) { 2593 warnx("ratecontrol: error allocating ccb"); 2594 return(1); 2595 } 2596 2597 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2598 switch(c){ 2599 case 'a': 2600 send_tur = 1; 2601 break; 2602 case 'c': 2603 user_settings = 0; 2604 break; 2605 case 'D': 2606 if (strncasecmp(optarg, "enable", 6) == 0) 2607 disc_enable = 1; 2608 else if (strncasecmp(optarg, "disable", 7) == 0) 2609 disc_enable = 0; 2610 else { 2611 warnx("-D argument \"%s\" is unknown", optarg); 2612 retval = 1; 2613 goto ratecontrol_bailout; 2614 } 2615 change_settings = 1; 2616 break; 2617 case 'O': 2618 offset = strtol(optarg, NULL, 0); 2619 if (offset < 0) { 2620 warnx("offset value %d is < 0", offset); 2621 retval = 1; 2622 goto ratecontrol_bailout; 2623 } 2624 change_settings = 1; 2625 break; 2626 case 'q': 2627 quiet++; 2628 break; 2629 case 'R': 2630 syncrate = atof(optarg); 2631 2632 if (syncrate < 0) { 2633 warnx("sync rate %f is < 0", syncrate); 2634 retval = 1; 2635 goto ratecontrol_bailout; 2636 } 2637 change_settings = 1; 2638 break; 2639 case 'T': 2640 if (strncasecmp(optarg, "enable", 6) == 0) 2641 tag_enable = 1; 2642 else if (strncasecmp(optarg, "disable", 7) == 0) 2643 tag_enable = 0; 2644 else { 2645 warnx("-T argument \"%s\" is unknown", optarg); 2646 retval = 1; 2647 goto ratecontrol_bailout; 2648 } 2649 change_settings = 1; 2650 break; 2651 case 'U': 2652 user_settings = 1; 2653 break; 2654 case 'W': 2655 bus_width = strtol(optarg, NULL, 0); 2656 if (bus_width < 0) { 2657 warnx("bus width %d is < 0", bus_width); 2658 retval = 1; 2659 goto ratecontrol_bailout; 2660 } 2661 change_settings = 1; 2662 break; 2663 default: 2664 break; 2665 } 2666 } 2667 2668 bzero(&(&ccb->ccb_h)[1], 2669 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2670 2671 /* 2672 * Grab path inquiry information, so we can determine whether 2673 * or not the initiator is capable of the things that the user 2674 * requests. 2675 */ 2676 ccb->ccb_h.func_code = XPT_PATH_INQ; 2677 2678 if (cam_send_ccb(device, ccb) < 0) { 2679 perror("error sending XPT_PATH_INQ CCB"); 2680 if (arglist & CAM_ARG_VERBOSE) { 2681 cam_error_print(device, ccb, CAM_ESF_ALL, 2682 CAM_EPF_ALL, stderr); 2683 } 2684 retval = 1; 2685 goto ratecontrol_bailout; 2686 } 2687 2688 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2689 warnx("XPT_PATH_INQ CCB failed"); 2690 if (arglist & CAM_ARG_VERBOSE) { 2691 cam_error_print(device, ccb, CAM_ESF_ALL, 2692 CAM_EPF_ALL, stderr); 2693 } 2694 retval = 1; 2695 goto ratecontrol_bailout; 2696 } 2697 2698 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq)); 2699 2700 bzero(&(&ccb->ccb_h)[1], 2701 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2702 2703 if (quiet == 0) 2704 fprintf(stdout, "Current Parameters:\n"); 2705 2706 retval = get_print_cts(device, user_settings, quiet, &ccb->cts); 2707 2708 if (retval != 0) 2709 goto ratecontrol_bailout; 2710 2711 if (arglist & CAM_ARG_VERBOSE) 2712 cpi_print(&cpi); 2713 2714 if (change_settings) { 2715 int didsettings = 0; 2716 struct ccb_trans_settings_spi *spi = NULL; 2717 struct ccb_trans_settings_scsi *scsi = NULL; 2718 2719 if (ccb->cts.transport == XPORT_SPI) { 2720 spi = &ccb->cts.xport_specific.spi; 2721 spi->valid = 0; 2722 } 2723 if (ccb->cts.protocol == PROTO_SCSI) { 2724 scsi = &ccb->cts.proto_specific.scsi; 2725 scsi->valid = 0; 2726 } 2727 if (spi && disc_enable != -1) { 2728 spi->valid |= CTS_SPI_VALID_DISC; 2729 if (disc_enable == 0) 2730 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB; 2731 else 2732 spi->flags |= CTS_SPI_FLAGS_DISC_ENB; 2733 } 2734 2735 if (scsi && tag_enable != -1) { 2736 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) { 2737 warnx("HBA does not support tagged queueing, " 2738 "so you cannot modify tag settings"); 2739 retval = 1; 2740 goto ratecontrol_bailout; 2741 } 2742 2743 scsi->valid |= CTS_SCSI_VALID_TQ; 2744 2745 if (tag_enable == 0) 2746 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB; 2747 else 2748 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB; 2749 didsettings++; 2750 } 2751 2752 if (spi && offset != -1) { 2753 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2754 warnx("HBA at %s%d is not cable of changing " 2755 "offset", cpi.dev_name, 2756 cpi.unit_number); 2757 retval = 1; 2758 goto ratecontrol_bailout; 2759 } 2760 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET; 2761 spi->sync_offset = offset; 2762 didsettings++; 2763 } 2764 2765 if (spi && syncrate != -1) { 2766 int prelim_sync_period; 2767 u_int freq; 2768 2769 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2770 warnx("HBA at %s%d is not cable of changing " 2771 "transfer rates", cpi.dev_name, 2772 cpi.unit_number); 2773 retval = 1; 2774 goto ratecontrol_bailout; 2775 } 2776 2777 spi->valid |= CTS_SPI_VALID_SYNC_RATE; 2778 2779 /* 2780 * The sync rate the user gives us is in MHz. 2781 * We need to translate it into KHz for this 2782 * calculation. 2783 */ 2784 syncrate *= 1000; 2785 2786 /* 2787 * Next, we calculate a "preliminary" sync period 2788 * in tenths of a nanosecond. 2789 */ 2790 if (syncrate == 0) 2791 prelim_sync_period = 0; 2792 else 2793 prelim_sync_period = 10000000 / syncrate; 2794 2795 spi->sync_period = 2796 scsi_calc_syncparam(prelim_sync_period); 2797 2798 freq = scsi_calc_syncsrate(spi->sync_period); 2799 didsettings++; 2800 } 2801 2802 /* 2803 * The bus_width argument goes like this: 2804 * 0 == 8 bit 2805 * 1 == 16 bit 2806 * 2 == 32 bit 2807 * Therefore, if you shift the number of bits given on the 2808 * command line right by 4, you should get the correct 2809 * number. 2810 */ 2811 if (spi && bus_width != -1) { 2812 2813 /* 2814 * We might as well validate things here with a 2815 * decipherable error message, rather than what 2816 * will probably be an indecipherable error message 2817 * by the time it gets back to us. 2818 */ 2819 if ((bus_width == 16) 2820 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) { 2821 warnx("HBA does not support 16 bit bus width"); 2822 retval = 1; 2823 goto ratecontrol_bailout; 2824 } else if ((bus_width == 32) 2825 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) { 2826 warnx("HBA does not support 32 bit bus width"); 2827 retval = 1; 2828 goto ratecontrol_bailout; 2829 } else if ((bus_width != 8) 2830 && (bus_width != 16) 2831 && (bus_width != 32)) { 2832 warnx("Invalid bus width %d", bus_width); 2833 retval = 1; 2834 goto ratecontrol_bailout; 2835 } 2836 2837 spi->valid |= CTS_SPI_VALID_BUS_WIDTH; 2838 spi->bus_width = bus_width >> 4; 2839 didsettings++; 2840 } 2841 2842 if (didsettings == 0) { 2843 goto ratecontrol_bailout; 2844 } 2845 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 2846 2847 if (cam_send_ccb(device, ccb) < 0) { 2848 perror("error sending XPT_SET_TRAN_SETTINGS CCB"); 2849 if (arglist & CAM_ARG_VERBOSE) { 2850 cam_error_print(device, ccb, CAM_ESF_ALL, 2851 CAM_EPF_ALL, stderr); 2852 } 2853 retval = 1; 2854 goto ratecontrol_bailout; 2855 } 2856 2857 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2858 warnx("XPT_SET_TRANS_SETTINGS CCB failed"); 2859 if (arglist & CAM_ARG_VERBOSE) { 2860 cam_error_print(device, ccb, CAM_ESF_ALL, 2861 CAM_EPF_ALL, stderr); 2862 } 2863 retval = 1; 2864 goto ratecontrol_bailout; 2865 } 2866 } 2867 2868 if (send_tur) { 2869 retval = testunitready(device, retry_count, timeout, 2870 (arglist & CAM_ARG_VERBOSE) ? 0 : 1); 2871 2872 /* 2873 * If the TUR didn't succeed, just bail. 2874 */ 2875 if (retval != 0) { 2876 if (quiet == 0) 2877 fprintf(stderr, "Test Unit Ready failed\n"); 2878 goto ratecontrol_bailout; 2879 } 2880 2881 /* 2882 * If the user wants things quiet, there's no sense in 2883 * getting the transfer settings, if we're not going 2884 * to print them. 2885 */ 2886 if (quiet != 0) 2887 goto ratecontrol_bailout; 2888 2889 fprintf(stdout, "New Parameters:\n"); 2890 retval = get_print_cts(device, user_settings, 0, NULL); 2891 } 2892 2893 ratecontrol_bailout: 2894 2895 cam_freeccb(ccb); 2896 return(retval); 2897 } 2898 2899 static int 2900 scsiformat(struct cam_device *device, int argc, char **argv, 2901 char *combinedopt, int retry_count, int timeout) 2902 { 2903 union ccb *ccb; 2904 int c; 2905 int ycount = 0, quiet = 0; 2906 int error = 0, response = 0, retval = 0; 2907 int use_timeout = 10800 * 1000; 2908 int immediate = 1; 2909 struct format_defect_list_header fh; 2910 u_int8_t *data_ptr = NULL; 2911 u_int32_t dxfer_len = 0; 2912 u_int8_t byte2 = 0; 2913 int num_warnings = 0; 2914 int reportonly = 0; 2915 2916 ccb = cam_getccb(device); 2917 2918 if (ccb == NULL) { 2919 warnx("scsiformat: error allocating ccb"); 2920 return(1); 2921 } 2922 2923 bzero(&(&ccb->ccb_h)[1], 2924 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 2925 2926 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2927 switch(c) { 2928 case 'q': 2929 quiet++; 2930 break; 2931 case 'r': 2932 reportonly = 1; 2933 break; 2934 case 'w': 2935 immediate = 0; 2936 break; 2937 case 'y': 2938 ycount++; 2939 break; 2940 } 2941 } 2942 2943 if (reportonly) 2944 goto doreport; 2945 2946 if (quiet == 0) { 2947 fprintf(stdout, "You are about to REMOVE ALL DATA from the " 2948 "following device:\n"); 2949 2950 error = scsidoinquiry(device, argc, argv, combinedopt, 2951 retry_count, timeout); 2952 2953 if (error != 0) { 2954 warnx("scsiformat: error sending inquiry"); 2955 goto scsiformat_bailout; 2956 } 2957 } 2958 2959 if (ycount == 0) { 2960 2961 do { 2962 char str[1024]; 2963 2964 fprintf(stdout, "Are you SURE you want to do " 2965 "this? (yes/no) "); 2966 2967 if (fgets(str, sizeof(str), stdin) != NULL) { 2968 2969 if (strncasecmp(str, "yes", 3) == 0) 2970 response = 1; 2971 else if (strncasecmp(str, "no", 2) == 0) 2972 response = -1; 2973 else { 2974 fprintf(stdout, "Please answer" 2975 " \"yes\" or \"no\"\n"); 2976 } 2977 } 2978 } while (response == 0); 2979 2980 if (response == -1) { 2981 error = 1; 2982 goto scsiformat_bailout; 2983 } 2984 } 2985 2986 if (timeout != 0) 2987 use_timeout = timeout; 2988 2989 if (quiet == 0) { 2990 fprintf(stdout, "Current format timeout is %d seconds\n", 2991 use_timeout / 1000); 2992 } 2993 2994 /* 2995 * If the user hasn't disabled questions and didn't specify a 2996 * timeout on the command line, ask them if they want the current 2997 * timeout. 2998 */ 2999 if ((ycount == 0) 3000 && (timeout == 0)) { 3001 char str[1024]; 3002 int new_timeout = 0; 3003 3004 fprintf(stdout, "Enter new timeout in seconds or press\n" 3005 "return to keep the current timeout [%d] ", 3006 use_timeout / 1000); 3007 3008 if (fgets(str, sizeof(str), stdin) != NULL) { 3009 if (str[0] != '\0') 3010 new_timeout = atoi(str); 3011 } 3012 3013 if (new_timeout != 0) { 3014 use_timeout = new_timeout * 1000; 3015 fprintf(stdout, "Using new timeout value %d\n", 3016 use_timeout / 1000); 3017 } 3018 } 3019 3020 /* 3021 * Keep this outside the if block below to silence any unused 3022 * variable warnings. 3023 */ 3024 bzero(&fh, sizeof(fh)); 3025 3026 /* 3027 * If we're in immediate mode, we've got to include the format 3028 * header 3029 */ 3030 if (immediate != 0) { 3031 fh.byte2 = FU_DLH_IMMED; 3032 data_ptr = (u_int8_t *)&fh; 3033 dxfer_len = sizeof(fh); 3034 byte2 = FU_FMT_DATA; 3035 } else if (quiet == 0) { 3036 fprintf(stdout, "Formatting..."); 3037 fflush(stdout); 3038 } 3039 3040 scsi_format_unit(&ccb->csio, 3041 /* retries */ retry_count, 3042 /* cbfcnp */ NULL, 3043 /* tag_action */ MSG_SIMPLE_Q_TAG, 3044 /* byte2 */ byte2, 3045 /* ileave */ 0, 3046 /* data_ptr */ data_ptr, 3047 /* dxfer_len */ dxfer_len, 3048 /* sense_len */ SSD_FULL_SIZE, 3049 /* timeout */ use_timeout); 3050 3051 /* Disable freezing the device queue */ 3052 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 3053 3054 if (arglist & CAM_ARG_ERR_RECOVER) 3055 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 3056 3057 if (((retval = cam_send_ccb(device, ccb)) < 0) 3058 || ((immediate == 0) 3059 && ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP))) { 3060 const char errstr[] = "error sending format command"; 3061 3062 if (retval < 0) 3063 warn(errstr); 3064 else 3065 warnx(errstr); 3066 3067 if (arglist & CAM_ARG_VERBOSE) { 3068 cam_error_print(device, ccb, CAM_ESF_ALL, 3069 CAM_EPF_ALL, stderr); 3070 } 3071 error = 1; 3072 goto scsiformat_bailout; 3073 } 3074 3075 /* 3076 * If we ran in non-immediate mode, we already checked for errors 3077 * above and printed out any necessary information. If we're in 3078 * immediate mode, we need to loop through and get status 3079 * information periodically. 3080 */ 3081 if (immediate == 0) { 3082 if (quiet == 0) { 3083 fprintf(stdout, "Format Complete\n"); 3084 } 3085 goto scsiformat_bailout; 3086 } 3087 3088 doreport: 3089 do { 3090 cam_status status; 3091 3092 bzero(&(&ccb->ccb_h)[1], 3093 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 3094 3095 /* 3096 * There's really no need to do error recovery or 3097 * retries here, since we're just going to sit in a 3098 * loop and wait for the device to finish formatting. 3099 */ 3100 scsi_test_unit_ready(&ccb->csio, 3101 /* retries */ 0, 3102 /* cbfcnp */ NULL, 3103 /* tag_action */ MSG_SIMPLE_Q_TAG, 3104 /* sense_len */ SSD_FULL_SIZE, 3105 /* timeout */ 5000); 3106 3107 /* Disable freezing the device queue */ 3108 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 3109 3110 retval = cam_send_ccb(device, ccb); 3111 3112 /* 3113 * If we get an error from the ioctl, bail out. SCSI 3114 * errors are expected. 3115 */ 3116 if (retval < 0) { 3117 warn("error sending CAMIOCOMMAND ioctl"); 3118 if (arglist & CAM_ARG_VERBOSE) { 3119 cam_error_print(device, ccb, CAM_ESF_ALL, 3120 CAM_EPF_ALL, stderr); 3121 } 3122 error = 1; 3123 goto scsiformat_bailout; 3124 } 3125 3126 status = ccb->ccb_h.status & CAM_STATUS_MASK; 3127 3128 if ((status != CAM_REQ_CMP) 3129 && (status == CAM_SCSI_STATUS_ERROR) 3130 && ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0)) { 3131 struct scsi_sense_data *sense; 3132 int error_code, sense_key, asc, ascq; 3133 3134 sense = &ccb->csio.sense_data; 3135 scsi_extract_sense(sense, &error_code, &sense_key, 3136 &asc, &ascq); 3137 3138 /* 3139 * According to the SCSI-2 and SCSI-3 specs, a 3140 * drive that is in the middle of a format should 3141 * return NOT READY with an ASC of "logical unit 3142 * not ready, format in progress". The sense key 3143 * specific bytes will then be a progress indicator. 3144 */ 3145 if ((sense_key == SSD_KEY_NOT_READY) 3146 && (asc == 0x04) && (ascq == 0x04)) { 3147 if ((sense->extra_len >= 10) 3148 && ((sense->sense_key_spec[0] & 3149 SSD_SCS_VALID) != 0) 3150 && (quiet == 0)) { 3151 int val; 3152 u_int64_t percentage; 3153 3154 val = scsi_2btoul( 3155 &sense->sense_key_spec[1]); 3156 percentage = 10000 * val; 3157 3158 fprintf(stdout, 3159 "\rFormatting: %ju.%02u %% " 3160 "(%d/%d) done", 3161 (uintmax_t)(percentage / 3162 (0x10000 * 100)), 3163 (unsigned)((percentage / 3164 0x10000) % 100), 3165 val, 0x10000); 3166 fflush(stdout); 3167 } else if ((quiet == 0) 3168 && (++num_warnings <= 1)) { 3169 warnx("Unexpected SCSI Sense Key " 3170 "Specific value returned " 3171 "during format:"); 3172 scsi_sense_print(device, &ccb->csio, 3173 stderr); 3174 warnx("Unable to print status " 3175 "information, but format will " 3176 "proceed."); 3177 warnx("will exit when format is " 3178 "complete"); 3179 } 3180 sleep(1); 3181 } else { 3182 warnx("Unexpected SCSI error during format"); 3183 cam_error_print(device, ccb, CAM_ESF_ALL, 3184 CAM_EPF_ALL, stderr); 3185 error = 1; 3186 goto scsiformat_bailout; 3187 } 3188 3189 } else if (status != CAM_REQ_CMP) { 3190 warnx("Unexpected CAM status %#x", status); 3191 if (arglist & CAM_ARG_VERBOSE) 3192 cam_error_print(device, ccb, CAM_ESF_ALL, 3193 CAM_EPF_ALL, stderr); 3194 error = 1; 3195 goto scsiformat_bailout; 3196 } 3197 3198 } while((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP); 3199 3200 if (quiet == 0) 3201 fprintf(stdout, "\nFormat Complete\n"); 3202 3203 scsiformat_bailout: 3204 3205 cam_freeccb(ccb); 3206 3207 return(error); 3208 } 3209 3210 static int 3211 scsireportluns(struct cam_device *device, int argc, char **argv, 3212 char *combinedopt, int retry_count, int timeout) 3213 { 3214 union ccb *ccb; 3215 int c, countonly, lunsonly; 3216 struct scsi_report_luns_data *lundata; 3217 int alloc_len; 3218 uint8_t report_type; 3219 uint32_t list_len, i, j; 3220 int retval; 3221 3222 retval = 0; 3223 lundata = NULL; 3224 report_type = RPL_REPORT_DEFAULT; 3225 ccb = cam_getccb(device); 3226 3227 if (ccb == NULL) { 3228 warnx("%s: error allocating ccb", __func__); 3229 return (1); 3230 } 3231 3232 bzero(&(&ccb->ccb_h)[1], 3233 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 3234 3235 countonly = 0; 3236 lunsonly = 0; 3237 3238 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3239 switch (c) { 3240 case 'c': 3241 countonly++; 3242 break; 3243 case 'l': 3244 lunsonly++; 3245 break; 3246 case 'r': 3247 if (strcasecmp(optarg, "default") == 0) 3248 report_type = RPL_REPORT_DEFAULT; 3249 else if (strcasecmp(optarg, "wellknown") == 0) 3250 report_type = RPL_REPORT_WELLKNOWN; 3251 else if (strcasecmp(optarg, "all") == 0) 3252 report_type = RPL_REPORT_ALL; 3253 else { 3254 warnx("%s: invalid report type \"%s\"", 3255 __func__, optarg); 3256 retval = 1; 3257 goto bailout; 3258 } 3259 break; 3260 default: 3261 break; 3262 } 3263 } 3264 3265 if ((countonly != 0) 3266 && (lunsonly != 0)) { 3267 warnx("%s: you can only specify one of -c or -l", __func__); 3268 retval = 1; 3269 goto bailout; 3270 } 3271 /* 3272 * According to SPC-4, the allocation length must be at least 16 3273 * bytes -- enough for the header and one LUN. 3274 */ 3275 alloc_len = sizeof(*lundata) + 8; 3276 3277 retry: 3278 3279 lundata = malloc(alloc_len); 3280 3281 if (lundata == NULL) { 3282 warn("%s: error mallocing %d bytes", __func__, alloc_len); 3283 retval = 1; 3284 goto bailout; 3285 } 3286 3287 scsi_report_luns(&ccb->csio, 3288 /*retries*/ retry_count, 3289 /*cbfcnp*/ NULL, 3290 /*tag_action*/ MSG_SIMPLE_Q_TAG, 3291 /*select_report*/ report_type, 3292 /*rpl_buf*/ lundata, 3293 /*alloc_len*/ alloc_len, 3294 /*sense_len*/ SSD_FULL_SIZE, 3295 /*timeout*/ timeout ? timeout : 5000); 3296 3297 /* Disable freezing the device queue */ 3298 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 3299 3300 if (arglist & CAM_ARG_ERR_RECOVER) 3301 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 3302 3303 if (cam_send_ccb(device, ccb) < 0) { 3304 warn("error sending REPORT LUNS command"); 3305 3306 if (arglist & CAM_ARG_VERBOSE) 3307 cam_error_print(device, ccb, CAM_ESF_ALL, 3308 CAM_EPF_ALL, stderr); 3309 3310 retval = 1; 3311 goto bailout; 3312 } 3313 3314 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 3315 cam_error_print(device, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr); 3316 retval = 1; 3317 goto bailout; 3318 } 3319 3320 3321 list_len = scsi_4btoul(lundata->length); 3322 3323 /* 3324 * If we need to list the LUNs, and our allocation 3325 * length was too short, reallocate and retry. 3326 */ 3327 if ((countonly == 0) 3328 && (list_len > (alloc_len - sizeof(*lundata)))) { 3329 alloc_len = list_len + sizeof(*lundata); 3330 free(lundata); 3331 goto retry; 3332 } 3333 3334 if (lunsonly == 0) 3335 fprintf(stdout, "%u LUN%s found\n", list_len / 8, 3336 ((list_len / 8) > 1) ? "s" : ""); 3337 3338 if (countonly != 0) 3339 goto bailout; 3340 3341 for (i = 0; i < (list_len / 8); i++) { 3342 int no_more; 3343 3344 no_more = 0; 3345 for (j = 0; j < sizeof(lundata->luns[i].lundata); j += 2) { 3346 if (j != 0) 3347 fprintf(stdout, ","); 3348 switch (lundata->luns[i].lundata[j] & 3349 RPL_LUNDATA_ATYP_MASK) { 3350 case RPL_LUNDATA_ATYP_PERIPH: 3351 if ((lundata->luns[i].lundata[j] & 3352 RPL_LUNDATA_PERIPH_BUS_MASK) != 0) 3353 fprintf(stdout, "%d:", 3354 lundata->luns[i].lundata[j] & 3355 RPL_LUNDATA_PERIPH_BUS_MASK); 3356 else if ((j == 0) 3357 && ((lundata->luns[i].lundata[j+2] & 3358 RPL_LUNDATA_PERIPH_BUS_MASK) == 0)) 3359 no_more = 1; 3360 3361 fprintf(stdout, "%d", 3362 lundata->luns[i].lundata[j+1]); 3363 break; 3364 case RPL_LUNDATA_ATYP_FLAT: { 3365 uint8_t tmplun[2]; 3366 tmplun[0] = lundata->luns[i].lundata[j] & 3367 RPL_LUNDATA_FLAT_LUN_MASK; 3368 tmplun[1] = lundata->luns[i].lundata[j+1]; 3369 3370 fprintf(stdout, "%d", scsi_2btoul(tmplun)); 3371 no_more = 1; 3372 break; 3373 } 3374 case RPL_LUNDATA_ATYP_LUN: 3375 fprintf(stdout, "%d:%d:%d", 3376 (lundata->luns[i].lundata[j+1] & 3377 RPL_LUNDATA_LUN_BUS_MASK) >> 5, 3378 lundata->luns[i].lundata[j] & 3379 RPL_LUNDATA_LUN_TARG_MASK, 3380 lundata->luns[i].lundata[j+1] & 3381 RPL_LUNDATA_LUN_LUN_MASK); 3382 break; 3383 case RPL_LUNDATA_ATYP_EXTLUN: { 3384 int field_len, field_len_code, eam_code; 3385 3386 eam_code = lundata->luns[i].lundata[j] & 3387 RPL_LUNDATA_EXT_EAM_MASK; 3388 field_len_code = (lundata->luns[i].lundata[j] & 3389 RPL_LUNDATA_EXT_LEN_MASK) >> 4; 3390 field_len = field_len_code * 2; 3391 3392 if ((eam_code == RPL_LUNDATA_EXT_EAM_WK) 3393 && (field_len_code == 0x00)) { 3394 fprintf(stdout, "%d", 3395 lundata->luns[i].lundata[j+1]); 3396 } else if ((eam_code == 3397 RPL_LUNDATA_EXT_EAM_NOT_SPEC) 3398 && (field_len_code == 0x03)) { 3399 uint8_t tmp_lun[8]; 3400 3401 /* 3402 * This format takes up all 8 bytes. 3403 * If we aren't starting at offset 0, 3404 * that's a bug. 3405 */ 3406 if (j != 0) { 3407 fprintf(stdout, "Invalid " 3408 "offset %d for " 3409 "Extended LUN not " 3410 "specified format", j); 3411 no_more = 1; 3412 break; 3413 } 3414 bzero(tmp_lun, sizeof(tmp_lun)); 3415 bcopy(&lundata->luns[i].lundata[j+1], 3416 &tmp_lun[1], sizeof(tmp_lun) - 1); 3417 fprintf(stdout, "%#jx", 3418 (intmax_t)scsi_8btou64(tmp_lun)); 3419 no_more = 1; 3420 } else { 3421 fprintf(stderr, "Unknown Extended LUN" 3422 "Address method %#x, length " 3423 "code %#x", eam_code, 3424 field_len_code); 3425 no_more = 1; 3426 } 3427 break; 3428 } 3429 default: 3430 fprintf(stderr, "Unknown LUN address method " 3431 "%#x\n", lundata->luns[i].lundata[0] & 3432 RPL_LUNDATA_ATYP_MASK); 3433 break; 3434 } 3435 /* 3436 * For the flat addressing method, there are no 3437 * other levels after it. 3438 */ 3439 if (no_more != 0) 3440 break; 3441 } 3442 fprintf(stdout, "\n"); 3443 } 3444 3445 bailout: 3446 3447 cam_freeccb(ccb); 3448 3449 free(lundata); 3450 3451 return (retval); 3452 } 3453 3454 #endif /* MINIMALISTIC */ 3455 3456 void 3457 usage(int verbose) 3458 { 3459 fprintf(verbose ? stdout : stderr, 3460 "usage: camcontrol <command> [device id][generic args][command args]\n" 3461 " camcontrol devlist [-v]\n" 3462 #ifndef MINIMALISTIC 3463 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n" 3464 " camcontrol tur [dev_id][generic args]\n" 3465 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n" 3466 " camcontrol reportluns [dev_id][generic args] [-c] [-l] [-r report]\n" 3467 " camcontrol start [dev_id][generic args]\n" 3468 " camcontrol stop [dev_id][generic args]\n" 3469 " camcontrol load [dev_id][generic args]\n" 3470 " camcontrol eject [dev_id][generic args]\n" 3471 #endif /* MINIMALISTIC */ 3472 " camcontrol rescan <all | bus[:target:lun]>\n" 3473 " camcontrol reset <all | bus[:target:lun]>\n" 3474 #ifndef MINIMALISTIC 3475 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n" 3476 " camcontrol modepage [dev_id][generic args] <-m page | -l>\n" 3477 " [-P pagectl][-e | -b][-d]\n" 3478 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n" 3479 " [-i len fmt|-o len fmt [args]]\n" 3480 " camcontrol debug [-I][-P][-T][-S][-X][-c]\n" 3481 " <all|bus[:target[:lun]]|off>\n" 3482 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n" 3483 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n" 3484 " [-D <enable|disable>][-O offset][-q]\n" 3485 " [-R syncrate][-v][-T <enable|disable>]\n" 3486 " [-U][-W bus_width]\n" 3487 " camcontrol format [dev_id][generic args][-q][-r][-w][-y]\n" 3488 #endif /* MINIMALISTIC */ 3489 " camcontrol help\n"); 3490 if (!verbose) 3491 return; 3492 #ifndef MINIMALISTIC 3493 fprintf(stdout, 3494 "Specify one of the following options:\n" 3495 "devlist list all CAM devices\n" 3496 "periphlist list all CAM peripheral drivers attached to a device\n" 3497 "tur send a test unit ready to the named device\n" 3498 "inquiry send a SCSI inquiry command to the named device\n" 3499 "reportluns send a SCSI report luns command to the device\n" 3500 "start send a Start Unit command to the device\n" 3501 "stop send a Stop Unit command to the device\n" 3502 "load send a Start Unit command to the device with the load bit set\n" 3503 "eject send a Stop Unit command to the device with the eject bit set\n" 3504 "rescan rescan all busses, the given bus, or bus:target:lun\n" 3505 "reset reset all busses, the given bus, or bus:target:lun\n" 3506 "defects read the defect list of the specified device\n" 3507 "modepage display or edit (-e) the given mode page\n" 3508 "cmd send the given scsi command, may need -i or -o as well\n" 3509 "debug turn debugging on/off for a bus, target, or lun, or all devices\n" 3510 "tags report or set the number of transaction slots for a device\n" 3511 "negotiate report or set device negotiation parameters\n" 3512 "format send the SCSI FORMAT UNIT command to the named device\n" 3513 "help this message\n" 3514 "Device Identifiers:\n" 3515 "bus:target specify the bus and target, lun defaults to 0\n" 3516 "bus:target:lun specify the bus, target and lun\n" 3517 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n" 3518 "Generic arguments:\n" 3519 "-v be verbose, print out sense information\n" 3520 "-t timeout command timeout in seconds, overrides default timeout\n" 3521 "-n dev_name specify device name, e.g. \"da\", \"cd\"\n" 3522 "-u unit specify unit number, e.g. \"0\", \"5\"\n" 3523 "-E have the kernel attempt to perform SCSI error recovery\n" 3524 "-C count specify the SCSI command retry count (needs -E to work)\n" 3525 "modepage arguments:\n" 3526 "-l list all available mode pages\n" 3527 "-m page specify the mode page to view or edit\n" 3528 "-e edit the specified mode page\n" 3529 "-b force view to binary mode\n" 3530 "-d disable block descriptors for mode sense\n" 3531 "-P pgctl page control field 0-3\n" 3532 "defects arguments:\n" 3533 "-f format specify defect list format (block, bfi or phys)\n" 3534 "-G get the grown defect list\n" 3535 "-P get the permanant defect list\n" 3536 "inquiry arguments:\n" 3537 "-D get the standard inquiry data\n" 3538 "-S get the serial number\n" 3539 "-R get the transfer rate, etc.\n" 3540 "reportluns arguments:\n" 3541 "-c only report a count of available LUNs\n" 3542 "-l only print out luns, and not a count\n" 3543 "-r <reporttype> specify \"default\", \"wellknown\" or \"all\"\n" 3544 "cmd arguments:\n" 3545 "-c cdb [args] specify the SCSI CDB\n" 3546 "-i len fmt specify input data and input data format\n" 3547 "-o len fmt [args] specify output data and output data fmt\n" 3548 "debug arguments:\n" 3549 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n" 3550 "-T CAM_DEBUG_TRACE -- routine flow tracking\n" 3551 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n" 3552 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n" 3553 "tags arguments:\n" 3554 "-N tags specify the number of tags to use for this device\n" 3555 "-q be quiet, don't report the number of tags\n" 3556 "-v report a number of tag-related parameters\n" 3557 "negotiate arguments:\n" 3558 "-a send a test unit ready after negotiation\n" 3559 "-c report/set current negotiation settings\n" 3560 "-D <arg> \"enable\" or \"disable\" disconnection\n" 3561 "-O offset set command delay offset\n" 3562 "-q be quiet, don't report anything\n" 3563 "-R syncrate synchronization rate in MHz\n" 3564 "-T <arg> \"enable\" or \"disable\" tagged queueing\n" 3565 "-U report/set user negotiation settings\n" 3566 "-W bus_width set the bus width in bits (8, 16 or 32)\n" 3567 "-v also print a Path Inquiry CCB for the controller\n" 3568 "format arguments:\n" 3569 "-q be quiet, don't print status messages\n" 3570 "-r run in report only mode\n" 3571 "-w don't send immediate format command\n" 3572 "-y don't ask any questions\n"); 3573 #endif /* MINIMALISTIC */ 3574 } 3575 3576 int 3577 main(int argc, char **argv) 3578 { 3579 int c; 3580 char *device = NULL; 3581 int unit = 0; 3582 struct cam_device *cam_dev = NULL; 3583 int timeout = 0, retry_count = 1; 3584 camcontrol_optret optreturn; 3585 char *tstr; 3586 const char *mainopt = "C:En:t:u:v"; 3587 const char *subopt = NULL; 3588 char combinedopt[256]; 3589 int error = 0, optstart = 2; 3590 int devopen = 1; 3591 #ifndef MINIMALISTIC 3592 int bus, target, lun; 3593 #endif /* MINIMALISTIC */ 3594 3595 cmdlist = CAM_CMD_NONE; 3596 arglist = CAM_ARG_NONE; 3597 3598 if (argc < 2) { 3599 usage(0); 3600 exit(1); 3601 } 3602 3603 /* 3604 * Get the base option. 3605 */ 3606 optreturn = getoption(argv[1], &cmdlist, &arglist, &subopt); 3607 3608 if (optreturn == CC_OR_AMBIGUOUS) { 3609 warnx("ambiguous option %s", argv[1]); 3610 usage(0); 3611 exit(1); 3612 } else if (optreturn == CC_OR_NOT_FOUND) { 3613 warnx("option %s not found", argv[1]); 3614 usage(0); 3615 exit(1); 3616 } 3617 3618 /* 3619 * Ahh, getopt(3) is a pain. 3620 * 3621 * This is a gross hack. There really aren't many other good 3622 * options (excuse the pun) for parsing options in a situation like 3623 * this. getopt is kinda braindead, so you end up having to run 3624 * through the options twice, and give each invocation of getopt 3625 * the option string for the other invocation. 3626 * 3627 * You would think that you could just have two groups of options. 3628 * The first group would get parsed by the first invocation of 3629 * getopt, and the second group would get parsed by the second 3630 * invocation of getopt. It doesn't quite work out that way. When 3631 * the first invocation of getopt finishes, it leaves optind pointing 3632 * to the argument _after_ the first argument in the second group. 3633 * So when the second invocation of getopt comes around, it doesn't 3634 * recognize the first argument it gets and then bails out. 3635 * 3636 * A nice alternative would be to have a flag for getopt that says 3637 * "just keep parsing arguments even when you encounter an unknown 3638 * argument", but there isn't one. So there's no real clean way to 3639 * easily parse two sets of arguments without having one invocation 3640 * of getopt know about the other. 3641 * 3642 * Without this hack, the first invocation of getopt would work as 3643 * long as the generic arguments are first, but the second invocation 3644 * (in the subfunction) would fail in one of two ways. In the case 3645 * where you don't set optreset, it would fail because optind may be 3646 * pointing to the argument after the one it should be pointing at. 3647 * In the case where you do set optreset, and reset optind, it would 3648 * fail because getopt would run into the first set of options, which 3649 * it doesn't understand. 3650 * 3651 * All of this would "sort of" work if you could somehow figure out 3652 * whether optind had been incremented one option too far. The 3653 * mechanics of that, however, are more daunting than just giving 3654 * both invocations all of the expect options for either invocation. 3655 * 3656 * Needless to say, I wouldn't mind if someone invented a better 3657 * (non-GPL!) command line parsing interface than getopt. I 3658 * wouldn't mind if someone added more knobs to getopt to make it 3659 * work better. Who knows, I may talk myself into doing it someday, 3660 * if the standards weenies let me. As it is, it just leads to 3661 * hackery like this and causes people to avoid it in some cases. 3662 * 3663 * KDM, September 8th, 1998 3664 */ 3665 if (subopt != NULL) 3666 sprintf(combinedopt, "%s%s", mainopt, subopt); 3667 else 3668 sprintf(combinedopt, "%s", mainopt); 3669 3670 /* 3671 * For these options we do not parse optional device arguments and 3672 * we do not open a passthrough device. 3673 */ 3674 if ((cmdlist == CAM_CMD_RESCAN) 3675 || (cmdlist == CAM_CMD_RESET) 3676 || (cmdlist == CAM_CMD_DEVTREE) 3677 || (cmdlist == CAM_CMD_USAGE) 3678 || (cmdlist == CAM_CMD_DEBUG)) 3679 devopen = 0; 3680 3681 #ifndef MINIMALISTIC 3682 if ((devopen == 1) 3683 && (argc > 2 && argv[2][0] != '-')) { 3684 char name[30]; 3685 int rv; 3686 3687 /* 3688 * First catch people who try to do things like: 3689 * camcontrol tur /dev/da0 3690 * camcontrol doesn't take device nodes as arguments. 3691 */ 3692 if (argv[2][0] == '/') { 3693 warnx("%s is not a valid device identifier", argv[2]); 3694 errx(1, "please read the camcontrol(8) man page"); 3695 } else if (isdigit(argv[2][0])) { 3696 /* device specified as bus:target[:lun] */ 3697 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist); 3698 if (rv < 2) 3699 errx(1, "numeric device specification must " 3700 "be either bus:target, or " 3701 "bus:target:lun"); 3702 /* default to 0 if lun was not specified */ 3703 if ((arglist & CAM_ARG_LUN) == 0) { 3704 lun = 0; 3705 arglist |= CAM_ARG_LUN; 3706 } 3707 optstart++; 3708 } else { 3709 if (cam_get_device(argv[2], name, sizeof name, &unit) 3710 == -1) 3711 errx(1, "%s", cam_errbuf); 3712 device = strdup(name); 3713 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT; 3714 optstart++; 3715 } 3716 } 3717 #endif /* MINIMALISTIC */ 3718 /* 3719 * Start getopt processing at argv[2/3], since we've already 3720 * accepted argv[1..2] as the command name, and as a possible 3721 * device name. 3722 */ 3723 optind = optstart; 3724 3725 /* 3726 * Now we run through the argument list looking for generic 3727 * options, and ignoring options that possibly belong to 3728 * subfunctions. 3729 */ 3730 while ((c = getopt(argc, argv, combinedopt))!= -1){ 3731 switch(c) { 3732 case 'C': 3733 retry_count = strtol(optarg, NULL, 0); 3734 if (retry_count < 0) 3735 errx(1, "retry count %d is < 0", 3736 retry_count); 3737 arglist |= CAM_ARG_RETRIES; 3738 break; 3739 case 'E': 3740 arglist |= CAM_ARG_ERR_RECOVER; 3741 break; 3742 case 'n': 3743 arglist |= CAM_ARG_DEVICE; 3744 tstr = optarg; 3745 while (isspace(*tstr) && (*tstr != '\0')) 3746 tstr++; 3747 device = (char *)strdup(tstr); 3748 break; 3749 case 't': 3750 timeout = strtol(optarg, NULL, 0); 3751 if (timeout < 0) 3752 errx(1, "invalid timeout %d", timeout); 3753 /* Convert the timeout from seconds to ms */ 3754 timeout *= 1000; 3755 arglist |= CAM_ARG_TIMEOUT; 3756 break; 3757 case 'u': 3758 arglist |= CAM_ARG_UNIT; 3759 unit = strtol(optarg, NULL, 0); 3760 break; 3761 case 'v': 3762 arglist |= CAM_ARG_VERBOSE; 3763 break; 3764 default: 3765 break; 3766 } 3767 } 3768 3769 #ifndef MINIMALISTIC 3770 /* 3771 * For most commands we'll want to open the passthrough device 3772 * associated with the specified device. In the case of the rescan 3773 * commands, we don't use a passthrough device at all, just the 3774 * transport layer device. 3775 */ 3776 if (devopen == 1) { 3777 if (((arglist & (CAM_ARG_BUS|CAM_ARG_TARGET)) == 0) 3778 && (((arglist & CAM_ARG_DEVICE) == 0) 3779 || ((arglist & CAM_ARG_UNIT) == 0))) { 3780 errx(1, "subcommand \"%s\" requires a valid device " 3781 "identifier", argv[1]); 3782 } 3783 3784 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))? 3785 cam_open_btl(bus, target, lun, O_RDWR, NULL) : 3786 cam_open_spec_device(device,unit,O_RDWR,NULL))) 3787 == NULL) 3788 errx(1,"%s", cam_errbuf); 3789 } 3790 #endif /* MINIMALISTIC */ 3791 3792 /* 3793 * Reset optind to 2, and reset getopt, so these routines can parse 3794 * the arguments again. 3795 */ 3796 optind = optstart; 3797 optreset = 1; 3798 3799 switch(cmdlist) { 3800 #ifndef MINIMALISTIC 3801 case CAM_CMD_DEVLIST: 3802 error = getdevlist(cam_dev); 3803 break; 3804 #endif /* MINIMALISTIC */ 3805 case CAM_CMD_DEVTREE: 3806 error = getdevtree(); 3807 break; 3808 #ifndef MINIMALISTIC 3809 case CAM_CMD_TUR: 3810 error = testunitready(cam_dev, retry_count, timeout, 0); 3811 break; 3812 case CAM_CMD_INQUIRY: 3813 error = scsidoinquiry(cam_dev, argc, argv, combinedopt, 3814 retry_count, timeout); 3815 break; 3816 case CAM_CMD_STARTSTOP: 3817 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT, 3818 arglist & CAM_ARG_EJECT, retry_count, 3819 timeout); 3820 break; 3821 #endif /* MINIMALISTIC */ 3822 case CAM_CMD_RESCAN: 3823 error = dorescan_or_reset(argc, argv, 1); 3824 break; 3825 case CAM_CMD_RESET: 3826 error = dorescan_or_reset(argc, argv, 0); 3827 break; 3828 #ifndef MINIMALISTIC 3829 case CAM_CMD_READ_DEFECTS: 3830 error = readdefects(cam_dev, argc, argv, combinedopt, 3831 retry_count, timeout); 3832 break; 3833 case CAM_CMD_MODE_PAGE: 3834 modepage(cam_dev, argc, argv, combinedopt, 3835 retry_count, timeout); 3836 break; 3837 case CAM_CMD_SCSI_CMD: 3838 error = scsicmd(cam_dev, argc, argv, combinedopt, 3839 retry_count, timeout); 3840 break; 3841 case CAM_CMD_DEBUG: 3842 error = camdebug(argc, argv, combinedopt); 3843 break; 3844 case CAM_CMD_TAG: 3845 error = tagcontrol(cam_dev, argc, argv, combinedopt); 3846 break; 3847 case CAM_CMD_RATE: 3848 error = ratecontrol(cam_dev, retry_count, timeout, 3849 argc, argv, combinedopt); 3850 break; 3851 case CAM_CMD_FORMAT: 3852 error = scsiformat(cam_dev, argc, argv, 3853 combinedopt, retry_count, timeout); 3854 break; 3855 case CAM_CMD_REPORTLUNS: 3856 error = scsireportluns(cam_dev, argc, argv, 3857 combinedopt, retry_count, 3858 timeout); 3859 break; 3860 #endif /* MINIMALISTIC */ 3861 case CAM_CMD_USAGE: 3862 usage(1); 3863 break; 3864 default: 3865 usage(0); 3866 error = 1; 3867 break; 3868 } 3869 3870 if (cam_dev != NULL) 3871 cam_close_device(cam_dev); 3872 3873 exit(error); 3874 } 3875