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