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);
1083 else if (path->device->protocol == PROTO_SEMB)
1085 (struct sep_identify_data *)&path->device->ident_data);
1087 printf("Unknown protocol device\n");
1088 if (bootverbose && path->device->serial_num_len > 0) {
1089 /* Don't wrap the screen - print only the first 60 chars */
1090 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1091 periph->unit_number, path->device->serial_num);
1093 /* Announce transport details. */
1094 (*(path->bus->xport->announce))(periph);
1095 /* Announce command queueing. */
1096 if (path->device->inq_flags & SID_CmdQue
1097 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1098 printf("%s%d: Command Queueing enabled\n",
1099 periph->periph_name, periph->unit_number);
1101 /* Announce caller's details if they've passed in. */
1102 if (announce_string != NULL)
1103 printf("%s%d: %s\n", periph->periph_name,
1104 periph->unit_number, announce_string);
1108 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1111 struct ccb_dev_advinfo cdai;
1113 memset(&cdai, 0, sizeof(cdai));
1114 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1115 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1118 if (!strcmp(attr, "GEOM::ident"))
1119 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1120 else if (!strcmp(attr, "GEOM::physpath"))
1121 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1125 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1126 if (cdai.buf == NULL) {
1130 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1131 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1132 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1133 if (cdai.provsiz == 0)
1136 if (strlcpy(buf, cdai.buf, len) >= len)
1140 if (cdai.buf != NULL)
1141 free(cdai.buf, M_CAMXPT);
1145 static dev_match_ret
1146 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1149 dev_match_ret retval;
1152 retval = DM_RET_NONE;
1155 * If we aren't given something to match against, that's an error.
1158 return(DM_RET_ERROR);
1161 * If there are no match entries, then this bus matches no
1164 if ((patterns == NULL) || (num_patterns == 0))
1165 return(DM_RET_DESCEND | DM_RET_COPY);
1167 for (i = 0; i < num_patterns; i++) {
1168 struct bus_match_pattern *cur_pattern;
1171 * If the pattern in question isn't for a bus node, we
1172 * aren't interested. However, we do indicate to the
1173 * calling routine that we should continue descending the
1174 * tree, since the user wants to match against lower-level
1177 if (patterns[i].type != DEV_MATCH_BUS) {
1178 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1179 retval |= DM_RET_DESCEND;
1183 cur_pattern = &patterns[i].pattern.bus_pattern;
1186 * If they want to match any bus node, we give them any
1189 if (cur_pattern->flags == BUS_MATCH_ANY) {
1190 /* set the copy flag */
1191 retval |= DM_RET_COPY;
1194 * If we've already decided on an action, go ahead
1197 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1202 * Not sure why someone would do this...
1204 if (cur_pattern->flags == BUS_MATCH_NONE)
1207 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1208 && (cur_pattern->path_id != bus->path_id))
1211 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1212 && (cur_pattern->bus_id != bus->sim->bus_id))
1215 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1216 && (cur_pattern->unit_number != bus->sim->unit_number))
1219 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1220 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1225 * If we get to this point, the user definitely wants
1226 * information on this bus. So tell the caller to copy the
1229 retval |= DM_RET_COPY;
1232 * If the return action has been set to descend, then we
1233 * know that we've already seen a non-bus matching
1234 * expression, therefore we need to further descend the tree.
1235 * This won't change by continuing around the loop, so we
1236 * go ahead and return. If we haven't seen a non-bus
1237 * matching expression, we keep going around the loop until
1238 * we exhaust the matching expressions. We'll set the stop
1239 * flag once we fall out of the loop.
1241 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1246 * If the return action hasn't been set to descend yet, that means
1247 * we haven't seen anything other than bus matching patterns. So
1248 * tell the caller to stop descending the tree -- the user doesn't
1249 * want to match against lower level tree elements.
1251 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1252 retval |= DM_RET_STOP;
1257 static dev_match_ret
1258 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1259 struct cam_ed *device)
1261 dev_match_ret retval;
1264 retval = DM_RET_NONE;
1267 * If we aren't given something to match against, that's an error.
1270 return(DM_RET_ERROR);
1273 * If there are no match entries, then this device matches no
1276 if ((patterns == NULL) || (num_patterns == 0))
1277 return(DM_RET_DESCEND | DM_RET_COPY);
1279 for (i = 0; i < num_patterns; i++) {
1280 struct device_match_pattern *cur_pattern;
1281 struct scsi_vpd_device_id *device_id_page;
1284 * If the pattern in question isn't for a device node, we
1285 * aren't interested.
1287 if (patterns[i].type != DEV_MATCH_DEVICE) {
1288 if ((patterns[i].type == DEV_MATCH_PERIPH)
1289 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1290 retval |= DM_RET_DESCEND;
1294 cur_pattern = &patterns[i].pattern.device_pattern;
1296 /* Error out if mutually exclusive options are specified. */
1297 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1298 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1299 return(DM_RET_ERROR);
1302 * If they want to match any device node, we give them any
1305 if (cur_pattern->flags == DEV_MATCH_ANY)
1309 * Not sure why someone would do this...
1311 if (cur_pattern->flags == DEV_MATCH_NONE)
1314 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1315 && (cur_pattern->path_id != device->target->bus->path_id))
1318 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1319 && (cur_pattern->target_id != device->target->target_id))
1322 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1323 && (cur_pattern->target_lun != device->lun_id))
1326 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1327 && (cam_quirkmatch((caddr_t)&device->inq_data,
1328 (caddr_t)&cur_pattern->data.inq_pat,
1329 1, sizeof(cur_pattern->data.inq_pat),
1330 scsi_static_inquiry_match) == NULL))
1333 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1334 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1335 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1336 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1337 device->device_id_len
1338 - SVPD_DEVICE_ID_HDR_LEN,
1339 cur_pattern->data.devid_pat.id,
1340 cur_pattern->data.devid_pat.id_len) != 0))
1345 * If we get to this point, the user definitely wants
1346 * information on this device. So tell the caller to copy
1349 retval |= DM_RET_COPY;
1352 * If the return action has been set to descend, then we
1353 * know that we've already seen a peripheral matching
1354 * expression, therefore we need to further descend the tree.
1355 * This won't change by continuing around the loop, so we
1356 * go ahead and return. If we haven't seen a peripheral
1357 * matching expression, we keep going around the loop until
1358 * we exhaust the matching expressions. We'll set the stop
1359 * flag once we fall out of the loop.
1361 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1366 * If the return action hasn't been set to descend yet, that means
1367 * we haven't seen any peripheral matching patterns. So tell the
1368 * caller to stop descending the tree -- the user doesn't want to
1369 * match against lower level tree elements.
1371 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1372 retval |= DM_RET_STOP;
1378 * Match a single peripheral against any number of match patterns.
1380 static dev_match_ret
1381 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1382 struct cam_periph *periph)
1384 dev_match_ret retval;
1388 * If we aren't given something to match against, that's an error.
1391 return(DM_RET_ERROR);
1394 * If there are no match entries, then this peripheral matches no
1397 if ((patterns == NULL) || (num_patterns == 0))
1398 return(DM_RET_STOP | DM_RET_COPY);
1401 * There aren't any nodes below a peripheral node, so there's no
1402 * reason to descend the tree any further.
1404 retval = DM_RET_STOP;
1406 for (i = 0; i < num_patterns; i++) {
1407 struct periph_match_pattern *cur_pattern;
1410 * If the pattern in question isn't for a peripheral, we
1411 * aren't interested.
1413 if (patterns[i].type != DEV_MATCH_PERIPH)
1416 cur_pattern = &patterns[i].pattern.periph_pattern;
1419 * If they want to match on anything, then we will do so.
1421 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1422 /* set the copy flag */
1423 retval |= DM_RET_COPY;
1426 * We've already set the return action to stop,
1427 * since there are no nodes below peripherals in
1434 * Not sure why someone would do this...
1436 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1439 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1440 && (cur_pattern->path_id != periph->path->bus->path_id))
1444 * For the target and lun id's, we have to make sure the
1445 * target and lun pointers aren't NULL. The xpt peripheral
1446 * has a wildcard target and device.
1448 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1449 && ((periph->path->target == NULL)
1450 ||(cur_pattern->target_id != periph->path->target->target_id)))
1453 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1454 && ((periph->path->device == NULL)
1455 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1458 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1459 && (cur_pattern->unit_number != periph->unit_number))
1462 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1463 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1468 * If we get to this point, the user definitely wants
1469 * information on this peripheral. So tell the caller to
1470 * copy the data out.
1472 retval |= DM_RET_COPY;
1475 * The return action has already been set to stop, since
1476 * peripherals don't have any nodes below them in the EDT.
1482 * If we get to this point, the peripheral that was passed in
1483 * doesn't match any of the patterns.
1489 xptedtbusfunc(struct cam_eb *bus, void *arg)
1491 struct ccb_dev_match *cdm;
1492 dev_match_ret retval;
1494 cdm = (struct ccb_dev_match *)arg;
1497 * If our position is for something deeper in the tree, that means
1498 * that we've already seen this node. So, we keep going down.
1500 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1501 && (cdm->pos.cookie.bus == bus)
1502 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1503 && (cdm->pos.cookie.target != NULL))
1504 retval = DM_RET_DESCEND;
1506 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1509 * If we got an error, bail out of the search.
1511 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1512 cdm->status = CAM_DEV_MATCH_ERROR;
1517 * If the copy flag is set, copy this bus out.
1519 if (retval & DM_RET_COPY) {
1522 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1523 sizeof(struct dev_match_result));
1526 * If we don't have enough space to put in another
1527 * match result, save our position and tell the
1528 * user there are more devices to check.
1530 if (spaceleft < sizeof(struct dev_match_result)) {
1531 bzero(&cdm->pos, sizeof(cdm->pos));
1532 cdm->pos.position_type =
1533 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1535 cdm->pos.cookie.bus = bus;
1536 cdm->pos.generations[CAM_BUS_GENERATION]=
1537 xsoftc.bus_generation;
1538 cdm->status = CAM_DEV_MATCH_MORE;
1541 j = cdm->num_matches;
1543 cdm->matches[j].type = DEV_MATCH_BUS;
1544 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1545 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1546 cdm->matches[j].result.bus_result.unit_number =
1547 bus->sim->unit_number;
1548 strncpy(cdm->matches[j].result.bus_result.dev_name,
1549 bus->sim->sim_name, DEV_IDLEN);
1553 * If the user is only interested in busses, there's no
1554 * reason to descend to the next level in the tree.
1556 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1560 * If there is a target generation recorded, check it to
1561 * make sure the target list hasn't changed.
1563 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1564 && (bus == cdm->pos.cookie.bus)
1565 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1566 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1567 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1569 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1573 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1574 && (cdm->pos.cookie.bus == bus)
1575 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1576 && (cdm->pos.cookie.target != NULL))
1577 return(xpttargettraverse(bus,
1578 (struct cam_et *)cdm->pos.cookie.target,
1579 xptedttargetfunc, arg));
1581 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1585 xptedttargetfunc(struct cam_et *target, void *arg)
1587 struct ccb_dev_match *cdm;
1589 cdm = (struct ccb_dev_match *)arg;
1592 * If there is a device list generation recorded, check it to
1593 * make sure the device list hasn't changed.
1595 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1596 && (cdm->pos.cookie.bus == target->bus)
1597 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1598 && (cdm->pos.cookie.target == target)
1599 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1600 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1601 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1602 target->generation)) {
1603 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1607 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1608 && (cdm->pos.cookie.bus == target->bus)
1609 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1610 && (cdm->pos.cookie.target == target)
1611 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1612 && (cdm->pos.cookie.device != NULL))
1613 return(xptdevicetraverse(target,
1614 (struct cam_ed *)cdm->pos.cookie.device,
1615 xptedtdevicefunc, arg));
1617 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1621 xptedtdevicefunc(struct cam_ed *device, void *arg)
1624 struct ccb_dev_match *cdm;
1625 dev_match_ret retval;
1627 cdm = (struct ccb_dev_match *)arg;
1630 * If our position is for something deeper in the tree, that means
1631 * that we've already seen this node. So, we keep going down.
1633 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1634 && (cdm->pos.cookie.device == device)
1635 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1636 && (cdm->pos.cookie.periph != NULL))
1637 retval = DM_RET_DESCEND;
1639 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1642 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1643 cdm->status = CAM_DEV_MATCH_ERROR;
1648 * If the copy flag is set, copy this device out.
1650 if (retval & DM_RET_COPY) {
1653 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1654 sizeof(struct dev_match_result));
1657 * If we don't have enough space to put in another
1658 * match result, save our position and tell the
1659 * user there are more devices to check.
1661 if (spaceleft < sizeof(struct dev_match_result)) {
1662 bzero(&cdm->pos, sizeof(cdm->pos));
1663 cdm->pos.position_type =
1664 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1665 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1667 cdm->pos.cookie.bus = device->target->bus;
1668 cdm->pos.generations[CAM_BUS_GENERATION]=
1669 xsoftc.bus_generation;
1670 cdm->pos.cookie.target = device->target;
1671 cdm->pos.generations[CAM_TARGET_GENERATION] =
1672 device->target->bus->generation;
1673 cdm->pos.cookie.device = device;
1674 cdm->pos.generations[CAM_DEV_GENERATION] =
1675 device->target->generation;
1676 cdm->status = CAM_DEV_MATCH_MORE;
1679 j = cdm->num_matches;
1681 cdm->matches[j].type = DEV_MATCH_DEVICE;
1682 cdm->matches[j].result.device_result.path_id =
1683 device->target->bus->path_id;
1684 cdm->matches[j].result.device_result.target_id =
1685 device->target->target_id;
1686 cdm->matches[j].result.device_result.target_lun =
1688 cdm->matches[j].result.device_result.protocol =
1690 bcopy(&device->inq_data,
1691 &cdm->matches[j].result.device_result.inq_data,
1692 sizeof(struct scsi_inquiry_data));
1693 bcopy(&device->ident_data,
1694 &cdm->matches[j].result.device_result.ident_data,
1695 sizeof(struct ata_params));
1697 /* Let the user know whether this device is unconfigured */
1698 if (device->flags & CAM_DEV_UNCONFIGURED)
1699 cdm->matches[j].result.device_result.flags =
1700 DEV_RESULT_UNCONFIGURED;
1702 cdm->matches[j].result.device_result.flags =
1707 * If the user isn't interested in peripherals, don't descend
1708 * the tree any further.
1710 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1714 * If there is a peripheral list generation recorded, make sure
1715 * it hasn't changed.
1717 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1718 && (device->target->bus == cdm->pos.cookie.bus)
1719 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1720 && (device->target == cdm->pos.cookie.target)
1721 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1722 && (device == cdm->pos.cookie.device)
1723 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1724 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1725 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1726 device->generation)){
1727 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1731 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1732 && (cdm->pos.cookie.bus == device->target->bus)
1733 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1734 && (cdm->pos.cookie.target == device->target)
1735 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1736 && (cdm->pos.cookie.device == device)
1737 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1738 && (cdm->pos.cookie.periph != NULL))
1739 return(xptperiphtraverse(device,
1740 (struct cam_periph *)cdm->pos.cookie.periph,
1741 xptedtperiphfunc, arg));
1743 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1747 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1749 struct ccb_dev_match *cdm;
1750 dev_match_ret retval;
1752 cdm = (struct ccb_dev_match *)arg;
1754 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1756 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1757 cdm->status = CAM_DEV_MATCH_ERROR;
1762 * If the copy flag is set, copy this peripheral out.
1764 if (retval & DM_RET_COPY) {
1767 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1768 sizeof(struct dev_match_result));
1771 * If we don't have enough space to put in another
1772 * match result, save our position and tell the
1773 * user there are more devices to check.
1775 if (spaceleft < sizeof(struct dev_match_result)) {
1776 bzero(&cdm->pos, sizeof(cdm->pos));
1777 cdm->pos.position_type =
1778 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1779 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1782 cdm->pos.cookie.bus = periph->path->bus;
1783 cdm->pos.generations[CAM_BUS_GENERATION]=
1784 xsoftc.bus_generation;
1785 cdm->pos.cookie.target = periph->path->target;
1786 cdm->pos.generations[CAM_TARGET_GENERATION] =
1787 periph->path->bus->generation;
1788 cdm->pos.cookie.device = periph->path->device;
1789 cdm->pos.generations[CAM_DEV_GENERATION] =
1790 periph->path->target->generation;
1791 cdm->pos.cookie.periph = periph;
1792 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1793 periph->path->device->generation;
1794 cdm->status = CAM_DEV_MATCH_MORE;
1798 j = cdm->num_matches;
1800 cdm->matches[j].type = DEV_MATCH_PERIPH;
1801 cdm->matches[j].result.periph_result.path_id =
1802 periph->path->bus->path_id;
1803 cdm->matches[j].result.periph_result.target_id =
1804 periph->path->target->target_id;
1805 cdm->matches[j].result.periph_result.target_lun =
1806 periph->path->device->lun_id;
1807 cdm->matches[j].result.periph_result.unit_number =
1808 periph->unit_number;
1809 strncpy(cdm->matches[j].result.periph_result.periph_name,
1810 periph->periph_name, DEV_IDLEN);
1817 xptedtmatch(struct ccb_dev_match *cdm)
1821 cdm->num_matches = 0;
1824 * Check the bus list generation. If it has changed, the user
1825 * needs to reset everything and start over.
1827 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1828 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1829 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1830 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1834 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1835 && (cdm->pos.cookie.bus != NULL))
1836 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1837 xptedtbusfunc, cdm);
1839 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1842 * If we get back 0, that means that we had to stop before fully
1843 * traversing the EDT. It also means that one of the subroutines
1844 * has set the status field to the proper value. If we get back 1,
1845 * we've fully traversed the EDT and copied out any matching entries.
1848 cdm->status = CAM_DEV_MATCH_LAST;
1854 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1856 struct ccb_dev_match *cdm;
1858 cdm = (struct ccb_dev_match *)arg;
1860 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1861 && (cdm->pos.cookie.pdrv == pdrv)
1862 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1863 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1864 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1865 (*pdrv)->generation)) {
1866 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1870 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1871 && (cdm->pos.cookie.pdrv == pdrv)
1872 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1873 && (cdm->pos.cookie.periph != NULL))
1874 return(xptpdperiphtraverse(pdrv,
1875 (struct cam_periph *)cdm->pos.cookie.periph,
1876 xptplistperiphfunc, arg));
1878 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1882 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1884 struct ccb_dev_match *cdm;
1885 dev_match_ret retval;
1887 cdm = (struct ccb_dev_match *)arg;
1889 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1891 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1892 cdm->status = CAM_DEV_MATCH_ERROR;
1897 * If the copy flag is set, copy this peripheral out.
1899 if (retval & DM_RET_COPY) {
1902 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1903 sizeof(struct dev_match_result));
1906 * If we don't have enough space to put in another
1907 * match result, save our position and tell the
1908 * user there are more devices to check.
1910 if (spaceleft < sizeof(struct dev_match_result)) {
1911 struct periph_driver **pdrv;
1914 bzero(&cdm->pos, sizeof(cdm->pos));
1915 cdm->pos.position_type =
1916 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1920 * This may look a bit non-sensical, but it is
1921 * actually quite logical. There are very few
1922 * peripheral drivers, and bloating every peripheral
1923 * structure with a pointer back to its parent
1924 * peripheral driver linker set entry would cost
1925 * more in the long run than doing this quick lookup.
1927 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1928 if (strcmp((*pdrv)->driver_name,
1929 periph->periph_name) == 0)
1933 if (*pdrv == NULL) {
1934 cdm->status = CAM_DEV_MATCH_ERROR;
1938 cdm->pos.cookie.pdrv = pdrv;
1940 * The periph generation slot does double duty, as
1941 * does the periph pointer slot. They are used for
1942 * both edt and pdrv lookups and positioning.
1944 cdm->pos.cookie.periph = periph;
1945 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1946 (*pdrv)->generation;
1947 cdm->status = CAM_DEV_MATCH_MORE;
1951 j = cdm->num_matches;
1953 cdm->matches[j].type = DEV_MATCH_PERIPH;
1954 cdm->matches[j].result.periph_result.path_id =
1955 periph->path->bus->path_id;
1958 * The transport layer peripheral doesn't have a target or
1961 if (periph->path->target)
1962 cdm->matches[j].result.periph_result.target_id =
1963 periph->path->target->target_id;
1965 cdm->matches[j].result.periph_result.target_id = -1;
1967 if (periph->path->device)
1968 cdm->matches[j].result.periph_result.target_lun =
1969 periph->path->device->lun_id;
1971 cdm->matches[j].result.periph_result.target_lun = -1;
1973 cdm->matches[j].result.periph_result.unit_number =
1974 periph->unit_number;
1975 strncpy(cdm->matches[j].result.periph_result.periph_name,
1976 periph->periph_name, DEV_IDLEN);
1983 xptperiphlistmatch(struct ccb_dev_match *cdm)
1987 cdm->num_matches = 0;
1990 * At this point in the edt traversal function, we check the bus
1991 * list generation to make sure that no busses have been added or
1992 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1993 * For the peripheral driver list traversal function, however, we
1994 * don't have to worry about new peripheral driver types coming or
1995 * going; they're in a linker set, and therefore can't change
1996 * without a recompile.
1999 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2000 && (cdm->pos.cookie.pdrv != NULL))
2001 ret = xptpdrvtraverse(
2002 (struct periph_driver **)cdm->pos.cookie.pdrv,
2003 xptplistpdrvfunc, cdm);
2005 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2008 * If we get back 0, that means that we had to stop before fully
2009 * traversing the peripheral driver tree. It also means that one of
2010 * the subroutines has set the status field to the proper value. If
2011 * we get back 1, we've fully traversed the EDT and copied out any
2015 cdm->status = CAM_DEV_MATCH_LAST;
2021 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2023 struct cam_eb *bus, *next_bus;
2028 mtx_lock(&xsoftc.xpt_topo_lock);
2029 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2036 * XXX The locking here is obviously very complex. We
2037 * should work to simplify it.
2039 mtx_unlock(&xsoftc.xpt_topo_lock);
2040 CAM_SIM_LOCK(bus->sim);
2041 retval = tr_func(bus, arg);
2042 CAM_SIM_UNLOCK(bus->sim);
2044 mtx_lock(&xsoftc.xpt_topo_lock);
2045 next_bus = TAILQ_NEXT(bus, links);
2046 mtx_unlock(&xsoftc.xpt_topo_lock);
2048 xpt_release_bus(bus);
2052 mtx_lock(&xsoftc.xpt_topo_lock);
2054 mtx_unlock(&xsoftc.xpt_topo_lock);
2060 xpt_sim_opened(struct cam_sim *sim)
2063 struct cam_et *target;
2064 struct cam_ed *device;
2065 struct cam_periph *periph;
2067 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2068 mtx_assert(sim->mtx, MA_OWNED);
2070 mtx_lock(&xsoftc.xpt_topo_lock);
2071 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2072 if (bus->sim != sim)
2075 TAILQ_FOREACH(target, &bus->et_entries, links) {
2076 TAILQ_FOREACH(device, &target->ed_entries, links) {
2077 SLIST_FOREACH(periph, &device->periphs,
2079 if (periph->refcount > 0) {
2080 mtx_unlock(&xsoftc.xpt_topo_lock);
2088 mtx_unlock(&xsoftc.xpt_topo_lock);
2093 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2094 xpt_targetfunc_t *tr_func, void *arg)
2096 struct cam_et *target, *next_target;
2100 for (target = (start_target ? start_target :
2101 TAILQ_FIRST(&bus->et_entries));
2102 target != NULL; target = next_target) {
2106 retval = tr_func(target, arg);
2108 next_target = TAILQ_NEXT(target, links);
2110 xpt_release_target(target);
2120 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2121 xpt_devicefunc_t *tr_func, void *arg)
2123 struct cam_ed *device, *next_device;
2127 for (device = (start_device ? start_device :
2128 TAILQ_FIRST(&target->ed_entries));
2130 device = next_device) {
2133 * Hold a reference so the current device does not go away
2138 retval = tr_func(device, arg);
2141 * Grab our next pointer before we release the current
2144 next_device = TAILQ_NEXT(device, links);
2146 xpt_release_device(device);
2156 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2157 xpt_periphfunc_t *tr_func, void *arg)
2159 struct cam_periph *periph, *next_periph;
2165 for (periph = (start_periph ? start_periph :
2166 SLIST_FIRST(&device->periphs));
2168 periph = next_periph) {
2172 * In this case, we want to show peripherals that have been
2173 * invalidated, but not peripherals that are scheduled to
2174 * be freed. So instead of calling cam_periph_acquire(),
2175 * which will fail if the periph has been invalidated, we
2176 * just check for the free flag here. If it is free, we
2177 * skip to the next periph.
2179 if (periph->flags & CAM_PERIPH_FREE) {
2180 next_periph = SLIST_NEXT(periph, periph_links);
2185 * Acquire a reference to this periph while we call the
2186 * traversal function, so it can't go away.
2192 retval = tr_func(periph, arg);
2195 * We need the lock for list traversal.
2200 * Grab the next peripheral before we release this one, so
2201 * our next pointer is still valid.
2203 next_periph = SLIST_NEXT(periph, periph_links);
2205 cam_periph_release_locked_buses(periph);
2219 xptpdrvtraverse(struct periph_driver **start_pdrv,
2220 xpt_pdrvfunc_t *tr_func, void *arg)
2222 struct periph_driver **pdrv;
2228 * We don't traverse the peripheral driver list like we do the
2229 * other lists, because it is a linker set, and therefore cannot be
2230 * changed during runtime. If the peripheral driver list is ever
2231 * re-done to be something other than a linker set (i.e. it can
2232 * change while the system is running), the list traversal should
2233 * be modified to work like the other traversal functions.
2235 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2236 *pdrv != NULL; pdrv++) {
2237 retval = tr_func(pdrv, arg);
2247 xptpdperiphtraverse(struct periph_driver **pdrv,
2248 struct cam_periph *start_periph,
2249 xpt_periphfunc_t *tr_func, void *arg)
2251 struct cam_periph *periph, *next_periph;
2257 for (periph = (start_periph ? start_periph :
2258 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2259 periph = next_periph) {
2263 * In this case, we want to show peripherals that have been
2264 * invalidated, but not peripherals that are scheduled to
2265 * be freed. So instead of calling cam_periph_acquire(),
2266 * which will fail if the periph has been invalidated, we
2267 * just check for the free flag here. If it is free, we
2268 * skip to the next periph.
2270 if (periph->flags & CAM_PERIPH_FREE) {
2271 next_periph = TAILQ_NEXT(periph, unit_links);
2276 * Acquire a reference to this periph while we call the
2277 * traversal function, so it can't go away.
2282 * XXX KDM we have the toplogy lock here, but in
2283 * xptperiphtraverse(), we drop it before calling the
2284 * traversal function. Which is correct?
2286 retval = tr_func(periph, arg);
2289 * Grab the next peripheral before we release this one, so
2290 * our next pointer is still valid.
2292 next_periph = TAILQ_NEXT(periph, unit_links);
2294 cam_periph_release_locked_buses(periph);
2307 xptdefbusfunc(struct cam_eb *bus, void *arg)
2309 struct xpt_traverse_config *tr_config;
2311 tr_config = (struct xpt_traverse_config *)arg;
2313 if (tr_config->depth == XPT_DEPTH_BUS) {
2314 xpt_busfunc_t *tr_func;
2316 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2318 return(tr_func(bus, tr_config->tr_arg));
2320 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2324 xptdeftargetfunc(struct cam_et *target, void *arg)
2326 struct xpt_traverse_config *tr_config;
2328 tr_config = (struct xpt_traverse_config *)arg;
2330 if (tr_config->depth == XPT_DEPTH_TARGET) {
2331 xpt_targetfunc_t *tr_func;
2333 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2335 return(tr_func(target, tr_config->tr_arg));
2337 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2341 xptdefdevicefunc(struct cam_ed *device, void *arg)
2343 struct xpt_traverse_config *tr_config;
2345 tr_config = (struct xpt_traverse_config *)arg;
2347 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2348 xpt_devicefunc_t *tr_func;
2350 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2352 return(tr_func(device, tr_config->tr_arg));
2354 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2358 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2360 struct xpt_traverse_config *tr_config;
2361 xpt_periphfunc_t *tr_func;
2363 tr_config = (struct xpt_traverse_config *)arg;
2365 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2368 * Unlike the other default functions, we don't check for depth
2369 * here. The peripheral driver level is the last level in the EDT,
2370 * so if we're here, we should execute the function in question.
2372 return(tr_func(periph, tr_config->tr_arg));
2376 * Execute the given function for every bus in the EDT.
2379 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2381 struct xpt_traverse_config tr_config;
2383 tr_config.depth = XPT_DEPTH_BUS;
2384 tr_config.tr_func = tr_func;
2385 tr_config.tr_arg = arg;
2387 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2391 * Execute the given function for every device in the EDT.
2394 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2396 struct xpt_traverse_config tr_config;
2398 tr_config.depth = XPT_DEPTH_DEVICE;
2399 tr_config.tr_func = tr_func;
2400 tr_config.tr_arg = arg;
2402 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2406 xptsetasyncfunc(struct cam_ed *device, void *arg)
2408 struct cam_path path;
2409 struct ccb_getdev cgd;
2410 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2413 * Don't report unconfigured devices (Wildcard devs,
2414 * devices only for target mode, device instances
2415 * that have been invalidated but are waiting for
2416 * their last reference count to be released).
2418 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2421 xpt_compile_path(&path,
2423 device->target->bus->path_id,
2424 device->target->target_id,
2426 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2427 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2428 xpt_action((union ccb *)&cgd);
2429 csa->callback(csa->callback_arg,
2432 xpt_release_path(&path);
2438 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2440 struct cam_path path;
2441 struct ccb_pathinq cpi;
2442 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2444 xpt_compile_path(&path, /*periph*/NULL,
2446 CAM_TARGET_WILDCARD,
2448 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2449 cpi.ccb_h.func_code = XPT_PATH_INQ;
2450 xpt_action((union ccb *)&cpi);
2451 csa->callback(csa->callback_arg,
2454 xpt_release_path(&path);
2460 xpt_action(union ccb *start_ccb)
2463 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2465 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2466 /* Compatibility for RL-unaware code. */
2467 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2468 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2469 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2473 xpt_action_default(union ccb *start_ccb)
2476 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2478 struct cam_path *path;
2480 path = start_ccb->ccb_h.path;
2481 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2483 switch (start_ccb->ccb_h.func_code) {
2486 struct cam_ed *device;
2489 * For the sake of compatibility with SCSI-1
2490 * devices that may not understand the identify
2491 * message, we include lun information in the
2492 * second byte of all commands. SCSI-1 specifies
2493 * that luns are a 3 bit value and reserves only 3
2494 * bits for lun information in the CDB. Later
2495 * revisions of the SCSI spec allow for more than 8
2496 * luns, but have deprecated lun information in the
2497 * CDB. So, if the lun won't fit, we must omit.
2499 * Also be aware that during initial probing for devices,
2500 * the inquiry information is unknown but initialized to 0.
2501 * This means that this code will be exercised while probing
2502 * devices with an ANSI revision greater than 2.
2504 device = path->device;
2505 if (device->protocol_version <= SCSI_REV_2
2506 && start_ccb->ccb_h.target_lun < 8
2507 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2509 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2510 start_ccb->ccb_h.target_lun << 5;
2512 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2513 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2514 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2515 &path->device->inq_data),
2516 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2517 cdb_str, sizeof(cdb_str))));
2521 case XPT_CONT_TARGET_IO:
2522 start_ccb->csio.sense_resid = 0;
2523 start_ccb->csio.resid = 0;
2526 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2527 start_ccb->ataio.resid = 0;
2528 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2529 ata_op_string(&start_ccb->ataio.cmd),
2530 ata_cmd_string(&start_ccb->ataio.cmd,
2531 cdb_str, sizeof(cdb_str))));
2540 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2541 path->device->sim->devq->alloc_openings += frozen;
2543 xpt_run_dev_allocq(path->bus);
2544 if (xpt_schedule_dev_sendq(path->bus, path->device))
2545 xpt_run_dev_sendq(path->bus);
2548 case XPT_CALC_GEOMETRY:
2550 struct cam_sim *sim;
2552 /* Filter out garbage */
2553 if (start_ccb->ccg.block_size == 0
2554 || start_ccb->ccg.volume_size == 0) {
2555 start_ccb->ccg.cylinders = 0;
2556 start_ccb->ccg.heads = 0;
2557 start_ccb->ccg.secs_per_track = 0;
2558 start_ccb->ccb_h.status = CAM_REQ_CMP;
2561 #if defined(PC98) || defined(__sparc64__)
2563 * In a PC-98 system, geometry translation depens on
2564 * the "real" device geometry obtained from mode page 4.
2565 * SCSI geometry translation is performed in the
2566 * initialization routine of the SCSI BIOS and the result
2567 * stored in host memory. If the translation is available
2568 * in host memory, use it. If not, rely on the default
2569 * translation the device driver performs.
2570 * For sparc64, we may need adjust the geometry of large
2571 * disks in order to fit the limitations of the 16-bit
2572 * fields of the VTOC8 disk label.
2574 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2575 start_ccb->ccb_h.status = CAM_REQ_CMP;
2579 sim = path->bus->sim;
2580 (*(sim->sim_action))(sim, start_ccb);
2585 union ccb* abort_ccb;
2587 abort_ccb = start_ccb->cab.abort_ccb;
2588 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2590 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2591 struct cam_ccbq *ccbq;
2592 struct cam_ed *device;
2594 device = abort_ccb->ccb_h.path->device;
2595 ccbq = &device->ccbq;
2596 device->sim->devq->alloc_openings -=
2597 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2598 abort_ccb->ccb_h.status =
2599 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2600 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2601 xpt_done(abort_ccb);
2602 start_ccb->ccb_h.status = CAM_REQ_CMP;
2605 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2606 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2608 * We've caught this ccb en route to
2609 * the SIM. Flag it for abort and the
2610 * SIM will do so just before starting
2611 * real work on the CCB.
2613 abort_ccb->ccb_h.status =
2614 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2615 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2616 start_ccb->ccb_h.status = CAM_REQ_CMP;
2620 if (XPT_FC_IS_QUEUED(abort_ccb)
2621 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2623 * It's already completed but waiting
2624 * for our SWI to get to it.
2626 start_ccb->ccb_h.status = CAM_UA_ABORT;
2630 * If we weren't able to take care of the abort request
2631 * in the XPT, pass the request down to the SIM for processing.
2635 case XPT_ACCEPT_TARGET_IO:
2637 case XPT_IMMED_NOTIFY:
2638 case XPT_NOTIFY_ACK:
2640 case XPT_IMMEDIATE_NOTIFY:
2641 case XPT_NOTIFY_ACKNOWLEDGE:
2642 case XPT_GET_SIM_KNOB:
2643 case XPT_SET_SIM_KNOB:
2645 struct cam_sim *sim;
2647 sim = path->bus->sim;
2648 (*(sim->sim_action))(sim, start_ccb);
2653 struct cam_sim *sim;
2655 sim = path->bus->sim;
2656 (*(sim->sim_action))(sim, start_ccb);
2659 case XPT_PATH_STATS:
2660 start_ccb->cpis.last_reset = path->bus->last_reset;
2661 start_ccb->ccb_h.status = CAM_REQ_CMP;
2668 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2669 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2671 struct ccb_getdev *cgd;
2673 cgd = &start_ccb->cgd;
2674 cgd->protocol = dev->protocol;
2675 cgd->inq_data = dev->inq_data;
2676 cgd->ident_data = dev->ident_data;
2677 cgd->inq_flags = dev->inq_flags;
2678 cgd->ccb_h.status = CAM_REQ_CMP;
2679 cgd->serial_num_len = dev->serial_num_len;
2680 if ((dev->serial_num_len > 0)
2681 && (dev->serial_num != NULL))
2682 bcopy(dev->serial_num, cgd->serial_num,
2683 dev->serial_num_len);
2687 case XPT_GDEV_STATS:
2692 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2693 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2695 struct ccb_getdevstats *cgds;
2699 cgds = &start_ccb->cgds;
2702 cgds->dev_openings = dev->ccbq.dev_openings;
2703 cgds->dev_active = dev->ccbq.dev_active;
2704 cgds->devq_openings = dev->ccbq.devq_openings;
2705 cgds->devq_queued = dev->ccbq.queue.entries;
2706 cgds->held = dev->ccbq.held;
2707 cgds->last_reset = tar->last_reset;
2708 cgds->maxtags = dev->maxtags;
2709 cgds->mintags = dev->mintags;
2710 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2711 cgds->last_reset = bus->last_reset;
2712 cgds->ccb_h.status = CAM_REQ_CMP;
2718 struct cam_periph *nperiph;
2719 struct periph_list *periph_head;
2720 struct ccb_getdevlist *cgdl;
2722 struct cam_ed *device;
2729 * Don't want anyone mucking with our data.
2731 device = path->device;
2732 periph_head = &device->periphs;
2733 cgdl = &start_ccb->cgdl;
2736 * Check and see if the list has changed since the user
2737 * last requested a list member. If so, tell them that the
2738 * list has changed, and therefore they need to start over
2739 * from the beginning.
2741 if ((cgdl->index != 0) &&
2742 (cgdl->generation != device->generation)) {
2743 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2748 * Traverse the list of peripherals and attempt to find
2749 * the requested peripheral.
2751 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2752 (nperiph != NULL) && (i <= cgdl->index);
2753 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2754 if (i == cgdl->index) {
2755 strncpy(cgdl->periph_name,
2756 nperiph->periph_name,
2758 cgdl->unit_number = nperiph->unit_number;
2763 cgdl->status = CAM_GDEVLIST_ERROR;
2767 if (nperiph == NULL)
2768 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2770 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2773 cgdl->generation = device->generation;
2775 cgdl->ccb_h.status = CAM_REQ_CMP;
2780 dev_pos_type position_type;
2781 struct ccb_dev_match *cdm;
2783 cdm = &start_ccb->cdm;
2786 * There are two ways of getting at information in the EDT.
2787 * The first way is via the primary EDT tree. It starts
2788 * with a list of busses, then a list of targets on a bus,
2789 * then devices/luns on a target, and then peripherals on a
2790 * device/lun. The "other" way is by the peripheral driver
2791 * lists. The peripheral driver lists are organized by
2792 * peripheral driver. (obviously) So it makes sense to
2793 * use the peripheral driver list if the user is looking
2794 * for something like "da1", or all "da" devices. If the
2795 * user is looking for something on a particular bus/target
2796 * or lun, it's generally better to go through the EDT tree.
2799 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2800 position_type = cdm->pos.position_type;
2804 position_type = CAM_DEV_POS_NONE;
2806 for (i = 0; i < cdm->num_patterns; i++) {
2807 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2808 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2809 position_type = CAM_DEV_POS_EDT;
2814 if (cdm->num_patterns == 0)
2815 position_type = CAM_DEV_POS_EDT;
2816 else if (position_type == CAM_DEV_POS_NONE)
2817 position_type = CAM_DEV_POS_PDRV;
2820 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2821 case CAM_DEV_POS_EDT:
2824 case CAM_DEV_POS_PDRV:
2825 xptperiphlistmatch(cdm);
2828 cdm->status = CAM_DEV_MATCH_ERROR;
2832 if (cdm->status == CAM_DEV_MATCH_ERROR)
2833 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2835 start_ccb->ccb_h.status = CAM_REQ_CMP;
2841 struct ccb_setasync *csa;
2842 struct async_node *cur_entry;
2843 struct async_list *async_head;
2846 csa = &start_ccb->csa;
2847 added = csa->event_enable;
2848 async_head = &path->device->asyncs;
2851 * If there is already an entry for us, simply
2854 cur_entry = SLIST_FIRST(async_head);
2855 while (cur_entry != NULL) {
2856 if ((cur_entry->callback_arg == csa->callback_arg)
2857 && (cur_entry->callback == csa->callback))
2859 cur_entry = SLIST_NEXT(cur_entry, links);
2862 if (cur_entry != NULL) {
2864 * If the request has no flags set,
2867 added &= ~cur_entry->event_enable;
2868 if (csa->event_enable == 0) {
2869 SLIST_REMOVE(async_head, cur_entry,
2871 xpt_release_device(path->device);
2872 free(cur_entry, M_CAMXPT);
2874 cur_entry->event_enable = csa->event_enable;
2876 csa->event_enable = added;
2878 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2880 if (cur_entry == NULL) {
2881 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2884 cur_entry->event_enable = csa->event_enable;
2885 cur_entry->callback_arg = csa->callback_arg;
2886 cur_entry->callback = csa->callback;
2887 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2888 xpt_acquire_device(path->device);
2890 start_ccb->ccb_h.status = CAM_REQ_CMP;
2895 struct ccb_relsim *crs;
2898 crs = &start_ccb->crs;
2902 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2906 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2908 /* Don't ever go below one opening */
2909 if (crs->openings > 0) {
2910 xpt_dev_ccbq_resize(path, crs->openings);
2913 "number of openings is now %d\n",
2919 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2921 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2924 * Just extend the old timeout and decrement
2925 * the freeze count so that a single timeout
2926 * is sufficient for releasing the queue.
2928 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2929 callout_stop(&dev->callout);
2932 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2935 callout_reset(&dev->callout,
2936 (crs->release_timeout * hz) / 1000,
2937 xpt_release_devq_timeout, dev);
2939 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2943 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2945 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2947 * Decrement the freeze count so that a single
2948 * completion is still sufficient to unfreeze
2951 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2954 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2955 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2959 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2961 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2962 || (dev->ccbq.dev_active == 0)) {
2964 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2967 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2968 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2972 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2973 xpt_release_devq_rl(path, /*runlevel*/
2974 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2975 crs->release_timeout : 0,
2976 /*count*/1, /*run_queue*/TRUE);
2978 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2979 start_ccb->ccb_h.status = CAM_REQ_CMP;
2984 #ifdef CAM_DEBUG_DELAY
2985 cam_debug_delay = CAM_DEBUG_DELAY;
2987 cam_dflags = start_ccb->cdbg.flags;
2988 if (cam_dpath != NULL) {
2989 xpt_free_path(cam_dpath);
2993 if (cam_dflags != CAM_DEBUG_NONE) {
2994 if (xpt_create_path(&cam_dpath, xpt_periph,
2995 start_ccb->ccb_h.path_id,
2996 start_ccb->ccb_h.target_id,
2997 start_ccb->ccb_h.target_lun) !=
2999 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3000 cam_dflags = CAM_DEBUG_NONE;
3002 start_ccb->ccb_h.status = CAM_REQ_CMP;
3003 xpt_print(cam_dpath, "debugging flags now %x\n",
3008 start_ccb->ccb_h.status = CAM_REQ_CMP;
3010 #else /* !CAMDEBUG */
3011 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3012 #endif /* CAMDEBUG */
3015 case XPT_FREEZE_QUEUE:
3017 struct ccb_relsim *crs = &start_ccb->crs;
3019 xpt_freeze_devq_rl(path, /*runlevel*/
3020 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3021 crs->release_timeout : 0, /*count*/1);
3022 start_ccb->ccb_h.status = CAM_REQ_CMP;
3026 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3027 xpt_freeze_devq(path, 1);
3028 start_ccb->ccb_h.status = CAM_REQ_CMP;
3035 printf("%s: CCB type %#x not supported\n", __func__,
3036 start_ccb->ccb_h.func_code);
3037 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3038 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3039 xpt_done(start_ccb);
3046 xpt_polled_action(union ccb *start_ccb)
3049 struct cam_sim *sim;
3050 struct cam_devq *devq;
3054 timeout = start_ccb->ccb_h.timeout * 10;
3055 sim = start_ccb->ccb_h.path->bus->sim;
3057 dev = start_ccb->ccb_h.path->device;
3059 mtx_assert(sim->mtx, MA_OWNED);
3061 /* Don't use ISR for this SIM while polling. */
3062 sim->flags |= CAM_SIM_POLLED;
3065 * Steal an opening so that no other queued requests
3066 * can get it before us while we simulate interrupts.
3068 dev->ccbq.devq_openings--;
3069 dev->ccbq.dev_openings--;
3071 while(((devq != NULL && devq->send_openings <= 0) ||
3072 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3074 (*(sim->sim_poll))(sim);
3075 camisr_runqueue(&sim->sim_doneq);
3078 dev->ccbq.devq_openings++;
3079 dev->ccbq.dev_openings++;
3082 xpt_action(start_ccb);
3083 while(--timeout > 0) {
3084 (*(sim->sim_poll))(sim);
3085 camisr_runqueue(&sim->sim_doneq);
3086 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3093 * XXX Is it worth adding a sim_timeout entry
3094 * point so we can attempt recovery? If
3095 * this is only used for dumps, I don't think
3098 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3101 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3104 /* We will use CAM ISR for this SIM again. */
3105 sim->flags &= ~CAM_SIM_POLLED;
3109 * Schedule a peripheral driver to receive a ccb when it's
3110 * target device has space for more transactions.
3113 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3115 struct cam_ed *device;
3118 mtx_assert(perph->sim->mtx, MA_OWNED);
3120 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3121 device = perph->path->device;
3122 if (periph_is_queued(perph)) {
3123 /* Simply reorder based on new priority */
3124 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3125 (" change priority to %d\n", new_priority));
3126 if (new_priority < perph->pinfo.priority) {
3127 camq_change_priority(&device->drvq,
3130 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3133 /* New entry on the queue */
3134 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3135 (" added periph to queue\n"));
3136 perph->pinfo.priority = new_priority;
3137 perph->pinfo.generation = ++device->drvq.generation;
3138 camq_insert(&device->drvq, &perph->pinfo);
3139 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3142 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3143 (" calling xpt_run_devq\n"));
3144 xpt_run_dev_allocq(perph->path->bus);
3150 * Schedule a device to run on a given queue.
3151 * If the device was inserted as a new entry on the queue,
3152 * return 1 meaning the device queue should be run. If we
3153 * were already queued, implying someone else has already
3154 * started the queue, return 0 so the caller doesn't attempt
3158 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3159 u_int32_t new_priority)
3162 u_int32_t old_priority;
3164 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3166 old_priority = pinfo->priority;
3169 * Are we already queued?
3171 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3172 /* Simply reorder based on new priority */
3173 if (new_priority < old_priority) {
3174 camq_change_priority(queue, pinfo->index,
3176 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3177 ("changed priority to %d\n",
3183 /* New entry on the queue */
3184 if (new_priority < old_priority)
3185 pinfo->priority = new_priority;
3187 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3188 ("Inserting onto queue\n"));
3189 pinfo->generation = ++queue->generation;
3190 camq_insert(queue, pinfo);
3197 xpt_run_dev_allocq(struct cam_eb *bus)
3199 struct cam_devq *devq;
3201 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3202 devq = bus->sim->devq;
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3205 (" qfrozen_cnt == 0x%x, entries == %d, "
3206 "openings == %d, active == %d\n",
3207 devq->alloc_queue.qfrozen_cnt[0],
3208 devq->alloc_queue.entries,
3209 devq->alloc_openings,
3210 devq->alloc_active));
3212 devq->alloc_queue.qfrozen_cnt[0]++;
3213 while ((devq->alloc_queue.entries > 0)
3214 && (devq->alloc_openings > 0)
3215 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3216 struct cam_ed_qinfo *qinfo;
3217 struct cam_ed *device;
3218 union ccb *work_ccb;
3219 struct cam_periph *drv;
3222 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3224 device = qinfo->device;
3225 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3226 ("running device %p\n", device));
3228 drvq = &device->drvq;
3231 if (drvq->entries <= 0) {
3232 panic("xpt_run_dev_allocq: "
3233 "Device on queue without any work to do");
3236 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3237 devq->alloc_openings--;
3238 devq->alloc_active++;
3239 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3240 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3241 drv->pinfo.priority);
3242 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3243 ("calling periph start\n"));
3244 drv->periph_start(drv, work_ccb);
3247 * Malloc failure in alloc_ccb
3250 * XXX add us to a list to be run from free_ccb
3251 * if we don't have any ccbs active on this
3252 * device queue otherwise we may never get run
3258 /* We may have more work. Attempt to reschedule. */
3259 xpt_schedule_dev_allocq(bus, device);
3261 devq->alloc_queue.qfrozen_cnt[0]--;
3265 xpt_run_dev_sendq(struct cam_eb *bus)
3267 struct cam_devq *devq;
3269 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3271 devq = bus->sim->devq;
3273 devq->send_queue.qfrozen_cnt[0]++;
3274 while ((devq->send_queue.entries > 0)
3275 && (devq->send_openings > 0)
3276 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3277 struct cam_ed_qinfo *qinfo;
3278 struct cam_ed *device;
3279 union ccb *work_ccb;
3280 struct cam_sim *sim;
3282 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3284 device = qinfo->device;
3285 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3286 ("running device %p\n", device));
3288 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3289 if (work_ccb == NULL) {
3290 printf("device on run queue with no ccbs???\n");
3294 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3296 mtx_lock(&xsoftc.xpt_lock);
3297 if (xsoftc.num_highpower <= 0) {
3299 * We got a high power command, but we
3300 * don't have any available slots. Freeze
3301 * the device queue until we have a slot
3304 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3305 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3309 mtx_unlock(&xsoftc.xpt_lock);
3313 * Consume a high power slot while
3316 xsoftc.num_highpower--;
3318 mtx_unlock(&xsoftc.xpt_lock);
3320 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3321 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3323 devq->send_openings--;
3324 devq->send_active++;
3326 xpt_schedule_dev_sendq(bus, device);
3328 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3330 * The client wants to freeze the queue
3331 * after this CCB is sent.
3333 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3336 /* In Target mode, the peripheral driver knows best... */
3337 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3338 if ((device->inq_flags & SID_CmdQue) != 0
3339 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3340 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3343 * Clear this in case of a retried CCB that
3344 * failed due to a rejected tag.
3346 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3350 * Device queues can be shared among multiple sim instances
3351 * that reside on different busses. Use the SIM in the queue
3352 * CCB's path, rather than the one in the bus that was passed
3353 * into this function.
3355 sim = work_ccb->ccb_h.path->bus->sim;
3356 (*(sim->sim_action))(sim, work_ccb);
3358 devq->send_queue.qfrozen_cnt[0]--;
3362 * This function merges stuff from the slave ccb into the master ccb, while
3363 * keeping important fields in the master ccb constant.
3366 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3370 * Pull fields that are valid for peripheral drivers to set
3371 * into the master CCB along with the CCB "payload".
3373 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3374 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3375 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3376 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3377 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3378 sizeof(union ccb) - sizeof(struct ccb_hdr));
3382 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3385 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3386 ccb_h->pinfo.priority = priority;
3388 ccb_h->path_id = path->bus->path_id;
3390 ccb_h->target_id = path->target->target_id;
3392 ccb_h->target_id = CAM_TARGET_WILDCARD;
3394 ccb_h->target_lun = path->device->lun_id;
3395 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3397 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3399 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3403 /* Path manipulation functions */
3405 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3406 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3408 struct cam_path *path;
3411 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3414 status = CAM_RESRC_UNAVAIL;
3417 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3418 if (status != CAM_REQ_CMP) {
3419 free(path, M_CAMXPT);
3422 *new_path_ptr = path;
3427 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3428 struct cam_periph *periph, path_id_t path_id,
3429 target_id_t target_id, lun_id_t lun_id)
3431 struct cam_path *path;
3432 struct cam_eb *bus = NULL;
3434 int need_unlock = 0;
3436 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3438 if (path_id != CAM_BUS_WILDCARD) {
3439 bus = xpt_find_bus(path_id);
3442 CAM_SIM_LOCK(bus->sim);
3445 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3447 CAM_SIM_UNLOCK(bus->sim);
3448 xpt_release_bus(bus);
3450 if (status != CAM_REQ_CMP) {
3451 free(path, M_CAMXPT);
3454 *new_path_ptr = path;
3459 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3460 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3463 struct cam_et *target;
3464 struct cam_ed *device;
3467 status = CAM_REQ_CMP; /* Completed without error */
3468 target = NULL; /* Wildcarded */
3469 device = NULL; /* Wildcarded */
3472 * We will potentially modify the EDT, so block interrupts
3473 * that may attempt to create cam paths.
3475 bus = xpt_find_bus(path_id);
3477 status = CAM_PATH_INVALID;
3479 target = xpt_find_target(bus, target_id);
3480 if (target == NULL) {
3482 struct cam_et *new_target;
3484 new_target = xpt_alloc_target(bus, target_id);
3485 if (new_target == NULL) {
3486 status = CAM_RESRC_UNAVAIL;
3488 target = new_target;
3491 if (target != NULL) {
3492 device = xpt_find_device(target, lun_id);
3493 if (device == NULL) {
3495 struct cam_ed *new_device;
3498 (*(bus->xport->alloc_device))(bus,
3501 if (new_device == NULL) {
3502 status = CAM_RESRC_UNAVAIL;
3504 device = new_device;
3511 * Only touch the user's data if we are successful.
3513 if (status == CAM_REQ_CMP) {
3514 new_path->periph = perph;
3515 new_path->bus = bus;
3516 new_path->target = target;
3517 new_path->device = device;
3518 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3521 xpt_release_device(device);
3523 xpt_release_target(target);
3525 xpt_release_bus(bus);
3531 xpt_release_path(struct cam_path *path)
3533 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3534 if (path->device != NULL) {
3535 xpt_release_device(path->device);
3536 path->device = NULL;
3538 if (path->target != NULL) {
3539 xpt_release_target(path->target);
3540 path->target = NULL;
3542 if (path->bus != NULL) {
3543 xpt_release_bus(path->bus);
3549 xpt_free_path(struct cam_path *path)
3552 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3553 xpt_release_path(path);
3554 free(path, M_CAMXPT);
3558 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3559 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3562 mtx_lock(&xsoftc.xpt_topo_lock);
3565 *bus_ref = path->bus->refcount;
3569 mtx_unlock(&xsoftc.xpt_topo_lock);
3572 *periph_ref = path->periph->refcount;
3578 *target_ref = path->target->refcount;
3584 *device_ref = path->device->refcount;
3591 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3592 * in path1, 2 for match with wildcards in path2.
3595 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3599 if (path1->bus != path2->bus) {
3600 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3602 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3607 if (path1->target != path2->target) {
3608 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3611 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3616 if (path1->device != path2->device) {
3617 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3620 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3629 xpt_print_path(struct cam_path *path)
3633 printf("(nopath): ");
3635 if (path->periph != NULL)
3636 printf("(%s%d:", path->periph->periph_name,
3637 path->periph->unit_number);
3639 printf("(noperiph:");
3641 if (path->bus != NULL)
3642 printf("%s%d:%d:", path->bus->sim->sim_name,
3643 path->bus->sim->unit_number,
3644 path->bus->sim->bus_id);
3648 if (path->target != NULL)
3649 printf("%d:", path->target->target_id);
3653 if (path->device != NULL)
3654 printf("%d): ", path->device->lun_id);
3661 xpt_print(struct cam_path *path, const char *fmt, ...)
3664 xpt_print_path(path);
3671 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3676 if (path != NULL && path->bus != NULL)
3677 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3680 sbuf_new(&sb, str, str_len, 0);
3683 sbuf_printf(&sb, "(nopath): ");
3685 if (path->periph != NULL)
3686 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3687 path->periph->unit_number);
3689 sbuf_printf(&sb, "(noperiph:");
3691 if (path->bus != NULL)
3692 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3693 path->bus->sim->unit_number,
3694 path->bus->sim->bus_id);
3696 sbuf_printf(&sb, "nobus:");
3698 if (path->target != NULL)
3699 sbuf_printf(&sb, "%d:", path->target->target_id);
3701 sbuf_printf(&sb, "X:");
3703 if (path->device != NULL)
3704 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3706 sbuf_printf(&sb, "X): ");
3710 return(sbuf_len(&sb));
3714 xpt_path_path_id(struct cam_path *path)
3716 return(path->bus->path_id);
3720 xpt_path_target_id(struct cam_path *path)
3722 if (path->target != NULL)
3723 return (path->target->target_id);
3725 return (CAM_TARGET_WILDCARD);
3729 xpt_path_lun_id(struct cam_path *path)
3731 if (path->device != NULL)
3732 return (path->device->lun_id);
3734 return (CAM_LUN_WILDCARD);
3738 xpt_path_sim(struct cam_path *path)
3741 return (path->bus->sim);
3745 xpt_path_periph(struct cam_path *path)
3747 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3749 return (path->periph);
3753 xpt_path_legacy_ata_id(struct cam_path *path)
3758 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3759 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3760 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3761 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3764 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3765 path->bus->sim->unit_number < 2) {
3766 bus_id = path->bus->sim->unit_number;
3770 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3771 if (bus == path->bus)
3773 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3774 bus->sim->unit_number >= 2) ||
3775 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3776 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3777 strcmp(bus->sim->sim_name, "siisch") == 0)
3782 if (path->target != NULL) {
3783 if (path->target->target_id < 2)
3784 return (bus_id * 2 + path->target->target_id);
3788 return (bus_id * 2);
3792 * Release a CAM control block for the caller. Remit the cost of the structure
3793 * to the device referenced by the path. If the this device had no 'credits'
3794 * and peripheral drivers have registered async callbacks for this notification
3798 xpt_release_ccb(union ccb *free_ccb)
3800 struct cam_path *path;
3801 struct cam_ed *device;
3803 struct cam_sim *sim;
3805 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3806 path = free_ccb->ccb_h.path;
3807 device = path->device;
3811 mtx_assert(sim->mtx, MA_OWNED);
3813 cam_ccbq_release_opening(&device->ccbq);
3814 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3815 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3816 cam_ccbq_resize(&device->ccbq,
3817 device->ccbq.dev_openings + device->ccbq.dev_active);
3819 if (sim->ccb_count > sim->max_ccbs) {
3820 xpt_free_ccb(free_ccb);
3823 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3826 if (sim->devq == NULL) {
3829 sim->devq->alloc_openings++;
3830 sim->devq->alloc_active--;
3831 if (device_is_alloc_queued(device) == 0)
3832 xpt_schedule_dev_allocq(bus, device);
3833 xpt_run_dev_allocq(bus);
3836 /* Functions accessed by SIM drivers */
3838 static struct xpt_xport xport_default = {
3839 .alloc_device = xpt_alloc_device_default,
3840 .action = xpt_action_default,
3841 .async = xpt_dev_async_default,
3845 * A sim structure, listing the SIM entry points and instance
3846 * identification info is passed to xpt_bus_register to hook the SIM
3847 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3848 * for this new bus and places it in the array of busses and assigns
3849 * it a path_id. The path_id may be influenced by "hard wiring"
3850 * information specified by the user. Once interrupt services are
3851 * available, the bus will be probed.
3854 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3856 struct cam_eb *new_bus;
3857 struct cam_eb *old_bus;
3858 struct ccb_pathinq cpi;
3859 struct cam_path *path;
3862 mtx_assert(sim->mtx, MA_OWNED);
3865 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3866 M_CAMXPT, M_NOWAIT);
3867 if (new_bus == NULL) {
3868 /* Couldn't satisfy request */
3869 return (CAM_RESRC_UNAVAIL);
3871 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3873 free(new_bus, M_CAMXPT);
3874 return (CAM_RESRC_UNAVAIL);
3877 if (strcmp(sim->sim_name, "xpt") != 0) {
3879 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3882 TAILQ_INIT(&new_bus->et_entries);
3883 new_bus->path_id = sim->path_id;
3886 timevalclear(&new_bus->last_reset);
3888 new_bus->refcount = 1; /* Held until a bus_deregister event */
3889 new_bus->generation = 0;
3891 mtx_lock(&xsoftc.xpt_topo_lock);
3892 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3893 while (old_bus != NULL
3894 && old_bus->path_id < new_bus->path_id)
3895 old_bus = TAILQ_NEXT(old_bus, links);
3896 if (old_bus != NULL)
3897 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3899 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3900 xsoftc.bus_generation++;
3901 mtx_unlock(&xsoftc.xpt_topo_lock);
3904 * Set a default transport so that a PATH_INQ can be issued to
3905 * the SIM. This will then allow for probing and attaching of
3906 * a more appropriate transport.
3908 new_bus->xport = &xport_default;
3910 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3911 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3912 if (status != CAM_REQ_CMP)
3913 printf("xpt_compile_path returned %d\n", status);
3915 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3916 cpi.ccb_h.func_code = XPT_PATH_INQ;
3917 xpt_action((union ccb *)&cpi);
3919 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3920 switch (cpi.transport) {
3927 new_bus->xport = scsi_get_xport();
3931 new_bus->xport = ata_get_xport();
3934 new_bus->xport = &xport_default;
3939 /* Notify interested parties */
3940 if (sim->path_id != CAM_XPT_PATH_ID) {
3941 union ccb *scan_ccb;
3943 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3944 /* Initiate bus rescan. */
3945 scan_ccb = xpt_alloc_ccb_nowait();
3946 scan_ccb->ccb_h.path = path;
3947 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3948 scan_ccb->crcn.flags = 0;
3949 xpt_rescan(scan_ccb);
3951 xpt_free_path(path);
3952 return (CAM_SUCCESS);
3956 xpt_bus_deregister(path_id_t pathid)
3958 struct cam_path bus_path;
3961 status = xpt_compile_path(&bus_path, NULL, pathid,
3962 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3963 if (status != CAM_REQ_CMP)
3966 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3967 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3969 /* Release the reference count held while registered. */
3970 xpt_release_bus(bus_path.bus);
3971 xpt_release_path(&bus_path);
3973 return (CAM_REQ_CMP);
3977 xptnextfreepathid(void)
3984 mtx_lock(&xsoftc.xpt_topo_lock);
3985 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3987 /* Find an unoccupied pathid */
3988 while (bus != NULL && bus->path_id <= pathid) {
3989 if (bus->path_id == pathid)
3991 bus = TAILQ_NEXT(bus, links);
3993 mtx_unlock(&xsoftc.xpt_topo_lock);
3996 * Ensure that this pathid is not reserved for
3997 * a bus that may be registered in the future.
3999 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4001 /* Start the search over */
4002 mtx_lock(&xsoftc.xpt_topo_lock);
4009 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4016 pathid = CAM_XPT_PATH_ID;
4017 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4019 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4020 if (strcmp(dname, "scbus")) {
4021 /* Avoid a bit of foot shooting. */
4024 if (dunit < 0) /* unwired?! */
4026 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4027 if (sim_bus == val) {
4031 } else if (sim_bus == 0) {
4032 /* Unspecified matches bus 0 */
4036 printf("Ambiguous scbus configuration for %s%d "
4037 "bus %d, cannot wire down. The kernel "
4038 "config entry for scbus%d should "
4039 "specify a controller bus.\n"
4040 "Scbus will be assigned dynamically.\n",
4041 sim_name, sim_unit, sim_bus, dunit);
4046 if (pathid == CAM_XPT_PATH_ID)
4047 pathid = xptnextfreepathid();
4052 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4055 struct cam_et *target, *next_target;
4056 struct cam_ed *device, *next_device;
4058 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4060 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4063 * Most async events come from a CAM interrupt context. In
4064 * a few cases, the error recovery code at the peripheral layer,
4065 * which may run from our SWI or a process context, may signal
4066 * deferred events with a call to xpt_async.
4071 if (async_code == AC_BUS_RESET) {
4072 /* Update our notion of when the last reset occurred */
4073 microtime(&bus->last_reset);
4076 for (target = TAILQ_FIRST(&bus->et_entries);
4078 target = next_target) {
4080 next_target = TAILQ_NEXT(target, links);
4082 if (path->target != target
4083 && path->target->target_id != CAM_TARGET_WILDCARD
4084 && target->target_id != CAM_TARGET_WILDCARD)
4087 if (async_code == AC_SENT_BDR) {
4088 /* Update our notion of when the last reset occurred */
4089 microtime(&path->target->last_reset);
4092 for (device = TAILQ_FIRST(&target->ed_entries);
4094 device = next_device) {
4096 next_device = TAILQ_NEXT(device, links);
4098 if (path->device != device
4099 && path->device->lun_id != CAM_LUN_WILDCARD
4100 && device->lun_id != CAM_LUN_WILDCARD)
4103 * The async callback could free the device.
4104 * If it is a broadcast async, it doesn't hold
4105 * device reference, so take our own reference.
4107 xpt_acquire_device(device);
4108 (*(bus->xport->async))(async_code, bus,
4112 xpt_async_bcast(&device->asyncs, async_code,
4114 xpt_release_device(device);
4119 * If this wasn't a fully wildcarded async, tell all
4120 * clients that want all async events.
4122 if (bus != xpt_periph->path->bus)
4123 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4128 xpt_async_bcast(struct async_list *async_head,
4129 u_int32_t async_code,
4130 struct cam_path *path, void *async_arg)
4132 struct async_node *cur_entry;
4134 cur_entry = SLIST_FIRST(async_head);
4135 while (cur_entry != NULL) {
4136 struct async_node *next_entry;
4138 * Grab the next list entry before we call the current
4139 * entry's callback. This is because the callback function
4140 * can delete its async callback entry.
4142 next_entry = SLIST_NEXT(cur_entry, links);
4143 if ((cur_entry->event_enable & async_code) != 0)
4144 cur_entry->callback(cur_entry->callback_arg,
4147 cur_entry = next_entry;
4152 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4153 struct cam_et *target, struct cam_ed *device,
4156 printf("%s called\n", __func__);
4160 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4162 struct cam_ed *dev = path->device;
4164 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4165 dev->sim->devq->alloc_openings +=
4166 cam_ccbq_freeze(&dev->ccbq, rl, count);
4167 /* Remove frozen device from allocq. */
4168 if (device_is_alloc_queued(dev) &&
4169 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4170 CAMQ_GET_PRIO(&dev->drvq)))) {
4171 camq_remove(&dev->sim->devq->alloc_queue,
4172 dev->alloc_ccb_entry.pinfo.index);
4174 /* Remove frozen device from sendq. */
4175 if (device_is_send_queued(dev) &&
4176 cam_ccbq_frozen_top(&dev->ccbq)) {
4177 camq_remove(&dev->sim->devq->send_queue,
4178 dev->send_ccb_entry.pinfo.index);
4180 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4184 xpt_freeze_devq(struct cam_path *path, u_int count)
4187 return (xpt_freeze_devq_rl(path, 0, count));
4191 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4194 mtx_assert(sim->mtx, MA_OWNED);
4195 sim->devq->send_queue.qfrozen_cnt[0] += count;
4196 return (sim->devq->send_queue.qfrozen_cnt[0]);
4200 xpt_release_devq_timeout(void *arg)
4202 struct cam_ed *device;
4204 device = (struct cam_ed *)arg;
4206 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4210 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4212 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4214 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4218 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4220 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4222 xpt_release_devq_device(path->device, rl, count, run_queue);
4226 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4229 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4231 printf("xpt_release_devq(%d): requested %u > present %u\n",
4232 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4234 count = dev->ccbq.queue.qfrozen_cnt[rl];
4236 dev->sim->devq->alloc_openings -=
4237 cam_ccbq_release(&dev->ccbq, rl, count);
4238 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4239 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4240 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4241 xpt_run_dev_allocq(dev->target->bus);
4243 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4245 * No longer need to wait for a successful
4246 * command completion.
4248 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4250 * Remove any timeouts that might be scheduled
4251 * to release this queue.
4253 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4254 callout_stop(&dev->callout);
4255 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4260 * Now that we are unfrozen schedule the
4261 * device so any pending transactions are
4264 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4265 xpt_run_dev_sendq(dev->target->bus);
4270 xpt_release_simq(struct cam_sim *sim, int run_queue)
4274 mtx_assert(sim->mtx, MA_OWNED);
4275 sendq = &(sim->devq->send_queue);
4276 if (sendq->qfrozen_cnt[0] <= 0) {
4278 printf("xpt_release_simq: requested 1 > present %u\n",
4279 sendq->qfrozen_cnt[0]);
4282 sendq->qfrozen_cnt[0]--;
4283 if (sendq->qfrozen_cnt[0] == 0) {
4285 * If there is a timeout scheduled to release this
4286 * sim queue, remove it. The queue frozen count is
4289 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4290 callout_stop(&sim->callout);
4291 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4297 * Now that we are unfrozen run the send queue.
4299 bus = xpt_find_bus(sim->path_id);
4300 xpt_run_dev_sendq(bus);
4301 xpt_release_bus(bus);
4307 * XXX Appears to be unused.
4310 xpt_release_simq_timeout(void *arg)
4312 struct cam_sim *sim;
4314 sim = (struct cam_sim *)arg;
4315 xpt_release_simq(sim, /* run_queue */ TRUE);
4319 xpt_done(union ccb *done_ccb)
4321 struct cam_sim *sim;
4324 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4325 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4327 * Queue up the request for handling by our SWI handler
4328 * any of the "non-immediate" type of ccbs.
4330 sim = done_ccb->ccb_h.path->bus->sim;
4331 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4333 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4334 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4335 CAM_SIM_BATCH)) == 0) {
4336 mtx_lock(&cam_simq_lock);
4337 first = TAILQ_EMPTY(&cam_simq);
4338 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4339 mtx_unlock(&cam_simq_lock);
4340 sim->flags |= CAM_SIM_ON_DONEQ;
4342 swi_sched(cambio_ih, 0);
4348 xpt_batch_start(struct cam_sim *sim)
4351 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4352 sim->flags |= CAM_SIM_BATCH;
4356 xpt_batch_done(struct cam_sim *sim)
4359 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4360 sim->flags &= ~CAM_SIM_BATCH;
4361 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4362 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4363 camisr_runqueue(&sim->sim_doneq);
4371 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4376 xpt_alloc_ccb_nowait()
4380 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4385 xpt_free_ccb(union ccb *free_ccb)
4387 free(free_ccb, M_CAMXPT);
4392 /* Private XPT functions */
4395 * Get a CAM control block for the caller. Charge the structure to the device
4396 * referenced by the path. If the this device has no 'credits' then the
4397 * device already has the maximum number of outstanding operations under way
4398 * and we return NULL. If we don't have sufficient resources to allocate more
4399 * ccbs, we also return NULL.
4402 xpt_get_ccb(struct cam_ed *device)
4405 struct cam_sim *sim;
4408 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4409 new_ccb = xpt_alloc_ccb_nowait();
4410 if (new_ccb == NULL) {
4413 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4414 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4415 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4419 cam_ccbq_take_opening(&device->ccbq);
4420 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4425 xpt_release_bus(struct cam_eb *bus)
4428 mtx_lock(&xsoftc.xpt_topo_lock);
4429 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4430 if ((--bus->refcount == 0)
4431 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4432 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4433 xsoftc.bus_generation++;
4434 mtx_unlock(&xsoftc.xpt_topo_lock);
4435 cam_sim_release(bus->sim);
4436 free(bus, M_CAMXPT);
4438 mtx_unlock(&xsoftc.xpt_topo_lock);
4441 static struct cam_et *
4442 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4444 struct cam_et *target;
4446 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4448 if (target != NULL) {
4449 struct cam_et *cur_target;
4451 TAILQ_INIT(&target->ed_entries);
4453 target->target_id = target_id;
4454 target->refcount = 1;
4455 target->generation = 0;
4456 target->luns = NULL;
4457 timevalclear(&target->last_reset);
4459 * Hold a reference to our parent bus so it
4460 * will not go away before we do.
4462 mtx_lock(&xsoftc.xpt_topo_lock);
4464 mtx_unlock(&xsoftc.xpt_topo_lock);
4466 /* Insertion sort into our bus's target list */
4467 cur_target = TAILQ_FIRST(&bus->et_entries);
4468 while (cur_target != NULL && cur_target->target_id < target_id)
4469 cur_target = TAILQ_NEXT(cur_target, links);
4471 if (cur_target != NULL) {
4472 TAILQ_INSERT_BEFORE(cur_target, target, links);
4474 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4482 xpt_release_target(struct cam_et *target)
4485 if (target->refcount == 1) {
4486 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4487 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4488 target->bus->generation++;
4489 xpt_release_bus(target->bus);
4491 free(target->luns, M_CAMXPT);
4492 free(target, M_CAMXPT);
4498 static struct cam_ed *
4499 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4502 struct cam_ed *device, *cur_device;
4504 device = xpt_alloc_device(bus, target, lun_id);
4508 device->mintags = 1;
4509 device->maxtags = 1;
4510 bus->sim->max_ccbs += device->ccbq.devq_openings;
4511 cur_device = TAILQ_FIRST(&target->ed_entries);
4512 while (cur_device != NULL && cur_device->lun_id < lun_id)
4513 cur_device = TAILQ_NEXT(cur_device, links);
4514 if (cur_device != NULL) {
4515 TAILQ_INSERT_BEFORE(cur_device, device, links);
4517 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4519 target->generation++;
4525 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4527 struct cam_ed *device;
4528 struct cam_devq *devq;
4531 /* Make space for us in the device queue on our bus */
4532 devq = bus->sim->devq;
4533 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4535 if (status != CAM_REQ_CMP) {
4538 device = (struct cam_ed *)malloc(sizeof(*device),
4539 M_CAMXPT, M_NOWAIT|M_ZERO);
4542 if (device != NULL) {
4543 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4544 device->alloc_ccb_entry.device = device;
4545 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4546 device->send_ccb_entry.device = device;
4547 device->target = target;
4548 device->lun_id = lun_id;
4549 device->sim = bus->sim;
4550 /* Initialize our queues */
4551 if (camq_init(&device->drvq, 0) != 0) {
4552 free(device, M_CAMXPT);
4555 if (cam_ccbq_init(&device->ccbq,
4556 bus->sim->max_dev_openings) != 0) {
4557 camq_fini(&device->drvq);
4558 free(device, M_CAMXPT);
4561 SLIST_INIT(&device->asyncs);
4562 SLIST_INIT(&device->periphs);
4563 device->generation = 0;
4564 device->owner = NULL;
4565 device->flags = CAM_DEV_UNCONFIGURED;
4566 device->tag_delay_count = 0;
4567 device->tag_saved_openings = 0;
4568 device->refcount = 1;
4569 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4572 * Hold a reference to our parent target so it
4573 * will not go away before we do.
4582 xpt_acquire_device(struct cam_ed *device)
4589 xpt_release_device(struct cam_ed *device)
4592 if (device->refcount == 1) {
4593 struct cam_devq *devq;
4595 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4596 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4597 panic("Removing device while still queued for ccbs");
4599 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4600 callout_stop(&device->callout);
4602 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4603 device->target->generation++;
4604 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4605 /* Release our slot in the devq */
4606 devq = device->target->bus->sim->devq;
4607 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4608 camq_fini(&device->drvq);
4609 cam_ccbq_fini(&device->ccbq);
4610 xpt_release_target(device->target);
4611 free(device, M_CAMXPT);
4617 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4625 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4626 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4627 if (result == CAM_REQ_CMP && (diff < 0)) {
4628 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4630 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4631 || (dev->inq_flags & SID_CmdQue) != 0)
4632 dev->tag_saved_openings = newopenings;
4633 /* Adjust the global limit */
4634 dev->sim->max_ccbs += diff;
4638 static struct cam_eb *
4639 xpt_find_bus(path_id_t path_id)
4643 mtx_lock(&xsoftc.xpt_topo_lock);
4644 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4646 bus = TAILQ_NEXT(bus, links)) {
4647 if (bus->path_id == path_id) {
4652 mtx_unlock(&xsoftc.xpt_topo_lock);
4656 static struct cam_et *
4657 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4659 struct cam_et *target;
4661 for (target = TAILQ_FIRST(&bus->et_entries);
4663 target = TAILQ_NEXT(target, links)) {
4664 if (target->target_id == target_id) {
4672 static struct cam_ed *
4673 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4675 struct cam_ed *device;
4677 for (device = TAILQ_FIRST(&target->ed_entries);
4679 device = TAILQ_NEXT(device, links)) {
4680 if (device->lun_id == lun_id) {
4689 xpt_start_tags(struct cam_path *path)
4691 struct ccb_relsim crs;
4692 struct cam_ed *device;
4693 struct cam_sim *sim;
4696 device = path->device;
4697 sim = path->bus->sim;
4698 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4699 xpt_freeze_devq(path, /*count*/1);
4700 device->inq_flags |= SID_CmdQue;
4701 if (device->tag_saved_openings != 0)
4702 newopenings = device->tag_saved_openings;
4704 newopenings = min(device->maxtags,
4705 sim->max_tagged_dev_openings);
4706 xpt_dev_ccbq_resize(path, newopenings);
4707 xpt_async(AC_GETDEV_CHANGED, path, NULL);
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_async(AC_GETDEV_CHANGED, path, NULL);
4733 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4734 crs.ccb_h.func_code = XPT_REL_SIMQ;
4735 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4737 = crs.release_timeout
4740 xpt_action((union ccb *)&crs);
4744 xpt_boot_delay(void *arg)
4751 xpt_config(void *arg)
4754 * Now that interrupts are enabled, go find our devices
4758 /* Setup debugging flags and path */
4759 #ifdef CAM_DEBUG_BUS
4760 if (cam_dflags != CAM_DEBUG_NONE) {
4762 * Locking is specifically omitted here. No SIMs have
4763 * registered yet, so xpt_create_path will only be searching
4764 * empty lists of targets and devices.
4766 if (xpt_create_path(&cam_dpath, xpt_periph,
4767 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4768 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4769 printf("xpt_config: xpt_create_path() failed for debug"
4770 " target %d:%d:%d, debugging disabled\n",
4771 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4772 cam_dflags = CAM_DEBUG_NONE;
4776 #else /* !CAM_DEBUG_BUS */
4778 #endif /* CAM_DEBUG_BUS */
4779 #endif /* CAMDEBUG */
4781 periphdriver_init(1);
4783 callout_init(&xsoftc.boot_callout, 1);
4784 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4785 xpt_boot_delay, NULL);
4786 /* Fire up rescan thread. */
4787 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4788 printf("xpt_config: failed to create rescan thread.\n");
4796 xsoftc.buses_to_config++;
4801 xpt_release_boot(void)
4804 xsoftc.buses_to_config--;
4805 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4806 struct xpt_task *task;
4808 xsoftc.buses_config_done = 1;
4810 /* Call manually because we don't have any busses */
4811 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4813 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4814 taskqueue_enqueue(taskqueue_thread, &task->task);
4821 * If the given device only has one peripheral attached to it, and if that
4822 * peripheral is the passthrough driver, announce it. This insures that the
4823 * user sees some sort of announcement for every peripheral in their system.
4826 xptpassannouncefunc(struct cam_ed *device, void *arg)
4828 struct cam_periph *periph;
4831 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4832 periph = SLIST_NEXT(periph, periph_links), i++);
4834 periph = SLIST_FIRST(&device->periphs);
4836 && (strncmp(periph->periph_name, "pass", 4) == 0))
4837 xpt_announce_periph(periph, NULL);
4843 xpt_finishconfig_task(void *context, int pending)
4846 periphdriver_init(2);
4848 * Check for devices with no "standard" peripheral driver
4849 * attached. For any devices like that, announce the
4850 * passthrough driver so the user will see something.
4853 xpt_for_all_devices(xptpassannouncefunc, NULL);
4855 /* Release our hook so that the boot can continue. */
4856 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4857 free(xsoftc.xpt_config_hook, M_CAMXPT);
4858 xsoftc.xpt_config_hook = NULL;
4860 free(context, M_CAMXPT);
4864 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4865 struct cam_path *path)
4867 struct ccb_setasync csa;
4872 mtx_lock(&xsoftc.xpt_lock);
4873 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4874 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4875 if (status != CAM_REQ_CMP) {
4876 mtx_unlock(&xsoftc.xpt_lock);
4882 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4883 csa.ccb_h.func_code = XPT_SASYNC_CB;
4884 csa.event_enable = event;
4885 csa.callback = cbfunc;
4886 csa.callback_arg = cbarg;
4887 xpt_action((union ccb *)&csa);
4888 status = csa.ccb_h.status;
4891 xpt_free_path(path);
4892 mtx_unlock(&xsoftc.xpt_lock);
4895 if ((status == CAM_REQ_CMP) &&
4896 (csa.event_enable & AC_FOUND_DEVICE)) {
4898 * Get this peripheral up to date with all
4899 * the currently existing devices.
4901 xpt_for_all_devices(xptsetasyncfunc, &csa);
4903 if ((status == CAM_REQ_CMP) &&
4904 (csa.event_enable & AC_PATH_REGISTERED)) {
4906 * Get this peripheral up to date with all
4907 * the currently existing busses.
4909 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4916 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4918 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4920 switch (work_ccb->ccb_h.func_code) {
4921 /* Common cases first */
4922 case XPT_PATH_INQ: /* Path routing inquiry */
4924 struct ccb_pathinq *cpi;
4926 cpi = &work_ccb->cpi;
4927 cpi->version_num = 1; /* XXX??? */
4928 cpi->hba_inquiry = 0;
4929 cpi->target_sprt = 0;
4931 cpi->hba_eng_cnt = 0;
4932 cpi->max_target = 0;
4934 cpi->initiator_id = 0;
4935 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4936 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4937 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4938 cpi->unit_number = sim->unit_number;
4939 cpi->bus_id = sim->bus_id;
4940 cpi->base_transfer_speed = 0;
4941 cpi->protocol = PROTO_UNSPECIFIED;
4942 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4943 cpi->transport = XPORT_UNSPECIFIED;
4944 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4945 cpi->ccb_h.status = CAM_REQ_CMP;
4950 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4957 * The xpt as a "controller" has no interrupt sources, so polling
4961 xptpoll(struct cam_sim *sim)
4966 xpt_lock_buses(void)
4968 mtx_lock(&xsoftc.xpt_topo_lock);
4972 xpt_unlock_buses(void)
4974 mtx_unlock(&xsoftc.xpt_topo_lock);
4981 struct cam_sim *sim;
4983 mtx_lock(&cam_simq_lock);
4985 while (!TAILQ_EMPTY(&cam_simq)) {
4986 TAILQ_CONCAT(&queue, &cam_simq, links);
4987 mtx_unlock(&cam_simq_lock);
4989 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4990 TAILQ_REMOVE(&queue, sim, links);
4992 sim->flags &= ~CAM_SIM_ON_DONEQ;
4993 camisr_runqueue(&sim->sim_doneq);
4994 CAM_SIM_UNLOCK(sim);
4996 mtx_lock(&cam_simq_lock);
4998 mtx_unlock(&cam_simq_lock);
5002 camisr_runqueue(void *V_queue)
5004 cam_isrq_t *queue = V_queue;
5005 struct ccb_hdr *ccb_h;
5007 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5010 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5011 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5013 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5018 if (ccb_h->flags & CAM_HIGH_POWER) {
5019 struct highpowerlist *hphead;
5020 union ccb *send_ccb;
5022 mtx_lock(&xsoftc.xpt_lock);
5023 hphead = &xsoftc.highpowerq;
5025 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5028 * Increment the count since this command is done.
5030 xsoftc.num_highpower++;
5033 * Any high powered commands queued up?
5035 if (send_ccb != NULL) {
5037 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5038 mtx_unlock(&xsoftc.xpt_lock);
5040 xpt_release_devq(send_ccb->ccb_h.path,
5041 /*count*/1, /*runqueue*/TRUE);
5043 mtx_unlock(&xsoftc.xpt_lock);
5046 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5049 dev = ccb_h->path->device;
5051 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5052 ccb_h->path->bus->sim->devq->send_active--;
5053 ccb_h->path->bus->sim->devq->send_openings++;
5056 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5057 && (dev->ccbq.dev_active == 0))) {
5058 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5059 xpt_release_devq(ccb_h->path, /*count*/1,
5060 /*run_queue*/FALSE);
5063 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5064 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5065 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5066 xpt_release_devq(ccb_h->path, /*count*/1,
5067 /*run_queue*/FALSE);
5070 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5071 && (--dev->tag_delay_count == 0))
5072 xpt_start_tags(ccb_h->path);
5073 if (!device_is_send_queued(dev)) {
5074 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5079 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5080 xpt_release_simq(ccb_h->path->bus->sim,
5082 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5086 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5087 && (ccb_h->status & CAM_DEV_QFRZN)) {
5088 xpt_release_devq(ccb_h->path, /*count*/1,
5090 ccb_h->status &= ~CAM_DEV_QFRZN;
5092 xpt_run_dev_sendq(ccb_h->path->bus);
5095 /* Call the peripheral driver's callback */
5096 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);