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/reboot.h>
43 #include <sys/interrupt.h>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
67 #include <machine/md_var.h> /* geometry translation */
68 #include <machine/stdarg.h> /* for xpt_print below */
73 * This is the maximum number of high powered commands (e.g. start unit)
74 * that can be outstanding at a particular time.
76 #ifndef CAM_MAX_HIGHPOWER
77 #define CAM_MAX_HIGHPOWER 4
80 /* Datastructures internal to the xpt layer */
81 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
83 /* Object for defering XPT actions to a taskqueue */
96 u_int32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
102 /* queue for handling async rescan requests. */
103 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
105 int buses_config_done;
107 /* Registered busses */
108 TAILQ_HEAD(,cam_eb) xpt_busses;
109 u_int bus_generation;
111 struct intr_config_hook *xpt_config_hook;
114 struct callout boot_callout;
116 struct mtx xpt_topo_lock;
122 DM_RET_FLAG_MASK = 0x0f,
125 DM_RET_DESCEND = 0x20,
127 DM_RET_ACTION_MASK = 0xf0
135 } xpt_traverse_depth;
137 struct xpt_traverse_config {
138 xpt_traverse_depth depth;
143 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
144 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
145 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
146 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
147 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
149 /* Transport layer configuration information */
150 static struct xpt_softc xsoftc;
152 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
153 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
154 &xsoftc.boot_delay, 0, "Bus registration wait time");
156 /* Queues for our software interrupt handler */
157 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
158 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
159 static cam_simq_t cam_simq;
160 static struct mtx cam_simq_lock;
162 /* Pointers to software interrupt handlers */
163 static void *cambio_ih;
165 struct cam_periph *xpt_periph;
167 static periph_init_t xpt_periph_init;
169 static struct periph_driver xpt_driver =
171 xpt_periph_init, "xpt",
172 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
176 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
178 static d_open_t xptopen;
179 static d_close_t xptclose;
180 static d_ioctl_t xptioctl;
182 static struct cdevsw xpt_cdevsw = {
183 .d_version = D_VERSION,
191 /* Storage for debugging datastructures */
193 struct cam_path *cam_dpath;
194 #ifdef CAM_DEBUG_FLAGS
195 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
197 u_int32_t cam_dflags = CAM_DEBUG_NONE;
199 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
200 SYSCTL_INT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
201 &cam_dflags, 0, "Cam Debug Flags");
202 u_int32_t cam_debug_delay;
203 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
204 SYSCTL_INT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
205 &cam_debug_delay, 0, "Cam Debug Flags");
208 /* Our boot-time initialization hook */
209 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
211 static moduledata_t cam_moduledata = {
213 cam_module_event_handler,
217 static int xpt_init(void *);
219 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
220 MODULE_VERSION(cam, 1);
223 static void xpt_async_bcast(struct async_list *async_head,
224 u_int32_t async_code,
225 struct cam_path *path,
227 static path_id_t xptnextfreepathid(void);
228 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
229 static union ccb *xpt_get_ccb(struct cam_ed *device);
230 static void xpt_run_dev_allocq(struct cam_eb *bus);
231 static void xpt_run_dev_sendq(struct cam_eb *bus);
232 static timeout_t xpt_release_devq_timeout;
233 static void xpt_release_simq_timeout(void *arg) __unused;
234 static void xpt_release_bus(struct cam_eb *bus);
235 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
236 u_int count, int run_queue);
237 static struct cam_et*
238 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
239 static void xpt_release_target(struct cam_et *target);
240 static struct cam_eb*
241 xpt_find_bus(path_id_t path_id);
242 static struct cam_et*
243 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
244 static struct cam_ed*
245 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
246 static void xpt_config(void *arg);
247 static xpt_devicefunc_t xptpassannouncefunc;
248 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
249 static void xptpoll(struct cam_sim *sim);
250 static void camisr(void *);
251 static void camisr_runqueue(void *);
252 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
253 u_int num_patterns, struct cam_eb *bus);
254 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
256 struct cam_ed *device);
257 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
259 struct cam_periph *periph);
260 static xpt_busfunc_t xptedtbusfunc;
261 static xpt_targetfunc_t xptedttargetfunc;
262 static xpt_devicefunc_t xptedtdevicefunc;
263 static xpt_periphfunc_t xptedtperiphfunc;
264 static xpt_pdrvfunc_t xptplistpdrvfunc;
265 static xpt_periphfunc_t xptplistperiphfunc;
266 static int xptedtmatch(struct ccb_dev_match *cdm);
267 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
268 static int xptbustraverse(struct cam_eb *start_bus,
269 xpt_busfunc_t *tr_func, void *arg);
270 static int xpttargettraverse(struct cam_eb *bus,
271 struct cam_et *start_target,
272 xpt_targetfunc_t *tr_func, void *arg);
273 static int xptdevicetraverse(struct cam_et *target,
274 struct cam_ed *start_device,
275 xpt_devicefunc_t *tr_func, void *arg);
276 static int xptperiphtraverse(struct cam_ed *device,
277 struct cam_periph *start_periph,
278 xpt_periphfunc_t *tr_func, void *arg);
279 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
280 xpt_pdrvfunc_t *tr_func, void *arg);
281 static int xptpdperiphtraverse(struct periph_driver **pdrv,
282 struct cam_periph *start_periph,
283 xpt_periphfunc_t *tr_func,
285 static xpt_busfunc_t xptdefbusfunc;
286 static xpt_targetfunc_t xptdeftargetfunc;
287 static xpt_devicefunc_t xptdefdevicefunc;
288 static xpt_periphfunc_t xptdefperiphfunc;
289 static void xpt_finishconfig_task(void *context, int pending);
290 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
291 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
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);
1108 static dev_match_ret
1109 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1112 dev_match_ret retval;
1115 retval = DM_RET_NONE;
1118 * If we aren't given something to match against, that's an error.
1121 return(DM_RET_ERROR);
1124 * If there are no match entries, then this bus matches no
1127 if ((patterns == NULL) || (num_patterns == 0))
1128 return(DM_RET_DESCEND | DM_RET_COPY);
1130 for (i = 0; i < num_patterns; i++) {
1131 struct bus_match_pattern *cur_pattern;
1134 * If the pattern in question isn't for a bus node, we
1135 * aren't interested. However, we do indicate to the
1136 * calling routine that we should continue descending the
1137 * tree, since the user wants to match against lower-level
1140 if (patterns[i].type != DEV_MATCH_BUS) {
1141 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1142 retval |= DM_RET_DESCEND;
1146 cur_pattern = &patterns[i].pattern.bus_pattern;
1149 * If they want to match any bus node, we give them any
1152 if (cur_pattern->flags == BUS_MATCH_ANY) {
1153 /* set the copy flag */
1154 retval |= DM_RET_COPY;
1157 * If we've already decided on an action, go ahead
1160 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1165 * Not sure why someone would do this...
1167 if (cur_pattern->flags == BUS_MATCH_NONE)
1170 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1171 && (cur_pattern->path_id != bus->path_id))
1174 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1175 && (cur_pattern->bus_id != bus->sim->bus_id))
1178 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1179 && (cur_pattern->unit_number != bus->sim->unit_number))
1182 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1183 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1188 * If we get to this point, the user definitely wants
1189 * information on this bus. So tell the caller to copy the
1192 retval |= DM_RET_COPY;
1195 * If the return action has been set to descend, then we
1196 * know that we've already seen a non-bus matching
1197 * expression, therefore we need to further descend the tree.
1198 * This won't change by continuing around the loop, so we
1199 * go ahead and return. If we haven't seen a non-bus
1200 * matching expression, we keep going around the loop until
1201 * we exhaust the matching expressions. We'll set the stop
1202 * flag once we fall out of the loop.
1204 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1209 * If the return action hasn't been set to descend yet, that means
1210 * we haven't seen anything other than bus matching patterns. So
1211 * tell the caller to stop descending the tree -- the user doesn't
1212 * want to match against lower level tree elements.
1214 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1215 retval |= DM_RET_STOP;
1220 static dev_match_ret
1221 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1222 struct cam_ed *device)
1224 dev_match_ret retval;
1227 retval = DM_RET_NONE;
1230 * If we aren't given something to match against, that's an error.
1233 return(DM_RET_ERROR);
1236 * If there are no match entries, then this device matches no
1239 if ((patterns == NULL) || (num_patterns == 0))
1240 return(DM_RET_DESCEND | DM_RET_COPY);
1242 for (i = 0; i < num_patterns; i++) {
1243 struct device_match_pattern *cur_pattern;
1246 * If the pattern in question isn't for a device node, we
1247 * aren't interested.
1249 if (patterns[i].type != DEV_MATCH_DEVICE) {
1250 if ((patterns[i].type == DEV_MATCH_PERIPH)
1251 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1252 retval |= DM_RET_DESCEND;
1256 cur_pattern = &patterns[i].pattern.device_pattern;
1259 * If they want to match any device node, we give them any
1262 if (cur_pattern->flags == DEV_MATCH_ANY) {
1263 /* set the copy flag */
1264 retval |= DM_RET_COPY;
1268 * If we've already decided on an action, go ahead
1271 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1276 * Not sure why someone would do this...
1278 if (cur_pattern->flags == DEV_MATCH_NONE)
1281 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1282 && (cur_pattern->path_id != device->target->bus->path_id))
1285 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1286 && (cur_pattern->target_id != device->target->target_id))
1289 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1290 && (cur_pattern->target_lun != device->lun_id))
1293 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1294 && (cam_quirkmatch((caddr_t)&device->inq_data,
1295 (caddr_t)&cur_pattern->inq_pat,
1296 1, sizeof(cur_pattern->inq_pat),
1297 scsi_static_inquiry_match) == NULL))
1301 * If we get to this point, the user definitely wants
1302 * information on this device. So tell the caller to copy
1305 retval |= DM_RET_COPY;
1308 * If the return action has been set to descend, then we
1309 * know that we've already seen a peripheral matching
1310 * expression, therefore we need to further descend the tree.
1311 * This won't change by continuing around the loop, so we
1312 * go ahead and return. If we haven't seen a peripheral
1313 * matching expression, we keep going around the loop until
1314 * we exhaust the matching expressions. We'll set the stop
1315 * flag once we fall out of the loop.
1317 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1322 * If the return action hasn't been set to descend yet, that means
1323 * we haven't seen any peripheral matching patterns. So tell the
1324 * caller to stop descending the tree -- the user doesn't want to
1325 * match against lower level tree elements.
1327 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1328 retval |= DM_RET_STOP;
1334 * Match a single peripheral against any number of match patterns.
1336 static dev_match_ret
1337 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1338 struct cam_periph *periph)
1340 dev_match_ret retval;
1344 * If we aren't given something to match against, that's an error.
1347 return(DM_RET_ERROR);
1350 * If there are no match entries, then this peripheral matches no
1353 if ((patterns == NULL) || (num_patterns == 0))
1354 return(DM_RET_STOP | DM_RET_COPY);
1357 * There aren't any nodes below a peripheral node, so there's no
1358 * reason to descend the tree any further.
1360 retval = DM_RET_STOP;
1362 for (i = 0; i < num_patterns; i++) {
1363 struct periph_match_pattern *cur_pattern;
1366 * If the pattern in question isn't for a peripheral, we
1367 * aren't interested.
1369 if (patterns[i].type != DEV_MATCH_PERIPH)
1372 cur_pattern = &patterns[i].pattern.periph_pattern;
1375 * If they want to match on anything, then we will do so.
1377 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1378 /* set the copy flag */
1379 retval |= DM_RET_COPY;
1382 * We've already set the return action to stop,
1383 * since there are no nodes below peripherals in
1390 * Not sure why someone would do this...
1392 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1395 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1396 && (cur_pattern->path_id != periph->path->bus->path_id))
1400 * For the target and lun id's, we have to make sure the
1401 * target and lun pointers aren't NULL. The xpt peripheral
1402 * has a wildcard target and device.
1404 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1405 && ((periph->path->target == NULL)
1406 ||(cur_pattern->target_id != periph->path->target->target_id)))
1409 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1410 && ((periph->path->device == NULL)
1411 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1414 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1415 && (cur_pattern->unit_number != periph->unit_number))
1418 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1419 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1424 * If we get to this point, the user definitely wants
1425 * information on this peripheral. So tell the caller to
1426 * copy the data out.
1428 retval |= DM_RET_COPY;
1431 * The return action has already been set to stop, since
1432 * peripherals don't have any nodes below them in the EDT.
1438 * If we get to this point, the peripheral that was passed in
1439 * doesn't match any of the patterns.
1445 xptedtbusfunc(struct cam_eb *bus, void *arg)
1447 struct ccb_dev_match *cdm;
1448 dev_match_ret retval;
1450 cdm = (struct ccb_dev_match *)arg;
1453 * If our position is for something deeper in the tree, that means
1454 * that we've already seen this node. So, we keep going down.
1456 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1457 && (cdm->pos.cookie.bus == bus)
1458 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1459 && (cdm->pos.cookie.target != NULL))
1460 retval = DM_RET_DESCEND;
1462 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1465 * If we got an error, bail out of the search.
1467 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1468 cdm->status = CAM_DEV_MATCH_ERROR;
1473 * If the copy flag is set, copy this bus out.
1475 if (retval & DM_RET_COPY) {
1478 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1479 sizeof(struct dev_match_result));
1482 * If we don't have enough space to put in another
1483 * match result, save our position and tell the
1484 * user there are more devices to check.
1486 if (spaceleft < sizeof(struct dev_match_result)) {
1487 bzero(&cdm->pos, sizeof(cdm->pos));
1488 cdm->pos.position_type =
1489 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1491 cdm->pos.cookie.bus = bus;
1492 cdm->pos.generations[CAM_BUS_GENERATION]=
1493 xsoftc.bus_generation;
1494 cdm->status = CAM_DEV_MATCH_MORE;
1497 j = cdm->num_matches;
1499 cdm->matches[j].type = DEV_MATCH_BUS;
1500 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1501 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1502 cdm->matches[j].result.bus_result.unit_number =
1503 bus->sim->unit_number;
1504 strncpy(cdm->matches[j].result.bus_result.dev_name,
1505 bus->sim->sim_name, DEV_IDLEN);
1509 * If the user is only interested in busses, there's no
1510 * reason to descend to the next level in the tree.
1512 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1516 * If there is a target generation recorded, check it to
1517 * make sure the target list hasn't changed.
1519 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1520 && (bus == cdm->pos.cookie.bus)
1521 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1522 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1523 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1525 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1529 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1530 && (cdm->pos.cookie.bus == bus)
1531 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1532 && (cdm->pos.cookie.target != NULL))
1533 return(xpttargettraverse(bus,
1534 (struct cam_et *)cdm->pos.cookie.target,
1535 xptedttargetfunc, arg));
1537 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1541 xptedttargetfunc(struct cam_et *target, void *arg)
1543 struct ccb_dev_match *cdm;
1545 cdm = (struct ccb_dev_match *)arg;
1548 * If there is a device list generation recorded, check it to
1549 * make sure the device list hasn't changed.
1551 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1552 && (cdm->pos.cookie.bus == target->bus)
1553 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1554 && (cdm->pos.cookie.target == target)
1555 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1556 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1557 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1558 target->generation)) {
1559 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1563 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1564 && (cdm->pos.cookie.bus == target->bus)
1565 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1566 && (cdm->pos.cookie.target == target)
1567 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1568 && (cdm->pos.cookie.device != NULL))
1569 return(xptdevicetraverse(target,
1570 (struct cam_ed *)cdm->pos.cookie.device,
1571 xptedtdevicefunc, arg));
1573 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1577 xptedtdevicefunc(struct cam_ed *device, void *arg)
1580 struct ccb_dev_match *cdm;
1581 dev_match_ret retval;
1583 cdm = (struct ccb_dev_match *)arg;
1586 * If our position is for something deeper in the tree, that means
1587 * that we've already seen this node. So, we keep going down.
1589 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1590 && (cdm->pos.cookie.device == device)
1591 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1592 && (cdm->pos.cookie.periph != NULL))
1593 retval = DM_RET_DESCEND;
1595 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1598 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1599 cdm->status = CAM_DEV_MATCH_ERROR;
1604 * If the copy flag is set, copy this device out.
1606 if (retval & DM_RET_COPY) {
1609 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1610 sizeof(struct dev_match_result));
1613 * If we don't have enough space to put in another
1614 * match result, save our position and tell the
1615 * user there are more devices to check.
1617 if (spaceleft < sizeof(struct dev_match_result)) {
1618 bzero(&cdm->pos, sizeof(cdm->pos));
1619 cdm->pos.position_type =
1620 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1621 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1623 cdm->pos.cookie.bus = device->target->bus;
1624 cdm->pos.generations[CAM_BUS_GENERATION]=
1625 xsoftc.bus_generation;
1626 cdm->pos.cookie.target = device->target;
1627 cdm->pos.generations[CAM_TARGET_GENERATION] =
1628 device->target->bus->generation;
1629 cdm->pos.cookie.device = device;
1630 cdm->pos.generations[CAM_DEV_GENERATION] =
1631 device->target->generation;
1632 cdm->status = CAM_DEV_MATCH_MORE;
1635 j = cdm->num_matches;
1637 cdm->matches[j].type = DEV_MATCH_DEVICE;
1638 cdm->matches[j].result.device_result.path_id =
1639 device->target->bus->path_id;
1640 cdm->matches[j].result.device_result.target_id =
1641 device->target->target_id;
1642 cdm->matches[j].result.device_result.target_lun =
1644 cdm->matches[j].result.device_result.protocol =
1646 bcopy(&device->inq_data,
1647 &cdm->matches[j].result.device_result.inq_data,
1648 sizeof(struct scsi_inquiry_data));
1649 bcopy(&device->ident_data,
1650 &cdm->matches[j].result.device_result.ident_data,
1651 sizeof(struct ata_params));
1653 /* Let the user know whether this device is unconfigured */
1654 if (device->flags & CAM_DEV_UNCONFIGURED)
1655 cdm->matches[j].result.device_result.flags =
1656 DEV_RESULT_UNCONFIGURED;
1658 cdm->matches[j].result.device_result.flags =
1663 * If the user isn't interested in peripherals, don't descend
1664 * the tree any further.
1666 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1670 * If there is a peripheral list generation recorded, make sure
1671 * it hasn't changed.
1673 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1674 && (device->target->bus == cdm->pos.cookie.bus)
1675 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1676 && (device->target == cdm->pos.cookie.target)
1677 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1678 && (device == cdm->pos.cookie.device)
1679 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1680 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1681 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1682 device->generation)){
1683 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1687 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1688 && (cdm->pos.cookie.bus == device->target->bus)
1689 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1690 && (cdm->pos.cookie.target == device->target)
1691 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1692 && (cdm->pos.cookie.device == device)
1693 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1694 && (cdm->pos.cookie.periph != NULL))
1695 return(xptperiphtraverse(device,
1696 (struct cam_periph *)cdm->pos.cookie.periph,
1697 xptedtperiphfunc, arg));
1699 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1703 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1705 struct ccb_dev_match *cdm;
1706 dev_match_ret retval;
1708 cdm = (struct ccb_dev_match *)arg;
1710 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1712 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1713 cdm->status = CAM_DEV_MATCH_ERROR;
1718 * If the copy flag is set, copy this peripheral out.
1720 if (retval & DM_RET_COPY) {
1723 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1724 sizeof(struct dev_match_result));
1727 * If we don't have enough space to put in another
1728 * match result, save our position and tell the
1729 * user there are more devices to check.
1731 if (spaceleft < sizeof(struct dev_match_result)) {
1732 bzero(&cdm->pos, sizeof(cdm->pos));
1733 cdm->pos.position_type =
1734 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1735 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1738 cdm->pos.cookie.bus = periph->path->bus;
1739 cdm->pos.generations[CAM_BUS_GENERATION]=
1740 xsoftc.bus_generation;
1741 cdm->pos.cookie.target = periph->path->target;
1742 cdm->pos.generations[CAM_TARGET_GENERATION] =
1743 periph->path->bus->generation;
1744 cdm->pos.cookie.device = periph->path->device;
1745 cdm->pos.generations[CAM_DEV_GENERATION] =
1746 periph->path->target->generation;
1747 cdm->pos.cookie.periph = periph;
1748 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1749 periph->path->device->generation;
1750 cdm->status = CAM_DEV_MATCH_MORE;
1754 j = cdm->num_matches;
1756 cdm->matches[j].type = DEV_MATCH_PERIPH;
1757 cdm->matches[j].result.periph_result.path_id =
1758 periph->path->bus->path_id;
1759 cdm->matches[j].result.periph_result.target_id =
1760 periph->path->target->target_id;
1761 cdm->matches[j].result.periph_result.target_lun =
1762 periph->path->device->lun_id;
1763 cdm->matches[j].result.periph_result.unit_number =
1764 periph->unit_number;
1765 strncpy(cdm->matches[j].result.periph_result.periph_name,
1766 periph->periph_name, DEV_IDLEN);
1773 xptedtmatch(struct ccb_dev_match *cdm)
1777 cdm->num_matches = 0;
1780 * Check the bus list generation. If it has changed, the user
1781 * needs to reset everything and start over.
1783 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1784 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1785 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1786 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1790 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1791 && (cdm->pos.cookie.bus != NULL))
1792 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1793 xptedtbusfunc, cdm);
1795 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1798 * If we get back 0, that means that we had to stop before fully
1799 * traversing the EDT. It also means that one of the subroutines
1800 * has set the status field to the proper value. If we get back 1,
1801 * we've fully traversed the EDT and copied out any matching entries.
1804 cdm->status = CAM_DEV_MATCH_LAST;
1810 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1812 struct ccb_dev_match *cdm;
1814 cdm = (struct ccb_dev_match *)arg;
1816 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1817 && (cdm->pos.cookie.pdrv == pdrv)
1818 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1819 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1820 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1821 (*pdrv)->generation)) {
1822 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1826 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1827 && (cdm->pos.cookie.pdrv == pdrv)
1828 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1829 && (cdm->pos.cookie.periph != NULL))
1830 return(xptpdperiphtraverse(pdrv,
1831 (struct cam_periph *)cdm->pos.cookie.periph,
1832 xptplistperiphfunc, arg));
1834 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1838 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1840 struct ccb_dev_match *cdm;
1841 dev_match_ret retval;
1843 cdm = (struct ccb_dev_match *)arg;
1845 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1847 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1848 cdm->status = CAM_DEV_MATCH_ERROR;
1853 * If the copy flag is set, copy this peripheral out.
1855 if (retval & DM_RET_COPY) {
1858 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1859 sizeof(struct dev_match_result));
1862 * If we don't have enough space to put in another
1863 * match result, save our position and tell the
1864 * user there are more devices to check.
1866 if (spaceleft < sizeof(struct dev_match_result)) {
1867 struct periph_driver **pdrv;
1870 bzero(&cdm->pos, sizeof(cdm->pos));
1871 cdm->pos.position_type =
1872 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1876 * This may look a bit non-sensical, but it is
1877 * actually quite logical. There are very few
1878 * peripheral drivers, and bloating every peripheral
1879 * structure with a pointer back to its parent
1880 * peripheral driver linker set entry would cost
1881 * more in the long run than doing this quick lookup.
1883 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1884 if (strcmp((*pdrv)->driver_name,
1885 periph->periph_name) == 0)
1889 if (*pdrv == NULL) {
1890 cdm->status = CAM_DEV_MATCH_ERROR;
1894 cdm->pos.cookie.pdrv = pdrv;
1896 * The periph generation slot does double duty, as
1897 * does the periph pointer slot. They are used for
1898 * both edt and pdrv lookups and positioning.
1900 cdm->pos.cookie.periph = periph;
1901 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1902 (*pdrv)->generation;
1903 cdm->status = CAM_DEV_MATCH_MORE;
1907 j = cdm->num_matches;
1909 cdm->matches[j].type = DEV_MATCH_PERIPH;
1910 cdm->matches[j].result.periph_result.path_id =
1911 periph->path->bus->path_id;
1914 * The transport layer peripheral doesn't have a target or
1917 if (periph->path->target)
1918 cdm->matches[j].result.periph_result.target_id =
1919 periph->path->target->target_id;
1921 cdm->matches[j].result.periph_result.target_id = -1;
1923 if (periph->path->device)
1924 cdm->matches[j].result.periph_result.target_lun =
1925 periph->path->device->lun_id;
1927 cdm->matches[j].result.periph_result.target_lun = -1;
1929 cdm->matches[j].result.periph_result.unit_number =
1930 periph->unit_number;
1931 strncpy(cdm->matches[j].result.periph_result.periph_name,
1932 periph->periph_name, DEV_IDLEN);
1939 xptperiphlistmatch(struct ccb_dev_match *cdm)
1943 cdm->num_matches = 0;
1946 * At this point in the edt traversal function, we check the bus
1947 * list generation to make sure that no busses have been added or
1948 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1949 * For the peripheral driver list traversal function, however, we
1950 * don't have to worry about new peripheral driver types coming or
1951 * going; they're in a linker set, and therefore can't change
1952 * without a recompile.
1955 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1956 && (cdm->pos.cookie.pdrv != NULL))
1957 ret = xptpdrvtraverse(
1958 (struct periph_driver **)cdm->pos.cookie.pdrv,
1959 xptplistpdrvfunc, cdm);
1961 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
1964 * If we get back 0, that means that we had to stop before fully
1965 * traversing the peripheral driver tree. It also means that one of
1966 * the subroutines has set the status field to the proper value. If
1967 * we get back 1, we've fully traversed the EDT and copied out any
1971 cdm->status = CAM_DEV_MATCH_LAST;
1977 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
1979 struct cam_eb *bus, *next_bus;
1984 mtx_lock(&xsoftc.xpt_topo_lock);
1985 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
1992 * XXX The locking here is obviously very complex. We
1993 * should work to simplify it.
1995 mtx_unlock(&xsoftc.xpt_topo_lock);
1996 CAM_SIM_LOCK(bus->sim);
1997 retval = tr_func(bus, arg);
1998 CAM_SIM_UNLOCK(bus->sim);
2000 mtx_lock(&xsoftc.xpt_topo_lock);
2001 next_bus = TAILQ_NEXT(bus, links);
2002 mtx_unlock(&xsoftc.xpt_topo_lock);
2004 xpt_release_bus(bus);
2008 mtx_lock(&xsoftc.xpt_topo_lock);
2010 mtx_unlock(&xsoftc.xpt_topo_lock);
2016 xpt_sim_opened(struct cam_sim *sim)
2019 struct cam_et *target;
2020 struct cam_ed *device;
2021 struct cam_periph *periph;
2023 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2024 mtx_assert(sim->mtx, MA_OWNED);
2026 mtx_lock(&xsoftc.xpt_topo_lock);
2027 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2028 if (bus->sim != sim)
2031 TAILQ_FOREACH(target, &bus->et_entries, links) {
2032 TAILQ_FOREACH(device, &target->ed_entries, links) {
2033 SLIST_FOREACH(periph, &device->periphs,
2035 if (periph->refcount > 0) {
2036 mtx_unlock(&xsoftc.xpt_topo_lock);
2044 mtx_unlock(&xsoftc.xpt_topo_lock);
2049 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2050 xpt_targetfunc_t *tr_func, void *arg)
2052 struct cam_et *target, *next_target;
2056 for (target = (start_target ? start_target :
2057 TAILQ_FIRST(&bus->et_entries));
2058 target != NULL; target = next_target) {
2062 retval = tr_func(target, arg);
2064 next_target = TAILQ_NEXT(target, links);
2066 xpt_release_target(target);
2076 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2077 xpt_devicefunc_t *tr_func, void *arg)
2079 struct cam_ed *device, *next_device;
2083 for (device = (start_device ? start_device :
2084 TAILQ_FIRST(&target->ed_entries));
2086 device = next_device) {
2089 * Hold a reference so the current device does not go away
2094 retval = tr_func(device, arg);
2097 * Grab our next pointer before we release the current
2100 next_device = TAILQ_NEXT(device, links);
2102 xpt_release_device(device);
2112 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2113 xpt_periphfunc_t *tr_func, void *arg)
2115 struct cam_periph *periph, *next_periph;
2121 for (periph = (start_periph ? start_periph :
2122 SLIST_FIRST(&device->periphs));
2124 periph = next_periph) {
2128 * In this case, we want to show peripherals that have been
2129 * invalidated, but not peripherals that are scheduled to
2130 * be freed. So instead of calling cam_periph_acquire(),
2131 * which will fail if the periph has been invalidated, we
2132 * just check for the free flag here. If it is free, we
2133 * skip to the next periph.
2135 if (periph->flags & CAM_PERIPH_FREE) {
2136 next_periph = SLIST_NEXT(periph, periph_links);
2141 * Acquire a reference to this periph while we call the
2142 * traversal function, so it can't go away.
2148 retval = tr_func(periph, arg);
2151 * We need the lock for list traversal.
2156 * Grab the next peripheral before we release this one, so
2157 * our next pointer is still valid.
2159 next_periph = SLIST_NEXT(periph, periph_links);
2161 cam_periph_release_locked_buses(periph);
2175 xptpdrvtraverse(struct periph_driver **start_pdrv,
2176 xpt_pdrvfunc_t *tr_func, void *arg)
2178 struct periph_driver **pdrv;
2184 * We don't traverse the peripheral driver list like we do the
2185 * other lists, because it is a linker set, and therefore cannot be
2186 * changed during runtime. If the peripheral driver list is ever
2187 * re-done to be something other than a linker set (i.e. it can
2188 * change while the system is running), the list traversal should
2189 * be modified to work like the other traversal functions.
2191 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2192 *pdrv != NULL; pdrv++) {
2193 retval = tr_func(pdrv, arg);
2203 xptpdperiphtraverse(struct periph_driver **pdrv,
2204 struct cam_periph *start_periph,
2205 xpt_periphfunc_t *tr_func, void *arg)
2207 struct cam_periph *periph, *next_periph;
2213 for (periph = (start_periph ? start_periph :
2214 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2215 periph = next_periph) {
2219 * In this case, we want to show peripherals that have been
2220 * invalidated, but not peripherals that are scheduled to
2221 * be freed. So instead of calling cam_periph_acquire(),
2222 * which will fail if the periph has been invalidated, we
2223 * just check for the free flag here. If it is free, we
2224 * skip to the next periph.
2226 if (periph->flags & CAM_PERIPH_FREE) {
2227 next_periph = TAILQ_NEXT(periph, unit_links);
2232 * Acquire a reference to this periph while we call the
2233 * traversal function, so it can't go away.
2238 * XXX KDM we have the toplogy lock here, but in
2239 * xptperiphtraverse(), we drop it before calling the
2240 * traversal function. Which is correct?
2242 retval = tr_func(periph, arg);
2245 * Grab the next peripheral before we release this one, so
2246 * our next pointer is still valid.
2248 next_periph = TAILQ_NEXT(periph, unit_links);
2250 cam_periph_release_locked_buses(periph);
2263 xptdefbusfunc(struct cam_eb *bus, void *arg)
2265 struct xpt_traverse_config *tr_config;
2267 tr_config = (struct xpt_traverse_config *)arg;
2269 if (tr_config->depth == XPT_DEPTH_BUS) {
2270 xpt_busfunc_t *tr_func;
2272 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2274 return(tr_func(bus, tr_config->tr_arg));
2276 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2280 xptdeftargetfunc(struct cam_et *target, void *arg)
2282 struct xpt_traverse_config *tr_config;
2284 tr_config = (struct xpt_traverse_config *)arg;
2286 if (tr_config->depth == XPT_DEPTH_TARGET) {
2287 xpt_targetfunc_t *tr_func;
2289 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2291 return(tr_func(target, tr_config->tr_arg));
2293 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2297 xptdefdevicefunc(struct cam_ed *device, void *arg)
2299 struct xpt_traverse_config *tr_config;
2301 tr_config = (struct xpt_traverse_config *)arg;
2303 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2304 xpt_devicefunc_t *tr_func;
2306 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2308 return(tr_func(device, tr_config->tr_arg));
2310 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2314 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2316 struct xpt_traverse_config *tr_config;
2317 xpt_periphfunc_t *tr_func;
2319 tr_config = (struct xpt_traverse_config *)arg;
2321 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2324 * Unlike the other default functions, we don't check for depth
2325 * here. The peripheral driver level is the last level in the EDT,
2326 * so if we're here, we should execute the function in question.
2328 return(tr_func(periph, tr_config->tr_arg));
2332 * Execute the given function for every bus in the EDT.
2335 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2337 struct xpt_traverse_config tr_config;
2339 tr_config.depth = XPT_DEPTH_BUS;
2340 tr_config.tr_func = tr_func;
2341 tr_config.tr_arg = arg;
2343 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2347 * Execute the given function for every device in the EDT.
2350 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2352 struct xpt_traverse_config tr_config;
2354 tr_config.depth = XPT_DEPTH_DEVICE;
2355 tr_config.tr_func = tr_func;
2356 tr_config.tr_arg = arg;
2358 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2362 xptsetasyncfunc(struct cam_ed *device, void *arg)
2364 struct cam_path path;
2365 struct ccb_getdev cgd;
2366 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2369 * Don't report unconfigured devices (Wildcard devs,
2370 * devices only for target mode, device instances
2371 * that have been invalidated but are waiting for
2372 * their last reference count to be released).
2374 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2377 xpt_compile_path(&path,
2379 device->target->bus->path_id,
2380 device->target->target_id,
2382 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2383 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2384 xpt_action((union ccb *)&cgd);
2385 csa->callback(csa->callback_arg,
2388 xpt_release_path(&path);
2394 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2396 struct cam_path path;
2397 struct ccb_pathinq cpi;
2398 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2400 xpt_compile_path(&path, /*periph*/NULL,
2402 CAM_TARGET_WILDCARD,
2404 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2405 cpi.ccb_h.func_code = XPT_PATH_INQ;
2406 xpt_action((union ccb *)&cpi);
2407 csa->callback(csa->callback_arg,
2410 xpt_release_path(&path);
2416 xpt_action(union ccb *start_ccb)
2419 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2421 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2422 /* Compatibility for RL-unaware code. */
2423 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2424 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2425 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2429 xpt_action_default(union ccb *start_ccb)
2432 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2434 struct cam_path *path;
2436 path = start_ccb->ccb_h.path;
2437 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2439 switch (start_ccb->ccb_h.func_code) {
2442 struct cam_ed *device;
2445 * For the sake of compatibility with SCSI-1
2446 * devices that may not understand the identify
2447 * message, we include lun information in the
2448 * second byte of all commands. SCSI-1 specifies
2449 * that luns are a 3 bit value and reserves only 3
2450 * bits for lun information in the CDB. Later
2451 * revisions of the SCSI spec allow for more than 8
2452 * luns, but have deprecated lun information in the
2453 * CDB. So, if the lun won't fit, we must omit.
2455 * Also be aware that during initial probing for devices,
2456 * the inquiry information is unknown but initialized to 0.
2457 * This means that this code will be exercised while probing
2458 * devices with an ANSI revision greater than 2.
2460 device = path->device;
2461 if (device->protocol_version <= SCSI_REV_2
2462 && start_ccb->ccb_h.target_lun < 8
2463 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2465 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2466 start_ccb->ccb_h.target_lun << 5;
2468 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2469 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2470 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2471 &path->device->inq_data),
2472 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2473 cdb_str, sizeof(cdb_str))));
2477 case XPT_CONT_TARGET_IO:
2478 start_ccb->csio.sense_resid = 0;
2479 start_ccb->csio.resid = 0;
2482 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2483 start_ccb->ataio.resid = 0;
2484 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2485 ata_op_string(&start_ccb->ataio.cmd),
2486 ata_cmd_string(&start_ccb->ataio.cmd,
2487 cdb_str, sizeof(cdb_str))));
2495 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2496 path->device->sim->devq->alloc_openings += frozen;
2498 xpt_run_dev_allocq(path->bus);
2499 if (xpt_schedule_dev_sendq(path->bus, path->device))
2500 xpt_run_dev_sendq(path->bus);
2503 case XPT_CALC_GEOMETRY:
2505 struct cam_sim *sim;
2507 /* Filter out garbage */
2508 if (start_ccb->ccg.block_size == 0
2509 || start_ccb->ccg.volume_size == 0) {
2510 start_ccb->ccg.cylinders = 0;
2511 start_ccb->ccg.heads = 0;
2512 start_ccb->ccg.secs_per_track = 0;
2513 start_ccb->ccb_h.status = CAM_REQ_CMP;
2516 #if defined(PC98) || defined(__sparc64__)
2518 * In a PC-98 system, geometry translation depens on
2519 * the "real" device geometry obtained from mode page 4.
2520 * SCSI geometry translation is performed in the
2521 * initialization routine of the SCSI BIOS and the result
2522 * stored in host memory. If the translation is available
2523 * in host memory, use it. If not, rely on the default
2524 * translation the device driver performs.
2525 * For sparc64, we may need adjust the geometry of large
2526 * disks in order to fit the limitations of the 16-bit
2527 * fields of the VTOC8 disk label.
2529 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2530 start_ccb->ccb_h.status = CAM_REQ_CMP;
2534 sim = path->bus->sim;
2535 (*(sim->sim_action))(sim, start_ccb);
2540 union ccb* abort_ccb;
2542 abort_ccb = start_ccb->cab.abort_ccb;
2543 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2545 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2546 struct cam_ccbq *ccbq;
2547 struct cam_ed *device;
2549 device = abort_ccb->ccb_h.path->device;
2550 ccbq = &device->ccbq;
2551 device->sim->devq->alloc_openings -=
2552 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2553 abort_ccb->ccb_h.status =
2554 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2555 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2556 xpt_done(abort_ccb);
2557 start_ccb->ccb_h.status = CAM_REQ_CMP;
2560 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2561 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2563 * We've caught this ccb en route to
2564 * the SIM. Flag it for abort and the
2565 * SIM will do so just before starting
2566 * real work on the CCB.
2568 abort_ccb->ccb_h.status =
2569 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2570 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2571 start_ccb->ccb_h.status = CAM_REQ_CMP;
2575 if (XPT_FC_IS_QUEUED(abort_ccb)
2576 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2578 * It's already completed but waiting
2579 * for our SWI to get to it.
2581 start_ccb->ccb_h.status = CAM_UA_ABORT;
2585 * If we weren't able to take care of the abort request
2586 * in the XPT, pass the request down to the SIM for processing.
2590 case XPT_ACCEPT_TARGET_IO:
2592 case XPT_IMMED_NOTIFY:
2593 case XPT_NOTIFY_ACK:
2595 case XPT_IMMEDIATE_NOTIFY:
2596 case XPT_NOTIFY_ACKNOWLEDGE:
2597 case XPT_GET_SIM_KNOB:
2598 case XPT_SET_SIM_KNOB:
2600 struct cam_sim *sim;
2602 sim = path->bus->sim;
2603 (*(sim->sim_action))(sim, start_ccb);
2608 struct cam_sim *sim;
2610 sim = path->bus->sim;
2611 (*(sim->sim_action))(sim, start_ccb);
2614 case XPT_PATH_STATS:
2615 start_ccb->cpis.last_reset = path->bus->last_reset;
2616 start_ccb->ccb_h.status = CAM_REQ_CMP;
2623 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2624 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2626 struct ccb_getdev *cgd;
2628 cgd = &start_ccb->cgd;
2629 cgd->protocol = dev->protocol;
2630 cgd->inq_data = dev->inq_data;
2631 cgd->ident_data = dev->ident_data;
2632 cgd->inq_flags = dev->inq_flags;
2633 cgd->ccb_h.status = CAM_REQ_CMP;
2634 cgd->serial_num_len = dev->serial_num_len;
2635 if ((dev->serial_num_len > 0)
2636 && (dev->serial_num != NULL))
2637 bcopy(dev->serial_num, cgd->serial_num,
2638 dev->serial_num_len);
2642 case XPT_GDEV_STATS:
2647 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2648 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2650 struct ccb_getdevstats *cgds;
2654 cgds = &start_ccb->cgds;
2657 cgds->dev_openings = dev->ccbq.dev_openings;
2658 cgds->dev_active = dev->ccbq.dev_active;
2659 cgds->devq_openings = dev->ccbq.devq_openings;
2660 cgds->devq_queued = dev->ccbq.queue.entries;
2661 cgds->held = dev->ccbq.held;
2662 cgds->last_reset = tar->last_reset;
2663 cgds->maxtags = dev->maxtags;
2664 cgds->mintags = dev->mintags;
2665 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2666 cgds->last_reset = bus->last_reset;
2667 cgds->ccb_h.status = CAM_REQ_CMP;
2673 struct cam_periph *nperiph;
2674 struct periph_list *periph_head;
2675 struct ccb_getdevlist *cgdl;
2677 struct cam_ed *device;
2684 * Don't want anyone mucking with our data.
2686 device = path->device;
2687 periph_head = &device->periphs;
2688 cgdl = &start_ccb->cgdl;
2691 * Check and see if the list has changed since the user
2692 * last requested a list member. If so, tell them that the
2693 * list has changed, and therefore they need to start over
2694 * from the beginning.
2696 if ((cgdl->index != 0) &&
2697 (cgdl->generation != device->generation)) {
2698 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2703 * Traverse the list of peripherals and attempt to find
2704 * the requested peripheral.
2706 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2707 (nperiph != NULL) && (i <= cgdl->index);
2708 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2709 if (i == cgdl->index) {
2710 strncpy(cgdl->periph_name,
2711 nperiph->periph_name,
2713 cgdl->unit_number = nperiph->unit_number;
2718 cgdl->status = CAM_GDEVLIST_ERROR;
2722 if (nperiph == NULL)
2723 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2725 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2728 cgdl->generation = device->generation;
2730 cgdl->ccb_h.status = CAM_REQ_CMP;
2735 dev_pos_type position_type;
2736 struct ccb_dev_match *cdm;
2738 cdm = &start_ccb->cdm;
2741 * There are two ways of getting at information in the EDT.
2742 * The first way is via the primary EDT tree. It starts
2743 * with a list of busses, then a list of targets on a bus,
2744 * then devices/luns on a target, and then peripherals on a
2745 * device/lun. The "other" way is by the peripheral driver
2746 * lists. The peripheral driver lists are organized by
2747 * peripheral driver. (obviously) So it makes sense to
2748 * use the peripheral driver list if the user is looking
2749 * for something like "da1", or all "da" devices. If the
2750 * user is looking for something on a particular bus/target
2751 * or lun, it's generally better to go through the EDT tree.
2754 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2755 position_type = cdm->pos.position_type;
2759 position_type = CAM_DEV_POS_NONE;
2761 for (i = 0; i < cdm->num_patterns; i++) {
2762 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2763 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2764 position_type = CAM_DEV_POS_EDT;
2769 if (cdm->num_patterns == 0)
2770 position_type = CAM_DEV_POS_EDT;
2771 else if (position_type == CAM_DEV_POS_NONE)
2772 position_type = CAM_DEV_POS_PDRV;
2775 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2776 case CAM_DEV_POS_EDT:
2779 case CAM_DEV_POS_PDRV:
2780 xptperiphlistmatch(cdm);
2783 cdm->status = CAM_DEV_MATCH_ERROR;
2787 if (cdm->status == CAM_DEV_MATCH_ERROR)
2788 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2790 start_ccb->ccb_h.status = CAM_REQ_CMP;
2796 struct ccb_setasync *csa;
2797 struct async_node *cur_entry;
2798 struct async_list *async_head;
2801 csa = &start_ccb->csa;
2802 added = csa->event_enable;
2803 async_head = &path->device->asyncs;
2806 * If there is already an entry for us, simply
2809 cur_entry = SLIST_FIRST(async_head);
2810 while (cur_entry != NULL) {
2811 if ((cur_entry->callback_arg == csa->callback_arg)
2812 && (cur_entry->callback == csa->callback))
2814 cur_entry = SLIST_NEXT(cur_entry, links);
2817 if (cur_entry != NULL) {
2819 * If the request has no flags set,
2822 added &= ~cur_entry->event_enable;
2823 if (csa->event_enable == 0) {
2824 SLIST_REMOVE(async_head, cur_entry,
2826 xpt_release_device(path->device);
2827 free(cur_entry, M_CAMXPT);
2829 cur_entry->event_enable = csa->event_enable;
2831 csa->event_enable = added;
2833 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2835 if (cur_entry == NULL) {
2836 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2839 cur_entry->event_enable = csa->event_enable;
2840 cur_entry->callback_arg = csa->callback_arg;
2841 cur_entry->callback = csa->callback;
2842 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2843 xpt_acquire_device(path->device);
2845 start_ccb->ccb_h.status = CAM_REQ_CMP;
2850 struct ccb_relsim *crs;
2853 crs = &start_ccb->crs;
2857 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2861 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2863 /* Don't ever go below one opening */
2864 if (crs->openings > 0) {
2865 xpt_dev_ccbq_resize(path, crs->openings);
2868 "number of openings is now %d\n",
2874 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2876 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2879 * Just extend the old timeout and decrement
2880 * the freeze count so that a single timeout
2881 * is sufficient for releasing the queue.
2883 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2884 callout_stop(&dev->callout);
2887 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2890 callout_reset(&dev->callout,
2891 (crs->release_timeout * hz) / 1000,
2892 xpt_release_devq_timeout, dev);
2894 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2898 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2900 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2902 * Decrement the freeze count so that a single
2903 * completion is still sufficient to unfreeze
2906 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2909 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2910 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2914 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2916 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2917 || (dev->ccbq.dev_active == 0)) {
2919 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2922 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2923 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2927 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2928 xpt_release_devq_rl(path, /*runlevel*/
2929 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2930 crs->release_timeout : 0,
2931 /*count*/1, /*run_queue*/TRUE);
2933 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2934 start_ccb->ccb_h.status = CAM_REQ_CMP;
2939 #ifdef CAM_DEBUG_DELAY
2940 cam_debug_delay = CAM_DEBUG_DELAY;
2942 cam_dflags = start_ccb->cdbg.flags;
2943 if (cam_dpath != NULL) {
2944 xpt_free_path(cam_dpath);
2948 if (cam_dflags != CAM_DEBUG_NONE) {
2949 if (xpt_create_path(&cam_dpath, xpt_periph,
2950 start_ccb->ccb_h.path_id,
2951 start_ccb->ccb_h.target_id,
2952 start_ccb->ccb_h.target_lun) !=
2954 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2955 cam_dflags = CAM_DEBUG_NONE;
2957 start_ccb->ccb_h.status = CAM_REQ_CMP;
2958 xpt_print(cam_dpath, "debugging flags now %x\n",
2963 start_ccb->ccb_h.status = CAM_REQ_CMP;
2965 #else /* !CAMDEBUG */
2966 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2967 #endif /* CAMDEBUG */
2970 case XPT_FREEZE_QUEUE:
2972 struct ccb_relsim *crs = &start_ccb->crs;
2974 xpt_freeze_devq_rl(path, /*runlevel*/
2975 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2976 crs->release_timeout : 0, /*count*/1);
2977 start_ccb->ccb_h.status = CAM_REQ_CMP;
2981 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2982 xpt_freeze_devq(path, 1);
2983 start_ccb->ccb_h.status = CAM_REQ_CMP;
2990 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2991 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2992 xpt_done(start_ccb);
2999 xpt_polled_action(union ccb *start_ccb)
3002 struct cam_sim *sim;
3003 struct cam_devq *devq;
3007 timeout = start_ccb->ccb_h.timeout * 10;
3008 sim = start_ccb->ccb_h.path->bus->sim;
3010 dev = start_ccb->ccb_h.path->device;
3012 mtx_assert(sim->mtx, MA_OWNED);
3014 /* Don't use ISR for this SIM while polling. */
3015 sim->flags |= CAM_SIM_POLLED;
3018 * Steal an opening so that no other queued requests
3019 * can get it before us while we simulate interrupts.
3021 dev->ccbq.devq_openings--;
3022 dev->ccbq.dev_openings--;
3024 while(((devq != NULL && devq->send_openings <= 0) ||
3025 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3027 (*(sim->sim_poll))(sim);
3028 camisr_runqueue(&sim->sim_doneq);
3031 dev->ccbq.devq_openings++;
3032 dev->ccbq.dev_openings++;
3035 xpt_action(start_ccb);
3036 while(--timeout > 0) {
3037 (*(sim->sim_poll))(sim);
3038 camisr_runqueue(&sim->sim_doneq);
3039 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3046 * XXX Is it worth adding a sim_timeout entry
3047 * point so we can attempt recovery? If
3048 * this is only used for dumps, I don't think
3051 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3054 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3057 /* We will use CAM ISR for this SIM again. */
3058 sim->flags &= ~CAM_SIM_POLLED;
3062 * Schedule a peripheral driver to receive a ccb when it's
3063 * target device has space for more transactions.
3066 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3068 struct cam_ed *device;
3071 mtx_assert(perph->sim->mtx, MA_OWNED);
3073 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3074 device = perph->path->device;
3075 if (periph_is_queued(perph)) {
3076 /* Simply reorder based on new priority */
3077 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3078 (" change priority to %d\n", new_priority));
3079 if (new_priority < perph->pinfo.priority) {
3080 camq_change_priority(&device->drvq,
3083 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3086 /* New entry on the queue */
3087 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3088 (" added periph to queue\n"));
3089 perph->pinfo.priority = new_priority;
3090 perph->pinfo.generation = ++device->drvq.generation;
3091 camq_insert(&device->drvq, &perph->pinfo);
3092 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3095 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3096 (" calling xpt_run_devq\n"));
3097 xpt_run_dev_allocq(perph->path->bus);
3103 * Schedule a device to run on a given queue.
3104 * If the device was inserted as a new entry on the queue,
3105 * return 1 meaning the device queue should be run. If we
3106 * were already queued, implying someone else has already
3107 * started the queue, return 0 so the caller doesn't attempt
3111 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3112 u_int32_t new_priority)
3115 u_int32_t old_priority;
3117 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3119 old_priority = pinfo->priority;
3122 * Are we already queued?
3124 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3125 /* Simply reorder based on new priority */
3126 if (new_priority < old_priority) {
3127 camq_change_priority(queue, pinfo->index,
3129 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3130 ("changed priority to %d\n",
3136 /* New entry on the queue */
3137 if (new_priority < old_priority)
3138 pinfo->priority = new_priority;
3140 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3141 ("Inserting onto queue\n"));
3142 pinfo->generation = ++queue->generation;
3143 camq_insert(queue, pinfo);
3150 xpt_run_dev_allocq(struct cam_eb *bus)
3152 struct cam_devq *devq;
3154 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3155 devq = bus->sim->devq;
3157 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3158 (" qfrozen_cnt == 0x%x, entries == %d, "
3159 "openings == %d, active == %d\n",
3160 devq->alloc_queue.qfrozen_cnt[0],
3161 devq->alloc_queue.entries,
3162 devq->alloc_openings,
3163 devq->alloc_active));
3165 devq->alloc_queue.qfrozen_cnt[0]++;
3166 while ((devq->alloc_queue.entries > 0)
3167 && (devq->alloc_openings > 0)
3168 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3169 struct cam_ed_qinfo *qinfo;
3170 struct cam_ed *device;
3171 union ccb *work_ccb;
3172 struct cam_periph *drv;
3175 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3177 device = qinfo->device;
3178 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3179 ("running device %p\n", device));
3181 drvq = &device->drvq;
3182 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3183 "Device on queue without any work to do"));
3184 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3185 devq->alloc_openings--;
3186 devq->alloc_active++;
3187 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3188 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3189 drv->pinfo.priority);
3190 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3191 ("calling periph start\n"));
3192 drv->periph_start(drv, work_ccb);
3195 * Malloc failure in alloc_ccb
3198 * XXX add us to a list to be run from free_ccb
3199 * if we don't have any ccbs active on this
3200 * device queue otherwise we may never get run
3206 /* We may have more work. Attempt to reschedule. */
3207 xpt_schedule_dev_allocq(bus, device);
3209 devq->alloc_queue.qfrozen_cnt[0]--;
3213 xpt_run_dev_sendq(struct cam_eb *bus)
3215 struct cam_devq *devq;
3217 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3219 devq = bus->sim->devq;
3221 devq->send_queue.qfrozen_cnt[0]++;
3222 while ((devq->send_queue.entries > 0)
3223 && (devq->send_openings > 0)
3224 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3225 struct cam_ed_qinfo *qinfo;
3226 struct cam_ed *device;
3227 union ccb *work_ccb;
3228 struct cam_sim *sim;
3230 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3232 device = qinfo->device;
3233 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3234 ("running device %p\n", device));
3236 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3237 if (work_ccb == NULL) {
3238 printf("device on run queue with no ccbs???\n");
3242 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3244 mtx_lock(&xsoftc.xpt_lock);
3245 if (xsoftc.num_highpower <= 0) {
3247 * We got a high power command, but we
3248 * don't have any available slots. Freeze
3249 * the device queue until we have a slot
3252 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3253 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3257 mtx_unlock(&xsoftc.xpt_lock);
3261 * Consume a high power slot while
3264 xsoftc.num_highpower--;
3266 mtx_unlock(&xsoftc.xpt_lock);
3268 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3269 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3271 devq->send_openings--;
3272 devq->send_active++;
3274 xpt_schedule_dev_sendq(bus, device);
3276 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3278 * The client wants to freeze the queue
3279 * after this CCB is sent.
3281 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3284 /* In Target mode, the peripheral driver knows best... */
3285 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3286 if ((device->inq_flags & SID_CmdQue) != 0
3287 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3288 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3291 * Clear this in case of a retried CCB that
3292 * failed due to a rejected tag.
3294 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3298 * Device queues can be shared among multiple sim instances
3299 * that reside on different busses. Use the SIM in the queue
3300 * CCB's path, rather than the one in the bus that was passed
3301 * into this function.
3303 sim = work_ccb->ccb_h.path->bus->sim;
3304 (*(sim->sim_action))(sim, work_ccb);
3306 devq->send_queue.qfrozen_cnt[0]--;
3310 * This function merges stuff from the slave ccb into the master ccb, while
3311 * keeping important fields in the master ccb constant.
3314 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3318 * Pull fields that are valid for peripheral drivers to set
3319 * into the master CCB along with the CCB "payload".
3321 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3322 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3323 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3324 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3325 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3326 sizeof(union ccb) - sizeof(struct ccb_hdr));
3330 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3333 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3334 ccb_h->pinfo.priority = priority;
3336 ccb_h->path_id = path->bus->path_id;
3338 ccb_h->target_id = path->target->target_id;
3340 ccb_h->target_id = CAM_TARGET_WILDCARD;
3342 ccb_h->target_lun = path->device->lun_id;
3343 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3345 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3347 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3351 /* Path manipulation functions */
3353 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3354 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3356 struct cam_path *path;
3359 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3362 status = CAM_RESRC_UNAVAIL;
3365 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3366 if (status != CAM_REQ_CMP) {
3367 free(path, M_CAMXPT);
3370 *new_path_ptr = path;
3375 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3376 struct cam_periph *periph, path_id_t path_id,
3377 target_id_t target_id, lun_id_t lun_id)
3379 struct cam_path *path;
3380 struct cam_eb *bus = NULL;
3382 int need_unlock = 0;
3384 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3386 if (path_id != CAM_BUS_WILDCARD) {
3387 bus = xpt_find_bus(path_id);
3390 CAM_SIM_LOCK(bus->sim);
3393 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3395 CAM_SIM_UNLOCK(bus->sim);
3396 xpt_release_bus(bus);
3398 if (status != CAM_REQ_CMP) {
3399 free(path, M_CAMXPT);
3402 *new_path_ptr = path;
3407 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3408 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3411 struct cam_et *target;
3412 struct cam_ed *device;
3415 status = CAM_REQ_CMP; /* Completed without error */
3416 target = NULL; /* Wildcarded */
3417 device = NULL; /* Wildcarded */
3420 * We will potentially modify the EDT, so block interrupts
3421 * that may attempt to create cam paths.
3423 bus = xpt_find_bus(path_id);
3425 status = CAM_PATH_INVALID;
3427 target = xpt_find_target(bus, target_id);
3428 if (target == NULL) {
3430 struct cam_et *new_target;
3432 new_target = xpt_alloc_target(bus, target_id);
3433 if (new_target == NULL) {
3434 status = CAM_RESRC_UNAVAIL;
3436 target = new_target;
3439 if (target != NULL) {
3440 device = xpt_find_device(target, lun_id);
3441 if (device == NULL) {
3443 struct cam_ed *new_device;
3446 (*(bus->xport->alloc_device))(bus,
3449 if (new_device == NULL) {
3450 status = CAM_RESRC_UNAVAIL;
3452 device = new_device;
3459 * Only touch the user's data if we are successful.
3461 if (status == CAM_REQ_CMP) {
3462 new_path->periph = perph;
3463 new_path->bus = bus;
3464 new_path->target = target;
3465 new_path->device = device;
3466 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3469 xpt_release_device(device);
3471 xpt_release_target(target);
3473 xpt_release_bus(bus);
3479 xpt_release_path(struct cam_path *path)
3481 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3482 if (path->device != NULL) {
3483 xpt_release_device(path->device);
3484 path->device = NULL;
3486 if (path->target != NULL) {
3487 xpt_release_target(path->target);
3488 path->target = NULL;
3490 if (path->bus != NULL) {
3491 xpt_release_bus(path->bus);
3497 xpt_free_path(struct cam_path *path)
3500 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3501 xpt_release_path(path);
3502 free(path, M_CAMXPT);
3506 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3507 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3510 mtx_lock(&xsoftc.xpt_topo_lock);
3513 *bus_ref = path->bus->refcount;
3517 mtx_unlock(&xsoftc.xpt_topo_lock);
3520 *periph_ref = path->periph->refcount;
3526 *target_ref = path->target->refcount;
3532 *device_ref = path->device->refcount;
3539 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3540 * in path1, 2 for match with wildcards in path2.
3543 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3547 if (path1->bus != path2->bus) {
3548 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3550 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3555 if (path1->target != path2->target) {
3556 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3559 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3564 if (path1->device != path2->device) {
3565 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3568 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3577 xpt_print_path(struct cam_path *path)
3581 printf("(nopath): ");
3583 if (path->periph != NULL)
3584 printf("(%s%d:", path->periph->periph_name,
3585 path->periph->unit_number);
3587 printf("(noperiph:");
3589 if (path->bus != NULL)
3590 printf("%s%d:%d:", path->bus->sim->sim_name,
3591 path->bus->sim->unit_number,
3592 path->bus->sim->bus_id);
3596 if (path->target != NULL)
3597 printf("%d:", path->target->target_id);
3601 if (path->device != NULL)
3602 printf("%d): ", path->device->lun_id);
3609 xpt_print(struct cam_path *path, const char *fmt, ...)
3612 xpt_print_path(path);
3619 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3624 if (path != NULL && path->bus != NULL)
3625 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3628 sbuf_new(&sb, str, str_len, 0);
3631 sbuf_printf(&sb, "(nopath): ");
3633 if (path->periph != NULL)
3634 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3635 path->periph->unit_number);
3637 sbuf_printf(&sb, "(noperiph:");
3639 if (path->bus != NULL)
3640 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3641 path->bus->sim->unit_number,
3642 path->bus->sim->bus_id);
3644 sbuf_printf(&sb, "nobus:");
3646 if (path->target != NULL)
3647 sbuf_printf(&sb, "%d:", path->target->target_id);
3649 sbuf_printf(&sb, "X:");
3651 if (path->device != NULL)
3652 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3654 sbuf_printf(&sb, "X): ");
3658 return(sbuf_len(&sb));
3662 xpt_path_path_id(struct cam_path *path)
3664 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3666 return(path->bus->path_id);
3670 xpt_path_target_id(struct cam_path *path)
3672 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3674 if (path->target != NULL)
3675 return (path->target->target_id);
3677 return (CAM_TARGET_WILDCARD);
3681 xpt_path_lun_id(struct cam_path *path)
3683 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3685 if (path->device != NULL)
3686 return (path->device->lun_id);
3688 return (CAM_LUN_WILDCARD);
3692 xpt_path_sim(struct cam_path *path)
3695 return (path->bus->sim);
3699 xpt_path_periph(struct cam_path *path)
3701 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3703 return (path->periph);
3707 xpt_path_legacy_ata_id(struct cam_path *path)
3712 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3713 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3714 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3715 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3718 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3719 path->bus->sim->unit_number < 2) {
3720 bus_id = path->bus->sim->unit_number;
3724 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3725 if (bus == path->bus)
3727 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3728 bus->sim->unit_number >= 2) ||
3729 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3730 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3731 strcmp(bus->sim->sim_name, "siisch") == 0)
3736 if (path->target != NULL) {
3737 if (path->target->target_id < 2)
3738 return (bus_id * 2 + path->target->target_id);
3742 return (bus_id * 2);
3746 * Release a CAM control block for the caller. Remit the cost of the structure
3747 * to the device referenced by the path. If the this device had no 'credits'
3748 * and peripheral drivers have registered async callbacks for this notification
3752 xpt_release_ccb(union ccb *free_ccb)
3754 struct cam_path *path;
3755 struct cam_ed *device;
3757 struct cam_sim *sim;
3759 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3760 path = free_ccb->ccb_h.path;
3761 device = path->device;
3765 mtx_assert(sim->mtx, MA_OWNED);
3767 cam_ccbq_release_opening(&device->ccbq);
3768 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3769 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3770 cam_ccbq_resize(&device->ccbq,
3771 device->ccbq.dev_openings + device->ccbq.dev_active);
3773 if (sim->ccb_count > sim->max_ccbs) {
3774 xpt_free_ccb(free_ccb);
3777 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3780 if (sim->devq == NULL) {
3783 sim->devq->alloc_openings++;
3784 sim->devq->alloc_active--;
3785 if (device_is_alloc_queued(device) == 0)
3786 xpt_schedule_dev_allocq(bus, device);
3787 xpt_run_dev_allocq(bus);
3790 /* Functions accessed by SIM drivers */
3792 static struct xpt_xport xport_default = {
3793 .alloc_device = xpt_alloc_device_default,
3794 .action = xpt_action_default,
3795 .async = xpt_dev_async_default,
3799 * A sim structure, listing the SIM entry points and instance
3800 * identification info is passed to xpt_bus_register to hook the SIM
3801 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3802 * for this new bus and places it in the array of busses and assigns
3803 * it a path_id. The path_id may be influenced by "hard wiring"
3804 * information specified by the user. Once interrupt services are
3805 * available, the bus will be probed.
3808 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3810 struct cam_eb *new_bus;
3811 struct cam_eb *old_bus;
3812 struct ccb_pathinq cpi;
3813 struct cam_path *path;
3816 mtx_assert(sim->mtx, MA_OWNED);
3819 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3820 M_CAMXPT, M_NOWAIT);
3821 if (new_bus == NULL) {
3822 /* Couldn't satisfy request */
3823 return (CAM_RESRC_UNAVAIL);
3825 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3827 free(new_bus, M_CAMXPT);
3828 return (CAM_RESRC_UNAVAIL);
3831 if (strcmp(sim->sim_name, "xpt") != 0) {
3833 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3836 TAILQ_INIT(&new_bus->et_entries);
3837 new_bus->path_id = sim->path_id;
3840 timevalclear(&new_bus->last_reset);
3842 new_bus->refcount = 1; /* Held until a bus_deregister event */
3843 new_bus->generation = 0;
3845 mtx_lock(&xsoftc.xpt_topo_lock);
3846 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3847 while (old_bus != NULL
3848 && old_bus->path_id < new_bus->path_id)
3849 old_bus = TAILQ_NEXT(old_bus, links);
3850 if (old_bus != NULL)
3851 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3853 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3854 xsoftc.bus_generation++;
3855 mtx_unlock(&xsoftc.xpt_topo_lock);
3858 * Set a default transport so that a PATH_INQ can be issued to
3859 * the SIM. This will then allow for probing and attaching of
3860 * a more appropriate transport.
3862 new_bus->xport = &xport_default;
3864 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3865 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3866 if (status != CAM_REQ_CMP)
3867 printf("xpt_compile_path returned %d\n", status);
3869 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3870 cpi.ccb_h.func_code = XPT_PATH_INQ;
3871 xpt_action((union ccb *)&cpi);
3873 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3874 switch (cpi.transport) {
3881 new_bus->xport = scsi_get_xport();
3885 new_bus->xport = ata_get_xport();
3888 new_bus->xport = &xport_default;
3893 /* Notify interested parties */
3894 if (sim->path_id != CAM_XPT_PATH_ID) {
3895 union ccb *scan_ccb;
3897 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3898 /* Initiate bus rescan. */
3899 scan_ccb = xpt_alloc_ccb_nowait();
3900 scan_ccb->ccb_h.path = path;
3901 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3902 scan_ccb->crcn.flags = 0;
3903 xpt_rescan(scan_ccb);
3905 xpt_free_path(path);
3906 return (CAM_SUCCESS);
3910 xpt_bus_deregister(path_id_t pathid)
3912 struct cam_path bus_path;
3915 status = xpt_compile_path(&bus_path, NULL, pathid,
3916 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3917 if (status != CAM_REQ_CMP)
3920 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3921 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3923 /* Release the reference count held while registered. */
3924 xpt_release_bus(bus_path.bus);
3925 xpt_release_path(&bus_path);
3927 return (CAM_REQ_CMP);
3931 xptnextfreepathid(void)
3938 mtx_lock(&xsoftc.xpt_topo_lock);
3939 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3941 /* Find an unoccupied pathid */
3942 while (bus != NULL && bus->path_id <= pathid) {
3943 if (bus->path_id == pathid)
3945 bus = TAILQ_NEXT(bus, links);
3947 mtx_unlock(&xsoftc.xpt_topo_lock);
3950 * Ensure that this pathid is not reserved for
3951 * a bus that may be registered in the future.
3953 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3955 /* Start the search over */
3956 mtx_lock(&xsoftc.xpt_topo_lock);
3963 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3970 pathid = CAM_XPT_PATH_ID;
3971 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3973 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3974 if (strcmp(dname, "scbus")) {
3975 /* Avoid a bit of foot shooting. */
3978 if (dunit < 0) /* unwired?! */
3980 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3981 if (sim_bus == val) {
3985 } else if (sim_bus == 0) {
3986 /* Unspecified matches bus 0 */
3990 printf("Ambiguous scbus configuration for %s%d "
3991 "bus %d, cannot wire down. The kernel "
3992 "config entry for scbus%d should "
3993 "specify a controller bus.\n"
3994 "Scbus will be assigned dynamically.\n",
3995 sim_name, sim_unit, sim_bus, dunit);
4000 if (pathid == CAM_XPT_PATH_ID)
4001 pathid = xptnextfreepathid();
4006 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4009 struct cam_et *target, *next_target;
4010 struct cam_ed *device, *next_device;
4012 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4014 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4017 * Most async events come from a CAM interrupt context. In
4018 * a few cases, the error recovery code at the peripheral layer,
4019 * which may run from our SWI or a process context, may signal
4020 * deferred events with a call to xpt_async.
4025 if (async_code == AC_BUS_RESET) {
4026 /* Update our notion of when the last reset occurred */
4027 microtime(&bus->last_reset);
4030 for (target = TAILQ_FIRST(&bus->et_entries);
4032 target = next_target) {
4034 next_target = TAILQ_NEXT(target, links);
4036 if (path->target != target
4037 && path->target->target_id != CAM_TARGET_WILDCARD
4038 && target->target_id != CAM_TARGET_WILDCARD)
4041 if (async_code == AC_SENT_BDR) {
4042 /* Update our notion of when the last reset occurred */
4043 microtime(&path->target->last_reset);
4046 for (device = TAILQ_FIRST(&target->ed_entries);
4048 device = next_device) {
4050 next_device = TAILQ_NEXT(device, links);
4052 if (path->device != device
4053 && path->device->lun_id != CAM_LUN_WILDCARD
4054 && device->lun_id != CAM_LUN_WILDCARD)
4057 * The async callback could free the device.
4058 * If it is a broadcast async, it doesn't hold
4059 * device reference, so take our own reference.
4061 xpt_acquire_device(device);
4062 (*(bus->xport->async))(async_code, bus,
4066 xpt_async_bcast(&device->asyncs, async_code,
4068 xpt_release_device(device);
4073 * If this wasn't a fully wildcarded async, tell all
4074 * clients that want all async events.
4076 if (bus != xpt_periph->path->bus)
4077 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4082 xpt_async_bcast(struct async_list *async_head,
4083 u_int32_t async_code,
4084 struct cam_path *path, void *async_arg)
4086 struct async_node *cur_entry;
4088 cur_entry = SLIST_FIRST(async_head);
4089 while (cur_entry != NULL) {
4090 struct async_node *next_entry;
4092 * Grab the next list entry before we call the current
4093 * entry's callback. This is because the callback function
4094 * can delete its async callback entry.
4096 next_entry = SLIST_NEXT(cur_entry, links);
4097 if ((cur_entry->event_enable & async_code) != 0)
4098 cur_entry->callback(cur_entry->callback_arg,
4101 cur_entry = next_entry;
4106 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4107 struct cam_et *target, struct cam_ed *device,
4110 printf("%s called\n", __func__);
4114 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4116 struct cam_ed *dev = path->device;
4118 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4119 dev->sim->devq->alloc_openings +=
4120 cam_ccbq_freeze(&dev->ccbq, rl, count);
4121 /* Remove frozen device from allocq. */
4122 if (device_is_alloc_queued(dev) &&
4123 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4124 CAMQ_GET_PRIO(&dev->drvq)))) {
4125 camq_remove(&dev->sim->devq->alloc_queue,
4126 dev->alloc_ccb_entry.pinfo.index);
4128 /* Remove frozen device from sendq. */
4129 if (device_is_send_queued(dev) &&
4130 cam_ccbq_frozen_top(&dev->ccbq)) {
4131 camq_remove(&dev->sim->devq->send_queue,
4132 dev->send_ccb_entry.pinfo.index);
4134 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4138 xpt_freeze_devq(struct cam_path *path, u_int count)
4141 return (xpt_freeze_devq_rl(path, 0, count));
4145 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4148 mtx_assert(sim->mtx, MA_OWNED);
4149 sim->devq->send_queue.qfrozen_cnt[0] += count;
4150 return (sim->devq->send_queue.qfrozen_cnt[0]);
4154 xpt_release_devq_timeout(void *arg)
4156 struct cam_ed *device;
4158 device = (struct cam_ed *)arg;
4160 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4164 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4166 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4168 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4172 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4174 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4176 xpt_release_devq_device(path->device, rl, count, run_queue);
4180 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4183 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4185 printf("xpt_release_devq(%d): requested %u > present %u\n",
4186 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4188 count = dev->ccbq.queue.qfrozen_cnt[rl];
4190 dev->sim->devq->alloc_openings -=
4191 cam_ccbq_release(&dev->ccbq, rl, count);
4192 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4193 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4194 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4195 xpt_run_dev_allocq(dev->target->bus);
4197 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4199 * No longer need to wait for a successful
4200 * command completion.
4202 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4204 * Remove any timeouts that might be scheduled
4205 * to release this queue.
4207 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4208 callout_stop(&dev->callout);
4209 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4214 * Now that we are unfrozen schedule the
4215 * device so any pending transactions are
4218 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4219 xpt_run_dev_sendq(dev->target->bus);
4224 xpt_release_simq(struct cam_sim *sim, int run_queue)
4228 mtx_assert(sim->mtx, MA_OWNED);
4229 sendq = &(sim->devq->send_queue);
4230 if (sendq->qfrozen_cnt[0] <= 0) {
4232 printf("xpt_release_simq: requested 1 > present %u\n",
4233 sendq->qfrozen_cnt[0]);
4236 sendq->qfrozen_cnt[0]--;
4237 if (sendq->qfrozen_cnt[0] == 0) {
4239 * If there is a timeout scheduled to release this
4240 * sim queue, remove it. The queue frozen count is
4243 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4244 callout_stop(&sim->callout);
4245 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4251 * Now that we are unfrozen run the send queue.
4253 bus = xpt_find_bus(sim->path_id);
4254 xpt_run_dev_sendq(bus);
4255 xpt_release_bus(bus);
4261 * XXX Appears to be unused.
4264 xpt_release_simq_timeout(void *arg)
4266 struct cam_sim *sim;
4268 sim = (struct cam_sim *)arg;
4269 xpt_release_simq(sim, /* run_queue */ TRUE);
4273 xpt_done(union ccb *done_ccb)
4275 struct cam_sim *sim;
4278 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4279 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4281 * Queue up the request for handling by our SWI handler
4282 * any of the "non-immediate" type of ccbs.
4284 sim = done_ccb->ccb_h.path->bus->sim;
4285 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4287 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4288 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4289 CAM_SIM_BATCH)) == 0) {
4290 mtx_lock(&cam_simq_lock);
4291 first = TAILQ_EMPTY(&cam_simq);
4292 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4293 mtx_unlock(&cam_simq_lock);
4294 sim->flags |= CAM_SIM_ON_DONEQ;
4296 swi_sched(cambio_ih, 0);
4302 xpt_batch_start(struct cam_sim *sim)
4305 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4306 sim->flags |= CAM_SIM_BATCH;
4310 xpt_batch_done(struct cam_sim *sim)
4313 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4314 sim->flags &= ~CAM_SIM_BATCH;
4315 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4316 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4317 camisr_runqueue(&sim->sim_doneq);
4325 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4330 xpt_alloc_ccb_nowait()
4334 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4339 xpt_free_ccb(union ccb *free_ccb)
4341 free(free_ccb, M_CAMXPT);
4346 /* Private XPT functions */
4349 * Get a CAM control block for the caller. Charge the structure to the device
4350 * referenced by the path. If the this device has no 'credits' then the
4351 * device already has the maximum number of outstanding operations under way
4352 * and we return NULL. If we don't have sufficient resources to allocate more
4353 * ccbs, we also return NULL.
4356 xpt_get_ccb(struct cam_ed *device)
4359 struct cam_sim *sim;
4362 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4363 new_ccb = xpt_alloc_ccb_nowait();
4364 if (new_ccb == NULL) {
4367 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4368 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4369 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4373 cam_ccbq_take_opening(&device->ccbq);
4374 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4379 xpt_release_bus(struct cam_eb *bus)
4382 mtx_lock(&xsoftc.xpt_topo_lock);
4383 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4384 if ((--bus->refcount == 0)
4385 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4386 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4387 xsoftc.bus_generation++;
4388 mtx_unlock(&xsoftc.xpt_topo_lock);
4389 cam_sim_release(bus->sim);
4390 free(bus, M_CAMXPT);
4392 mtx_unlock(&xsoftc.xpt_topo_lock);
4395 static struct cam_et *
4396 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4398 struct cam_et *target;
4400 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4401 if (target != NULL) {
4402 struct cam_et *cur_target;
4404 TAILQ_INIT(&target->ed_entries);
4406 target->target_id = target_id;
4407 target->refcount = 1;
4408 target->generation = 0;
4409 target->luns = NULL;
4410 timevalclear(&target->last_reset);
4412 * Hold a reference to our parent bus so it
4413 * will not go away before we do.
4415 mtx_lock(&xsoftc.xpt_topo_lock);
4417 mtx_unlock(&xsoftc.xpt_topo_lock);
4419 /* Insertion sort into our bus's target list */
4420 cur_target = TAILQ_FIRST(&bus->et_entries);
4421 while (cur_target != NULL && cur_target->target_id < target_id)
4422 cur_target = TAILQ_NEXT(cur_target, links);
4424 if (cur_target != NULL) {
4425 TAILQ_INSERT_BEFORE(cur_target, target, links);
4427 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4435 xpt_release_target(struct cam_et *target)
4438 if (target->refcount == 1) {
4439 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4440 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4441 target->bus->generation++;
4442 xpt_release_bus(target->bus);
4444 free(target->luns, M_CAMXPT);
4445 free(target, M_CAMXPT);
4451 static struct cam_ed *
4452 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4455 struct cam_ed *device, *cur_device;
4457 device = xpt_alloc_device(bus, target, lun_id);
4461 device->mintags = 1;
4462 device->maxtags = 1;
4463 bus->sim->max_ccbs += device->ccbq.devq_openings;
4464 cur_device = TAILQ_FIRST(&target->ed_entries);
4465 while (cur_device != NULL && cur_device->lun_id < lun_id)
4466 cur_device = TAILQ_NEXT(cur_device, links);
4467 if (cur_device != NULL) {
4468 TAILQ_INSERT_BEFORE(cur_device, device, links);
4470 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4472 target->generation++;
4478 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4480 struct cam_ed *device;
4481 struct cam_devq *devq;
4484 /* Make space for us in the device queue on our bus */
4485 devq = bus->sim->devq;
4486 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4488 if (status != CAM_REQ_CMP) {
4491 device = (struct cam_ed *)malloc(sizeof(*device),
4492 M_CAMXPT, M_NOWAIT);
4495 if (device != NULL) {
4496 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4497 device->alloc_ccb_entry.device = device;
4498 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4499 device->send_ccb_entry.device = device;
4500 device->target = target;
4501 device->lun_id = lun_id;
4502 device->sim = bus->sim;
4503 /* Initialize our queues */
4504 if (camq_init(&device->drvq, 0) != 0) {
4505 free(device, M_CAMXPT);
4508 if (cam_ccbq_init(&device->ccbq,
4509 bus->sim->max_dev_openings) != 0) {
4510 camq_fini(&device->drvq);
4511 free(device, M_CAMXPT);
4514 SLIST_INIT(&device->asyncs);
4515 SLIST_INIT(&device->periphs);
4516 device->generation = 0;
4517 device->owner = NULL;
4518 device->flags = CAM_DEV_UNCONFIGURED;
4519 device->tag_delay_count = 0;
4520 device->tag_saved_openings = 0;
4521 device->refcount = 1;
4522 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4525 * Hold a reference to our parent target so it
4526 * will not go away before we do.
4535 xpt_acquire_device(struct cam_ed *device)
4542 xpt_release_device(struct cam_ed *device)
4545 if (device->refcount == 1) {
4546 struct cam_devq *devq;
4548 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4549 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4550 panic("Removing device while still queued for ccbs");
4552 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4553 callout_stop(&device->callout);
4555 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4556 device->target->generation++;
4557 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4558 /* Release our slot in the devq */
4559 devq = device->target->bus->sim->devq;
4560 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4561 camq_fini(&device->drvq);
4562 cam_ccbq_fini(&device->ccbq);
4563 xpt_release_target(device->target);
4564 free(device, M_CAMXPT);
4570 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4578 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4579 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4580 if (result == CAM_REQ_CMP && (diff < 0)) {
4581 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4583 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4584 || (dev->inq_flags & SID_CmdQue) != 0)
4585 dev->tag_saved_openings = newopenings;
4586 /* Adjust the global limit */
4587 dev->sim->max_ccbs += diff;
4591 static struct cam_eb *
4592 xpt_find_bus(path_id_t path_id)
4596 mtx_lock(&xsoftc.xpt_topo_lock);
4597 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4599 bus = TAILQ_NEXT(bus, links)) {
4600 if (bus->path_id == path_id) {
4605 mtx_unlock(&xsoftc.xpt_topo_lock);
4609 static struct cam_et *
4610 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4612 struct cam_et *target;
4614 for (target = TAILQ_FIRST(&bus->et_entries);
4616 target = TAILQ_NEXT(target, links)) {
4617 if (target->target_id == target_id) {
4625 static struct cam_ed *
4626 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4628 struct cam_ed *device;
4630 for (device = TAILQ_FIRST(&target->ed_entries);
4632 device = TAILQ_NEXT(device, links)) {
4633 if (device->lun_id == lun_id) {
4642 xpt_start_tags(struct cam_path *path)
4644 struct ccb_relsim crs;
4645 struct cam_ed *device;
4646 struct cam_sim *sim;
4649 device = path->device;
4650 sim = path->bus->sim;
4651 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4652 xpt_freeze_devq(path, /*count*/1);
4653 device->inq_flags |= SID_CmdQue;
4654 if (device->tag_saved_openings != 0)
4655 newopenings = device->tag_saved_openings;
4657 newopenings = min(device->maxtags,
4658 sim->max_tagged_dev_openings);
4659 xpt_dev_ccbq_resize(path, newopenings);
4660 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4661 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4662 crs.ccb_h.func_code = XPT_REL_SIMQ;
4663 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4665 = crs.release_timeout
4668 xpt_action((union ccb *)&crs);
4672 xpt_stop_tags(struct cam_path *path)
4674 struct ccb_relsim crs;
4675 struct cam_ed *device;
4676 struct cam_sim *sim;
4678 device = path->device;
4679 sim = path->bus->sim;
4680 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4681 device->tag_delay_count = 0;
4682 xpt_freeze_devq(path, /*count*/1);
4683 device->inq_flags &= ~SID_CmdQue;
4684 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4685 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4686 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4687 crs.ccb_h.func_code = XPT_REL_SIMQ;
4688 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4690 = crs.release_timeout
4693 xpt_action((union ccb *)&crs);
4697 xpt_boot_delay(void *arg)
4704 xpt_config(void *arg)
4707 * Now that interrupts are enabled, go find our devices
4711 /* Setup debugging flags and path */
4712 #ifdef CAM_DEBUG_BUS
4713 if (cam_dflags != CAM_DEBUG_NONE) {
4715 * Locking is specifically omitted here. No SIMs have
4716 * registered yet, so xpt_create_path will only be searching
4717 * empty lists of targets and devices.
4719 if (xpt_create_path(&cam_dpath, xpt_periph,
4720 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4721 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4722 printf("xpt_config: xpt_create_path() failed for debug"
4723 " target %d:%d:%d, debugging disabled\n",
4724 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4725 cam_dflags = CAM_DEBUG_NONE;
4729 #else /* !CAM_DEBUG_BUS */
4731 #endif /* CAM_DEBUG_BUS */
4732 #endif /* CAMDEBUG */
4734 periphdriver_init(1);
4736 callout_init(&xsoftc.boot_callout, 1);
4737 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4738 xpt_boot_delay, NULL);
4739 /* Fire up rescan thread. */
4740 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4741 printf("xpt_init: failed to create rescan thread\n");
4749 xsoftc.buses_to_config++;
4754 xpt_release_boot(void)
4757 xsoftc.buses_to_config--;
4758 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4759 struct xpt_task *task;
4761 xsoftc.buses_config_done = 1;
4763 /* Call manually because we don't have any busses */
4764 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4766 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4767 taskqueue_enqueue(taskqueue_thread, &task->task);
4774 * If the given device only has one peripheral attached to it, and if that
4775 * peripheral is the passthrough driver, announce it. This insures that the
4776 * user sees some sort of announcement for every peripheral in their system.
4779 xptpassannouncefunc(struct cam_ed *device, void *arg)
4781 struct cam_periph *periph;
4784 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4785 periph = SLIST_NEXT(periph, periph_links), i++);
4787 periph = SLIST_FIRST(&device->periphs);
4789 && (strncmp(periph->periph_name, "pass", 4) == 0))
4790 xpt_announce_periph(periph, NULL);
4796 xpt_finishconfig_task(void *context, int pending)
4799 periphdriver_init(2);
4801 * Check for devices with no "standard" peripheral driver
4802 * attached. For any devices like that, announce the
4803 * passthrough driver so the user will see something.
4805 xpt_for_all_devices(xptpassannouncefunc, NULL);
4807 /* Release our hook so that the boot can continue. */
4808 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4809 free(xsoftc.xpt_config_hook, M_CAMXPT);
4810 xsoftc.xpt_config_hook = NULL;
4812 free(context, M_CAMXPT);
4816 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4817 struct cam_path *path)
4819 struct ccb_setasync csa;
4824 mtx_lock(&xsoftc.xpt_lock);
4825 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4826 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4827 if (status != CAM_REQ_CMP) {
4828 mtx_unlock(&xsoftc.xpt_lock);
4834 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4835 csa.ccb_h.func_code = XPT_SASYNC_CB;
4836 csa.event_enable = event;
4837 csa.callback = cbfunc;
4838 csa.callback_arg = cbarg;
4839 xpt_action((union ccb *)&csa);
4840 status = csa.ccb_h.status;
4842 xpt_free_path(path);
4843 mtx_unlock(&xsoftc.xpt_lock);
4845 if ((status == CAM_REQ_CMP) &&
4846 (csa.event_enable & AC_FOUND_DEVICE)) {
4848 * Get this peripheral up to date with all
4849 * the currently existing devices.
4851 xpt_for_all_devices(xptsetasyncfunc, &csa);
4853 if ((status == CAM_REQ_CMP) &&
4854 (csa.event_enable & AC_PATH_REGISTERED)) {
4856 * Get this peripheral up to date with all
4857 * the currently existing busses.
4859 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4866 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4868 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4870 switch (work_ccb->ccb_h.func_code) {
4871 /* Common cases first */
4872 case XPT_PATH_INQ: /* Path routing inquiry */
4874 struct ccb_pathinq *cpi;
4876 cpi = &work_ccb->cpi;
4877 cpi->version_num = 1; /* XXX??? */
4878 cpi->hba_inquiry = 0;
4879 cpi->target_sprt = 0;
4881 cpi->hba_eng_cnt = 0;
4882 cpi->max_target = 0;
4884 cpi->initiator_id = 0;
4885 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4886 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4887 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4888 cpi->unit_number = sim->unit_number;
4889 cpi->bus_id = sim->bus_id;
4890 cpi->base_transfer_speed = 0;
4891 cpi->protocol = PROTO_UNSPECIFIED;
4892 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4893 cpi->transport = XPORT_UNSPECIFIED;
4894 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4895 cpi->ccb_h.status = CAM_REQ_CMP;
4900 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4907 * The xpt as a "controller" has no interrupt sources, so polling
4911 xptpoll(struct cam_sim *sim)
4916 xpt_lock_buses(void)
4918 mtx_lock(&xsoftc.xpt_topo_lock);
4922 xpt_unlock_buses(void)
4924 mtx_unlock(&xsoftc.xpt_topo_lock);
4931 struct cam_sim *sim;
4933 mtx_lock(&cam_simq_lock);
4935 while (!TAILQ_EMPTY(&cam_simq)) {
4936 TAILQ_CONCAT(&queue, &cam_simq, links);
4937 mtx_unlock(&cam_simq_lock);
4939 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4940 TAILQ_REMOVE(&queue, sim, links);
4942 sim->flags &= ~CAM_SIM_ON_DONEQ;
4943 camisr_runqueue(&sim->sim_doneq);
4944 CAM_SIM_UNLOCK(sim);
4946 mtx_lock(&cam_simq_lock);
4948 mtx_unlock(&cam_simq_lock);
4952 camisr_runqueue(void *V_queue)
4954 cam_isrq_t *queue = V_queue;
4955 struct ccb_hdr *ccb_h;
4957 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4960 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4961 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4963 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4968 if (ccb_h->flags & CAM_HIGH_POWER) {
4969 struct highpowerlist *hphead;
4970 union ccb *send_ccb;
4972 mtx_lock(&xsoftc.xpt_lock);
4973 hphead = &xsoftc.highpowerq;
4975 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4978 * Increment the count since this command is done.
4980 xsoftc.num_highpower++;
4983 * Any high powered commands queued up?
4985 if (send_ccb != NULL) {
4987 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4988 mtx_unlock(&xsoftc.xpt_lock);
4990 xpt_release_devq(send_ccb->ccb_h.path,
4991 /*count*/1, /*runqueue*/TRUE);
4993 mtx_unlock(&xsoftc.xpt_lock);
4996 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4999 dev = ccb_h->path->device;
5001 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5002 ccb_h->path->bus->sim->devq->send_active--;
5003 ccb_h->path->bus->sim->devq->send_openings++;
5006 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5007 && (dev->ccbq.dev_active == 0))) {
5008 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5009 xpt_release_devq(ccb_h->path, /*count*/1,
5010 /*run_queue*/FALSE);
5013 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5014 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5015 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5016 xpt_release_devq(ccb_h->path, /*count*/1,
5017 /*run_queue*/FALSE);
5020 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5021 && (--dev->tag_delay_count == 0))
5022 xpt_start_tags(ccb_h->path);
5023 if (!device_is_send_queued(dev))
5024 xpt_schedule_dev_sendq(ccb_h->path->bus, dev);
5027 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5028 xpt_release_simq(ccb_h->path->bus->sim,
5030 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5034 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5035 && (ccb_h->status & CAM_DEV_QFRZN)) {
5036 xpt_release_devq(ccb_h->path, /*count*/1,
5038 ccb_h->status &= ~CAM_DEV_QFRZN;
5040 xpt_run_dev_sendq(ccb_h->path->bus);
5043 /* Call the peripheral driver's callback */
5044 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);