254static void xptaction(struct cam_sim *sim, union ccb *work_ccb); 255static void xptpoll(struct cam_sim *sim); 256static void camisr(void *); 257static void camisr_runqueue(void *); 258static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns, 259 u_int num_patterns, struct cam_eb *bus); 260static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns, 261 u_int num_patterns, 262 struct cam_ed *device); 263static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns, 264 u_int num_patterns, 265 struct cam_periph *periph); 266static xpt_busfunc_t xptedtbusfunc; 267static xpt_targetfunc_t xptedttargetfunc; 268static xpt_devicefunc_t xptedtdevicefunc; 269static xpt_periphfunc_t xptedtperiphfunc; 270static xpt_pdrvfunc_t xptplistpdrvfunc; 271static xpt_periphfunc_t xptplistperiphfunc; 272static int xptedtmatch(struct ccb_dev_match *cdm); 273static int xptperiphlistmatch(struct ccb_dev_match *cdm); 274static int xptbustraverse(struct cam_eb *start_bus, 275 xpt_busfunc_t *tr_func, void *arg); 276static int xpttargettraverse(struct cam_eb *bus, 277 struct cam_et *start_target, 278 xpt_targetfunc_t *tr_func, void *arg); 279static int xptdevicetraverse(struct cam_et *target, 280 struct cam_ed *start_device, 281 xpt_devicefunc_t *tr_func, void *arg); 282static int xptperiphtraverse(struct cam_ed *device, 283 struct cam_periph *start_periph, 284 xpt_periphfunc_t *tr_func, void *arg); 285static int xptpdrvtraverse(struct periph_driver **start_pdrv, 286 xpt_pdrvfunc_t *tr_func, void *arg); 287static int xptpdperiphtraverse(struct periph_driver **pdrv, 288 struct cam_periph *start_periph, 289 xpt_periphfunc_t *tr_func, 290 void *arg); 291static xpt_busfunc_t xptdefbusfunc; 292static xpt_targetfunc_t xptdeftargetfunc; 293static xpt_devicefunc_t xptdefdevicefunc; 294static xpt_periphfunc_t xptdefperiphfunc; 295static void xpt_finishconfig_task(void *context, int pending); 296static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg); 297static int xpt_for_all_devices(xpt_devicefunc_t *tr_func, 298 void *arg); 299static void xpt_dev_async_default(u_int32_t async_code, 300 struct cam_eb *bus, 301 struct cam_et *target, 302 struct cam_ed *device, 303 void *async_arg); 304static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus, 305 struct cam_et *target, 306 lun_id_t lun_id); 307static xpt_devicefunc_t xptsetasyncfunc; 308static xpt_busfunc_t xptsetasyncbusfunc; 309static cam_status xptregister(struct cam_periph *periph, 310 void *arg); 311static __inline int periph_is_queued(struct cam_periph *periph); 312static __inline int device_is_alloc_queued(struct cam_ed *device); 313static __inline int device_is_send_queued(struct cam_ed *device); 314 315static __inline int 316xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev) 317{ 318 int retval; 319 320 if ((dev->drvq.entries > 0) && 321 (dev->ccbq.devq_openings > 0) && 322 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 323 CAMQ_GET_PRIO(&dev->drvq))) == 0)) { 324 /* 325 * The priority of a device waiting for CCB resources 326 * is that of the the highest priority peripheral driver 327 * enqueued. 328 */ 329 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue, 330 &dev->alloc_ccb_entry.pinfo, 331 CAMQ_GET_PRIO(&dev->drvq)); 332 } else { 333 retval = 0; 334 } 335 336 return (retval); 337} 338 339static __inline int 340xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev) 341{ 342 int retval; 343 344 if ((dev->ccbq.queue.entries > 0) && 345 (dev->ccbq.dev_openings > 0) && 346 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) { 347 /* 348 * The priority of a device waiting for controller 349 * resources is that of the the highest priority CCB 350 * enqueued. 351 */ 352 retval = 353 xpt_schedule_dev(&bus->sim->devq->send_queue, 354 &dev->send_ccb_entry.pinfo, 355 CAMQ_GET_PRIO(&dev->ccbq.queue)); 356 } else { 357 retval = 0; 358 } 359 return (retval); 360} 361 362static __inline int 363periph_is_queued(struct cam_periph *periph) 364{ 365 return (periph->pinfo.index != CAM_UNQUEUED_INDEX); 366} 367 368static __inline int 369device_is_alloc_queued(struct cam_ed *device) 370{ 371 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 372} 373 374static __inline int 375device_is_send_queued(struct cam_ed *device) 376{ 377 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 378} 379 380static void 381xpt_periph_init() 382{ 383 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0"); 384} 385 386static void 387xptdone(struct cam_periph *periph, union ccb *done_ccb) 388{ 389 /* Caller will release the CCB */ 390 wakeup(&done_ccb->ccb_h.cbfcnp); 391} 392 393static int 394xptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 395{ 396 397 /* 398 * Only allow read-write access. 399 */ 400 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) 401 return(EPERM); 402 403 /* 404 * We don't allow nonblocking access. 405 */ 406 if ((flags & O_NONBLOCK) != 0) { 407 printf("%s: can't do nonblocking access\n", devtoname(dev)); 408 return(ENODEV); 409 } 410 411 /* Mark ourselves open */ 412 mtx_lock(&xsoftc.xpt_lock); 413 xsoftc.flags |= XPT_FLAG_OPEN; 414 mtx_unlock(&xsoftc.xpt_lock); 415 416 return(0); 417} 418 419static int 420xptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 421{ 422 423 /* Mark ourselves closed */ 424 mtx_lock(&xsoftc.xpt_lock); 425 xsoftc.flags &= ~XPT_FLAG_OPEN; 426 mtx_unlock(&xsoftc.xpt_lock); 427 428 return(0); 429} 430 431/* 432 * Don't automatically grab the xpt softc lock here even though this is going 433 * through the xpt device. The xpt device is really just a back door for 434 * accessing other devices and SIMs, so the right thing to do is to grab 435 * the appropriate SIM lock once the bus/SIM is located. 436 */ 437static int 438xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 439{ 440 int error; 441 442 error = 0; 443 444 switch(cmd) { 445 /* 446 * For the transport layer CAMIOCOMMAND ioctl, we really only want 447 * to accept CCB types that don't quite make sense to send through a 448 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated 449 * in the CAM spec. 450 */ 451 case CAMIOCOMMAND: { 452 union ccb *ccb; 453 union ccb *inccb; 454 struct cam_eb *bus; 455 456 inccb = (union ccb *)addr; 457 458 bus = xpt_find_bus(inccb->ccb_h.path_id); 459 if (bus == NULL) 460 return (EINVAL); 461 462 switch (inccb->ccb_h.func_code) { 463 case XPT_SCAN_BUS: 464 case XPT_RESET_BUS: 465 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD || 466 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 467 xpt_release_bus(bus); 468 return (EINVAL); 469 } 470 break; 471 case XPT_SCAN_TGT: 472 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD || 473 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 474 xpt_release_bus(bus); 475 return (EINVAL); 476 } 477 break; 478 default: 479 break; 480 } 481 482 switch(inccb->ccb_h.func_code) { 483 case XPT_SCAN_BUS: 484 case XPT_RESET_BUS: 485 case XPT_PATH_INQ: 486 case XPT_ENG_INQ: 487 case XPT_SCAN_LUN: 488 case XPT_SCAN_TGT: 489 490 ccb = xpt_alloc_ccb(); 491 492 CAM_SIM_LOCK(bus->sim); 493 494 /* 495 * Create a path using the bus, target, and lun the 496 * user passed in. 497 */ 498 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, 499 inccb->ccb_h.path_id, 500 inccb->ccb_h.target_id, 501 inccb->ccb_h.target_lun) != 502 CAM_REQ_CMP){ 503 error = EINVAL; 504 CAM_SIM_UNLOCK(bus->sim); 505 xpt_free_ccb(ccb); 506 break; 507 } 508 /* Ensure all of our fields are correct */ 509 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 510 inccb->ccb_h.pinfo.priority); 511 xpt_merge_ccb(ccb, inccb); 512 ccb->ccb_h.cbfcnp = xptdone; 513 cam_periph_runccb(ccb, NULL, 0, 0, NULL); 514 bcopy(ccb, inccb, sizeof(union ccb)); 515 xpt_free_path(ccb->ccb_h.path); 516 xpt_free_ccb(ccb); 517 CAM_SIM_UNLOCK(bus->sim); 518 break; 519 520 case XPT_DEBUG: { 521 union ccb ccb; 522 523 /* 524 * This is an immediate CCB, so it's okay to 525 * allocate it on the stack. 526 */ 527 528 CAM_SIM_LOCK(bus->sim); 529 530 /* 531 * Create a path using the bus, target, and lun the 532 * user passed in. 533 */ 534 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph, 535 inccb->ccb_h.path_id, 536 inccb->ccb_h.target_id, 537 inccb->ccb_h.target_lun) != 538 CAM_REQ_CMP){ 539 error = EINVAL; 540 CAM_SIM_UNLOCK(bus->sim); 541 break; 542 } 543 /* Ensure all of our fields are correct */ 544 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path, 545 inccb->ccb_h.pinfo.priority); 546 xpt_merge_ccb(&ccb, inccb); 547 ccb.ccb_h.cbfcnp = xptdone; 548 xpt_action(&ccb); 549 CAM_SIM_UNLOCK(bus->sim); 550 bcopy(&ccb, inccb, sizeof(union ccb)); 551 xpt_free_path(ccb.ccb_h.path); 552 break; 553 554 } 555 case XPT_DEV_MATCH: { 556 struct cam_periph_map_info mapinfo; 557 struct cam_path *old_path; 558 559 /* 560 * We can't deal with physical addresses for this 561 * type of transaction. 562 */ 563 if (inccb->ccb_h.flags & CAM_DATA_PHYS) { 564 error = EINVAL; 565 break; 566 } 567 568 /* 569 * Save this in case the caller had it set to 570 * something in particular. 571 */ 572 old_path = inccb->ccb_h.path; 573 574 /* 575 * We really don't need a path for the matching 576 * code. The path is needed because of the 577 * debugging statements in xpt_action(). They 578 * assume that the CCB has a valid path. 579 */ 580 inccb->ccb_h.path = xpt_periph->path; 581 582 bzero(&mapinfo, sizeof(mapinfo)); 583 584 /* 585 * Map the pattern and match buffers into kernel 586 * virtual address space. 587 */ 588 error = cam_periph_mapmem(inccb, &mapinfo); 589 590 if (error) { 591 inccb->ccb_h.path = old_path; 592 break; 593 } 594 595 /* 596 * This is an immediate CCB, we can send it on directly. 597 */ 598 xpt_action(inccb); 599 600 /* 601 * Map the buffers back into user space. 602 */ 603 cam_periph_unmapmem(inccb, &mapinfo); 604 605 inccb->ccb_h.path = old_path; 606 607 error = 0; 608 break; 609 } 610 default: 611 error = ENOTSUP; 612 break; 613 } 614 xpt_release_bus(bus); 615 break; 616 } 617 /* 618 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input, 619 * with the periphal driver name and unit name filled in. The other 620 * fields don't really matter as input. The passthrough driver name 621 * ("pass"), and unit number are passed back in the ccb. The current 622 * device generation number, and the index into the device peripheral 623 * driver list, and the status are also passed back. Note that 624 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb, 625 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is 626 * (or rather should be) impossible for the device peripheral driver 627 * list to change since we look at the whole thing in one pass, and 628 * we do it with lock protection. 629 * 630 */ 631 case CAMGETPASSTHRU: { 632 union ccb *ccb; 633 struct cam_periph *periph; 634 struct periph_driver **p_drv; 635 char *name; 636 u_int unit; 637 u_int cur_generation; 638 int base_periph_found; 639 int splbreaknum; 640 641 ccb = (union ccb *)addr; 642 unit = ccb->cgdl.unit_number; 643 name = ccb->cgdl.periph_name; 644 /* 645 * Every 100 devices, we want to drop our lock protection to 646 * give the software interrupt handler a chance to run. 647 * Most systems won't run into this check, but this should 648 * avoid starvation in the software interrupt handler in 649 * large systems. 650 */ 651 splbreaknum = 100; 652 653 ccb = (union ccb *)addr; 654 655 base_periph_found = 0; 656 657 /* 658 * Sanity check -- make sure we don't get a null peripheral 659 * driver name. 660 */ 661 if (*ccb->cgdl.periph_name == '\0') { 662 error = EINVAL; 663 break; 664 } 665 666 /* Keep the list from changing while we traverse it */ 667 mtx_lock(&xsoftc.xpt_topo_lock); 668ptstartover: 669 cur_generation = xsoftc.xpt_generation; 670 671 /* first find our driver in the list of drivers */ 672 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) 673 if (strcmp((*p_drv)->driver_name, name) == 0) 674 break; 675 676 if (*p_drv == NULL) { 677 mtx_unlock(&xsoftc.xpt_topo_lock); 678 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 679 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 680 *ccb->cgdl.periph_name = '\0'; 681 ccb->cgdl.unit_number = 0; 682 error = ENOENT; 683 break; 684 } 685 686 /* 687 * Run through every peripheral instance of this driver 688 * and check to see whether it matches the unit passed 689 * in by the user. If it does, get out of the loops and 690 * find the passthrough driver associated with that 691 * peripheral driver. 692 */ 693 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL; 694 periph = TAILQ_NEXT(periph, unit_links)) { 695 696 if (periph->unit_number == unit) { 697 break; 698 } else if (--splbreaknum == 0) { 699 mtx_unlock(&xsoftc.xpt_topo_lock); 700 mtx_lock(&xsoftc.xpt_topo_lock); 701 splbreaknum = 100; 702 if (cur_generation != xsoftc.xpt_generation) 703 goto ptstartover; 704 } 705 } 706 /* 707 * If we found the peripheral driver that the user passed 708 * in, go through all of the peripheral drivers for that 709 * particular device and look for a passthrough driver. 710 */ 711 if (periph != NULL) { 712 struct cam_ed *device; 713 int i; 714 715 base_periph_found = 1; 716 device = periph->path->device; 717 for (i = 0, periph = SLIST_FIRST(&device->periphs); 718 periph != NULL; 719 periph = SLIST_NEXT(periph, periph_links), i++) { 720 /* 721 * Check to see whether we have a 722 * passthrough device or not. 723 */ 724 if (strcmp(periph->periph_name, "pass") == 0) { 725 /* 726 * Fill in the getdevlist fields. 727 */ 728 strcpy(ccb->cgdl.periph_name, 729 periph->periph_name); 730 ccb->cgdl.unit_number = 731 periph->unit_number; 732 if (SLIST_NEXT(periph, periph_links)) 733 ccb->cgdl.status = 734 CAM_GDEVLIST_MORE_DEVS; 735 else 736 ccb->cgdl.status = 737 CAM_GDEVLIST_LAST_DEVICE; 738 ccb->cgdl.generation = 739 device->generation; 740 ccb->cgdl.index = i; 741 /* 742 * Fill in some CCB header fields 743 * that the user may want. 744 */ 745 ccb->ccb_h.path_id = 746 periph->path->bus->path_id; 747 ccb->ccb_h.target_id = 748 periph->path->target->target_id; 749 ccb->ccb_h.target_lun = 750 periph->path->device->lun_id; 751 ccb->ccb_h.status = CAM_REQ_CMP; 752 break; 753 } 754 } 755 } 756 757 /* 758 * If the periph is null here, one of two things has 759 * happened. The first possibility is that we couldn't 760 * find the unit number of the particular peripheral driver 761 * that the user is asking about. e.g. the user asks for 762 * the passthrough driver for "da11". We find the list of 763 * "da" peripherals all right, but there is no unit 11. 764 * The other possibility is that we went through the list 765 * of peripheral drivers attached to the device structure, 766 * but didn't find one with the name "pass". Either way, 767 * we return ENOENT, since we couldn't find something. 768 */ 769 if (periph == NULL) { 770 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 771 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 772 *ccb->cgdl.periph_name = '\0'; 773 ccb->cgdl.unit_number = 0; 774 error = ENOENT; 775 /* 776 * It is unfortunate that this is even necessary, 777 * but there are many, many clueless users out there. 778 * If this is true, the user is looking for the 779 * passthrough driver, but doesn't have one in his 780 * kernel. 781 */ 782 if (base_periph_found == 1) { 783 printf("xptioctl: pass driver is not in the " 784 "kernel\n"); 785 printf("xptioctl: put \"device pass\" in " 786 "your kernel config file\n"); 787 } 788 } 789 mtx_unlock(&xsoftc.xpt_topo_lock); 790 break; 791 } 792 default: 793 error = ENOTTY; 794 break; 795 } 796 797 return(error); 798} 799 800static int 801cam_module_event_handler(module_t mod, int what, void *arg) 802{ 803 int error; 804 805 switch (what) { 806 case MOD_LOAD: 807 if ((error = xpt_init(NULL)) != 0) 808 return (error); 809 break; 810 case MOD_UNLOAD: 811 return EBUSY; 812 default: 813 return EOPNOTSUPP; 814 } 815 816 return 0; 817} 818 819static void 820xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb) 821{ 822 823 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) { 824 xpt_free_path(done_ccb->ccb_h.path); 825 xpt_free_ccb(done_ccb); 826 } else { 827 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1; 828 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb); 829 } 830 xpt_release_boot(); 831} 832 833/* thread to handle bus rescans */ 834static void 835xpt_scanner_thread(void *dummy) 836{ 837 union ccb *ccb; 838 struct cam_sim *sim; 839 840 xpt_lock_buses(); 841 for (;;) { 842 if (TAILQ_EMPTY(&xsoftc.ccb_scanq)) 843 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO, 844 "ccb_scanq", 0); 845 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) { 846 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 847 xpt_unlock_buses(); 848 849 sim = ccb->ccb_h.path->bus->sim; 850 CAM_SIM_LOCK(sim); 851 xpt_action(ccb); 852 CAM_SIM_UNLOCK(sim); 853 854 xpt_lock_buses(); 855 } 856 } 857} 858 859void 860xpt_rescan(union ccb *ccb) 861{ 862 struct ccb_hdr *hdr; 863 864 /* Prepare request */ 865 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD && 866 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 867 ccb->ccb_h.func_code = XPT_SCAN_BUS; 868 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 869 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 870 ccb->ccb_h.func_code = XPT_SCAN_TGT; 871 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 872 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD) 873 ccb->ccb_h.func_code = XPT_SCAN_LUN; 874 else { 875 xpt_print(ccb->ccb_h.path, "illegal scan path\n"); 876 xpt_free_path(ccb->ccb_h.path); 877 xpt_free_ccb(ccb); 878 return; 879 } 880 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp; 881 ccb->ccb_h.cbfcnp = xpt_rescan_done; 882 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT); 883 /* Don't make duplicate entries for the same paths. */ 884 xpt_lock_buses(); 885 if (ccb->ccb_h.ppriv_ptr1 == NULL) { 886 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) { 887 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) { 888 wakeup(&xsoftc.ccb_scanq); 889 xpt_unlock_buses(); 890 xpt_print(ccb->ccb_h.path, "rescan already queued\n"); 891 xpt_free_path(ccb->ccb_h.path); 892 xpt_free_ccb(ccb); 893 return; 894 } 895 } 896 } 897 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 898 xsoftc.buses_to_config++; 899 wakeup(&xsoftc.ccb_scanq); 900 xpt_unlock_buses(); 901} 902 903/* Functions accessed by the peripheral drivers */ 904static int 905xpt_init(void *dummy) 906{ 907 struct cam_sim *xpt_sim; 908 struct cam_path *path; 909 struct cam_devq *devq; 910 cam_status status; 911 912 TAILQ_INIT(&xsoftc.xpt_busses); 913 TAILQ_INIT(&cam_simq); 914 TAILQ_INIT(&xsoftc.ccb_scanq); 915 STAILQ_INIT(&xsoftc.highpowerq); 916 xsoftc.num_highpower = CAM_MAX_HIGHPOWER; 917 918 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF); 919 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF); 920 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF); 921 922 /* 923 * The xpt layer is, itself, the equivelent of a SIM. 924 * Allow 16 ccbs in the ccb pool for it. This should 925 * give decent parallelism when we probe busses and 926 * perform other XPT functions. 927 */ 928 devq = cam_simq_alloc(16); 929 xpt_sim = cam_sim_alloc(xptaction, 930 xptpoll, 931 "xpt", 932 /*softc*/NULL, 933 /*unit*/0, 934 /*mtx*/&xsoftc.xpt_lock, 935 /*max_dev_transactions*/0, 936 /*max_tagged_dev_transactions*/0, 937 devq); 938 if (xpt_sim == NULL) 939 return (ENOMEM); 940 941 mtx_lock(&xsoftc.xpt_lock); 942 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) { 943 mtx_unlock(&xsoftc.xpt_lock); 944 printf("xpt_init: xpt_bus_register failed with status %#x," 945 " failing attach\n", status); 946 return (EINVAL); 947 } 948 949 /* 950 * Looking at the XPT from the SIM layer, the XPT is 951 * the equivelent of a peripheral driver. Allocate 952 * a peripheral driver entry for us. 953 */ 954 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID, 955 CAM_TARGET_WILDCARD, 956 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) { 957 mtx_unlock(&xsoftc.xpt_lock); 958 printf("xpt_init: xpt_create_path failed with status %#x," 959 " failing attach\n", status); 960 return (EINVAL); 961 } 962 963 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO, 964 path, NULL, 0, xpt_sim); 965 xpt_free_path(path); 966 mtx_unlock(&xsoftc.xpt_lock); 967 /* Install our software interrupt handlers */ 968 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih); 969 /* 970 * Register a callback for when interrupts are enabled. 971 */ 972 xsoftc.xpt_config_hook = 973 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook), 974 M_CAMXPT, M_NOWAIT | M_ZERO); 975 if (xsoftc.xpt_config_hook == NULL) { 976 printf("xpt_init: Cannot malloc config hook " 977 "- failing attach\n"); 978 return (ENOMEM); 979 } 980 xsoftc.xpt_config_hook->ich_func = xpt_config; 981 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) { 982 free (xsoftc.xpt_config_hook, M_CAMXPT); 983 printf("xpt_init: config_intrhook_establish failed " 984 "- failing attach\n"); 985 } 986 987 return (0); 988} 989 990static cam_status 991xptregister(struct cam_periph *periph, void *arg) 992{ 993 struct cam_sim *xpt_sim; 994 995 if (periph == NULL) { 996 printf("xptregister: periph was NULL!!\n"); 997 return(CAM_REQ_CMP_ERR); 998 } 999 1000 xpt_sim = (struct cam_sim *)arg; 1001 xpt_sim->softc = periph; 1002 xpt_periph = periph; 1003 periph->softc = NULL; 1004 1005 return(CAM_REQ_CMP); 1006} 1007 1008int32_t 1009xpt_add_periph(struct cam_periph *periph) 1010{ 1011 struct cam_ed *device; 1012 int32_t status; 1013 struct periph_list *periph_head; 1014 1015 mtx_assert(periph->sim->mtx, MA_OWNED); 1016 1017 device = periph->path->device; 1018 1019 periph_head = &device->periphs; 1020 1021 status = CAM_REQ_CMP; 1022 1023 if (device != NULL) { 1024 /* 1025 * Make room for this peripheral 1026 * so it will fit in the queue 1027 * when it's scheduled to run 1028 */ 1029 status = camq_resize(&device->drvq, 1030 device->drvq.array_size + 1); 1031 1032 device->generation++; 1033 1034 SLIST_INSERT_HEAD(periph_head, periph, periph_links); 1035 } 1036 1037 mtx_lock(&xsoftc.xpt_topo_lock); 1038 xsoftc.xpt_generation++; 1039 mtx_unlock(&xsoftc.xpt_topo_lock); 1040 1041 return (status); 1042} 1043 1044void 1045xpt_remove_periph(struct cam_periph *periph) 1046{ 1047 struct cam_ed *device; 1048 1049 mtx_assert(periph->sim->mtx, MA_OWNED); 1050 1051 device = periph->path->device; 1052 1053 if (device != NULL) { 1054 struct periph_list *periph_head; 1055 1056 periph_head = &device->periphs; 1057 1058 /* Release the slot for this peripheral */ 1059 camq_resize(&device->drvq, device->drvq.array_size - 1); 1060 1061 device->generation++; 1062 1063 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links); 1064 } 1065 1066 mtx_lock(&xsoftc.xpt_topo_lock); 1067 xsoftc.xpt_generation++; 1068 mtx_unlock(&xsoftc.xpt_topo_lock); 1069} 1070 1071 1072void 1073xpt_announce_periph(struct cam_periph *periph, char *announce_string) 1074{ 1075 struct cam_path *path = periph->path; 1076 1077 mtx_assert(periph->sim->mtx, MA_OWNED); 1078 1079 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n", 1080 periph->periph_name, periph->unit_number, 1081 path->bus->sim->sim_name, 1082 path->bus->sim->unit_number, 1083 path->bus->sim->bus_id, 1084 path->bus->path_id, 1085 path->target->target_id, 1086 path->device->lun_id); 1087 printf("%s%d: ", periph->periph_name, periph->unit_number); 1088 if (path->device->protocol == PROTO_SCSI) 1089 scsi_print_inquiry(&path->device->inq_data); 1090 else if (path->device->protocol == PROTO_ATA || 1091 path->device->protocol == PROTO_SATAPM) 1092 ata_print_ident(&path->device->ident_data); 1093 else 1094 printf("Unknown protocol device\n"); 1095 if (bootverbose && path->device->serial_num_len > 0) { 1096 /* Don't wrap the screen - print only the first 60 chars */ 1097 printf("%s%d: Serial Number %.60s\n", periph->periph_name, 1098 periph->unit_number, path->device->serial_num); 1099 } 1100 /* Announce transport details. */ 1101 (*(path->bus->xport->announce))(periph); 1102 /* Announce command queueing. */ 1103 if (path->device->inq_flags & SID_CmdQue 1104 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) { 1105 printf("%s%d: Command Queueing enabled\n", 1106 periph->periph_name, periph->unit_number); 1107 } 1108 /* Announce caller's details if they've passed in. */ 1109 if (announce_string != NULL) 1110 printf("%s%d: %s\n", periph->periph_name, 1111 periph->unit_number, announce_string); 1112} 1113 1114static dev_match_ret 1115xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns, 1116 struct cam_eb *bus) 1117{ 1118 dev_match_ret retval; 1119 int i; 1120 1121 retval = DM_RET_NONE; 1122 1123 /* 1124 * If we aren't given something to match against, that's an error. 1125 */ 1126 if (bus == NULL) 1127 return(DM_RET_ERROR); 1128 1129 /* 1130 * If there are no match entries, then this bus matches no 1131 * matter what. 1132 */ 1133 if ((patterns == NULL) || (num_patterns == 0)) 1134 return(DM_RET_DESCEND | DM_RET_COPY); 1135 1136 for (i = 0; i < num_patterns; i++) { 1137 struct bus_match_pattern *cur_pattern; 1138 1139 /* 1140 * If the pattern in question isn't for a bus node, we 1141 * aren't interested. However, we do indicate to the 1142 * calling routine that we should continue descending the 1143 * tree, since the user wants to match against lower-level 1144 * EDT elements. 1145 */ 1146 if (patterns[i].type != DEV_MATCH_BUS) { 1147 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1148 retval |= DM_RET_DESCEND; 1149 continue; 1150 } 1151 1152 cur_pattern = &patterns[i].pattern.bus_pattern; 1153 1154 /* 1155 * If they want to match any bus node, we give them any 1156 * device node. 1157 */ 1158 if (cur_pattern->flags == BUS_MATCH_ANY) { 1159 /* set the copy flag */ 1160 retval |= DM_RET_COPY; 1161 1162 /* 1163 * If we've already decided on an action, go ahead 1164 * and return. 1165 */ 1166 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1167 return(retval); 1168 } 1169 1170 /* 1171 * Not sure why someone would do this... 1172 */ 1173 if (cur_pattern->flags == BUS_MATCH_NONE) 1174 continue; 1175 1176 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0) 1177 && (cur_pattern->path_id != bus->path_id)) 1178 continue; 1179 1180 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0) 1181 && (cur_pattern->bus_id != bus->sim->bus_id)) 1182 continue; 1183 1184 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0) 1185 && (cur_pattern->unit_number != bus->sim->unit_number)) 1186 continue; 1187 1188 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0) 1189 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name, 1190 DEV_IDLEN) != 0)) 1191 continue; 1192 1193 /* 1194 * If we get to this point, the user definitely wants 1195 * information on this bus. So tell the caller to copy the 1196 * data out. 1197 */ 1198 retval |= DM_RET_COPY; 1199 1200 /* 1201 * If the return action has been set to descend, then we 1202 * know that we've already seen a non-bus matching 1203 * expression, therefore we need to further descend the tree. 1204 * This won't change by continuing around the loop, so we 1205 * go ahead and return. If we haven't seen a non-bus 1206 * matching expression, we keep going around the loop until 1207 * we exhaust the matching expressions. We'll set the stop 1208 * flag once we fall out of the loop. 1209 */ 1210 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1211 return(retval); 1212 } 1213 1214 /* 1215 * If the return action hasn't been set to descend yet, that means 1216 * we haven't seen anything other than bus matching patterns. So 1217 * tell the caller to stop descending the tree -- the user doesn't 1218 * want to match against lower level tree elements. 1219 */ 1220 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1221 retval |= DM_RET_STOP; 1222 1223 return(retval); 1224} 1225 1226static dev_match_ret 1227xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns, 1228 struct cam_ed *device) 1229{ 1230 dev_match_ret retval; 1231 int i; 1232 1233 retval = DM_RET_NONE; 1234 1235 /* 1236 * If we aren't given something to match against, that's an error. 1237 */ 1238 if (device == NULL) 1239 return(DM_RET_ERROR); 1240 1241 /* 1242 * If there are no match entries, then this device matches no 1243 * matter what. 1244 */ 1245 if ((patterns == NULL) || (num_patterns == 0)) 1246 return(DM_RET_DESCEND | DM_RET_COPY); 1247 1248 for (i = 0; i < num_patterns; i++) { 1249 struct device_match_pattern *cur_pattern; 1250 1251 /* 1252 * If the pattern in question isn't for a device node, we 1253 * aren't interested. 1254 */ 1255 if (patterns[i].type != DEV_MATCH_DEVICE) { 1256 if ((patterns[i].type == DEV_MATCH_PERIPH) 1257 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)) 1258 retval |= DM_RET_DESCEND; 1259 continue; 1260 } 1261 1262 cur_pattern = &patterns[i].pattern.device_pattern; 1263 1264 /* 1265 * If they want to match any device node, we give them any 1266 * device node. 1267 */ 1268 if (cur_pattern->flags == DEV_MATCH_ANY) { 1269 /* set the copy flag */ 1270 retval |= DM_RET_COPY; 1271 1272 1273 /* 1274 * If we've already decided on an action, go ahead 1275 * and return. 1276 */ 1277 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1278 return(retval); 1279 } 1280 1281 /* 1282 * Not sure why someone would do this... 1283 */ 1284 if (cur_pattern->flags == DEV_MATCH_NONE) 1285 continue; 1286 1287 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0) 1288 && (cur_pattern->path_id != device->target->bus->path_id)) 1289 continue; 1290 1291 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0) 1292 && (cur_pattern->target_id != device->target->target_id)) 1293 continue; 1294 1295 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0) 1296 && (cur_pattern->target_lun != device->lun_id)) 1297 continue; 1298 1299 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0) 1300 && (cam_quirkmatch((caddr_t)&device->inq_data, 1301 (caddr_t)&cur_pattern->inq_pat, 1302 1, sizeof(cur_pattern->inq_pat), 1303 scsi_static_inquiry_match) == NULL)) 1304 continue; 1305 1306 /* 1307 * If we get to this point, the user definitely wants 1308 * information on this device. So tell the caller to copy 1309 * the data out. 1310 */ 1311 retval |= DM_RET_COPY; 1312 1313 /* 1314 * If the return action has been set to descend, then we 1315 * know that we've already seen a peripheral matching 1316 * expression, therefore we need to further descend the tree. 1317 * This won't change by continuing around the loop, so we 1318 * go ahead and return. If we haven't seen a peripheral 1319 * matching expression, we keep going around the loop until 1320 * we exhaust the matching expressions. We'll set the stop 1321 * flag once we fall out of the loop. 1322 */ 1323 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1324 return(retval); 1325 } 1326 1327 /* 1328 * If the return action hasn't been set to descend yet, that means 1329 * we haven't seen any peripheral matching patterns. So tell the 1330 * caller to stop descending the tree -- the user doesn't want to 1331 * match against lower level tree elements. 1332 */ 1333 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1334 retval |= DM_RET_STOP; 1335 1336 return(retval); 1337} 1338 1339/* 1340 * Match a single peripheral against any number of match patterns. 1341 */ 1342static dev_match_ret 1343xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns, 1344 struct cam_periph *periph) 1345{ 1346 dev_match_ret retval; 1347 int i; 1348 1349 /* 1350 * If we aren't given something to match against, that's an error. 1351 */ 1352 if (periph == NULL) 1353 return(DM_RET_ERROR); 1354 1355 /* 1356 * If there are no match entries, then this peripheral matches no 1357 * matter what. 1358 */ 1359 if ((patterns == NULL) || (num_patterns == 0)) 1360 return(DM_RET_STOP | DM_RET_COPY); 1361 1362 /* 1363 * There aren't any nodes below a peripheral node, so there's no 1364 * reason to descend the tree any further. 1365 */ 1366 retval = DM_RET_STOP; 1367 1368 for (i = 0; i < num_patterns; i++) { 1369 struct periph_match_pattern *cur_pattern; 1370 1371 /* 1372 * If the pattern in question isn't for a peripheral, we 1373 * aren't interested. 1374 */ 1375 if (patterns[i].type != DEV_MATCH_PERIPH) 1376 continue; 1377 1378 cur_pattern = &patterns[i].pattern.periph_pattern; 1379 1380 /* 1381 * If they want to match on anything, then we will do so. 1382 */ 1383 if (cur_pattern->flags == PERIPH_MATCH_ANY) { 1384 /* set the copy flag */ 1385 retval |= DM_RET_COPY; 1386 1387 /* 1388 * We've already set the return action to stop, 1389 * since there are no nodes below peripherals in 1390 * the tree. 1391 */ 1392 return(retval); 1393 } 1394 1395 /* 1396 * Not sure why someone would do this... 1397 */ 1398 if (cur_pattern->flags == PERIPH_MATCH_NONE) 1399 continue; 1400 1401 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0) 1402 && (cur_pattern->path_id != periph->path->bus->path_id)) 1403 continue; 1404 1405 /* 1406 * For the target and lun id's, we have to make sure the 1407 * target and lun pointers aren't NULL. The xpt peripheral 1408 * has a wildcard target and device. 1409 */ 1410 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0) 1411 && ((periph->path->target == NULL) 1412 ||(cur_pattern->target_id != periph->path->target->target_id))) 1413 continue; 1414 1415 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0) 1416 && ((periph->path->device == NULL) 1417 || (cur_pattern->target_lun != periph->path->device->lun_id))) 1418 continue; 1419 1420 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0) 1421 && (cur_pattern->unit_number != periph->unit_number)) 1422 continue; 1423 1424 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0) 1425 && (strncmp(cur_pattern->periph_name, periph->periph_name, 1426 DEV_IDLEN) != 0)) 1427 continue; 1428 1429 /* 1430 * If we get to this point, the user definitely wants 1431 * information on this peripheral. So tell the caller to 1432 * copy the data out. 1433 */ 1434 retval |= DM_RET_COPY; 1435 1436 /* 1437 * The return action has already been set to stop, since 1438 * peripherals don't have any nodes below them in the EDT. 1439 */ 1440 return(retval); 1441 } 1442 1443 /* 1444 * If we get to this point, the peripheral that was passed in 1445 * doesn't match any of the patterns. 1446 */ 1447 return(retval); 1448} 1449 1450static int 1451xptedtbusfunc(struct cam_eb *bus, void *arg) 1452{ 1453 struct ccb_dev_match *cdm; 1454 dev_match_ret retval; 1455 1456 cdm = (struct ccb_dev_match *)arg; 1457 1458 /* 1459 * If our position is for something deeper in the tree, that means 1460 * that we've already seen this node. So, we keep going down. 1461 */ 1462 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1463 && (cdm->pos.cookie.bus == bus) 1464 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1465 && (cdm->pos.cookie.target != NULL)) 1466 retval = DM_RET_DESCEND; 1467 else 1468 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus); 1469 1470 /* 1471 * If we got an error, bail out of the search. 1472 */ 1473 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1474 cdm->status = CAM_DEV_MATCH_ERROR; 1475 return(0); 1476 } 1477 1478 /* 1479 * If the copy flag is set, copy this bus out. 1480 */ 1481 if (retval & DM_RET_COPY) { 1482 int spaceleft, j; 1483 1484 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1485 sizeof(struct dev_match_result)); 1486 1487 /* 1488 * If we don't have enough space to put in another 1489 * match result, save our position and tell the 1490 * user there are more devices to check. 1491 */ 1492 if (spaceleft < sizeof(struct dev_match_result)) { 1493 bzero(&cdm->pos, sizeof(cdm->pos)); 1494 cdm->pos.position_type = 1495 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS; 1496 1497 cdm->pos.cookie.bus = bus; 1498 cdm->pos.generations[CAM_BUS_GENERATION]= 1499 xsoftc.bus_generation; 1500 cdm->status = CAM_DEV_MATCH_MORE; 1501 return(0); 1502 } 1503 j = cdm->num_matches; 1504 cdm->num_matches++; 1505 cdm->matches[j].type = DEV_MATCH_BUS; 1506 cdm->matches[j].result.bus_result.path_id = bus->path_id; 1507 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id; 1508 cdm->matches[j].result.bus_result.unit_number = 1509 bus->sim->unit_number; 1510 strncpy(cdm->matches[j].result.bus_result.dev_name, 1511 bus->sim->sim_name, DEV_IDLEN); 1512 } 1513 1514 /* 1515 * If the user is only interested in busses, there's no 1516 * reason to descend to the next level in the tree. 1517 */ 1518 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 1519 return(1); 1520 1521 /* 1522 * If there is a target generation recorded, check it to 1523 * make sure the target list hasn't changed. 1524 */ 1525 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1526 && (bus == cdm->pos.cookie.bus) 1527 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1528 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0) 1529 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 1530 bus->generation)) { 1531 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1532 return(0); 1533 } 1534 1535 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1536 && (cdm->pos.cookie.bus == bus) 1537 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1538 && (cdm->pos.cookie.target != NULL)) 1539 return(xpttargettraverse(bus, 1540 (struct cam_et *)cdm->pos.cookie.target, 1541 xptedttargetfunc, arg)); 1542 else 1543 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg)); 1544} 1545 1546static int 1547xptedttargetfunc(struct cam_et *target, void *arg) 1548{ 1549 struct ccb_dev_match *cdm; 1550 1551 cdm = (struct ccb_dev_match *)arg; 1552 1553 /* 1554 * If there is a device list generation recorded, check it to 1555 * make sure the device list hasn't changed. 1556 */ 1557 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1558 && (cdm->pos.cookie.bus == target->bus) 1559 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1560 && (cdm->pos.cookie.target == target) 1561 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1562 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0) 1563 && (cdm->pos.generations[CAM_DEV_GENERATION] != 1564 target->generation)) { 1565 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1566 return(0); 1567 } 1568 1569 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1570 && (cdm->pos.cookie.bus == target->bus) 1571 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1572 && (cdm->pos.cookie.target == target) 1573 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1574 && (cdm->pos.cookie.device != NULL)) 1575 return(xptdevicetraverse(target, 1576 (struct cam_ed *)cdm->pos.cookie.device, 1577 xptedtdevicefunc, arg)); 1578 else 1579 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg)); 1580} 1581 1582static int 1583xptedtdevicefunc(struct cam_ed *device, void *arg) 1584{ 1585 1586 struct ccb_dev_match *cdm; 1587 dev_match_ret retval; 1588 1589 cdm = (struct ccb_dev_match *)arg; 1590 1591 /* 1592 * If our position is for something deeper in the tree, that means 1593 * that we've already seen this node. So, we keep going down. 1594 */ 1595 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1596 && (cdm->pos.cookie.device == device) 1597 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1598 && (cdm->pos.cookie.periph != NULL)) 1599 retval = DM_RET_DESCEND; 1600 else 1601 retval = xptdevicematch(cdm->patterns, cdm->num_patterns, 1602 device); 1603 1604 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1605 cdm->status = CAM_DEV_MATCH_ERROR; 1606 return(0); 1607 } 1608 1609 /* 1610 * If the copy flag is set, copy this device out. 1611 */ 1612 if (retval & DM_RET_COPY) { 1613 int spaceleft, j; 1614 1615 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1616 sizeof(struct dev_match_result)); 1617 1618 /* 1619 * If we don't have enough space to put in another 1620 * match result, save our position and tell the 1621 * user there are more devices to check. 1622 */ 1623 if (spaceleft < sizeof(struct dev_match_result)) { 1624 bzero(&cdm->pos, sizeof(cdm->pos)); 1625 cdm->pos.position_type = 1626 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 1627 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE; 1628 1629 cdm->pos.cookie.bus = device->target->bus; 1630 cdm->pos.generations[CAM_BUS_GENERATION]= 1631 xsoftc.bus_generation; 1632 cdm->pos.cookie.target = device->target; 1633 cdm->pos.generations[CAM_TARGET_GENERATION] = 1634 device->target->bus->generation; 1635 cdm->pos.cookie.device = device; 1636 cdm->pos.generations[CAM_DEV_GENERATION] = 1637 device->target->generation; 1638 cdm->status = CAM_DEV_MATCH_MORE; 1639 return(0); 1640 } 1641 j = cdm->num_matches; 1642 cdm->num_matches++; 1643 cdm->matches[j].type = DEV_MATCH_DEVICE; 1644 cdm->matches[j].result.device_result.path_id = 1645 device->target->bus->path_id; 1646 cdm->matches[j].result.device_result.target_id = 1647 device->target->target_id; 1648 cdm->matches[j].result.device_result.target_lun = 1649 device->lun_id; 1650 cdm->matches[j].result.device_result.protocol = 1651 device->protocol; 1652 bcopy(&device->inq_data, 1653 &cdm->matches[j].result.device_result.inq_data, 1654 sizeof(struct scsi_inquiry_data)); 1655 bcopy(&device->ident_data, 1656 &cdm->matches[j].result.device_result.ident_data, 1657 sizeof(struct ata_params)); 1658 1659 /* Let the user know whether this device is unconfigured */ 1660 if (device->flags & CAM_DEV_UNCONFIGURED) 1661 cdm->matches[j].result.device_result.flags = 1662 DEV_RESULT_UNCONFIGURED; 1663 else 1664 cdm->matches[j].result.device_result.flags = 1665 DEV_RESULT_NOFLAG; 1666 } 1667 1668 /* 1669 * If the user isn't interested in peripherals, don't descend 1670 * the tree any further. 1671 */ 1672 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 1673 return(1); 1674 1675 /* 1676 * If there is a peripheral list generation recorded, make sure 1677 * it hasn't changed. 1678 */ 1679 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1680 && (device->target->bus == cdm->pos.cookie.bus) 1681 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1682 && (device->target == cdm->pos.cookie.target) 1683 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1684 && (device == cdm->pos.cookie.device) 1685 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1686 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 1687 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1688 device->generation)){ 1689 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1690 return(0); 1691 } 1692 1693 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1694 && (cdm->pos.cookie.bus == device->target->bus) 1695 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1696 && (cdm->pos.cookie.target == device->target) 1697 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1698 && (cdm->pos.cookie.device == device) 1699 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1700 && (cdm->pos.cookie.periph != NULL)) 1701 return(xptperiphtraverse(device, 1702 (struct cam_periph *)cdm->pos.cookie.periph, 1703 xptedtperiphfunc, arg)); 1704 else 1705 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg)); 1706} 1707 1708static int 1709xptedtperiphfunc(struct cam_periph *periph, void *arg) 1710{ 1711 struct ccb_dev_match *cdm; 1712 dev_match_ret retval; 1713 1714 cdm = (struct ccb_dev_match *)arg; 1715 1716 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 1717 1718 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1719 cdm->status = CAM_DEV_MATCH_ERROR; 1720 return(0); 1721 } 1722 1723 /* 1724 * If the copy flag is set, copy this peripheral out. 1725 */ 1726 if (retval & DM_RET_COPY) { 1727 int spaceleft, j; 1728 1729 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1730 sizeof(struct dev_match_result)); 1731 1732 /* 1733 * If we don't have enough space to put in another 1734 * match result, save our position and tell the 1735 * user there are more devices to check. 1736 */ 1737 if (spaceleft < sizeof(struct dev_match_result)) { 1738 bzero(&cdm->pos, sizeof(cdm->pos)); 1739 cdm->pos.position_type = 1740 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 1741 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE | 1742 CAM_DEV_POS_PERIPH; 1743 1744 cdm->pos.cookie.bus = periph->path->bus; 1745 cdm->pos.generations[CAM_BUS_GENERATION]= 1746 xsoftc.bus_generation; 1747 cdm->pos.cookie.target = periph->path->target; 1748 cdm->pos.generations[CAM_TARGET_GENERATION] = 1749 periph->path->bus->generation; 1750 cdm->pos.cookie.device = periph->path->device; 1751 cdm->pos.generations[CAM_DEV_GENERATION] = 1752 periph->path->target->generation; 1753 cdm->pos.cookie.periph = periph; 1754 cdm->pos.generations[CAM_PERIPH_GENERATION] = 1755 periph->path->device->generation; 1756 cdm->status = CAM_DEV_MATCH_MORE; 1757 return(0); 1758 } 1759 1760 j = cdm->num_matches; 1761 cdm->num_matches++; 1762 cdm->matches[j].type = DEV_MATCH_PERIPH; 1763 cdm->matches[j].result.periph_result.path_id = 1764 periph->path->bus->path_id; 1765 cdm->matches[j].result.periph_result.target_id = 1766 periph->path->target->target_id; 1767 cdm->matches[j].result.periph_result.target_lun = 1768 periph->path->device->lun_id; 1769 cdm->matches[j].result.periph_result.unit_number = 1770 periph->unit_number; 1771 strncpy(cdm->matches[j].result.periph_result.periph_name, 1772 periph->periph_name, DEV_IDLEN); 1773 } 1774 1775 return(1); 1776} 1777 1778static int 1779xptedtmatch(struct ccb_dev_match *cdm) 1780{ 1781 int ret; 1782 1783 cdm->num_matches = 0; 1784 1785 /* 1786 * Check the bus list generation. If it has changed, the user 1787 * needs to reset everything and start over. 1788 */ 1789 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1790 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0) 1791 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) { 1792 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1793 return(0); 1794 } 1795 1796 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1797 && (cdm->pos.cookie.bus != NULL)) 1798 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus, 1799 xptedtbusfunc, cdm); 1800 else 1801 ret = xptbustraverse(NULL, xptedtbusfunc, cdm); 1802 1803 /* 1804 * If we get back 0, that means that we had to stop before fully 1805 * traversing the EDT. It also means that one of the subroutines 1806 * has set the status field to the proper value. If we get back 1, 1807 * we've fully traversed the EDT and copied out any matching entries. 1808 */ 1809 if (ret == 1) 1810 cdm->status = CAM_DEV_MATCH_LAST; 1811 1812 return(ret); 1813} 1814 1815static int 1816xptplistpdrvfunc(struct periph_driver **pdrv, void *arg) 1817{ 1818 struct ccb_dev_match *cdm; 1819 1820 cdm = (struct ccb_dev_match *)arg; 1821 1822 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1823 && (cdm->pos.cookie.pdrv == pdrv) 1824 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1825 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 1826 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1827 (*pdrv)->generation)) { 1828 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1829 return(0); 1830 } 1831 1832 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1833 && (cdm->pos.cookie.pdrv == pdrv) 1834 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1835 && (cdm->pos.cookie.periph != NULL)) 1836 return(xptpdperiphtraverse(pdrv, 1837 (struct cam_periph *)cdm->pos.cookie.periph, 1838 xptplistperiphfunc, arg)); 1839 else 1840 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg)); 1841} 1842 1843static int 1844xptplistperiphfunc(struct cam_periph *periph, void *arg) 1845{ 1846 struct ccb_dev_match *cdm; 1847 dev_match_ret retval; 1848 1849 cdm = (struct ccb_dev_match *)arg; 1850 1851 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 1852 1853 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1854 cdm->status = CAM_DEV_MATCH_ERROR; 1855 return(0); 1856 } 1857 1858 /* 1859 * If the copy flag is set, copy this peripheral out. 1860 */ 1861 if (retval & DM_RET_COPY) { 1862 int spaceleft, j; 1863 1864 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1865 sizeof(struct dev_match_result)); 1866 1867 /* 1868 * If we don't have enough space to put in another 1869 * match result, save our position and tell the 1870 * user there are more devices to check. 1871 */ 1872 if (spaceleft < sizeof(struct dev_match_result)) { 1873 struct periph_driver **pdrv; 1874 1875 pdrv = NULL; 1876 bzero(&cdm->pos, sizeof(cdm->pos)); 1877 cdm->pos.position_type = 1878 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR | 1879 CAM_DEV_POS_PERIPH; 1880 1881 /* 1882 * This may look a bit non-sensical, but it is 1883 * actually quite logical. There are very few 1884 * peripheral drivers, and bloating every peripheral 1885 * structure with a pointer back to its parent 1886 * peripheral driver linker set entry would cost 1887 * more in the long run than doing this quick lookup. 1888 */ 1889 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) { 1890 if (strcmp((*pdrv)->driver_name, 1891 periph->periph_name) == 0) 1892 break; 1893 } 1894 1895 if (*pdrv == NULL) { 1896 cdm->status = CAM_DEV_MATCH_ERROR; 1897 return(0); 1898 } 1899 1900 cdm->pos.cookie.pdrv = pdrv; 1901 /* 1902 * The periph generation slot does double duty, as 1903 * does the periph pointer slot. They are used for 1904 * both edt and pdrv lookups and positioning. 1905 */ 1906 cdm->pos.cookie.periph = periph; 1907 cdm->pos.generations[CAM_PERIPH_GENERATION] = 1908 (*pdrv)->generation; 1909 cdm->status = CAM_DEV_MATCH_MORE; 1910 return(0); 1911 } 1912 1913 j = cdm->num_matches; 1914 cdm->num_matches++; 1915 cdm->matches[j].type = DEV_MATCH_PERIPH; 1916 cdm->matches[j].result.periph_result.path_id = 1917 periph->path->bus->path_id; 1918 1919 /* 1920 * The transport layer peripheral doesn't have a target or 1921 * lun. 1922 */ 1923 if (periph->path->target) 1924 cdm->matches[j].result.periph_result.target_id = 1925 periph->path->target->target_id; 1926 else 1927 cdm->matches[j].result.periph_result.target_id = -1; 1928 1929 if (periph->path->device) 1930 cdm->matches[j].result.periph_result.target_lun = 1931 periph->path->device->lun_id; 1932 else 1933 cdm->matches[j].result.periph_result.target_lun = -1; 1934 1935 cdm->matches[j].result.periph_result.unit_number = 1936 periph->unit_number; 1937 strncpy(cdm->matches[j].result.periph_result.periph_name, 1938 periph->periph_name, DEV_IDLEN); 1939 } 1940 1941 return(1); 1942} 1943 1944static int 1945xptperiphlistmatch(struct ccb_dev_match *cdm) 1946{ 1947 int ret; 1948 1949 cdm->num_matches = 0; 1950 1951 /* 1952 * At this point in the edt traversal function, we check the bus 1953 * list generation to make sure that no busses have been added or 1954 * removed since the user last sent a XPT_DEV_MATCH ccb through. 1955 * For the peripheral driver list traversal function, however, we 1956 * don't have to worry about new peripheral driver types coming or 1957 * going; they're in a linker set, and therefore can't change 1958 * without a recompile. 1959 */ 1960 1961 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1962 && (cdm->pos.cookie.pdrv != NULL)) 1963 ret = xptpdrvtraverse( 1964 (struct periph_driver **)cdm->pos.cookie.pdrv, 1965 xptplistpdrvfunc, cdm); 1966 else 1967 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm); 1968 1969 /* 1970 * If we get back 0, that means that we had to stop before fully 1971 * traversing the peripheral driver tree. It also means that one of 1972 * the subroutines has set the status field to the proper value. If 1973 * we get back 1, we've fully traversed the EDT and copied out any 1974 * matching entries. 1975 */ 1976 if (ret == 1) 1977 cdm->status = CAM_DEV_MATCH_LAST; 1978 1979 return(ret); 1980} 1981 1982static int 1983xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg) 1984{ 1985 struct cam_eb *bus, *next_bus; 1986 int retval; 1987 1988 retval = 1; 1989 1990 mtx_lock(&xsoftc.xpt_topo_lock); 1991 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses)); 1992 bus != NULL; 1993 bus = next_bus) { 1994 next_bus = TAILQ_NEXT(bus, links); 1995 1996 mtx_unlock(&xsoftc.xpt_topo_lock); 1997 CAM_SIM_LOCK(bus->sim); 1998 retval = tr_func(bus, arg); 1999 CAM_SIM_UNLOCK(bus->sim); 2000 if (retval == 0) 2001 return(retval); 2002 mtx_lock(&xsoftc.xpt_topo_lock); 2003 } 2004 mtx_unlock(&xsoftc.xpt_topo_lock); 2005 2006 return(retval); 2007} 2008 2009int 2010xpt_sim_opened(struct cam_sim *sim) 2011{ 2012 struct cam_eb *bus; 2013 struct cam_et *target; 2014 struct cam_ed *device; 2015 struct cam_periph *periph; 2016 2017 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 2018 mtx_assert(sim->mtx, MA_OWNED); 2019 2020 mtx_lock(&xsoftc.xpt_topo_lock); 2021 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) { 2022 if (bus->sim != sim) 2023 continue; 2024 2025 TAILQ_FOREACH(target, &bus->et_entries, links) { 2026 TAILQ_FOREACH(device, &target->ed_entries, links) { 2027 SLIST_FOREACH(periph, &device->periphs, 2028 periph_links) { 2029 if (periph->refcount > 0) { 2030 mtx_unlock(&xsoftc.xpt_topo_lock); 2031 return (1); 2032 } 2033 } 2034 } 2035 } 2036 } 2037 2038 mtx_unlock(&xsoftc.xpt_topo_lock); 2039 return (0); 2040} 2041 2042static int 2043xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target, 2044 xpt_targetfunc_t *tr_func, void *arg) 2045{ 2046 struct cam_et *target, *next_target; 2047 int retval; 2048 2049 retval = 1; 2050 for (target = (start_target ? start_target : 2051 TAILQ_FIRST(&bus->et_entries)); 2052 target != NULL; target = next_target) { 2053 2054 next_target = TAILQ_NEXT(target, links); 2055 2056 retval = tr_func(target, arg); 2057 2058 if (retval == 0) 2059 return(retval); 2060 } 2061 2062 return(retval); 2063} 2064 2065static int 2066xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device, 2067 xpt_devicefunc_t *tr_func, void *arg) 2068{ 2069 struct cam_ed *device, *next_device; 2070 int retval; 2071 2072 retval = 1; 2073 for (device = (start_device ? start_device : 2074 TAILQ_FIRST(&target->ed_entries)); 2075 device != NULL; 2076 device = next_device) { 2077 2078 next_device = TAILQ_NEXT(device, links); 2079 2080 retval = tr_func(device, arg); 2081 2082 if (retval == 0) 2083 return(retval); 2084 } 2085 2086 return(retval); 2087} 2088 2089static int 2090xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph, 2091 xpt_periphfunc_t *tr_func, void *arg) 2092{ 2093 struct cam_periph *periph, *next_periph; 2094 int retval; 2095 2096 retval = 1; 2097 2098 for (periph = (start_periph ? start_periph : 2099 SLIST_FIRST(&device->periphs)); 2100 periph != NULL; 2101 periph = next_periph) { 2102 2103 next_periph = SLIST_NEXT(periph, periph_links); 2104 2105 retval = tr_func(periph, arg); 2106 if (retval == 0) 2107 return(retval); 2108 } 2109 2110 return(retval); 2111} 2112 2113static int 2114xptpdrvtraverse(struct periph_driver **start_pdrv, 2115 xpt_pdrvfunc_t *tr_func, void *arg) 2116{ 2117 struct periph_driver **pdrv; 2118 int retval; 2119 2120 retval = 1; 2121 2122 /* 2123 * We don't traverse the peripheral driver list like we do the 2124 * other lists, because it is a linker set, and therefore cannot be 2125 * changed during runtime. If the peripheral driver list is ever 2126 * re-done to be something other than a linker set (i.e. it can 2127 * change while the system is running), the list traversal should 2128 * be modified to work like the other traversal functions. 2129 */ 2130 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers); 2131 *pdrv != NULL; pdrv++) { 2132 retval = tr_func(pdrv, arg); 2133 2134 if (retval == 0) 2135 return(retval); 2136 } 2137 2138 return(retval); 2139} 2140 2141static int 2142xptpdperiphtraverse(struct periph_driver **pdrv, 2143 struct cam_periph *start_periph, 2144 xpt_periphfunc_t *tr_func, void *arg) 2145{ 2146 struct cam_periph *periph, *next_periph; 2147 int retval; 2148 2149 retval = 1; 2150 2151 xpt_lock_buses(); 2152 for (periph = (start_periph ? start_periph : 2153 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL; 2154 periph = next_periph) { 2155 2156 next_periph = TAILQ_NEXT(periph, unit_links); 2157 2158 retval = tr_func(periph, arg); 2159 if (retval == 0) { 2160 xpt_unlock_buses(); 2161 return(retval); 2162 } 2163 } 2164 xpt_unlock_buses(); 2165 return(retval); 2166} 2167 2168static int 2169xptdefbusfunc(struct cam_eb *bus, void *arg) 2170{ 2171 struct xpt_traverse_config *tr_config; 2172 2173 tr_config = (struct xpt_traverse_config *)arg; 2174 2175 if (tr_config->depth == XPT_DEPTH_BUS) { 2176 xpt_busfunc_t *tr_func; 2177 2178 tr_func = (xpt_busfunc_t *)tr_config->tr_func; 2179 2180 return(tr_func(bus, tr_config->tr_arg)); 2181 } else 2182 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg)); 2183} 2184 2185static int 2186xptdeftargetfunc(struct cam_et *target, void *arg) 2187{ 2188 struct xpt_traverse_config *tr_config; 2189 2190 tr_config = (struct xpt_traverse_config *)arg; 2191 2192 if (tr_config->depth == XPT_DEPTH_TARGET) { 2193 xpt_targetfunc_t *tr_func; 2194 2195 tr_func = (xpt_targetfunc_t *)tr_config->tr_func; 2196 2197 return(tr_func(target, tr_config->tr_arg)); 2198 } else 2199 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg)); 2200} 2201 2202static int 2203xptdefdevicefunc(struct cam_ed *device, void *arg) 2204{ 2205 struct xpt_traverse_config *tr_config; 2206 2207 tr_config = (struct xpt_traverse_config *)arg; 2208 2209 if (tr_config->depth == XPT_DEPTH_DEVICE) { 2210 xpt_devicefunc_t *tr_func; 2211 2212 tr_func = (xpt_devicefunc_t *)tr_config->tr_func; 2213 2214 return(tr_func(device, tr_config->tr_arg)); 2215 } else 2216 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg)); 2217} 2218 2219static int 2220xptdefperiphfunc(struct cam_periph *periph, void *arg) 2221{ 2222 struct xpt_traverse_config *tr_config; 2223 xpt_periphfunc_t *tr_func; 2224 2225 tr_config = (struct xpt_traverse_config *)arg; 2226 2227 tr_func = (xpt_periphfunc_t *)tr_config->tr_func; 2228 2229 /* 2230 * Unlike the other default functions, we don't check for depth 2231 * here. The peripheral driver level is the last level in the EDT, 2232 * so if we're here, we should execute the function in question. 2233 */ 2234 return(tr_func(periph, tr_config->tr_arg)); 2235} 2236 2237/* 2238 * Execute the given function for every bus in the EDT. 2239 */ 2240static int 2241xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg) 2242{ 2243 struct xpt_traverse_config tr_config; 2244 2245 tr_config.depth = XPT_DEPTH_BUS; 2246 tr_config.tr_func = tr_func; 2247 tr_config.tr_arg = arg; 2248 2249 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2250} 2251 2252/* 2253 * Execute the given function for every device in the EDT. 2254 */ 2255static int 2256xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg) 2257{ 2258 struct xpt_traverse_config tr_config; 2259 2260 tr_config.depth = XPT_DEPTH_DEVICE; 2261 tr_config.tr_func = tr_func; 2262 tr_config.tr_arg = arg; 2263 2264 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2265} 2266 2267static int 2268xptsetasyncfunc(struct cam_ed *device, void *arg) 2269{ 2270 struct cam_path path; 2271 struct ccb_getdev cgd; 2272 struct ccb_setasync *csa = (struct ccb_setasync *)arg; 2273 2274 /* 2275 * Don't report unconfigured devices (Wildcard devs, 2276 * devices only for target mode, device instances 2277 * that have been invalidated but are waiting for 2278 * their last reference count to be released). 2279 */ 2280 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0) 2281 return (1); 2282 2283 xpt_compile_path(&path, 2284 NULL, 2285 device->target->bus->path_id, 2286 device->target->target_id, 2287 device->lun_id); 2288 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL); 2289 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2290 xpt_action((union ccb *)&cgd); 2291 csa->callback(csa->callback_arg, 2292 AC_FOUND_DEVICE, 2293 &path, &cgd); 2294 xpt_release_path(&path); 2295 2296 return(1); 2297} 2298 2299static int 2300xptsetasyncbusfunc(struct cam_eb *bus, void *arg) 2301{ 2302 struct cam_path path; 2303 struct ccb_pathinq cpi; 2304 struct ccb_setasync *csa = (struct ccb_setasync *)arg; 2305 2306 xpt_compile_path(&path, /*periph*/NULL, 2307 bus->sim->path_id, 2308 CAM_TARGET_WILDCARD, 2309 CAM_LUN_WILDCARD); 2310 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL); 2311 cpi.ccb_h.func_code = XPT_PATH_INQ; 2312 xpt_action((union ccb *)&cpi); 2313 csa->callback(csa->callback_arg, 2314 AC_PATH_REGISTERED, 2315 &path, &cpi); 2316 xpt_release_path(&path); 2317 2318 return(1); 2319} 2320 2321void 2322xpt_action(union ccb *start_ccb) 2323{ 2324 2325 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n")); 2326 2327 start_ccb->ccb_h.status = CAM_REQ_INPROG; 2328 /* Compatibility for RL-unaware code. */ 2329 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0) 2330 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1; 2331 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb); 2332} 2333 2334void 2335xpt_action_default(union ccb *start_ccb) 2336{ 2337 2338 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action_default\n")); 2339 2340 switch (start_ccb->ccb_h.func_code) { 2341 case XPT_SCSI_IO: 2342 { 2343 struct cam_ed *device; 2344#ifdef CAMDEBUG 2345 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 2346 struct cam_path *path; 2347 2348 path = start_ccb->ccb_h.path; 2349#endif 2350 2351 /* 2352 * For the sake of compatibility with SCSI-1 2353 * devices that may not understand the identify 2354 * message, we include lun information in the 2355 * second byte of all commands. SCSI-1 specifies 2356 * that luns are a 3 bit value and reserves only 3 2357 * bits for lun information in the CDB. Later 2358 * revisions of the SCSI spec allow for more than 8 2359 * luns, but have deprecated lun information in the 2360 * CDB. So, if the lun won't fit, we must omit. 2361 * 2362 * Also be aware that during initial probing for devices, 2363 * the inquiry information is unknown but initialized to 0. 2364 * This means that this code will be exercised while probing 2365 * devices with an ANSI revision greater than 2. 2366 */ 2367 device = start_ccb->ccb_h.path->device; 2368 if (device->protocol_version <= SCSI_REV_2 2369 && start_ccb->ccb_h.target_lun < 8 2370 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) { 2371 2372 start_ccb->csio.cdb_io.cdb_bytes[1] |= 2373 start_ccb->ccb_h.target_lun << 5; 2374 } 2375 start_ccb->csio.scsi_status = SCSI_STATUS_OK; 2376 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n", 2377 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0], 2378 &path->device->inq_data), 2379 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes, 2380 cdb_str, sizeof(cdb_str)))); 2381 } 2382 /* FALLTHROUGH */ 2383 case XPT_TARGET_IO: 2384 case XPT_CONT_TARGET_IO: 2385 start_ccb->csio.sense_resid = 0; 2386 start_ccb->csio.resid = 0; 2387 /* FALLTHROUGH */ 2388 case XPT_ATA_IO: 2389 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) { 2390 start_ccb->ataio.resid = 0; 2391 } 2392 /* FALLTHROUGH */ 2393 case XPT_RESET_DEV: 2394 case XPT_ENG_EXEC: 2395 { 2396 struct cam_path *path = start_ccb->ccb_h.path; 2397 int frozen; 2398 2399 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb); 2400 path->device->sim->devq->alloc_openings += frozen; 2401 if (frozen > 0) 2402 xpt_run_dev_allocq(path->bus); 2403 if (xpt_schedule_dev_sendq(path->bus, path->device)) 2404 xpt_run_dev_sendq(path->bus); 2405 break; 2406 } 2407 case XPT_CALC_GEOMETRY: 2408 { 2409 struct cam_sim *sim; 2410 2411 /* Filter out garbage */ 2412 if (start_ccb->ccg.block_size == 0 2413 || start_ccb->ccg.volume_size == 0) { 2414 start_ccb->ccg.cylinders = 0; 2415 start_ccb->ccg.heads = 0; 2416 start_ccb->ccg.secs_per_track = 0; 2417 start_ccb->ccb_h.status = CAM_REQ_CMP; 2418 break; 2419 } 2420#ifdef PC98 2421 /* 2422 * In a PC-98 system, geometry translation depens on 2423 * the "real" device geometry obtained from mode page 4. 2424 * SCSI geometry translation is performed in the 2425 * initialization routine of the SCSI BIOS and the result 2426 * stored in host memory. If the translation is available 2427 * in host memory, use it. If not, rely on the default 2428 * translation the device driver performs. 2429 */ 2430 if (scsi_da_bios_params(&start_ccb->ccg) != 0) { 2431 start_ccb->ccb_h.status = CAM_REQ_CMP; 2432 break; 2433 } 2434#endif 2435 sim = start_ccb->ccb_h.path->bus->sim; 2436 (*(sim->sim_action))(sim, start_ccb); 2437 break; 2438 } 2439 case XPT_ABORT: 2440 { 2441 union ccb* abort_ccb; 2442 2443 abort_ccb = start_ccb->cab.abort_ccb; 2444 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) { 2445 2446 if (abort_ccb->ccb_h.pinfo.index >= 0) { 2447 struct cam_ccbq *ccbq; 2448 struct cam_ed *device; 2449 2450 device = abort_ccb->ccb_h.path->device; 2451 ccbq = &device->ccbq; 2452 device->sim->devq->alloc_openings -= 2453 cam_ccbq_remove_ccb(ccbq, abort_ccb); 2454 abort_ccb->ccb_h.status = 2455 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2456 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2457 xpt_done(abort_ccb); 2458 start_ccb->ccb_h.status = CAM_REQ_CMP; 2459 break; 2460 } 2461 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX 2462 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) { 2463 /* 2464 * We've caught this ccb en route to 2465 * the SIM. Flag it for abort and the 2466 * SIM will do so just before starting 2467 * real work on the CCB. 2468 */ 2469 abort_ccb->ccb_h.status = 2470 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2471 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2472 start_ccb->ccb_h.status = CAM_REQ_CMP; 2473 break; 2474 } 2475 } 2476 if (XPT_FC_IS_QUEUED(abort_ccb) 2477 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) { 2478 /* 2479 * It's already completed but waiting 2480 * for our SWI to get to it. 2481 */ 2482 start_ccb->ccb_h.status = CAM_UA_ABORT; 2483 break; 2484 } 2485 /* 2486 * If we weren't able to take care of the abort request 2487 * in the XPT, pass the request down to the SIM for processing. 2488 */ 2489 } 2490 /* FALLTHROUGH */ 2491 case XPT_ACCEPT_TARGET_IO: 2492 case XPT_EN_LUN: 2493 case XPT_IMMED_NOTIFY: 2494 case XPT_NOTIFY_ACK: 2495 case XPT_RESET_BUS: 2496 case XPT_IMMEDIATE_NOTIFY: 2497 case XPT_NOTIFY_ACKNOWLEDGE: 2498 case XPT_GET_SIM_KNOB: 2499 case XPT_SET_SIM_KNOB: 2500 { 2501 struct cam_sim *sim; 2502 2503 sim = start_ccb->ccb_h.path->bus->sim; 2504 (*(sim->sim_action))(sim, start_ccb); 2505 break; 2506 } 2507 case XPT_PATH_INQ: 2508 { 2509 struct cam_sim *sim; 2510 2511 sim = start_ccb->ccb_h.path->bus->sim; 2512 (*(sim->sim_action))(sim, start_ccb); 2513 break; 2514 } 2515 case XPT_PATH_STATS: 2516 start_ccb->cpis.last_reset = 2517 start_ccb->ccb_h.path->bus->last_reset; 2518 start_ccb->ccb_h.status = CAM_REQ_CMP; 2519 break; 2520 case XPT_GDEV_TYPE: 2521 { 2522 struct cam_ed *dev; 2523 2524 dev = start_ccb->ccb_h.path->device; 2525 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2526 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2527 } else { 2528 struct ccb_getdev *cgd; 2529 struct cam_eb *bus; 2530 struct cam_et *tar; 2531 2532 cgd = &start_ccb->cgd; 2533 bus = cgd->ccb_h.path->bus; 2534 tar = cgd->ccb_h.path->target; 2535 cgd->protocol = dev->protocol; 2536 cgd->inq_data = dev->inq_data; 2537 cgd->ident_data = dev->ident_data; 2538 cgd->inq_flags = dev->inq_flags; 2539 cgd->ccb_h.status = CAM_REQ_CMP; 2540 cgd->serial_num_len = dev->serial_num_len; 2541 if ((dev->serial_num_len > 0) 2542 && (dev->serial_num != NULL)) 2543 bcopy(dev->serial_num, cgd->serial_num, 2544 dev->serial_num_len); 2545 } 2546 break; 2547 } 2548 case XPT_GDEV_STATS: 2549 { 2550 struct cam_ed *dev; 2551 2552 dev = start_ccb->ccb_h.path->device; 2553 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2554 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2555 } else { 2556 struct ccb_getdevstats *cgds; 2557 struct cam_eb *bus; 2558 struct cam_et *tar; 2559 2560 cgds = &start_ccb->cgds; 2561 bus = cgds->ccb_h.path->bus; 2562 tar = cgds->ccb_h.path->target; 2563 cgds->dev_openings = dev->ccbq.dev_openings; 2564 cgds->dev_active = dev->ccbq.dev_active; 2565 cgds->devq_openings = dev->ccbq.devq_openings; 2566 cgds->devq_queued = dev->ccbq.queue.entries; 2567 cgds->held = dev->ccbq.held; 2568 cgds->last_reset = tar->last_reset; 2569 cgds->maxtags = dev->maxtags; 2570 cgds->mintags = dev->mintags; 2571 if (timevalcmp(&tar->last_reset, &bus->last_reset, <)) 2572 cgds->last_reset = bus->last_reset; 2573 cgds->ccb_h.status = CAM_REQ_CMP; 2574 } 2575 break; 2576 } 2577 case XPT_GDEVLIST: 2578 { 2579 struct cam_periph *nperiph; 2580 struct periph_list *periph_head; 2581 struct ccb_getdevlist *cgdl; 2582 u_int i; 2583 struct cam_ed *device; 2584 int found; 2585 2586 2587 found = 0; 2588 2589 /* 2590 * Don't want anyone mucking with our data. 2591 */ 2592 device = start_ccb->ccb_h.path->device; 2593 periph_head = &device->periphs; 2594 cgdl = &start_ccb->cgdl; 2595 2596 /* 2597 * Check and see if the list has changed since the user 2598 * last requested a list member. If so, tell them that the 2599 * list has changed, and therefore they need to start over 2600 * from the beginning. 2601 */ 2602 if ((cgdl->index != 0) && 2603 (cgdl->generation != device->generation)) { 2604 cgdl->status = CAM_GDEVLIST_LIST_CHANGED; 2605 break; 2606 } 2607 2608 /* 2609 * Traverse the list of peripherals and attempt to find 2610 * the requested peripheral. 2611 */ 2612 for (nperiph = SLIST_FIRST(periph_head), i = 0; 2613 (nperiph != NULL) && (i <= cgdl->index); 2614 nperiph = SLIST_NEXT(nperiph, periph_links), i++) { 2615 if (i == cgdl->index) { 2616 strncpy(cgdl->periph_name, 2617 nperiph->periph_name, 2618 DEV_IDLEN); 2619 cgdl->unit_number = nperiph->unit_number; 2620 found = 1; 2621 } 2622 } 2623 if (found == 0) { 2624 cgdl->status = CAM_GDEVLIST_ERROR; 2625 break; 2626 } 2627 2628 if (nperiph == NULL) 2629 cgdl->status = CAM_GDEVLIST_LAST_DEVICE; 2630 else 2631 cgdl->status = CAM_GDEVLIST_MORE_DEVS; 2632 2633 cgdl->index++; 2634 cgdl->generation = device->generation; 2635 2636 cgdl->ccb_h.status = CAM_REQ_CMP; 2637 break; 2638 } 2639 case XPT_DEV_MATCH: 2640 { 2641 dev_pos_type position_type; 2642 struct ccb_dev_match *cdm; 2643 2644 cdm = &start_ccb->cdm; 2645 2646 /* 2647 * There are two ways of getting at information in the EDT. 2648 * The first way is via the primary EDT tree. It starts 2649 * with a list of busses, then a list of targets on a bus, 2650 * then devices/luns on a target, and then peripherals on a 2651 * device/lun. The "other" way is by the peripheral driver 2652 * lists. The peripheral driver lists are organized by 2653 * peripheral driver. (obviously) So it makes sense to 2654 * use the peripheral driver list if the user is looking 2655 * for something like "da1", or all "da" devices. If the 2656 * user is looking for something on a particular bus/target 2657 * or lun, it's generally better to go through the EDT tree. 2658 */ 2659 2660 if (cdm->pos.position_type != CAM_DEV_POS_NONE) 2661 position_type = cdm->pos.position_type; 2662 else { 2663 u_int i; 2664 2665 position_type = CAM_DEV_POS_NONE; 2666 2667 for (i = 0; i < cdm->num_patterns; i++) { 2668 if ((cdm->patterns[i].type == DEV_MATCH_BUS) 2669 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){ 2670 position_type = CAM_DEV_POS_EDT; 2671 break; 2672 } 2673 } 2674 2675 if (cdm->num_patterns == 0) 2676 position_type = CAM_DEV_POS_EDT; 2677 else if (position_type == CAM_DEV_POS_NONE) 2678 position_type = CAM_DEV_POS_PDRV; 2679 } 2680 2681 switch(position_type & CAM_DEV_POS_TYPEMASK) { 2682 case CAM_DEV_POS_EDT: 2683 xptedtmatch(cdm); 2684 break; 2685 case CAM_DEV_POS_PDRV: 2686 xptperiphlistmatch(cdm); 2687 break; 2688 default: 2689 cdm->status = CAM_DEV_MATCH_ERROR; 2690 break; 2691 } 2692 2693 if (cdm->status == CAM_DEV_MATCH_ERROR) 2694 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2695 else 2696 start_ccb->ccb_h.status = CAM_REQ_CMP; 2697 2698 break; 2699 } 2700 case XPT_SASYNC_CB: 2701 { 2702 struct ccb_setasync *csa; 2703 struct async_node *cur_entry; 2704 struct async_list *async_head; 2705 u_int32_t added; 2706 2707 csa = &start_ccb->csa; 2708 added = csa->event_enable; 2709 async_head = &csa->ccb_h.path->device->asyncs; 2710 2711 /* 2712 * If there is already an entry for us, simply 2713 * update it. 2714 */ 2715 cur_entry = SLIST_FIRST(async_head); 2716 while (cur_entry != NULL) { 2717 if ((cur_entry->callback_arg == csa->callback_arg) 2718 && (cur_entry->callback == csa->callback)) 2719 break; 2720 cur_entry = SLIST_NEXT(cur_entry, links); 2721 } 2722 2723 if (cur_entry != NULL) { 2724 /* 2725 * If the request has no flags set, 2726 * remove the entry. 2727 */ 2728 added &= ~cur_entry->event_enable; 2729 if (csa->event_enable == 0) { 2730 SLIST_REMOVE(async_head, cur_entry, 2731 async_node, links); 2732 xpt_release_device(csa->ccb_h.path->device); 2733 free(cur_entry, M_CAMXPT); 2734 } else { 2735 cur_entry->event_enable = csa->event_enable; 2736 } 2737 csa->event_enable = added; 2738 } else { 2739 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT, 2740 M_NOWAIT); 2741 if (cur_entry == NULL) { 2742 csa->ccb_h.status = CAM_RESRC_UNAVAIL; 2743 break; 2744 } 2745 cur_entry->event_enable = csa->event_enable; 2746 cur_entry->callback_arg = csa->callback_arg; 2747 cur_entry->callback = csa->callback; 2748 SLIST_INSERT_HEAD(async_head, cur_entry, links); 2749 xpt_acquire_device(csa->ccb_h.path->device); 2750 } 2751 start_ccb->ccb_h.status = CAM_REQ_CMP; 2752 break; 2753 } 2754 case XPT_REL_SIMQ: 2755 { 2756 struct ccb_relsim *crs; 2757 struct cam_ed *dev; 2758 2759 crs = &start_ccb->crs; 2760 dev = crs->ccb_h.path->device; 2761 if (dev == NULL) { 2762 2763 crs->ccb_h.status = CAM_DEV_NOT_THERE; 2764 break; 2765 } 2766 2767 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) { 2768 2769 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) { 2770 /* Don't ever go below one opening */ 2771 if (crs->openings > 0) { 2772 xpt_dev_ccbq_resize(crs->ccb_h.path, 2773 crs->openings); 2774 2775 if (bootverbose) { 2776 xpt_print(crs->ccb_h.path, 2777 "tagged openings now %d\n", 2778 crs->openings); 2779 } 2780 } 2781 } 2782 } 2783 2784 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) { 2785 2786 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 2787 2788 /* 2789 * Just extend the old timeout and decrement 2790 * the freeze count so that a single timeout 2791 * is sufficient for releasing the queue. 2792 */ 2793 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2794 callout_stop(&dev->callout); 2795 } else { 2796 2797 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2798 } 2799 2800 callout_reset(&dev->callout, 2801 (crs->release_timeout * hz) / 1000, 2802 xpt_release_devq_timeout, dev); 2803 2804 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING; 2805 2806 } 2807 2808 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) { 2809 2810 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) { 2811 /* 2812 * Decrement the freeze count so that a single 2813 * completion is still sufficient to unfreeze 2814 * the queue. 2815 */ 2816 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2817 } else { 2818 2819 dev->flags |= CAM_DEV_REL_ON_COMPLETE; 2820 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2821 } 2822 } 2823 2824 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) { 2825 2826 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 2827 || (dev->ccbq.dev_active == 0)) { 2828 2829 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2830 } else { 2831 2832 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY; 2833 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2834 } 2835 } 2836 2837 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) { 2838 xpt_release_devq_rl(crs->ccb_h.path, /*runlevel*/ 2839 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ? 2840 crs->release_timeout : 0, 2841 /*count*/1, /*run_queue*/TRUE); 2842 } 2843 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0]; 2844 start_ccb->ccb_h.status = CAM_REQ_CMP; 2845 break; 2846 } 2847 case XPT_DEBUG: { 2848#ifdef CAMDEBUG 2849#ifdef CAM_DEBUG_DELAY 2850 cam_debug_delay = CAM_DEBUG_DELAY; 2851#endif 2852 cam_dflags = start_ccb->cdbg.flags; 2853 if (cam_dpath != NULL) { 2854 xpt_free_path(cam_dpath); 2855 cam_dpath = NULL; 2856 } 2857 2858 if (cam_dflags != CAM_DEBUG_NONE) { 2859 if (xpt_create_path(&cam_dpath, xpt_periph, 2860 start_ccb->ccb_h.path_id, 2861 start_ccb->ccb_h.target_id, 2862 start_ccb->ccb_h.target_lun) != 2863 CAM_REQ_CMP) { 2864 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2865 cam_dflags = CAM_DEBUG_NONE; 2866 } else { 2867 start_ccb->ccb_h.status = CAM_REQ_CMP; 2868 xpt_print(cam_dpath, "debugging flags now %x\n", 2869 cam_dflags); 2870 } 2871 } else { 2872 cam_dpath = NULL; 2873 start_ccb->ccb_h.status = CAM_REQ_CMP; 2874 } 2875#else /* !CAMDEBUG */ 2876 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2877#endif /* CAMDEBUG */ 2878 break; 2879 } 2880 case XPT_FREEZE_QUEUE: 2881 { 2882 struct ccb_relsim *crs = &start_ccb->crs; 2883 2884 xpt_freeze_devq_rl(crs->ccb_h.path, /*runlevel*/ 2885 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ? 2886 crs->release_timeout : 0, /*count*/1); 2887 start_ccb->ccb_h.status = CAM_REQ_CMP; 2888 break; 2889 } 2890 case XPT_NOOP: 2891 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) 2892 xpt_freeze_devq(start_ccb->ccb_h.path, 1); 2893 start_ccb->ccb_h.status = CAM_REQ_CMP; 2894 break; 2895 default: 2896 case XPT_SDEV_TYPE: 2897 case XPT_TERM_IO: 2898 case XPT_ENG_INQ: 2899 /* XXX Implement */ 2900 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL; 2901 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) { 2902 xpt_done(start_ccb); 2903 } 2904 break; 2905 } 2906} 2907 2908void 2909xpt_polled_action(union ccb *start_ccb) 2910{ 2911 u_int32_t timeout; 2912 struct cam_sim *sim; 2913 struct cam_devq *devq; 2914 struct cam_ed *dev; 2915 2916 2917 timeout = start_ccb->ccb_h.timeout * 10; 2918 sim = start_ccb->ccb_h.path->bus->sim; 2919 devq = sim->devq; 2920 dev = start_ccb->ccb_h.path->device; 2921 2922 mtx_assert(sim->mtx, MA_OWNED); 2923 2924 /* 2925 * Steal an opening so that no other queued requests 2926 * can get it before us while we simulate interrupts. 2927 */ 2928 dev->ccbq.devq_openings--; 2929 dev->ccbq.dev_openings--; 2930 2931 while(((devq != NULL && devq->send_openings <= 0) || 2932 dev->ccbq.dev_openings < 0) && (--timeout > 0)) { 2933 DELAY(100); 2934 (*(sim->sim_poll))(sim); 2935 camisr_runqueue(&sim->sim_doneq); 2936 } 2937 2938 dev->ccbq.devq_openings++; 2939 dev->ccbq.dev_openings++; 2940 2941 if (timeout != 0) { 2942 xpt_action(start_ccb); 2943 while(--timeout > 0) { 2944 (*(sim->sim_poll))(sim); 2945 camisr_runqueue(&sim->sim_doneq); 2946 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK) 2947 != CAM_REQ_INPROG) 2948 break; 2949 DELAY(100); 2950 } 2951 if (timeout == 0) { 2952 /* 2953 * XXX Is it worth adding a sim_timeout entry 2954 * point so we can attempt recovery? If 2955 * this is only used for dumps, I don't think 2956 * it is. 2957 */ 2958 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT; 2959 } 2960 } else { 2961 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2962 } 2963} 2964 2965/* 2966 * Schedule a peripheral driver to receive a ccb when it's 2967 * target device has space for more transactions. 2968 */ 2969void 2970xpt_schedule(struct cam_periph *perph, u_int32_t new_priority) 2971{ 2972 struct cam_ed *device; 2973 int runq = 0; 2974 2975 mtx_assert(perph->sim->mtx, MA_OWNED); 2976 2977 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n")); 2978 device = perph->path->device; 2979 if (periph_is_queued(perph)) { 2980 /* Simply reorder based on new priority */ 2981 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 2982 (" change priority to %d\n", new_priority)); 2983 if (new_priority < perph->pinfo.priority) { 2984 camq_change_priority(&device->drvq, 2985 perph->pinfo.index, 2986 new_priority); 2987 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 2988 } 2989 } else { 2990 /* New entry on the queue */ 2991 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 2992 (" added periph to queue\n")); 2993 perph->pinfo.priority = new_priority; 2994 perph->pinfo.generation = ++device->drvq.generation; 2995 camq_insert(&device->drvq, &perph->pinfo); 2996 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 2997 } 2998 if (runq != 0) { 2999 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 3000 (" calling xpt_run_devq\n")); 3001 xpt_run_dev_allocq(perph->path->bus); 3002 } 3003} 3004 3005 3006/* 3007 * Schedule a device to run on a given queue. 3008 * If the device was inserted as a new entry on the queue, 3009 * return 1 meaning the device queue should be run. If we 3010 * were already queued, implying someone else has already 3011 * started the queue, return 0 so the caller doesn't attempt 3012 * to run the queue. 3013 */ 3014int 3015xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo, 3016 u_int32_t new_priority) 3017{ 3018 int retval; 3019 u_int32_t old_priority; 3020 3021 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n")); 3022 3023 old_priority = pinfo->priority; 3024 3025 /* 3026 * Are we already queued? 3027 */ 3028 if (pinfo->index != CAM_UNQUEUED_INDEX) { 3029 /* Simply reorder based on new priority */ 3030 if (new_priority < old_priority) { 3031 camq_change_priority(queue, pinfo->index, 3032 new_priority); 3033 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3034 ("changed priority to %d\n", 3035 new_priority)); 3036 retval = 1; 3037 } else 3038 retval = 0; 3039 } else { 3040 /* New entry on the queue */ 3041 if (new_priority < old_priority) 3042 pinfo->priority = new_priority; 3043 3044 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3045 ("Inserting onto queue\n")); 3046 pinfo->generation = ++queue->generation; 3047 camq_insert(queue, pinfo); 3048 retval = 1; 3049 } 3050 return (retval); 3051} 3052 3053static void 3054xpt_run_dev_allocq(struct cam_eb *bus) 3055{ 3056 struct cam_devq *devq; 3057 3058 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n")); 3059 devq = bus->sim->devq; 3060 3061 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3062 (" qfrozen_cnt == 0x%x, entries == %d, " 3063 "openings == %d, active == %d\n", 3064 devq->alloc_queue.qfrozen_cnt[0], 3065 devq->alloc_queue.entries, 3066 devq->alloc_openings, 3067 devq->alloc_active)); 3068 3069 devq->alloc_queue.qfrozen_cnt[0]++; 3070 while ((devq->alloc_queue.entries > 0) 3071 && (devq->alloc_openings > 0) 3072 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) { 3073 struct cam_ed_qinfo *qinfo; 3074 struct cam_ed *device; 3075 union ccb *work_ccb; 3076 struct cam_periph *drv; 3077 struct camq *drvq; 3078 3079 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue, 3080 CAMQ_HEAD); 3081 device = qinfo->device; 3082 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3083 ("running device %p\n", device)); 3084 3085 drvq = &device->drvq; 3086 3087#ifdef CAMDEBUG 3088 if (drvq->entries <= 0) { 3089 panic("xpt_run_dev_allocq: " 3090 "Device on queue without any work to do"); 3091 } 3092#endif 3093 if ((work_ccb = xpt_get_ccb(device)) != NULL) { 3094 devq->alloc_openings--; 3095 devq->alloc_active++; 3096 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD); 3097 xpt_setup_ccb(&work_ccb->ccb_h, drv->path, 3098 drv->pinfo.priority); 3099 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3100 ("calling periph start\n")); 3101 drv->periph_start(drv, work_ccb); 3102 } else { 3103 /* 3104 * Malloc failure in alloc_ccb 3105 */ 3106 /* 3107 * XXX add us to a list to be run from free_ccb 3108 * if we don't have any ccbs active on this 3109 * device queue otherwise we may never get run 3110 * again. 3111 */ 3112 break; 3113 } 3114 3115 /* We may have more work. Attempt to reschedule. */ 3116 xpt_schedule_dev_allocq(bus, device); 3117 } 3118 devq->alloc_queue.qfrozen_cnt[0]--; 3119} 3120 3121static void 3122xpt_run_dev_sendq(struct cam_eb *bus) 3123{ 3124 struct cam_devq *devq; 3125 3126 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n")); 3127 3128 devq = bus->sim->devq; 3129 3130 devq->send_queue.qfrozen_cnt[0]++; 3131 while ((devq->send_queue.entries > 0) 3132 && (devq->send_openings > 0) 3133 && (devq->send_queue.qfrozen_cnt[0] <= 1)) { 3134 struct cam_ed_qinfo *qinfo; 3135 struct cam_ed *device; 3136 union ccb *work_ccb; 3137 struct cam_sim *sim; 3138 3139 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue, 3140 CAMQ_HEAD); 3141 device = qinfo->device; 3142 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3143 ("running device %p\n", device)); 3144 3145 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 3146 if (work_ccb == NULL) { 3147 printf("device on run queue with no ccbs???\n"); 3148 continue; 3149 } 3150 3151 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) { 3152 3153 mtx_lock(&xsoftc.xpt_lock); 3154 if (xsoftc.num_highpower <= 0) { 3155 /* 3156 * We got a high power command, but we 3157 * don't have any available slots. Freeze 3158 * the device queue until we have a slot 3159 * available. 3160 */ 3161 xpt_freeze_devq(work_ccb->ccb_h.path, 1); 3162 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, 3163 &work_ccb->ccb_h, 3164 xpt_links.stqe); 3165 3166 mtx_unlock(&xsoftc.xpt_lock); 3167 continue; 3168 } else { 3169 /* 3170 * Consume a high power slot while 3171 * this ccb runs. 3172 */ 3173 xsoftc.num_highpower--; 3174 } 3175 mtx_unlock(&xsoftc.xpt_lock); 3176 } 3177 cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 3178 cam_ccbq_send_ccb(&device->ccbq, work_ccb); 3179 3180 devq->send_openings--; 3181 devq->send_active++; 3182 3183 xpt_schedule_dev_sendq(bus, device); 3184 3185 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){ 3186 /* 3187 * The client wants to freeze the queue 3188 * after this CCB is sent. 3189 */ 3190 xpt_freeze_devq(work_ccb->ccb_h.path, 1); 3191 } 3192 3193 /* In Target mode, the peripheral driver knows best... */ 3194 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) { 3195 if ((device->inq_flags & SID_CmdQue) != 0 3196 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE) 3197 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID; 3198 else 3199 /* 3200 * Clear this in case of a retried CCB that 3201 * failed due to a rejected tag. 3202 */ 3203 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 3204 } 3205 3206 /* 3207 * Device queues can be shared among multiple sim instances 3208 * that reside on different busses. Use the SIM in the queue 3209 * CCB's path, rather than the one in the bus that was passed 3210 * into this function. 3211 */ 3212 sim = work_ccb->ccb_h.path->bus->sim; 3213 (*(sim->sim_action))(sim, work_ccb); 3214 } 3215 devq->send_queue.qfrozen_cnt[0]--; 3216} 3217 3218/* 3219 * This function merges stuff from the slave ccb into the master ccb, while 3220 * keeping important fields in the master ccb constant. 3221 */ 3222void 3223xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb) 3224{ 3225 3226 /* 3227 * Pull fields that are valid for peripheral drivers to set 3228 * into the master CCB along with the CCB "payload". 3229 */ 3230 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count; 3231 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code; 3232 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout; 3233 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags; 3234 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1], 3235 sizeof(union ccb) - sizeof(struct ccb_hdr)); 3236} 3237 3238void 3239xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority) 3240{ 3241 3242 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n")); 3243 ccb_h->pinfo.priority = priority; 3244 ccb_h->path = path; 3245 ccb_h->path_id = path->bus->path_id; 3246 if (path->target) 3247 ccb_h->target_id = path->target->target_id; 3248 else 3249 ccb_h->target_id = CAM_TARGET_WILDCARD; 3250 if (path->device) { 3251 ccb_h->target_lun = path->device->lun_id; 3252 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation; 3253 } else { 3254 ccb_h->target_lun = CAM_TARGET_WILDCARD; 3255 } 3256 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 3257 ccb_h->flags = 0; 3258} 3259 3260/* Path manipulation functions */ 3261cam_status 3262xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph, 3263 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3264{ 3265 struct cam_path *path; 3266 cam_status status; 3267 3268 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT); 3269 3270 if (path == NULL) { 3271 status = CAM_RESRC_UNAVAIL; 3272 return(status); 3273 } 3274 status = xpt_compile_path(path, perph, path_id, target_id, lun_id); 3275 if (status != CAM_REQ_CMP) { 3276 free(path, M_CAMXPT); 3277 path = NULL; 3278 } 3279 *new_path_ptr = path; 3280 return (status); 3281} 3282 3283cam_status 3284xpt_create_path_unlocked(struct cam_path **new_path_ptr, 3285 struct cam_periph *periph, path_id_t path_id, 3286 target_id_t target_id, lun_id_t lun_id) 3287{ 3288 struct cam_path *path; 3289 struct cam_eb *bus = NULL; 3290 cam_status status; 3291 int need_unlock = 0; 3292 3293 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK); 3294 3295 if (path_id != CAM_BUS_WILDCARD) { 3296 bus = xpt_find_bus(path_id); 3297 if (bus != NULL) { 3298 need_unlock = 1; 3299 CAM_SIM_LOCK(bus->sim); 3300 } 3301 } 3302 status = xpt_compile_path(path, periph, path_id, target_id, lun_id); 3303 if (need_unlock) 3304 CAM_SIM_UNLOCK(bus->sim); 3305 if (status != CAM_REQ_CMP) { 3306 free(path, M_CAMXPT); 3307 path = NULL; 3308 } 3309 *new_path_ptr = path; 3310 return (status); 3311} 3312 3313cam_status 3314xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph, 3315 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3316{ 3317 struct cam_eb *bus; 3318 struct cam_et *target; 3319 struct cam_ed *device; 3320 cam_status status; 3321 3322 status = CAM_REQ_CMP; /* Completed without error */ 3323 target = NULL; /* Wildcarded */ 3324 device = NULL; /* Wildcarded */ 3325 3326 /* 3327 * We will potentially modify the EDT, so block interrupts 3328 * that may attempt to create cam paths. 3329 */ 3330 bus = xpt_find_bus(path_id); 3331 if (bus == NULL) { 3332 status = CAM_PATH_INVALID; 3333 } else { 3334 target = xpt_find_target(bus, target_id); 3335 if (target == NULL) { 3336 /* Create one */ 3337 struct cam_et *new_target; 3338 3339 new_target = xpt_alloc_target(bus, target_id); 3340 if (new_target == NULL) { 3341 status = CAM_RESRC_UNAVAIL; 3342 } else { 3343 target = new_target; 3344 } 3345 } 3346 if (target != NULL) { 3347 device = xpt_find_device(target, lun_id); 3348 if (device == NULL) { 3349 /* Create one */ 3350 struct cam_ed *new_device; 3351 3352 new_device = 3353 (*(bus->xport->alloc_device))(bus, 3354 target, 3355 lun_id); 3356 if (new_device == NULL) { 3357 status = CAM_RESRC_UNAVAIL; 3358 } else { 3359 device = new_device; 3360 } 3361 } 3362 } 3363 } 3364 3365 /* 3366 * Only touch the user's data if we are successful. 3367 */ 3368 if (status == CAM_REQ_CMP) { 3369 new_path->periph = perph; 3370 new_path->bus = bus; 3371 new_path->target = target; 3372 new_path->device = device; 3373 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n")); 3374 } else { 3375 if (device != NULL) 3376 xpt_release_device(device); 3377 if (target != NULL) 3378 xpt_release_target(target); 3379 if (bus != NULL) 3380 xpt_release_bus(bus); 3381 } 3382 return (status); 3383} 3384 3385void 3386xpt_release_path(struct cam_path *path) 3387{ 3388 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n")); 3389 if (path->device != NULL) { 3390 xpt_release_device(path->device); 3391 path->device = NULL; 3392 } 3393 if (path->target != NULL) { 3394 xpt_release_target(path->target); 3395 path->target = NULL; 3396 } 3397 if (path->bus != NULL) { 3398 xpt_release_bus(path->bus); 3399 path->bus = NULL; 3400 } 3401} 3402 3403void 3404xpt_free_path(struct cam_path *path) 3405{ 3406 3407 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n")); 3408 xpt_release_path(path); 3409 free(path, M_CAMXPT); 3410} 3411 3412 3413/* 3414 * Return -1 for failure, 0 for exact match, 1 for match with wildcards 3415 * in path1, 2 for match with wildcards in path2. 3416 */ 3417int 3418xpt_path_comp(struct cam_path *path1, struct cam_path *path2) 3419{ 3420 int retval = 0; 3421 3422 if (path1->bus != path2->bus) { 3423 if (path1->bus->path_id == CAM_BUS_WILDCARD) 3424 retval = 1; 3425 else if (path2->bus->path_id == CAM_BUS_WILDCARD) 3426 retval = 2; 3427 else 3428 return (-1); 3429 } 3430 if (path1->target != path2->target) { 3431 if (path1->target->target_id == CAM_TARGET_WILDCARD) { 3432 if (retval == 0) 3433 retval = 1; 3434 } else if (path2->target->target_id == CAM_TARGET_WILDCARD) 3435 retval = 2; 3436 else 3437 return (-1); 3438 } 3439 if (path1->device != path2->device) { 3440 if (path1->device->lun_id == CAM_LUN_WILDCARD) { 3441 if (retval == 0) 3442 retval = 1; 3443 } else if (path2->device->lun_id == CAM_LUN_WILDCARD) 3444 retval = 2; 3445 else 3446 return (-1); 3447 } 3448 return (retval); 3449} 3450 3451void 3452xpt_print_path(struct cam_path *path) 3453{ 3454 3455 if (path == NULL) 3456 printf("(nopath): "); 3457 else { 3458 if (path->periph != NULL) 3459 printf("(%s%d:", path->periph->periph_name, 3460 path->periph->unit_number); 3461 else 3462 printf("(noperiph:"); 3463 3464 if (path->bus != NULL) 3465 printf("%s%d:%d:", path->bus->sim->sim_name, 3466 path->bus->sim->unit_number, 3467 path->bus->sim->bus_id); 3468 else 3469 printf("nobus:"); 3470 3471 if (path->target != NULL) 3472 printf("%d:", path->target->target_id); 3473 else 3474 printf("X:"); 3475 3476 if (path->device != NULL) 3477 printf("%d): ", path->device->lun_id); 3478 else 3479 printf("X): "); 3480 } 3481} 3482 3483void 3484xpt_print(struct cam_path *path, const char *fmt, ...) 3485{ 3486 va_list ap; 3487 xpt_print_path(path); 3488 va_start(ap, fmt); 3489 vprintf(fmt, ap); 3490 va_end(ap); 3491} 3492 3493int 3494xpt_path_string(struct cam_path *path, char *str, size_t str_len) 3495{ 3496 struct sbuf sb; 3497 3498#ifdef INVARIANTS 3499 if (path != NULL && path->bus != NULL) 3500 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3501#endif 3502 3503 sbuf_new(&sb, str, str_len, 0); 3504 3505 if (path == NULL) 3506 sbuf_printf(&sb, "(nopath): "); 3507 else { 3508 if (path->periph != NULL) 3509 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name, 3510 path->periph->unit_number); 3511 else 3512 sbuf_printf(&sb, "(noperiph:"); 3513 3514 if (path->bus != NULL) 3515 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name, 3516 path->bus->sim->unit_number, 3517 path->bus->sim->bus_id); 3518 else 3519 sbuf_printf(&sb, "nobus:"); 3520 3521 if (path->target != NULL) 3522 sbuf_printf(&sb, "%d:", path->target->target_id); 3523 else 3524 sbuf_printf(&sb, "X:"); 3525 3526 if (path->device != NULL) 3527 sbuf_printf(&sb, "%d): ", path->device->lun_id); 3528 else 3529 sbuf_printf(&sb, "X): "); 3530 } 3531 sbuf_finish(&sb); 3532 3533 return(sbuf_len(&sb)); 3534} 3535 3536path_id_t 3537xpt_path_path_id(struct cam_path *path) 3538{ 3539 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3540 3541 return(path->bus->path_id); 3542} 3543 3544target_id_t 3545xpt_path_target_id(struct cam_path *path) 3546{ 3547 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3548 3549 if (path->target != NULL) 3550 return (path->target->target_id); 3551 else 3552 return (CAM_TARGET_WILDCARD); 3553} 3554 3555lun_id_t 3556xpt_path_lun_id(struct cam_path *path) 3557{ 3558 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3559 3560 if (path->device != NULL) 3561 return (path->device->lun_id); 3562 else 3563 return (CAM_LUN_WILDCARD); 3564} 3565 3566struct cam_sim * 3567xpt_path_sim(struct cam_path *path) 3568{ 3569 3570 return (path->bus->sim); 3571} 3572 3573struct cam_periph* 3574xpt_path_periph(struct cam_path *path) 3575{ 3576 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3577 3578 return (path->periph); 3579} 3580 3581/* 3582 * Release a CAM control block for the caller. Remit the cost of the structure 3583 * to the device referenced by the path. If the this device had no 'credits' 3584 * and peripheral drivers have registered async callbacks for this notification 3585 * call them now. 3586 */ 3587void 3588xpt_release_ccb(union ccb *free_ccb) 3589{ 3590 struct cam_path *path; 3591 struct cam_ed *device; 3592 struct cam_eb *bus; 3593 struct cam_sim *sim; 3594 3595 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n")); 3596 path = free_ccb->ccb_h.path; 3597 device = path->device; 3598 bus = path->bus; 3599 sim = bus->sim; 3600 3601 mtx_assert(sim->mtx, MA_OWNED); 3602 3603 cam_ccbq_release_opening(&device->ccbq); 3604 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) { 3605 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED; 3606 cam_ccbq_resize(&device->ccbq, 3607 device->ccbq.dev_openings + device->ccbq.dev_active); 3608 } 3609 if (sim->ccb_count > sim->max_ccbs) { 3610 xpt_free_ccb(free_ccb); 3611 sim->ccb_count--; 3612 } else { 3613 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h, 3614 xpt_links.sle); 3615 } 3616 if (sim->devq == NULL) { 3617 return; 3618 } 3619 sim->devq->alloc_openings++; 3620 sim->devq->alloc_active--; 3621 if (device_is_alloc_queued(device) == 0) 3622 xpt_schedule_dev_allocq(bus, device); 3623 xpt_run_dev_allocq(bus); 3624} 3625 3626/* Functions accessed by SIM drivers */ 3627 3628static struct xpt_xport xport_default = { 3629 .alloc_device = xpt_alloc_device_default, 3630 .action = xpt_action_default, 3631 .async = xpt_dev_async_default, 3632}; 3633 3634/* 3635 * A sim structure, listing the SIM entry points and instance 3636 * identification info is passed to xpt_bus_register to hook the SIM 3637 * into the CAM framework. xpt_bus_register creates a cam_eb entry 3638 * for this new bus and places it in the array of busses and assigns 3639 * it a path_id. The path_id may be influenced by "hard wiring" 3640 * information specified by the user. Once interrupt services are 3641 * available, the bus will be probed. 3642 */ 3643int32_t 3644xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus) 3645{ 3646 struct cam_eb *new_bus; 3647 struct cam_eb *old_bus; 3648 struct ccb_pathinq cpi; 3649 struct cam_path *path; 3650 cam_status status; 3651 3652 mtx_assert(sim->mtx, MA_OWNED); 3653 3654 sim->bus_id = bus; 3655 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus), 3656 M_CAMXPT, M_NOWAIT); 3657 if (new_bus == NULL) { 3658 /* Couldn't satisfy request */ 3659 return (CAM_RESRC_UNAVAIL); 3660 } 3661 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT); 3662 if (path == NULL) { 3663 free(new_bus, M_CAMXPT); 3664 return (CAM_RESRC_UNAVAIL); 3665 } 3666 3667 if (strcmp(sim->sim_name, "xpt") != 0) { 3668 sim->path_id = 3669 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id); 3670 } 3671 3672 TAILQ_INIT(&new_bus->et_entries); 3673 new_bus->path_id = sim->path_id; 3674 cam_sim_hold(sim); 3675 new_bus->sim = sim; 3676 timevalclear(&new_bus->last_reset); 3677 new_bus->flags = 0; 3678 new_bus->refcount = 1; /* Held until a bus_deregister event */ 3679 new_bus->generation = 0; 3680 3681 mtx_lock(&xsoftc.xpt_topo_lock); 3682 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3683 while (old_bus != NULL 3684 && old_bus->path_id < new_bus->path_id) 3685 old_bus = TAILQ_NEXT(old_bus, links); 3686 if (old_bus != NULL) 3687 TAILQ_INSERT_BEFORE(old_bus, new_bus, links); 3688 else 3689 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links); 3690 xsoftc.bus_generation++; 3691 mtx_unlock(&xsoftc.xpt_topo_lock); 3692 3693 /* 3694 * Set a default transport so that a PATH_INQ can be issued to 3695 * the SIM. This will then allow for probing and attaching of 3696 * a more appropriate transport. 3697 */ 3698 new_bus->xport = &xport_default; 3699 3700 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id, 3701 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3702 if (status != CAM_REQ_CMP) 3703 printf("xpt_compile_path returned %d\n", status); 3704 3705 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 3706 cpi.ccb_h.func_code = XPT_PATH_INQ; 3707 xpt_action((union ccb *)&cpi); 3708 3709 if (cpi.ccb_h.status == CAM_REQ_CMP) { 3710 switch (cpi.transport) { 3711 case XPORT_SPI: 3712 case XPORT_SAS: 3713 case XPORT_FC: 3714 case XPORT_USB: 3715 case XPORT_ISCSI: 3716 case XPORT_PPB: 3717 new_bus->xport = scsi_get_xport(); 3718 break; 3719 case XPORT_ATA: 3720 case XPORT_SATA: 3721 new_bus->xport = ata_get_xport(); 3722 break; 3723 default: 3724 new_bus->xport = &xport_default; 3725 break; 3726 } 3727 } 3728 3729 /* Notify interested parties */ 3730 if (sim->path_id != CAM_XPT_PATH_ID) { 3731 union ccb *scan_ccb; 3732 3733 xpt_async(AC_PATH_REGISTERED, path, &cpi); 3734 /* Initiate bus rescan. */ 3735 scan_ccb = xpt_alloc_ccb_nowait(); 3736 scan_ccb->ccb_h.path = path; 3737 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS; 3738 scan_ccb->crcn.flags = 0; 3739 xpt_rescan(scan_ccb); 3740 } else 3741 xpt_free_path(path); 3742 return (CAM_SUCCESS); 3743} 3744 3745int32_t 3746xpt_bus_deregister(path_id_t pathid) 3747{ 3748 struct cam_path bus_path; 3749 cam_status status; 3750 3751 status = xpt_compile_path(&bus_path, NULL, pathid, 3752 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3753 if (status != CAM_REQ_CMP) 3754 return (status); 3755 3756 xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 3757 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 3758 3759 /* Release the reference count held while registered. */ 3760 xpt_release_bus(bus_path.bus); 3761 xpt_release_path(&bus_path); 3762 3763 return (CAM_REQ_CMP); 3764} 3765 3766static path_id_t 3767xptnextfreepathid(void) 3768{ 3769 struct cam_eb *bus; 3770 path_id_t pathid; 3771 const char *strval; 3772 3773 pathid = 0; 3774 mtx_lock(&xsoftc.xpt_topo_lock); 3775 bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3776retry: 3777 /* Find an unoccupied pathid */ 3778 while (bus != NULL && bus->path_id <= pathid) { 3779 if (bus->path_id == pathid) 3780 pathid++; 3781 bus = TAILQ_NEXT(bus, links); 3782 } 3783 mtx_unlock(&xsoftc.xpt_topo_lock); 3784 3785 /* 3786 * Ensure that this pathid is not reserved for 3787 * a bus that may be registered in the future. 3788 */ 3789 if (resource_string_value("scbus", pathid, "at", &strval) == 0) { 3790 ++pathid; 3791 /* Start the search over */ 3792 mtx_lock(&xsoftc.xpt_topo_lock); 3793 goto retry; 3794 } 3795 return (pathid); 3796} 3797 3798static path_id_t 3799xptpathid(const char *sim_name, int sim_unit, int sim_bus) 3800{ 3801 path_id_t pathid; 3802 int i, dunit, val; 3803 char buf[32]; 3804 const char *dname; 3805 3806 pathid = CAM_XPT_PATH_ID; 3807 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit); 3808 i = 0; 3809 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) { 3810 if (strcmp(dname, "scbus")) { 3811 /* Avoid a bit of foot shooting. */ 3812 continue; 3813 } 3814 if (dunit < 0) /* unwired?! */ 3815 continue; 3816 if (resource_int_value("scbus", dunit, "bus", &val) == 0) { 3817 if (sim_bus == val) { 3818 pathid = dunit; 3819 break; 3820 } 3821 } else if (sim_bus == 0) { 3822 /* Unspecified matches bus 0 */ 3823 pathid = dunit; 3824 break; 3825 } else { 3826 printf("Ambiguous scbus configuration for %s%d " 3827 "bus %d, cannot wire down. The kernel " 3828 "config entry for scbus%d should " 3829 "specify a controller bus.\n" 3830 "Scbus will be assigned dynamically.\n", 3831 sim_name, sim_unit, sim_bus, dunit); 3832 break; 3833 } 3834 } 3835 3836 if (pathid == CAM_XPT_PATH_ID) 3837 pathid = xptnextfreepathid(); 3838 return (pathid); 3839} 3840 3841void 3842xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg) 3843{ 3844 struct cam_eb *bus; 3845 struct cam_et *target, *next_target; 3846 struct cam_ed *device, *next_device; 3847 3848 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3849 3850 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n")); 3851 3852 /* 3853 * Most async events come from a CAM interrupt context. In 3854 * a few cases, the error recovery code at the peripheral layer, 3855 * which may run from our SWI or a process context, may signal 3856 * deferred events with a call to xpt_async. 3857 */ 3858 3859 bus = path->bus; 3860 3861 if (async_code == AC_BUS_RESET) { 3862 /* Update our notion of when the last reset occurred */ 3863 microtime(&bus->last_reset); 3864 } 3865 3866 for (target = TAILQ_FIRST(&bus->et_entries); 3867 target != NULL; 3868 target = next_target) { 3869 3870 next_target = TAILQ_NEXT(target, links); 3871 3872 if (path->target != target 3873 && path->target->target_id != CAM_TARGET_WILDCARD 3874 && target->target_id != CAM_TARGET_WILDCARD) 3875 continue; 3876 3877 if (async_code == AC_SENT_BDR) { 3878 /* Update our notion of when the last reset occurred */ 3879 microtime(&path->target->last_reset); 3880 } 3881 3882 for (device = TAILQ_FIRST(&target->ed_entries); 3883 device != NULL; 3884 device = next_device) { 3885 3886 next_device = TAILQ_NEXT(device, links); 3887 3888 if (path->device != device 3889 && path->device->lun_id != CAM_LUN_WILDCARD 3890 && device->lun_id != CAM_LUN_WILDCARD) 3891 continue; 3892 /* 3893 * The async callback could free the device. 3894 * If it is a broadcast async, it doesn't hold 3895 * device reference, so take our own reference. 3896 */ 3897 xpt_acquire_device(device); 3898 (*(bus->xport->async))(async_code, bus, 3899 target, device, 3900 async_arg); 3901 3902 xpt_async_bcast(&device->asyncs, async_code, 3903 path, async_arg); 3904 xpt_release_device(device); 3905 } 3906 } 3907 3908 /* 3909 * If this wasn't a fully wildcarded async, tell all 3910 * clients that want all async events. 3911 */ 3912 if (bus != xpt_periph->path->bus) 3913 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code, 3914 path, async_arg); 3915} 3916 3917static void 3918xpt_async_bcast(struct async_list *async_head, 3919 u_int32_t async_code, 3920 struct cam_path *path, void *async_arg) 3921{ 3922 struct async_node *cur_entry; 3923 3924 cur_entry = SLIST_FIRST(async_head); 3925 while (cur_entry != NULL) { 3926 struct async_node *next_entry; 3927 /* 3928 * Grab the next list entry before we call the current 3929 * entry's callback. This is because the callback function 3930 * can delete its async callback entry. 3931 */ 3932 next_entry = SLIST_NEXT(cur_entry, links); 3933 if ((cur_entry->event_enable & async_code) != 0) 3934 cur_entry->callback(cur_entry->callback_arg, 3935 async_code, path, 3936 async_arg); 3937 cur_entry = next_entry; 3938 } 3939} 3940 3941static void 3942xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus, 3943 struct cam_et *target, struct cam_ed *device, 3944 void *async_arg) 3945{ 3946 printf("%s called\n", __func__); 3947} 3948 3949u_int32_t 3950xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count) 3951{ 3952 struct cam_ed *dev = path->device; 3953 3954 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3955 dev->sim->devq->alloc_openings += 3956 cam_ccbq_freeze(&dev->ccbq, rl, count); 3957 /* Remove frozen device from allocq. */ 3958 if (device_is_alloc_queued(dev) && 3959 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 3960 CAMQ_GET_PRIO(&dev->drvq)))) { 3961 camq_remove(&dev->sim->devq->alloc_queue, 3962 dev->alloc_ccb_entry.pinfo.index); 3963 } 3964 /* Remove frozen device from sendq. */ 3965 if (device_is_send_queued(dev) && 3966 cam_ccbq_frozen_top(&dev->ccbq)) { 3967 camq_remove(&dev->sim->devq->send_queue, 3968 dev->send_ccb_entry.pinfo.index); 3969 } 3970 return (dev->ccbq.queue.qfrozen_cnt[rl]); 3971} 3972 3973u_int32_t 3974xpt_freeze_devq(struct cam_path *path, u_int count) 3975{ 3976 3977 return (xpt_freeze_devq_rl(path, 0, count)); 3978} 3979 3980u_int32_t 3981xpt_freeze_simq(struct cam_sim *sim, u_int count) 3982{ 3983 3984 mtx_assert(sim->mtx, MA_OWNED); 3985 sim->devq->send_queue.qfrozen_cnt[0] += count; 3986 return (sim->devq->send_queue.qfrozen_cnt[0]); 3987} 3988 3989static void 3990xpt_release_devq_timeout(void *arg) 3991{ 3992 struct cam_ed *device; 3993 3994 device = (struct cam_ed *)arg; 3995 3996 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE); 3997} 3998 3999void 4000xpt_release_devq(struct cam_path *path, u_int count, int run_queue) 4001{ 4002 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4003 4004 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue); 4005} 4006 4007void 4008xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue) 4009{ 4010 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4011 4012 xpt_release_devq_device(path->device, rl, count, run_queue); 4013} 4014 4015static void 4016xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue) 4017{ 4018 4019 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) { 4020#ifdef INVARIANTS 4021 printf("xpt_release_devq(%d): requested %u > present %u\n", 4022 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]); 4023#endif 4024 count = dev->ccbq.queue.qfrozen_cnt[rl]; 4025 } 4026 dev->sim->devq->alloc_openings -= 4027 cam_ccbq_release(&dev->ccbq, rl, count); 4028 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 4029 CAMQ_GET_PRIO(&dev->drvq))) == 0) { 4030 if (xpt_schedule_dev_allocq(dev->target->bus, dev)) 4031 xpt_run_dev_allocq(dev->target->bus); 4032 } 4033 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) { 4034 /* 4035 * No longer need to wait for a successful 4036 * command completion. 4037 */ 4038 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 4039 /* 4040 * Remove any timeouts that might be scheduled 4041 * to release this queue. 4042 */ 4043 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 4044 callout_stop(&dev->callout); 4045 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING; 4046 } 4047 if (run_queue == 0) 4048 return; 4049 /* 4050 * Now that we are unfrozen schedule the 4051 * device so any pending transactions are 4052 * run. 4053 */ 4054 if (xpt_schedule_dev_sendq(dev->target->bus, dev)) 4055 xpt_run_dev_sendq(dev->target->bus); 4056 } 4057} 4058 4059void 4060xpt_release_simq(struct cam_sim *sim, int run_queue) 4061{ 4062 struct camq *sendq; 4063 4064 mtx_assert(sim->mtx, MA_OWNED); 4065 sendq = &(sim->devq->send_queue); 4066 if (sendq->qfrozen_cnt[0] <= 0) { 4067#ifdef INVARIANTS 4068 printf("xpt_release_simq: requested 1 > present %u\n", 4069 sendq->qfrozen_cnt[0]); 4070#endif 4071 } else 4072 sendq->qfrozen_cnt[0]--; 4073 if (sendq->qfrozen_cnt[0] == 0) { 4074 /* 4075 * If there is a timeout scheduled to release this 4076 * sim queue, remove it. The queue frozen count is 4077 * already at 0. 4078 */ 4079 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){ 4080 callout_stop(&sim->callout); 4081 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING; 4082 } 4083 if (run_queue) { 4084 struct cam_eb *bus; 4085 4086 /* 4087 * Now that we are unfrozen run the send queue. 4088 */ 4089 bus = xpt_find_bus(sim->path_id); 4090 xpt_run_dev_sendq(bus); 4091 xpt_release_bus(bus); 4092 } 4093 } 4094} 4095 4096/* 4097 * XXX Appears to be unused. 4098 */ 4099static void 4100xpt_release_simq_timeout(void *arg) 4101{ 4102 struct cam_sim *sim; 4103 4104 sim = (struct cam_sim *)arg; 4105 xpt_release_simq(sim, /* run_queue */ TRUE); 4106} 4107 4108void 4109xpt_done(union ccb *done_ccb) 4110{ 4111 struct cam_sim *sim; 4112 int first; 4113 4114 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n")); 4115 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 4116 /* 4117 * Queue up the request for handling by our SWI handler 4118 * any of the "non-immediate" type of ccbs. 4119 */ 4120 sim = done_ccb->ccb_h.path->bus->sim; 4121 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h, 4122 sim_links.tqe); 4123 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4124 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) { 4125 mtx_lock(&cam_simq_lock); 4126 first = TAILQ_EMPTY(&cam_simq); 4127 TAILQ_INSERT_TAIL(&cam_simq, sim, links); 4128 mtx_unlock(&cam_simq_lock); 4129 sim->flags |= CAM_SIM_ON_DONEQ; 4130 if (first) 4131 swi_sched(cambio_ih, 0); 4132 } 4133 } 4134} 4135 4136union ccb * 4137xpt_alloc_ccb() 4138{ 4139 union ccb *new_ccb; 4140 4141 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK); 4142 return (new_ccb); 4143} 4144 4145union ccb * 4146xpt_alloc_ccb_nowait() 4147{ 4148 union ccb *new_ccb; 4149 4150 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT); 4151 return (new_ccb); 4152} 4153 4154void 4155xpt_free_ccb(union ccb *free_ccb) 4156{ 4157 free(free_ccb, M_CAMXPT); 4158} 4159 4160 4161 4162/* Private XPT functions */ 4163 4164/* 4165 * Get a CAM control block for the caller. Charge the structure to the device 4166 * referenced by the path. If the this device has no 'credits' then the 4167 * device already has the maximum number of outstanding operations under way 4168 * and we return NULL. If we don't have sufficient resources to allocate more 4169 * ccbs, we also return NULL. 4170 */ 4171static union ccb * 4172xpt_get_ccb(struct cam_ed *device) 4173{ 4174 union ccb *new_ccb; 4175 struct cam_sim *sim; 4176 4177 sim = device->sim; 4178 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) { 4179 new_ccb = xpt_alloc_ccb_nowait(); 4180 if (new_ccb == NULL) { 4181 return (NULL); 4182 } 4183 if ((sim->flags & CAM_SIM_MPSAFE) == 0) 4184 callout_handle_init(&new_ccb->ccb_h.timeout_ch); 4185 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h, 4186 xpt_links.sle); 4187 sim->ccb_count++; 4188 } 4189 cam_ccbq_take_opening(&device->ccbq); 4190 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle); 4191 return (new_ccb); 4192} 4193 4194static void 4195xpt_release_bus(struct cam_eb *bus) 4196{ 4197 4198 if ((--bus->refcount == 0) 4199 && (TAILQ_FIRST(&bus->et_entries) == NULL)) { 4200 mtx_lock(&xsoftc.xpt_topo_lock); 4201 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links); 4202 xsoftc.bus_generation++; 4203 mtx_unlock(&xsoftc.xpt_topo_lock); 4204 cam_sim_release(bus->sim); 4205 free(bus, M_CAMXPT); 4206 } 4207} 4208 4209static struct cam_et * 4210xpt_alloc_target(struct cam_eb *bus, target_id_t target_id) 4211{ 4212 struct cam_et *target; 4213 4214 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT); 4215 if (target != NULL) { 4216 struct cam_et *cur_target; 4217 4218 TAILQ_INIT(&target->ed_entries); 4219 target->bus = bus; 4220 target->target_id = target_id; 4221 target->refcount = 1; 4222 target->generation = 0; 4223 target->luns = NULL; 4224 timevalclear(&target->last_reset); 4225 /* 4226 * Hold a reference to our parent bus so it 4227 * will not go away before we do. 4228 */ 4229 bus->refcount++; 4230 4231 /* Insertion sort into our bus's target list */ 4232 cur_target = TAILQ_FIRST(&bus->et_entries); 4233 while (cur_target != NULL && cur_target->target_id < target_id) 4234 cur_target = TAILQ_NEXT(cur_target, links); 4235 4236 if (cur_target != NULL) { 4237 TAILQ_INSERT_BEFORE(cur_target, target, links); 4238 } else { 4239 TAILQ_INSERT_TAIL(&bus->et_entries, target, links); 4240 } 4241 bus->generation++; 4242 } 4243 return (target); 4244} 4245 4246static void 4247xpt_release_target(struct cam_et *target) 4248{ 4249 4250 if ((--target->refcount == 0) 4251 && (TAILQ_FIRST(&target->ed_entries) == NULL)) { 4252 TAILQ_REMOVE(&target->bus->et_entries, target, links); 4253 target->bus->generation++; 4254 xpt_release_bus(target->bus); 4255 if (target->luns) 4256 free(target->luns, M_CAMXPT); 4257 free(target, M_CAMXPT); 4258 } 4259} 4260 4261static struct cam_ed * 4262xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target, 4263 lun_id_t lun_id) 4264{ 4265 struct cam_ed *device, *cur_device; 4266 4267 device = xpt_alloc_device(bus, target, lun_id); 4268 if (device == NULL) 4269 return (NULL); 4270 4271 device->mintags = 1; 4272 device->maxtags = 1; 4273 bus->sim->max_ccbs += device->ccbq.devq_openings; 4274 cur_device = TAILQ_FIRST(&target->ed_entries); 4275 while (cur_device != NULL && cur_device->lun_id < lun_id) 4276 cur_device = TAILQ_NEXT(cur_device, links); 4277 if (cur_device != NULL) { 4278 TAILQ_INSERT_BEFORE(cur_device, device, links); 4279 } else { 4280 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 4281 } 4282 target->generation++; 4283 4284 return (device); 4285} 4286 4287struct cam_ed * 4288xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 4289{ 4290 struct cam_ed *device; 4291 struct cam_devq *devq; 4292 cam_status status; 4293 4294 /* Make space for us in the device queue on our bus */ 4295 devq = bus->sim->devq; 4296 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1); 4297 4298 if (status != CAM_REQ_CMP) { 4299 device = NULL; 4300 } else { 4301 device = (struct cam_ed *)malloc(sizeof(*device), 4302 M_CAMXPT, M_NOWAIT); 4303 } 4304 4305 if (device != NULL) { 4306 cam_init_pinfo(&device->alloc_ccb_entry.pinfo); 4307 device->alloc_ccb_entry.device = device; 4308 cam_init_pinfo(&device->send_ccb_entry.pinfo); 4309 device->send_ccb_entry.device = device; 4310 device->target = target; 4311 device->lun_id = lun_id; 4312 device->sim = bus->sim; 4313 /* Initialize our queues */ 4314 if (camq_init(&device->drvq, 0) != 0) { 4315 free(device, M_CAMXPT); 4316 return (NULL); 4317 } 4318 if (cam_ccbq_init(&device->ccbq, 4319 bus->sim->max_dev_openings) != 0) { 4320 camq_fini(&device->drvq); 4321 free(device, M_CAMXPT); 4322 return (NULL); 4323 } 4324 SLIST_INIT(&device->asyncs); 4325 SLIST_INIT(&device->periphs); 4326 device->generation = 0; 4327 device->owner = NULL; 4328 device->flags = CAM_DEV_UNCONFIGURED; 4329 device->tag_delay_count = 0; 4330 device->tag_saved_openings = 0; 4331 device->refcount = 1; 4332 callout_init_mtx(&device->callout, bus->sim->mtx, 0); 4333 4334 /* 4335 * Hold a reference to our parent target so it 4336 * will not go away before we do. 4337 */ 4338 target->refcount++; 4339 4340 } 4341 return (device); 4342} 4343 4344void 4345xpt_acquire_device(struct cam_ed *device) 4346{ 4347 4348 device->refcount++; 4349} 4350 4351void 4352xpt_release_device(struct cam_ed *device) 4353{ 4354 4355 if (--device->refcount == 0) { 4356 struct cam_devq *devq; 4357 4358 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX 4359 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX) 4360 panic("Removing device while still queued for ccbs"); 4361 4362 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) 4363 callout_stop(&device->callout); 4364 4365 TAILQ_REMOVE(&device->target->ed_entries, device,links); 4366 device->target->generation++; 4367 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings; 4368 /* Release our slot in the devq */ 4369 devq = device->target->bus->sim->devq; 4370 cam_devq_resize(devq, devq->alloc_queue.array_size - 1); 4371 camq_fini(&device->drvq); 4372 cam_ccbq_fini(&device->ccbq); 4373 xpt_release_target(device->target); 4374 free(device, M_CAMXPT); 4375 } 4376} 4377 4378u_int32_t 4379xpt_dev_ccbq_resize(struct cam_path *path, int newopenings) 4380{ 4381 int diff; 4382 int result; 4383 struct cam_ed *dev; 4384 4385 dev = path->device; 4386 4387 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings); 4388 result = cam_ccbq_resize(&dev->ccbq, newopenings); 4389 if (result == CAM_REQ_CMP && (diff < 0)) { 4390 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED; 4391 } 4392 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 4393 || (dev->inq_flags & SID_CmdQue) != 0) 4394 dev->tag_saved_openings = newopenings; 4395 /* Adjust the global limit */ 4396 dev->sim->max_ccbs += diff; 4397 return (result); 4398} 4399 4400static struct cam_eb * 4401xpt_find_bus(path_id_t path_id) 4402{ 4403 struct cam_eb *bus; 4404 4405 mtx_lock(&xsoftc.xpt_topo_lock); 4406 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses); 4407 bus != NULL; 4408 bus = TAILQ_NEXT(bus, links)) { 4409 if (bus->path_id == path_id) { 4410 bus->refcount++; 4411 break; 4412 } 4413 } 4414 mtx_unlock(&xsoftc.xpt_topo_lock); 4415 return (bus); 4416} 4417 4418static struct cam_et * 4419xpt_find_target(struct cam_eb *bus, target_id_t target_id) 4420{ 4421 struct cam_et *target; 4422 4423 for (target = TAILQ_FIRST(&bus->et_entries); 4424 target != NULL; 4425 target = TAILQ_NEXT(target, links)) { 4426 if (target->target_id == target_id) { 4427 target->refcount++; 4428 break; 4429 } 4430 } 4431 return (target); 4432} 4433 4434static struct cam_ed * 4435xpt_find_device(struct cam_et *target, lun_id_t lun_id) 4436{ 4437 struct cam_ed *device; 4438 4439 for (device = TAILQ_FIRST(&target->ed_entries); 4440 device != NULL; 4441 device = TAILQ_NEXT(device, links)) { 4442 if (device->lun_id == lun_id) { 4443 device->refcount++; 4444 break; 4445 } 4446 } 4447 return (device); 4448} 4449 4450void 4451xpt_start_tags(struct cam_path *path) 4452{ 4453 struct ccb_relsim crs; 4454 struct cam_ed *device; 4455 struct cam_sim *sim; 4456 int newopenings; 4457 4458 device = path->device; 4459 sim = path->bus->sim; 4460 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 4461 xpt_freeze_devq(path, /*count*/1); 4462 device->inq_flags |= SID_CmdQue; 4463 if (device->tag_saved_openings != 0) 4464 newopenings = device->tag_saved_openings; 4465 else 4466 newopenings = min(device->maxtags, 4467 sim->max_tagged_dev_openings); 4468 xpt_dev_ccbq_resize(path, newopenings); 4469 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 4470 crs.ccb_h.func_code = XPT_REL_SIMQ; 4471 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 4472 crs.openings 4473 = crs.release_timeout 4474 = crs.qfrozen_cnt 4475 = 0; 4476 xpt_action((union ccb *)&crs); 4477} 4478 4479void 4480xpt_stop_tags(struct cam_path *path) 4481{ 4482 struct ccb_relsim crs; 4483 struct cam_ed *device; 4484 struct cam_sim *sim; 4485 4486 device = path->device; 4487 sim = path->bus->sim; 4488 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 4489 device->tag_delay_count = 0; 4490 xpt_freeze_devq(path, /*count*/1); 4491 device->inq_flags &= ~SID_CmdQue; 4492 xpt_dev_ccbq_resize(path, sim->max_dev_openings); 4493 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 4494 crs.ccb_h.func_code = XPT_REL_SIMQ; 4495 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 4496 crs.openings 4497 = crs.release_timeout 4498 = crs.qfrozen_cnt 4499 = 0; 4500 xpt_action((union ccb *)&crs); 4501} 4502 4503static void 4504xpt_boot_delay(void *arg) 4505{ 4506 4507 xpt_release_boot(); 4508} 4509 4510static void 4511xpt_config(void *arg) 4512{ 4513 /* 4514 * Now that interrupts are enabled, go find our devices 4515 */ 4516 4517#ifdef CAMDEBUG 4518 /* Setup debugging flags and path */ 4519#ifdef CAM_DEBUG_BUS 4520 if (cam_dflags != CAM_DEBUG_NONE) { 4521 /* 4522 * Locking is specifically omitted here. No SIMs have 4523 * registered yet, so xpt_create_path will only be searching 4524 * empty lists of targets and devices. 4525 */ 4526 if (xpt_create_path(&cam_dpath, xpt_periph, 4527 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, 4528 CAM_DEBUG_LUN) != CAM_REQ_CMP) { 4529 printf("xpt_config: xpt_create_path() failed for debug" 4530 " target %d:%d:%d, debugging disabled\n", 4531 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN); 4532 cam_dflags = CAM_DEBUG_NONE; 4533 } 4534 } else 4535 cam_dpath = NULL; 4536#else /* !CAM_DEBUG_BUS */ 4537 cam_dpath = NULL; 4538#endif /* CAM_DEBUG_BUS */ 4539#endif /* CAMDEBUG */ 4540
| 248static void xptaction(struct cam_sim *sim, union ccb *work_ccb); 249static void xptpoll(struct cam_sim *sim); 250static void camisr(void *); 251static void camisr_runqueue(void *); 252static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns, 253 u_int num_patterns, struct cam_eb *bus); 254static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns, 255 u_int num_patterns, 256 struct cam_ed *device); 257static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns, 258 u_int num_patterns, 259 struct cam_periph *periph); 260static xpt_busfunc_t xptedtbusfunc; 261static xpt_targetfunc_t xptedttargetfunc; 262static xpt_devicefunc_t xptedtdevicefunc; 263static xpt_periphfunc_t xptedtperiphfunc; 264static xpt_pdrvfunc_t xptplistpdrvfunc; 265static xpt_periphfunc_t xptplistperiphfunc; 266static int xptedtmatch(struct ccb_dev_match *cdm); 267static int xptperiphlistmatch(struct ccb_dev_match *cdm); 268static int xptbustraverse(struct cam_eb *start_bus, 269 xpt_busfunc_t *tr_func, void *arg); 270static int xpttargettraverse(struct cam_eb *bus, 271 struct cam_et *start_target, 272 xpt_targetfunc_t *tr_func, void *arg); 273static int xptdevicetraverse(struct cam_et *target, 274 struct cam_ed *start_device, 275 xpt_devicefunc_t *tr_func, void *arg); 276static int xptperiphtraverse(struct cam_ed *device, 277 struct cam_periph *start_periph, 278 xpt_periphfunc_t *tr_func, void *arg); 279static int xptpdrvtraverse(struct periph_driver **start_pdrv, 280 xpt_pdrvfunc_t *tr_func, void *arg); 281static int xptpdperiphtraverse(struct periph_driver **pdrv, 282 struct cam_periph *start_periph, 283 xpt_periphfunc_t *tr_func, 284 void *arg); 285static xpt_busfunc_t xptdefbusfunc; 286static xpt_targetfunc_t xptdeftargetfunc; 287static xpt_devicefunc_t xptdefdevicefunc; 288static xpt_periphfunc_t xptdefperiphfunc; 289static void xpt_finishconfig_task(void *context, int pending); 290static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg); 291static int xpt_for_all_devices(xpt_devicefunc_t *tr_func, 292 void *arg); 293static void xpt_dev_async_default(u_int32_t async_code, 294 struct cam_eb *bus, 295 struct cam_et *target, 296 struct cam_ed *device, 297 void *async_arg); 298static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus, 299 struct cam_et *target, 300 lun_id_t lun_id); 301static xpt_devicefunc_t xptsetasyncfunc; 302static xpt_busfunc_t xptsetasyncbusfunc; 303static cam_status xptregister(struct cam_periph *periph, 304 void *arg); 305static __inline int periph_is_queued(struct cam_periph *periph); 306static __inline int device_is_alloc_queued(struct cam_ed *device); 307static __inline int device_is_send_queued(struct cam_ed *device); 308 309static __inline int 310xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev) 311{ 312 int retval; 313 314 if ((dev->drvq.entries > 0) && 315 (dev->ccbq.devq_openings > 0) && 316 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 317 CAMQ_GET_PRIO(&dev->drvq))) == 0)) { 318 /* 319 * The priority of a device waiting for CCB resources 320 * is that of the the highest priority peripheral driver 321 * enqueued. 322 */ 323 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue, 324 &dev->alloc_ccb_entry.pinfo, 325 CAMQ_GET_PRIO(&dev->drvq)); 326 } else { 327 retval = 0; 328 } 329 330 return (retval); 331} 332 333static __inline int 334xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev) 335{ 336 int retval; 337 338 if ((dev->ccbq.queue.entries > 0) && 339 (dev->ccbq.dev_openings > 0) && 340 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) { 341 /* 342 * The priority of a device waiting for controller 343 * resources is that of the the highest priority CCB 344 * enqueued. 345 */ 346 retval = 347 xpt_schedule_dev(&bus->sim->devq->send_queue, 348 &dev->send_ccb_entry.pinfo, 349 CAMQ_GET_PRIO(&dev->ccbq.queue)); 350 } else { 351 retval = 0; 352 } 353 return (retval); 354} 355 356static __inline int 357periph_is_queued(struct cam_periph *periph) 358{ 359 return (periph->pinfo.index != CAM_UNQUEUED_INDEX); 360} 361 362static __inline int 363device_is_alloc_queued(struct cam_ed *device) 364{ 365 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 366} 367 368static __inline int 369device_is_send_queued(struct cam_ed *device) 370{ 371 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX); 372} 373 374static void 375xpt_periph_init() 376{ 377 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0"); 378} 379 380static void 381xptdone(struct cam_periph *periph, union ccb *done_ccb) 382{ 383 /* Caller will release the CCB */ 384 wakeup(&done_ccb->ccb_h.cbfcnp); 385} 386 387static int 388xptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 389{ 390 391 /* 392 * Only allow read-write access. 393 */ 394 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) 395 return(EPERM); 396 397 /* 398 * We don't allow nonblocking access. 399 */ 400 if ((flags & O_NONBLOCK) != 0) { 401 printf("%s: can't do nonblocking access\n", devtoname(dev)); 402 return(ENODEV); 403 } 404 405 /* Mark ourselves open */ 406 mtx_lock(&xsoftc.xpt_lock); 407 xsoftc.flags |= XPT_FLAG_OPEN; 408 mtx_unlock(&xsoftc.xpt_lock); 409 410 return(0); 411} 412 413static int 414xptclose(struct cdev *dev, int flag, int fmt, struct thread *td) 415{ 416 417 /* Mark ourselves closed */ 418 mtx_lock(&xsoftc.xpt_lock); 419 xsoftc.flags &= ~XPT_FLAG_OPEN; 420 mtx_unlock(&xsoftc.xpt_lock); 421 422 return(0); 423} 424 425/* 426 * Don't automatically grab the xpt softc lock here even though this is going 427 * through the xpt device. The xpt device is really just a back door for 428 * accessing other devices and SIMs, so the right thing to do is to grab 429 * the appropriate SIM lock once the bus/SIM is located. 430 */ 431static int 432xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 433{ 434 int error; 435 436 error = 0; 437 438 switch(cmd) { 439 /* 440 * For the transport layer CAMIOCOMMAND ioctl, we really only want 441 * to accept CCB types that don't quite make sense to send through a 442 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated 443 * in the CAM spec. 444 */ 445 case CAMIOCOMMAND: { 446 union ccb *ccb; 447 union ccb *inccb; 448 struct cam_eb *bus; 449 450 inccb = (union ccb *)addr; 451 452 bus = xpt_find_bus(inccb->ccb_h.path_id); 453 if (bus == NULL) 454 return (EINVAL); 455 456 switch (inccb->ccb_h.func_code) { 457 case XPT_SCAN_BUS: 458 case XPT_RESET_BUS: 459 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD || 460 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 461 xpt_release_bus(bus); 462 return (EINVAL); 463 } 464 break; 465 case XPT_SCAN_TGT: 466 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD || 467 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) { 468 xpt_release_bus(bus); 469 return (EINVAL); 470 } 471 break; 472 default: 473 break; 474 } 475 476 switch(inccb->ccb_h.func_code) { 477 case XPT_SCAN_BUS: 478 case XPT_RESET_BUS: 479 case XPT_PATH_INQ: 480 case XPT_ENG_INQ: 481 case XPT_SCAN_LUN: 482 case XPT_SCAN_TGT: 483 484 ccb = xpt_alloc_ccb(); 485 486 CAM_SIM_LOCK(bus->sim); 487 488 /* 489 * Create a path using the bus, target, and lun the 490 * user passed in. 491 */ 492 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, 493 inccb->ccb_h.path_id, 494 inccb->ccb_h.target_id, 495 inccb->ccb_h.target_lun) != 496 CAM_REQ_CMP){ 497 error = EINVAL; 498 CAM_SIM_UNLOCK(bus->sim); 499 xpt_free_ccb(ccb); 500 break; 501 } 502 /* Ensure all of our fields are correct */ 503 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 504 inccb->ccb_h.pinfo.priority); 505 xpt_merge_ccb(ccb, inccb); 506 ccb->ccb_h.cbfcnp = xptdone; 507 cam_periph_runccb(ccb, NULL, 0, 0, NULL); 508 bcopy(ccb, inccb, sizeof(union ccb)); 509 xpt_free_path(ccb->ccb_h.path); 510 xpt_free_ccb(ccb); 511 CAM_SIM_UNLOCK(bus->sim); 512 break; 513 514 case XPT_DEBUG: { 515 union ccb ccb; 516 517 /* 518 * This is an immediate CCB, so it's okay to 519 * allocate it on the stack. 520 */ 521 522 CAM_SIM_LOCK(bus->sim); 523 524 /* 525 * Create a path using the bus, target, and lun the 526 * user passed in. 527 */ 528 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph, 529 inccb->ccb_h.path_id, 530 inccb->ccb_h.target_id, 531 inccb->ccb_h.target_lun) != 532 CAM_REQ_CMP){ 533 error = EINVAL; 534 CAM_SIM_UNLOCK(bus->sim); 535 break; 536 } 537 /* Ensure all of our fields are correct */ 538 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path, 539 inccb->ccb_h.pinfo.priority); 540 xpt_merge_ccb(&ccb, inccb); 541 ccb.ccb_h.cbfcnp = xptdone; 542 xpt_action(&ccb); 543 CAM_SIM_UNLOCK(bus->sim); 544 bcopy(&ccb, inccb, sizeof(union ccb)); 545 xpt_free_path(ccb.ccb_h.path); 546 break; 547 548 } 549 case XPT_DEV_MATCH: { 550 struct cam_periph_map_info mapinfo; 551 struct cam_path *old_path; 552 553 /* 554 * We can't deal with physical addresses for this 555 * type of transaction. 556 */ 557 if (inccb->ccb_h.flags & CAM_DATA_PHYS) { 558 error = EINVAL; 559 break; 560 } 561 562 /* 563 * Save this in case the caller had it set to 564 * something in particular. 565 */ 566 old_path = inccb->ccb_h.path; 567 568 /* 569 * We really don't need a path for the matching 570 * code. The path is needed because of the 571 * debugging statements in xpt_action(). They 572 * assume that the CCB has a valid path. 573 */ 574 inccb->ccb_h.path = xpt_periph->path; 575 576 bzero(&mapinfo, sizeof(mapinfo)); 577 578 /* 579 * Map the pattern and match buffers into kernel 580 * virtual address space. 581 */ 582 error = cam_periph_mapmem(inccb, &mapinfo); 583 584 if (error) { 585 inccb->ccb_h.path = old_path; 586 break; 587 } 588 589 /* 590 * This is an immediate CCB, we can send it on directly. 591 */ 592 xpt_action(inccb); 593 594 /* 595 * Map the buffers back into user space. 596 */ 597 cam_periph_unmapmem(inccb, &mapinfo); 598 599 inccb->ccb_h.path = old_path; 600 601 error = 0; 602 break; 603 } 604 default: 605 error = ENOTSUP; 606 break; 607 } 608 xpt_release_bus(bus); 609 break; 610 } 611 /* 612 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input, 613 * with the periphal driver name and unit name filled in. The other 614 * fields don't really matter as input. The passthrough driver name 615 * ("pass"), and unit number are passed back in the ccb. The current 616 * device generation number, and the index into the device peripheral 617 * driver list, and the status are also passed back. Note that 618 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb, 619 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is 620 * (or rather should be) impossible for the device peripheral driver 621 * list to change since we look at the whole thing in one pass, and 622 * we do it with lock protection. 623 * 624 */ 625 case CAMGETPASSTHRU: { 626 union ccb *ccb; 627 struct cam_periph *periph; 628 struct periph_driver **p_drv; 629 char *name; 630 u_int unit; 631 u_int cur_generation; 632 int base_periph_found; 633 int splbreaknum; 634 635 ccb = (union ccb *)addr; 636 unit = ccb->cgdl.unit_number; 637 name = ccb->cgdl.periph_name; 638 /* 639 * Every 100 devices, we want to drop our lock protection to 640 * give the software interrupt handler a chance to run. 641 * Most systems won't run into this check, but this should 642 * avoid starvation in the software interrupt handler in 643 * large systems. 644 */ 645 splbreaknum = 100; 646 647 ccb = (union ccb *)addr; 648 649 base_periph_found = 0; 650 651 /* 652 * Sanity check -- make sure we don't get a null peripheral 653 * driver name. 654 */ 655 if (*ccb->cgdl.periph_name == '\0') { 656 error = EINVAL; 657 break; 658 } 659 660 /* Keep the list from changing while we traverse it */ 661 mtx_lock(&xsoftc.xpt_topo_lock); 662ptstartover: 663 cur_generation = xsoftc.xpt_generation; 664 665 /* first find our driver in the list of drivers */ 666 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) 667 if (strcmp((*p_drv)->driver_name, name) == 0) 668 break; 669 670 if (*p_drv == NULL) { 671 mtx_unlock(&xsoftc.xpt_topo_lock); 672 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 673 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 674 *ccb->cgdl.periph_name = '\0'; 675 ccb->cgdl.unit_number = 0; 676 error = ENOENT; 677 break; 678 } 679 680 /* 681 * Run through every peripheral instance of this driver 682 * and check to see whether it matches the unit passed 683 * in by the user. If it does, get out of the loops and 684 * find the passthrough driver associated with that 685 * peripheral driver. 686 */ 687 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL; 688 periph = TAILQ_NEXT(periph, unit_links)) { 689 690 if (periph->unit_number == unit) { 691 break; 692 } else if (--splbreaknum == 0) { 693 mtx_unlock(&xsoftc.xpt_topo_lock); 694 mtx_lock(&xsoftc.xpt_topo_lock); 695 splbreaknum = 100; 696 if (cur_generation != xsoftc.xpt_generation) 697 goto ptstartover; 698 } 699 } 700 /* 701 * If we found the peripheral driver that the user passed 702 * in, go through all of the peripheral drivers for that 703 * particular device and look for a passthrough driver. 704 */ 705 if (periph != NULL) { 706 struct cam_ed *device; 707 int i; 708 709 base_periph_found = 1; 710 device = periph->path->device; 711 for (i = 0, periph = SLIST_FIRST(&device->periphs); 712 periph != NULL; 713 periph = SLIST_NEXT(periph, periph_links), i++) { 714 /* 715 * Check to see whether we have a 716 * passthrough device or not. 717 */ 718 if (strcmp(periph->periph_name, "pass") == 0) { 719 /* 720 * Fill in the getdevlist fields. 721 */ 722 strcpy(ccb->cgdl.periph_name, 723 periph->periph_name); 724 ccb->cgdl.unit_number = 725 periph->unit_number; 726 if (SLIST_NEXT(periph, periph_links)) 727 ccb->cgdl.status = 728 CAM_GDEVLIST_MORE_DEVS; 729 else 730 ccb->cgdl.status = 731 CAM_GDEVLIST_LAST_DEVICE; 732 ccb->cgdl.generation = 733 device->generation; 734 ccb->cgdl.index = i; 735 /* 736 * Fill in some CCB header fields 737 * that the user may want. 738 */ 739 ccb->ccb_h.path_id = 740 periph->path->bus->path_id; 741 ccb->ccb_h.target_id = 742 periph->path->target->target_id; 743 ccb->ccb_h.target_lun = 744 periph->path->device->lun_id; 745 ccb->ccb_h.status = CAM_REQ_CMP; 746 break; 747 } 748 } 749 } 750 751 /* 752 * If the periph is null here, one of two things has 753 * happened. The first possibility is that we couldn't 754 * find the unit number of the particular peripheral driver 755 * that the user is asking about. e.g. the user asks for 756 * the passthrough driver for "da11". We find the list of 757 * "da" peripherals all right, but there is no unit 11. 758 * The other possibility is that we went through the list 759 * of peripheral drivers attached to the device structure, 760 * but didn't find one with the name "pass". Either way, 761 * we return ENOENT, since we couldn't find something. 762 */ 763 if (periph == NULL) { 764 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 765 ccb->cgdl.status = CAM_GDEVLIST_ERROR; 766 *ccb->cgdl.periph_name = '\0'; 767 ccb->cgdl.unit_number = 0; 768 error = ENOENT; 769 /* 770 * It is unfortunate that this is even necessary, 771 * but there are many, many clueless users out there. 772 * If this is true, the user is looking for the 773 * passthrough driver, but doesn't have one in his 774 * kernel. 775 */ 776 if (base_periph_found == 1) { 777 printf("xptioctl: pass driver is not in the " 778 "kernel\n"); 779 printf("xptioctl: put \"device pass\" in " 780 "your kernel config file\n"); 781 } 782 } 783 mtx_unlock(&xsoftc.xpt_topo_lock); 784 break; 785 } 786 default: 787 error = ENOTTY; 788 break; 789 } 790 791 return(error); 792} 793 794static int 795cam_module_event_handler(module_t mod, int what, void *arg) 796{ 797 int error; 798 799 switch (what) { 800 case MOD_LOAD: 801 if ((error = xpt_init(NULL)) != 0) 802 return (error); 803 break; 804 case MOD_UNLOAD: 805 return EBUSY; 806 default: 807 return EOPNOTSUPP; 808 } 809 810 return 0; 811} 812 813static void 814xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb) 815{ 816 817 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) { 818 xpt_free_path(done_ccb->ccb_h.path); 819 xpt_free_ccb(done_ccb); 820 } else { 821 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1; 822 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb); 823 } 824 xpt_release_boot(); 825} 826 827/* thread to handle bus rescans */ 828static void 829xpt_scanner_thread(void *dummy) 830{ 831 union ccb *ccb; 832 struct cam_sim *sim; 833 834 xpt_lock_buses(); 835 for (;;) { 836 if (TAILQ_EMPTY(&xsoftc.ccb_scanq)) 837 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO, 838 "ccb_scanq", 0); 839 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) { 840 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 841 xpt_unlock_buses(); 842 843 sim = ccb->ccb_h.path->bus->sim; 844 CAM_SIM_LOCK(sim); 845 xpt_action(ccb); 846 CAM_SIM_UNLOCK(sim); 847 848 xpt_lock_buses(); 849 } 850 } 851} 852 853void 854xpt_rescan(union ccb *ccb) 855{ 856 struct ccb_hdr *hdr; 857 858 /* Prepare request */ 859 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD && 860 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 861 ccb->ccb_h.func_code = XPT_SCAN_BUS; 862 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 863 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD) 864 ccb->ccb_h.func_code = XPT_SCAN_TGT; 865 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD && 866 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD) 867 ccb->ccb_h.func_code = XPT_SCAN_LUN; 868 else { 869 xpt_print(ccb->ccb_h.path, "illegal scan path\n"); 870 xpt_free_path(ccb->ccb_h.path); 871 xpt_free_ccb(ccb); 872 return; 873 } 874 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp; 875 ccb->ccb_h.cbfcnp = xpt_rescan_done; 876 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT); 877 /* Don't make duplicate entries for the same paths. */ 878 xpt_lock_buses(); 879 if (ccb->ccb_h.ppriv_ptr1 == NULL) { 880 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) { 881 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) { 882 wakeup(&xsoftc.ccb_scanq); 883 xpt_unlock_buses(); 884 xpt_print(ccb->ccb_h.path, "rescan already queued\n"); 885 xpt_free_path(ccb->ccb_h.path); 886 xpt_free_ccb(ccb); 887 return; 888 } 889 } 890 } 891 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe); 892 xsoftc.buses_to_config++; 893 wakeup(&xsoftc.ccb_scanq); 894 xpt_unlock_buses(); 895} 896 897/* Functions accessed by the peripheral drivers */ 898static int 899xpt_init(void *dummy) 900{ 901 struct cam_sim *xpt_sim; 902 struct cam_path *path; 903 struct cam_devq *devq; 904 cam_status status; 905 906 TAILQ_INIT(&xsoftc.xpt_busses); 907 TAILQ_INIT(&cam_simq); 908 TAILQ_INIT(&xsoftc.ccb_scanq); 909 STAILQ_INIT(&xsoftc.highpowerq); 910 xsoftc.num_highpower = CAM_MAX_HIGHPOWER; 911 912 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF); 913 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF); 914 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF); 915 916 /* 917 * The xpt layer is, itself, the equivelent of a SIM. 918 * Allow 16 ccbs in the ccb pool for it. This should 919 * give decent parallelism when we probe busses and 920 * perform other XPT functions. 921 */ 922 devq = cam_simq_alloc(16); 923 xpt_sim = cam_sim_alloc(xptaction, 924 xptpoll, 925 "xpt", 926 /*softc*/NULL, 927 /*unit*/0, 928 /*mtx*/&xsoftc.xpt_lock, 929 /*max_dev_transactions*/0, 930 /*max_tagged_dev_transactions*/0, 931 devq); 932 if (xpt_sim == NULL) 933 return (ENOMEM); 934 935 mtx_lock(&xsoftc.xpt_lock); 936 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) { 937 mtx_unlock(&xsoftc.xpt_lock); 938 printf("xpt_init: xpt_bus_register failed with status %#x," 939 " failing attach\n", status); 940 return (EINVAL); 941 } 942 943 /* 944 * Looking at the XPT from the SIM layer, the XPT is 945 * the equivelent of a peripheral driver. Allocate 946 * a peripheral driver entry for us. 947 */ 948 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID, 949 CAM_TARGET_WILDCARD, 950 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) { 951 mtx_unlock(&xsoftc.xpt_lock); 952 printf("xpt_init: xpt_create_path failed with status %#x," 953 " failing attach\n", status); 954 return (EINVAL); 955 } 956 957 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO, 958 path, NULL, 0, xpt_sim); 959 xpt_free_path(path); 960 mtx_unlock(&xsoftc.xpt_lock); 961 /* Install our software interrupt handlers */ 962 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih); 963 /* 964 * Register a callback for when interrupts are enabled. 965 */ 966 xsoftc.xpt_config_hook = 967 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook), 968 M_CAMXPT, M_NOWAIT | M_ZERO); 969 if (xsoftc.xpt_config_hook == NULL) { 970 printf("xpt_init: Cannot malloc config hook " 971 "- failing attach\n"); 972 return (ENOMEM); 973 } 974 xsoftc.xpt_config_hook->ich_func = xpt_config; 975 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) { 976 free (xsoftc.xpt_config_hook, M_CAMXPT); 977 printf("xpt_init: config_intrhook_establish failed " 978 "- failing attach\n"); 979 } 980 981 return (0); 982} 983 984static cam_status 985xptregister(struct cam_periph *periph, void *arg) 986{ 987 struct cam_sim *xpt_sim; 988 989 if (periph == NULL) { 990 printf("xptregister: periph was NULL!!\n"); 991 return(CAM_REQ_CMP_ERR); 992 } 993 994 xpt_sim = (struct cam_sim *)arg; 995 xpt_sim->softc = periph; 996 xpt_periph = periph; 997 periph->softc = NULL; 998 999 return(CAM_REQ_CMP); 1000} 1001 1002int32_t 1003xpt_add_periph(struct cam_periph *periph) 1004{ 1005 struct cam_ed *device; 1006 int32_t status; 1007 struct periph_list *periph_head; 1008 1009 mtx_assert(periph->sim->mtx, MA_OWNED); 1010 1011 device = periph->path->device; 1012 1013 periph_head = &device->periphs; 1014 1015 status = CAM_REQ_CMP; 1016 1017 if (device != NULL) { 1018 /* 1019 * Make room for this peripheral 1020 * so it will fit in the queue 1021 * when it's scheduled to run 1022 */ 1023 status = camq_resize(&device->drvq, 1024 device->drvq.array_size + 1); 1025 1026 device->generation++; 1027 1028 SLIST_INSERT_HEAD(periph_head, periph, periph_links); 1029 } 1030 1031 mtx_lock(&xsoftc.xpt_topo_lock); 1032 xsoftc.xpt_generation++; 1033 mtx_unlock(&xsoftc.xpt_topo_lock); 1034 1035 return (status); 1036} 1037 1038void 1039xpt_remove_periph(struct cam_periph *periph) 1040{ 1041 struct cam_ed *device; 1042 1043 mtx_assert(periph->sim->mtx, MA_OWNED); 1044 1045 device = periph->path->device; 1046 1047 if (device != NULL) { 1048 struct periph_list *periph_head; 1049 1050 periph_head = &device->periphs; 1051 1052 /* Release the slot for this peripheral */ 1053 camq_resize(&device->drvq, device->drvq.array_size - 1); 1054 1055 device->generation++; 1056 1057 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links); 1058 } 1059 1060 mtx_lock(&xsoftc.xpt_topo_lock); 1061 xsoftc.xpt_generation++; 1062 mtx_unlock(&xsoftc.xpt_topo_lock); 1063} 1064 1065 1066void 1067xpt_announce_periph(struct cam_periph *periph, char *announce_string) 1068{ 1069 struct cam_path *path = periph->path; 1070 1071 mtx_assert(periph->sim->mtx, MA_OWNED); 1072 1073 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n", 1074 periph->periph_name, periph->unit_number, 1075 path->bus->sim->sim_name, 1076 path->bus->sim->unit_number, 1077 path->bus->sim->bus_id, 1078 path->bus->path_id, 1079 path->target->target_id, 1080 path->device->lun_id); 1081 printf("%s%d: ", periph->periph_name, periph->unit_number); 1082 if (path->device->protocol == PROTO_SCSI) 1083 scsi_print_inquiry(&path->device->inq_data); 1084 else if (path->device->protocol == PROTO_ATA || 1085 path->device->protocol == PROTO_SATAPM) 1086 ata_print_ident(&path->device->ident_data); 1087 else 1088 printf("Unknown protocol device\n"); 1089 if (bootverbose && path->device->serial_num_len > 0) { 1090 /* Don't wrap the screen - print only the first 60 chars */ 1091 printf("%s%d: Serial Number %.60s\n", periph->periph_name, 1092 periph->unit_number, path->device->serial_num); 1093 } 1094 /* Announce transport details. */ 1095 (*(path->bus->xport->announce))(periph); 1096 /* Announce command queueing. */ 1097 if (path->device->inq_flags & SID_CmdQue 1098 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) { 1099 printf("%s%d: Command Queueing enabled\n", 1100 periph->periph_name, periph->unit_number); 1101 } 1102 /* Announce caller's details if they've passed in. */ 1103 if (announce_string != NULL) 1104 printf("%s%d: %s\n", periph->periph_name, 1105 periph->unit_number, announce_string); 1106} 1107 1108static dev_match_ret 1109xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns, 1110 struct cam_eb *bus) 1111{ 1112 dev_match_ret retval; 1113 int i; 1114 1115 retval = DM_RET_NONE; 1116 1117 /* 1118 * If we aren't given something to match against, that's an error. 1119 */ 1120 if (bus == NULL) 1121 return(DM_RET_ERROR); 1122 1123 /* 1124 * If there are no match entries, then this bus matches no 1125 * matter what. 1126 */ 1127 if ((patterns == NULL) || (num_patterns == 0)) 1128 return(DM_RET_DESCEND | DM_RET_COPY); 1129 1130 for (i = 0; i < num_patterns; i++) { 1131 struct bus_match_pattern *cur_pattern; 1132 1133 /* 1134 * If the pattern in question isn't for a bus node, we 1135 * aren't interested. However, we do indicate to the 1136 * calling routine that we should continue descending the 1137 * tree, since the user wants to match against lower-level 1138 * EDT elements. 1139 */ 1140 if (patterns[i].type != DEV_MATCH_BUS) { 1141 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1142 retval |= DM_RET_DESCEND; 1143 continue; 1144 } 1145 1146 cur_pattern = &patterns[i].pattern.bus_pattern; 1147 1148 /* 1149 * If they want to match any bus node, we give them any 1150 * device node. 1151 */ 1152 if (cur_pattern->flags == BUS_MATCH_ANY) { 1153 /* set the copy flag */ 1154 retval |= DM_RET_COPY; 1155 1156 /* 1157 * If we've already decided on an action, go ahead 1158 * and return. 1159 */ 1160 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1161 return(retval); 1162 } 1163 1164 /* 1165 * Not sure why someone would do this... 1166 */ 1167 if (cur_pattern->flags == BUS_MATCH_NONE) 1168 continue; 1169 1170 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0) 1171 && (cur_pattern->path_id != bus->path_id)) 1172 continue; 1173 1174 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0) 1175 && (cur_pattern->bus_id != bus->sim->bus_id)) 1176 continue; 1177 1178 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0) 1179 && (cur_pattern->unit_number != bus->sim->unit_number)) 1180 continue; 1181 1182 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0) 1183 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name, 1184 DEV_IDLEN) != 0)) 1185 continue; 1186 1187 /* 1188 * If we get to this point, the user definitely wants 1189 * information on this bus. So tell the caller to copy the 1190 * data out. 1191 */ 1192 retval |= DM_RET_COPY; 1193 1194 /* 1195 * If the return action has been set to descend, then we 1196 * know that we've already seen a non-bus matching 1197 * expression, therefore we need to further descend the tree. 1198 * This won't change by continuing around the loop, so we 1199 * go ahead and return. If we haven't seen a non-bus 1200 * matching expression, we keep going around the loop until 1201 * we exhaust the matching expressions. We'll set the stop 1202 * flag once we fall out of the loop. 1203 */ 1204 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1205 return(retval); 1206 } 1207 1208 /* 1209 * If the return action hasn't been set to descend yet, that means 1210 * we haven't seen anything other than bus matching patterns. So 1211 * tell the caller to stop descending the tree -- the user doesn't 1212 * want to match against lower level tree elements. 1213 */ 1214 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1215 retval |= DM_RET_STOP; 1216 1217 return(retval); 1218} 1219 1220static dev_match_ret 1221xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns, 1222 struct cam_ed *device) 1223{ 1224 dev_match_ret retval; 1225 int i; 1226 1227 retval = DM_RET_NONE; 1228 1229 /* 1230 * If we aren't given something to match against, that's an error. 1231 */ 1232 if (device == NULL) 1233 return(DM_RET_ERROR); 1234 1235 /* 1236 * If there are no match entries, then this device matches no 1237 * matter what. 1238 */ 1239 if ((patterns == NULL) || (num_patterns == 0)) 1240 return(DM_RET_DESCEND | DM_RET_COPY); 1241 1242 for (i = 0; i < num_patterns; i++) { 1243 struct device_match_pattern *cur_pattern; 1244 1245 /* 1246 * If the pattern in question isn't for a device node, we 1247 * aren't interested. 1248 */ 1249 if (patterns[i].type != DEV_MATCH_DEVICE) { 1250 if ((patterns[i].type == DEV_MATCH_PERIPH) 1251 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)) 1252 retval |= DM_RET_DESCEND; 1253 continue; 1254 } 1255 1256 cur_pattern = &patterns[i].pattern.device_pattern; 1257 1258 /* 1259 * If they want to match any device node, we give them any 1260 * device node. 1261 */ 1262 if (cur_pattern->flags == DEV_MATCH_ANY) { 1263 /* set the copy flag */ 1264 retval |= DM_RET_COPY; 1265 1266 1267 /* 1268 * If we've already decided on an action, go ahead 1269 * and return. 1270 */ 1271 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE) 1272 return(retval); 1273 } 1274 1275 /* 1276 * Not sure why someone would do this... 1277 */ 1278 if (cur_pattern->flags == DEV_MATCH_NONE) 1279 continue; 1280 1281 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0) 1282 && (cur_pattern->path_id != device->target->bus->path_id)) 1283 continue; 1284 1285 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0) 1286 && (cur_pattern->target_id != device->target->target_id)) 1287 continue; 1288 1289 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0) 1290 && (cur_pattern->target_lun != device->lun_id)) 1291 continue; 1292 1293 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0) 1294 && (cam_quirkmatch((caddr_t)&device->inq_data, 1295 (caddr_t)&cur_pattern->inq_pat, 1296 1, sizeof(cur_pattern->inq_pat), 1297 scsi_static_inquiry_match) == NULL)) 1298 continue; 1299 1300 /* 1301 * If we get to this point, the user definitely wants 1302 * information on this device. So tell the caller to copy 1303 * the data out. 1304 */ 1305 retval |= DM_RET_COPY; 1306 1307 /* 1308 * If the return action has been set to descend, then we 1309 * know that we've already seen a peripheral matching 1310 * expression, therefore we need to further descend the tree. 1311 * This won't change by continuing around the loop, so we 1312 * go ahead and return. If we haven't seen a peripheral 1313 * matching expression, we keep going around the loop until 1314 * we exhaust the matching expressions. We'll set the stop 1315 * flag once we fall out of the loop. 1316 */ 1317 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND) 1318 return(retval); 1319 } 1320 1321 /* 1322 * If the return action hasn't been set to descend yet, that means 1323 * we haven't seen any peripheral matching patterns. So tell the 1324 * caller to stop descending the tree -- the user doesn't want to 1325 * match against lower level tree elements. 1326 */ 1327 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE) 1328 retval |= DM_RET_STOP; 1329 1330 return(retval); 1331} 1332 1333/* 1334 * Match a single peripheral against any number of match patterns. 1335 */ 1336static dev_match_ret 1337xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns, 1338 struct cam_periph *periph) 1339{ 1340 dev_match_ret retval; 1341 int i; 1342 1343 /* 1344 * If we aren't given something to match against, that's an error. 1345 */ 1346 if (periph == NULL) 1347 return(DM_RET_ERROR); 1348 1349 /* 1350 * If there are no match entries, then this peripheral matches no 1351 * matter what. 1352 */ 1353 if ((patterns == NULL) || (num_patterns == 0)) 1354 return(DM_RET_STOP | DM_RET_COPY); 1355 1356 /* 1357 * There aren't any nodes below a peripheral node, so there's no 1358 * reason to descend the tree any further. 1359 */ 1360 retval = DM_RET_STOP; 1361 1362 for (i = 0; i < num_patterns; i++) { 1363 struct periph_match_pattern *cur_pattern; 1364 1365 /* 1366 * If the pattern in question isn't for a peripheral, we 1367 * aren't interested. 1368 */ 1369 if (patterns[i].type != DEV_MATCH_PERIPH) 1370 continue; 1371 1372 cur_pattern = &patterns[i].pattern.periph_pattern; 1373 1374 /* 1375 * If they want to match on anything, then we will do so. 1376 */ 1377 if (cur_pattern->flags == PERIPH_MATCH_ANY) { 1378 /* set the copy flag */ 1379 retval |= DM_RET_COPY; 1380 1381 /* 1382 * We've already set the return action to stop, 1383 * since there are no nodes below peripherals in 1384 * the tree. 1385 */ 1386 return(retval); 1387 } 1388 1389 /* 1390 * Not sure why someone would do this... 1391 */ 1392 if (cur_pattern->flags == PERIPH_MATCH_NONE) 1393 continue; 1394 1395 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0) 1396 && (cur_pattern->path_id != periph->path->bus->path_id)) 1397 continue; 1398 1399 /* 1400 * For the target and lun id's, we have to make sure the 1401 * target and lun pointers aren't NULL. The xpt peripheral 1402 * has a wildcard target and device. 1403 */ 1404 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0) 1405 && ((periph->path->target == NULL) 1406 ||(cur_pattern->target_id != periph->path->target->target_id))) 1407 continue; 1408 1409 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0) 1410 && ((periph->path->device == NULL) 1411 || (cur_pattern->target_lun != periph->path->device->lun_id))) 1412 continue; 1413 1414 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0) 1415 && (cur_pattern->unit_number != periph->unit_number)) 1416 continue; 1417 1418 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0) 1419 && (strncmp(cur_pattern->periph_name, periph->periph_name, 1420 DEV_IDLEN) != 0)) 1421 continue; 1422 1423 /* 1424 * If we get to this point, the user definitely wants 1425 * information on this peripheral. So tell the caller to 1426 * copy the data out. 1427 */ 1428 retval |= DM_RET_COPY; 1429 1430 /* 1431 * The return action has already been set to stop, since 1432 * peripherals don't have any nodes below them in the EDT. 1433 */ 1434 return(retval); 1435 } 1436 1437 /* 1438 * If we get to this point, the peripheral that was passed in 1439 * doesn't match any of the patterns. 1440 */ 1441 return(retval); 1442} 1443 1444static int 1445xptedtbusfunc(struct cam_eb *bus, void *arg) 1446{ 1447 struct ccb_dev_match *cdm; 1448 dev_match_ret retval; 1449 1450 cdm = (struct ccb_dev_match *)arg; 1451 1452 /* 1453 * If our position is for something deeper in the tree, that means 1454 * that we've already seen this node. So, we keep going down. 1455 */ 1456 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1457 && (cdm->pos.cookie.bus == bus) 1458 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1459 && (cdm->pos.cookie.target != NULL)) 1460 retval = DM_RET_DESCEND; 1461 else 1462 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus); 1463 1464 /* 1465 * If we got an error, bail out of the search. 1466 */ 1467 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1468 cdm->status = CAM_DEV_MATCH_ERROR; 1469 return(0); 1470 } 1471 1472 /* 1473 * If the copy flag is set, copy this bus out. 1474 */ 1475 if (retval & DM_RET_COPY) { 1476 int spaceleft, j; 1477 1478 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1479 sizeof(struct dev_match_result)); 1480 1481 /* 1482 * If we don't have enough space to put in another 1483 * match result, save our position and tell the 1484 * user there are more devices to check. 1485 */ 1486 if (spaceleft < sizeof(struct dev_match_result)) { 1487 bzero(&cdm->pos, sizeof(cdm->pos)); 1488 cdm->pos.position_type = 1489 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS; 1490 1491 cdm->pos.cookie.bus = bus; 1492 cdm->pos.generations[CAM_BUS_GENERATION]= 1493 xsoftc.bus_generation; 1494 cdm->status = CAM_DEV_MATCH_MORE; 1495 return(0); 1496 } 1497 j = cdm->num_matches; 1498 cdm->num_matches++; 1499 cdm->matches[j].type = DEV_MATCH_BUS; 1500 cdm->matches[j].result.bus_result.path_id = bus->path_id; 1501 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id; 1502 cdm->matches[j].result.bus_result.unit_number = 1503 bus->sim->unit_number; 1504 strncpy(cdm->matches[j].result.bus_result.dev_name, 1505 bus->sim->sim_name, DEV_IDLEN); 1506 } 1507 1508 /* 1509 * If the user is only interested in busses, there's no 1510 * reason to descend to the next level in the tree. 1511 */ 1512 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 1513 return(1); 1514 1515 /* 1516 * If there is a target generation recorded, check it to 1517 * make sure the target list hasn't changed. 1518 */ 1519 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1520 && (bus == cdm->pos.cookie.bus) 1521 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1522 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0) 1523 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 1524 bus->generation)) { 1525 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1526 return(0); 1527 } 1528 1529 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1530 && (cdm->pos.cookie.bus == bus) 1531 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1532 && (cdm->pos.cookie.target != NULL)) 1533 return(xpttargettraverse(bus, 1534 (struct cam_et *)cdm->pos.cookie.target, 1535 xptedttargetfunc, arg)); 1536 else 1537 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg)); 1538} 1539 1540static int 1541xptedttargetfunc(struct cam_et *target, void *arg) 1542{ 1543 struct ccb_dev_match *cdm; 1544 1545 cdm = (struct ccb_dev_match *)arg; 1546 1547 /* 1548 * If there is a device list generation recorded, check it to 1549 * make sure the device list hasn't changed. 1550 */ 1551 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1552 && (cdm->pos.cookie.bus == target->bus) 1553 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1554 && (cdm->pos.cookie.target == target) 1555 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1556 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0) 1557 && (cdm->pos.generations[CAM_DEV_GENERATION] != 1558 target->generation)) { 1559 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1560 return(0); 1561 } 1562 1563 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1564 && (cdm->pos.cookie.bus == target->bus) 1565 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1566 && (cdm->pos.cookie.target == target) 1567 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1568 && (cdm->pos.cookie.device != NULL)) 1569 return(xptdevicetraverse(target, 1570 (struct cam_ed *)cdm->pos.cookie.device, 1571 xptedtdevicefunc, arg)); 1572 else 1573 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg)); 1574} 1575 1576static int 1577xptedtdevicefunc(struct cam_ed *device, void *arg) 1578{ 1579 1580 struct ccb_dev_match *cdm; 1581 dev_match_ret retval; 1582 1583 cdm = (struct ccb_dev_match *)arg; 1584 1585 /* 1586 * If our position is for something deeper in the tree, that means 1587 * that we've already seen this node. So, we keep going down. 1588 */ 1589 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1590 && (cdm->pos.cookie.device == device) 1591 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1592 && (cdm->pos.cookie.periph != NULL)) 1593 retval = DM_RET_DESCEND; 1594 else 1595 retval = xptdevicematch(cdm->patterns, cdm->num_patterns, 1596 device); 1597 1598 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1599 cdm->status = CAM_DEV_MATCH_ERROR; 1600 return(0); 1601 } 1602 1603 /* 1604 * If the copy flag is set, copy this device out. 1605 */ 1606 if (retval & DM_RET_COPY) { 1607 int spaceleft, j; 1608 1609 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1610 sizeof(struct dev_match_result)); 1611 1612 /* 1613 * If we don't have enough space to put in another 1614 * match result, save our position and tell the 1615 * user there are more devices to check. 1616 */ 1617 if (spaceleft < sizeof(struct dev_match_result)) { 1618 bzero(&cdm->pos, sizeof(cdm->pos)); 1619 cdm->pos.position_type = 1620 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 1621 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE; 1622 1623 cdm->pos.cookie.bus = device->target->bus; 1624 cdm->pos.generations[CAM_BUS_GENERATION]= 1625 xsoftc.bus_generation; 1626 cdm->pos.cookie.target = device->target; 1627 cdm->pos.generations[CAM_TARGET_GENERATION] = 1628 device->target->bus->generation; 1629 cdm->pos.cookie.device = device; 1630 cdm->pos.generations[CAM_DEV_GENERATION] = 1631 device->target->generation; 1632 cdm->status = CAM_DEV_MATCH_MORE; 1633 return(0); 1634 } 1635 j = cdm->num_matches; 1636 cdm->num_matches++; 1637 cdm->matches[j].type = DEV_MATCH_DEVICE; 1638 cdm->matches[j].result.device_result.path_id = 1639 device->target->bus->path_id; 1640 cdm->matches[j].result.device_result.target_id = 1641 device->target->target_id; 1642 cdm->matches[j].result.device_result.target_lun = 1643 device->lun_id; 1644 cdm->matches[j].result.device_result.protocol = 1645 device->protocol; 1646 bcopy(&device->inq_data, 1647 &cdm->matches[j].result.device_result.inq_data, 1648 sizeof(struct scsi_inquiry_data)); 1649 bcopy(&device->ident_data, 1650 &cdm->matches[j].result.device_result.ident_data, 1651 sizeof(struct ata_params)); 1652 1653 /* Let the user know whether this device is unconfigured */ 1654 if (device->flags & CAM_DEV_UNCONFIGURED) 1655 cdm->matches[j].result.device_result.flags = 1656 DEV_RESULT_UNCONFIGURED; 1657 else 1658 cdm->matches[j].result.device_result.flags = 1659 DEV_RESULT_NOFLAG; 1660 } 1661 1662 /* 1663 * If the user isn't interested in peripherals, don't descend 1664 * the tree any further. 1665 */ 1666 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP) 1667 return(1); 1668 1669 /* 1670 * If there is a peripheral list generation recorded, make sure 1671 * it hasn't changed. 1672 */ 1673 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1674 && (device->target->bus == cdm->pos.cookie.bus) 1675 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1676 && (device->target == cdm->pos.cookie.target) 1677 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1678 && (device == cdm->pos.cookie.device) 1679 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1680 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 1681 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1682 device->generation)){ 1683 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1684 return(0); 1685 } 1686 1687 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1688 && (cdm->pos.cookie.bus == device->target->bus) 1689 && (cdm->pos.position_type & CAM_DEV_POS_TARGET) 1690 && (cdm->pos.cookie.target == device->target) 1691 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE) 1692 && (cdm->pos.cookie.device == device) 1693 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1694 && (cdm->pos.cookie.periph != NULL)) 1695 return(xptperiphtraverse(device, 1696 (struct cam_periph *)cdm->pos.cookie.periph, 1697 xptedtperiphfunc, arg)); 1698 else 1699 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg)); 1700} 1701 1702static int 1703xptedtperiphfunc(struct cam_periph *periph, void *arg) 1704{ 1705 struct ccb_dev_match *cdm; 1706 dev_match_ret retval; 1707 1708 cdm = (struct ccb_dev_match *)arg; 1709 1710 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 1711 1712 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1713 cdm->status = CAM_DEV_MATCH_ERROR; 1714 return(0); 1715 } 1716 1717 /* 1718 * If the copy flag is set, copy this peripheral out. 1719 */ 1720 if (retval & DM_RET_COPY) { 1721 int spaceleft, j; 1722 1723 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1724 sizeof(struct dev_match_result)); 1725 1726 /* 1727 * If we don't have enough space to put in another 1728 * match result, save our position and tell the 1729 * user there are more devices to check. 1730 */ 1731 if (spaceleft < sizeof(struct dev_match_result)) { 1732 bzero(&cdm->pos, sizeof(cdm->pos)); 1733 cdm->pos.position_type = 1734 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS | 1735 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE | 1736 CAM_DEV_POS_PERIPH; 1737 1738 cdm->pos.cookie.bus = periph->path->bus; 1739 cdm->pos.generations[CAM_BUS_GENERATION]= 1740 xsoftc.bus_generation; 1741 cdm->pos.cookie.target = periph->path->target; 1742 cdm->pos.generations[CAM_TARGET_GENERATION] = 1743 periph->path->bus->generation; 1744 cdm->pos.cookie.device = periph->path->device; 1745 cdm->pos.generations[CAM_DEV_GENERATION] = 1746 periph->path->target->generation; 1747 cdm->pos.cookie.periph = periph; 1748 cdm->pos.generations[CAM_PERIPH_GENERATION] = 1749 periph->path->device->generation; 1750 cdm->status = CAM_DEV_MATCH_MORE; 1751 return(0); 1752 } 1753 1754 j = cdm->num_matches; 1755 cdm->num_matches++; 1756 cdm->matches[j].type = DEV_MATCH_PERIPH; 1757 cdm->matches[j].result.periph_result.path_id = 1758 periph->path->bus->path_id; 1759 cdm->matches[j].result.periph_result.target_id = 1760 periph->path->target->target_id; 1761 cdm->matches[j].result.periph_result.target_lun = 1762 periph->path->device->lun_id; 1763 cdm->matches[j].result.periph_result.unit_number = 1764 periph->unit_number; 1765 strncpy(cdm->matches[j].result.periph_result.periph_name, 1766 periph->periph_name, DEV_IDLEN); 1767 } 1768 1769 return(1); 1770} 1771 1772static int 1773xptedtmatch(struct ccb_dev_match *cdm) 1774{ 1775 int ret; 1776 1777 cdm->num_matches = 0; 1778 1779 /* 1780 * Check the bus list generation. If it has changed, the user 1781 * needs to reset everything and start over. 1782 */ 1783 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1784 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0) 1785 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) { 1786 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1787 return(0); 1788 } 1789 1790 if ((cdm->pos.position_type & CAM_DEV_POS_BUS) 1791 && (cdm->pos.cookie.bus != NULL)) 1792 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus, 1793 xptedtbusfunc, cdm); 1794 else 1795 ret = xptbustraverse(NULL, xptedtbusfunc, cdm); 1796 1797 /* 1798 * If we get back 0, that means that we had to stop before fully 1799 * traversing the EDT. It also means that one of the subroutines 1800 * has set the status field to the proper value. If we get back 1, 1801 * we've fully traversed the EDT and copied out any matching entries. 1802 */ 1803 if (ret == 1) 1804 cdm->status = CAM_DEV_MATCH_LAST; 1805 1806 return(ret); 1807} 1808 1809static int 1810xptplistpdrvfunc(struct periph_driver **pdrv, void *arg) 1811{ 1812 struct ccb_dev_match *cdm; 1813 1814 cdm = (struct ccb_dev_match *)arg; 1815 1816 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1817 && (cdm->pos.cookie.pdrv == pdrv) 1818 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1819 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0) 1820 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 1821 (*pdrv)->generation)) { 1822 cdm->status = CAM_DEV_MATCH_LIST_CHANGED; 1823 return(0); 1824 } 1825 1826 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1827 && (cdm->pos.cookie.pdrv == pdrv) 1828 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH) 1829 && (cdm->pos.cookie.periph != NULL)) 1830 return(xptpdperiphtraverse(pdrv, 1831 (struct cam_periph *)cdm->pos.cookie.periph, 1832 xptplistperiphfunc, arg)); 1833 else 1834 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg)); 1835} 1836 1837static int 1838xptplistperiphfunc(struct cam_periph *periph, void *arg) 1839{ 1840 struct ccb_dev_match *cdm; 1841 dev_match_ret retval; 1842 1843 cdm = (struct ccb_dev_match *)arg; 1844 1845 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph); 1846 1847 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) { 1848 cdm->status = CAM_DEV_MATCH_ERROR; 1849 return(0); 1850 } 1851 1852 /* 1853 * If the copy flag is set, copy this peripheral out. 1854 */ 1855 if (retval & DM_RET_COPY) { 1856 int spaceleft, j; 1857 1858 spaceleft = cdm->match_buf_len - (cdm->num_matches * 1859 sizeof(struct dev_match_result)); 1860 1861 /* 1862 * If we don't have enough space to put in another 1863 * match result, save our position and tell the 1864 * user there are more devices to check. 1865 */ 1866 if (spaceleft < sizeof(struct dev_match_result)) { 1867 struct periph_driver **pdrv; 1868 1869 pdrv = NULL; 1870 bzero(&cdm->pos, sizeof(cdm->pos)); 1871 cdm->pos.position_type = 1872 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR | 1873 CAM_DEV_POS_PERIPH; 1874 1875 /* 1876 * This may look a bit non-sensical, but it is 1877 * actually quite logical. There are very few 1878 * peripheral drivers, and bloating every peripheral 1879 * structure with a pointer back to its parent 1880 * peripheral driver linker set entry would cost 1881 * more in the long run than doing this quick lookup. 1882 */ 1883 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) { 1884 if (strcmp((*pdrv)->driver_name, 1885 periph->periph_name) == 0) 1886 break; 1887 } 1888 1889 if (*pdrv == NULL) { 1890 cdm->status = CAM_DEV_MATCH_ERROR; 1891 return(0); 1892 } 1893 1894 cdm->pos.cookie.pdrv = pdrv; 1895 /* 1896 * The periph generation slot does double duty, as 1897 * does the periph pointer slot. They are used for 1898 * both edt and pdrv lookups and positioning. 1899 */ 1900 cdm->pos.cookie.periph = periph; 1901 cdm->pos.generations[CAM_PERIPH_GENERATION] = 1902 (*pdrv)->generation; 1903 cdm->status = CAM_DEV_MATCH_MORE; 1904 return(0); 1905 } 1906 1907 j = cdm->num_matches; 1908 cdm->num_matches++; 1909 cdm->matches[j].type = DEV_MATCH_PERIPH; 1910 cdm->matches[j].result.periph_result.path_id = 1911 periph->path->bus->path_id; 1912 1913 /* 1914 * The transport layer peripheral doesn't have a target or 1915 * lun. 1916 */ 1917 if (periph->path->target) 1918 cdm->matches[j].result.periph_result.target_id = 1919 periph->path->target->target_id; 1920 else 1921 cdm->matches[j].result.periph_result.target_id = -1; 1922 1923 if (periph->path->device) 1924 cdm->matches[j].result.periph_result.target_lun = 1925 periph->path->device->lun_id; 1926 else 1927 cdm->matches[j].result.periph_result.target_lun = -1; 1928 1929 cdm->matches[j].result.periph_result.unit_number = 1930 periph->unit_number; 1931 strncpy(cdm->matches[j].result.periph_result.periph_name, 1932 periph->periph_name, DEV_IDLEN); 1933 } 1934 1935 return(1); 1936} 1937 1938static int 1939xptperiphlistmatch(struct ccb_dev_match *cdm) 1940{ 1941 int ret; 1942 1943 cdm->num_matches = 0; 1944 1945 /* 1946 * At this point in the edt traversal function, we check the bus 1947 * list generation to make sure that no busses have been added or 1948 * removed since the user last sent a XPT_DEV_MATCH ccb through. 1949 * For the peripheral driver list traversal function, however, we 1950 * don't have to worry about new peripheral driver types coming or 1951 * going; they're in a linker set, and therefore can't change 1952 * without a recompile. 1953 */ 1954 1955 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR) 1956 && (cdm->pos.cookie.pdrv != NULL)) 1957 ret = xptpdrvtraverse( 1958 (struct periph_driver **)cdm->pos.cookie.pdrv, 1959 xptplistpdrvfunc, cdm); 1960 else 1961 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm); 1962 1963 /* 1964 * If we get back 0, that means that we had to stop before fully 1965 * traversing the peripheral driver tree. It also means that one of 1966 * the subroutines has set the status field to the proper value. If 1967 * we get back 1, we've fully traversed the EDT and copied out any 1968 * matching entries. 1969 */ 1970 if (ret == 1) 1971 cdm->status = CAM_DEV_MATCH_LAST; 1972 1973 return(ret); 1974} 1975 1976static int 1977xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg) 1978{ 1979 struct cam_eb *bus, *next_bus; 1980 int retval; 1981 1982 retval = 1; 1983 1984 mtx_lock(&xsoftc.xpt_topo_lock); 1985 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses)); 1986 bus != NULL; 1987 bus = next_bus) { 1988 next_bus = TAILQ_NEXT(bus, links); 1989 1990 mtx_unlock(&xsoftc.xpt_topo_lock); 1991 CAM_SIM_LOCK(bus->sim); 1992 retval = tr_func(bus, arg); 1993 CAM_SIM_UNLOCK(bus->sim); 1994 if (retval == 0) 1995 return(retval); 1996 mtx_lock(&xsoftc.xpt_topo_lock); 1997 } 1998 mtx_unlock(&xsoftc.xpt_topo_lock); 1999 2000 return(retval); 2001} 2002 2003int 2004xpt_sim_opened(struct cam_sim *sim) 2005{ 2006 struct cam_eb *bus; 2007 struct cam_et *target; 2008 struct cam_ed *device; 2009 struct cam_periph *periph; 2010 2011 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 2012 mtx_assert(sim->mtx, MA_OWNED); 2013 2014 mtx_lock(&xsoftc.xpt_topo_lock); 2015 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) { 2016 if (bus->sim != sim) 2017 continue; 2018 2019 TAILQ_FOREACH(target, &bus->et_entries, links) { 2020 TAILQ_FOREACH(device, &target->ed_entries, links) { 2021 SLIST_FOREACH(periph, &device->periphs, 2022 periph_links) { 2023 if (periph->refcount > 0) { 2024 mtx_unlock(&xsoftc.xpt_topo_lock); 2025 return (1); 2026 } 2027 } 2028 } 2029 } 2030 } 2031 2032 mtx_unlock(&xsoftc.xpt_topo_lock); 2033 return (0); 2034} 2035 2036static int 2037xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target, 2038 xpt_targetfunc_t *tr_func, void *arg) 2039{ 2040 struct cam_et *target, *next_target; 2041 int retval; 2042 2043 retval = 1; 2044 for (target = (start_target ? start_target : 2045 TAILQ_FIRST(&bus->et_entries)); 2046 target != NULL; target = next_target) { 2047 2048 next_target = TAILQ_NEXT(target, links); 2049 2050 retval = tr_func(target, arg); 2051 2052 if (retval == 0) 2053 return(retval); 2054 } 2055 2056 return(retval); 2057} 2058 2059static int 2060xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device, 2061 xpt_devicefunc_t *tr_func, void *arg) 2062{ 2063 struct cam_ed *device, *next_device; 2064 int retval; 2065 2066 retval = 1; 2067 for (device = (start_device ? start_device : 2068 TAILQ_FIRST(&target->ed_entries)); 2069 device != NULL; 2070 device = next_device) { 2071 2072 next_device = TAILQ_NEXT(device, links); 2073 2074 retval = tr_func(device, arg); 2075 2076 if (retval == 0) 2077 return(retval); 2078 } 2079 2080 return(retval); 2081} 2082 2083static int 2084xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph, 2085 xpt_periphfunc_t *tr_func, void *arg) 2086{ 2087 struct cam_periph *periph, *next_periph; 2088 int retval; 2089 2090 retval = 1; 2091 2092 for (periph = (start_periph ? start_periph : 2093 SLIST_FIRST(&device->periphs)); 2094 periph != NULL; 2095 periph = next_periph) { 2096 2097 next_periph = SLIST_NEXT(periph, periph_links); 2098 2099 retval = tr_func(periph, arg); 2100 if (retval == 0) 2101 return(retval); 2102 } 2103 2104 return(retval); 2105} 2106 2107static int 2108xptpdrvtraverse(struct periph_driver **start_pdrv, 2109 xpt_pdrvfunc_t *tr_func, void *arg) 2110{ 2111 struct periph_driver **pdrv; 2112 int retval; 2113 2114 retval = 1; 2115 2116 /* 2117 * We don't traverse the peripheral driver list like we do the 2118 * other lists, because it is a linker set, and therefore cannot be 2119 * changed during runtime. If the peripheral driver list is ever 2120 * re-done to be something other than a linker set (i.e. it can 2121 * change while the system is running), the list traversal should 2122 * be modified to work like the other traversal functions. 2123 */ 2124 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers); 2125 *pdrv != NULL; pdrv++) { 2126 retval = tr_func(pdrv, arg); 2127 2128 if (retval == 0) 2129 return(retval); 2130 } 2131 2132 return(retval); 2133} 2134 2135static int 2136xptpdperiphtraverse(struct periph_driver **pdrv, 2137 struct cam_periph *start_periph, 2138 xpt_periphfunc_t *tr_func, void *arg) 2139{ 2140 struct cam_periph *periph, *next_periph; 2141 int retval; 2142 2143 retval = 1; 2144 2145 xpt_lock_buses(); 2146 for (periph = (start_periph ? start_periph : 2147 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL; 2148 periph = next_periph) { 2149 2150 next_periph = TAILQ_NEXT(periph, unit_links); 2151 2152 retval = tr_func(periph, arg); 2153 if (retval == 0) { 2154 xpt_unlock_buses(); 2155 return(retval); 2156 } 2157 } 2158 xpt_unlock_buses(); 2159 return(retval); 2160} 2161 2162static int 2163xptdefbusfunc(struct cam_eb *bus, void *arg) 2164{ 2165 struct xpt_traverse_config *tr_config; 2166 2167 tr_config = (struct xpt_traverse_config *)arg; 2168 2169 if (tr_config->depth == XPT_DEPTH_BUS) { 2170 xpt_busfunc_t *tr_func; 2171 2172 tr_func = (xpt_busfunc_t *)tr_config->tr_func; 2173 2174 return(tr_func(bus, tr_config->tr_arg)); 2175 } else 2176 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg)); 2177} 2178 2179static int 2180xptdeftargetfunc(struct cam_et *target, void *arg) 2181{ 2182 struct xpt_traverse_config *tr_config; 2183 2184 tr_config = (struct xpt_traverse_config *)arg; 2185 2186 if (tr_config->depth == XPT_DEPTH_TARGET) { 2187 xpt_targetfunc_t *tr_func; 2188 2189 tr_func = (xpt_targetfunc_t *)tr_config->tr_func; 2190 2191 return(tr_func(target, tr_config->tr_arg)); 2192 } else 2193 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg)); 2194} 2195 2196static int 2197xptdefdevicefunc(struct cam_ed *device, void *arg) 2198{ 2199 struct xpt_traverse_config *tr_config; 2200 2201 tr_config = (struct xpt_traverse_config *)arg; 2202 2203 if (tr_config->depth == XPT_DEPTH_DEVICE) { 2204 xpt_devicefunc_t *tr_func; 2205 2206 tr_func = (xpt_devicefunc_t *)tr_config->tr_func; 2207 2208 return(tr_func(device, tr_config->tr_arg)); 2209 } else 2210 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg)); 2211} 2212 2213static int 2214xptdefperiphfunc(struct cam_periph *periph, void *arg) 2215{ 2216 struct xpt_traverse_config *tr_config; 2217 xpt_periphfunc_t *tr_func; 2218 2219 tr_config = (struct xpt_traverse_config *)arg; 2220 2221 tr_func = (xpt_periphfunc_t *)tr_config->tr_func; 2222 2223 /* 2224 * Unlike the other default functions, we don't check for depth 2225 * here. The peripheral driver level is the last level in the EDT, 2226 * so if we're here, we should execute the function in question. 2227 */ 2228 return(tr_func(periph, tr_config->tr_arg)); 2229} 2230 2231/* 2232 * Execute the given function for every bus in the EDT. 2233 */ 2234static int 2235xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg) 2236{ 2237 struct xpt_traverse_config tr_config; 2238 2239 tr_config.depth = XPT_DEPTH_BUS; 2240 tr_config.tr_func = tr_func; 2241 tr_config.tr_arg = arg; 2242 2243 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2244} 2245 2246/* 2247 * Execute the given function for every device in the EDT. 2248 */ 2249static int 2250xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg) 2251{ 2252 struct xpt_traverse_config tr_config; 2253 2254 tr_config.depth = XPT_DEPTH_DEVICE; 2255 tr_config.tr_func = tr_func; 2256 tr_config.tr_arg = arg; 2257 2258 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config)); 2259} 2260 2261static int 2262xptsetasyncfunc(struct cam_ed *device, void *arg) 2263{ 2264 struct cam_path path; 2265 struct ccb_getdev cgd; 2266 struct ccb_setasync *csa = (struct ccb_setasync *)arg; 2267 2268 /* 2269 * Don't report unconfigured devices (Wildcard devs, 2270 * devices only for target mode, device instances 2271 * that have been invalidated but are waiting for 2272 * their last reference count to be released). 2273 */ 2274 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0) 2275 return (1); 2276 2277 xpt_compile_path(&path, 2278 NULL, 2279 device->target->bus->path_id, 2280 device->target->target_id, 2281 device->lun_id); 2282 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL); 2283 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 2284 xpt_action((union ccb *)&cgd); 2285 csa->callback(csa->callback_arg, 2286 AC_FOUND_DEVICE, 2287 &path, &cgd); 2288 xpt_release_path(&path); 2289 2290 return(1); 2291} 2292 2293static int 2294xptsetasyncbusfunc(struct cam_eb *bus, void *arg) 2295{ 2296 struct cam_path path; 2297 struct ccb_pathinq cpi; 2298 struct ccb_setasync *csa = (struct ccb_setasync *)arg; 2299 2300 xpt_compile_path(&path, /*periph*/NULL, 2301 bus->sim->path_id, 2302 CAM_TARGET_WILDCARD, 2303 CAM_LUN_WILDCARD); 2304 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL); 2305 cpi.ccb_h.func_code = XPT_PATH_INQ; 2306 xpt_action((union ccb *)&cpi); 2307 csa->callback(csa->callback_arg, 2308 AC_PATH_REGISTERED, 2309 &path, &cpi); 2310 xpt_release_path(&path); 2311 2312 return(1); 2313} 2314 2315void 2316xpt_action(union ccb *start_ccb) 2317{ 2318 2319 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n")); 2320 2321 start_ccb->ccb_h.status = CAM_REQ_INPROG; 2322 /* Compatibility for RL-unaware code. */ 2323 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0) 2324 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1; 2325 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb); 2326} 2327 2328void 2329xpt_action_default(union ccb *start_ccb) 2330{ 2331 2332 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action_default\n")); 2333 2334 switch (start_ccb->ccb_h.func_code) { 2335 case XPT_SCSI_IO: 2336 { 2337 struct cam_ed *device; 2338#ifdef CAMDEBUG 2339 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 2340 struct cam_path *path; 2341 2342 path = start_ccb->ccb_h.path; 2343#endif 2344 2345 /* 2346 * For the sake of compatibility with SCSI-1 2347 * devices that may not understand the identify 2348 * message, we include lun information in the 2349 * second byte of all commands. SCSI-1 specifies 2350 * that luns are a 3 bit value and reserves only 3 2351 * bits for lun information in the CDB. Later 2352 * revisions of the SCSI spec allow for more than 8 2353 * luns, but have deprecated lun information in the 2354 * CDB. So, if the lun won't fit, we must omit. 2355 * 2356 * Also be aware that during initial probing for devices, 2357 * the inquiry information is unknown but initialized to 0. 2358 * This means that this code will be exercised while probing 2359 * devices with an ANSI revision greater than 2. 2360 */ 2361 device = start_ccb->ccb_h.path->device; 2362 if (device->protocol_version <= SCSI_REV_2 2363 && start_ccb->ccb_h.target_lun < 8 2364 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) { 2365 2366 start_ccb->csio.cdb_io.cdb_bytes[1] |= 2367 start_ccb->ccb_h.target_lun << 5; 2368 } 2369 start_ccb->csio.scsi_status = SCSI_STATUS_OK; 2370 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n", 2371 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0], 2372 &path->device->inq_data), 2373 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes, 2374 cdb_str, sizeof(cdb_str)))); 2375 } 2376 /* FALLTHROUGH */ 2377 case XPT_TARGET_IO: 2378 case XPT_CONT_TARGET_IO: 2379 start_ccb->csio.sense_resid = 0; 2380 start_ccb->csio.resid = 0; 2381 /* FALLTHROUGH */ 2382 case XPT_ATA_IO: 2383 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) { 2384 start_ccb->ataio.resid = 0; 2385 } 2386 /* FALLTHROUGH */ 2387 case XPT_RESET_DEV: 2388 case XPT_ENG_EXEC: 2389 { 2390 struct cam_path *path = start_ccb->ccb_h.path; 2391 int frozen; 2392 2393 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb); 2394 path->device->sim->devq->alloc_openings += frozen; 2395 if (frozen > 0) 2396 xpt_run_dev_allocq(path->bus); 2397 if (xpt_schedule_dev_sendq(path->bus, path->device)) 2398 xpt_run_dev_sendq(path->bus); 2399 break; 2400 } 2401 case XPT_CALC_GEOMETRY: 2402 { 2403 struct cam_sim *sim; 2404 2405 /* Filter out garbage */ 2406 if (start_ccb->ccg.block_size == 0 2407 || start_ccb->ccg.volume_size == 0) { 2408 start_ccb->ccg.cylinders = 0; 2409 start_ccb->ccg.heads = 0; 2410 start_ccb->ccg.secs_per_track = 0; 2411 start_ccb->ccb_h.status = CAM_REQ_CMP; 2412 break; 2413 } 2414#ifdef PC98 2415 /* 2416 * In a PC-98 system, geometry translation depens on 2417 * the "real" device geometry obtained from mode page 4. 2418 * SCSI geometry translation is performed in the 2419 * initialization routine of the SCSI BIOS and the result 2420 * stored in host memory. If the translation is available 2421 * in host memory, use it. If not, rely on the default 2422 * translation the device driver performs. 2423 */ 2424 if (scsi_da_bios_params(&start_ccb->ccg) != 0) { 2425 start_ccb->ccb_h.status = CAM_REQ_CMP; 2426 break; 2427 } 2428#endif 2429 sim = start_ccb->ccb_h.path->bus->sim; 2430 (*(sim->sim_action))(sim, start_ccb); 2431 break; 2432 } 2433 case XPT_ABORT: 2434 { 2435 union ccb* abort_ccb; 2436 2437 abort_ccb = start_ccb->cab.abort_ccb; 2438 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) { 2439 2440 if (abort_ccb->ccb_h.pinfo.index >= 0) { 2441 struct cam_ccbq *ccbq; 2442 struct cam_ed *device; 2443 2444 device = abort_ccb->ccb_h.path->device; 2445 ccbq = &device->ccbq; 2446 device->sim->devq->alloc_openings -= 2447 cam_ccbq_remove_ccb(ccbq, abort_ccb); 2448 abort_ccb->ccb_h.status = 2449 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2450 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2451 xpt_done(abort_ccb); 2452 start_ccb->ccb_h.status = CAM_REQ_CMP; 2453 break; 2454 } 2455 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX 2456 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) { 2457 /* 2458 * We've caught this ccb en route to 2459 * the SIM. Flag it for abort and the 2460 * SIM will do so just before starting 2461 * real work on the CCB. 2462 */ 2463 abort_ccb->ccb_h.status = 2464 CAM_REQ_ABORTED|CAM_DEV_QFRZN; 2465 xpt_freeze_devq(abort_ccb->ccb_h.path, 1); 2466 start_ccb->ccb_h.status = CAM_REQ_CMP; 2467 break; 2468 } 2469 } 2470 if (XPT_FC_IS_QUEUED(abort_ccb) 2471 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) { 2472 /* 2473 * It's already completed but waiting 2474 * for our SWI to get to it. 2475 */ 2476 start_ccb->ccb_h.status = CAM_UA_ABORT; 2477 break; 2478 } 2479 /* 2480 * If we weren't able to take care of the abort request 2481 * in the XPT, pass the request down to the SIM for processing. 2482 */ 2483 } 2484 /* FALLTHROUGH */ 2485 case XPT_ACCEPT_TARGET_IO: 2486 case XPT_EN_LUN: 2487 case XPT_IMMED_NOTIFY: 2488 case XPT_NOTIFY_ACK: 2489 case XPT_RESET_BUS: 2490 case XPT_IMMEDIATE_NOTIFY: 2491 case XPT_NOTIFY_ACKNOWLEDGE: 2492 case XPT_GET_SIM_KNOB: 2493 case XPT_SET_SIM_KNOB: 2494 { 2495 struct cam_sim *sim; 2496 2497 sim = start_ccb->ccb_h.path->bus->sim; 2498 (*(sim->sim_action))(sim, start_ccb); 2499 break; 2500 } 2501 case XPT_PATH_INQ: 2502 { 2503 struct cam_sim *sim; 2504 2505 sim = start_ccb->ccb_h.path->bus->sim; 2506 (*(sim->sim_action))(sim, start_ccb); 2507 break; 2508 } 2509 case XPT_PATH_STATS: 2510 start_ccb->cpis.last_reset = 2511 start_ccb->ccb_h.path->bus->last_reset; 2512 start_ccb->ccb_h.status = CAM_REQ_CMP; 2513 break; 2514 case XPT_GDEV_TYPE: 2515 { 2516 struct cam_ed *dev; 2517 2518 dev = start_ccb->ccb_h.path->device; 2519 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2520 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2521 } else { 2522 struct ccb_getdev *cgd; 2523 struct cam_eb *bus; 2524 struct cam_et *tar; 2525 2526 cgd = &start_ccb->cgd; 2527 bus = cgd->ccb_h.path->bus; 2528 tar = cgd->ccb_h.path->target; 2529 cgd->protocol = dev->protocol; 2530 cgd->inq_data = dev->inq_data; 2531 cgd->ident_data = dev->ident_data; 2532 cgd->inq_flags = dev->inq_flags; 2533 cgd->ccb_h.status = CAM_REQ_CMP; 2534 cgd->serial_num_len = dev->serial_num_len; 2535 if ((dev->serial_num_len > 0) 2536 && (dev->serial_num != NULL)) 2537 bcopy(dev->serial_num, cgd->serial_num, 2538 dev->serial_num_len); 2539 } 2540 break; 2541 } 2542 case XPT_GDEV_STATS: 2543 { 2544 struct cam_ed *dev; 2545 2546 dev = start_ccb->ccb_h.path->device; 2547 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) { 2548 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2549 } else { 2550 struct ccb_getdevstats *cgds; 2551 struct cam_eb *bus; 2552 struct cam_et *tar; 2553 2554 cgds = &start_ccb->cgds; 2555 bus = cgds->ccb_h.path->bus; 2556 tar = cgds->ccb_h.path->target; 2557 cgds->dev_openings = dev->ccbq.dev_openings; 2558 cgds->dev_active = dev->ccbq.dev_active; 2559 cgds->devq_openings = dev->ccbq.devq_openings; 2560 cgds->devq_queued = dev->ccbq.queue.entries; 2561 cgds->held = dev->ccbq.held; 2562 cgds->last_reset = tar->last_reset; 2563 cgds->maxtags = dev->maxtags; 2564 cgds->mintags = dev->mintags; 2565 if (timevalcmp(&tar->last_reset, &bus->last_reset, <)) 2566 cgds->last_reset = bus->last_reset; 2567 cgds->ccb_h.status = CAM_REQ_CMP; 2568 } 2569 break; 2570 } 2571 case XPT_GDEVLIST: 2572 { 2573 struct cam_periph *nperiph; 2574 struct periph_list *periph_head; 2575 struct ccb_getdevlist *cgdl; 2576 u_int i; 2577 struct cam_ed *device; 2578 int found; 2579 2580 2581 found = 0; 2582 2583 /* 2584 * Don't want anyone mucking with our data. 2585 */ 2586 device = start_ccb->ccb_h.path->device; 2587 periph_head = &device->periphs; 2588 cgdl = &start_ccb->cgdl; 2589 2590 /* 2591 * Check and see if the list has changed since the user 2592 * last requested a list member. If so, tell them that the 2593 * list has changed, and therefore they need to start over 2594 * from the beginning. 2595 */ 2596 if ((cgdl->index != 0) && 2597 (cgdl->generation != device->generation)) { 2598 cgdl->status = CAM_GDEVLIST_LIST_CHANGED; 2599 break; 2600 } 2601 2602 /* 2603 * Traverse the list of peripherals and attempt to find 2604 * the requested peripheral. 2605 */ 2606 for (nperiph = SLIST_FIRST(periph_head), i = 0; 2607 (nperiph != NULL) && (i <= cgdl->index); 2608 nperiph = SLIST_NEXT(nperiph, periph_links), i++) { 2609 if (i == cgdl->index) { 2610 strncpy(cgdl->periph_name, 2611 nperiph->periph_name, 2612 DEV_IDLEN); 2613 cgdl->unit_number = nperiph->unit_number; 2614 found = 1; 2615 } 2616 } 2617 if (found == 0) { 2618 cgdl->status = CAM_GDEVLIST_ERROR; 2619 break; 2620 } 2621 2622 if (nperiph == NULL) 2623 cgdl->status = CAM_GDEVLIST_LAST_DEVICE; 2624 else 2625 cgdl->status = CAM_GDEVLIST_MORE_DEVS; 2626 2627 cgdl->index++; 2628 cgdl->generation = device->generation; 2629 2630 cgdl->ccb_h.status = CAM_REQ_CMP; 2631 break; 2632 } 2633 case XPT_DEV_MATCH: 2634 { 2635 dev_pos_type position_type; 2636 struct ccb_dev_match *cdm; 2637 2638 cdm = &start_ccb->cdm; 2639 2640 /* 2641 * There are two ways of getting at information in the EDT. 2642 * The first way is via the primary EDT tree. It starts 2643 * with a list of busses, then a list of targets on a bus, 2644 * then devices/luns on a target, and then peripherals on a 2645 * device/lun. The "other" way is by the peripheral driver 2646 * lists. The peripheral driver lists are organized by 2647 * peripheral driver. (obviously) So it makes sense to 2648 * use the peripheral driver list if the user is looking 2649 * for something like "da1", or all "da" devices. If the 2650 * user is looking for something on a particular bus/target 2651 * or lun, it's generally better to go through the EDT tree. 2652 */ 2653 2654 if (cdm->pos.position_type != CAM_DEV_POS_NONE) 2655 position_type = cdm->pos.position_type; 2656 else { 2657 u_int i; 2658 2659 position_type = CAM_DEV_POS_NONE; 2660 2661 for (i = 0; i < cdm->num_patterns; i++) { 2662 if ((cdm->patterns[i].type == DEV_MATCH_BUS) 2663 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){ 2664 position_type = CAM_DEV_POS_EDT; 2665 break; 2666 } 2667 } 2668 2669 if (cdm->num_patterns == 0) 2670 position_type = CAM_DEV_POS_EDT; 2671 else if (position_type == CAM_DEV_POS_NONE) 2672 position_type = CAM_DEV_POS_PDRV; 2673 } 2674 2675 switch(position_type & CAM_DEV_POS_TYPEMASK) { 2676 case CAM_DEV_POS_EDT: 2677 xptedtmatch(cdm); 2678 break; 2679 case CAM_DEV_POS_PDRV: 2680 xptperiphlistmatch(cdm); 2681 break; 2682 default: 2683 cdm->status = CAM_DEV_MATCH_ERROR; 2684 break; 2685 } 2686 2687 if (cdm->status == CAM_DEV_MATCH_ERROR) 2688 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2689 else 2690 start_ccb->ccb_h.status = CAM_REQ_CMP; 2691 2692 break; 2693 } 2694 case XPT_SASYNC_CB: 2695 { 2696 struct ccb_setasync *csa; 2697 struct async_node *cur_entry; 2698 struct async_list *async_head; 2699 u_int32_t added; 2700 2701 csa = &start_ccb->csa; 2702 added = csa->event_enable; 2703 async_head = &csa->ccb_h.path->device->asyncs; 2704 2705 /* 2706 * If there is already an entry for us, simply 2707 * update it. 2708 */ 2709 cur_entry = SLIST_FIRST(async_head); 2710 while (cur_entry != NULL) { 2711 if ((cur_entry->callback_arg == csa->callback_arg) 2712 && (cur_entry->callback == csa->callback)) 2713 break; 2714 cur_entry = SLIST_NEXT(cur_entry, links); 2715 } 2716 2717 if (cur_entry != NULL) { 2718 /* 2719 * If the request has no flags set, 2720 * remove the entry. 2721 */ 2722 added &= ~cur_entry->event_enable; 2723 if (csa->event_enable == 0) { 2724 SLIST_REMOVE(async_head, cur_entry, 2725 async_node, links); 2726 xpt_release_device(csa->ccb_h.path->device); 2727 free(cur_entry, M_CAMXPT); 2728 } else { 2729 cur_entry->event_enable = csa->event_enable; 2730 } 2731 csa->event_enable = added; 2732 } else { 2733 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT, 2734 M_NOWAIT); 2735 if (cur_entry == NULL) { 2736 csa->ccb_h.status = CAM_RESRC_UNAVAIL; 2737 break; 2738 } 2739 cur_entry->event_enable = csa->event_enable; 2740 cur_entry->callback_arg = csa->callback_arg; 2741 cur_entry->callback = csa->callback; 2742 SLIST_INSERT_HEAD(async_head, cur_entry, links); 2743 xpt_acquire_device(csa->ccb_h.path->device); 2744 } 2745 start_ccb->ccb_h.status = CAM_REQ_CMP; 2746 break; 2747 } 2748 case XPT_REL_SIMQ: 2749 { 2750 struct ccb_relsim *crs; 2751 struct cam_ed *dev; 2752 2753 crs = &start_ccb->crs; 2754 dev = crs->ccb_h.path->device; 2755 if (dev == NULL) { 2756 2757 crs->ccb_h.status = CAM_DEV_NOT_THERE; 2758 break; 2759 } 2760 2761 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) { 2762 2763 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) { 2764 /* Don't ever go below one opening */ 2765 if (crs->openings > 0) { 2766 xpt_dev_ccbq_resize(crs->ccb_h.path, 2767 crs->openings); 2768 2769 if (bootverbose) { 2770 xpt_print(crs->ccb_h.path, 2771 "tagged openings now %d\n", 2772 crs->openings); 2773 } 2774 } 2775 } 2776 } 2777 2778 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) { 2779 2780 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 2781 2782 /* 2783 * Just extend the old timeout and decrement 2784 * the freeze count so that a single timeout 2785 * is sufficient for releasing the queue. 2786 */ 2787 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2788 callout_stop(&dev->callout); 2789 } else { 2790 2791 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2792 } 2793 2794 callout_reset(&dev->callout, 2795 (crs->release_timeout * hz) / 1000, 2796 xpt_release_devq_timeout, dev); 2797 2798 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING; 2799 2800 } 2801 2802 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) { 2803 2804 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) { 2805 /* 2806 * Decrement the freeze count so that a single 2807 * completion is still sufficient to unfreeze 2808 * the queue. 2809 */ 2810 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2811 } else { 2812 2813 dev->flags |= CAM_DEV_REL_ON_COMPLETE; 2814 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2815 } 2816 } 2817 2818 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) { 2819 2820 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0 2821 || (dev->ccbq.dev_active == 0)) { 2822 2823 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE; 2824 } else { 2825 2826 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY; 2827 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 2828 } 2829 } 2830 2831 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) { 2832 xpt_release_devq_rl(crs->ccb_h.path, /*runlevel*/ 2833 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ? 2834 crs->release_timeout : 0, 2835 /*count*/1, /*run_queue*/TRUE); 2836 } 2837 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0]; 2838 start_ccb->ccb_h.status = CAM_REQ_CMP; 2839 break; 2840 } 2841 case XPT_DEBUG: { 2842#ifdef CAMDEBUG 2843#ifdef CAM_DEBUG_DELAY 2844 cam_debug_delay = CAM_DEBUG_DELAY; 2845#endif 2846 cam_dflags = start_ccb->cdbg.flags; 2847 if (cam_dpath != NULL) { 2848 xpt_free_path(cam_dpath); 2849 cam_dpath = NULL; 2850 } 2851 2852 if (cam_dflags != CAM_DEBUG_NONE) { 2853 if (xpt_create_path(&cam_dpath, xpt_periph, 2854 start_ccb->ccb_h.path_id, 2855 start_ccb->ccb_h.target_id, 2856 start_ccb->ccb_h.target_lun) != 2857 CAM_REQ_CMP) { 2858 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2859 cam_dflags = CAM_DEBUG_NONE; 2860 } else { 2861 start_ccb->ccb_h.status = CAM_REQ_CMP; 2862 xpt_print(cam_dpath, "debugging flags now %x\n", 2863 cam_dflags); 2864 } 2865 } else { 2866 cam_dpath = NULL; 2867 start_ccb->ccb_h.status = CAM_REQ_CMP; 2868 } 2869#else /* !CAMDEBUG */ 2870 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2871#endif /* CAMDEBUG */ 2872 break; 2873 } 2874 case XPT_FREEZE_QUEUE: 2875 { 2876 struct ccb_relsim *crs = &start_ccb->crs; 2877 2878 xpt_freeze_devq_rl(crs->ccb_h.path, /*runlevel*/ 2879 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ? 2880 crs->release_timeout : 0, /*count*/1); 2881 start_ccb->ccb_h.status = CAM_REQ_CMP; 2882 break; 2883 } 2884 case XPT_NOOP: 2885 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) 2886 xpt_freeze_devq(start_ccb->ccb_h.path, 1); 2887 start_ccb->ccb_h.status = CAM_REQ_CMP; 2888 break; 2889 default: 2890 case XPT_SDEV_TYPE: 2891 case XPT_TERM_IO: 2892 case XPT_ENG_INQ: 2893 /* XXX Implement */ 2894 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL; 2895 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) { 2896 xpt_done(start_ccb); 2897 } 2898 break; 2899 } 2900} 2901 2902void 2903xpt_polled_action(union ccb *start_ccb) 2904{ 2905 u_int32_t timeout; 2906 struct cam_sim *sim; 2907 struct cam_devq *devq; 2908 struct cam_ed *dev; 2909 2910 2911 timeout = start_ccb->ccb_h.timeout * 10; 2912 sim = start_ccb->ccb_h.path->bus->sim; 2913 devq = sim->devq; 2914 dev = start_ccb->ccb_h.path->device; 2915 2916 mtx_assert(sim->mtx, MA_OWNED); 2917 2918 /* 2919 * Steal an opening so that no other queued requests 2920 * can get it before us while we simulate interrupts. 2921 */ 2922 dev->ccbq.devq_openings--; 2923 dev->ccbq.dev_openings--; 2924 2925 while(((devq != NULL && devq->send_openings <= 0) || 2926 dev->ccbq.dev_openings < 0) && (--timeout > 0)) { 2927 DELAY(100); 2928 (*(sim->sim_poll))(sim); 2929 camisr_runqueue(&sim->sim_doneq); 2930 } 2931 2932 dev->ccbq.devq_openings++; 2933 dev->ccbq.dev_openings++; 2934 2935 if (timeout != 0) { 2936 xpt_action(start_ccb); 2937 while(--timeout > 0) { 2938 (*(sim->sim_poll))(sim); 2939 camisr_runqueue(&sim->sim_doneq); 2940 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK) 2941 != CAM_REQ_INPROG) 2942 break; 2943 DELAY(100); 2944 } 2945 if (timeout == 0) { 2946 /* 2947 * XXX Is it worth adding a sim_timeout entry 2948 * point so we can attempt recovery? If 2949 * this is only used for dumps, I don't think 2950 * it is. 2951 */ 2952 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT; 2953 } 2954 } else { 2955 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2956 } 2957} 2958 2959/* 2960 * Schedule a peripheral driver to receive a ccb when it's 2961 * target device has space for more transactions. 2962 */ 2963void 2964xpt_schedule(struct cam_periph *perph, u_int32_t new_priority) 2965{ 2966 struct cam_ed *device; 2967 int runq = 0; 2968 2969 mtx_assert(perph->sim->mtx, MA_OWNED); 2970 2971 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n")); 2972 device = perph->path->device; 2973 if (periph_is_queued(perph)) { 2974 /* Simply reorder based on new priority */ 2975 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 2976 (" change priority to %d\n", new_priority)); 2977 if (new_priority < perph->pinfo.priority) { 2978 camq_change_priority(&device->drvq, 2979 perph->pinfo.index, 2980 new_priority); 2981 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 2982 } 2983 } else { 2984 /* New entry on the queue */ 2985 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 2986 (" added periph to queue\n")); 2987 perph->pinfo.priority = new_priority; 2988 perph->pinfo.generation = ++device->drvq.generation; 2989 camq_insert(&device->drvq, &perph->pinfo); 2990 runq = xpt_schedule_dev_allocq(perph->path->bus, device); 2991 } 2992 if (runq != 0) { 2993 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE, 2994 (" calling xpt_run_devq\n")); 2995 xpt_run_dev_allocq(perph->path->bus); 2996 } 2997} 2998 2999 3000/* 3001 * Schedule a device to run on a given queue. 3002 * If the device was inserted as a new entry on the queue, 3003 * return 1 meaning the device queue should be run. If we 3004 * were already queued, implying someone else has already 3005 * started the queue, return 0 so the caller doesn't attempt 3006 * to run the queue. 3007 */ 3008int 3009xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo, 3010 u_int32_t new_priority) 3011{ 3012 int retval; 3013 u_int32_t old_priority; 3014 3015 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n")); 3016 3017 old_priority = pinfo->priority; 3018 3019 /* 3020 * Are we already queued? 3021 */ 3022 if (pinfo->index != CAM_UNQUEUED_INDEX) { 3023 /* Simply reorder based on new priority */ 3024 if (new_priority < old_priority) { 3025 camq_change_priority(queue, pinfo->index, 3026 new_priority); 3027 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3028 ("changed priority to %d\n", 3029 new_priority)); 3030 retval = 1; 3031 } else 3032 retval = 0; 3033 } else { 3034 /* New entry on the queue */ 3035 if (new_priority < old_priority) 3036 pinfo->priority = new_priority; 3037 3038 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3039 ("Inserting onto queue\n")); 3040 pinfo->generation = ++queue->generation; 3041 camq_insert(queue, pinfo); 3042 retval = 1; 3043 } 3044 return (retval); 3045} 3046 3047static void 3048xpt_run_dev_allocq(struct cam_eb *bus) 3049{ 3050 struct cam_devq *devq; 3051 3052 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n")); 3053 devq = bus->sim->devq; 3054 3055 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3056 (" qfrozen_cnt == 0x%x, entries == %d, " 3057 "openings == %d, active == %d\n", 3058 devq->alloc_queue.qfrozen_cnt[0], 3059 devq->alloc_queue.entries, 3060 devq->alloc_openings, 3061 devq->alloc_active)); 3062 3063 devq->alloc_queue.qfrozen_cnt[0]++; 3064 while ((devq->alloc_queue.entries > 0) 3065 && (devq->alloc_openings > 0) 3066 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) { 3067 struct cam_ed_qinfo *qinfo; 3068 struct cam_ed *device; 3069 union ccb *work_ccb; 3070 struct cam_periph *drv; 3071 struct camq *drvq; 3072 3073 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue, 3074 CAMQ_HEAD); 3075 device = qinfo->device; 3076 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3077 ("running device %p\n", device)); 3078 3079 drvq = &device->drvq; 3080 3081#ifdef CAMDEBUG 3082 if (drvq->entries <= 0) { 3083 panic("xpt_run_dev_allocq: " 3084 "Device on queue without any work to do"); 3085 } 3086#endif 3087 if ((work_ccb = xpt_get_ccb(device)) != NULL) { 3088 devq->alloc_openings--; 3089 devq->alloc_active++; 3090 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD); 3091 xpt_setup_ccb(&work_ccb->ccb_h, drv->path, 3092 drv->pinfo.priority); 3093 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3094 ("calling periph start\n")); 3095 drv->periph_start(drv, work_ccb); 3096 } else { 3097 /* 3098 * Malloc failure in alloc_ccb 3099 */ 3100 /* 3101 * XXX add us to a list to be run from free_ccb 3102 * if we don't have any ccbs active on this 3103 * device queue otherwise we may never get run 3104 * again. 3105 */ 3106 break; 3107 } 3108 3109 /* We may have more work. Attempt to reschedule. */ 3110 xpt_schedule_dev_allocq(bus, device); 3111 } 3112 devq->alloc_queue.qfrozen_cnt[0]--; 3113} 3114 3115static void 3116xpt_run_dev_sendq(struct cam_eb *bus) 3117{ 3118 struct cam_devq *devq; 3119 3120 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n")); 3121 3122 devq = bus->sim->devq; 3123 3124 devq->send_queue.qfrozen_cnt[0]++; 3125 while ((devq->send_queue.entries > 0) 3126 && (devq->send_openings > 0) 3127 && (devq->send_queue.qfrozen_cnt[0] <= 1)) { 3128 struct cam_ed_qinfo *qinfo; 3129 struct cam_ed *device; 3130 union ccb *work_ccb; 3131 struct cam_sim *sim; 3132 3133 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue, 3134 CAMQ_HEAD); 3135 device = qinfo->device; 3136 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, 3137 ("running device %p\n", device)); 3138 3139 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD); 3140 if (work_ccb == NULL) { 3141 printf("device on run queue with no ccbs???\n"); 3142 continue; 3143 } 3144 3145 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) { 3146 3147 mtx_lock(&xsoftc.xpt_lock); 3148 if (xsoftc.num_highpower <= 0) { 3149 /* 3150 * We got a high power command, but we 3151 * don't have any available slots. Freeze 3152 * the device queue until we have a slot 3153 * available. 3154 */ 3155 xpt_freeze_devq(work_ccb->ccb_h.path, 1); 3156 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, 3157 &work_ccb->ccb_h, 3158 xpt_links.stqe); 3159 3160 mtx_unlock(&xsoftc.xpt_lock); 3161 continue; 3162 } else { 3163 /* 3164 * Consume a high power slot while 3165 * this ccb runs. 3166 */ 3167 xsoftc.num_highpower--; 3168 } 3169 mtx_unlock(&xsoftc.xpt_lock); 3170 } 3171 cam_ccbq_remove_ccb(&device->ccbq, work_ccb); 3172 cam_ccbq_send_ccb(&device->ccbq, work_ccb); 3173 3174 devq->send_openings--; 3175 devq->send_active++; 3176 3177 xpt_schedule_dev_sendq(bus, device); 3178 3179 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){ 3180 /* 3181 * The client wants to freeze the queue 3182 * after this CCB is sent. 3183 */ 3184 xpt_freeze_devq(work_ccb->ccb_h.path, 1); 3185 } 3186 3187 /* In Target mode, the peripheral driver knows best... */ 3188 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) { 3189 if ((device->inq_flags & SID_CmdQue) != 0 3190 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE) 3191 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID; 3192 else 3193 /* 3194 * Clear this in case of a retried CCB that 3195 * failed due to a rejected tag. 3196 */ 3197 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 3198 } 3199 3200 /* 3201 * Device queues can be shared among multiple sim instances 3202 * that reside on different busses. Use the SIM in the queue 3203 * CCB's path, rather than the one in the bus that was passed 3204 * into this function. 3205 */ 3206 sim = work_ccb->ccb_h.path->bus->sim; 3207 (*(sim->sim_action))(sim, work_ccb); 3208 } 3209 devq->send_queue.qfrozen_cnt[0]--; 3210} 3211 3212/* 3213 * This function merges stuff from the slave ccb into the master ccb, while 3214 * keeping important fields in the master ccb constant. 3215 */ 3216void 3217xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb) 3218{ 3219 3220 /* 3221 * Pull fields that are valid for peripheral drivers to set 3222 * into the master CCB along with the CCB "payload". 3223 */ 3224 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count; 3225 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code; 3226 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout; 3227 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags; 3228 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1], 3229 sizeof(union ccb) - sizeof(struct ccb_hdr)); 3230} 3231 3232void 3233xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority) 3234{ 3235 3236 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n")); 3237 ccb_h->pinfo.priority = priority; 3238 ccb_h->path = path; 3239 ccb_h->path_id = path->bus->path_id; 3240 if (path->target) 3241 ccb_h->target_id = path->target->target_id; 3242 else 3243 ccb_h->target_id = CAM_TARGET_WILDCARD; 3244 if (path->device) { 3245 ccb_h->target_lun = path->device->lun_id; 3246 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation; 3247 } else { 3248 ccb_h->target_lun = CAM_TARGET_WILDCARD; 3249 } 3250 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX; 3251 ccb_h->flags = 0; 3252} 3253 3254/* Path manipulation functions */ 3255cam_status 3256xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph, 3257 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3258{ 3259 struct cam_path *path; 3260 cam_status status; 3261 3262 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT); 3263 3264 if (path == NULL) { 3265 status = CAM_RESRC_UNAVAIL; 3266 return(status); 3267 } 3268 status = xpt_compile_path(path, perph, path_id, target_id, lun_id); 3269 if (status != CAM_REQ_CMP) { 3270 free(path, M_CAMXPT); 3271 path = NULL; 3272 } 3273 *new_path_ptr = path; 3274 return (status); 3275} 3276 3277cam_status 3278xpt_create_path_unlocked(struct cam_path **new_path_ptr, 3279 struct cam_periph *periph, path_id_t path_id, 3280 target_id_t target_id, lun_id_t lun_id) 3281{ 3282 struct cam_path *path; 3283 struct cam_eb *bus = NULL; 3284 cam_status status; 3285 int need_unlock = 0; 3286 3287 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK); 3288 3289 if (path_id != CAM_BUS_WILDCARD) { 3290 bus = xpt_find_bus(path_id); 3291 if (bus != NULL) { 3292 need_unlock = 1; 3293 CAM_SIM_LOCK(bus->sim); 3294 } 3295 } 3296 status = xpt_compile_path(path, periph, path_id, target_id, lun_id); 3297 if (need_unlock) 3298 CAM_SIM_UNLOCK(bus->sim); 3299 if (status != CAM_REQ_CMP) { 3300 free(path, M_CAMXPT); 3301 path = NULL; 3302 } 3303 *new_path_ptr = path; 3304 return (status); 3305} 3306 3307cam_status 3308xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph, 3309 path_id_t path_id, target_id_t target_id, lun_id_t lun_id) 3310{ 3311 struct cam_eb *bus; 3312 struct cam_et *target; 3313 struct cam_ed *device; 3314 cam_status status; 3315 3316 status = CAM_REQ_CMP; /* Completed without error */ 3317 target = NULL; /* Wildcarded */ 3318 device = NULL; /* Wildcarded */ 3319 3320 /* 3321 * We will potentially modify the EDT, so block interrupts 3322 * that may attempt to create cam paths. 3323 */ 3324 bus = xpt_find_bus(path_id); 3325 if (bus == NULL) { 3326 status = CAM_PATH_INVALID; 3327 } else { 3328 target = xpt_find_target(bus, target_id); 3329 if (target == NULL) { 3330 /* Create one */ 3331 struct cam_et *new_target; 3332 3333 new_target = xpt_alloc_target(bus, target_id); 3334 if (new_target == NULL) { 3335 status = CAM_RESRC_UNAVAIL; 3336 } else { 3337 target = new_target; 3338 } 3339 } 3340 if (target != NULL) { 3341 device = xpt_find_device(target, lun_id); 3342 if (device == NULL) { 3343 /* Create one */ 3344 struct cam_ed *new_device; 3345 3346 new_device = 3347 (*(bus->xport->alloc_device))(bus, 3348 target, 3349 lun_id); 3350 if (new_device == NULL) { 3351 status = CAM_RESRC_UNAVAIL; 3352 } else { 3353 device = new_device; 3354 } 3355 } 3356 } 3357 } 3358 3359 /* 3360 * Only touch the user's data if we are successful. 3361 */ 3362 if (status == CAM_REQ_CMP) { 3363 new_path->periph = perph; 3364 new_path->bus = bus; 3365 new_path->target = target; 3366 new_path->device = device; 3367 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n")); 3368 } else { 3369 if (device != NULL) 3370 xpt_release_device(device); 3371 if (target != NULL) 3372 xpt_release_target(target); 3373 if (bus != NULL) 3374 xpt_release_bus(bus); 3375 } 3376 return (status); 3377} 3378 3379void 3380xpt_release_path(struct cam_path *path) 3381{ 3382 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n")); 3383 if (path->device != NULL) { 3384 xpt_release_device(path->device); 3385 path->device = NULL; 3386 } 3387 if (path->target != NULL) { 3388 xpt_release_target(path->target); 3389 path->target = NULL; 3390 } 3391 if (path->bus != NULL) { 3392 xpt_release_bus(path->bus); 3393 path->bus = NULL; 3394 } 3395} 3396 3397void 3398xpt_free_path(struct cam_path *path) 3399{ 3400 3401 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n")); 3402 xpt_release_path(path); 3403 free(path, M_CAMXPT); 3404} 3405 3406 3407/* 3408 * Return -1 for failure, 0 for exact match, 1 for match with wildcards 3409 * in path1, 2 for match with wildcards in path2. 3410 */ 3411int 3412xpt_path_comp(struct cam_path *path1, struct cam_path *path2) 3413{ 3414 int retval = 0; 3415 3416 if (path1->bus != path2->bus) { 3417 if (path1->bus->path_id == CAM_BUS_WILDCARD) 3418 retval = 1; 3419 else if (path2->bus->path_id == CAM_BUS_WILDCARD) 3420 retval = 2; 3421 else 3422 return (-1); 3423 } 3424 if (path1->target != path2->target) { 3425 if (path1->target->target_id == CAM_TARGET_WILDCARD) { 3426 if (retval == 0) 3427 retval = 1; 3428 } else if (path2->target->target_id == CAM_TARGET_WILDCARD) 3429 retval = 2; 3430 else 3431 return (-1); 3432 } 3433 if (path1->device != path2->device) { 3434 if (path1->device->lun_id == CAM_LUN_WILDCARD) { 3435 if (retval == 0) 3436 retval = 1; 3437 } else if (path2->device->lun_id == CAM_LUN_WILDCARD) 3438 retval = 2; 3439 else 3440 return (-1); 3441 } 3442 return (retval); 3443} 3444 3445void 3446xpt_print_path(struct cam_path *path) 3447{ 3448 3449 if (path == NULL) 3450 printf("(nopath): "); 3451 else { 3452 if (path->periph != NULL) 3453 printf("(%s%d:", path->periph->periph_name, 3454 path->periph->unit_number); 3455 else 3456 printf("(noperiph:"); 3457 3458 if (path->bus != NULL) 3459 printf("%s%d:%d:", path->bus->sim->sim_name, 3460 path->bus->sim->unit_number, 3461 path->bus->sim->bus_id); 3462 else 3463 printf("nobus:"); 3464 3465 if (path->target != NULL) 3466 printf("%d:", path->target->target_id); 3467 else 3468 printf("X:"); 3469 3470 if (path->device != NULL) 3471 printf("%d): ", path->device->lun_id); 3472 else 3473 printf("X): "); 3474 } 3475} 3476 3477void 3478xpt_print(struct cam_path *path, const char *fmt, ...) 3479{ 3480 va_list ap; 3481 xpt_print_path(path); 3482 va_start(ap, fmt); 3483 vprintf(fmt, ap); 3484 va_end(ap); 3485} 3486 3487int 3488xpt_path_string(struct cam_path *path, char *str, size_t str_len) 3489{ 3490 struct sbuf sb; 3491 3492#ifdef INVARIANTS 3493 if (path != NULL && path->bus != NULL) 3494 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3495#endif 3496 3497 sbuf_new(&sb, str, str_len, 0); 3498 3499 if (path == NULL) 3500 sbuf_printf(&sb, "(nopath): "); 3501 else { 3502 if (path->periph != NULL) 3503 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name, 3504 path->periph->unit_number); 3505 else 3506 sbuf_printf(&sb, "(noperiph:"); 3507 3508 if (path->bus != NULL) 3509 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name, 3510 path->bus->sim->unit_number, 3511 path->bus->sim->bus_id); 3512 else 3513 sbuf_printf(&sb, "nobus:"); 3514 3515 if (path->target != NULL) 3516 sbuf_printf(&sb, "%d:", path->target->target_id); 3517 else 3518 sbuf_printf(&sb, "X:"); 3519 3520 if (path->device != NULL) 3521 sbuf_printf(&sb, "%d): ", path->device->lun_id); 3522 else 3523 sbuf_printf(&sb, "X): "); 3524 } 3525 sbuf_finish(&sb); 3526 3527 return(sbuf_len(&sb)); 3528} 3529 3530path_id_t 3531xpt_path_path_id(struct cam_path *path) 3532{ 3533 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3534 3535 return(path->bus->path_id); 3536} 3537 3538target_id_t 3539xpt_path_target_id(struct cam_path *path) 3540{ 3541 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3542 3543 if (path->target != NULL) 3544 return (path->target->target_id); 3545 else 3546 return (CAM_TARGET_WILDCARD); 3547} 3548 3549lun_id_t 3550xpt_path_lun_id(struct cam_path *path) 3551{ 3552 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3553 3554 if (path->device != NULL) 3555 return (path->device->lun_id); 3556 else 3557 return (CAM_LUN_WILDCARD); 3558} 3559 3560struct cam_sim * 3561xpt_path_sim(struct cam_path *path) 3562{ 3563 3564 return (path->bus->sim); 3565} 3566 3567struct cam_periph* 3568xpt_path_periph(struct cam_path *path) 3569{ 3570 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3571 3572 return (path->periph); 3573} 3574 3575/* 3576 * Release a CAM control block for the caller. Remit the cost of the structure 3577 * to the device referenced by the path. If the this device had no 'credits' 3578 * and peripheral drivers have registered async callbacks for this notification 3579 * call them now. 3580 */ 3581void 3582xpt_release_ccb(union ccb *free_ccb) 3583{ 3584 struct cam_path *path; 3585 struct cam_ed *device; 3586 struct cam_eb *bus; 3587 struct cam_sim *sim; 3588 3589 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n")); 3590 path = free_ccb->ccb_h.path; 3591 device = path->device; 3592 bus = path->bus; 3593 sim = bus->sim; 3594 3595 mtx_assert(sim->mtx, MA_OWNED); 3596 3597 cam_ccbq_release_opening(&device->ccbq); 3598 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) { 3599 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED; 3600 cam_ccbq_resize(&device->ccbq, 3601 device->ccbq.dev_openings + device->ccbq.dev_active); 3602 } 3603 if (sim->ccb_count > sim->max_ccbs) { 3604 xpt_free_ccb(free_ccb); 3605 sim->ccb_count--; 3606 } else { 3607 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h, 3608 xpt_links.sle); 3609 } 3610 if (sim->devq == NULL) { 3611 return; 3612 } 3613 sim->devq->alloc_openings++; 3614 sim->devq->alloc_active--; 3615 if (device_is_alloc_queued(device) == 0) 3616 xpt_schedule_dev_allocq(bus, device); 3617 xpt_run_dev_allocq(bus); 3618} 3619 3620/* Functions accessed by SIM drivers */ 3621 3622static struct xpt_xport xport_default = { 3623 .alloc_device = xpt_alloc_device_default, 3624 .action = xpt_action_default, 3625 .async = xpt_dev_async_default, 3626}; 3627 3628/* 3629 * A sim structure, listing the SIM entry points and instance 3630 * identification info is passed to xpt_bus_register to hook the SIM 3631 * into the CAM framework. xpt_bus_register creates a cam_eb entry 3632 * for this new bus and places it in the array of busses and assigns 3633 * it a path_id. The path_id may be influenced by "hard wiring" 3634 * information specified by the user. Once interrupt services are 3635 * available, the bus will be probed. 3636 */ 3637int32_t 3638xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus) 3639{ 3640 struct cam_eb *new_bus; 3641 struct cam_eb *old_bus; 3642 struct ccb_pathinq cpi; 3643 struct cam_path *path; 3644 cam_status status; 3645 3646 mtx_assert(sim->mtx, MA_OWNED); 3647 3648 sim->bus_id = bus; 3649 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus), 3650 M_CAMXPT, M_NOWAIT); 3651 if (new_bus == NULL) { 3652 /* Couldn't satisfy request */ 3653 return (CAM_RESRC_UNAVAIL); 3654 } 3655 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT); 3656 if (path == NULL) { 3657 free(new_bus, M_CAMXPT); 3658 return (CAM_RESRC_UNAVAIL); 3659 } 3660 3661 if (strcmp(sim->sim_name, "xpt") != 0) { 3662 sim->path_id = 3663 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id); 3664 } 3665 3666 TAILQ_INIT(&new_bus->et_entries); 3667 new_bus->path_id = sim->path_id; 3668 cam_sim_hold(sim); 3669 new_bus->sim = sim; 3670 timevalclear(&new_bus->last_reset); 3671 new_bus->flags = 0; 3672 new_bus->refcount = 1; /* Held until a bus_deregister event */ 3673 new_bus->generation = 0; 3674 3675 mtx_lock(&xsoftc.xpt_topo_lock); 3676 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3677 while (old_bus != NULL 3678 && old_bus->path_id < new_bus->path_id) 3679 old_bus = TAILQ_NEXT(old_bus, links); 3680 if (old_bus != NULL) 3681 TAILQ_INSERT_BEFORE(old_bus, new_bus, links); 3682 else 3683 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links); 3684 xsoftc.bus_generation++; 3685 mtx_unlock(&xsoftc.xpt_topo_lock); 3686 3687 /* 3688 * Set a default transport so that a PATH_INQ can be issued to 3689 * the SIM. This will then allow for probing and attaching of 3690 * a more appropriate transport. 3691 */ 3692 new_bus->xport = &xport_default; 3693 3694 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id, 3695 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3696 if (status != CAM_REQ_CMP) 3697 printf("xpt_compile_path returned %d\n", status); 3698 3699 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL); 3700 cpi.ccb_h.func_code = XPT_PATH_INQ; 3701 xpt_action((union ccb *)&cpi); 3702 3703 if (cpi.ccb_h.status == CAM_REQ_CMP) { 3704 switch (cpi.transport) { 3705 case XPORT_SPI: 3706 case XPORT_SAS: 3707 case XPORT_FC: 3708 case XPORT_USB: 3709 case XPORT_ISCSI: 3710 case XPORT_PPB: 3711 new_bus->xport = scsi_get_xport(); 3712 break; 3713 case XPORT_ATA: 3714 case XPORT_SATA: 3715 new_bus->xport = ata_get_xport(); 3716 break; 3717 default: 3718 new_bus->xport = &xport_default; 3719 break; 3720 } 3721 } 3722 3723 /* Notify interested parties */ 3724 if (sim->path_id != CAM_XPT_PATH_ID) { 3725 union ccb *scan_ccb; 3726 3727 xpt_async(AC_PATH_REGISTERED, path, &cpi); 3728 /* Initiate bus rescan. */ 3729 scan_ccb = xpt_alloc_ccb_nowait(); 3730 scan_ccb->ccb_h.path = path; 3731 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS; 3732 scan_ccb->crcn.flags = 0; 3733 xpt_rescan(scan_ccb); 3734 } else 3735 xpt_free_path(path); 3736 return (CAM_SUCCESS); 3737} 3738 3739int32_t 3740xpt_bus_deregister(path_id_t pathid) 3741{ 3742 struct cam_path bus_path; 3743 cam_status status; 3744 3745 status = xpt_compile_path(&bus_path, NULL, pathid, 3746 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 3747 if (status != CAM_REQ_CMP) 3748 return (status); 3749 3750 xpt_async(AC_LOST_DEVICE, &bus_path, NULL); 3751 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL); 3752 3753 /* Release the reference count held while registered. */ 3754 xpt_release_bus(bus_path.bus); 3755 xpt_release_path(&bus_path); 3756 3757 return (CAM_REQ_CMP); 3758} 3759 3760static path_id_t 3761xptnextfreepathid(void) 3762{ 3763 struct cam_eb *bus; 3764 path_id_t pathid; 3765 const char *strval; 3766 3767 pathid = 0; 3768 mtx_lock(&xsoftc.xpt_topo_lock); 3769 bus = TAILQ_FIRST(&xsoftc.xpt_busses); 3770retry: 3771 /* Find an unoccupied pathid */ 3772 while (bus != NULL && bus->path_id <= pathid) { 3773 if (bus->path_id == pathid) 3774 pathid++; 3775 bus = TAILQ_NEXT(bus, links); 3776 } 3777 mtx_unlock(&xsoftc.xpt_topo_lock); 3778 3779 /* 3780 * Ensure that this pathid is not reserved for 3781 * a bus that may be registered in the future. 3782 */ 3783 if (resource_string_value("scbus", pathid, "at", &strval) == 0) { 3784 ++pathid; 3785 /* Start the search over */ 3786 mtx_lock(&xsoftc.xpt_topo_lock); 3787 goto retry; 3788 } 3789 return (pathid); 3790} 3791 3792static path_id_t 3793xptpathid(const char *sim_name, int sim_unit, int sim_bus) 3794{ 3795 path_id_t pathid; 3796 int i, dunit, val; 3797 char buf[32]; 3798 const char *dname; 3799 3800 pathid = CAM_XPT_PATH_ID; 3801 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit); 3802 i = 0; 3803 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) { 3804 if (strcmp(dname, "scbus")) { 3805 /* Avoid a bit of foot shooting. */ 3806 continue; 3807 } 3808 if (dunit < 0) /* unwired?! */ 3809 continue; 3810 if (resource_int_value("scbus", dunit, "bus", &val) == 0) { 3811 if (sim_bus == val) { 3812 pathid = dunit; 3813 break; 3814 } 3815 } else if (sim_bus == 0) { 3816 /* Unspecified matches bus 0 */ 3817 pathid = dunit; 3818 break; 3819 } else { 3820 printf("Ambiguous scbus configuration for %s%d " 3821 "bus %d, cannot wire down. The kernel " 3822 "config entry for scbus%d should " 3823 "specify a controller bus.\n" 3824 "Scbus will be assigned dynamically.\n", 3825 sim_name, sim_unit, sim_bus, dunit); 3826 break; 3827 } 3828 } 3829 3830 if (pathid == CAM_XPT_PATH_ID) 3831 pathid = xptnextfreepathid(); 3832 return (pathid); 3833} 3834 3835void 3836xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg) 3837{ 3838 struct cam_eb *bus; 3839 struct cam_et *target, *next_target; 3840 struct cam_ed *device, *next_device; 3841 3842 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3843 3844 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n")); 3845 3846 /* 3847 * Most async events come from a CAM interrupt context. In 3848 * a few cases, the error recovery code at the peripheral layer, 3849 * which may run from our SWI or a process context, may signal 3850 * deferred events with a call to xpt_async. 3851 */ 3852 3853 bus = path->bus; 3854 3855 if (async_code == AC_BUS_RESET) { 3856 /* Update our notion of when the last reset occurred */ 3857 microtime(&bus->last_reset); 3858 } 3859 3860 for (target = TAILQ_FIRST(&bus->et_entries); 3861 target != NULL; 3862 target = next_target) { 3863 3864 next_target = TAILQ_NEXT(target, links); 3865 3866 if (path->target != target 3867 && path->target->target_id != CAM_TARGET_WILDCARD 3868 && target->target_id != CAM_TARGET_WILDCARD) 3869 continue; 3870 3871 if (async_code == AC_SENT_BDR) { 3872 /* Update our notion of when the last reset occurred */ 3873 microtime(&path->target->last_reset); 3874 } 3875 3876 for (device = TAILQ_FIRST(&target->ed_entries); 3877 device != NULL; 3878 device = next_device) { 3879 3880 next_device = TAILQ_NEXT(device, links); 3881 3882 if (path->device != device 3883 && path->device->lun_id != CAM_LUN_WILDCARD 3884 && device->lun_id != CAM_LUN_WILDCARD) 3885 continue; 3886 /* 3887 * The async callback could free the device. 3888 * If it is a broadcast async, it doesn't hold 3889 * device reference, so take our own reference. 3890 */ 3891 xpt_acquire_device(device); 3892 (*(bus->xport->async))(async_code, bus, 3893 target, device, 3894 async_arg); 3895 3896 xpt_async_bcast(&device->asyncs, async_code, 3897 path, async_arg); 3898 xpt_release_device(device); 3899 } 3900 } 3901 3902 /* 3903 * If this wasn't a fully wildcarded async, tell all 3904 * clients that want all async events. 3905 */ 3906 if (bus != xpt_periph->path->bus) 3907 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code, 3908 path, async_arg); 3909} 3910 3911static void 3912xpt_async_bcast(struct async_list *async_head, 3913 u_int32_t async_code, 3914 struct cam_path *path, void *async_arg) 3915{ 3916 struct async_node *cur_entry; 3917 3918 cur_entry = SLIST_FIRST(async_head); 3919 while (cur_entry != NULL) { 3920 struct async_node *next_entry; 3921 /* 3922 * Grab the next list entry before we call the current 3923 * entry's callback. This is because the callback function 3924 * can delete its async callback entry. 3925 */ 3926 next_entry = SLIST_NEXT(cur_entry, links); 3927 if ((cur_entry->event_enable & async_code) != 0) 3928 cur_entry->callback(cur_entry->callback_arg, 3929 async_code, path, 3930 async_arg); 3931 cur_entry = next_entry; 3932 } 3933} 3934 3935static void 3936xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus, 3937 struct cam_et *target, struct cam_ed *device, 3938 void *async_arg) 3939{ 3940 printf("%s called\n", __func__); 3941} 3942 3943u_int32_t 3944xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count) 3945{ 3946 struct cam_ed *dev = path->device; 3947 3948 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3949 dev->sim->devq->alloc_openings += 3950 cam_ccbq_freeze(&dev->ccbq, rl, count); 3951 /* Remove frozen device from allocq. */ 3952 if (device_is_alloc_queued(dev) && 3953 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 3954 CAMQ_GET_PRIO(&dev->drvq)))) { 3955 camq_remove(&dev->sim->devq->alloc_queue, 3956 dev->alloc_ccb_entry.pinfo.index); 3957 } 3958 /* Remove frozen device from sendq. */ 3959 if (device_is_send_queued(dev) && 3960 cam_ccbq_frozen_top(&dev->ccbq)) { 3961 camq_remove(&dev->sim->devq->send_queue, 3962 dev->send_ccb_entry.pinfo.index); 3963 } 3964 return (dev->ccbq.queue.qfrozen_cnt[rl]); 3965} 3966 3967u_int32_t 3968xpt_freeze_devq(struct cam_path *path, u_int count) 3969{ 3970 3971 return (xpt_freeze_devq_rl(path, 0, count)); 3972} 3973 3974u_int32_t 3975xpt_freeze_simq(struct cam_sim *sim, u_int count) 3976{ 3977 3978 mtx_assert(sim->mtx, MA_OWNED); 3979 sim->devq->send_queue.qfrozen_cnt[0] += count; 3980 return (sim->devq->send_queue.qfrozen_cnt[0]); 3981} 3982 3983static void 3984xpt_release_devq_timeout(void *arg) 3985{ 3986 struct cam_ed *device; 3987 3988 device = (struct cam_ed *)arg; 3989 3990 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE); 3991} 3992 3993void 3994xpt_release_devq(struct cam_path *path, u_int count, int run_queue) 3995{ 3996 mtx_assert(path->bus->sim->mtx, MA_OWNED); 3997 3998 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue); 3999} 4000 4001void 4002xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue) 4003{ 4004 mtx_assert(path->bus->sim->mtx, MA_OWNED); 4005 4006 xpt_release_devq_device(path->device, rl, count, run_queue); 4007} 4008 4009static void 4010xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue) 4011{ 4012 4013 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) { 4014#ifdef INVARIANTS 4015 printf("xpt_release_devq(%d): requested %u > present %u\n", 4016 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]); 4017#endif 4018 count = dev->ccbq.queue.qfrozen_cnt[rl]; 4019 } 4020 dev->sim->devq->alloc_openings -= 4021 cam_ccbq_release(&dev->ccbq, rl, count); 4022 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL( 4023 CAMQ_GET_PRIO(&dev->drvq))) == 0) { 4024 if (xpt_schedule_dev_allocq(dev->target->bus, dev)) 4025 xpt_run_dev_allocq(dev->target->bus); 4026 } 4027 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) { 4028 /* 4029 * No longer need to wait for a successful 4030 * command completion. 4031 */ 4032 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE; 4033 /* 4034 * Remove any timeouts that might be scheduled 4035 * to release this queue. 4036 */ 4037 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) { 4038 callout_stop(&dev->callout); 4039 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING; 4040 } 4041 if (run_queue == 0) 4042 return; 4043 /* 4044 * Now that we are unfrozen schedule the 4045 * device so any pending transactions are 4046 * run. 4047 */ 4048 if (xpt_schedule_dev_sendq(dev->target->bus, dev)) 4049 xpt_run_dev_sendq(dev->target->bus); 4050 } 4051} 4052 4053void 4054xpt_release_simq(struct cam_sim *sim, int run_queue) 4055{ 4056 struct camq *sendq; 4057 4058 mtx_assert(sim->mtx, MA_OWNED); 4059 sendq = &(sim->devq->send_queue); 4060 if (sendq->qfrozen_cnt[0] <= 0) { 4061#ifdef INVARIANTS 4062 printf("xpt_release_simq: requested 1 > present %u\n", 4063 sendq->qfrozen_cnt[0]); 4064#endif 4065 } else 4066 sendq->qfrozen_cnt[0]--; 4067 if (sendq->qfrozen_cnt[0] == 0) { 4068 /* 4069 * If there is a timeout scheduled to release this 4070 * sim queue, remove it. The queue frozen count is 4071 * already at 0. 4072 */ 4073 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){ 4074 callout_stop(&sim->callout); 4075 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING; 4076 } 4077 if (run_queue) { 4078 struct cam_eb *bus; 4079 4080 /* 4081 * Now that we are unfrozen run the send queue. 4082 */ 4083 bus = xpt_find_bus(sim->path_id); 4084 xpt_run_dev_sendq(bus); 4085 xpt_release_bus(bus); 4086 } 4087 } 4088} 4089 4090/* 4091 * XXX Appears to be unused. 4092 */ 4093static void 4094xpt_release_simq_timeout(void *arg) 4095{ 4096 struct cam_sim *sim; 4097 4098 sim = (struct cam_sim *)arg; 4099 xpt_release_simq(sim, /* run_queue */ TRUE); 4100} 4101 4102void 4103xpt_done(union ccb *done_ccb) 4104{ 4105 struct cam_sim *sim; 4106 int first; 4107 4108 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n")); 4109 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 4110 /* 4111 * Queue up the request for handling by our SWI handler 4112 * any of the "non-immediate" type of ccbs. 4113 */ 4114 sim = done_ccb->ccb_h.path->bus->sim; 4115 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h, 4116 sim_links.tqe); 4117 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX; 4118 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) { 4119 mtx_lock(&cam_simq_lock); 4120 first = TAILQ_EMPTY(&cam_simq); 4121 TAILQ_INSERT_TAIL(&cam_simq, sim, links); 4122 mtx_unlock(&cam_simq_lock); 4123 sim->flags |= CAM_SIM_ON_DONEQ; 4124 if (first) 4125 swi_sched(cambio_ih, 0); 4126 } 4127 } 4128} 4129 4130union ccb * 4131xpt_alloc_ccb() 4132{ 4133 union ccb *new_ccb; 4134 4135 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK); 4136 return (new_ccb); 4137} 4138 4139union ccb * 4140xpt_alloc_ccb_nowait() 4141{ 4142 union ccb *new_ccb; 4143 4144 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT); 4145 return (new_ccb); 4146} 4147 4148void 4149xpt_free_ccb(union ccb *free_ccb) 4150{ 4151 free(free_ccb, M_CAMXPT); 4152} 4153 4154 4155 4156/* Private XPT functions */ 4157 4158/* 4159 * Get a CAM control block for the caller. Charge the structure to the device 4160 * referenced by the path. If the this device has no 'credits' then the 4161 * device already has the maximum number of outstanding operations under way 4162 * and we return NULL. If we don't have sufficient resources to allocate more 4163 * ccbs, we also return NULL. 4164 */ 4165static union ccb * 4166xpt_get_ccb(struct cam_ed *device) 4167{ 4168 union ccb *new_ccb; 4169 struct cam_sim *sim; 4170 4171 sim = device->sim; 4172 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) { 4173 new_ccb = xpt_alloc_ccb_nowait(); 4174 if (new_ccb == NULL) { 4175 return (NULL); 4176 } 4177 if ((sim->flags & CAM_SIM_MPSAFE) == 0) 4178 callout_handle_init(&new_ccb->ccb_h.timeout_ch); 4179 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h, 4180 xpt_links.sle); 4181 sim->ccb_count++; 4182 } 4183 cam_ccbq_take_opening(&device->ccbq); 4184 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle); 4185 return (new_ccb); 4186} 4187 4188static void 4189xpt_release_bus(struct cam_eb *bus) 4190{ 4191 4192 if ((--bus->refcount == 0) 4193 && (TAILQ_FIRST(&bus->et_entries) == NULL)) { 4194 mtx_lock(&xsoftc.xpt_topo_lock); 4195 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links); 4196 xsoftc.bus_generation++; 4197 mtx_unlock(&xsoftc.xpt_topo_lock); 4198 cam_sim_release(bus->sim); 4199 free(bus, M_CAMXPT); 4200 } 4201} 4202 4203static struct cam_et * 4204xpt_alloc_target(struct cam_eb *bus, target_id_t target_id) 4205{ 4206 struct cam_et *target; 4207 4208 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT); 4209 if (target != NULL) { 4210 struct cam_et *cur_target; 4211 4212 TAILQ_INIT(&target->ed_entries); 4213 target->bus = bus; 4214 target->target_id = target_id; 4215 target->refcount = 1; 4216 target->generation = 0; 4217 target->luns = NULL; 4218 timevalclear(&target->last_reset); 4219 /* 4220 * Hold a reference to our parent bus so it 4221 * will not go away before we do. 4222 */ 4223 bus->refcount++; 4224 4225 /* Insertion sort into our bus's target list */ 4226 cur_target = TAILQ_FIRST(&bus->et_entries); 4227 while (cur_target != NULL && cur_target->target_id < target_id) 4228 cur_target = TAILQ_NEXT(cur_target, links); 4229 4230 if (cur_target != NULL) { 4231 TAILQ_INSERT_BEFORE(cur_target, target, links); 4232 } else { 4233 TAILQ_INSERT_TAIL(&bus->et_entries, target, links); 4234 } 4235 bus->generation++; 4236 } 4237 return (target); 4238} 4239 4240static void 4241xpt_release_target(struct cam_et *target) 4242{ 4243 4244 if ((--target->refcount == 0) 4245 && (TAILQ_FIRST(&target->ed_entries) == NULL)) { 4246 TAILQ_REMOVE(&target->bus->et_entries, target, links); 4247 target->bus->generation++; 4248 xpt_release_bus(target->bus); 4249 if (target->luns) 4250 free(target->luns, M_CAMXPT); 4251 free(target, M_CAMXPT); 4252 } 4253} 4254 4255static struct cam_ed * 4256xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target, 4257 lun_id_t lun_id) 4258{ 4259 struct cam_ed *device, *cur_device; 4260 4261 device = xpt_alloc_device(bus, target, lun_id); 4262 if (device == NULL) 4263 return (NULL); 4264 4265 device->mintags = 1; 4266 device->maxtags = 1; 4267 bus->sim->max_ccbs += device->ccbq.devq_openings; 4268 cur_device = TAILQ_FIRST(&target->ed_entries); 4269 while (cur_device != NULL && cur_device->lun_id < lun_id) 4270 cur_device = TAILQ_NEXT(cur_device, links); 4271 if (cur_device != NULL) { 4272 TAILQ_INSERT_BEFORE(cur_device, device, links); 4273 } else { 4274 TAILQ_INSERT_TAIL(&target->ed_entries, device, links); 4275 } 4276 target->generation++; 4277 4278 return (device); 4279} 4280 4281struct cam_ed * 4282xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id) 4283{ 4284 struct cam_ed *device; 4285 struct cam_devq *devq; 4286 cam_status status; 4287 4288 /* Make space for us in the device queue on our bus */ 4289 devq = bus->sim->devq; 4290 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1); 4291 4292 if (status != CAM_REQ_CMP) { 4293 device = NULL; 4294 } else { 4295 device = (struct cam_ed *)malloc(sizeof(*device), 4296 M_CAMXPT, M_NOWAIT); 4297 } 4298 4299 if (device != NULL) { 4300 cam_init_pinfo(&device->alloc_ccb_entry.pinfo); 4301 device->alloc_ccb_entry.device = device; 4302 cam_init_pinfo(&device->send_ccb_entry.pinfo); 4303 device->send_ccb_entry.device = device; 4304 device->target = target; 4305 device->lun_id = lun_id; 4306 device->sim = bus->sim; 4307 /* Initialize our queues */ 4308 if (camq_init(&device->drvq, 0) != 0) { 4309 free(device, M_CAMXPT); 4310 return (NULL); 4311 } 4312 if (cam_ccbq_init(&device->ccbq, 4313 bus->sim->max_dev_openings) != 0) { 4314 camq_fini(&device->drvq); 4315 free(device, M_CAMXPT); 4316 return (NULL); 4317 } 4318 SLIST_INIT(&device->asyncs); 4319 SLIST_INIT(&device->periphs); 4320 device->generation = 0; 4321 device->owner = NULL; 4322 device->flags = CAM_DEV_UNCONFIGURED; 4323 device->tag_delay_count = 0; 4324 device->tag_saved_openings = 0; 4325 device->refcount = 1; 4326 callout_init_mtx(&device->callout, bus->sim->mtx, 0); 4327 4328 /* 4329 * Hold a reference to our parent target so it 4330 * will not go away before we do. 4331 */ 4332 target->refcount++; 4333 4334 } 4335 return (device); 4336} 4337 4338void 4339xpt_acquire_device(struct cam_ed *device) 4340{ 4341 4342 device->refcount++; 4343} 4344 4345void 4346xpt_release_device(struct cam_ed *device) 4347{ 4348 4349 if (--device->refcount == 0) { 4350 struct cam_devq *devq; 4351 4352 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX 4353 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX) 4354 panic("Removing device while still queued for ccbs"); 4355 4356 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) 4357 callout_stop(&device->callout); 4358 4359 TAILQ_REMOVE(&device->target->ed_entries, device,links); 4360 device->target->generation++; 4361 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings; 4362 /* Release our slot in the devq */ 4363 devq = device->target->bus->sim->devq; 4364 cam_devq_resize(devq, devq->alloc_queue.array_size - 1); 4365 camq_fini(&device->drvq); 4366 cam_ccbq_fini(&device->ccbq); 4367 xpt_release_target(device->target); 4368 free(device, M_CAMXPT); 4369 } 4370} 4371 4372u_int32_t 4373xpt_dev_ccbq_resize(struct cam_path *path, int newopenings) 4374{ 4375 int diff; 4376 int result; 4377 struct cam_ed *dev; 4378 4379 dev = path->device; 4380 4381 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings); 4382 result = cam_ccbq_resize(&dev->ccbq, newopenings); 4383 if (result == CAM_REQ_CMP && (diff < 0)) { 4384 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED; 4385 } 4386 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0 4387 || (dev->inq_flags & SID_CmdQue) != 0) 4388 dev->tag_saved_openings = newopenings; 4389 /* Adjust the global limit */ 4390 dev->sim->max_ccbs += diff; 4391 return (result); 4392} 4393 4394static struct cam_eb * 4395xpt_find_bus(path_id_t path_id) 4396{ 4397 struct cam_eb *bus; 4398 4399 mtx_lock(&xsoftc.xpt_topo_lock); 4400 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses); 4401 bus != NULL; 4402 bus = TAILQ_NEXT(bus, links)) { 4403 if (bus->path_id == path_id) { 4404 bus->refcount++; 4405 break; 4406 } 4407 } 4408 mtx_unlock(&xsoftc.xpt_topo_lock); 4409 return (bus); 4410} 4411 4412static struct cam_et * 4413xpt_find_target(struct cam_eb *bus, target_id_t target_id) 4414{ 4415 struct cam_et *target; 4416 4417 for (target = TAILQ_FIRST(&bus->et_entries); 4418 target != NULL; 4419 target = TAILQ_NEXT(target, links)) { 4420 if (target->target_id == target_id) { 4421 target->refcount++; 4422 break; 4423 } 4424 } 4425 return (target); 4426} 4427 4428static struct cam_ed * 4429xpt_find_device(struct cam_et *target, lun_id_t lun_id) 4430{ 4431 struct cam_ed *device; 4432 4433 for (device = TAILQ_FIRST(&target->ed_entries); 4434 device != NULL; 4435 device = TAILQ_NEXT(device, links)) { 4436 if (device->lun_id == lun_id) { 4437 device->refcount++; 4438 break; 4439 } 4440 } 4441 return (device); 4442} 4443 4444void 4445xpt_start_tags(struct cam_path *path) 4446{ 4447 struct ccb_relsim crs; 4448 struct cam_ed *device; 4449 struct cam_sim *sim; 4450 int newopenings; 4451 4452 device = path->device; 4453 sim = path->bus->sim; 4454 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 4455 xpt_freeze_devq(path, /*count*/1); 4456 device->inq_flags |= SID_CmdQue; 4457 if (device->tag_saved_openings != 0) 4458 newopenings = device->tag_saved_openings; 4459 else 4460 newopenings = min(device->maxtags, 4461 sim->max_tagged_dev_openings); 4462 xpt_dev_ccbq_resize(path, newopenings); 4463 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 4464 crs.ccb_h.func_code = XPT_REL_SIMQ; 4465 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 4466 crs.openings 4467 = crs.release_timeout 4468 = crs.qfrozen_cnt 4469 = 0; 4470 xpt_action((union ccb *)&crs); 4471} 4472 4473void 4474xpt_stop_tags(struct cam_path *path) 4475{ 4476 struct ccb_relsim crs; 4477 struct cam_ed *device; 4478 struct cam_sim *sim; 4479 4480 device = path->device; 4481 sim = path->bus->sim; 4482 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT; 4483 device->tag_delay_count = 0; 4484 xpt_freeze_devq(path, /*count*/1); 4485 device->inq_flags &= ~SID_CmdQue; 4486 xpt_dev_ccbq_resize(path, sim->max_dev_openings); 4487 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 4488 crs.ccb_h.func_code = XPT_REL_SIMQ; 4489 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY; 4490 crs.openings 4491 = crs.release_timeout 4492 = crs.qfrozen_cnt 4493 = 0; 4494 xpt_action((union ccb *)&crs); 4495} 4496 4497static void 4498xpt_boot_delay(void *arg) 4499{ 4500 4501 xpt_release_boot(); 4502} 4503 4504static void 4505xpt_config(void *arg) 4506{ 4507 /* 4508 * Now that interrupts are enabled, go find our devices 4509 */ 4510 4511#ifdef CAMDEBUG 4512 /* Setup debugging flags and path */ 4513#ifdef CAM_DEBUG_BUS 4514 if (cam_dflags != CAM_DEBUG_NONE) { 4515 /* 4516 * Locking is specifically omitted here. No SIMs have 4517 * registered yet, so xpt_create_path will only be searching 4518 * empty lists of targets and devices. 4519 */ 4520 if (xpt_create_path(&cam_dpath, xpt_periph, 4521 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, 4522 CAM_DEBUG_LUN) != CAM_REQ_CMP) { 4523 printf("xpt_config: xpt_create_path() failed for debug" 4524 " target %d:%d:%d, debugging disabled\n", 4525 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN); 4526 cam_dflags = CAM_DEBUG_NONE; 4527 } 4528 } else 4529 cam_dpath = NULL; 4530#else /* !CAM_DEBUG_BUS */ 4531 cam_dpath = NULL; 4532#endif /* CAM_DEBUG_BUS */ 4533#endif /* CAMDEBUG */ 4534
|