1 /* 2 * Copyright (c) 1997, 1998, 1999, 2002 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. The name of the author may not be used to endorse or promote products 11 * derived from this software without specific prior written permission. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <fcntl.h> 35 #include <unistd.h> 36 #include <errno.h> 37 #include <ctype.h> 38 39 #include <cam/cam.h> 40 #include <cam/scsi/scsi_all.h> 41 #include <cam/cam_ccb.h> 42 #include <cam/scsi/scsi_pass.h> 43 #include "camlib.h" 44 45 struct cam_devequiv { 46 char *given_dev; 47 char *real_dev; 48 }; 49 50 struct cam_devequiv devmatchtable[] = { 51 {"sd", "da"}, 52 {"st", "sa"} 53 }; 54 55 char cam_errbuf[CAM_ERRBUF_SIZE]; 56 57 static struct cam_device *cam_real_open_device(const char *path, int flags, 58 struct cam_device *device, 59 const char *given_path, 60 const char *given_dev_name, 61 int given_unit_number); 62 static struct cam_device *cam_lookup_pass(const char *dev_name, int unit, 63 int flags, const char *given_path, 64 struct cam_device *device); 65 66 /* 67 * Send a ccb to a passthrough device. 68 */ 69 int 70 cam_send_ccb(struct cam_device *device, union ccb *ccb) 71 { 72 return(ioctl(device->fd, CAMIOCOMMAND, ccb)); 73 } 74 75 /* 76 * Malloc a CCB, zero out the header and set its path, target and lun ids. 77 */ 78 union ccb * 79 cam_getccb(struct cam_device *dev) 80 { 81 union ccb *ccb; 82 83 ccb = (union ccb *)malloc(sizeof(union ccb)); 84 if (ccb != NULL) { 85 bzero(&ccb->ccb_h, sizeof(struct ccb_hdr)); 86 ccb->ccb_h.path_id = dev->path_id; 87 ccb->ccb_h.target_id = dev->target_id; 88 ccb->ccb_h.target_lun = dev->target_lun; 89 } 90 91 return(ccb); 92 } 93 94 /* 95 * Free a CCB. 96 */ 97 void 98 cam_freeccb(union ccb *ccb) 99 { 100 if (ccb != NULL) 101 free(ccb); 102 } 103 104 /* 105 * Take a device name or path passed in by the user, and attempt to figure 106 * out the device name and unit number. Some possible device name formats are: 107 * /dev/foo0a 108 * /dev/rfoo0a 109 * /dev/rfoos2c 110 * foo0 111 * foo0a 112 * rfoo0 113 * rfoo0a 114 * nrfoo0 115 * 116 * If the caller passes in an old style device name like 'sd' or 'st', 117 * it will be converted to the new style device name based upon devmatchtable 118 * above. 119 * 120 * Input parameters: device name/path, length of devname string 121 * Output: device name, unit number 122 * Return values: returns 0 for success, -1 for failure 123 */ 124 int 125 cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit) 126 { 127 char *func_name = "cam_get_device"; 128 char *tmpstr, *tmpstr2; 129 char *newpath; 130 int unit_offset; 131 int i, found = 0; 132 133 134 if (path == NULL) { 135 sprintf(cam_errbuf, "%s: device pathname was NULL", func_name); 136 return(-1); 137 } 138 139 /* 140 * We can be rather destructive to the path string. Make a copy of 141 * it so we don't hose the user's string. 142 */ 143 newpath = (char *)strdup(path); 144 tmpstr = newpath; 145 146 /* Get rid of any leading white space */ 147 while (isspace(*tmpstr) && (*tmpstr != '\0')) 148 tmpstr++; 149 150 /* 151 * Check to see whether we have an absolute pathname. 152 */ 153 if (*tmpstr == '/') { 154 tmpstr2 = tmpstr; 155 tmpstr = (char *)rindex(tmpstr2, '/'); 156 if ((tmpstr != NULL) && (*tmpstr != '\0')) 157 tmpstr++; 158 } 159 160 if (*tmpstr == '\0') { 161 sprintf(cam_errbuf, "%s: no text after slash", func_name); 162 free(newpath); 163 return(-1); 164 } 165 166 /* 167 * Check to see whether the user has given us a nonrewound tape 168 * device. 169 */ 170 if (*tmpstr == 'n') 171 tmpstr++; 172 173 if (*tmpstr == '\0') { 174 sprintf(cam_errbuf, "%s: no text after leading 'n'", func_name); 175 free(newpath); 176 return(-1); 177 } 178 179 /* 180 * See if the user has given us a character device. 181 */ 182 if (*tmpstr == 'r') 183 tmpstr++; 184 185 if (*tmpstr == '\0') { 186 sprintf(cam_errbuf, "%s: no text after leading 'r'", func_name); 187 free(newpath); 188 return(-1); 189 } 190 191 /* 192 * Try to get rid of any trailing white space or partition letters. 193 */ 194 tmpstr2 = &tmpstr[strlen(tmpstr) - 1]; 195 196 while ((*tmpstr2 != '\0') && (tmpstr2 > tmpstr) &&(!isdigit(*tmpstr2))){ 197 *tmpstr2 = '\0'; 198 tmpstr2--; 199 } 200 201 /* 202 * Check to see whether we have been given a partition with a slice 203 * name. If so, get rid of the slice name/number. 204 */ 205 if (strlen(tmpstr) > 3) { 206 /* 207 * Basically, we're looking for a string that ends in the 208 * following general manner: 1s1 -- a number, the letter 209 * s, and then another number. This indicates that the 210 * user has given us a slice. We substitute nulls for the 211 * s and the slice number. 212 */ 213 if ((isdigit(tmpstr[strlen(tmpstr) - 1])) 214 && (tmpstr[strlen(tmpstr) - 2] == 's') 215 && (isdigit(tmpstr[strlen(tmpstr) - 3]))) { 216 tmpstr[strlen(tmpstr) - 1] = '\0'; 217 tmpstr[strlen(tmpstr) - 1] = '\0'; 218 } 219 } 220 221 /* 222 * After we nuke off the slice, we should have just a device name 223 * and unit number. That means there must be at least 2 224 * characters. If we only have 1, we don't have a valid device name. 225 */ 226 if (strlen(tmpstr) < 2) { 227 sprintf(cam_errbuf, 228 "%s: must have both device name and unit number", 229 func_name); 230 free(newpath); 231 return(-1); 232 } 233 234 /* 235 * If the first character of the string is a digit, then the user 236 * has probably given us all numbers. Point out the error. 237 */ 238 if (isdigit(*tmpstr)) { 239 sprintf(cam_errbuf, 240 "%s: device name cannot begin with a number", 241 func_name); 242 free(newpath); 243 return(-1); 244 } 245 246 /* 247 * At this point, if the last character of the string isn't a 248 * number, we know the user either didn't give us a device number, 249 * or he gave us a device name/number format we don't recognize. 250 */ 251 if (!isdigit(tmpstr[strlen(tmpstr) - 1])) { 252 sprintf(cam_errbuf, "%s: unable to find device unit number", 253 func_name); 254 free(newpath); 255 return(-1); 256 } 257 258 /* 259 * Attempt to figure out where the device name ends and the unit 260 * number begins. As long as unit_offset is at least 1 less than 261 * the length of the string, we can still potentially have a device 262 * name at the front of the string. When we get to something that 263 * isn't a digit, we've hit the device name. Because of the check 264 * above, we know that this cannot happen when unit_offset == 1. 265 * Therefore it is okay to decrement unit_offset -- it won't cause 266 * us to go past the end of the character array. 267 */ 268 for (unit_offset = 1; 269 (unit_offset < (strlen(tmpstr))) 270 && (isdigit(tmpstr[strlen(tmpstr) - unit_offset])); unit_offset++); 271 272 unit_offset--; 273 274 /* 275 * Grab the unit number. 276 */ 277 *unit = atoi(&tmpstr[strlen(tmpstr) - unit_offset]); 278 279 /* 280 * Put a null in place of the first number of the unit number so 281 * that all we have left is the device name. 282 */ 283 tmpstr[strlen(tmpstr) - unit_offset] = '\0'; 284 285 /* 286 * Look through our equivalency table and see if the device name 287 * the user gave us is an old style device name. If so, translate 288 * it to the new style device name. 289 */ 290 for (i = 0;i < (sizeof(devmatchtable)/sizeof(struct cam_devequiv));i++){ 291 if (strcmp(tmpstr, devmatchtable[i].given_dev) == 0) { 292 strlcpy(dev_name,devmatchtable[i].real_dev, devnamelen); 293 found = 1; 294 break; 295 } 296 } 297 if (found == 0) 298 strlcpy(dev_name, tmpstr, devnamelen); 299 300 /* Clean up allocated memory */ 301 free(newpath); 302 303 return(0); 304 305 } 306 307 /* 308 * Backwards compatible wrapper for the real open routine. This translates 309 * a pathname into a device name and unit number for use with the real open 310 * routine. 311 */ 312 struct cam_device * 313 cam_open_device(const char *path, int flags) 314 { 315 int unit; 316 char dev_name[DEV_IDLEN + 1]; 317 318 /* 319 * cam_get_device() has already put an error message in cam_errbuf, 320 * so we don't need to. 321 */ 322 if (cam_get_device(path, dev_name, sizeof(dev_name), &unit) == -1) 323 return(NULL); 324 325 return(cam_lookup_pass(dev_name, unit, flags, path, NULL)); 326 } 327 328 /* 329 * Open the passthrough device for a given bus, target and lun, if the 330 * passthrough device exists. 331 */ 332 struct cam_device * 333 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun, 334 int flags, struct cam_device *device) 335 { 336 union ccb ccb; 337 struct periph_match_pattern *match_pat; 338 char *func_name = "cam_open_btl"; 339 int fd, bufsize; 340 341 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 342 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 343 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE, 344 func_name, strerror(errno)); 345 return(NULL); 346 } 347 348 bzero(&ccb, sizeof(union ccb)); 349 ccb.ccb_h.func_code = XPT_DEV_MATCH; 350 351 /* Setup the result buffer */ 352 bufsize = sizeof(struct dev_match_result); 353 ccb.cdm.match_buf_len = bufsize; 354 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize); 355 if (ccb.cdm.matches == NULL) { 356 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 357 "%s: couldn't malloc match buffer", func_name); 358 close(fd); 359 return(NULL); 360 } 361 ccb.cdm.num_matches = 0; 362 363 /* Setup the pattern buffer */ 364 ccb.cdm.num_patterns = 1; 365 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern); 366 ccb.cdm.patterns = (struct dev_match_pattern *)malloc( 367 sizeof(struct dev_match_pattern)); 368 if (ccb.cdm.patterns == NULL) { 369 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 370 "%s: couldn't malloc pattern buffer", func_name); 371 free(ccb.cdm.matches); 372 close(fd); 373 return(NULL); 374 } 375 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH; 376 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern; 377 378 /* 379 * We're looking for the passthrough device associated with this 380 * particular bus/target/lun. 381 */ 382 sprintf(match_pat->periph_name, "pass"); 383 match_pat->path_id = path_id; 384 match_pat->target_id = target_id; 385 match_pat->target_lun = target_lun; 386 /* Now set the flags to indicate what we're looking for. */ 387 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET | 388 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME; 389 390 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 391 sprintf(cam_errbuf, "%s: CAMIOCOMMAND ioctl failed\n" 392 "%s: %s", func_name, func_name, strerror(errno)); 393 goto btl_bailout; 394 } 395 396 /* 397 * Check for an outright error. 398 */ 399 if ((ccb.ccb_h.status != CAM_REQ_CMP) 400 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST) 401 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) { 402 sprintf(cam_errbuf, "%s: CAM error %#x, CDM error %d " 403 "returned from XPT_DEV_MATCH ccb", func_name, 404 ccb.ccb_h.status, ccb.cdm.status); 405 goto btl_bailout; 406 } 407 408 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) { 409 sprintf(cam_errbuf, "%s: CDM reported more than one" 410 " passthrough device at %d:%d:%d!!\n", 411 func_name, path_id, target_id, target_lun); 412 goto btl_bailout; 413 } 414 415 if (ccb.cdm.num_matches == 0) { 416 sprintf(cam_errbuf, "%s: no passthrough device found at" 417 " %d:%d:%d", func_name, path_id, target_id, 418 target_lun); 419 goto btl_bailout; 420 } 421 422 switch(ccb.cdm.matches[0].type) { 423 case DEV_MATCH_PERIPH: { 424 int pass_unit; 425 char dev_path[256]; 426 struct periph_match_result *periph_result; 427 428 periph_result = &ccb.cdm.matches[0].result.periph_result; 429 pass_unit = periph_result->unit_number; 430 free(ccb.cdm.matches); 431 free(ccb.cdm.patterns); 432 close(fd); 433 sprintf(dev_path, "/dev/pass%d", pass_unit); 434 return(cam_real_open_device(dev_path, flags, device, NULL, 435 NULL, 0)); 436 break; /* NOTREACHED */ 437 } 438 default: 439 sprintf(cam_errbuf, "%s: asked for a peripheral match, but" 440 " got a bus or device match??!!", func_name); 441 goto btl_bailout; 442 break; /* NOTREACHED */ 443 } 444 445 btl_bailout: 446 free(ccb.cdm.matches); 447 free(ccb.cdm.patterns); 448 close(fd); 449 return(NULL); 450 } 451 452 struct cam_device * 453 cam_open_spec_device(const char *dev_name, int unit, int flags, 454 struct cam_device *device) 455 { 456 return(cam_lookup_pass(dev_name, unit, flags, NULL, device)); 457 } 458 459 struct cam_device * 460 cam_open_pass(const char *path, int flags, struct cam_device *device) 461 { 462 return(cam_real_open_device(path, flags, device, path, NULL, 0)); 463 } 464 465 static struct cam_device * 466 cam_lookup_pass(const char *dev_name, int unit, int flags, 467 const char *given_path, struct cam_device *device) 468 { 469 int fd; 470 union ccb ccb; 471 char dev_path[256]; 472 char *func_name = "cam_lookup_pass"; 473 474 /* 475 * The flags argument above only applies to the actual passthrough 476 * device open, not our open of the given device to find the 477 * passthrough device. 478 */ 479 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 480 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 481 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE, 482 func_name, strerror(errno)); 483 return(NULL); 484 } 485 486 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */ 487 ccb.ccb_h.func_code = XPT_GDEVLIST; 488 489 /* These two are necessary for the GETPASSTHRU ioctl to work. */ 490 strlcpy(ccb.cgdl.periph_name, dev_name, sizeof(ccb.cgdl.periph_name)); 491 ccb.cgdl.unit_number = unit; 492 493 /* 494 * Attempt to get the passthrough device. This ioctl will fail if 495 * the device name is null, if the device doesn't exist, or if the 496 * passthrough driver isn't in the kernel. 497 */ 498 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 499 char tmpstr[256]; 500 501 /* 502 * If we get ENOENT from the transport layer version of 503 * the CAMGETPASSTHRU ioctl, it means one of two things: 504 * either the device name/unit number passed in doesn't 505 * exist, or the passthrough driver isn't in the kernel. 506 */ 507 if (errno == ENOENT) { 508 snprintf(tmpstr, sizeof(tmpstr), 509 "\n%s: either the pass driver isn't in " 510 "your kernel\n%s: or %s%d doesn't exist", 511 func_name, func_name, dev_name, unit); 512 } 513 snprintf(cam_errbuf, sizeof(cam_errbuf), 514 "%s: CAMGETPASSTHRU ioctl failed\n" 515 "%s: %s%s", func_name, func_name, strerror(errno), 516 (errno == ENOENT) ? tmpstr : ""); 517 518 return(NULL); 519 } 520 521 close(fd); 522 523 /* 524 * If the ioctl returned the right status, but we got an error back 525 * in the ccb, that means that the kernel found the device the user 526 * passed in, but was unable to find the passthrough device for 527 * the device the user gave us. 528 */ 529 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 530 sprintf(cam_errbuf, "%s: device %s%d does not exist", 531 func_name, dev_name, unit); 532 return(NULL); 533 } 534 535 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name, 536 ccb.cgdl.unit_number); 537 538 return(cam_real_open_device(dev_path, flags, device, NULL, 539 dev_name, unit)); 540 } 541 542 /* 543 * Open a given device. The path argument isn't strictly necessary, but it 544 * is copied into the cam_device structure as a convenience to the user. 545 */ 546 static struct cam_device * 547 cam_real_open_device(const char *path, int flags, struct cam_device *device, 548 const char *given_path, const char *given_dev_name, 549 int given_unit_number) 550 { 551 char *func_name = "cam_real_open_device"; 552 union ccb ccb; 553 int fd = -1, malloced_device = 0; 554 555 /* 556 * See if the user wants us to malloc a device for him. 557 */ 558 if (device == NULL) { 559 if ((device = (struct cam_device *)malloc( 560 sizeof(struct cam_device))) == NULL) { 561 sprintf(cam_errbuf, "%s: device structure malloc" 562 " failed\n%s: %s", func_name, func_name, 563 strerror(errno)); 564 return(NULL); 565 } 566 device->fd = -1; 567 malloced_device = 1; 568 } 569 570 /* 571 * If the user passed in a path, save it for him. 572 */ 573 if (given_path != NULL) 574 strlcpy(device->device_path, given_path, 575 sizeof(device->device_path)); 576 else 577 device->device_path[0] = '\0'; 578 579 /* 580 * If the user passed in a device name and unit number pair, save 581 * those as well. 582 */ 583 if (given_dev_name != NULL) 584 strlcpy(device->given_dev_name, given_dev_name, 585 sizeof(device->given_dev_name)); 586 else 587 device->given_dev_name[0] = '\0'; 588 device->given_unit_number = given_unit_number; 589 590 if ((fd = open(path, flags)) < 0) { 591 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 592 "%s: couldn't open passthrough device %s\n" 593 "%s: %s", func_name, path, func_name, 594 strerror(errno)); 595 goto crod_bailout; 596 } 597 598 device->fd = fd; 599 600 bzero(&ccb, sizeof(union ccb)); 601 602 /* 603 * Unlike the transport layer version of the GETPASSTHRU ioctl, 604 * we don't have to set any fields. 605 */ 606 ccb.ccb_h.func_code = XPT_GDEVLIST; 607 608 /* 609 * We're only doing this to get some information on the device in 610 * question. Otherwise, we'd have to pass in yet another 611 * parameter: the passthrough driver unit number. 612 */ 613 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 614 /* 615 * At this point we know the passthrough device must exist 616 * because we just opened it above. The only way this 617 * ioctl can fail is if the ccb size is wrong. 618 */ 619 sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n" 620 "%s: %s", func_name, func_name, strerror(errno)); 621 goto crod_bailout; 622 } 623 624 /* 625 * If the ioctl returned the right status, but we got an error back 626 * in the ccb, that means that the kernel found the device the user 627 * passed in, but was unable to find the passthrough device for 628 * the device the user gave us. 629 */ 630 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 631 sprintf(cam_errbuf, "%s: passthrough device does not exist??!!", 632 func_name); 633 goto crod_bailout; 634 } 635 636 device->dev_unit_num = ccb.cgdl.unit_number; 637 strlcpy(device->device_name, ccb.cgdl.periph_name, 638 sizeof(device->device_name)); 639 device->path_id = ccb.ccb_h.path_id; 640 device->target_id = ccb.ccb_h.target_id; 641 device->target_lun = ccb.ccb_h.target_lun; 642 643 ccb.ccb_h.func_code = XPT_PATH_INQ; 644 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 645 sprintf(cam_errbuf, "%s: Path Inquiry CCB failed\n" 646 "%s: %s", func_name, func_name, strerror(errno)); 647 goto crod_bailout; 648 } 649 strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name)); 650 device->sim_unit_number = ccb.cpi.unit_number; 651 device->bus_id = ccb.cpi.bus_id; 652 653 /* 654 * It doesn't really matter what is in the payload for a getdev 655 * CCB, the kernel doesn't look at it. 656 */ 657 ccb.ccb_h.func_code = XPT_GDEV_TYPE; 658 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 659 sprintf(cam_errbuf, "%s: Get Device Type CCB failed\n" 660 "%s: %s", func_name, func_name, strerror(errno)); 661 goto crod_bailout; 662 } 663 device->pd_type = SID_TYPE(&ccb.cgd.inq_data); 664 bcopy(&ccb.cgd.inq_data, &device->inq_data, 665 sizeof(struct scsi_inquiry_data)); 666 device->serial_num_len = ccb.cgd.serial_num_len; 667 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len); 668 669 /* 670 * Zero the payload, the kernel does look at the flags. 671 */ 672 bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings)); 673 674 /* 675 * Get transfer settings for this device. 676 */ 677 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 678 679 ccb.cts.flags = CCB_TRANS_CURRENT_SETTINGS; 680 681 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 682 sprintf(cam_errbuf, "%s: Get Transfer Settings CCB failed\n" 683 "%s: %s", func_name, func_name, strerror(errno)); 684 goto crod_bailout; 685 } 686 device->sync_period = ccb.cts.sync_period; 687 device->sync_offset = ccb.cts.sync_offset; 688 device->bus_width = ccb.cts.bus_width; 689 690 return(device); 691 692 crod_bailout: 693 694 if (fd >= 0) 695 close(fd); 696 697 if (malloced_device) 698 free(device); 699 700 return(NULL); 701 } 702 703 void 704 cam_close_device(struct cam_device *dev) 705 { 706 if (dev == NULL) 707 return; 708 709 cam_close_spec_device(dev); 710 711 if (dev != NULL) 712 free(dev); 713 } 714 715 void 716 cam_close_spec_device(struct cam_device *dev) 717 { 718 if (dev == NULL) 719 return; 720 721 if (dev->fd >= 0) 722 close(dev->fd); 723 } 724 725 char * 726 cam_path_string(struct cam_device *dev, char *str, int len) 727 { 728 if (dev == NULL) { 729 snprintf(str, len, "No path"); 730 return(str); 731 } 732 733 snprintf(str, len, "(%s%d:%s%d:%d:%d:%d): ", 734 (dev->device_name[0] != '\0') ? dev->device_name : "pass", 735 dev->dev_unit_num, 736 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown", 737 dev->sim_unit_number, 738 dev->bus_id, 739 dev->target_id, 740 dev->target_lun); 741 742 return(str); 743 } 744 745 /* 746 * Malloc/duplicate a CAM device structure. 747 */ 748 struct cam_device * 749 cam_device_dup(struct cam_device *device) 750 { 751 char *func_name = "cam_device_dup"; 752 struct cam_device *newdev; 753 754 if (device == NULL) { 755 sprintf(cam_errbuf, "%s: device is NULL", func_name); 756 return(NULL); 757 } 758 759 newdev = malloc(sizeof(struct cam_device)); 760 761 bcopy(device, newdev, sizeof(struct cam_device)); 762 763 return(newdev); 764 } 765 766 /* 767 * Copy a CAM device structure. 768 */ 769 void 770 cam_device_copy(struct cam_device *src, struct cam_device *dst) 771 { 772 char *func_name = "cam_device_copy"; 773 774 if (src == NULL) { 775 sprintf(cam_errbuf, "%s: source device struct was NULL", 776 func_name); 777 return; 778 } 779 780 if (dst == NULL) { 781 sprintf(cam_errbuf, "%s: destination device struct was NULL", 782 func_name); 783 return; 784 } 785 786 bcopy(src, dst, sizeof(struct cam_device)); 787 788 } 789