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. 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 strncpy(dev_name,devmatchtable[i].real_dev, devnamelen); 293 found = 1; 294 break; 295 } 296 } 297 if (found == 0) 298 strncpy(dev_name, tmpstr, devnamelen); 299 300 /* Make sure we pass back a null-terminated string */ 301 dev_name[devnamelen - 1] = '\0'; 302 303 /* Clean up allocated memory */ 304 free(newpath); 305 306 return(0); 307 308 } 309 310 /* 311 * Backwards compatible wrapper for the real open routine. This translates 312 * a pathname into a device name and unit number for use with the real open 313 * routine. 314 */ 315 struct cam_device * 316 cam_open_device(const char *path, int flags) 317 { 318 int unit; 319 char dev_name[DEV_IDLEN + 1]; 320 321 /* 322 * cam_get_device() has already put an error message in cam_errbuf, 323 * so we don't need to. 324 */ 325 if (cam_get_device(path, dev_name, DEV_IDLEN + 1, &unit) == -1) 326 return(NULL); 327 328 return(cam_lookup_pass(dev_name, unit, flags, path, NULL)); 329 } 330 331 /* 332 * Open the passthrough device for a given bus, target and lun, if the 333 * passthrough device exists. 334 */ 335 struct cam_device * 336 cam_open_btl(path_id_t path_id, target_id_t target_id, lun_id_t target_lun, 337 int flags, struct cam_device *device) 338 { 339 union ccb ccb; 340 struct periph_match_pattern *match_pat; 341 char *func_name = "cam_open_btl"; 342 int fd, bufsize; 343 344 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 345 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 346 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE, 347 func_name, strerror(errno)); 348 return(NULL); 349 } 350 351 bzero(&ccb, sizeof(union ccb)); 352 ccb.ccb_h.func_code = XPT_DEV_MATCH; 353 354 /* Setup the result buffer */ 355 bufsize = sizeof(struct dev_match_result); 356 ccb.cdm.match_buf_len = bufsize; 357 ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize); 358 if (ccb.cdm.matches == NULL) { 359 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 360 "%s: couldn't malloc match buffer", func_name); 361 close(fd); 362 return(NULL); 363 } 364 ccb.cdm.num_matches = 0; 365 366 /* Setup the pattern buffer */ 367 ccb.cdm.num_patterns = 1; 368 ccb.cdm.pattern_buf_len = sizeof(struct dev_match_pattern); 369 ccb.cdm.patterns = (struct dev_match_pattern *)malloc( 370 sizeof(struct dev_match_pattern)); 371 if (ccb.cdm.patterns == NULL) { 372 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 373 "%s: couldn't malloc pattern buffer", func_name); 374 free(ccb.cdm.matches); 375 close(fd); 376 return(NULL); 377 } 378 ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH; 379 match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern; 380 381 /* 382 * We're looking for the passthrough device associated with this 383 * particular bus/target/lun. 384 */ 385 sprintf(match_pat->periph_name, "pass"); 386 match_pat->path_id = path_id; 387 match_pat->target_id = target_id; 388 match_pat->target_lun = target_lun; 389 /* Now set the flags to indicate what we're looking for. */ 390 match_pat->flags = PERIPH_MATCH_PATH | PERIPH_MATCH_TARGET | 391 PERIPH_MATCH_LUN | PERIPH_MATCH_NAME; 392 393 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 394 sprintf(cam_errbuf, "%s: CAMIOCOMMAND ioctl failed\n" 395 "%s: %s", func_name, func_name, strerror(errno)); 396 goto btl_bailout; 397 } 398 399 /* 400 * Check for an outright error. 401 */ 402 if ((ccb.ccb_h.status != CAM_REQ_CMP) 403 || ((ccb.cdm.status != CAM_DEV_MATCH_LAST) 404 && (ccb.cdm.status != CAM_DEV_MATCH_MORE))) { 405 sprintf(cam_errbuf, "%s: CAM error %#x, CDM error %d " 406 "returned from XPT_DEV_MATCH ccb", func_name, 407 ccb.ccb_h.status, ccb.cdm.status); 408 goto btl_bailout; 409 } 410 411 if (ccb.cdm.status == CAM_DEV_MATCH_MORE) { 412 sprintf(cam_errbuf, "%s: CDM reported more than one" 413 " passthrough device at %d:%d:%d!!\n", 414 func_name, path_id, target_id, target_lun); 415 goto btl_bailout; 416 } 417 418 if (ccb.cdm.num_matches == 0) { 419 sprintf(cam_errbuf, "%s: no passthrough device found at" 420 " %d:%d:%d", func_name, path_id, target_id, 421 target_lun); 422 goto btl_bailout; 423 } 424 425 switch(ccb.cdm.matches[0].type) { 426 case DEV_MATCH_PERIPH: { 427 int pass_unit; 428 char dev_path[256]; 429 struct periph_match_result *periph_result; 430 431 periph_result = &ccb.cdm.matches[0].result.periph_result; 432 pass_unit = periph_result->unit_number; 433 free(ccb.cdm.matches); 434 free(ccb.cdm.patterns); 435 close(fd); 436 sprintf(dev_path, "/dev/pass%d", pass_unit); 437 return(cam_real_open_device(dev_path, flags, device, NULL, 438 NULL, 0)); 439 break; /* NOTREACHED */ 440 } 441 default: 442 sprintf(cam_errbuf, "%s: asked for a peripheral match, but" 443 " got a bus or device match??!!", func_name); 444 goto btl_bailout; 445 break; /* NOTREACHED */ 446 } 447 448 btl_bailout: 449 free(ccb.cdm.matches); 450 free(ccb.cdm.patterns); 451 close(fd); 452 return(NULL); 453 } 454 455 struct cam_device * 456 cam_open_spec_device(const char *dev_name, int unit, int flags, 457 struct cam_device *device) 458 { 459 return(cam_lookup_pass(dev_name, unit, flags, NULL, device)); 460 } 461 462 struct cam_device * 463 cam_open_pass(const char *path, int flags, struct cam_device *device) 464 { 465 return(cam_real_open_device(path, flags, device, path, NULL, 0)); 466 } 467 468 static struct cam_device * 469 cam_lookup_pass(const char *dev_name, int unit, int flags, 470 const char *given_path, struct cam_device *device) 471 { 472 int fd; 473 union ccb ccb; 474 char dev_path[256]; 475 char *func_name = "cam_lookup_pass"; 476 477 /* 478 * The flags argument above only applies to the actual passthrough 479 * device open, not our open of the given device to find the 480 * passthrough device. 481 */ 482 if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) { 483 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 484 "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE, 485 func_name, strerror(errno)); 486 return(NULL); 487 } 488 489 /* This isn't strictly necessary for the GETPASSTHRU ioctl. */ 490 ccb.ccb_h.func_code = XPT_GDEVLIST; 491 492 /* These two are necessary for the GETPASSTHRU ioctl to work. */ 493 strncpy(ccb.cgdl.periph_name, dev_name, DEV_IDLEN - 1); 494 ccb.cgdl.periph_name[DEV_IDLEN - 1] = '\0'; 495 ccb.cgdl.unit_number = unit; 496 497 /* 498 * Attempt to get the passthrough device. This ioctl will fail if 499 * the device name is null, if the device doesn't exist, or if the 500 * passthrough driver isn't in the kernel. 501 */ 502 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 503 char tmpstr[256]; 504 505 /* 506 * If we get ENOENT from the transport layer version of 507 * the CAMGETPASSTHRU ioctl, it means one of two things: 508 * either the device name/unit number passed in doesn't 509 * exist, or the passthrough driver isn't in the kernel. 510 */ 511 if (errno == ENOENT) { 512 snprintf(tmpstr, sizeof(tmpstr), 513 "\n%s: either the pass driver isn't in " 514 "your kernel\n%s: or %s%d doesn't exist", 515 func_name, func_name, dev_name, unit); 516 } 517 snprintf(cam_errbuf, sizeof(cam_errbuf), 518 "%s: CAMGETPASSTHRU ioctl failed\n" 519 "%s: %s%s", func_name, func_name, strerror(errno), 520 (errno == ENOENT) ? tmpstr : ""); 521 522 return(NULL); 523 } 524 525 close(fd); 526 527 /* 528 * If the ioctl returned the right status, but we got an error back 529 * in the ccb, that means that the kernel found the device the user 530 * passed in, but was unable to find the passthrough device for 531 * the device the user gave us. 532 */ 533 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 534 sprintf(cam_errbuf, "%s: device %s%d does not exist", 535 func_name, dev_name, unit); 536 return(NULL); 537 } 538 539 sprintf(dev_path, "/dev/%s%d", ccb.cgdl.periph_name, 540 ccb.cgdl.unit_number); 541 542 return(cam_real_open_device(dev_path, flags, device, NULL, 543 dev_name, unit)); 544 } 545 546 /* 547 * Open a given device. The path argument isn't strictly necessary, but it 548 * is copied into the cam_device structure as a convenience to the user. 549 */ 550 static struct cam_device * 551 cam_real_open_device(const char *path, int flags, struct cam_device *device, 552 const char *given_path, const char *given_dev_name, 553 int given_unit_number) 554 { 555 char newpath[MAXPATHLEN+1]; 556 char *func_name = "cam_real_open_device"; 557 union ccb ccb; 558 int fd = -1, malloced_device = 0; 559 560 /* 561 * See if the user wants us to malloc a device for him. 562 */ 563 if (device == NULL) { 564 if ((device = (struct cam_device *)malloc( 565 sizeof(struct cam_device))) == NULL) { 566 sprintf(cam_errbuf, "%s: device structure malloc" 567 " failed\n%s: %s", func_name, func_name, 568 strerror(errno)); 569 return(NULL); 570 } 571 device->fd = -1; 572 malloced_device = 1; 573 } 574 575 /* 576 * If the user passed in a path, save it for him. 577 */ 578 if (given_path != NULL) 579 strncpy(device->device_path, given_path, MAXPATHLEN + 1); 580 else 581 device->device_path[0] = '\0'; 582 583 /* 584 * If the user passed in a device name and unit number pair, save 585 * those as well. 586 */ 587 if (given_dev_name != NULL) 588 strncpy(device->given_dev_name, given_dev_name, DEV_IDLEN); 589 else 590 device->given_dev_name[0] = '\0'; 591 device->given_unit_number = given_unit_number; 592 593 if ((fd = open(path, flags)) < 0) { 594 snprintf(cam_errbuf, CAM_ERRBUF_SIZE, 595 "%s: couldn't open passthrough device %s\n" 596 "%s: %s", func_name, path, func_name, 597 strerror(errno)); 598 goto crod_bailout; 599 } 600 601 device->fd = fd; 602 603 bzero(&ccb, sizeof(union ccb)); 604 605 /* 606 * Unlike the transport layer version of the GETPASSTHRU ioctl, 607 * we don't have to set any fields. 608 */ 609 ccb.ccb_h.func_code = XPT_GDEVLIST; 610 611 /* 612 * We're only doing this to get some information on the device in 613 * question. Otherwise, we'd have to pass in yet another 614 * parameter: the passthrough driver unit number. 615 */ 616 if (ioctl(fd, CAMGETPASSTHRU, &ccb) == -1) { 617 /* 618 * At this point we know the passthrough device must exist 619 * because we just opened it above. The only way this 620 * ioctl can fail is if the ccb size is wrong. 621 */ 622 sprintf(cam_errbuf, "%s: CAMGETPASSTHRU ioctl failed\n" 623 "%s: %s", func_name, func_name, strerror(errno)); 624 goto crod_bailout; 625 } 626 627 /* 628 * If the ioctl returned the right status, but we got an error back 629 * in the ccb, that means that the kernel found the device the user 630 * passed in, but was unable to find the passthrough device for 631 * the device the user gave us. 632 */ 633 if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) { 634 sprintf(cam_errbuf, "%s: passthrough device does not exist??!!", 635 func_name); 636 goto crod_bailout; 637 } 638 639 device->dev_unit_num = ccb.cgdl.unit_number; 640 strcpy(device->device_name, ccb.cgdl.periph_name); 641 device->path_id = ccb.ccb_h.path_id; 642 device->target_id = ccb.ccb_h.target_id; 643 device->target_lun = ccb.ccb_h.target_lun; 644 645 ccb.ccb_h.func_code = XPT_PATH_INQ; 646 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 647 sprintf(cam_errbuf, "%s: Path Inquiry CCB failed\n" 648 "%s: %s", func_name, func_name, strerror(errno)); 649 goto crod_bailout; 650 } 651 strncpy(device->sim_name, ccb.cpi.dev_name, SIM_IDLEN); 652 device->sim_unit_number = ccb.cpi.unit_number; 653 device->bus_id = ccb.cpi.bus_id; 654 655 /* 656 * It doesn't really matter what is in the payload for a getdev 657 * CCB, the kernel doesn't look at it. 658 */ 659 ccb.ccb_h.func_code = XPT_GDEV_TYPE; 660 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 661 sprintf(cam_errbuf, "%s: Get Device Type CCB failed\n" 662 "%s: %s", func_name, func_name, strerror(errno)); 663 goto crod_bailout; 664 } 665 device->pd_type = SID_TYPE(&ccb.cgd.inq_data); 666 bcopy(&ccb.cgd.inq_data, &device->inq_data, 667 sizeof(struct scsi_inquiry_data)); 668 device->serial_num_len = ccb.cgd.serial_num_len; 669 bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len); 670 671 /* 672 * Zero the payload, the kernel does look at the flags. 673 */ 674 bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings)); 675 676 /* 677 * Get transfer settings for this device. 678 */ 679 ccb.ccb_h.func_code = XPT_GET_TRAN_SETTINGS; 680 681 ccb.cts.flags = CCB_TRANS_CURRENT_SETTINGS; 682 683 if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) { 684 sprintf(cam_errbuf, "%s: Get Transfer Settings CCB failed\n" 685 "%s: %s", func_name, func_name, strerror(errno)); 686 goto crod_bailout; 687 } 688 device->sync_period = ccb.cts.sync_period; 689 device->sync_offset = ccb.cts.sync_offset; 690 device->bus_width = ccb.cts.bus_width; 691 692 return(device); 693 694 crod_bailout: 695 696 if (fd >= 0) 697 close(fd); 698 699 if (malloced_device) 700 free(device); 701 702 return(NULL); 703 } 704 705 void 706 cam_close_device(struct cam_device *dev) 707 { 708 if (dev == NULL) 709 return; 710 711 cam_close_spec_device(dev); 712 713 if (dev != NULL) 714 free(dev); 715 } 716 717 void 718 cam_close_spec_device(struct cam_device *dev) 719 { 720 if (dev == NULL) 721 return; 722 723 if (dev->fd >= 0) 724 close(dev->fd); 725 } 726 727 char * 728 cam_path_string(struct cam_device *dev, char *str, int len) 729 { 730 if (dev == NULL) { 731 snprintf(str, len, "No path"); 732 return(str); 733 } 734 735 snprintf(str, len, "(%s%d:%s%d:%d:%d:%d): ", 736 (dev->device_name[0] != '\0') ? dev->device_name : "pass", 737 dev->dev_unit_num, 738 (dev->sim_name[0] != '\0') ? dev->sim_name : "unknown", 739 dev->sim_unit_number, 740 dev->bus_id, 741 dev->target_id, 742 dev->target_lun); 743 744 return(str); 745 } 746 747 /* 748 * Malloc/duplicate a CAM device structure. 749 */ 750 struct cam_device * 751 cam_device_dup(struct cam_device *device) 752 { 753 char *func_name = "cam_device_dup"; 754 struct cam_device *newdev; 755 756 if (device == NULL) { 757 sprintf(cam_errbuf, "%s: device is NULL", func_name); 758 return(NULL); 759 } 760 761 newdev = malloc(sizeof(struct cam_device)); 762 763 bcopy(device, newdev, sizeof(struct cam_device)); 764 765 return(newdev); 766 } 767 768 /* 769 * Copy a CAM device structure. 770 */ 771 void 772 cam_device_copy(struct cam_device *src, struct cam_device *dst) 773 { 774 char *func_name = "cam_device_copy"; 775 776 if (src == NULL) { 777 sprintf(cam_errbuf, "%s: source device struct was NULL", 778 func_name); 779 return; 780 } 781 782 if (dst == NULL) { 783 sprintf(cam_errbuf, "%s: destination device struct was NULL", 784 func_name); 785 return; 786 } 787 788 bcopy(src, dst, sizeof(struct cam_device)); 789 790 } 791