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