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 * $Id: camcontrol.c,v 1.11 1999/05/10 23:30:01 ken Exp $ 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 char *tstr, *tmpstr = NULL; 962 963 if (argc < 3) { 964 warnx(must, rescan? "rescan" : "reset"); 965 return(1); 966 } 967 rv = parse_btl(argv[optind], &bus, &target, &lun, &arglist); 968 if (rv != 1 && rv != 3) { 969 warnx(must, rescan? "rescan" : "reset"); 970 return(1); 971 } 972 973 if ((arglist & CAM_ARG_BUS) 974 && (arglist & CAM_ARG_TARGET) 975 && (arglist & CAM_ARG_LUN)) 976 error = scanlun_or_reset_dev(bus, target, lun, rescan); 977 else 978 error = rescan_or_reset_bus(bus, rescan); 979 980 return(error); 981 } 982 983 static int 984 rescan_or_reset_bus(int bus, int rescan) 985 { 986 union ccb ccb; 987 int fd; 988 989 if (bus < 0) { 990 warnx("invalid bus number %d", bus); 991 return(1); 992 } 993 994 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 995 warnx("error opening tranport layer device %s", XPT_DEVICE); 996 warn("%s", XPT_DEVICE); 997 return(1); 998 } 999 1000 ccb.ccb_h.func_code = rescan? XPT_SCAN_BUS : XPT_RESET_BUS; 1001 ccb.ccb_h.path_id = bus; 1002 ccb.ccb_h.target_id = CAM_TARGET_WILDCARD; 1003 ccb.ccb_h.target_lun = CAM_LUN_WILDCARD; 1004 ccb.crcn.flags = CAM_FLAG_NONE; 1005 1006 /* run this at a low priority */ 1007 ccb.ccb_h.pinfo.priority = 5; 1008 1009 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1010 warn("CAMIOCOMMAND ioctl failed"); 1011 close(fd); 1012 return(1); 1013 } 1014 1015 close(fd); 1016 1017 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1018 fprintf(stdout, "%s of bus %d was successful\n", 1019 rescan? "Re-scan" : "Reset", bus); 1020 return(0); 1021 } else { 1022 fprintf(stdout, "%s of bus %d returned error %#x\n", 1023 rescan? "Re-scan" : "Reset", bus, 1024 ccb.ccb_h.status & CAM_STATUS_MASK); 1025 return(1); 1026 } 1027 } 1028 1029 static int 1030 scanlun_or_reset_dev(int bus, int target, int lun, int scan) 1031 { 1032 union ccb ccb; 1033 int fd; 1034 1035 if (bus < 0) { 1036 warnx("invalid bus number %d", bus); 1037 return(1); 1038 } 1039 1040 if (target < 0) { 1041 warnx("invalid target number %d", target); 1042 return(1); 1043 } 1044 1045 if (lun < 0) { 1046 warnx("invalid lun number %d", lun); 1047 return(1); 1048 } 1049 1050 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1051 warnx("error opening tranport layer device %s\n", 1052 XPT_DEVICE); 1053 warn("%s", XPT_DEVICE); 1054 return(1); 1055 } 1056 1057 ccb.ccb_h.func_code = (scan)? XPT_SCAN_LUN : XPT_RESET_DEV; 1058 ccb.ccb_h.path_id = bus; 1059 ccb.ccb_h.target_id = target; 1060 ccb.ccb_h.target_lun = lun; 1061 ccb.crcn.flags = CAM_FLAG_NONE; 1062 1063 /* run this at a low priority */ 1064 ccb.ccb_h.pinfo.priority = 5; 1065 1066 if (ioctl(fd, CAMIOCOMMAND, &ccb) < 0) { 1067 warn("CAMIOCOMMAND ioctl failed"); 1068 close(fd); 1069 return(1); 1070 } 1071 1072 close(fd); 1073 1074 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1075 fprintf(stdout, "%s of %d:%d:%d was successful\n", 1076 scan? "Re-scan" : "Reset", bus, target, lun); 1077 return(0); 1078 } else { 1079 fprintf(stdout, "%s of %d:%d:%d returned error %#x\n", 1080 scan? "Re-scan" : "Reset", bus, target, lun, 1081 ccb.ccb_h.status & CAM_STATUS_MASK); 1082 return(1); 1083 } 1084 } 1085 1086 static int 1087 readdefects(struct cam_device *device, int argc, char **argv, 1088 char *combinedopt, int retry_count, int timeout) 1089 { 1090 union ccb *ccb = NULL; 1091 struct scsi_read_defect_data_10 *rdd_cdb; 1092 u_int8_t *defect_list = NULL; 1093 u_int32_t dlist_length = 65000; 1094 u_int32_t returned_length = 0; 1095 u_int32_t num_returned = 0; 1096 u_int8_t returned_format; 1097 register int i; 1098 int c, error = 0; 1099 int lists_specified = 0; 1100 1101 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1102 switch(c){ 1103 case 'f': 1104 { 1105 char *tstr; 1106 tstr = optarg; 1107 while (isspace(*tstr) && (*tstr != '\0')) 1108 tstr++; 1109 if (strcmp(tstr, "block") == 0) 1110 arglist |= CAM_ARG_FORMAT_BLOCK; 1111 else if (strcmp(tstr, "bfi") == 0) 1112 arglist |= CAM_ARG_FORMAT_BFI; 1113 else if (strcmp(tstr, "phys") == 0) 1114 arglist |= CAM_ARG_FORMAT_PHYS; 1115 else { 1116 error = 1; 1117 warnx("invalid defect format %s", tstr); 1118 goto defect_bailout; 1119 } 1120 break; 1121 } 1122 case 'G': 1123 arglist |= CAM_ARG_GLIST; 1124 break; 1125 case 'P': 1126 arglist |= CAM_ARG_PLIST; 1127 break; 1128 default: 1129 break; 1130 } 1131 } 1132 1133 ccb = cam_getccb(device); 1134 1135 /* 1136 * Hopefully 65000 bytes is enough to hold the defect list. If it 1137 * isn't, the disk is probably dead already. We'd have to go with 1138 * 12 byte command (i.e. alloc_length is 32 bits instead of 16) 1139 * to hold them all. 1140 */ 1141 defect_list = malloc(dlist_length); 1142 1143 rdd_cdb =(struct scsi_read_defect_data_10 *)&ccb->csio.cdb_io.cdb_bytes; 1144 1145 /* 1146 * cam_getccb() zeros the CCB header only. So we need to zero the 1147 * payload portion of the ccb. 1148 */ 1149 bzero(&(&ccb->ccb_h)[1], 1150 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1151 1152 cam_fill_csio(&ccb->csio, 1153 /*retries*/ retry_count, 1154 /*cbfcnp*/ NULL, 1155 /*flags*/ CAM_DIR_IN | (arglist & CAM_ARG_ERR_RECOVER) ? 1156 CAM_PASS_ERR_RECOVER : 0, 1157 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1158 /*data_ptr*/ defect_list, 1159 /*dxfer_len*/ dlist_length, 1160 /*sense_len*/ SSD_FULL_SIZE, 1161 /*cdb_len*/ sizeof(struct scsi_read_defect_data_10), 1162 /*timeout*/ timeout ? timeout : 5000); 1163 1164 rdd_cdb->opcode = READ_DEFECT_DATA_10; 1165 if (arglist & CAM_ARG_FORMAT_BLOCK) 1166 rdd_cdb->format = SRDD10_BLOCK_FORMAT; 1167 else if (arglist & CAM_ARG_FORMAT_BFI) 1168 rdd_cdb->format = SRDD10_BYTES_FROM_INDEX_FORMAT; 1169 else if (arglist & CAM_ARG_FORMAT_PHYS) 1170 rdd_cdb->format = SRDD10_PHYSICAL_SECTOR_FORMAT; 1171 else { 1172 error = 1; 1173 warnx("no defect list format specified"); 1174 goto defect_bailout; 1175 } 1176 if (arglist & CAM_ARG_PLIST) { 1177 rdd_cdb->format |= SRDD10_PLIST; 1178 lists_specified++; 1179 } 1180 1181 if (arglist & CAM_ARG_GLIST) { 1182 rdd_cdb->format |= SRDD10_GLIST; 1183 lists_specified++; 1184 } 1185 1186 scsi_ulto2b(dlist_length, rdd_cdb->alloc_length); 1187 1188 /* Disable freezing the device queue */ 1189 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1190 1191 if (cam_send_ccb(device, ccb) < 0) { 1192 perror("error reading defect list"); 1193 1194 if (arglist & CAM_ARG_VERBOSE) { 1195 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1196 CAM_SCSI_STATUS_ERROR) 1197 scsi_sense_print(device, &ccb->csio, stderr); 1198 else 1199 fprintf(stderr, "CAM status is %#x\n", 1200 ccb->ccb_h.status); 1201 } 1202 1203 error = 1; 1204 goto defect_bailout; 1205 } 1206 1207 if (arglist & CAM_ARG_VERBOSE) 1208 scsi_sense_print(device, &ccb->csio, stderr); 1209 1210 returned_length = scsi_2btoul(((struct 1211 scsi_read_defect_data_hdr_10 *)defect_list)->length); 1212 1213 returned_format = ((struct scsi_read_defect_data_hdr_10 *) 1214 defect_list)->format; 1215 1216 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1217 struct scsi_sense_data *sense; 1218 int error_code, sense_key, asc, ascq; 1219 1220 sense = &ccb->csio.sense_data; 1221 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq); 1222 1223 /* 1224 * According to the SCSI spec, if the disk doesn't support 1225 * the requested format, it will generally return a sense 1226 * key of RECOVERED ERROR, and an additional sense code 1227 * of "DEFECT LIST NOT FOUND". So, we check for that, and 1228 * also check to make sure that the returned length is 1229 * greater than 0, and then print out whatever format the 1230 * disk gave us. 1231 */ 1232 if ((sense_key == SSD_KEY_RECOVERED_ERROR) 1233 && (asc == 0x1c) && (ascq == 0x00) 1234 && (returned_length > 0)) { 1235 warnx("requested defect format not available"); 1236 switch(returned_format & SRDDH10_DLIST_FORMAT_MASK) { 1237 case SRDD10_BLOCK_FORMAT: 1238 warnx("Device returned block format"); 1239 break; 1240 case SRDD10_BYTES_FROM_INDEX_FORMAT: 1241 warnx("Device returned bytes from index" 1242 " format"); 1243 break; 1244 case SRDD10_PHYSICAL_SECTOR_FORMAT: 1245 warnx("Device returned physical sector format"); 1246 break; 1247 default: 1248 error = 1; 1249 warnx("Device returned unknown defect" 1250 " data format %#x", returned_format); 1251 goto defect_bailout; 1252 break; /* NOTREACHED */ 1253 } 1254 } else { 1255 error = 1; 1256 warnx("Error returned from read defect data command"); 1257 goto defect_bailout; 1258 } 1259 } 1260 1261 /* 1262 * XXX KDM I should probably clean up the printout format for the 1263 * disk defects. 1264 */ 1265 switch (returned_format & SRDDH10_DLIST_FORMAT_MASK){ 1266 case SRDDH10_PHYSICAL_SECTOR_FORMAT: 1267 { 1268 struct scsi_defect_desc_phys_sector *dlist; 1269 1270 dlist = (struct scsi_defect_desc_phys_sector *) 1271 (defect_list + 1272 sizeof(struct scsi_read_defect_data_hdr_10)); 1273 1274 num_returned = returned_length / 1275 sizeof(struct scsi_defect_desc_phys_sector); 1276 1277 fprintf(stderr, "Got %d defect", num_returned); 1278 1279 if ((lists_specified == 0) || (num_returned == 0)) { 1280 fprintf(stderr, "s.\n"); 1281 break; 1282 } else if (num_returned == 1) 1283 fprintf(stderr, ":\n"); 1284 else 1285 fprintf(stderr, "s:\n"); 1286 1287 for (i = 0; i < num_returned; i++) { 1288 fprintf(stdout, "%d:%d:%d\n", 1289 scsi_3btoul(dlist[i].cylinder), 1290 dlist[i].head, 1291 scsi_4btoul(dlist[i].sector)); 1292 } 1293 break; 1294 } 1295 case SRDDH10_BYTES_FROM_INDEX_FORMAT: 1296 { 1297 struct scsi_defect_desc_bytes_from_index *dlist; 1298 1299 dlist = (struct scsi_defect_desc_bytes_from_index *) 1300 (defect_list + 1301 sizeof(struct scsi_read_defect_data_hdr_10)); 1302 1303 num_returned = returned_length / 1304 sizeof(struct scsi_defect_desc_bytes_from_index); 1305 1306 fprintf(stderr, "Got %d defect", num_returned); 1307 1308 if ((lists_specified == 0) || (num_returned == 0)) { 1309 fprintf(stderr, "s.\n"); 1310 break; 1311 } else if (num_returned == 1) 1312 fprintf(stderr, ":\n"); 1313 else 1314 fprintf(stderr, "s:\n"); 1315 1316 for (i = 0; i < num_returned; i++) { 1317 fprintf(stdout, "%d:%d:%d\n", 1318 scsi_3btoul(dlist[i].cylinder), 1319 dlist[i].head, 1320 scsi_4btoul(dlist[i].bytes_from_index)); 1321 } 1322 break; 1323 } 1324 case SRDDH10_BLOCK_FORMAT: 1325 { 1326 struct scsi_defect_desc_block *dlist; 1327 1328 dlist = (struct scsi_defect_desc_block *)(defect_list + 1329 sizeof(struct scsi_read_defect_data_hdr_10)); 1330 1331 num_returned = returned_length / 1332 sizeof(struct scsi_defect_desc_block); 1333 1334 fprintf(stderr, "Got %d defect", num_returned); 1335 1336 if ((lists_specified == 0) || (num_returned == 0)) { 1337 fprintf(stderr, "s.\n"); 1338 break; 1339 } else if (num_returned == 1) 1340 fprintf(stderr, ":\n"); 1341 else 1342 fprintf(stderr, "s:\n"); 1343 1344 for (i = 0; i < num_returned; i++) 1345 fprintf(stdout, "%u\n", 1346 scsi_4btoul(dlist[i].address)); 1347 break; 1348 } 1349 default: 1350 fprintf(stderr, "Unknown defect format %d\n", 1351 returned_format & SRDDH10_DLIST_FORMAT_MASK); 1352 error = 1; 1353 break; 1354 } 1355 defect_bailout: 1356 1357 if (defect_list != NULL) 1358 free(defect_list); 1359 1360 if (ccb != NULL) 1361 cam_freeccb(ccb); 1362 1363 return(error); 1364 } 1365 1366 #if 0 1367 void 1368 reassignblocks(struct cam_device *device, u_int32_t *blocks, int num_blocks) 1369 { 1370 union ccb *ccb; 1371 1372 ccb = cam_getccb(device); 1373 1374 cam_freeccb(ccb); 1375 } 1376 #endif 1377 1378 void 1379 mode_sense(struct cam_device *device, int mode_page, int page_control, 1380 int dbd, int retry_count, int timeout, u_int8_t *data, int datalen) 1381 { 1382 union ccb *ccb; 1383 int retval; 1384 1385 ccb = cam_getccb(device); 1386 1387 if (ccb == NULL) 1388 errx(1, "mode_sense: couldn't allocate CCB"); 1389 1390 bzero(&(&ccb->ccb_h)[1], 1391 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1392 1393 scsi_mode_sense(&ccb->csio, 1394 /* retries */ retry_count, 1395 /* cbfcnp */ NULL, 1396 /* tag_action */ MSG_SIMPLE_Q_TAG, 1397 /* dbd */ dbd, 1398 /* page_code */ page_control << 6, 1399 /* page */ mode_page, 1400 /* param_buf */ data, 1401 /* param_len */ datalen, 1402 /* sense_len */ SSD_FULL_SIZE, 1403 /* timeout */ timeout ? timeout : 5000); 1404 1405 if (arglist & CAM_ARG_ERR_RECOVER) 1406 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1407 1408 /* Disable freezing the device queue */ 1409 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1410 1411 if (((retval = cam_send_ccb(device, ccb)) < 0) 1412 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1413 if (arglist & CAM_ARG_VERBOSE) { 1414 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1415 CAM_SCSI_STATUS_ERROR) 1416 scsi_sense_print(device, &ccb->csio, stderr); 1417 else 1418 fprintf(stderr, "CAM status is %#x\n", 1419 ccb->ccb_h.status); 1420 } 1421 cam_freeccb(ccb); 1422 cam_close_device(device); 1423 if (retval < 0) 1424 err(1, "error sending mode sense command"); 1425 else 1426 errx(1, "error sending mode sense command"); 1427 } 1428 1429 cam_freeccb(ccb); 1430 } 1431 1432 void 1433 mode_select(struct cam_device *device, int save_pages, int retry_count, 1434 int timeout, u_int8_t *data, int datalen) 1435 { 1436 union ccb *ccb; 1437 int retval; 1438 1439 ccb = cam_getccb(device); 1440 1441 if (ccb == NULL) 1442 errx(1, "mode_select: couldn't allocate CCB"); 1443 1444 bzero(&(&ccb->ccb_h)[1], 1445 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1446 1447 scsi_mode_select(&ccb->csio, 1448 /* retries */ retry_count, 1449 /* cbfcnp */ NULL, 1450 /* tag_action */ MSG_SIMPLE_Q_TAG, 1451 /* scsi_page_fmt */ 1, 1452 /* save_pages */ save_pages, 1453 /* param_buf */ data, 1454 /* param_len */ datalen, 1455 /* sense_len */ SSD_FULL_SIZE, 1456 /* timeout */ timeout ? timeout : 5000); 1457 1458 if (arglist & CAM_ARG_ERR_RECOVER) 1459 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; 1460 1461 /* Disable freezing the device queue */ 1462 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; 1463 1464 if (((retval = cam_send_ccb(device, ccb)) < 0) 1465 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1466 if (arglist & CAM_ARG_VERBOSE) { 1467 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1468 CAM_SCSI_STATUS_ERROR) 1469 scsi_sense_print(device, &ccb->csio, stderr); 1470 else 1471 fprintf(stderr, "CAM status is %#x\n", 1472 ccb->ccb_h.status); 1473 } 1474 cam_freeccb(ccb); 1475 cam_close_device(device); 1476 1477 if (retval < 0) 1478 err(1, "error sending mode select command"); 1479 else 1480 errx(1, "error sending mode select command"); 1481 1482 } 1483 1484 cam_freeccb(ccb); 1485 } 1486 1487 void 1488 modepage(struct cam_device *device, int argc, char **argv, char *combinedopt, 1489 int retry_count, int timeout) 1490 { 1491 int c, mode_page = -1, page_control = 0; 1492 1493 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1494 switch(c) { 1495 case 'd': 1496 arglist |= CAM_ARG_DBD; 1497 break; 1498 case 'e': 1499 arglist |= CAM_ARG_MODE_EDIT; 1500 break; 1501 case 'm': 1502 mode_page = strtol(optarg, NULL, 0); 1503 if (mode_page < 0) 1504 errx(1, "invalid mode page %d", mode_page); 1505 break; 1506 case 'P': 1507 page_control = strtol(optarg, NULL, 0); 1508 if ((page_control < 0) || (page_control > 3)) 1509 errx(1, "invalid page control field %d", 1510 page_control); 1511 arglist |= CAM_ARG_PAGE_CNTL; 1512 break; 1513 default: 1514 break; 1515 } 1516 } 1517 1518 if (mode_page == -1) 1519 errx(1, "you must specify a mode page!"); 1520 1521 mode_edit(device, mode_page, page_control, arglist & CAM_ARG_DBD, 1522 arglist & CAM_ARG_MODE_EDIT, retry_count, timeout); 1523 } 1524 1525 static int 1526 scsicmd(struct cam_device *device, int argc, char **argv, char *combinedopt, 1527 int retry_count, int timeout) 1528 { 1529 union ccb *ccb; 1530 u_int32_t flags = CAM_DIR_NONE; 1531 u_int8_t *data_ptr = NULL; 1532 u_int8_t cdb[20]; 1533 struct get_hook hook; 1534 int c, data_bytes = 0; 1535 int cdb_len = 0; 1536 char *datastr = NULL, *tstr; 1537 int error = 0; 1538 int fd_data = 0; 1539 int retval; 1540 1541 ccb = cam_getccb(device); 1542 1543 if (ccb == NULL) { 1544 warnx("scsicmd: error allocating ccb"); 1545 return(1); 1546 } 1547 1548 bzero(&(&ccb->ccb_h)[1], 1549 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr)); 1550 1551 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1552 switch(c) { 1553 case 'c': 1554 tstr = optarg; 1555 while (isspace(*tstr) && (*tstr != '\0')) 1556 tstr++; 1557 hook.argc = argc - optind; 1558 hook.argv = argv + optind; 1559 hook.got = 0; 1560 cdb_len = buff_encode_visit(cdb, sizeof(cdb), tstr, 1561 iget, &hook); 1562 /* 1563 * Increment optind by the number of arguments the 1564 * encoding routine processed. After each call to 1565 * getopt(3), optind points to the argument that 1566 * getopt should process _next_. In this case, 1567 * that means it points to the first command string 1568 * argument, if there is one. Once we increment 1569 * this, it should point to either the next command 1570 * line argument, or it should be past the end of 1571 * the list. 1572 */ 1573 optind += hook.got; 1574 break; 1575 case 'i': 1576 if (arglist & CAM_ARG_CMD_OUT) { 1577 warnx("command must either be " 1578 "read or write, not both"); 1579 error = 1; 1580 goto scsicmd_bailout; 1581 } 1582 arglist |= CAM_ARG_CMD_IN; 1583 flags = CAM_DIR_IN; 1584 data_bytes = strtol(optarg, NULL, 0); 1585 if (data_bytes <= 0) { 1586 warnx("invalid number of input bytes %d", 1587 data_bytes); 1588 error = 1; 1589 goto scsicmd_bailout; 1590 } 1591 hook.argc = argc - optind; 1592 hook.argv = argv + optind; 1593 hook.got = 0; 1594 optind++; 1595 datastr = cget(&hook, NULL); 1596 /* 1597 * If the user supplied "-" instead of a format, he 1598 * wants the data to be written to stdout. 1599 */ 1600 if ((datastr != NULL) 1601 && (datastr[0] == '-')) 1602 fd_data = 1; 1603 1604 data_ptr = (u_int8_t *)malloc(data_bytes); 1605 break; 1606 case 'o': 1607 if (arglist & CAM_ARG_CMD_IN) { 1608 warnx("command must either be " 1609 "read or write, not both"); 1610 error = 1; 1611 goto scsicmd_bailout; 1612 } 1613 arglist |= CAM_ARG_CMD_OUT; 1614 flags = CAM_DIR_OUT; 1615 data_bytes = strtol(optarg, NULL, 0); 1616 if (data_bytes <= 0) { 1617 warnx("invalid number of output bytes %d", 1618 data_bytes); 1619 error = 1; 1620 goto scsicmd_bailout; 1621 } 1622 hook.argc = argc - optind; 1623 hook.argv = argv + optind; 1624 hook.got = 0; 1625 datastr = cget(&hook, NULL); 1626 data_ptr = (u_int8_t *)malloc(data_bytes); 1627 /* 1628 * If the user supplied "-" instead of a format, he 1629 * wants the data to be read from stdin. 1630 */ 1631 if ((datastr != NULL) 1632 && (datastr[0] == '-')) 1633 fd_data = 1; 1634 else 1635 buff_encode_visit(data_ptr, data_bytes, datastr, 1636 iget, &hook); 1637 optind += hook.got; 1638 break; 1639 default: 1640 break; 1641 } 1642 } 1643 1644 /* 1645 * If fd_data is set, and we're writing to the device, we need to 1646 * read the data the user wants written from stdin. 1647 */ 1648 if ((fd_data == 1) && (arglist & CAM_ARG_CMD_OUT)) { 1649 size_t amt_read; 1650 int amt_to_read = data_bytes; 1651 u_int8_t *buf_ptr = data_ptr; 1652 1653 for (amt_read = 0; amt_to_read > 0; 1654 amt_read = read(0, buf_ptr, amt_to_read)) { 1655 if (amt_read == -1) { 1656 warn("error reading data from stdin"); 1657 error = 1; 1658 goto scsicmd_bailout; 1659 } 1660 amt_to_read -= amt_read; 1661 buf_ptr += amt_read; 1662 } 1663 } 1664 1665 if (arglist & CAM_ARG_ERR_RECOVER) 1666 flags |= CAM_PASS_ERR_RECOVER; 1667 1668 /* Disable freezing the device queue */ 1669 flags |= CAM_DEV_QFRZDIS; 1670 1671 /* 1672 * This is taken from the SCSI-3 draft spec. 1673 * (T10/1157D revision 0.3) 1674 * The top 3 bits of an opcode are the group code. The next 5 bits 1675 * are the command code. 1676 * Group 0: six byte commands 1677 * Group 1: ten byte commands 1678 * Group 2: ten byte commands 1679 * Group 3: reserved 1680 * Group 4: sixteen byte commands 1681 * Group 5: twelve byte commands 1682 * Group 6: vendor specific 1683 * Group 7: vendor specific 1684 */ 1685 switch((cdb[0] >> 5) & 0x7) { 1686 case 0: 1687 cdb_len = 6; 1688 break; 1689 case 1: 1690 case 2: 1691 cdb_len = 10; 1692 break; 1693 case 3: 1694 case 6: 1695 case 7: 1696 /* computed by buff_encode_visit */ 1697 break; 1698 case 4: 1699 cdb_len = 16; 1700 break; 1701 case 5: 1702 cdb_len = 12; 1703 break; 1704 } 1705 1706 /* 1707 * We should probably use csio_build_visit or something like that 1708 * here, but it's easier to encode arguments as you go. The 1709 * alternative would be skipping the CDB argument and then encoding 1710 * it here, since we've got the data buffer argument by now. 1711 */ 1712 bcopy(cdb, &ccb->csio.cdb_io.cdb_bytes, cdb_len); 1713 1714 cam_fill_csio(&ccb->csio, 1715 /*retries*/ retry_count, 1716 /*cbfcnp*/ NULL, 1717 /*flags*/ flags, 1718 /*tag_action*/ MSG_SIMPLE_Q_TAG, 1719 /*data_ptr*/ data_ptr, 1720 /*dxfer_len*/ data_bytes, 1721 /*sense_len*/ SSD_FULL_SIZE, 1722 /*cdb_len*/ cdb_len, 1723 /*timeout*/ timeout ? timeout : 5000); 1724 1725 if (((retval = cam_send_ccb(device, ccb)) < 0) 1726 || ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)) { 1727 if (retval < 0) 1728 warn("error sending command"); 1729 else 1730 warnx("error sending command"); 1731 1732 if (arglist & CAM_ARG_VERBOSE) { 1733 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == 1734 CAM_SCSI_STATUS_ERROR) 1735 scsi_sense_print(device, &ccb->csio, stderr); 1736 else 1737 fprintf(stderr, "CAM status is %#x\n", 1738 ccb->ccb_h.status); 1739 } 1740 1741 error = 1; 1742 goto scsicmd_bailout; 1743 } 1744 1745 1746 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1747 && (arglist & CAM_ARG_CMD_IN) 1748 && (data_bytes > 0)) { 1749 if (fd_data == 0) { 1750 buff_decode_visit(data_ptr, data_bytes, datastr, 1751 arg_put, NULL); 1752 fprintf(stdout, "\n"); 1753 } else { 1754 size_t amt_written; 1755 int amt_to_write = data_bytes; 1756 u_int8_t *buf_ptr = data_ptr; 1757 1758 for (amt_written = 0; (amt_to_write > 0) && 1759 (amt_written =write(1, buf_ptr,amt_to_write))> 0;){ 1760 amt_to_write -= amt_written; 1761 buf_ptr += amt_written; 1762 } 1763 if (amt_written == -1) { 1764 warn("error writing data to stdout"); 1765 error = 1; 1766 goto scsicmd_bailout; 1767 } else if ((amt_written == 0) 1768 && (amt_to_write > 0)) { 1769 warnx("only wrote %u bytes out of %u", 1770 data_bytes - amt_to_write, data_bytes); 1771 } 1772 } 1773 } 1774 1775 scsicmd_bailout: 1776 1777 if ((data_bytes > 0) && (data_ptr != NULL)) 1778 free(data_ptr); 1779 1780 cam_freeccb(ccb); 1781 1782 return(error); 1783 } 1784 1785 static int 1786 camdebug(int argc, char **argv, char *combinedopt) 1787 { 1788 int c, fd; 1789 int bus = -1, target = -1, lun = -1; 1790 char *tstr, *tmpstr = NULL; 1791 union ccb ccb; 1792 int error = 0; 1793 1794 bzero(&ccb, sizeof(union ccb)); 1795 1796 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1797 switch(c) { 1798 case 'I': 1799 arglist |= CAM_ARG_DEBUG_INFO; 1800 ccb.cdbg.flags |= CAM_DEBUG_INFO; 1801 break; 1802 case 'S': 1803 arglist |= CAM_ARG_DEBUG_TRACE; 1804 ccb.cdbg.flags |= CAM_DEBUG_TRACE; 1805 break; 1806 case 'T': 1807 arglist |= CAM_ARG_DEBUG_SUBTRACE; 1808 ccb.cdbg.flags |= CAM_DEBUG_SUBTRACE; 1809 break; 1810 case 'c': 1811 arglist |= CAM_ARG_DEBUG_CDB; 1812 ccb.cdbg.flags |= CAM_DEBUG_CDB; 1813 break; 1814 default: 1815 break; 1816 } 1817 } 1818 1819 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 1820 warnx("error opening transport layer device %s", XPT_DEVICE); 1821 warn("%s", XPT_DEVICE); 1822 return(1); 1823 } 1824 argc -= optind; 1825 argv += optind; 1826 1827 if (argc <= 0) { 1828 warnx("you must specify \"off\", \"all\" or a bus,"); 1829 warnx("bus:target, or bus:target:lun"); 1830 close(fd); 1831 return(1); 1832 } 1833 1834 tstr = *argv; 1835 1836 while (isspace(*tstr) && (*tstr != '\0')) 1837 tstr++; 1838 1839 if (strncmp(tstr, "off", 3) == 0) { 1840 ccb.cdbg.flags = CAM_DEBUG_NONE; 1841 arglist &= ~(CAM_ARG_DEBUG_INFO|CAM_ARG_DEBUG_TRACE| 1842 CAM_ARG_DEBUG_SUBTRACE); 1843 } else if (strncmp(tstr, "all", 3) != 0) { 1844 tmpstr = (char *)strtok(tstr, ":"); 1845 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1846 bus = strtol(tmpstr, NULL, 0); 1847 arglist |= CAM_ARG_BUS; 1848 tmpstr = (char *)strtok(NULL, ":"); 1849 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1850 target = strtol(tmpstr, NULL, 0); 1851 arglist |= CAM_ARG_TARGET; 1852 tmpstr = (char *)strtok(NULL, ":"); 1853 if ((tmpstr != NULL) && (*tmpstr != '\0')){ 1854 lun = strtol(tmpstr, NULL, 0); 1855 arglist |= CAM_ARG_LUN; 1856 } 1857 } 1858 } else { 1859 error = 1; 1860 warnx("you must specify \"all\", \"off\", or a bus,"); 1861 warnx("bus:target, or bus:target:lun to debug"); 1862 } 1863 } 1864 1865 if (error == 0) { 1866 1867 ccb.ccb_h.func_code = XPT_DEBUG; 1868 ccb.ccb_h.path_id = bus; 1869 ccb.ccb_h.target_id = target; 1870 ccb.ccb_h.target_lun = lun; 1871 1872 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 1873 warn("CAMIOCOMMAND ioctl failed"); 1874 error = 1; 1875 } 1876 1877 if (error == 0) { 1878 if ((ccb.ccb_h.status & CAM_STATUS_MASK) == 1879 CAM_FUNC_NOTAVAIL) { 1880 warnx("CAM debugging not available"); 1881 warnx("you need to put options CAMDEBUG in" 1882 " your kernel config file!"); 1883 error = 1; 1884 } else if ((ccb.ccb_h.status & CAM_STATUS_MASK) != 1885 CAM_REQ_CMP) { 1886 warnx("XPT_DEBUG CCB failed with status %#x", 1887 ccb.ccb_h.status); 1888 error = 1; 1889 } else { 1890 if (ccb.cdbg.flags == CAM_DEBUG_NONE) { 1891 fprintf(stderr, 1892 "Debugging turned off\n"); 1893 } else { 1894 fprintf(stderr, 1895 "Debugging enabled for " 1896 "%d:%d:%d\n", 1897 bus, target, lun); 1898 } 1899 } 1900 } 1901 close(fd); 1902 } 1903 1904 return(error); 1905 } 1906 1907 static int 1908 tagcontrol(struct cam_device *device, int argc, char **argv, 1909 char *combinedopt) 1910 { 1911 int c; 1912 union ccb *ccb; 1913 int numtags = -1; 1914 int retval = 0; 1915 int quiet = 0; 1916 char pathstr[1024]; 1917 1918 ccb = cam_getccb(device); 1919 1920 if (ccb == NULL) { 1921 warnx("tagcontrol: error allocating ccb"); 1922 return(1); 1923 } 1924 1925 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1926 switch(c) { 1927 case 'N': 1928 numtags = strtol(optarg, NULL, 0); 1929 if (numtags < 0) { 1930 warnx("tag count %d is < 0", numtags); 1931 retval = 1; 1932 goto tagcontrol_bailout; 1933 } 1934 break; 1935 case 'q': 1936 quiet++; 1937 break; 1938 default: 1939 break; 1940 } 1941 } 1942 1943 cam_path_string(device, pathstr, sizeof(pathstr)); 1944 1945 if (numtags >= 0) { 1946 bzero(&(&ccb->ccb_h)[1], 1947 sizeof(struct ccb_relsim) - sizeof(struct ccb_hdr)); 1948 ccb->ccb_h.func_code = XPT_REL_SIMQ; 1949 ccb->crs.release_flags = RELSIM_ADJUST_OPENINGS; 1950 ccb->crs.openings = numtags; 1951 1952 1953 if (cam_send_ccb(device, ccb) < 0) { 1954 perror("error sending XPT_REL_SIMQ CCB"); 1955 retval = 1; 1956 goto tagcontrol_bailout; 1957 } 1958 1959 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1960 warnx("XPT_REL_SIMQ CCB failed, status %#x", 1961 ccb->ccb_h.status); 1962 retval = 1; 1963 goto tagcontrol_bailout; 1964 } 1965 1966 1967 if (quiet == 0) 1968 fprintf(stdout, "%stagged openings now %d\n", 1969 pathstr, ccb->crs.openings); 1970 } 1971 1972 bzero(&(&ccb->ccb_h)[1], 1973 sizeof(struct ccb_getdev) - sizeof(struct ccb_hdr)); 1974 1975 ccb->ccb_h.func_code = XPT_GDEV_TYPE; 1976 1977 if (cam_send_ccb(device, ccb) < 0) { 1978 perror("error sending XPT_GDEV_TYPE CCB"); 1979 retval = 1; 1980 goto tagcontrol_bailout; 1981 } 1982 1983 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1984 warnx("XPT_GDEV_TYPE CCB failed, status %#x", 1985 ccb->ccb_h.status); 1986 retval = 1; 1987 goto tagcontrol_bailout; 1988 } 1989 1990 if (arglist & CAM_ARG_VERBOSE) { 1991 fprintf(stdout, "%s", pathstr); 1992 fprintf(stdout, "dev_openings %d\n", ccb->cgd.dev_openings); 1993 fprintf(stdout, "%s", pathstr); 1994 fprintf(stdout, "dev_active %d\n", ccb->cgd.dev_active); 1995 fprintf(stdout, "%s", pathstr); 1996 fprintf(stdout, "devq_openings %d\n", ccb->cgd.devq_openings); 1997 fprintf(stdout, "%s", pathstr); 1998 fprintf(stdout, "devq_queued %d\n", ccb->cgd.devq_queued); 1999 fprintf(stdout, "%s", pathstr); 2000 fprintf(stdout, "held %d\n", ccb->cgd.held); 2001 fprintf(stdout, "%s", pathstr); 2002 fprintf(stdout, "mintags %d\n", ccb->cgd.mintags); 2003 fprintf(stdout, "%s", pathstr); 2004 fprintf(stdout, "maxtags %d\n", ccb->cgd.maxtags); 2005 } else { 2006 if (quiet == 0) { 2007 fprintf(stdout, "%s", pathstr); 2008 fprintf(stdout, "device openings: "); 2009 } 2010 fprintf(stdout, "%d\n", ccb->cgd.dev_openings + 2011 ccb->cgd.dev_active); 2012 } 2013 2014 tagcontrol_bailout: 2015 2016 cam_freeccb(ccb); 2017 return(retval); 2018 } 2019 2020 static void 2021 cts_print(struct cam_device *device, struct ccb_trans_settings *cts) 2022 { 2023 char pathstr[1024]; 2024 2025 cam_path_string(device, pathstr, sizeof(pathstr)); 2026 2027 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) { 2028 2029 fprintf(stdout, "%ssync parameter: %d\n", pathstr, 2030 cts->sync_period); 2031 2032 if (cts->sync_offset != 0) { 2033 u_int freq; 2034 u_int speed; 2035 2036 freq = scsi_calc_syncsrate(cts->sync_period); 2037 fprintf(stdout, "%sfrequencey: %d.%03dMHz\n", pathstr, 2038 freq / 1000, freq % 1000); 2039 } 2040 } 2041 2042 if (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) 2043 fprintf(stdout, "%soffset: %d\n", pathstr, cts->sync_offset); 2044 2045 if (cts->valid & CCB_TRANS_BUS_WIDTH_VALID) 2046 fprintf(stdout, "%sbus width: %d bits\n", pathstr, 2047 (0x01 << cts->bus_width) * 8); 2048 2049 if (cts->valid & CCB_TRANS_DISC_VALID) 2050 fprintf(stdout, "%sdisconnection is %s\n", pathstr, 2051 (cts->flags & CCB_TRANS_DISC_ENB) ? "enabled" : 2052 "disabled"); 2053 2054 if (cts->valid & CCB_TRANS_TQ_VALID) 2055 fprintf(stdout, "%stagged queueing is %s\n", pathstr, 2056 (cts->flags & CCB_TRANS_TAG_ENB) ? "enabled" : 2057 "disabled"); 2058 2059 } 2060 2061 /* 2062 * Get a path inquiry CCB for the specified device. 2063 */ 2064 static int 2065 get_cpi(struct cam_device *device, struct ccb_pathinq *cpi) 2066 { 2067 union ccb *ccb; 2068 int retval = 0; 2069 2070 ccb = cam_getccb(device); 2071 2072 if (ccb == NULL) { 2073 warnx("get_cpi: couldn't allocate CCB"); 2074 return(1); 2075 } 2076 2077 bzero(&(&ccb->ccb_h)[1], 2078 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2079 2080 ccb->ccb_h.func_code = XPT_PATH_INQ; 2081 2082 if (cam_send_ccb(device, ccb) < 0) { 2083 warn("get_cpi: error sending Path Inquiry CCB"); 2084 2085 if (arglist & CAM_ARG_VERBOSE) 2086 fprintf(stderr, "CAM status is %#x\n", 2087 ccb->ccb_h.status); 2088 2089 retval = 1; 2090 2091 goto get_cpi_bailout; 2092 } 2093 2094 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2095 2096 if (arglist & CAM_ARG_VERBOSE) 2097 fprintf(stderr, "get_cpi: CAM status is %#x\n", 2098 ccb->ccb_h.status); 2099 2100 retval = 1; 2101 2102 goto get_cpi_bailout; 2103 } 2104 2105 bcopy(&ccb->cpi, cpi, sizeof(struct ccb_pathinq)); 2106 2107 get_cpi_bailout: 2108 2109 cam_freeccb(ccb); 2110 2111 return(retval); 2112 } 2113 2114 static void 2115 cpi_print(struct ccb_pathinq *cpi) 2116 { 2117 char adapter_str[1024]; 2118 int i; 2119 2120 snprintf(adapter_str, sizeof(adapter_str), 2121 "%s%d:", cpi->dev_name, cpi->unit_number); 2122 2123 fprintf(stdout, "%s SIM/HBA version: %d\n", adapter_str, 2124 cpi->version_num); 2125 2126 for (i = 1; i < 0xff; i = i << 1) { 2127 char *str; 2128 2129 if ((i & cpi->hba_inquiry) == 0) 2130 continue; 2131 2132 fprintf(stdout, "%s supports ", adapter_str); 2133 2134 switch(i) { 2135 case PI_MDP_ABLE: 2136 str = "MDP message"; 2137 break; 2138 case PI_WIDE_32: 2139 str = "32 bit wide SCSI"; 2140 break; 2141 case PI_WIDE_16: 2142 str = "16 bit wide SCSI"; 2143 break; 2144 case PI_SDTR_ABLE: 2145 str = "SDTR message"; 2146 break; 2147 case PI_LINKED_CDB: 2148 str = "linked CDBs"; 2149 break; 2150 case PI_TAG_ABLE: 2151 str = "tag queue messages"; 2152 break; 2153 case PI_SOFT_RST: 2154 str = "soft reset alternative"; 2155 break; 2156 } 2157 fprintf(stdout, "%s\n", str); 2158 } 2159 2160 for (i = 1; i < 0xff; i = i << 1) { 2161 char *str; 2162 2163 if ((i & cpi->hba_misc) == 0) 2164 continue; 2165 2166 fprintf(stdout, "%s ", adapter_str); 2167 2168 switch(i) { 2169 case PIM_SCANHILO: 2170 str = "bus scans from high ID to low ID"; 2171 break; 2172 case PIM_NOREMOVE: 2173 str = "removable devices not included in scan"; 2174 break; 2175 case PIM_NOINITIATOR: 2176 str = "initiator role not supported"; 2177 break; 2178 case PIM_NOBUSRESET: 2179 str = "user has disabled initial BUS RESET or" 2180 " controller is in target/mixed mode"; 2181 break; 2182 } 2183 fprintf(stdout, "%s\n", str); 2184 } 2185 2186 for (i = 1; i < 0xff; i = i << 1) { 2187 char *str; 2188 2189 if ((i & cpi->target_sprt) == 0) 2190 continue; 2191 2192 fprintf(stdout, "%s supports ", adapter_str); 2193 switch(i) { 2194 case PIT_PROCESSOR: 2195 str = "target mode processor mode"; 2196 break; 2197 case PIT_PHASE: 2198 str = "target mode phase cog. mode"; 2199 break; 2200 case PIT_DISCONNECT: 2201 str = "disconnects in target mode"; 2202 break; 2203 case PIT_TERM_IO: 2204 str = "terminate I/O message in target mode"; 2205 break; 2206 case PIT_GRP_6: 2207 str = "group 6 commands in target mode"; 2208 break; 2209 case PIT_GRP_7: 2210 str = "group 7 commands in target mode"; 2211 break; 2212 } 2213 2214 fprintf(stdout, "%s\n", str); 2215 } 2216 fprintf(stdout, "%s HBA engine count: %d\n", adapter_str, 2217 cpi->hba_eng_cnt); 2218 fprintf(stdout, "%s maxium target: %d\n", adapter_str, 2219 cpi->max_target); 2220 fprintf(stdout, "%s maxium LUN: %d\n", adapter_str, 2221 cpi->max_lun); 2222 fprintf(stdout, "%s highest path ID in subsystem: %d\n", 2223 adapter_str, cpi->hpath_id); 2224 fprintf(stdout, "%s SIM vendor: %s\n", adapter_str, cpi->sim_vid); 2225 fprintf(stdout, "%s HBA vendor: %s\n", adapter_str, cpi->hba_vid); 2226 fprintf(stdout, "%s bus ID: %d\n", adapter_str, cpi->bus_id); 2227 fprintf(stdout, "%s base transfer speed: ", adapter_str); 2228 if (cpi->base_transfer_speed > 1000) 2229 fprintf(stdout, "%d.%03dMB/sec\n", 2230 cpi->base_transfer_speed / 1000, 2231 cpi->base_transfer_speed % 1000); 2232 else 2233 fprintf(stdout, "%dKB/sec\n", 2234 (cpi->base_transfer_speed % 1000) * 1000); 2235 } 2236 2237 static int 2238 get_print_cts(struct cam_device *device, int user_settings, int quiet, 2239 struct ccb_trans_settings *cts) 2240 { 2241 int retval; 2242 union ccb *ccb; 2243 2244 retval = 0; 2245 ccb = cam_getccb(device); 2246 2247 if (ccb == NULL) { 2248 warnx("get_print_cts: error allocating ccb"); 2249 return(1); 2250 } 2251 2252 bzero(&(&ccb->ccb_h)[1], 2253 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2254 2255 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 2256 2257 if (user_settings == 0) 2258 ccb->cts.flags = CCB_TRANS_CURRENT_SETTINGS; 2259 else 2260 ccb->cts.flags = CCB_TRANS_USER_SETTINGS; 2261 2262 if (cam_send_ccb(device, ccb) < 0) { 2263 perror("error sending XPT_GET_TRAN_SETTINGS CCB"); 2264 retval = 1; 2265 goto get_print_cts_bailout; 2266 } 2267 2268 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2269 warnx("XPT_GET_TRANS_SETTINGS CCB failed, status %#x", 2270 ccb->ccb_h.status); 2271 retval = 1; 2272 goto get_print_cts_bailout; 2273 } 2274 2275 if (quiet == 0) 2276 cts_print(device, &ccb->cts); 2277 2278 if (cts != NULL) 2279 bcopy(&ccb->cts, cts, sizeof(struct ccb_trans_settings)); 2280 2281 get_print_cts_bailout: 2282 2283 cam_freeccb(ccb); 2284 2285 return(retval); 2286 } 2287 2288 static int 2289 ratecontrol(struct cam_device *device, int retry_count, int timeout, 2290 int argc, char **argv, char *combinedopt) 2291 { 2292 int c; 2293 union ccb *ccb; 2294 int user_settings = 0; 2295 int retval = 0; 2296 int disc_enable = -1, tag_enable = -1; 2297 int offset = -1; 2298 double syncrate = -1; 2299 int bus_width = -1; 2300 int quiet = 0; 2301 int change_settings = 0, send_tur = 0; 2302 struct ccb_pathinq cpi; 2303 2304 ccb = cam_getccb(device); 2305 2306 if (ccb == NULL) { 2307 warnx("ratecontrol: error allocating ccb"); 2308 return(1); 2309 } 2310 2311 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2312 switch(c){ 2313 case 'a': 2314 send_tur = 1; 2315 break; 2316 case 'c': 2317 user_settings = 0; 2318 break; 2319 case 'D': 2320 if (strncasecmp(optarg, "enable", 6) == 0) 2321 disc_enable = 1; 2322 else if (strncasecmp(optarg, "disable", 7) == 0) 2323 disc_enable = 0; 2324 else { 2325 warnx("-D argument \"%s\" is unknown", optarg); 2326 retval = 1; 2327 goto ratecontrol_bailout; 2328 } 2329 change_settings = 1; 2330 break; 2331 case 'O': 2332 offset = strtol(optarg, NULL, 0); 2333 if (offset < 0) { 2334 warnx("offset value %d is < 0", offset); 2335 retval = 1; 2336 goto ratecontrol_bailout; 2337 } 2338 change_settings = 1; 2339 break; 2340 case 'q': 2341 quiet++; 2342 break; 2343 case 'R': 2344 syncrate = atof(optarg); 2345 2346 if (syncrate < 0) { 2347 warnx("sync rate %f is < 0", syncrate); 2348 retval = 1; 2349 goto ratecontrol_bailout; 2350 } 2351 change_settings = 1; 2352 break; 2353 case 'T': 2354 if (strncasecmp(optarg, "enable", 6) == 0) 2355 tag_enable = 1; 2356 else if (strncasecmp(optarg, "disable", 7) == 0) 2357 tag_enable = 0; 2358 else { 2359 warnx("-T argument \"%s\" is unknown", optarg); 2360 retval = 1; 2361 goto ratecontrol_bailout; 2362 } 2363 change_settings = 1; 2364 break; 2365 case 'U': 2366 user_settings = 1; 2367 break; 2368 case 'W': 2369 bus_width = strtol(optarg, NULL, 0); 2370 if (bus_width < 0) { 2371 warnx("bus width %d is < 0", bus_width); 2372 retval = 1; 2373 goto ratecontrol_bailout; 2374 } 2375 change_settings = 1; 2376 break; 2377 default: 2378 break; 2379 } 2380 } 2381 2382 bzero(&(&ccb->ccb_h)[1], 2383 sizeof(struct ccb_pathinq) - sizeof(struct ccb_hdr)); 2384 2385 /* 2386 * Grab path inquiry information, so we can determine whether 2387 * or not the initiator is capable of the things that the user 2388 * requests. 2389 */ 2390 ccb->ccb_h.func_code = XPT_PATH_INQ; 2391 2392 if (cam_send_ccb(device, ccb) < 0) { 2393 perror("error sending XPT_PATH_INQ CCB"); 2394 retval = 1; 2395 goto ratecontrol_bailout; 2396 } 2397 2398 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2399 warnx("XPT_PATH_INQ CCB failed, status %#x", 2400 ccb->ccb_h.status); 2401 retval = 1; 2402 goto ratecontrol_bailout; 2403 } 2404 2405 bcopy(&ccb->cpi, &cpi, sizeof(struct ccb_pathinq)); 2406 2407 bzero(&(&ccb->ccb_h)[1], 2408 sizeof(struct ccb_trans_settings) - sizeof(struct ccb_hdr)); 2409 2410 if (quiet == 0) 2411 fprintf(stdout, "Current Parameters:\n"); 2412 2413 retval = get_print_cts(device, user_settings, quiet, &ccb->cts); 2414 2415 if (retval != 0) 2416 goto ratecontrol_bailout; 2417 2418 if (arglist & CAM_ARG_VERBOSE) 2419 cpi_print(&cpi); 2420 2421 if (change_settings) { 2422 if (disc_enable != -1) { 2423 ccb->cts.valid |= CCB_TRANS_DISC_VALID; 2424 if (disc_enable == 0) 2425 ccb->cts.flags &= ~CCB_TRANS_DISC_ENB; 2426 else 2427 ccb->cts.flags |= CCB_TRANS_DISC_ENB; 2428 } else 2429 ccb->cts.valid &= ~CCB_TRANS_DISC_VALID; 2430 2431 if (tag_enable != -1) { 2432 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0) { 2433 warnx("HBA does not support tagged queueing, " 2434 "so you cannot modify tag settings"); 2435 retval = 1; 2436 goto ratecontrol_bailout; 2437 } 2438 2439 ccb->cts.valid |= CCB_TRANS_TQ_VALID; 2440 2441 if (tag_enable == 0) 2442 ccb->cts.flags &= ~CCB_TRANS_TAG_ENB; 2443 else 2444 ccb->cts.flags |= CCB_TRANS_TAG_ENB; 2445 } else 2446 ccb->cts.valid &= ~CCB_TRANS_TQ_VALID; 2447 2448 if (offset != -1) { 2449 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2450 warnx("HBA at %s%d is not cable of changing " 2451 "offset", cpi.dev_name, 2452 cpi.unit_number); 2453 retval = 1; 2454 goto ratecontrol_bailout; 2455 } 2456 ccb->cts.valid |= CCB_TRANS_SYNC_OFFSET_VALID; 2457 ccb->cts.sync_offset = offset; 2458 } else 2459 ccb->cts.valid &= ~CCB_TRANS_SYNC_OFFSET_VALID; 2460 2461 if (syncrate != -1) { 2462 int num_syncrates; 2463 int prelim_sync_period; 2464 int period_factor_set = 0; 2465 u_int freq; 2466 int i; 2467 2468 if ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0) { 2469 warnx("HBA at %s%d is not cable of changing " 2470 "transfer rates", cpi.dev_name, 2471 cpi.unit_number); 2472 retval = 1; 2473 goto ratecontrol_bailout; 2474 } 2475 2476 ccb->cts.valid |= CCB_TRANS_SYNC_RATE_VALID; 2477 2478 /* 2479 * The sync rate the user gives us is in MHz. 2480 * We need to translate it into KHz for this 2481 * calculation. 2482 */ 2483 syncrate *= 1000; 2484 2485 /* 2486 * Next, we calculate a "preliminary" sync period 2487 * in tenths of a nanosecond. 2488 */ 2489 if (syncrate == 0) 2490 prelim_sync_period = 0; 2491 else 2492 prelim_sync_period = 10000000 / syncrate; 2493 2494 ccb->cts.sync_period = 2495 scsi_calc_syncparam(prelim_sync_period); 2496 2497 freq = scsi_calc_syncsrate(ccb->cts.sync_period); 2498 } else 2499 ccb->cts.valid &= ~CCB_TRANS_SYNC_RATE_VALID; 2500 2501 /* 2502 * The bus_width argument goes like this: 2503 * 0 == 8 bit 2504 * 1 == 16 bit 2505 * 2 == 32 bit 2506 * Therefore, if you shift the number of bits given on the 2507 * command line right by 4, you should get the correct 2508 * number. 2509 */ 2510 if (bus_width != -1) { 2511 2512 /* 2513 * We might as well validate things here with a 2514 * decipherable error message, rather than what 2515 * will probably be an indecipherable error message 2516 * by the time it gets back to us. 2517 */ 2518 if ((bus_width == 16) 2519 && ((cpi.hba_inquiry & PI_WIDE_16) == 0)) { 2520 warnx("HBA does not support 16 bit bus width"); 2521 retval = 1; 2522 goto ratecontrol_bailout; 2523 } else if ((bus_width == 32) 2524 && ((cpi.hba_inquiry & PI_WIDE_32) == 0)) { 2525 warnx("HBA does not support 32 bit bus width"); 2526 retval = 1; 2527 goto ratecontrol_bailout; 2528 } else if (bus_width != 8) { 2529 warnx("Invalid bus width %d", bus_width); 2530 retval = 1; 2531 goto ratecontrol_bailout; 2532 } 2533 2534 ccb->cts.valid |= CCB_TRANS_BUS_WIDTH_VALID; 2535 ccb->cts.bus_width = bus_width >> 4; 2536 } else 2537 ccb->cts.valid &= ~CCB_TRANS_BUS_WIDTH_VALID; 2538 2539 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS; 2540 2541 if (cam_send_ccb(device, ccb) < 0) { 2542 perror("error sending XPT_SET_TRAN_SETTINGS CCB"); 2543 retval = 1; 2544 goto ratecontrol_bailout; 2545 } 2546 2547 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 2548 warnx("XPT_SET_TRANS_SETTINGS CCB failed, status %#x", 2549 ccb->ccb_h.status); 2550 retval = 1; 2551 goto ratecontrol_bailout; 2552 } 2553 } 2554 2555 if (send_tur) { 2556 retval = testunitready(device, retry_count, timeout, 2557 (arglist & CAM_ARG_VERBOSE) ? 0 : 1); 2558 2559 /* 2560 * If the TUR didn't succeed, just bail. 2561 */ 2562 if (retval != 0) { 2563 if (quiet == 0) 2564 fprintf(stderr, "Test Unit Ready failed\n"); 2565 goto ratecontrol_bailout; 2566 } 2567 2568 /* 2569 * If the user wants things quiet, there's no sense in 2570 * getting the transfer settings, if we're not going 2571 * to print them. 2572 */ 2573 if (quiet != 0) 2574 goto ratecontrol_bailout; 2575 2576 fprintf(stdout, "New Parameters:\n"); 2577 retval = get_print_cts(device, user_settings, 0, NULL); 2578 } 2579 2580 ratecontrol_bailout: 2581 2582 cam_freeccb(ccb); 2583 return(retval); 2584 } 2585 2586 void 2587 usage(int verbose) 2588 { 2589 fprintf(stderr, 2590 "usage: camcontrol <command> [device id][generic args][command args]\n" 2591 " camcontrol devlist [-v]\n" 2592 " camcontrol periphlist [dev_id][-n dev_name] [-u unit]\n" 2593 " camcontrol tur [dev_id][generic args]\n" 2594 " camcontrol inquiry [dev_id][generic args] [-D] [-S] [-R]\n" 2595 " camcontrol start [dev_id][generic args]\n" 2596 " camcontrol stop [dev_id][generic args]\n" 2597 " camcontrol eject [dev_id][generic args]\n" 2598 " camcontrol rescan <bus[:target:lun]>\n" 2599 " camcontrol reset <bus[:target:lun]>\n" 2600 " camcontrol defects [dev_id][generic args] <-f format> [-P][-G]\n" 2601 " camcontrol modepage [dev_id][generic args] <-m page> [-P pagectl]\n" 2602 " [-e][-d]\n" 2603 " camcontrol cmd [dev_id][generic args] <-c cmd [args]>\n" 2604 " [-i len fmt|-o len fmt [args]]\n" 2605 " camcontrol debug [-I][-T][-S][-c] <all|bus[:target[:lun]]|off>\n" 2606 " camcontrol tags [dev_id][generic args] [-N tags] [-q] [-v]\n" 2607 " camcontrol negotiate [dev_id][generic args] [-a][-c]\n" 2608 " [-D <enable|disable>][-O offset][-q]\n" 2609 " [-R syncrate][-v][-T <enable|disable>]\n" 2610 " [-U][-W bus_width]\n" 2611 " camcontrol help\n"); 2612 if (!verbose) 2613 return; 2614 fprintf(stderr, 2615 "Specify one of the following options:\n" 2616 "devlist list all CAM devices\n" 2617 "periphlist list all CAM peripheral drivers attached to a device\n" 2618 "tur send a test unit ready to the named device\n" 2619 "inquiry send a SCSI inquiry command to the named device\n" 2620 "start send a Start Unit command to the device\n" 2621 "stop send a Stop Unit command to the device\n" 2622 "eject send a Stop Unit command to the device with the eject bit set\n" 2623 "rescan rescan the given bus, or bus:target:lun\n" 2624 "reset reset the given bus, or bus:target:lun\n" 2625 "defects read the defect list of the specified device\n" 2626 "modepage display or edit (-e) the given mode page\n" 2627 "cmd send the given scsi command, may need -i or -o as well\n" 2628 "debug turn debugging on/off for a bus, target, or lun, or all devices\n" 2629 "tags report or set the number of transaction slots for a device\n" 2630 "negotiate report or set device negotiation parameters\n" 2631 "help this message\n" 2632 "Device Identifiers:\n" 2633 "bus:target specify the bus and target, lun defaults to 0\n" 2634 "bus:target:lun specify the bus, target and lun\n" 2635 "deviceUNIT specify the device name, like \"da4\" or \"cd2\"\n" 2636 "Generic arguments:\n" 2637 "-v be verbose, print out sense information\n" 2638 "-t timeout command timeout in seconds, overrides default timeout\n" 2639 "-n dev_name specify device name (default is %s)\n" 2640 "-u unit specify unit number (default is %d)\n" 2641 "-E have the kernel attempt to perform SCSI error recovery\n" 2642 "-C count specify the SCSI command retry count (needs -E to work)\n" 2643 "modepage arguments:\n" 2644 "-m page specify the mode page to view or edit\n" 2645 "-e edit the specified mode page\n" 2646 "-d disable block descriptors for mode sense\n" 2647 "-P pgctl page control field 0-3\n" 2648 "defects arguments:\n" 2649 "-f format specify defect list format (block, bfi or phys)\n" 2650 "-G get the grown defect list\n" 2651 "-P get the permanant defect list\n" 2652 "inquiry arguments:\n" 2653 "-D get the standard inquiry data\n" 2654 "-S get the serial number\n" 2655 "-R get the transfer rate, etc.\n" 2656 "cmd arguments:\n" 2657 "-c cdb [args] specify the SCSI CDB\n" 2658 "-i len fmt specify input data and input data format\n" 2659 "-o len fmt [args] specify output data and output data fmt\n" 2660 "debug arguments:\n" 2661 "-I CAM_DEBUG_INFO -- scsi commands, errors, data\n" 2662 "-T CAM_DEBUG_TRACE -- routine flow tracking\n" 2663 "-S CAM_DEBUG_SUBTRACE -- internal routine command flow\n" 2664 "-c CAM_DEBUG_CDB -- print out SCSI CDBs only\n" 2665 "tags arguments:\n" 2666 "-N tags specify the number of tags to use for this device\n" 2667 "-q be quiet, don't report the number of tags\n" 2668 "-v report a number of tag-related parameters\n" 2669 "negotiate arguments:\n" 2670 "-a send a test unit ready after negotiation\n" 2671 "-c report/set current negotiation settings\n" 2672 "-D <arg> \"enable\" or \"disable\" disconnection\n" 2673 "-O offset set command delay offset\n" 2674 "-q be quiet, don't report anything\n" 2675 "-R syncrate synchronization rate in MHz\n" 2676 "-T <arg> \"enable\" or \"disable\" tagged queueing\n" 2677 "-U report/set user negotiation settings\n" 2678 "-W bus_width set the bus width in bits (8, 16 or 32)\n" 2679 "-v also print a Path Inquiry CCB for the controller\n", 2680 DEFAULT_DEVICE, DEFAULT_UNIT); 2681 } 2682 2683 int 2684 main(int argc, char **argv) 2685 { 2686 int c; 2687 char *device = NULL; 2688 int unit = 0; 2689 struct cam_device *cam_dev = NULL; 2690 int timeout = 0, retry_count = 1; 2691 camcontrol_optret optreturn; 2692 char *tstr; 2693 char *mainopt = "C:En:t:u:v"; 2694 char *subopt = NULL; 2695 char combinedopt[256]; 2696 int error = 0, optstart = 2; 2697 int devopen = 1; 2698 2699 arglist = CAM_ARG_NONE; 2700 2701 if (argc < 2) { 2702 usage(0); 2703 exit(1); 2704 } 2705 2706 /* 2707 * Get the base option. 2708 */ 2709 optreturn = getoption(argv[1], &arglist, &subopt); 2710 2711 if (optreturn == CC_OR_AMBIGUOUS) { 2712 warnx("ambiguous option %s", argv[1]); 2713 usage(0); 2714 exit(1); 2715 } else if (optreturn == CC_OR_NOT_FOUND) { 2716 warnx("option %s not found", argv[1]); 2717 usage(0); 2718 exit(1); 2719 } 2720 2721 /* 2722 * Ahh, getopt(3) is a pain. 2723 * 2724 * This is a gross hack. There really aren't many other good 2725 * options (excuse the pun) for parsing options in a situation like 2726 * this. getopt is kinda braindead, so you end up having to run 2727 * through the options twice, and give each invocation of getopt 2728 * the option string for the other invocation. 2729 * 2730 * You would think that you could just have two groups of options. 2731 * The first group would get parsed by the first invocation of 2732 * getopt, and the second group would get parsed by the second 2733 * invocation of getopt. It doesn't quite work out that way. When 2734 * the first invocation of getopt finishes, it leaves optind pointing 2735 * to the argument _after_ the first argument in the second group. 2736 * So when the second invocation of getopt comes around, it doesn't 2737 * recognize the first argument it gets and then bails out. 2738 * 2739 * A nice alternative would be to have a flag for getopt that says 2740 * "just keep parsing arguments even when you encounter an unknown 2741 * argument", but there isn't one. So there's no real clean way to 2742 * easily parse two sets of arguments without having one invocation 2743 * of getopt know about the other. 2744 * 2745 * Without this hack, the first invocation of getopt would work as 2746 * long as the generic arguments are first, but the second invocation 2747 * (in the subfunction) would fail in one of two ways. In the case 2748 * where you don't set optreset, it would fail because optind may be 2749 * pointing to the argument after the one it should be pointing at. 2750 * In the case where you do set optreset, and reset optind, it would 2751 * fail because getopt would run into the first set of options, which 2752 * it doesn't understand. 2753 * 2754 * All of this would "sort of" work if you could somehow figure out 2755 * whether optind had been incremented one option too far. The 2756 * mechanics of that, however, are more daunting than just giving 2757 * both invocations all of the expect options for either invocation. 2758 * 2759 * Needless to say, I wouldn't mind if someone invented a better 2760 * (non-GPL!) command line parsing interface than getopt. I 2761 * wouldn't mind if someone added more knobs to getopt to make it 2762 * work better. Who knows, I may talk myself into doing it someday, 2763 * if the standards weenies let me. As it is, it just leads to 2764 * hackery like this and causes people to avoid it in some cases. 2765 * 2766 * KDM, September 8th, 1998 2767 */ 2768 if (subopt != NULL) 2769 sprintf(combinedopt, "%s%s", mainopt, subopt); 2770 else 2771 sprintf(combinedopt, "%s", mainopt); 2772 2773 /* 2774 * For these options we do not parse optional device arguments and 2775 * we do not open a passthrough device. 2776 */ 2777 if (((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_RESCAN) 2778 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_RESET) 2779 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_DEVTREE) 2780 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_USAGE) 2781 || ((arglist & CAM_ARG_OPT_MASK) == CAM_ARG_DEBUG)) 2782 devopen = 0; 2783 2784 if ((devopen == 1) 2785 && (argc > 2 && argv[2][0] != '-')) { 2786 char name[30]; 2787 int rv; 2788 2789 /* 2790 * First catch people who try to do things like: 2791 * camcontrol tur /dev/rsd0.ctl 2792 * camcontrol doesn't take device nodes as arguments. 2793 */ 2794 if (argv[2][0] == '/') { 2795 warnx("%s is not a valid device identifier", argv[2]); 2796 errx(1, "please read the camcontrol(8) man page"); 2797 } else if (isdigit(argv[2][0])) { 2798 /* device specified as bus:target[:lun] */ 2799 rv = parse_btl(argv[2], &bus, &target, &lun, &arglist); 2800 if (rv < 2) 2801 errx(1, "numeric device specification must " 2802 "be either bus:target, or " 2803 "bus:target:lun"); 2804 optstart++; 2805 } else { 2806 if (cam_get_device(argv[2], name, sizeof name, &unit) 2807 == -1) 2808 errx(1, "%s", cam_errbuf); 2809 device = strdup(name); 2810 arglist |= CAM_ARG_DEVICE | CAM_ARG_UNIT; 2811 optstart++; 2812 } 2813 } 2814 /* 2815 * Start getopt processing at argv[2/3], since we've already 2816 * accepted argv[1..2] as the command name, and as a possible 2817 * device name. 2818 */ 2819 optind = optstart; 2820 2821 /* 2822 * Now we run through the argument list looking for generic 2823 * options, and ignoring options that possibly belong to 2824 * subfunctions. 2825 */ 2826 while ((c = getopt(argc, argv, combinedopt))!= -1){ 2827 switch(c) { 2828 case 'C': 2829 retry_count = strtol(optarg, NULL, 0); 2830 if (retry_count < 0) 2831 errx(1, "retry count %d is < 0", 2832 retry_count); 2833 arglist |= CAM_ARG_RETRIES; 2834 break; 2835 case 'E': 2836 arglist |= CAM_ARG_ERR_RECOVER; 2837 break; 2838 case 'n': 2839 arglist |= CAM_ARG_DEVICE; 2840 tstr = optarg; 2841 while (isspace(*tstr) && (*tstr != '\0')) 2842 tstr++; 2843 device = (char *)strdup(tstr); 2844 break; 2845 case 't': 2846 timeout = strtol(optarg, NULL, 0); 2847 if (timeout < 0) 2848 errx(1, "invalid timeout %d", timeout); 2849 /* Convert the timeout from seconds to ms */ 2850 timeout *= 1000; 2851 arglist |= CAM_ARG_TIMEOUT; 2852 break; 2853 case 'u': 2854 arglist |= CAM_ARG_UNIT; 2855 unit = strtol(optarg, NULL, 0); 2856 break; 2857 case 'v': 2858 arglist |= CAM_ARG_VERBOSE; 2859 break; 2860 default: 2861 break; 2862 } 2863 } 2864 2865 if ((arglist & CAM_ARG_DEVICE) == 0) 2866 device = (char *)strdup(DEFAULT_DEVICE); 2867 2868 if ((arglist & CAM_ARG_UNIT) == 0) 2869 unit = DEFAULT_UNIT; 2870 2871 /* 2872 * For most commands we'll want to open the passthrough device 2873 * associated with the specified device. In the case of the rescan 2874 * commands, we don't use a passthrough device at all, just the 2875 * transport layer device. 2876 */ 2877 if (devopen == 1) { 2878 if ((cam_dev = ((arglist & (CAM_ARG_BUS | CAM_ARG_TARGET))? 2879 cam_open_btl(bus, target, lun, O_RDWR, NULL) : 2880 cam_open_spec_device(device,unit,O_RDWR,NULL))) 2881 == NULL) 2882 errx(1,"%s", cam_errbuf); 2883 } 2884 2885 /* 2886 * Reset optind to 2, and reset getopt, so these routines can parse 2887 * the arguments again. 2888 */ 2889 optind = optstart; 2890 optreset = 1; 2891 2892 switch(arglist & CAM_ARG_OPT_MASK) { 2893 case CAM_ARG_DEVLIST: 2894 error = getdevlist(cam_dev); 2895 break; 2896 case CAM_ARG_DEVTREE: 2897 error = getdevtree(); 2898 break; 2899 case CAM_ARG_TUR: 2900 error = testunitready(cam_dev, retry_count, timeout, 0); 2901 break; 2902 case CAM_ARG_INQUIRY: 2903 error = scsidoinquiry(cam_dev, argc, argv, combinedopt, 2904 retry_count, timeout); 2905 break; 2906 case CAM_ARG_STARTSTOP: 2907 error = scsistart(cam_dev, arglist & CAM_ARG_START_UNIT, 2908 arglist & CAM_ARG_EJECT, retry_count, 2909 timeout); 2910 break; 2911 case CAM_ARG_RESCAN: 2912 error = dorescan_or_reset(argc, argv, 1); 2913 break; 2914 case CAM_ARG_RESET: 2915 error = dorescan_or_reset(argc, argv, 0); 2916 break; 2917 case CAM_ARG_READ_DEFECTS: 2918 error = readdefects(cam_dev, argc, argv, combinedopt, 2919 retry_count, timeout); 2920 break; 2921 case CAM_ARG_MODE_PAGE: 2922 modepage(cam_dev, argc, argv, combinedopt, 2923 retry_count, timeout); 2924 break; 2925 case CAM_ARG_SCSI_CMD: 2926 error = scsicmd(cam_dev, argc, argv, combinedopt, 2927 retry_count, timeout); 2928 break; 2929 case CAM_ARG_DEBUG: 2930 error = camdebug(argc, argv, combinedopt); 2931 break; 2932 case CAM_ARG_TAG: 2933 error = tagcontrol(cam_dev, argc, argv, combinedopt); 2934 break; 2935 case CAM_ARG_RATE: 2936 error = ratecontrol(cam_dev, retry_count, timeout, 2937 argc, argv, combinedopt); 2938 break; 2939 case CAM_ARG_USAGE: 2940 usage(1); 2941 break; 2942 default: 2943 usage(0); 2944 error = 1; 2945 break; 2946 } 2947 2948 if (cam_dev != NULL) 2949 cam_close_device(cam_dev); 2950 2951 exit(error); 2952 } 2953