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