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