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