2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_queue.h>
59 #include <cam/cam_sim.h>
60 #include <cam/cam_xpt.h>
61 #include <cam/cam_xpt_sim.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_xpt_internal.h>
64 #include <cam/cam_debug.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68 #include <cam/scsi/scsi_pass.h>
70 #include <machine/md_var.h> /* geometry translation */
71 #include <machine/stdarg.h> /* for xpt_print below */
76 * This is the maximum number of high powered commands (e.g. start unit)
77 * that can be outstanding at a particular time.
79 #ifndef CAM_MAX_HIGHPOWER
80 #define CAM_MAX_HIGHPOWER 4
83 /* Datastructures internal to the xpt layer */
84 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
86 /* Object for defering XPT actions to a taskqueue */
99 u_int32_t xpt_generation;
101 /* number of high powered commands that can go through right now */
102 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
105 /* queue for handling async rescan requests. */
106 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
108 int buses_config_done;
110 /* Registered busses */
111 TAILQ_HEAD(,cam_eb) xpt_busses;
112 u_int bus_generation;
114 struct intr_config_hook *xpt_config_hook;
117 struct callout boot_callout;
119 struct mtx xpt_topo_lock;
125 DM_RET_FLAG_MASK = 0x0f,
128 DM_RET_DESCEND = 0x20,
130 DM_RET_ACTION_MASK = 0xf0
138 } xpt_traverse_depth;
140 struct xpt_traverse_config {
141 xpt_traverse_depth depth;
146 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
147 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
148 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
149 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
150 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
152 /* Transport layer configuration information */
153 static struct xpt_softc xsoftc;
155 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
159 /* Queues for our software interrupt handler */
160 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
161 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
162 static cam_simq_t cam_simq;
163 static struct mtx cam_simq_lock;
165 /* Pointers to software interrupt handlers */
166 static void *cambio_ih;
168 struct cam_periph *xpt_periph;
170 static periph_init_t xpt_periph_init;
172 static struct periph_driver xpt_driver =
174 xpt_periph_init, "xpt",
175 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
179 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
181 static d_open_t xptopen;
182 static d_close_t xptclose;
183 static d_ioctl_t xptioctl;
185 static struct cdevsw xpt_cdevsw = {
186 .d_version = D_VERSION,
194 /* Storage for debugging datastructures */
196 struct cam_path *cam_dpath;
197 #ifdef CAM_DEBUG_FLAGS
198 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
200 u_int32_t cam_dflags = CAM_DEBUG_NONE;
202 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
203 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
204 &cam_dflags, 0, "Cam Debug Flags");
205 u_int32_t cam_debug_delay;
206 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
207 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
208 &cam_debug_delay, 0, "Cam Debug Flags");
211 /* Our boot-time initialization hook */
212 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
214 static moduledata_t cam_moduledata = {
216 cam_module_event_handler,
220 static int xpt_init(void *);
222 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
223 MODULE_VERSION(cam, 1);
226 static void xpt_async_bcast(struct async_list *async_head,
227 u_int32_t async_code,
228 struct cam_path *path,
230 static path_id_t xptnextfreepathid(void);
231 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
232 static union ccb *xpt_get_ccb(struct cam_ed *device);
233 static void xpt_run_dev_allocq(struct cam_eb *bus);
234 static void xpt_run_dev_sendq(struct cam_eb *bus);
235 static timeout_t xpt_release_devq_timeout;
236 static void xpt_release_simq_timeout(void *arg) __unused;
237 static void xpt_release_bus(struct cam_eb *bus);
238 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
239 u_int count, int run_queue);
240 static struct cam_et*
241 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
242 static void xpt_release_target(struct cam_et *target);
243 static struct cam_eb*
244 xpt_find_bus(path_id_t path_id);
245 static struct cam_et*
246 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
247 static struct cam_ed*
248 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
249 static void xpt_config(void *arg);
250 static xpt_devicefunc_t xptpassannouncefunc;
251 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
252 static void xptpoll(struct cam_sim *sim);
253 static void camisr(void *);
254 static void camisr_runqueue(void *);
255 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
256 u_int num_patterns, struct cam_eb *bus);
257 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
259 struct cam_ed *device);
260 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
262 struct cam_periph *periph);
263 static xpt_busfunc_t xptedtbusfunc;
264 static xpt_targetfunc_t xptedttargetfunc;
265 static xpt_devicefunc_t xptedtdevicefunc;
266 static xpt_periphfunc_t xptedtperiphfunc;
267 static xpt_pdrvfunc_t xptplistpdrvfunc;
268 static xpt_periphfunc_t xptplistperiphfunc;
269 static int xptedtmatch(struct ccb_dev_match *cdm);
270 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
271 static int xptbustraverse(struct cam_eb *start_bus,
272 xpt_busfunc_t *tr_func, void *arg);
273 static int xpttargettraverse(struct cam_eb *bus,
274 struct cam_et *start_target,
275 xpt_targetfunc_t *tr_func, void *arg);
276 static int xptdevicetraverse(struct cam_et *target,
277 struct cam_ed *start_device,
278 xpt_devicefunc_t *tr_func, void *arg);
279 static int xptperiphtraverse(struct cam_ed *device,
280 struct cam_periph *start_periph,
281 xpt_periphfunc_t *tr_func, void *arg);
282 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
283 xpt_pdrvfunc_t *tr_func, void *arg);
284 static int xptpdperiphtraverse(struct periph_driver **pdrv,
285 struct cam_periph *start_periph,
286 xpt_periphfunc_t *tr_func,
288 static xpt_busfunc_t xptdefbusfunc;
289 static xpt_targetfunc_t xptdeftargetfunc;
290 static xpt_devicefunc_t xptdefdevicefunc;
291 static xpt_periphfunc_t xptdefperiphfunc;
292 static void xpt_finishconfig_task(void *context, int pending);
293 static void xpt_dev_async_default(u_int32_t async_code,
295 struct cam_et *target,
296 struct cam_ed *device,
298 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
299 struct cam_et *target,
301 static xpt_devicefunc_t xptsetasyncfunc;
302 static xpt_busfunc_t xptsetasyncbusfunc;
303 static cam_status xptregister(struct cam_periph *periph,
305 static __inline int periph_is_queued(struct cam_periph *periph);
306 static __inline int device_is_alloc_queued(struct cam_ed *device);
307 static __inline int device_is_send_queued(struct cam_ed *device);
310 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
314 if ((dev->drvq.entries > 0) &&
315 (dev->ccbq.devq_openings > 0) &&
316 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
317 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
319 * The priority of a device waiting for CCB resources
320 * is that of the highest priority peripheral driver
323 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
324 &dev->alloc_ccb_entry.pinfo,
325 CAMQ_GET_PRIO(&dev->drvq));
334 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
338 if ((dev->ccbq.queue.entries > 0) &&
339 (dev->ccbq.dev_openings > 0) &&
340 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
342 * The priority of a device waiting for controller
343 * resources is that of the highest priority CCB
347 xpt_schedule_dev(&bus->sim->devq->send_queue,
348 &dev->send_ccb_entry.pinfo,
349 CAMQ_GET_PRIO(&dev->ccbq.queue));
357 periph_is_queued(struct cam_periph *periph)
359 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
363 device_is_alloc_queued(struct cam_ed *device)
365 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
369 device_is_send_queued(struct cam_ed *device)
371 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
377 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
381 xptdone(struct cam_periph *periph, union ccb *done_ccb)
383 /* Caller will release the CCB */
384 wakeup(&done_ccb->ccb_h.cbfcnp);
388 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
392 * Only allow read-write access.
394 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
398 * We don't allow nonblocking access.
400 if ((flags & O_NONBLOCK) != 0) {
401 printf("%s: can't do nonblocking access\n", devtoname(dev));
405 /* Mark ourselves open */
406 mtx_lock(&xsoftc.xpt_lock);
407 xsoftc.flags |= XPT_FLAG_OPEN;
408 mtx_unlock(&xsoftc.xpt_lock);
414 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
417 /* Mark ourselves closed */
418 mtx_lock(&xsoftc.xpt_lock);
419 xsoftc.flags &= ~XPT_FLAG_OPEN;
420 mtx_unlock(&xsoftc.xpt_lock);
426 * Don't automatically grab the xpt softc lock here even though this is going
427 * through the xpt device. The xpt device is really just a back door for
428 * accessing other devices and SIMs, so the right thing to do is to grab
429 * the appropriate SIM lock once the bus/SIM is located.
432 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
440 * For the transport layer CAMIOCOMMAND ioctl, we really only want
441 * to accept CCB types that don't quite make sense to send through a
442 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
450 inccb = (union ccb *)addr;
452 bus = xpt_find_bus(inccb->ccb_h.path_id);
456 switch (inccb->ccb_h.func_code) {
459 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
460 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
461 xpt_release_bus(bus);
466 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
467 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
468 xpt_release_bus(bus);
476 switch(inccb->ccb_h.func_code) {
484 ccb = xpt_alloc_ccb();
486 CAM_SIM_LOCK(bus->sim);
489 * Create a path using the bus, target, and lun the
492 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
493 inccb->ccb_h.path_id,
494 inccb->ccb_h.target_id,
495 inccb->ccb_h.target_lun) !=
498 CAM_SIM_UNLOCK(bus->sim);
502 /* Ensure all of our fields are correct */
503 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
504 inccb->ccb_h.pinfo.priority);
505 xpt_merge_ccb(ccb, inccb);
506 ccb->ccb_h.cbfcnp = xptdone;
507 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
508 bcopy(ccb, inccb, sizeof(union ccb));
509 xpt_free_path(ccb->ccb_h.path);
511 CAM_SIM_UNLOCK(bus->sim);
518 * This is an immediate CCB, so it's okay to
519 * allocate it on the stack.
522 CAM_SIM_LOCK(bus->sim);
525 * Create a path using the bus, target, and lun the
528 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
529 inccb->ccb_h.path_id,
530 inccb->ccb_h.target_id,
531 inccb->ccb_h.target_lun) !=
534 CAM_SIM_UNLOCK(bus->sim);
537 /* Ensure all of our fields are correct */
538 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
539 inccb->ccb_h.pinfo.priority);
540 xpt_merge_ccb(&ccb, inccb);
541 ccb.ccb_h.cbfcnp = xptdone;
543 CAM_SIM_UNLOCK(bus->sim);
544 bcopy(&ccb, inccb, sizeof(union ccb));
545 xpt_free_path(ccb.ccb_h.path);
549 case XPT_DEV_MATCH: {
550 struct cam_periph_map_info mapinfo;
551 struct cam_path *old_path;
554 * We can't deal with physical addresses for this
555 * type of transaction.
557 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
563 * Save this in case the caller had it set to
564 * something in particular.
566 old_path = inccb->ccb_h.path;
569 * We really don't need a path for the matching
570 * code. The path is needed because of the
571 * debugging statements in xpt_action(). They
572 * assume that the CCB has a valid path.
574 inccb->ccb_h.path = xpt_periph->path;
576 bzero(&mapinfo, sizeof(mapinfo));
579 * Map the pattern and match buffers into kernel
580 * virtual address space.
582 error = cam_periph_mapmem(inccb, &mapinfo);
585 inccb->ccb_h.path = old_path;
590 * This is an immediate CCB, we can send it on directly.
595 * Map the buffers back into user space.
597 cam_periph_unmapmem(inccb, &mapinfo);
599 inccb->ccb_h.path = old_path;
608 xpt_release_bus(bus);
612 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
613 * with the periphal driver name and unit name filled in. The other
614 * fields don't really matter as input. The passthrough driver name
615 * ("pass"), and unit number are passed back in the ccb. The current
616 * device generation number, and the index into the device peripheral
617 * driver list, and the status are also passed back. Note that
618 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
619 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
620 * (or rather should be) impossible for the device peripheral driver
621 * list to change since we look at the whole thing in one pass, and
622 * we do it with lock protection.
625 case CAMGETPASSTHRU: {
627 struct cam_periph *periph;
628 struct periph_driver **p_drv;
631 u_int cur_generation;
632 int base_periph_found;
635 ccb = (union ccb *)addr;
636 unit = ccb->cgdl.unit_number;
637 name = ccb->cgdl.periph_name;
639 * Every 100 devices, we want to drop our lock protection to
640 * give the software interrupt handler a chance to run.
641 * Most systems won't run into this check, but this should
642 * avoid starvation in the software interrupt handler in
647 ccb = (union ccb *)addr;
649 base_periph_found = 0;
652 * Sanity check -- make sure we don't get a null peripheral
655 if (*ccb->cgdl.periph_name == '\0') {
660 /* Keep the list from changing while we traverse it */
661 mtx_lock(&xsoftc.xpt_topo_lock);
663 cur_generation = xsoftc.xpt_generation;
665 /* first find our driver in the list of drivers */
666 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
667 if (strcmp((*p_drv)->driver_name, name) == 0)
670 if (*p_drv == NULL) {
671 mtx_unlock(&xsoftc.xpt_topo_lock);
672 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
673 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
674 *ccb->cgdl.periph_name = '\0';
675 ccb->cgdl.unit_number = 0;
681 * Run through every peripheral instance of this driver
682 * and check to see whether it matches the unit passed
683 * in by the user. If it does, get out of the loops and
684 * find the passthrough driver associated with that
687 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
688 periph = TAILQ_NEXT(periph, unit_links)) {
690 if (periph->unit_number == unit) {
692 } else if (--splbreaknum == 0) {
693 mtx_unlock(&xsoftc.xpt_topo_lock);
694 mtx_lock(&xsoftc.xpt_topo_lock);
696 if (cur_generation != xsoftc.xpt_generation)
701 * If we found the peripheral driver that the user passed
702 * in, go through all of the peripheral drivers for that
703 * particular device and look for a passthrough driver.
705 if (periph != NULL) {
706 struct cam_ed *device;
709 base_periph_found = 1;
710 device = periph->path->device;
711 for (i = 0, periph = SLIST_FIRST(&device->periphs);
713 periph = SLIST_NEXT(periph, periph_links), i++) {
715 * Check to see whether we have a
716 * passthrough device or not.
718 if (strcmp(periph->periph_name, "pass") == 0) {
720 * Fill in the getdevlist fields.
722 strcpy(ccb->cgdl.periph_name,
723 periph->periph_name);
724 ccb->cgdl.unit_number =
726 if (SLIST_NEXT(periph, periph_links))
728 CAM_GDEVLIST_MORE_DEVS;
731 CAM_GDEVLIST_LAST_DEVICE;
732 ccb->cgdl.generation =
736 * Fill in some CCB header fields
737 * that the user may want.
740 periph->path->bus->path_id;
741 ccb->ccb_h.target_id =
742 periph->path->target->target_id;
743 ccb->ccb_h.target_lun =
744 periph->path->device->lun_id;
745 ccb->ccb_h.status = CAM_REQ_CMP;
752 * If the periph is null here, one of two things has
753 * happened. The first possibility is that we couldn't
754 * find the unit number of the particular peripheral driver
755 * that the user is asking about. e.g. the user asks for
756 * the passthrough driver for "da11". We find the list of
757 * "da" peripherals all right, but there is no unit 11.
758 * The other possibility is that we went through the list
759 * of peripheral drivers attached to the device structure,
760 * but didn't find one with the name "pass". Either way,
761 * we return ENOENT, since we couldn't find something.
763 if (periph == NULL) {
764 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
765 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
766 *ccb->cgdl.periph_name = '\0';
767 ccb->cgdl.unit_number = 0;
770 * It is unfortunate that this is even necessary,
771 * but there are many, many clueless users out there.
772 * If this is true, the user is looking for the
773 * passthrough driver, but doesn't have one in his
776 if (base_periph_found == 1) {
777 printf("xptioctl: pass driver is not in the "
779 printf("xptioctl: put \"device pass\" in "
780 "your kernel config file\n");
783 mtx_unlock(&xsoftc.xpt_topo_lock);
795 cam_module_event_handler(module_t mod, int what, void *arg)
801 if ((error = xpt_init(NULL)) != 0)
814 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
817 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
818 xpt_free_path(done_ccb->ccb_h.path);
819 xpt_free_ccb(done_ccb);
821 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
822 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
827 /* thread to handle bus rescans */
829 xpt_scanner_thread(void *dummy)
836 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
837 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
839 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
840 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
843 sim = ccb->ccb_h.path->bus->sim;
854 xpt_rescan(union ccb *ccb)
858 /* Prepare request */
859 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
860 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
861 ccb->ccb_h.func_code = XPT_SCAN_BUS;
862 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
863 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
864 ccb->ccb_h.func_code = XPT_SCAN_TGT;
865 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
866 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
867 ccb->ccb_h.func_code = XPT_SCAN_LUN;
869 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
870 xpt_free_path(ccb->ccb_h.path);
874 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
875 ccb->ccb_h.cbfcnp = xpt_rescan_done;
876 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
877 /* Don't make duplicate entries for the same paths. */
879 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
880 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
881 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
882 wakeup(&xsoftc.ccb_scanq);
884 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
885 xpt_free_path(ccb->ccb_h.path);
891 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
892 xsoftc.buses_to_config++;
893 wakeup(&xsoftc.ccb_scanq);
897 /* Functions accessed by the peripheral drivers */
899 xpt_init(void *dummy)
901 struct cam_sim *xpt_sim;
902 struct cam_path *path;
903 struct cam_devq *devq;
906 TAILQ_INIT(&xsoftc.xpt_busses);
907 TAILQ_INIT(&cam_simq);
908 TAILQ_INIT(&xsoftc.ccb_scanq);
909 STAILQ_INIT(&xsoftc.highpowerq);
910 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
912 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
913 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
914 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
917 * The xpt layer is, itself, the equivelent of a SIM.
918 * Allow 16 ccbs in the ccb pool for it. This should
919 * give decent parallelism when we probe busses and
920 * perform other XPT functions.
922 devq = cam_simq_alloc(16);
923 xpt_sim = cam_sim_alloc(xptaction,
928 /*mtx*/&xsoftc.xpt_lock,
929 /*max_dev_transactions*/0,
930 /*max_tagged_dev_transactions*/0,
935 mtx_lock(&xsoftc.xpt_lock);
936 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
937 mtx_unlock(&xsoftc.xpt_lock);
938 printf("xpt_init: xpt_bus_register failed with status %#x,"
939 " failing attach\n", status);
944 * Looking at the XPT from the SIM layer, the XPT is
945 * the equivelent of a peripheral driver. Allocate
946 * a peripheral driver entry for us.
948 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
950 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
951 mtx_unlock(&xsoftc.xpt_lock);
952 printf("xpt_init: xpt_create_path failed with status %#x,"
953 " failing attach\n", status);
957 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
958 path, NULL, 0, xpt_sim);
960 mtx_unlock(&xsoftc.xpt_lock);
961 /* Install our software interrupt handlers */
962 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
964 * Register a callback for when interrupts are enabled.
966 xsoftc.xpt_config_hook =
967 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
968 M_CAMXPT, M_NOWAIT | M_ZERO);
969 if (xsoftc.xpt_config_hook == NULL) {
970 printf("xpt_init: Cannot malloc config hook "
971 "- failing attach\n");
974 xsoftc.xpt_config_hook->ich_func = xpt_config;
975 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
976 free (xsoftc.xpt_config_hook, M_CAMXPT);
977 printf("xpt_init: config_intrhook_establish failed "
978 "- failing attach\n");
985 xptregister(struct cam_periph *periph, void *arg)
987 struct cam_sim *xpt_sim;
989 if (periph == NULL) {
990 printf("xptregister: periph was NULL!!\n");
991 return(CAM_REQ_CMP_ERR);
994 xpt_sim = (struct cam_sim *)arg;
995 xpt_sim->softc = periph;
997 periph->softc = NULL;
1003 xpt_add_periph(struct cam_periph *periph)
1005 struct cam_ed *device;
1007 struct periph_list *periph_head;
1009 mtx_assert(periph->sim->mtx, MA_OWNED);
1011 device = periph->path->device;
1013 periph_head = &device->periphs;
1015 status = CAM_REQ_CMP;
1017 if (device != NULL) {
1019 * Make room for this peripheral
1020 * so it will fit in the queue
1021 * when it's scheduled to run
1023 status = camq_resize(&device->drvq,
1024 device->drvq.array_size + 1);
1026 device->generation++;
1028 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1031 mtx_lock(&xsoftc.xpt_topo_lock);
1032 xsoftc.xpt_generation++;
1033 mtx_unlock(&xsoftc.xpt_topo_lock);
1039 xpt_remove_periph(struct cam_periph *periph)
1041 struct cam_ed *device;
1043 mtx_assert(periph->sim->mtx, MA_OWNED);
1045 device = periph->path->device;
1047 if (device != NULL) {
1048 struct periph_list *periph_head;
1050 periph_head = &device->periphs;
1052 /* Release the slot for this peripheral */
1053 camq_resize(&device->drvq, device->drvq.array_size - 1);
1055 device->generation++;
1057 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1060 mtx_lock(&xsoftc.xpt_topo_lock);
1061 xsoftc.xpt_generation++;
1062 mtx_unlock(&xsoftc.xpt_topo_lock);
1067 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1069 struct cam_path *path = periph->path;
1071 mtx_assert(periph->sim->mtx, MA_OWNED);
1073 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1074 periph->periph_name, periph->unit_number,
1075 path->bus->sim->sim_name,
1076 path->bus->sim->unit_number,
1077 path->bus->sim->bus_id,
1079 path->target->target_id,
1080 path->device->lun_id);
1081 printf("%s%d: ", periph->periph_name, periph->unit_number);
1082 if (path->device->protocol == PROTO_SCSI)
1083 scsi_print_inquiry(&path->device->inq_data);
1084 else if (path->device->protocol == PROTO_ATA ||
1085 path->device->protocol == PROTO_SATAPM)
1086 ata_print_ident(&path->device->ident_data);
1088 printf("Unknown protocol device\n");
1089 if (bootverbose && path->device->serial_num_len > 0) {
1090 /* Don't wrap the screen - print only the first 60 chars */
1091 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1092 periph->unit_number, path->device->serial_num);
1094 /* Announce transport details. */
1095 (*(path->bus->xport->announce))(periph);
1096 /* Announce command queueing. */
1097 if (path->device->inq_flags & SID_CmdQue
1098 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1099 printf("%s%d: Command Queueing enabled\n",
1100 periph->periph_name, periph->unit_number);
1102 /* Announce caller's details if they've passed in. */
1103 if (announce_string != NULL)
1104 printf("%s%d: %s\n", periph->periph_name,
1105 periph->unit_number, announce_string);
1109 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1112 struct ccb_dev_advinfo cdai;
1114 memset(&cdai, 0, sizeof(cdai));
1115 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1116 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1119 if (!strcmp(attr, "GEOM::ident"))
1120 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1121 else if (!strcmp(attr, "GEOM::physpath"))
1122 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1126 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1127 if (cdai.buf == NULL) {
1131 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1132 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1133 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1134 if (cdai.provsiz == 0)
1137 if (strlcpy(buf, cdai.buf, len) >= len)
1141 if (cdai.buf != NULL)
1142 free(cdai.buf, M_CAMXPT);
1146 static dev_match_ret
1147 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1150 dev_match_ret retval;
1153 retval = DM_RET_NONE;
1156 * If we aren't given something to match against, that's an error.
1159 return(DM_RET_ERROR);
1162 * If there are no match entries, then this bus matches no
1165 if ((patterns == NULL) || (num_patterns == 0))
1166 return(DM_RET_DESCEND | DM_RET_COPY);
1168 for (i = 0; i < num_patterns; i++) {
1169 struct bus_match_pattern *cur_pattern;
1172 * If the pattern in question isn't for a bus node, we
1173 * aren't interested. However, we do indicate to the
1174 * calling routine that we should continue descending the
1175 * tree, since the user wants to match against lower-level
1178 if (patterns[i].type != DEV_MATCH_BUS) {
1179 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1180 retval |= DM_RET_DESCEND;
1184 cur_pattern = &patterns[i].pattern.bus_pattern;
1187 * If they want to match any bus node, we give them any
1190 if (cur_pattern->flags == BUS_MATCH_ANY) {
1191 /* set the copy flag */
1192 retval |= DM_RET_COPY;
1195 * If we've already decided on an action, go ahead
1198 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1203 * Not sure why someone would do this...
1205 if (cur_pattern->flags == BUS_MATCH_NONE)
1208 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1209 && (cur_pattern->path_id != bus->path_id))
1212 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1213 && (cur_pattern->bus_id != bus->sim->bus_id))
1216 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1217 && (cur_pattern->unit_number != bus->sim->unit_number))
1220 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1221 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1226 * If we get to this point, the user definitely wants
1227 * information on this bus. So tell the caller to copy the
1230 retval |= DM_RET_COPY;
1233 * If the return action has been set to descend, then we
1234 * know that we've already seen a non-bus matching
1235 * expression, therefore we need to further descend the tree.
1236 * This won't change by continuing around the loop, so we
1237 * go ahead and return. If we haven't seen a non-bus
1238 * matching expression, we keep going around the loop until
1239 * we exhaust the matching expressions. We'll set the stop
1240 * flag once we fall out of the loop.
1242 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1247 * If the return action hasn't been set to descend yet, that means
1248 * we haven't seen anything other than bus matching patterns. So
1249 * tell the caller to stop descending the tree -- the user doesn't
1250 * want to match against lower level tree elements.
1252 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1253 retval |= DM_RET_STOP;
1258 static dev_match_ret
1259 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1260 struct cam_ed *device)
1262 dev_match_ret retval;
1265 retval = DM_RET_NONE;
1268 * If we aren't given something to match against, that's an error.
1271 return(DM_RET_ERROR);
1274 * If there are no match entries, then this device matches no
1277 if ((patterns == NULL) || (num_patterns == 0))
1278 return(DM_RET_DESCEND | DM_RET_COPY);
1280 for (i = 0; i < num_patterns; i++) {
1281 struct device_match_pattern *cur_pattern;
1282 struct scsi_vpd_device_id *device_id_page;
1285 * If the pattern in question isn't for a device node, we
1286 * aren't interested.
1288 if (patterns[i].type != DEV_MATCH_DEVICE) {
1289 if ((patterns[i].type == DEV_MATCH_PERIPH)
1290 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1291 retval |= DM_RET_DESCEND;
1295 cur_pattern = &patterns[i].pattern.device_pattern;
1297 /* Error out if mutually exclusive options are specified. */
1298 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1299 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1300 return(DM_RET_ERROR);
1303 * If they want to match any device node, we give them any
1306 if (cur_pattern->flags == DEV_MATCH_ANY)
1310 * Not sure why someone would do this...
1312 if (cur_pattern->flags == DEV_MATCH_NONE)
1315 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1316 && (cur_pattern->path_id != device->target->bus->path_id))
1319 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1320 && (cur_pattern->target_id != device->target->target_id))
1323 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1324 && (cur_pattern->target_lun != device->lun_id))
1327 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1328 && (cam_quirkmatch((caddr_t)&device->inq_data,
1329 (caddr_t)&cur_pattern->data.inq_pat,
1330 1, sizeof(cur_pattern->data.inq_pat),
1331 scsi_static_inquiry_match) == NULL))
1334 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1335 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1336 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1337 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1338 device->device_id_len
1339 - SVPD_DEVICE_ID_HDR_LEN,
1340 cur_pattern->data.devid_pat.id,
1341 cur_pattern->data.devid_pat.id_len) != 0))
1346 * If we get to this point, the user definitely wants
1347 * information on this device. So tell the caller to copy
1350 retval |= DM_RET_COPY;
1353 * If the return action has been set to descend, then we
1354 * know that we've already seen a peripheral matching
1355 * expression, therefore we need to further descend the tree.
1356 * This won't change by continuing around the loop, so we
1357 * go ahead and return. If we haven't seen a peripheral
1358 * matching expression, we keep going around the loop until
1359 * we exhaust the matching expressions. We'll set the stop
1360 * flag once we fall out of the loop.
1362 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1367 * If the return action hasn't been set to descend yet, that means
1368 * we haven't seen any peripheral matching patterns. So tell the
1369 * caller to stop descending the tree -- the user doesn't want to
1370 * match against lower level tree elements.
1372 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1373 retval |= DM_RET_STOP;
1379 * Match a single peripheral against any number of match patterns.
1381 static dev_match_ret
1382 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1383 struct cam_periph *periph)
1385 dev_match_ret retval;
1389 * If we aren't given something to match against, that's an error.
1392 return(DM_RET_ERROR);
1395 * If there are no match entries, then this peripheral matches no
1398 if ((patterns == NULL) || (num_patterns == 0))
1399 return(DM_RET_STOP | DM_RET_COPY);
1402 * There aren't any nodes below a peripheral node, so there's no
1403 * reason to descend the tree any further.
1405 retval = DM_RET_STOP;
1407 for (i = 0; i < num_patterns; i++) {
1408 struct periph_match_pattern *cur_pattern;
1411 * If the pattern in question isn't for a peripheral, we
1412 * aren't interested.
1414 if (patterns[i].type != DEV_MATCH_PERIPH)
1417 cur_pattern = &patterns[i].pattern.periph_pattern;
1420 * If they want to match on anything, then we will do so.
1422 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1423 /* set the copy flag */
1424 retval |= DM_RET_COPY;
1427 * We've already set the return action to stop,
1428 * since there are no nodes below peripherals in
1435 * Not sure why someone would do this...
1437 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1440 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1441 && (cur_pattern->path_id != periph->path->bus->path_id))
1445 * For the target and lun id's, we have to make sure the
1446 * target and lun pointers aren't NULL. The xpt peripheral
1447 * has a wildcard target and device.
1449 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1450 && ((periph->path->target == NULL)
1451 ||(cur_pattern->target_id != periph->path->target->target_id)))
1454 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1455 && ((periph->path->device == NULL)
1456 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1459 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1460 && (cur_pattern->unit_number != periph->unit_number))
1463 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1464 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1469 * If we get to this point, the user definitely wants
1470 * information on this peripheral. So tell the caller to
1471 * copy the data out.
1473 retval |= DM_RET_COPY;
1476 * The return action has already been set to stop, since
1477 * peripherals don't have any nodes below them in the EDT.
1483 * If we get to this point, the peripheral that was passed in
1484 * doesn't match any of the patterns.
1490 xptedtbusfunc(struct cam_eb *bus, void *arg)
1492 struct ccb_dev_match *cdm;
1493 dev_match_ret retval;
1495 cdm = (struct ccb_dev_match *)arg;
1498 * If our position is for something deeper in the tree, that means
1499 * that we've already seen this node. So, we keep going down.
1501 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1502 && (cdm->pos.cookie.bus == bus)
1503 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1504 && (cdm->pos.cookie.target != NULL))
1505 retval = DM_RET_DESCEND;
1507 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1510 * If we got an error, bail out of the search.
1512 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1513 cdm->status = CAM_DEV_MATCH_ERROR;
1518 * If the copy flag is set, copy this bus out.
1520 if (retval & DM_RET_COPY) {
1523 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1524 sizeof(struct dev_match_result));
1527 * If we don't have enough space to put in another
1528 * match result, save our position and tell the
1529 * user there are more devices to check.
1531 if (spaceleft < sizeof(struct dev_match_result)) {
1532 bzero(&cdm->pos, sizeof(cdm->pos));
1533 cdm->pos.position_type =
1534 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1536 cdm->pos.cookie.bus = bus;
1537 cdm->pos.generations[CAM_BUS_GENERATION]=
1538 xsoftc.bus_generation;
1539 cdm->status = CAM_DEV_MATCH_MORE;
1542 j = cdm->num_matches;
1544 cdm->matches[j].type = DEV_MATCH_BUS;
1545 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1546 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1547 cdm->matches[j].result.bus_result.unit_number =
1548 bus->sim->unit_number;
1549 strncpy(cdm->matches[j].result.bus_result.dev_name,
1550 bus->sim->sim_name, DEV_IDLEN);
1554 * If the user is only interested in busses, there's no
1555 * reason to descend to the next level in the tree.
1557 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1561 * If there is a target generation recorded, check it to
1562 * make sure the target list hasn't changed.
1564 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1565 && (bus == cdm->pos.cookie.bus)
1566 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1567 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1568 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1570 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1574 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1575 && (cdm->pos.cookie.bus == bus)
1576 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1577 && (cdm->pos.cookie.target != NULL))
1578 return(xpttargettraverse(bus,
1579 (struct cam_et *)cdm->pos.cookie.target,
1580 xptedttargetfunc, arg));
1582 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1586 xptedttargetfunc(struct cam_et *target, void *arg)
1588 struct ccb_dev_match *cdm;
1590 cdm = (struct ccb_dev_match *)arg;
1593 * If there is a device list generation recorded, check it to
1594 * make sure the device list hasn't changed.
1596 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1597 && (cdm->pos.cookie.bus == target->bus)
1598 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1599 && (cdm->pos.cookie.target == target)
1600 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1601 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1602 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1603 target->generation)) {
1604 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1608 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1609 && (cdm->pos.cookie.bus == target->bus)
1610 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1611 && (cdm->pos.cookie.target == target)
1612 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1613 && (cdm->pos.cookie.device != NULL))
1614 return(xptdevicetraverse(target,
1615 (struct cam_ed *)cdm->pos.cookie.device,
1616 xptedtdevicefunc, arg));
1618 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1622 xptedtdevicefunc(struct cam_ed *device, void *arg)
1625 struct ccb_dev_match *cdm;
1626 dev_match_ret retval;
1628 cdm = (struct ccb_dev_match *)arg;
1631 * If our position is for something deeper in the tree, that means
1632 * that we've already seen this node. So, we keep going down.
1634 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1635 && (cdm->pos.cookie.device == device)
1636 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1637 && (cdm->pos.cookie.periph != NULL))
1638 retval = DM_RET_DESCEND;
1640 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1643 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1644 cdm->status = CAM_DEV_MATCH_ERROR;
1649 * If the copy flag is set, copy this device out.
1651 if (retval & DM_RET_COPY) {
1654 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1655 sizeof(struct dev_match_result));
1658 * If we don't have enough space to put in another
1659 * match result, save our position and tell the
1660 * user there are more devices to check.
1662 if (spaceleft < sizeof(struct dev_match_result)) {
1663 bzero(&cdm->pos, sizeof(cdm->pos));
1664 cdm->pos.position_type =
1665 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1666 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1668 cdm->pos.cookie.bus = device->target->bus;
1669 cdm->pos.generations[CAM_BUS_GENERATION]=
1670 xsoftc.bus_generation;
1671 cdm->pos.cookie.target = device->target;
1672 cdm->pos.generations[CAM_TARGET_GENERATION] =
1673 device->target->bus->generation;
1674 cdm->pos.cookie.device = device;
1675 cdm->pos.generations[CAM_DEV_GENERATION] =
1676 device->target->generation;
1677 cdm->status = CAM_DEV_MATCH_MORE;
1680 j = cdm->num_matches;
1682 cdm->matches[j].type = DEV_MATCH_DEVICE;
1683 cdm->matches[j].result.device_result.path_id =
1684 device->target->bus->path_id;
1685 cdm->matches[j].result.device_result.target_id =
1686 device->target->target_id;
1687 cdm->matches[j].result.device_result.target_lun =
1689 cdm->matches[j].result.device_result.protocol =
1691 bcopy(&device->inq_data,
1692 &cdm->matches[j].result.device_result.inq_data,
1693 sizeof(struct scsi_inquiry_data));
1694 bcopy(&device->ident_data,
1695 &cdm->matches[j].result.device_result.ident_data,
1696 sizeof(struct ata_params));
1698 /* Let the user know whether this device is unconfigured */
1699 if (device->flags & CAM_DEV_UNCONFIGURED)
1700 cdm->matches[j].result.device_result.flags =
1701 DEV_RESULT_UNCONFIGURED;
1703 cdm->matches[j].result.device_result.flags =
1708 * If the user isn't interested in peripherals, don't descend
1709 * the tree any further.
1711 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1715 * If there is a peripheral list generation recorded, make sure
1716 * it hasn't changed.
1718 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1719 && (device->target->bus == cdm->pos.cookie.bus)
1720 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1721 && (device->target == cdm->pos.cookie.target)
1722 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1723 && (device == cdm->pos.cookie.device)
1724 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1725 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1726 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1727 device->generation)){
1728 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1732 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1733 && (cdm->pos.cookie.bus == device->target->bus)
1734 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1735 && (cdm->pos.cookie.target == device->target)
1736 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1737 && (cdm->pos.cookie.device == device)
1738 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1739 && (cdm->pos.cookie.periph != NULL))
1740 return(xptperiphtraverse(device,
1741 (struct cam_periph *)cdm->pos.cookie.periph,
1742 xptedtperiphfunc, arg));
1744 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1748 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1750 struct ccb_dev_match *cdm;
1751 dev_match_ret retval;
1753 cdm = (struct ccb_dev_match *)arg;
1755 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1757 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1758 cdm->status = CAM_DEV_MATCH_ERROR;
1763 * If the copy flag is set, copy this peripheral out.
1765 if (retval & DM_RET_COPY) {
1768 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1769 sizeof(struct dev_match_result));
1772 * If we don't have enough space to put in another
1773 * match result, save our position and tell the
1774 * user there are more devices to check.
1776 if (spaceleft < sizeof(struct dev_match_result)) {
1777 bzero(&cdm->pos, sizeof(cdm->pos));
1778 cdm->pos.position_type =
1779 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1780 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1783 cdm->pos.cookie.bus = periph->path->bus;
1784 cdm->pos.generations[CAM_BUS_GENERATION]=
1785 xsoftc.bus_generation;
1786 cdm->pos.cookie.target = periph->path->target;
1787 cdm->pos.generations[CAM_TARGET_GENERATION] =
1788 periph->path->bus->generation;
1789 cdm->pos.cookie.device = periph->path->device;
1790 cdm->pos.generations[CAM_DEV_GENERATION] =
1791 periph->path->target->generation;
1792 cdm->pos.cookie.periph = periph;
1793 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1794 periph->path->device->generation;
1795 cdm->status = CAM_DEV_MATCH_MORE;
1799 j = cdm->num_matches;
1801 cdm->matches[j].type = DEV_MATCH_PERIPH;
1802 cdm->matches[j].result.periph_result.path_id =
1803 periph->path->bus->path_id;
1804 cdm->matches[j].result.periph_result.target_id =
1805 periph->path->target->target_id;
1806 cdm->matches[j].result.periph_result.target_lun =
1807 periph->path->device->lun_id;
1808 cdm->matches[j].result.periph_result.unit_number =
1809 periph->unit_number;
1810 strncpy(cdm->matches[j].result.periph_result.periph_name,
1811 periph->periph_name, DEV_IDLEN);
1818 xptedtmatch(struct ccb_dev_match *cdm)
1822 cdm->num_matches = 0;
1825 * Check the bus list generation. If it has changed, the user
1826 * needs to reset everything and start over.
1828 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1829 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1830 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1831 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1835 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1836 && (cdm->pos.cookie.bus != NULL))
1837 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1838 xptedtbusfunc, cdm);
1840 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1843 * If we get back 0, that means that we had to stop before fully
1844 * traversing the EDT. It also means that one of the subroutines
1845 * has set the status field to the proper value. If we get back 1,
1846 * we've fully traversed the EDT and copied out any matching entries.
1849 cdm->status = CAM_DEV_MATCH_LAST;
1855 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1857 struct ccb_dev_match *cdm;
1859 cdm = (struct ccb_dev_match *)arg;
1861 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1862 && (cdm->pos.cookie.pdrv == pdrv)
1863 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1864 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1865 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1866 (*pdrv)->generation)) {
1867 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1871 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1872 && (cdm->pos.cookie.pdrv == pdrv)
1873 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1874 && (cdm->pos.cookie.periph != NULL))
1875 return(xptpdperiphtraverse(pdrv,
1876 (struct cam_periph *)cdm->pos.cookie.periph,
1877 xptplistperiphfunc, arg));
1879 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1883 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1885 struct ccb_dev_match *cdm;
1886 dev_match_ret retval;
1888 cdm = (struct ccb_dev_match *)arg;
1890 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1892 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1893 cdm->status = CAM_DEV_MATCH_ERROR;
1898 * If the copy flag is set, copy this peripheral out.
1900 if (retval & DM_RET_COPY) {
1903 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1904 sizeof(struct dev_match_result));
1907 * If we don't have enough space to put in another
1908 * match result, save our position and tell the
1909 * user there are more devices to check.
1911 if (spaceleft < sizeof(struct dev_match_result)) {
1912 struct periph_driver **pdrv;
1915 bzero(&cdm->pos, sizeof(cdm->pos));
1916 cdm->pos.position_type =
1917 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1921 * This may look a bit non-sensical, but it is
1922 * actually quite logical. There are very few
1923 * peripheral drivers, and bloating every peripheral
1924 * structure with a pointer back to its parent
1925 * peripheral driver linker set entry would cost
1926 * more in the long run than doing this quick lookup.
1928 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1929 if (strcmp((*pdrv)->driver_name,
1930 periph->periph_name) == 0)
1934 if (*pdrv == NULL) {
1935 cdm->status = CAM_DEV_MATCH_ERROR;
1939 cdm->pos.cookie.pdrv = pdrv;
1941 * The periph generation slot does double duty, as
1942 * does the periph pointer slot. They are used for
1943 * both edt and pdrv lookups and positioning.
1945 cdm->pos.cookie.periph = periph;
1946 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1947 (*pdrv)->generation;
1948 cdm->status = CAM_DEV_MATCH_MORE;
1952 j = cdm->num_matches;
1954 cdm->matches[j].type = DEV_MATCH_PERIPH;
1955 cdm->matches[j].result.periph_result.path_id =
1956 periph->path->bus->path_id;
1959 * The transport layer peripheral doesn't have a target or
1962 if (periph->path->target)
1963 cdm->matches[j].result.periph_result.target_id =
1964 periph->path->target->target_id;
1966 cdm->matches[j].result.periph_result.target_id = -1;
1968 if (periph->path->device)
1969 cdm->matches[j].result.periph_result.target_lun =
1970 periph->path->device->lun_id;
1972 cdm->matches[j].result.periph_result.target_lun = -1;
1974 cdm->matches[j].result.periph_result.unit_number =
1975 periph->unit_number;
1976 strncpy(cdm->matches[j].result.periph_result.periph_name,
1977 periph->periph_name, DEV_IDLEN);
1984 xptperiphlistmatch(struct ccb_dev_match *cdm)
1988 cdm->num_matches = 0;
1991 * At this point in the edt traversal function, we check the bus
1992 * list generation to make sure that no busses have been added or
1993 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1994 * For the peripheral driver list traversal function, however, we
1995 * don't have to worry about new peripheral driver types coming or
1996 * going; they're in a linker set, and therefore can't change
1997 * without a recompile.
2000 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2001 && (cdm->pos.cookie.pdrv != NULL))
2002 ret = xptpdrvtraverse(
2003 (struct periph_driver **)cdm->pos.cookie.pdrv,
2004 xptplistpdrvfunc, cdm);
2006 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2009 * If we get back 0, that means that we had to stop before fully
2010 * traversing the peripheral driver tree. It also means that one of
2011 * the subroutines has set the status field to the proper value. If
2012 * we get back 1, we've fully traversed the EDT and copied out any
2016 cdm->status = CAM_DEV_MATCH_LAST;
2022 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2024 struct cam_eb *bus, *next_bus;
2029 mtx_lock(&xsoftc.xpt_topo_lock);
2030 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2033 next_bus = TAILQ_NEXT(bus, links);
2035 mtx_unlock(&xsoftc.xpt_topo_lock);
2036 CAM_SIM_LOCK(bus->sim);
2037 retval = tr_func(bus, arg);
2038 CAM_SIM_UNLOCK(bus->sim);
2041 mtx_lock(&xsoftc.xpt_topo_lock);
2043 mtx_unlock(&xsoftc.xpt_topo_lock);
2049 xpt_sim_opened(struct cam_sim *sim)
2052 struct cam_et *target;
2053 struct cam_ed *device;
2054 struct cam_periph *periph;
2056 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2057 mtx_assert(sim->mtx, MA_OWNED);
2059 mtx_lock(&xsoftc.xpt_topo_lock);
2060 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2061 if (bus->sim != sim)
2064 TAILQ_FOREACH(target, &bus->et_entries, links) {
2065 TAILQ_FOREACH(device, &target->ed_entries, links) {
2066 SLIST_FOREACH(periph, &device->periphs,
2068 if (periph->refcount > 0) {
2069 mtx_unlock(&xsoftc.xpt_topo_lock);
2077 mtx_unlock(&xsoftc.xpt_topo_lock);
2082 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2083 xpt_targetfunc_t *tr_func, void *arg)
2085 struct cam_et *target, *next_target;
2089 for (target = (start_target ? start_target :
2090 TAILQ_FIRST(&bus->et_entries));
2091 target != NULL; target = next_target) {
2093 next_target = TAILQ_NEXT(target, links);
2095 retval = tr_func(target, arg);
2105 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2106 xpt_devicefunc_t *tr_func, void *arg)
2108 struct cam_ed *device, *next_device;
2112 for (device = (start_device ? start_device :
2113 TAILQ_FIRST(&target->ed_entries));
2115 device = next_device) {
2117 next_device = TAILQ_NEXT(device, links);
2119 retval = tr_func(device, arg);
2129 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2130 xpt_periphfunc_t *tr_func, void *arg)
2132 struct cam_periph *periph, *next_periph;
2137 for (periph = (start_periph ? start_periph :
2138 SLIST_FIRST(&device->periphs));
2140 periph = next_periph) {
2142 next_periph = SLIST_NEXT(periph, periph_links);
2144 retval = tr_func(periph, arg);
2153 xptpdrvtraverse(struct periph_driver **start_pdrv,
2154 xpt_pdrvfunc_t *tr_func, void *arg)
2156 struct periph_driver **pdrv;
2162 * We don't traverse the peripheral driver list like we do the
2163 * other lists, because it is a linker set, and therefore cannot be
2164 * changed during runtime. If the peripheral driver list is ever
2165 * re-done to be something other than a linker set (i.e. it can
2166 * change while the system is running), the list traversal should
2167 * be modified to work like the other traversal functions.
2169 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2170 *pdrv != NULL; pdrv++) {
2171 retval = tr_func(pdrv, arg);
2181 xptpdperiphtraverse(struct periph_driver **pdrv,
2182 struct cam_periph *start_periph,
2183 xpt_periphfunc_t *tr_func, void *arg)
2185 struct cam_periph *periph, *next_periph;
2191 for (periph = (start_periph ? start_periph :
2192 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2193 periph = next_periph) {
2195 next_periph = TAILQ_NEXT(periph, unit_links);
2197 retval = tr_func(periph, arg);
2208 xptdefbusfunc(struct cam_eb *bus, void *arg)
2210 struct xpt_traverse_config *tr_config;
2212 tr_config = (struct xpt_traverse_config *)arg;
2214 if (tr_config->depth == XPT_DEPTH_BUS) {
2215 xpt_busfunc_t *tr_func;
2217 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2219 return(tr_func(bus, tr_config->tr_arg));
2221 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2225 xptdeftargetfunc(struct cam_et *target, void *arg)
2227 struct xpt_traverse_config *tr_config;
2229 tr_config = (struct xpt_traverse_config *)arg;
2231 if (tr_config->depth == XPT_DEPTH_TARGET) {
2232 xpt_targetfunc_t *tr_func;
2234 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2236 return(tr_func(target, tr_config->tr_arg));
2238 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2242 xptdefdevicefunc(struct cam_ed *device, void *arg)
2244 struct xpt_traverse_config *tr_config;
2246 tr_config = (struct xpt_traverse_config *)arg;
2248 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2249 xpt_devicefunc_t *tr_func;
2251 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2253 return(tr_func(device, tr_config->tr_arg));
2255 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2259 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2261 struct xpt_traverse_config *tr_config;
2262 xpt_periphfunc_t *tr_func;
2264 tr_config = (struct xpt_traverse_config *)arg;
2266 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2269 * Unlike the other default functions, we don't check for depth
2270 * here. The peripheral driver level is the last level in the EDT,
2271 * so if we're here, we should execute the function in question.
2273 return(tr_func(periph, tr_config->tr_arg));
2277 * Execute the given function for every bus in the EDT.
2280 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2282 struct xpt_traverse_config tr_config;
2284 tr_config.depth = XPT_DEPTH_BUS;
2285 tr_config.tr_func = tr_func;
2286 tr_config.tr_arg = arg;
2288 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2292 * Execute the given function for every device in the EDT.
2295 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2297 struct xpt_traverse_config tr_config;
2299 tr_config.depth = XPT_DEPTH_DEVICE;
2300 tr_config.tr_func = tr_func;
2301 tr_config.tr_arg = arg;
2303 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2307 xptsetasyncfunc(struct cam_ed *device, void *arg)
2309 struct cam_path path;
2310 struct ccb_getdev cgd;
2311 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2314 * Don't report unconfigured devices (Wildcard devs,
2315 * devices only for target mode, device instances
2316 * that have been invalidated but are waiting for
2317 * their last reference count to be released).
2319 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2322 xpt_compile_path(&path,
2324 device->target->bus->path_id,
2325 device->target->target_id,
2327 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2328 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2329 xpt_action((union ccb *)&cgd);
2330 csa->callback(csa->callback_arg,
2333 xpt_release_path(&path);
2339 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2341 struct cam_path path;
2342 struct ccb_pathinq cpi;
2343 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2345 xpt_compile_path(&path, /*periph*/NULL,
2347 CAM_TARGET_WILDCARD,
2349 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2350 cpi.ccb_h.func_code = XPT_PATH_INQ;
2351 xpt_action((union ccb *)&cpi);
2352 csa->callback(csa->callback_arg,
2355 xpt_release_path(&path);
2361 xpt_action(union ccb *start_ccb)
2364 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2366 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2367 /* Compatibility for RL-unaware code. */
2368 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2369 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2370 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2374 xpt_action_default(union ccb *start_ccb)
2377 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2379 struct cam_path *path;
2381 path = start_ccb->ccb_h.path;
2382 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2384 switch (start_ccb->ccb_h.func_code) {
2387 struct cam_ed *device;
2390 * For the sake of compatibility with SCSI-1
2391 * devices that may not understand the identify
2392 * message, we include lun information in the
2393 * second byte of all commands. SCSI-1 specifies
2394 * that luns are a 3 bit value and reserves only 3
2395 * bits for lun information in the CDB. Later
2396 * revisions of the SCSI spec allow for more than 8
2397 * luns, but have deprecated lun information in the
2398 * CDB. So, if the lun won't fit, we must omit.
2400 * Also be aware that during initial probing for devices,
2401 * the inquiry information is unknown but initialized to 0.
2402 * This means that this code will be exercised while probing
2403 * devices with an ANSI revision greater than 2.
2405 device = path->device;
2406 if (device->protocol_version <= SCSI_REV_2
2407 && start_ccb->ccb_h.target_lun < 8
2408 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2410 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2411 start_ccb->ccb_h.target_lun << 5;
2413 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2414 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2415 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2416 &path->device->inq_data),
2417 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2418 cdb_str, sizeof(cdb_str))));
2422 case XPT_CONT_TARGET_IO:
2423 start_ccb->csio.sense_resid = 0;
2424 start_ccb->csio.resid = 0;
2427 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2428 start_ccb->ataio.resid = 0;
2429 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2430 ata_op_string(&start_ccb->ataio.cmd),
2431 ata_cmd_string(&start_ccb->ataio.cmd,
2432 cdb_str, sizeof(cdb_str))));
2441 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2442 path->device->sim->devq->alloc_openings += frozen;
2444 xpt_run_dev_allocq(path->bus);
2445 if (xpt_schedule_dev_sendq(path->bus, path->device))
2446 xpt_run_dev_sendq(path->bus);
2449 case XPT_CALC_GEOMETRY:
2451 struct cam_sim *sim;
2453 /* Filter out garbage */
2454 if (start_ccb->ccg.block_size == 0
2455 || start_ccb->ccg.volume_size == 0) {
2456 start_ccb->ccg.cylinders = 0;
2457 start_ccb->ccg.heads = 0;
2458 start_ccb->ccg.secs_per_track = 0;
2459 start_ccb->ccb_h.status = CAM_REQ_CMP;
2462 #if defined(PC98) || defined(__sparc64__)
2464 * In a PC-98 system, geometry translation depens on
2465 * the "real" device geometry obtained from mode page 4.
2466 * SCSI geometry translation is performed in the
2467 * initialization routine of the SCSI BIOS and the result
2468 * stored in host memory. If the translation is available
2469 * in host memory, use it. If not, rely on the default
2470 * translation the device driver performs.
2471 * For sparc64, we may need adjust the geometry of large
2472 * disks in order to fit the limitations of the 16-bit
2473 * fields of the VTOC8 disk label.
2475 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2476 start_ccb->ccb_h.status = CAM_REQ_CMP;
2480 sim = path->bus->sim;
2481 (*(sim->sim_action))(sim, start_ccb);
2486 union ccb* abort_ccb;
2488 abort_ccb = start_ccb->cab.abort_ccb;
2489 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2491 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2492 struct cam_ccbq *ccbq;
2493 struct cam_ed *device;
2495 device = abort_ccb->ccb_h.path->device;
2496 ccbq = &device->ccbq;
2497 device->sim->devq->alloc_openings -=
2498 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2499 abort_ccb->ccb_h.status =
2500 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2501 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2502 xpt_done(abort_ccb);
2503 start_ccb->ccb_h.status = CAM_REQ_CMP;
2506 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2507 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2509 * We've caught this ccb en route to
2510 * the SIM. Flag it for abort and the
2511 * SIM will do so just before starting
2512 * real work on the CCB.
2514 abort_ccb->ccb_h.status =
2515 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2516 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2517 start_ccb->ccb_h.status = CAM_REQ_CMP;
2521 if (XPT_FC_IS_QUEUED(abort_ccb)
2522 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2524 * It's already completed but waiting
2525 * for our SWI to get to it.
2527 start_ccb->ccb_h.status = CAM_UA_ABORT;
2531 * If we weren't able to take care of the abort request
2532 * in the XPT, pass the request down to the SIM for processing.
2536 case XPT_ACCEPT_TARGET_IO:
2538 case XPT_IMMED_NOTIFY:
2539 case XPT_NOTIFY_ACK:
2541 case XPT_IMMEDIATE_NOTIFY:
2542 case XPT_NOTIFY_ACKNOWLEDGE:
2543 case XPT_GET_SIM_KNOB:
2544 case XPT_SET_SIM_KNOB:
2546 struct cam_sim *sim;
2548 sim = path->bus->sim;
2549 (*(sim->sim_action))(sim, start_ccb);
2554 struct cam_sim *sim;
2556 sim = path->bus->sim;
2557 (*(sim->sim_action))(sim, start_ccb);
2560 case XPT_PATH_STATS:
2561 start_ccb->cpis.last_reset = path->bus->last_reset;
2562 start_ccb->ccb_h.status = CAM_REQ_CMP;
2569 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2570 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2572 struct ccb_getdev *cgd;
2574 cgd = &start_ccb->cgd;
2575 cgd->protocol = dev->protocol;
2576 cgd->inq_data = dev->inq_data;
2577 cgd->ident_data = dev->ident_data;
2578 cgd->inq_flags = dev->inq_flags;
2579 cgd->ccb_h.status = CAM_REQ_CMP;
2580 cgd->serial_num_len = dev->serial_num_len;
2581 if ((dev->serial_num_len > 0)
2582 && (dev->serial_num != NULL))
2583 bcopy(dev->serial_num, cgd->serial_num,
2584 dev->serial_num_len);
2588 case XPT_GDEV_STATS:
2593 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2594 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2596 struct ccb_getdevstats *cgds;
2600 cgds = &start_ccb->cgds;
2603 cgds->dev_openings = dev->ccbq.dev_openings;
2604 cgds->dev_active = dev->ccbq.dev_active;
2605 cgds->devq_openings = dev->ccbq.devq_openings;
2606 cgds->devq_queued = dev->ccbq.queue.entries;
2607 cgds->held = dev->ccbq.held;
2608 cgds->last_reset = tar->last_reset;
2609 cgds->maxtags = dev->maxtags;
2610 cgds->mintags = dev->mintags;
2611 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2612 cgds->last_reset = bus->last_reset;
2613 cgds->ccb_h.status = CAM_REQ_CMP;
2619 struct cam_periph *nperiph;
2620 struct periph_list *periph_head;
2621 struct ccb_getdevlist *cgdl;
2623 struct cam_ed *device;
2630 * Don't want anyone mucking with our data.
2632 device = path->device;
2633 periph_head = &device->periphs;
2634 cgdl = &start_ccb->cgdl;
2637 * Check and see if the list has changed since the user
2638 * last requested a list member. If so, tell them that the
2639 * list has changed, and therefore they need to start over
2640 * from the beginning.
2642 if ((cgdl->index != 0) &&
2643 (cgdl->generation != device->generation)) {
2644 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2649 * Traverse the list of peripherals and attempt to find
2650 * the requested peripheral.
2652 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2653 (nperiph != NULL) && (i <= cgdl->index);
2654 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2655 if (i == cgdl->index) {
2656 strncpy(cgdl->periph_name,
2657 nperiph->periph_name,
2659 cgdl->unit_number = nperiph->unit_number;
2664 cgdl->status = CAM_GDEVLIST_ERROR;
2668 if (nperiph == NULL)
2669 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2671 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2674 cgdl->generation = device->generation;
2676 cgdl->ccb_h.status = CAM_REQ_CMP;
2681 dev_pos_type position_type;
2682 struct ccb_dev_match *cdm;
2684 cdm = &start_ccb->cdm;
2687 * There are two ways of getting at information in the EDT.
2688 * The first way is via the primary EDT tree. It starts
2689 * with a list of busses, then a list of targets on a bus,
2690 * then devices/luns on a target, and then peripherals on a
2691 * device/lun. The "other" way is by the peripheral driver
2692 * lists. The peripheral driver lists are organized by
2693 * peripheral driver. (obviously) So it makes sense to
2694 * use the peripheral driver list if the user is looking
2695 * for something like "da1", or all "da" devices. If the
2696 * user is looking for something on a particular bus/target
2697 * or lun, it's generally better to go through the EDT tree.
2700 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2701 position_type = cdm->pos.position_type;
2705 position_type = CAM_DEV_POS_NONE;
2707 for (i = 0; i < cdm->num_patterns; i++) {
2708 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2709 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2710 position_type = CAM_DEV_POS_EDT;
2715 if (cdm->num_patterns == 0)
2716 position_type = CAM_DEV_POS_EDT;
2717 else if (position_type == CAM_DEV_POS_NONE)
2718 position_type = CAM_DEV_POS_PDRV;
2721 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2722 case CAM_DEV_POS_EDT:
2725 case CAM_DEV_POS_PDRV:
2726 xptperiphlistmatch(cdm);
2729 cdm->status = CAM_DEV_MATCH_ERROR;
2733 if (cdm->status == CAM_DEV_MATCH_ERROR)
2734 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2736 start_ccb->ccb_h.status = CAM_REQ_CMP;
2742 struct ccb_setasync *csa;
2743 struct async_node *cur_entry;
2744 struct async_list *async_head;
2747 csa = &start_ccb->csa;
2748 added = csa->event_enable;
2749 async_head = &path->device->asyncs;
2752 * If there is already an entry for us, simply
2755 cur_entry = SLIST_FIRST(async_head);
2756 while (cur_entry != NULL) {
2757 if ((cur_entry->callback_arg == csa->callback_arg)
2758 && (cur_entry->callback == csa->callback))
2760 cur_entry = SLIST_NEXT(cur_entry, links);
2763 if (cur_entry != NULL) {
2765 * If the request has no flags set,
2768 added &= ~cur_entry->event_enable;
2769 if (csa->event_enable == 0) {
2770 SLIST_REMOVE(async_head, cur_entry,
2772 xpt_release_device(path->device);
2773 free(cur_entry, M_CAMXPT);
2775 cur_entry->event_enable = csa->event_enable;
2777 csa->event_enable = added;
2779 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2781 if (cur_entry == NULL) {
2782 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2785 cur_entry->event_enable = csa->event_enable;
2786 cur_entry->callback_arg = csa->callback_arg;
2787 cur_entry->callback = csa->callback;
2788 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2789 xpt_acquire_device(path->device);
2791 start_ccb->ccb_h.status = CAM_REQ_CMP;
2796 struct ccb_relsim *crs;
2799 crs = &start_ccb->crs;
2803 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2807 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2809 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2810 /* Don't ever go below one opening */
2811 if (crs->openings > 0) {
2812 xpt_dev_ccbq_resize(path,
2817 "tagged openings now %d\n",
2824 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2826 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2829 * Just extend the old timeout and decrement
2830 * the freeze count so that a single timeout
2831 * is sufficient for releasing the queue.
2833 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2834 callout_stop(&dev->callout);
2837 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2840 callout_reset(&dev->callout,
2841 (crs->release_timeout * hz) / 1000,
2842 xpt_release_devq_timeout, dev);
2844 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2848 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2850 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2852 * Decrement the freeze count so that a single
2853 * completion is still sufficient to unfreeze
2856 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2859 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2860 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2864 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2866 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2867 || (dev->ccbq.dev_active == 0)) {
2869 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2872 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2873 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2877 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2878 xpt_release_devq_rl(path, /*runlevel*/
2879 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2880 crs->release_timeout : 0,
2881 /*count*/1, /*run_queue*/TRUE);
2883 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2884 start_ccb->ccb_h.status = CAM_REQ_CMP;
2889 #ifdef CAM_DEBUG_DELAY
2890 cam_debug_delay = CAM_DEBUG_DELAY;
2892 cam_dflags = start_ccb->cdbg.flags;
2893 if (cam_dpath != NULL) {
2894 xpt_free_path(cam_dpath);
2898 if (cam_dflags != CAM_DEBUG_NONE) {
2899 if (xpt_create_path(&cam_dpath, xpt_periph,
2900 start_ccb->ccb_h.path_id,
2901 start_ccb->ccb_h.target_id,
2902 start_ccb->ccb_h.target_lun) !=
2904 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2905 cam_dflags = CAM_DEBUG_NONE;
2907 start_ccb->ccb_h.status = CAM_REQ_CMP;
2908 xpt_print(cam_dpath, "debugging flags now %x\n",
2913 start_ccb->ccb_h.status = CAM_REQ_CMP;
2915 #else /* !CAMDEBUG */
2916 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2917 #endif /* CAMDEBUG */
2920 case XPT_FREEZE_QUEUE:
2922 struct ccb_relsim *crs = &start_ccb->crs;
2924 xpt_freeze_devq_rl(path, /*runlevel*/
2925 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2926 crs->release_timeout : 0, /*count*/1);
2927 start_ccb->ccb_h.status = CAM_REQ_CMP;
2931 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2932 xpt_freeze_devq(path, 1);
2933 start_ccb->ccb_h.status = CAM_REQ_CMP;
2940 printf("%s: CCB type %#x not supported\n", __func__,
2941 start_ccb->ccb_h.func_code);
2942 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2943 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2944 xpt_done(start_ccb);
2951 xpt_polled_action(union ccb *start_ccb)
2954 struct cam_sim *sim;
2955 struct cam_devq *devq;
2959 timeout = start_ccb->ccb_h.timeout * 10;
2960 sim = start_ccb->ccb_h.path->bus->sim;
2962 dev = start_ccb->ccb_h.path->device;
2964 mtx_assert(sim->mtx, MA_OWNED);
2966 /* Don't use ISR for this SIM while polling. */
2967 sim->flags |= CAM_SIM_POLLED;
2970 * Steal an opening so that no other queued requests
2971 * can get it before us while we simulate interrupts.
2973 dev->ccbq.devq_openings--;
2974 dev->ccbq.dev_openings--;
2976 while(((devq != NULL && devq->send_openings <= 0) ||
2977 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
2979 (*(sim->sim_poll))(sim);
2980 camisr_runqueue(&sim->sim_doneq);
2983 dev->ccbq.devq_openings++;
2984 dev->ccbq.dev_openings++;
2987 xpt_action(start_ccb);
2988 while(--timeout > 0) {
2989 (*(sim->sim_poll))(sim);
2990 camisr_runqueue(&sim->sim_doneq);
2991 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
2998 * XXX Is it worth adding a sim_timeout entry
2999 * point so we can attempt recovery? If
3000 * this is only used for dumps, I don't think
3003 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3006 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3009 /* We will use CAM ISR for this SIM again. */
3010 sim->flags &= ~CAM_SIM_POLLED;
3014 * Schedule a peripheral driver to receive a ccb when it's
3015 * target device has space for more transactions.
3018 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3020 struct cam_ed *device;
3023 mtx_assert(perph->sim->mtx, MA_OWNED);
3025 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3026 device = perph->path->device;
3027 if (periph_is_queued(perph)) {
3028 /* Simply reorder based on new priority */
3029 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3030 (" change priority to %d\n", new_priority));
3031 if (new_priority < perph->pinfo.priority) {
3032 camq_change_priority(&device->drvq,
3035 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3038 /* New entry on the queue */
3039 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3040 (" added periph to queue\n"));
3041 perph->pinfo.priority = new_priority;
3042 perph->pinfo.generation = ++device->drvq.generation;
3043 camq_insert(&device->drvq, &perph->pinfo);
3044 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3047 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3048 (" calling xpt_run_devq\n"));
3049 xpt_run_dev_allocq(perph->path->bus);
3055 * Schedule a device to run on a given queue.
3056 * If the device was inserted as a new entry on the queue,
3057 * return 1 meaning the device queue should be run. If we
3058 * were already queued, implying someone else has already
3059 * started the queue, return 0 so the caller doesn't attempt
3063 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3064 u_int32_t new_priority)
3067 u_int32_t old_priority;
3069 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3071 old_priority = pinfo->priority;
3074 * Are we already queued?
3076 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3077 /* Simply reorder based on new priority */
3078 if (new_priority < old_priority) {
3079 camq_change_priority(queue, pinfo->index,
3081 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3082 ("changed priority to %d\n",
3088 /* New entry on the queue */
3089 if (new_priority < old_priority)
3090 pinfo->priority = new_priority;
3092 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3093 ("Inserting onto queue\n"));
3094 pinfo->generation = ++queue->generation;
3095 camq_insert(queue, pinfo);
3102 xpt_run_dev_allocq(struct cam_eb *bus)
3104 struct cam_devq *devq;
3106 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3107 devq = bus->sim->devq;
3109 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3110 (" qfrozen_cnt == 0x%x, entries == %d, "
3111 "openings == %d, active == %d\n",
3112 devq->alloc_queue.qfrozen_cnt[0],
3113 devq->alloc_queue.entries,
3114 devq->alloc_openings,
3115 devq->alloc_active));
3117 devq->alloc_queue.qfrozen_cnt[0]++;
3118 while ((devq->alloc_queue.entries > 0)
3119 && (devq->alloc_openings > 0)
3120 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3121 struct cam_ed_qinfo *qinfo;
3122 struct cam_ed *device;
3123 union ccb *work_ccb;
3124 struct cam_periph *drv;
3127 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3129 device = qinfo->device;
3130 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3131 ("running device %p\n", device));
3133 drvq = &device->drvq;
3136 if (drvq->entries <= 0) {
3137 panic("xpt_run_dev_allocq: "
3138 "Device on queue without any work to do");
3141 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3142 devq->alloc_openings--;
3143 devq->alloc_active++;
3144 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3145 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3146 drv->pinfo.priority);
3147 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3148 ("calling periph start\n"));
3149 drv->periph_start(drv, work_ccb);
3152 * Malloc failure in alloc_ccb
3155 * XXX add us to a list to be run from free_ccb
3156 * if we don't have any ccbs active on this
3157 * device queue otherwise we may never get run
3163 /* We may have more work. Attempt to reschedule. */
3164 xpt_schedule_dev_allocq(bus, device);
3166 devq->alloc_queue.qfrozen_cnt[0]--;
3170 xpt_run_dev_sendq(struct cam_eb *bus)
3172 struct cam_devq *devq;
3174 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3176 devq = bus->sim->devq;
3178 devq->send_queue.qfrozen_cnt[0]++;
3179 while ((devq->send_queue.entries > 0)
3180 && (devq->send_openings > 0)
3181 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3182 struct cam_ed_qinfo *qinfo;
3183 struct cam_ed *device;
3184 union ccb *work_ccb;
3185 struct cam_sim *sim;
3187 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3189 device = qinfo->device;
3190 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3191 ("running device %p\n", device));
3193 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3194 if (work_ccb == NULL) {
3195 printf("device on run queue with no ccbs???\n");
3199 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3201 mtx_lock(&xsoftc.xpt_lock);
3202 if (xsoftc.num_highpower <= 0) {
3204 * We got a high power command, but we
3205 * don't have any available slots. Freeze
3206 * the device queue until we have a slot
3209 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3210 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3214 mtx_unlock(&xsoftc.xpt_lock);
3218 * Consume a high power slot while
3221 xsoftc.num_highpower--;
3223 mtx_unlock(&xsoftc.xpt_lock);
3225 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3226 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3228 devq->send_openings--;
3229 devq->send_active++;
3231 xpt_schedule_dev_sendq(bus, device);
3233 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3235 * The client wants to freeze the queue
3236 * after this CCB is sent.
3238 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3241 /* In Target mode, the peripheral driver knows best... */
3242 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3243 if ((device->inq_flags & SID_CmdQue) != 0
3244 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3245 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3248 * Clear this in case of a retried CCB that
3249 * failed due to a rejected tag.
3251 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3255 * Device queues can be shared among multiple sim instances
3256 * that reside on different busses. Use the SIM in the queue
3257 * CCB's path, rather than the one in the bus that was passed
3258 * into this function.
3260 sim = work_ccb->ccb_h.path->bus->sim;
3261 (*(sim->sim_action))(sim, work_ccb);
3263 devq->send_queue.qfrozen_cnt[0]--;
3267 * This function merges stuff from the slave ccb into the master ccb, while
3268 * keeping important fields in the master ccb constant.
3271 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3275 * Pull fields that are valid for peripheral drivers to set
3276 * into the master CCB along with the CCB "payload".
3278 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3279 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3280 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3281 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3282 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3283 sizeof(union ccb) - sizeof(struct ccb_hdr));
3287 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3290 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3291 ccb_h->pinfo.priority = priority;
3293 ccb_h->path_id = path->bus->path_id;
3295 ccb_h->target_id = path->target->target_id;
3297 ccb_h->target_id = CAM_TARGET_WILDCARD;
3299 ccb_h->target_lun = path->device->lun_id;
3300 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3302 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3304 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3308 /* Path manipulation functions */
3310 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3311 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3313 struct cam_path *path;
3316 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3319 status = CAM_RESRC_UNAVAIL;
3322 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3323 if (status != CAM_REQ_CMP) {
3324 free(path, M_CAMXPT);
3327 *new_path_ptr = path;
3332 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3333 struct cam_periph *periph, path_id_t path_id,
3334 target_id_t target_id, lun_id_t lun_id)
3336 struct cam_path *path;
3337 struct cam_eb *bus = NULL;
3339 int need_unlock = 0;
3341 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3343 if (path_id != CAM_BUS_WILDCARD) {
3344 bus = xpt_find_bus(path_id);
3347 CAM_SIM_LOCK(bus->sim);
3350 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3352 CAM_SIM_UNLOCK(bus->sim);
3353 xpt_release_bus(bus);
3355 if (status != CAM_REQ_CMP) {
3356 free(path, M_CAMXPT);
3359 *new_path_ptr = path;
3364 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3365 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3368 struct cam_et *target;
3369 struct cam_ed *device;
3372 status = CAM_REQ_CMP; /* Completed without error */
3373 target = NULL; /* Wildcarded */
3374 device = NULL; /* Wildcarded */
3377 * We will potentially modify the EDT, so block interrupts
3378 * that may attempt to create cam paths.
3380 bus = xpt_find_bus(path_id);
3382 status = CAM_PATH_INVALID;
3384 target = xpt_find_target(bus, target_id);
3385 if (target == NULL) {
3387 struct cam_et *new_target;
3389 new_target = xpt_alloc_target(bus, target_id);
3390 if (new_target == NULL) {
3391 status = CAM_RESRC_UNAVAIL;
3393 target = new_target;
3396 if (target != NULL) {
3397 device = xpt_find_device(target, lun_id);
3398 if (device == NULL) {
3400 struct cam_ed *new_device;
3403 (*(bus->xport->alloc_device))(bus,
3406 if (new_device == NULL) {
3407 status = CAM_RESRC_UNAVAIL;
3409 device = new_device;
3416 * Only touch the user's data if we are successful.
3418 if (status == CAM_REQ_CMP) {
3419 new_path->periph = perph;
3420 new_path->bus = bus;
3421 new_path->target = target;
3422 new_path->device = device;
3423 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3426 xpt_release_device(device);
3428 xpt_release_target(target);
3430 xpt_release_bus(bus);
3436 xpt_release_path(struct cam_path *path)
3438 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3439 if (path->device != NULL) {
3440 xpt_release_device(path->device);
3441 path->device = NULL;
3443 if (path->target != NULL) {
3444 xpt_release_target(path->target);
3445 path->target = NULL;
3447 if (path->bus != NULL) {
3448 xpt_release_bus(path->bus);
3454 xpt_free_path(struct cam_path *path)
3457 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3458 xpt_release_path(path);
3459 free(path, M_CAMXPT);
3463 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3464 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3467 mtx_lock(&xsoftc.xpt_topo_lock);
3470 *bus_ref = path->bus->refcount;
3474 mtx_unlock(&xsoftc.xpt_topo_lock);
3477 *periph_ref = path->periph->refcount;
3483 *target_ref = path->target->refcount;
3489 *device_ref = path->device->refcount;
3496 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3497 * in path1, 2 for match with wildcards in path2.
3500 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3504 if (path1->bus != path2->bus) {
3505 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3507 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3512 if (path1->target != path2->target) {
3513 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3516 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3521 if (path1->device != path2->device) {
3522 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3525 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3534 xpt_print_path(struct cam_path *path)
3538 printf("(nopath): ");
3540 if (path->periph != NULL)
3541 printf("(%s%d:", path->periph->periph_name,
3542 path->periph->unit_number);
3544 printf("(noperiph:");
3546 if (path->bus != NULL)
3547 printf("%s%d:%d:", path->bus->sim->sim_name,
3548 path->bus->sim->unit_number,
3549 path->bus->sim->bus_id);
3553 if (path->target != NULL)
3554 printf("%d:", path->target->target_id);
3558 if (path->device != NULL)
3559 printf("%d): ", path->device->lun_id);
3566 xpt_print(struct cam_path *path, const char *fmt, ...)
3569 xpt_print_path(path);
3576 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3581 if (path != NULL && path->bus != NULL)
3582 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3585 sbuf_new(&sb, str, str_len, 0);
3588 sbuf_printf(&sb, "(nopath): ");
3590 if (path->periph != NULL)
3591 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3592 path->periph->unit_number);
3594 sbuf_printf(&sb, "(noperiph:");
3596 if (path->bus != NULL)
3597 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3598 path->bus->sim->unit_number,
3599 path->bus->sim->bus_id);
3601 sbuf_printf(&sb, "nobus:");
3603 if (path->target != NULL)
3604 sbuf_printf(&sb, "%d:", path->target->target_id);
3606 sbuf_printf(&sb, "X:");
3608 if (path->device != NULL)
3609 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3611 sbuf_printf(&sb, "X): ");
3615 return(sbuf_len(&sb));
3619 xpt_path_path_id(struct cam_path *path)
3621 return(path->bus->path_id);
3625 xpt_path_target_id(struct cam_path *path)
3627 if (path->target != NULL)
3628 return (path->target->target_id);
3630 return (CAM_TARGET_WILDCARD);
3634 xpt_path_lun_id(struct cam_path *path)
3636 if (path->device != NULL)
3637 return (path->device->lun_id);
3639 return (CAM_LUN_WILDCARD);
3643 xpt_path_sim(struct cam_path *path)
3646 return (path->bus->sim);
3650 xpt_path_periph(struct cam_path *path)
3652 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3654 return (path->periph);
3658 xpt_path_legacy_ata_id(struct cam_path *path)
3663 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3664 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3665 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3666 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3669 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3670 path->bus->sim->unit_number < 2) {
3671 bus_id = path->bus->sim->unit_number;
3675 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3676 if (bus == path->bus)
3678 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3679 bus->sim->unit_number >= 2) ||
3680 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3681 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3682 strcmp(bus->sim->sim_name, "siisch") == 0)
3687 if (path->target != NULL) {
3688 if (path->target->target_id < 2)
3689 return (bus_id * 2 + path->target->target_id);
3693 return (bus_id * 2);
3697 * Release a CAM control block for the caller. Remit the cost of the structure
3698 * to the device referenced by the path. If the this device had no 'credits'
3699 * and peripheral drivers have registered async callbacks for this notification
3703 xpt_release_ccb(union ccb *free_ccb)
3705 struct cam_path *path;
3706 struct cam_ed *device;
3708 struct cam_sim *sim;
3710 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3711 path = free_ccb->ccb_h.path;
3712 device = path->device;
3716 mtx_assert(sim->mtx, MA_OWNED);
3718 cam_ccbq_release_opening(&device->ccbq);
3719 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3720 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3721 cam_ccbq_resize(&device->ccbq,
3722 device->ccbq.dev_openings + device->ccbq.dev_active);
3724 if (sim->ccb_count > sim->max_ccbs) {
3725 xpt_free_ccb(free_ccb);
3728 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3731 if (sim->devq == NULL) {
3734 sim->devq->alloc_openings++;
3735 sim->devq->alloc_active--;
3736 if (device_is_alloc_queued(device) == 0)
3737 xpt_schedule_dev_allocq(bus, device);
3738 xpt_run_dev_allocq(bus);
3741 /* Functions accessed by SIM drivers */
3743 static struct xpt_xport xport_default = {
3744 .alloc_device = xpt_alloc_device_default,
3745 .action = xpt_action_default,
3746 .async = xpt_dev_async_default,
3750 * A sim structure, listing the SIM entry points and instance
3751 * identification info is passed to xpt_bus_register to hook the SIM
3752 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3753 * for this new bus and places it in the array of busses and assigns
3754 * it a path_id. The path_id may be influenced by "hard wiring"
3755 * information specified by the user. Once interrupt services are
3756 * available, the bus will be probed.
3759 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3761 struct cam_eb *new_bus;
3762 struct cam_eb *old_bus;
3763 struct ccb_pathinq cpi;
3764 struct cam_path *path;
3767 mtx_assert(sim->mtx, MA_OWNED);
3770 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3771 M_CAMXPT, M_NOWAIT);
3772 if (new_bus == NULL) {
3773 /* Couldn't satisfy request */
3774 return (CAM_RESRC_UNAVAIL);
3776 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3778 free(new_bus, M_CAMXPT);
3779 return (CAM_RESRC_UNAVAIL);
3782 if (strcmp(sim->sim_name, "xpt") != 0) {
3784 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3787 TAILQ_INIT(&new_bus->et_entries);
3788 new_bus->path_id = sim->path_id;
3791 timevalclear(&new_bus->last_reset);
3793 new_bus->refcount = 1; /* Held until a bus_deregister event */
3794 new_bus->generation = 0;
3796 mtx_lock(&xsoftc.xpt_topo_lock);
3797 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3798 while (old_bus != NULL
3799 && old_bus->path_id < new_bus->path_id)
3800 old_bus = TAILQ_NEXT(old_bus, links);
3801 if (old_bus != NULL)
3802 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3804 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3805 xsoftc.bus_generation++;
3806 mtx_unlock(&xsoftc.xpt_topo_lock);
3809 * Set a default transport so that a PATH_INQ can be issued to
3810 * the SIM. This will then allow for probing and attaching of
3811 * a more appropriate transport.
3813 new_bus->xport = &xport_default;
3815 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3816 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3817 if (status != CAM_REQ_CMP)
3818 printf("xpt_compile_path returned %d\n", status);
3820 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3821 cpi.ccb_h.func_code = XPT_PATH_INQ;
3822 xpt_action((union ccb *)&cpi);
3824 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3825 switch (cpi.transport) {
3832 new_bus->xport = scsi_get_xport();
3836 new_bus->xport = ata_get_xport();
3839 new_bus->xport = &xport_default;
3844 /* Notify interested parties */
3845 if (sim->path_id != CAM_XPT_PATH_ID) {
3846 union ccb *scan_ccb;
3848 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3849 /* Initiate bus rescan. */
3850 scan_ccb = xpt_alloc_ccb_nowait();
3851 scan_ccb->ccb_h.path = path;
3852 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3853 scan_ccb->crcn.flags = 0;
3854 xpt_rescan(scan_ccb);
3856 xpt_free_path(path);
3857 return (CAM_SUCCESS);
3861 xpt_bus_deregister(path_id_t pathid)
3863 struct cam_path bus_path;
3866 status = xpt_compile_path(&bus_path, NULL, pathid,
3867 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3868 if (status != CAM_REQ_CMP)
3871 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3872 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3874 /* Release the reference count held while registered. */
3875 xpt_release_bus(bus_path.bus);
3876 xpt_release_path(&bus_path);
3878 return (CAM_REQ_CMP);
3882 xptnextfreepathid(void)
3889 mtx_lock(&xsoftc.xpt_topo_lock);
3890 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3892 /* Find an unoccupied pathid */
3893 while (bus != NULL && bus->path_id <= pathid) {
3894 if (bus->path_id == pathid)
3896 bus = TAILQ_NEXT(bus, links);
3898 mtx_unlock(&xsoftc.xpt_topo_lock);
3901 * Ensure that this pathid is not reserved for
3902 * a bus that may be registered in the future.
3904 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3906 /* Start the search over */
3907 mtx_lock(&xsoftc.xpt_topo_lock);
3914 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3921 pathid = CAM_XPT_PATH_ID;
3922 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3924 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3925 if (strcmp(dname, "scbus")) {
3926 /* Avoid a bit of foot shooting. */
3929 if (dunit < 0) /* unwired?! */
3931 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3932 if (sim_bus == val) {
3936 } else if (sim_bus == 0) {
3937 /* Unspecified matches bus 0 */
3941 printf("Ambiguous scbus configuration for %s%d "
3942 "bus %d, cannot wire down. The kernel "
3943 "config entry for scbus%d should "
3944 "specify a controller bus.\n"
3945 "Scbus will be assigned dynamically.\n",
3946 sim_name, sim_unit, sim_bus, dunit);
3951 if (pathid == CAM_XPT_PATH_ID)
3952 pathid = xptnextfreepathid();
3957 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
3960 struct cam_et *target, *next_target;
3961 struct cam_ed *device, *next_device;
3963 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3965 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
3968 * Most async events come from a CAM interrupt context. In
3969 * a few cases, the error recovery code at the peripheral layer,
3970 * which may run from our SWI or a process context, may signal
3971 * deferred events with a call to xpt_async.
3976 if (async_code == AC_BUS_RESET) {
3977 /* Update our notion of when the last reset occurred */
3978 microtime(&bus->last_reset);
3981 for (target = TAILQ_FIRST(&bus->et_entries);
3983 target = next_target) {
3985 next_target = TAILQ_NEXT(target, links);
3987 if (path->target != target
3988 && path->target->target_id != CAM_TARGET_WILDCARD
3989 && target->target_id != CAM_TARGET_WILDCARD)
3992 if (async_code == AC_SENT_BDR) {
3993 /* Update our notion of when the last reset occurred */
3994 microtime(&path->target->last_reset);
3997 for (device = TAILQ_FIRST(&target->ed_entries);
3999 device = next_device) {
4001 next_device = TAILQ_NEXT(device, links);
4003 if (path->device != device
4004 && path->device->lun_id != CAM_LUN_WILDCARD
4005 && device->lun_id != CAM_LUN_WILDCARD)
4008 * The async callback could free the device.
4009 * If it is a broadcast async, it doesn't hold
4010 * device reference, so take our own reference.
4012 xpt_acquire_device(device);
4013 (*(bus->xport->async))(async_code, bus,
4017 xpt_async_bcast(&device->asyncs, async_code,
4019 xpt_release_device(device);
4024 * If this wasn't a fully wildcarded async, tell all
4025 * clients that want all async events.
4027 if (bus != xpt_periph->path->bus)
4028 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4033 xpt_async_bcast(struct async_list *async_head,
4034 u_int32_t async_code,
4035 struct cam_path *path, void *async_arg)
4037 struct async_node *cur_entry;
4039 cur_entry = SLIST_FIRST(async_head);
4040 while (cur_entry != NULL) {
4041 struct async_node *next_entry;
4043 * Grab the next list entry before we call the current
4044 * entry's callback. This is because the callback function
4045 * can delete its async callback entry.
4047 next_entry = SLIST_NEXT(cur_entry, links);
4048 if ((cur_entry->event_enable & async_code) != 0)
4049 cur_entry->callback(cur_entry->callback_arg,
4052 cur_entry = next_entry;
4057 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4058 struct cam_et *target, struct cam_ed *device,
4061 printf("%s called\n", __func__);
4065 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4067 struct cam_ed *dev = path->device;
4069 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4070 dev->sim->devq->alloc_openings +=
4071 cam_ccbq_freeze(&dev->ccbq, rl, count);
4072 /* Remove frozen device from allocq. */
4073 if (device_is_alloc_queued(dev) &&
4074 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4075 CAMQ_GET_PRIO(&dev->drvq)))) {
4076 camq_remove(&dev->sim->devq->alloc_queue,
4077 dev->alloc_ccb_entry.pinfo.index);
4079 /* Remove frozen device from sendq. */
4080 if (device_is_send_queued(dev) &&
4081 cam_ccbq_frozen_top(&dev->ccbq)) {
4082 camq_remove(&dev->sim->devq->send_queue,
4083 dev->send_ccb_entry.pinfo.index);
4085 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4089 xpt_freeze_devq(struct cam_path *path, u_int count)
4092 return (xpt_freeze_devq_rl(path, 0, count));
4096 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4099 mtx_assert(sim->mtx, MA_OWNED);
4100 sim->devq->send_queue.qfrozen_cnt[0] += count;
4101 return (sim->devq->send_queue.qfrozen_cnt[0]);
4105 xpt_release_devq_timeout(void *arg)
4107 struct cam_ed *device;
4109 device = (struct cam_ed *)arg;
4111 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4115 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4117 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4119 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4123 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4125 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4127 xpt_release_devq_device(path->device, rl, count, run_queue);
4131 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4134 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4136 printf("xpt_release_devq(%d): requested %u > present %u\n",
4137 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4139 count = dev->ccbq.queue.qfrozen_cnt[rl];
4141 dev->sim->devq->alloc_openings -=
4142 cam_ccbq_release(&dev->ccbq, rl, count);
4143 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4144 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4145 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4146 xpt_run_dev_allocq(dev->target->bus);
4148 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4150 * No longer need to wait for a successful
4151 * command completion.
4153 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4155 * Remove any timeouts that might be scheduled
4156 * to release this queue.
4158 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4159 callout_stop(&dev->callout);
4160 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4165 * Now that we are unfrozen schedule the
4166 * device so any pending transactions are
4169 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4170 xpt_run_dev_sendq(dev->target->bus);
4175 xpt_release_simq(struct cam_sim *sim, int run_queue)
4179 mtx_assert(sim->mtx, MA_OWNED);
4180 sendq = &(sim->devq->send_queue);
4181 if (sendq->qfrozen_cnt[0] <= 0) {
4183 printf("xpt_release_simq: requested 1 > present %u\n",
4184 sendq->qfrozen_cnt[0]);
4187 sendq->qfrozen_cnt[0]--;
4188 if (sendq->qfrozen_cnt[0] == 0) {
4190 * If there is a timeout scheduled to release this
4191 * sim queue, remove it. The queue frozen count is
4194 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4195 callout_stop(&sim->callout);
4196 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4202 * Now that we are unfrozen run the send queue.
4204 bus = xpt_find_bus(sim->path_id);
4205 xpt_run_dev_sendq(bus);
4206 xpt_release_bus(bus);
4212 * XXX Appears to be unused.
4215 xpt_release_simq_timeout(void *arg)
4217 struct cam_sim *sim;
4219 sim = (struct cam_sim *)arg;
4220 xpt_release_simq(sim, /* run_queue */ TRUE);
4224 xpt_done(union ccb *done_ccb)
4226 struct cam_sim *sim;
4229 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4230 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4232 * Queue up the request for handling by our SWI handler
4233 * any of the "non-immediate" type of ccbs.
4235 sim = done_ccb->ccb_h.path->bus->sim;
4236 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4238 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4239 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED)) == 0) {
4240 mtx_lock(&cam_simq_lock);
4241 first = TAILQ_EMPTY(&cam_simq);
4242 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4243 mtx_unlock(&cam_simq_lock);
4244 sim->flags |= CAM_SIM_ON_DONEQ;
4246 swi_sched(cambio_ih, 0);
4256 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4261 xpt_alloc_ccb_nowait()
4265 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4270 xpt_free_ccb(union ccb *free_ccb)
4272 free(free_ccb, M_CAMXPT);
4277 /* Private XPT functions */
4280 * Get a CAM control block for the caller. Charge the structure to the device
4281 * referenced by the path. If the this device has no 'credits' then the
4282 * device already has the maximum number of outstanding operations under way
4283 * and we return NULL. If we don't have sufficient resources to allocate more
4284 * ccbs, we also return NULL.
4287 xpt_get_ccb(struct cam_ed *device)
4290 struct cam_sim *sim;
4293 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4294 new_ccb = xpt_alloc_ccb_nowait();
4295 if (new_ccb == NULL) {
4298 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4299 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4300 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4304 cam_ccbq_take_opening(&device->ccbq);
4305 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4310 xpt_release_bus(struct cam_eb *bus)
4313 mtx_lock(&xsoftc.xpt_topo_lock);
4314 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4315 if ((--bus->refcount == 0)
4316 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4317 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4318 xsoftc.bus_generation++;
4319 mtx_unlock(&xsoftc.xpt_topo_lock);
4320 cam_sim_release(bus->sim);
4321 free(bus, M_CAMXPT);
4323 mtx_unlock(&xsoftc.xpt_topo_lock);
4326 static struct cam_et *
4327 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4329 struct cam_et *target;
4331 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4333 if (target != NULL) {
4334 struct cam_et *cur_target;
4336 TAILQ_INIT(&target->ed_entries);
4338 target->target_id = target_id;
4339 target->refcount = 1;
4340 target->generation = 0;
4341 target->luns = NULL;
4342 timevalclear(&target->last_reset);
4344 * Hold a reference to our parent bus so it
4345 * will not go away before we do.
4347 mtx_lock(&xsoftc.xpt_topo_lock);
4349 mtx_unlock(&xsoftc.xpt_topo_lock);
4351 /* Insertion sort into our bus's target list */
4352 cur_target = TAILQ_FIRST(&bus->et_entries);
4353 while (cur_target != NULL && cur_target->target_id < target_id)
4354 cur_target = TAILQ_NEXT(cur_target, links);
4356 if (cur_target != NULL) {
4357 TAILQ_INSERT_BEFORE(cur_target, target, links);
4359 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4367 xpt_release_target(struct cam_et *target)
4370 if (target->refcount == 1) {
4371 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4372 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4373 target->bus->generation++;
4374 xpt_release_bus(target->bus);
4376 free(target->luns, M_CAMXPT);
4377 free(target, M_CAMXPT);
4383 static struct cam_ed *
4384 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4387 struct cam_ed *device, *cur_device;
4389 device = xpt_alloc_device(bus, target, lun_id);
4393 device->mintags = 1;
4394 device->maxtags = 1;
4395 bus->sim->max_ccbs += device->ccbq.devq_openings;
4396 cur_device = TAILQ_FIRST(&target->ed_entries);
4397 while (cur_device != NULL && cur_device->lun_id < lun_id)
4398 cur_device = TAILQ_NEXT(cur_device, links);
4399 if (cur_device != NULL) {
4400 TAILQ_INSERT_BEFORE(cur_device, device, links);
4402 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4404 target->generation++;
4410 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4412 struct cam_ed *device;
4413 struct cam_devq *devq;
4416 /* Make space for us in the device queue on our bus */
4417 devq = bus->sim->devq;
4418 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4420 if (status != CAM_REQ_CMP) {
4423 device = (struct cam_ed *)malloc(sizeof(*device),
4424 M_CAMXPT, M_NOWAIT|M_ZERO);
4427 if (device != NULL) {
4428 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4429 device->alloc_ccb_entry.device = device;
4430 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4431 device->send_ccb_entry.device = device;
4432 device->target = target;
4433 device->lun_id = lun_id;
4434 device->sim = bus->sim;
4435 /* Initialize our queues */
4436 if (camq_init(&device->drvq, 0) != 0) {
4437 free(device, M_CAMXPT);
4440 if (cam_ccbq_init(&device->ccbq,
4441 bus->sim->max_dev_openings) != 0) {
4442 camq_fini(&device->drvq);
4443 free(device, M_CAMXPT);
4446 SLIST_INIT(&device->asyncs);
4447 SLIST_INIT(&device->periphs);
4448 device->generation = 0;
4449 device->owner = NULL;
4450 device->flags = CAM_DEV_UNCONFIGURED;
4451 device->tag_delay_count = 0;
4452 device->tag_saved_openings = 0;
4453 device->refcount = 1;
4454 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4457 * Hold a reference to our parent target so it
4458 * will not go away before we do.
4467 xpt_acquire_device(struct cam_ed *device)
4474 xpt_release_device(struct cam_ed *device)
4477 if (device->refcount == 1) {
4478 struct cam_devq *devq;
4480 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4481 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4482 panic("Removing device while still queued for ccbs");
4484 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4485 callout_stop(&device->callout);
4487 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4488 device->target->generation++;
4489 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4490 /* Release our slot in the devq */
4491 devq = device->target->bus->sim->devq;
4492 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4493 camq_fini(&device->drvq);
4494 cam_ccbq_fini(&device->ccbq);
4495 xpt_release_target(device->target);
4496 free(device, M_CAMXPT);
4502 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4510 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4511 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4512 if (result == CAM_REQ_CMP && (diff < 0)) {
4513 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4515 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4516 || (dev->inq_flags & SID_CmdQue) != 0)
4517 dev->tag_saved_openings = newopenings;
4518 /* Adjust the global limit */
4519 dev->sim->max_ccbs += diff;
4523 static struct cam_eb *
4524 xpt_find_bus(path_id_t path_id)
4528 mtx_lock(&xsoftc.xpt_topo_lock);
4529 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4531 bus = TAILQ_NEXT(bus, links)) {
4532 if (bus->path_id == path_id) {
4537 mtx_unlock(&xsoftc.xpt_topo_lock);
4541 static struct cam_et *
4542 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4544 struct cam_et *target;
4546 for (target = TAILQ_FIRST(&bus->et_entries);
4548 target = TAILQ_NEXT(target, links)) {
4549 if (target->target_id == target_id) {
4557 static struct cam_ed *
4558 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4560 struct cam_ed *device;
4562 for (device = TAILQ_FIRST(&target->ed_entries);
4564 device = TAILQ_NEXT(device, links)) {
4565 if (device->lun_id == lun_id) {
4574 xpt_start_tags(struct cam_path *path)
4576 struct ccb_relsim crs;
4577 struct cam_ed *device;
4578 struct cam_sim *sim;
4581 device = path->device;
4582 sim = path->bus->sim;
4583 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4584 xpt_freeze_devq(path, /*count*/1);
4585 device->inq_flags |= SID_CmdQue;
4586 if (device->tag_saved_openings != 0)
4587 newopenings = device->tag_saved_openings;
4589 newopenings = min(device->maxtags,
4590 sim->max_tagged_dev_openings);
4591 xpt_dev_ccbq_resize(path, newopenings);
4592 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4593 crs.ccb_h.func_code = XPT_REL_SIMQ;
4594 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4596 = crs.release_timeout
4599 xpt_action((union ccb *)&crs);
4603 xpt_stop_tags(struct cam_path *path)
4605 struct ccb_relsim crs;
4606 struct cam_ed *device;
4607 struct cam_sim *sim;
4609 device = path->device;
4610 sim = path->bus->sim;
4611 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4612 device->tag_delay_count = 0;
4613 xpt_freeze_devq(path, /*count*/1);
4614 device->inq_flags &= ~SID_CmdQue;
4615 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4616 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4617 crs.ccb_h.func_code = XPT_REL_SIMQ;
4618 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4620 = crs.release_timeout
4623 xpt_action((union ccb *)&crs);
4627 xpt_boot_delay(void *arg)
4634 xpt_config(void *arg)
4637 * Now that interrupts are enabled, go find our devices
4641 /* Setup debugging flags and path */
4642 #ifdef CAM_DEBUG_BUS
4643 if (cam_dflags != CAM_DEBUG_NONE) {
4645 * Locking is specifically omitted here. No SIMs have
4646 * registered yet, so xpt_create_path will only be searching
4647 * empty lists of targets and devices.
4649 if (xpt_create_path(&cam_dpath, xpt_periph,
4650 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4651 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4652 printf("xpt_config: xpt_create_path() failed for debug"
4653 " target %d:%d:%d, debugging disabled\n",
4654 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4655 cam_dflags = CAM_DEBUG_NONE;
4659 #else /* !CAM_DEBUG_BUS */
4661 #endif /* CAM_DEBUG_BUS */
4662 #endif /* CAMDEBUG */
4664 periphdriver_init(1);
4666 callout_init(&xsoftc.boot_callout, 1);
4667 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4668 xpt_boot_delay, NULL);
4669 /* Fire up rescan thread. */
4670 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4671 printf("xpt_config: failed to create rescan thread.\n");
4679 xsoftc.buses_to_config++;
4684 xpt_release_boot(void)
4687 xsoftc.buses_to_config--;
4688 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4689 struct xpt_task *task;
4691 xsoftc.buses_config_done = 1;
4693 /* Call manually because we don't have any busses */
4694 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4696 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4697 taskqueue_enqueue(taskqueue_thread, &task->task);
4704 * If the given device only has one peripheral attached to it, and if that
4705 * peripheral is the passthrough driver, announce it. This insures that the
4706 * user sees some sort of announcement for every peripheral in their system.
4709 xptpassannouncefunc(struct cam_ed *device, void *arg)
4711 struct cam_periph *periph;
4714 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4715 periph = SLIST_NEXT(periph, periph_links), i++);
4717 periph = SLIST_FIRST(&device->periphs);
4719 && (strncmp(periph->periph_name, "pass", 4) == 0))
4720 xpt_announce_periph(periph, NULL);
4726 xpt_finishconfig_task(void *context, int pending)
4729 periphdriver_init(2);
4731 * Check for devices with no "standard" peripheral driver
4732 * attached. For any devices like that, announce the
4733 * passthrough driver so the user will see something.
4735 xpt_for_all_devices(xptpassannouncefunc, NULL);
4737 /* Release our hook so that the boot can continue. */
4738 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4739 free(xsoftc.xpt_config_hook, M_CAMXPT);
4740 xsoftc.xpt_config_hook = NULL;
4742 free(context, M_CAMXPT);
4746 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4747 struct cam_path *path)
4749 struct ccb_setasync csa;
4754 mtx_lock(&xsoftc.xpt_lock);
4755 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4756 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4757 if (status != CAM_REQ_CMP) {
4758 mtx_unlock(&xsoftc.xpt_lock);
4764 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4765 csa.ccb_h.func_code = XPT_SASYNC_CB;
4766 csa.event_enable = event;
4767 csa.callback = cbfunc;
4768 csa.callback_arg = cbarg;
4769 xpt_action((union ccb *)&csa);
4770 status = csa.ccb_h.status;
4773 xpt_free_path(path);
4774 mtx_unlock(&xsoftc.xpt_lock);
4777 if ((status == CAM_REQ_CMP) &&
4778 (csa.event_enable & AC_FOUND_DEVICE)) {
4780 * Get this peripheral up to date with all
4781 * the currently existing devices.
4783 xpt_for_all_devices(xptsetasyncfunc, &csa);
4785 if ((status == CAM_REQ_CMP) &&
4786 (csa.event_enable & AC_PATH_REGISTERED)) {
4788 * Get this peripheral up to date with all
4789 * the currently existing busses.
4791 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4798 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4800 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4802 switch (work_ccb->ccb_h.func_code) {
4803 /* Common cases first */
4804 case XPT_PATH_INQ: /* Path routing inquiry */
4806 struct ccb_pathinq *cpi;
4808 cpi = &work_ccb->cpi;
4809 cpi->version_num = 1; /* XXX??? */
4810 cpi->hba_inquiry = 0;
4811 cpi->target_sprt = 0;
4813 cpi->hba_eng_cnt = 0;
4814 cpi->max_target = 0;
4816 cpi->initiator_id = 0;
4817 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4818 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4819 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4820 cpi->unit_number = sim->unit_number;
4821 cpi->bus_id = sim->bus_id;
4822 cpi->base_transfer_speed = 0;
4823 cpi->protocol = PROTO_UNSPECIFIED;
4824 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4825 cpi->transport = XPORT_UNSPECIFIED;
4826 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4827 cpi->ccb_h.status = CAM_REQ_CMP;
4832 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4839 * The xpt as a "controller" has no interrupt sources, so polling
4843 xptpoll(struct cam_sim *sim)
4848 xpt_lock_buses(void)
4850 mtx_lock(&xsoftc.xpt_topo_lock);
4854 xpt_unlock_buses(void)
4856 mtx_unlock(&xsoftc.xpt_topo_lock);
4863 struct cam_sim *sim;
4865 mtx_lock(&cam_simq_lock);
4867 while (!TAILQ_EMPTY(&cam_simq)) {
4868 TAILQ_CONCAT(&queue, &cam_simq, links);
4869 mtx_unlock(&cam_simq_lock);
4871 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4872 TAILQ_REMOVE(&queue, sim, links);
4874 sim->flags &= ~CAM_SIM_ON_DONEQ;
4875 camisr_runqueue(&sim->sim_doneq);
4876 CAM_SIM_UNLOCK(sim);
4878 mtx_lock(&cam_simq_lock);
4880 mtx_unlock(&cam_simq_lock);
4884 camisr_runqueue(void *V_queue)
4886 cam_isrq_t *queue = V_queue;
4887 struct ccb_hdr *ccb_h;
4889 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4892 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4893 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4895 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4900 if (ccb_h->flags & CAM_HIGH_POWER) {
4901 struct highpowerlist *hphead;
4902 union ccb *send_ccb;
4904 mtx_lock(&xsoftc.xpt_lock);
4905 hphead = &xsoftc.highpowerq;
4907 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4910 * Increment the count since this command is done.
4912 xsoftc.num_highpower++;
4915 * Any high powered commands queued up?
4917 if (send_ccb != NULL) {
4919 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4920 mtx_unlock(&xsoftc.xpt_lock);
4922 xpt_release_devq(send_ccb->ccb_h.path,
4923 /*count*/1, /*runqueue*/TRUE);
4925 mtx_unlock(&xsoftc.xpt_lock);
4928 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4931 dev = ccb_h->path->device;
4933 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
4934 ccb_h->path->bus->sim->devq->send_active--;
4935 ccb_h->path->bus->sim->devq->send_openings++;
4938 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4939 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
4940 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
4941 && (dev->ccbq.dev_active == 0))) {
4942 xpt_release_devq(ccb_h->path, /*count*/1,
4943 /*run_queue*/FALSE);
4946 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4947 && (--dev->tag_delay_count == 0))
4948 xpt_start_tags(ccb_h->path);
4949 if (!device_is_send_queued(dev)) {
4950 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
4955 if (ccb_h->status & CAM_RELEASE_SIMQ) {
4956 xpt_release_simq(ccb_h->path->bus->sim,
4958 ccb_h->status &= ~CAM_RELEASE_SIMQ;
4962 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
4963 && (ccb_h->status & CAM_DEV_QFRZN)) {
4964 xpt_release_devq(ccb_h->path, /*count*/1,
4966 ccb_h->status &= ~CAM_DEV_QFRZN;
4968 xpt_run_dev_sendq(ccb_h->path->bus);
4971 /* Call the peripheral driver's callback */
4972 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);