2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_xpt_internal.h>
60 #include <cam/cam_debug.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
66 #include <machine/md_var.h> /* geometry translation */
67 #include <machine/stdarg.h> /* for xpt_print below */
72 * This is the maximum number of high powered commands (e.g. start unit)
73 * that can be outstanding at a particular time.
75 #ifndef CAM_MAX_HIGHPOWER
76 #define CAM_MAX_HIGHPOWER 4
79 /* Datastructures internal to the xpt layer */
80 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
82 /* Object for defering XPT actions to a taskqueue */
95 u_int32_t xpt_generation;
97 /* number of high powered commands that can go through right now */
98 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
101 /* queue for handling async rescan requests. */
102 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
104 int buses_config_done;
106 /* Registered busses */
107 TAILQ_HEAD(,cam_eb) xpt_busses;
108 u_int bus_generation;
110 struct intr_config_hook *xpt_config_hook;
113 struct callout boot_callout;
115 struct mtx xpt_topo_lock;
121 DM_RET_FLAG_MASK = 0x0f,
124 DM_RET_DESCEND = 0x20,
126 DM_RET_ACTION_MASK = 0xf0
134 } xpt_traverse_depth;
136 struct xpt_traverse_config {
137 xpt_traverse_depth depth;
142 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
143 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
144 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
145 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
146 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
148 /* Transport layer configuration information */
149 static struct xpt_softc xsoftc;
151 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
152 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
153 &xsoftc.boot_delay, 0, "Bus registration wait time");
155 /* Queues for our software interrupt handler */
156 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
157 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
158 static cam_simq_t cam_simq;
159 static struct mtx cam_simq_lock;
161 /* Pointers to software interrupt handlers */
162 static void *cambio_ih;
164 struct cam_periph *xpt_periph;
166 static periph_init_t xpt_periph_init;
168 static struct periph_driver xpt_driver =
170 xpt_periph_init, "xpt",
171 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
175 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
177 static d_open_t xptopen;
178 static d_close_t xptclose;
179 static d_ioctl_t xptioctl;
181 static struct cdevsw xpt_cdevsw = {
182 .d_version = D_VERSION,
190 /* Storage for debugging datastructures */
191 struct cam_path *cam_dpath;
192 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
193 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
194 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
195 &cam_dflags, 0, "Enabled debug flags");
196 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
197 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
198 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
199 &cam_debug_delay, 0, "Delay in us after each debug message");
201 /* Our boot-time initialization hook */
202 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
204 static moduledata_t cam_moduledata = {
206 cam_module_event_handler,
210 static int xpt_init(void *);
212 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
213 MODULE_VERSION(cam, 1);
216 static void xpt_async_bcast(struct async_list *async_head,
217 u_int32_t async_code,
218 struct cam_path *path,
220 static path_id_t xptnextfreepathid(void);
221 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
222 static union ccb *xpt_get_ccb(struct cam_ed *device);
223 static void xpt_run_dev_allocq(struct cam_eb *bus);
224 static void xpt_run_dev_sendq(struct cam_eb *bus);
225 static timeout_t xpt_release_devq_timeout;
226 static void xpt_release_simq_timeout(void *arg) __unused;
227 static void xpt_release_bus(struct cam_eb *bus);
228 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
229 u_int count, int run_queue);
230 static struct cam_et*
231 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
232 static void xpt_release_target(struct cam_et *target);
233 static struct cam_eb*
234 xpt_find_bus(path_id_t path_id);
235 static struct cam_et*
236 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
237 static struct cam_ed*
238 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
239 static void xpt_config(void *arg);
240 static xpt_devicefunc_t xptpassannouncefunc;
241 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
242 static void xptpoll(struct cam_sim *sim);
243 static void camisr(void *);
244 static void camisr_runqueue(void *);
245 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
246 u_int num_patterns, struct cam_eb *bus);
247 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
249 struct cam_ed *device);
250 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
252 struct cam_periph *periph);
253 static xpt_busfunc_t xptedtbusfunc;
254 static xpt_targetfunc_t xptedttargetfunc;
255 static xpt_devicefunc_t xptedtdevicefunc;
256 static xpt_periphfunc_t xptedtperiphfunc;
257 static xpt_pdrvfunc_t xptplistpdrvfunc;
258 static xpt_periphfunc_t xptplistperiphfunc;
259 static int xptedtmatch(struct ccb_dev_match *cdm);
260 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
261 static int xptbustraverse(struct cam_eb *start_bus,
262 xpt_busfunc_t *tr_func, void *arg);
263 static int xpttargettraverse(struct cam_eb *bus,
264 struct cam_et *start_target,
265 xpt_targetfunc_t *tr_func, void *arg);
266 static int xptdevicetraverse(struct cam_et *target,
267 struct cam_ed *start_device,
268 xpt_devicefunc_t *tr_func, void *arg);
269 static int xptperiphtraverse(struct cam_ed *device,
270 struct cam_periph *start_periph,
271 xpt_periphfunc_t *tr_func, void *arg);
272 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
273 xpt_pdrvfunc_t *tr_func, void *arg);
274 static int xptpdperiphtraverse(struct periph_driver **pdrv,
275 struct cam_periph *start_periph,
276 xpt_periphfunc_t *tr_func,
278 static xpt_busfunc_t xptdefbusfunc;
279 static xpt_targetfunc_t xptdeftargetfunc;
280 static xpt_devicefunc_t xptdefdevicefunc;
281 static xpt_periphfunc_t xptdefperiphfunc;
282 static void xpt_finishconfig_task(void *context, int pending);
283 static void xpt_dev_async_default(u_int32_t async_code,
285 struct cam_et *target,
286 struct cam_ed *device,
288 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
289 struct cam_et *target,
291 static xpt_devicefunc_t xptsetasyncfunc;
292 static xpt_busfunc_t xptsetasyncbusfunc;
293 static cam_status xptregister(struct cam_periph *periph,
295 static __inline int periph_is_queued(struct cam_periph *periph);
296 static __inline int device_is_alloc_queued(struct cam_ed *device);
297 static __inline int device_is_send_queued(struct cam_ed *device);
300 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
304 if ((dev->drvq.entries > 0) &&
305 (dev->ccbq.devq_openings > 0) &&
306 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
307 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
309 * The priority of a device waiting for CCB resources
310 * is that of the highest priority peripheral driver
313 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
314 &dev->alloc_ccb_entry.pinfo,
315 CAMQ_GET_PRIO(&dev->drvq));
324 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
328 if ((dev->ccbq.queue.entries > 0) &&
329 (dev->ccbq.dev_openings > 0) &&
330 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
332 * The priority of a device waiting for controller
333 * resources is that of the highest priority CCB
337 xpt_schedule_dev(&bus->sim->devq->send_queue,
338 &dev->send_ccb_entry.pinfo,
339 CAMQ_GET_PRIO(&dev->ccbq.queue));
347 periph_is_queued(struct cam_periph *periph)
349 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
353 device_is_alloc_queued(struct cam_ed *device)
355 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
359 device_is_send_queued(struct cam_ed *device)
361 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
367 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
371 xptdone(struct cam_periph *periph, union ccb *done_ccb)
373 /* Caller will release the CCB */
374 wakeup(&done_ccb->ccb_h.cbfcnp);
378 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
382 * Only allow read-write access.
384 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
388 * We don't allow nonblocking access.
390 if ((flags & O_NONBLOCK) != 0) {
391 printf("%s: can't do nonblocking access\n", devtoname(dev));
395 /* Mark ourselves open */
396 mtx_lock(&xsoftc.xpt_lock);
397 xsoftc.flags |= XPT_FLAG_OPEN;
398 mtx_unlock(&xsoftc.xpt_lock);
404 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
407 /* Mark ourselves closed */
408 mtx_lock(&xsoftc.xpt_lock);
409 xsoftc.flags &= ~XPT_FLAG_OPEN;
410 mtx_unlock(&xsoftc.xpt_lock);
416 * Don't automatically grab the xpt softc lock here even though this is going
417 * through the xpt device. The xpt device is really just a back door for
418 * accessing other devices and SIMs, so the right thing to do is to grab
419 * the appropriate SIM lock once the bus/SIM is located.
422 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
430 * For the transport layer CAMIOCOMMAND ioctl, we really only want
431 * to accept CCB types that don't quite make sense to send through a
432 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
440 inccb = (union ccb *)addr;
442 bus = xpt_find_bus(inccb->ccb_h.path_id);
446 switch (inccb->ccb_h.func_code) {
449 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
450 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
451 xpt_release_bus(bus);
456 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
457 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
458 xpt_release_bus(bus);
466 switch(inccb->ccb_h.func_code) {
474 ccb = xpt_alloc_ccb();
476 CAM_SIM_LOCK(bus->sim);
479 * Create a path using the bus, target, and lun the
482 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
483 inccb->ccb_h.path_id,
484 inccb->ccb_h.target_id,
485 inccb->ccb_h.target_lun) !=
488 CAM_SIM_UNLOCK(bus->sim);
492 /* Ensure all of our fields are correct */
493 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
494 inccb->ccb_h.pinfo.priority);
495 xpt_merge_ccb(ccb, inccb);
496 ccb->ccb_h.cbfcnp = xptdone;
497 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
498 bcopy(ccb, inccb, sizeof(union ccb));
499 xpt_free_path(ccb->ccb_h.path);
501 CAM_SIM_UNLOCK(bus->sim);
508 * This is an immediate CCB, so it's okay to
509 * allocate it on the stack.
512 CAM_SIM_LOCK(bus->sim);
515 * Create a path using the bus, target, and lun the
518 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
519 inccb->ccb_h.path_id,
520 inccb->ccb_h.target_id,
521 inccb->ccb_h.target_lun) !=
524 CAM_SIM_UNLOCK(bus->sim);
527 /* Ensure all of our fields are correct */
528 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
529 inccb->ccb_h.pinfo.priority);
530 xpt_merge_ccb(&ccb, inccb);
531 ccb.ccb_h.cbfcnp = xptdone;
533 CAM_SIM_UNLOCK(bus->sim);
534 bcopy(&ccb, inccb, sizeof(union ccb));
535 xpt_free_path(ccb.ccb_h.path);
539 case XPT_DEV_MATCH: {
540 struct cam_periph_map_info mapinfo;
541 struct cam_path *old_path;
544 * We can't deal with physical addresses for this
545 * type of transaction.
547 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
553 * Save this in case the caller had it set to
554 * something in particular.
556 old_path = inccb->ccb_h.path;
559 * We really don't need a path for the matching
560 * code. The path is needed because of the
561 * debugging statements in xpt_action(). They
562 * assume that the CCB has a valid path.
564 inccb->ccb_h.path = xpt_periph->path;
566 bzero(&mapinfo, sizeof(mapinfo));
569 * Map the pattern and match buffers into kernel
570 * virtual address space.
572 error = cam_periph_mapmem(inccb, &mapinfo);
575 inccb->ccb_h.path = old_path;
580 * This is an immediate CCB, we can send it on directly.
585 * Map the buffers back into user space.
587 cam_periph_unmapmem(inccb, &mapinfo);
589 inccb->ccb_h.path = old_path;
598 xpt_release_bus(bus);
602 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
603 * with the periphal driver name and unit name filled in. The other
604 * fields don't really matter as input. The passthrough driver name
605 * ("pass"), and unit number are passed back in the ccb. The current
606 * device generation number, and the index into the device peripheral
607 * driver list, and the status are also passed back. Note that
608 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
609 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
610 * (or rather should be) impossible for the device peripheral driver
611 * list to change since we look at the whole thing in one pass, and
612 * we do it with lock protection.
615 case CAMGETPASSTHRU: {
617 struct cam_periph *periph;
618 struct periph_driver **p_drv;
621 u_int cur_generation;
622 int base_periph_found;
625 ccb = (union ccb *)addr;
626 unit = ccb->cgdl.unit_number;
627 name = ccb->cgdl.periph_name;
629 * Every 100 devices, we want to drop our lock protection to
630 * give the software interrupt handler a chance to run.
631 * Most systems won't run into this check, but this should
632 * avoid starvation in the software interrupt handler in
637 ccb = (union ccb *)addr;
639 base_periph_found = 0;
642 * Sanity check -- make sure we don't get a null peripheral
645 if (*ccb->cgdl.periph_name == '\0') {
650 /* Keep the list from changing while we traverse it */
651 mtx_lock(&xsoftc.xpt_topo_lock);
653 cur_generation = xsoftc.xpt_generation;
655 /* first find our driver in the list of drivers */
656 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
657 if (strcmp((*p_drv)->driver_name, name) == 0)
660 if (*p_drv == NULL) {
661 mtx_unlock(&xsoftc.xpt_topo_lock);
662 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
663 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
664 *ccb->cgdl.periph_name = '\0';
665 ccb->cgdl.unit_number = 0;
671 * Run through every peripheral instance of this driver
672 * and check to see whether it matches the unit passed
673 * in by the user. If it does, get out of the loops and
674 * find the passthrough driver associated with that
677 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
678 periph = TAILQ_NEXT(periph, unit_links)) {
680 if (periph->unit_number == unit) {
682 } else if (--splbreaknum == 0) {
683 mtx_unlock(&xsoftc.xpt_topo_lock);
684 mtx_lock(&xsoftc.xpt_topo_lock);
686 if (cur_generation != xsoftc.xpt_generation)
691 * If we found the peripheral driver that the user passed
692 * in, go through all of the peripheral drivers for that
693 * particular device and look for a passthrough driver.
695 if (periph != NULL) {
696 struct cam_ed *device;
699 base_periph_found = 1;
700 device = periph->path->device;
701 for (i = 0, periph = SLIST_FIRST(&device->periphs);
703 periph = SLIST_NEXT(periph, periph_links), i++) {
705 * Check to see whether we have a
706 * passthrough device or not.
708 if (strcmp(periph->periph_name, "pass") == 0) {
710 * Fill in the getdevlist fields.
712 strcpy(ccb->cgdl.periph_name,
713 periph->periph_name);
714 ccb->cgdl.unit_number =
716 if (SLIST_NEXT(periph, periph_links))
718 CAM_GDEVLIST_MORE_DEVS;
721 CAM_GDEVLIST_LAST_DEVICE;
722 ccb->cgdl.generation =
726 * Fill in some CCB header fields
727 * that the user may want.
730 periph->path->bus->path_id;
731 ccb->ccb_h.target_id =
732 periph->path->target->target_id;
733 ccb->ccb_h.target_lun =
734 periph->path->device->lun_id;
735 ccb->ccb_h.status = CAM_REQ_CMP;
742 * If the periph is null here, one of two things has
743 * happened. The first possibility is that we couldn't
744 * find the unit number of the particular peripheral driver
745 * that the user is asking about. e.g. the user asks for
746 * the passthrough driver for "da11". We find the list of
747 * "da" peripherals all right, but there is no unit 11.
748 * The other possibility is that we went through the list
749 * of peripheral drivers attached to the device structure,
750 * but didn't find one with the name "pass". Either way,
751 * we return ENOENT, since we couldn't find something.
753 if (periph == NULL) {
754 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
755 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
756 *ccb->cgdl.periph_name = '\0';
757 ccb->cgdl.unit_number = 0;
760 * It is unfortunate that this is even necessary,
761 * but there are many, many clueless users out there.
762 * If this is true, the user is looking for the
763 * passthrough driver, but doesn't have one in his
766 if (base_periph_found == 1) {
767 printf("xptioctl: pass driver is not in the "
769 printf("xptioctl: put \"device pass\" in "
770 "your kernel config file\n");
773 mtx_unlock(&xsoftc.xpt_topo_lock);
785 cam_module_event_handler(module_t mod, int what, void *arg)
791 if ((error = xpt_init(NULL)) != 0)
804 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
807 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
808 xpt_free_path(done_ccb->ccb_h.path);
809 xpt_free_ccb(done_ccb);
811 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
812 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
817 /* thread to handle bus rescans */
819 xpt_scanner_thread(void *dummy)
826 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
827 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
829 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
830 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
833 sim = ccb->ccb_h.path->bus->sim;
844 xpt_rescan(union ccb *ccb)
848 /* Prepare request */
849 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
850 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
851 ccb->ccb_h.func_code = XPT_SCAN_BUS;
852 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
853 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
854 ccb->ccb_h.func_code = XPT_SCAN_TGT;
855 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
856 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
857 ccb->ccb_h.func_code = XPT_SCAN_LUN;
859 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
860 xpt_free_path(ccb->ccb_h.path);
864 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
865 ccb->ccb_h.cbfcnp = xpt_rescan_done;
866 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
867 /* Don't make duplicate entries for the same paths. */
869 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
870 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
871 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
872 wakeup(&xsoftc.ccb_scanq);
874 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
875 xpt_free_path(ccb->ccb_h.path);
881 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
882 xsoftc.buses_to_config++;
883 wakeup(&xsoftc.ccb_scanq);
887 /* Functions accessed by the peripheral drivers */
889 xpt_init(void *dummy)
891 struct cam_sim *xpt_sim;
892 struct cam_path *path;
893 struct cam_devq *devq;
896 TAILQ_INIT(&xsoftc.xpt_busses);
897 TAILQ_INIT(&cam_simq);
898 TAILQ_INIT(&xsoftc.ccb_scanq);
899 STAILQ_INIT(&xsoftc.highpowerq);
900 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
902 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
903 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
904 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
907 * The xpt layer is, itself, the equivelent of a SIM.
908 * Allow 16 ccbs in the ccb pool for it. This should
909 * give decent parallelism when we probe busses and
910 * perform other XPT functions.
912 devq = cam_simq_alloc(16);
913 xpt_sim = cam_sim_alloc(xptaction,
918 /*mtx*/&xsoftc.xpt_lock,
919 /*max_dev_transactions*/0,
920 /*max_tagged_dev_transactions*/0,
925 mtx_lock(&xsoftc.xpt_lock);
926 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
927 mtx_unlock(&xsoftc.xpt_lock);
928 printf("xpt_init: xpt_bus_register failed with status %#x,"
929 " failing attach\n", status);
934 * Looking at the XPT from the SIM layer, the XPT is
935 * the equivelent of a peripheral driver. Allocate
936 * a peripheral driver entry for us.
938 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
940 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
941 mtx_unlock(&xsoftc.xpt_lock);
942 printf("xpt_init: xpt_create_path failed with status %#x,"
943 " failing attach\n", status);
947 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
948 path, NULL, 0, xpt_sim);
950 mtx_unlock(&xsoftc.xpt_lock);
951 /* Install our software interrupt handlers */
952 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
954 * Register a callback for when interrupts are enabled.
956 xsoftc.xpt_config_hook =
957 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
958 M_CAMXPT, M_NOWAIT | M_ZERO);
959 if (xsoftc.xpt_config_hook == NULL) {
960 printf("xpt_init: Cannot malloc config hook "
961 "- failing attach\n");
964 xsoftc.xpt_config_hook->ich_func = xpt_config;
965 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
966 free (xsoftc.xpt_config_hook, M_CAMXPT);
967 printf("xpt_init: config_intrhook_establish failed "
968 "- failing attach\n");
975 xptregister(struct cam_periph *periph, void *arg)
977 struct cam_sim *xpt_sim;
979 if (periph == NULL) {
980 printf("xptregister: periph was NULL!!\n");
981 return(CAM_REQ_CMP_ERR);
984 xpt_sim = (struct cam_sim *)arg;
985 xpt_sim->softc = periph;
987 periph->softc = NULL;
993 xpt_add_periph(struct cam_periph *periph)
995 struct cam_ed *device;
997 struct periph_list *periph_head;
999 mtx_assert(periph->sim->mtx, MA_OWNED);
1001 device = periph->path->device;
1003 periph_head = &device->periphs;
1005 status = CAM_REQ_CMP;
1007 if (device != NULL) {
1009 * Make room for this peripheral
1010 * so it will fit in the queue
1011 * when it's scheduled to run
1013 status = camq_resize(&device->drvq,
1014 device->drvq.array_size + 1);
1016 device->generation++;
1018 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1021 mtx_lock(&xsoftc.xpt_topo_lock);
1022 xsoftc.xpt_generation++;
1023 mtx_unlock(&xsoftc.xpt_topo_lock);
1029 xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
1031 struct cam_ed *device;
1033 mtx_assert(periph->sim->mtx, MA_OWNED);
1035 device = periph->path->device;
1037 if (device != NULL) {
1038 struct periph_list *periph_head;
1040 periph_head = &device->periphs;
1042 /* Release the slot for this peripheral */
1043 camq_resize(&device->drvq, device->drvq.array_size - 1);
1045 device->generation++;
1047 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1050 if (topology_lock_held == 0)
1051 mtx_lock(&xsoftc.xpt_topo_lock);
1053 xsoftc.xpt_generation++;
1055 if (topology_lock_held == 0)
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);
1081 else if (path->device->protocol == PROTO_SEMB)
1083 (struct sep_identify_data *)&path->device->ident_data);
1085 printf("Unknown protocol device\n");
1086 if (bootverbose && path->device->serial_num_len > 0) {
1087 /* Don't wrap the screen - print only the first 60 chars */
1088 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1089 periph->unit_number, path->device->serial_num);
1091 /* Announce transport details. */
1092 (*(path->bus->xport->announce))(periph);
1093 /* Announce command queueing. */
1094 if (path->device->inq_flags & SID_CmdQue
1095 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1096 printf("%s%d: Command Queueing enabled\n",
1097 periph->periph_name, periph->unit_number);
1099 /* Announce caller's details if they've passed in. */
1100 if (announce_string != NULL)
1101 printf("%s%d: %s\n", periph->periph_name,
1102 periph->unit_number, announce_string);
1106 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1109 struct ccb_dev_advinfo cdai;
1111 memset(&cdai, 0, sizeof(cdai));
1112 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1113 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1116 if (!strcmp(attr, "GEOM::ident"))
1117 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1118 else if (!strcmp(attr, "GEOM::physpath"))
1119 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1123 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1124 if (cdai.buf == NULL) {
1128 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1129 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1130 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1131 if (cdai.provsiz == 0)
1134 if (strlcpy(buf, cdai.buf, len) >= len)
1138 if (cdai.buf != NULL)
1139 free(cdai.buf, M_CAMXPT);
1143 static dev_match_ret
1144 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1147 dev_match_ret retval;
1150 retval = DM_RET_NONE;
1153 * If we aren't given something to match against, that's an error.
1156 return(DM_RET_ERROR);
1159 * If there are no match entries, then this bus matches no
1162 if ((patterns == NULL) || (num_patterns == 0))
1163 return(DM_RET_DESCEND | DM_RET_COPY);
1165 for (i = 0; i < num_patterns; i++) {
1166 struct bus_match_pattern *cur_pattern;
1169 * If the pattern in question isn't for a bus node, we
1170 * aren't interested. However, we do indicate to the
1171 * calling routine that we should continue descending the
1172 * tree, since the user wants to match against lower-level
1175 if (patterns[i].type != DEV_MATCH_BUS) {
1176 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1177 retval |= DM_RET_DESCEND;
1181 cur_pattern = &patterns[i].pattern.bus_pattern;
1184 * If they want to match any bus node, we give them any
1187 if (cur_pattern->flags == BUS_MATCH_ANY) {
1188 /* set the copy flag */
1189 retval |= DM_RET_COPY;
1192 * If we've already decided on an action, go ahead
1195 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1200 * Not sure why someone would do this...
1202 if (cur_pattern->flags == BUS_MATCH_NONE)
1205 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1206 && (cur_pattern->path_id != bus->path_id))
1209 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1210 && (cur_pattern->bus_id != bus->sim->bus_id))
1213 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1214 && (cur_pattern->unit_number != bus->sim->unit_number))
1217 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1218 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1223 * If we get to this point, the user definitely wants
1224 * information on this bus. So tell the caller to copy the
1227 retval |= DM_RET_COPY;
1230 * If the return action has been set to descend, then we
1231 * know that we've already seen a non-bus matching
1232 * expression, therefore we need to further descend the tree.
1233 * This won't change by continuing around the loop, so we
1234 * go ahead and return. If we haven't seen a non-bus
1235 * matching expression, we keep going around the loop until
1236 * we exhaust the matching expressions. We'll set the stop
1237 * flag once we fall out of the loop.
1239 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1244 * If the return action hasn't been set to descend yet, that means
1245 * we haven't seen anything other than bus matching patterns. So
1246 * tell the caller to stop descending the tree -- the user doesn't
1247 * want to match against lower level tree elements.
1249 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1250 retval |= DM_RET_STOP;
1255 static dev_match_ret
1256 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1257 struct cam_ed *device)
1259 dev_match_ret retval;
1262 retval = DM_RET_NONE;
1265 * If we aren't given something to match against, that's an error.
1268 return(DM_RET_ERROR);
1271 * If there are no match entries, then this device matches no
1274 if ((patterns == NULL) || (num_patterns == 0))
1275 return(DM_RET_DESCEND | DM_RET_COPY);
1277 for (i = 0; i < num_patterns; i++) {
1278 struct device_match_pattern *cur_pattern;
1279 struct scsi_vpd_device_id *device_id_page;
1282 * If the pattern in question isn't for a device node, we
1283 * aren't interested.
1285 if (patterns[i].type != DEV_MATCH_DEVICE) {
1286 if ((patterns[i].type == DEV_MATCH_PERIPH)
1287 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1288 retval |= DM_RET_DESCEND;
1292 cur_pattern = &patterns[i].pattern.device_pattern;
1294 /* Error out if mutually exclusive options are specified. */
1295 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1296 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1297 return(DM_RET_ERROR);
1300 * If they want to match any device node, we give them any
1303 if (cur_pattern->flags == DEV_MATCH_ANY)
1307 * Not sure why someone would do this...
1309 if (cur_pattern->flags == DEV_MATCH_NONE)
1312 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1313 && (cur_pattern->path_id != device->target->bus->path_id))
1316 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1317 && (cur_pattern->target_id != device->target->target_id))
1320 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1321 && (cur_pattern->target_lun != device->lun_id))
1324 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1325 && (cam_quirkmatch((caddr_t)&device->inq_data,
1326 (caddr_t)&cur_pattern->data.inq_pat,
1327 1, sizeof(cur_pattern->data.inq_pat),
1328 scsi_static_inquiry_match) == NULL))
1331 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1332 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1333 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1334 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1335 device->device_id_len
1336 - SVPD_DEVICE_ID_HDR_LEN,
1337 cur_pattern->data.devid_pat.id,
1338 cur_pattern->data.devid_pat.id_len) != 0))
1343 * If we get to this point, the user definitely wants
1344 * information on this device. So tell the caller to copy
1347 retval |= DM_RET_COPY;
1350 * If the return action has been set to descend, then we
1351 * know that we've already seen a peripheral matching
1352 * expression, therefore we need to further descend the tree.
1353 * This won't change by continuing around the loop, so we
1354 * go ahead and return. If we haven't seen a peripheral
1355 * matching expression, we keep going around the loop until
1356 * we exhaust the matching expressions. We'll set the stop
1357 * flag once we fall out of the loop.
1359 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1364 * If the return action hasn't been set to descend yet, that means
1365 * we haven't seen any peripheral matching patterns. So tell the
1366 * caller to stop descending the tree -- the user doesn't want to
1367 * match against lower level tree elements.
1369 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1370 retval |= DM_RET_STOP;
1376 * Match a single peripheral against any number of match patterns.
1378 static dev_match_ret
1379 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1380 struct cam_periph *periph)
1382 dev_match_ret retval;
1386 * If we aren't given something to match against, that's an error.
1389 return(DM_RET_ERROR);
1392 * If there are no match entries, then this peripheral matches no
1395 if ((patterns == NULL) || (num_patterns == 0))
1396 return(DM_RET_STOP | DM_RET_COPY);
1399 * There aren't any nodes below a peripheral node, so there's no
1400 * reason to descend the tree any further.
1402 retval = DM_RET_STOP;
1404 for (i = 0; i < num_patterns; i++) {
1405 struct periph_match_pattern *cur_pattern;
1408 * If the pattern in question isn't for a peripheral, we
1409 * aren't interested.
1411 if (patterns[i].type != DEV_MATCH_PERIPH)
1414 cur_pattern = &patterns[i].pattern.periph_pattern;
1417 * If they want to match on anything, then we will do so.
1419 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1420 /* set the copy flag */
1421 retval |= DM_RET_COPY;
1424 * We've already set the return action to stop,
1425 * since there are no nodes below peripherals in
1432 * Not sure why someone would do this...
1434 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1437 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1438 && (cur_pattern->path_id != periph->path->bus->path_id))
1442 * For the target and lun id's, we have to make sure the
1443 * target and lun pointers aren't NULL. The xpt peripheral
1444 * has a wildcard target and device.
1446 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1447 && ((periph->path->target == NULL)
1448 ||(cur_pattern->target_id != periph->path->target->target_id)))
1451 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1452 && ((periph->path->device == NULL)
1453 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1456 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1457 && (cur_pattern->unit_number != periph->unit_number))
1460 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1461 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1466 * If we get to this point, the user definitely wants
1467 * information on this peripheral. So tell the caller to
1468 * copy the data out.
1470 retval |= DM_RET_COPY;
1473 * The return action has already been set to stop, since
1474 * peripherals don't have any nodes below them in the EDT.
1480 * If we get to this point, the peripheral that was passed in
1481 * doesn't match any of the patterns.
1487 xptedtbusfunc(struct cam_eb *bus, void *arg)
1489 struct ccb_dev_match *cdm;
1490 dev_match_ret retval;
1492 cdm = (struct ccb_dev_match *)arg;
1495 * If our position is for something deeper in the tree, that means
1496 * that we've already seen this node. So, we keep going down.
1498 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1499 && (cdm->pos.cookie.bus == bus)
1500 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1501 && (cdm->pos.cookie.target != NULL))
1502 retval = DM_RET_DESCEND;
1504 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1507 * If we got an error, bail out of the search.
1509 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1510 cdm->status = CAM_DEV_MATCH_ERROR;
1515 * If the copy flag is set, copy this bus out.
1517 if (retval & DM_RET_COPY) {
1520 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1521 sizeof(struct dev_match_result));
1524 * If we don't have enough space to put in another
1525 * match result, save our position and tell the
1526 * user there are more devices to check.
1528 if (spaceleft < sizeof(struct dev_match_result)) {
1529 bzero(&cdm->pos, sizeof(cdm->pos));
1530 cdm->pos.position_type =
1531 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1533 cdm->pos.cookie.bus = bus;
1534 cdm->pos.generations[CAM_BUS_GENERATION]=
1535 xsoftc.bus_generation;
1536 cdm->status = CAM_DEV_MATCH_MORE;
1539 j = cdm->num_matches;
1541 cdm->matches[j].type = DEV_MATCH_BUS;
1542 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1543 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1544 cdm->matches[j].result.bus_result.unit_number =
1545 bus->sim->unit_number;
1546 strncpy(cdm->matches[j].result.bus_result.dev_name,
1547 bus->sim->sim_name, DEV_IDLEN);
1551 * If the user is only interested in busses, there's no
1552 * reason to descend to the next level in the tree.
1554 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1558 * If there is a target generation recorded, check it to
1559 * make sure the target list hasn't changed.
1561 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1562 && (bus == cdm->pos.cookie.bus)
1563 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1564 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1565 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1567 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1571 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1572 && (cdm->pos.cookie.bus == bus)
1573 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1574 && (cdm->pos.cookie.target != NULL))
1575 return(xpttargettraverse(bus,
1576 (struct cam_et *)cdm->pos.cookie.target,
1577 xptedttargetfunc, arg));
1579 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1583 xptedttargetfunc(struct cam_et *target, void *arg)
1585 struct ccb_dev_match *cdm;
1587 cdm = (struct ccb_dev_match *)arg;
1590 * If there is a device list generation recorded, check it to
1591 * make sure the device list hasn't changed.
1593 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1594 && (cdm->pos.cookie.bus == target->bus)
1595 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1596 && (cdm->pos.cookie.target == target)
1597 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1598 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1599 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1600 target->generation)) {
1601 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1605 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1606 && (cdm->pos.cookie.bus == target->bus)
1607 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1608 && (cdm->pos.cookie.target == target)
1609 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1610 && (cdm->pos.cookie.device != NULL))
1611 return(xptdevicetraverse(target,
1612 (struct cam_ed *)cdm->pos.cookie.device,
1613 xptedtdevicefunc, arg));
1615 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1619 xptedtdevicefunc(struct cam_ed *device, void *arg)
1622 struct ccb_dev_match *cdm;
1623 dev_match_ret retval;
1625 cdm = (struct ccb_dev_match *)arg;
1628 * If our position is for something deeper in the tree, that means
1629 * that we've already seen this node. So, we keep going down.
1631 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1632 && (cdm->pos.cookie.device == device)
1633 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1634 && (cdm->pos.cookie.periph != NULL))
1635 retval = DM_RET_DESCEND;
1637 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1640 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1641 cdm->status = CAM_DEV_MATCH_ERROR;
1646 * If the copy flag is set, copy this device out.
1648 if (retval & DM_RET_COPY) {
1651 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1652 sizeof(struct dev_match_result));
1655 * If we don't have enough space to put in another
1656 * match result, save our position and tell the
1657 * user there are more devices to check.
1659 if (spaceleft < sizeof(struct dev_match_result)) {
1660 bzero(&cdm->pos, sizeof(cdm->pos));
1661 cdm->pos.position_type =
1662 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1663 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1665 cdm->pos.cookie.bus = device->target->bus;
1666 cdm->pos.generations[CAM_BUS_GENERATION]=
1667 xsoftc.bus_generation;
1668 cdm->pos.cookie.target = device->target;
1669 cdm->pos.generations[CAM_TARGET_GENERATION] =
1670 device->target->bus->generation;
1671 cdm->pos.cookie.device = device;
1672 cdm->pos.generations[CAM_DEV_GENERATION] =
1673 device->target->generation;
1674 cdm->status = CAM_DEV_MATCH_MORE;
1677 j = cdm->num_matches;
1679 cdm->matches[j].type = DEV_MATCH_DEVICE;
1680 cdm->matches[j].result.device_result.path_id =
1681 device->target->bus->path_id;
1682 cdm->matches[j].result.device_result.target_id =
1683 device->target->target_id;
1684 cdm->matches[j].result.device_result.target_lun =
1686 cdm->matches[j].result.device_result.protocol =
1688 bcopy(&device->inq_data,
1689 &cdm->matches[j].result.device_result.inq_data,
1690 sizeof(struct scsi_inquiry_data));
1691 bcopy(&device->ident_data,
1692 &cdm->matches[j].result.device_result.ident_data,
1693 sizeof(struct ata_params));
1695 /* Let the user know whether this device is unconfigured */
1696 if (device->flags & CAM_DEV_UNCONFIGURED)
1697 cdm->matches[j].result.device_result.flags =
1698 DEV_RESULT_UNCONFIGURED;
1700 cdm->matches[j].result.device_result.flags =
1705 * If the user isn't interested in peripherals, don't descend
1706 * the tree any further.
1708 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1712 * If there is a peripheral list generation recorded, make sure
1713 * it hasn't changed.
1715 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1716 && (device->target->bus == cdm->pos.cookie.bus)
1717 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1718 && (device->target == cdm->pos.cookie.target)
1719 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1720 && (device == cdm->pos.cookie.device)
1721 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1722 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1723 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1724 device->generation)){
1725 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1729 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1730 && (cdm->pos.cookie.bus == device->target->bus)
1731 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1732 && (cdm->pos.cookie.target == device->target)
1733 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1734 && (cdm->pos.cookie.device == device)
1735 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1736 && (cdm->pos.cookie.periph != NULL))
1737 return(xptperiphtraverse(device,
1738 (struct cam_periph *)cdm->pos.cookie.periph,
1739 xptedtperiphfunc, arg));
1741 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1745 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1747 struct ccb_dev_match *cdm;
1748 dev_match_ret retval;
1750 cdm = (struct ccb_dev_match *)arg;
1752 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1754 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1755 cdm->status = CAM_DEV_MATCH_ERROR;
1760 * If the copy flag is set, copy this peripheral out.
1762 if (retval & DM_RET_COPY) {
1765 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1766 sizeof(struct dev_match_result));
1769 * If we don't have enough space to put in another
1770 * match result, save our position and tell the
1771 * user there are more devices to check.
1773 if (spaceleft < sizeof(struct dev_match_result)) {
1774 bzero(&cdm->pos, sizeof(cdm->pos));
1775 cdm->pos.position_type =
1776 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1777 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1780 cdm->pos.cookie.bus = periph->path->bus;
1781 cdm->pos.generations[CAM_BUS_GENERATION]=
1782 xsoftc.bus_generation;
1783 cdm->pos.cookie.target = periph->path->target;
1784 cdm->pos.generations[CAM_TARGET_GENERATION] =
1785 periph->path->bus->generation;
1786 cdm->pos.cookie.device = periph->path->device;
1787 cdm->pos.generations[CAM_DEV_GENERATION] =
1788 periph->path->target->generation;
1789 cdm->pos.cookie.periph = periph;
1790 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1791 periph->path->device->generation;
1792 cdm->status = CAM_DEV_MATCH_MORE;
1796 j = cdm->num_matches;
1798 cdm->matches[j].type = DEV_MATCH_PERIPH;
1799 cdm->matches[j].result.periph_result.path_id =
1800 periph->path->bus->path_id;
1801 cdm->matches[j].result.periph_result.target_id =
1802 periph->path->target->target_id;
1803 cdm->matches[j].result.periph_result.target_lun =
1804 periph->path->device->lun_id;
1805 cdm->matches[j].result.periph_result.unit_number =
1806 periph->unit_number;
1807 strncpy(cdm->matches[j].result.periph_result.periph_name,
1808 periph->periph_name, DEV_IDLEN);
1815 xptedtmatch(struct ccb_dev_match *cdm)
1819 cdm->num_matches = 0;
1822 * Check the bus list generation. If it has changed, the user
1823 * needs to reset everything and start over.
1825 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1826 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1827 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1828 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1832 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1833 && (cdm->pos.cookie.bus != NULL))
1834 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1835 xptedtbusfunc, cdm);
1837 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1840 * If we get back 0, that means that we had to stop before fully
1841 * traversing the EDT. It also means that one of the subroutines
1842 * has set the status field to the proper value. If we get back 1,
1843 * we've fully traversed the EDT and copied out any matching entries.
1846 cdm->status = CAM_DEV_MATCH_LAST;
1852 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1854 struct ccb_dev_match *cdm;
1856 cdm = (struct ccb_dev_match *)arg;
1858 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1859 && (cdm->pos.cookie.pdrv == pdrv)
1860 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1861 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1862 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1863 (*pdrv)->generation)) {
1864 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1868 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1869 && (cdm->pos.cookie.pdrv == pdrv)
1870 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1871 && (cdm->pos.cookie.periph != NULL))
1872 return(xptpdperiphtraverse(pdrv,
1873 (struct cam_periph *)cdm->pos.cookie.periph,
1874 xptplistperiphfunc, arg));
1876 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1880 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1882 struct ccb_dev_match *cdm;
1883 dev_match_ret retval;
1885 cdm = (struct ccb_dev_match *)arg;
1887 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1889 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1890 cdm->status = CAM_DEV_MATCH_ERROR;
1895 * If the copy flag is set, copy this peripheral out.
1897 if (retval & DM_RET_COPY) {
1900 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1901 sizeof(struct dev_match_result));
1904 * If we don't have enough space to put in another
1905 * match result, save our position and tell the
1906 * user there are more devices to check.
1908 if (spaceleft < sizeof(struct dev_match_result)) {
1909 struct periph_driver **pdrv;
1912 bzero(&cdm->pos, sizeof(cdm->pos));
1913 cdm->pos.position_type =
1914 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1918 * This may look a bit non-sensical, but it is
1919 * actually quite logical. There are very few
1920 * peripheral drivers, and bloating every peripheral
1921 * structure with a pointer back to its parent
1922 * peripheral driver linker set entry would cost
1923 * more in the long run than doing this quick lookup.
1925 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1926 if (strcmp((*pdrv)->driver_name,
1927 periph->periph_name) == 0)
1931 if (*pdrv == NULL) {
1932 cdm->status = CAM_DEV_MATCH_ERROR;
1936 cdm->pos.cookie.pdrv = pdrv;
1938 * The periph generation slot does double duty, as
1939 * does the periph pointer slot. They are used for
1940 * both edt and pdrv lookups and positioning.
1942 cdm->pos.cookie.periph = periph;
1943 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1944 (*pdrv)->generation;
1945 cdm->status = CAM_DEV_MATCH_MORE;
1949 j = cdm->num_matches;
1951 cdm->matches[j].type = DEV_MATCH_PERIPH;
1952 cdm->matches[j].result.periph_result.path_id =
1953 periph->path->bus->path_id;
1956 * The transport layer peripheral doesn't have a target or
1959 if (periph->path->target)
1960 cdm->matches[j].result.periph_result.target_id =
1961 periph->path->target->target_id;
1963 cdm->matches[j].result.periph_result.target_id = -1;
1965 if (periph->path->device)
1966 cdm->matches[j].result.periph_result.target_lun =
1967 periph->path->device->lun_id;
1969 cdm->matches[j].result.periph_result.target_lun = -1;
1971 cdm->matches[j].result.periph_result.unit_number =
1972 periph->unit_number;
1973 strncpy(cdm->matches[j].result.periph_result.periph_name,
1974 periph->periph_name, DEV_IDLEN);
1981 xptperiphlistmatch(struct ccb_dev_match *cdm)
1985 cdm->num_matches = 0;
1988 * At this point in the edt traversal function, we check the bus
1989 * list generation to make sure that no busses have been added or
1990 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1991 * For the peripheral driver list traversal function, however, we
1992 * don't have to worry about new peripheral driver types coming or
1993 * going; they're in a linker set, and therefore can't change
1994 * without a recompile.
1997 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1998 && (cdm->pos.cookie.pdrv != NULL))
1999 ret = xptpdrvtraverse(
2000 (struct periph_driver **)cdm->pos.cookie.pdrv,
2001 xptplistpdrvfunc, cdm);
2003 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2006 * If we get back 0, that means that we had to stop before fully
2007 * traversing the peripheral driver tree. It also means that one of
2008 * the subroutines has set the status field to the proper value. If
2009 * we get back 1, we've fully traversed the EDT and copied out any
2013 cdm->status = CAM_DEV_MATCH_LAST;
2019 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2021 struct cam_eb *bus, *next_bus;
2026 mtx_lock(&xsoftc.xpt_topo_lock);
2027 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2034 * XXX The locking here is obviously very complex. We
2035 * should work to simplify it.
2037 mtx_unlock(&xsoftc.xpt_topo_lock);
2038 CAM_SIM_LOCK(bus->sim);
2039 retval = tr_func(bus, arg);
2040 CAM_SIM_UNLOCK(bus->sim);
2042 mtx_lock(&xsoftc.xpt_topo_lock);
2043 next_bus = TAILQ_NEXT(bus, links);
2044 mtx_unlock(&xsoftc.xpt_topo_lock);
2046 xpt_release_bus(bus);
2050 mtx_lock(&xsoftc.xpt_topo_lock);
2052 mtx_unlock(&xsoftc.xpt_topo_lock);
2058 xpt_sim_opened(struct cam_sim *sim)
2061 struct cam_et *target;
2062 struct cam_ed *device;
2063 struct cam_periph *periph;
2065 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2066 mtx_assert(sim->mtx, MA_OWNED);
2068 mtx_lock(&xsoftc.xpt_topo_lock);
2069 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2070 if (bus->sim != sim)
2073 TAILQ_FOREACH(target, &bus->et_entries, links) {
2074 TAILQ_FOREACH(device, &target->ed_entries, links) {
2075 SLIST_FOREACH(periph, &device->periphs,
2077 if (periph->refcount > 0) {
2078 mtx_unlock(&xsoftc.xpt_topo_lock);
2086 mtx_unlock(&xsoftc.xpt_topo_lock);
2091 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2092 xpt_targetfunc_t *tr_func, void *arg)
2094 struct cam_et *target, *next_target;
2098 for (target = (start_target ? start_target :
2099 TAILQ_FIRST(&bus->et_entries));
2100 target != NULL; target = next_target) {
2104 retval = tr_func(target, arg);
2106 next_target = TAILQ_NEXT(target, links);
2108 xpt_release_target(target);
2118 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2119 xpt_devicefunc_t *tr_func, void *arg)
2121 struct cam_ed *device, *next_device;
2125 for (device = (start_device ? start_device :
2126 TAILQ_FIRST(&target->ed_entries));
2128 device = next_device) {
2131 * Hold a reference so the current device does not go away
2136 retval = tr_func(device, arg);
2139 * Grab our next pointer before we release the current
2142 next_device = TAILQ_NEXT(device, links);
2144 xpt_release_device(device);
2154 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2155 xpt_periphfunc_t *tr_func, void *arg)
2157 struct cam_periph *periph, *next_periph;
2163 for (periph = (start_periph ? start_periph :
2164 SLIST_FIRST(&device->periphs));
2166 periph = next_periph) {
2170 * In this case, we want to show peripherals that have been
2171 * invalidated, but not peripherals that are scheduled to
2172 * be freed. So instead of calling cam_periph_acquire(),
2173 * which will fail if the periph has been invalidated, we
2174 * just check for the free flag here. If it is free, we
2175 * skip to the next periph.
2177 if (periph->flags & CAM_PERIPH_FREE) {
2178 next_periph = SLIST_NEXT(periph, periph_links);
2183 * Acquire a reference to this periph while we call the
2184 * traversal function, so it can't go away.
2190 retval = tr_func(periph, arg);
2193 * We need the lock for list traversal.
2198 * Grab the next peripheral before we release this one, so
2199 * our next pointer is still valid.
2201 next_periph = SLIST_NEXT(periph, periph_links);
2203 cam_periph_release_locked_buses(periph);
2217 xptpdrvtraverse(struct periph_driver **start_pdrv,
2218 xpt_pdrvfunc_t *tr_func, void *arg)
2220 struct periph_driver **pdrv;
2226 * We don't traverse the peripheral driver list like we do the
2227 * other lists, because it is a linker set, and therefore cannot be
2228 * changed during runtime. If the peripheral driver list is ever
2229 * re-done to be something other than a linker set (i.e. it can
2230 * change while the system is running), the list traversal should
2231 * be modified to work like the other traversal functions.
2233 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2234 *pdrv != NULL; pdrv++) {
2235 retval = tr_func(pdrv, arg);
2245 xptpdperiphtraverse(struct periph_driver **pdrv,
2246 struct cam_periph *start_periph,
2247 xpt_periphfunc_t *tr_func, void *arg)
2249 struct cam_periph *periph, *next_periph;
2255 for (periph = (start_periph ? start_periph :
2256 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2257 periph = next_periph) {
2261 * In this case, we want to show peripherals that have been
2262 * invalidated, but not peripherals that are scheduled to
2263 * be freed. So instead of calling cam_periph_acquire(),
2264 * which will fail if the periph has been invalidated, we
2265 * just check for the free flag here. If it is free, we
2266 * skip to the next periph.
2268 if (periph->flags & CAM_PERIPH_FREE) {
2269 next_periph = TAILQ_NEXT(periph, unit_links);
2274 * Acquire a reference to this periph while we call the
2275 * traversal function, so it can't go away.
2280 * XXX KDM we have the toplogy lock here, but in
2281 * xptperiphtraverse(), we drop it before calling the
2282 * traversal function. Which is correct?
2284 retval = tr_func(periph, arg);
2287 * Grab the next peripheral before we release this one, so
2288 * our next pointer is still valid.
2290 next_periph = TAILQ_NEXT(periph, unit_links);
2292 cam_periph_release_locked_buses(periph);
2305 xptdefbusfunc(struct cam_eb *bus, void *arg)
2307 struct xpt_traverse_config *tr_config;
2309 tr_config = (struct xpt_traverse_config *)arg;
2311 if (tr_config->depth == XPT_DEPTH_BUS) {
2312 xpt_busfunc_t *tr_func;
2314 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2316 return(tr_func(bus, tr_config->tr_arg));
2318 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2322 xptdeftargetfunc(struct cam_et *target, void *arg)
2324 struct xpt_traverse_config *tr_config;
2326 tr_config = (struct xpt_traverse_config *)arg;
2328 if (tr_config->depth == XPT_DEPTH_TARGET) {
2329 xpt_targetfunc_t *tr_func;
2331 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2333 return(tr_func(target, tr_config->tr_arg));
2335 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2339 xptdefdevicefunc(struct cam_ed *device, void *arg)
2341 struct xpt_traverse_config *tr_config;
2343 tr_config = (struct xpt_traverse_config *)arg;
2345 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2346 xpt_devicefunc_t *tr_func;
2348 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2350 return(tr_func(device, tr_config->tr_arg));
2352 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2356 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2358 struct xpt_traverse_config *tr_config;
2359 xpt_periphfunc_t *tr_func;
2361 tr_config = (struct xpt_traverse_config *)arg;
2363 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2366 * Unlike the other default functions, we don't check for depth
2367 * here. The peripheral driver level is the last level in the EDT,
2368 * so if we're here, we should execute the function in question.
2370 return(tr_func(periph, tr_config->tr_arg));
2374 * Execute the given function for every bus in the EDT.
2377 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2379 struct xpt_traverse_config tr_config;
2381 tr_config.depth = XPT_DEPTH_BUS;
2382 tr_config.tr_func = tr_func;
2383 tr_config.tr_arg = arg;
2385 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2389 * Execute the given function for every device in the EDT.
2392 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2394 struct xpt_traverse_config tr_config;
2396 tr_config.depth = XPT_DEPTH_DEVICE;
2397 tr_config.tr_func = tr_func;
2398 tr_config.tr_arg = arg;
2400 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2404 xptsetasyncfunc(struct cam_ed *device, void *arg)
2406 struct cam_path path;
2407 struct ccb_getdev cgd;
2408 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2411 * Don't report unconfigured devices (Wildcard devs,
2412 * devices only for target mode, device instances
2413 * that have been invalidated but are waiting for
2414 * their last reference count to be released).
2416 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2419 xpt_compile_path(&path,
2421 device->target->bus->path_id,
2422 device->target->target_id,
2424 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2425 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2426 xpt_action((union ccb *)&cgd);
2427 csa->callback(csa->callback_arg,
2430 xpt_release_path(&path);
2436 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2438 struct cam_path path;
2439 struct ccb_pathinq cpi;
2440 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2442 xpt_compile_path(&path, /*periph*/NULL,
2444 CAM_TARGET_WILDCARD,
2446 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2447 cpi.ccb_h.func_code = XPT_PATH_INQ;
2448 xpt_action((union ccb *)&cpi);
2449 csa->callback(csa->callback_arg,
2452 xpt_release_path(&path);
2458 xpt_action(union ccb *start_ccb)
2461 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2463 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2464 /* Compatibility for RL-unaware code. */
2465 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2466 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2467 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2471 xpt_action_default(union ccb *start_ccb)
2473 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2474 struct cam_path *path;
2476 path = start_ccb->ccb_h.path;
2477 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2479 switch (start_ccb->ccb_h.func_code) {
2482 struct cam_ed *device;
2485 * For the sake of compatibility with SCSI-1
2486 * devices that may not understand the identify
2487 * message, we include lun information in the
2488 * second byte of all commands. SCSI-1 specifies
2489 * that luns are a 3 bit value and reserves only 3
2490 * bits for lun information in the CDB. Later
2491 * revisions of the SCSI spec allow for more than 8
2492 * luns, but have deprecated lun information in the
2493 * CDB. So, if the lun won't fit, we must omit.
2495 * Also be aware that during initial probing for devices,
2496 * the inquiry information is unknown but initialized to 0.
2497 * This means that this code will be exercised while probing
2498 * devices with an ANSI revision greater than 2.
2500 device = path->device;
2501 if (device->protocol_version <= SCSI_REV_2
2502 && start_ccb->ccb_h.target_lun < 8
2503 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2505 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2506 start_ccb->ccb_h.target_lun << 5;
2508 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2509 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2510 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2511 &path->device->inq_data),
2512 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2513 cdb_str, sizeof(cdb_str))));
2517 case XPT_CONT_TARGET_IO:
2518 start_ccb->csio.sense_resid = 0;
2519 start_ccb->csio.resid = 0;
2522 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2523 start_ccb->ataio.resid = 0;
2524 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2525 ata_op_string(&start_ccb->ataio.cmd),
2526 ata_cmd_string(&start_ccb->ataio.cmd,
2527 cdb_str, sizeof(cdb_str))));
2536 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2537 path->device->sim->devq->alloc_openings += frozen;
2539 xpt_run_dev_allocq(path->bus);
2540 if (xpt_schedule_dev_sendq(path->bus, path->device))
2541 xpt_run_dev_sendq(path->bus);
2544 case XPT_CALC_GEOMETRY:
2546 struct cam_sim *sim;
2548 /* Filter out garbage */
2549 if (start_ccb->ccg.block_size == 0
2550 || start_ccb->ccg.volume_size == 0) {
2551 start_ccb->ccg.cylinders = 0;
2552 start_ccb->ccg.heads = 0;
2553 start_ccb->ccg.secs_per_track = 0;
2554 start_ccb->ccb_h.status = CAM_REQ_CMP;
2557 #if defined(PC98) || defined(__sparc64__)
2559 * In a PC-98 system, geometry translation depens on
2560 * the "real" device geometry obtained from mode page 4.
2561 * SCSI geometry translation is performed in the
2562 * initialization routine of the SCSI BIOS and the result
2563 * stored in host memory. If the translation is available
2564 * in host memory, use it. If not, rely on the default
2565 * translation the device driver performs.
2566 * For sparc64, we may need adjust the geometry of large
2567 * disks in order to fit the limitations of the 16-bit
2568 * fields of the VTOC8 disk label.
2570 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2571 start_ccb->ccb_h.status = CAM_REQ_CMP;
2575 sim = path->bus->sim;
2576 (*(sim->sim_action))(sim, start_ccb);
2581 union ccb* abort_ccb;
2583 abort_ccb = start_ccb->cab.abort_ccb;
2584 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2586 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2587 struct cam_ccbq *ccbq;
2588 struct cam_ed *device;
2590 device = abort_ccb->ccb_h.path->device;
2591 ccbq = &device->ccbq;
2592 device->sim->devq->alloc_openings -=
2593 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2594 abort_ccb->ccb_h.status =
2595 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2596 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2597 xpt_done(abort_ccb);
2598 start_ccb->ccb_h.status = CAM_REQ_CMP;
2601 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2602 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2604 * We've caught this ccb en route to
2605 * the SIM. Flag it for abort and the
2606 * SIM will do so just before starting
2607 * real work on the CCB.
2609 abort_ccb->ccb_h.status =
2610 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2611 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2612 start_ccb->ccb_h.status = CAM_REQ_CMP;
2616 if (XPT_FC_IS_QUEUED(abort_ccb)
2617 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2619 * It's already completed but waiting
2620 * for our SWI to get to it.
2622 start_ccb->ccb_h.status = CAM_UA_ABORT;
2626 * If we weren't able to take care of the abort request
2627 * in the XPT, pass the request down to the SIM for processing.
2631 case XPT_ACCEPT_TARGET_IO:
2633 case XPT_IMMED_NOTIFY:
2634 case XPT_NOTIFY_ACK:
2636 case XPT_IMMEDIATE_NOTIFY:
2637 case XPT_NOTIFY_ACKNOWLEDGE:
2638 case XPT_GET_SIM_KNOB:
2639 case XPT_SET_SIM_KNOB:
2641 struct cam_sim *sim;
2643 sim = path->bus->sim;
2644 (*(sim->sim_action))(sim, start_ccb);
2649 struct cam_sim *sim;
2651 sim = path->bus->sim;
2652 (*(sim->sim_action))(sim, start_ccb);
2655 case XPT_PATH_STATS:
2656 start_ccb->cpis.last_reset = path->bus->last_reset;
2657 start_ccb->ccb_h.status = CAM_REQ_CMP;
2664 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2665 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2667 struct ccb_getdev *cgd;
2669 cgd = &start_ccb->cgd;
2670 cgd->protocol = dev->protocol;
2671 cgd->inq_data = dev->inq_data;
2672 cgd->ident_data = dev->ident_data;
2673 cgd->inq_flags = dev->inq_flags;
2674 cgd->ccb_h.status = CAM_REQ_CMP;
2675 cgd->serial_num_len = dev->serial_num_len;
2676 if ((dev->serial_num_len > 0)
2677 && (dev->serial_num != NULL))
2678 bcopy(dev->serial_num, cgd->serial_num,
2679 dev->serial_num_len);
2683 case XPT_GDEV_STATS:
2688 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2689 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2691 struct ccb_getdevstats *cgds;
2695 cgds = &start_ccb->cgds;
2698 cgds->dev_openings = dev->ccbq.dev_openings;
2699 cgds->dev_active = dev->ccbq.dev_active;
2700 cgds->devq_openings = dev->ccbq.devq_openings;
2701 cgds->devq_queued = dev->ccbq.queue.entries;
2702 cgds->held = dev->ccbq.held;
2703 cgds->last_reset = tar->last_reset;
2704 cgds->maxtags = dev->maxtags;
2705 cgds->mintags = dev->mintags;
2706 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2707 cgds->last_reset = bus->last_reset;
2708 cgds->ccb_h.status = CAM_REQ_CMP;
2714 struct cam_periph *nperiph;
2715 struct periph_list *periph_head;
2716 struct ccb_getdevlist *cgdl;
2718 struct cam_ed *device;
2725 * Don't want anyone mucking with our data.
2727 device = path->device;
2728 periph_head = &device->periphs;
2729 cgdl = &start_ccb->cgdl;
2732 * Check and see if the list has changed since the user
2733 * last requested a list member. If so, tell them that the
2734 * list has changed, and therefore they need to start over
2735 * from the beginning.
2737 if ((cgdl->index != 0) &&
2738 (cgdl->generation != device->generation)) {
2739 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2744 * Traverse the list of peripherals and attempt to find
2745 * the requested peripheral.
2747 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2748 (nperiph != NULL) && (i <= cgdl->index);
2749 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2750 if (i == cgdl->index) {
2751 strncpy(cgdl->periph_name,
2752 nperiph->periph_name,
2754 cgdl->unit_number = nperiph->unit_number;
2759 cgdl->status = CAM_GDEVLIST_ERROR;
2763 if (nperiph == NULL)
2764 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2766 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2769 cgdl->generation = device->generation;
2771 cgdl->ccb_h.status = CAM_REQ_CMP;
2776 dev_pos_type position_type;
2777 struct ccb_dev_match *cdm;
2779 cdm = &start_ccb->cdm;
2782 * There are two ways of getting at information in the EDT.
2783 * The first way is via the primary EDT tree. It starts
2784 * with a list of busses, then a list of targets on a bus,
2785 * then devices/luns on a target, and then peripherals on a
2786 * device/lun. The "other" way is by the peripheral driver
2787 * lists. The peripheral driver lists are organized by
2788 * peripheral driver. (obviously) So it makes sense to
2789 * use the peripheral driver list if the user is looking
2790 * for something like "da1", or all "da" devices. If the
2791 * user is looking for something on a particular bus/target
2792 * or lun, it's generally better to go through the EDT tree.
2795 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2796 position_type = cdm->pos.position_type;
2800 position_type = CAM_DEV_POS_NONE;
2802 for (i = 0; i < cdm->num_patterns; i++) {
2803 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2804 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2805 position_type = CAM_DEV_POS_EDT;
2810 if (cdm->num_patterns == 0)
2811 position_type = CAM_DEV_POS_EDT;
2812 else if (position_type == CAM_DEV_POS_NONE)
2813 position_type = CAM_DEV_POS_PDRV;
2816 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2817 case CAM_DEV_POS_EDT:
2820 case CAM_DEV_POS_PDRV:
2821 xptperiphlistmatch(cdm);
2824 cdm->status = CAM_DEV_MATCH_ERROR;
2828 if (cdm->status == CAM_DEV_MATCH_ERROR)
2829 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2831 start_ccb->ccb_h.status = CAM_REQ_CMP;
2837 struct ccb_setasync *csa;
2838 struct async_node *cur_entry;
2839 struct async_list *async_head;
2842 csa = &start_ccb->csa;
2843 added = csa->event_enable;
2844 async_head = &path->device->asyncs;
2847 * If there is already an entry for us, simply
2850 cur_entry = SLIST_FIRST(async_head);
2851 while (cur_entry != NULL) {
2852 if ((cur_entry->callback_arg == csa->callback_arg)
2853 && (cur_entry->callback == csa->callback))
2855 cur_entry = SLIST_NEXT(cur_entry, links);
2858 if (cur_entry != NULL) {
2860 * If the request has no flags set,
2863 added &= ~cur_entry->event_enable;
2864 if (csa->event_enable == 0) {
2865 SLIST_REMOVE(async_head, cur_entry,
2867 xpt_release_device(path->device);
2868 free(cur_entry, M_CAMXPT);
2870 cur_entry->event_enable = csa->event_enable;
2872 csa->event_enable = added;
2874 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2876 if (cur_entry == NULL) {
2877 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2880 cur_entry->event_enable = csa->event_enable;
2881 cur_entry->callback_arg = csa->callback_arg;
2882 cur_entry->callback = csa->callback;
2883 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2884 xpt_acquire_device(path->device);
2886 start_ccb->ccb_h.status = CAM_REQ_CMP;
2891 struct ccb_relsim *crs;
2894 crs = &start_ccb->crs;
2898 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2902 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2904 /* Don't ever go below one opening */
2905 if (crs->openings > 0) {
2906 xpt_dev_ccbq_resize(path, crs->openings);
2909 "number of openings is now %d\n",
2915 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2917 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2920 * Just extend the old timeout and decrement
2921 * the freeze count so that a single timeout
2922 * is sufficient for releasing the queue.
2924 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2925 callout_stop(&dev->callout);
2928 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2931 callout_reset(&dev->callout,
2932 (crs->release_timeout * hz) / 1000,
2933 xpt_release_devq_timeout, dev);
2935 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2939 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2941 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2943 * Decrement the freeze count so that a single
2944 * completion is still sufficient to unfreeze
2947 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2950 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2951 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2955 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2957 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2958 || (dev->ccbq.dev_active == 0)) {
2960 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2963 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2964 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2968 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2969 xpt_release_devq_rl(path, /*runlevel*/
2970 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2971 crs->release_timeout : 0,
2972 /*count*/1, /*run_queue*/TRUE);
2974 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2975 start_ccb->ccb_h.status = CAM_REQ_CMP;
2979 /* Check that all request bits are supported. */
2980 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2981 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2985 cam_dflags = start_ccb->cdbg.flags;
2986 if (cam_dpath != NULL) {
2987 xpt_free_path(cam_dpath);
2990 if (cam_dflags != CAM_DEBUG_NONE) {
2991 if (xpt_create_path(&cam_dpath, xpt_periph,
2992 start_ccb->ccb_h.path_id,
2993 start_ccb->ccb_h.target_id,
2994 start_ccb->ccb_h.target_lun) !=
2996 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2997 cam_dflags = CAM_DEBUG_NONE;
2999 start_ccb->ccb_h.status = CAM_REQ_CMP;
3000 xpt_print(cam_dpath, "debugging flags now %x\n",
3005 start_ccb->ccb_h.status = CAM_REQ_CMP;
3009 case XPT_FREEZE_QUEUE:
3011 struct ccb_relsim *crs = &start_ccb->crs;
3013 xpt_freeze_devq_rl(path, /*runlevel*/
3014 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3015 crs->release_timeout : 0, /*count*/1);
3016 start_ccb->ccb_h.status = CAM_REQ_CMP;
3020 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3021 xpt_freeze_devq(path, 1);
3022 start_ccb->ccb_h.status = CAM_REQ_CMP;
3029 printf("%s: CCB type %#x not supported\n", __func__,
3030 start_ccb->ccb_h.func_code);
3031 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3032 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3033 xpt_done(start_ccb);
3040 xpt_polled_action(union ccb *start_ccb)
3043 struct cam_sim *sim;
3044 struct cam_devq *devq;
3048 timeout = start_ccb->ccb_h.timeout * 10;
3049 sim = start_ccb->ccb_h.path->bus->sim;
3051 dev = start_ccb->ccb_h.path->device;
3053 mtx_assert(sim->mtx, MA_OWNED);
3055 /* Don't use ISR for this SIM while polling. */
3056 sim->flags |= CAM_SIM_POLLED;
3059 * Steal an opening so that no other queued requests
3060 * can get it before us while we simulate interrupts.
3062 dev->ccbq.devq_openings--;
3063 dev->ccbq.dev_openings--;
3065 while(((devq != NULL && devq->send_openings <= 0) ||
3066 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3068 (*(sim->sim_poll))(sim);
3069 camisr_runqueue(&sim->sim_doneq);
3072 dev->ccbq.devq_openings++;
3073 dev->ccbq.dev_openings++;
3076 xpt_action(start_ccb);
3077 while(--timeout > 0) {
3078 (*(sim->sim_poll))(sim);
3079 camisr_runqueue(&sim->sim_doneq);
3080 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3087 * XXX Is it worth adding a sim_timeout entry
3088 * point so we can attempt recovery? If
3089 * this is only used for dumps, I don't think
3092 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3095 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3098 /* We will use CAM ISR for this SIM again. */
3099 sim->flags &= ~CAM_SIM_POLLED;
3103 * Schedule a peripheral driver to receive a ccb when it's
3104 * target device has space for more transactions.
3107 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3109 struct cam_ed *device;
3112 mtx_assert(perph->sim->mtx, MA_OWNED);
3114 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3115 device = perph->path->device;
3116 if (periph_is_queued(perph)) {
3117 /* Simply reorder based on new priority */
3118 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3119 (" change priority to %d\n", new_priority));
3120 if (new_priority < perph->pinfo.priority) {
3121 camq_change_priority(&device->drvq,
3124 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3127 /* New entry on the queue */
3128 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3129 (" added periph to queue\n"));
3130 perph->pinfo.priority = new_priority;
3131 perph->pinfo.generation = ++device->drvq.generation;
3132 camq_insert(&device->drvq, &perph->pinfo);
3133 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3136 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3137 (" calling xpt_run_devq\n"));
3138 xpt_run_dev_allocq(perph->path->bus);
3144 * Schedule a device to run on a given queue.
3145 * If the device was inserted as a new entry on the queue,
3146 * return 1 meaning the device queue should be run. If we
3147 * were already queued, implying someone else has already
3148 * started the queue, return 0 so the caller doesn't attempt
3152 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3153 u_int32_t new_priority)
3156 u_int32_t old_priority;
3158 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3160 old_priority = pinfo->priority;
3163 * Are we already queued?
3165 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3166 /* Simply reorder based on new priority */
3167 if (new_priority < old_priority) {
3168 camq_change_priority(queue, pinfo->index,
3170 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3171 ("changed priority to %d\n",
3177 /* New entry on the queue */
3178 if (new_priority < old_priority)
3179 pinfo->priority = new_priority;
3181 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3182 ("Inserting onto queue\n"));
3183 pinfo->generation = ++queue->generation;
3184 camq_insert(queue, pinfo);
3191 xpt_run_dev_allocq(struct cam_eb *bus)
3193 struct cam_devq *devq;
3195 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3196 devq = bus->sim->devq;
3198 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3199 (" qfrozen_cnt == 0x%x, entries == %d, "
3200 "openings == %d, active == %d\n",
3201 devq->alloc_queue.qfrozen_cnt[0],
3202 devq->alloc_queue.entries,
3203 devq->alloc_openings,
3204 devq->alloc_active));
3206 devq->alloc_queue.qfrozen_cnt[0]++;
3207 while ((devq->alloc_queue.entries > 0)
3208 && (devq->alloc_openings > 0)
3209 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3210 struct cam_ed_qinfo *qinfo;
3211 struct cam_ed *device;
3212 union ccb *work_ccb;
3213 struct cam_periph *drv;
3216 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3218 device = qinfo->device;
3219 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3220 ("running device %p\n", device));
3222 drvq = &device->drvq;
3223 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3224 "Device on queue without any work to do"));
3225 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3226 devq->alloc_openings--;
3227 devq->alloc_active++;
3228 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3229 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3230 drv->pinfo.priority);
3231 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3232 ("calling periph start\n"));
3233 drv->periph_start(drv, work_ccb);
3236 * Malloc failure in alloc_ccb
3239 * XXX add us to a list to be run from free_ccb
3240 * if we don't have any ccbs active on this
3241 * device queue otherwise we may never get run
3247 /* We may have more work. Attempt to reschedule. */
3248 xpt_schedule_dev_allocq(bus, device);
3250 devq->alloc_queue.qfrozen_cnt[0]--;
3254 xpt_run_dev_sendq(struct cam_eb *bus)
3256 struct cam_devq *devq;
3258 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3260 devq = bus->sim->devq;
3262 devq->send_queue.qfrozen_cnt[0]++;
3263 while ((devq->send_queue.entries > 0)
3264 && (devq->send_openings > 0)
3265 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3266 struct cam_ed_qinfo *qinfo;
3267 struct cam_ed *device;
3268 union ccb *work_ccb;
3269 struct cam_sim *sim;
3271 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3273 device = qinfo->device;
3274 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3275 ("running device %p\n", device));
3277 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3278 if (work_ccb == NULL) {
3279 printf("device on run queue with no ccbs???\n");
3283 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3285 mtx_lock(&xsoftc.xpt_lock);
3286 if (xsoftc.num_highpower <= 0) {
3288 * We got a high power command, but we
3289 * don't have any available slots. Freeze
3290 * the device queue until we have a slot
3293 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3294 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3298 mtx_unlock(&xsoftc.xpt_lock);
3302 * Consume a high power slot while
3305 xsoftc.num_highpower--;
3307 mtx_unlock(&xsoftc.xpt_lock);
3309 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3310 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3312 devq->send_openings--;
3313 devq->send_active++;
3315 xpt_schedule_dev_sendq(bus, device);
3317 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3319 * The client wants to freeze the queue
3320 * after this CCB is sent.
3322 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3325 /* In Target mode, the peripheral driver knows best... */
3326 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3327 if ((device->inq_flags & SID_CmdQue) != 0
3328 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3329 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3332 * Clear this in case of a retried CCB that
3333 * failed due to a rejected tag.
3335 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3339 * Device queues can be shared among multiple sim instances
3340 * that reside on different busses. Use the SIM in the queue
3341 * CCB's path, rather than the one in the bus that was passed
3342 * into this function.
3344 sim = work_ccb->ccb_h.path->bus->sim;
3345 (*(sim->sim_action))(sim, work_ccb);
3347 devq->send_queue.qfrozen_cnt[0]--;
3351 * This function merges stuff from the slave ccb into the master ccb, while
3352 * keeping important fields in the master ccb constant.
3355 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3359 * Pull fields that are valid for peripheral drivers to set
3360 * into the master CCB along with the CCB "payload".
3362 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3363 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3364 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3365 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3366 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3367 sizeof(union ccb) - sizeof(struct ccb_hdr));
3371 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3374 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3375 ccb_h->pinfo.priority = priority;
3377 ccb_h->path_id = path->bus->path_id;
3379 ccb_h->target_id = path->target->target_id;
3381 ccb_h->target_id = CAM_TARGET_WILDCARD;
3383 ccb_h->target_lun = path->device->lun_id;
3384 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3386 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3388 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3392 /* Path manipulation functions */
3394 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3395 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3397 struct cam_path *path;
3400 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3403 status = CAM_RESRC_UNAVAIL;
3406 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3407 if (status != CAM_REQ_CMP) {
3408 free(path, M_CAMXPT);
3411 *new_path_ptr = path;
3416 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3417 struct cam_periph *periph, path_id_t path_id,
3418 target_id_t target_id, lun_id_t lun_id)
3420 struct cam_path *path;
3421 struct cam_eb *bus = NULL;
3423 int need_unlock = 0;
3425 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3427 if (path_id != CAM_BUS_WILDCARD) {
3428 bus = xpt_find_bus(path_id);
3431 CAM_SIM_LOCK(bus->sim);
3434 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3436 CAM_SIM_UNLOCK(bus->sim);
3437 xpt_release_bus(bus);
3439 if (status != CAM_REQ_CMP) {
3440 free(path, M_CAMXPT);
3443 *new_path_ptr = path;
3448 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3449 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3452 struct cam_et *target;
3453 struct cam_ed *device;
3456 status = CAM_REQ_CMP; /* Completed without error */
3457 target = NULL; /* Wildcarded */
3458 device = NULL; /* Wildcarded */
3461 * We will potentially modify the EDT, so block interrupts
3462 * that may attempt to create cam paths.
3464 bus = xpt_find_bus(path_id);
3466 status = CAM_PATH_INVALID;
3468 target = xpt_find_target(bus, target_id);
3469 if (target == NULL) {
3471 struct cam_et *new_target;
3473 new_target = xpt_alloc_target(bus, target_id);
3474 if (new_target == NULL) {
3475 status = CAM_RESRC_UNAVAIL;
3477 target = new_target;
3480 if (target != NULL) {
3481 device = xpt_find_device(target, lun_id);
3482 if (device == NULL) {
3484 struct cam_ed *new_device;
3487 (*(bus->xport->alloc_device))(bus,
3490 if (new_device == NULL) {
3491 status = CAM_RESRC_UNAVAIL;
3493 device = new_device;
3500 * Only touch the user's data if we are successful.
3502 if (status == CAM_REQ_CMP) {
3503 new_path->periph = perph;
3504 new_path->bus = bus;
3505 new_path->target = target;
3506 new_path->device = device;
3507 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3510 xpt_release_device(device);
3512 xpt_release_target(target);
3514 xpt_release_bus(bus);
3520 xpt_release_path(struct cam_path *path)
3522 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3523 if (path->device != NULL) {
3524 xpt_release_device(path->device);
3525 path->device = NULL;
3527 if (path->target != NULL) {
3528 xpt_release_target(path->target);
3529 path->target = NULL;
3531 if (path->bus != NULL) {
3532 xpt_release_bus(path->bus);
3538 xpt_free_path(struct cam_path *path)
3541 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3542 xpt_release_path(path);
3543 free(path, M_CAMXPT);
3547 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3548 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3551 mtx_lock(&xsoftc.xpt_topo_lock);
3554 *bus_ref = path->bus->refcount;
3558 mtx_unlock(&xsoftc.xpt_topo_lock);
3561 *periph_ref = path->periph->refcount;
3567 *target_ref = path->target->refcount;
3573 *device_ref = path->device->refcount;
3580 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3581 * in path1, 2 for match with wildcards in path2.
3584 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3588 if (path1->bus != path2->bus) {
3589 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3591 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3596 if (path1->target != path2->target) {
3597 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3600 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3605 if (path1->device != path2->device) {
3606 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3609 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3618 xpt_print_path(struct cam_path *path)
3622 printf("(nopath): ");
3624 if (path->periph != NULL)
3625 printf("(%s%d:", path->periph->periph_name,
3626 path->periph->unit_number);
3628 printf("(noperiph:");
3630 if (path->bus != NULL)
3631 printf("%s%d:%d:", path->bus->sim->sim_name,
3632 path->bus->sim->unit_number,
3633 path->bus->sim->bus_id);
3637 if (path->target != NULL)
3638 printf("%d:", path->target->target_id);
3642 if (path->device != NULL)
3643 printf("%d): ", path->device->lun_id);
3650 xpt_print(struct cam_path *path, const char *fmt, ...)
3653 xpt_print_path(path);
3660 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3665 if (path != NULL && path->bus != NULL)
3666 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3669 sbuf_new(&sb, str, str_len, 0);
3672 sbuf_printf(&sb, "(nopath): ");
3674 if (path->periph != NULL)
3675 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3676 path->periph->unit_number);
3678 sbuf_printf(&sb, "(noperiph:");
3680 if (path->bus != NULL)
3681 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3682 path->bus->sim->unit_number,
3683 path->bus->sim->bus_id);
3685 sbuf_printf(&sb, "nobus:");
3687 if (path->target != NULL)
3688 sbuf_printf(&sb, "%d:", path->target->target_id);
3690 sbuf_printf(&sb, "X:");
3692 if (path->device != NULL)
3693 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3695 sbuf_printf(&sb, "X): ");
3699 return(sbuf_len(&sb));
3703 xpt_path_path_id(struct cam_path *path)
3705 return(path->bus->path_id);
3709 xpt_path_target_id(struct cam_path *path)
3711 if (path->target != NULL)
3712 return (path->target->target_id);
3714 return (CAM_TARGET_WILDCARD);
3718 xpt_path_lun_id(struct cam_path *path)
3720 if (path->device != NULL)
3721 return (path->device->lun_id);
3723 return (CAM_LUN_WILDCARD);
3727 xpt_path_sim(struct cam_path *path)
3730 return (path->bus->sim);
3734 xpt_path_periph(struct cam_path *path)
3736 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3738 return (path->periph);
3742 xpt_path_legacy_ata_id(struct cam_path *path)
3747 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3748 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3749 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3750 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3753 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3754 path->bus->sim->unit_number < 2) {
3755 bus_id = path->bus->sim->unit_number;
3759 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3760 if (bus == path->bus)
3762 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3763 bus->sim->unit_number >= 2) ||
3764 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3765 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3766 strcmp(bus->sim->sim_name, "siisch") == 0)
3771 if (path->target != NULL) {
3772 if (path->target->target_id < 2)
3773 return (bus_id * 2 + path->target->target_id);
3777 return (bus_id * 2);
3781 * Release a CAM control block for the caller. Remit the cost of the structure
3782 * to the device referenced by the path. If the this device had no 'credits'
3783 * and peripheral drivers have registered async callbacks for this notification
3787 xpt_release_ccb(union ccb *free_ccb)
3789 struct cam_path *path;
3790 struct cam_ed *device;
3792 struct cam_sim *sim;
3794 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3795 path = free_ccb->ccb_h.path;
3796 device = path->device;
3800 mtx_assert(sim->mtx, MA_OWNED);
3802 cam_ccbq_release_opening(&device->ccbq);
3803 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3804 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3805 cam_ccbq_resize(&device->ccbq,
3806 device->ccbq.dev_openings + device->ccbq.dev_active);
3808 if (sim->ccb_count > sim->max_ccbs) {
3809 xpt_free_ccb(free_ccb);
3812 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3815 if (sim->devq == NULL) {
3818 sim->devq->alloc_openings++;
3819 sim->devq->alloc_active--;
3820 if (device_is_alloc_queued(device) == 0)
3821 xpt_schedule_dev_allocq(bus, device);
3822 xpt_run_dev_allocq(bus);
3825 /* Functions accessed by SIM drivers */
3827 static struct xpt_xport xport_default = {
3828 .alloc_device = xpt_alloc_device_default,
3829 .action = xpt_action_default,
3830 .async = xpt_dev_async_default,
3834 * A sim structure, listing the SIM entry points and instance
3835 * identification info is passed to xpt_bus_register to hook the SIM
3836 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3837 * for this new bus and places it in the array of busses and assigns
3838 * it a path_id. The path_id may be influenced by "hard wiring"
3839 * information specified by the user. Once interrupt services are
3840 * available, the bus will be probed.
3843 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3845 struct cam_eb *new_bus;
3846 struct cam_eb *old_bus;
3847 struct ccb_pathinq cpi;
3848 struct cam_path *path;
3851 mtx_assert(sim->mtx, MA_OWNED);
3854 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3855 M_CAMXPT, M_NOWAIT);
3856 if (new_bus == NULL) {
3857 /* Couldn't satisfy request */
3858 return (CAM_RESRC_UNAVAIL);
3860 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3862 free(new_bus, M_CAMXPT);
3863 return (CAM_RESRC_UNAVAIL);
3866 if (strcmp(sim->sim_name, "xpt") != 0) {
3868 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3871 TAILQ_INIT(&new_bus->et_entries);
3872 new_bus->path_id = sim->path_id;
3875 timevalclear(&new_bus->last_reset);
3877 new_bus->refcount = 1; /* Held until a bus_deregister event */
3878 new_bus->generation = 0;
3880 mtx_lock(&xsoftc.xpt_topo_lock);
3881 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3882 while (old_bus != NULL
3883 && old_bus->path_id < new_bus->path_id)
3884 old_bus = TAILQ_NEXT(old_bus, links);
3885 if (old_bus != NULL)
3886 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3888 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3889 xsoftc.bus_generation++;
3890 mtx_unlock(&xsoftc.xpt_topo_lock);
3893 * Set a default transport so that a PATH_INQ can be issued to
3894 * the SIM. This will then allow for probing and attaching of
3895 * a more appropriate transport.
3897 new_bus->xport = &xport_default;
3899 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3900 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3901 if (status != CAM_REQ_CMP)
3902 printf("xpt_compile_path returned %d\n", status);
3904 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3905 cpi.ccb_h.func_code = XPT_PATH_INQ;
3906 xpt_action((union ccb *)&cpi);
3908 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3909 switch (cpi.transport) {
3916 new_bus->xport = scsi_get_xport();
3920 new_bus->xport = ata_get_xport();
3923 new_bus->xport = &xport_default;
3928 /* Notify interested parties */
3929 if (sim->path_id != CAM_XPT_PATH_ID) {
3930 union ccb *scan_ccb;
3932 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3933 /* Initiate bus rescan. */
3934 scan_ccb = xpt_alloc_ccb_nowait();
3935 scan_ccb->ccb_h.path = path;
3936 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3937 scan_ccb->crcn.flags = 0;
3938 xpt_rescan(scan_ccb);
3940 xpt_free_path(path);
3941 return (CAM_SUCCESS);
3945 xpt_bus_deregister(path_id_t pathid)
3947 struct cam_path bus_path;
3950 status = xpt_compile_path(&bus_path, NULL, pathid,
3951 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3952 if (status != CAM_REQ_CMP)
3955 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3956 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3958 /* Release the reference count held while registered. */
3959 xpt_release_bus(bus_path.bus);
3960 xpt_release_path(&bus_path);
3962 return (CAM_REQ_CMP);
3966 xptnextfreepathid(void)
3973 mtx_lock(&xsoftc.xpt_topo_lock);
3974 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3976 /* Find an unoccupied pathid */
3977 while (bus != NULL && bus->path_id <= pathid) {
3978 if (bus->path_id == pathid)
3980 bus = TAILQ_NEXT(bus, links);
3982 mtx_unlock(&xsoftc.xpt_topo_lock);
3985 * Ensure that this pathid is not reserved for
3986 * a bus that may be registered in the future.
3988 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3990 /* Start the search over */
3991 mtx_lock(&xsoftc.xpt_topo_lock);
3998 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4005 pathid = CAM_XPT_PATH_ID;
4006 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4008 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4009 if (strcmp(dname, "scbus")) {
4010 /* Avoid a bit of foot shooting. */
4013 if (dunit < 0) /* unwired?! */
4015 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4016 if (sim_bus == val) {
4020 } else if (sim_bus == 0) {
4021 /* Unspecified matches bus 0 */
4025 printf("Ambiguous scbus configuration for %s%d "
4026 "bus %d, cannot wire down. The kernel "
4027 "config entry for scbus%d should "
4028 "specify a controller bus.\n"
4029 "Scbus will be assigned dynamically.\n",
4030 sim_name, sim_unit, sim_bus, dunit);
4035 if (pathid == CAM_XPT_PATH_ID)
4036 pathid = xptnextfreepathid();
4041 xpt_async_string(u_int32_t async_code)
4044 switch (async_code) {
4045 case AC_BUS_RESET: return ("AC_BUS_RESET");
4046 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4047 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4048 case AC_SENT_BDR: return ("AC_SENT_BDR");
4049 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4050 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4051 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4052 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4053 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4054 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4055 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4056 case AC_CONTRACT: return ("AC_CONTRACT");
4057 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4059 return ("AC_UNKNOWN");
4063 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4066 struct cam_et *target, *next_target;
4067 struct cam_ed *device, *next_device;
4069 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4070 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4071 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4074 * Most async events come from a CAM interrupt context. In
4075 * a few cases, the error recovery code at the peripheral layer,
4076 * which may run from our SWI or a process context, may signal
4077 * deferred events with a call to xpt_async.
4082 if (async_code == AC_BUS_RESET) {
4083 /* Update our notion of when the last reset occurred */
4084 microtime(&bus->last_reset);
4087 for (target = TAILQ_FIRST(&bus->et_entries);
4089 target = next_target) {
4091 next_target = TAILQ_NEXT(target, links);
4093 if (path->target != target
4094 && path->target->target_id != CAM_TARGET_WILDCARD
4095 && target->target_id != CAM_TARGET_WILDCARD)
4098 if (async_code == AC_SENT_BDR) {
4099 /* Update our notion of when the last reset occurred */
4100 microtime(&path->target->last_reset);
4103 for (device = TAILQ_FIRST(&target->ed_entries);
4105 device = next_device) {
4107 next_device = TAILQ_NEXT(device, links);
4109 if (path->device != device
4110 && path->device->lun_id != CAM_LUN_WILDCARD
4111 && device->lun_id != CAM_LUN_WILDCARD)
4114 * The async callback could free the device.
4115 * If it is a broadcast async, it doesn't hold
4116 * device reference, so take our own reference.
4118 xpt_acquire_device(device);
4119 (*(bus->xport->async))(async_code, bus,
4123 xpt_async_bcast(&device->asyncs, async_code,
4125 xpt_release_device(device);
4130 * If this wasn't a fully wildcarded async, tell all
4131 * clients that want all async events.
4133 if (bus != xpt_periph->path->bus)
4134 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4139 xpt_async_bcast(struct async_list *async_head,
4140 u_int32_t async_code,
4141 struct cam_path *path, void *async_arg)
4143 struct async_node *cur_entry;
4145 cur_entry = SLIST_FIRST(async_head);
4146 while (cur_entry != NULL) {
4147 struct async_node *next_entry;
4149 * Grab the next list entry before we call the current
4150 * entry's callback. This is because the callback function
4151 * can delete its async callback entry.
4153 next_entry = SLIST_NEXT(cur_entry, links);
4154 if ((cur_entry->event_enable & async_code) != 0)
4155 cur_entry->callback(cur_entry->callback_arg,
4158 cur_entry = next_entry;
4163 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4164 struct cam_et *target, struct cam_ed *device,
4167 printf("%s called\n", __func__);
4171 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4173 struct cam_ed *dev = path->device;
4175 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4176 dev->sim->devq->alloc_openings +=
4177 cam_ccbq_freeze(&dev->ccbq, rl, count);
4178 /* Remove frozen device from allocq. */
4179 if (device_is_alloc_queued(dev) &&
4180 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4181 CAMQ_GET_PRIO(&dev->drvq)))) {
4182 camq_remove(&dev->sim->devq->alloc_queue,
4183 dev->alloc_ccb_entry.pinfo.index);
4185 /* Remove frozen device from sendq. */
4186 if (device_is_send_queued(dev) &&
4187 cam_ccbq_frozen_top(&dev->ccbq)) {
4188 camq_remove(&dev->sim->devq->send_queue,
4189 dev->send_ccb_entry.pinfo.index);
4191 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4195 xpt_freeze_devq(struct cam_path *path, u_int count)
4198 return (xpt_freeze_devq_rl(path, 0, count));
4202 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4205 mtx_assert(sim->mtx, MA_OWNED);
4206 sim->devq->send_queue.qfrozen_cnt[0] += count;
4207 return (sim->devq->send_queue.qfrozen_cnt[0]);
4211 xpt_release_devq_timeout(void *arg)
4213 struct cam_ed *device;
4215 device = (struct cam_ed *)arg;
4217 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4221 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4223 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4225 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4229 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4231 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4233 xpt_release_devq_device(path->device, rl, count, run_queue);
4237 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4240 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4242 printf("xpt_release_devq(%d): requested %u > present %u\n",
4243 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4245 count = dev->ccbq.queue.qfrozen_cnt[rl];
4247 dev->sim->devq->alloc_openings -=
4248 cam_ccbq_release(&dev->ccbq, rl, count);
4249 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4250 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4251 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4252 xpt_run_dev_allocq(dev->target->bus);
4254 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4256 * No longer need to wait for a successful
4257 * command completion.
4259 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4261 * Remove any timeouts that might be scheduled
4262 * to release this queue.
4264 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4265 callout_stop(&dev->callout);
4266 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4271 * Now that we are unfrozen schedule the
4272 * device so any pending transactions are
4275 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4276 xpt_run_dev_sendq(dev->target->bus);
4281 xpt_release_simq(struct cam_sim *sim, int run_queue)
4285 mtx_assert(sim->mtx, MA_OWNED);
4286 sendq = &(sim->devq->send_queue);
4287 if (sendq->qfrozen_cnt[0] <= 0) {
4289 printf("xpt_release_simq: requested 1 > present %u\n",
4290 sendq->qfrozen_cnt[0]);
4293 sendq->qfrozen_cnt[0]--;
4294 if (sendq->qfrozen_cnt[0] == 0) {
4296 * If there is a timeout scheduled to release this
4297 * sim queue, remove it. The queue frozen count is
4300 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4301 callout_stop(&sim->callout);
4302 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4308 * Now that we are unfrozen run the send queue.
4310 bus = xpt_find_bus(sim->path_id);
4311 xpt_run_dev_sendq(bus);
4312 xpt_release_bus(bus);
4318 * XXX Appears to be unused.
4321 xpt_release_simq_timeout(void *arg)
4323 struct cam_sim *sim;
4325 sim = (struct cam_sim *)arg;
4326 xpt_release_simq(sim, /* run_queue */ TRUE);
4330 xpt_done(union ccb *done_ccb)
4332 struct cam_sim *sim;
4335 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4336 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4338 * Queue up the request for handling by our SWI handler
4339 * any of the "non-immediate" type of ccbs.
4341 sim = done_ccb->ccb_h.path->bus->sim;
4342 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4344 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4345 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4346 CAM_SIM_BATCH)) == 0) {
4347 mtx_lock(&cam_simq_lock);
4348 first = TAILQ_EMPTY(&cam_simq);
4349 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4350 mtx_unlock(&cam_simq_lock);
4351 sim->flags |= CAM_SIM_ON_DONEQ;
4353 swi_sched(cambio_ih, 0);
4359 xpt_batch_start(struct cam_sim *sim)
4362 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4363 sim->flags |= CAM_SIM_BATCH;
4367 xpt_batch_done(struct cam_sim *sim)
4370 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4371 sim->flags &= ~CAM_SIM_BATCH;
4372 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4373 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4374 camisr_runqueue(&sim->sim_doneq);
4382 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4387 xpt_alloc_ccb_nowait()
4391 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4396 xpt_free_ccb(union ccb *free_ccb)
4398 free(free_ccb, M_CAMXPT);
4403 /* Private XPT functions */
4406 * Get a CAM control block for the caller. Charge the structure to the device
4407 * referenced by the path. If the this device has no 'credits' then the
4408 * device already has the maximum number of outstanding operations under way
4409 * and we return NULL. If we don't have sufficient resources to allocate more
4410 * ccbs, we also return NULL.
4413 xpt_get_ccb(struct cam_ed *device)
4416 struct cam_sim *sim;
4419 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4420 new_ccb = xpt_alloc_ccb_nowait();
4421 if (new_ccb == NULL) {
4424 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4425 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4426 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4430 cam_ccbq_take_opening(&device->ccbq);
4431 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4436 xpt_release_bus(struct cam_eb *bus)
4439 mtx_lock(&xsoftc.xpt_topo_lock);
4440 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4441 if ((--bus->refcount == 0)
4442 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4443 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4444 xsoftc.bus_generation++;
4445 mtx_unlock(&xsoftc.xpt_topo_lock);
4446 cam_sim_release(bus->sim);
4447 free(bus, M_CAMXPT);
4449 mtx_unlock(&xsoftc.xpt_topo_lock);
4452 static struct cam_et *
4453 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4455 struct cam_et *target;
4457 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4459 if (target != NULL) {
4460 struct cam_et *cur_target;
4462 TAILQ_INIT(&target->ed_entries);
4464 target->target_id = target_id;
4465 target->refcount = 1;
4466 target->generation = 0;
4467 target->luns = NULL;
4468 timevalclear(&target->last_reset);
4470 * Hold a reference to our parent bus so it
4471 * will not go away before we do.
4473 mtx_lock(&xsoftc.xpt_topo_lock);
4475 mtx_unlock(&xsoftc.xpt_topo_lock);
4477 /* Insertion sort into our bus's target list */
4478 cur_target = TAILQ_FIRST(&bus->et_entries);
4479 while (cur_target != NULL && cur_target->target_id < target_id)
4480 cur_target = TAILQ_NEXT(cur_target, links);
4482 if (cur_target != NULL) {
4483 TAILQ_INSERT_BEFORE(cur_target, target, links);
4485 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4493 xpt_release_target(struct cam_et *target)
4496 if (target->refcount == 1) {
4497 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4498 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4499 target->bus->generation++;
4500 xpt_release_bus(target->bus);
4502 free(target->luns, M_CAMXPT);
4503 free(target, M_CAMXPT);
4509 static struct cam_ed *
4510 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4513 struct cam_ed *device, *cur_device;
4515 device = xpt_alloc_device(bus, target, lun_id);
4519 device->mintags = 1;
4520 device->maxtags = 1;
4521 bus->sim->max_ccbs += device->ccbq.devq_openings;
4522 cur_device = TAILQ_FIRST(&target->ed_entries);
4523 while (cur_device != NULL && cur_device->lun_id < lun_id)
4524 cur_device = TAILQ_NEXT(cur_device, links);
4525 if (cur_device != NULL) {
4526 TAILQ_INSERT_BEFORE(cur_device, device, links);
4528 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4530 target->generation++;
4536 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4538 struct cam_ed *device;
4539 struct cam_devq *devq;
4542 /* Make space for us in the device queue on our bus */
4543 devq = bus->sim->devq;
4544 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4546 if (status != CAM_REQ_CMP) {
4549 device = (struct cam_ed *)malloc(sizeof(*device),
4550 M_CAMXPT, M_NOWAIT|M_ZERO);
4553 if (device != NULL) {
4554 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4555 device->alloc_ccb_entry.device = device;
4556 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4557 device->send_ccb_entry.device = device;
4558 device->target = target;
4559 device->lun_id = lun_id;
4560 device->sim = bus->sim;
4561 /* Initialize our queues */
4562 if (camq_init(&device->drvq, 0) != 0) {
4563 free(device, M_CAMXPT);
4566 if (cam_ccbq_init(&device->ccbq,
4567 bus->sim->max_dev_openings) != 0) {
4568 camq_fini(&device->drvq);
4569 free(device, M_CAMXPT);
4572 SLIST_INIT(&device->asyncs);
4573 SLIST_INIT(&device->periphs);
4574 device->generation = 0;
4575 device->owner = NULL;
4576 device->flags = CAM_DEV_UNCONFIGURED;
4577 device->tag_delay_count = 0;
4578 device->tag_saved_openings = 0;
4579 device->refcount = 1;
4580 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4583 * Hold a reference to our parent target so it
4584 * will not go away before we do.
4593 xpt_acquire_device(struct cam_ed *device)
4600 xpt_release_device(struct cam_ed *device)
4603 if (device->refcount == 1) {
4604 struct cam_devq *devq;
4606 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4607 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4608 panic("Removing device while still queued for ccbs");
4610 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4611 callout_stop(&device->callout);
4613 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4614 device->target->generation++;
4615 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4616 /* Release our slot in the devq */
4617 devq = device->target->bus->sim->devq;
4618 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4619 camq_fini(&device->drvq);
4620 cam_ccbq_fini(&device->ccbq);
4621 xpt_release_target(device->target);
4622 free(device, M_CAMXPT);
4628 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4636 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4637 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4638 if (result == CAM_REQ_CMP && (diff < 0)) {
4639 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4641 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4642 || (dev->inq_flags & SID_CmdQue) != 0)
4643 dev->tag_saved_openings = newopenings;
4644 /* Adjust the global limit */
4645 dev->sim->max_ccbs += diff;
4649 static struct cam_eb *
4650 xpt_find_bus(path_id_t path_id)
4654 mtx_lock(&xsoftc.xpt_topo_lock);
4655 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4657 bus = TAILQ_NEXT(bus, links)) {
4658 if (bus->path_id == path_id) {
4663 mtx_unlock(&xsoftc.xpt_topo_lock);
4667 static struct cam_et *
4668 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4670 struct cam_et *target;
4672 for (target = TAILQ_FIRST(&bus->et_entries);
4674 target = TAILQ_NEXT(target, links)) {
4675 if (target->target_id == target_id) {
4683 static struct cam_ed *
4684 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4686 struct cam_ed *device;
4688 for (device = TAILQ_FIRST(&target->ed_entries);
4690 device = TAILQ_NEXT(device, links)) {
4691 if (device->lun_id == lun_id) {
4700 xpt_start_tags(struct cam_path *path)
4702 struct ccb_relsim crs;
4703 struct cam_ed *device;
4704 struct cam_sim *sim;
4707 device = path->device;
4708 sim = path->bus->sim;
4709 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4710 xpt_freeze_devq(path, /*count*/1);
4711 device->inq_flags |= SID_CmdQue;
4712 if (device->tag_saved_openings != 0)
4713 newopenings = device->tag_saved_openings;
4715 newopenings = min(device->maxtags,
4716 sim->max_tagged_dev_openings);
4717 xpt_dev_ccbq_resize(path, newopenings);
4718 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4719 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4720 crs.ccb_h.func_code = XPT_REL_SIMQ;
4721 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4723 = crs.release_timeout
4726 xpt_action((union ccb *)&crs);
4730 xpt_stop_tags(struct cam_path *path)
4732 struct ccb_relsim crs;
4733 struct cam_ed *device;
4734 struct cam_sim *sim;
4736 device = path->device;
4737 sim = path->bus->sim;
4738 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4739 device->tag_delay_count = 0;
4740 xpt_freeze_devq(path, /*count*/1);
4741 device->inq_flags &= ~SID_CmdQue;
4742 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4743 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4744 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4745 crs.ccb_h.func_code = XPT_REL_SIMQ;
4746 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4748 = crs.release_timeout
4751 xpt_action((union ccb *)&crs);
4755 xpt_boot_delay(void *arg)
4762 xpt_config(void *arg)
4765 * Now that interrupts are enabled, go find our devices
4768 /* Setup debugging path */
4769 if (cam_dflags != CAM_DEBUG_NONE) {
4771 * Locking is specifically omitted here. No SIMs have
4772 * registered yet, so xpt_create_path will only be searching
4773 * empty lists of targets and devices.
4775 if (xpt_create_path(&cam_dpath, xpt_periph,
4776 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4777 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4778 printf("xpt_config: xpt_create_path() failed for debug"
4779 " target %d:%d:%d, debugging disabled\n",
4780 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4781 cam_dflags = CAM_DEBUG_NONE;
4786 periphdriver_init(1);
4788 callout_init(&xsoftc.boot_callout, 1);
4789 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4790 xpt_boot_delay, NULL);
4791 /* Fire up rescan thread. */
4792 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4793 printf("xpt_config: failed to create rescan thread.\n");
4801 xsoftc.buses_to_config++;
4806 xpt_release_boot(void)
4809 xsoftc.buses_to_config--;
4810 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4811 struct xpt_task *task;
4813 xsoftc.buses_config_done = 1;
4815 /* Call manually because we don't have any busses */
4816 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4818 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4819 taskqueue_enqueue(taskqueue_thread, &task->task);
4826 * If the given device only has one peripheral attached to it, and if that
4827 * peripheral is the passthrough driver, announce it. This insures that the
4828 * user sees some sort of announcement for every peripheral in their system.
4831 xptpassannouncefunc(struct cam_ed *device, void *arg)
4833 struct cam_periph *periph;
4836 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4837 periph = SLIST_NEXT(periph, periph_links), i++);
4839 periph = SLIST_FIRST(&device->periphs);
4841 && (strncmp(periph->periph_name, "pass", 4) == 0))
4842 xpt_announce_periph(periph, NULL);
4848 xpt_finishconfig_task(void *context, int pending)
4851 periphdriver_init(2);
4853 * Check for devices with no "standard" peripheral driver
4854 * attached. For any devices like that, announce the
4855 * passthrough driver so the user will see something.
4858 xpt_for_all_devices(xptpassannouncefunc, NULL);
4860 /* Release our hook so that the boot can continue. */
4861 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4862 free(xsoftc.xpt_config_hook, M_CAMXPT);
4863 xsoftc.xpt_config_hook = NULL;
4865 free(context, M_CAMXPT);
4869 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4870 struct cam_path *path)
4872 struct ccb_setasync csa;
4877 mtx_lock(&xsoftc.xpt_lock);
4878 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4879 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4880 if (status != CAM_REQ_CMP) {
4881 mtx_unlock(&xsoftc.xpt_lock);
4887 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4888 csa.ccb_h.func_code = XPT_SASYNC_CB;
4889 csa.event_enable = event;
4890 csa.callback = cbfunc;
4891 csa.callback_arg = cbarg;
4892 xpt_action((union ccb *)&csa);
4893 status = csa.ccb_h.status;
4896 xpt_free_path(path);
4897 mtx_unlock(&xsoftc.xpt_lock);
4900 if ((status == CAM_REQ_CMP) &&
4901 (csa.event_enable & AC_FOUND_DEVICE)) {
4903 * Get this peripheral up to date with all
4904 * the currently existing devices.
4906 xpt_for_all_devices(xptsetasyncfunc, &csa);
4908 if ((status == CAM_REQ_CMP) &&
4909 (csa.event_enable & AC_PATH_REGISTERED)) {
4911 * Get this peripheral up to date with all
4912 * the currently existing busses.
4914 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4921 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4923 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4925 switch (work_ccb->ccb_h.func_code) {
4926 /* Common cases first */
4927 case XPT_PATH_INQ: /* Path routing inquiry */
4929 struct ccb_pathinq *cpi;
4931 cpi = &work_ccb->cpi;
4932 cpi->version_num = 1; /* XXX??? */
4933 cpi->hba_inquiry = 0;
4934 cpi->target_sprt = 0;
4936 cpi->hba_eng_cnt = 0;
4937 cpi->max_target = 0;
4939 cpi->initiator_id = 0;
4940 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4941 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4942 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4943 cpi->unit_number = sim->unit_number;
4944 cpi->bus_id = sim->bus_id;
4945 cpi->base_transfer_speed = 0;
4946 cpi->protocol = PROTO_UNSPECIFIED;
4947 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4948 cpi->transport = XPORT_UNSPECIFIED;
4949 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4950 cpi->ccb_h.status = CAM_REQ_CMP;
4955 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4962 * The xpt as a "controller" has no interrupt sources, so polling
4966 xptpoll(struct cam_sim *sim)
4971 xpt_lock_buses(void)
4973 mtx_lock(&xsoftc.xpt_topo_lock);
4977 xpt_unlock_buses(void)
4979 mtx_unlock(&xsoftc.xpt_topo_lock);
4986 struct cam_sim *sim;
4988 mtx_lock(&cam_simq_lock);
4990 while (!TAILQ_EMPTY(&cam_simq)) {
4991 TAILQ_CONCAT(&queue, &cam_simq, links);
4992 mtx_unlock(&cam_simq_lock);
4994 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4995 TAILQ_REMOVE(&queue, sim, links);
4997 camisr_runqueue(&sim->sim_doneq);
4998 sim->flags &= ~CAM_SIM_ON_DONEQ;
4999 CAM_SIM_UNLOCK(sim);
5001 mtx_lock(&cam_simq_lock);
5003 mtx_unlock(&cam_simq_lock);
5007 camisr_runqueue(void *V_queue)
5009 cam_isrq_t *queue = V_queue;
5010 struct ccb_hdr *ccb_h;
5012 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5015 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5016 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5018 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5023 if (ccb_h->flags & CAM_HIGH_POWER) {
5024 struct highpowerlist *hphead;
5025 union ccb *send_ccb;
5027 mtx_lock(&xsoftc.xpt_lock);
5028 hphead = &xsoftc.highpowerq;
5030 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5033 * Increment the count since this command is done.
5035 xsoftc.num_highpower++;
5038 * Any high powered commands queued up?
5040 if (send_ccb != NULL) {
5042 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5043 mtx_unlock(&xsoftc.xpt_lock);
5045 xpt_release_devq(send_ccb->ccb_h.path,
5046 /*count*/1, /*runqueue*/TRUE);
5048 mtx_unlock(&xsoftc.xpt_lock);
5051 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5054 dev = ccb_h->path->device;
5056 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5057 ccb_h->path->bus->sim->devq->send_active--;
5058 ccb_h->path->bus->sim->devq->send_openings++;
5061 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5062 && (dev->ccbq.dev_active == 0))) {
5063 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5064 xpt_release_devq(ccb_h->path, /*count*/1,
5065 /*run_queue*/FALSE);
5068 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5069 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5070 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5071 xpt_release_devq(ccb_h->path, /*count*/1,
5072 /*run_queue*/FALSE);
5075 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5076 && (--dev->tag_delay_count == 0))
5077 xpt_start_tags(ccb_h->path);
5078 if (!device_is_send_queued(dev)) {
5079 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5084 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5085 xpt_release_simq(ccb_h->path->bus->sim,
5087 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5091 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5092 && (ccb_h->status & CAM_DEV_QFRZN)) {
5093 xpt_release_devq(ccb_h->path, /*count*/1,
5095 ccb_h->status &= ~CAM_DEV_QFRZN;
5097 xpt_run_dev_sendq(ccb_h->path->bus);
5100 /* Call the peripheral driver's callback */
5101 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);