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