1 /* 2 * Copyright (c) 1997, 1998, 1999 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 * $FreeBSD$ 29 */ 30 31 #include <sys/ioctl.h> 32 #include <sys/types.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <fcntl.h> 38 #include <ctype.h> 39 #include <err.h> 40 41 #include <cam/cam.h> 42 #include <cam/cam_debug.h> 43 #include <cam/cam_ccb.h> 44 #include <cam/scsi/scsi_all.h> 45 #include <cam/scsi/scsi_da.h> 46 #include <cam/scsi/scsi_pass.h> 47 #include <cam/scsi/scsi_message.h> 48 #include <camlib.h> 49 #include "camcontrol.h" 50 51 #define DEFAULT_DEVICE "da" 52 #define DEFAULT_UNIT 0 53 54 typedef enum { 55 CAM_ARG_NONE = 0x00000000, 56 CAM_ARG_DEVLIST = 0x00000001, 57 CAM_ARG_TUR = 0x00000002, 58 CAM_ARG_INQUIRY = 0x00000003, 59 CAM_ARG_STARTSTOP = 0x00000004, 60 CAM_ARG_RESCAN = 0x00000005, 61 CAM_ARG_READ_DEFECTS = 0x00000006, 62 CAM_ARG_MODE_PAGE = 0x00000007, 63 CAM_ARG_SCSI_CMD = 0x00000008, 64 CAM_ARG_DEVTREE = 0x00000009, 65 CAM_ARG_USAGE = 0x0000000a, 66 CAM_ARG_DEBUG = 0x0000000b, 67 CAM_ARG_RESET = 0x0000000c, 68 CAM_ARG_FORMAT = 0x0000000d, 69 CAM_ARG_TAG = 0x0000000e, 70 CAM_ARG_RATE = 0x0000000f, 71 CAM_ARG_OPT_MASK = 0x0000000f, 72 CAM_ARG_VERBOSE = 0x00000010, 73 CAM_ARG_DEVICE = 0x00000020, 74 CAM_ARG_BUS = 0x00000040, 75 CAM_ARG_TARGET = 0x00000080, 76 CAM_ARG_LUN = 0x00000100, 77 CAM_ARG_EJECT = 0x00000200, 78 CAM_ARG_UNIT = 0x00000400, 79 CAM_ARG_FORMAT_BLOCK = 0x00000800, 80 CAM_ARG_FORMAT_BFI = 0x00001000, 81 CAM_ARG_FORMAT_PHYS = 0x00002000, 82 CAM_ARG_PLIST = 0x00004000, 83 CAM_ARG_GLIST = 0x00008000, 84 CAM_ARG_GET_SERIAL = 0x00010000, 85 CAM_ARG_GET_STDINQ = 0x00020000, 86 CAM_ARG_GET_XFERRATE = 0x00040000, 87 CAM_ARG_INQ_MASK = 0x00070000, 88 CAM_ARG_MODE_EDIT = 0x00080000, 89 CAM_ARG_PAGE_CNTL = 0x00100000, 90 CAM_ARG_TIMEOUT = 0x00200000, 91 CAM_ARG_CMD_IN = 0x00400000, 92 CAM_ARG_CMD_OUT = 0x00800000, 93 CAM_ARG_DBD = 0x01000000, 94 CAM_ARG_ERR_RECOVER = 0x02000000, 95 CAM_ARG_RETRIES = 0x04000000, 96 CAM_ARG_START_UNIT = 0x08000000, 97 CAM_ARG_DEBUG_INFO = 0x10000000, 98 CAM_ARG_DEBUG_TRACE = 0x20000000, 99 CAM_ARG_DEBUG_SUBTRACE = 0x40000000, 100 CAM_ARG_DEBUG_CDB = 0x80000000, 101 CAM_ARG_FLAG_MASK = 0xfffffff0 102 } cam_argmask; 103 104 struct camcontrol_opts { 105 char *optname; 106 cam_argmask argnum; 107 const char *subopt; 108 }; 109 110 extern int optreset; 111 112 static const char scsicmd_opts[] = "c:i:o:"; 113 static const char readdefect_opts[] = "f:GP"; 114 static const char negotiate_opts[] = "acD:O:qR:T:UW:"; 115 116 struct camcontrol_opts option_table[] = { 117 {"tur", CAM_ARG_TUR, NULL}, 118 {"inquiry", CAM_ARG_INQUIRY, "DSR"}, 119 {"start", CAM_ARG_STARTSTOP | CAM_ARG_START_UNIT, NULL}, 120 {"stop", CAM_ARG_STARTSTOP, NULL}, 121 {"eject", CAM_ARG_STARTSTOP | CAM_ARG_EJECT, NULL}, 122 {"rescan", CAM_ARG_RESCAN, NULL}, 123 {"reset", CAM_ARG_RESET, NULL}, 124 {"cmd", CAM_ARG_SCSI_CMD, scsicmd_opts}, 125 {"command", CAM_ARG_SCSI_CMD, scsicmd_opts}, 126 {"defects", CAM_ARG_READ_DEFECTS, readdefect_opts}, 127 {"defectlist", CAM_ARG_READ_DEFECTS, readdefect_opts}, 128 {"devlist", CAM_ARG_DEVTREE, NULL}, 129 {"periphlist", CAM_ARG_DEVLIST, NULL}, 130 {"modepage", CAM_ARG_MODE_PAGE, "dem:P:"}, 131 {"tags", CAM_ARG_TAG, "N:q"}, 132 {"negotiate", CAM_ARG_RATE, negotiate_opts}, 133 {"rate", CAM_ARG_RATE, negotiate_opts}, 134 {"debug", CAM_ARG_DEBUG, "ITSc"}, 135 {"help", CAM_ARG_USAGE, NULL}, 136 {"-?", CAM_ARG_USAGE, NULL}, 137 {"-h", CAM_ARG_USAGE, NULL}, 138 {NULL, 0, NULL} 139 }; 140 141 typedef enum { 142 CC_OR_NOT_FOUND, 143 CC_OR_AMBIGUOUS, 144 CC_OR_FOUND 145 } camcontrol_optret; 146 147 cam_argmask arglist; 148 int bus, target, lun; 149 150 151 camcontrol_optret getoption(char *arg, cam_argmask *argnum, char **subopt); 152 static int getdevlist(struct cam_device *device); 153 static int getdevtree(void); 154 static int testunitready(struct cam_device *device, int retry_count, 155 int timeout, int quiet); 156 static int scsistart(struct cam_device *device, int startstop, int loadeject, 157 int retry_count, int timeout); 158 static int scsidoinquiry(struct cam_device *device, int argc, char **argv, 159 char *combinedopt, int retry_count, int timeout); 160 static int scsiinquiry(struct cam_device *device, int retry_count, int timeout); 161 static int scsiserial(struct cam_device *device, int retry_count, int timeout); 162 static int scsixferrate(struct cam_device *device); 163 static int parse_btl(char *tstr, int *bus, int *target, int *lun, 164 cam_argmask *arglist); 165 static int dorescan_or_reset(int argc, char **argv, int rescan); 166 static int rescan_or_reset_bus(int bus, int rescan); 167 static int scanlun_or_reset_dev(int bus, int target, int lun, int scan); 168 static int readdefects(struct cam_device *device, int argc, char **argv, 169 char *combinedopt, int retry_count, int timeout); 170 static void modepage(struct cam_device *device, int argc, char **argv, 171 char *combinedopt, int retry_count, int timeout); 172 static int scsicmd(struct cam_device *device, int argc, char **argv, 173 char *combinedopt, int retry_count, int timeout); 174 static int tagcontrol(struct cam_device *device, int argc, char **argv, 175 char *combinedopt); 176 static void cts_print(struct cam_device *device, 177 struct ccb_trans_settings *cts); 178 static void cpi_print(struct ccb_pathinq *cpi); 179 static int get_cpi(struct cam_device *device, struct ccb_pathinq *cpi); 180 static int get_print_cts(struct cam_device *device, int user_settings, 181 int quiet, struct ccb_trans_settings *cts); 182 static int ratecontrol(struct cam_device *device, int retry_count, 183 int timeout, int argc, char **argv, char *combinedopt); 184 185 camcontrol_optret 186 getoption(char *arg, cam_argmask *argnum, char **subopt) 187 { 188 struct camcontrol_opts *opts; 189 int num_matches = 0; 190 191 for (opts = option_table; (opts != NULL) && (opts->optname != NULL); 192 opts++) { 193 if (strncmp(opts->optname, arg, strlen(arg)) == 0) { 194 *argnum = opts->argnum; 195 *subopt = (char *)opts->subopt; 196 if (++num_matches > 1) 197 return(CC_OR_AMBIGUOUS); 198 } 199 } 200 201 if (num_matches > 0) 202 return(CC_OR_FOUND); 203 else 204 return(CC_OR_NOT_FOUND); 205 } 206 207 static int 208 getdevlist(struct cam_device *device) 209 { 210 union ccb *ccb; 211 char status[32]; 212 int error = 0; 213 214 ccb = cam_getccb(device); 215 216 ccb->ccb_h.func_code = XPT_GDEVLIST; 217 ccb->ccb_h.flags = CAM_DIR_NONE; 218 ccb->ccb_h.retry_count = 1; 219 ccb->cgdl.index = 0; 220 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS; 221 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) { 222 if (cam_send_ccb(device, ccb) < 0) { 223 perror("error getting device list"); 224 cam_freeccb(ccb); 225 return(1); 226 } 227 228 status[0] = '\0'; 229 230 switch (ccb->cgdl.status) { 231 case CAM_GDEVLIST_MORE_DEVS: 232 strcpy(status, "MORE"); 233 break; 234 case CAM_GDEVLIST_LAST_DEVICE: 235 strcpy(status, "LAST"); 236 break; 237 case CAM_GDEVLIST_LIST_CHANGED: 238 strcpy(status, "CHANGED"); 239 break; 240 case CAM_GDEVLIST_ERROR: 241 strcpy(status, "ERROR"); 242 error = 1; 243 break; 244 } 245 246 fprintf(stdout, "%s%d: generation: %d index: %d status: %s\n", 247 ccb->cgdl.periph_name, 248 ccb->cgdl.unit_number, 249 ccb->cgdl.generation, 250 ccb->cgdl.index, 251 status); 252 253 /* 254 * If the list has changed, we need to start over from the 255 * beginning. 256 */ 257 if (ccb->cgdl.status == CAM_GDEVLIST_LIST_CHANGED) 258 ccb->cgdl.index = 0; 259 } 260 261 cam_freeccb(ccb); 262 263 return(error); 264 } 265 266 static int 267 getdevtree(void) 268 { 269 union ccb ccb; 270 int bufsize, i, fd; 271 int need_close = 0; 272 int error = 0; 273 int skip_device = 0; 274 275 if ((fd = open(XPT_DEVICE, O_RDWR)) == -1) { 276 warn("couldn't open %s", XPT_DEVICE); 277 return(1); 278 } 279 280 bzero(&(&ccb.ccb_h)[1], 281 sizeof(struct ccb_dev_match) - sizeof(struct ccb_hdr)); 282 283 ccb.ccb_h.func_code = XPT_DEV_MATCH; 284 bufsize = sizeof(struct dev_match_result) * 100; 285 ccb.cdm.match_buf_len = bufsize; 286 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize); 287 ccb.cdm.num_matches = 0; 288 289 /* 290 * We fetch all nodes, since we display most of them in the default 291 * case, and all in the verbose case. 292 */ 293 ccb.cdm.num_patterns = 0; 294 ccb.cdm.pattern_buf_len = 0; 295 296 /* 297 * We do the ioctl multiple times if necessary, in case there are 298 * more than 100 nodes in the EDT. 299 */ 300 do { 301 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 302 warn("error sending CAMIOCOMMAND ioctl"); 303 error = 1; 304 break; 305 } 306 307 if ((ccb.ccb_h.status != CAM_REQ_CMP) 308 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST) 309 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) { 310 fprintf(stderr, "got CAM error %#x, CDM error %d\n", 311 ccb.ccb_h.status, ccb.cdm.status); 312 error = 1; 313 break; 314 } 315 316 for (i = 0; i < ccb.cdm.num_matches; i++) { 317 switch(ccb.cdm.matches[i].type) { 318 case DEV_MATCH_BUS: { 319 struct bus_match_result *bus_result; 320 321 /* 322 * Only print the bus information if the 323 * user turns on the verbose flag. 324 */ 325 if ((arglist & CAM_ARG_VERBOSE) == 0) 326 break; 327 328 bus_result = 329 &ccb.cdm.matches[i].result.bus_result; 330 331 if (need_close) { 332 fprintf(stdout, ")\n"); 333 need_close = 0; 334 } 335 336 fprintf(stdout, "scbus%d on %s%d bus %d:\n", 337 bus_result->path_id, 338 bus_result->dev_name, 339 bus_result->unit_number, 340 bus_result->bus_id); 341 break; 342 } 343 case DEV_MATCH_DEVICE: { 344 struct device_match_result *dev_result; 345 char vendor[16], product[48], revision[16]; 346 char tmpstr[256]; 347 348 dev_result = 349 &ccb.cdm.matches[i].result.device_result; 350 351 if ((dev_result->flags 352 & DEV_RESULT_UNCONFIGURED) 353 && ((arglist & CAM_ARG_VERBOSE) == 0)) { 354 skip_device = 1; 355 break; 356 } else 357 skip_device = 0; 358 359 cam_strvis(vendor, dev_result->inq_data.vendor, 360 sizeof(dev_result->inq_data.vendor), 361 sizeof(vendor)); 362 cam_strvis(product, 363 dev_result->inq_data.product, 364 sizeof(dev_result->inq_data.product), 365 sizeof(product)); 366 cam_strvis(revision, 367 dev_result->inq_data.revision, 368 sizeof(dev_result->inq_data.revision), 369 sizeof(revision)); 370 sprintf(tmpstr, "<%s %s %s>", vendor, product, 371 revision); 372 if (need_close) { 373 fprintf(stdout, ")\n"); 374 need_close = 0; 375 } 376 377 fprintf(stdout, "%-33s at scbus%d " 378 "target %d lun %d (", 379 tmpstr, 380 dev_result->path_id, 381 dev_result->target_id, 382 dev_result->target_lun); 383 384 need_close = 1; 385 386 break; 387 } 388 case DEV_MATCH_PERIPH: { 389 struct periph_match_result *periph_result; 390 391 periph_result = 392 &ccb.cdm.matches[i].result.periph_result; 393 394 if (skip_device != 0) 395 break; 396 397 if (need_close > 1) 398 fprintf(stdout, ","); 399 400 fprintf(stdout, "%s%d", 401 periph_result->periph_name, 402 periph_result->unit_number); 403 404 need_close++; 405 break; 406 } 407 default: 408 fprintf(stdout, "unknown match type\n"); 409 break; 410 } 411 } 412 413 } while ((ccb.ccb_h.status == CAM_REQ_CMP) 414 && (ccb.cdm.status == CAM_DEV_MATCH_MORE)); 415 416 if (need_close) 417 fprintf(stdout, ")\n"); 418 419 close(fd); 420 421 return(error); 422 } 423 424 static int 425 testunitready(struct cam_device *device, int retry_count, int timeout, 426 int quiet) 427 { 428 int error = 0; 429 union ccb *ccb; 430 431 ccb = cam_getccb(device); 432 433 scsi_test_unit_ready(&ccb->csio, 434 /* retries */ retry_count, 435 /* cbfcnp */ NULL, 436 /* tag_action */ MSG_SIMPLE_Q_TAG, 437 /* sense_len */ SSD_FULL_SIZE, 438 /* timeout */ timeout ? timeout : 5000); 439 440 /* Disable freezing the device queue */ 441 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 442 443 if (arglist & CAM_ARG_ERR_RECOVER) 444 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 445 446 if (cam_send_ccb(device, ccb) < 0) { 447 if (quiet == 0) 448 perror("error sending test unit ready"); 449 450 if (arglist & CAM_ARG_VERBOSE) { 451 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 452 CAM_SCSI_STATUS_ERROR) 453 scsi_sense_print(device, &ccb->csio, stderr); 454 else 455 fprintf(stderr, "CAM status is %#x\n", 456 ccb->ccb_h.status); 457 } 458 459 cam_freeccb(ccb); 460 return(1); 461 } 462 463 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 464 if (quiet == 0) 465 fprintf(stdout, "Unit is ready\n"); 466 } else { 467 if (quiet == 0) 468 fprintf(stdout, "Unit is not ready\n"); 469 error = 1; 470 471 if (arglist & CAM_ARG_VERBOSE) { 472 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 473 CAM_SCSI_STATUS_ERROR) 474 scsi_sense_print(device, &ccb->csio, stderr); 475 else 476 fprintf(stderr, "CAM status is %#x\n", 477 ccb->ccb_h.status); 478 } 479 } 480 481 cam_freeccb(ccb); 482 483 return(error); 484 } 485 486 static int 487 scsistart(struct cam_device *device, int startstop, int loadeject, 488 int retry_count, int timeout) 489 { 490 union ccb *ccb; 491 int error = 0; 492 493 ccb = cam_getccb(device); 494 495 /* 496 * If we're stopping, send an ordered tag so the drive in question 497 * will finish any previously queued writes before stopping. If 498 * the device isn't capable of tagged queueing, or if tagged 499 * queueing is turned off, the tag action is a no-op. 500 */ 501 scsi_start_stop(&ccb->csio, 502 /* retries */ retry_count, 503 /* cbfcnp */ NULL, 504 /* tag_action */ startstop ? MSG_SIMPLE_Q_TAG : 505 MSG_ORDERED_Q_TAG, 506 /* start/stop */ startstop, 507 /* load_eject */ loadeject, 508 /* immediate */ 0, 509 /* sense_len */ SSD_FULL_SIZE, 510 /* timeout */ timeout ? timeout : 120000); 511 512 /* Disable freezing the device queue */ 513 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 514 515 if (arglist & CAM_ARG_ERR_RECOVER) 516 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 517 518 if (cam_send_ccb(device, ccb) < 0) { 519 perror("error sending start unit"); 520 521 if (arglist & CAM_ARG_VERBOSE) { 522 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 523 CAM_SCSI_STATUS_ERROR) 524 scsi_sense_print(device, &ccb->csio, stderr); 525 else 526 fprintf(stderr, "CAM status is %#x\n", 527 ccb->ccb_h.status); 528 } 529 530 cam_freeccb(ccb); 531 return(1); 532 } 533 534 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 535 if (startstop) { 536 fprintf(stdout, "Unit started successfully"); 537 if (loadeject) 538 fprintf(stdout,", Media loaded\n"); 539 else 540 fprintf(stdout,"\n"); 541 } else { 542 fprintf(stdout, "Unit stopped successfully"); 543 if (loadeject) 544 fprintf(stdout, ", Media ejected\n"); 545 else 546 fprintf(stdout, "\n"); 547 } 548 else { 549 error = 1; 550 if (startstop) 551 fprintf(stdout, 552 "Error received from start unit command\n"); 553 else 554 fprintf(stdout, 555 "Error received from stop unit command\n"); 556 557 if (arglist & CAM_ARG_VERBOSE) { 558 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 559 CAM_SCSI_STATUS_ERROR) 560 scsi_sense_print(device, &ccb->csio, stderr); 561 else 562 fprintf(stderr, "CAM status is %#x\n", 563 ccb->ccb_h.status); 564 } 565 } 566 567 cam_freeccb(ccb); 568 569 return(error); 570 } 571 572 static int 573 scsidoinquiry(struct cam_device *device, int argc, char **argv, 574 char *combinedopt, int retry_count, int timeout) 575 { 576 int c; 577 int error = 0; 578 579 while ((c = getopt(argc, argv, combinedopt)) != -1) { 580 switch(c) { 581 case 'D': 582 arglist |= CAM_ARG_GET_STDINQ; 583 break; 584 case 'R': 585 arglist |= CAM_ARG_GET_XFERRATE; 586 break; 587 case 'S': 588 arglist |= CAM_ARG_GET_SERIAL; 589 break; 590 default: 591 break; 592 } 593 } 594 595 /* 596 * If the user didn't specify any inquiry options, he wants all of 597 * them. 598 */ 599 if ((arglist & CAM_ARG_INQ_MASK) == 0) 600 arglist |= CAM_ARG_INQ_MASK; 601 602 if (arglist & CAM_ARG_GET_STDINQ) 603 error = scsiinquiry(device, retry_count, timeout); 604 605 if (error != 0) 606 return(error); 607 608 if (arglist & CAM_ARG_GET_SERIAL) 609 scsiserial(device, retry_count, timeout); 610 611 if (error != 0) 612 return(error); 613 614 if (arglist & CAM_ARG_GET_XFERRATE) 615 error = scsixferrate(device); 616 617 return(error); 618 } 619 620 static int 621 scsiinquiry(struct cam_device *device, int retry_count, int timeout) 622 { 623 union ccb *ccb; 624 struct scsi_inquiry_data *inq_buf; 625 int error = 0; 626 627 ccb = cam_getccb(device); 628 629 if (ccb == NULL) { 630 warnx("couldn't allocate CCB"); 631 return(1); 632 } 633 634 /* cam_getccb cleans up the header, caller has to zero the payload */ 635 bzero(&(&ccb->ccb_h)[1], 636 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 637 638 inq_buf = (struct scsi_inquiry_data *)malloc( 639 sizeof(struct scsi_inquiry_data)); 640 641 if (inq_buf == NULL) { 642 cam_freeccb(ccb); 643 warnx("can't malloc memory for inquiry\n"); 644 return(1); 645 } 646 bzero(inq_buf, sizeof(*inq_buf)); 647 648 scsi_inquiry(&ccb->csio, 649 /* retries */ retry_count, 650 /* cbfcnp */ NULL, 651 /* tag_action */ MSG_SIMPLE_Q_TAG, 652 /* inq_buf */ (u_int8_t *)inq_buf, 653 /* inq_len */ sizeof(struct scsi_inquiry_data), 654 /* evpd */ 0, 655 /* page_code */ 0, 656 /* sense_len */ SSD_FULL_SIZE, 657 /* timeout */ timeout ? timeout : 5000); 658 659 /* Disable freezing the device queue */ 660 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 661 662 if (arglist & CAM_ARG_ERR_RECOVER) 663 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 664 665 if (cam_send_ccb(device, ccb) < 0) { 666 perror("error sending SCSI inquiry"); 667 668 if (arglist & CAM_ARG_VERBOSE) { 669 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 670 CAM_SCSI_STATUS_ERROR) 671 scsi_sense_print(device, &ccb->csio, stderr); 672 else 673 fprintf(stderr, "CAM status is %#x\n", 674 ccb->ccb_h.status); 675 } 676 677 cam_freeccb(ccb); 678 return(1); 679 } 680 681 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 682 error = 1; 683 684 if (arglist & CAM_ARG_VERBOSE) { 685 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 686 CAM_SCSI_STATUS_ERROR) 687 scsi_sense_print(device, &ccb->csio, stderr); 688 else 689 fprintf(stderr, "CAM status is %#x\n", 690 ccb->ccb_h.status); 691 } 692 } 693 694 cam_freeccb(ccb); 695 696 if (error != 0) { 697 free(inq_buf); 698 return(error); 699 } 700 701 fprintf(stdout, "%s%d: ", device->device_name, 702 device->dev_unit_num); 703 scsi_print_inquiry(inq_buf); 704 705 free(inq_buf); 706 707 return(0); 708 } 709 710 static int 711 scsiserial(struct cam_device *device, int retry_count, int timeout) 712 { 713 union ccb *ccb; 714 struct scsi_vpd_unit_serial_number *serial_buf; 715 char serial_num[SVPD_SERIAL_NUM_SIZE + 1]; 716 int error = 0; 717 718 ccb = cam_getccb(device); 719 720 if (ccb == NULL) { 721 warnx("couldn't allocate CCB"); 722 return(1); 723 } 724 725 /* cam_getccb cleans up the header, caller has to zero the payload */ 726 bzero(&(&ccb->ccb_h)[1], 727 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 728 729 serial_buf = (struct scsi_vpd_unit_serial_number *) 730 malloc(sizeof(*serial_buf)); 731 732 if (serial_buf == NULL) { 733 cam_freeccb(ccb); 734 warnx("can't malloc memory for serial number"); 735 return(1); 736 } 737 738 scsi_inquiry(&ccb->csio, 739 /*retries*/ retry_count, 740 /*cbfcnp*/ NULL, 741 /* tag_action */ MSG_SIMPLE_Q_TAG, 742 /* inq_buf */ (u_int8_t *)serial_buf, 743 /* inq_len */ sizeof(*serial_buf), 744 /* evpd */ 1, 745 /* page_code */ SVPD_UNIT_SERIAL_NUMBER, 746 /* sense_len */ SSD_FULL_SIZE, 747 /* timeout */ timeout ? timeout : 5000); 748 749 /* Disable freezing the device queue */ 750 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 751 752 if (arglist & CAM_ARG_ERR_RECOVER) 753 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 754 755 if (cam_send_ccb(device, ccb) < 0) { 756 warn("error getting serial number"); 757 758 if (arglist & CAM_ARG_VERBOSE) { 759 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 760 CAM_SCSI_STATUS_ERROR) 761 scsi_sense_print(device, &ccb->csio, stderr); 762 else 763 fprintf(stderr, "CAM status is %#x\n", 764 ccb->ccb_h.status); 765 } 766 767 cam_freeccb(ccb); 768 free(serial_buf); 769 return(1); 770 } 771 772 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 773 error = 1; 774 775 if (arglist & CAM_ARG_VERBOSE) { 776 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 777 CAM_SCSI_STATUS_ERROR) 778 scsi_sense_print(device, &ccb->csio, stderr); 779 else 780 fprintf(stderr, "CAM status is %#x\n", 781 ccb->ccb_h.status); 782 } 783 } 784 785 cam_freeccb(ccb); 786 787 if (error != 0) { 788 free(serial_buf); 789 return(error); 790 } 791 792 bcopy(serial_buf->serial_num, serial_num, serial_buf->length); 793 serial_num[serial_buf->length] = '\0'; 794 795 if ((arglist & CAM_ARG_GET_STDINQ) 796 || (arglist & CAM_ARG_GET_XFERRATE)) 797 fprintf(stdout, "%s%d: Serial Number ", 798 device->device_name, device->dev_unit_num); 799 800 fprintf(stdout, "%.60s\n", serial_num); 801 802 free(serial_buf); 803 804 return(0); 805 } 806 807 static int 808 scsixferrate(struct cam_device *device) 809 { 810 u_int32_t freq; 811 u_int32_t speed; 812 union ccb *ccb; 813 u_int mb; 814 int retval = 0; 815 816 ccb = cam_getccb(device); 817 818 if (ccb == NULL) { 819 warnx("couldn't allocate CCB"); 820 return(1); 821 } 822 823 bzero(&(&ccb->ccb_h)[1], 824 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 825 826 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 827 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS; 828 829 if (((retval = cam_send_ccb(device, ccb)) < 0) 830 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 831 char *error_string = "error getting transfer settings"; 832 833 if (retval < 0) 834 warn(error_string); 835 else 836 warnx(error_string); 837 838 /* 839 * If there is an error, it won't be a SCSI error since 840 * this isn't a SCSI CCB. 841 */ 842 if (arglist & CAM_ARG_VERBOSE) 843 fprintf(stderr, "CAM status is %#x\n", 844 ccb->ccb_h.status); 845 846 retval = 1; 847 848 goto xferrate_bailout; 849 850 } 851 852 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) 853 && (ccb->cts.sync_offset != 0)) { 854 freq = scsi_calc_syncsrate(ccb->cts.sync_period); 855 speed = freq; 856 } else { 857 struct ccb_pathinq cpi; 858 859 retval = get_cpi(device, &cpi); 860 861 if (retval != 0) 862 goto xferrate_bailout; 863 864 speed = cpi.base_transfer_speed; 865 freq = 0; 866 } 867 868 fprintf(stdout, "%s%d: ", device->device_name, 869 device->dev_unit_num); 870 871 if ((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) 872 speed *= (0x01 << device->bus_width); 873 874 mb = speed / 1000; 875 876 if (mb > 0) 877 fprintf(stdout, "%d.%03dMB/s transfers ", 878 mb, speed % 1000); 879 else 880 fprintf(stdout, "%dKB/s transfers ", 881 (speed % 1000) * 1000); 882 883 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) 884 && (ccb->cts.sync_offset != 0)) 885 fprintf(stdout, "(%d.%03dMHz, offset %d", freq / 1000, 886 freq % 1000, ccb->cts.sync_offset); 887 888 if (((ccb->cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) 889 && (ccb->cts.bus_width > 0)) { 890 if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) 891 && (ccb->cts.sync_offset != 0)) { 892 fprintf(stdout, ", "); 893 } else { 894 fprintf(stdout, " ("); 895 } 896 fprintf(stdout, "%dbit)", 8 * (0x01 << ccb->cts.bus_width)); 897 } else if (((ccb->cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) 898 && (ccb->cts.sync_offset != 0)) { 899 fprintf(stdout, ")"); 900 } 901 902 if (device->inq_data.flags & SID_CmdQue) 903 fprintf(stdout, ", Tagged Queueing Enabled"); 904 905 fprintf(stdout, "\n"); 906 907 xferrate_bailout: 908 909 cam_freeccb(ccb); 910 911 return(retval); 912 } 913 914 /* 915 * Parse out a bus, or a bus, target and lun in the following 916 * format: 917 * bus 918 * bus:target 919 * bus:target:lun 920 * 921 * Returns the number of parsed components, or 0. 922 */ 923 static int 924 parse_btl(char *tstr, int *bus, int *target, int *lun, cam_argmask *arglist) 925 { 926 char *tmpstr; 927 int convs = 0; 928 929 while (isspace(*tstr) && (*tstr != '\0')) 930 tstr++; 931 932 tmpstr = (char *)strtok(tstr, ":"); 933 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 934 *bus = strtol(tmpstr, NULL, 0); 935 *arglist |= CAM_ARG_BUS; 936 convs++; 937 tmpstr = (char *)strtok(NULL, ":"); 938 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 939 *target = strtol(tmpstr, NULL, 0); 940 *arglist |= CAM_ARG_TARGET; 941 convs++; 942 tmpstr = (char *)strtok(NULL, ":"); 943 if ((tmpstr != NULL) && (*tmpstr != '\0')) { 944 *lun = strtol(tmpstr, NULL, 0); 945 *arglist |= CAM_ARG_LUN; 946 convs++; 947 } 948 } 949 } 950 951 return convs; 952 } 953 954 static int 955 dorescan_or_reset(int argc, char **argv, int rescan) 956 { 957 static const char *must = 958 "you must specify a bus, or a bus:target:lun to %s"; 959 int rv, error = 0; 960 int bus = -1, target = -1, lun = -1; 961 962 if (argc < 3) { 963 warnx(must, rescan? "rescan" : "reset"); 964 return(1); 965 } 966 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist); 967 if (rv != 1 && rv != 3) { 968 warnx(must, rescan? "rescan" : "reset"); 969 return(1); 970 } 971 972 if ((arglist & CAM_ARG_BUS) 973 && (arglist & CAM_ARG_TARGET) 974 && (arglist & CAM_ARG_LUN)) 975 error = scanlun_or_reset_dev(bus, target, lun, rescan); 976 else 977 error = rescan_or_reset_bus(bus, rescan); 978 979 return(error); 980 } 981 982 static int 983 rescan_or_reset_bus(int bus, int rescan) 984 { 985 union ccb ccb; 986 int fd; 987 988 if (bus < 0) { 989 warnx("invalid bus number %d", bus); 990 return(1); 991 } 992 993 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 994 warnx("error opening tranport layer device %s", XPT_DEVICE); 995 warn("%s", XPT_DEVICE); 996 return(1); 997 } 998 999 ccb.ccb_h.func_code = rescan? XPT_SCAN_BUS : XPT_RESET_BUS; 1000 ccb.ccb_h.path_id = bus; 1001 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 1002 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 1003 ccb.crcn.flags = CAM_FLAG_NONE; 1004 1005 /* run this at a low priority */ 1006 ccb.ccb_h.pinfo.priority = 5; 1007 1008 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1009 warn("CAMIOCOMMAND ioctl failed"); 1010 close(fd); 1011 return(1); 1012 } 1013 1014 close(fd); 1015 1016 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1017 fprintf(stdout, "%s of bus %d was successful\n", 1018 rescan? "Re-scan" : "Reset", bus); 1019 return(0); 1020 } else { 1021 fprintf(stdout, "%s of bus %d returned error %#x\n", 1022 rescan? "Re-scan" : "Reset", bus, 1023 ccb.ccb_h.status & CAM_STATUS_MASK); 1024 return(1); 1025 } 1026 } 1027 1028 static int 1029 scanlun_or_reset_dev(int bus, int target, int lun, int scan) 1030 { 1031 union ccb ccb; 1032 int fd; 1033 1034 if (bus < 0) { 1035 warnx("invalid bus number %d", bus); 1036 return(1); 1037 } 1038 1039 if (target < 0) { 1040 warnx("invalid target number %d", target); 1041 return(1); 1042 } 1043 1044 if (lun < 0) { 1045 warnx("invalid lun number %d", lun); 1046 return(1); 1047 } 1048 1049 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1050 warnx("error opening tranport layer device %s\n", 1051 XPT_DEVICE); 1052 warn("%s", XPT_DEVICE); 1053 return(1); 1054 } 1055 1056 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV; 1057 ccb.ccb_h.path_id = bus; 1058 ccb.ccb_h.target_id = target; 1059 ccb.ccb_h.target_lun = lun; 1060 ccb.crcn.flags = CAM_FLAG_NONE; 1061 1062 /* run this at a low priority */ 1063 ccb.ccb_h.pinfo.priority = 5; 1064 1065 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) { 1066 warn("CAMIOCOMMAND ioctl failed"); 1067 close(fd); 1068 return(1); 1069 } 1070 1071 close(fd); 1072 1073 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1074 fprintf(stdout, "%s of %d:%d:%d was successful\n", 1075 scan? "Re-scan" : "Reset", bus, target, lun); 1076 return(0); 1077 } else { 1078 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n", 1079 scan? "Re-scan" : "Reset", bus, target, lun, 1080 ccb.ccb_h.status & CAM_STATUS_MASK); 1081 return(1); 1082 } 1083 } 1084 1085 static int 1086 readdefects(struct cam_device *device, int argc, char **argv, 1087 char *combinedopt, int retry_count, int timeout) 1088 { 1089 union ccb *ccb = NULL; 1090 struct scsi_read_defect_data_10 *rdd_cdb; 1091 u_int8_t *defect_list = NULL; 1092 u_int32_t dlist_length = 65000; 1093 u_int32_t returned_length = 0; 1094 u_int32_t num_returned = 0; 1095 u_int8_t returned_format; 1096 register int i; 1097 int c, error = 0; 1098 int lists_specified = 0; 1099 1100 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1101 switch(c){ 1102 case 'f': 1103 { 1104 char *tstr; 1105 tstr = optarg; 1106 while (isspace(*tstr) && (*tstr != '\0')) 1107 tstr++; 1108 if (strcmp(tstr, "block") == 0) 1109 arglist |= CAM_ARG_FORMAT_BLOCK; 1110 else if (strcmp(tstr, "bfi") == 0) 1111 arglist |= CAM_ARG_FORMAT_BFI; 1112 else if (strcmp(tstr, "phys") == 0) 1113 arglist |= CAM_ARG_FORMAT_PHYS; 1114 else { 1115 error = 1; 1116 warnx("invalid defect format %s", tstr); 1117 goto defect_bailout; 1118 } 1119 break; 1120 } 1121 case 'G': 1122 arglist |= CAM_ARG_GLIST; 1123 break; 1124 case 'P': 1125 arglist |= CAM_ARG_PLIST; 1126 break; 1127 default: 1128 break; 1129 } 1130 } 1131 1132 ccb = cam_getccb(device); 1133 1134 /* 1135 * Hopefully 65000 bytes is enough to hold the defect list. If it 1136 * isn't, the disk is probably dead already. We'd have to go with 1137 * 12 byte command (i.e. alloc_length is 32 bits instead of 16) 1138 * to hold them all. 1139 */ 1140 defect_list = malloc(dlist_length); 1141 1142 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes; 1143 1144 /* 1145 * cam_getccb() zeros the CCB header only. So we need to zero the 1146 * payload portion of the ccb. 1147 */ 1148 bzero(&(&ccb->ccb_h)[1], 1149 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1150 1151 cam_fill_csio(&ccb->csio, 1152 /*retries*/ retry_count, 1153 /*cbfcnp*/ NULL, 1154 /*flags*/ CAM_DIR_IN | ((arglist & CAM_ARG_ERR_RECOVER) ? 1155 CAM_PASS_ERR_RECOVER : 0), 1156 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1157 /*data_ptr*/ defect_list, 1158 /*dxfer_len*/ dlist_length, 1159 /*sense_len*/ SSD_FULL_SIZE, 1160 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10), 1161 /*timeout*/ timeout ? timeout : 5000); 1162 1163 rdd_cdb->opcode = READ_DEFECT_DATA_10; 1164 if (arglist & CAM_ARG_FORMAT_BLOCK) 1165 rdd_cdb->format = SRDD10_BLOCK_FORMAT; 1166 else if (arglist & CAM_ARG_FORMAT_BFI) 1167 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT; 1168 else if (arglist & CAM_ARG_FORMAT_PHYS) 1169 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT; 1170 else { 1171 error = 1; 1172 warnx("no defect list format specified"); 1173 goto defect_bailout; 1174 } 1175 if (arglist & CAM_ARG_PLIST) { 1176 rdd_cdb->format |= SRDD10_PLIST; 1177 lists_specified++; 1178 } 1179 1180 if (arglist & CAM_ARG_GLIST) { 1181 rdd_cdb->format |= SRDD10_GLIST; 1182 lists_specified++; 1183 } 1184 1185 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length); 1186 1187 /* Disable freezing the device queue */ 1188 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1189 1190 if (cam_send_ccb(device, ccb) < 0) { 1191 perror("error reading defect list"); 1192 1193 if (arglist & CAM_ARG_VERBOSE) { 1194 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1195 CAM_SCSI_STATUS_ERROR) 1196 scsi_sense_print(device, &ccb->csio, stderr); 1197 else 1198 fprintf(stderr, "CAM status is %#x\n", 1199 ccb->ccb_h.status); 1200 } 1201 1202 error = 1; 1203 goto defect_bailout; 1204 } 1205 1206 if (arglist & CAM_ARG_VERBOSE) 1207 scsi_sense_print(device, &ccb->csio, stderr); 1208 1209 returned_length = scsi_2btoul(((struct 1210 scsi_read_defect_data_hdr_10 *)defect_list)->length); 1211 1212 returned_format = ((struct scsi_read_defect_data_hdr_10 *) 1213 defect_list)->format; 1214 1215 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1216 struct scsi_sense_data *sense; 1217 int error_code, sense_key, asc, ascq; 1218 1219 sense = &ccb->csio.sense_data; 1220 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq); 1221 1222 /* 1223 * According to the SCSI spec, if the disk doesn't support 1224 * the requested format, it will generally return a sense 1225 * key of RECOVERED ERROR, and an additional sense code 1226 * of "DEFECT LIST NOT FOUND". So, we check for that, and 1227 * also check to make sure that the returned length is 1228 * greater than 0, and then print out whatever format the 1229 * disk gave us. 1230 */ 1231 if ((sense_key == SSD_KEY_RECOVERED_ERROR) 1232 && (asc == 0x1c) && (ascq == 0x00) 1233 && (returned_length > 0)) { 1234 warnx("requested defect format not available"); 1235 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) { 1236 case SRDD10_BLOCK_FORMAT: 1237 warnx("Device returned block format"); 1238 break; 1239 case SRDD10_BYTES_FROM_INDEX_FORMAT: 1240 warnx("Device returned bytes from index" 1241 " format"); 1242 break; 1243 case SRDD10_PHYSICAL_SECTOR_FORMAT: 1244 warnx("Device returned physical sector format"); 1245 break; 1246 default: 1247 error = 1; 1248 warnx("Device returned unknown defect" 1249 " data format %#x", returned_format); 1250 goto defect_bailout; 1251 break; /* NOTREACHED */ 1252 } 1253 } else { 1254 error = 1; 1255 warnx("Error returned from read defect data command"); 1256 goto defect_bailout; 1257 } 1258 } 1259 1260 /* 1261 * XXX KDM I should probably clean up the printout format for the 1262 * disk defects. 1263 */ 1264 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){ 1265 case SRDDH10_PHYSICAL_SECTOR_FORMAT: 1266 { 1267 struct scsi_defect_desc_phys_sector *dlist; 1268 1269 dlist = (struct scsi_defect_desc_phys_sector *) 1270 (defect_list + 1271 sizeof(struct scsi_read_defect_data_hdr_10)); 1272 1273 num_returned = returned_length / 1274 sizeof(struct scsi_defect_desc_phys_sector); 1275 1276 fprintf(stderr, "Got %d defect", num_returned); 1277 1278 if ((lists_specified == 0) || (num_returned == 0)) { 1279 fprintf(stderr, "s.\n"); 1280 break; 1281 } else if (num_returned == 1) 1282 fprintf(stderr, ":\n"); 1283 else 1284 fprintf(stderr, "s:\n"); 1285 1286 for (i = 0; i < num_returned; i++) { 1287 fprintf(stdout, "%d:%d:%d\n", 1288 scsi_3btoul(dlist[i].cylinder), 1289 dlist[i].head, 1290 scsi_4btoul(dlist[i].sector)); 1291 } 1292 break; 1293 } 1294 case SRDDH10_BYTES_FROM_INDEX_FORMAT: 1295 { 1296 struct scsi_defect_desc_bytes_from_index *dlist; 1297 1298 dlist = (struct scsi_defect_desc_bytes_from_index *) 1299 (defect_list + 1300 sizeof(struct scsi_read_defect_data_hdr_10)); 1301 1302 num_returned = returned_length / 1303 sizeof(struct scsi_defect_desc_bytes_from_index); 1304 1305 fprintf(stderr, "Got %d defect", num_returned); 1306 1307 if ((lists_specified == 0) || (num_returned == 0)) { 1308 fprintf(stderr, "s.\n"); 1309 break; 1310 } else if (num_returned == 1) 1311 fprintf(stderr, ":\n"); 1312 else 1313 fprintf(stderr, "s:\n"); 1314 1315 for (i = 0; i < num_returned; i++) { 1316 fprintf(stdout, "%d:%d:%d\n", 1317 scsi_3btoul(dlist[i].cylinder), 1318 dlist[i].head, 1319 scsi_4btoul(dlist[i].bytes_from_index)); 1320 } 1321 break; 1322 } 1323 case SRDDH10_BLOCK_FORMAT: 1324 { 1325 struct scsi_defect_desc_block *dlist; 1326 1327 dlist = (struct scsi_defect_desc_block *)(defect_list + 1328 sizeof(struct scsi_read_defect_data_hdr_10)); 1329 1330 num_returned = returned_length / 1331 sizeof(struct scsi_defect_desc_block); 1332 1333 fprintf(stderr, "Got %d defect", num_returned); 1334 1335 if ((lists_specified == 0) || (num_returned == 0)) { 1336 fprintf(stderr, "s.\n"); 1337 break; 1338 } else if (num_returned == 1) 1339 fprintf(stderr, ":\n"); 1340 else 1341 fprintf(stderr, "s:\n"); 1342 1343 for (i = 0; i < num_returned; i++) 1344 fprintf(stdout, "%u\n", 1345 scsi_4btoul(dlist[i].address)); 1346 break; 1347 } 1348 default: 1349 fprintf(stderr, "Unknown defect format %d\n", 1350 returned_format & SRDDH10_DLIST_FORMAT_MASK); 1351 error = 1; 1352 break; 1353 } 1354 defect_bailout: 1355 1356 if (defect_list != NULL) 1357 free(defect_list); 1358 1359 if (ccb != NULL) 1360 cam_freeccb(ccb); 1361 1362 return(error); 1363 } 1364 1365 #if 0 1366 void 1367 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks) 1368 { 1369 union ccb *ccb; 1370 1371 ccb = cam_getccb(device); 1372 1373 cam_freeccb(ccb); 1374 } 1375 #endif 1376 1377 void 1378 mode_sense(struct cam_device *device, int mode_page, int page_control, 1379 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen) 1380 { 1381 union ccb *ccb; 1382 int retval; 1383 1384 ccb = cam_getccb(device); 1385 1386 if (ccb == NULL) 1387 errx(1, "mode_sense: couldn't allocate CCB"); 1388 1389 bzero(&(&ccb->ccb_h)[1], 1390 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1391 1392 scsi_mode_sense(&ccb->csio, 1393 /* retries */ retry_count, 1394 /* cbfcnp */ NULL, 1395 /* tag_action */ MSG_SIMPLE_Q_TAG, 1396 /* dbd */ dbd, 1397 /* page_code */ page_control << 6, 1398 /* page */ mode_page, 1399 /* param_buf */ data, 1400 /* param_len */ datalen, 1401 /* sense_len */ SSD_FULL_SIZE, 1402 /* timeout */ timeout ? timeout : 5000); 1403 1404 if (arglist & CAM_ARG_ERR_RECOVER) 1405 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1406 1407 /* Disable freezing the device queue */ 1408 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1409 1410 if (((retval = cam_send_ccb(device, ccb)) < 0) 1411 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1412 if (arglist & CAM_ARG_VERBOSE) { 1413 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1414 CAM_SCSI_STATUS_ERROR) 1415 scsi_sense_print(device, &ccb->csio, stderr); 1416 else 1417 fprintf(stderr, "CAM status is %#x\n", 1418 ccb->ccb_h.status); 1419 } 1420 cam_freeccb(ccb); 1421 cam_close_device(device); 1422 if (retval < 0) 1423 err(1, "error sending mode sense command"); 1424 else 1425 errx(1, "error sending mode sense command"); 1426 } 1427 1428 cam_freeccb(ccb); 1429 } 1430 1431 void 1432 mode_select(struct cam_device *device, int save_pages, int retry_count, 1433 int timeout, u_int8_t *data, int datalen) 1434 { 1435 union ccb *ccb; 1436 int retval; 1437 1438 ccb = cam_getccb(device); 1439 1440 if (ccb == NULL) 1441 errx(1, "mode_select: couldn't allocate CCB"); 1442 1443 bzero(&(&ccb->ccb_h)[1], 1444 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1445 1446 scsi_mode_select(&ccb->csio, 1447 /* retries */ retry_count, 1448 /* cbfcnp */ NULL, 1449 /* tag_action */ MSG_SIMPLE_Q_TAG, 1450 /* scsi_page_fmt */ 1, 1451 /* save_pages */ save_pages, 1452 /* param_buf */ data, 1453 /* param_len */ datalen, 1454 /* sense_len */ SSD_FULL_SIZE, 1455 /* timeout */ timeout ? timeout : 5000); 1456 1457 if (arglist & CAM_ARG_ERR_RECOVER) 1458 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1459 1460 /* Disable freezing the device queue */ 1461 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1462 1463 if (((retval = cam_send_ccb(device, ccb)) < 0) 1464 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1465 if (arglist & CAM_ARG_VERBOSE) { 1466 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1467 CAM_SCSI_STATUS_ERROR) 1468 scsi_sense_print(device, &ccb->csio, stderr); 1469 else 1470 fprintf(stderr, "CAM status is %#x\n", 1471 ccb->ccb_h.status); 1472 } 1473 cam_freeccb(ccb); 1474 cam_close_device(device); 1475 1476 if (retval < 0) 1477 err(1, "error sending mode select command"); 1478 else 1479 errx(1, "error sending mode select command"); 1480 1481 } 1482 1483 cam_freeccb(ccb); 1484 } 1485 1486 void 1487 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt, 1488 int retry_count, int timeout) 1489 { 1490 int c, mode_page = -1, page_control = 0; 1491 1492 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1493 switch(c) { 1494 case 'd': 1495 arglist |= CAM_ARG_DBD; 1496 break; 1497 case 'e': 1498 arglist |= CAM_ARG_MODE_EDIT; 1499 break; 1500 case 'm': 1501 mode_page = strtol(optarg, NULL, 0); 1502 if (mode_page < 0) 1503 errx(1, "invalid mode page %d", mode_page); 1504 break; 1505 case 'P': 1506 page_control = strtol(optarg, NULL, 0); 1507 if ((page_control < 0) || (page_control > 3)) 1508 errx(1, "invalid page control field %d", 1509 page_control); 1510 arglist |= CAM_ARG_PAGE_CNTL; 1511 break; 1512 default: 1513 break; 1514 } 1515 } 1516 1517 if (mode_page == -1) 1518 errx(1, "you must specify a mode page!"); 1519 1520 mode_edit(device, mode_page, page_control, arglist & CAM_ARG_DBD, 1521 arglist & CAM_ARG_MODE_EDIT, retry_count, timeout); 1522 } 1523 1524 static int 1525 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt, 1526 int retry_count, int timeout) 1527 { 1528 union ccb *ccb; 1529 u_int32_t flags = CAM_DIR_NONE; 1530 u_int8_t *data_ptr = NULL; 1531 u_int8_t cdb[20]; 1532 struct get_hook hook; 1533 int c, data_bytes = 0; 1534 int cdb_len = 0; 1535 char *datastr = NULL, *tstr; 1536 int error = 0; 1537 int fd_data = 0; 1538 int retval; 1539 1540 ccb = cam_getccb(device); 1541 1542 if (ccb == NULL) { 1543 warnx("scsicmd: error allocating ccb"); 1544 return(1); 1545 } 1546 1547 bzero(&(&ccb->ccb_h)[1], 1548 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1549 1550 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1551 switch(c) { 1552 case 'c': 1553 tstr = optarg; 1554 while (isspace(*tstr) && (*tstr != '\0')) 1555 tstr++; 1556 hook.argc = argc - optind; 1557 hook.argv = argv + optind; 1558 hook.got = 0; 1559 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr, 1560 iget, &hook); 1561 /* 1562 * Increment optind by the number of arguments the 1563 * encoding routine processed. After each call to 1564 * getopt(3), optind points to the argument that 1565 * getopt should process _next_. In this case, 1566 * that means it points to the first command string 1567 * argument, if there is one. Once we increment 1568 * this, it should point to either the next command 1569 * line argument, or it should be past the end of 1570 * the list. 1571 */ 1572 optind += hook.got; 1573 break; 1574 case 'i': 1575 if (arglist & CAM_ARG_CMD_OUT) { 1576 warnx("command must either be " 1577 "read or write, not both"); 1578 error = 1; 1579 goto scsicmd_bailout; 1580 } 1581 arglist |= CAM_ARG_CMD_IN; 1582 flags = CAM_DIR_IN; 1583 data_bytes = strtol(optarg, NULL, 0); 1584 if (data_bytes <= 0) { 1585 warnx("invalid number of input bytes %d", 1586 data_bytes); 1587 error = 1; 1588 goto scsicmd_bailout; 1589 } 1590 hook.argc = argc - optind; 1591 hook.argv = argv + optind; 1592 hook.got = 0; 1593 optind++; 1594 datastr = cget(&hook, NULL); 1595 /* 1596 * If the user supplied "-" instead of a format, he 1597 * wants the data to be written to stdout. 1598 */ 1599 if ((datastr != NULL) 1600 && (datastr[0] == '-')) 1601 fd_data = 1; 1602 1603 data_ptr = (u_int8_t *)malloc(data_bytes); 1604 break; 1605 case 'o': 1606 if (arglist & CAM_ARG_CMD_IN) { 1607 warnx("command must either be " 1608 "read or write, not both"); 1609 error = 1; 1610 goto scsicmd_bailout; 1611 } 1612 arglist |= CAM_ARG_CMD_OUT; 1613 flags = CAM_DIR_OUT; 1614 data_bytes = strtol(optarg, NULL, 0); 1615 if (data_bytes <= 0) { 1616 warnx("invalid number of output bytes %d", 1617 data_bytes); 1618 error = 1; 1619 goto scsicmd_bailout; 1620 } 1621 hook.argc = argc - optind; 1622 hook.argv = argv + optind; 1623 hook.got = 0; 1624 datastr = cget(&hook, NULL); 1625 data_ptr = (u_int8_t *)malloc(data_bytes); 1626 /* 1627 * If the user supplied "-" instead of a format, he 1628 * wants the data to be read from stdin. 1629 */ 1630 if ((datastr != NULL) 1631 && (datastr[0] == '-')) 1632 fd_data = 1; 1633 else 1634 buff_encode_visit(data_ptr, data_bytes, datastr, 1635 iget, &hook); 1636 optind += hook.got; 1637 break; 1638 default: 1639 break; 1640 } 1641 } 1642 1643 /* 1644 * If fd_data is set, and we're writing to the device, we need to 1645 * read the data the user wants written from stdin. 1646 */ 1647 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) { 1648 size_t amt_read; 1649 int amt_to_read = data_bytes; 1650 u_int8_t *buf_ptr = data_ptr; 1651 1652 for (amt_read = 0; amt_to_read > 0; 1653 amt_read = read(0, buf_ptr, amt_to_read)) { 1654 if (amt_read == -1) { 1655 warn("error reading data from stdin"); 1656 error = 1; 1657 goto scsicmd_bailout; 1658 } 1659 amt_to_read -= amt_read; 1660 buf_ptr += amt_read; 1661 } 1662 } 1663 1664 if (arglist & CAM_ARG_ERR_RECOVER) 1665 flags |= CAM_PASS_ERR_RECOVER; 1666 1667 /* Disable freezing the device queue */ 1668 flags |= CAM_DEV_QFRZDIS; 1669 1670 /* 1671 * This is taken from the SCSI-3 draft spec. 1672 * (T10/1157D revision 0.3) 1673 * The top 3 bits of an opcode are the group code. The next 5 bits 1674 * are the command code. 1675 * Group 0: six byte commands 1676 * Group 1: ten byte commands 1677 * Group 2: ten byte commands 1678 * Group 3: reserved 1679 * Group 4: sixteen byte commands 1680 * Group 5: twelve byte commands 1681 * Group 6: vendor specific 1682 * Group 7: vendor specific 1683 */ 1684 switch((cdb[0] >> 5) & 0x7) { 1685 case 0: 1686 cdb_len = 6; 1687 break; 1688 case 1: 1689 case 2: 1690 cdb_len = 10; 1691 break; 1692 case 3: 1693 case 6: 1694 case 7: 1695 /* computed by buff_encode_visit */ 1696 break; 1697 case 4: 1698 cdb_len = 16; 1699 break; 1700 case 5: 1701 cdb_len = 12; 1702 break; 1703 } 1704 1705 /* 1706 * We should probably use csio_build_visit or something like that 1707 * here, but it's easier to encode arguments as you go. The 1708 * alternative would be skipping the CDB argument and then encoding 1709 * it here, since we've got the data buffer argument by now. 1710 */ 1711 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len); 1712 1713 cam_fill_csio(&ccb->csio, 1714 /*retries*/ retry_count, 1715 /*cbfcnp*/ NULL, 1716 /*flags*/ flags, 1717 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1718 /*data_ptr*/ data_ptr, 1719 /*dxfer_len*/ data_bytes, 1720 /*sense_len*/ SSD_FULL_SIZE, 1721 /*cdb_len*/ cdb_len, 1722 /*timeout*/ timeout ? timeout : 5000); 1723 1724 if (((retval = cam_send_ccb(device, ccb)) < 0) 1725 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1726 if (retval < 0) 1727 warn("error sending command"); 1728 else 1729 warnx("error sending command"); 1730 1731 if (arglist & CAM_ARG_VERBOSE) { 1732 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1733 CAM_SCSI_STATUS_ERROR) 1734 scsi_sense_print(device, &ccb->csio, stderr); 1735 else 1736 fprintf(stderr, "CAM status is %#x\n", 1737 ccb->ccb_h.status); 1738 } 1739 1740 error = 1; 1741 goto scsicmd_bailout; 1742 } 1743 1744 1745 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1746 && (arglist & CAM_ARG_CMD_IN) 1747 && (data_bytes > 0)) { 1748 if (fd_data == 0) { 1749 buff_decode_visit(data_ptr, data_bytes, datastr, 1750 arg_put, NULL); 1751 fprintf(stdout, "\n"); 1752 } else { 1753 size_t amt_written; 1754 int amt_to_write = data_bytes; 1755 u_int8_t *buf_ptr = data_ptr; 1756 1757 for (amt_written = 0; (amt_to_write > 0) && 1758 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){ 1759 amt_to_write -= amt_written; 1760 buf_ptr += amt_written; 1761 } 1762 if (amt_written == -1) { 1763 warn("error writing data to stdout"); 1764 error = 1; 1765 goto scsicmd_bailout; 1766 } else if ((amt_written == 0) 1767 && (amt_to_write > 0)) { 1768 warnx("only wrote %u bytes out of %u", 1769 data_bytes - amt_to_write, data_bytes); 1770 } 1771 } 1772 } 1773 1774 scsicmd_bailout: 1775 1776 if ((data_bytes > 0) && (data_ptr != NULL)) 1777 free(data_ptr); 1778 1779 cam_freeccb(ccb); 1780 1781 return(error); 1782 } 1783 1784 static int 1785 camdebug(int argc, char **argv, char *combinedopt) 1786 { 1787 int c, fd; 1788 int bus = -1, target = -1, lun = -1; 1789 char *tstr, *tmpstr = NULL; 1790 union ccb ccb; 1791 int error = 0; 1792 1793 bzero(&ccb, sizeof(union ccb)); 1794 1795 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1796 switch(c) { 1797 case 'I': 1798 arglist |= CAM_ARG_DEBUG_INFO; 1799 ccb.cdbg.flags |= CAM_DEBUG_INFO; 1800 break; 1801 case 'S': 1802 arglist |= CAM_ARG_DEBUG_TRACE; 1803 ccb.cdbg.flags |= CAM_DEBUG_TRACE; 1804 break; 1805 case 'T': 1806 arglist |= CAM_ARG_DEBUG_SUBTRACE; 1807 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE; 1808 break; 1809 case 'c': 1810 arglist |= CAM_ARG_DEBUG_CDB; 1811 ccb.cdbg.flags |= CAM_DEBUG_CDB; 1812 break; 1813 default: 1814 break; 1815 } 1816 } 1817 1818 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1819 warnx("error opening transport layer device %s", XPT_DEVICE); 1820 warn("%s", XPT_DEVICE); 1821 return(1); 1822 } 1823 argc -= optind; 1824 argv += optind; 1825 1826 if (argc <= 0) { 1827 warnx("you must specify \"off\", \"all\" or a bus,"); 1828 warnx("bus:target, or bus:target:lun"); 1829 close(fd); 1830 return(1); 1831 } 1832 1833 tstr = *argv; 1834 1835 while (isspace(*tstr) && (*tstr != '\0')) 1836 tstr++; 1837 1838 if (strncmp(tstr, "off", 3) == 0) { 1839 ccb.cdbg.flags = CAM_DEBUG_NONE; 1840 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_TRACE| 1841 CAM_ARG_DEBUG_SUBTRACE); 1842 } else if (strncmp(tstr, "all", 3) != 0) { 1843 tmpstr = (char *)strtok(tstr, ":"); 1844 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1845 bus = strtol(tmpstr, NULL, 0); 1846 arglist |= CAM_ARG_BUS; 1847 tmpstr = (char *)strtok(NULL, ":"); 1848 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1849 target = strtol(tmpstr, NULL, 0); 1850 arglist |= CAM_ARG_TARGET; 1851 tmpstr = (char *)strtok(NULL, ":"); 1852 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1853 lun = strtol(tmpstr, NULL, 0); 1854 arglist |= CAM_ARG_LUN; 1855 } 1856 } 1857 } else { 1858 error = 1; 1859 warnx("you must specify \"all\", \"off\", or a bus,"); 1860 warnx("bus:target, or bus:target:lun to debug"); 1861 } 1862 } 1863 1864 if (error == 0) { 1865 1866 ccb.ccb_h.func_code = XPT_DEBUG; 1867 ccb.ccb_h.path_id = bus; 1868 ccb.ccb_h.target_id = target; 1869 ccb.ccb_h.target_lun = lun; 1870 1871 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1872 warn("CAMIOCOMMAND ioctl failed"); 1873 error = 1; 1874 } 1875 1876 if (error == 0) { 1877 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == 1878 CAM_FUNC_NOTAVAIL) { 1879 warnx("CAM debugging not available"); 1880 warnx("you need to put options CAMDEBUG in" 1881 " your kernel config file!"); 1882 error = 1; 1883 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) != 1884 CAM_REQ_CMP) { 1885 warnx("XPT_DEBUG CCB failed with status %#x", 1886 ccb.ccb_h.status); 1887 error = 1; 1888 } else { 1889 if (ccb.cdbg.flags == CAM_DEBUG_NONE) { 1890 fprintf(stderr, 1891 "Debugging turned off\n"); 1892 } else { 1893 fprintf(stderr, 1894 "Debugging enabled for " 1895 "%d:%d:%d\n", 1896 bus, target, lun); 1897 } 1898 } 1899 } 1900 close(fd); 1901 } 1902 1903 return(error); 1904 } 1905 1906 static int 1907 tagcontrol(struct cam_device *device, int argc, char **argv, 1908 char *combinedopt) 1909 { 1910 int c; 1911 union ccb *ccb; 1912 int numtags = -1; 1913 int retval = 0; 1914 int quiet = 0; 1915 char pathstr[1024]; 1916 1917 ccb = cam_getccb(device); 1918 1919 if (ccb == NULL) { 1920 warnx("tagcontrol: error allocating ccb"); 1921 return(1); 1922 } 1923 1924 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1925 switch(c) { 1926 case 'N': 1927 numtags = strtol(optarg, NULL, 0); 1928 if (numtags < 0) { 1929 warnx("tag count %d is < 0", numtags); 1930 retval = 1; 1931 goto tagcontrol_bailout; 1932 } 1933 break; 1934 case 'q': 1935 quiet++; 1936 break; 1937 default: 1938 break; 1939 } 1940 } 1941 1942 cam_path_string(device, pathstr, sizeof(pathstr)); 1943 1944 if (numtags >= 0) { 1945 bzero(&(&ccb->ccb_h)[1], 1946 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr)); 1947 ccb->ccb_h.func_code = XPT_REL_SIMQ; 1948 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS; 1949 ccb->crs.openings = numtags; 1950 1951 1952 if (cam_send_ccb(device, ccb) < 0) { 1953 perror("error sending XPT_REL_SIMQ CCB"); 1954 retval = 1; 1955 goto tagcontrol_bailout; 1956 } 1957 1958 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1959 warnx("XPT_REL_SIMQ CCB failed, status %#x", 1960 ccb->ccb_h.status); 1961 retval = 1; 1962 goto tagcontrol_bailout; 1963 } 1964 1965 1966 if (quiet == 0) 1967 fprintf(stdout, "%stagged openings now %d\n", 1968 pathstr, ccb->crs.openings); 1969 } 1970 1971 bzero(&(&ccb->ccb_h)[1], 1972 sizeof(struct ccb_getdev) - sizeof(struct ccb_hdr)); 1973 1974 ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1975 1976 if (cam_send_ccb(device, ccb) < 0) { 1977 perror("error sending XPT_GDEV_TYPE CCB"); 1978 retval = 1; 1979 goto tagcontrol_bailout; 1980 } 1981 1982 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1983 warnx("XPT_GDEV_TYPE CCB failed, status %#x", 1984 ccb->ccb_h.status); 1985 retval = 1; 1986 goto tagcontrol_bailout; 1987 } 1988 1989 if (arglist & CAM_ARG_VERBOSE) { 1990 fprintf(stdout, "%s", pathstr); 1991 fprintf(stdout, "dev_openings %d\n", ccb->cgd.dev_openings); 1992 fprintf(stdout, "%s", pathstr); 1993 fprintf(stdout, "dev_active %d\n", ccb->cgd.dev_active); 1994 fprintf(stdout, "%s", pathstr); 1995 fprintf(stdout, "devq_openings %d\n", ccb->cgd.devq_openings); 1996 fprintf(stdout, "%s", pathstr); 1997 fprintf(stdout, "devq_queued %d\n", ccb->cgd.devq_queued); 1998 fprintf(stdout, "%s", pathstr); 1999 fprintf(stdout, "held %d\n", ccb->cgd.held); 2000 fprintf(stdout, "%s", pathstr); 2001 fprintf(stdout, "mintags %d\n", ccb->cgd.mintags); 2002 fprintf(stdout, "%s", pathstr); 2003 fprintf(stdout, "maxtags %d\n", ccb->cgd.maxtags); 2004 } else { 2005 if (quiet == 0) { 2006 fprintf(stdout, "%s", pathstr); 2007 fprintf(stdout, "device openings: "); 2008 } 2009 fprintf(stdout, "%d\n", ccb->cgd.dev_openings + 2010 ccb->cgd.dev_active); 2011 } 2012 2013 tagcontrol_bailout: 2014 2015 cam_freeccb(ccb); 2016 return(retval); 2017 } 2018 2019 static void 2020 cts_print(struct cam_device *device, struct ccb_trans_settings *cts) 2021 { 2022 char pathstr[1024]; 2023 2024 cam_path_string(device, pathstr, sizeof(pathstr)); 2025 2026 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) { 2027 2028 fprintf(stdout, "%ssync parameter: %d\n", pathstr, 2029 cts->sync_period); 2030 2031 if (cts->sync_offset != 0) { 2032 u_int freq; 2033 2034 freq = scsi_calc_syncsrate(cts->sync_period); 2035 fprintf(stdout, "%sfrequencey: %d.%03dMHz\n", pathstr, 2036 freq / 1000, freq % 1000); 2037 } 2038 } 2039 2040 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) 2041 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset); 2042 2043 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) 2044 fprintf(stdout, "%sbus width: %d bits\n", pathstr, 2045 (0x01 << cts->bus_width) * 8); 2046 2047 if (cts->valid & CCB_TRANS_DISC_VALID) 2048 fprintf(stdout, "%sdisconnection is %s\n", pathstr, 2049 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" : 2050 "disabled"); 2051 2052 if (cts->valid & CCB_TRANS_TQ_VALID) 2053 fprintf(stdout, "%stagged queueing is %s\n", pathstr, 2054 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" : 2055 "disabled"); 2056 2057 } 2058 2059 /* 2060 * Get a path inquiry CCB for the specified device. 2061 */ 2062 static int 2063 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi) 2064 { 2065 union ccb *ccb; 2066 int retval = 0; 2067 2068 ccb = cam_getccb(device); 2069 2070 if (ccb == NULL) { 2071 warnx("get_cpi: couldn't allocate CCB"); 2072 return(1); 2073 } 2074 2075 bzero(&(&ccb->ccb_h)[1], 2076 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2077 2078 ccb->ccb_h.func_code = XPT_PATH_INQ; 2079 2080 if (cam_send_ccb(device, ccb) < 0) { 2081 warn("get_cpi: error sending Path Inquiry CCB"); 2082 2083 if (arglist & CAM_ARG_VERBOSE) 2084 fprintf(stderr, "CAM status is %#x\n", 2085 ccb->ccb_h.status); 2086 2087 retval = 1; 2088 2089 goto get_cpi_bailout; 2090 } 2091 2092 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2093 2094 if (arglist & CAM_ARG_VERBOSE) 2095 fprintf(stderr, "get_cpi: CAM status is %#x\n", 2096 ccb->ccb_h.status); 2097 2098 retval = 1; 2099 2100 goto get_cpi_bailout; 2101 } 2102 2103 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq)); 2104 2105 get_cpi_bailout: 2106 2107 cam_freeccb(ccb); 2108 2109 return(retval); 2110 } 2111 2112 static void 2113 cpi_print(struct ccb_pathinq *cpi) 2114 { 2115 char adapter_str[1024]; 2116 int i; 2117 2118 snprintf(adapter_str, sizeof(adapter_str), 2119 "%s%d:", cpi->dev_name, cpi->unit_number); 2120 2121 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str, 2122 cpi->version_num); 2123 2124 for (i = 1; i < 0xff; i = i << 1) { 2125 char *str; 2126 2127 if ((i & cpi->hba_inquiry) == 0) 2128 continue; 2129 2130 fprintf(stdout, "%s supports ", adapter_str); 2131 2132 switch(i) { 2133 case PI_MDP_ABLE: 2134 str = "MDP message"; 2135 break; 2136 case PI_WIDE_32: 2137 str = "32 bit wide SCSI"; 2138 break; 2139 case PI_WIDE_16: 2140 str = "16 bit wide SCSI"; 2141 break; 2142 case PI_SDTR_ABLE: 2143 str = "SDTR message"; 2144 break; 2145 case PI_LINKED_CDB: 2146 str = "linked CDBs"; 2147 break; 2148 case PI_TAG_ABLE: 2149 str = "tag queue messages"; 2150 break; 2151 case PI_SOFT_RST: 2152 str = "soft reset alternative"; 2153 break; 2154 default: 2155 str = "unknown PI bit set"; 2156 break; 2157 } 2158 fprintf(stdout, "%s\n", str); 2159 } 2160 2161 for (i = 1; i < 0xff; i = i << 1) { 2162 char *str; 2163 2164 if ((i & cpi->hba_misc) == 0) 2165 continue; 2166 2167 fprintf(stdout, "%s ", adapter_str); 2168 2169 switch(i) { 2170 case PIM_SCANHILO: 2171 str = "bus scans from high ID to low ID"; 2172 break; 2173 case PIM_NOREMOVE: 2174 str = "removable devices not included in scan"; 2175 break; 2176 case PIM_NOINITIATOR: 2177 str = "initiator role not supported"; 2178 break; 2179 case PIM_NOBUSRESET: 2180 str = "user has disabled initial BUS RESET or" 2181 " controller is in target/mixed mode"; 2182 break; 2183 default: 2184 str = "unknown PIM bit set"; 2185 break; 2186 } 2187 fprintf(stdout, "%s\n", str); 2188 } 2189 2190 for (i = 1; i < 0xff; i = i << 1) { 2191 char *str; 2192 2193 if ((i & cpi->target_sprt) == 0) 2194 continue; 2195 2196 fprintf(stdout, "%s supports ", adapter_str); 2197 switch(i) { 2198 case PIT_PROCESSOR: 2199 str = "target mode processor mode"; 2200 break; 2201 case PIT_PHASE: 2202 str = "target mode phase cog. mode"; 2203 break; 2204 case PIT_DISCONNECT: 2205 str = "disconnects in target mode"; 2206 break; 2207 case PIT_TERM_IO: 2208 str = "terminate I/O message in target mode"; 2209 break; 2210 case PIT_GRP_6: 2211 str = "group 6 commands in target mode"; 2212 break; 2213 case PIT_GRP_7: 2214 str = "group 7 commands in target mode"; 2215 break; 2216 default: 2217 str = "unknown PIT bit set"; 2218 break; 2219 } 2220 2221 fprintf(stdout, "%s\n", str); 2222 } 2223 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str, 2224 cpi->hba_eng_cnt); 2225 fprintf(stdout, "%s maxium target: %d\n", adapter_str, 2226 cpi->max_target); 2227 fprintf(stdout, "%s maxium LUN: %d\n", adapter_str, 2228 cpi->max_lun); 2229 fprintf(stdout, "%s highest path ID in subsystem: %d\n", 2230 adapter_str, cpi->hpath_id); 2231 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid); 2232 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid); 2233 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id); 2234 fprintf(stdout, "%s base transfer speed: ", adapter_str); 2235 if (cpi->base_transfer_speed > 1000) 2236 fprintf(stdout, "%d.%03dMB/sec\n", 2237 cpi->base_transfer_speed / 1000, 2238 cpi->base_transfer_speed % 1000); 2239 else 2240 fprintf(stdout, "%dKB/sec\n", 2241 (cpi->base_transfer_speed % 1000) * 1000); 2242 } 2243 2244 static int 2245 get_print_cts(struct cam_device *device, int user_settings, int quiet, 2246 struct ccb_trans_settings *cts) 2247 { 2248 int retval; 2249 union ccb *ccb; 2250 2251 retval = 0; 2252 ccb = cam_getccb(device); 2253 2254 if (ccb == NULL) { 2255 warnx("get_print_cts: error allocating ccb"); 2256 return(1); 2257 } 2258 2259 bzero(&(&ccb->ccb_h)[1], 2260 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2261 2262 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 2263 2264 if (user_settings == 0) 2265 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS; 2266 else 2267 ccb->cts.flags = CCB_TRANS_USER_SETTINGS; 2268 2269 if (cam_send_ccb(device, ccb) < 0) { 2270 perror("error sending XPT_GET_TRAN_SETTINGS CCB"); 2271 retval = 1; 2272 goto get_print_cts_bailout; 2273 } 2274 2275 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2276 warnx("XPT_GET_TRANS_SETTINGS CCB failed, status %#x", 2277 ccb->ccb_h.status); 2278 retval = 1; 2279 goto get_print_cts_bailout; 2280 } 2281 2282 if (quiet == 0) 2283 cts_print(device, &ccb->cts); 2284 2285 if (cts != NULL) 2286 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings)); 2287 2288 get_print_cts_bailout: 2289 2290 cam_freeccb(ccb); 2291 2292 return(retval); 2293 } 2294 2295 static int 2296 ratecontrol(struct cam_device *device, int retry_count, int timeout, 2297 int argc, char **argv, char *combinedopt) 2298 { 2299 int c; 2300 union ccb *ccb; 2301 int user_settings = 0; 2302 int retval = 0; 2303 int disc_enable = -1, tag_enable = -1; 2304 int offset = -1; 2305 double syncrate = -1; 2306 int bus_width = -1; 2307 int quiet = 0; 2308 int change_settings = 0, send_tur = 0; 2309 struct ccb_pathinq cpi; 2310 2311 ccb = cam_getccb(device); 2312 2313 if (ccb == NULL) { 2314 warnx("ratecontrol: error allocating ccb"); 2315 return(1); 2316 } 2317 2318 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2319 switch(c){ 2320 case 'a': 2321 send_tur = 1; 2322 break; 2323 case 'c': 2324 user_settings = 0; 2325 break; 2326 case 'D': 2327 if (strncasecmp(optarg, "enable", 6) == 0) 2328 disc_enable = 1; 2329 else if (strncasecmp(optarg, "disable", 7) == 0) 2330 disc_enable = 0; 2331 else { 2332 warnx("-D argument \"%s\" is unknown", optarg); 2333 retval = 1; 2334 goto ratecontrol_bailout; 2335 } 2336 change_settings = 1; 2337 break; 2338 case 'O': 2339 offset = strtol(optarg, NULL, 0); 2340 if (offset < 0) { 2341 warnx("offset value %d is < 0", offset); 2342 retval = 1; 2343 goto ratecontrol_bailout; 2344 } 2345 change_settings = 1; 2346 break; 2347 case 'q': 2348 quiet++; 2349 break; 2350 case 'R': 2351 syncrate = atof(optarg); 2352 2353 if (syncrate < 0) { 2354 warnx("sync rate %f is < 0", syncrate); 2355 retval = 1; 2356 goto ratecontrol_bailout; 2357 } 2358 change_settings = 1; 2359 break; 2360 case 'T': 2361 if (strncasecmp(optarg, "enable", 6) == 0) 2362 tag_enable = 1; 2363 else if (strncasecmp(optarg, "disable", 7) == 0) 2364 tag_enable = 0; 2365 else { 2366 warnx("-T argument \"%s\" is unknown", optarg); 2367 retval = 1; 2368 goto ratecontrol_bailout; 2369 } 2370 change_settings = 1; 2371 break; 2372 case 'U': 2373 user_settings = 1; 2374 break; 2375 case 'W': 2376 bus_width = strtol(optarg, NULL, 0); 2377 if (bus_width < 0) { 2378 warnx("bus width %d is < 0", bus_width); 2379 retval = 1; 2380 goto ratecontrol_bailout; 2381 } 2382 change_settings = 1; 2383 break; 2384 default: 2385 break; 2386 } 2387 } 2388 2389 bzero(&(&ccb->ccb_h)[1], 2390 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2391 2392 /* 2393 * Grab path inquiry information, so we can determine whether 2394 * or not the initiator is capable of the things that the user 2395 * requests. 2396 */ 2397 ccb->ccb_h.func_code = XPT_PATH_INQ; 2398 2399 if (cam_send_ccb(device, ccb) < 0) { 2400 perror("error sending XPT_PATH_INQ CCB"); 2401 retval = 1; 2402 goto ratecontrol_bailout; 2403 } 2404 2405 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2406 warnx("XPT_PATH_INQ CCB failed, status %#x", 2407 ccb->ccb_h.status); 2408 retval = 1; 2409 goto ratecontrol_bailout; 2410 } 2411 2412 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq)); 2413 2414 bzero(&(&ccb->ccb_h)[1], 2415 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2416 2417 if (quiet == 0) 2418 fprintf(stdout, "Current Parameters:\n"); 2419 2420 retval = get_print_cts(device, user_settings, quiet, &ccb->cts); 2421 2422 if (retval != 0) 2423 goto ratecontrol_bailout; 2424 2425 if (arglist & CAM_ARG_VERBOSE) 2426 cpi_print(&cpi); 2427 2428 if (change_settings) { 2429 if (disc_enable != -1) { 2430 ccb->cts.valid |= CCB_TRANS_DISC_VALID; 2431 if (disc_enable == 0) 2432 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB; 2433 else 2434 ccb->cts.flags |= CCB_TRANS_DISC_ENB; 2435 } else 2436 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID; 2437 2438 if (tag_enable != -1) { 2439 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) { 2440 warnx("HBA does not support tagged queueing, " 2441 "so you cannot modify tag settings"); 2442 retval = 1; 2443 goto ratecontrol_bailout; 2444 } 2445 2446 ccb->cts.valid |= CCB_TRANS_TQ_VALID; 2447 2448 if (tag_enable == 0) 2449 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB; 2450 else 2451 ccb->cts.flags |= CCB_TRANS_TAG_ENB; 2452 } else 2453 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID; 2454 2455 if (offset != -1) { 2456 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2457 warnx("HBA at %s%d is not cable of changing " 2458 "offset", cpi.dev_name, 2459 cpi.unit_number); 2460 retval = 1; 2461 goto ratecontrol_bailout; 2462 } 2463 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID; 2464 ccb->cts.sync_offset = offset; 2465 } else 2466 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID; 2467 2468 if (syncrate != -1) { 2469 int prelim_sync_period; 2470 u_int freq; 2471 2472 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2473 warnx("HBA at %s%d is not cable of changing " 2474 "transfer rates", cpi.dev_name, 2475 cpi.unit_number); 2476 retval = 1; 2477 goto ratecontrol_bailout; 2478 } 2479 2480 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID; 2481 2482 /* 2483 * The sync rate the user gives us is in MHz. 2484 * We need to translate it into KHz for this 2485 * calculation. 2486 */ 2487 syncrate *= 1000; 2488 2489 /* 2490 * Next, we calculate a "preliminary" sync period 2491 * in tenths of a nanosecond. 2492 */ 2493 if (syncrate == 0) 2494 prelim_sync_period = 0; 2495 else 2496 prelim_sync_period = 10000000 / syncrate; 2497 2498 ccb->cts.sync_period = 2499 scsi_calc_syncparam(prelim_sync_period); 2500 2501 freq = scsi_calc_syncsrate(ccb->cts.sync_period); 2502 } else 2503 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID; 2504 2505 /* 2506 * The bus_width argument goes like this: 2507 * 0 == 8 bit 2508 * 1 == 16 bit 2509 * 2 == 32 bit 2510 * Therefore, if you shift the number of bits given on the 2511 * command line right by 4, you should get the correct 2512 * number. 2513 */ 2514 if (bus_width != -1) { 2515 2516 /* 2517 * We might as well validate things here with a 2518 * decipherable error message, rather than what 2519 * will probably be an indecipherable error message 2520 * by the time it gets back to us. 2521 */ 2522 if ((bus_width == 16) 2523 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) { 2524 warnx("HBA does not support 16 bit bus width"); 2525 retval = 1; 2526 goto ratecontrol_bailout; 2527 } else if ((bus_width == 32) 2528 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) { 2529 warnx("HBA does not support 32 bit bus width"); 2530 retval = 1; 2531 goto ratecontrol_bailout; 2532 } else if ((bus_width != 8) 2533 && (bus_width != 16) 2534 && (bus_width != 32)) { 2535 warnx("Invalid bus width %d", bus_width); 2536 retval = 1; 2537 goto ratecontrol_bailout; 2538 } 2539 2540 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID; 2541 ccb->cts.bus_width = bus_width >> 4; 2542 } else 2543 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID; 2544 2545 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 2546 2547 if (cam_send_ccb(device, ccb) < 0) { 2548 perror("error sending XPT_SET_TRAN_SETTINGS CCB"); 2549 retval = 1; 2550 goto ratecontrol_bailout; 2551 } 2552 2553 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2554 warnx("XPT_SET_TRANS_SETTINGS CCB failed, status %#x", 2555 ccb->ccb_h.status); 2556 retval = 1; 2557 goto ratecontrol_bailout; 2558 } 2559 } 2560 2561 if (send_tur) { 2562 retval = testunitready(device, retry_count, timeout, 2563 (arglist & CAM_ARG_VERBOSE) ? 0 : 1); 2564 2565 /* 2566 * If the TUR didn't succeed, just bail. 2567 */ 2568 if (retval != 0) { 2569 if (quiet == 0) 2570 fprintf(stderr, "Test Unit Ready failed\n"); 2571 goto ratecontrol_bailout; 2572 } 2573 2574 /* 2575 * If the user wants things quiet, there's no sense in 2576 * getting the transfer settings, if we're not going 2577 * to print them. 2578 */ 2579 if (quiet != 0) 2580 goto ratecontrol_bailout; 2581 2582 fprintf(stdout, "New Parameters:\n"); 2583 retval = get_print_cts(device, user_settings, 0, NULL); 2584 } 2585 2586 ratecontrol_bailout: 2587 2588 cam_freeccb(ccb); 2589 return(retval); 2590 } 2591 2592 void 2593 usage(int verbose) 2594 { 2595 fprintf(stderr, 2596 "usage: camcontrol <command> [device id][generic args][command args]\n" 2597 " camcontrol devlist [-v]\n" 2598 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n" 2599 " camcontrol tur [dev_id][generic args]\n" 2600 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n" 2601 " camcontrol start [dev_id][generic args]\n" 2602 " camcontrol stop [dev_id][generic args]\n" 2603 " camcontrol eject [dev_id][generic args]\n" 2604 " camcontrol rescan <bus[:target:lun]>\n" 2605 " camcontrol reset <bus[:target:lun]>\n" 2606 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n" 2607 " camcontrol modepage [dev_id][generic args] <-m page> [-P pagectl]\n" 2608 " [-e][-d]\n" 2609 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n" 2610 " [-i len fmt|-o len fmt [args]]\n" 2611 " camcontrol debug [-I][-T][-S][-c] <all|bus[:target[:lun]]|off>\n" 2612 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n" 2613 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n" 2614 " [-D <enable|disable>][-O offset][-q]\n" 2615 " [-R syncrate][-v][-T <enable|disable>]\n" 2616 " [-U][-W bus_width]\n" 2617 " camcontrol help\n"); 2618 if (!verbose) 2619 return; 2620 fprintf(stderr, 2621 "Specify one of the following options:\n" 2622 "devlist list all CAM devices\n" 2623 "periphlist list all CAM peripheral drivers attached to a device\n" 2624 "tur send a test unit ready to the named device\n" 2625 "inquiry send a SCSI inquiry command to the named device\n" 2626 "start send a Start Unit command to the device\n" 2627 "stop send a Stop Unit command to the device\n" 2628 "eject send a Stop Unit command to the device with the eject bit set\n" 2629 "rescan rescan the given bus, or bus:target:lun\n" 2630 "reset reset the given bus, or bus:target:lun\n" 2631 "defects read the defect list of the specified device\n" 2632 "modepage display or edit (-e) the given mode page\n" 2633 "cmd send the given scsi command, may need -i or -o as well\n" 2634 "debug turn debugging on/off for a bus, target, or lun, or all devices\n" 2635 "tags report or set the number of transaction slots for a device\n" 2636 "negotiate report or set device negotiation parameters\n" 2637 "help this message\n" 2638 "Device Identifiers:\n" 2639 "bus:target specify the bus and target, lun defaults to 0\n" 2640 "bus:target:lun specify the bus, target and lun\n" 2641 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n" 2642 "Generic arguments:\n" 2643 "-v be verbose, print out sense information\n" 2644 "-t timeout command timeout in seconds, overrides default timeout\n" 2645 "-n dev_name specify device name (default is %s)\n" 2646 "-u unit specify unit number (default is %d)\n" 2647 "-E have the kernel attempt to perform SCSI error recovery\n" 2648 "-C count specify the SCSI command retry count (needs -E to work)\n" 2649 "modepage arguments:\n" 2650 "-m page specify the mode page to view or edit\n" 2651 "-e edit the specified mode page\n" 2652 "-d disable block descriptors for mode sense\n" 2653 "-P pgctl page control field 0-3\n" 2654 "defects arguments:\n" 2655 "-f format specify defect list format (block, bfi or phys)\n" 2656 "-G get the grown defect list\n" 2657 "-P get the permanant defect list\n" 2658 "inquiry arguments:\n" 2659 "-D get the standard inquiry data\n" 2660 "-S get the serial number\n" 2661 "-R get the transfer rate, etc.\n" 2662 "cmd arguments:\n" 2663 "-c cdb [args] specify the SCSI CDB\n" 2664 "-i len fmt specify input data and input data format\n" 2665 "-o len fmt [args] specify output data and output data fmt\n" 2666 "debug arguments:\n" 2667 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n" 2668 "-T CAM_DEBUG_TRACE -- routine flow tracking\n" 2669 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n" 2670 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n" 2671 "tags arguments:\n" 2672 "-N tags specify the number of tags to use for this device\n" 2673 "-q be quiet, don't report the number of tags\n" 2674 "-v report a number of tag-related parameters\n" 2675 "negotiate arguments:\n" 2676 "-a send a test unit ready after negotiation\n" 2677 "-c report/set current negotiation settings\n" 2678 "-D <arg> \"enable\" or \"disable\" disconnection\n" 2679 "-O offset set command delay offset\n" 2680 "-q be quiet, don't report anything\n" 2681 "-R syncrate synchronization rate in MHz\n" 2682 "-T <arg> \"enable\" or \"disable\" tagged queueing\n" 2683 "-U report/set user negotiation settings\n" 2684 "-W bus_width set the bus width in bits (8, 16 or 32)\n" 2685 "-v also print a Path Inquiry CCB for the controller\n", 2686 DEFAULT_DEVICE, DEFAULT_UNIT); 2687 } 2688 2689 int 2690 main(int argc, char **argv) 2691 { 2692 int c; 2693 char *device = NULL; 2694 int unit = 0; 2695 struct cam_device *cam_dev = NULL; 2696 int timeout = 0, retry_count = 1; 2697 camcontrol_optret optreturn; 2698 char *tstr; 2699 char *mainopt = "C:En:t:u:v"; 2700 char *subopt = NULL; 2701 char combinedopt[256]; 2702 int error = 0, optstart = 2; 2703 int devopen = 1; 2704 2705 arglist = CAM_ARG_NONE; 2706 2707 if (argc < 2) { 2708 usage(0); 2709 exit(1); 2710 } 2711 2712 /* 2713 * Get the base option. 2714 */ 2715 optreturn = getoption(argv[1], &arglist, &subopt); 2716 2717 if (optreturn == CC_OR_AMBIGUOUS) { 2718 warnx("ambiguous option %s", argv[1]); 2719 usage(0); 2720 exit(1); 2721 } else if (optreturn == CC_OR_NOT_FOUND) { 2722 warnx("option %s not found", argv[1]); 2723 usage(0); 2724 exit(1); 2725 } 2726 2727 /* 2728 * Ahh, getopt(3) is a pain. 2729 * 2730 * This is a gross hack. There really aren't many other good 2731 * options (excuse the pun) for parsing options in a situation like 2732 * this. getopt is kinda braindead, so you end up having to run 2733 * through the options twice, and give each invocation of getopt 2734 * the option string for the other invocation. 2735 * 2736 * You would think that you could just have two groups of options. 2737 * The first group would get parsed by the first invocation of 2738 * getopt, and the second group would get parsed by the second 2739 * invocation of getopt. It doesn't quite work out that way. When 2740 * the first invocation of getopt finishes, it leaves optind pointing 2741 * to the argument _after_ the first argument in the second group. 2742 * So when the second invocation of getopt comes around, it doesn't 2743 * recognize the first argument it gets and then bails out. 2744 * 2745 * A nice alternative would be to have a flag for getopt that says 2746 * "just keep parsing arguments even when you encounter an unknown 2747 * argument", but there isn't one. So there's no real clean way to 2748 * easily parse two sets of arguments without having one invocation 2749 * of getopt know about the other. 2750 * 2751 * Without this hack, the first invocation of getopt would work as 2752 * long as the generic arguments are first, but the second invocation 2753 * (in the subfunction) would fail in one of two ways. In the case 2754 * where you don't set optreset, it would fail because optind may be 2755 * pointing to the argument after the one it should be pointing at. 2756 * In the case where you do set optreset, and reset optind, it would 2757 * fail because getopt would run into the first set of options, which 2758 * it doesn't understand. 2759 * 2760 * All of this would "sort of" work if you could somehow figure out 2761 * whether optind had been incremented one option too far. The 2762 * mechanics of that, however, are more daunting than just giving 2763 * both invocations all of the expect options for either invocation. 2764 * 2765 * Needless to say, I wouldn't mind if someone invented a better 2766 * (non-GPL!) command line parsing interface than getopt. I 2767 * wouldn't mind if someone added more knobs to getopt to make it 2768 * work better. Who knows, I may talk myself into doing it someday, 2769 * if the standards weenies let me. As it is, it just leads to 2770 * hackery like this and causes people to avoid it in some cases. 2771 * 2772 * KDM, September 8th, 1998 2773 */ 2774 if (subopt != NULL) 2775 sprintf(combinedopt, "%s%s", mainopt, subopt); 2776 else 2777 sprintf(combinedopt, "%s", mainopt); 2778 2779 /* 2780 * For these options we do not parse optional device arguments and 2781 * we do not open a passthrough device. 2782 */ 2783 if (((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_RESCAN) 2784 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_RESET) 2785 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_DEVTREE) 2786 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_USAGE) 2787 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_DEBUG)) 2788 devopen = 0; 2789 2790 if ((devopen == 1) 2791 && (argc > 2 && argv[2][0] != '-')) { 2792 char name[30]; 2793 int rv; 2794 2795 /* 2796 * First catch people who try to do things like: 2797 * camcontrol tur /dev/rsd0.ctl 2798 * camcontrol doesn't take device nodes as arguments. 2799 */ 2800 if (argv[2][0] == '/') { 2801 warnx("%s is not a valid device identifier", argv[2]); 2802 errx(1, "please read the camcontrol(8) man page"); 2803 } else if (isdigit(argv[2][0])) { 2804 /* device specified as bus:target[:lun] */ 2805 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist); 2806 if (rv < 2) 2807 errx(1, "numeric device specification must " 2808 "be either bus:target, or " 2809 "bus:target:lun"); 2810 optstart++; 2811 } else { 2812 if (cam_get_device(argv[2], name, sizeof name, &unit) 2813 == -1) 2814 errx(1, "%s", cam_errbuf); 2815 device = strdup(name); 2816 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT; 2817 optstart++; 2818 } 2819 } 2820 /* 2821 * Start getopt processing at argv[2/3], since we've already 2822 * accepted argv[1..2] as the command name, and as a possible 2823 * device name. 2824 */ 2825 optind = optstart; 2826 2827 /* 2828 * Now we run through the argument list looking for generic 2829 * options, and ignoring options that possibly belong to 2830 * subfunctions. 2831 */ 2832 while ((c = getopt(argc, argv, combinedopt))!= -1){ 2833 switch(c) { 2834 case 'C': 2835 retry_count = strtol(optarg, NULL, 0); 2836 if (retry_count < 0) 2837 errx(1, "retry count %d is < 0", 2838 retry_count); 2839 arglist |= CAM_ARG_RETRIES; 2840 break; 2841 case 'E': 2842 arglist |= CAM_ARG_ERR_RECOVER; 2843 break; 2844 case 'n': 2845 arglist |= CAM_ARG_DEVICE; 2846 tstr = optarg; 2847 while (isspace(*tstr) && (*tstr != '\0')) 2848 tstr++; 2849 device = (char *)strdup(tstr); 2850 break; 2851 case 't': 2852 timeout = strtol(optarg, NULL, 0); 2853 if (timeout < 0) 2854 errx(1, "invalid timeout %d", timeout); 2855 /* Convert the timeout from seconds to ms */ 2856 timeout *= 1000; 2857 arglist |= CAM_ARG_TIMEOUT; 2858 break; 2859 case 'u': 2860 arglist |= CAM_ARG_UNIT; 2861 unit = strtol(optarg, NULL, 0); 2862 break; 2863 case 'v': 2864 arglist |= CAM_ARG_VERBOSE; 2865 break; 2866 default: 2867 break; 2868 } 2869 } 2870 2871 if ((arglist & CAM_ARG_DEVICE) == 0) 2872 device = (char *)strdup(DEFAULT_DEVICE); 2873 2874 if ((arglist & CAM_ARG_UNIT) == 0) 2875 unit = DEFAULT_UNIT; 2876 2877 /* 2878 * For most commands we'll want to open the passthrough device 2879 * associated with the specified device. In the case of the rescan 2880 * commands, we don't use a passthrough device at all, just the 2881 * transport layer device. 2882 */ 2883 if (devopen == 1) { 2884 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))? 2885 cam_open_btl(bus, target, lun, O_RDWR, NULL) : 2886 cam_open_spec_device(device,unit,O_RDWR,NULL))) 2887 == NULL) 2888 errx(1,"%s", cam_errbuf); 2889 } 2890 2891 /* 2892 * Reset optind to 2, and reset getopt, so these routines can parse 2893 * the arguments again. 2894 */ 2895 optind = optstart; 2896 optreset = 1; 2897 2898 switch(arglist & CAM_ARG_OPT_MASK) { 2899 case CAM_ARG_DEVLIST: 2900 error = getdevlist(cam_dev); 2901 break; 2902 case CAM_ARG_DEVTREE: 2903 error = getdevtree(); 2904 break; 2905 case CAM_ARG_TUR: 2906 error = testunitready(cam_dev, retry_count, timeout, 0); 2907 break; 2908 case CAM_ARG_INQUIRY: 2909 error = scsidoinquiry(cam_dev, argc, argv, combinedopt, 2910 retry_count, timeout); 2911 break; 2912 case CAM_ARG_STARTSTOP: 2913 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT, 2914 arglist & CAM_ARG_EJECT, retry_count, 2915 timeout); 2916 break; 2917 case CAM_ARG_RESCAN: 2918 error = dorescan_or_reset(argc, argv, 1); 2919 break; 2920 case CAM_ARG_RESET: 2921 error = dorescan_or_reset(argc, argv, 0); 2922 break; 2923 case CAM_ARG_READ_DEFECTS: 2924 error = readdefects(cam_dev, argc, argv, combinedopt, 2925 retry_count, timeout); 2926 break; 2927 case CAM_ARG_MODE_PAGE: 2928 modepage(cam_dev, argc, argv, combinedopt, 2929 retry_count, timeout); 2930 break; 2931 case CAM_ARG_SCSI_CMD: 2932 error = scsicmd(cam_dev, argc, argv, combinedopt, 2933 retry_count, timeout); 2934 break; 2935 case CAM_ARG_DEBUG: 2936 error = camdebug(argc, argv, combinedopt); 2937 break; 2938 case CAM_ARG_TAG: 2939 error = tagcontrol(cam_dev, argc, argv, combinedopt); 2940 break; 2941 case CAM_ARG_RATE: 2942 error = ratecontrol(cam_dev, retry_count, timeout, 2943 argc, argv, combinedopt); 2944 break; 2945 case CAM_ARG_USAGE: 2946 usage(1); 2947 break; 2948 default: 2949 usage(0); 2950 error = 1; 2951 break; 2952 } 2953 2954 if (cam_dev != NULL) 2955 cam_close_device(cam_dev); 2956 2957 exit(error); 2958 } 2959