2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_xpt_internal.h>
60 #include <cam/cam_debug.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
66 #include <machine/md_var.h> /* geometry translation */
67 #include <machine/stdarg.h> /* for xpt_print below */
72 * This is the maximum number of high powered commands (e.g. start unit)
73 * that can be outstanding at a particular time.
75 #ifndef CAM_MAX_HIGHPOWER
76 #define CAM_MAX_HIGHPOWER 4
79 /* Datastructures internal to the xpt layer */
80 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
82 /* Object for defering XPT actions to a taskqueue */
95 u_int32_t xpt_generation;
97 /* number of high powered commands that can go through right now */
98 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
101 /* queue for handling async rescan requests. */
102 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
104 int buses_config_done;
106 /* Registered busses */
107 TAILQ_HEAD(,cam_eb) xpt_busses;
108 u_int bus_generation;
110 struct intr_config_hook *xpt_config_hook;
113 struct callout boot_callout;
115 struct mtx xpt_topo_lock;
121 DM_RET_FLAG_MASK = 0x0f,
124 DM_RET_DESCEND = 0x20,
126 DM_RET_ACTION_MASK = 0xf0
134 } xpt_traverse_depth;
136 struct xpt_traverse_config {
137 xpt_traverse_depth depth;
142 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
143 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
144 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
145 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
146 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
148 /* Transport layer configuration information */
149 static struct xpt_softc xsoftc;
151 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
152 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
153 &xsoftc.boot_delay, 0, "Bus registration wait time");
155 /* Queues for our software interrupt handler */
156 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
157 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
158 static cam_simq_t cam_simq;
159 static struct mtx cam_simq_lock;
161 /* Pointers to software interrupt handlers */
162 static void *cambio_ih;
164 struct cam_periph *xpt_periph;
166 static periph_init_t xpt_periph_init;
168 static struct periph_driver xpt_driver =
170 xpt_periph_init, "xpt",
171 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
175 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
177 static d_open_t xptopen;
178 static d_close_t xptclose;
179 static d_ioctl_t xptioctl;
181 static struct cdevsw xpt_cdevsw = {
182 .d_version = D_VERSION,
190 /* Storage for debugging datastructures */
192 struct cam_path *cam_dpath;
193 #ifdef CAM_DEBUG_FLAGS
194 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
196 u_int32_t cam_dflags = CAM_DEBUG_NONE;
198 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
199 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
200 &cam_dflags, 0, "Cam Debug Flags");
201 u_int32_t cam_debug_delay;
202 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
203 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
204 &cam_debug_delay, 0, "Cam Debug Flags");
207 /* Our boot-time initialization hook */
208 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
210 static moduledata_t cam_moduledata = {
212 cam_module_event_handler,
216 static int xpt_init(void *);
218 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
219 MODULE_VERSION(cam, 1);
222 static void xpt_async_bcast(struct async_list *async_head,
223 u_int32_t async_code,
224 struct cam_path *path,
226 static path_id_t xptnextfreepathid(void);
227 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
228 static union ccb *xpt_get_ccb(struct cam_ed *device);
229 static void xpt_run_dev_allocq(struct cam_eb *bus);
230 static void xpt_run_dev_sendq(struct cam_eb *bus);
231 static timeout_t xpt_release_devq_timeout;
232 static void xpt_release_simq_timeout(void *arg) __unused;
233 static void xpt_release_bus(struct cam_eb *bus);
234 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
235 u_int count, int run_queue);
236 static struct cam_et*
237 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
238 static void xpt_release_target(struct cam_et *target);
239 static struct cam_eb*
240 xpt_find_bus(path_id_t path_id);
241 static struct cam_et*
242 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
243 static struct cam_ed*
244 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
245 static void xpt_config(void *arg);
246 static xpt_devicefunc_t xptpassannouncefunc;
247 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
248 static void xptpoll(struct cam_sim *sim);
249 static void camisr(void *);
250 static void camisr_runqueue(void *);
251 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
252 u_int num_patterns, struct cam_eb *bus);
253 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
255 struct cam_ed *device);
256 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
258 struct cam_periph *periph);
259 static xpt_busfunc_t xptedtbusfunc;
260 static xpt_targetfunc_t xptedttargetfunc;
261 static xpt_devicefunc_t xptedtdevicefunc;
262 static xpt_periphfunc_t xptedtperiphfunc;
263 static xpt_pdrvfunc_t xptplistpdrvfunc;
264 static xpt_periphfunc_t xptplistperiphfunc;
265 static int xptedtmatch(struct ccb_dev_match *cdm);
266 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
267 static int xptbustraverse(struct cam_eb *start_bus,
268 xpt_busfunc_t *tr_func, void *arg);
269 static int xpttargettraverse(struct cam_eb *bus,
270 struct cam_et *start_target,
271 xpt_targetfunc_t *tr_func, void *arg);
272 static int xptdevicetraverse(struct cam_et *target,
273 struct cam_ed *start_device,
274 xpt_devicefunc_t *tr_func, void *arg);
275 static int xptperiphtraverse(struct cam_ed *device,
276 struct cam_periph *start_periph,
277 xpt_periphfunc_t *tr_func, void *arg);
278 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
279 xpt_pdrvfunc_t *tr_func, void *arg);
280 static int xptpdperiphtraverse(struct periph_driver **pdrv,
281 struct cam_periph *start_periph,
282 xpt_periphfunc_t *tr_func,
284 static xpt_busfunc_t xptdefbusfunc;
285 static xpt_targetfunc_t xptdeftargetfunc;
286 static xpt_devicefunc_t xptdefdevicefunc;
287 static xpt_periphfunc_t xptdefperiphfunc;
288 static void xpt_finishconfig_task(void *context, int pending);
289 static void xpt_dev_async_default(u_int32_t async_code,
291 struct cam_et *target,
292 struct cam_ed *device,
294 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
295 struct cam_et *target,
297 static xpt_devicefunc_t xptsetasyncfunc;
298 static xpt_busfunc_t xptsetasyncbusfunc;
299 static cam_status xptregister(struct cam_periph *periph,
301 static __inline int periph_is_queued(struct cam_periph *periph);
302 static __inline int device_is_alloc_queued(struct cam_ed *device);
303 static __inline int device_is_send_queued(struct cam_ed *device);
306 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
310 if ((dev->drvq.entries > 0) &&
311 (dev->ccbq.devq_openings > 0) &&
312 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
313 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
315 * The priority of a device waiting for CCB resources
316 * is that of the highest priority peripheral driver
319 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
320 &dev->alloc_ccb_entry.pinfo,
321 CAMQ_GET_PRIO(&dev->drvq));
330 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
334 if ((dev->ccbq.queue.entries > 0) &&
335 (dev->ccbq.dev_openings > 0) &&
336 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
338 * The priority of a device waiting for controller
339 * resources is that of the highest priority CCB
343 xpt_schedule_dev(&bus->sim->devq->send_queue,
344 &dev->send_ccb_entry.pinfo,
345 CAMQ_GET_PRIO(&dev->ccbq.queue));
353 periph_is_queued(struct cam_periph *periph)
355 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
359 device_is_alloc_queued(struct cam_ed *device)
361 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
365 device_is_send_queued(struct cam_ed *device)
367 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
373 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
377 xptdone(struct cam_periph *periph, union ccb *done_ccb)
379 /* Caller will release the CCB */
380 wakeup(&done_ccb->ccb_h.cbfcnp);
384 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
388 * Only allow read-write access.
390 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
394 * We don't allow nonblocking access.
396 if ((flags & O_NONBLOCK) != 0) {
397 printf("%s: can't do nonblocking access\n", devtoname(dev));
401 /* Mark ourselves open */
402 mtx_lock(&xsoftc.xpt_lock);
403 xsoftc.flags |= XPT_FLAG_OPEN;
404 mtx_unlock(&xsoftc.xpt_lock);
410 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
413 /* Mark ourselves closed */
414 mtx_lock(&xsoftc.xpt_lock);
415 xsoftc.flags &= ~XPT_FLAG_OPEN;
416 mtx_unlock(&xsoftc.xpt_lock);
422 * Don't automatically grab the xpt softc lock here even though this is going
423 * through the xpt device. The xpt device is really just a back door for
424 * accessing other devices and SIMs, so the right thing to do is to grab
425 * the appropriate SIM lock once the bus/SIM is located.
428 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
436 * For the transport layer CAMIOCOMMAND ioctl, we really only want
437 * to accept CCB types that don't quite make sense to send through a
438 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
446 inccb = (union ccb *)addr;
448 bus = xpt_find_bus(inccb->ccb_h.path_id);
452 switch (inccb->ccb_h.func_code) {
455 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
456 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
457 xpt_release_bus(bus);
462 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
463 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
464 xpt_release_bus(bus);
472 switch(inccb->ccb_h.func_code) {
480 ccb = xpt_alloc_ccb();
482 CAM_SIM_LOCK(bus->sim);
485 * Create a path using the bus, target, and lun the
488 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
489 inccb->ccb_h.path_id,
490 inccb->ccb_h.target_id,
491 inccb->ccb_h.target_lun) !=
494 CAM_SIM_UNLOCK(bus->sim);
498 /* Ensure all of our fields are correct */
499 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
500 inccb->ccb_h.pinfo.priority);
501 xpt_merge_ccb(ccb, inccb);
502 ccb->ccb_h.cbfcnp = xptdone;
503 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
504 bcopy(ccb, inccb, sizeof(union ccb));
505 xpt_free_path(ccb->ccb_h.path);
507 CAM_SIM_UNLOCK(bus->sim);
514 * This is an immediate CCB, so it's okay to
515 * allocate it on the stack.
518 CAM_SIM_LOCK(bus->sim);
521 * Create a path using the bus, target, and lun the
524 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
525 inccb->ccb_h.path_id,
526 inccb->ccb_h.target_id,
527 inccb->ccb_h.target_lun) !=
530 CAM_SIM_UNLOCK(bus->sim);
533 /* Ensure all of our fields are correct */
534 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
535 inccb->ccb_h.pinfo.priority);
536 xpt_merge_ccb(&ccb, inccb);
537 ccb.ccb_h.cbfcnp = xptdone;
539 CAM_SIM_UNLOCK(bus->sim);
540 bcopy(&ccb, inccb, sizeof(union ccb));
541 xpt_free_path(ccb.ccb_h.path);
545 case XPT_DEV_MATCH: {
546 struct cam_periph_map_info mapinfo;
547 struct cam_path *old_path;
550 * We can't deal with physical addresses for this
551 * type of transaction.
553 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
559 * Save this in case the caller had it set to
560 * something in particular.
562 old_path = inccb->ccb_h.path;
565 * We really don't need a path for the matching
566 * code. The path is needed because of the
567 * debugging statements in xpt_action(). They
568 * assume that the CCB has a valid path.
570 inccb->ccb_h.path = xpt_periph->path;
572 bzero(&mapinfo, sizeof(mapinfo));
575 * Map the pattern and match buffers into kernel
576 * virtual address space.
578 error = cam_periph_mapmem(inccb, &mapinfo);
581 inccb->ccb_h.path = old_path;
586 * This is an immediate CCB, we can send it on directly.
591 * Map the buffers back into user space.
593 cam_periph_unmapmem(inccb, &mapinfo);
595 inccb->ccb_h.path = old_path;
604 xpt_release_bus(bus);
608 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
609 * with the periphal driver name and unit name filled in. The other
610 * fields don't really matter as input. The passthrough driver name
611 * ("pass"), and unit number are passed back in the ccb. The current
612 * device generation number, and the index into the device peripheral
613 * driver list, and the status are also passed back. Note that
614 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
615 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
616 * (or rather should be) impossible for the device peripheral driver
617 * list to change since we look at the whole thing in one pass, and
618 * we do it with lock protection.
621 case CAMGETPASSTHRU: {
623 struct cam_periph *periph;
624 struct periph_driver **p_drv;
627 u_int cur_generation;
628 int base_periph_found;
631 ccb = (union ccb *)addr;
632 unit = ccb->cgdl.unit_number;
633 name = ccb->cgdl.periph_name;
635 * Every 100 devices, we want to drop our lock protection to
636 * give the software interrupt handler a chance to run.
637 * Most systems won't run into this check, but this should
638 * avoid starvation in the software interrupt handler in
643 ccb = (union ccb *)addr;
645 base_periph_found = 0;
648 * Sanity check -- make sure we don't get a null peripheral
651 if (*ccb->cgdl.periph_name == '\0') {
656 /* Keep the list from changing while we traverse it */
657 mtx_lock(&xsoftc.xpt_topo_lock);
659 cur_generation = xsoftc.xpt_generation;
661 /* first find our driver in the list of drivers */
662 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
663 if (strcmp((*p_drv)->driver_name, name) == 0)
666 if (*p_drv == NULL) {
667 mtx_unlock(&xsoftc.xpt_topo_lock);
668 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
669 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
670 *ccb->cgdl.periph_name = '\0';
671 ccb->cgdl.unit_number = 0;
677 * Run through every peripheral instance of this driver
678 * and check to see whether it matches the unit passed
679 * in by the user. If it does, get out of the loops and
680 * find the passthrough driver associated with that
683 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
684 periph = TAILQ_NEXT(periph, unit_links)) {
686 if (periph->unit_number == unit) {
688 } else if (--splbreaknum == 0) {
689 mtx_unlock(&xsoftc.xpt_topo_lock);
690 mtx_lock(&xsoftc.xpt_topo_lock);
692 if (cur_generation != xsoftc.xpt_generation)
697 * If we found the peripheral driver that the user passed
698 * in, go through all of the peripheral drivers for that
699 * particular device and look for a passthrough driver.
701 if (periph != NULL) {
702 struct cam_ed *device;
705 base_periph_found = 1;
706 device = periph->path->device;
707 for (i = 0, periph = SLIST_FIRST(&device->periphs);
709 periph = SLIST_NEXT(periph, periph_links), i++) {
711 * Check to see whether we have a
712 * passthrough device or not.
714 if (strcmp(periph->periph_name, "pass") == 0) {
716 * Fill in the getdevlist fields.
718 strcpy(ccb->cgdl.periph_name,
719 periph->periph_name);
720 ccb->cgdl.unit_number =
722 if (SLIST_NEXT(periph, periph_links))
724 CAM_GDEVLIST_MORE_DEVS;
727 CAM_GDEVLIST_LAST_DEVICE;
728 ccb->cgdl.generation =
732 * Fill in some CCB header fields
733 * that the user may want.
736 periph->path->bus->path_id;
737 ccb->ccb_h.target_id =
738 periph->path->target->target_id;
739 ccb->ccb_h.target_lun =
740 periph->path->device->lun_id;
741 ccb->ccb_h.status = CAM_REQ_CMP;
748 * If the periph is null here, one of two things has
749 * happened. The first possibility is that we couldn't
750 * find the unit number of the particular peripheral driver
751 * that the user is asking about. e.g. the user asks for
752 * the passthrough driver for "da11". We find the list of
753 * "da" peripherals all right, but there is no unit 11.
754 * The other possibility is that we went through the list
755 * of peripheral drivers attached to the device structure,
756 * but didn't find one with the name "pass". Either way,
757 * we return ENOENT, since we couldn't find something.
759 if (periph == NULL) {
760 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
761 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
762 *ccb->cgdl.periph_name = '\0';
763 ccb->cgdl.unit_number = 0;
766 * It is unfortunate that this is even necessary,
767 * but there are many, many clueless users out there.
768 * If this is true, the user is looking for the
769 * passthrough driver, but doesn't have one in his
772 if (base_periph_found == 1) {
773 printf("xptioctl: pass driver is not in the "
775 printf("xptioctl: put \"device pass\" in "
776 "your kernel config file\n");
779 mtx_unlock(&xsoftc.xpt_topo_lock);
791 cam_module_event_handler(module_t mod, int what, void *arg)
797 if ((error = xpt_init(NULL)) != 0)
810 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
813 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
814 xpt_free_path(done_ccb->ccb_h.path);
815 xpt_free_ccb(done_ccb);
817 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
818 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
823 /* thread to handle bus rescans */
825 xpt_scanner_thread(void *dummy)
832 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
833 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
835 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
836 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
839 sim = ccb->ccb_h.path->bus->sim;
850 xpt_rescan(union ccb *ccb)
854 /* Prepare request */
855 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
856 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
857 ccb->ccb_h.func_code = XPT_SCAN_BUS;
858 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
859 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
860 ccb->ccb_h.func_code = XPT_SCAN_TGT;
861 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
862 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
863 ccb->ccb_h.func_code = XPT_SCAN_LUN;
865 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
866 xpt_free_path(ccb->ccb_h.path);
870 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
871 ccb->ccb_h.cbfcnp = xpt_rescan_done;
872 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
873 /* Don't make duplicate entries for the same paths. */
875 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
876 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
877 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
878 wakeup(&xsoftc.ccb_scanq);
880 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
881 xpt_free_path(ccb->ccb_h.path);
887 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
888 xsoftc.buses_to_config++;
889 wakeup(&xsoftc.ccb_scanq);
893 /* Functions accessed by the peripheral drivers */
895 xpt_init(void *dummy)
897 struct cam_sim *xpt_sim;
898 struct cam_path *path;
899 struct cam_devq *devq;
902 TAILQ_INIT(&xsoftc.xpt_busses);
903 TAILQ_INIT(&cam_simq);
904 TAILQ_INIT(&xsoftc.ccb_scanq);
905 STAILQ_INIT(&xsoftc.highpowerq);
906 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
908 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
909 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
910 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
913 * The xpt layer is, itself, the equivelent of a SIM.
914 * Allow 16 ccbs in the ccb pool for it. This should
915 * give decent parallelism when we probe busses and
916 * perform other XPT functions.
918 devq = cam_simq_alloc(16);
919 xpt_sim = cam_sim_alloc(xptaction,
924 /*mtx*/&xsoftc.xpt_lock,
925 /*max_dev_transactions*/0,
926 /*max_tagged_dev_transactions*/0,
931 mtx_lock(&xsoftc.xpt_lock);
932 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
933 mtx_unlock(&xsoftc.xpt_lock);
934 printf("xpt_init: xpt_bus_register failed with status %#x,"
935 " failing attach\n", status);
940 * Looking at the XPT from the SIM layer, the XPT is
941 * the equivelent of a peripheral driver. Allocate
942 * a peripheral driver entry for us.
944 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
946 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
947 mtx_unlock(&xsoftc.xpt_lock);
948 printf("xpt_init: xpt_create_path failed with status %#x,"
949 " failing attach\n", status);
953 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
954 path, NULL, 0, xpt_sim);
956 mtx_unlock(&xsoftc.xpt_lock);
957 /* Install our software interrupt handlers */
958 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
960 * Register a callback for when interrupts are enabled.
962 xsoftc.xpt_config_hook =
963 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
964 M_CAMXPT, M_NOWAIT | M_ZERO);
965 if (xsoftc.xpt_config_hook == NULL) {
966 printf("xpt_init: Cannot malloc config hook "
967 "- failing attach\n");
970 xsoftc.xpt_config_hook->ich_func = xpt_config;
971 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
972 free (xsoftc.xpt_config_hook, M_CAMXPT);
973 printf("xpt_init: config_intrhook_establish failed "
974 "- failing attach\n");
981 xptregister(struct cam_periph *periph, void *arg)
983 struct cam_sim *xpt_sim;
985 if (periph == NULL) {
986 printf("xptregister: periph was NULL!!\n");
987 return(CAM_REQ_CMP_ERR);
990 xpt_sim = (struct cam_sim *)arg;
991 xpt_sim->softc = periph;
993 periph->softc = NULL;
999 xpt_add_periph(struct cam_periph *periph)
1001 struct cam_ed *device;
1003 struct periph_list *periph_head;
1005 mtx_assert(periph->sim->mtx, MA_OWNED);
1007 device = periph->path->device;
1009 periph_head = &device->periphs;
1011 status = CAM_REQ_CMP;
1013 if (device != NULL) {
1015 * Make room for this peripheral
1016 * so it will fit in the queue
1017 * when it's scheduled to run
1019 status = camq_resize(&device->drvq,
1020 device->drvq.array_size + 1);
1022 device->generation++;
1024 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1027 mtx_lock(&xsoftc.xpt_topo_lock);
1028 xsoftc.xpt_generation++;
1029 mtx_unlock(&xsoftc.xpt_topo_lock);
1035 xpt_remove_periph(struct cam_periph *periph)
1037 struct cam_ed *device;
1039 mtx_assert(periph->sim->mtx, MA_OWNED);
1041 device = periph->path->device;
1043 if (device != NULL) {
1044 struct periph_list *periph_head;
1046 periph_head = &device->periphs;
1048 /* Release the slot for this peripheral */
1049 camq_resize(&device->drvq, device->drvq.array_size - 1);
1051 device->generation++;
1053 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1056 mtx_lock(&xsoftc.xpt_topo_lock);
1057 xsoftc.xpt_generation++;
1058 mtx_unlock(&xsoftc.xpt_topo_lock);
1063 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1065 struct cam_path *path = periph->path;
1067 mtx_assert(periph->sim->mtx, MA_OWNED);
1069 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1070 periph->periph_name, periph->unit_number,
1071 path->bus->sim->sim_name,
1072 path->bus->sim->unit_number,
1073 path->bus->sim->bus_id,
1075 path->target->target_id,
1076 path->device->lun_id);
1077 printf("%s%d: ", periph->periph_name, periph->unit_number);
1078 if (path->device->protocol == PROTO_SCSI)
1079 scsi_print_inquiry(&path->device->inq_data);
1080 else if (path->device->protocol == PROTO_ATA ||
1081 path->device->protocol == PROTO_SATAPM)
1082 ata_print_ident(&path->device->ident_data);
1084 printf("Unknown protocol device\n");
1085 if (bootverbose && path->device->serial_num_len > 0) {
1086 /* Don't wrap the screen - print only the first 60 chars */
1087 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1088 periph->unit_number, path->device->serial_num);
1090 /* Announce transport details. */
1091 (*(path->bus->xport->announce))(periph);
1092 /* Announce command queueing. */
1093 if (path->device->inq_flags & SID_CmdQue
1094 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1095 printf("%s%d: Command Queueing enabled\n",
1096 periph->periph_name, periph->unit_number);
1098 /* Announce caller's details if they've passed in. */
1099 if (announce_string != NULL)
1100 printf("%s%d: %s\n", periph->periph_name,
1101 periph->unit_number, announce_string);
1105 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1108 struct ccb_dev_advinfo cdai;
1110 memset(&cdai, 0, sizeof(cdai));
1111 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1112 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1115 if (!strcmp(attr, "GEOM::ident"))
1116 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1117 else if (!strcmp(attr, "GEOM::physpath"))
1118 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1122 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1123 if (cdai.buf == NULL) {
1127 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1128 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1129 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1130 if (cdai.provsiz == 0)
1133 if (strlcpy(buf, cdai.buf, len) >= len)
1137 if (cdai.buf != NULL)
1138 free(cdai.buf, M_CAMXPT);
1142 static dev_match_ret
1143 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1146 dev_match_ret retval;
1149 retval = DM_RET_NONE;
1152 * If we aren't given something to match against, that's an error.
1155 return(DM_RET_ERROR);
1158 * If there are no match entries, then this bus matches no
1161 if ((patterns == NULL) || (num_patterns == 0))
1162 return(DM_RET_DESCEND | DM_RET_COPY);
1164 for (i = 0; i < num_patterns; i++) {
1165 struct bus_match_pattern *cur_pattern;
1168 * If the pattern in question isn't for a bus node, we
1169 * aren't interested. However, we do indicate to the
1170 * calling routine that we should continue descending the
1171 * tree, since the user wants to match against lower-level
1174 if (patterns[i].type != DEV_MATCH_BUS) {
1175 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1176 retval |= DM_RET_DESCEND;
1180 cur_pattern = &patterns[i].pattern.bus_pattern;
1183 * If they want to match any bus node, we give them any
1186 if (cur_pattern->flags == BUS_MATCH_ANY) {
1187 /* set the copy flag */
1188 retval |= DM_RET_COPY;
1191 * If we've already decided on an action, go ahead
1194 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1199 * Not sure why someone would do this...
1201 if (cur_pattern->flags == BUS_MATCH_NONE)
1204 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1205 && (cur_pattern->path_id != bus->path_id))
1208 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1209 && (cur_pattern->bus_id != bus->sim->bus_id))
1212 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1213 && (cur_pattern->unit_number != bus->sim->unit_number))
1216 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1217 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1222 * If we get to this point, the user definitely wants
1223 * information on this bus. So tell the caller to copy the
1226 retval |= DM_RET_COPY;
1229 * If the return action has been set to descend, then we
1230 * know that we've already seen a non-bus matching
1231 * expression, therefore we need to further descend the tree.
1232 * This won't change by continuing around the loop, so we
1233 * go ahead and return. If we haven't seen a non-bus
1234 * matching expression, we keep going around the loop until
1235 * we exhaust the matching expressions. We'll set the stop
1236 * flag once we fall out of the loop.
1238 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1243 * If the return action hasn't been set to descend yet, that means
1244 * we haven't seen anything other than bus matching patterns. So
1245 * tell the caller to stop descending the tree -- the user doesn't
1246 * want to match against lower level tree elements.
1248 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1249 retval |= DM_RET_STOP;
1254 static dev_match_ret
1255 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1256 struct cam_ed *device)
1258 dev_match_ret retval;
1261 retval = DM_RET_NONE;
1264 * If we aren't given something to match against, that's an error.
1267 return(DM_RET_ERROR);
1270 * If there are no match entries, then this device matches no
1273 if ((patterns == NULL) || (num_patterns == 0))
1274 return(DM_RET_DESCEND | DM_RET_COPY);
1276 for (i = 0; i < num_patterns; i++) {
1277 struct device_match_pattern *cur_pattern;
1278 struct scsi_vpd_device_id *device_id_page;
1281 * If the pattern in question isn't for a device node, we
1282 * aren't interested.
1284 if (patterns[i].type != DEV_MATCH_DEVICE) {
1285 if ((patterns[i].type == DEV_MATCH_PERIPH)
1286 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1287 retval |= DM_RET_DESCEND;
1291 cur_pattern = &patterns[i].pattern.device_pattern;
1293 /* Error out if mutually exclusive options are specified. */
1294 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1295 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1296 return(DM_RET_ERROR);
1299 * If they want to match any device node, we give them any
1302 if (cur_pattern->flags == DEV_MATCH_ANY)
1306 * Not sure why someone would do this...
1308 if (cur_pattern->flags == DEV_MATCH_NONE)
1311 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1312 && (cur_pattern->path_id != device->target->bus->path_id))
1315 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1316 && (cur_pattern->target_id != device->target->target_id))
1319 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1320 && (cur_pattern->target_lun != device->lun_id))
1323 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1324 && (cam_quirkmatch((caddr_t)&device->inq_data,
1325 (caddr_t)&cur_pattern->data.inq_pat,
1326 1, sizeof(cur_pattern->data.inq_pat),
1327 scsi_static_inquiry_match) == NULL))
1330 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1331 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1332 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1333 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1334 device->device_id_len
1335 - SVPD_DEVICE_ID_HDR_LEN,
1336 cur_pattern->data.devid_pat.id,
1337 cur_pattern->data.devid_pat.id_len) != 0))
1342 * If we get to this point, the user definitely wants
1343 * information on this device. So tell the caller to copy
1346 retval |= DM_RET_COPY;
1349 * If the return action has been set to descend, then we
1350 * know that we've already seen a peripheral matching
1351 * expression, therefore we need to further descend the tree.
1352 * This won't change by continuing around the loop, so we
1353 * go ahead and return. If we haven't seen a peripheral
1354 * matching expression, we keep going around the loop until
1355 * we exhaust the matching expressions. We'll set the stop
1356 * flag once we fall out of the loop.
1358 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1363 * If the return action hasn't been set to descend yet, that means
1364 * we haven't seen any peripheral matching patterns. So tell the
1365 * caller to stop descending the tree -- the user doesn't want to
1366 * match against lower level tree elements.
1368 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1369 retval |= DM_RET_STOP;
1375 * Match a single peripheral against any number of match patterns.
1377 static dev_match_ret
1378 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1379 struct cam_periph *periph)
1381 dev_match_ret retval;
1385 * If we aren't given something to match against, that's an error.
1388 return(DM_RET_ERROR);
1391 * If there are no match entries, then this peripheral matches no
1394 if ((patterns == NULL) || (num_patterns == 0))
1395 return(DM_RET_STOP | DM_RET_COPY);
1398 * There aren't any nodes below a peripheral node, so there's no
1399 * reason to descend the tree any further.
1401 retval = DM_RET_STOP;
1403 for (i = 0; i < num_patterns; i++) {
1404 struct periph_match_pattern *cur_pattern;
1407 * If the pattern in question isn't for a peripheral, we
1408 * aren't interested.
1410 if (patterns[i].type != DEV_MATCH_PERIPH)
1413 cur_pattern = &patterns[i].pattern.periph_pattern;
1416 * If they want to match on anything, then we will do so.
1418 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1419 /* set the copy flag */
1420 retval |= DM_RET_COPY;
1423 * We've already set the return action to stop,
1424 * since there are no nodes below peripherals in
1431 * Not sure why someone would do this...
1433 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1436 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1437 && (cur_pattern->path_id != periph->path->bus->path_id))
1441 * For the target and lun id's, we have to make sure the
1442 * target and lun pointers aren't NULL. The xpt peripheral
1443 * has a wildcard target and device.
1445 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1446 && ((periph->path->target == NULL)
1447 ||(cur_pattern->target_id != periph->path->target->target_id)))
1450 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1451 && ((periph->path->device == NULL)
1452 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1455 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1456 && (cur_pattern->unit_number != periph->unit_number))
1459 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1460 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1465 * If we get to this point, the user definitely wants
1466 * information on this peripheral. So tell the caller to
1467 * copy the data out.
1469 retval |= DM_RET_COPY;
1472 * The return action has already been set to stop, since
1473 * peripherals don't have any nodes below them in the EDT.
1479 * If we get to this point, the peripheral that was passed in
1480 * doesn't match any of the patterns.
1486 xptedtbusfunc(struct cam_eb *bus, void *arg)
1488 struct ccb_dev_match *cdm;
1489 dev_match_ret retval;
1491 cdm = (struct ccb_dev_match *)arg;
1494 * If our position is for something deeper in the tree, that means
1495 * that we've already seen this node. So, we keep going down.
1497 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1498 && (cdm->pos.cookie.bus == bus)
1499 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1500 && (cdm->pos.cookie.target != NULL))
1501 retval = DM_RET_DESCEND;
1503 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1506 * If we got an error, bail out of the search.
1508 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1509 cdm->status = CAM_DEV_MATCH_ERROR;
1514 * If the copy flag is set, copy this bus out.
1516 if (retval & DM_RET_COPY) {
1519 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1520 sizeof(struct dev_match_result));
1523 * If we don't have enough space to put in another
1524 * match result, save our position and tell the
1525 * user there are more devices to check.
1527 if (spaceleft < sizeof(struct dev_match_result)) {
1528 bzero(&cdm->pos, sizeof(cdm->pos));
1529 cdm->pos.position_type =
1530 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1532 cdm->pos.cookie.bus = bus;
1533 cdm->pos.generations[CAM_BUS_GENERATION]=
1534 xsoftc.bus_generation;
1535 cdm->status = CAM_DEV_MATCH_MORE;
1538 j = cdm->num_matches;
1540 cdm->matches[j].type = DEV_MATCH_BUS;
1541 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1542 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1543 cdm->matches[j].result.bus_result.unit_number =
1544 bus->sim->unit_number;
1545 strncpy(cdm->matches[j].result.bus_result.dev_name,
1546 bus->sim->sim_name, DEV_IDLEN);
1550 * If the user is only interested in busses, there's no
1551 * reason to descend to the next level in the tree.
1553 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1557 * If there is a target generation recorded, check it to
1558 * make sure the target list hasn't changed.
1560 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1561 && (bus == cdm->pos.cookie.bus)
1562 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1563 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1564 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1566 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1570 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1571 && (cdm->pos.cookie.bus == bus)
1572 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1573 && (cdm->pos.cookie.target != NULL))
1574 return(xpttargettraverse(bus,
1575 (struct cam_et *)cdm->pos.cookie.target,
1576 xptedttargetfunc, arg));
1578 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1582 xptedttargetfunc(struct cam_et *target, void *arg)
1584 struct ccb_dev_match *cdm;
1586 cdm = (struct ccb_dev_match *)arg;
1589 * If there is a device list generation recorded, check it to
1590 * make sure the device list hasn't changed.
1592 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1593 && (cdm->pos.cookie.bus == target->bus)
1594 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1595 && (cdm->pos.cookie.target == target)
1596 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1597 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1598 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1599 target->generation)) {
1600 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1604 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1605 && (cdm->pos.cookie.bus == target->bus)
1606 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1607 && (cdm->pos.cookie.target == target)
1608 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1609 && (cdm->pos.cookie.device != NULL))
1610 return(xptdevicetraverse(target,
1611 (struct cam_ed *)cdm->pos.cookie.device,
1612 xptedtdevicefunc, arg));
1614 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1618 xptedtdevicefunc(struct cam_ed *device, void *arg)
1621 struct ccb_dev_match *cdm;
1622 dev_match_ret retval;
1624 cdm = (struct ccb_dev_match *)arg;
1627 * If our position is for something deeper in the tree, that means
1628 * that we've already seen this node. So, we keep going down.
1630 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1631 && (cdm->pos.cookie.device == device)
1632 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1633 && (cdm->pos.cookie.periph != NULL))
1634 retval = DM_RET_DESCEND;
1636 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1639 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1640 cdm->status = CAM_DEV_MATCH_ERROR;
1645 * If the copy flag is set, copy this device out.
1647 if (retval & DM_RET_COPY) {
1650 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1651 sizeof(struct dev_match_result));
1654 * If we don't have enough space to put in another
1655 * match result, save our position and tell the
1656 * user there are more devices to check.
1658 if (spaceleft < sizeof(struct dev_match_result)) {
1659 bzero(&cdm->pos, sizeof(cdm->pos));
1660 cdm->pos.position_type =
1661 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1662 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1664 cdm->pos.cookie.bus = device->target->bus;
1665 cdm->pos.generations[CAM_BUS_GENERATION]=
1666 xsoftc.bus_generation;
1667 cdm->pos.cookie.target = device->target;
1668 cdm->pos.generations[CAM_TARGET_GENERATION] =
1669 device->target->bus->generation;
1670 cdm->pos.cookie.device = device;
1671 cdm->pos.generations[CAM_DEV_GENERATION] =
1672 device->target->generation;
1673 cdm->status = CAM_DEV_MATCH_MORE;
1676 j = cdm->num_matches;
1678 cdm->matches[j].type = DEV_MATCH_DEVICE;
1679 cdm->matches[j].result.device_result.path_id =
1680 device->target->bus->path_id;
1681 cdm->matches[j].result.device_result.target_id =
1682 device->target->target_id;
1683 cdm->matches[j].result.device_result.target_lun =
1685 cdm->matches[j].result.device_result.protocol =
1687 bcopy(&device->inq_data,
1688 &cdm->matches[j].result.device_result.inq_data,
1689 sizeof(struct scsi_inquiry_data));
1690 bcopy(&device->ident_data,
1691 &cdm->matches[j].result.device_result.ident_data,
1692 sizeof(struct ata_params));
1694 /* Let the user know whether this device is unconfigured */
1695 if (device->flags & CAM_DEV_UNCONFIGURED)
1696 cdm->matches[j].result.device_result.flags =
1697 DEV_RESULT_UNCONFIGURED;
1699 cdm->matches[j].result.device_result.flags =
1704 * If the user isn't interested in peripherals, don't descend
1705 * the tree any further.
1707 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1711 * If there is a peripheral list generation recorded, make sure
1712 * it hasn't changed.
1714 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1715 && (device->target->bus == cdm->pos.cookie.bus)
1716 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1717 && (device->target == cdm->pos.cookie.target)
1718 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1719 && (device == cdm->pos.cookie.device)
1720 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1721 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1722 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1723 device->generation)){
1724 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1728 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1729 && (cdm->pos.cookie.bus == device->target->bus)
1730 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1731 && (cdm->pos.cookie.target == device->target)
1732 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1733 && (cdm->pos.cookie.device == device)
1734 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1735 && (cdm->pos.cookie.periph != NULL))
1736 return(xptperiphtraverse(device,
1737 (struct cam_periph *)cdm->pos.cookie.periph,
1738 xptedtperiphfunc, arg));
1740 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1744 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1746 struct ccb_dev_match *cdm;
1747 dev_match_ret retval;
1749 cdm = (struct ccb_dev_match *)arg;
1751 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1753 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1754 cdm->status = CAM_DEV_MATCH_ERROR;
1759 * If the copy flag is set, copy this peripheral out.
1761 if (retval & DM_RET_COPY) {
1764 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1765 sizeof(struct dev_match_result));
1768 * If we don't have enough space to put in another
1769 * match result, save our position and tell the
1770 * user there are more devices to check.
1772 if (spaceleft < sizeof(struct dev_match_result)) {
1773 bzero(&cdm->pos, sizeof(cdm->pos));
1774 cdm->pos.position_type =
1775 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1776 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1779 cdm->pos.cookie.bus = periph->path->bus;
1780 cdm->pos.generations[CAM_BUS_GENERATION]=
1781 xsoftc.bus_generation;
1782 cdm->pos.cookie.target = periph->path->target;
1783 cdm->pos.generations[CAM_TARGET_GENERATION] =
1784 periph->path->bus->generation;
1785 cdm->pos.cookie.device = periph->path->device;
1786 cdm->pos.generations[CAM_DEV_GENERATION] =
1787 periph->path->target->generation;
1788 cdm->pos.cookie.periph = periph;
1789 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1790 periph->path->device->generation;
1791 cdm->status = CAM_DEV_MATCH_MORE;
1795 j = cdm->num_matches;
1797 cdm->matches[j].type = DEV_MATCH_PERIPH;
1798 cdm->matches[j].result.periph_result.path_id =
1799 periph->path->bus->path_id;
1800 cdm->matches[j].result.periph_result.target_id =
1801 periph->path->target->target_id;
1802 cdm->matches[j].result.periph_result.target_lun =
1803 periph->path->device->lun_id;
1804 cdm->matches[j].result.periph_result.unit_number =
1805 periph->unit_number;
1806 strncpy(cdm->matches[j].result.periph_result.periph_name,
1807 periph->periph_name, DEV_IDLEN);
1814 xptedtmatch(struct ccb_dev_match *cdm)
1818 cdm->num_matches = 0;
1821 * Check the bus list generation. If it has changed, the user
1822 * needs to reset everything and start over.
1824 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1825 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1826 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1827 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1831 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1832 && (cdm->pos.cookie.bus != NULL))
1833 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1834 xptedtbusfunc, cdm);
1836 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1839 * If we get back 0, that means that we had to stop before fully
1840 * traversing the EDT. It also means that one of the subroutines
1841 * has set the status field to the proper value. If we get back 1,
1842 * we've fully traversed the EDT and copied out any matching entries.
1845 cdm->status = CAM_DEV_MATCH_LAST;
1851 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1853 struct ccb_dev_match *cdm;
1855 cdm = (struct ccb_dev_match *)arg;
1857 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1858 && (cdm->pos.cookie.pdrv == pdrv)
1859 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1860 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1861 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1862 (*pdrv)->generation)) {
1863 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1867 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1868 && (cdm->pos.cookie.pdrv == pdrv)
1869 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1870 && (cdm->pos.cookie.periph != NULL))
1871 return(xptpdperiphtraverse(pdrv,
1872 (struct cam_periph *)cdm->pos.cookie.periph,
1873 xptplistperiphfunc, arg));
1875 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1879 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1881 struct ccb_dev_match *cdm;
1882 dev_match_ret retval;
1884 cdm = (struct ccb_dev_match *)arg;
1886 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1888 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1889 cdm->status = CAM_DEV_MATCH_ERROR;
1894 * If the copy flag is set, copy this peripheral out.
1896 if (retval & DM_RET_COPY) {
1899 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1900 sizeof(struct dev_match_result));
1903 * If we don't have enough space to put in another
1904 * match result, save our position and tell the
1905 * user there are more devices to check.
1907 if (spaceleft < sizeof(struct dev_match_result)) {
1908 struct periph_driver **pdrv;
1911 bzero(&cdm->pos, sizeof(cdm->pos));
1912 cdm->pos.position_type =
1913 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1917 * This may look a bit non-sensical, but it is
1918 * actually quite logical. There are very few
1919 * peripheral drivers, and bloating every peripheral
1920 * structure with a pointer back to its parent
1921 * peripheral driver linker set entry would cost
1922 * more in the long run than doing this quick lookup.
1924 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1925 if (strcmp((*pdrv)->driver_name,
1926 periph->periph_name) == 0)
1930 if (*pdrv == NULL) {
1931 cdm->status = CAM_DEV_MATCH_ERROR;
1935 cdm->pos.cookie.pdrv = pdrv;
1937 * The periph generation slot does double duty, as
1938 * does the periph pointer slot. They are used for
1939 * both edt and pdrv lookups and positioning.
1941 cdm->pos.cookie.periph = periph;
1942 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1943 (*pdrv)->generation;
1944 cdm->status = CAM_DEV_MATCH_MORE;
1948 j = cdm->num_matches;
1950 cdm->matches[j].type = DEV_MATCH_PERIPH;
1951 cdm->matches[j].result.periph_result.path_id =
1952 periph->path->bus->path_id;
1955 * The transport layer peripheral doesn't have a target or
1958 if (periph->path->target)
1959 cdm->matches[j].result.periph_result.target_id =
1960 periph->path->target->target_id;
1962 cdm->matches[j].result.periph_result.target_id = -1;
1964 if (periph->path->device)
1965 cdm->matches[j].result.periph_result.target_lun =
1966 periph->path->device->lun_id;
1968 cdm->matches[j].result.periph_result.target_lun = -1;
1970 cdm->matches[j].result.periph_result.unit_number =
1971 periph->unit_number;
1972 strncpy(cdm->matches[j].result.periph_result.periph_name,
1973 periph->periph_name, DEV_IDLEN);
1980 xptperiphlistmatch(struct ccb_dev_match *cdm)
1984 cdm->num_matches = 0;
1987 * At this point in the edt traversal function, we check the bus
1988 * list generation to make sure that no busses have been added or
1989 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1990 * For the peripheral driver list traversal function, however, we
1991 * don't have to worry about new peripheral driver types coming or
1992 * going; they're in a linker set, and therefore can't change
1993 * without a recompile.
1996 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1997 && (cdm->pos.cookie.pdrv != NULL))
1998 ret = xptpdrvtraverse(
1999 (struct periph_driver **)cdm->pos.cookie.pdrv,
2000 xptplistpdrvfunc, cdm);
2002 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2005 * If we get back 0, that means that we had to stop before fully
2006 * traversing the peripheral driver tree. It also means that one of
2007 * the subroutines has set the status field to the proper value. If
2008 * we get back 1, we've fully traversed the EDT and copied out any
2012 cdm->status = CAM_DEV_MATCH_LAST;
2018 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2020 struct cam_eb *bus, *next_bus;
2025 mtx_lock(&xsoftc.xpt_topo_lock);
2026 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2033 * XXX The locking here is obviously very complex. We
2034 * should work to simplify it.
2036 mtx_unlock(&xsoftc.xpt_topo_lock);
2037 CAM_SIM_LOCK(bus->sim);
2038 retval = tr_func(bus, arg);
2039 CAM_SIM_UNLOCK(bus->sim);
2041 mtx_lock(&xsoftc.xpt_topo_lock);
2042 next_bus = TAILQ_NEXT(bus, links);
2043 mtx_unlock(&xsoftc.xpt_topo_lock);
2045 xpt_release_bus(bus);
2049 mtx_lock(&xsoftc.xpt_topo_lock);
2051 mtx_unlock(&xsoftc.xpt_topo_lock);
2057 xpt_sim_opened(struct cam_sim *sim)
2060 struct cam_et *target;
2061 struct cam_ed *device;
2062 struct cam_periph *periph;
2064 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2065 mtx_assert(sim->mtx, MA_OWNED);
2067 mtx_lock(&xsoftc.xpt_topo_lock);
2068 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2069 if (bus->sim != sim)
2072 TAILQ_FOREACH(target, &bus->et_entries, links) {
2073 TAILQ_FOREACH(device, &target->ed_entries, links) {
2074 SLIST_FOREACH(periph, &device->periphs,
2076 if (periph->refcount > 0) {
2077 mtx_unlock(&xsoftc.xpt_topo_lock);
2085 mtx_unlock(&xsoftc.xpt_topo_lock);
2090 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2091 xpt_targetfunc_t *tr_func, void *arg)
2093 struct cam_et *target, *next_target;
2097 for (target = (start_target ? start_target :
2098 TAILQ_FIRST(&bus->et_entries));
2099 target != NULL; target = next_target) {
2103 retval = tr_func(target, arg);
2105 next_target = TAILQ_NEXT(target, links);
2107 xpt_release_target(target);
2117 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2118 xpt_devicefunc_t *tr_func, void *arg)
2120 struct cam_ed *device, *next_device;
2124 for (device = (start_device ? start_device :
2125 TAILQ_FIRST(&target->ed_entries));
2127 device = next_device) {
2130 * Hold a reference so the current device does not go away
2135 retval = tr_func(device, arg);
2138 * Grab our next pointer before we release the current
2141 next_device = TAILQ_NEXT(device, links);
2143 xpt_release_device(device);
2153 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2154 xpt_periphfunc_t *tr_func, void *arg)
2156 struct cam_periph *periph, *next_periph;
2162 for (periph = (start_periph ? start_periph :
2163 SLIST_FIRST(&device->periphs));
2165 periph = next_periph) {
2169 * In this case, we want to show peripherals that have been
2170 * invalidated, but not peripherals that are scheduled to
2171 * be freed. So instead of calling cam_periph_acquire(),
2172 * which will fail if the periph has been invalidated, we
2173 * just check for the free flag here. If it is free, we
2174 * skip to the next periph.
2176 if (periph->flags & CAM_PERIPH_FREE) {
2177 next_periph = SLIST_NEXT(periph, periph_links);
2182 * Acquire a reference to this periph while we call the
2183 * traversal function, so it can't go away.
2189 retval = tr_func(periph, arg);
2192 * We need the lock for list traversal.
2197 * Grab the next peripheral before we release this one, so
2198 * our next pointer is still valid.
2200 next_periph = SLIST_NEXT(periph, periph_links);
2202 cam_periph_release_locked_buses(periph);
2216 xptpdrvtraverse(struct periph_driver **start_pdrv,
2217 xpt_pdrvfunc_t *tr_func, void *arg)
2219 struct periph_driver **pdrv;
2225 * We don't traverse the peripheral driver list like we do the
2226 * other lists, because it is a linker set, and therefore cannot be
2227 * changed during runtime. If the peripheral driver list is ever
2228 * re-done to be something other than a linker set (i.e. it can
2229 * change while the system is running), the list traversal should
2230 * be modified to work like the other traversal functions.
2232 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2233 *pdrv != NULL; pdrv++) {
2234 retval = tr_func(pdrv, arg);
2244 xptpdperiphtraverse(struct periph_driver **pdrv,
2245 struct cam_periph *start_periph,
2246 xpt_periphfunc_t *tr_func, void *arg)
2248 struct cam_periph *periph, *next_periph;
2254 for (periph = (start_periph ? start_periph :
2255 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2256 periph = next_periph) {
2260 * In this case, we want to show peripherals that have been
2261 * invalidated, but not peripherals that are scheduled to
2262 * be freed. So instead of calling cam_periph_acquire(),
2263 * which will fail if the periph has been invalidated, we
2264 * just check for the free flag here. If it is free, we
2265 * skip to the next periph.
2267 if (periph->flags & CAM_PERIPH_FREE) {
2268 next_periph = TAILQ_NEXT(periph, unit_links);
2273 * Acquire a reference to this periph while we call the
2274 * traversal function, so it can't go away.
2279 * XXX KDM we have the toplogy lock here, but in
2280 * xptperiphtraverse(), we drop it before calling the
2281 * traversal function. Which is correct?
2283 retval = tr_func(periph, arg);
2286 * Grab the next peripheral before we release this one, so
2287 * our next pointer is still valid.
2289 next_periph = TAILQ_NEXT(periph, unit_links);
2291 cam_periph_release_locked_buses(periph);
2304 xptdefbusfunc(struct cam_eb *bus, void *arg)
2306 struct xpt_traverse_config *tr_config;
2308 tr_config = (struct xpt_traverse_config *)arg;
2310 if (tr_config->depth == XPT_DEPTH_BUS) {
2311 xpt_busfunc_t *tr_func;
2313 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2315 return(tr_func(bus, tr_config->tr_arg));
2317 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2321 xptdeftargetfunc(struct cam_et *target, void *arg)
2323 struct xpt_traverse_config *tr_config;
2325 tr_config = (struct xpt_traverse_config *)arg;
2327 if (tr_config->depth == XPT_DEPTH_TARGET) {
2328 xpt_targetfunc_t *tr_func;
2330 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2332 return(tr_func(target, tr_config->tr_arg));
2334 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2338 xptdefdevicefunc(struct cam_ed *device, void *arg)
2340 struct xpt_traverse_config *tr_config;
2342 tr_config = (struct xpt_traverse_config *)arg;
2344 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2345 xpt_devicefunc_t *tr_func;
2347 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2349 return(tr_func(device, tr_config->tr_arg));
2351 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2355 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2357 struct xpt_traverse_config *tr_config;
2358 xpt_periphfunc_t *tr_func;
2360 tr_config = (struct xpt_traverse_config *)arg;
2362 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2365 * Unlike the other default functions, we don't check for depth
2366 * here. The peripheral driver level is the last level in the EDT,
2367 * so if we're here, we should execute the function in question.
2369 return(tr_func(periph, tr_config->tr_arg));
2373 * Execute the given function for every bus in the EDT.
2376 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2378 struct xpt_traverse_config tr_config;
2380 tr_config.depth = XPT_DEPTH_BUS;
2381 tr_config.tr_func = tr_func;
2382 tr_config.tr_arg = arg;
2384 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2388 * Execute the given function for every device in the EDT.
2391 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2393 struct xpt_traverse_config tr_config;
2395 tr_config.depth = XPT_DEPTH_DEVICE;
2396 tr_config.tr_func = tr_func;
2397 tr_config.tr_arg = arg;
2399 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2403 xptsetasyncfunc(struct cam_ed *device, void *arg)
2405 struct cam_path path;
2406 struct ccb_getdev cgd;
2407 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2410 * Don't report unconfigured devices (Wildcard devs,
2411 * devices only for target mode, device instances
2412 * that have been invalidated but are waiting for
2413 * their last reference count to be released).
2415 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2418 xpt_compile_path(&path,
2420 device->target->bus->path_id,
2421 device->target->target_id,
2423 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2424 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2425 xpt_action((union ccb *)&cgd);
2426 csa->callback(csa->callback_arg,
2429 xpt_release_path(&path);
2435 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2437 struct cam_path path;
2438 struct ccb_pathinq cpi;
2439 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2441 xpt_compile_path(&path, /*periph*/NULL,
2443 CAM_TARGET_WILDCARD,
2445 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2446 cpi.ccb_h.func_code = XPT_PATH_INQ;
2447 xpt_action((union ccb *)&cpi);
2448 csa->callback(csa->callback_arg,
2451 xpt_release_path(&path);
2457 xpt_action(union ccb *start_ccb)
2460 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2462 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2463 /* Compatibility for RL-unaware code. */
2464 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2465 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2466 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2470 xpt_action_default(union ccb *start_ccb)
2473 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2475 struct cam_path *path;
2477 path = start_ccb->ccb_h.path;
2478 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2480 switch (start_ccb->ccb_h.func_code) {
2483 struct cam_ed *device;
2486 * For the sake of compatibility with SCSI-1
2487 * devices that may not understand the identify
2488 * message, we include lun information in the
2489 * second byte of all commands. SCSI-1 specifies
2490 * that luns are a 3 bit value and reserves only 3
2491 * bits for lun information in the CDB. Later
2492 * revisions of the SCSI spec allow for more than 8
2493 * luns, but have deprecated lun information in the
2494 * CDB. So, if the lun won't fit, we must omit.
2496 * Also be aware that during initial probing for devices,
2497 * the inquiry information is unknown but initialized to 0.
2498 * This means that this code will be exercised while probing
2499 * devices with an ANSI revision greater than 2.
2501 device = path->device;
2502 if (device->protocol_version <= SCSI_REV_2
2503 && start_ccb->ccb_h.target_lun < 8
2504 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2506 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2507 start_ccb->ccb_h.target_lun << 5;
2509 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2510 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2511 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2512 &path->device->inq_data),
2513 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2514 cdb_str, sizeof(cdb_str))));
2518 case XPT_CONT_TARGET_IO:
2519 start_ccb->csio.sense_resid = 0;
2520 start_ccb->csio.resid = 0;
2523 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2524 start_ccb->ataio.resid = 0;
2525 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2526 ata_op_string(&start_ccb->ataio.cmd),
2527 ata_cmd_string(&start_ccb->ataio.cmd,
2528 cdb_str, sizeof(cdb_str))));
2537 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2538 path->device->sim->devq->alloc_openings += frozen;
2540 xpt_run_dev_allocq(path->bus);
2541 if (xpt_schedule_dev_sendq(path->bus, path->device))
2542 xpt_run_dev_sendq(path->bus);
2545 case XPT_CALC_GEOMETRY:
2547 struct cam_sim *sim;
2549 /* Filter out garbage */
2550 if (start_ccb->ccg.block_size == 0
2551 || start_ccb->ccg.volume_size == 0) {
2552 start_ccb->ccg.cylinders = 0;
2553 start_ccb->ccg.heads = 0;
2554 start_ccb->ccg.secs_per_track = 0;
2555 start_ccb->ccb_h.status = CAM_REQ_CMP;
2558 #if defined(PC98) || defined(__sparc64__)
2560 * In a PC-98 system, geometry translation depens on
2561 * the "real" device geometry obtained from mode page 4.
2562 * SCSI geometry translation is performed in the
2563 * initialization routine of the SCSI BIOS and the result
2564 * stored in host memory. If the translation is available
2565 * in host memory, use it. If not, rely on the default
2566 * translation the device driver performs.
2567 * For sparc64, we may need adjust the geometry of large
2568 * disks in order to fit the limitations of the 16-bit
2569 * fields of the VTOC8 disk label.
2571 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2572 start_ccb->ccb_h.status = CAM_REQ_CMP;
2576 sim = path->bus->sim;
2577 (*(sim->sim_action))(sim, start_ccb);
2582 union ccb* abort_ccb;
2584 abort_ccb = start_ccb->cab.abort_ccb;
2585 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2587 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2588 struct cam_ccbq *ccbq;
2589 struct cam_ed *device;
2591 device = abort_ccb->ccb_h.path->device;
2592 ccbq = &device->ccbq;
2593 device->sim->devq->alloc_openings -=
2594 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2595 abort_ccb->ccb_h.status =
2596 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2597 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2598 xpt_done(abort_ccb);
2599 start_ccb->ccb_h.status = CAM_REQ_CMP;
2602 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2603 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2605 * We've caught this ccb en route to
2606 * the SIM. Flag it for abort and the
2607 * SIM will do so just before starting
2608 * real work on the CCB.
2610 abort_ccb->ccb_h.status =
2611 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2612 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2613 start_ccb->ccb_h.status = CAM_REQ_CMP;
2617 if (XPT_FC_IS_QUEUED(abort_ccb)
2618 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2620 * It's already completed but waiting
2621 * for our SWI to get to it.
2623 start_ccb->ccb_h.status = CAM_UA_ABORT;
2627 * If we weren't able to take care of the abort request
2628 * in the XPT, pass the request down to the SIM for processing.
2632 case XPT_ACCEPT_TARGET_IO:
2634 case XPT_IMMED_NOTIFY:
2635 case XPT_NOTIFY_ACK:
2637 case XPT_IMMEDIATE_NOTIFY:
2638 case XPT_NOTIFY_ACKNOWLEDGE:
2639 case XPT_GET_SIM_KNOB:
2640 case XPT_SET_SIM_KNOB:
2642 struct cam_sim *sim;
2644 sim = path->bus->sim;
2645 (*(sim->sim_action))(sim, start_ccb);
2650 struct cam_sim *sim;
2652 sim = path->bus->sim;
2653 (*(sim->sim_action))(sim, start_ccb);
2656 case XPT_PATH_STATS:
2657 start_ccb->cpis.last_reset = path->bus->last_reset;
2658 start_ccb->ccb_h.status = CAM_REQ_CMP;
2665 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2666 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2668 struct ccb_getdev *cgd;
2670 cgd = &start_ccb->cgd;
2671 cgd->protocol = dev->protocol;
2672 cgd->inq_data = dev->inq_data;
2673 cgd->ident_data = dev->ident_data;
2674 cgd->inq_flags = dev->inq_flags;
2675 cgd->ccb_h.status = CAM_REQ_CMP;
2676 cgd->serial_num_len = dev->serial_num_len;
2677 if ((dev->serial_num_len > 0)
2678 && (dev->serial_num != NULL))
2679 bcopy(dev->serial_num, cgd->serial_num,
2680 dev->serial_num_len);
2684 case XPT_GDEV_STATS:
2689 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2690 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2692 struct ccb_getdevstats *cgds;
2696 cgds = &start_ccb->cgds;
2699 cgds->dev_openings = dev->ccbq.dev_openings;
2700 cgds->dev_active = dev->ccbq.dev_active;
2701 cgds->devq_openings = dev->ccbq.devq_openings;
2702 cgds->devq_queued = dev->ccbq.queue.entries;
2703 cgds->held = dev->ccbq.held;
2704 cgds->last_reset = tar->last_reset;
2705 cgds->maxtags = dev->maxtags;
2706 cgds->mintags = dev->mintags;
2707 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2708 cgds->last_reset = bus->last_reset;
2709 cgds->ccb_h.status = CAM_REQ_CMP;
2715 struct cam_periph *nperiph;
2716 struct periph_list *periph_head;
2717 struct ccb_getdevlist *cgdl;
2719 struct cam_ed *device;
2726 * Don't want anyone mucking with our data.
2728 device = path->device;
2729 periph_head = &device->periphs;
2730 cgdl = &start_ccb->cgdl;
2733 * Check and see if the list has changed since the user
2734 * last requested a list member. If so, tell them that the
2735 * list has changed, and therefore they need to start over
2736 * from the beginning.
2738 if ((cgdl->index != 0) &&
2739 (cgdl->generation != device->generation)) {
2740 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2745 * Traverse the list of peripherals and attempt to find
2746 * the requested peripheral.
2748 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2749 (nperiph != NULL) && (i <= cgdl->index);
2750 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2751 if (i == cgdl->index) {
2752 strncpy(cgdl->periph_name,
2753 nperiph->periph_name,
2755 cgdl->unit_number = nperiph->unit_number;
2760 cgdl->status = CAM_GDEVLIST_ERROR;
2764 if (nperiph == NULL)
2765 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2767 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2770 cgdl->generation = device->generation;
2772 cgdl->ccb_h.status = CAM_REQ_CMP;
2777 dev_pos_type position_type;
2778 struct ccb_dev_match *cdm;
2780 cdm = &start_ccb->cdm;
2783 * There are two ways of getting at information in the EDT.
2784 * The first way is via the primary EDT tree. It starts
2785 * with a list of busses, then a list of targets on a bus,
2786 * then devices/luns on a target, and then peripherals on a
2787 * device/lun. The "other" way is by the peripheral driver
2788 * lists. The peripheral driver lists are organized by
2789 * peripheral driver. (obviously) So it makes sense to
2790 * use the peripheral driver list if the user is looking
2791 * for something like "da1", or all "da" devices. If the
2792 * user is looking for something on a particular bus/target
2793 * or lun, it's generally better to go through the EDT tree.
2796 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2797 position_type = cdm->pos.position_type;
2801 position_type = CAM_DEV_POS_NONE;
2803 for (i = 0; i < cdm->num_patterns; i++) {
2804 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2805 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2806 position_type = CAM_DEV_POS_EDT;
2811 if (cdm->num_patterns == 0)
2812 position_type = CAM_DEV_POS_EDT;
2813 else if (position_type == CAM_DEV_POS_NONE)
2814 position_type = CAM_DEV_POS_PDRV;
2817 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2818 case CAM_DEV_POS_EDT:
2821 case CAM_DEV_POS_PDRV:
2822 xptperiphlistmatch(cdm);
2825 cdm->status = CAM_DEV_MATCH_ERROR;
2829 if (cdm->status == CAM_DEV_MATCH_ERROR)
2830 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2832 start_ccb->ccb_h.status = CAM_REQ_CMP;
2838 struct ccb_setasync *csa;
2839 struct async_node *cur_entry;
2840 struct async_list *async_head;
2843 csa = &start_ccb->csa;
2844 added = csa->event_enable;
2845 async_head = &path->device->asyncs;
2848 * If there is already an entry for us, simply
2851 cur_entry = SLIST_FIRST(async_head);
2852 while (cur_entry != NULL) {
2853 if ((cur_entry->callback_arg == csa->callback_arg)
2854 && (cur_entry->callback == csa->callback))
2856 cur_entry = SLIST_NEXT(cur_entry, links);
2859 if (cur_entry != NULL) {
2861 * If the request has no flags set,
2864 added &= ~cur_entry->event_enable;
2865 if (csa->event_enable == 0) {
2866 SLIST_REMOVE(async_head, cur_entry,
2868 xpt_release_device(path->device);
2869 free(cur_entry, M_CAMXPT);
2871 cur_entry->event_enable = csa->event_enable;
2873 csa->event_enable = added;
2875 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2877 if (cur_entry == NULL) {
2878 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2881 cur_entry->event_enable = csa->event_enable;
2882 cur_entry->callback_arg = csa->callback_arg;
2883 cur_entry->callback = csa->callback;
2884 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2885 xpt_acquire_device(path->device);
2887 start_ccb->ccb_h.status = CAM_REQ_CMP;
2892 struct ccb_relsim *crs;
2895 crs = &start_ccb->crs;
2899 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2903 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2905 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2906 /* Don't ever go below one opening */
2907 if (crs->openings > 0) {
2908 xpt_dev_ccbq_resize(path,
2913 "tagged openings now %d\n",
2920 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2922 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2925 * Just extend the old timeout and decrement
2926 * the freeze count so that a single timeout
2927 * is sufficient for releasing the queue.
2929 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2930 callout_stop(&dev->callout);
2933 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2936 callout_reset(&dev->callout,
2937 (crs->release_timeout * hz) / 1000,
2938 xpt_release_devq_timeout, dev);
2940 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2944 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2946 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2948 * Decrement the freeze count so that a single
2949 * completion is still sufficient to unfreeze
2952 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2955 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2956 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2960 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2962 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2963 || (dev->ccbq.dev_active == 0)) {
2965 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2968 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2969 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2973 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2974 xpt_release_devq_rl(path, /*runlevel*/
2975 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2976 crs->release_timeout : 0,
2977 /*count*/1, /*run_queue*/TRUE);
2979 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2980 start_ccb->ccb_h.status = CAM_REQ_CMP;
2985 #ifdef CAM_DEBUG_DELAY
2986 cam_debug_delay = CAM_DEBUG_DELAY;
2988 cam_dflags = start_ccb->cdbg.flags;
2989 if (cam_dpath != NULL) {
2990 xpt_free_path(cam_dpath);
2994 if (cam_dflags != CAM_DEBUG_NONE) {
2995 if (xpt_create_path(&cam_dpath, xpt_periph,
2996 start_ccb->ccb_h.path_id,
2997 start_ccb->ccb_h.target_id,
2998 start_ccb->ccb_h.target_lun) !=
3000 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3001 cam_dflags = CAM_DEBUG_NONE;
3003 start_ccb->ccb_h.status = CAM_REQ_CMP;
3004 xpt_print(cam_dpath, "debugging flags now %x\n",
3009 start_ccb->ccb_h.status = CAM_REQ_CMP;
3011 #else /* !CAMDEBUG */
3012 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3013 #endif /* CAMDEBUG */
3016 case XPT_FREEZE_QUEUE:
3018 struct ccb_relsim *crs = &start_ccb->crs;
3020 xpt_freeze_devq_rl(path, /*runlevel*/
3021 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3022 crs->release_timeout : 0, /*count*/1);
3023 start_ccb->ccb_h.status = CAM_REQ_CMP;
3027 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3028 xpt_freeze_devq(path, 1);
3029 start_ccb->ccb_h.status = CAM_REQ_CMP;
3036 printf("%s: CCB type %#x not supported\n", __func__,
3037 start_ccb->ccb_h.func_code);
3038 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3039 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3040 xpt_done(start_ccb);
3047 xpt_polled_action(union ccb *start_ccb)
3050 struct cam_sim *sim;
3051 struct cam_devq *devq;
3055 timeout = start_ccb->ccb_h.timeout * 10;
3056 sim = start_ccb->ccb_h.path->bus->sim;
3058 dev = start_ccb->ccb_h.path->device;
3060 mtx_assert(sim->mtx, MA_OWNED);
3062 /* Don't use ISR for this SIM while polling. */
3063 sim->flags |= CAM_SIM_POLLED;
3066 * Steal an opening so that no other queued requests
3067 * can get it before us while we simulate interrupts.
3069 dev->ccbq.devq_openings--;
3070 dev->ccbq.dev_openings--;
3072 while(((devq != NULL && devq->send_openings <= 0) ||
3073 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3075 (*(sim->sim_poll))(sim);
3076 camisr_runqueue(&sim->sim_doneq);
3079 dev->ccbq.devq_openings++;
3080 dev->ccbq.dev_openings++;
3083 xpt_action(start_ccb);
3084 while(--timeout > 0) {
3085 (*(sim->sim_poll))(sim);
3086 camisr_runqueue(&sim->sim_doneq);
3087 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3094 * XXX Is it worth adding a sim_timeout entry
3095 * point so we can attempt recovery? If
3096 * this is only used for dumps, I don't think
3099 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3102 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3105 /* We will use CAM ISR for this SIM again. */
3106 sim->flags &= ~CAM_SIM_POLLED;
3110 * Schedule a peripheral driver to receive a ccb when it's
3111 * target device has space for more transactions.
3114 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3116 struct cam_ed *device;
3119 mtx_assert(perph->sim->mtx, MA_OWNED);
3121 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3122 device = perph->path->device;
3123 if (periph_is_queued(perph)) {
3124 /* Simply reorder based on new priority */
3125 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3126 (" change priority to %d\n", new_priority));
3127 if (new_priority < perph->pinfo.priority) {
3128 camq_change_priority(&device->drvq,
3131 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3134 /* New entry on the queue */
3135 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3136 (" added periph to queue\n"));
3137 perph->pinfo.priority = new_priority;
3138 perph->pinfo.generation = ++device->drvq.generation;
3139 camq_insert(&device->drvq, &perph->pinfo);
3140 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3143 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3144 (" calling xpt_run_devq\n"));
3145 xpt_run_dev_allocq(perph->path->bus);
3151 * Schedule a device to run on a given queue.
3152 * If the device was inserted as a new entry on the queue,
3153 * return 1 meaning the device queue should be run. If we
3154 * were already queued, implying someone else has already
3155 * started the queue, return 0 so the caller doesn't attempt
3159 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3160 u_int32_t new_priority)
3163 u_int32_t old_priority;
3165 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3167 old_priority = pinfo->priority;
3170 * Are we already queued?
3172 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3173 /* Simply reorder based on new priority */
3174 if (new_priority < old_priority) {
3175 camq_change_priority(queue, pinfo->index,
3177 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3178 ("changed priority to %d\n",
3184 /* New entry on the queue */
3185 if (new_priority < old_priority)
3186 pinfo->priority = new_priority;
3188 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3189 ("Inserting onto queue\n"));
3190 pinfo->generation = ++queue->generation;
3191 camq_insert(queue, pinfo);
3198 xpt_run_dev_allocq(struct cam_eb *bus)
3200 struct cam_devq *devq;
3202 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3203 devq = bus->sim->devq;
3205 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3206 (" qfrozen_cnt == 0x%x, entries == %d, "
3207 "openings == %d, active == %d\n",
3208 devq->alloc_queue.qfrozen_cnt[0],
3209 devq->alloc_queue.entries,
3210 devq->alloc_openings,
3211 devq->alloc_active));
3213 devq->alloc_queue.qfrozen_cnt[0]++;
3214 while ((devq->alloc_queue.entries > 0)
3215 && (devq->alloc_openings > 0)
3216 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3217 struct cam_ed_qinfo *qinfo;
3218 struct cam_ed *device;
3219 union ccb *work_ccb;
3220 struct cam_periph *drv;
3223 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3225 device = qinfo->device;
3226 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3227 ("running device %p\n", device));
3229 drvq = &device->drvq;
3232 if (drvq->entries <= 0) {
3233 panic("xpt_run_dev_allocq: "
3234 "Device on queue without any work to do");
3237 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3238 devq->alloc_openings--;
3239 devq->alloc_active++;
3240 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3241 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3242 drv->pinfo.priority);
3243 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3244 ("calling periph start\n"));
3245 drv->periph_start(drv, work_ccb);
3248 * Malloc failure in alloc_ccb
3251 * XXX add us to a list to be run from free_ccb
3252 * if we don't have any ccbs active on this
3253 * device queue otherwise we may never get run
3259 /* We may have more work. Attempt to reschedule. */
3260 xpt_schedule_dev_allocq(bus, device);
3262 devq->alloc_queue.qfrozen_cnt[0]--;
3266 xpt_run_dev_sendq(struct cam_eb *bus)
3268 struct cam_devq *devq;
3270 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3272 devq = bus->sim->devq;
3274 devq->send_queue.qfrozen_cnt[0]++;
3275 while ((devq->send_queue.entries > 0)
3276 && (devq->send_openings > 0)
3277 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3278 struct cam_ed_qinfo *qinfo;
3279 struct cam_ed *device;
3280 union ccb *work_ccb;
3281 struct cam_sim *sim;
3283 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3285 device = qinfo->device;
3286 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3287 ("running device %p\n", device));
3289 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3290 if (work_ccb == NULL) {
3291 printf("device on run queue with no ccbs???\n");
3295 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3297 mtx_lock(&xsoftc.xpt_lock);
3298 if (xsoftc.num_highpower <= 0) {
3300 * We got a high power command, but we
3301 * don't have any available slots. Freeze
3302 * the device queue until we have a slot
3305 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3306 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3310 mtx_unlock(&xsoftc.xpt_lock);
3314 * Consume a high power slot while
3317 xsoftc.num_highpower--;
3319 mtx_unlock(&xsoftc.xpt_lock);
3321 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3322 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3324 devq->send_openings--;
3325 devq->send_active++;
3327 xpt_schedule_dev_sendq(bus, device);
3329 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3331 * The client wants to freeze the queue
3332 * after this CCB is sent.
3334 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3337 /* In Target mode, the peripheral driver knows best... */
3338 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3339 if ((device->inq_flags & SID_CmdQue) != 0
3340 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3341 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3344 * Clear this in case of a retried CCB that
3345 * failed due to a rejected tag.
3347 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3351 * Device queues can be shared among multiple sim instances
3352 * that reside on different busses. Use the SIM in the queue
3353 * CCB's path, rather than the one in the bus that was passed
3354 * into this function.
3356 sim = work_ccb->ccb_h.path->bus->sim;
3357 (*(sim->sim_action))(sim, work_ccb);
3359 devq->send_queue.qfrozen_cnt[0]--;
3363 * This function merges stuff from the slave ccb into the master ccb, while
3364 * keeping important fields in the master ccb constant.
3367 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3371 * Pull fields that are valid for peripheral drivers to set
3372 * into the master CCB along with the CCB "payload".
3374 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3375 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3376 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3377 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3378 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3379 sizeof(union ccb) - sizeof(struct ccb_hdr));
3383 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3386 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3387 ccb_h->pinfo.priority = priority;
3389 ccb_h->path_id = path->bus->path_id;
3391 ccb_h->target_id = path->target->target_id;
3393 ccb_h->target_id = CAM_TARGET_WILDCARD;
3395 ccb_h->target_lun = path->device->lun_id;
3396 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3398 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3400 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3404 /* Path manipulation functions */
3406 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3407 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3409 struct cam_path *path;
3412 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3415 status = CAM_RESRC_UNAVAIL;
3418 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3419 if (status != CAM_REQ_CMP) {
3420 free(path, M_CAMXPT);
3423 *new_path_ptr = path;
3428 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3429 struct cam_periph *periph, path_id_t path_id,
3430 target_id_t target_id, lun_id_t lun_id)
3432 struct cam_path *path;
3433 struct cam_eb *bus = NULL;
3435 int need_unlock = 0;
3437 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3439 if (path_id != CAM_BUS_WILDCARD) {
3440 bus = xpt_find_bus(path_id);
3443 CAM_SIM_LOCK(bus->sim);
3446 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3448 CAM_SIM_UNLOCK(bus->sim);
3449 xpt_release_bus(bus);
3451 if (status != CAM_REQ_CMP) {
3452 free(path, M_CAMXPT);
3455 *new_path_ptr = path;
3460 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3461 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3464 struct cam_et *target;
3465 struct cam_ed *device;
3468 status = CAM_REQ_CMP; /* Completed without error */
3469 target = NULL; /* Wildcarded */
3470 device = NULL; /* Wildcarded */
3473 * We will potentially modify the EDT, so block interrupts
3474 * that may attempt to create cam paths.
3476 bus = xpt_find_bus(path_id);
3478 status = CAM_PATH_INVALID;
3480 target = xpt_find_target(bus, target_id);
3481 if (target == NULL) {
3483 struct cam_et *new_target;
3485 new_target = xpt_alloc_target(bus, target_id);
3486 if (new_target == NULL) {
3487 status = CAM_RESRC_UNAVAIL;
3489 target = new_target;
3492 if (target != NULL) {
3493 device = xpt_find_device(target, lun_id);
3494 if (device == NULL) {
3496 struct cam_ed *new_device;
3499 (*(bus->xport->alloc_device))(bus,
3502 if (new_device == NULL) {
3503 status = CAM_RESRC_UNAVAIL;
3505 device = new_device;
3512 * Only touch the user's data if we are successful.
3514 if (status == CAM_REQ_CMP) {
3515 new_path->periph = perph;
3516 new_path->bus = bus;
3517 new_path->target = target;
3518 new_path->device = device;
3519 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3522 xpt_release_device(device);
3524 xpt_release_target(target);
3526 xpt_release_bus(bus);
3532 xpt_release_path(struct cam_path *path)
3534 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3535 if (path->device != NULL) {
3536 xpt_release_device(path->device);
3537 path->device = NULL;
3539 if (path->target != NULL) {
3540 xpt_release_target(path->target);
3541 path->target = NULL;
3543 if (path->bus != NULL) {
3544 xpt_release_bus(path->bus);
3550 xpt_free_path(struct cam_path *path)
3553 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3554 xpt_release_path(path);
3555 free(path, M_CAMXPT);
3559 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3560 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3563 mtx_lock(&xsoftc.xpt_topo_lock);
3566 *bus_ref = path->bus->refcount;
3570 mtx_unlock(&xsoftc.xpt_topo_lock);
3573 *periph_ref = path->periph->refcount;
3579 *target_ref = path->target->refcount;
3585 *device_ref = path->device->refcount;
3592 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3593 * in path1, 2 for match with wildcards in path2.
3596 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3600 if (path1->bus != path2->bus) {
3601 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3603 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3608 if (path1->target != path2->target) {
3609 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3612 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3617 if (path1->device != path2->device) {
3618 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3621 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3630 xpt_print_path(struct cam_path *path)
3634 printf("(nopath): ");
3636 if (path->periph != NULL)
3637 printf("(%s%d:", path->periph->periph_name,
3638 path->periph->unit_number);
3640 printf("(noperiph:");
3642 if (path->bus != NULL)
3643 printf("%s%d:%d:", path->bus->sim->sim_name,
3644 path->bus->sim->unit_number,
3645 path->bus->sim->bus_id);
3649 if (path->target != NULL)
3650 printf("%d:", path->target->target_id);
3654 if (path->device != NULL)
3655 printf("%d): ", path->device->lun_id);
3662 xpt_print(struct cam_path *path, const char *fmt, ...)
3665 xpt_print_path(path);
3672 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3677 if (path != NULL && path->bus != NULL)
3678 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3681 sbuf_new(&sb, str, str_len, 0);
3684 sbuf_printf(&sb, "(nopath): ");
3686 if (path->periph != NULL)
3687 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3688 path->periph->unit_number);
3690 sbuf_printf(&sb, "(noperiph:");
3692 if (path->bus != NULL)
3693 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3694 path->bus->sim->unit_number,
3695 path->bus->sim->bus_id);
3697 sbuf_printf(&sb, "nobus:");
3699 if (path->target != NULL)
3700 sbuf_printf(&sb, "%d:", path->target->target_id);
3702 sbuf_printf(&sb, "X:");
3704 if (path->device != NULL)
3705 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3707 sbuf_printf(&sb, "X): ");
3711 return(sbuf_len(&sb));
3715 xpt_path_path_id(struct cam_path *path)
3717 return(path->bus->path_id);
3721 xpt_path_target_id(struct cam_path *path)
3723 if (path->target != NULL)
3724 return (path->target->target_id);
3726 return (CAM_TARGET_WILDCARD);
3730 xpt_path_lun_id(struct cam_path *path)
3732 if (path->device != NULL)
3733 return (path->device->lun_id);
3735 return (CAM_LUN_WILDCARD);
3739 xpt_path_sim(struct cam_path *path)
3742 return (path->bus->sim);
3746 xpt_path_periph(struct cam_path *path)
3748 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3750 return (path->periph);
3754 xpt_path_legacy_ata_id(struct cam_path *path)
3759 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3760 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3761 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3762 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3765 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3766 path->bus->sim->unit_number < 2) {
3767 bus_id = path->bus->sim->unit_number;
3771 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3772 if (bus == path->bus)
3774 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3775 bus->sim->unit_number >= 2) ||
3776 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3777 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3778 strcmp(bus->sim->sim_name, "siisch") == 0)
3783 if (path->target != NULL) {
3784 if (path->target->target_id < 2)
3785 return (bus_id * 2 + path->target->target_id);
3789 return (bus_id * 2);
3793 * Release a CAM control block for the caller. Remit the cost of the structure
3794 * to the device referenced by the path. If the this device had no 'credits'
3795 * and peripheral drivers have registered async callbacks for this notification
3799 xpt_release_ccb(union ccb *free_ccb)
3801 struct cam_path *path;
3802 struct cam_ed *device;
3804 struct cam_sim *sim;
3806 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3807 path = free_ccb->ccb_h.path;
3808 device = path->device;
3812 mtx_assert(sim->mtx, MA_OWNED);
3814 cam_ccbq_release_opening(&device->ccbq);
3815 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3816 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3817 cam_ccbq_resize(&device->ccbq,
3818 device->ccbq.dev_openings + device->ccbq.dev_active);
3820 if (sim->ccb_count > sim->max_ccbs) {
3821 xpt_free_ccb(free_ccb);
3824 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3827 if (sim->devq == NULL) {
3830 sim->devq->alloc_openings++;
3831 sim->devq->alloc_active--;
3832 if (device_is_alloc_queued(device) == 0)
3833 xpt_schedule_dev_allocq(bus, device);
3834 xpt_run_dev_allocq(bus);
3837 /* Functions accessed by SIM drivers */
3839 static struct xpt_xport xport_default = {
3840 .alloc_device = xpt_alloc_device_default,
3841 .action = xpt_action_default,
3842 .async = xpt_dev_async_default,
3846 * A sim structure, listing the SIM entry points and instance
3847 * identification info is passed to xpt_bus_register to hook the SIM
3848 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3849 * for this new bus and places it in the array of busses and assigns
3850 * it a path_id. The path_id may be influenced by "hard wiring"
3851 * information specified by the user. Once interrupt services are
3852 * available, the bus will be probed.
3855 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3857 struct cam_eb *new_bus;
3858 struct cam_eb *old_bus;
3859 struct ccb_pathinq cpi;
3860 struct cam_path *path;
3863 mtx_assert(sim->mtx, MA_OWNED);
3866 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3867 M_CAMXPT, M_NOWAIT);
3868 if (new_bus == NULL) {
3869 /* Couldn't satisfy request */
3870 return (CAM_RESRC_UNAVAIL);
3872 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3874 free(new_bus, M_CAMXPT);
3875 return (CAM_RESRC_UNAVAIL);
3878 if (strcmp(sim->sim_name, "xpt") != 0) {
3880 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3883 TAILQ_INIT(&new_bus->et_entries);
3884 new_bus->path_id = sim->path_id;
3887 timevalclear(&new_bus->last_reset);
3889 new_bus->refcount = 1; /* Held until a bus_deregister event */
3890 new_bus->generation = 0;
3892 mtx_lock(&xsoftc.xpt_topo_lock);
3893 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3894 while (old_bus != NULL
3895 && old_bus->path_id < new_bus->path_id)
3896 old_bus = TAILQ_NEXT(old_bus, links);
3897 if (old_bus != NULL)
3898 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3900 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3901 xsoftc.bus_generation++;
3902 mtx_unlock(&xsoftc.xpt_topo_lock);
3905 * Set a default transport so that a PATH_INQ can be issued to
3906 * the SIM. This will then allow for probing and attaching of
3907 * a more appropriate transport.
3909 new_bus->xport = &xport_default;
3911 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3912 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3913 if (status != CAM_REQ_CMP)
3914 printf("xpt_compile_path returned %d\n", status);
3916 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3917 cpi.ccb_h.func_code = XPT_PATH_INQ;
3918 xpt_action((union ccb *)&cpi);
3920 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3921 switch (cpi.transport) {
3928 new_bus->xport = scsi_get_xport();
3932 new_bus->xport = ata_get_xport();
3935 new_bus->xport = &xport_default;
3940 /* Notify interested parties */
3941 if (sim->path_id != CAM_XPT_PATH_ID) {
3942 union ccb *scan_ccb;
3944 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3945 /* Initiate bus rescan. */
3946 scan_ccb = xpt_alloc_ccb_nowait();
3947 scan_ccb->ccb_h.path = path;
3948 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3949 scan_ccb->crcn.flags = 0;
3950 xpt_rescan(scan_ccb);
3952 xpt_free_path(path);
3953 return (CAM_SUCCESS);
3957 xpt_bus_deregister(path_id_t pathid)
3959 struct cam_path bus_path;
3962 status = xpt_compile_path(&bus_path, NULL, pathid,
3963 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3964 if (status != CAM_REQ_CMP)
3967 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3968 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3970 /* Release the reference count held while registered. */
3971 xpt_release_bus(bus_path.bus);
3972 xpt_release_path(&bus_path);
3974 return (CAM_REQ_CMP);
3978 xptnextfreepathid(void)
3985 mtx_lock(&xsoftc.xpt_topo_lock);
3986 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3988 /* Find an unoccupied pathid */
3989 while (bus != NULL && bus->path_id <= pathid) {
3990 if (bus->path_id == pathid)
3992 bus = TAILQ_NEXT(bus, links);
3994 mtx_unlock(&xsoftc.xpt_topo_lock);
3997 * Ensure that this pathid is not reserved for
3998 * a bus that may be registered in the future.
4000 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4002 /* Start the search over */
4003 mtx_lock(&xsoftc.xpt_topo_lock);
4010 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4017 pathid = CAM_XPT_PATH_ID;
4018 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4020 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4021 if (strcmp(dname, "scbus")) {
4022 /* Avoid a bit of foot shooting. */
4025 if (dunit < 0) /* unwired?! */
4027 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4028 if (sim_bus == val) {
4032 } else if (sim_bus == 0) {
4033 /* Unspecified matches bus 0 */
4037 printf("Ambiguous scbus configuration for %s%d "
4038 "bus %d, cannot wire down. The kernel "
4039 "config entry for scbus%d should "
4040 "specify a controller bus.\n"
4041 "Scbus will be assigned dynamically.\n",
4042 sim_name, sim_unit, sim_bus, dunit);
4047 if (pathid == CAM_XPT_PATH_ID)
4048 pathid = xptnextfreepathid();
4053 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4056 struct cam_et *target, *next_target;
4057 struct cam_ed *device, *next_device;
4059 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4061 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4064 * Most async events come from a CAM interrupt context. In
4065 * a few cases, the error recovery code at the peripheral layer,
4066 * which may run from our SWI or a process context, may signal
4067 * deferred events with a call to xpt_async.
4072 if (async_code == AC_BUS_RESET) {
4073 /* Update our notion of when the last reset occurred */
4074 microtime(&bus->last_reset);
4077 for (target = TAILQ_FIRST(&bus->et_entries);
4079 target = next_target) {
4081 next_target = TAILQ_NEXT(target, links);
4083 if (path->target != target
4084 && path->target->target_id != CAM_TARGET_WILDCARD
4085 && target->target_id != CAM_TARGET_WILDCARD)
4088 if (async_code == AC_SENT_BDR) {
4089 /* Update our notion of when the last reset occurred */
4090 microtime(&path->target->last_reset);
4093 for (device = TAILQ_FIRST(&target->ed_entries);
4095 device = next_device) {
4097 next_device = TAILQ_NEXT(device, links);
4099 if (path->device != device
4100 && path->device->lun_id != CAM_LUN_WILDCARD
4101 && device->lun_id != CAM_LUN_WILDCARD)
4104 * The async callback could free the device.
4105 * If it is a broadcast async, it doesn't hold
4106 * device reference, so take our own reference.
4108 xpt_acquire_device(device);
4109 (*(bus->xport->async))(async_code, bus,
4113 xpt_async_bcast(&device->asyncs, async_code,
4115 xpt_release_device(device);
4120 * If this wasn't a fully wildcarded async, tell all
4121 * clients that want all async events.
4123 if (bus != xpt_periph->path->bus)
4124 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4129 xpt_async_bcast(struct async_list *async_head,
4130 u_int32_t async_code,
4131 struct cam_path *path, void *async_arg)
4133 struct async_node *cur_entry;
4135 cur_entry = SLIST_FIRST(async_head);
4136 while (cur_entry != NULL) {
4137 struct async_node *next_entry;
4139 * Grab the next list entry before we call the current
4140 * entry's callback. This is because the callback function
4141 * can delete its async callback entry.
4143 next_entry = SLIST_NEXT(cur_entry, links);
4144 if ((cur_entry->event_enable & async_code) != 0)
4145 cur_entry->callback(cur_entry->callback_arg,
4148 cur_entry = next_entry;
4153 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4154 struct cam_et *target, struct cam_ed *device,
4157 printf("%s called\n", __func__);
4161 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4163 struct cam_ed *dev = path->device;
4165 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4166 dev->sim->devq->alloc_openings +=
4167 cam_ccbq_freeze(&dev->ccbq, rl, count);
4168 /* Remove frozen device from allocq. */
4169 if (device_is_alloc_queued(dev) &&
4170 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4171 CAMQ_GET_PRIO(&dev->drvq)))) {
4172 camq_remove(&dev->sim->devq->alloc_queue,
4173 dev->alloc_ccb_entry.pinfo.index);
4175 /* Remove frozen device from sendq. */
4176 if (device_is_send_queued(dev) &&
4177 cam_ccbq_frozen_top(&dev->ccbq)) {
4178 camq_remove(&dev->sim->devq->send_queue,
4179 dev->send_ccb_entry.pinfo.index);
4181 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4185 xpt_freeze_devq(struct cam_path *path, u_int count)
4188 return (xpt_freeze_devq_rl(path, 0, count));
4192 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4195 mtx_assert(sim->mtx, MA_OWNED);
4196 sim->devq->send_queue.qfrozen_cnt[0] += count;
4197 return (sim->devq->send_queue.qfrozen_cnt[0]);
4201 xpt_release_devq_timeout(void *arg)
4203 struct cam_ed *device;
4205 device = (struct cam_ed *)arg;
4207 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4211 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4213 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4215 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4219 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4221 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4223 xpt_release_devq_device(path->device, rl, count, run_queue);
4227 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4230 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4232 printf("xpt_release_devq(%d): requested %u > present %u\n",
4233 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4235 count = dev->ccbq.queue.qfrozen_cnt[rl];
4237 dev->sim->devq->alloc_openings -=
4238 cam_ccbq_release(&dev->ccbq, rl, count);
4239 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4240 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4241 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4242 xpt_run_dev_allocq(dev->target->bus);
4244 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4246 * No longer need to wait for a successful
4247 * command completion.
4249 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4251 * Remove any timeouts that might be scheduled
4252 * to release this queue.
4254 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4255 callout_stop(&dev->callout);
4256 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4261 * Now that we are unfrozen schedule the
4262 * device so any pending transactions are
4265 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4266 xpt_run_dev_sendq(dev->target->bus);
4271 xpt_release_simq(struct cam_sim *sim, int run_queue)
4275 mtx_assert(sim->mtx, MA_OWNED);
4276 sendq = &(sim->devq->send_queue);
4277 if (sendq->qfrozen_cnt[0] <= 0) {
4279 printf("xpt_release_simq: requested 1 > present %u\n",
4280 sendq->qfrozen_cnt[0]);
4283 sendq->qfrozen_cnt[0]--;
4284 if (sendq->qfrozen_cnt[0] == 0) {
4286 * If there is a timeout scheduled to release this
4287 * sim queue, remove it. The queue frozen count is
4290 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4291 callout_stop(&sim->callout);
4292 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4298 * Now that we are unfrozen run the send queue.
4300 bus = xpt_find_bus(sim->path_id);
4301 xpt_run_dev_sendq(bus);
4302 xpt_release_bus(bus);
4308 * XXX Appears to be unused.
4311 xpt_release_simq_timeout(void *arg)
4313 struct cam_sim *sim;
4315 sim = (struct cam_sim *)arg;
4316 xpt_release_simq(sim, /* run_queue */ TRUE);
4320 xpt_done(union ccb *done_ccb)
4322 struct cam_sim *sim;
4325 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4326 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4328 * Queue up the request for handling by our SWI handler
4329 * any of the "non-immediate" type of ccbs.
4331 sim = done_ccb->ccb_h.path->bus->sim;
4332 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4334 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4335 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4336 CAM_SIM_BATCH)) == 0) {
4337 mtx_lock(&cam_simq_lock);
4338 first = TAILQ_EMPTY(&cam_simq);
4339 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4340 mtx_unlock(&cam_simq_lock);
4341 sim->flags |= CAM_SIM_ON_DONEQ;
4343 swi_sched(cambio_ih, 0);
4349 xpt_batch_start(struct cam_sim *sim)
4352 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4353 sim->flags |= CAM_SIM_BATCH;
4357 xpt_batch_done(struct cam_sim *sim)
4360 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4361 sim->flags &= ~CAM_SIM_BATCH;
4362 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4363 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4364 camisr_runqueue(&sim->sim_doneq);
4372 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4377 xpt_alloc_ccb_nowait()
4381 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4386 xpt_free_ccb(union ccb *free_ccb)
4388 free(free_ccb, M_CAMXPT);
4393 /* Private XPT functions */
4396 * Get a CAM control block for the caller. Charge the structure to the device
4397 * referenced by the path. If the this device has no 'credits' then the
4398 * device already has the maximum number of outstanding operations under way
4399 * and we return NULL. If we don't have sufficient resources to allocate more
4400 * ccbs, we also return NULL.
4403 xpt_get_ccb(struct cam_ed *device)
4406 struct cam_sim *sim;
4409 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4410 new_ccb = xpt_alloc_ccb_nowait();
4411 if (new_ccb == NULL) {
4414 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4415 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4416 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4420 cam_ccbq_take_opening(&device->ccbq);
4421 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4426 xpt_release_bus(struct cam_eb *bus)
4429 mtx_lock(&xsoftc.xpt_topo_lock);
4430 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4431 if ((--bus->refcount == 0)
4432 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4433 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4434 xsoftc.bus_generation++;
4435 mtx_unlock(&xsoftc.xpt_topo_lock);
4436 cam_sim_release(bus->sim);
4437 free(bus, M_CAMXPT);
4439 mtx_unlock(&xsoftc.xpt_topo_lock);
4442 static struct cam_et *
4443 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4445 struct cam_et *target;
4447 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4449 if (target != NULL) {
4450 struct cam_et *cur_target;
4452 TAILQ_INIT(&target->ed_entries);
4454 target->target_id = target_id;
4455 target->refcount = 1;
4456 target->generation = 0;
4457 target->luns = NULL;
4458 timevalclear(&target->last_reset);
4460 * Hold a reference to our parent bus so it
4461 * will not go away before we do.
4463 mtx_lock(&xsoftc.xpt_topo_lock);
4465 mtx_unlock(&xsoftc.xpt_topo_lock);
4467 /* Insertion sort into our bus's target list */
4468 cur_target = TAILQ_FIRST(&bus->et_entries);
4469 while (cur_target != NULL && cur_target->target_id < target_id)
4470 cur_target = TAILQ_NEXT(cur_target, links);
4472 if (cur_target != NULL) {
4473 TAILQ_INSERT_BEFORE(cur_target, target, links);
4475 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4483 xpt_release_target(struct cam_et *target)
4486 if (target->refcount == 1) {
4487 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4488 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4489 target->bus->generation++;
4490 xpt_release_bus(target->bus);
4492 free(target->luns, M_CAMXPT);
4493 free(target, M_CAMXPT);
4499 static struct cam_ed *
4500 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4503 struct cam_ed *device, *cur_device;
4505 device = xpt_alloc_device(bus, target, lun_id);
4509 device->mintags = 1;
4510 device->maxtags = 1;
4511 bus->sim->max_ccbs += device->ccbq.devq_openings;
4512 cur_device = TAILQ_FIRST(&target->ed_entries);
4513 while (cur_device != NULL && cur_device->lun_id < lun_id)
4514 cur_device = TAILQ_NEXT(cur_device, links);
4515 if (cur_device != NULL) {
4516 TAILQ_INSERT_BEFORE(cur_device, device, links);
4518 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4520 target->generation++;
4526 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4528 struct cam_ed *device;
4529 struct cam_devq *devq;
4532 /* Make space for us in the device queue on our bus */
4533 devq = bus->sim->devq;
4534 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4536 if (status != CAM_REQ_CMP) {
4539 device = (struct cam_ed *)malloc(sizeof(*device),
4540 M_CAMXPT, M_NOWAIT|M_ZERO);
4543 if (device != NULL) {
4544 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4545 device->alloc_ccb_entry.device = device;
4546 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4547 device->send_ccb_entry.device = device;
4548 device->target = target;
4549 device->lun_id = lun_id;
4550 device->sim = bus->sim;
4551 /* Initialize our queues */
4552 if (camq_init(&device->drvq, 0) != 0) {
4553 free(device, M_CAMXPT);
4556 if (cam_ccbq_init(&device->ccbq,
4557 bus->sim->max_dev_openings) != 0) {
4558 camq_fini(&device->drvq);
4559 free(device, M_CAMXPT);
4562 SLIST_INIT(&device->asyncs);
4563 SLIST_INIT(&device->periphs);
4564 device->generation = 0;
4565 device->owner = NULL;
4566 device->flags = CAM_DEV_UNCONFIGURED;
4567 device->tag_delay_count = 0;
4568 device->tag_saved_openings = 0;
4569 device->refcount = 1;
4570 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4573 * Hold a reference to our parent target so it
4574 * will not go away before we do.
4583 xpt_acquire_device(struct cam_ed *device)
4590 xpt_release_device(struct cam_ed *device)
4593 if (device->refcount == 1) {
4594 struct cam_devq *devq;
4596 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4597 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4598 panic("Removing device while still queued for ccbs");
4600 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4601 callout_stop(&device->callout);
4603 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4604 device->target->generation++;
4605 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4606 /* Release our slot in the devq */
4607 devq = device->target->bus->sim->devq;
4608 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4609 camq_fini(&device->drvq);
4610 cam_ccbq_fini(&device->ccbq);
4611 xpt_release_target(device->target);
4612 free(device, M_CAMXPT);
4618 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4626 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4627 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4628 if (result == CAM_REQ_CMP && (diff < 0)) {
4629 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4631 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4632 || (dev->inq_flags & SID_CmdQue) != 0)
4633 dev->tag_saved_openings = newopenings;
4634 /* Adjust the global limit */
4635 dev->sim->max_ccbs += diff;
4639 static struct cam_eb *
4640 xpt_find_bus(path_id_t path_id)
4644 mtx_lock(&xsoftc.xpt_topo_lock);
4645 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4647 bus = TAILQ_NEXT(bus, links)) {
4648 if (bus->path_id == path_id) {
4653 mtx_unlock(&xsoftc.xpt_topo_lock);
4657 static struct cam_et *
4658 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4660 struct cam_et *target;
4662 for (target = TAILQ_FIRST(&bus->et_entries);
4664 target = TAILQ_NEXT(target, links)) {
4665 if (target->target_id == target_id) {
4673 static struct cam_ed *
4674 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4676 struct cam_ed *device;
4678 for (device = TAILQ_FIRST(&target->ed_entries);
4680 device = TAILQ_NEXT(device, links)) {
4681 if (device->lun_id == lun_id) {
4690 xpt_start_tags(struct cam_path *path)
4692 struct ccb_relsim crs;
4693 struct cam_ed *device;
4694 struct cam_sim *sim;
4697 device = path->device;
4698 sim = path->bus->sim;
4699 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4700 xpt_freeze_devq(path, /*count*/1);
4701 device->inq_flags |= SID_CmdQue;
4702 if (device->tag_saved_openings != 0)
4703 newopenings = device->tag_saved_openings;
4705 newopenings = min(device->maxtags,
4706 sim->max_tagged_dev_openings);
4707 xpt_dev_ccbq_resize(path, newopenings);
4708 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4709 crs.ccb_h.func_code = XPT_REL_SIMQ;
4710 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4712 = crs.release_timeout
4715 xpt_action((union ccb *)&crs);
4719 xpt_stop_tags(struct cam_path *path)
4721 struct ccb_relsim crs;
4722 struct cam_ed *device;
4723 struct cam_sim *sim;
4725 device = path->device;
4726 sim = path->bus->sim;
4727 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4728 device->tag_delay_count = 0;
4729 xpt_freeze_devq(path, /*count*/1);
4730 device->inq_flags &= ~SID_CmdQue;
4731 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4732 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4733 crs.ccb_h.func_code = XPT_REL_SIMQ;
4734 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4736 = crs.release_timeout
4739 xpt_action((union ccb *)&crs);
4743 xpt_boot_delay(void *arg)
4750 xpt_config(void *arg)
4753 * Now that interrupts are enabled, go find our devices
4757 /* Setup debugging flags and path */
4758 #ifdef CAM_DEBUG_BUS
4759 if (cam_dflags != CAM_DEBUG_NONE) {
4761 * Locking is specifically omitted here. No SIMs have
4762 * registered yet, so xpt_create_path will only be searching
4763 * empty lists of targets and devices.
4765 if (xpt_create_path(&cam_dpath, xpt_periph,
4766 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4767 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4768 printf("xpt_config: xpt_create_path() failed for debug"
4769 " target %d:%d:%d, debugging disabled\n",
4770 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4771 cam_dflags = CAM_DEBUG_NONE;
4775 #else /* !CAM_DEBUG_BUS */
4777 #endif /* CAM_DEBUG_BUS */
4778 #endif /* CAMDEBUG */
4780 periphdriver_init(1);
4782 callout_init(&xsoftc.boot_callout, 1);
4783 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4784 xpt_boot_delay, NULL);
4785 /* Fire up rescan thread. */
4786 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4787 printf("xpt_config: failed to create rescan thread.\n");
4795 xsoftc.buses_to_config++;
4800 xpt_release_boot(void)
4803 xsoftc.buses_to_config--;
4804 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4805 struct xpt_task *task;
4807 xsoftc.buses_config_done = 1;
4809 /* Call manually because we don't have any busses */
4810 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4812 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4813 taskqueue_enqueue(taskqueue_thread, &task->task);
4820 * If the given device only has one peripheral attached to it, and if that
4821 * peripheral is the passthrough driver, announce it. This insures that the
4822 * user sees some sort of announcement for every peripheral in their system.
4825 xptpassannouncefunc(struct cam_ed *device, void *arg)
4827 struct cam_periph *periph;
4830 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4831 periph = SLIST_NEXT(periph, periph_links), i++);
4833 periph = SLIST_FIRST(&device->periphs);
4835 && (strncmp(periph->periph_name, "pass", 4) == 0))
4836 xpt_announce_periph(periph, NULL);
4842 xpt_finishconfig_task(void *context, int pending)
4845 periphdriver_init(2);
4847 * Check for devices with no "standard" peripheral driver
4848 * attached. For any devices like that, announce the
4849 * passthrough driver so the user will see something.
4851 xpt_for_all_devices(xptpassannouncefunc, NULL);
4853 /* Release our hook so that the boot can continue. */
4854 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4855 free(xsoftc.xpt_config_hook, M_CAMXPT);
4856 xsoftc.xpt_config_hook = NULL;
4858 free(context, M_CAMXPT);
4862 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4863 struct cam_path *path)
4865 struct ccb_setasync csa;
4870 mtx_lock(&xsoftc.xpt_lock);
4871 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4872 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4873 if (status != CAM_REQ_CMP) {
4874 mtx_unlock(&xsoftc.xpt_lock);
4880 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4881 csa.ccb_h.func_code = XPT_SASYNC_CB;
4882 csa.event_enable = event;
4883 csa.callback = cbfunc;
4884 csa.callback_arg = cbarg;
4885 xpt_action((union ccb *)&csa);
4886 status = csa.ccb_h.status;
4889 xpt_free_path(path);
4890 mtx_unlock(&xsoftc.xpt_lock);
4893 if ((status == CAM_REQ_CMP) &&
4894 (csa.event_enable & AC_FOUND_DEVICE)) {
4896 * Get this peripheral up to date with all
4897 * the currently existing devices.
4899 xpt_for_all_devices(xptsetasyncfunc, &csa);
4901 if ((status == CAM_REQ_CMP) &&
4902 (csa.event_enable & AC_PATH_REGISTERED)) {
4904 * Get this peripheral up to date with all
4905 * the currently existing busses.
4907 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4914 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4916 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4918 switch (work_ccb->ccb_h.func_code) {
4919 /* Common cases first */
4920 case XPT_PATH_INQ: /* Path routing inquiry */
4922 struct ccb_pathinq *cpi;
4924 cpi = &work_ccb->cpi;
4925 cpi->version_num = 1; /* XXX??? */
4926 cpi->hba_inquiry = 0;
4927 cpi->target_sprt = 0;
4929 cpi->hba_eng_cnt = 0;
4930 cpi->max_target = 0;
4932 cpi->initiator_id = 0;
4933 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4934 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4935 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4936 cpi->unit_number = sim->unit_number;
4937 cpi->bus_id = sim->bus_id;
4938 cpi->base_transfer_speed = 0;
4939 cpi->protocol = PROTO_UNSPECIFIED;
4940 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4941 cpi->transport = XPORT_UNSPECIFIED;
4942 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4943 cpi->ccb_h.status = CAM_REQ_CMP;
4948 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4955 * The xpt as a "controller" has no interrupt sources, so polling
4959 xptpoll(struct cam_sim *sim)
4964 xpt_lock_buses(void)
4966 mtx_lock(&xsoftc.xpt_topo_lock);
4970 xpt_unlock_buses(void)
4972 mtx_unlock(&xsoftc.xpt_topo_lock);
4979 struct cam_sim *sim;
4981 mtx_lock(&cam_simq_lock);
4983 while (!TAILQ_EMPTY(&cam_simq)) {
4984 TAILQ_CONCAT(&queue, &cam_simq, links);
4985 mtx_unlock(&cam_simq_lock);
4987 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4988 TAILQ_REMOVE(&queue, sim, links);
4990 sim->flags &= ~CAM_SIM_ON_DONEQ;
4991 camisr_runqueue(&sim->sim_doneq);
4992 CAM_SIM_UNLOCK(sim);
4994 mtx_lock(&cam_simq_lock);
4996 mtx_unlock(&cam_simq_lock);
5000 camisr_runqueue(void *V_queue)
5002 cam_isrq_t *queue = V_queue;
5003 struct ccb_hdr *ccb_h;
5005 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5008 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5009 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5011 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5016 if (ccb_h->flags & CAM_HIGH_POWER) {
5017 struct highpowerlist *hphead;
5018 union ccb *send_ccb;
5020 mtx_lock(&xsoftc.xpt_lock);
5021 hphead = &xsoftc.highpowerq;
5023 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5026 * Increment the count since this command is done.
5028 xsoftc.num_highpower++;
5031 * Any high powered commands queued up?
5033 if (send_ccb != NULL) {
5035 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5036 mtx_unlock(&xsoftc.xpt_lock);
5038 xpt_release_devq(send_ccb->ccb_h.path,
5039 /*count*/1, /*runqueue*/TRUE);
5041 mtx_unlock(&xsoftc.xpt_lock);
5044 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5047 dev = ccb_h->path->device;
5049 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5050 ccb_h->path->bus->sim->devq->send_active--;
5051 ccb_h->path->bus->sim->devq->send_openings++;
5054 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5055 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
5056 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5057 && (dev->ccbq.dev_active == 0))) {
5058 xpt_release_devq(ccb_h->path, /*count*/1,
5059 /*run_queue*/FALSE);
5062 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5063 && (--dev->tag_delay_count == 0))
5064 xpt_start_tags(ccb_h->path);
5065 if (!device_is_send_queued(dev)) {
5066 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5071 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5072 xpt_release_simq(ccb_h->path->bus->sim,
5074 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5078 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5079 && (ccb_h->status & CAM_DEV_QFRZN)) {
5080 xpt_release_devq(ccb_h->path, /*count*/1,
5082 ccb_h->status &= ~CAM_DEV_QFRZN;
5084 xpt_run_dev_sendq(ccb_h->path->bus);
5087 /* Call the peripheral driver's callback */
5088 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);