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