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