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