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