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