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 */
192 struct cam_path *cam_dpath;
193 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
194 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
195 SYSCTL_INT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
196 &cam_dflags, 0, "Enabled debug flags");
197 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
198 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
199 SYSCTL_INT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
200 &cam_debug_delay, 0, "Delay in us after each debug message");
202 /* Our boot-time initialization hook */
203 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
205 static moduledata_t cam_moduledata = {
207 cam_module_event_handler,
211 static int xpt_init(void *);
213 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
214 MODULE_VERSION(cam, 1);
217 static void xpt_async_bcast(struct async_list *async_head,
218 u_int32_t async_code,
219 struct cam_path *path,
221 static path_id_t xptnextfreepathid(void);
222 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
223 static union ccb *xpt_get_ccb(struct cam_ed *device);
224 static void xpt_run_dev_allocq(struct cam_eb *bus);
225 static void xpt_run_dev_sendq(struct cam_eb *bus);
226 static timeout_t xpt_release_devq_timeout;
227 static void xpt_release_simq_timeout(void *arg) __unused;
228 static void xpt_release_bus(struct cam_eb *bus);
229 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
230 u_int count, int run_queue);
231 static struct cam_et*
232 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
233 static void xpt_release_target(struct cam_et *target);
234 static struct cam_eb*
235 xpt_find_bus(path_id_t path_id);
236 static struct cam_et*
237 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
238 static struct cam_ed*
239 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
240 static void xpt_config(void *arg);
241 static xpt_devicefunc_t xptpassannouncefunc;
242 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
243 static void xptpoll(struct cam_sim *sim);
244 static void camisr(void *);
245 static void camisr_runqueue(void *);
246 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
247 u_int num_patterns, struct cam_eb *bus);
248 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
250 struct cam_ed *device);
251 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
253 struct cam_periph *periph);
254 static xpt_busfunc_t xptedtbusfunc;
255 static xpt_targetfunc_t xptedttargetfunc;
256 static xpt_devicefunc_t xptedtdevicefunc;
257 static xpt_periphfunc_t xptedtperiphfunc;
258 static xpt_pdrvfunc_t xptplistpdrvfunc;
259 static xpt_periphfunc_t xptplistperiphfunc;
260 static int xptedtmatch(struct ccb_dev_match *cdm);
261 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
262 static int xptbustraverse(struct cam_eb *start_bus,
263 xpt_busfunc_t *tr_func, void *arg);
264 static int xpttargettraverse(struct cam_eb *bus,
265 struct cam_et *start_target,
266 xpt_targetfunc_t *tr_func, void *arg);
267 static int xptdevicetraverse(struct cam_et *target,
268 struct cam_ed *start_device,
269 xpt_devicefunc_t *tr_func, void *arg);
270 static int xptperiphtraverse(struct cam_ed *device,
271 struct cam_periph *start_periph,
272 xpt_periphfunc_t *tr_func, void *arg);
273 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
274 xpt_pdrvfunc_t *tr_func, void *arg);
275 static int xptpdperiphtraverse(struct periph_driver **pdrv,
276 struct cam_periph *start_periph,
277 xpt_periphfunc_t *tr_func,
279 static xpt_busfunc_t xptdefbusfunc;
280 static xpt_targetfunc_t xptdeftargetfunc;
281 static xpt_devicefunc_t xptdefdevicefunc;
282 static xpt_periphfunc_t xptdefperiphfunc;
283 static void xpt_finishconfig_task(void *context, int pending);
284 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
285 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
287 static void xpt_dev_async_default(u_int32_t async_code,
289 struct cam_et *target,
290 struct cam_ed *device,
292 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
293 struct cam_et *target,
295 static xpt_devicefunc_t xptsetasyncfunc;
296 static xpt_busfunc_t xptsetasyncbusfunc;
297 static cam_status xptregister(struct cam_periph *periph,
299 static __inline int periph_is_queued(struct cam_periph *periph);
300 static __inline int device_is_alloc_queued(struct cam_ed *device);
301 static __inline int device_is_send_queued(struct cam_ed *device);
304 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
308 if ((dev->drvq.entries > 0) &&
309 (dev->ccbq.devq_openings > 0) &&
310 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
311 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
313 * The priority of a device waiting for CCB resources
314 * is that of the highest priority peripheral driver
317 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
318 &dev->alloc_ccb_entry.pinfo,
319 CAMQ_GET_PRIO(&dev->drvq));
328 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
332 if ((dev->ccbq.queue.entries > 0) &&
333 (dev->ccbq.dev_openings > 0) &&
334 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
336 * The priority of a device waiting for controller
337 * resources is that of the highest priority CCB
341 xpt_schedule_dev(&bus->sim->devq->send_queue,
342 &dev->send_ccb_entry.pinfo,
343 CAMQ_GET_PRIO(&dev->ccbq.queue));
351 periph_is_queued(struct cam_periph *periph)
353 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
357 device_is_alloc_queued(struct cam_ed *device)
359 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
363 device_is_send_queued(struct cam_ed *device)
365 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
371 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
375 xptdone(struct cam_periph *periph, union ccb *done_ccb)
377 /* Caller will release the CCB */
378 wakeup(&done_ccb->ccb_h.cbfcnp);
382 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
386 * Only allow read-write access.
388 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
392 * We don't allow nonblocking access.
394 if ((flags & O_NONBLOCK) != 0) {
395 printf("%s: can't do nonblocking access\n", devtoname(dev));
399 /* Mark ourselves open */
400 mtx_lock(&xsoftc.xpt_lock);
401 xsoftc.flags |= XPT_FLAG_OPEN;
402 mtx_unlock(&xsoftc.xpt_lock);
408 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
411 /* Mark ourselves closed */
412 mtx_lock(&xsoftc.xpt_lock);
413 xsoftc.flags &= ~XPT_FLAG_OPEN;
414 mtx_unlock(&xsoftc.xpt_lock);
420 * Don't automatically grab the xpt softc lock here even though this is going
421 * through the xpt device. The xpt device is really just a back door for
422 * accessing other devices and SIMs, so the right thing to do is to grab
423 * the appropriate SIM lock once the bus/SIM is located.
426 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
434 * For the transport layer CAMIOCOMMAND ioctl, we really only want
435 * to accept CCB types that don't quite make sense to send through a
436 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
444 inccb = (union ccb *)addr;
446 bus = xpt_find_bus(inccb->ccb_h.path_id);
450 switch (inccb->ccb_h.func_code) {
453 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
454 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
455 xpt_release_bus(bus);
460 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
461 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
462 xpt_release_bus(bus);
470 switch(inccb->ccb_h.func_code) {
478 ccb = xpt_alloc_ccb();
480 CAM_SIM_LOCK(bus->sim);
483 * Create a path using the bus, target, and lun the
486 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
487 inccb->ccb_h.path_id,
488 inccb->ccb_h.target_id,
489 inccb->ccb_h.target_lun) !=
492 CAM_SIM_UNLOCK(bus->sim);
496 /* Ensure all of our fields are correct */
497 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
498 inccb->ccb_h.pinfo.priority);
499 xpt_merge_ccb(ccb, inccb);
500 ccb->ccb_h.cbfcnp = xptdone;
501 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
502 bcopy(ccb, inccb, sizeof(union ccb));
503 xpt_free_path(ccb->ccb_h.path);
505 CAM_SIM_UNLOCK(bus->sim);
512 * This is an immediate CCB, so it's okay to
513 * allocate it on the stack.
516 CAM_SIM_LOCK(bus->sim);
519 * Create a path using the bus, target, and lun the
522 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
523 inccb->ccb_h.path_id,
524 inccb->ccb_h.target_id,
525 inccb->ccb_h.target_lun) !=
528 CAM_SIM_UNLOCK(bus->sim);
531 /* Ensure all of our fields are correct */
532 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
533 inccb->ccb_h.pinfo.priority);
534 xpt_merge_ccb(&ccb, inccb);
535 ccb.ccb_h.cbfcnp = xptdone;
537 CAM_SIM_UNLOCK(bus->sim);
538 bcopy(&ccb, inccb, sizeof(union ccb));
539 xpt_free_path(ccb.ccb_h.path);
543 case XPT_DEV_MATCH: {
544 struct cam_periph_map_info mapinfo;
545 struct cam_path *old_path;
548 * We can't deal with physical addresses for this
549 * type of transaction.
551 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
557 * Save this in case the caller had it set to
558 * something in particular.
560 old_path = inccb->ccb_h.path;
563 * We really don't need a path for the matching
564 * code. The path is needed because of the
565 * debugging statements in xpt_action(). They
566 * assume that the CCB has a valid path.
568 inccb->ccb_h.path = xpt_periph->path;
570 bzero(&mapinfo, sizeof(mapinfo));
573 * Map the pattern and match buffers into kernel
574 * virtual address space.
576 error = cam_periph_mapmem(inccb, &mapinfo);
579 inccb->ccb_h.path = old_path;
584 * This is an immediate CCB, we can send it on directly.
589 * Map the buffers back into user space.
591 cam_periph_unmapmem(inccb, &mapinfo);
593 inccb->ccb_h.path = old_path;
602 xpt_release_bus(bus);
606 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
607 * with the periphal driver name and unit name filled in. The other
608 * fields don't really matter as input. The passthrough driver name
609 * ("pass"), and unit number are passed back in the ccb. The current
610 * device generation number, and the index into the device peripheral
611 * driver list, and the status are also passed back. Note that
612 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
613 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
614 * (or rather should be) impossible for the device peripheral driver
615 * list to change since we look at the whole thing in one pass, and
616 * we do it with lock protection.
619 case CAMGETPASSTHRU: {
621 struct cam_periph *periph;
622 struct periph_driver **p_drv;
625 u_int cur_generation;
626 int base_periph_found;
629 ccb = (union ccb *)addr;
630 unit = ccb->cgdl.unit_number;
631 name = ccb->cgdl.periph_name;
633 * Every 100 devices, we want to drop our lock protection to
634 * give the software interrupt handler a chance to run.
635 * Most systems won't run into this check, but this should
636 * avoid starvation in the software interrupt handler in
641 ccb = (union ccb *)addr;
643 base_periph_found = 0;
646 * Sanity check -- make sure we don't get a null peripheral
649 if (*ccb->cgdl.periph_name == '\0') {
654 /* Keep the list from changing while we traverse it */
655 mtx_lock(&xsoftc.xpt_topo_lock);
657 cur_generation = xsoftc.xpt_generation;
659 /* first find our driver in the list of drivers */
660 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
661 if (strcmp((*p_drv)->driver_name, name) == 0)
664 if (*p_drv == NULL) {
665 mtx_unlock(&xsoftc.xpt_topo_lock);
666 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
667 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
668 *ccb->cgdl.periph_name = '\0';
669 ccb->cgdl.unit_number = 0;
675 * Run through every peripheral instance of this driver
676 * and check to see whether it matches the unit passed
677 * in by the user. If it does, get out of the loops and
678 * find the passthrough driver associated with that
681 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
682 periph = TAILQ_NEXT(periph, unit_links)) {
684 if (periph->unit_number == unit) {
686 } else if (--splbreaknum == 0) {
687 mtx_unlock(&xsoftc.xpt_topo_lock);
688 mtx_lock(&xsoftc.xpt_topo_lock);
690 if (cur_generation != xsoftc.xpt_generation)
695 * If we found the peripheral driver that the user passed
696 * in, go through all of the peripheral drivers for that
697 * particular device and look for a passthrough driver.
699 if (periph != NULL) {
700 struct cam_ed *device;
703 base_periph_found = 1;
704 device = periph->path->device;
705 for (i = 0, periph = SLIST_FIRST(&device->periphs);
707 periph = SLIST_NEXT(periph, periph_links), i++) {
709 * Check to see whether we have a
710 * passthrough device or not.
712 if (strcmp(periph->periph_name, "pass") == 0) {
714 * Fill in the getdevlist fields.
716 strcpy(ccb->cgdl.periph_name,
717 periph->periph_name);
718 ccb->cgdl.unit_number =
720 if (SLIST_NEXT(periph, periph_links))
722 CAM_GDEVLIST_MORE_DEVS;
725 CAM_GDEVLIST_LAST_DEVICE;
726 ccb->cgdl.generation =
730 * Fill in some CCB header fields
731 * that the user may want.
734 periph->path->bus->path_id;
735 ccb->ccb_h.target_id =
736 periph->path->target->target_id;
737 ccb->ccb_h.target_lun =
738 periph->path->device->lun_id;
739 ccb->ccb_h.status = CAM_REQ_CMP;
746 * If the periph is null here, one of two things has
747 * happened. The first possibility is that we couldn't
748 * find the unit number of the particular peripheral driver
749 * that the user is asking about. e.g. the user asks for
750 * the passthrough driver for "da11". We find the list of
751 * "da" peripherals all right, but there is no unit 11.
752 * The other possibility is that we went through the list
753 * of peripheral drivers attached to the device structure,
754 * but didn't find one with the name "pass". Either way,
755 * we return ENOENT, since we couldn't find something.
757 if (periph == NULL) {
758 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
759 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
760 *ccb->cgdl.periph_name = '\0';
761 ccb->cgdl.unit_number = 0;
764 * It is unfortunate that this is even necessary,
765 * but there are many, many clueless users out there.
766 * If this is true, the user is looking for the
767 * passthrough driver, but doesn't have one in his
770 if (base_periph_found == 1) {
771 printf("xptioctl: pass driver is not in the "
773 printf("xptioctl: put \"device pass\" in "
774 "your kernel config file\n");
777 mtx_unlock(&xsoftc.xpt_topo_lock);
789 cam_module_event_handler(module_t mod, int what, void *arg)
795 if ((error = xpt_init(NULL)) != 0)
808 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
811 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
812 xpt_free_path(done_ccb->ccb_h.path);
813 xpt_free_ccb(done_ccb);
815 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
816 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
821 /* thread to handle bus rescans */
823 xpt_scanner_thread(void *dummy)
830 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
831 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
833 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
834 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
837 sim = ccb->ccb_h.path->bus->sim;
848 xpt_rescan(union ccb *ccb)
852 /* Prepare request */
853 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
854 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
855 ccb->ccb_h.func_code = XPT_SCAN_BUS;
856 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
857 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
858 ccb->ccb_h.func_code = XPT_SCAN_TGT;
859 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
860 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
861 ccb->ccb_h.func_code = XPT_SCAN_LUN;
863 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
864 xpt_free_path(ccb->ccb_h.path);
868 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
869 ccb->ccb_h.cbfcnp = xpt_rescan_done;
870 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
871 /* Don't make duplicate entries for the same paths. */
873 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
874 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
875 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
876 wakeup(&xsoftc.ccb_scanq);
878 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
879 xpt_free_path(ccb->ccb_h.path);
885 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
886 xsoftc.buses_to_config++;
887 wakeup(&xsoftc.ccb_scanq);
891 /* Functions accessed by the peripheral drivers */
893 xpt_init(void *dummy)
895 struct cam_sim *xpt_sim;
896 struct cam_path *path;
897 struct cam_devq *devq;
900 TAILQ_INIT(&xsoftc.xpt_busses);
901 TAILQ_INIT(&cam_simq);
902 TAILQ_INIT(&xsoftc.ccb_scanq);
903 STAILQ_INIT(&xsoftc.highpowerq);
904 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
906 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
907 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
908 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
911 * The xpt layer is, itself, the equivelent of a SIM.
912 * Allow 16 ccbs in the ccb pool for it. This should
913 * give decent parallelism when we probe busses and
914 * perform other XPT functions.
916 devq = cam_simq_alloc(16);
917 xpt_sim = cam_sim_alloc(xptaction,
922 /*mtx*/&xsoftc.xpt_lock,
923 /*max_dev_transactions*/0,
924 /*max_tagged_dev_transactions*/0,
929 mtx_lock(&xsoftc.xpt_lock);
930 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
931 mtx_unlock(&xsoftc.xpt_lock);
932 printf("xpt_init: xpt_bus_register failed with status %#x,"
933 " failing attach\n", status);
938 * Looking at the XPT from the SIM layer, the XPT is
939 * the equivelent of a peripheral driver. Allocate
940 * a peripheral driver entry for us.
942 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
944 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
945 mtx_unlock(&xsoftc.xpt_lock);
946 printf("xpt_init: xpt_create_path failed with status %#x,"
947 " failing attach\n", status);
951 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
952 path, NULL, 0, xpt_sim);
954 mtx_unlock(&xsoftc.xpt_lock);
955 /* Install our software interrupt handlers */
956 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
958 * Register a callback for when interrupts are enabled.
960 xsoftc.xpt_config_hook =
961 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
962 M_CAMXPT, M_NOWAIT | M_ZERO);
963 if (xsoftc.xpt_config_hook == NULL) {
964 printf("xpt_init: Cannot malloc config hook "
965 "- failing attach\n");
968 xsoftc.xpt_config_hook->ich_func = xpt_config;
969 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
970 free (xsoftc.xpt_config_hook, M_CAMXPT);
971 printf("xpt_init: config_intrhook_establish failed "
972 "- failing attach\n");
979 xptregister(struct cam_periph *periph, void *arg)
981 struct cam_sim *xpt_sim;
983 if (periph == NULL) {
984 printf("xptregister: periph was NULL!!\n");
985 return(CAM_REQ_CMP_ERR);
988 xpt_sim = (struct cam_sim *)arg;
989 xpt_sim->softc = periph;
991 periph->softc = NULL;
997 xpt_add_periph(struct cam_periph *periph)
999 struct cam_ed *device;
1001 struct periph_list *periph_head;
1003 mtx_assert(periph->sim->mtx, MA_OWNED);
1005 device = periph->path->device;
1007 periph_head = &device->periphs;
1009 status = CAM_REQ_CMP;
1011 if (device != NULL) {
1013 * Make room for this peripheral
1014 * so it will fit in the queue
1015 * when it's scheduled to run
1017 status = camq_resize(&device->drvq,
1018 device->drvq.array_size + 1);
1020 device->generation++;
1022 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1025 mtx_lock(&xsoftc.xpt_topo_lock);
1026 xsoftc.xpt_generation++;
1027 mtx_unlock(&xsoftc.xpt_topo_lock);
1033 xpt_remove_periph(struct cam_periph *periph)
1035 struct cam_ed *device;
1037 mtx_assert(periph->sim->mtx, MA_OWNED);
1039 device = periph->path->device;
1041 if (device != NULL) {
1042 struct periph_list *periph_head;
1044 periph_head = &device->periphs;
1046 /* Release the slot for this peripheral */
1047 camq_resize(&device->drvq, device->drvq.array_size - 1);
1049 device->generation++;
1051 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1054 mtx_lock(&xsoftc.xpt_topo_lock);
1055 xsoftc.xpt_generation++;
1056 mtx_unlock(&xsoftc.xpt_topo_lock);
1061 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1063 struct cam_path *path = periph->path;
1065 mtx_assert(periph->sim->mtx, MA_OWNED);
1067 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1068 periph->periph_name, periph->unit_number,
1069 path->bus->sim->sim_name,
1070 path->bus->sim->unit_number,
1071 path->bus->sim->bus_id,
1073 path->target->target_id,
1074 path->device->lun_id);
1075 printf("%s%d: ", periph->periph_name, periph->unit_number);
1076 if (path->device->protocol == PROTO_SCSI)
1077 scsi_print_inquiry(&path->device->inq_data);
1078 else if (path->device->protocol == PROTO_ATA ||
1079 path->device->protocol == PROTO_SATAPM)
1080 ata_print_ident(&path->device->ident_data);
1082 printf("Unknown protocol device\n");
1083 if (bootverbose && path->device->serial_num_len > 0) {
1084 /* Don't wrap the screen - print only the first 60 chars */
1085 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1086 periph->unit_number, path->device->serial_num);
1088 /* Announce transport details. */
1089 (*(path->bus->xport->announce))(periph);
1090 /* Announce command queueing. */
1091 if (path->device->inq_flags & SID_CmdQue
1092 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1093 printf("%s%d: Command Queueing enabled\n",
1094 periph->periph_name, periph->unit_number);
1096 /* Announce caller's details if they've passed in. */
1097 if (announce_string != NULL)
1098 printf("%s%d: %s\n", periph->periph_name,
1099 periph->unit_number, announce_string);
1102 static dev_match_ret
1103 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1106 dev_match_ret retval;
1109 retval = DM_RET_NONE;
1112 * If we aren't given something to match against, that's an error.
1115 return(DM_RET_ERROR);
1118 * If there are no match entries, then this bus matches no
1121 if ((patterns == NULL) || (num_patterns == 0))
1122 return(DM_RET_DESCEND | DM_RET_COPY);
1124 for (i = 0; i < num_patterns; i++) {
1125 struct bus_match_pattern *cur_pattern;
1128 * If the pattern in question isn't for a bus node, we
1129 * aren't interested. However, we do indicate to the
1130 * calling routine that we should continue descending the
1131 * tree, since the user wants to match against lower-level
1134 if (patterns[i].type != DEV_MATCH_BUS) {
1135 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1136 retval |= DM_RET_DESCEND;
1140 cur_pattern = &patterns[i].pattern.bus_pattern;
1143 * If they want to match any bus node, we give them any
1146 if (cur_pattern->flags == BUS_MATCH_ANY) {
1147 /* set the copy flag */
1148 retval |= DM_RET_COPY;
1151 * If we've already decided on an action, go ahead
1154 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1159 * Not sure why someone would do this...
1161 if (cur_pattern->flags == BUS_MATCH_NONE)
1164 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1165 && (cur_pattern->path_id != bus->path_id))
1168 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1169 && (cur_pattern->bus_id != bus->sim->bus_id))
1172 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1173 && (cur_pattern->unit_number != bus->sim->unit_number))
1176 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1177 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1182 * If we get to this point, the user definitely wants
1183 * information on this bus. So tell the caller to copy the
1186 retval |= DM_RET_COPY;
1189 * If the return action has been set to descend, then we
1190 * know that we've already seen a non-bus matching
1191 * expression, therefore we need to further descend the tree.
1192 * This won't change by continuing around the loop, so we
1193 * go ahead and return. If we haven't seen a non-bus
1194 * matching expression, we keep going around the loop until
1195 * we exhaust the matching expressions. We'll set the stop
1196 * flag once we fall out of the loop.
1198 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1203 * If the return action hasn't been set to descend yet, that means
1204 * we haven't seen anything other than bus matching patterns. So
1205 * tell the caller to stop descending the tree -- the user doesn't
1206 * want to match against lower level tree elements.
1208 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1209 retval |= DM_RET_STOP;
1214 static dev_match_ret
1215 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1216 struct cam_ed *device)
1218 dev_match_ret retval;
1221 retval = DM_RET_NONE;
1224 * If we aren't given something to match against, that's an error.
1227 return(DM_RET_ERROR);
1230 * If there are no match entries, then this device matches no
1233 if ((patterns == NULL) || (num_patterns == 0))
1234 return(DM_RET_DESCEND | DM_RET_COPY);
1236 for (i = 0; i < num_patterns; i++) {
1237 struct device_match_pattern *cur_pattern;
1240 * If the pattern in question isn't for a device node, we
1241 * aren't interested.
1243 if (patterns[i].type != DEV_MATCH_DEVICE) {
1244 if ((patterns[i].type == DEV_MATCH_PERIPH)
1245 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1246 retval |= DM_RET_DESCEND;
1250 cur_pattern = &patterns[i].pattern.device_pattern;
1253 * If they want to match any device node, we give them any
1256 if (cur_pattern->flags == DEV_MATCH_ANY) {
1257 /* set the copy flag */
1258 retval |= DM_RET_COPY;
1262 * If we've already decided on an action, go ahead
1265 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1270 * Not sure why someone would do this...
1272 if (cur_pattern->flags == DEV_MATCH_NONE)
1275 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1276 && (cur_pattern->path_id != device->target->bus->path_id))
1279 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1280 && (cur_pattern->target_id != device->target->target_id))
1283 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1284 && (cur_pattern->target_lun != device->lun_id))
1287 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1288 && (cam_quirkmatch((caddr_t)&device->inq_data,
1289 (caddr_t)&cur_pattern->inq_pat,
1290 1, sizeof(cur_pattern->inq_pat),
1291 scsi_static_inquiry_match) == NULL))
1295 * If we get to this point, the user definitely wants
1296 * information on this device. So tell the caller to copy
1299 retval |= DM_RET_COPY;
1302 * If the return action has been set to descend, then we
1303 * know that we've already seen a peripheral matching
1304 * expression, therefore we need to further descend the tree.
1305 * This won't change by continuing around the loop, so we
1306 * go ahead and return. If we haven't seen a peripheral
1307 * matching expression, we keep going around the loop until
1308 * we exhaust the matching expressions. We'll set the stop
1309 * flag once we fall out of the loop.
1311 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1316 * If the return action hasn't been set to descend yet, that means
1317 * we haven't seen any peripheral matching patterns. So tell the
1318 * caller to stop descending the tree -- the user doesn't want to
1319 * match against lower level tree elements.
1321 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1322 retval |= DM_RET_STOP;
1328 * Match a single peripheral against any number of match patterns.
1330 static dev_match_ret
1331 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1332 struct cam_periph *periph)
1334 dev_match_ret retval;
1338 * If we aren't given something to match against, that's an error.
1341 return(DM_RET_ERROR);
1344 * If there are no match entries, then this peripheral matches no
1347 if ((patterns == NULL) || (num_patterns == 0))
1348 return(DM_RET_STOP | DM_RET_COPY);
1351 * There aren't any nodes below a peripheral node, so there's no
1352 * reason to descend the tree any further.
1354 retval = DM_RET_STOP;
1356 for (i = 0; i < num_patterns; i++) {
1357 struct periph_match_pattern *cur_pattern;
1360 * If the pattern in question isn't for a peripheral, we
1361 * aren't interested.
1363 if (patterns[i].type != DEV_MATCH_PERIPH)
1366 cur_pattern = &patterns[i].pattern.periph_pattern;
1369 * If they want to match on anything, then we will do so.
1371 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1372 /* set the copy flag */
1373 retval |= DM_RET_COPY;
1376 * We've already set the return action to stop,
1377 * since there are no nodes below peripherals in
1384 * Not sure why someone would do this...
1386 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1389 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1390 && (cur_pattern->path_id != periph->path->bus->path_id))
1394 * For the target and lun id's, we have to make sure the
1395 * target and lun pointers aren't NULL. The xpt peripheral
1396 * has a wildcard target and device.
1398 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1399 && ((periph->path->target == NULL)
1400 ||(cur_pattern->target_id != periph->path->target->target_id)))
1403 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1404 && ((periph->path->device == NULL)
1405 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1408 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1409 && (cur_pattern->unit_number != periph->unit_number))
1412 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1413 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1418 * If we get to this point, the user definitely wants
1419 * information on this peripheral. So tell the caller to
1420 * copy the data out.
1422 retval |= DM_RET_COPY;
1425 * The return action has already been set to stop, since
1426 * peripherals don't have any nodes below them in the EDT.
1432 * If we get to this point, the peripheral that was passed in
1433 * doesn't match any of the patterns.
1439 xptedtbusfunc(struct cam_eb *bus, void *arg)
1441 struct ccb_dev_match *cdm;
1442 dev_match_ret retval;
1444 cdm = (struct ccb_dev_match *)arg;
1447 * If our position is for something deeper in the tree, that means
1448 * that we've already seen this node. So, we keep going down.
1450 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1451 && (cdm->pos.cookie.bus == bus)
1452 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1453 && (cdm->pos.cookie.target != NULL))
1454 retval = DM_RET_DESCEND;
1456 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1459 * If we got an error, bail out of the search.
1461 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1462 cdm->status = CAM_DEV_MATCH_ERROR;
1467 * If the copy flag is set, copy this bus out.
1469 if (retval & DM_RET_COPY) {
1472 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1473 sizeof(struct dev_match_result));
1476 * If we don't have enough space to put in another
1477 * match result, save our position and tell the
1478 * user there are more devices to check.
1480 if (spaceleft < sizeof(struct dev_match_result)) {
1481 bzero(&cdm->pos, sizeof(cdm->pos));
1482 cdm->pos.position_type =
1483 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1485 cdm->pos.cookie.bus = bus;
1486 cdm->pos.generations[CAM_BUS_GENERATION]=
1487 xsoftc.bus_generation;
1488 cdm->status = CAM_DEV_MATCH_MORE;
1491 j = cdm->num_matches;
1493 cdm->matches[j].type = DEV_MATCH_BUS;
1494 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1495 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1496 cdm->matches[j].result.bus_result.unit_number =
1497 bus->sim->unit_number;
1498 strncpy(cdm->matches[j].result.bus_result.dev_name,
1499 bus->sim->sim_name, DEV_IDLEN);
1503 * If the user is only interested in busses, there's no
1504 * reason to descend to the next level in the tree.
1506 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1510 * If there is a target generation recorded, check it to
1511 * make sure the target list hasn't changed.
1513 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1514 && (bus == cdm->pos.cookie.bus)
1515 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1516 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1517 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1519 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1523 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1524 && (cdm->pos.cookie.bus == bus)
1525 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1526 && (cdm->pos.cookie.target != NULL))
1527 return(xpttargettraverse(bus,
1528 (struct cam_et *)cdm->pos.cookie.target,
1529 xptedttargetfunc, arg));
1531 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1535 xptedttargetfunc(struct cam_et *target, void *arg)
1537 struct ccb_dev_match *cdm;
1539 cdm = (struct ccb_dev_match *)arg;
1542 * If there is a device list generation recorded, check it to
1543 * make sure the device list hasn't changed.
1545 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1546 && (cdm->pos.cookie.bus == target->bus)
1547 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1548 && (cdm->pos.cookie.target == target)
1549 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1550 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1551 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1552 target->generation)) {
1553 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1557 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1558 && (cdm->pos.cookie.bus == target->bus)
1559 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1560 && (cdm->pos.cookie.target == target)
1561 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1562 && (cdm->pos.cookie.device != NULL))
1563 return(xptdevicetraverse(target,
1564 (struct cam_ed *)cdm->pos.cookie.device,
1565 xptedtdevicefunc, arg));
1567 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1571 xptedtdevicefunc(struct cam_ed *device, void *arg)
1574 struct ccb_dev_match *cdm;
1575 dev_match_ret retval;
1577 cdm = (struct ccb_dev_match *)arg;
1580 * If our position is for something deeper in the tree, that means
1581 * that we've already seen this node. So, we keep going down.
1583 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1584 && (cdm->pos.cookie.device == device)
1585 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1586 && (cdm->pos.cookie.periph != NULL))
1587 retval = DM_RET_DESCEND;
1589 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1592 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1593 cdm->status = CAM_DEV_MATCH_ERROR;
1598 * If the copy flag is set, copy this device out.
1600 if (retval & DM_RET_COPY) {
1603 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1604 sizeof(struct dev_match_result));
1607 * If we don't have enough space to put in another
1608 * match result, save our position and tell the
1609 * user there are more devices to check.
1611 if (spaceleft < sizeof(struct dev_match_result)) {
1612 bzero(&cdm->pos, sizeof(cdm->pos));
1613 cdm->pos.position_type =
1614 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1615 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1617 cdm->pos.cookie.bus = device->target->bus;
1618 cdm->pos.generations[CAM_BUS_GENERATION]=
1619 xsoftc.bus_generation;
1620 cdm->pos.cookie.target = device->target;
1621 cdm->pos.generations[CAM_TARGET_GENERATION] =
1622 device->target->bus->generation;
1623 cdm->pos.cookie.device = device;
1624 cdm->pos.generations[CAM_DEV_GENERATION] =
1625 device->target->generation;
1626 cdm->status = CAM_DEV_MATCH_MORE;
1629 j = cdm->num_matches;
1631 cdm->matches[j].type = DEV_MATCH_DEVICE;
1632 cdm->matches[j].result.device_result.path_id =
1633 device->target->bus->path_id;
1634 cdm->matches[j].result.device_result.target_id =
1635 device->target->target_id;
1636 cdm->matches[j].result.device_result.target_lun =
1638 cdm->matches[j].result.device_result.protocol =
1640 bcopy(&device->inq_data,
1641 &cdm->matches[j].result.device_result.inq_data,
1642 sizeof(struct scsi_inquiry_data));
1643 bcopy(&device->ident_data,
1644 &cdm->matches[j].result.device_result.ident_data,
1645 sizeof(struct ata_params));
1647 /* Let the user know whether this device is unconfigured */
1648 if (device->flags & CAM_DEV_UNCONFIGURED)
1649 cdm->matches[j].result.device_result.flags =
1650 DEV_RESULT_UNCONFIGURED;
1652 cdm->matches[j].result.device_result.flags =
1657 * If the user isn't interested in peripherals, don't descend
1658 * the tree any further.
1660 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1664 * If there is a peripheral list generation recorded, make sure
1665 * it hasn't changed.
1667 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1668 && (device->target->bus == cdm->pos.cookie.bus)
1669 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1670 && (device->target == cdm->pos.cookie.target)
1671 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1672 && (device == cdm->pos.cookie.device)
1673 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1674 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1675 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1676 device->generation)){
1677 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1681 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1682 && (cdm->pos.cookie.bus == device->target->bus)
1683 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1684 && (cdm->pos.cookie.target == device->target)
1685 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1686 && (cdm->pos.cookie.device == device)
1687 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1688 && (cdm->pos.cookie.periph != NULL))
1689 return(xptperiphtraverse(device,
1690 (struct cam_periph *)cdm->pos.cookie.periph,
1691 xptedtperiphfunc, arg));
1693 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1697 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1699 struct ccb_dev_match *cdm;
1700 dev_match_ret retval;
1702 cdm = (struct ccb_dev_match *)arg;
1704 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1706 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1707 cdm->status = CAM_DEV_MATCH_ERROR;
1712 * If the copy flag is set, copy this peripheral out.
1714 if (retval & DM_RET_COPY) {
1717 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1718 sizeof(struct dev_match_result));
1721 * If we don't have enough space to put in another
1722 * match result, save our position and tell the
1723 * user there are more devices to check.
1725 if (spaceleft < sizeof(struct dev_match_result)) {
1726 bzero(&cdm->pos, sizeof(cdm->pos));
1727 cdm->pos.position_type =
1728 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1729 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1732 cdm->pos.cookie.bus = periph->path->bus;
1733 cdm->pos.generations[CAM_BUS_GENERATION]=
1734 xsoftc.bus_generation;
1735 cdm->pos.cookie.target = periph->path->target;
1736 cdm->pos.generations[CAM_TARGET_GENERATION] =
1737 periph->path->bus->generation;
1738 cdm->pos.cookie.device = periph->path->device;
1739 cdm->pos.generations[CAM_DEV_GENERATION] =
1740 periph->path->target->generation;
1741 cdm->pos.cookie.periph = periph;
1742 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1743 periph->path->device->generation;
1744 cdm->status = CAM_DEV_MATCH_MORE;
1748 j = cdm->num_matches;
1750 cdm->matches[j].type = DEV_MATCH_PERIPH;
1751 cdm->matches[j].result.periph_result.path_id =
1752 periph->path->bus->path_id;
1753 cdm->matches[j].result.periph_result.target_id =
1754 periph->path->target->target_id;
1755 cdm->matches[j].result.periph_result.target_lun =
1756 periph->path->device->lun_id;
1757 cdm->matches[j].result.periph_result.unit_number =
1758 periph->unit_number;
1759 strncpy(cdm->matches[j].result.periph_result.periph_name,
1760 periph->periph_name, DEV_IDLEN);
1767 xptedtmatch(struct ccb_dev_match *cdm)
1771 cdm->num_matches = 0;
1774 * Check the bus list generation. If it has changed, the user
1775 * needs to reset everything and start over.
1777 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1778 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1779 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1780 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1784 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1785 && (cdm->pos.cookie.bus != NULL))
1786 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1787 xptedtbusfunc, cdm);
1789 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1792 * If we get back 0, that means that we had to stop before fully
1793 * traversing the EDT. It also means that one of the subroutines
1794 * has set the status field to the proper value. If we get back 1,
1795 * we've fully traversed the EDT and copied out any matching entries.
1798 cdm->status = CAM_DEV_MATCH_LAST;
1804 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1806 struct ccb_dev_match *cdm;
1808 cdm = (struct ccb_dev_match *)arg;
1810 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1811 && (cdm->pos.cookie.pdrv == pdrv)
1812 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1813 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1814 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1815 (*pdrv)->generation)) {
1816 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1820 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1821 && (cdm->pos.cookie.pdrv == pdrv)
1822 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1823 && (cdm->pos.cookie.periph != NULL))
1824 return(xptpdperiphtraverse(pdrv,
1825 (struct cam_periph *)cdm->pos.cookie.periph,
1826 xptplistperiphfunc, arg));
1828 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1832 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1834 struct ccb_dev_match *cdm;
1835 dev_match_ret retval;
1837 cdm = (struct ccb_dev_match *)arg;
1839 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1841 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1842 cdm->status = CAM_DEV_MATCH_ERROR;
1847 * If the copy flag is set, copy this peripheral out.
1849 if (retval & DM_RET_COPY) {
1852 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1853 sizeof(struct dev_match_result));
1856 * If we don't have enough space to put in another
1857 * match result, save our position and tell the
1858 * user there are more devices to check.
1860 if (spaceleft < sizeof(struct dev_match_result)) {
1861 struct periph_driver **pdrv;
1864 bzero(&cdm->pos, sizeof(cdm->pos));
1865 cdm->pos.position_type =
1866 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1870 * This may look a bit non-sensical, but it is
1871 * actually quite logical. There are very few
1872 * peripheral drivers, and bloating every peripheral
1873 * structure with a pointer back to its parent
1874 * peripheral driver linker set entry would cost
1875 * more in the long run than doing this quick lookup.
1877 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1878 if (strcmp((*pdrv)->driver_name,
1879 periph->periph_name) == 0)
1883 if (*pdrv == NULL) {
1884 cdm->status = CAM_DEV_MATCH_ERROR;
1888 cdm->pos.cookie.pdrv = pdrv;
1890 * The periph generation slot does double duty, as
1891 * does the periph pointer slot. They are used for
1892 * both edt and pdrv lookups and positioning.
1894 cdm->pos.cookie.periph = periph;
1895 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1896 (*pdrv)->generation;
1897 cdm->status = CAM_DEV_MATCH_MORE;
1901 j = cdm->num_matches;
1903 cdm->matches[j].type = DEV_MATCH_PERIPH;
1904 cdm->matches[j].result.periph_result.path_id =
1905 periph->path->bus->path_id;
1908 * The transport layer peripheral doesn't have a target or
1911 if (periph->path->target)
1912 cdm->matches[j].result.periph_result.target_id =
1913 periph->path->target->target_id;
1915 cdm->matches[j].result.periph_result.target_id = -1;
1917 if (periph->path->device)
1918 cdm->matches[j].result.periph_result.target_lun =
1919 periph->path->device->lun_id;
1921 cdm->matches[j].result.periph_result.target_lun = -1;
1923 cdm->matches[j].result.periph_result.unit_number =
1924 periph->unit_number;
1925 strncpy(cdm->matches[j].result.periph_result.periph_name,
1926 periph->periph_name, DEV_IDLEN);
1933 xptperiphlistmatch(struct ccb_dev_match *cdm)
1937 cdm->num_matches = 0;
1940 * At this point in the edt traversal function, we check the bus
1941 * list generation to make sure that no busses have been added or
1942 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1943 * For the peripheral driver list traversal function, however, we
1944 * don't have to worry about new peripheral driver types coming or
1945 * going; they're in a linker set, and therefore can't change
1946 * without a recompile.
1949 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1950 && (cdm->pos.cookie.pdrv != NULL))
1951 ret = xptpdrvtraverse(
1952 (struct periph_driver **)cdm->pos.cookie.pdrv,
1953 xptplistpdrvfunc, cdm);
1955 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
1958 * If we get back 0, that means that we had to stop before fully
1959 * traversing the peripheral driver tree. It also means that one of
1960 * the subroutines has set the status field to the proper value. If
1961 * we get back 1, we've fully traversed the EDT and copied out any
1965 cdm->status = CAM_DEV_MATCH_LAST;
1971 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
1973 struct cam_eb *bus, *next_bus;
1978 mtx_lock(&xsoftc.xpt_topo_lock);
1979 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
1986 * XXX The locking here is obviously very complex. We
1987 * should work to simplify it.
1989 mtx_unlock(&xsoftc.xpt_topo_lock);
1990 CAM_SIM_LOCK(bus->sim);
1991 retval = tr_func(bus, arg);
1992 CAM_SIM_UNLOCK(bus->sim);
1994 mtx_lock(&xsoftc.xpt_topo_lock);
1995 next_bus = TAILQ_NEXT(bus, links);
1996 mtx_unlock(&xsoftc.xpt_topo_lock);
1998 xpt_release_bus(bus);
2002 mtx_lock(&xsoftc.xpt_topo_lock);
2004 mtx_unlock(&xsoftc.xpt_topo_lock);
2010 xpt_sim_opened(struct cam_sim *sim)
2013 struct cam_et *target;
2014 struct cam_ed *device;
2015 struct cam_periph *periph;
2017 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2018 mtx_assert(sim->mtx, MA_OWNED);
2020 mtx_lock(&xsoftc.xpt_topo_lock);
2021 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2022 if (bus->sim != sim)
2025 TAILQ_FOREACH(target, &bus->et_entries, links) {
2026 TAILQ_FOREACH(device, &target->ed_entries, links) {
2027 SLIST_FOREACH(periph, &device->periphs,
2029 if (periph->refcount > 0) {
2030 mtx_unlock(&xsoftc.xpt_topo_lock);
2038 mtx_unlock(&xsoftc.xpt_topo_lock);
2043 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2044 xpt_targetfunc_t *tr_func, void *arg)
2046 struct cam_et *target, *next_target;
2050 for (target = (start_target ? start_target :
2051 TAILQ_FIRST(&bus->et_entries));
2052 target != NULL; target = next_target) {
2056 retval = tr_func(target, arg);
2058 next_target = TAILQ_NEXT(target, links);
2060 xpt_release_target(target);
2070 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2071 xpt_devicefunc_t *tr_func, void *arg)
2073 struct cam_ed *device, *next_device;
2077 for (device = (start_device ? start_device :
2078 TAILQ_FIRST(&target->ed_entries));
2080 device = next_device) {
2083 * Hold a reference so the current device does not go away
2088 retval = tr_func(device, arg);
2091 * Grab our next pointer before we release the current
2094 next_device = TAILQ_NEXT(device, links);
2096 xpt_release_device(device);
2106 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2107 xpt_periphfunc_t *tr_func, void *arg)
2109 struct cam_periph *periph, *next_periph;
2115 for (periph = (start_periph ? start_periph :
2116 SLIST_FIRST(&device->periphs));
2118 periph = next_periph) {
2122 * In this case, we want to show peripherals that have been
2123 * invalidated, but not peripherals that are scheduled to
2124 * be freed. So instead of calling cam_periph_acquire(),
2125 * which will fail if the periph has been invalidated, we
2126 * just check for the free flag here. If it is free, we
2127 * skip to the next periph.
2129 if (periph->flags & CAM_PERIPH_FREE) {
2130 next_periph = SLIST_NEXT(periph, periph_links);
2135 * Acquire a reference to this periph while we call the
2136 * traversal function, so it can't go away.
2142 retval = tr_func(periph, arg);
2145 * We need the lock for list traversal.
2150 * Grab the next peripheral before we release this one, so
2151 * our next pointer is still valid.
2153 next_periph = SLIST_NEXT(periph, periph_links);
2155 cam_periph_release_locked_buses(periph);
2169 xptpdrvtraverse(struct periph_driver **start_pdrv,
2170 xpt_pdrvfunc_t *tr_func, void *arg)
2172 struct periph_driver **pdrv;
2178 * We don't traverse the peripheral driver list like we do the
2179 * other lists, because it is a linker set, and therefore cannot be
2180 * changed during runtime. If the peripheral driver list is ever
2181 * re-done to be something other than a linker set (i.e. it can
2182 * change while the system is running), the list traversal should
2183 * be modified to work like the other traversal functions.
2185 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2186 *pdrv != NULL; pdrv++) {
2187 retval = tr_func(pdrv, arg);
2197 xptpdperiphtraverse(struct periph_driver **pdrv,
2198 struct cam_periph *start_periph,
2199 xpt_periphfunc_t *tr_func, void *arg)
2201 struct cam_periph *periph, *next_periph;
2207 for (periph = (start_periph ? start_periph :
2208 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2209 periph = next_periph) {
2213 * In this case, we want to show peripherals that have been
2214 * invalidated, but not peripherals that are scheduled to
2215 * be freed. So instead of calling cam_periph_acquire(),
2216 * which will fail if the periph has been invalidated, we
2217 * just check for the free flag here. If it is free, we
2218 * skip to the next periph.
2220 if (periph->flags & CAM_PERIPH_FREE) {
2221 next_periph = TAILQ_NEXT(periph, unit_links);
2226 * Acquire a reference to this periph while we call the
2227 * traversal function, so it can't go away.
2232 * XXX KDM we have the toplogy lock here, but in
2233 * xptperiphtraverse(), we drop it before calling the
2234 * traversal function. Which is correct?
2236 retval = tr_func(periph, arg);
2239 * Grab the next peripheral before we release this one, so
2240 * our next pointer is still valid.
2242 next_periph = TAILQ_NEXT(periph, unit_links);
2244 cam_periph_release_locked_buses(periph);
2257 xptdefbusfunc(struct cam_eb *bus, void *arg)
2259 struct xpt_traverse_config *tr_config;
2261 tr_config = (struct xpt_traverse_config *)arg;
2263 if (tr_config->depth == XPT_DEPTH_BUS) {
2264 xpt_busfunc_t *tr_func;
2266 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2268 return(tr_func(bus, tr_config->tr_arg));
2270 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2274 xptdeftargetfunc(struct cam_et *target, void *arg)
2276 struct xpt_traverse_config *tr_config;
2278 tr_config = (struct xpt_traverse_config *)arg;
2280 if (tr_config->depth == XPT_DEPTH_TARGET) {
2281 xpt_targetfunc_t *tr_func;
2283 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2285 return(tr_func(target, tr_config->tr_arg));
2287 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2291 xptdefdevicefunc(struct cam_ed *device, void *arg)
2293 struct xpt_traverse_config *tr_config;
2295 tr_config = (struct xpt_traverse_config *)arg;
2297 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2298 xpt_devicefunc_t *tr_func;
2300 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2302 return(tr_func(device, tr_config->tr_arg));
2304 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2308 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2310 struct xpt_traverse_config *tr_config;
2311 xpt_periphfunc_t *tr_func;
2313 tr_config = (struct xpt_traverse_config *)arg;
2315 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2318 * Unlike the other default functions, we don't check for depth
2319 * here. The peripheral driver level is the last level in the EDT,
2320 * so if we're here, we should execute the function in question.
2322 return(tr_func(periph, tr_config->tr_arg));
2326 * Execute the given function for every bus in the EDT.
2329 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2331 struct xpt_traverse_config tr_config;
2333 tr_config.depth = XPT_DEPTH_BUS;
2334 tr_config.tr_func = tr_func;
2335 tr_config.tr_arg = arg;
2337 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2341 * Execute the given function for every device in the EDT.
2344 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2346 struct xpt_traverse_config tr_config;
2348 tr_config.depth = XPT_DEPTH_DEVICE;
2349 tr_config.tr_func = tr_func;
2350 tr_config.tr_arg = arg;
2352 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2356 xptsetasyncfunc(struct cam_ed *device, void *arg)
2358 struct cam_path path;
2359 struct ccb_getdev cgd;
2360 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2363 * Don't report unconfigured devices (Wildcard devs,
2364 * devices only for target mode, device instances
2365 * that have been invalidated but are waiting for
2366 * their last reference count to be released).
2368 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2371 xpt_compile_path(&path,
2373 device->target->bus->path_id,
2374 device->target->target_id,
2376 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2377 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2378 xpt_action((union ccb *)&cgd);
2379 csa->callback(csa->callback_arg,
2382 xpt_release_path(&path);
2388 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2390 struct cam_path path;
2391 struct ccb_pathinq cpi;
2392 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2394 xpt_compile_path(&path, /*periph*/NULL,
2396 CAM_TARGET_WILDCARD,
2398 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2399 cpi.ccb_h.func_code = XPT_PATH_INQ;
2400 xpt_action((union ccb *)&cpi);
2401 csa->callback(csa->callback_arg,
2404 xpt_release_path(&path);
2410 xpt_action(union ccb *start_ccb)
2413 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2415 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2416 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2420 xpt_action_default(union ccb *start_ccb)
2422 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2423 struct cam_path *path;
2425 path = start_ccb->ccb_h.path;
2426 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2428 switch (start_ccb->ccb_h.func_code) {
2431 struct cam_ed *device;
2434 * For the sake of compatibility with SCSI-1
2435 * devices that may not understand the identify
2436 * message, we include lun information in the
2437 * second byte of all commands. SCSI-1 specifies
2438 * that luns are a 3 bit value and reserves only 3
2439 * bits for lun information in the CDB. Later
2440 * revisions of the SCSI spec allow for more than 8
2441 * luns, but have deprecated lun information in the
2442 * CDB. So, if the lun won't fit, we must omit.
2444 * Also be aware that during initial probing for devices,
2445 * the inquiry information is unknown but initialized to 0.
2446 * This means that this code will be exercised while probing
2447 * devices with an ANSI revision greater than 2.
2449 device = path->device;
2450 if (device->protocol_version <= SCSI_REV_2
2451 && start_ccb->ccb_h.target_lun < 8
2452 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2454 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2455 start_ccb->ccb_h.target_lun << 5;
2457 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2458 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2459 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2460 &path->device->inq_data),
2461 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2462 cdb_str, sizeof(cdb_str))));
2466 case XPT_CONT_TARGET_IO:
2467 start_ccb->csio.sense_resid = 0;
2468 start_ccb->csio.resid = 0;
2471 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2472 start_ccb->ataio.resid = 0;
2473 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2474 ata_op_string(&start_ccb->ataio.cmd),
2475 ata_cmd_string(&start_ccb->ataio.cmd,
2476 cdb_str, sizeof(cdb_str))));
2484 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2485 path->device->sim->devq->alloc_openings += frozen;
2487 xpt_run_dev_allocq(path->bus);
2488 if (xpt_schedule_dev_sendq(path->bus, path->device))
2489 xpt_run_dev_sendq(path->bus);
2492 case XPT_CALC_GEOMETRY:
2494 struct cam_sim *sim;
2496 /* Filter out garbage */
2497 if (start_ccb->ccg.block_size == 0
2498 || start_ccb->ccg.volume_size == 0) {
2499 start_ccb->ccg.cylinders = 0;
2500 start_ccb->ccg.heads = 0;
2501 start_ccb->ccg.secs_per_track = 0;
2502 start_ccb->ccb_h.status = CAM_REQ_CMP;
2505 #if defined(PC98) || defined(__sparc64__)
2507 * In a PC-98 system, geometry translation depens on
2508 * the "real" device geometry obtained from mode page 4.
2509 * SCSI geometry translation is performed in the
2510 * initialization routine of the SCSI BIOS and the result
2511 * stored in host memory. If the translation is available
2512 * in host memory, use it. If not, rely on the default
2513 * translation the device driver performs.
2514 * For sparc64, we may need adjust the geometry of large
2515 * disks in order to fit the limitations of the 16-bit
2516 * fields of the VTOC8 disk label.
2518 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2519 start_ccb->ccb_h.status = CAM_REQ_CMP;
2523 sim = path->bus->sim;
2524 (*(sim->sim_action))(sim, start_ccb);
2529 union ccb* abort_ccb;
2531 abort_ccb = start_ccb->cab.abort_ccb;
2532 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2534 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2535 struct cam_ccbq *ccbq;
2536 struct cam_ed *device;
2538 device = abort_ccb->ccb_h.path->device;
2539 ccbq = &device->ccbq;
2540 device->sim->devq->alloc_openings -=
2541 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2542 abort_ccb->ccb_h.status =
2543 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2544 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2545 xpt_done(abort_ccb);
2546 start_ccb->ccb_h.status = CAM_REQ_CMP;
2549 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2550 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2552 * We've caught this ccb en route to
2553 * the SIM. Flag it for abort and the
2554 * SIM will do so just before starting
2555 * real work on the CCB.
2557 abort_ccb->ccb_h.status =
2558 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2559 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2560 start_ccb->ccb_h.status = CAM_REQ_CMP;
2564 if (XPT_FC_IS_QUEUED(abort_ccb)
2565 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2567 * It's already completed but waiting
2568 * for our SWI to get to it.
2570 start_ccb->ccb_h.status = CAM_UA_ABORT;
2574 * If we weren't able to take care of the abort request
2575 * in the XPT, pass the request down to the SIM for processing.
2579 case XPT_ACCEPT_TARGET_IO:
2581 case XPT_IMMED_NOTIFY:
2582 case XPT_NOTIFY_ACK:
2584 case XPT_IMMEDIATE_NOTIFY:
2585 case XPT_NOTIFY_ACKNOWLEDGE:
2586 case XPT_GET_SIM_KNOB:
2587 case XPT_SET_SIM_KNOB:
2589 struct cam_sim *sim;
2591 sim = path->bus->sim;
2592 (*(sim->sim_action))(sim, start_ccb);
2597 struct cam_sim *sim;
2599 sim = path->bus->sim;
2600 (*(sim->sim_action))(sim, start_ccb);
2603 case XPT_PATH_STATS:
2604 start_ccb->cpis.last_reset = path->bus->last_reset;
2605 start_ccb->ccb_h.status = CAM_REQ_CMP;
2612 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2613 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2615 struct ccb_getdev *cgd;
2617 cgd = &start_ccb->cgd;
2618 cgd->protocol = dev->protocol;
2619 cgd->inq_data = dev->inq_data;
2620 cgd->ident_data = dev->ident_data;
2621 cgd->inq_flags = dev->inq_flags;
2622 cgd->ccb_h.status = CAM_REQ_CMP;
2623 cgd->serial_num_len = dev->serial_num_len;
2624 if ((dev->serial_num_len > 0)
2625 && (dev->serial_num != NULL))
2626 bcopy(dev->serial_num, cgd->serial_num,
2627 dev->serial_num_len);
2631 case XPT_GDEV_STATS:
2636 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2637 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2639 struct ccb_getdevstats *cgds;
2643 cgds = &start_ccb->cgds;
2646 cgds->dev_openings = dev->ccbq.dev_openings;
2647 cgds->dev_active = dev->ccbq.dev_active;
2648 cgds->devq_openings = dev->ccbq.devq_openings;
2649 cgds->devq_queued = dev->ccbq.queue.entries;
2650 cgds->held = dev->ccbq.held;
2651 cgds->last_reset = tar->last_reset;
2652 cgds->maxtags = dev->maxtags;
2653 cgds->mintags = dev->mintags;
2654 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2655 cgds->last_reset = bus->last_reset;
2656 cgds->ccb_h.status = CAM_REQ_CMP;
2662 struct cam_periph *nperiph;
2663 struct periph_list *periph_head;
2664 struct ccb_getdevlist *cgdl;
2666 struct cam_ed *device;
2673 * Don't want anyone mucking with our data.
2675 device = path->device;
2676 periph_head = &device->periphs;
2677 cgdl = &start_ccb->cgdl;
2680 * Check and see if the list has changed since the user
2681 * last requested a list member. If so, tell them that the
2682 * list has changed, and therefore they need to start over
2683 * from the beginning.
2685 if ((cgdl->index != 0) &&
2686 (cgdl->generation != device->generation)) {
2687 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2692 * Traverse the list of peripherals and attempt to find
2693 * the requested peripheral.
2695 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2696 (nperiph != NULL) && (i <= cgdl->index);
2697 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2698 if (i == cgdl->index) {
2699 strncpy(cgdl->periph_name,
2700 nperiph->periph_name,
2702 cgdl->unit_number = nperiph->unit_number;
2707 cgdl->status = CAM_GDEVLIST_ERROR;
2711 if (nperiph == NULL)
2712 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2714 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2717 cgdl->generation = device->generation;
2719 cgdl->ccb_h.status = CAM_REQ_CMP;
2724 dev_pos_type position_type;
2725 struct ccb_dev_match *cdm;
2727 cdm = &start_ccb->cdm;
2730 * There are two ways of getting at information in the EDT.
2731 * The first way is via the primary EDT tree. It starts
2732 * with a list of busses, then a list of targets on a bus,
2733 * then devices/luns on a target, and then peripherals on a
2734 * device/lun. The "other" way is by the peripheral driver
2735 * lists. The peripheral driver lists are organized by
2736 * peripheral driver. (obviously) So it makes sense to
2737 * use the peripheral driver list if the user is looking
2738 * for something like "da1", or all "da" devices. If the
2739 * user is looking for something on a particular bus/target
2740 * or lun, it's generally better to go through the EDT tree.
2743 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2744 position_type = cdm->pos.position_type;
2748 position_type = CAM_DEV_POS_NONE;
2750 for (i = 0; i < cdm->num_patterns; i++) {
2751 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2752 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2753 position_type = CAM_DEV_POS_EDT;
2758 if (cdm->num_patterns == 0)
2759 position_type = CAM_DEV_POS_EDT;
2760 else if (position_type == CAM_DEV_POS_NONE)
2761 position_type = CAM_DEV_POS_PDRV;
2764 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2765 case CAM_DEV_POS_EDT:
2768 case CAM_DEV_POS_PDRV:
2769 xptperiphlistmatch(cdm);
2772 cdm->status = CAM_DEV_MATCH_ERROR;
2776 if (cdm->status == CAM_DEV_MATCH_ERROR)
2777 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2779 start_ccb->ccb_h.status = CAM_REQ_CMP;
2785 struct ccb_setasync *csa;
2786 struct async_node *cur_entry;
2787 struct async_list *async_head;
2790 csa = &start_ccb->csa;
2791 added = csa->event_enable;
2792 async_head = &path->device->asyncs;
2795 * If there is already an entry for us, simply
2798 cur_entry = SLIST_FIRST(async_head);
2799 while (cur_entry != NULL) {
2800 if ((cur_entry->callback_arg == csa->callback_arg)
2801 && (cur_entry->callback == csa->callback))
2803 cur_entry = SLIST_NEXT(cur_entry, links);
2806 if (cur_entry != NULL) {
2808 * If the request has no flags set,
2811 added &= ~cur_entry->event_enable;
2812 if (csa->event_enable == 0) {
2813 SLIST_REMOVE(async_head, cur_entry,
2815 xpt_release_device(path->device);
2816 free(cur_entry, M_CAMXPT);
2818 cur_entry->event_enable = csa->event_enable;
2820 csa->event_enable = added;
2822 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2824 if (cur_entry == NULL) {
2825 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2828 cur_entry->event_enable = csa->event_enable;
2829 cur_entry->callback_arg = csa->callback_arg;
2830 cur_entry->callback = csa->callback;
2831 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2832 xpt_acquire_device(path->device);
2834 start_ccb->ccb_h.status = CAM_REQ_CMP;
2839 struct ccb_relsim *crs;
2842 crs = &start_ccb->crs;
2846 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2850 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2852 /* Don't ever go below one opening */
2853 if (crs->openings > 0) {
2854 xpt_dev_ccbq_resize(path, crs->openings);
2857 "number of openings is now %d\n",
2863 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2865 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2868 * Just extend the old timeout and decrement
2869 * the freeze count so that a single timeout
2870 * is sufficient for releasing the queue.
2872 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2873 callout_stop(&dev->callout);
2876 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2879 callout_reset(&dev->callout,
2880 (crs->release_timeout * hz) / 1000,
2881 xpt_release_devq_timeout, dev);
2883 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2887 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2889 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2891 * Decrement the freeze count so that a single
2892 * completion is still sufficient to unfreeze
2895 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2898 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2899 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2903 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2905 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2906 || (dev->ccbq.dev_active == 0)) {
2908 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2911 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2912 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2916 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2917 xpt_release_devq_rl(path, /*runlevel*/
2918 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2919 crs->release_timeout : 0,
2920 /*count*/1, /*run_queue*/TRUE);
2922 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2923 start_ccb->ccb_h.status = CAM_REQ_CMP;
2927 /* Check that all request bits are supported. */
2928 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2929 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2933 cam_dflags = start_ccb->cdbg.flags;
2934 if (cam_dpath != NULL) {
2935 xpt_free_path(cam_dpath);
2938 if (cam_dflags != CAM_DEBUG_NONE) {
2939 if (xpt_create_path(&cam_dpath, xpt_periph,
2940 start_ccb->ccb_h.path_id,
2941 start_ccb->ccb_h.target_id,
2942 start_ccb->ccb_h.target_lun) !=
2944 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2945 cam_dflags = CAM_DEBUG_NONE;
2947 start_ccb->ccb_h.status = CAM_REQ_CMP;
2948 xpt_print(cam_dpath, "debugging flags now %x\n",
2953 start_ccb->ccb_h.status = CAM_REQ_CMP;
2957 case XPT_FREEZE_QUEUE:
2959 struct ccb_relsim *crs = &start_ccb->crs;
2961 xpt_freeze_devq_rl(path, /*runlevel*/
2962 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2963 crs->release_timeout : 0, /*count*/1);
2964 start_ccb->ccb_h.status = CAM_REQ_CMP;
2968 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2969 xpt_freeze_devq(path, 1);
2970 start_ccb->ccb_h.status = CAM_REQ_CMP;
2977 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2978 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2979 xpt_done(start_ccb);
2986 xpt_polled_action(union ccb *start_ccb)
2989 struct cam_sim *sim;
2990 struct cam_devq *devq;
2994 timeout = start_ccb->ccb_h.timeout * 10;
2995 sim = start_ccb->ccb_h.path->bus->sim;
2997 dev = start_ccb->ccb_h.path->device;
2999 mtx_assert(sim->mtx, MA_OWNED);
3001 /* Don't use ISR for this SIM while polling. */
3002 sim->flags |= CAM_SIM_POLLED;
3005 * Steal an opening so that no other queued requests
3006 * can get it before us while we simulate interrupts.
3008 dev->ccbq.devq_openings--;
3009 dev->ccbq.dev_openings--;
3011 while(((devq != NULL && devq->send_openings <= 0) ||
3012 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3014 (*(sim->sim_poll))(sim);
3015 camisr_runqueue(&sim->sim_doneq);
3018 dev->ccbq.devq_openings++;
3019 dev->ccbq.dev_openings++;
3022 xpt_action(start_ccb);
3023 while(--timeout > 0) {
3024 (*(sim->sim_poll))(sim);
3025 camisr_runqueue(&sim->sim_doneq);
3026 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3033 * XXX Is it worth adding a sim_timeout entry
3034 * point so we can attempt recovery? If
3035 * this is only used for dumps, I don't think
3038 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3041 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3044 /* We will use CAM ISR for this SIM again. */
3045 sim->flags &= ~CAM_SIM_POLLED;
3049 * Schedule a peripheral driver to receive a ccb when it's
3050 * target device has space for more transactions.
3053 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3055 struct cam_ed *device;
3058 mtx_assert(perph->sim->mtx, MA_OWNED);
3060 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3061 device = perph->path->device;
3062 if (periph_is_queued(perph)) {
3063 /* Simply reorder based on new priority */
3064 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3065 (" change priority to %d\n", new_priority));
3066 if (new_priority < perph->pinfo.priority) {
3067 camq_change_priority(&device->drvq,
3070 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3073 /* New entry on the queue */
3074 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3075 (" added periph to queue\n"));
3076 perph->pinfo.priority = new_priority;
3077 perph->pinfo.generation = ++device->drvq.generation;
3078 camq_insert(&device->drvq, &perph->pinfo);
3079 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3082 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3083 (" calling xpt_run_devq\n"));
3084 xpt_run_dev_allocq(perph->path->bus);
3090 * Schedule a device to run on a given queue.
3091 * If the device was inserted as a new entry on the queue,
3092 * return 1 meaning the device queue should be run. If we
3093 * were already queued, implying someone else has already
3094 * started the queue, return 0 so the caller doesn't attempt
3098 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3099 u_int32_t new_priority)
3102 u_int32_t old_priority;
3104 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3106 old_priority = pinfo->priority;
3109 * Are we already queued?
3111 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3112 /* Simply reorder based on new priority */
3113 if (new_priority < old_priority) {
3114 camq_change_priority(queue, pinfo->index,
3116 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3117 ("changed priority to %d\n",
3123 /* New entry on the queue */
3124 if (new_priority < old_priority)
3125 pinfo->priority = new_priority;
3127 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3128 ("Inserting onto queue\n"));
3129 pinfo->generation = ++queue->generation;
3130 camq_insert(queue, pinfo);
3137 xpt_run_dev_allocq(struct cam_eb *bus)
3139 struct cam_devq *devq;
3141 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3142 devq = bus->sim->devq;
3144 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3145 (" qfrozen_cnt == 0x%x, entries == %d, "
3146 "openings == %d, active == %d\n",
3147 devq->alloc_queue.qfrozen_cnt[0],
3148 devq->alloc_queue.entries,
3149 devq->alloc_openings,
3150 devq->alloc_active));
3152 devq->alloc_queue.qfrozen_cnt[0]++;
3153 while ((devq->alloc_queue.entries > 0)
3154 && (devq->alloc_openings > 0)
3155 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3156 struct cam_ed_qinfo *qinfo;
3157 struct cam_ed *device;
3158 union ccb *work_ccb;
3159 struct cam_periph *drv;
3162 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3164 device = qinfo->device;
3165 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3166 ("running device %p\n", device));
3168 drvq = &device->drvq;
3169 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3170 "Device on queue without any work to do"));
3171 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3172 devq->alloc_openings--;
3173 devq->alloc_active++;
3174 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3175 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3176 drv->pinfo.priority);
3177 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3178 ("calling periph start\n"));
3179 drv->periph_start(drv, work_ccb);
3182 * Malloc failure in alloc_ccb
3185 * XXX add us to a list to be run from free_ccb
3186 * if we don't have any ccbs active on this
3187 * device queue otherwise we may never get run
3193 /* We may have more work. Attempt to reschedule. */
3194 xpt_schedule_dev_allocq(bus, device);
3196 devq->alloc_queue.qfrozen_cnt[0]--;
3200 xpt_run_dev_sendq(struct cam_eb *bus)
3202 struct cam_devq *devq;
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3206 devq = bus->sim->devq;
3208 devq->send_queue.qfrozen_cnt[0]++;
3209 while ((devq->send_queue.entries > 0)
3210 && (devq->send_openings > 0)
3211 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3212 struct cam_ed_qinfo *qinfo;
3213 struct cam_ed *device;
3214 union ccb *work_ccb;
3215 struct cam_sim *sim;
3217 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3219 device = qinfo->device;
3220 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3221 ("running device %p\n", device));
3223 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3224 if (work_ccb == NULL) {
3225 printf("device on run queue with no ccbs???\n");
3229 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3231 mtx_lock(&xsoftc.xpt_lock);
3232 if (xsoftc.num_highpower <= 0) {
3234 * We got a high power command, but we
3235 * don't have any available slots. Freeze
3236 * the device queue until we have a slot
3239 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3240 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3244 mtx_unlock(&xsoftc.xpt_lock);
3248 * Consume a high power slot while
3251 xsoftc.num_highpower--;
3253 mtx_unlock(&xsoftc.xpt_lock);
3255 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3256 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3258 devq->send_openings--;
3259 devq->send_active++;
3261 xpt_schedule_dev_sendq(bus, device);
3263 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3265 * The client wants to freeze the queue
3266 * after this CCB is sent.
3268 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3271 /* In Target mode, the peripheral driver knows best... */
3272 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3273 if ((device->inq_flags & SID_CmdQue) != 0
3274 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3275 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3278 * Clear this in case of a retried CCB that
3279 * failed due to a rejected tag.
3281 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3285 * Device queues can be shared among multiple sim instances
3286 * that reside on different busses. Use the SIM in the queue
3287 * CCB's path, rather than the one in the bus that was passed
3288 * into this function.
3290 sim = work_ccb->ccb_h.path->bus->sim;
3291 (*(sim->sim_action))(sim, work_ccb);
3293 devq->send_queue.qfrozen_cnt[0]--;
3297 * This function merges stuff from the slave ccb into the master ccb, while
3298 * keeping important fields in the master ccb constant.
3301 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3305 * Pull fields that are valid for peripheral drivers to set
3306 * into the master CCB along with the CCB "payload".
3308 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3309 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3310 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3311 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3312 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3313 sizeof(union ccb) - sizeof(struct ccb_hdr));
3317 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3320 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3321 ccb_h->pinfo.priority = priority;
3323 ccb_h->path_id = path->bus->path_id;
3325 ccb_h->target_id = path->target->target_id;
3327 ccb_h->target_id = CAM_TARGET_WILDCARD;
3329 ccb_h->target_lun = path->device->lun_id;
3330 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3332 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3334 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3338 /* Path manipulation functions */
3340 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3341 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3343 struct cam_path *path;
3346 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3349 status = CAM_RESRC_UNAVAIL;
3352 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3353 if (status != CAM_REQ_CMP) {
3354 free(path, M_CAMXPT);
3357 *new_path_ptr = path;
3362 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3363 struct cam_periph *periph, path_id_t path_id,
3364 target_id_t target_id, lun_id_t lun_id)
3366 struct cam_path *path;
3367 struct cam_eb *bus = NULL;
3369 int need_unlock = 0;
3371 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3373 if (path_id != CAM_BUS_WILDCARD) {
3374 bus = xpt_find_bus(path_id);
3377 CAM_SIM_LOCK(bus->sim);
3380 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3382 CAM_SIM_UNLOCK(bus->sim);
3383 xpt_release_bus(bus);
3385 if (status != CAM_REQ_CMP) {
3386 free(path, M_CAMXPT);
3389 *new_path_ptr = path;
3394 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3395 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3398 struct cam_et *target;
3399 struct cam_ed *device;
3402 status = CAM_REQ_CMP; /* Completed without error */
3403 target = NULL; /* Wildcarded */
3404 device = NULL; /* Wildcarded */
3407 * We will potentially modify the EDT, so block interrupts
3408 * that may attempt to create cam paths.
3410 bus = xpt_find_bus(path_id);
3412 status = CAM_PATH_INVALID;
3414 target = xpt_find_target(bus, target_id);
3415 if (target == NULL) {
3417 struct cam_et *new_target;
3419 new_target = xpt_alloc_target(bus, target_id);
3420 if (new_target == NULL) {
3421 status = CAM_RESRC_UNAVAIL;
3423 target = new_target;
3426 if (target != NULL) {
3427 device = xpt_find_device(target, lun_id);
3428 if (device == NULL) {
3430 struct cam_ed *new_device;
3433 (*(bus->xport->alloc_device))(bus,
3436 if (new_device == NULL) {
3437 status = CAM_RESRC_UNAVAIL;
3439 device = new_device;
3446 * Only touch the user's data if we are successful.
3448 if (status == CAM_REQ_CMP) {
3449 new_path->periph = perph;
3450 new_path->bus = bus;
3451 new_path->target = target;
3452 new_path->device = device;
3453 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3456 xpt_release_device(device);
3458 xpt_release_target(target);
3460 xpt_release_bus(bus);
3466 xpt_release_path(struct cam_path *path)
3468 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3469 if (path->device != NULL) {
3470 xpt_release_device(path->device);
3471 path->device = NULL;
3473 if (path->target != NULL) {
3474 xpt_release_target(path->target);
3475 path->target = NULL;
3477 if (path->bus != NULL) {
3478 xpt_release_bus(path->bus);
3484 xpt_free_path(struct cam_path *path)
3487 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3488 xpt_release_path(path);
3489 free(path, M_CAMXPT);
3493 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3494 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3497 mtx_lock(&xsoftc.xpt_topo_lock);
3500 *bus_ref = path->bus->refcount;
3504 mtx_unlock(&xsoftc.xpt_topo_lock);
3507 *periph_ref = path->periph->refcount;
3513 *target_ref = path->target->refcount;
3519 *device_ref = path->device->refcount;
3526 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3527 * in path1, 2 for match with wildcards in path2.
3530 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3534 if (path1->bus != path2->bus) {
3535 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3537 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3542 if (path1->target != path2->target) {
3543 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3546 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3551 if (path1->device != path2->device) {
3552 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3555 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3564 xpt_print_path(struct cam_path *path)
3568 printf("(nopath): ");
3570 if (path->periph != NULL)
3571 printf("(%s%d:", path->periph->periph_name,
3572 path->periph->unit_number);
3574 printf("(noperiph:");
3576 if (path->bus != NULL)
3577 printf("%s%d:%d:", path->bus->sim->sim_name,
3578 path->bus->sim->unit_number,
3579 path->bus->sim->bus_id);
3583 if (path->target != NULL)
3584 printf("%d:", path->target->target_id);
3588 if (path->device != NULL)
3589 printf("%d): ", path->device->lun_id);
3596 xpt_print(struct cam_path *path, const char *fmt, ...)
3599 xpt_print_path(path);
3606 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3611 if (path != NULL && path->bus != NULL)
3612 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3615 sbuf_new(&sb, str, str_len, 0);
3618 sbuf_printf(&sb, "(nopath): ");
3620 if (path->periph != NULL)
3621 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3622 path->periph->unit_number);
3624 sbuf_printf(&sb, "(noperiph:");
3626 if (path->bus != NULL)
3627 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3628 path->bus->sim->unit_number,
3629 path->bus->sim->bus_id);
3631 sbuf_printf(&sb, "nobus:");
3633 if (path->target != NULL)
3634 sbuf_printf(&sb, "%d:", path->target->target_id);
3636 sbuf_printf(&sb, "X:");
3638 if (path->device != NULL)
3639 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3641 sbuf_printf(&sb, "X): ");
3645 return(sbuf_len(&sb));
3649 xpt_path_path_id(struct cam_path *path)
3651 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3653 return(path->bus->path_id);
3657 xpt_path_target_id(struct cam_path *path)
3659 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3661 if (path->target != NULL)
3662 return (path->target->target_id);
3664 return (CAM_TARGET_WILDCARD);
3668 xpt_path_lun_id(struct cam_path *path)
3670 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3672 if (path->device != NULL)
3673 return (path->device->lun_id);
3675 return (CAM_LUN_WILDCARD);
3679 xpt_path_sim(struct cam_path *path)
3682 return (path->bus->sim);
3686 xpt_path_periph(struct cam_path *path)
3688 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3690 return (path->periph);
3694 xpt_path_legacy_ata_id(struct cam_path *path)
3699 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3700 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3701 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3702 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3705 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3706 path->bus->sim->unit_number < 2) {
3707 bus_id = path->bus->sim->unit_number;
3711 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3712 if (bus == path->bus)
3714 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3715 bus->sim->unit_number >= 2) ||
3716 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3717 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3718 strcmp(bus->sim->sim_name, "siisch") == 0)
3723 if (path->target != NULL) {
3724 if (path->target->target_id < 2)
3725 return (bus_id * 2 + path->target->target_id);
3729 return (bus_id * 2);
3733 * Release a CAM control block for the caller. Remit the cost of the structure
3734 * to the device referenced by the path. If the this device had no 'credits'
3735 * and peripheral drivers have registered async callbacks for this notification
3739 xpt_release_ccb(union ccb *free_ccb)
3741 struct cam_path *path;
3742 struct cam_ed *device;
3744 struct cam_sim *sim;
3746 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3747 path = free_ccb->ccb_h.path;
3748 device = path->device;
3752 mtx_assert(sim->mtx, MA_OWNED);
3754 cam_ccbq_release_opening(&device->ccbq);
3755 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3756 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3757 cam_ccbq_resize(&device->ccbq,
3758 device->ccbq.dev_openings + device->ccbq.dev_active);
3760 if (sim->ccb_count > sim->max_ccbs) {
3761 xpt_free_ccb(free_ccb);
3764 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3767 if (sim->devq == NULL) {
3770 sim->devq->alloc_openings++;
3771 sim->devq->alloc_active--;
3772 if (device_is_alloc_queued(device) == 0)
3773 xpt_schedule_dev_allocq(bus, device);
3774 xpt_run_dev_allocq(bus);
3777 /* Functions accessed by SIM drivers */
3779 static struct xpt_xport xport_default = {
3780 .alloc_device = xpt_alloc_device_default,
3781 .action = xpt_action_default,
3782 .async = xpt_dev_async_default,
3786 * A sim structure, listing the SIM entry points and instance
3787 * identification info is passed to xpt_bus_register to hook the SIM
3788 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3789 * for this new bus and places it in the array of busses and assigns
3790 * it a path_id. The path_id may be influenced by "hard wiring"
3791 * information specified by the user. Once interrupt services are
3792 * available, the bus will be probed.
3795 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3797 struct cam_eb *new_bus;
3798 struct cam_eb *old_bus;
3799 struct ccb_pathinq cpi;
3800 struct cam_path *path;
3803 mtx_assert(sim->mtx, MA_OWNED);
3806 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3807 M_CAMXPT, M_NOWAIT);
3808 if (new_bus == NULL) {
3809 /* Couldn't satisfy request */
3810 return (CAM_RESRC_UNAVAIL);
3812 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3814 free(new_bus, M_CAMXPT);
3815 return (CAM_RESRC_UNAVAIL);
3818 if (strcmp(sim->sim_name, "xpt") != 0) {
3820 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3823 TAILQ_INIT(&new_bus->et_entries);
3824 new_bus->path_id = sim->path_id;
3827 timevalclear(&new_bus->last_reset);
3829 new_bus->refcount = 1; /* Held until a bus_deregister event */
3830 new_bus->generation = 0;
3832 mtx_lock(&xsoftc.xpt_topo_lock);
3833 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3834 while (old_bus != NULL
3835 && old_bus->path_id < new_bus->path_id)
3836 old_bus = TAILQ_NEXT(old_bus, links);
3837 if (old_bus != NULL)
3838 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3840 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3841 xsoftc.bus_generation++;
3842 mtx_unlock(&xsoftc.xpt_topo_lock);
3845 * Set a default transport so that a PATH_INQ can be issued to
3846 * the SIM. This will then allow for probing and attaching of
3847 * a more appropriate transport.
3849 new_bus->xport = &xport_default;
3851 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3852 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3853 if (status != CAM_REQ_CMP)
3854 printf("xpt_compile_path returned %d\n", status);
3856 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3857 cpi.ccb_h.func_code = XPT_PATH_INQ;
3858 xpt_action((union ccb *)&cpi);
3860 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3861 switch (cpi.transport) {
3868 new_bus->xport = scsi_get_xport();
3872 new_bus->xport = ata_get_xport();
3875 new_bus->xport = &xport_default;
3880 /* Notify interested parties */
3881 if (sim->path_id != CAM_XPT_PATH_ID) {
3882 union ccb *scan_ccb;
3884 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3885 /* Initiate bus rescan. */
3886 scan_ccb = xpt_alloc_ccb_nowait();
3887 scan_ccb->ccb_h.path = path;
3888 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3889 scan_ccb->crcn.flags = 0;
3890 xpt_rescan(scan_ccb);
3892 xpt_free_path(path);
3893 return (CAM_SUCCESS);
3897 xpt_bus_deregister(path_id_t pathid)
3899 struct cam_path bus_path;
3902 status = xpt_compile_path(&bus_path, NULL, pathid,
3903 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3904 if (status != CAM_REQ_CMP)
3907 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3908 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3910 /* Release the reference count held while registered. */
3911 xpt_release_bus(bus_path.bus);
3912 xpt_release_path(&bus_path);
3914 return (CAM_REQ_CMP);
3918 xptnextfreepathid(void)
3925 mtx_lock(&xsoftc.xpt_topo_lock);
3926 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3928 /* Find an unoccupied pathid */
3929 while (bus != NULL && bus->path_id <= pathid) {
3930 if (bus->path_id == pathid)
3932 bus = TAILQ_NEXT(bus, links);
3934 mtx_unlock(&xsoftc.xpt_topo_lock);
3937 * Ensure that this pathid is not reserved for
3938 * a bus that may be registered in the future.
3940 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3942 /* Start the search over */
3943 mtx_lock(&xsoftc.xpt_topo_lock);
3950 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3957 pathid = CAM_XPT_PATH_ID;
3958 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3960 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3961 if (strcmp(dname, "scbus")) {
3962 /* Avoid a bit of foot shooting. */
3965 if (dunit < 0) /* unwired?! */
3967 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3968 if (sim_bus == val) {
3972 } else if (sim_bus == 0) {
3973 /* Unspecified matches bus 0 */
3977 printf("Ambiguous scbus configuration for %s%d "
3978 "bus %d, cannot wire down. The kernel "
3979 "config entry for scbus%d should "
3980 "specify a controller bus.\n"
3981 "Scbus will be assigned dynamically.\n",
3982 sim_name, sim_unit, sim_bus, dunit);
3987 if (pathid == CAM_XPT_PATH_ID)
3988 pathid = xptnextfreepathid();
3993 xpt_async_string(u_int32_t async_code)
3996 switch (async_code) {
3997 case AC_BUS_RESET: return ("AC_BUS_RESET");
3998 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
3999 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4000 case AC_SENT_BDR: return ("AC_SENT_BDR");
4001 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4002 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4003 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4004 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4005 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4006 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4007 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4008 case AC_CONTRACT: return ("AC_CONTRACT");
4010 return ("AC_UNKNOWN");
4014 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4017 struct cam_et *target, *next_target;
4018 struct cam_ed *device, *next_device;
4020 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4021 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4022 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4025 * Most async events come from a CAM interrupt context. In
4026 * a few cases, the error recovery code at the peripheral layer,
4027 * which may run from our SWI or a process context, may signal
4028 * deferred events with a call to xpt_async.
4033 if (async_code == AC_BUS_RESET) {
4034 /* Update our notion of when the last reset occurred */
4035 microtime(&bus->last_reset);
4038 for (target = TAILQ_FIRST(&bus->et_entries);
4040 target = next_target) {
4042 next_target = TAILQ_NEXT(target, links);
4044 if (path->target != target
4045 && path->target->target_id != CAM_TARGET_WILDCARD
4046 && target->target_id != CAM_TARGET_WILDCARD)
4049 if (async_code == AC_SENT_BDR) {
4050 /* Update our notion of when the last reset occurred */
4051 microtime(&path->target->last_reset);
4054 for (device = TAILQ_FIRST(&target->ed_entries);
4056 device = next_device) {
4058 next_device = TAILQ_NEXT(device, links);
4060 if (path->device != device
4061 && path->device->lun_id != CAM_LUN_WILDCARD
4062 && device->lun_id != CAM_LUN_WILDCARD)
4065 * The async callback could free the device.
4066 * If it is a broadcast async, it doesn't hold
4067 * device reference, so take our own reference.
4069 xpt_acquire_device(device);
4070 (*(bus->xport->async))(async_code, bus,
4074 xpt_async_bcast(&device->asyncs, async_code,
4076 xpt_release_device(device);
4081 * If this wasn't a fully wildcarded async, tell all
4082 * clients that want all async events.
4084 if (bus != xpt_periph->path->bus)
4085 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4090 xpt_async_bcast(struct async_list *async_head,
4091 u_int32_t async_code,
4092 struct cam_path *path, void *async_arg)
4094 struct async_node *cur_entry;
4096 cur_entry = SLIST_FIRST(async_head);
4097 while (cur_entry != NULL) {
4098 struct async_node *next_entry;
4100 * Grab the next list entry before we call the current
4101 * entry's callback. This is because the callback function
4102 * can delete its async callback entry.
4104 next_entry = SLIST_NEXT(cur_entry, links);
4105 if ((cur_entry->event_enable & async_code) != 0)
4106 cur_entry->callback(cur_entry->callback_arg,
4109 cur_entry = next_entry;
4114 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4115 struct cam_et *target, struct cam_ed *device,
4118 printf("%s called\n", __func__);
4122 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4124 struct cam_ed *dev = path->device;
4126 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4127 dev->sim->devq->alloc_openings +=
4128 cam_ccbq_freeze(&dev->ccbq, rl, count);
4129 /* Remove frozen device from allocq. */
4130 if (device_is_alloc_queued(dev) &&
4131 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4132 CAMQ_GET_PRIO(&dev->drvq)))) {
4133 camq_remove(&dev->sim->devq->alloc_queue,
4134 dev->alloc_ccb_entry.pinfo.index);
4136 /* Remove frozen device from sendq. */
4137 if (device_is_send_queued(dev) &&
4138 cam_ccbq_frozen_top(&dev->ccbq)) {
4139 camq_remove(&dev->sim->devq->send_queue,
4140 dev->send_ccb_entry.pinfo.index);
4142 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4146 xpt_freeze_devq(struct cam_path *path, u_int count)
4149 return (xpt_freeze_devq_rl(path, 0, count));
4153 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4156 mtx_assert(sim->mtx, MA_OWNED);
4157 sim->devq->send_queue.qfrozen_cnt[0] += count;
4158 return (sim->devq->send_queue.qfrozen_cnt[0]);
4162 xpt_release_devq_timeout(void *arg)
4164 struct cam_ed *device;
4166 device = (struct cam_ed *)arg;
4168 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4172 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4174 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4176 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4180 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4182 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4184 xpt_release_devq_device(path->device, rl, count, run_queue);
4188 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4191 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4193 printf("xpt_release_devq(%d): requested %u > present %u\n",
4194 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4196 count = dev->ccbq.queue.qfrozen_cnt[rl];
4198 dev->sim->devq->alloc_openings -=
4199 cam_ccbq_release(&dev->ccbq, rl, count);
4200 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4201 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4202 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4203 xpt_run_dev_allocq(dev->target->bus);
4205 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4207 * No longer need to wait for a successful
4208 * command completion.
4210 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4212 * Remove any timeouts that might be scheduled
4213 * to release this queue.
4215 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4216 callout_stop(&dev->callout);
4217 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4222 * Now that we are unfrozen schedule the
4223 * device so any pending transactions are
4226 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4227 xpt_run_dev_sendq(dev->target->bus);
4232 xpt_release_simq(struct cam_sim *sim, int run_queue)
4236 mtx_assert(sim->mtx, MA_OWNED);
4237 sendq = &(sim->devq->send_queue);
4238 if (sendq->qfrozen_cnt[0] <= 0) {
4240 printf("xpt_release_simq: requested 1 > present %u\n",
4241 sendq->qfrozen_cnt[0]);
4244 sendq->qfrozen_cnt[0]--;
4245 if (sendq->qfrozen_cnt[0] == 0) {
4247 * If there is a timeout scheduled to release this
4248 * sim queue, remove it. The queue frozen count is
4251 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4252 callout_stop(&sim->callout);
4253 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4259 * Now that we are unfrozen run the send queue.
4261 bus = xpt_find_bus(sim->path_id);
4262 xpt_run_dev_sendq(bus);
4263 xpt_release_bus(bus);
4269 * XXX Appears to be unused.
4272 xpt_release_simq_timeout(void *arg)
4274 struct cam_sim *sim;
4276 sim = (struct cam_sim *)arg;
4277 xpt_release_simq(sim, /* run_queue */ TRUE);
4281 xpt_done(union ccb *done_ccb)
4283 struct cam_sim *sim;
4286 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4287 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4289 * Queue up the request for handling by our SWI handler
4290 * any of the "non-immediate" type of ccbs.
4292 sim = done_ccb->ccb_h.path->bus->sim;
4293 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4295 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4296 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4297 CAM_SIM_BATCH)) == 0) {
4298 mtx_lock(&cam_simq_lock);
4299 first = TAILQ_EMPTY(&cam_simq);
4300 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4301 mtx_unlock(&cam_simq_lock);
4302 sim->flags |= CAM_SIM_ON_DONEQ;
4304 swi_sched(cambio_ih, 0);
4310 xpt_batch_start(struct cam_sim *sim)
4313 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4314 sim->flags |= CAM_SIM_BATCH;
4318 xpt_batch_done(struct cam_sim *sim)
4321 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4322 sim->flags &= ~CAM_SIM_BATCH;
4323 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4324 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4325 camisr_runqueue(&sim->sim_doneq);
4333 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4338 xpt_alloc_ccb_nowait()
4342 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4347 xpt_free_ccb(union ccb *free_ccb)
4349 free(free_ccb, M_CAMXPT);
4354 /* Private XPT functions */
4357 * Get a CAM control block for the caller. Charge the structure to the device
4358 * referenced by the path. If the this device has no 'credits' then the
4359 * device already has the maximum number of outstanding operations under way
4360 * and we return NULL. If we don't have sufficient resources to allocate more
4361 * ccbs, we also return NULL.
4364 xpt_get_ccb(struct cam_ed *device)
4367 struct cam_sim *sim;
4370 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4371 new_ccb = xpt_alloc_ccb_nowait();
4372 if (new_ccb == NULL) {
4375 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4376 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4377 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4381 cam_ccbq_take_opening(&device->ccbq);
4382 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4387 xpt_release_bus(struct cam_eb *bus)
4390 mtx_lock(&xsoftc.xpt_topo_lock);
4391 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4392 if ((--bus->refcount == 0)
4393 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4394 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4395 xsoftc.bus_generation++;
4396 mtx_unlock(&xsoftc.xpt_topo_lock);
4397 cam_sim_release(bus->sim);
4398 free(bus, M_CAMXPT);
4400 mtx_unlock(&xsoftc.xpt_topo_lock);
4403 static struct cam_et *
4404 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4406 struct cam_et *target;
4408 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4409 if (target != NULL) {
4410 struct cam_et *cur_target;
4412 TAILQ_INIT(&target->ed_entries);
4414 target->target_id = target_id;
4415 target->refcount = 1;
4416 target->generation = 0;
4417 target->luns = NULL;
4418 timevalclear(&target->last_reset);
4420 * Hold a reference to our parent bus so it
4421 * will not go away before we do.
4423 mtx_lock(&xsoftc.xpt_topo_lock);
4425 mtx_unlock(&xsoftc.xpt_topo_lock);
4427 /* Insertion sort into our bus's target list */
4428 cur_target = TAILQ_FIRST(&bus->et_entries);
4429 while (cur_target != NULL && cur_target->target_id < target_id)
4430 cur_target = TAILQ_NEXT(cur_target, links);
4432 if (cur_target != NULL) {
4433 TAILQ_INSERT_BEFORE(cur_target, target, links);
4435 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4443 xpt_release_target(struct cam_et *target)
4446 if (target->refcount == 1) {
4447 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4448 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4449 target->bus->generation++;
4450 xpt_release_bus(target->bus);
4452 free(target->luns, M_CAMXPT);
4453 free(target, M_CAMXPT);
4459 static struct cam_ed *
4460 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4463 struct cam_ed *device, *cur_device;
4465 device = xpt_alloc_device(bus, target, lun_id);
4469 device->mintags = 1;
4470 device->maxtags = 1;
4471 bus->sim->max_ccbs += device->ccbq.devq_openings;
4472 cur_device = TAILQ_FIRST(&target->ed_entries);
4473 while (cur_device != NULL && cur_device->lun_id < lun_id)
4474 cur_device = TAILQ_NEXT(cur_device, links);
4475 if (cur_device != NULL) {
4476 TAILQ_INSERT_BEFORE(cur_device, device, links);
4478 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4480 target->generation++;
4486 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4488 struct cam_ed *device;
4489 struct cam_devq *devq;
4492 /* Make space for us in the device queue on our bus */
4493 devq = bus->sim->devq;
4494 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4496 if (status != CAM_REQ_CMP) {
4499 device = (struct cam_ed *)malloc(sizeof(*device),
4500 M_CAMXPT, M_NOWAIT);
4503 if (device != NULL) {
4504 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4505 device->alloc_ccb_entry.device = device;
4506 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4507 device->send_ccb_entry.device = device;
4508 device->target = target;
4509 device->lun_id = lun_id;
4510 device->sim = bus->sim;
4511 /* Initialize our queues */
4512 if (camq_init(&device->drvq, 0) != 0) {
4513 free(device, M_CAMXPT);
4516 if (cam_ccbq_init(&device->ccbq,
4517 bus->sim->max_dev_openings) != 0) {
4518 camq_fini(&device->drvq);
4519 free(device, M_CAMXPT);
4522 SLIST_INIT(&device->asyncs);
4523 SLIST_INIT(&device->periphs);
4524 device->generation = 0;
4525 device->owner = NULL;
4526 device->flags = CAM_DEV_UNCONFIGURED;
4527 device->tag_delay_count = 0;
4528 device->tag_saved_openings = 0;
4529 device->refcount = 1;
4530 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4533 * Hold a reference to our parent target so it
4534 * will not go away before we do.
4543 xpt_acquire_device(struct cam_ed *device)
4550 xpt_release_device(struct cam_ed *device)
4553 if (device->refcount == 1) {
4554 struct cam_devq *devq;
4556 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4557 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4558 panic("Removing device while still queued for ccbs");
4560 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4561 callout_stop(&device->callout);
4563 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4564 device->target->generation++;
4565 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4566 /* Release our slot in the devq */
4567 devq = device->target->bus->sim->devq;
4568 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4569 camq_fini(&device->drvq);
4570 cam_ccbq_fini(&device->ccbq);
4571 xpt_release_target(device->target);
4572 free(device, M_CAMXPT);
4578 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4586 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4587 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4588 if (result == CAM_REQ_CMP && (diff < 0)) {
4589 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4591 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4592 || (dev->inq_flags & SID_CmdQue) != 0)
4593 dev->tag_saved_openings = newopenings;
4594 /* Adjust the global limit */
4595 dev->sim->max_ccbs += diff;
4599 static struct cam_eb *
4600 xpt_find_bus(path_id_t path_id)
4604 mtx_lock(&xsoftc.xpt_topo_lock);
4605 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4607 bus = TAILQ_NEXT(bus, links)) {
4608 if (bus->path_id == path_id) {
4613 mtx_unlock(&xsoftc.xpt_topo_lock);
4617 static struct cam_et *
4618 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4620 struct cam_et *target;
4622 for (target = TAILQ_FIRST(&bus->et_entries);
4624 target = TAILQ_NEXT(target, links)) {
4625 if (target->target_id == target_id) {
4633 static struct cam_ed *
4634 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4636 struct cam_ed *device;
4638 for (device = TAILQ_FIRST(&target->ed_entries);
4640 device = TAILQ_NEXT(device, links)) {
4641 if (device->lun_id == lun_id) {
4650 xpt_start_tags(struct cam_path *path)
4652 struct ccb_relsim crs;
4653 struct cam_ed *device;
4654 struct cam_sim *sim;
4657 device = path->device;
4658 sim = path->bus->sim;
4659 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4660 xpt_freeze_devq(path, /*count*/1);
4661 device->inq_flags |= SID_CmdQue;
4662 if (device->tag_saved_openings != 0)
4663 newopenings = device->tag_saved_openings;
4665 newopenings = min(device->maxtags,
4666 sim->max_tagged_dev_openings);
4667 xpt_dev_ccbq_resize(path, newopenings);
4668 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4669 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4670 crs.ccb_h.func_code = XPT_REL_SIMQ;
4671 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4673 = crs.release_timeout
4676 xpt_action((union ccb *)&crs);
4680 xpt_stop_tags(struct cam_path *path)
4682 struct ccb_relsim crs;
4683 struct cam_ed *device;
4684 struct cam_sim *sim;
4686 device = path->device;
4687 sim = path->bus->sim;
4688 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4689 device->tag_delay_count = 0;
4690 xpt_freeze_devq(path, /*count*/1);
4691 device->inq_flags &= ~SID_CmdQue;
4692 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4693 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4694 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4695 crs.ccb_h.func_code = XPT_REL_SIMQ;
4696 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4698 = crs.release_timeout
4701 xpt_action((union ccb *)&crs);
4705 xpt_boot_delay(void *arg)
4712 xpt_config(void *arg)
4715 * Now that interrupts are enabled, go find our devices
4718 /* Setup debugging path */
4719 if (cam_dflags != CAM_DEBUG_NONE) {
4721 * Locking is specifically omitted here. No SIMs have
4722 * registered yet, so xpt_create_path will only be searching
4723 * empty lists of targets and devices.
4725 if (xpt_create_path(&cam_dpath, xpt_periph,
4726 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4727 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4728 printf("xpt_config: xpt_create_path() failed for debug"
4729 " target %d:%d:%d, debugging disabled\n",
4730 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4731 cam_dflags = CAM_DEBUG_NONE;
4736 periphdriver_init(1);
4738 callout_init(&xsoftc.boot_callout, 1);
4739 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4740 xpt_boot_delay, NULL);
4741 /* Fire up rescan thread. */
4742 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4743 printf("xpt_init: failed to create rescan thread\n");
4751 xsoftc.buses_to_config++;
4756 xpt_release_boot(void)
4759 xsoftc.buses_to_config--;
4760 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4761 struct xpt_task *task;
4763 xsoftc.buses_config_done = 1;
4765 /* Call manually because we don't have any busses */
4766 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4768 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4769 taskqueue_enqueue(taskqueue_thread, &task->task);
4776 * If the given device only has one peripheral attached to it, and if that
4777 * peripheral is the passthrough driver, announce it. This insures that the
4778 * user sees some sort of announcement for every peripheral in their system.
4781 xptpassannouncefunc(struct cam_ed *device, void *arg)
4783 struct cam_periph *periph;
4786 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4787 periph = SLIST_NEXT(periph, periph_links), i++);
4789 periph = SLIST_FIRST(&device->periphs);
4791 && (strncmp(periph->periph_name, "pass", 4) == 0))
4792 xpt_announce_periph(periph, NULL);
4798 xpt_finishconfig_task(void *context, int pending)
4801 periphdriver_init(2);
4803 * Check for devices with no "standard" peripheral driver
4804 * attached. For any devices like that, announce the
4805 * passthrough driver so the user will see something.
4807 xpt_for_all_devices(xptpassannouncefunc, NULL);
4809 /* Release our hook so that the boot can continue. */
4810 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4811 free(xsoftc.xpt_config_hook, M_CAMXPT);
4812 xsoftc.xpt_config_hook = NULL;
4814 free(context, M_CAMXPT);
4818 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4819 struct cam_path *path)
4821 struct ccb_setasync csa;
4826 mtx_lock(&xsoftc.xpt_lock);
4827 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4828 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4829 if (status != CAM_REQ_CMP) {
4830 mtx_unlock(&xsoftc.xpt_lock);
4836 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4837 csa.ccb_h.func_code = XPT_SASYNC_CB;
4838 csa.event_enable = event;
4839 csa.callback = cbfunc;
4840 csa.callback_arg = cbarg;
4841 xpt_action((union ccb *)&csa);
4842 status = csa.ccb_h.status;
4844 xpt_free_path(path);
4845 mtx_unlock(&xsoftc.xpt_lock);
4847 if ((status == CAM_REQ_CMP) &&
4848 (csa.event_enable & AC_FOUND_DEVICE)) {
4850 * Get this peripheral up to date with all
4851 * the currently existing devices.
4853 xpt_for_all_devices(xptsetasyncfunc, &csa);
4855 if ((status == CAM_REQ_CMP) &&
4856 (csa.event_enable & AC_PATH_REGISTERED)) {
4858 * Get this peripheral up to date with all
4859 * the currently existing busses.
4861 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4868 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4870 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4872 switch (work_ccb->ccb_h.func_code) {
4873 /* Common cases first */
4874 case XPT_PATH_INQ: /* Path routing inquiry */
4876 struct ccb_pathinq *cpi;
4878 cpi = &work_ccb->cpi;
4879 cpi->version_num = 1; /* XXX??? */
4880 cpi->hba_inquiry = 0;
4881 cpi->target_sprt = 0;
4883 cpi->hba_eng_cnt = 0;
4884 cpi->max_target = 0;
4886 cpi->initiator_id = 0;
4887 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4888 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4889 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4890 cpi->unit_number = sim->unit_number;
4891 cpi->bus_id = sim->bus_id;
4892 cpi->base_transfer_speed = 0;
4893 cpi->protocol = PROTO_UNSPECIFIED;
4894 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4895 cpi->transport = XPORT_UNSPECIFIED;
4896 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4897 cpi->ccb_h.status = CAM_REQ_CMP;
4902 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4909 * The xpt as a "controller" has no interrupt sources, so polling
4913 xptpoll(struct cam_sim *sim)
4918 xpt_lock_buses(void)
4920 mtx_lock(&xsoftc.xpt_topo_lock);
4924 xpt_unlock_buses(void)
4926 mtx_unlock(&xsoftc.xpt_topo_lock);
4933 struct cam_sim *sim;
4935 mtx_lock(&cam_simq_lock);
4937 while (!TAILQ_EMPTY(&cam_simq)) {
4938 TAILQ_CONCAT(&queue, &cam_simq, links);
4939 mtx_unlock(&cam_simq_lock);
4941 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4942 TAILQ_REMOVE(&queue, sim, links);
4944 camisr_runqueue(&sim->sim_doneq);
4945 sim->flags &= ~CAM_SIM_ON_DONEQ;
4946 CAM_SIM_UNLOCK(sim);
4948 mtx_lock(&cam_simq_lock);
4950 mtx_unlock(&cam_simq_lock);
4954 camisr_runqueue(void *V_queue)
4956 cam_isrq_t *queue = V_queue;
4957 struct ccb_hdr *ccb_h;
4959 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4962 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4963 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4965 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4970 if (ccb_h->flags & CAM_HIGH_POWER) {
4971 struct highpowerlist *hphead;
4972 union ccb *send_ccb;
4974 mtx_lock(&xsoftc.xpt_lock);
4975 hphead = &xsoftc.highpowerq;
4977 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4980 * Increment the count since this command is done.
4982 xsoftc.num_highpower++;
4985 * Any high powered commands queued up?
4987 if (send_ccb != NULL) {
4989 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4990 mtx_unlock(&xsoftc.xpt_lock);
4992 xpt_release_devq(send_ccb->ccb_h.path,
4993 /*count*/1, /*runqueue*/TRUE);
4995 mtx_unlock(&xsoftc.xpt_lock);
4998 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5001 dev = ccb_h->path->device;
5003 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5004 ccb_h->path->bus->sim->devq->send_active--;
5005 ccb_h->path->bus->sim->devq->send_openings++;
5008 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5009 && (dev->ccbq.dev_active == 0))) {
5010 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5011 xpt_release_devq(ccb_h->path, /*count*/1,
5012 /*run_queue*/FALSE);
5015 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5016 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5017 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5018 xpt_release_devq(ccb_h->path, /*count*/1,
5019 /*run_queue*/FALSE);
5022 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5023 && (--dev->tag_delay_count == 0))
5024 xpt_start_tags(ccb_h->path);
5025 if (!device_is_send_queued(dev))
5026 xpt_schedule_dev_sendq(ccb_h->path->bus, dev);
5029 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5030 xpt_release_simq(ccb_h->path->bus->sim,
5032 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5036 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5037 && (ccb_h->status & CAM_DEV_QFRZN)) {
5038 xpt_release_devq(ccb_h->path, /*count*/1,
5040 ccb_h->status &= ~CAM_DEV_QFRZN;
5042 xpt_run_dev_sendq(ccb_h->path->bus);
5045 /* Call the peripheral driver's callback */
5046 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);