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