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