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>
61 #include <cam/cam_compat.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
67 #include <machine/md_var.h> /* geometry translation */
68 #include <machine/stdarg.h> /* for xpt_print below */
73 * This is the maximum number of high powered commands (e.g. start unit)
74 * that can be outstanding at a particular time.
76 #ifndef CAM_MAX_HIGHPOWER
77 #define CAM_MAX_HIGHPOWER 4
80 /* Datastructures internal to the xpt layer */
81 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
82 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
83 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
84 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
86 /* Object for defering XPT actions to a taskqueue */
99 u_int32_t xpt_generation;
101 /* number of high powered commands that can go through right now */
102 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
105 /* queue for handling async rescan requests. */
106 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
108 int buses_config_done;
110 /* Registered busses */
111 TAILQ_HEAD(,cam_eb) xpt_busses;
112 u_int bus_generation;
114 struct intr_config_hook *xpt_config_hook;
117 struct callout boot_callout;
119 struct mtx xpt_topo_lock;
125 DM_RET_FLAG_MASK = 0x0f,
128 DM_RET_DESCEND = 0x20,
130 DM_RET_ACTION_MASK = 0xf0
138 } xpt_traverse_depth;
140 struct xpt_traverse_config {
141 xpt_traverse_depth depth;
146 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
147 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
148 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
149 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
150 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
152 /* Transport layer configuration information */
153 static struct xpt_softc xsoftc;
155 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
159 /* Queues for our software interrupt handler */
160 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
161 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
162 static cam_simq_t cam_simq;
163 static struct mtx cam_simq_lock;
165 /* Pointers to software interrupt handlers */
166 static void *cambio_ih;
168 struct cam_periph *xpt_periph;
170 static periph_init_t xpt_periph_init;
172 static struct periph_driver xpt_driver =
174 xpt_periph_init, "xpt",
175 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
179 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
181 static d_open_t xptopen;
182 static d_close_t xptclose;
183 static d_ioctl_t xptioctl;
184 static d_ioctl_t xptdoioctl;
186 static struct cdevsw xpt_cdevsw = {
187 .d_version = D_VERSION,
195 /* Storage for debugging datastructures */
196 struct cam_path *cam_dpath;
197 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
198 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
199 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
200 &cam_dflags, 0, "Enabled debug flags");
201 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
202 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
203 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
204 &cam_debug_delay, 0, "Delay in us after each debug message");
206 /* Our boot-time initialization hook */
207 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
209 static moduledata_t cam_moduledata = {
211 cam_module_event_handler,
215 static int xpt_init(void *);
217 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
218 MODULE_VERSION(cam, 1);
221 static void xpt_async_bcast(struct async_list *async_head,
222 u_int32_t async_code,
223 struct cam_path *path,
225 static path_id_t xptnextfreepathid(void);
226 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
227 static union ccb *xpt_get_ccb(struct cam_ed *device);
228 static void xpt_run_dev_allocq(struct cam_eb *bus);
229 static void xpt_run_dev_sendq(struct cam_eb *bus);
230 static timeout_t xpt_release_devq_timeout;
231 static void xpt_release_simq_timeout(void *arg) __unused;
232 static void xpt_release_bus(struct cam_eb *bus);
233 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
234 u_int count, int run_queue);
235 static struct cam_et*
236 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
237 static void xpt_release_target(struct cam_et *target);
238 static struct cam_eb*
239 xpt_find_bus(path_id_t path_id);
240 static struct cam_et*
241 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
242 static struct cam_ed*
243 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
244 static void xpt_config(void *arg);
245 static xpt_devicefunc_t xptpassannouncefunc;
246 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
247 static void xptpoll(struct cam_sim *sim);
248 static void camisr(void *);
249 static void camisr_runqueue(void *);
250 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
251 u_int num_patterns, struct cam_eb *bus);
252 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
254 struct cam_ed *device);
255 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
257 struct cam_periph *periph);
258 static xpt_busfunc_t xptedtbusfunc;
259 static xpt_targetfunc_t xptedttargetfunc;
260 static xpt_devicefunc_t xptedtdevicefunc;
261 static xpt_periphfunc_t xptedtperiphfunc;
262 static xpt_pdrvfunc_t xptplistpdrvfunc;
263 static xpt_periphfunc_t xptplistperiphfunc;
264 static int xptedtmatch(struct ccb_dev_match *cdm);
265 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
266 static int xptbustraverse(struct cam_eb *start_bus,
267 xpt_busfunc_t *tr_func, void *arg);
268 static int xpttargettraverse(struct cam_eb *bus,
269 struct cam_et *start_target,
270 xpt_targetfunc_t *tr_func, void *arg);
271 static int xptdevicetraverse(struct cam_et *target,
272 struct cam_ed *start_device,
273 xpt_devicefunc_t *tr_func, void *arg);
274 static int xptperiphtraverse(struct cam_ed *device,
275 struct cam_periph *start_periph,
276 xpt_periphfunc_t *tr_func, void *arg);
277 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
278 xpt_pdrvfunc_t *tr_func, void *arg);
279 static int xptpdperiphtraverse(struct periph_driver **pdrv,
280 struct cam_periph *start_periph,
281 xpt_periphfunc_t *tr_func,
283 static xpt_busfunc_t xptdefbusfunc;
284 static xpt_targetfunc_t xptdeftargetfunc;
285 static xpt_devicefunc_t xptdefdevicefunc;
286 static xpt_periphfunc_t xptdefperiphfunc;
287 static void xpt_finishconfig_task(void *context, int pending);
288 static void xpt_dev_async_default(u_int32_t async_code,
290 struct cam_et *target,
291 struct cam_ed *device,
293 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
294 struct cam_et *target,
296 static xpt_devicefunc_t xptsetasyncfunc;
297 static xpt_busfunc_t xptsetasyncbusfunc;
298 static cam_status xptregister(struct cam_periph *periph,
300 static __inline int periph_is_queued(struct cam_periph *periph);
301 static __inline int device_is_alloc_queued(struct cam_ed *device);
302 static __inline int device_is_send_queued(struct cam_ed *device);
305 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
309 if ((dev->drvq.entries > 0) &&
310 (dev->ccbq.devq_openings > 0) &&
311 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
312 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
314 * The priority of a device waiting for CCB resources
315 * is that of the highest priority peripheral driver
318 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
319 &dev->alloc_ccb_entry.pinfo,
320 CAMQ_GET_PRIO(&dev->drvq));
329 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
333 if ((dev->ccbq.queue.entries > 0) &&
334 (dev->ccbq.dev_openings > 0) &&
335 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
337 * The priority of a device waiting for controller
338 * resources is that of the highest priority CCB
342 xpt_schedule_dev(&bus->sim->devq->send_queue,
343 &dev->send_ccb_entry.pinfo,
344 CAMQ_GET_PRIO(&dev->ccbq.queue));
352 periph_is_queued(struct cam_periph *periph)
354 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
358 device_is_alloc_queued(struct cam_ed *device)
360 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
364 device_is_send_queued(struct cam_ed *device)
366 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
372 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
376 xptdone(struct cam_periph *periph, union ccb *done_ccb)
378 /* Caller will release the CCB */
379 wakeup(&done_ccb->ccb_h.cbfcnp);
383 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
387 * Only allow read-write access.
389 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
393 * We don't allow nonblocking access.
395 if ((flags & O_NONBLOCK) != 0) {
396 printf("%s: can't do nonblocking access\n", devtoname(dev));
400 /* Mark ourselves open */
401 mtx_lock(&xsoftc.xpt_lock);
402 xsoftc.flags |= XPT_FLAG_OPEN;
403 mtx_unlock(&xsoftc.xpt_lock);
409 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
412 /* Mark ourselves closed */
413 mtx_lock(&xsoftc.xpt_lock);
414 xsoftc.flags &= ~XPT_FLAG_OPEN;
415 mtx_unlock(&xsoftc.xpt_lock);
421 * Don't automatically grab the xpt softc lock here even though this is going
422 * through the xpt device. The xpt device is really just a back door for
423 * accessing other devices and SIMs, so the right thing to do is to grab
424 * the appropriate SIM lock once the bus/SIM is located.
427 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
431 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
432 error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
434 return (xptdoioctl(dev, cmd, addr, flag, td));
440 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
448 * For the transport layer CAMIOCOMMAND ioctl, we really only want
449 * to accept CCB types that don't quite make sense to send through a
450 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
458 inccb = (union ccb *)addr;
460 bus = xpt_find_bus(inccb->ccb_h.path_id);
464 switch (inccb->ccb_h.func_code) {
467 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
468 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
469 xpt_release_bus(bus);
474 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
475 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
476 xpt_release_bus(bus);
484 switch(inccb->ccb_h.func_code) {
492 ccb = xpt_alloc_ccb();
494 CAM_SIM_LOCK(bus->sim);
497 * Create a path using the bus, target, and lun the
500 if (xpt_create_path(&ccb->ccb_h.path, NULL,
501 inccb->ccb_h.path_id,
502 inccb->ccb_h.target_id,
503 inccb->ccb_h.target_lun) !=
506 CAM_SIM_UNLOCK(bus->sim);
510 /* Ensure all of our fields are correct */
511 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
512 inccb->ccb_h.pinfo.priority);
513 xpt_merge_ccb(ccb, inccb);
514 ccb->ccb_h.cbfcnp = xptdone;
515 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
516 bcopy(ccb, inccb, sizeof(union ccb));
517 xpt_free_path(ccb->ccb_h.path);
519 CAM_SIM_UNLOCK(bus->sim);
526 * This is an immediate CCB, so it's okay to
527 * allocate it on the stack.
530 CAM_SIM_LOCK(bus->sim);
533 * Create a path using the bus, target, and lun the
536 if (xpt_create_path(&ccb.ccb_h.path, NULL,
537 inccb->ccb_h.path_id,
538 inccb->ccb_h.target_id,
539 inccb->ccb_h.target_lun) !=
542 CAM_SIM_UNLOCK(bus->sim);
545 /* Ensure all of our fields are correct */
546 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
547 inccb->ccb_h.pinfo.priority);
548 xpt_merge_ccb(&ccb, inccb);
549 ccb.ccb_h.cbfcnp = xptdone;
551 bcopy(&ccb, inccb, sizeof(union ccb));
552 xpt_free_path(ccb.ccb_h.path);
553 CAM_SIM_UNLOCK(bus->sim);
557 case XPT_DEV_MATCH: {
558 struct cam_periph_map_info mapinfo;
559 struct cam_path *old_path;
562 * We can't deal with physical addresses for this
563 * type of transaction.
565 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
572 * Save this in case the caller had it set to
573 * something in particular.
575 old_path = inccb->ccb_h.path;
578 * We really don't need a path for the matching
579 * code. The path is needed because of the
580 * debugging statements in xpt_action(). They
581 * assume that the CCB has a valid path.
583 inccb->ccb_h.path = xpt_periph->path;
585 bzero(&mapinfo, sizeof(mapinfo));
588 * Map the pattern and match buffers into kernel
589 * virtual address space.
591 error = cam_periph_mapmem(inccb, &mapinfo);
594 inccb->ccb_h.path = old_path;
599 * This is an immediate CCB, we can send it on directly.
601 CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
603 CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
606 * Map the buffers back into user space.
608 cam_periph_unmapmem(inccb, &mapinfo);
610 inccb->ccb_h.path = old_path;
619 xpt_release_bus(bus);
623 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
624 * with the periphal driver name and unit name filled in. The other
625 * fields don't really matter as input. The passthrough driver name
626 * ("pass"), and unit number are passed back in the ccb. The current
627 * device generation number, and the index into the device peripheral
628 * driver list, and the status are also passed back. Note that
629 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
630 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
631 * (or rather should be) impossible for the device peripheral driver
632 * list to change since we look at the whole thing in one pass, and
633 * we do it with lock protection.
636 case CAMGETPASSTHRU: {
638 struct cam_periph *periph;
639 struct periph_driver **p_drv;
642 u_int cur_generation;
643 int base_periph_found;
646 ccb = (union ccb *)addr;
647 unit = ccb->cgdl.unit_number;
648 name = ccb->cgdl.periph_name;
650 * Every 100 devices, we want to drop our lock protection to
651 * give the software interrupt handler a chance to run.
652 * Most systems won't run into this check, but this should
653 * avoid starvation in the software interrupt handler in
658 ccb = (union ccb *)addr;
660 base_periph_found = 0;
663 * Sanity check -- make sure we don't get a null peripheral
666 if (*ccb->cgdl.periph_name == '\0') {
671 /* Keep the list from changing while we traverse it */
674 cur_generation = xsoftc.xpt_generation;
676 /* first find our driver in the list of drivers */
677 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
678 if (strcmp((*p_drv)->driver_name, name) == 0)
681 if (*p_drv == NULL) {
683 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
684 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
685 *ccb->cgdl.periph_name = '\0';
686 ccb->cgdl.unit_number = 0;
692 * Run through every peripheral instance of this driver
693 * and check to see whether it matches the unit passed
694 * in by the user. If it does, get out of the loops and
695 * find the passthrough driver associated with that
698 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
699 periph = TAILQ_NEXT(periph, unit_links)) {
701 if (periph->unit_number == unit) {
703 } else if (--splbreaknum == 0) {
707 if (cur_generation != xsoftc.xpt_generation)
712 * If we found the peripheral driver that the user passed
713 * in, go through all of the peripheral drivers for that
714 * particular device and look for a passthrough driver.
716 if (periph != NULL) {
717 struct cam_ed *device;
720 base_periph_found = 1;
721 device = periph->path->device;
722 for (i = 0, periph = SLIST_FIRST(&device->periphs);
724 periph = SLIST_NEXT(periph, periph_links), i++) {
726 * Check to see whether we have a
727 * passthrough device or not.
729 if (strcmp(periph->periph_name, "pass") == 0) {
731 * Fill in the getdevlist fields.
733 strcpy(ccb->cgdl.periph_name,
734 periph->periph_name);
735 ccb->cgdl.unit_number =
737 if (SLIST_NEXT(periph, periph_links))
739 CAM_GDEVLIST_MORE_DEVS;
742 CAM_GDEVLIST_LAST_DEVICE;
743 ccb->cgdl.generation =
747 * Fill in some CCB header fields
748 * that the user may want.
751 periph->path->bus->path_id;
752 ccb->ccb_h.target_id =
753 periph->path->target->target_id;
754 ccb->ccb_h.target_lun =
755 periph->path->device->lun_id;
756 ccb->ccb_h.status = CAM_REQ_CMP;
763 * If the periph is null here, one of two things has
764 * happened. The first possibility is that we couldn't
765 * find the unit number of the particular peripheral driver
766 * that the user is asking about. e.g. the user asks for
767 * the passthrough driver for "da11". We find the list of
768 * "da" peripherals all right, but there is no unit 11.
769 * The other possibility is that we went through the list
770 * of peripheral drivers attached to the device structure,
771 * but didn't find one with the name "pass". Either way,
772 * we return ENOENT, since we couldn't find something.
774 if (periph == NULL) {
775 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
776 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
777 *ccb->cgdl.periph_name = '\0';
778 ccb->cgdl.unit_number = 0;
781 * It is unfortunate that this is even necessary,
782 * but there are many, many clueless users out there.
783 * If this is true, the user is looking for the
784 * passthrough driver, but doesn't have one in his
787 if (base_periph_found == 1) {
788 printf("xptioctl: pass driver is not in the "
790 printf("xptioctl: put \"device pass\" in "
791 "your kernel config file\n");
806 cam_module_event_handler(module_t mod, int what, void *arg)
812 if ((error = xpt_init(NULL)) != 0)
825 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
828 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
829 xpt_free_path(done_ccb->ccb_h.path);
830 xpt_free_ccb(done_ccb);
832 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
833 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
838 /* thread to handle bus rescans */
840 xpt_scanner_thread(void *dummy)
847 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
848 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
850 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
851 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
854 sim = ccb->ccb_h.path->bus->sim;
865 xpt_rescan(union ccb *ccb)
869 /* Prepare request */
870 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
871 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
872 ccb->ccb_h.func_code = XPT_SCAN_BUS;
873 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
874 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
875 ccb->ccb_h.func_code = XPT_SCAN_TGT;
876 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
877 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
878 ccb->ccb_h.func_code = XPT_SCAN_LUN;
880 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
881 xpt_free_path(ccb->ccb_h.path);
885 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
886 ccb->ccb_h.cbfcnp = xpt_rescan_done;
887 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
888 /* Don't make duplicate entries for the same paths. */
890 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
891 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
892 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
893 wakeup(&xsoftc.ccb_scanq);
895 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
896 xpt_free_path(ccb->ccb_h.path);
902 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
903 xsoftc.buses_to_config++;
904 wakeup(&xsoftc.ccb_scanq);
908 /* Functions accessed by the peripheral drivers */
910 xpt_init(void *dummy)
912 struct cam_sim *xpt_sim;
913 struct cam_path *path;
914 struct cam_devq *devq;
917 TAILQ_INIT(&xsoftc.xpt_busses);
918 TAILQ_INIT(&cam_simq);
919 TAILQ_INIT(&xsoftc.ccb_scanq);
920 STAILQ_INIT(&xsoftc.highpowerq);
921 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
923 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
924 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
925 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
927 #ifdef CAM_BOOT_DELAY
929 * Override this value at compile time to assist our users
930 * who don't use loader to boot a kernel.
932 xsoftc.boot_delay = CAM_BOOT_DELAY;
935 * The xpt layer is, itself, the equivelent of a SIM.
936 * Allow 16 ccbs in the ccb pool for it. This should
937 * give decent parallelism when we probe busses and
938 * perform other XPT functions.
940 devq = cam_simq_alloc(16);
941 xpt_sim = cam_sim_alloc(xptaction,
946 /*mtx*/&xsoftc.xpt_lock,
947 /*max_dev_transactions*/0,
948 /*max_tagged_dev_transactions*/0,
953 mtx_lock(&xsoftc.xpt_lock);
954 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
955 mtx_unlock(&xsoftc.xpt_lock);
956 printf("xpt_init: xpt_bus_register failed with status %#x,"
957 " failing attach\n", status);
962 * Looking at the XPT from the SIM layer, the XPT is
963 * the equivelent of a peripheral driver. Allocate
964 * a peripheral driver entry for us.
966 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
968 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
969 mtx_unlock(&xsoftc.xpt_lock);
970 printf("xpt_init: xpt_create_path failed with status %#x,"
971 " failing attach\n", status);
975 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
976 path, NULL, 0, xpt_sim);
978 mtx_unlock(&xsoftc.xpt_lock);
979 /* Install our software interrupt handlers */
980 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
982 * Register a callback for when interrupts are enabled.
984 xsoftc.xpt_config_hook =
985 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
986 M_CAMXPT, M_NOWAIT | M_ZERO);
987 if (xsoftc.xpt_config_hook == NULL) {
988 printf("xpt_init: Cannot malloc config hook "
989 "- failing attach\n");
992 xsoftc.xpt_config_hook->ich_func = xpt_config;
993 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
994 free (xsoftc.xpt_config_hook, M_CAMXPT);
995 printf("xpt_init: config_intrhook_establish failed "
996 "- failing attach\n");
1003 xptregister(struct cam_periph *periph, void *arg)
1005 struct cam_sim *xpt_sim;
1007 if (periph == NULL) {
1008 printf("xptregister: periph was NULL!!\n");
1009 return(CAM_REQ_CMP_ERR);
1012 xpt_sim = (struct cam_sim *)arg;
1013 xpt_sim->softc = periph;
1014 xpt_periph = periph;
1015 periph->softc = NULL;
1017 return(CAM_REQ_CMP);
1021 xpt_add_periph(struct cam_periph *periph)
1023 struct cam_ed *device;
1025 struct periph_list *periph_head;
1027 mtx_assert(periph->sim->mtx, MA_OWNED);
1029 device = periph->path->device;
1031 periph_head = &device->periphs;
1033 status = CAM_REQ_CMP;
1035 if (device != NULL) {
1037 * Make room for this peripheral
1038 * so it will fit in the queue
1039 * when it's scheduled to run
1041 status = camq_resize(&device->drvq,
1042 device->drvq.array_size + 1);
1044 device->generation++;
1046 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1050 xsoftc.xpt_generation++;
1057 xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
1059 struct cam_ed *device;
1061 mtx_assert(periph->sim->mtx, MA_OWNED);
1063 device = periph->path->device;
1065 if (device != NULL) {
1066 struct periph_list *periph_head;
1068 periph_head = &device->periphs;
1070 /* Release the slot for this peripheral */
1071 camq_resize(&device->drvq, device->drvq.array_size - 1);
1073 device->generation++;
1075 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1078 if (topology_lock_held == 0)
1081 xsoftc.xpt_generation++;
1083 if (topology_lock_held == 0)
1089 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1091 struct cam_path *path = periph->path;
1093 mtx_assert(periph->sim->mtx, MA_OWNED);
1095 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1096 periph->periph_name, periph->unit_number,
1097 path->bus->sim->sim_name,
1098 path->bus->sim->unit_number,
1099 path->bus->sim->bus_id,
1101 path->target->target_id,
1102 path->device->lun_id);
1103 printf("%s%d: ", periph->periph_name, periph->unit_number);
1104 if (path->device->protocol == PROTO_SCSI)
1105 scsi_print_inquiry(&path->device->inq_data);
1106 else if (path->device->protocol == PROTO_ATA ||
1107 path->device->protocol == PROTO_SATAPM)
1108 ata_print_ident(&path->device->ident_data);
1109 else if (path->device->protocol == PROTO_SEMB)
1111 (struct sep_identify_data *)&path->device->ident_data);
1113 printf("Unknown protocol device\n");
1114 if (bootverbose && path->device->serial_num_len > 0) {
1115 /* Don't wrap the screen - print only the first 60 chars */
1116 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1117 periph->unit_number, path->device->serial_num);
1119 /* Announce transport details. */
1120 (*(path->bus->xport->announce))(periph);
1121 /* Announce command queueing. */
1122 if (path->device->inq_flags & SID_CmdQue
1123 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1124 printf("%s%d: Command Queueing enabled\n",
1125 periph->periph_name, periph->unit_number);
1127 /* Announce caller's details if they've passed in. */
1128 if (announce_string != NULL)
1129 printf("%s%d: %s\n", periph->periph_name,
1130 periph->unit_number, announce_string);
1134 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1137 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1138 periph->unit_number, quirks, bit_string);
1143 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1146 struct ccb_dev_advinfo cdai;
1147 struct scsi_vpd_id_descriptor *idd;
1149 mtx_assert(path->bus->sim->mtx, MA_OWNED);
1151 memset(&cdai, 0, sizeof(cdai));
1152 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1153 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1156 if (!strcmp(attr, "GEOM::ident"))
1157 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1158 else if (!strcmp(attr, "GEOM::physpath"))
1159 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1160 else if (!strcmp(attr, "GEOM::lunid")) {
1161 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1162 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1166 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1167 if (cdai.buf == NULL) {
1171 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1172 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1173 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1174 if (cdai.provsiz == 0)
1176 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1177 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1178 cdai.provsiz, scsi_devid_is_lun_naa);
1180 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1181 cdai.provsiz, scsi_devid_is_lun_eui64);
1183 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1184 cdai.provsiz, scsi_devid_is_lun_t10);
1186 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1187 cdai.provsiz, scsi_devid_is_lun_name);
1191 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1192 (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1193 l = strnlen(idd->identifier, idd->length);
1195 bcopy(idd->identifier, buf, l);
1200 if (idd->length * 2 < len) {
1201 for (l = 0; l < idd->length; l++)
1202 sprintf(buf + l * 2, "%02x",
1203 idd->identifier[l]);
1209 if (strlcpy(buf, cdai.buf, len) >= len)
1214 if (cdai.buf != NULL)
1215 free(cdai.buf, M_CAMXPT);
1219 static dev_match_ret
1220 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1223 dev_match_ret retval;
1226 retval = DM_RET_NONE;
1229 * If we aren't given something to match against, that's an error.
1232 return(DM_RET_ERROR);
1235 * If there are no match entries, then this bus matches no
1238 if ((patterns == NULL) || (num_patterns == 0))
1239 return(DM_RET_DESCEND | DM_RET_COPY);
1241 for (i = 0; i < num_patterns; i++) {
1242 struct bus_match_pattern *cur_pattern;
1245 * If the pattern in question isn't for a bus node, we
1246 * aren't interested. However, we do indicate to the
1247 * calling routine that we should continue descending the
1248 * tree, since the user wants to match against lower-level
1251 if (patterns[i].type != DEV_MATCH_BUS) {
1252 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1253 retval |= DM_RET_DESCEND;
1257 cur_pattern = &patterns[i].pattern.bus_pattern;
1260 * If they want to match any bus node, we give them any
1263 if (cur_pattern->flags == BUS_MATCH_ANY) {
1264 /* set the copy flag */
1265 retval |= DM_RET_COPY;
1268 * If we've already decided on an action, go ahead
1271 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1276 * Not sure why someone would do this...
1278 if (cur_pattern->flags == BUS_MATCH_NONE)
1281 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1282 && (cur_pattern->path_id != bus->path_id))
1285 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1286 && (cur_pattern->bus_id != bus->sim->bus_id))
1289 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1290 && (cur_pattern->unit_number != bus->sim->unit_number))
1293 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1294 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1299 * If we get to this point, the user definitely wants
1300 * information on this bus. So tell the caller to copy the
1303 retval |= DM_RET_COPY;
1306 * If the return action has been set to descend, then we
1307 * know that we've already seen a non-bus matching
1308 * expression, therefore we need to further descend the tree.
1309 * This won't change by continuing around the loop, so we
1310 * go ahead and return. If we haven't seen a non-bus
1311 * matching expression, we keep going around the loop until
1312 * we exhaust the matching expressions. We'll set the stop
1313 * flag once we fall out of the loop.
1315 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1320 * If the return action hasn't been set to descend yet, that means
1321 * we haven't seen anything other than bus matching patterns. So
1322 * tell the caller to stop descending the tree -- the user doesn't
1323 * want to match against lower level tree elements.
1325 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1326 retval |= DM_RET_STOP;
1331 static dev_match_ret
1332 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1333 struct cam_ed *device)
1335 dev_match_ret retval;
1338 retval = DM_RET_NONE;
1341 * If we aren't given something to match against, that's an error.
1344 return(DM_RET_ERROR);
1347 * If there are no match entries, then this device matches no
1350 if ((patterns == NULL) || (num_patterns == 0))
1351 return(DM_RET_DESCEND | DM_RET_COPY);
1353 for (i = 0; i < num_patterns; i++) {
1354 struct device_match_pattern *cur_pattern;
1355 struct scsi_vpd_device_id *device_id_page;
1358 * If the pattern in question isn't for a device node, we
1359 * aren't interested.
1361 if (patterns[i].type != DEV_MATCH_DEVICE) {
1362 if ((patterns[i].type == DEV_MATCH_PERIPH)
1363 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1364 retval |= DM_RET_DESCEND;
1368 cur_pattern = &patterns[i].pattern.device_pattern;
1370 /* Error out if mutually exclusive options are specified. */
1371 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1372 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1373 return(DM_RET_ERROR);
1376 * If they want to match any device node, we give them any
1379 if (cur_pattern->flags == DEV_MATCH_ANY)
1383 * Not sure why someone would do this...
1385 if (cur_pattern->flags == DEV_MATCH_NONE)
1388 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1389 && (cur_pattern->path_id != device->target->bus->path_id))
1392 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1393 && (cur_pattern->target_id != device->target->target_id))
1396 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1397 && (cur_pattern->target_lun != device->lun_id))
1400 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1401 && (cam_quirkmatch((caddr_t)&device->inq_data,
1402 (caddr_t)&cur_pattern->data.inq_pat,
1403 1, sizeof(cur_pattern->data.inq_pat),
1404 scsi_static_inquiry_match) == NULL))
1407 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1408 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1409 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1410 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1411 device->device_id_len
1412 - SVPD_DEVICE_ID_HDR_LEN,
1413 cur_pattern->data.devid_pat.id,
1414 cur_pattern->data.devid_pat.id_len) != 0))
1419 * If we get to this point, the user definitely wants
1420 * information on this device. So tell the caller to copy
1423 retval |= DM_RET_COPY;
1426 * If the return action has been set to descend, then we
1427 * know that we've already seen a peripheral matching
1428 * expression, therefore we need to further descend the tree.
1429 * This won't change by continuing around the loop, so we
1430 * go ahead and return. If we haven't seen a peripheral
1431 * matching expression, we keep going around the loop until
1432 * we exhaust the matching expressions. We'll set the stop
1433 * flag once we fall out of the loop.
1435 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1440 * If the return action hasn't been set to descend yet, that means
1441 * we haven't seen any peripheral matching patterns. So tell the
1442 * caller to stop descending the tree -- the user doesn't want to
1443 * match against lower level tree elements.
1445 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1446 retval |= DM_RET_STOP;
1452 * Match a single peripheral against any number of match patterns.
1454 static dev_match_ret
1455 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1456 struct cam_periph *periph)
1458 dev_match_ret retval;
1462 * If we aren't given something to match against, that's an error.
1465 return(DM_RET_ERROR);
1468 * If there are no match entries, then this peripheral matches no
1471 if ((patterns == NULL) || (num_patterns == 0))
1472 return(DM_RET_STOP | DM_RET_COPY);
1475 * There aren't any nodes below a peripheral node, so there's no
1476 * reason to descend the tree any further.
1478 retval = DM_RET_STOP;
1480 for (i = 0; i < num_patterns; i++) {
1481 struct periph_match_pattern *cur_pattern;
1484 * If the pattern in question isn't for a peripheral, we
1485 * aren't interested.
1487 if (patterns[i].type != DEV_MATCH_PERIPH)
1490 cur_pattern = &patterns[i].pattern.periph_pattern;
1493 * If they want to match on anything, then we will do so.
1495 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1496 /* set the copy flag */
1497 retval |= DM_RET_COPY;
1500 * We've already set the return action to stop,
1501 * since there are no nodes below peripherals in
1508 * Not sure why someone would do this...
1510 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1513 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1514 && (cur_pattern->path_id != periph->path->bus->path_id))
1518 * For the target and lun id's, we have to make sure the
1519 * target and lun pointers aren't NULL. The xpt peripheral
1520 * has a wildcard target and device.
1522 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1523 && ((periph->path->target == NULL)
1524 ||(cur_pattern->target_id != periph->path->target->target_id)))
1527 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1528 && ((periph->path->device == NULL)
1529 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1532 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1533 && (cur_pattern->unit_number != periph->unit_number))
1536 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1537 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1542 * If we get to this point, the user definitely wants
1543 * information on this peripheral. So tell the caller to
1544 * copy the data out.
1546 retval |= DM_RET_COPY;
1549 * The return action has already been set to stop, since
1550 * peripherals don't have any nodes below them in the EDT.
1556 * If we get to this point, the peripheral that was passed in
1557 * doesn't match any of the patterns.
1563 xptedtbusfunc(struct cam_eb *bus, void *arg)
1565 struct ccb_dev_match *cdm;
1566 dev_match_ret retval;
1568 cdm = (struct ccb_dev_match *)arg;
1571 * If our position is for something deeper in the tree, that means
1572 * that we've already seen this node. So, we keep going down.
1574 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1575 && (cdm->pos.cookie.bus == bus)
1576 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1577 && (cdm->pos.cookie.target != NULL))
1578 retval = DM_RET_DESCEND;
1580 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1583 * If we got an error, bail out of the search.
1585 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1586 cdm->status = CAM_DEV_MATCH_ERROR;
1591 * If the copy flag is set, copy this bus out.
1593 if (retval & DM_RET_COPY) {
1596 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1597 sizeof(struct dev_match_result));
1600 * If we don't have enough space to put in another
1601 * match result, save our position and tell the
1602 * user there are more devices to check.
1604 if (spaceleft < sizeof(struct dev_match_result)) {
1605 bzero(&cdm->pos, sizeof(cdm->pos));
1606 cdm->pos.position_type =
1607 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1609 cdm->pos.cookie.bus = bus;
1610 cdm->pos.generations[CAM_BUS_GENERATION]=
1611 xsoftc.bus_generation;
1612 cdm->status = CAM_DEV_MATCH_MORE;
1615 j = cdm->num_matches;
1617 cdm->matches[j].type = DEV_MATCH_BUS;
1618 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1619 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1620 cdm->matches[j].result.bus_result.unit_number =
1621 bus->sim->unit_number;
1622 strncpy(cdm->matches[j].result.bus_result.dev_name,
1623 bus->sim->sim_name, DEV_IDLEN);
1627 * If the user is only interested in busses, there's no
1628 * reason to descend to the next level in the tree.
1630 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1634 * If there is a target generation recorded, check it to
1635 * make sure the target list hasn't changed.
1637 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1638 && (bus == cdm->pos.cookie.bus)
1639 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1640 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1641 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1643 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1647 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1648 && (cdm->pos.cookie.bus == bus)
1649 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1650 && (cdm->pos.cookie.target != NULL))
1651 return(xpttargettraverse(bus,
1652 (struct cam_et *)cdm->pos.cookie.target,
1653 xptedttargetfunc, arg));
1655 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1659 xptedttargetfunc(struct cam_et *target, void *arg)
1661 struct ccb_dev_match *cdm;
1663 cdm = (struct ccb_dev_match *)arg;
1666 * If there is a device list generation recorded, check it to
1667 * make sure the device list hasn't changed.
1669 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1670 && (cdm->pos.cookie.bus == target->bus)
1671 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1672 && (cdm->pos.cookie.target == target)
1673 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1674 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1675 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1676 target->generation)) {
1677 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1681 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1682 && (cdm->pos.cookie.bus == target->bus)
1683 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1684 && (cdm->pos.cookie.target == target)
1685 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1686 && (cdm->pos.cookie.device != NULL))
1687 return(xptdevicetraverse(target,
1688 (struct cam_ed *)cdm->pos.cookie.device,
1689 xptedtdevicefunc, arg));
1691 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1695 xptedtdevicefunc(struct cam_ed *device, void *arg)
1698 struct ccb_dev_match *cdm;
1699 dev_match_ret retval;
1701 cdm = (struct ccb_dev_match *)arg;
1704 * If our position is for something deeper in the tree, that means
1705 * that we've already seen this node. So, we keep going down.
1707 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1708 && (cdm->pos.cookie.device == device)
1709 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1710 && (cdm->pos.cookie.periph != NULL))
1711 retval = DM_RET_DESCEND;
1713 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1716 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1717 cdm->status = CAM_DEV_MATCH_ERROR;
1722 * If the copy flag is set, copy this device out.
1724 if (retval & DM_RET_COPY) {
1727 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1728 sizeof(struct dev_match_result));
1731 * If we don't have enough space to put in another
1732 * match result, save our position and tell the
1733 * user there are more devices to check.
1735 if (spaceleft < sizeof(struct dev_match_result)) {
1736 bzero(&cdm->pos, sizeof(cdm->pos));
1737 cdm->pos.position_type =
1738 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1739 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1741 cdm->pos.cookie.bus = device->target->bus;
1742 cdm->pos.generations[CAM_BUS_GENERATION]=
1743 xsoftc.bus_generation;
1744 cdm->pos.cookie.target = device->target;
1745 cdm->pos.generations[CAM_TARGET_GENERATION] =
1746 device->target->bus->generation;
1747 cdm->pos.cookie.device = device;
1748 cdm->pos.generations[CAM_DEV_GENERATION] =
1749 device->target->generation;
1750 cdm->status = CAM_DEV_MATCH_MORE;
1753 j = cdm->num_matches;
1755 cdm->matches[j].type = DEV_MATCH_DEVICE;
1756 cdm->matches[j].result.device_result.path_id =
1757 device->target->bus->path_id;
1758 cdm->matches[j].result.device_result.target_id =
1759 device->target->target_id;
1760 cdm->matches[j].result.device_result.target_lun =
1762 cdm->matches[j].result.device_result.protocol =
1764 bcopy(&device->inq_data,
1765 &cdm->matches[j].result.device_result.inq_data,
1766 sizeof(struct scsi_inquiry_data));
1767 bcopy(&device->ident_data,
1768 &cdm->matches[j].result.device_result.ident_data,
1769 sizeof(struct ata_params));
1771 /* Let the user know whether this device is unconfigured */
1772 if (device->flags & CAM_DEV_UNCONFIGURED)
1773 cdm->matches[j].result.device_result.flags =
1774 DEV_RESULT_UNCONFIGURED;
1776 cdm->matches[j].result.device_result.flags =
1781 * If the user isn't interested in peripherals, don't descend
1782 * the tree any further.
1784 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1788 * If there is a peripheral list generation recorded, make sure
1789 * it hasn't changed.
1791 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1792 && (device->target->bus == cdm->pos.cookie.bus)
1793 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1794 && (device->target == cdm->pos.cookie.target)
1795 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1796 && (device == cdm->pos.cookie.device)
1797 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1798 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1799 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1800 device->generation)){
1801 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1805 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1806 && (cdm->pos.cookie.bus == device->target->bus)
1807 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1808 && (cdm->pos.cookie.target == device->target)
1809 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1810 && (cdm->pos.cookie.device == device)
1811 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1812 && (cdm->pos.cookie.periph != NULL))
1813 return(xptperiphtraverse(device,
1814 (struct cam_periph *)cdm->pos.cookie.periph,
1815 xptedtperiphfunc, arg));
1817 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1821 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1823 struct ccb_dev_match *cdm;
1824 dev_match_ret retval;
1826 cdm = (struct ccb_dev_match *)arg;
1828 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1830 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1831 cdm->status = CAM_DEV_MATCH_ERROR;
1836 * If the copy flag is set, copy this peripheral out.
1838 if (retval & DM_RET_COPY) {
1841 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1842 sizeof(struct dev_match_result));
1845 * If we don't have enough space to put in another
1846 * match result, save our position and tell the
1847 * user there are more devices to check.
1849 if (spaceleft < sizeof(struct dev_match_result)) {
1850 bzero(&cdm->pos, sizeof(cdm->pos));
1851 cdm->pos.position_type =
1852 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1853 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1856 cdm->pos.cookie.bus = periph->path->bus;
1857 cdm->pos.generations[CAM_BUS_GENERATION]=
1858 xsoftc.bus_generation;
1859 cdm->pos.cookie.target = periph->path->target;
1860 cdm->pos.generations[CAM_TARGET_GENERATION] =
1861 periph->path->bus->generation;
1862 cdm->pos.cookie.device = periph->path->device;
1863 cdm->pos.generations[CAM_DEV_GENERATION] =
1864 periph->path->target->generation;
1865 cdm->pos.cookie.periph = periph;
1866 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1867 periph->path->device->generation;
1868 cdm->status = CAM_DEV_MATCH_MORE;
1872 j = cdm->num_matches;
1874 cdm->matches[j].type = DEV_MATCH_PERIPH;
1875 cdm->matches[j].result.periph_result.path_id =
1876 periph->path->bus->path_id;
1877 cdm->matches[j].result.periph_result.target_id =
1878 periph->path->target->target_id;
1879 cdm->matches[j].result.periph_result.target_lun =
1880 periph->path->device->lun_id;
1881 cdm->matches[j].result.periph_result.unit_number =
1882 periph->unit_number;
1883 strncpy(cdm->matches[j].result.periph_result.periph_name,
1884 periph->periph_name, DEV_IDLEN);
1891 xptedtmatch(struct ccb_dev_match *cdm)
1895 cdm->num_matches = 0;
1898 * Check the bus list generation. If it has changed, the user
1899 * needs to reset everything and start over.
1901 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1902 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1903 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1904 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1908 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1909 && (cdm->pos.cookie.bus != NULL))
1910 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1911 xptedtbusfunc, cdm);
1913 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1916 * If we get back 0, that means that we had to stop before fully
1917 * traversing the EDT. It also means that one of the subroutines
1918 * has set the status field to the proper value. If we get back 1,
1919 * we've fully traversed the EDT and copied out any matching entries.
1922 cdm->status = CAM_DEV_MATCH_LAST;
1928 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1930 struct ccb_dev_match *cdm;
1932 cdm = (struct ccb_dev_match *)arg;
1934 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1935 && (cdm->pos.cookie.pdrv == pdrv)
1936 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1937 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1938 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1939 (*pdrv)->generation)) {
1940 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1944 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1945 && (cdm->pos.cookie.pdrv == pdrv)
1946 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1947 && (cdm->pos.cookie.periph != NULL))
1948 return(xptpdperiphtraverse(pdrv,
1949 (struct cam_periph *)cdm->pos.cookie.periph,
1950 xptplistperiphfunc, arg));
1952 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1956 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1958 struct ccb_dev_match *cdm;
1959 dev_match_ret retval;
1961 cdm = (struct ccb_dev_match *)arg;
1963 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1965 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1966 cdm->status = CAM_DEV_MATCH_ERROR;
1971 * If the copy flag is set, copy this peripheral out.
1973 if (retval & DM_RET_COPY) {
1976 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1977 sizeof(struct dev_match_result));
1980 * If we don't have enough space to put in another
1981 * match result, save our position and tell the
1982 * user there are more devices to check.
1984 if (spaceleft < sizeof(struct dev_match_result)) {
1985 struct periph_driver **pdrv;
1988 bzero(&cdm->pos, sizeof(cdm->pos));
1989 cdm->pos.position_type =
1990 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1994 * This may look a bit non-sensical, but it is
1995 * actually quite logical. There are very few
1996 * peripheral drivers, and bloating every peripheral
1997 * structure with a pointer back to its parent
1998 * peripheral driver linker set entry would cost
1999 * more in the long run than doing this quick lookup.
2001 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2002 if (strcmp((*pdrv)->driver_name,
2003 periph->periph_name) == 0)
2007 if (*pdrv == NULL) {
2008 cdm->status = CAM_DEV_MATCH_ERROR;
2012 cdm->pos.cookie.pdrv = pdrv;
2014 * The periph generation slot does double duty, as
2015 * does the periph pointer slot. They are used for
2016 * both edt and pdrv lookups and positioning.
2018 cdm->pos.cookie.periph = periph;
2019 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2020 (*pdrv)->generation;
2021 cdm->status = CAM_DEV_MATCH_MORE;
2025 j = cdm->num_matches;
2027 cdm->matches[j].type = DEV_MATCH_PERIPH;
2028 cdm->matches[j].result.periph_result.path_id =
2029 periph->path->bus->path_id;
2032 * The transport layer peripheral doesn't have a target or
2035 if (periph->path->target)
2036 cdm->matches[j].result.periph_result.target_id =
2037 periph->path->target->target_id;
2039 cdm->matches[j].result.periph_result.target_id = -1;
2041 if (periph->path->device)
2042 cdm->matches[j].result.periph_result.target_lun =
2043 periph->path->device->lun_id;
2045 cdm->matches[j].result.periph_result.target_lun = -1;
2047 cdm->matches[j].result.periph_result.unit_number =
2048 periph->unit_number;
2049 strncpy(cdm->matches[j].result.periph_result.periph_name,
2050 periph->periph_name, DEV_IDLEN);
2057 xptperiphlistmatch(struct ccb_dev_match *cdm)
2061 cdm->num_matches = 0;
2064 * At this point in the edt traversal function, we check the bus
2065 * list generation to make sure that no busses have been added or
2066 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2067 * For the peripheral driver list traversal function, however, we
2068 * don't have to worry about new peripheral driver types coming or
2069 * going; they're in a linker set, and therefore can't change
2070 * without a recompile.
2073 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2074 && (cdm->pos.cookie.pdrv != NULL))
2075 ret = xptpdrvtraverse(
2076 (struct periph_driver **)cdm->pos.cookie.pdrv,
2077 xptplistpdrvfunc, cdm);
2079 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2082 * If we get back 0, that means that we had to stop before fully
2083 * traversing the peripheral driver tree. It also means that one of
2084 * the subroutines has set the status field to the proper value. If
2085 * we get back 1, we've fully traversed the EDT and copied out any
2089 cdm->status = CAM_DEV_MATCH_LAST;
2095 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2097 struct cam_eb *bus, *next_bus;
2103 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2110 * XXX The locking here is obviously very complex. We
2111 * should work to simplify it.
2114 CAM_SIM_LOCK(bus->sim);
2115 retval = tr_func(bus, arg);
2116 CAM_SIM_UNLOCK(bus->sim);
2119 next_bus = TAILQ_NEXT(bus, links);
2122 xpt_release_bus(bus);
2134 xpt_sim_opened(struct cam_sim *sim)
2137 struct cam_et *target;
2138 struct cam_ed *device;
2139 struct cam_periph *periph;
2141 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2142 mtx_assert(sim->mtx, MA_OWNED);
2145 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2146 if (bus->sim != sim)
2149 TAILQ_FOREACH(target, &bus->et_entries, links) {
2150 TAILQ_FOREACH(device, &target->ed_entries, links) {
2151 SLIST_FOREACH(periph, &device->periphs,
2153 if (periph->refcount > 0) {
2167 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2168 xpt_targetfunc_t *tr_func, void *arg)
2170 struct cam_et *target, *next_target;
2173 mtx_assert(bus->sim->mtx, MA_OWNED);
2175 for (target = (start_target ? start_target :
2176 TAILQ_FIRST(&bus->et_entries));
2177 target != NULL; target = next_target) {
2181 retval = tr_func(target, arg);
2183 next_target = TAILQ_NEXT(target, links);
2185 xpt_release_target(target);
2195 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2196 xpt_devicefunc_t *tr_func, void *arg)
2198 struct cam_ed *device, *next_device;
2201 mtx_assert(target->bus->sim->mtx, MA_OWNED);
2203 for (device = (start_device ? start_device :
2204 TAILQ_FIRST(&target->ed_entries));
2206 device = next_device) {
2209 * Hold a reference so the current device does not go away
2214 retval = tr_func(device, arg);
2217 * Grab our next pointer before we release the current
2220 next_device = TAILQ_NEXT(device, links);
2222 xpt_release_device(device);
2232 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2233 xpt_periphfunc_t *tr_func, void *arg)
2235 struct cam_periph *periph, *next_periph;
2240 mtx_assert(device->sim->mtx, MA_OWNED);
2242 for (periph = (start_periph ? start_periph :
2243 SLIST_FIRST(&device->periphs));
2245 periph = next_periph) {
2249 * In this case, we want to show peripherals that have been
2250 * invalidated, but not peripherals that are scheduled to
2251 * be freed. So instead of calling cam_periph_acquire(),
2252 * which will fail if the periph has been invalidated, we
2253 * just check for the free flag here. If it is in the
2254 * process of being freed, we skip to the next periph.
2256 if (periph->flags & CAM_PERIPH_FREE) {
2257 next_periph = SLIST_NEXT(periph, periph_links);
2262 * Acquire a reference to this periph while we call the
2263 * traversal function, so it can't go away.
2267 retval = tr_func(periph, arg);
2270 * Grab the next peripheral before we release this one, so
2271 * our next pointer is still valid.
2273 next_periph = SLIST_NEXT(periph, periph_links);
2275 cam_periph_release_locked_buses(periph);
2289 xptpdrvtraverse(struct periph_driver **start_pdrv,
2290 xpt_pdrvfunc_t *tr_func, void *arg)
2292 struct periph_driver **pdrv;
2298 * We don't traverse the peripheral driver list like we do the
2299 * other lists, because it is a linker set, and therefore cannot be
2300 * changed during runtime. If the peripheral driver list is ever
2301 * re-done to be something other than a linker set (i.e. it can
2302 * change while the system is running), the list traversal should
2303 * be modified to work like the other traversal functions.
2305 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2306 *pdrv != NULL; pdrv++) {
2307 retval = tr_func(pdrv, arg);
2317 xptpdperiphtraverse(struct periph_driver **pdrv,
2318 struct cam_periph *start_periph,
2319 xpt_periphfunc_t *tr_func, void *arg)
2321 struct cam_periph *periph, *next_periph;
2322 struct cam_sim *sim;
2328 for (periph = (start_periph ? start_periph :
2329 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2330 periph = next_periph) {
2334 * In this case, we want to show peripherals that have been
2335 * invalidated, but not peripherals that are scheduled to
2336 * be freed. So instead of calling cam_periph_acquire(),
2337 * which will fail if the periph has been invalidated, we
2338 * just check for the free flag here. If it is free, we
2339 * skip to the next periph.
2341 if (periph->flags & CAM_PERIPH_FREE) {
2342 next_periph = TAILQ_NEXT(periph, unit_links);
2347 * Acquire a reference to this periph while we call the
2348 * traversal function, so it can't go away.
2355 retval = tr_func(periph, arg);
2358 * Grab the next peripheral before we release this one, so
2359 * our next pointer is still valid.
2361 next_periph = TAILQ_NEXT(periph, unit_links);
2363 cam_periph_release_locked_buses(periph);
2364 CAM_SIM_UNLOCK(sim);
2377 xptdefbusfunc(struct cam_eb *bus, void *arg)
2379 struct xpt_traverse_config *tr_config;
2381 tr_config = (struct xpt_traverse_config *)arg;
2383 if (tr_config->depth == XPT_DEPTH_BUS) {
2384 xpt_busfunc_t *tr_func;
2386 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2388 return(tr_func(bus, tr_config->tr_arg));
2390 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2394 xptdeftargetfunc(struct cam_et *target, void *arg)
2396 struct xpt_traverse_config *tr_config;
2398 tr_config = (struct xpt_traverse_config *)arg;
2400 if (tr_config->depth == XPT_DEPTH_TARGET) {
2401 xpt_targetfunc_t *tr_func;
2403 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2405 return(tr_func(target, tr_config->tr_arg));
2407 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2411 xptdefdevicefunc(struct cam_ed *device, void *arg)
2413 struct xpt_traverse_config *tr_config;
2415 tr_config = (struct xpt_traverse_config *)arg;
2417 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2418 xpt_devicefunc_t *tr_func;
2420 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2422 return(tr_func(device, tr_config->tr_arg));
2424 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2428 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2430 struct xpt_traverse_config *tr_config;
2431 xpt_periphfunc_t *tr_func;
2433 tr_config = (struct xpt_traverse_config *)arg;
2435 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2438 * Unlike the other default functions, we don't check for depth
2439 * here. The peripheral driver level is the last level in the EDT,
2440 * so if we're here, we should execute the function in question.
2442 return(tr_func(periph, tr_config->tr_arg));
2446 * Execute the given function for every bus in the EDT.
2449 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2451 struct xpt_traverse_config tr_config;
2453 tr_config.depth = XPT_DEPTH_BUS;
2454 tr_config.tr_func = tr_func;
2455 tr_config.tr_arg = arg;
2457 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2461 * Execute the given function for every device in the EDT.
2464 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2466 struct xpt_traverse_config tr_config;
2468 tr_config.depth = XPT_DEPTH_DEVICE;
2469 tr_config.tr_func = tr_func;
2470 tr_config.tr_arg = arg;
2472 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2476 xptsetasyncfunc(struct cam_ed *device, void *arg)
2478 struct cam_path path;
2479 struct ccb_getdev cgd;
2480 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2483 * Don't report unconfigured devices (Wildcard devs,
2484 * devices only for target mode, device instances
2485 * that have been invalidated but are waiting for
2486 * their last reference count to be released).
2488 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2491 xpt_compile_path(&path,
2493 device->target->bus->path_id,
2494 device->target->target_id,
2496 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2497 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2498 xpt_action((union ccb *)&cgd);
2499 csa->callback(csa->callback_arg,
2502 xpt_release_path(&path);
2508 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2510 struct cam_path path;
2511 struct ccb_pathinq cpi;
2512 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2514 xpt_compile_path(&path, /*periph*/NULL,
2516 CAM_TARGET_WILDCARD,
2518 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2519 cpi.ccb_h.func_code = XPT_PATH_INQ;
2520 xpt_action((union ccb *)&cpi);
2521 csa->callback(csa->callback_arg,
2524 xpt_release_path(&path);
2530 xpt_action(union ccb *start_ccb)
2533 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2535 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2536 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2540 xpt_action_default(union ccb *start_ccb)
2542 struct cam_path *path;
2544 path = start_ccb->ccb_h.path;
2545 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2547 switch (start_ccb->ccb_h.func_code) {
2550 struct cam_ed *device;
2553 * For the sake of compatibility with SCSI-1
2554 * devices that may not understand the identify
2555 * message, we include lun information in the
2556 * second byte of all commands. SCSI-1 specifies
2557 * that luns are a 3 bit value and reserves only 3
2558 * bits for lun information in the CDB. Later
2559 * revisions of the SCSI spec allow for more than 8
2560 * luns, but have deprecated lun information in the
2561 * CDB. So, if the lun won't fit, we must omit.
2563 * Also be aware that during initial probing for devices,
2564 * the inquiry information is unknown but initialized to 0.
2565 * This means that this code will be exercised while probing
2566 * devices with an ANSI revision greater than 2.
2568 device = path->device;
2569 if (device->protocol_version <= SCSI_REV_2
2570 && start_ccb->ccb_h.target_lun < 8
2571 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2573 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2574 start_ccb->ccb_h.target_lun << 5;
2576 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2580 case XPT_CONT_TARGET_IO:
2581 start_ccb->csio.sense_resid = 0;
2582 start_ccb->csio.resid = 0;
2585 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2586 start_ccb->ataio.resid = 0;
2594 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2595 path->device->sim->devq->alloc_openings += frozen;
2597 xpt_run_dev_allocq(path->bus);
2598 if (xpt_schedule_dev_sendq(path->bus, path->device))
2599 xpt_run_dev_sendq(path->bus);
2602 case XPT_CALC_GEOMETRY:
2604 struct cam_sim *sim;
2606 /* Filter out garbage */
2607 if (start_ccb->ccg.block_size == 0
2608 || start_ccb->ccg.volume_size == 0) {
2609 start_ccb->ccg.cylinders = 0;
2610 start_ccb->ccg.heads = 0;
2611 start_ccb->ccg.secs_per_track = 0;
2612 start_ccb->ccb_h.status = CAM_REQ_CMP;
2615 #if defined(PC98) || defined(__sparc64__)
2617 * In a PC-98 system, geometry translation depens on
2618 * the "real" device geometry obtained from mode page 4.
2619 * SCSI geometry translation is performed in the
2620 * initialization routine of the SCSI BIOS and the result
2621 * stored in host memory. If the translation is available
2622 * in host memory, use it. If not, rely on the default
2623 * translation the device driver performs.
2624 * For sparc64, we may need adjust the geometry of large
2625 * disks in order to fit the limitations of the 16-bit
2626 * fields of the VTOC8 disk label.
2628 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2629 start_ccb->ccb_h.status = CAM_REQ_CMP;
2633 sim = path->bus->sim;
2634 (*(sim->sim_action))(sim, start_ccb);
2639 union ccb* abort_ccb;
2641 abort_ccb = start_ccb->cab.abort_ccb;
2642 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2644 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2645 struct cam_ccbq *ccbq;
2646 struct cam_ed *device;
2648 device = abort_ccb->ccb_h.path->device;
2649 ccbq = &device->ccbq;
2650 device->sim->devq->alloc_openings -=
2651 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2652 abort_ccb->ccb_h.status =
2653 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2654 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2655 xpt_done(abort_ccb);
2656 start_ccb->ccb_h.status = CAM_REQ_CMP;
2659 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2660 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2662 * We've caught this ccb en route to
2663 * the SIM. Flag it for abort and the
2664 * SIM will do so just before starting
2665 * real work on the CCB.
2667 abort_ccb->ccb_h.status =
2668 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2669 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2670 start_ccb->ccb_h.status = CAM_REQ_CMP;
2674 if (XPT_FC_IS_QUEUED(abort_ccb)
2675 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2677 * It's already completed but waiting
2678 * for our SWI to get to it.
2680 start_ccb->ccb_h.status = CAM_UA_ABORT;
2684 * If we weren't able to take care of the abort request
2685 * in the XPT, pass the request down to the SIM for processing.
2689 case XPT_ACCEPT_TARGET_IO:
2691 case XPT_IMMED_NOTIFY:
2692 case XPT_NOTIFY_ACK:
2694 case XPT_IMMEDIATE_NOTIFY:
2695 case XPT_NOTIFY_ACKNOWLEDGE:
2696 case XPT_GET_SIM_KNOB:
2697 case XPT_SET_SIM_KNOB:
2699 struct cam_sim *sim;
2701 sim = path->bus->sim;
2702 (*(sim->sim_action))(sim, start_ccb);
2707 struct cam_sim *sim;
2709 sim = path->bus->sim;
2710 (*(sim->sim_action))(sim, start_ccb);
2713 case XPT_PATH_STATS:
2714 start_ccb->cpis.last_reset = path->bus->last_reset;
2715 start_ccb->ccb_h.status = CAM_REQ_CMP;
2722 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2723 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2725 struct ccb_getdev *cgd;
2727 cgd = &start_ccb->cgd;
2728 cgd->protocol = dev->protocol;
2729 cgd->inq_data = dev->inq_data;
2730 cgd->ident_data = dev->ident_data;
2731 cgd->inq_flags = dev->inq_flags;
2732 cgd->ccb_h.status = CAM_REQ_CMP;
2733 cgd->serial_num_len = dev->serial_num_len;
2734 if ((dev->serial_num_len > 0)
2735 && (dev->serial_num != NULL))
2736 bcopy(dev->serial_num, cgd->serial_num,
2737 dev->serial_num_len);
2741 case XPT_GDEV_STATS:
2746 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2747 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2749 struct ccb_getdevstats *cgds;
2753 cgds = &start_ccb->cgds;
2756 cgds->dev_openings = dev->ccbq.dev_openings;
2757 cgds->dev_active = dev->ccbq.dev_active;
2758 cgds->devq_openings = dev->ccbq.devq_openings;
2759 cgds->devq_queued = dev->ccbq.queue.entries;
2760 cgds->held = dev->ccbq.held;
2761 cgds->last_reset = tar->last_reset;
2762 cgds->maxtags = dev->maxtags;
2763 cgds->mintags = dev->mintags;
2764 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2765 cgds->last_reset = bus->last_reset;
2766 cgds->ccb_h.status = CAM_REQ_CMP;
2772 struct cam_periph *nperiph;
2773 struct periph_list *periph_head;
2774 struct ccb_getdevlist *cgdl;
2776 struct cam_ed *device;
2783 * Don't want anyone mucking with our data.
2785 device = path->device;
2786 periph_head = &device->periphs;
2787 cgdl = &start_ccb->cgdl;
2790 * Check and see if the list has changed since the user
2791 * last requested a list member. If so, tell them that the
2792 * list has changed, and therefore they need to start over
2793 * from the beginning.
2795 if ((cgdl->index != 0) &&
2796 (cgdl->generation != device->generation)) {
2797 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2802 * Traverse the list of peripherals and attempt to find
2803 * the requested peripheral.
2805 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2806 (nperiph != NULL) && (i <= cgdl->index);
2807 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2808 if (i == cgdl->index) {
2809 strncpy(cgdl->periph_name,
2810 nperiph->periph_name,
2812 cgdl->unit_number = nperiph->unit_number;
2817 cgdl->status = CAM_GDEVLIST_ERROR;
2821 if (nperiph == NULL)
2822 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2824 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2827 cgdl->generation = device->generation;
2829 cgdl->ccb_h.status = CAM_REQ_CMP;
2834 dev_pos_type position_type;
2835 struct ccb_dev_match *cdm;
2837 cdm = &start_ccb->cdm;
2840 * There are two ways of getting at information in the EDT.
2841 * The first way is via the primary EDT tree. It starts
2842 * with a list of busses, then a list of targets on a bus,
2843 * then devices/luns on a target, and then peripherals on a
2844 * device/lun. The "other" way is by the peripheral driver
2845 * lists. The peripheral driver lists are organized by
2846 * peripheral driver. (obviously) So it makes sense to
2847 * use the peripheral driver list if the user is looking
2848 * for something like "da1", or all "da" devices. If the
2849 * user is looking for something on a particular bus/target
2850 * or lun, it's generally better to go through the EDT tree.
2853 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2854 position_type = cdm->pos.position_type;
2858 position_type = CAM_DEV_POS_NONE;
2860 for (i = 0; i < cdm->num_patterns; i++) {
2861 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2862 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2863 position_type = CAM_DEV_POS_EDT;
2868 if (cdm->num_patterns == 0)
2869 position_type = CAM_DEV_POS_EDT;
2870 else if (position_type == CAM_DEV_POS_NONE)
2871 position_type = CAM_DEV_POS_PDRV;
2875 * Note that we drop the SIM lock here, because the EDT
2876 * traversal code needs to do its own locking.
2878 CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
2879 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2880 case CAM_DEV_POS_EDT:
2883 case CAM_DEV_POS_PDRV:
2884 xptperiphlistmatch(cdm);
2887 cdm->status = CAM_DEV_MATCH_ERROR;
2890 CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
2892 if (cdm->status == CAM_DEV_MATCH_ERROR)
2893 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2895 start_ccb->ccb_h.status = CAM_REQ_CMP;
2901 struct ccb_setasync *csa;
2902 struct async_node *cur_entry;
2903 struct async_list *async_head;
2906 csa = &start_ccb->csa;
2907 added = csa->event_enable;
2908 async_head = &path->device->asyncs;
2911 * If there is already an entry for us, simply
2914 cur_entry = SLIST_FIRST(async_head);
2915 while (cur_entry != NULL) {
2916 if ((cur_entry->callback_arg == csa->callback_arg)
2917 && (cur_entry->callback == csa->callback))
2919 cur_entry = SLIST_NEXT(cur_entry, links);
2922 if (cur_entry != NULL) {
2924 * If the request has no flags set,
2927 added &= ~cur_entry->event_enable;
2928 if (csa->event_enable == 0) {
2929 SLIST_REMOVE(async_head, cur_entry,
2931 xpt_release_device(path->device);
2932 free(cur_entry, M_CAMXPT);
2934 cur_entry->event_enable = csa->event_enable;
2936 csa->event_enable = added;
2938 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2940 if (cur_entry == NULL) {
2941 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2944 cur_entry->event_enable = csa->event_enable;
2945 cur_entry->callback_arg = csa->callback_arg;
2946 cur_entry->callback = csa->callback;
2947 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2948 xpt_acquire_device(path->device);
2950 start_ccb->ccb_h.status = CAM_REQ_CMP;
2955 struct ccb_relsim *crs;
2958 crs = &start_ccb->crs;
2962 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2966 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2968 /* Don't ever go below one opening */
2969 if (crs->openings > 0) {
2970 xpt_dev_ccbq_resize(path, crs->openings);
2973 "number of openings is now %d\n",
2979 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2981 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2984 * Just extend the old timeout and decrement
2985 * the freeze count so that a single timeout
2986 * is sufficient for releasing the queue.
2988 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2989 callout_stop(&dev->callout);
2992 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2995 callout_reset(&dev->callout,
2996 (crs->release_timeout * hz) / 1000,
2997 xpt_release_devq_timeout, dev);
2999 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3003 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3005 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3007 * Decrement the freeze count so that a single
3008 * completion is still sufficient to unfreeze
3011 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3014 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3015 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3019 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3021 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3022 || (dev->ccbq.dev_active == 0)) {
3024 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3027 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3028 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3032 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3033 xpt_release_devq_rl(path, /*runlevel*/
3034 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3035 crs->release_timeout : 0,
3036 /*count*/1, /*run_queue*/TRUE);
3038 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
3039 start_ccb->ccb_h.status = CAM_REQ_CMP;
3043 struct cam_path *oldpath;
3044 struct cam_sim *oldsim;
3046 /* Check that all request bits are supported. */
3047 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3048 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3052 cam_dflags = CAM_DEBUG_NONE;
3053 if (cam_dpath != NULL) {
3054 /* To release the old path we must hold proper lock. */
3055 oldpath = cam_dpath;
3057 oldsim = xpt_path_sim(oldpath);
3058 CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
3059 CAM_SIM_LOCK(oldsim);
3060 xpt_free_path(oldpath);
3061 CAM_SIM_UNLOCK(oldsim);
3062 CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
3064 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3065 if (xpt_create_path(&cam_dpath, NULL,
3066 start_ccb->ccb_h.path_id,
3067 start_ccb->ccb_h.target_id,
3068 start_ccb->ccb_h.target_lun) !=
3070 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3072 cam_dflags = start_ccb->cdbg.flags;
3073 start_ccb->ccb_h.status = CAM_REQ_CMP;
3074 xpt_print(cam_dpath, "debugging flags now %x\n",
3078 start_ccb->ccb_h.status = CAM_REQ_CMP;
3081 case XPT_FREEZE_QUEUE:
3083 struct ccb_relsim *crs = &start_ccb->crs;
3085 xpt_freeze_devq_rl(path, /*runlevel*/
3086 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3087 crs->release_timeout : 0, /*count*/1);
3088 start_ccb->ccb_h.status = CAM_REQ_CMP;
3092 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3093 xpt_freeze_devq(path, 1);
3094 start_ccb->ccb_h.status = CAM_REQ_CMP;
3101 printf("%s: CCB type %#x not supported\n", __func__,
3102 start_ccb->ccb_h.func_code);
3103 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3104 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3105 xpt_done(start_ccb);
3112 xpt_polled_action(union ccb *start_ccb)
3115 struct cam_sim *sim;
3116 struct cam_devq *devq;
3120 timeout = start_ccb->ccb_h.timeout * 10;
3121 sim = start_ccb->ccb_h.path->bus->sim;
3123 dev = start_ccb->ccb_h.path->device;
3125 mtx_assert(sim->mtx, MA_OWNED);
3127 /* Don't use ISR for this SIM while polling. */
3128 sim->flags |= CAM_SIM_POLLED;
3131 * Steal an opening so that no other queued requests
3132 * can get it before us while we simulate interrupts.
3134 dev->ccbq.devq_openings--;
3135 dev->ccbq.dev_openings--;
3137 while(((devq != NULL && devq->send_openings <= 0) ||
3138 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3140 (*(sim->sim_poll))(sim);
3141 camisr_runqueue(&sim->sim_doneq);
3144 dev->ccbq.devq_openings++;
3145 dev->ccbq.dev_openings++;
3148 xpt_action(start_ccb);
3149 while(--timeout > 0) {
3150 (*(sim->sim_poll))(sim);
3151 camisr_runqueue(&sim->sim_doneq);
3152 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3159 * XXX Is it worth adding a sim_timeout entry
3160 * point so we can attempt recovery? If
3161 * this is only used for dumps, I don't think
3164 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3167 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3170 /* We will use CAM ISR for this SIM again. */
3171 sim->flags &= ~CAM_SIM_POLLED;
3175 * Schedule a peripheral driver to receive a ccb when it's
3176 * target device has space for more transactions.
3179 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3181 struct cam_ed *device;
3184 mtx_assert(perph->sim->mtx, MA_OWNED);
3186 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3187 device = perph->path->device;
3188 if (periph_is_queued(perph)) {
3189 /* Simply reorder based on new priority */
3190 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3191 (" change priority to %d\n", new_priority));
3192 if (new_priority < perph->pinfo.priority) {
3193 camq_change_priority(&device->drvq,
3196 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3199 /* New entry on the queue */
3200 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3201 (" added periph to queue\n"));
3202 perph->pinfo.priority = new_priority;
3203 perph->pinfo.generation = ++device->drvq.generation;
3204 camq_insert(&device->drvq, &perph->pinfo);
3205 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3208 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3209 (" calling xpt_run_devq\n"));
3210 xpt_run_dev_allocq(perph->path->bus);
3216 * Schedule a device to run on a given queue.
3217 * If the device was inserted as a new entry on the queue,
3218 * return 1 meaning the device queue should be run. If we
3219 * were already queued, implying someone else has already
3220 * started the queue, return 0 so the caller doesn't attempt
3224 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3225 u_int32_t new_priority)
3228 u_int32_t old_priority;
3230 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3232 old_priority = pinfo->priority;
3235 * Are we already queued?
3237 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3238 /* Simply reorder based on new priority */
3239 if (new_priority < old_priority) {
3240 camq_change_priority(queue, pinfo->index,
3242 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3243 ("changed priority to %d\n",
3249 /* New entry on the queue */
3250 if (new_priority < old_priority)
3251 pinfo->priority = new_priority;
3253 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3254 ("Inserting onto queue\n"));
3255 pinfo->generation = ++queue->generation;
3256 camq_insert(queue, pinfo);
3263 xpt_run_dev_allocq(struct cam_eb *bus)
3265 struct cam_devq *devq;
3267 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3268 devq = bus->sim->devq;
3270 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3271 (" qfrozen_cnt == 0x%x, entries == %d, "
3272 "openings == %d, active == %d\n",
3273 devq->alloc_queue.qfrozen_cnt[0],
3274 devq->alloc_queue.entries,
3275 devq->alloc_openings,
3276 devq->alloc_active));
3278 devq->alloc_queue.qfrozen_cnt[0]++;
3279 while ((devq->alloc_queue.entries > 0)
3280 && (devq->alloc_openings > 0)
3281 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3282 struct cam_ed_qinfo *qinfo;
3283 struct cam_ed *device;
3284 union ccb *work_ccb;
3285 struct cam_periph *drv;
3288 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3290 device = qinfo->device;
3291 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3292 ("running device %p\n", device));
3294 drvq = &device->drvq;
3295 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3296 "Device on queue without any work to do"));
3297 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3298 devq->alloc_openings--;
3299 devq->alloc_active++;
3300 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3301 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3302 drv->pinfo.priority);
3303 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3304 ("calling periph start\n"));
3305 drv->periph_start(drv, work_ccb);
3308 * Malloc failure in alloc_ccb
3311 * XXX add us to a list to be run from free_ccb
3312 * if we don't have any ccbs active on this
3313 * device queue otherwise we may never get run
3319 /* We may have more work. Attempt to reschedule. */
3320 xpt_schedule_dev_allocq(bus, device);
3322 devq->alloc_queue.qfrozen_cnt[0]--;
3326 xpt_run_dev_sendq(struct cam_eb *bus)
3328 struct cam_devq *devq;
3329 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3331 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3333 devq = bus->sim->devq;
3335 devq->send_queue.qfrozen_cnt[0]++;
3336 while ((devq->send_queue.entries > 0)
3337 && (devq->send_openings > 0)
3338 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3339 struct cam_ed_qinfo *qinfo;
3340 struct cam_ed *device;
3341 union ccb *work_ccb;
3342 struct cam_sim *sim;
3344 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3346 device = qinfo->device;
3347 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3348 ("running device %p\n", device));
3350 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3351 if (work_ccb == NULL) {
3352 printf("device on run queue with no ccbs???\n");
3356 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3358 mtx_lock(&xsoftc.xpt_lock);
3359 if (xsoftc.num_highpower <= 0) {
3361 * We got a high power command, but we
3362 * don't have any available slots. Freeze
3363 * the device queue until we have a slot
3366 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3367 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3371 mtx_unlock(&xsoftc.xpt_lock);
3375 * Consume a high power slot while
3378 xsoftc.num_highpower--;
3380 mtx_unlock(&xsoftc.xpt_lock);
3382 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3383 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3385 devq->send_openings--;
3386 devq->send_active++;
3388 xpt_schedule_dev_sendq(bus, device);
3390 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3392 * The client wants to freeze the queue
3393 * after this CCB is sent.
3395 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3398 /* In Target mode, the peripheral driver knows best... */
3399 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3400 if ((device->inq_flags & SID_CmdQue) != 0
3401 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3402 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3405 * Clear this in case of a retried CCB that
3406 * failed due to a rejected tag.
3408 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3411 switch (work_ccb->ccb_h.func_code) {
3413 CAM_DEBUG(work_ccb->ccb_h.path,
3414 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3415 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3417 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3418 cdb_str, sizeof(cdb_str))));
3421 CAM_DEBUG(work_ccb->ccb_h.path,
3422 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3423 ata_op_string(&work_ccb->ataio.cmd),
3424 ata_cmd_string(&work_ccb->ataio.cmd,
3425 cdb_str, sizeof(cdb_str))));
3432 * Device queues can be shared among multiple sim instances
3433 * that reside on different busses. Use the SIM in the queue
3434 * CCB's path, rather than the one in the bus that was passed
3435 * into this function.
3437 sim = work_ccb->ccb_h.path->bus->sim;
3438 (*(sim->sim_action))(sim, work_ccb);
3440 devq->send_queue.qfrozen_cnt[0]--;
3444 * This function merges stuff from the slave ccb into the master ccb, while
3445 * keeping important fields in the master ccb constant.
3448 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3452 * Pull fields that are valid for peripheral drivers to set
3453 * into the master CCB along with the CCB "payload".
3455 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3456 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3457 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3458 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3459 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3460 sizeof(union ccb) - sizeof(struct ccb_hdr));
3464 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3467 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3468 ccb_h->pinfo.priority = priority;
3470 ccb_h->path_id = path->bus->path_id;
3472 ccb_h->target_id = path->target->target_id;
3474 ccb_h->target_id = CAM_TARGET_WILDCARD;
3476 ccb_h->target_lun = path->device->lun_id;
3477 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3479 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3481 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3485 /* Path manipulation functions */
3487 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3488 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3490 struct cam_path *path;
3493 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3496 status = CAM_RESRC_UNAVAIL;
3499 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3500 if (status != CAM_REQ_CMP) {
3501 free(path, M_CAMPATH);
3504 *new_path_ptr = path;
3509 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3510 struct cam_periph *periph, path_id_t path_id,
3511 target_id_t target_id, lun_id_t lun_id)
3513 struct cam_path *path;
3514 struct cam_eb *bus = NULL;
3517 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
3519 bus = xpt_find_bus(path_id);
3521 CAM_SIM_LOCK(bus->sim);
3522 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3524 CAM_SIM_UNLOCK(bus->sim);
3525 xpt_release_bus(bus);
3527 if (status != CAM_REQ_CMP) {
3528 free(path, M_CAMPATH);
3531 *new_path_ptr = path;
3536 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3537 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3540 struct cam_et *target;
3541 struct cam_ed *device;
3544 status = CAM_REQ_CMP; /* Completed without error */
3545 target = NULL; /* Wildcarded */
3546 device = NULL; /* Wildcarded */
3549 * We will potentially modify the EDT, so block interrupts
3550 * that may attempt to create cam paths.
3552 bus = xpt_find_bus(path_id);
3554 status = CAM_PATH_INVALID;
3556 target = xpt_find_target(bus, target_id);
3557 if (target == NULL) {
3559 struct cam_et *new_target;
3561 new_target = xpt_alloc_target(bus, target_id);
3562 if (new_target == NULL) {
3563 status = CAM_RESRC_UNAVAIL;
3565 target = new_target;
3568 if (target != NULL) {
3569 device = xpt_find_device(target, lun_id);
3570 if (device == NULL) {
3572 struct cam_ed *new_device;
3575 (*(bus->xport->alloc_device))(bus,
3578 if (new_device == NULL) {
3579 status = CAM_RESRC_UNAVAIL;
3581 device = new_device;
3588 * Only touch the user's data if we are successful.
3590 if (status == CAM_REQ_CMP) {
3591 new_path->periph = perph;
3592 new_path->bus = bus;
3593 new_path->target = target;
3594 new_path->device = device;
3595 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3598 xpt_release_device(device);
3600 xpt_release_target(target);
3602 xpt_release_bus(bus);
3608 xpt_release_path(struct cam_path *path)
3610 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3611 if (path->device != NULL) {
3612 xpt_release_device(path->device);
3613 path->device = NULL;
3615 if (path->target != NULL) {
3616 xpt_release_target(path->target);
3617 path->target = NULL;
3619 if (path->bus != NULL) {
3620 xpt_release_bus(path->bus);
3626 xpt_free_path(struct cam_path *path)
3629 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3630 xpt_release_path(path);
3631 free(path, M_CAMPATH);
3635 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3636 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3642 *bus_ref = path->bus->refcount;
3648 *periph_ref = path->periph->refcount;
3655 *target_ref = path->target->refcount;
3661 *device_ref = path->device->refcount;
3668 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3669 * in path1, 2 for match with wildcards in path2.
3672 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3676 if (path1->bus != path2->bus) {
3677 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3679 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3684 if (path1->target != path2->target) {
3685 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3688 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3693 if (path1->device != path2->device) {
3694 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3697 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3706 xpt_print_path(struct cam_path *path)
3710 printf("(nopath): ");
3712 if (path->periph != NULL)
3713 printf("(%s%d:", path->periph->periph_name,
3714 path->periph->unit_number);
3716 printf("(noperiph:");
3718 if (path->bus != NULL)
3719 printf("%s%d:%d:", path->bus->sim->sim_name,
3720 path->bus->sim->unit_number,
3721 path->bus->sim->bus_id);
3725 if (path->target != NULL)
3726 printf("%d:", path->target->target_id);
3730 if (path->device != NULL)
3731 printf("%d): ", path->device->lun_id);
3738 xpt_print(struct cam_path *path, const char *fmt, ...)
3741 xpt_print_path(path);
3748 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3753 if (path != NULL && path->bus != NULL)
3754 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3757 sbuf_new(&sb, str, str_len, 0);
3760 sbuf_printf(&sb, "(nopath): ");
3762 if (path->periph != NULL)
3763 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3764 path->periph->unit_number);
3766 sbuf_printf(&sb, "(noperiph:");
3768 if (path->bus != NULL)
3769 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3770 path->bus->sim->unit_number,
3771 path->bus->sim->bus_id);
3773 sbuf_printf(&sb, "nobus:");
3775 if (path->target != NULL)
3776 sbuf_printf(&sb, "%d:", path->target->target_id);
3778 sbuf_printf(&sb, "X:");
3780 if (path->device != NULL)
3781 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3783 sbuf_printf(&sb, "X): ");
3787 return(sbuf_len(&sb));
3791 xpt_path_path_id(struct cam_path *path)
3793 return(path->bus->path_id);
3797 xpt_path_target_id(struct cam_path *path)
3799 if (path->target != NULL)
3800 return (path->target->target_id);
3802 return (CAM_TARGET_WILDCARD);
3806 xpt_path_lun_id(struct cam_path *path)
3808 if (path->device != NULL)
3809 return (path->device->lun_id);
3811 return (CAM_LUN_WILDCARD);
3815 xpt_path_sim(struct cam_path *path)
3818 return (path->bus->sim);
3822 xpt_path_periph(struct cam_path *path)
3824 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3826 return (path->periph);
3830 xpt_path_legacy_ata_id(struct cam_path *path)
3835 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3836 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3837 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3838 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3841 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3842 path->bus->sim->unit_number < 2) {
3843 bus_id = path->bus->sim->unit_number;
3847 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3848 if (bus == path->bus)
3850 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3851 bus->sim->unit_number >= 2) ||
3852 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3853 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3854 strcmp(bus->sim->sim_name, "siisch") == 0)
3859 if (path->target != NULL) {
3860 if (path->target->target_id < 2)
3861 return (bus_id * 2 + path->target->target_id);
3865 return (bus_id * 2);
3869 * Release a CAM control block for the caller. Remit the cost of the structure
3870 * to the device referenced by the path. If the this device had no 'credits'
3871 * and peripheral drivers have registered async callbacks for this notification
3875 xpt_release_ccb(union ccb *free_ccb)
3877 struct cam_path *path;
3878 struct cam_ed *device;
3880 struct cam_sim *sim;
3882 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3883 path = free_ccb->ccb_h.path;
3884 device = path->device;
3888 mtx_assert(sim->mtx, MA_OWNED);
3890 cam_ccbq_release_opening(&device->ccbq);
3891 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3892 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3893 cam_ccbq_resize(&device->ccbq,
3894 device->ccbq.dev_openings + device->ccbq.dev_active);
3896 if (sim->ccb_count > sim->max_ccbs) {
3897 xpt_free_ccb(free_ccb);
3900 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3903 if (sim->devq == NULL) {
3906 sim->devq->alloc_openings++;
3907 sim->devq->alloc_active--;
3908 if (device_is_alloc_queued(device) == 0)
3909 xpt_schedule_dev_allocq(bus, device);
3910 xpt_run_dev_allocq(bus);
3913 /* Functions accessed by SIM drivers */
3915 static struct xpt_xport xport_default = {
3916 .alloc_device = xpt_alloc_device_default,
3917 .action = xpt_action_default,
3918 .async = xpt_dev_async_default,
3922 * A sim structure, listing the SIM entry points and instance
3923 * identification info is passed to xpt_bus_register to hook the SIM
3924 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3925 * for this new bus and places it in the array of busses and assigns
3926 * it a path_id. The path_id may be influenced by "hard wiring"
3927 * information specified by the user. Once interrupt services are
3928 * available, the bus will be probed.
3931 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3933 struct cam_eb *new_bus;
3934 struct cam_eb *old_bus;
3935 struct ccb_pathinq cpi;
3936 struct cam_path *path;
3939 mtx_assert(sim->mtx, MA_OWNED);
3942 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3943 M_CAMXPT, M_NOWAIT);
3944 if (new_bus == NULL) {
3945 /* Couldn't satisfy request */
3946 return (CAM_RESRC_UNAVAIL);
3948 if (strcmp(sim->sim_name, "xpt") != 0) {
3950 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3953 TAILQ_INIT(&new_bus->et_entries);
3954 new_bus->path_id = sim->path_id;
3957 timevalclear(&new_bus->last_reset);
3959 new_bus->refcount = 1; /* Held until a bus_deregister event */
3960 new_bus->generation = 0;
3963 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3964 while (old_bus != NULL
3965 && old_bus->path_id < new_bus->path_id)
3966 old_bus = TAILQ_NEXT(old_bus, links);
3967 if (old_bus != NULL)
3968 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3970 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3971 xsoftc.bus_generation++;
3975 * Set a default transport so that a PATH_INQ can be issued to
3976 * the SIM. This will then allow for probing and attaching of
3977 * a more appropriate transport.
3979 new_bus->xport = &xport_default;
3981 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3982 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3983 if (status != CAM_REQ_CMP) {
3984 xpt_release_bus(new_bus);
3985 free(path, M_CAMXPT);
3986 return (CAM_RESRC_UNAVAIL);
3989 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3990 cpi.ccb_h.func_code = XPT_PATH_INQ;
3991 xpt_action((union ccb *)&cpi);
3993 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3994 switch (cpi.transport) {
4001 new_bus->xport = scsi_get_xport();
4005 new_bus->xport = ata_get_xport();
4008 new_bus->xport = &xport_default;
4013 /* Notify interested parties */
4014 if (sim->path_id != CAM_XPT_PATH_ID) {
4016 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4017 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4018 union ccb *scan_ccb;
4020 /* Initiate bus rescan. */
4021 scan_ccb = xpt_alloc_ccb_nowait();
4022 if (scan_ccb != NULL) {
4023 scan_ccb->ccb_h.path = path;
4024 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4025 scan_ccb->crcn.flags = 0;
4026 xpt_rescan(scan_ccb);
4029 "Can't allocate CCB to scan bus\n");
4031 xpt_free_path(path);
4033 xpt_free_path(path);
4034 return (CAM_SUCCESS);
4038 xpt_bus_deregister(path_id_t pathid)
4040 struct cam_path bus_path;
4043 status = xpt_compile_path(&bus_path, NULL, pathid,
4044 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4045 if (status != CAM_REQ_CMP)
4048 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4049 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4051 /* Release the reference count held while registered. */
4052 xpt_release_bus(bus_path.bus);
4053 xpt_release_path(&bus_path);
4055 return (CAM_REQ_CMP);
4059 xptnextfreepathid(void)
4067 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4069 /* Find an unoccupied pathid */
4070 while (bus != NULL && bus->path_id <= pathid) {
4071 if (bus->path_id == pathid)
4073 bus = TAILQ_NEXT(bus, links);
4078 * Ensure that this pathid is not reserved for
4079 * a bus that may be registered in the future.
4081 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4083 /* Start the search over */
4091 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4098 pathid = CAM_XPT_PATH_ID;
4099 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4101 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4102 if (strcmp(dname, "scbus")) {
4103 /* Avoid a bit of foot shooting. */
4106 if (dunit < 0) /* unwired?! */
4108 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4109 if (sim_bus == val) {
4113 } else if (sim_bus == 0) {
4114 /* Unspecified matches bus 0 */
4118 printf("Ambiguous scbus configuration for %s%d "
4119 "bus %d, cannot wire down. The kernel "
4120 "config entry for scbus%d should "
4121 "specify a controller bus.\n"
4122 "Scbus will be assigned dynamically.\n",
4123 sim_name, sim_unit, sim_bus, dunit);
4128 if (pathid == CAM_XPT_PATH_ID)
4129 pathid = xptnextfreepathid();
4134 xpt_async_string(u_int32_t async_code)
4137 switch (async_code) {
4138 case AC_BUS_RESET: return ("AC_BUS_RESET");
4139 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4140 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4141 case AC_SENT_BDR: return ("AC_SENT_BDR");
4142 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4143 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4144 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4145 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4146 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4147 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4148 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4149 case AC_CONTRACT: return ("AC_CONTRACT");
4150 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4151 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4153 return ("AC_UNKNOWN");
4157 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4160 struct cam_et *target, *next_target;
4161 struct cam_ed *device, *next_device;
4163 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4164 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4165 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4168 * Most async events come from a CAM interrupt context. In
4169 * a few cases, the error recovery code at the peripheral layer,
4170 * which may run from our SWI or a process context, may signal
4171 * deferred events with a call to xpt_async.
4176 if (async_code == AC_BUS_RESET) {
4177 /* Update our notion of when the last reset occurred */
4178 microtime(&bus->last_reset);
4181 for (target = TAILQ_FIRST(&bus->et_entries);
4183 target = next_target) {
4185 next_target = TAILQ_NEXT(target, links);
4187 if (path->target != target
4188 && path->target->target_id != CAM_TARGET_WILDCARD
4189 && target->target_id != CAM_TARGET_WILDCARD)
4192 if (async_code == AC_SENT_BDR) {
4193 /* Update our notion of when the last reset occurred */
4194 microtime(&path->target->last_reset);
4197 for (device = TAILQ_FIRST(&target->ed_entries);
4199 device = next_device) {
4201 next_device = TAILQ_NEXT(device, links);
4203 if (path->device != device
4204 && path->device->lun_id != CAM_LUN_WILDCARD
4205 && device->lun_id != CAM_LUN_WILDCARD)
4208 * The async callback could free the device.
4209 * If it is a broadcast async, it doesn't hold
4210 * device reference, so take our own reference.
4212 xpt_acquire_device(device);
4213 (*(bus->xport->async))(async_code, bus,
4217 xpt_async_bcast(&device->asyncs, async_code,
4219 xpt_release_device(device);
4224 * If this wasn't a fully wildcarded async, tell all
4225 * clients that want all async events.
4227 if (bus != xpt_periph->path->bus)
4228 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4233 xpt_async_bcast(struct async_list *async_head,
4234 u_int32_t async_code,
4235 struct cam_path *path, void *async_arg)
4237 struct async_node *cur_entry;
4239 cur_entry = SLIST_FIRST(async_head);
4240 while (cur_entry != NULL) {
4241 struct async_node *next_entry;
4243 * Grab the next list entry before we call the current
4244 * entry's callback. This is because the callback function
4245 * can delete its async callback entry.
4247 next_entry = SLIST_NEXT(cur_entry, links);
4248 if ((cur_entry->event_enable & async_code) != 0)
4249 cur_entry->callback(cur_entry->callback_arg,
4252 cur_entry = next_entry;
4257 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4258 struct cam_et *target, struct cam_ed *device,
4261 printf("%s called\n", __func__);
4265 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4267 struct cam_ed *dev = path->device;
4269 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4270 dev->sim->devq->alloc_openings +=
4271 cam_ccbq_freeze(&dev->ccbq, rl, count);
4272 /* Remove frozen device from allocq. */
4273 if (device_is_alloc_queued(dev) &&
4274 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4275 CAMQ_GET_PRIO(&dev->drvq)))) {
4276 camq_remove(&dev->sim->devq->alloc_queue,
4277 dev->alloc_ccb_entry.pinfo.index);
4279 /* Remove frozen device from sendq. */
4280 if (device_is_send_queued(dev) &&
4281 cam_ccbq_frozen_top(&dev->ccbq)) {
4282 camq_remove(&dev->sim->devq->send_queue,
4283 dev->send_ccb_entry.pinfo.index);
4285 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4289 xpt_freeze_devq(struct cam_path *path, u_int count)
4292 return (xpt_freeze_devq_rl(path, 0, count));
4296 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4299 mtx_assert(sim->mtx, MA_OWNED);
4300 sim->devq->send_queue.qfrozen_cnt[0] += count;
4301 return (sim->devq->send_queue.qfrozen_cnt[0]);
4305 xpt_release_devq_timeout(void *arg)
4307 struct cam_ed *device;
4309 device = (struct cam_ed *)arg;
4311 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4315 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4317 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4319 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4323 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4325 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4327 xpt_release_devq_device(path->device, rl, count, run_queue);
4331 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4334 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4336 printf("xpt_release_devq(%d): requested %u > present %u\n",
4337 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4339 count = dev->ccbq.queue.qfrozen_cnt[rl];
4341 dev->sim->devq->alloc_openings -=
4342 cam_ccbq_release(&dev->ccbq, rl, count);
4343 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4344 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4345 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4346 xpt_run_dev_allocq(dev->target->bus);
4348 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4350 * No longer need to wait for a successful
4351 * command completion.
4353 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4355 * Remove any timeouts that might be scheduled
4356 * to release this queue.
4358 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4359 callout_stop(&dev->callout);
4360 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4365 * Now that we are unfrozen schedule the
4366 * device so any pending transactions are
4369 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4370 xpt_run_dev_sendq(dev->target->bus);
4375 xpt_release_simq(struct cam_sim *sim, int run_queue)
4379 mtx_assert(sim->mtx, MA_OWNED);
4380 sendq = &(sim->devq->send_queue);
4381 if (sendq->qfrozen_cnt[0] <= 0) {
4383 printf("xpt_release_simq: requested 1 > present %u\n",
4384 sendq->qfrozen_cnt[0]);
4387 sendq->qfrozen_cnt[0]--;
4388 if (sendq->qfrozen_cnt[0] == 0) {
4390 * If there is a timeout scheduled to release this
4391 * sim queue, remove it. The queue frozen count is
4394 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4395 callout_stop(&sim->callout);
4396 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4402 * Now that we are unfrozen run the send queue.
4404 bus = xpt_find_bus(sim->path_id);
4405 xpt_run_dev_sendq(bus);
4406 xpt_release_bus(bus);
4412 * XXX Appears to be unused.
4415 xpt_release_simq_timeout(void *arg)
4417 struct cam_sim *sim;
4419 sim = (struct cam_sim *)arg;
4420 xpt_release_simq(sim, /* run_queue */ TRUE);
4424 xpt_done(union ccb *done_ccb)
4426 struct cam_sim *sim;
4429 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4430 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4432 * Queue up the request for handling by our SWI handler
4433 * any of the "non-immediate" type of ccbs.
4435 sim = done_ccb->ccb_h.path->bus->sim;
4436 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4438 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4439 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4440 CAM_SIM_BATCH)) == 0) {
4441 mtx_lock(&cam_simq_lock);
4442 first = TAILQ_EMPTY(&cam_simq);
4443 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4444 mtx_unlock(&cam_simq_lock);
4445 sim->flags |= CAM_SIM_ON_DONEQ;
4447 swi_sched(cambio_ih, 0);
4453 xpt_batch_start(struct cam_sim *sim)
4456 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4457 sim->flags |= CAM_SIM_BATCH;
4461 xpt_batch_done(struct cam_sim *sim)
4464 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4465 sim->flags &= ~CAM_SIM_BATCH;
4466 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4467 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4468 camisr_runqueue(&sim->sim_doneq);
4476 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4481 xpt_alloc_ccb_nowait()
4485 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4490 xpt_free_ccb(union ccb *free_ccb)
4492 free(free_ccb, M_CAMCCB);
4497 /* Private XPT functions */
4500 * Get a CAM control block for the caller. Charge the structure to the device
4501 * referenced by the path. If the this device has no 'credits' then the
4502 * device already has the maximum number of outstanding operations under way
4503 * and we return NULL. If we don't have sufficient resources to allocate more
4504 * ccbs, we also return NULL.
4507 xpt_get_ccb(struct cam_ed *device)
4510 struct cam_sim *sim;
4513 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4514 new_ccb = xpt_alloc_ccb_nowait();
4515 if (new_ccb == NULL) {
4518 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4519 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4520 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4524 cam_ccbq_take_opening(&device->ccbq);
4525 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4530 xpt_release_bus(struct cam_eb *bus)
4534 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4535 if (--bus->refcount > 0) {
4539 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4540 ("refcount is zero, but target list is not empty"));
4541 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4542 xsoftc.bus_generation++;
4544 cam_sim_release(bus->sim);
4545 free(bus, M_CAMXPT);
4548 static struct cam_et *
4549 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4551 struct cam_et *cur_target, *target;
4553 mtx_assert(bus->sim->mtx, MA_OWNED);
4554 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4559 TAILQ_INIT(&target->ed_entries);
4561 target->target_id = target_id;
4562 target->refcount = 1;
4563 target->generation = 0;
4564 target->luns = NULL;
4565 timevalclear(&target->last_reset);
4567 * Hold a reference to our parent bus so it
4568 * will not go away before we do.
4574 /* Insertion sort into our bus's target list */
4575 cur_target = TAILQ_FIRST(&bus->et_entries);
4576 while (cur_target != NULL && cur_target->target_id < target_id)
4577 cur_target = TAILQ_NEXT(cur_target, links);
4578 if (cur_target != NULL) {
4579 TAILQ_INSERT_BEFORE(cur_target, target, links);
4581 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4588 xpt_release_target(struct cam_et *target)
4591 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4592 if (--target->refcount > 0)
4594 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4595 ("refcount is zero, but device list is not empty"));
4596 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4597 target->bus->generation++;
4598 xpt_release_bus(target->bus);
4600 free(target->luns, M_CAMXPT);
4601 free(target, M_CAMXPT);
4604 static struct cam_ed *
4605 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4608 struct cam_ed *device;
4610 device = xpt_alloc_device(bus, target, lun_id);
4614 device->mintags = 1;
4615 device->maxtags = 1;
4616 bus->sim->max_ccbs += device->ccbq.devq_openings;
4621 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4623 struct cam_ed *cur_device, *device;
4624 struct cam_devq *devq;
4627 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4628 /* Make space for us in the device queue on our bus */
4629 devq = bus->sim->devq;
4630 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4631 if (status != CAM_REQ_CMP)
4634 device = (struct cam_ed *)malloc(sizeof(*device),
4635 M_CAMDEV, M_NOWAIT|M_ZERO);
4639 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4640 device->alloc_ccb_entry.device = device;
4641 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4642 device->send_ccb_entry.device = device;
4643 device->target = target;
4644 device->lun_id = lun_id;
4645 device->sim = bus->sim;
4646 /* Initialize our queues */
4647 if (camq_init(&device->drvq, 0) != 0) {
4648 free(device, M_CAMDEV);
4651 if (cam_ccbq_init(&device->ccbq,
4652 bus->sim->max_dev_openings) != 0) {
4653 camq_fini(&device->drvq);
4654 free(device, M_CAMDEV);
4657 SLIST_INIT(&device->asyncs);
4658 SLIST_INIT(&device->periphs);
4659 device->generation = 0;
4660 device->owner = NULL;
4661 device->flags = CAM_DEV_UNCONFIGURED;
4662 device->tag_delay_count = 0;
4663 device->tag_saved_openings = 0;
4664 device->refcount = 1;
4665 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4667 cur_device = TAILQ_FIRST(&target->ed_entries);
4668 while (cur_device != NULL && cur_device->lun_id < lun_id)
4669 cur_device = TAILQ_NEXT(cur_device, links);
4670 if (cur_device != NULL)
4671 TAILQ_INSERT_BEFORE(cur_device, device, links);
4673 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4675 target->generation++;
4680 xpt_acquire_device(struct cam_ed *device)
4683 mtx_assert(device->sim->mtx, MA_OWNED);
4688 xpt_release_device(struct cam_ed *device)
4690 struct cam_devq *devq;
4692 mtx_assert(device->sim->mtx, MA_OWNED);
4693 if (--device->refcount > 0)
4696 KASSERT(SLIST_EMPTY(&device->periphs),
4697 ("refcount is zero, but periphs list is not empty"));
4698 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4699 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4700 panic("Removing device while still queued for ccbs");
4702 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4703 callout_stop(&device->callout);
4705 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4706 device->target->generation++;
4707 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4708 /* Release our slot in the devq */
4709 devq = device->target->bus->sim->devq;
4710 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4711 camq_fini(&device->drvq);
4712 cam_ccbq_fini(&device->ccbq);
4714 * Free allocated memory. free(9) does nothing if the
4715 * supplied pointer is NULL, so it is safe to call without
4718 free(device->supported_vpds, M_CAMXPT);
4719 free(device->device_id, M_CAMXPT);
4720 free(device->physpath, M_CAMXPT);
4721 free(device->rcap_buf, M_CAMXPT);
4722 free(device->serial_num, M_CAMXPT);
4724 xpt_release_target(device->target);
4725 free(device, M_CAMDEV);
4729 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4737 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4738 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4739 if (result == CAM_REQ_CMP && (diff < 0)) {
4740 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4742 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4743 || (dev->inq_flags & SID_CmdQue) != 0)
4744 dev->tag_saved_openings = newopenings;
4745 /* Adjust the global limit */
4746 dev->sim->max_ccbs += diff;
4750 static struct cam_eb *
4751 xpt_find_bus(path_id_t path_id)
4756 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4758 bus = TAILQ_NEXT(bus, links)) {
4759 if (bus->path_id == path_id) {
4768 static struct cam_et *
4769 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4771 struct cam_et *target;
4773 mtx_assert(bus->sim->mtx, MA_OWNED);
4774 for (target = TAILQ_FIRST(&bus->et_entries);
4776 target = TAILQ_NEXT(target, links)) {
4777 if (target->target_id == target_id) {
4785 static struct cam_ed *
4786 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4788 struct cam_ed *device;
4790 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4791 for (device = TAILQ_FIRST(&target->ed_entries);
4793 device = TAILQ_NEXT(device, links)) {
4794 if (device->lun_id == lun_id) {
4803 xpt_start_tags(struct cam_path *path)
4805 struct ccb_relsim crs;
4806 struct cam_ed *device;
4807 struct cam_sim *sim;
4810 device = path->device;
4811 sim = path->bus->sim;
4812 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4813 xpt_freeze_devq(path, /*count*/1);
4814 device->inq_flags |= SID_CmdQue;
4815 if (device->tag_saved_openings != 0)
4816 newopenings = device->tag_saved_openings;
4818 newopenings = min(device->maxtags,
4819 sim->max_tagged_dev_openings);
4820 xpt_dev_ccbq_resize(path, newopenings);
4821 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4822 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4823 crs.ccb_h.func_code = XPT_REL_SIMQ;
4824 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4826 = crs.release_timeout
4829 xpt_action((union ccb *)&crs);
4833 xpt_stop_tags(struct cam_path *path)
4835 struct ccb_relsim crs;
4836 struct cam_ed *device;
4837 struct cam_sim *sim;
4839 device = path->device;
4840 sim = path->bus->sim;
4841 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4842 device->tag_delay_count = 0;
4843 xpt_freeze_devq(path, /*count*/1);
4844 device->inq_flags &= ~SID_CmdQue;
4845 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4846 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4847 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4848 crs.ccb_h.func_code = XPT_REL_SIMQ;
4849 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4851 = crs.release_timeout
4854 xpt_action((union ccb *)&crs);
4858 xpt_boot_delay(void *arg)
4865 xpt_config(void *arg)
4868 * Now that interrupts are enabled, go find our devices
4871 /* Setup debugging path */
4872 if (cam_dflags != CAM_DEBUG_NONE) {
4873 if (xpt_create_path_unlocked(&cam_dpath, NULL,
4874 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4875 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4876 printf("xpt_config: xpt_create_path() failed for debug"
4877 " target %d:%d:%d, debugging disabled\n",
4878 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4879 cam_dflags = CAM_DEBUG_NONE;
4884 periphdriver_init(1);
4886 callout_init(&xsoftc.boot_callout, 1);
4887 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4888 xpt_boot_delay, NULL);
4889 /* Fire up rescan thread. */
4890 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4891 printf("xpt_config: failed to create rescan thread.\n");
4899 xsoftc.buses_to_config++;
4904 xpt_release_boot(void)
4907 xsoftc.buses_to_config--;
4908 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4909 struct xpt_task *task;
4911 xsoftc.buses_config_done = 1;
4913 /* Call manually because we don't have any busses */
4914 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4916 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4917 taskqueue_enqueue(taskqueue_thread, &task->task);
4924 * If the given device only has one peripheral attached to it, and if that
4925 * peripheral is the passthrough driver, announce it. This insures that the
4926 * user sees some sort of announcement for every peripheral in their system.
4929 xptpassannouncefunc(struct cam_ed *device, void *arg)
4931 struct cam_periph *periph;
4934 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4935 periph = SLIST_NEXT(periph, periph_links), i++);
4937 periph = SLIST_FIRST(&device->periphs);
4939 && (strncmp(periph->periph_name, "pass", 4) == 0))
4940 xpt_announce_periph(periph, NULL);
4946 xpt_finishconfig_task(void *context, int pending)
4949 periphdriver_init(2);
4951 * Check for devices with no "standard" peripheral driver
4952 * attached. For any devices like that, announce the
4953 * passthrough driver so the user will see something.
4956 xpt_for_all_devices(xptpassannouncefunc, NULL);
4958 /* Release our hook so that the boot can continue. */
4959 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4960 free(xsoftc.xpt_config_hook, M_CAMXPT);
4961 xsoftc.xpt_config_hook = NULL;
4963 free(context, M_CAMXPT);
4967 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4968 struct cam_path *path)
4970 struct ccb_setasync csa;
4975 mtx_lock(&xsoftc.xpt_lock);
4976 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4977 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4978 if (status != CAM_REQ_CMP) {
4979 mtx_unlock(&xsoftc.xpt_lock);
4985 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4986 csa.ccb_h.func_code = XPT_SASYNC_CB;
4987 csa.event_enable = event;
4988 csa.callback = cbfunc;
4989 csa.callback_arg = cbarg;
4990 xpt_action((union ccb *)&csa);
4991 status = csa.ccb_h.status;
4994 xpt_free_path(path);
4995 mtx_unlock(&xsoftc.xpt_lock);
4998 if ((status == CAM_REQ_CMP) &&
4999 (csa.event_enable & AC_FOUND_DEVICE)) {
5001 * Get this peripheral up to date with all
5002 * the currently existing devices.
5004 xpt_for_all_devices(xptsetasyncfunc, &csa);
5006 if ((status == CAM_REQ_CMP) &&
5007 (csa.event_enable & AC_PATH_REGISTERED)) {
5009 * Get this peripheral up to date with all
5010 * the currently existing busses.
5012 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5019 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5021 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5023 switch (work_ccb->ccb_h.func_code) {
5024 /* Common cases first */
5025 case XPT_PATH_INQ: /* Path routing inquiry */
5027 struct ccb_pathinq *cpi;
5029 cpi = &work_ccb->cpi;
5030 cpi->version_num = 1; /* XXX??? */
5031 cpi->hba_inquiry = 0;
5032 cpi->target_sprt = 0;
5034 cpi->hba_eng_cnt = 0;
5035 cpi->max_target = 0;
5037 cpi->initiator_id = 0;
5038 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5039 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5040 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5041 cpi->unit_number = sim->unit_number;
5042 cpi->bus_id = sim->bus_id;
5043 cpi->base_transfer_speed = 0;
5044 cpi->protocol = PROTO_UNSPECIFIED;
5045 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5046 cpi->transport = XPORT_UNSPECIFIED;
5047 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5048 cpi->ccb_h.status = CAM_REQ_CMP;
5053 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5060 * The xpt as a "controller" has no interrupt sources, so polling
5064 xptpoll(struct cam_sim *sim)
5069 xpt_lock_buses(void)
5071 mtx_lock(&xsoftc.xpt_topo_lock);
5075 xpt_unlock_buses(void)
5077 mtx_unlock(&xsoftc.xpt_topo_lock);
5084 struct cam_sim *sim;
5086 mtx_lock(&cam_simq_lock);
5088 while (!TAILQ_EMPTY(&cam_simq)) {
5089 TAILQ_CONCAT(&queue, &cam_simq, links);
5090 mtx_unlock(&cam_simq_lock);
5092 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
5093 TAILQ_REMOVE(&queue, sim, links);
5095 camisr_runqueue(&sim->sim_doneq);
5096 sim->flags &= ~CAM_SIM_ON_DONEQ;
5097 CAM_SIM_UNLOCK(sim);
5099 mtx_lock(&cam_simq_lock);
5101 mtx_unlock(&cam_simq_lock);
5105 camisr_runqueue(void *V_queue)
5107 cam_isrq_t *queue = V_queue;
5108 struct ccb_hdr *ccb_h;
5110 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5113 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5114 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5116 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5121 if (ccb_h->flags & CAM_HIGH_POWER) {
5122 struct highpowerlist *hphead;
5123 union ccb *send_ccb;
5125 mtx_lock(&xsoftc.xpt_lock);
5126 hphead = &xsoftc.highpowerq;
5128 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5131 * Increment the count since this command is done.
5133 xsoftc.num_highpower++;
5136 * Any high powered commands queued up?
5138 if (send_ccb != NULL) {
5140 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5141 mtx_unlock(&xsoftc.xpt_lock);
5143 xpt_release_devq(send_ccb->ccb_h.path,
5144 /*count*/1, /*runqueue*/TRUE);
5146 mtx_unlock(&xsoftc.xpt_lock);
5149 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5152 dev = ccb_h->path->device;
5154 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5155 ccb_h->path->bus->sim->devq->send_active--;
5156 ccb_h->path->bus->sim->devq->send_openings++;
5159 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5160 && (dev->ccbq.dev_active == 0))) {
5161 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5162 xpt_release_devq(ccb_h->path, /*count*/1,
5163 /*run_queue*/FALSE);
5166 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5167 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5168 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5169 xpt_release_devq(ccb_h->path, /*count*/1,
5170 /*run_queue*/FALSE);
5173 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5174 && (--dev->tag_delay_count == 0))
5175 xpt_start_tags(ccb_h->path);
5176 if (!device_is_send_queued(dev)) {
5177 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5182 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5183 xpt_release_simq(ccb_h->path->bus->sim,
5185 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5189 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5190 && (ccb_h->status & CAM_DEV_QFRZN)) {
5191 xpt_release_devq(ccb_h->path, /*count*/1,
5193 ccb_h->status &= ~CAM_DEV_QFRZN;
5195 xpt_run_dev_sendq(ccb_h->path->bus);
5198 /* Call the peripheral driver's callback */
5199 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);