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