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