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