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