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