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