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