2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/reboot.h>
43 #include <sys/interrupt.h>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
67 #include <machine/md_var.h> /* geometry translation */
68 #include <machine/stdarg.h> /* for xpt_print below */
73 * This is the maximum number of high powered commands (e.g. start unit)
74 * that can be outstanding at a particular time.
76 #ifndef CAM_MAX_HIGHPOWER
77 #define CAM_MAX_HIGHPOWER 4
80 /* Datastructures internal to the xpt layer */
81 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
83 /* Object for defering XPT actions to a taskqueue */
96 u_int32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
102 /* queue for handling async rescan requests. */
103 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
105 int buses_config_done;
107 /* Registered busses */
108 TAILQ_HEAD(,cam_eb) xpt_busses;
109 u_int bus_generation;
111 struct intr_config_hook *xpt_config_hook;
114 struct callout boot_callout;
116 struct mtx xpt_topo_lock;
122 DM_RET_FLAG_MASK = 0x0f,
125 DM_RET_DESCEND = 0x20,
127 DM_RET_ACTION_MASK = 0xf0
135 } xpt_traverse_depth;
137 struct xpt_traverse_config {
138 xpt_traverse_depth depth;
143 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
144 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
145 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
146 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
147 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
149 /* Transport layer configuration information */
150 static struct xpt_softc xsoftc;
152 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
153 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
154 &xsoftc.boot_delay, 0, "Bus registration wait time");
156 /* Queues for our software interrupt handler */
157 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
158 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
159 static cam_simq_t cam_simq;
160 static struct mtx cam_simq_lock;
162 /* Pointers to software interrupt handlers */
163 static void *cambio_ih;
165 struct cam_periph *xpt_periph;
167 static periph_init_t xpt_periph_init;
169 static struct periph_driver xpt_driver =
171 xpt_periph_init, "xpt",
172 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
176 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
178 static d_open_t xptopen;
179 static d_close_t xptclose;
180 static d_ioctl_t xptioctl;
182 static struct cdevsw xpt_cdevsw = {
183 .d_version = D_VERSION,
191 /* Storage for debugging datastructures */
192 struct cam_path *cam_dpath;
193 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
194 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
195 SYSCTL_INT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
196 &cam_dflags, 0, "Enabled debug flags");
197 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
198 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
199 SYSCTL_INT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
200 &cam_debug_delay, 0, "Delay in us after each debug message");
202 /* Our boot-time initialization hook */
203 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
205 static moduledata_t cam_moduledata = {
207 cam_module_event_handler,
211 static int xpt_init(void *);
213 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
214 MODULE_VERSION(cam, 1);
217 static void xpt_async_bcast(struct async_list *async_head,
218 u_int32_t async_code,
219 struct cam_path *path,
221 static path_id_t xptnextfreepathid(void);
222 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
223 static union ccb *xpt_get_ccb(struct cam_ed *device);
224 static void xpt_run_dev_allocq(struct cam_eb *bus);
225 static void xpt_run_dev_sendq(struct cam_eb *bus);
226 static timeout_t xpt_release_devq_timeout;
227 static void xpt_release_simq_timeout(void *arg) __unused;
228 static void xpt_release_bus(struct cam_eb *bus);
229 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
230 u_int count, int run_queue);
231 static struct cam_et*
232 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
233 static void xpt_release_target(struct cam_et *target);
234 static struct cam_eb*
235 xpt_find_bus(path_id_t path_id);
236 static struct cam_et*
237 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
238 static struct cam_ed*
239 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
240 static void xpt_config(void *arg);
241 static xpt_devicefunc_t xptpassannouncefunc;
242 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
243 static void xptpoll(struct cam_sim *sim);
244 static void camisr(void *);
245 static void camisr_runqueue(void *);
246 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
247 u_int num_patterns, struct cam_eb *bus);
248 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
250 struct cam_ed *device);
251 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
253 struct cam_periph *periph);
254 static xpt_busfunc_t xptedtbusfunc;
255 static xpt_targetfunc_t xptedttargetfunc;
256 static xpt_devicefunc_t xptedtdevicefunc;
257 static xpt_periphfunc_t xptedtperiphfunc;
258 static xpt_pdrvfunc_t xptplistpdrvfunc;
259 static xpt_periphfunc_t xptplistperiphfunc;
260 static int xptedtmatch(struct ccb_dev_match *cdm);
261 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
262 static int xptbustraverse(struct cam_eb *start_bus,
263 xpt_busfunc_t *tr_func, void *arg);
264 static int xpttargettraverse(struct cam_eb *bus,
265 struct cam_et *start_target,
266 xpt_targetfunc_t *tr_func, void *arg);
267 static int xptdevicetraverse(struct cam_et *target,
268 struct cam_ed *start_device,
269 xpt_devicefunc_t *tr_func, void *arg);
270 static int xptperiphtraverse(struct cam_ed *device,
271 struct cam_periph *start_periph,
272 xpt_periphfunc_t *tr_func, void *arg);
273 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
274 xpt_pdrvfunc_t *tr_func, void *arg);
275 static int xptpdperiphtraverse(struct periph_driver **pdrv,
276 struct cam_periph *start_periph,
277 xpt_periphfunc_t *tr_func,
279 static xpt_busfunc_t xptdefbusfunc;
280 static xpt_targetfunc_t xptdeftargetfunc;
281 static xpt_devicefunc_t xptdefdevicefunc;
282 static xpt_periphfunc_t xptdefperiphfunc;
283 static void xpt_finishconfig_task(void *context, int pending);
284 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
285 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
287 static void xpt_dev_async_default(u_int32_t async_code,
289 struct cam_et *target,
290 struct cam_ed *device,
292 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
293 struct cam_et *target,
295 static xpt_devicefunc_t xptsetasyncfunc;
296 static xpt_busfunc_t xptsetasyncbusfunc;
297 static cam_status xptregister(struct cam_periph *periph,
299 static __inline int periph_is_queued(struct cam_periph *periph);
300 static __inline int device_is_alloc_queued(struct cam_ed *device);
301 static __inline int device_is_send_queued(struct cam_ed *device);
304 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
308 if ((dev->drvq.entries > 0) &&
309 (dev->ccbq.devq_openings > 0) &&
310 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
311 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
313 * The priority of a device waiting for CCB resources
314 * is that of the highest priority peripheral driver
317 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
318 &dev->alloc_ccb_entry.pinfo,
319 CAMQ_GET_PRIO(&dev->drvq));
328 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
332 if ((dev->ccbq.queue.entries > 0) &&
333 (dev->ccbq.dev_openings > 0) &&
334 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
336 * The priority of a device waiting for controller
337 * resources is that of the highest priority CCB
341 xpt_schedule_dev(&bus->sim->devq->send_queue,
342 &dev->send_ccb_entry.pinfo,
343 CAMQ_GET_PRIO(&dev->ccbq.queue));
351 periph_is_queued(struct cam_periph *periph)
353 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
357 device_is_alloc_queued(struct cam_ed *device)
359 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
363 device_is_send_queued(struct cam_ed *device)
365 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
371 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
375 xptdone(struct cam_periph *periph, union ccb *done_ccb)
377 /* Caller will release the CCB */
378 wakeup(&done_ccb->ccb_h.cbfcnp);
382 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
386 * Only allow read-write access.
388 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
392 * We don't allow nonblocking access.
394 if ((flags & O_NONBLOCK) != 0) {
395 printf("%s: can't do nonblocking access\n", devtoname(dev));
399 /* Mark ourselves open */
400 mtx_lock(&xsoftc.xpt_lock);
401 xsoftc.flags |= XPT_FLAG_OPEN;
402 mtx_unlock(&xsoftc.xpt_lock);
408 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
411 /* Mark ourselves closed */
412 mtx_lock(&xsoftc.xpt_lock);
413 xsoftc.flags &= ~XPT_FLAG_OPEN;
414 mtx_unlock(&xsoftc.xpt_lock);
420 * Don't automatically grab the xpt softc lock here even though this is going
421 * through the xpt device. The xpt device is really just a back door for
422 * accessing other devices and SIMs, so the right thing to do is to grab
423 * the appropriate SIM lock once the bus/SIM is located.
426 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
434 * For the transport layer CAMIOCOMMAND ioctl, we really only want
435 * to accept CCB types that don't quite make sense to send through a
436 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
444 inccb = (union ccb *)addr;
446 bus = xpt_find_bus(inccb->ccb_h.path_id);
450 switch (inccb->ccb_h.func_code) {
453 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
454 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
455 xpt_release_bus(bus);
460 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
461 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
462 xpt_release_bus(bus);
470 switch(inccb->ccb_h.func_code) {
478 ccb = xpt_alloc_ccb();
480 CAM_SIM_LOCK(bus->sim);
483 * Create a path using the bus, target, and lun the
486 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
487 inccb->ccb_h.path_id,
488 inccb->ccb_h.target_id,
489 inccb->ccb_h.target_lun) !=
492 CAM_SIM_UNLOCK(bus->sim);
496 /* Ensure all of our fields are correct */
497 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
498 inccb->ccb_h.pinfo.priority);
499 xpt_merge_ccb(ccb, inccb);
500 ccb->ccb_h.cbfcnp = xptdone;
501 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
502 bcopy(ccb, inccb, sizeof(union ccb));
503 xpt_free_path(ccb->ccb_h.path);
505 CAM_SIM_UNLOCK(bus->sim);
512 * This is an immediate CCB, so it's okay to
513 * allocate it on the stack.
516 CAM_SIM_LOCK(bus->sim);
519 * Create a path using the bus, target, and lun the
522 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
523 inccb->ccb_h.path_id,
524 inccb->ccb_h.target_id,
525 inccb->ccb_h.target_lun) !=
528 CAM_SIM_UNLOCK(bus->sim);
531 /* Ensure all of our fields are correct */
532 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
533 inccb->ccb_h.pinfo.priority);
534 xpt_merge_ccb(&ccb, inccb);
535 ccb.ccb_h.cbfcnp = xptdone;
537 CAM_SIM_UNLOCK(bus->sim);
538 bcopy(&ccb, inccb, sizeof(union ccb));
539 xpt_free_path(ccb.ccb_h.path);
543 case XPT_DEV_MATCH: {
544 struct cam_periph_map_info mapinfo;
545 struct cam_path *old_path;
548 * We can't deal with physical addresses for this
549 * type of transaction.
551 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
557 * Save this in case the caller had it set to
558 * something in particular.
560 old_path = inccb->ccb_h.path;
563 * We really don't need a path for the matching
564 * code. The path is needed because of the
565 * debugging statements in xpt_action(). They
566 * assume that the CCB has a valid path.
568 inccb->ccb_h.path = xpt_periph->path;
570 bzero(&mapinfo, sizeof(mapinfo));
573 * Map the pattern and match buffers into kernel
574 * virtual address space.
576 error = cam_periph_mapmem(inccb, &mapinfo);
579 inccb->ccb_h.path = old_path;
584 * This is an immediate CCB, we can send it on directly.
589 * Map the buffers back into user space.
591 cam_periph_unmapmem(inccb, &mapinfo);
593 inccb->ccb_h.path = old_path;
602 xpt_release_bus(bus);
606 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
607 * with the periphal driver name and unit name filled in. The other
608 * fields don't really matter as input. The passthrough driver name
609 * ("pass"), and unit number are passed back in the ccb. The current
610 * device generation number, and the index into the device peripheral
611 * driver list, and the status are also passed back. Note that
612 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
613 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
614 * (or rather should be) impossible for the device peripheral driver
615 * list to change since we look at the whole thing in one pass, and
616 * we do it with lock protection.
619 case CAMGETPASSTHRU: {
621 struct cam_periph *periph;
622 struct periph_driver **p_drv;
625 u_int cur_generation;
626 int base_periph_found;
629 ccb = (union ccb *)addr;
630 unit = ccb->cgdl.unit_number;
631 name = ccb->cgdl.periph_name;
633 * Every 100 devices, we want to drop our lock protection to
634 * give the software interrupt handler a chance to run.
635 * Most systems won't run into this check, but this should
636 * avoid starvation in the software interrupt handler in
641 ccb = (union ccb *)addr;
643 base_periph_found = 0;
646 * Sanity check -- make sure we don't get a null peripheral
649 if (*ccb->cgdl.periph_name == '\0') {
654 /* Keep the list from changing while we traverse it */
655 mtx_lock(&xsoftc.xpt_topo_lock);
657 cur_generation = xsoftc.xpt_generation;
659 /* first find our driver in the list of drivers */
660 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
661 if (strcmp((*p_drv)->driver_name, name) == 0)
664 if (*p_drv == NULL) {
665 mtx_unlock(&xsoftc.xpt_topo_lock);
666 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
667 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
668 *ccb->cgdl.periph_name = '\0';
669 ccb->cgdl.unit_number = 0;
675 * Run through every peripheral instance of this driver
676 * and check to see whether it matches the unit passed
677 * in by the user. If it does, get out of the loops and
678 * find the passthrough driver associated with that
681 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
682 periph = TAILQ_NEXT(periph, unit_links)) {
684 if (periph->unit_number == unit) {
686 } else if (--splbreaknum == 0) {
687 mtx_unlock(&xsoftc.xpt_topo_lock);
688 mtx_lock(&xsoftc.xpt_topo_lock);
690 if (cur_generation != xsoftc.xpt_generation)
695 * If we found the peripheral driver that the user passed
696 * in, go through all of the peripheral drivers for that
697 * particular device and look for a passthrough driver.
699 if (periph != NULL) {
700 struct cam_ed *device;
703 base_periph_found = 1;
704 device = periph->path->device;
705 for (i = 0, periph = SLIST_FIRST(&device->periphs);
707 periph = SLIST_NEXT(periph, periph_links), i++) {
709 * Check to see whether we have a
710 * passthrough device or not.
712 if (strcmp(periph->periph_name, "pass") == 0) {
714 * Fill in the getdevlist fields.
716 strcpy(ccb->cgdl.periph_name,
717 periph->periph_name);
718 ccb->cgdl.unit_number =
720 if (SLIST_NEXT(periph, periph_links))
722 CAM_GDEVLIST_MORE_DEVS;
725 CAM_GDEVLIST_LAST_DEVICE;
726 ccb->cgdl.generation =
730 * Fill in some CCB header fields
731 * that the user may want.
734 periph->path->bus->path_id;
735 ccb->ccb_h.target_id =
736 periph->path->target->target_id;
737 ccb->ccb_h.target_lun =
738 periph->path->device->lun_id;
739 ccb->ccb_h.status = CAM_REQ_CMP;
746 * If the periph is null here, one of two things has
747 * happened. The first possibility is that we couldn't
748 * find the unit number of the particular peripheral driver
749 * that the user is asking about. e.g. the user asks for
750 * the passthrough driver for "da11". We find the list of
751 * "da" peripherals all right, but there is no unit 11.
752 * The other possibility is that we went through the list
753 * of peripheral drivers attached to the device structure,
754 * but didn't find one with the name "pass". Either way,
755 * we return ENOENT, since we couldn't find something.
757 if (periph == NULL) {
758 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
759 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
760 *ccb->cgdl.periph_name = '\0';
761 ccb->cgdl.unit_number = 0;
764 * It is unfortunate that this is even necessary,
765 * but there are many, many clueless users out there.
766 * If this is true, the user is looking for the
767 * passthrough driver, but doesn't have one in his
770 if (base_periph_found == 1) {
771 printf("xptioctl: pass driver is not in the "
773 printf("xptioctl: put \"device pass\" in "
774 "your kernel config file\n");
777 mtx_unlock(&xsoftc.xpt_topo_lock);
789 cam_module_event_handler(module_t mod, int what, void *arg)
795 if ((error = xpt_init(NULL)) != 0)
808 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
811 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
812 xpt_free_path(done_ccb->ccb_h.path);
813 xpt_free_ccb(done_ccb);
815 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
816 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
821 /* thread to handle bus rescans */
823 xpt_scanner_thread(void *dummy)
830 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
831 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
833 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
834 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
837 sim = ccb->ccb_h.path->bus->sim;
848 xpt_rescan(union ccb *ccb)
852 /* Prepare request */
853 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
854 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
855 ccb->ccb_h.func_code = XPT_SCAN_BUS;
856 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
857 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
858 ccb->ccb_h.func_code = XPT_SCAN_TGT;
859 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
860 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
861 ccb->ccb_h.func_code = XPT_SCAN_LUN;
863 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
864 xpt_free_path(ccb->ccb_h.path);
868 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
869 ccb->ccb_h.cbfcnp = xpt_rescan_done;
870 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
871 /* Don't make duplicate entries for the same paths. */
873 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
874 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
875 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
876 wakeup(&xsoftc.ccb_scanq);
878 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
879 xpt_free_path(ccb->ccb_h.path);
885 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
886 xsoftc.buses_to_config++;
887 wakeup(&xsoftc.ccb_scanq);
891 /* Functions accessed by the peripheral drivers */
893 xpt_init(void *dummy)
895 struct cam_sim *xpt_sim;
896 struct cam_path *path;
897 struct cam_devq *devq;
900 TAILQ_INIT(&xsoftc.xpt_busses);
901 TAILQ_INIT(&cam_simq);
902 TAILQ_INIT(&xsoftc.ccb_scanq);
903 STAILQ_INIT(&xsoftc.highpowerq);
904 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
906 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
907 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
908 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
911 * The xpt layer is, itself, the equivelent of a SIM.
912 * Allow 16 ccbs in the ccb pool for it. This should
913 * give decent parallelism when we probe busses and
914 * perform other XPT functions.
916 devq = cam_simq_alloc(16);
917 xpt_sim = cam_sim_alloc(xptaction,
922 /*mtx*/&xsoftc.xpt_lock,
923 /*max_dev_transactions*/0,
924 /*max_tagged_dev_transactions*/0,
929 mtx_lock(&xsoftc.xpt_lock);
930 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
931 mtx_unlock(&xsoftc.xpt_lock);
932 printf("xpt_init: xpt_bus_register failed with status %#x,"
933 " failing attach\n", status);
938 * Looking at the XPT from the SIM layer, the XPT is
939 * the equivelent of a peripheral driver. Allocate
940 * a peripheral driver entry for us.
942 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
944 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
945 mtx_unlock(&xsoftc.xpt_lock);
946 printf("xpt_init: xpt_create_path failed with status %#x,"
947 " failing attach\n", status);
951 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
952 path, NULL, 0, xpt_sim);
954 mtx_unlock(&xsoftc.xpt_lock);
955 /* Install our software interrupt handlers */
956 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
958 * Register a callback for when interrupts are enabled.
960 xsoftc.xpt_config_hook =
961 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
962 M_CAMXPT, M_NOWAIT | M_ZERO);
963 if (xsoftc.xpt_config_hook == NULL) {
964 printf("xpt_init: Cannot malloc config hook "
965 "- failing attach\n");
968 xsoftc.xpt_config_hook->ich_func = xpt_config;
969 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
970 free (xsoftc.xpt_config_hook, M_CAMXPT);
971 printf("xpt_init: config_intrhook_establish failed "
972 "- failing attach\n");
979 xptregister(struct cam_periph *periph, void *arg)
981 struct cam_sim *xpt_sim;
983 if (periph == NULL) {
984 printf("xptregister: periph was NULL!!\n");
985 return(CAM_REQ_CMP_ERR);
988 xpt_sim = (struct cam_sim *)arg;
989 xpt_sim->softc = periph;
991 periph->softc = NULL;
997 xpt_add_periph(struct cam_periph *periph)
999 struct cam_ed *device;
1001 struct periph_list *periph_head;
1003 mtx_assert(periph->sim->mtx, MA_OWNED);
1005 device = periph->path->device;
1007 periph_head = &device->periphs;
1009 status = CAM_REQ_CMP;
1011 if (device != NULL) {
1013 * Make room for this peripheral
1014 * so it will fit in the queue
1015 * when it's scheduled to run
1017 status = camq_resize(&device->drvq,
1018 device->drvq.array_size + 1);
1020 device->generation++;
1022 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1025 mtx_lock(&xsoftc.xpt_topo_lock);
1026 xsoftc.xpt_generation++;
1027 mtx_unlock(&xsoftc.xpt_topo_lock);
1033 xpt_remove_periph(struct cam_periph *periph)
1035 struct cam_ed *device;
1037 mtx_assert(periph->sim->mtx, MA_OWNED);
1039 device = periph->path->device;
1041 if (device != NULL) {
1042 struct periph_list *periph_head;
1044 periph_head = &device->periphs;
1046 /* Release the slot for this peripheral */
1047 camq_resize(&device->drvq, device->drvq.array_size - 1);
1049 device->generation++;
1051 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1054 mtx_lock(&xsoftc.xpt_topo_lock);
1055 xsoftc.xpt_generation++;
1056 mtx_unlock(&xsoftc.xpt_topo_lock);
1061 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1063 struct cam_path *path = periph->path;
1065 mtx_assert(periph->sim->mtx, MA_OWNED);
1067 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1068 periph->periph_name, periph->unit_number,
1069 path->bus->sim->sim_name,
1070 path->bus->sim->unit_number,
1071 path->bus->sim->bus_id,
1073 path->target->target_id,
1074 path->device->lun_id);
1075 printf("%s%d: ", periph->periph_name, periph->unit_number);
1076 if (path->device->protocol == PROTO_SCSI)
1077 scsi_print_inquiry(&path->device->inq_data);
1078 else if (path->device->protocol == PROTO_ATA ||
1079 path->device->protocol == PROTO_SATAPM)
1080 ata_print_ident(&path->device->ident_data);
1082 printf("Unknown protocol device\n");
1083 if (bootverbose && path->device->serial_num_len > 0) {
1084 /* Don't wrap the screen - print only the first 60 chars */
1085 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1086 periph->unit_number, path->device->serial_num);
1088 /* Announce transport details. */
1089 (*(path->bus->xport->announce))(periph);
1090 /* Announce command queueing. */
1091 if (path->device->inq_flags & SID_CmdQue
1092 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1093 printf("%s%d: Command Queueing enabled\n",
1094 periph->periph_name, periph->unit_number);
1096 /* Announce caller's details if they've passed in. */
1097 if (announce_string != NULL)
1098 printf("%s%d: %s\n", periph->periph_name,
1099 periph->unit_number, announce_string);
1103 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1106 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1107 periph->unit_number, quirks, bit_string);
1111 static dev_match_ret
1112 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1115 dev_match_ret retval;
1118 retval = DM_RET_NONE;
1121 * If we aren't given something to match against, that's an error.
1124 return(DM_RET_ERROR);
1127 * If there are no match entries, then this bus matches no
1130 if ((patterns == NULL) || (num_patterns == 0))
1131 return(DM_RET_DESCEND | DM_RET_COPY);
1133 for (i = 0; i < num_patterns; i++) {
1134 struct bus_match_pattern *cur_pattern;
1137 * If the pattern in question isn't for a bus node, we
1138 * aren't interested. However, we do indicate to the
1139 * calling routine that we should continue descending the
1140 * tree, since the user wants to match against lower-level
1143 if (patterns[i].type != DEV_MATCH_BUS) {
1144 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1145 retval |= DM_RET_DESCEND;
1149 cur_pattern = &patterns[i].pattern.bus_pattern;
1152 * If they want to match any bus node, we give them any
1155 if (cur_pattern->flags == BUS_MATCH_ANY) {
1156 /* set the copy flag */
1157 retval |= DM_RET_COPY;
1160 * If we've already decided on an action, go ahead
1163 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1168 * Not sure why someone would do this...
1170 if (cur_pattern->flags == BUS_MATCH_NONE)
1173 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1174 && (cur_pattern->path_id != bus->path_id))
1177 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1178 && (cur_pattern->bus_id != bus->sim->bus_id))
1181 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1182 && (cur_pattern->unit_number != bus->sim->unit_number))
1185 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1186 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1191 * If we get to this point, the user definitely wants
1192 * information on this bus. So tell the caller to copy the
1195 retval |= DM_RET_COPY;
1198 * If the return action has been set to descend, then we
1199 * know that we've already seen a non-bus matching
1200 * expression, therefore we need to further descend the tree.
1201 * This won't change by continuing around the loop, so we
1202 * go ahead and return. If we haven't seen a non-bus
1203 * matching expression, we keep going around the loop until
1204 * we exhaust the matching expressions. We'll set the stop
1205 * flag once we fall out of the loop.
1207 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1212 * If the return action hasn't been set to descend yet, that means
1213 * we haven't seen anything other than bus matching patterns. So
1214 * tell the caller to stop descending the tree -- the user doesn't
1215 * want to match against lower level tree elements.
1217 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1218 retval |= DM_RET_STOP;
1223 static dev_match_ret
1224 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1225 struct cam_ed *device)
1227 dev_match_ret retval;
1230 retval = DM_RET_NONE;
1233 * If we aren't given something to match against, that's an error.
1236 return(DM_RET_ERROR);
1239 * If there are no match entries, then this device matches no
1242 if ((patterns == NULL) || (num_patterns == 0))
1243 return(DM_RET_DESCEND | DM_RET_COPY);
1245 for (i = 0; i < num_patterns; i++) {
1246 struct device_match_pattern *cur_pattern;
1249 * If the pattern in question isn't for a device node, we
1250 * aren't interested.
1252 if (patterns[i].type != DEV_MATCH_DEVICE) {
1253 if ((patterns[i].type == DEV_MATCH_PERIPH)
1254 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1255 retval |= DM_RET_DESCEND;
1259 cur_pattern = &patterns[i].pattern.device_pattern;
1262 * If they want to match any device node, we give them any
1265 if (cur_pattern->flags == DEV_MATCH_ANY) {
1266 /* set the copy flag */
1267 retval |= DM_RET_COPY;
1271 * If we've already decided on an action, go ahead
1274 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1279 * Not sure why someone would do this...
1281 if (cur_pattern->flags == DEV_MATCH_NONE)
1284 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1285 && (cur_pattern->path_id != device->target->bus->path_id))
1288 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1289 && (cur_pattern->target_id != device->target->target_id))
1292 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1293 && (cur_pattern->target_lun != device->lun_id))
1296 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1297 && (cam_quirkmatch((caddr_t)&device->inq_data,
1298 (caddr_t)&cur_pattern->inq_pat,
1299 1, sizeof(cur_pattern->inq_pat),
1300 scsi_static_inquiry_match) == NULL))
1304 * If we get to this point, the user definitely wants
1305 * information on this device. So tell the caller to copy
1308 retval |= DM_RET_COPY;
1311 * If the return action has been set to descend, then we
1312 * know that we've already seen a peripheral matching
1313 * expression, therefore we need to further descend the tree.
1314 * This won't change by continuing around the loop, so we
1315 * go ahead and return. If we haven't seen a peripheral
1316 * matching expression, we keep going around the loop until
1317 * we exhaust the matching expressions. We'll set the stop
1318 * flag once we fall out of the loop.
1320 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1325 * If the return action hasn't been set to descend yet, that means
1326 * we haven't seen any peripheral matching patterns. So tell the
1327 * caller to stop descending the tree -- the user doesn't want to
1328 * match against lower level tree elements.
1330 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1331 retval |= DM_RET_STOP;
1337 * Match a single peripheral against any number of match patterns.
1339 static dev_match_ret
1340 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1341 struct cam_periph *periph)
1343 dev_match_ret retval;
1347 * If we aren't given something to match against, that's an error.
1350 return(DM_RET_ERROR);
1353 * If there are no match entries, then this peripheral matches no
1356 if ((patterns == NULL) || (num_patterns == 0))
1357 return(DM_RET_STOP | DM_RET_COPY);
1360 * There aren't any nodes below a peripheral node, so there's no
1361 * reason to descend the tree any further.
1363 retval = DM_RET_STOP;
1365 for (i = 0; i < num_patterns; i++) {
1366 struct periph_match_pattern *cur_pattern;
1369 * If the pattern in question isn't for a peripheral, we
1370 * aren't interested.
1372 if (patterns[i].type != DEV_MATCH_PERIPH)
1375 cur_pattern = &patterns[i].pattern.periph_pattern;
1378 * If they want to match on anything, then we will do so.
1380 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1381 /* set the copy flag */
1382 retval |= DM_RET_COPY;
1385 * We've already set the return action to stop,
1386 * since there are no nodes below peripherals in
1393 * Not sure why someone would do this...
1395 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1398 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1399 && (cur_pattern->path_id != periph->path->bus->path_id))
1403 * For the target and lun id's, we have to make sure the
1404 * target and lun pointers aren't NULL. The xpt peripheral
1405 * has a wildcard target and device.
1407 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1408 && ((periph->path->target == NULL)
1409 ||(cur_pattern->target_id != periph->path->target->target_id)))
1412 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1413 && ((periph->path->device == NULL)
1414 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1417 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1418 && (cur_pattern->unit_number != periph->unit_number))
1421 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1422 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1427 * If we get to this point, the user definitely wants
1428 * information on this peripheral. So tell the caller to
1429 * copy the data out.
1431 retval |= DM_RET_COPY;
1434 * The return action has already been set to stop, since
1435 * peripherals don't have any nodes below them in the EDT.
1441 * If we get to this point, the peripheral that was passed in
1442 * doesn't match any of the patterns.
1448 xptedtbusfunc(struct cam_eb *bus, void *arg)
1450 struct ccb_dev_match *cdm;
1451 dev_match_ret retval;
1453 cdm = (struct ccb_dev_match *)arg;
1456 * If our position is for something deeper in the tree, that means
1457 * that we've already seen this node. So, we keep going down.
1459 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1460 && (cdm->pos.cookie.bus == bus)
1461 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1462 && (cdm->pos.cookie.target != NULL))
1463 retval = DM_RET_DESCEND;
1465 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1468 * If we got an error, bail out of the search.
1470 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1471 cdm->status = CAM_DEV_MATCH_ERROR;
1476 * If the copy flag is set, copy this bus out.
1478 if (retval & DM_RET_COPY) {
1481 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1482 sizeof(struct dev_match_result));
1485 * If we don't have enough space to put in another
1486 * match result, save our position and tell the
1487 * user there are more devices to check.
1489 if (spaceleft < sizeof(struct dev_match_result)) {
1490 bzero(&cdm->pos, sizeof(cdm->pos));
1491 cdm->pos.position_type =
1492 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1494 cdm->pos.cookie.bus = bus;
1495 cdm->pos.generations[CAM_BUS_GENERATION]=
1496 xsoftc.bus_generation;
1497 cdm->status = CAM_DEV_MATCH_MORE;
1500 j = cdm->num_matches;
1502 cdm->matches[j].type = DEV_MATCH_BUS;
1503 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1504 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1505 cdm->matches[j].result.bus_result.unit_number =
1506 bus->sim->unit_number;
1507 strncpy(cdm->matches[j].result.bus_result.dev_name,
1508 bus->sim->sim_name, DEV_IDLEN);
1512 * If the user is only interested in busses, there's no
1513 * reason to descend to the next level in the tree.
1515 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1519 * If there is a target generation recorded, check it to
1520 * make sure the target list hasn't changed.
1522 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1523 && (bus == cdm->pos.cookie.bus)
1524 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1525 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1526 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1528 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1532 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1533 && (cdm->pos.cookie.bus == bus)
1534 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1535 && (cdm->pos.cookie.target != NULL))
1536 return(xpttargettraverse(bus,
1537 (struct cam_et *)cdm->pos.cookie.target,
1538 xptedttargetfunc, arg));
1540 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1544 xptedttargetfunc(struct cam_et *target, void *arg)
1546 struct ccb_dev_match *cdm;
1548 cdm = (struct ccb_dev_match *)arg;
1551 * If there is a device list generation recorded, check it to
1552 * make sure the device list hasn't changed.
1554 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1555 && (cdm->pos.cookie.bus == target->bus)
1556 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1557 && (cdm->pos.cookie.target == target)
1558 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1559 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1560 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1561 target->generation)) {
1562 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1566 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1567 && (cdm->pos.cookie.bus == target->bus)
1568 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1569 && (cdm->pos.cookie.target == target)
1570 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1571 && (cdm->pos.cookie.device != NULL))
1572 return(xptdevicetraverse(target,
1573 (struct cam_ed *)cdm->pos.cookie.device,
1574 xptedtdevicefunc, arg));
1576 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1580 xptedtdevicefunc(struct cam_ed *device, void *arg)
1583 struct ccb_dev_match *cdm;
1584 dev_match_ret retval;
1586 cdm = (struct ccb_dev_match *)arg;
1589 * If our position is for something deeper in the tree, that means
1590 * that we've already seen this node. So, we keep going down.
1592 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1593 && (cdm->pos.cookie.device == device)
1594 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1595 && (cdm->pos.cookie.periph != NULL))
1596 retval = DM_RET_DESCEND;
1598 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1601 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1602 cdm->status = CAM_DEV_MATCH_ERROR;
1607 * If the copy flag is set, copy this device out.
1609 if (retval & DM_RET_COPY) {
1612 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1613 sizeof(struct dev_match_result));
1616 * If we don't have enough space to put in another
1617 * match result, save our position and tell the
1618 * user there are more devices to check.
1620 if (spaceleft < sizeof(struct dev_match_result)) {
1621 bzero(&cdm->pos, sizeof(cdm->pos));
1622 cdm->pos.position_type =
1623 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1624 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1626 cdm->pos.cookie.bus = device->target->bus;
1627 cdm->pos.generations[CAM_BUS_GENERATION]=
1628 xsoftc.bus_generation;
1629 cdm->pos.cookie.target = device->target;
1630 cdm->pos.generations[CAM_TARGET_GENERATION] =
1631 device->target->bus->generation;
1632 cdm->pos.cookie.device = device;
1633 cdm->pos.generations[CAM_DEV_GENERATION] =
1634 device->target->generation;
1635 cdm->status = CAM_DEV_MATCH_MORE;
1638 j = cdm->num_matches;
1640 cdm->matches[j].type = DEV_MATCH_DEVICE;
1641 cdm->matches[j].result.device_result.path_id =
1642 device->target->bus->path_id;
1643 cdm->matches[j].result.device_result.target_id =
1644 device->target->target_id;
1645 cdm->matches[j].result.device_result.target_lun =
1647 cdm->matches[j].result.device_result.protocol =
1649 bcopy(&device->inq_data,
1650 &cdm->matches[j].result.device_result.inq_data,
1651 sizeof(struct scsi_inquiry_data));
1652 bcopy(&device->ident_data,
1653 &cdm->matches[j].result.device_result.ident_data,
1654 sizeof(struct ata_params));
1656 /* Let the user know whether this device is unconfigured */
1657 if (device->flags & CAM_DEV_UNCONFIGURED)
1658 cdm->matches[j].result.device_result.flags =
1659 DEV_RESULT_UNCONFIGURED;
1661 cdm->matches[j].result.device_result.flags =
1666 * If the user isn't interested in peripherals, don't descend
1667 * the tree any further.
1669 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1673 * If there is a peripheral list generation recorded, make sure
1674 * it hasn't changed.
1676 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1677 && (device->target->bus == cdm->pos.cookie.bus)
1678 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1679 && (device->target == cdm->pos.cookie.target)
1680 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1681 && (device == cdm->pos.cookie.device)
1682 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1683 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1684 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1685 device->generation)){
1686 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1690 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1691 && (cdm->pos.cookie.bus == device->target->bus)
1692 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1693 && (cdm->pos.cookie.target == device->target)
1694 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1695 && (cdm->pos.cookie.device == device)
1696 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1697 && (cdm->pos.cookie.periph != NULL))
1698 return(xptperiphtraverse(device,
1699 (struct cam_periph *)cdm->pos.cookie.periph,
1700 xptedtperiphfunc, arg));
1702 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1706 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1708 struct ccb_dev_match *cdm;
1709 dev_match_ret retval;
1711 cdm = (struct ccb_dev_match *)arg;
1713 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1715 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1716 cdm->status = CAM_DEV_MATCH_ERROR;
1721 * If the copy flag is set, copy this peripheral out.
1723 if (retval & DM_RET_COPY) {
1726 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1727 sizeof(struct dev_match_result));
1730 * If we don't have enough space to put in another
1731 * match result, save our position and tell the
1732 * user there are more devices to check.
1734 if (spaceleft < sizeof(struct dev_match_result)) {
1735 bzero(&cdm->pos, sizeof(cdm->pos));
1736 cdm->pos.position_type =
1737 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1738 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1741 cdm->pos.cookie.bus = periph->path->bus;
1742 cdm->pos.generations[CAM_BUS_GENERATION]=
1743 xsoftc.bus_generation;
1744 cdm->pos.cookie.target = periph->path->target;
1745 cdm->pos.generations[CAM_TARGET_GENERATION] =
1746 periph->path->bus->generation;
1747 cdm->pos.cookie.device = periph->path->device;
1748 cdm->pos.generations[CAM_DEV_GENERATION] =
1749 periph->path->target->generation;
1750 cdm->pos.cookie.periph = periph;
1751 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1752 periph->path->device->generation;
1753 cdm->status = CAM_DEV_MATCH_MORE;
1757 j = cdm->num_matches;
1759 cdm->matches[j].type = DEV_MATCH_PERIPH;
1760 cdm->matches[j].result.periph_result.path_id =
1761 periph->path->bus->path_id;
1762 cdm->matches[j].result.periph_result.target_id =
1763 periph->path->target->target_id;
1764 cdm->matches[j].result.periph_result.target_lun =
1765 periph->path->device->lun_id;
1766 cdm->matches[j].result.periph_result.unit_number =
1767 periph->unit_number;
1768 strncpy(cdm->matches[j].result.periph_result.periph_name,
1769 periph->periph_name, DEV_IDLEN);
1776 xptedtmatch(struct ccb_dev_match *cdm)
1780 cdm->num_matches = 0;
1783 * Check the bus list generation. If it has changed, the user
1784 * needs to reset everything and start over.
1786 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1787 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1788 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1789 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1793 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1794 && (cdm->pos.cookie.bus != NULL))
1795 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1796 xptedtbusfunc, cdm);
1798 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1801 * If we get back 0, that means that we had to stop before fully
1802 * traversing the EDT. It also means that one of the subroutines
1803 * has set the status field to the proper value. If we get back 1,
1804 * we've fully traversed the EDT and copied out any matching entries.
1807 cdm->status = CAM_DEV_MATCH_LAST;
1813 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1815 struct ccb_dev_match *cdm;
1817 cdm = (struct ccb_dev_match *)arg;
1819 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1820 && (cdm->pos.cookie.pdrv == pdrv)
1821 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1822 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1823 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1824 (*pdrv)->generation)) {
1825 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1829 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1830 && (cdm->pos.cookie.pdrv == pdrv)
1831 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1832 && (cdm->pos.cookie.periph != NULL))
1833 return(xptpdperiphtraverse(pdrv,
1834 (struct cam_periph *)cdm->pos.cookie.periph,
1835 xptplistperiphfunc, arg));
1837 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1841 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1843 struct ccb_dev_match *cdm;
1844 dev_match_ret retval;
1846 cdm = (struct ccb_dev_match *)arg;
1848 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1850 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1851 cdm->status = CAM_DEV_MATCH_ERROR;
1856 * If the copy flag is set, copy this peripheral out.
1858 if (retval & DM_RET_COPY) {
1861 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1862 sizeof(struct dev_match_result));
1865 * If we don't have enough space to put in another
1866 * match result, save our position and tell the
1867 * user there are more devices to check.
1869 if (spaceleft < sizeof(struct dev_match_result)) {
1870 struct periph_driver **pdrv;
1873 bzero(&cdm->pos, sizeof(cdm->pos));
1874 cdm->pos.position_type =
1875 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1879 * This may look a bit non-sensical, but it is
1880 * actually quite logical. There are very few
1881 * peripheral drivers, and bloating every peripheral
1882 * structure with a pointer back to its parent
1883 * peripheral driver linker set entry would cost
1884 * more in the long run than doing this quick lookup.
1886 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1887 if (strcmp((*pdrv)->driver_name,
1888 periph->periph_name) == 0)
1892 if (*pdrv == NULL) {
1893 cdm->status = CAM_DEV_MATCH_ERROR;
1897 cdm->pos.cookie.pdrv = pdrv;
1899 * The periph generation slot does double duty, as
1900 * does the periph pointer slot. They are used for
1901 * both edt and pdrv lookups and positioning.
1903 cdm->pos.cookie.periph = periph;
1904 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1905 (*pdrv)->generation;
1906 cdm->status = CAM_DEV_MATCH_MORE;
1910 j = cdm->num_matches;
1912 cdm->matches[j].type = DEV_MATCH_PERIPH;
1913 cdm->matches[j].result.periph_result.path_id =
1914 periph->path->bus->path_id;
1917 * The transport layer peripheral doesn't have a target or
1920 if (periph->path->target)
1921 cdm->matches[j].result.periph_result.target_id =
1922 periph->path->target->target_id;
1924 cdm->matches[j].result.periph_result.target_id = -1;
1926 if (periph->path->device)
1927 cdm->matches[j].result.periph_result.target_lun =
1928 periph->path->device->lun_id;
1930 cdm->matches[j].result.periph_result.target_lun = -1;
1932 cdm->matches[j].result.periph_result.unit_number =
1933 periph->unit_number;
1934 strncpy(cdm->matches[j].result.periph_result.periph_name,
1935 periph->periph_name, DEV_IDLEN);
1942 xptperiphlistmatch(struct ccb_dev_match *cdm)
1946 cdm->num_matches = 0;
1949 * At this point in the edt traversal function, we check the bus
1950 * list generation to make sure that no busses have been added or
1951 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1952 * For the peripheral driver list traversal function, however, we
1953 * don't have to worry about new peripheral driver types coming or
1954 * going; they're in a linker set, and therefore can't change
1955 * without a recompile.
1958 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1959 && (cdm->pos.cookie.pdrv != NULL))
1960 ret = xptpdrvtraverse(
1961 (struct periph_driver **)cdm->pos.cookie.pdrv,
1962 xptplistpdrvfunc, cdm);
1964 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
1967 * If we get back 0, that means that we had to stop before fully
1968 * traversing the peripheral driver tree. It also means that one of
1969 * the subroutines has set the status field to the proper value. If
1970 * we get back 1, we've fully traversed the EDT and copied out any
1974 cdm->status = CAM_DEV_MATCH_LAST;
1980 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
1982 struct cam_eb *bus, *next_bus;
1987 mtx_lock(&xsoftc.xpt_topo_lock);
1988 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
1995 * XXX The locking here is obviously very complex. We
1996 * should work to simplify it.
1998 mtx_unlock(&xsoftc.xpt_topo_lock);
1999 CAM_SIM_LOCK(bus->sim);
2000 retval = tr_func(bus, arg);
2001 CAM_SIM_UNLOCK(bus->sim);
2003 mtx_lock(&xsoftc.xpt_topo_lock);
2004 next_bus = TAILQ_NEXT(bus, links);
2005 mtx_unlock(&xsoftc.xpt_topo_lock);
2007 xpt_release_bus(bus);
2011 mtx_lock(&xsoftc.xpt_topo_lock);
2013 mtx_unlock(&xsoftc.xpt_topo_lock);
2019 xpt_sim_opened(struct cam_sim *sim)
2022 struct cam_et *target;
2023 struct cam_ed *device;
2024 struct cam_periph *periph;
2026 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2027 mtx_assert(sim->mtx, MA_OWNED);
2029 mtx_lock(&xsoftc.xpt_topo_lock);
2030 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2031 if (bus->sim != sim)
2034 TAILQ_FOREACH(target, &bus->et_entries, links) {
2035 TAILQ_FOREACH(device, &target->ed_entries, links) {
2036 SLIST_FOREACH(periph, &device->periphs,
2038 if (periph->refcount > 0) {
2039 mtx_unlock(&xsoftc.xpt_topo_lock);
2047 mtx_unlock(&xsoftc.xpt_topo_lock);
2052 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2053 xpt_targetfunc_t *tr_func, void *arg)
2055 struct cam_et *target, *next_target;
2059 for (target = (start_target ? start_target :
2060 TAILQ_FIRST(&bus->et_entries));
2061 target != NULL; target = next_target) {
2065 retval = tr_func(target, arg);
2067 next_target = TAILQ_NEXT(target, links);
2069 xpt_release_target(target);
2079 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2080 xpt_devicefunc_t *tr_func, void *arg)
2082 struct cam_ed *device, *next_device;
2086 for (device = (start_device ? start_device :
2087 TAILQ_FIRST(&target->ed_entries));
2089 device = next_device) {
2092 * Hold a reference so the current device does not go away
2097 retval = tr_func(device, arg);
2100 * Grab our next pointer before we release the current
2103 next_device = TAILQ_NEXT(device, links);
2105 xpt_release_device(device);
2115 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2116 xpt_periphfunc_t *tr_func, void *arg)
2118 struct cam_periph *periph, *next_periph;
2124 for (periph = (start_periph ? start_periph :
2125 SLIST_FIRST(&device->periphs));
2127 periph = next_periph) {
2131 * In this case, we want to show peripherals that have been
2132 * invalidated, but not peripherals that are scheduled to
2133 * be freed. So instead of calling cam_periph_acquire(),
2134 * which will fail if the periph has been invalidated, we
2135 * just check for the free flag here. If it is free, we
2136 * skip to the next periph.
2138 if (periph->flags & CAM_PERIPH_FREE) {
2139 next_periph = SLIST_NEXT(periph, periph_links);
2144 * Acquire a reference to this periph while we call the
2145 * traversal function, so it can't go away.
2151 retval = tr_func(periph, arg);
2154 * We need the lock for list traversal.
2159 * Grab the next peripheral before we release this one, so
2160 * our next pointer is still valid.
2162 next_periph = SLIST_NEXT(periph, periph_links);
2164 cam_periph_release_locked_buses(periph);
2178 xptpdrvtraverse(struct periph_driver **start_pdrv,
2179 xpt_pdrvfunc_t *tr_func, void *arg)
2181 struct periph_driver **pdrv;
2187 * We don't traverse the peripheral driver list like we do the
2188 * other lists, because it is a linker set, and therefore cannot be
2189 * changed during runtime. If the peripheral driver list is ever
2190 * re-done to be something other than a linker set (i.e. it can
2191 * change while the system is running), the list traversal should
2192 * be modified to work like the other traversal functions.
2194 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2195 *pdrv != NULL; pdrv++) {
2196 retval = tr_func(pdrv, arg);
2206 xptpdperiphtraverse(struct periph_driver **pdrv,
2207 struct cam_periph *start_periph,
2208 xpt_periphfunc_t *tr_func, void *arg)
2210 struct cam_periph *periph, *next_periph;
2216 for (periph = (start_periph ? start_periph :
2217 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2218 periph = next_periph) {
2222 * In this case, we want to show peripherals that have been
2223 * invalidated, but not peripherals that are scheduled to
2224 * be freed. So instead of calling cam_periph_acquire(),
2225 * which will fail if the periph has been invalidated, we
2226 * just check for the free flag here. If it is free, we
2227 * skip to the next periph.
2229 if (periph->flags & CAM_PERIPH_FREE) {
2230 next_periph = TAILQ_NEXT(periph, unit_links);
2235 * Acquire a reference to this periph while we call the
2236 * traversal function, so it can't go away.
2241 * XXX KDM we have the toplogy lock here, but in
2242 * xptperiphtraverse(), we drop it before calling the
2243 * traversal function. Which is correct?
2245 retval = tr_func(periph, arg);
2248 * Grab the next peripheral before we release this one, so
2249 * our next pointer is still valid.
2251 next_periph = TAILQ_NEXT(periph, unit_links);
2253 cam_periph_release_locked_buses(periph);
2266 xptdefbusfunc(struct cam_eb *bus, void *arg)
2268 struct xpt_traverse_config *tr_config;
2270 tr_config = (struct xpt_traverse_config *)arg;
2272 if (tr_config->depth == XPT_DEPTH_BUS) {
2273 xpt_busfunc_t *tr_func;
2275 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2277 return(tr_func(bus, tr_config->tr_arg));
2279 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2283 xptdeftargetfunc(struct cam_et *target, void *arg)
2285 struct xpt_traverse_config *tr_config;
2287 tr_config = (struct xpt_traverse_config *)arg;
2289 if (tr_config->depth == XPT_DEPTH_TARGET) {
2290 xpt_targetfunc_t *tr_func;
2292 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2294 return(tr_func(target, tr_config->tr_arg));
2296 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2300 xptdefdevicefunc(struct cam_ed *device, void *arg)
2302 struct xpt_traverse_config *tr_config;
2304 tr_config = (struct xpt_traverse_config *)arg;
2306 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2307 xpt_devicefunc_t *tr_func;
2309 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2311 return(tr_func(device, tr_config->tr_arg));
2313 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2317 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2319 struct xpt_traverse_config *tr_config;
2320 xpt_periphfunc_t *tr_func;
2322 tr_config = (struct xpt_traverse_config *)arg;
2324 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2327 * Unlike the other default functions, we don't check for depth
2328 * here. The peripheral driver level is the last level in the EDT,
2329 * so if we're here, we should execute the function in question.
2331 return(tr_func(periph, tr_config->tr_arg));
2335 * Execute the given function for every bus in the EDT.
2338 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2340 struct xpt_traverse_config tr_config;
2342 tr_config.depth = XPT_DEPTH_BUS;
2343 tr_config.tr_func = tr_func;
2344 tr_config.tr_arg = arg;
2346 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2350 * Execute the given function for every device in the EDT.
2353 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2355 struct xpt_traverse_config tr_config;
2357 tr_config.depth = XPT_DEPTH_DEVICE;
2358 tr_config.tr_func = tr_func;
2359 tr_config.tr_arg = arg;
2361 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2365 xptsetasyncfunc(struct cam_ed *device, void *arg)
2367 struct cam_path path;
2368 struct ccb_getdev cgd;
2369 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2372 * Don't report unconfigured devices (Wildcard devs,
2373 * devices only for target mode, device instances
2374 * that have been invalidated but are waiting for
2375 * their last reference count to be released).
2377 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2380 xpt_compile_path(&path,
2382 device->target->bus->path_id,
2383 device->target->target_id,
2385 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2386 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2387 xpt_action((union ccb *)&cgd);
2388 csa->callback(csa->callback_arg,
2391 xpt_release_path(&path);
2397 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2399 struct cam_path path;
2400 struct ccb_pathinq cpi;
2401 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2403 xpt_compile_path(&path, /*periph*/NULL,
2405 CAM_TARGET_WILDCARD,
2407 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2408 cpi.ccb_h.func_code = XPT_PATH_INQ;
2409 xpt_action((union ccb *)&cpi);
2410 csa->callback(csa->callback_arg,
2413 xpt_release_path(&path);
2419 xpt_action(union ccb *start_ccb)
2422 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2424 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2425 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2429 xpt_action_default(union ccb *start_ccb)
2431 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2432 struct cam_path *path;
2434 path = start_ccb->ccb_h.path;
2435 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2437 switch (start_ccb->ccb_h.func_code) {
2440 struct cam_ed *device;
2443 * For the sake of compatibility with SCSI-1
2444 * devices that may not understand the identify
2445 * message, we include lun information in the
2446 * second byte of all commands. SCSI-1 specifies
2447 * that luns are a 3 bit value and reserves only 3
2448 * bits for lun information in the CDB. Later
2449 * revisions of the SCSI spec allow for more than 8
2450 * luns, but have deprecated lun information in the
2451 * CDB. So, if the lun won't fit, we must omit.
2453 * Also be aware that during initial probing for devices,
2454 * the inquiry information is unknown but initialized to 0.
2455 * This means that this code will be exercised while probing
2456 * devices with an ANSI revision greater than 2.
2458 device = path->device;
2459 if (device->protocol_version <= SCSI_REV_2
2460 && start_ccb->ccb_h.target_lun < 8
2461 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2463 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2464 start_ccb->ccb_h.target_lun << 5;
2466 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2467 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2468 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2469 &path->device->inq_data),
2470 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2471 cdb_str, sizeof(cdb_str))));
2475 case XPT_CONT_TARGET_IO:
2476 start_ccb->csio.sense_resid = 0;
2477 start_ccb->csio.resid = 0;
2480 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2481 start_ccb->ataio.resid = 0;
2482 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2483 ata_op_string(&start_ccb->ataio.cmd),
2484 ata_cmd_string(&start_ccb->ataio.cmd,
2485 cdb_str, sizeof(cdb_str))));
2493 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2494 path->device->sim->devq->alloc_openings += frozen;
2496 xpt_run_dev_allocq(path->bus);
2497 if (xpt_schedule_dev_sendq(path->bus, path->device))
2498 xpt_run_dev_sendq(path->bus);
2501 case XPT_CALC_GEOMETRY:
2503 struct cam_sim *sim;
2505 /* Filter out garbage */
2506 if (start_ccb->ccg.block_size == 0
2507 || start_ccb->ccg.volume_size == 0) {
2508 start_ccb->ccg.cylinders = 0;
2509 start_ccb->ccg.heads = 0;
2510 start_ccb->ccg.secs_per_track = 0;
2511 start_ccb->ccb_h.status = CAM_REQ_CMP;
2514 #if defined(PC98) || defined(__sparc64__)
2516 * In a PC-98 system, geometry translation depens on
2517 * the "real" device geometry obtained from mode page 4.
2518 * SCSI geometry translation is performed in the
2519 * initialization routine of the SCSI BIOS and the result
2520 * stored in host memory. If the translation is available
2521 * in host memory, use it. If not, rely on the default
2522 * translation the device driver performs.
2523 * For sparc64, we may need adjust the geometry of large
2524 * disks in order to fit the limitations of the 16-bit
2525 * fields of the VTOC8 disk label.
2527 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2528 start_ccb->ccb_h.status = CAM_REQ_CMP;
2532 sim = path->bus->sim;
2533 (*(sim->sim_action))(sim, start_ccb);
2538 union ccb* abort_ccb;
2540 abort_ccb = start_ccb->cab.abort_ccb;
2541 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2543 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2544 struct cam_ccbq *ccbq;
2545 struct cam_ed *device;
2547 device = abort_ccb->ccb_h.path->device;
2548 ccbq = &device->ccbq;
2549 device->sim->devq->alloc_openings -=
2550 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2551 abort_ccb->ccb_h.status =
2552 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2553 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2554 xpt_done(abort_ccb);
2555 start_ccb->ccb_h.status = CAM_REQ_CMP;
2558 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2559 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2561 * We've caught this ccb en route to
2562 * the SIM. Flag it for abort and the
2563 * SIM will do so just before starting
2564 * real work on the CCB.
2566 abort_ccb->ccb_h.status =
2567 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2568 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2569 start_ccb->ccb_h.status = CAM_REQ_CMP;
2573 if (XPT_FC_IS_QUEUED(abort_ccb)
2574 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2576 * It's already completed but waiting
2577 * for our SWI to get to it.
2579 start_ccb->ccb_h.status = CAM_UA_ABORT;
2583 * If we weren't able to take care of the abort request
2584 * in the XPT, pass the request down to the SIM for processing.
2588 case XPT_ACCEPT_TARGET_IO:
2590 case XPT_IMMED_NOTIFY:
2591 case XPT_NOTIFY_ACK:
2593 case XPT_IMMEDIATE_NOTIFY:
2594 case XPT_NOTIFY_ACKNOWLEDGE:
2595 case XPT_GET_SIM_KNOB:
2596 case XPT_SET_SIM_KNOB:
2598 struct cam_sim *sim;
2600 sim = path->bus->sim;
2601 (*(sim->sim_action))(sim, start_ccb);
2606 struct cam_sim *sim;
2608 sim = path->bus->sim;
2609 (*(sim->sim_action))(sim, start_ccb);
2612 case XPT_PATH_STATS:
2613 start_ccb->cpis.last_reset = path->bus->last_reset;
2614 start_ccb->ccb_h.status = CAM_REQ_CMP;
2621 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2622 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2624 struct ccb_getdev *cgd;
2626 cgd = &start_ccb->cgd;
2627 cgd->protocol = dev->protocol;
2628 cgd->inq_data = dev->inq_data;
2629 cgd->ident_data = dev->ident_data;
2630 cgd->inq_flags = dev->inq_flags;
2631 cgd->ccb_h.status = CAM_REQ_CMP;
2632 cgd->serial_num_len = dev->serial_num_len;
2633 if ((dev->serial_num_len > 0)
2634 && (dev->serial_num != NULL))
2635 bcopy(dev->serial_num, cgd->serial_num,
2636 dev->serial_num_len);
2640 case XPT_GDEV_STATS:
2645 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2646 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2648 struct ccb_getdevstats *cgds;
2652 cgds = &start_ccb->cgds;
2655 cgds->dev_openings = dev->ccbq.dev_openings;
2656 cgds->dev_active = dev->ccbq.dev_active;
2657 cgds->devq_openings = dev->ccbq.devq_openings;
2658 cgds->devq_queued = dev->ccbq.queue.entries;
2659 cgds->held = dev->ccbq.held;
2660 cgds->last_reset = tar->last_reset;
2661 cgds->maxtags = dev->maxtags;
2662 cgds->mintags = dev->mintags;
2663 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2664 cgds->last_reset = bus->last_reset;
2665 cgds->ccb_h.status = CAM_REQ_CMP;
2671 struct cam_periph *nperiph;
2672 struct periph_list *periph_head;
2673 struct ccb_getdevlist *cgdl;
2675 struct cam_ed *device;
2682 * Don't want anyone mucking with our data.
2684 device = path->device;
2685 periph_head = &device->periphs;
2686 cgdl = &start_ccb->cgdl;
2689 * Check and see if the list has changed since the user
2690 * last requested a list member. If so, tell them that the
2691 * list has changed, and therefore they need to start over
2692 * from the beginning.
2694 if ((cgdl->index != 0) &&
2695 (cgdl->generation != device->generation)) {
2696 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2701 * Traverse the list of peripherals and attempt to find
2702 * the requested peripheral.
2704 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2705 (nperiph != NULL) && (i <= cgdl->index);
2706 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2707 if (i == cgdl->index) {
2708 strncpy(cgdl->periph_name,
2709 nperiph->periph_name,
2711 cgdl->unit_number = nperiph->unit_number;
2716 cgdl->status = CAM_GDEVLIST_ERROR;
2720 if (nperiph == NULL)
2721 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2723 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2726 cgdl->generation = device->generation;
2728 cgdl->ccb_h.status = CAM_REQ_CMP;
2733 dev_pos_type position_type;
2734 struct ccb_dev_match *cdm;
2736 cdm = &start_ccb->cdm;
2739 * There are two ways of getting at information in the EDT.
2740 * The first way is via the primary EDT tree. It starts
2741 * with a list of busses, then a list of targets on a bus,
2742 * then devices/luns on a target, and then peripherals on a
2743 * device/lun. The "other" way is by the peripheral driver
2744 * lists. The peripheral driver lists are organized by
2745 * peripheral driver. (obviously) So it makes sense to
2746 * use the peripheral driver list if the user is looking
2747 * for something like "da1", or all "da" devices. If the
2748 * user is looking for something on a particular bus/target
2749 * or lun, it's generally better to go through the EDT tree.
2752 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2753 position_type = cdm->pos.position_type;
2757 position_type = CAM_DEV_POS_NONE;
2759 for (i = 0; i < cdm->num_patterns; i++) {
2760 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2761 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2762 position_type = CAM_DEV_POS_EDT;
2767 if (cdm->num_patterns == 0)
2768 position_type = CAM_DEV_POS_EDT;
2769 else if (position_type == CAM_DEV_POS_NONE)
2770 position_type = CAM_DEV_POS_PDRV;
2773 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2774 case CAM_DEV_POS_EDT:
2777 case CAM_DEV_POS_PDRV:
2778 xptperiphlistmatch(cdm);
2781 cdm->status = CAM_DEV_MATCH_ERROR;
2785 if (cdm->status == CAM_DEV_MATCH_ERROR)
2786 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2788 start_ccb->ccb_h.status = CAM_REQ_CMP;
2794 struct ccb_setasync *csa;
2795 struct async_node *cur_entry;
2796 struct async_list *async_head;
2799 csa = &start_ccb->csa;
2800 added = csa->event_enable;
2801 async_head = &path->device->asyncs;
2804 * If there is already an entry for us, simply
2807 cur_entry = SLIST_FIRST(async_head);
2808 while (cur_entry != NULL) {
2809 if ((cur_entry->callback_arg == csa->callback_arg)
2810 && (cur_entry->callback == csa->callback))
2812 cur_entry = SLIST_NEXT(cur_entry, links);
2815 if (cur_entry != NULL) {
2817 * If the request has no flags set,
2820 added &= ~cur_entry->event_enable;
2821 if (csa->event_enable == 0) {
2822 SLIST_REMOVE(async_head, cur_entry,
2824 xpt_release_device(path->device);
2825 free(cur_entry, M_CAMXPT);
2827 cur_entry->event_enable = csa->event_enable;
2829 csa->event_enable = added;
2831 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2833 if (cur_entry == NULL) {
2834 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2837 cur_entry->event_enable = csa->event_enable;
2838 cur_entry->callback_arg = csa->callback_arg;
2839 cur_entry->callback = csa->callback;
2840 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2841 xpt_acquire_device(path->device);
2843 start_ccb->ccb_h.status = CAM_REQ_CMP;
2848 struct ccb_relsim *crs;
2851 crs = &start_ccb->crs;
2855 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2859 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2861 /* Don't ever go below one opening */
2862 if (crs->openings > 0) {
2863 xpt_dev_ccbq_resize(path, crs->openings);
2866 "number of openings is now %d\n",
2872 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2874 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2877 * Just extend the old timeout and decrement
2878 * the freeze count so that a single timeout
2879 * is sufficient for releasing the queue.
2881 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2882 callout_stop(&dev->callout);
2885 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2888 callout_reset(&dev->callout,
2889 (crs->release_timeout * hz) / 1000,
2890 xpt_release_devq_timeout, dev);
2892 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2896 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2898 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2900 * Decrement the freeze count so that a single
2901 * completion is still sufficient to unfreeze
2904 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2907 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2908 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2912 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2914 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2915 || (dev->ccbq.dev_active == 0)) {
2917 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2920 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2921 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2925 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2926 xpt_release_devq_rl(path, /*runlevel*/
2927 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2928 crs->release_timeout : 0,
2929 /*count*/1, /*run_queue*/TRUE);
2931 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2932 start_ccb->ccb_h.status = CAM_REQ_CMP;
2936 /* Check that all request bits are supported. */
2937 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2938 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2942 cam_dflags = start_ccb->cdbg.flags;
2943 if (cam_dpath != NULL) {
2944 xpt_free_path(cam_dpath);
2947 if (cam_dflags != CAM_DEBUG_NONE) {
2948 if (xpt_create_path(&cam_dpath, xpt_periph,
2949 start_ccb->ccb_h.path_id,
2950 start_ccb->ccb_h.target_id,
2951 start_ccb->ccb_h.target_lun) !=
2953 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2954 cam_dflags = CAM_DEBUG_NONE;
2956 start_ccb->ccb_h.status = CAM_REQ_CMP;
2957 xpt_print(cam_dpath, "debugging flags now %x\n",
2962 start_ccb->ccb_h.status = CAM_REQ_CMP;
2966 case XPT_FREEZE_QUEUE:
2968 struct ccb_relsim *crs = &start_ccb->crs;
2970 xpt_freeze_devq_rl(path, /*runlevel*/
2971 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2972 crs->release_timeout : 0, /*count*/1);
2973 start_ccb->ccb_h.status = CAM_REQ_CMP;
2977 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2978 xpt_freeze_devq(path, 1);
2979 start_ccb->ccb_h.status = CAM_REQ_CMP;
2986 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2987 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2988 xpt_done(start_ccb);
2995 xpt_polled_action(union ccb *start_ccb)
2998 struct cam_sim *sim;
2999 struct cam_devq *devq;
3003 timeout = start_ccb->ccb_h.timeout * 10;
3004 sim = start_ccb->ccb_h.path->bus->sim;
3006 dev = start_ccb->ccb_h.path->device;
3008 mtx_assert(sim->mtx, MA_OWNED);
3010 /* Don't use ISR for this SIM while polling. */
3011 sim->flags |= CAM_SIM_POLLED;
3014 * Steal an opening so that no other queued requests
3015 * can get it before us while we simulate interrupts.
3017 dev->ccbq.devq_openings--;
3018 dev->ccbq.dev_openings--;
3020 while(((devq != NULL && devq->send_openings <= 0) ||
3021 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3023 (*(sim->sim_poll))(sim);
3024 camisr_runqueue(&sim->sim_doneq);
3027 dev->ccbq.devq_openings++;
3028 dev->ccbq.dev_openings++;
3031 xpt_action(start_ccb);
3032 while(--timeout > 0) {
3033 (*(sim->sim_poll))(sim);
3034 camisr_runqueue(&sim->sim_doneq);
3035 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3042 * XXX Is it worth adding a sim_timeout entry
3043 * point so we can attempt recovery? If
3044 * this is only used for dumps, I don't think
3047 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3050 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3053 /* We will use CAM ISR for this SIM again. */
3054 sim->flags &= ~CAM_SIM_POLLED;
3058 * Schedule a peripheral driver to receive a ccb when it's
3059 * target device has space for more transactions.
3062 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3064 struct cam_ed *device;
3067 mtx_assert(perph->sim->mtx, MA_OWNED);
3069 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3070 device = perph->path->device;
3071 if (periph_is_queued(perph)) {
3072 /* Simply reorder based on new priority */
3073 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3074 (" change priority to %d\n", new_priority));
3075 if (new_priority < perph->pinfo.priority) {
3076 camq_change_priority(&device->drvq,
3079 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3082 /* New entry on the queue */
3083 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3084 (" added periph to queue\n"));
3085 perph->pinfo.priority = new_priority;
3086 perph->pinfo.generation = ++device->drvq.generation;
3087 camq_insert(&device->drvq, &perph->pinfo);
3088 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3091 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3092 (" calling xpt_run_devq\n"));
3093 xpt_run_dev_allocq(perph->path->bus);
3099 * Schedule a device to run on a given queue.
3100 * If the device was inserted as a new entry on the queue,
3101 * return 1 meaning the device queue should be run. If we
3102 * were already queued, implying someone else has already
3103 * started the queue, return 0 so the caller doesn't attempt
3107 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3108 u_int32_t new_priority)
3111 u_int32_t old_priority;
3113 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3115 old_priority = pinfo->priority;
3118 * Are we already queued?
3120 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3121 /* Simply reorder based on new priority */
3122 if (new_priority < old_priority) {
3123 camq_change_priority(queue, pinfo->index,
3125 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3126 ("changed priority to %d\n",
3132 /* New entry on the queue */
3133 if (new_priority < old_priority)
3134 pinfo->priority = new_priority;
3136 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3137 ("Inserting onto queue\n"));
3138 pinfo->generation = ++queue->generation;
3139 camq_insert(queue, pinfo);
3146 xpt_run_dev_allocq(struct cam_eb *bus)
3148 struct cam_devq *devq;
3150 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3151 devq = bus->sim->devq;
3153 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3154 (" qfrozen_cnt == 0x%x, entries == %d, "
3155 "openings == %d, active == %d\n",
3156 devq->alloc_queue.qfrozen_cnt[0],
3157 devq->alloc_queue.entries,
3158 devq->alloc_openings,
3159 devq->alloc_active));
3161 devq->alloc_queue.qfrozen_cnt[0]++;
3162 while ((devq->alloc_queue.entries > 0)
3163 && (devq->alloc_openings > 0)
3164 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3165 struct cam_ed_qinfo *qinfo;
3166 struct cam_ed *device;
3167 union ccb *work_ccb;
3168 struct cam_periph *drv;
3171 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3173 device = qinfo->device;
3174 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3175 ("running device %p\n", device));
3177 drvq = &device->drvq;
3178 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3179 "Device on queue without any work to do"));
3180 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3181 devq->alloc_openings--;
3182 devq->alloc_active++;
3183 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3184 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3185 drv->pinfo.priority);
3186 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3187 ("calling periph start\n"));
3188 drv->periph_start(drv, work_ccb);
3191 * Malloc failure in alloc_ccb
3194 * XXX add us to a list to be run from free_ccb
3195 * if we don't have any ccbs active on this
3196 * device queue otherwise we may never get run
3202 /* We may have more work. Attempt to reschedule. */
3203 xpt_schedule_dev_allocq(bus, device);
3205 devq->alloc_queue.qfrozen_cnt[0]--;
3209 xpt_run_dev_sendq(struct cam_eb *bus)
3211 struct cam_devq *devq;
3213 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3215 devq = bus->sim->devq;
3217 devq->send_queue.qfrozen_cnt[0]++;
3218 while ((devq->send_queue.entries > 0)
3219 && (devq->send_openings > 0)
3220 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3221 struct cam_ed_qinfo *qinfo;
3222 struct cam_ed *device;
3223 union ccb *work_ccb;
3224 struct cam_sim *sim;
3226 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3228 device = qinfo->device;
3229 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3230 ("running device %p\n", device));
3232 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3233 if (work_ccb == NULL) {
3234 printf("device on run queue with no ccbs???\n");
3238 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3240 mtx_lock(&xsoftc.xpt_lock);
3241 if (xsoftc.num_highpower <= 0) {
3243 * We got a high power command, but we
3244 * don't have any available slots. Freeze
3245 * the device queue until we have a slot
3248 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3249 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3253 mtx_unlock(&xsoftc.xpt_lock);
3257 * Consume a high power slot while
3260 xsoftc.num_highpower--;
3262 mtx_unlock(&xsoftc.xpt_lock);
3264 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3265 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3267 devq->send_openings--;
3268 devq->send_active++;
3270 xpt_schedule_dev_sendq(bus, device);
3272 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3274 * The client wants to freeze the queue
3275 * after this CCB is sent.
3277 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3280 /* In Target mode, the peripheral driver knows best... */
3281 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3282 if ((device->inq_flags & SID_CmdQue) != 0
3283 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3284 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3287 * Clear this in case of a retried CCB that
3288 * failed due to a rejected tag.
3290 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3294 * Device queues can be shared among multiple sim instances
3295 * that reside on different busses. Use the SIM in the queue
3296 * CCB's path, rather than the one in the bus that was passed
3297 * into this function.
3299 sim = work_ccb->ccb_h.path->bus->sim;
3300 (*(sim->sim_action))(sim, work_ccb);
3302 devq->send_queue.qfrozen_cnt[0]--;
3306 * This function merges stuff from the slave ccb into the master ccb, while
3307 * keeping important fields in the master ccb constant.
3310 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3314 * Pull fields that are valid for peripheral drivers to set
3315 * into the master CCB along with the CCB "payload".
3317 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3318 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3319 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3320 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3321 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3322 sizeof(union ccb) - sizeof(struct ccb_hdr));
3326 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3329 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3330 ccb_h->pinfo.priority = priority;
3332 ccb_h->path_id = path->bus->path_id;
3334 ccb_h->target_id = path->target->target_id;
3336 ccb_h->target_id = CAM_TARGET_WILDCARD;
3338 ccb_h->target_lun = path->device->lun_id;
3339 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3341 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3343 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3347 /* Path manipulation functions */
3349 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3350 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3352 struct cam_path *path;
3355 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3358 status = CAM_RESRC_UNAVAIL;
3361 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3362 if (status != CAM_REQ_CMP) {
3363 free(path, M_CAMXPT);
3366 *new_path_ptr = path;
3371 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3372 struct cam_periph *periph, path_id_t path_id,
3373 target_id_t target_id, lun_id_t lun_id)
3375 struct cam_path *path;
3376 struct cam_eb *bus = NULL;
3378 int need_unlock = 0;
3380 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3382 if (path_id != CAM_BUS_WILDCARD) {
3383 bus = xpt_find_bus(path_id);
3386 CAM_SIM_LOCK(bus->sim);
3389 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3391 CAM_SIM_UNLOCK(bus->sim);
3392 xpt_release_bus(bus);
3394 if (status != CAM_REQ_CMP) {
3395 free(path, M_CAMXPT);
3398 *new_path_ptr = path;
3403 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3404 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3407 struct cam_et *target;
3408 struct cam_ed *device;
3411 status = CAM_REQ_CMP; /* Completed without error */
3412 target = NULL; /* Wildcarded */
3413 device = NULL; /* Wildcarded */
3416 * We will potentially modify the EDT, so block interrupts
3417 * that may attempt to create cam paths.
3419 bus = xpt_find_bus(path_id);
3421 status = CAM_PATH_INVALID;
3423 target = xpt_find_target(bus, target_id);
3424 if (target == NULL) {
3426 struct cam_et *new_target;
3428 new_target = xpt_alloc_target(bus, target_id);
3429 if (new_target == NULL) {
3430 status = CAM_RESRC_UNAVAIL;
3432 target = new_target;
3435 if (target != NULL) {
3436 device = xpt_find_device(target, lun_id);
3437 if (device == NULL) {
3439 struct cam_ed *new_device;
3442 (*(bus->xport->alloc_device))(bus,
3445 if (new_device == NULL) {
3446 status = CAM_RESRC_UNAVAIL;
3448 device = new_device;
3455 * Only touch the user's data if we are successful.
3457 if (status == CAM_REQ_CMP) {
3458 new_path->periph = perph;
3459 new_path->bus = bus;
3460 new_path->target = target;
3461 new_path->device = device;
3462 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3465 xpt_release_device(device);
3467 xpt_release_target(target);
3469 xpt_release_bus(bus);
3475 xpt_release_path(struct cam_path *path)
3477 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3478 if (path->device != NULL) {
3479 xpt_release_device(path->device);
3480 path->device = NULL;
3482 if (path->target != NULL) {
3483 xpt_release_target(path->target);
3484 path->target = NULL;
3486 if (path->bus != NULL) {
3487 xpt_release_bus(path->bus);
3493 xpt_free_path(struct cam_path *path)
3496 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3497 xpt_release_path(path);
3498 free(path, M_CAMXPT);
3502 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3503 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3506 mtx_lock(&xsoftc.xpt_topo_lock);
3509 *bus_ref = path->bus->refcount;
3513 mtx_unlock(&xsoftc.xpt_topo_lock);
3516 *periph_ref = path->periph->refcount;
3522 *target_ref = path->target->refcount;
3528 *device_ref = path->device->refcount;
3535 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3536 * in path1, 2 for match with wildcards in path2.
3539 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3543 if (path1->bus != path2->bus) {
3544 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3546 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3551 if (path1->target != path2->target) {
3552 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3555 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3560 if (path1->device != path2->device) {
3561 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3564 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3573 xpt_print_path(struct cam_path *path)
3577 printf("(nopath): ");
3579 if (path->periph != NULL)
3580 printf("(%s%d:", path->periph->periph_name,
3581 path->periph->unit_number);
3583 printf("(noperiph:");
3585 if (path->bus != NULL)
3586 printf("%s%d:%d:", path->bus->sim->sim_name,
3587 path->bus->sim->unit_number,
3588 path->bus->sim->bus_id);
3592 if (path->target != NULL)
3593 printf("%d:", path->target->target_id);
3597 if (path->device != NULL)
3598 printf("%d): ", path->device->lun_id);
3605 xpt_print(struct cam_path *path, const char *fmt, ...)
3608 xpt_print_path(path);
3615 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3620 if (path != NULL && path->bus != NULL)
3621 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3624 sbuf_new(&sb, str, str_len, 0);
3627 sbuf_printf(&sb, "(nopath): ");
3629 if (path->periph != NULL)
3630 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3631 path->periph->unit_number);
3633 sbuf_printf(&sb, "(noperiph:");
3635 if (path->bus != NULL)
3636 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3637 path->bus->sim->unit_number,
3638 path->bus->sim->bus_id);
3640 sbuf_printf(&sb, "nobus:");
3642 if (path->target != NULL)
3643 sbuf_printf(&sb, "%d:", path->target->target_id);
3645 sbuf_printf(&sb, "X:");
3647 if (path->device != NULL)
3648 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3650 sbuf_printf(&sb, "X): ");
3654 return(sbuf_len(&sb));
3658 xpt_path_path_id(struct cam_path *path)
3660 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3662 return(path->bus->path_id);
3666 xpt_path_target_id(struct cam_path *path)
3668 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3670 if (path->target != NULL)
3671 return (path->target->target_id);
3673 return (CAM_TARGET_WILDCARD);
3677 xpt_path_lun_id(struct cam_path *path)
3679 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3681 if (path->device != NULL)
3682 return (path->device->lun_id);
3684 return (CAM_LUN_WILDCARD);
3688 xpt_path_sim(struct cam_path *path)
3691 return (path->bus->sim);
3695 xpt_path_periph(struct cam_path *path)
3697 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3699 return (path->periph);
3703 xpt_path_legacy_ata_id(struct cam_path *path)
3708 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3709 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3710 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3711 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3714 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3715 path->bus->sim->unit_number < 2) {
3716 bus_id = path->bus->sim->unit_number;
3720 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3721 if (bus == path->bus)
3723 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3724 bus->sim->unit_number >= 2) ||
3725 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3726 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3727 strcmp(bus->sim->sim_name, "siisch") == 0)
3732 if (path->target != NULL) {
3733 if (path->target->target_id < 2)
3734 return (bus_id * 2 + path->target->target_id);
3738 return (bus_id * 2);
3742 * Release a CAM control block for the caller. Remit the cost of the structure
3743 * to the device referenced by the path. If the this device had no 'credits'
3744 * and peripheral drivers have registered async callbacks for this notification
3748 xpt_release_ccb(union ccb *free_ccb)
3750 struct cam_path *path;
3751 struct cam_ed *device;
3753 struct cam_sim *sim;
3755 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3756 path = free_ccb->ccb_h.path;
3757 device = path->device;
3761 mtx_assert(sim->mtx, MA_OWNED);
3763 cam_ccbq_release_opening(&device->ccbq);
3764 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3765 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3766 cam_ccbq_resize(&device->ccbq,
3767 device->ccbq.dev_openings + device->ccbq.dev_active);
3769 if (sim->ccb_count > sim->max_ccbs) {
3770 xpt_free_ccb(free_ccb);
3773 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3776 if (sim->devq == NULL) {
3779 sim->devq->alloc_openings++;
3780 sim->devq->alloc_active--;
3781 if (device_is_alloc_queued(device) == 0)
3782 xpt_schedule_dev_allocq(bus, device);
3783 xpt_run_dev_allocq(bus);
3786 /* Functions accessed by SIM drivers */
3788 static struct xpt_xport xport_default = {
3789 .alloc_device = xpt_alloc_device_default,
3790 .action = xpt_action_default,
3791 .async = xpt_dev_async_default,
3795 * A sim structure, listing the SIM entry points and instance
3796 * identification info is passed to xpt_bus_register to hook the SIM
3797 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3798 * for this new bus and places it in the array of busses and assigns
3799 * it a path_id. The path_id may be influenced by "hard wiring"
3800 * information specified by the user. Once interrupt services are
3801 * available, the bus will be probed.
3804 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3806 struct cam_eb *new_bus;
3807 struct cam_eb *old_bus;
3808 struct ccb_pathinq cpi;
3809 struct cam_path *path;
3812 mtx_assert(sim->mtx, MA_OWNED);
3815 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3816 M_CAMXPT, M_NOWAIT);
3817 if (new_bus == NULL) {
3818 /* Couldn't satisfy request */
3819 return (CAM_RESRC_UNAVAIL);
3821 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3823 free(new_bus, M_CAMXPT);
3824 return (CAM_RESRC_UNAVAIL);
3827 if (strcmp(sim->sim_name, "xpt") != 0) {
3829 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3832 TAILQ_INIT(&new_bus->et_entries);
3833 new_bus->path_id = sim->path_id;
3836 timevalclear(&new_bus->last_reset);
3838 new_bus->refcount = 1; /* Held until a bus_deregister event */
3839 new_bus->generation = 0;
3841 mtx_lock(&xsoftc.xpt_topo_lock);
3842 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3843 while (old_bus != NULL
3844 && old_bus->path_id < new_bus->path_id)
3845 old_bus = TAILQ_NEXT(old_bus, links);
3846 if (old_bus != NULL)
3847 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3849 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3850 xsoftc.bus_generation++;
3851 mtx_unlock(&xsoftc.xpt_topo_lock);
3854 * Set a default transport so that a PATH_INQ can be issued to
3855 * the SIM. This will then allow for probing and attaching of
3856 * a more appropriate transport.
3858 new_bus->xport = &xport_default;
3860 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3861 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3862 if (status != CAM_REQ_CMP)
3863 printf("xpt_compile_path returned %d\n", status);
3865 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3866 cpi.ccb_h.func_code = XPT_PATH_INQ;
3867 xpt_action((union ccb *)&cpi);
3869 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3870 switch (cpi.transport) {
3877 new_bus->xport = scsi_get_xport();
3881 new_bus->xport = ata_get_xport();
3884 new_bus->xport = &xport_default;
3889 /* Notify interested parties */
3890 if (sim->path_id != CAM_XPT_PATH_ID) {
3891 union ccb *scan_ccb;
3893 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3894 /* Initiate bus rescan. */
3895 scan_ccb = xpt_alloc_ccb_nowait();
3896 scan_ccb->ccb_h.path = path;
3897 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3898 scan_ccb->crcn.flags = 0;
3899 xpt_rescan(scan_ccb);
3901 xpt_free_path(path);
3902 return (CAM_SUCCESS);
3906 xpt_bus_deregister(path_id_t pathid)
3908 struct cam_path bus_path;
3911 status = xpt_compile_path(&bus_path, NULL, pathid,
3912 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3913 if (status != CAM_REQ_CMP)
3916 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3917 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3919 /* Release the reference count held while registered. */
3920 xpt_release_bus(bus_path.bus);
3921 xpt_release_path(&bus_path);
3923 return (CAM_REQ_CMP);
3927 xptnextfreepathid(void)
3934 mtx_lock(&xsoftc.xpt_topo_lock);
3935 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3937 /* Find an unoccupied pathid */
3938 while (bus != NULL && bus->path_id <= pathid) {
3939 if (bus->path_id == pathid)
3941 bus = TAILQ_NEXT(bus, links);
3943 mtx_unlock(&xsoftc.xpt_topo_lock);
3946 * Ensure that this pathid is not reserved for
3947 * a bus that may be registered in the future.
3949 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3951 /* Start the search over */
3952 mtx_lock(&xsoftc.xpt_topo_lock);
3959 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3966 pathid = CAM_XPT_PATH_ID;
3967 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3969 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3970 if (strcmp(dname, "scbus")) {
3971 /* Avoid a bit of foot shooting. */
3974 if (dunit < 0) /* unwired?! */
3976 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3977 if (sim_bus == val) {
3981 } else if (sim_bus == 0) {
3982 /* Unspecified matches bus 0 */
3986 printf("Ambiguous scbus configuration for %s%d "
3987 "bus %d, cannot wire down. The kernel "
3988 "config entry for scbus%d should "
3989 "specify a controller bus.\n"
3990 "Scbus will be assigned dynamically.\n",
3991 sim_name, sim_unit, sim_bus, dunit);
3996 if (pathid == CAM_XPT_PATH_ID)
3997 pathid = xptnextfreepathid();
4002 xpt_async_string(u_int32_t async_code)
4005 switch (async_code) {
4006 case AC_BUS_RESET: return ("AC_BUS_RESET");
4007 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4008 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4009 case AC_SENT_BDR: return ("AC_SENT_BDR");
4010 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4011 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4012 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4013 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4014 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4015 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4016 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4017 case AC_CONTRACT: return ("AC_CONTRACT");
4019 return ("AC_UNKNOWN");
4023 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4026 struct cam_et *target, *next_target;
4027 struct cam_ed *device, *next_device;
4029 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4030 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4031 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4034 * Most async events come from a CAM interrupt context. In
4035 * a few cases, the error recovery code at the peripheral layer,
4036 * which may run from our SWI or a process context, may signal
4037 * deferred events with a call to xpt_async.
4042 if (async_code == AC_BUS_RESET) {
4043 /* Update our notion of when the last reset occurred */
4044 microtime(&bus->last_reset);
4047 for (target = TAILQ_FIRST(&bus->et_entries);
4049 target = next_target) {
4051 next_target = TAILQ_NEXT(target, links);
4053 if (path->target != target
4054 && path->target->target_id != CAM_TARGET_WILDCARD
4055 && target->target_id != CAM_TARGET_WILDCARD)
4058 if (async_code == AC_SENT_BDR) {
4059 /* Update our notion of when the last reset occurred */
4060 microtime(&path->target->last_reset);
4063 for (device = TAILQ_FIRST(&target->ed_entries);
4065 device = next_device) {
4067 next_device = TAILQ_NEXT(device, links);
4069 if (path->device != device
4070 && path->device->lun_id != CAM_LUN_WILDCARD
4071 && device->lun_id != CAM_LUN_WILDCARD)
4074 * The async callback could free the device.
4075 * If it is a broadcast async, it doesn't hold
4076 * device reference, so take our own reference.
4078 xpt_acquire_device(device);
4079 (*(bus->xport->async))(async_code, bus,
4083 xpt_async_bcast(&device->asyncs, async_code,
4085 xpt_release_device(device);
4090 * If this wasn't a fully wildcarded async, tell all
4091 * clients that want all async events.
4093 if (bus != xpt_periph->path->bus)
4094 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4099 xpt_async_bcast(struct async_list *async_head,
4100 u_int32_t async_code,
4101 struct cam_path *path, void *async_arg)
4103 struct async_node *cur_entry;
4105 cur_entry = SLIST_FIRST(async_head);
4106 while (cur_entry != NULL) {
4107 struct async_node *next_entry;
4109 * Grab the next list entry before we call the current
4110 * entry's callback. This is because the callback function
4111 * can delete its async callback entry.
4113 next_entry = SLIST_NEXT(cur_entry, links);
4114 if ((cur_entry->event_enable & async_code) != 0)
4115 cur_entry->callback(cur_entry->callback_arg,
4118 cur_entry = next_entry;
4123 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4124 struct cam_et *target, struct cam_ed *device,
4127 printf("%s called\n", __func__);
4131 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4133 struct cam_ed *dev = path->device;
4135 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4136 dev->sim->devq->alloc_openings +=
4137 cam_ccbq_freeze(&dev->ccbq, rl, count);
4138 /* Remove frozen device from allocq. */
4139 if (device_is_alloc_queued(dev) &&
4140 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4141 CAMQ_GET_PRIO(&dev->drvq)))) {
4142 camq_remove(&dev->sim->devq->alloc_queue,
4143 dev->alloc_ccb_entry.pinfo.index);
4145 /* Remove frozen device from sendq. */
4146 if (device_is_send_queued(dev) &&
4147 cam_ccbq_frozen_top(&dev->ccbq)) {
4148 camq_remove(&dev->sim->devq->send_queue,
4149 dev->send_ccb_entry.pinfo.index);
4151 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4155 xpt_freeze_devq(struct cam_path *path, u_int count)
4158 return (xpt_freeze_devq_rl(path, 0, count));
4162 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4165 mtx_assert(sim->mtx, MA_OWNED);
4166 sim->devq->send_queue.qfrozen_cnt[0] += count;
4167 return (sim->devq->send_queue.qfrozen_cnt[0]);
4171 xpt_release_devq_timeout(void *arg)
4173 struct cam_ed *device;
4175 device = (struct cam_ed *)arg;
4177 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4181 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4183 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4185 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4189 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4191 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4193 xpt_release_devq_device(path->device, rl, count, run_queue);
4197 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4200 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4202 printf("xpt_release_devq(%d): requested %u > present %u\n",
4203 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4205 count = dev->ccbq.queue.qfrozen_cnt[rl];
4207 dev->sim->devq->alloc_openings -=
4208 cam_ccbq_release(&dev->ccbq, rl, count);
4209 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4210 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4211 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4212 xpt_run_dev_allocq(dev->target->bus);
4214 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4216 * No longer need to wait for a successful
4217 * command completion.
4219 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4221 * Remove any timeouts that might be scheduled
4222 * to release this queue.
4224 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4225 callout_stop(&dev->callout);
4226 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4231 * Now that we are unfrozen schedule the
4232 * device so any pending transactions are
4235 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4236 xpt_run_dev_sendq(dev->target->bus);
4241 xpt_release_simq(struct cam_sim *sim, int run_queue)
4245 mtx_assert(sim->mtx, MA_OWNED);
4246 sendq = &(sim->devq->send_queue);
4247 if (sendq->qfrozen_cnt[0] <= 0) {
4249 printf("xpt_release_simq: requested 1 > present %u\n",
4250 sendq->qfrozen_cnt[0]);
4253 sendq->qfrozen_cnt[0]--;
4254 if (sendq->qfrozen_cnt[0] == 0) {
4256 * If there is a timeout scheduled to release this
4257 * sim queue, remove it. The queue frozen count is
4260 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4261 callout_stop(&sim->callout);
4262 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4268 * Now that we are unfrozen run the send queue.
4270 bus = xpt_find_bus(sim->path_id);
4271 xpt_run_dev_sendq(bus);
4272 xpt_release_bus(bus);
4278 * XXX Appears to be unused.
4281 xpt_release_simq_timeout(void *arg)
4283 struct cam_sim *sim;
4285 sim = (struct cam_sim *)arg;
4286 xpt_release_simq(sim, /* run_queue */ TRUE);
4290 xpt_done(union ccb *done_ccb)
4292 struct cam_sim *sim;
4295 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4296 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4298 * Queue up the request for handling by our SWI handler
4299 * any of the "non-immediate" type of ccbs.
4301 sim = done_ccb->ccb_h.path->bus->sim;
4302 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4304 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4305 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4306 CAM_SIM_BATCH)) == 0) {
4307 mtx_lock(&cam_simq_lock);
4308 first = TAILQ_EMPTY(&cam_simq);
4309 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4310 mtx_unlock(&cam_simq_lock);
4311 sim->flags |= CAM_SIM_ON_DONEQ;
4313 swi_sched(cambio_ih, 0);
4319 xpt_batch_start(struct cam_sim *sim)
4322 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4323 sim->flags |= CAM_SIM_BATCH;
4327 xpt_batch_done(struct cam_sim *sim)
4330 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4331 sim->flags &= ~CAM_SIM_BATCH;
4332 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4333 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4334 camisr_runqueue(&sim->sim_doneq);
4342 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4347 xpt_alloc_ccb_nowait()
4351 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4356 xpt_free_ccb(union ccb *free_ccb)
4358 free(free_ccb, M_CAMXPT);
4363 /* Private XPT functions */
4366 * Get a CAM control block for the caller. Charge the structure to the device
4367 * referenced by the path. If the this device has no 'credits' then the
4368 * device already has the maximum number of outstanding operations under way
4369 * and we return NULL. If we don't have sufficient resources to allocate more
4370 * ccbs, we also return NULL.
4373 xpt_get_ccb(struct cam_ed *device)
4376 struct cam_sim *sim;
4379 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4380 new_ccb = xpt_alloc_ccb_nowait();
4381 if (new_ccb == NULL) {
4384 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4385 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4386 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4390 cam_ccbq_take_opening(&device->ccbq);
4391 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4396 xpt_release_bus(struct cam_eb *bus)
4399 mtx_lock(&xsoftc.xpt_topo_lock);
4400 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4401 if ((--bus->refcount == 0)
4402 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4403 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4404 xsoftc.bus_generation++;
4405 mtx_unlock(&xsoftc.xpt_topo_lock);
4406 cam_sim_release(bus->sim);
4407 free(bus, M_CAMXPT);
4409 mtx_unlock(&xsoftc.xpt_topo_lock);
4412 static struct cam_et *
4413 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4415 struct cam_et *target;
4417 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4418 if (target != NULL) {
4419 struct cam_et *cur_target;
4421 TAILQ_INIT(&target->ed_entries);
4423 target->target_id = target_id;
4424 target->refcount = 1;
4425 target->generation = 0;
4426 target->luns = NULL;
4427 timevalclear(&target->last_reset);
4429 * Hold a reference to our parent bus so it
4430 * will not go away before we do.
4432 mtx_lock(&xsoftc.xpt_topo_lock);
4434 mtx_unlock(&xsoftc.xpt_topo_lock);
4436 /* Insertion sort into our bus's target list */
4437 cur_target = TAILQ_FIRST(&bus->et_entries);
4438 while (cur_target != NULL && cur_target->target_id < target_id)
4439 cur_target = TAILQ_NEXT(cur_target, links);
4441 if (cur_target != NULL) {
4442 TAILQ_INSERT_BEFORE(cur_target, target, links);
4444 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4452 xpt_release_target(struct cam_et *target)
4455 if (target->refcount == 1) {
4456 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4457 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4458 target->bus->generation++;
4459 xpt_release_bus(target->bus);
4461 free(target->luns, M_CAMXPT);
4462 free(target, M_CAMXPT);
4468 static struct cam_ed *
4469 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4472 struct cam_ed *device, *cur_device;
4474 device = xpt_alloc_device(bus, target, lun_id);
4478 device->mintags = 1;
4479 device->maxtags = 1;
4480 bus->sim->max_ccbs += device->ccbq.devq_openings;
4481 cur_device = TAILQ_FIRST(&target->ed_entries);
4482 while (cur_device != NULL && cur_device->lun_id < lun_id)
4483 cur_device = TAILQ_NEXT(cur_device, links);
4484 if (cur_device != NULL) {
4485 TAILQ_INSERT_BEFORE(cur_device, device, links);
4487 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4489 target->generation++;
4495 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4497 struct cam_ed *device;
4498 struct cam_devq *devq;
4501 /* Make space for us in the device queue on our bus */
4502 devq = bus->sim->devq;
4503 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4505 if (status != CAM_REQ_CMP) {
4508 device = (struct cam_ed *)malloc(sizeof(*device),
4509 M_CAMXPT, M_NOWAIT|M_ZERO);
4512 if (device != NULL) {
4513 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4514 device->alloc_ccb_entry.device = device;
4515 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4516 device->send_ccb_entry.device = device;
4517 device->target = target;
4518 device->lun_id = lun_id;
4519 device->sim = bus->sim;
4520 /* Initialize our queues */
4521 if (camq_init(&device->drvq, 0) != 0) {
4522 free(device, M_CAMXPT);
4525 if (cam_ccbq_init(&device->ccbq,
4526 bus->sim->max_dev_openings) != 0) {
4527 camq_fini(&device->drvq);
4528 free(device, M_CAMXPT);
4531 SLIST_INIT(&device->asyncs);
4532 SLIST_INIT(&device->periphs);
4533 device->generation = 0;
4534 device->owner = NULL;
4535 device->flags = CAM_DEV_UNCONFIGURED;
4536 device->tag_delay_count = 0;
4537 device->tag_saved_openings = 0;
4538 device->refcount = 1;
4539 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4542 * Hold a reference to our parent target so it
4543 * will not go away before we do.
4552 xpt_acquire_device(struct cam_ed *device)
4559 xpt_release_device(struct cam_ed *device)
4562 if (device->refcount == 1) {
4563 struct cam_devq *devq;
4565 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4566 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4567 panic("Removing device while still queued for ccbs");
4569 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4570 callout_stop(&device->callout);
4572 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4573 device->target->generation++;
4574 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4575 /* Release our slot in the devq */
4576 devq = device->target->bus->sim->devq;
4577 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4578 camq_fini(&device->drvq);
4579 cam_ccbq_fini(&device->ccbq);
4581 * Free allocated memory. free(9) does nothing if the
4582 * supplied pointer is NULL, so it is safe to call without
4585 free(device->supported_vpds, M_CAMXPT);
4586 free(device->serial_num, M_CAMXPT);
4588 xpt_release_target(device->target);
4589 free(device, M_CAMXPT);
4595 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4603 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4604 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4605 if (result == CAM_REQ_CMP && (diff < 0)) {
4606 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4608 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4609 || (dev->inq_flags & SID_CmdQue) != 0)
4610 dev->tag_saved_openings = newopenings;
4611 /* Adjust the global limit */
4612 dev->sim->max_ccbs += diff;
4616 static struct cam_eb *
4617 xpt_find_bus(path_id_t path_id)
4621 mtx_lock(&xsoftc.xpt_topo_lock);
4622 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4624 bus = TAILQ_NEXT(bus, links)) {
4625 if (bus->path_id == path_id) {
4630 mtx_unlock(&xsoftc.xpt_topo_lock);
4634 static struct cam_et *
4635 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4637 struct cam_et *target;
4639 for (target = TAILQ_FIRST(&bus->et_entries);
4641 target = TAILQ_NEXT(target, links)) {
4642 if (target->target_id == target_id) {
4650 static struct cam_ed *
4651 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4653 struct cam_ed *device;
4655 for (device = TAILQ_FIRST(&target->ed_entries);
4657 device = TAILQ_NEXT(device, links)) {
4658 if (device->lun_id == lun_id) {
4667 xpt_start_tags(struct cam_path *path)
4669 struct ccb_relsim crs;
4670 struct cam_ed *device;
4671 struct cam_sim *sim;
4674 device = path->device;
4675 sim = path->bus->sim;
4676 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4677 xpt_freeze_devq(path, /*count*/1);
4678 device->inq_flags |= SID_CmdQue;
4679 if (device->tag_saved_openings != 0)
4680 newopenings = device->tag_saved_openings;
4682 newopenings = min(device->maxtags,
4683 sim->max_tagged_dev_openings);
4684 xpt_dev_ccbq_resize(path, newopenings);
4685 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4686 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4687 crs.ccb_h.func_code = XPT_REL_SIMQ;
4688 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4690 = crs.release_timeout
4693 xpt_action((union ccb *)&crs);
4697 xpt_stop_tags(struct cam_path *path)
4699 struct ccb_relsim crs;
4700 struct cam_ed *device;
4701 struct cam_sim *sim;
4703 device = path->device;
4704 sim = path->bus->sim;
4705 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4706 device->tag_delay_count = 0;
4707 xpt_freeze_devq(path, /*count*/1);
4708 device->inq_flags &= ~SID_CmdQue;
4709 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4710 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4711 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4712 crs.ccb_h.func_code = XPT_REL_SIMQ;
4713 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4715 = crs.release_timeout
4718 xpt_action((union ccb *)&crs);
4722 xpt_boot_delay(void *arg)
4729 xpt_config(void *arg)
4732 * Now that interrupts are enabled, go find our devices
4735 /* Setup debugging path */
4736 if (cam_dflags != CAM_DEBUG_NONE) {
4738 * Locking is specifically omitted here. No SIMs have
4739 * registered yet, so xpt_create_path will only be searching
4740 * empty lists of targets and devices.
4742 if (xpt_create_path(&cam_dpath, xpt_periph,
4743 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4744 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4745 printf("xpt_config: xpt_create_path() failed for debug"
4746 " target %d:%d:%d, debugging disabled\n",
4747 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4748 cam_dflags = CAM_DEBUG_NONE;
4753 periphdriver_init(1);
4755 callout_init(&xsoftc.boot_callout, 1);
4756 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4757 xpt_boot_delay, NULL);
4758 /* Fire up rescan thread. */
4759 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4760 printf("xpt_init: failed to create rescan thread\n");
4768 xsoftc.buses_to_config++;
4773 xpt_release_boot(void)
4776 xsoftc.buses_to_config--;
4777 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4778 struct xpt_task *task;
4780 xsoftc.buses_config_done = 1;
4782 /* Call manually because we don't have any busses */
4783 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4785 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4786 taskqueue_enqueue(taskqueue_thread, &task->task);
4793 * If the given device only has one peripheral attached to it, and if that
4794 * peripheral is the passthrough driver, announce it. This insures that the
4795 * user sees some sort of announcement for every peripheral in their system.
4798 xptpassannouncefunc(struct cam_ed *device, void *arg)
4800 struct cam_periph *periph;
4803 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4804 periph = SLIST_NEXT(periph, periph_links), i++);
4806 periph = SLIST_FIRST(&device->periphs);
4808 && (strncmp(periph->periph_name, "pass", 4) == 0))
4809 xpt_announce_periph(periph, NULL);
4815 xpt_finishconfig_task(void *context, int pending)
4818 periphdriver_init(2);
4820 * Check for devices with no "standard" peripheral driver
4821 * attached. For any devices like that, announce the
4822 * passthrough driver so the user will see something.
4824 xpt_for_all_devices(xptpassannouncefunc, NULL);
4826 /* Release our hook so that the boot can continue. */
4827 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4828 free(xsoftc.xpt_config_hook, M_CAMXPT);
4829 xsoftc.xpt_config_hook = NULL;
4831 free(context, M_CAMXPT);
4835 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4836 struct cam_path *path)
4838 struct ccb_setasync csa;
4843 mtx_lock(&xsoftc.xpt_lock);
4844 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4845 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4846 if (status != CAM_REQ_CMP) {
4847 mtx_unlock(&xsoftc.xpt_lock);
4853 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4854 csa.ccb_h.func_code = XPT_SASYNC_CB;
4855 csa.event_enable = event;
4856 csa.callback = cbfunc;
4857 csa.callback_arg = cbarg;
4858 xpt_action((union ccb *)&csa);
4859 status = csa.ccb_h.status;
4861 xpt_free_path(path);
4862 mtx_unlock(&xsoftc.xpt_lock);
4864 if ((status == CAM_REQ_CMP) &&
4865 (csa.event_enable & AC_FOUND_DEVICE)) {
4867 * Get this peripheral up to date with all
4868 * the currently existing devices.
4870 xpt_for_all_devices(xptsetasyncfunc, &csa);
4872 if ((status == CAM_REQ_CMP) &&
4873 (csa.event_enable & AC_PATH_REGISTERED)) {
4875 * Get this peripheral up to date with all
4876 * the currently existing busses.
4878 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4885 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4887 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4889 switch (work_ccb->ccb_h.func_code) {
4890 /* Common cases first */
4891 case XPT_PATH_INQ: /* Path routing inquiry */
4893 struct ccb_pathinq *cpi;
4895 cpi = &work_ccb->cpi;
4896 cpi->version_num = 1; /* XXX??? */
4897 cpi->hba_inquiry = 0;
4898 cpi->target_sprt = 0;
4900 cpi->hba_eng_cnt = 0;
4901 cpi->max_target = 0;
4903 cpi->initiator_id = 0;
4904 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4905 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4906 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4907 cpi->unit_number = sim->unit_number;
4908 cpi->bus_id = sim->bus_id;
4909 cpi->base_transfer_speed = 0;
4910 cpi->protocol = PROTO_UNSPECIFIED;
4911 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4912 cpi->transport = XPORT_UNSPECIFIED;
4913 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4914 cpi->ccb_h.status = CAM_REQ_CMP;
4919 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4926 * The xpt as a "controller" has no interrupt sources, so polling
4930 xptpoll(struct cam_sim *sim)
4935 xpt_lock_buses(void)
4937 mtx_lock(&xsoftc.xpt_topo_lock);
4941 xpt_unlock_buses(void)
4943 mtx_unlock(&xsoftc.xpt_topo_lock);
4950 struct cam_sim *sim;
4952 mtx_lock(&cam_simq_lock);
4954 while (!TAILQ_EMPTY(&cam_simq)) {
4955 TAILQ_CONCAT(&queue, &cam_simq, links);
4956 mtx_unlock(&cam_simq_lock);
4958 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4959 TAILQ_REMOVE(&queue, sim, links);
4961 camisr_runqueue(&sim->sim_doneq);
4962 sim->flags &= ~CAM_SIM_ON_DONEQ;
4963 CAM_SIM_UNLOCK(sim);
4965 mtx_lock(&cam_simq_lock);
4967 mtx_unlock(&cam_simq_lock);
4971 camisr_runqueue(void *V_queue)
4973 cam_isrq_t *queue = V_queue;
4974 struct ccb_hdr *ccb_h;
4976 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4979 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4980 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4982 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4987 if (ccb_h->flags & CAM_HIGH_POWER) {
4988 struct highpowerlist *hphead;
4989 union ccb *send_ccb;
4991 mtx_lock(&xsoftc.xpt_lock);
4992 hphead = &xsoftc.highpowerq;
4994 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4997 * Increment the count since this command is done.
4999 xsoftc.num_highpower++;
5002 * Any high powered commands queued up?
5004 if (send_ccb != NULL) {
5006 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5007 mtx_unlock(&xsoftc.xpt_lock);
5009 xpt_release_devq(send_ccb->ccb_h.path,
5010 /*count*/1, /*runqueue*/TRUE);
5012 mtx_unlock(&xsoftc.xpt_lock);
5015 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5018 dev = ccb_h->path->device;
5020 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5021 ccb_h->path->bus->sim->devq->send_active--;
5022 ccb_h->path->bus->sim->devq->send_openings++;
5025 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5026 && (dev->ccbq.dev_active == 0))) {
5027 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5028 xpt_release_devq(ccb_h->path, /*count*/1,
5029 /*run_queue*/FALSE);
5032 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5033 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5034 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5035 xpt_release_devq(ccb_h->path, /*count*/1,
5036 /*run_queue*/FALSE);
5039 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5040 && (--dev->tag_delay_count == 0))
5041 xpt_start_tags(ccb_h->path);
5042 if (!device_is_send_queued(dev))
5043 xpt_schedule_dev_sendq(ccb_h->path->bus, dev);
5046 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5047 xpt_release_simq(ccb_h->path->bus->sim,
5049 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5053 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5054 && (ccb_h->status & CAM_DEV_QFRZN)) {
5055 xpt_release_devq(ccb_h->path, /*count*/1,
5057 ccb_h->status &= ~CAM_DEV_QFRZN;
5059 xpt_run_dev_sendq(ccb_h->path->bus);
5062 /* Call the peripheral driver's callback */
5063 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);