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 */
100 /* number of high powered commands that can go through right now */
101 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
104 /* queue for handling async rescan requests. */
105 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
107 int buses_config_done;
109 /* Registered busses */
110 TAILQ_HEAD(,cam_eb) xpt_busses;
111 u_int bus_generation;
113 struct intr_config_hook *xpt_config_hook;
116 struct callout boot_callout;
118 struct mtx xpt_topo_lock;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
155 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
156 &xsoftc.boot_delay, 0, "Bus registration wait time");
158 /* Queues for our software interrupt handler */
159 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
160 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
161 static cam_simq_t cam_simq;
162 static struct mtx cam_simq_lock;
164 /* Pointers to software interrupt handlers */
165 static void *cambio_ih;
167 struct cam_periph *xpt_periph;
169 static periph_init_t xpt_periph_init;
171 static struct periph_driver xpt_driver =
173 xpt_periph_init, "xpt",
174 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
178 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
180 static d_open_t xptopen;
181 static d_close_t xptclose;
182 static d_ioctl_t xptioctl;
183 static d_ioctl_t xptdoioctl;
185 static struct cdevsw xpt_cdevsw = {
186 .d_version = D_VERSION,
194 /* Storage for debugging datastructures */
195 struct cam_path *cam_dpath;
196 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
197 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
198 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
199 &cam_dflags, 0, "Enabled debug flags");
200 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
201 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
202 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
203 &cam_debug_delay, 0, "Delay in us after each debug message");
205 /* Our boot-time initialization hook */
206 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
208 static moduledata_t cam_moduledata = {
210 cam_module_event_handler,
214 static int xpt_init(void *);
216 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
217 MODULE_VERSION(cam, 1);
220 static void xpt_async_bcast(struct async_list *async_head,
221 u_int32_t async_code,
222 struct cam_path *path,
224 static path_id_t xptnextfreepathid(void);
225 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
226 static union ccb *xpt_get_ccb(struct cam_ed *device);
227 static void xpt_run_dev_allocq(struct cam_ed *device);
228 static void xpt_run_devq(struct cam_devq *devq);
229 static timeout_t xpt_release_devq_timeout;
230 static void xpt_release_simq_timeout(void *arg) __unused;
231 static void xpt_release_bus(struct cam_eb *bus);
232 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
234 static struct cam_et*
235 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
236 static void xpt_release_target(struct cam_et *target);
237 static struct cam_eb*
238 xpt_find_bus(path_id_t path_id);
239 static struct cam_et*
240 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
241 static struct cam_ed*
242 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
243 static void xpt_config(void *arg);
244 static xpt_devicefunc_t xptpassannouncefunc;
245 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
246 static void xptpoll(struct cam_sim *sim);
247 static void camisr(void *);
248 static void camisr_runqueue(struct cam_sim *);
249 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
250 u_int num_patterns, struct cam_eb *bus);
251 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
253 struct cam_ed *device);
254 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
256 struct cam_periph *periph);
257 static xpt_busfunc_t xptedtbusfunc;
258 static xpt_targetfunc_t xptedttargetfunc;
259 static xpt_devicefunc_t xptedtdevicefunc;
260 static xpt_periphfunc_t xptedtperiphfunc;
261 static xpt_pdrvfunc_t xptplistpdrvfunc;
262 static xpt_periphfunc_t xptplistperiphfunc;
263 static int xptedtmatch(struct ccb_dev_match *cdm);
264 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
265 static int xptbustraverse(struct cam_eb *start_bus,
266 xpt_busfunc_t *tr_func, void *arg);
267 static int xpttargettraverse(struct cam_eb *bus,
268 struct cam_et *start_target,
269 xpt_targetfunc_t *tr_func, void *arg);
270 static int xptdevicetraverse(struct cam_et *target,
271 struct cam_ed *start_device,
272 xpt_devicefunc_t *tr_func, void *arg);
273 static int xptperiphtraverse(struct cam_ed *device,
274 struct cam_periph *start_periph,
275 xpt_periphfunc_t *tr_func, void *arg);
276 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
277 xpt_pdrvfunc_t *tr_func, void *arg);
278 static int xptpdperiphtraverse(struct periph_driver **pdrv,
279 struct cam_periph *start_periph,
280 xpt_periphfunc_t *tr_func,
282 static xpt_busfunc_t xptdefbusfunc;
283 static xpt_targetfunc_t xptdeftargetfunc;
284 static xpt_devicefunc_t xptdefdevicefunc;
285 static xpt_periphfunc_t xptdefperiphfunc;
286 static void xpt_finishconfig_task(void *context, int pending);
287 static void xpt_dev_async_default(u_int32_t async_code,
289 struct cam_et *target,
290 struct cam_ed *device,
292 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
293 struct cam_et *target,
295 static xpt_devicefunc_t xptsetasyncfunc;
296 static xpt_busfunc_t xptsetasyncbusfunc;
297 static cam_status xptregister(struct cam_periph *periph,
299 static __inline int periph_is_queued(struct cam_periph *periph);
300 static __inline int device_is_queued(struct cam_ed *device);
303 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
307 if ((dev->ccbq.queue.entries > 0) &&
308 (dev->ccbq.dev_openings > 0) &&
309 (dev->ccbq.queue.qfrozen_cnt == 0)) {
311 * The priority of a device waiting for controller
312 * resources is that of the highest priority CCB
316 xpt_schedule_dev(&devq->send_queue,
317 &dev->devq_entry.pinfo,
318 CAMQ_GET_PRIO(&dev->ccbq.queue));
326 periph_is_queued(struct cam_periph *periph)
328 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
332 device_is_queued(struct cam_ed *device)
334 return (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX);
340 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
344 xptdone(struct cam_periph *periph, union ccb *done_ccb)
346 /* Caller will release the CCB */
347 wakeup(&done_ccb->ccb_h.cbfcnp);
351 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
355 * Only allow read-write access.
357 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
361 * We don't allow nonblocking access.
363 if ((flags & O_NONBLOCK) != 0) {
364 printf("%s: can't do nonblocking access\n", devtoname(dev));
368 /* Mark ourselves open */
369 mtx_lock(&xsoftc.xpt_lock);
370 xsoftc.flags |= XPT_FLAG_OPEN;
371 mtx_unlock(&xsoftc.xpt_lock);
377 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
380 /* Mark ourselves closed */
381 mtx_lock(&xsoftc.xpt_lock);
382 xsoftc.flags &= ~XPT_FLAG_OPEN;
383 mtx_unlock(&xsoftc.xpt_lock);
389 * Don't automatically grab the xpt softc lock here even though this is going
390 * through the xpt device. The xpt device is really just a back door for
391 * accessing other devices and SIMs, so the right thing to do is to grab
392 * the appropriate SIM lock once the bus/SIM is located.
395 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
399 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
400 error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
402 return (xptdoioctl(dev, cmd, addr, flag, td));
408 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
416 * For the transport layer CAMIOCOMMAND ioctl, we really only want
417 * to accept CCB types that don't quite make sense to send through a
418 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
426 inccb = (union ccb *)addr;
428 bus = xpt_find_bus(inccb->ccb_h.path_id);
432 switch (inccb->ccb_h.func_code) {
435 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
436 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
437 xpt_release_bus(bus);
442 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
443 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
444 xpt_release_bus(bus);
452 switch(inccb->ccb_h.func_code) {
460 ccb = xpt_alloc_ccb();
462 CAM_SIM_LOCK(bus->sim);
465 * Create a path using the bus, target, and lun the
468 if (xpt_create_path(&ccb->ccb_h.path, NULL,
469 inccb->ccb_h.path_id,
470 inccb->ccb_h.target_id,
471 inccb->ccb_h.target_lun) !=
474 CAM_SIM_UNLOCK(bus->sim);
478 /* Ensure all of our fields are correct */
479 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
480 inccb->ccb_h.pinfo.priority);
481 xpt_merge_ccb(ccb, inccb);
482 ccb->ccb_h.cbfcnp = xptdone;
483 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
484 bcopy(ccb, inccb, sizeof(union ccb));
485 xpt_free_path(ccb->ccb_h.path);
487 CAM_SIM_UNLOCK(bus->sim);
494 * This is an immediate CCB, so it's okay to
495 * allocate it on the stack.
498 CAM_SIM_LOCK(bus->sim);
501 * Create a path using the bus, target, and lun the
504 if (xpt_create_path(&ccb.ccb_h.path, NULL,
505 inccb->ccb_h.path_id,
506 inccb->ccb_h.target_id,
507 inccb->ccb_h.target_lun) !=
510 CAM_SIM_UNLOCK(bus->sim);
513 /* Ensure all of our fields are correct */
514 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
515 inccb->ccb_h.pinfo.priority);
516 xpt_merge_ccb(&ccb, inccb);
517 ccb.ccb_h.cbfcnp = xptdone;
519 bcopy(&ccb, inccb, sizeof(union ccb));
520 xpt_free_path(ccb.ccb_h.path);
521 CAM_SIM_UNLOCK(bus->sim);
525 case XPT_DEV_MATCH: {
526 struct cam_periph_map_info mapinfo;
527 struct cam_path *old_path;
530 * We can't deal with physical addresses for this
531 * type of transaction.
533 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
540 * Save this in case the caller had it set to
541 * something in particular.
543 old_path = inccb->ccb_h.path;
546 * We really don't need a path for the matching
547 * code. The path is needed because of the
548 * debugging statements in xpt_action(). They
549 * assume that the CCB has a valid path.
551 inccb->ccb_h.path = xpt_periph->path;
553 bzero(&mapinfo, sizeof(mapinfo));
556 * Map the pattern and match buffers into kernel
557 * virtual address space.
559 error = cam_periph_mapmem(inccb, &mapinfo);
562 inccb->ccb_h.path = old_path;
567 * This is an immediate CCB, we can send it on directly.
569 CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
571 CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
574 * Map the buffers back into user space.
576 cam_periph_unmapmem(inccb, &mapinfo);
578 inccb->ccb_h.path = old_path;
587 xpt_release_bus(bus);
591 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
592 * with the periphal driver name and unit name filled in. The other
593 * fields don't really matter as input. The passthrough driver name
594 * ("pass"), and unit number are passed back in the ccb. The current
595 * device generation number, and the index into the device peripheral
596 * driver list, and the status are also passed back. Note that
597 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
598 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
599 * (or rather should be) impossible for the device peripheral driver
600 * list to change since we look at the whole thing in one pass, and
601 * we do it with lock protection.
604 case CAMGETPASSTHRU: {
606 struct cam_periph *periph;
607 struct periph_driver **p_drv;
610 int base_periph_found;
612 ccb = (union ccb *)addr;
613 unit = ccb->cgdl.unit_number;
614 name = ccb->cgdl.periph_name;
615 base_periph_found = 0;
618 * Sanity check -- make sure we don't get a null peripheral
621 if (*ccb->cgdl.periph_name == '\0') {
626 /* Keep the list from changing while we traverse it */
629 /* first find our driver in the list of drivers */
630 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
631 if (strcmp((*p_drv)->driver_name, name) == 0)
634 if (*p_drv == NULL) {
636 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
637 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
638 *ccb->cgdl.periph_name = '\0';
639 ccb->cgdl.unit_number = 0;
645 * Run through every peripheral instance of this driver
646 * and check to see whether it matches the unit passed
647 * in by the user. If it does, get out of the loops and
648 * find the passthrough driver associated with that
651 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
652 periph = TAILQ_NEXT(periph, unit_links)) {
654 if (periph->unit_number == unit)
658 * If we found the peripheral driver that the user passed
659 * in, go through all of the peripheral drivers for that
660 * particular device and look for a passthrough driver.
662 if (periph != NULL) {
663 struct cam_ed *device;
666 base_periph_found = 1;
667 device = periph->path->device;
668 for (i = 0, periph = SLIST_FIRST(&device->periphs);
670 periph = SLIST_NEXT(periph, periph_links), i++) {
672 * Check to see whether we have a
673 * passthrough device or not.
675 if (strcmp(periph->periph_name, "pass") == 0) {
677 * Fill in the getdevlist fields.
679 strcpy(ccb->cgdl.periph_name,
680 periph->periph_name);
681 ccb->cgdl.unit_number =
683 if (SLIST_NEXT(periph, periph_links))
685 CAM_GDEVLIST_MORE_DEVS;
688 CAM_GDEVLIST_LAST_DEVICE;
689 ccb->cgdl.generation =
693 * Fill in some CCB header fields
694 * that the user may want.
697 periph->path->bus->path_id;
698 ccb->ccb_h.target_id =
699 periph->path->target->target_id;
700 ccb->ccb_h.target_lun =
701 periph->path->device->lun_id;
702 ccb->ccb_h.status = CAM_REQ_CMP;
709 * If the periph is null here, one of two things has
710 * happened. The first possibility is that we couldn't
711 * find the unit number of the particular peripheral driver
712 * that the user is asking about. e.g. the user asks for
713 * the passthrough driver for "da11". We find the list of
714 * "da" peripherals all right, but there is no unit 11.
715 * The other possibility is that we went through the list
716 * of peripheral drivers attached to the device structure,
717 * but didn't find one with the name "pass". Either way,
718 * we return ENOENT, since we couldn't find something.
720 if (periph == NULL) {
721 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
722 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
723 *ccb->cgdl.periph_name = '\0';
724 ccb->cgdl.unit_number = 0;
727 * It is unfortunate that this is even necessary,
728 * but there are many, many clueless users out there.
729 * If this is true, the user is looking for the
730 * passthrough driver, but doesn't have one in his
733 if (base_periph_found == 1) {
734 printf("xptioctl: pass driver is not in the "
736 printf("xptioctl: put \"device pass\" in "
737 "your kernel config file\n");
752 cam_module_event_handler(module_t mod, int what, void *arg)
758 if ((error = xpt_init(NULL)) != 0)
771 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
774 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
775 xpt_free_path(done_ccb->ccb_h.path);
776 xpt_free_ccb(done_ccb);
778 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
779 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
784 /* thread to handle bus rescans */
786 xpt_scanner_thread(void *dummy)
793 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
794 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
796 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
797 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
800 sim = ccb->ccb_h.path->bus->sim;
811 xpt_rescan(union ccb *ccb)
815 /* Prepare request */
816 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
817 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
818 ccb->ccb_h.func_code = XPT_SCAN_BUS;
819 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
820 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
821 ccb->ccb_h.func_code = XPT_SCAN_TGT;
822 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
823 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
824 ccb->ccb_h.func_code = XPT_SCAN_LUN;
826 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
827 xpt_free_path(ccb->ccb_h.path);
831 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
832 ccb->ccb_h.cbfcnp = xpt_rescan_done;
833 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
834 /* Don't make duplicate entries for the same paths. */
836 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
837 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
838 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
839 wakeup(&xsoftc.ccb_scanq);
841 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
842 xpt_free_path(ccb->ccb_h.path);
848 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
849 xsoftc.buses_to_config++;
850 wakeup(&xsoftc.ccb_scanq);
854 /* Functions accessed by the peripheral drivers */
856 xpt_init(void *dummy)
858 struct cam_sim *xpt_sim;
859 struct cam_path *path;
860 struct cam_devq *devq;
863 TAILQ_INIT(&xsoftc.xpt_busses);
864 TAILQ_INIT(&cam_simq);
865 TAILQ_INIT(&xsoftc.ccb_scanq);
866 STAILQ_INIT(&xsoftc.highpowerq);
867 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
869 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
870 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
871 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
873 #ifdef CAM_BOOT_DELAY
875 * Override this value at compile time to assist our users
876 * who don't use loader to boot a kernel.
878 xsoftc.boot_delay = CAM_BOOT_DELAY;
881 * The xpt layer is, itself, the equivelent of a SIM.
882 * Allow 16 ccbs in the ccb pool for it. This should
883 * give decent parallelism when we probe busses and
884 * perform other XPT functions.
886 devq = cam_simq_alloc(16);
887 xpt_sim = cam_sim_alloc(xptaction,
892 /*mtx*/&xsoftc.xpt_lock,
893 /*max_dev_transactions*/0,
894 /*max_tagged_dev_transactions*/0,
899 mtx_lock(&xsoftc.xpt_lock);
900 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
901 mtx_unlock(&xsoftc.xpt_lock);
902 printf("xpt_init: xpt_bus_register failed with status %#x,"
903 " failing attach\n", status);
908 * Looking at the XPT from the SIM layer, the XPT is
909 * the equivelent of a peripheral driver. Allocate
910 * a peripheral driver entry for us.
912 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
914 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
915 mtx_unlock(&xsoftc.xpt_lock);
916 printf("xpt_init: xpt_create_path failed with status %#x,"
917 " failing attach\n", status);
921 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
922 path, NULL, 0, xpt_sim);
924 mtx_unlock(&xsoftc.xpt_lock);
925 /* Install our software interrupt handlers */
926 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
928 * Register a callback for when interrupts are enabled.
930 xsoftc.xpt_config_hook =
931 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
932 M_CAMXPT, M_NOWAIT | M_ZERO);
933 if (xsoftc.xpt_config_hook == NULL) {
934 printf("xpt_init: Cannot malloc config hook "
935 "- failing attach\n");
938 xsoftc.xpt_config_hook->ich_func = xpt_config;
939 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
940 free (xsoftc.xpt_config_hook, M_CAMXPT);
941 printf("xpt_init: config_intrhook_establish failed "
942 "- failing attach\n");
949 xptregister(struct cam_periph *periph, void *arg)
951 struct cam_sim *xpt_sim;
953 if (periph == NULL) {
954 printf("xptregister: periph was NULL!!\n");
955 return(CAM_REQ_CMP_ERR);
958 xpt_sim = (struct cam_sim *)arg;
959 xpt_sim->softc = periph;
961 periph->softc = NULL;
967 xpt_add_periph(struct cam_periph *periph)
969 struct cam_ed *device;
971 struct periph_list *periph_head;
973 mtx_assert(periph->sim->mtx, MA_OWNED);
975 device = periph->path->device;
977 periph_head = &device->periphs;
979 status = CAM_REQ_CMP;
981 if (device != NULL) {
983 * Make room for this peripheral
984 * so it will fit in the queue
985 * when it's scheduled to run
987 status = camq_resize(&device->drvq,
988 device->drvq.array_size + 1);
990 device->generation++;
992 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
999 xpt_remove_periph(struct cam_periph *periph)
1001 struct cam_ed *device;
1003 mtx_assert(periph->sim->mtx, MA_OWNED);
1005 device = periph->path->device;
1007 if (device != NULL) {
1008 struct periph_list *periph_head;
1010 periph_head = &device->periphs;
1012 /* Release the slot for this peripheral */
1013 camq_resize(&device->drvq, device->drvq.array_size - 1);
1015 device->generation++;
1017 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1023 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1025 struct cam_path *path = periph->path;
1027 mtx_assert(periph->sim->mtx, MA_OWNED);
1028 periph->flags |= CAM_PERIPH_ANNOUNCED;
1030 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1031 periph->periph_name, periph->unit_number,
1032 path->bus->sim->sim_name,
1033 path->bus->sim->unit_number,
1034 path->bus->sim->bus_id,
1036 path->target->target_id,
1037 path->device->lun_id);
1038 printf("%s%d: ", periph->periph_name, periph->unit_number);
1039 if (path->device->protocol == PROTO_SCSI)
1040 scsi_print_inquiry(&path->device->inq_data);
1041 else if (path->device->protocol == PROTO_ATA ||
1042 path->device->protocol == PROTO_SATAPM)
1043 ata_print_ident(&path->device->ident_data);
1044 else if (path->device->protocol == PROTO_SEMB)
1046 (struct sep_identify_data *)&path->device->ident_data);
1048 printf("Unknown protocol device\n");
1049 if (path->device->serial_num_len > 0) {
1050 /* Don't wrap the screen - print only the first 60 chars */
1051 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1052 periph->unit_number, path->device->serial_num);
1054 /* Announce transport details. */
1055 (*(path->bus->xport->announce))(periph);
1056 /* Announce command queueing. */
1057 if (path->device->inq_flags & SID_CmdQue
1058 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1059 printf("%s%d: Command Queueing enabled\n",
1060 periph->periph_name, periph->unit_number);
1062 /* Announce caller's details if they've passed in. */
1063 if (announce_string != NULL)
1064 printf("%s%d: %s\n", periph->periph_name,
1065 periph->unit_number, announce_string);
1069 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1072 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1073 periph->unit_number, quirks, bit_string);
1078 xpt_denounce_periph(struct cam_periph *periph)
1080 struct cam_path *path = periph->path;
1082 mtx_assert(periph->sim->mtx, MA_OWNED);
1083 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1084 periph->periph_name, periph->unit_number,
1085 path->bus->sim->sim_name,
1086 path->bus->sim->unit_number,
1087 path->bus->sim->bus_id,
1089 path->target->target_id,
1090 path->device->lun_id);
1091 printf("%s%d: ", periph->periph_name, periph->unit_number);
1092 if (path->device->protocol == PROTO_SCSI)
1093 scsi_print_inquiry_short(&path->device->inq_data);
1094 else if (path->device->protocol == PROTO_ATA ||
1095 path->device->protocol == PROTO_SATAPM)
1096 ata_print_ident_short(&path->device->ident_data);
1097 else if (path->device->protocol == PROTO_SEMB)
1098 semb_print_ident_short(
1099 (struct sep_identify_data *)&path->device->ident_data);
1101 printf("Unknown protocol device");
1102 if (path->device->serial_num_len > 0)
1103 printf(" s/n %.60s", path->device->serial_num);
1104 printf(" detached\n");
1109 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1112 struct ccb_dev_advinfo cdai;
1113 struct scsi_vpd_id_descriptor *idd;
1115 mtx_assert(path->bus->sim->mtx, MA_OWNED);
1117 memset(&cdai, 0, sizeof(cdai));
1118 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1119 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1122 if (!strcmp(attr, "GEOM::ident"))
1123 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1124 else if (!strcmp(attr, "GEOM::physpath"))
1125 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1126 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1127 strcmp(attr, "GEOM::lunname") == 0) {
1128 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1129 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1133 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1134 if (cdai.buf == NULL) {
1138 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1139 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1140 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1141 if (cdai.provsiz == 0)
1143 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1144 if (strcmp(attr, "GEOM::lunid") == 0) {
1145 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1146 cdai.provsiz, scsi_devid_is_lun_naa);
1148 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1149 cdai.provsiz, scsi_devid_is_lun_eui64);
1153 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1154 cdai.provsiz, scsi_devid_is_lun_t10);
1156 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1157 cdai.provsiz, scsi_devid_is_lun_name);
1161 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1162 (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1163 l = strnlen(idd->identifier, idd->length);
1165 bcopy(idd->identifier, buf, l);
1170 if (idd->length * 2 < len) {
1171 for (l = 0; l < idd->length; l++)
1172 sprintf(buf + l * 2, "%02x",
1173 idd->identifier[l]);
1179 if (strlcpy(buf, cdai.buf, len) >= len)
1184 if (cdai.buf != NULL)
1185 free(cdai.buf, M_CAMXPT);
1189 static dev_match_ret
1190 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1193 dev_match_ret retval;
1196 retval = DM_RET_NONE;
1199 * If we aren't given something to match against, that's an error.
1202 return(DM_RET_ERROR);
1205 * If there are no match entries, then this bus matches no
1208 if ((patterns == NULL) || (num_patterns == 0))
1209 return(DM_RET_DESCEND | DM_RET_COPY);
1211 for (i = 0; i < num_patterns; i++) {
1212 struct bus_match_pattern *cur_pattern;
1215 * If the pattern in question isn't for a bus node, we
1216 * aren't interested. However, we do indicate to the
1217 * calling routine that we should continue descending the
1218 * tree, since the user wants to match against lower-level
1221 if (patterns[i].type != DEV_MATCH_BUS) {
1222 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1223 retval |= DM_RET_DESCEND;
1227 cur_pattern = &patterns[i].pattern.bus_pattern;
1230 * If they want to match any bus node, we give them any
1233 if (cur_pattern->flags == BUS_MATCH_ANY) {
1234 /* set the copy flag */
1235 retval |= DM_RET_COPY;
1238 * If we've already decided on an action, go ahead
1241 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1246 * Not sure why someone would do this...
1248 if (cur_pattern->flags == BUS_MATCH_NONE)
1251 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1252 && (cur_pattern->path_id != bus->path_id))
1255 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1256 && (cur_pattern->bus_id != bus->sim->bus_id))
1259 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1260 && (cur_pattern->unit_number != bus->sim->unit_number))
1263 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1264 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1269 * If we get to this point, the user definitely wants
1270 * information on this bus. So tell the caller to copy the
1273 retval |= DM_RET_COPY;
1276 * If the return action has been set to descend, then we
1277 * know that we've already seen a non-bus matching
1278 * expression, therefore we need to further descend the tree.
1279 * This won't change by continuing around the loop, so we
1280 * go ahead and return. If we haven't seen a non-bus
1281 * matching expression, we keep going around the loop until
1282 * we exhaust the matching expressions. We'll set the stop
1283 * flag once we fall out of the loop.
1285 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1290 * If the return action hasn't been set to descend yet, that means
1291 * we haven't seen anything other than bus matching patterns. So
1292 * tell the caller to stop descending the tree -- the user doesn't
1293 * want to match against lower level tree elements.
1295 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1296 retval |= DM_RET_STOP;
1301 static dev_match_ret
1302 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1303 struct cam_ed *device)
1305 dev_match_ret retval;
1308 retval = DM_RET_NONE;
1311 * If we aren't given something to match against, that's an error.
1314 return(DM_RET_ERROR);
1317 * If there are no match entries, then this device matches no
1320 if ((patterns == NULL) || (num_patterns == 0))
1321 return(DM_RET_DESCEND | DM_RET_COPY);
1323 for (i = 0; i < num_patterns; i++) {
1324 struct device_match_pattern *cur_pattern;
1325 struct scsi_vpd_device_id *device_id_page;
1328 * If the pattern in question isn't for a device node, we
1329 * aren't interested.
1331 if (patterns[i].type != DEV_MATCH_DEVICE) {
1332 if ((patterns[i].type == DEV_MATCH_PERIPH)
1333 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1334 retval |= DM_RET_DESCEND;
1338 cur_pattern = &patterns[i].pattern.device_pattern;
1340 /* Error out if mutually exclusive options are specified. */
1341 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1342 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1343 return(DM_RET_ERROR);
1346 * If they want to match any device node, we give them any
1349 if (cur_pattern->flags == DEV_MATCH_ANY)
1353 * Not sure why someone would do this...
1355 if (cur_pattern->flags == DEV_MATCH_NONE)
1358 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1359 && (cur_pattern->path_id != device->target->bus->path_id))
1362 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1363 && (cur_pattern->target_id != device->target->target_id))
1366 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1367 && (cur_pattern->target_lun != device->lun_id))
1370 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1371 && (cam_quirkmatch((caddr_t)&device->inq_data,
1372 (caddr_t)&cur_pattern->data.inq_pat,
1373 1, sizeof(cur_pattern->data.inq_pat),
1374 scsi_static_inquiry_match) == NULL))
1377 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1378 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1379 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1380 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1381 device->device_id_len
1382 - SVPD_DEVICE_ID_HDR_LEN,
1383 cur_pattern->data.devid_pat.id,
1384 cur_pattern->data.devid_pat.id_len) != 0))
1389 * If we get to this point, the user definitely wants
1390 * information on this device. So tell the caller to copy
1393 retval |= DM_RET_COPY;
1396 * If the return action has been set to descend, then we
1397 * know that we've already seen a peripheral matching
1398 * expression, therefore we need to further descend the tree.
1399 * This won't change by continuing around the loop, so we
1400 * go ahead and return. If we haven't seen a peripheral
1401 * matching expression, we keep going around the loop until
1402 * we exhaust the matching expressions. We'll set the stop
1403 * flag once we fall out of the loop.
1405 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1410 * If the return action hasn't been set to descend yet, that means
1411 * we haven't seen any peripheral matching patterns. So tell the
1412 * caller to stop descending the tree -- the user doesn't want to
1413 * match against lower level tree elements.
1415 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1416 retval |= DM_RET_STOP;
1422 * Match a single peripheral against any number of match patterns.
1424 static dev_match_ret
1425 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1426 struct cam_periph *periph)
1428 dev_match_ret retval;
1432 * If we aren't given something to match against, that's an error.
1435 return(DM_RET_ERROR);
1438 * If there are no match entries, then this peripheral matches no
1441 if ((patterns == NULL) || (num_patterns == 0))
1442 return(DM_RET_STOP | DM_RET_COPY);
1445 * There aren't any nodes below a peripheral node, so there's no
1446 * reason to descend the tree any further.
1448 retval = DM_RET_STOP;
1450 for (i = 0; i < num_patterns; i++) {
1451 struct periph_match_pattern *cur_pattern;
1454 * If the pattern in question isn't for a peripheral, we
1455 * aren't interested.
1457 if (patterns[i].type != DEV_MATCH_PERIPH)
1460 cur_pattern = &patterns[i].pattern.periph_pattern;
1463 * If they want to match on anything, then we will do so.
1465 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1466 /* set the copy flag */
1467 retval |= DM_RET_COPY;
1470 * We've already set the return action to stop,
1471 * since there are no nodes below peripherals in
1478 * Not sure why someone would do this...
1480 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1483 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1484 && (cur_pattern->path_id != periph->path->bus->path_id))
1488 * For the target and lun id's, we have to make sure the
1489 * target and lun pointers aren't NULL. The xpt peripheral
1490 * has a wildcard target and device.
1492 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1493 && ((periph->path->target == NULL)
1494 ||(cur_pattern->target_id != periph->path->target->target_id)))
1497 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1498 && ((periph->path->device == NULL)
1499 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1502 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1503 && (cur_pattern->unit_number != periph->unit_number))
1506 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1507 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1512 * If we get to this point, the user definitely wants
1513 * information on this peripheral. So tell the caller to
1514 * copy the data out.
1516 retval |= DM_RET_COPY;
1519 * The return action has already been set to stop, since
1520 * peripherals don't have any nodes below them in the EDT.
1526 * If we get to this point, the peripheral that was passed in
1527 * doesn't match any of the patterns.
1533 xptedtbusfunc(struct cam_eb *bus, void *arg)
1535 struct ccb_dev_match *cdm;
1536 dev_match_ret retval;
1538 cdm = (struct ccb_dev_match *)arg;
1541 * If our position is for something deeper in the tree, that means
1542 * that we've already seen this node. So, we keep going down.
1544 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1545 && (cdm->pos.cookie.bus == bus)
1546 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1547 && (cdm->pos.cookie.target != NULL))
1548 retval = DM_RET_DESCEND;
1550 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1553 * If we got an error, bail out of the search.
1555 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1556 cdm->status = CAM_DEV_MATCH_ERROR;
1561 * If the copy flag is set, copy this bus out.
1563 if (retval & DM_RET_COPY) {
1566 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1567 sizeof(struct dev_match_result));
1570 * If we don't have enough space to put in another
1571 * match result, save our position and tell the
1572 * user there are more devices to check.
1574 if (spaceleft < sizeof(struct dev_match_result)) {
1575 bzero(&cdm->pos, sizeof(cdm->pos));
1576 cdm->pos.position_type =
1577 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1579 cdm->pos.cookie.bus = bus;
1580 cdm->pos.generations[CAM_BUS_GENERATION]=
1581 xsoftc.bus_generation;
1582 cdm->status = CAM_DEV_MATCH_MORE;
1585 j = cdm->num_matches;
1587 cdm->matches[j].type = DEV_MATCH_BUS;
1588 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1589 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1590 cdm->matches[j].result.bus_result.unit_number =
1591 bus->sim->unit_number;
1592 strncpy(cdm->matches[j].result.bus_result.dev_name,
1593 bus->sim->sim_name, DEV_IDLEN);
1597 * If the user is only interested in busses, there's no
1598 * reason to descend to the next level in the tree.
1600 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1604 * If there is a target generation recorded, check it to
1605 * make sure the target list hasn't changed.
1607 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1608 && (bus == cdm->pos.cookie.bus)
1609 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1610 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1611 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1613 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1617 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1618 && (cdm->pos.cookie.bus == bus)
1619 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1620 && (cdm->pos.cookie.target != NULL))
1621 return(xpttargettraverse(bus,
1622 (struct cam_et *)cdm->pos.cookie.target,
1623 xptedttargetfunc, arg));
1625 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1629 xptedttargetfunc(struct cam_et *target, void *arg)
1631 struct ccb_dev_match *cdm;
1633 cdm = (struct ccb_dev_match *)arg;
1636 * If there is a device list generation recorded, check it to
1637 * make sure the device list hasn't changed.
1639 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1640 && (cdm->pos.cookie.bus == target->bus)
1641 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1642 && (cdm->pos.cookie.target == target)
1643 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1644 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1645 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1646 target->generation)) {
1647 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1651 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1652 && (cdm->pos.cookie.bus == target->bus)
1653 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1654 && (cdm->pos.cookie.target == target)
1655 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1656 && (cdm->pos.cookie.device != NULL))
1657 return(xptdevicetraverse(target,
1658 (struct cam_ed *)cdm->pos.cookie.device,
1659 xptedtdevicefunc, arg));
1661 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1665 xptedtdevicefunc(struct cam_ed *device, void *arg)
1668 struct ccb_dev_match *cdm;
1669 dev_match_ret retval;
1671 cdm = (struct ccb_dev_match *)arg;
1674 * If our position is for something deeper in the tree, that means
1675 * that we've already seen this node. So, we keep going down.
1677 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1678 && (cdm->pos.cookie.device == device)
1679 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1680 && (cdm->pos.cookie.periph != NULL))
1681 retval = DM_RET_DESCEND;
1683 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1686 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1687 cdm->status = CAM_DEV_MATCH_ERROR;
1692 * If the copy flag is set, copy this device out.
1694 if (retval & DM_RET_COPY) {
1697 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1698 sizeof(struct dev_match_result));
1701 * If we don't have enough space to put in another
1702 * match result, save our position and tell the
1703 * user there are more devices to check.
1705 if (spaceleft < sizeof(struct dev_match_result)) {
1706 bzero(&cdm->pos, sizeof(cdm->pos));
1707 cdm->pos.position_type =
1708 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1709 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1711 cdm->pos.cookie.bus = device->target->bus;
1712 cdm->pos.generations[CAM_BUS_GENERATION]=
1713 xsoftc.bus_generation;
1714 cdm->pos.cookie.target = device->target;
1715 cdm->pos.generations[CAM_TARGET_GENERATION] =
1716 device->target->bus->generation;
1717 cdm->pos.cookie.device = device;
1718 cdm->pos.generations[CAM_DEV_GENERATION] =
1719 device->target->generation;
1720 cdm->status = CAM_DEV_MATCH_MORE;
1723 j = cdm->num_matches;
1725 cdm->matches[j].type = DEV_MATCH_DEVICE;
1726 cdm->matches[j].result.device_result.path_id =
1727 device->target->bus->path_id;
1728 cdm->matches[j].result.device_result.target_id =
1729 device->target->target_id;
1730 cdm->matches[j].result.device_result.target_lun =
1732 cdm->matches[j].result.device_result.protocol =
1734 bcopy(&device->inq_data,
1735 &cdm->matches[j].result.device_result.inq_data,
1736 sizeof(struct scsi_inquiry_data));
1737 bcopy(&device->ident_data,
1738 &cdm->matches[j].result.device_result.ident_data,
1739 sizeof(struct ata_params));
1741 /* Let the user know whether this device is unconfigured */
1742 if (device->flags & CAM_DEV_UNCONFIGURED)
1743 cdm->matches[j].result.device_result.flags =
1744 DEV_RESULT_UNCONFIGURED;
1746 cdm->matches[j].result.device_result.flags =
1751 * If the user isn't interested in peripherals, don't descend
1752 * the tree any further.
1754 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1758 * If there is a peripheral list generation recorded, make sure
1759 * it hasn't changed.
1761 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1762 && (device->target->bus == cdm->pos.cookie.bus)
1763 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1764 && (device->target == cdm->pos.cookie.target)
1765 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1766 && (device == cdm->pos.cookie.device)
1767 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1768 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1769 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1770 device->generation)){
1771 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1775 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1776 && (cdm->pos.cookie.bus == device->target->bus)
1777 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1778 && (cdm->pos.cookie.target == device->target)
1779 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1780 && (cdm->pos.cookie.device == device)
1781 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1782 && (cdm->pos.cookie.periph != NULL))
1783 return(xptperiphtraverse(device,
1784 (struct cam_periph *)cdm->pos.cookie.periph,
1785 xptedtperiphfunc, arg));
1787 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1791 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1793 struct ccb_dev_match *cdm;
1794 dev_match_ret retval;
1796 cdm = (struct ccb_dev_match *)arg;
1798 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1800 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1801 cdm->status = CAM_DEV_MATCH_ERROR;
1806 * If the copy flag is set, copy this peripheral out.
1808 if (retval & DM_RET_COPY) {
1811 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1812 sizeof(struct dev_match_result));
1815 * If we don't have enough space to put in another
1816 * match result, save our position and tell the
1817 * user there are more devices to check.
1819 if (spaceleft < sizeof(struct dev_match_result)) {
1820 bzero(&cdm->pos, sizeof(cdm->pos));
1821 cdm->pos.position_type =
1822 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1823 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1826 cdm->pos.cookie.bus = periph->path->bus;
1827 cdm->pos.generations[CAM_BUS_GENERATION]=
1828 xsoftc.bus_generation;
1829 cdm->pos.cookie.target = periph->path->target;
1830 cdm->pos.generations[CAM_TARGET_GENERATION] =
1831 periph->path->bus->generation;
1832 cdm->pos.cookie.device = periph->path->device;
1833 cdm->pos.generations[CAM_DEV_GENERATION] =
1834 periph->path->target->generation;
1835 cdm->pos.cookie.periph = periph;
1836 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1837 periph->path->device->generation;
1838 cdm->status = CAM_DEV_MATCH_MORE;
1842 j = cdm->num_matches;
1844 cdm->matches[j].type = DEV_MATCH_PERIPH;
1845 cdm->matches[j].result.periph_result.path_id =
1846 periph->path->bus->path_id;
1847 cdm->matches[j].result.periph_result.target_id =
1848 periph->path->target->target_id;
1849 cdm->matches[j].result.periph_result.target_lun =
1850 periph->path->device->lun_id;
1851 cdm->matches[j].result.periph_result.unit_number =
1852 periph->unit_number;
1853 strncpy(cdm->matches[j].result.periph_result.periph_name,
1854 periph->periph_name, DEV_IDLEN);
1861 xptedtmatch(struct ccb_dev_match *cdm)
1865 cdm->num_matches = 0;
1868 * Check the bus list generation. If it has changed, the user
1869 * needs to reset everything and start over.
1871 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1872 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1873 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1874 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1878 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1879 && (cdm->pos.cookie.bus != NULL))
1880 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1881 xptedtbusfunc, cdm);
1883 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1886 * If we get back 0, that means that we had to stop before fully
1887 * traversing the EDT. It also means that one of the subroutines
1888 * has set the status field to the proper value. If we get back 1,
1889 * we've fully traversed the EDT and copied out any matching entries.
1892 cdm->status = CAM_DEV_MATCH_LAST;
1898 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1900 struct ccb_dev_match *cdm;
1902 cdm = (struct ccb_dev_match *)arg;
1904 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1905 && (cdm->pos.cookie.pdrv == pdrv)
1906 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1907 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1908 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1909 (*pdrv)->generation)) {
1910 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1914 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1915 && (cdm->pos.cookie.pdrv == pdrv)
1916 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1917 && (cdm->pos.cookie.periph != NULL))
1918 return(xptpdperiphtraverse(pdrv,
1919 (struct cam_periph *)cdm->pos.cookie.periph,
1920 xptplistperiphfunc, arg));
1922 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1926 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1928 struct ccb_dev_match *cdm;
1929 dev_match_ret retval;
1931 cdm = (struct ccb_dev_match *)arg;
1933 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1935 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1936 cdm->status = CAM_DEV_MATCH_ERROR;
1941 * If the copy flag is set, copy this peripheral out.
1943 if (retval & DM_RET_COPY) {
1946 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1947 sizeof(struct dev_match_result));
1950 * If we don't have enough space to put in another
1951 * match result, save our position and tell the
1952 * user there are more devices to check.
1954 if (spaceleft < sizeof(struct dev_match_result)) {
1955 struct periph_driver **pdrv;
1958 bzero(&cdm->pos, sizeof(cdm->pos));
1959 cdm->pos.position_type =
1960 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1964 * This may look a bit non-sensical, but it is
1965 * actually quite logical. There are very few
1966 * peripheral drivers, and bloating every peripheral
1967 * structure with a pointer back to its parent
1968 * peripheral driver linker set entry would cost
1969 * more in the long run than doing this quick lookup.
1971 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1972 if (strcmp((*pdrv)->driver_name,
1973 periph->periph_name) == 0)
1977 if (*pdrv == NULL) {
1978 cdm->status = CAM_DEV_MATCH_ERROR;
1982 cdm->pos.cookie.pdrv = pdrv;
1984 * The periph generation slot does double duty, as
1985 * does the periph pointer slot. They are used for
1986 * both edt and pdrv lookups and positioning.
1988 cdm->pos.cookie.periph = periph;
1989 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1990 (*pdrv)->generation;
1991 cdm->status = CAM_DEV_MATCH_MORE;
1995 j = cdm->num_matches;
1997 cdm->matches[j].type = DEV_MATCH_PERIPH;
1998 cdm->matches[j].result.periph_result.path_id =
1999 periph->path->bus->path_id;
2002 * The transport layer peripheral doesn't have a target or
2005 if (periph->path->target)
2006 cdm->matches[j].result.periph_result.target_id =
2007 periph->path->target->target_id;
2009 cdm->matches[j].result.periph_result.target_id = -1;
2011 if (periph->path->device)
2012 cdm->matches[j].result.periph_result.target_lun =
2013 periph->path->device->lun_id;
2015 cdm->matches[j].result.periph_result.target_lun = -1;
2017 cdm->matches[j].result.periph_result.unit_number =
2018 periph->unit_number;
2019 strncpy(cdm->matches[j].result.periph_result.periph_name,
2020 periph->periph_name, DEV_IDLEN);
2027 xptperiphlistmatch(struct ccb_dev_match *cdm)
2031 cdm->num_matches = 0;
2034 * At this point in the edt traversal function, we check the bus
2035 * list generation to make sure that no busses have been added or
2036 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2037 * For the peripheral driver list traversal function, however, we
2038 * don't have to worry about new peripheral driver types coming or
2039 * going; they're in a linker set, and therefore can't change
2040 * without a recompile.
2043 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2044 && (cdm->pos.cookie.pdrv != NULL))
2045 ret = xptpdrvtraverse(
2046 (struct periph_driver **)cdm->pos.cookie.pdrv,
2047 xptplistpdrvfunc, cdm);
2049 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2052 * If we get back 0, that means that we had to stop before fully
2053 * traversing the peripheral driver tree. It also means that one of
2054 * the subroutines has set the status field to the proper value. If
2055 * we get back 1, we've fully traversed the EDT and copied out any
2059 cdm->status = CAM_DEV_MATCH_LAST;
2065 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2067 struct cam_eb *bus, *next_bus;
2073 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2080 * XXX The locking here is obviously very complex. We
2081 * should work to simplify it.
2084 CAM_SIM_LOCK(bus->sim);
2085 retval = tr_func(bus, arg);
2086 CAM_SIM_UNLOCK(bus->sim);
2089 next_bus = TAILQ_NEXT(bus, links);
2092 xpt_release_bus(bus);
2104 xpt_sim_opened(struct cam_sim *sim)
2107 struct cam_et *target;
2108 struct cam_ed *device;
2109 struct cam_periph *periph;
2111 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2112 mtx_assert(sim->mtx, MA_OWNED);
2115 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2116 if (bus->sim != sim)
2119 TAILQ_FOREACH(target, &bus->et_entries, links) {
2120 TAILQ_FOREACH(device, &target->ed_entries, links) {
2121 SLIST_FOREACH(periph, &device->periphs,
2123 if (periph->refcount > 0) {
2137 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2138 xpt_targetfunc_t *tr_func, void *arg)
2140 struct cam_et *target, *next_target;
2143 mtx_assert(bus->sim->mtx, MA_OWNED);
2145 for (target = (start_target ? start_target :
2146 TAILQ_FIRST(&bus->et_entries));
2147 target != NULL; target = next_target) {
2151 retval = tr_func(target, arg);
2153 next_target = TAILQ_NEXT(target, links);
2155 xpt_release_target(target);
2165 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2166 xpt_devicefunc_t *tr_func, void *arg)
2168 struct cam_ed *device, *next_device;
2171 mtx_assert(target->bus->sim->mtx, MA_OWNED);
2173 for (device = (start_device ? start_device :
2174 TAILQ_FIRST(&target->ed_entries));
2176 device = next_device) {
2179 * Hold a reference so the current device does not go away
2184 retval = tr_func(device, arg);
2187 * Grab our next pointer before we release the current
2190 next_device = TAILQ_NEXT(device, links);
2192 xpt_release_device(device);
2202 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2203 xpt_periphfunc_t *tr_func, void *arg)
2205 struct cam_periph *periph, *next_periph;
2210 mtx_assert(device->sim->mtx, MA_OWNED);
2212 for (periph = (start_periph ? start_periph :
2213 SLIST_FIRST(&device->periphs));
2215 periph = next_periph) {
2219 * In this case, we want to show peripherals that have been
2220 * invalidated, but not peripherals that are scheduled to
2221 * be freed. So instead of calling cam_periph_acquire(),
2222 * which will fail if the periph has been invalidated, we
2223 * just check for the free flag here. If it is in the
2224 * process of being freed, we skip to the next periph.
2226 if (periph->flags & CAM_PERIPH_FREE) {
2227 next_periph = SLIST_NEXT(periph, periph_links);
2232 * Acquire a reference to this periph while we call the
2233 * traversal function, so it can't go away.
2237 retval = tr_func(periph, arg);
2240 * Grab the next peripheral before we release this one, so
2241 * our next pointer is still valid.
2243 next_periph = SLIST_NEXT(periph, periph_links);
2245 cam_periph_release_locked_buses(periph);
2259 xptpdrvtraverse(struct periph_driver **start_pdrv,
2260 xpt_pdrvfunc_t *tr_func, void *arg)
2262 struct periph_driver **pdrv;
2268 * We don't traverse the peripheral driver list like we do the
2269 * other lists, because it is a linker set, and therefore cannot be
2270 * changed during runtime. If the peripheral driver list is ever
2271 * re-done to be something other than a linker set (i.e. it can
2272 * change while the system is running), the list traversal should
2273 * be modified to work like the other traversal functions.
2275 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2276 *pdrv != NULL; pdrv++) {
2277 retval = tr_func(pdrv, arg);
2287 xptpdperiphtraverse(struct periph_driver **pdrv,
2288 struct cam_periph *start_periph,
2289 xpt_periphfunc_t *tr_func, void *arg)
2291 struct cam_periph *periph, *next_periph;
2292 struct cam_sim *sim;
2298 for (periph = (start_periph ? start_periph :
2299 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2300 periph = next_periph) {
2304 * In this case, we want to show peripherals that have been
2305 * invalidated, but not peripherals that are scheduled to
2306 * be freed. So instead of calling cam_periph_acquire(),
2307 * which will fail if the periph has been invalidated, we
2308 * just check for the free flag here. If it is free, we
2309 * skip to the next periph.
2311 if (periph->flags & CAM_PERIPH_FREE) {
2312 next_periph = TAILQ_NEXT(periph, unit_links);
2317 * Acquire a reference to this periph while we call the
2318 * traversal function, so it can't go away.
2325 retval = tr_func(periph, arg);
2328 * Grab the next peripheral before we release this one, so
2329 * our next pointer is still valid.
2331 next_periph = TAILQ_NEXT(periph, unit_links);
2333 cam_periph_release_locked_buses(periph);
2334 CAM_SIM_UNLOCK(sim);
2347 xptdefbusfunc(struct cam_eb *bus, void *arg)
2349 struct xpt_traverse_config *tr_config;
2351 tr_config = (struct xpt_traverse_config *)arg;
2353 if (tr_config->depth == XPT_DEPTH_BUS) {
2354 xpt_busfunc_t *tr_func;
2356 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2358 return(tr_func(bus, tr_config->tr_arg));
2360 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2364 xptdeftargetfunc(struct cam_et *target, void *arg)
2366 struct xpt_traverse_config *tr_config;
2368 tr_config = (struct xpt_traverse_config *)arg;
2370 if (tr_config->depth == XPT_DEPTH_TARGET) {
2371 xpt_targetfunc_t *tr_func;
2373 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2375 return(tr_func(target, tr_config->tr_arg));
2377 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2381 xptdefdevicefunc(struct cam_ed *device, void *arg)
2383 struct xpt_traverse_config *tr_config;
2385 tr_config = (struct xpt_traverse_config *)arg;
2387 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2388 xpt_devicefunc_t *tr_func;
2390 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2392 return(tr_func(device, tr_config->tr_arg));
2394 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2398 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2400 struct xpt_traverse_config *tr_config;
2401 xpt_periphfunc_t *tr_func;
2403 tr_config = (struct xpt_traverse_config *)arg;
2405 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2408 * Unlike the other default functions, we don't check for depth
2409 * here. The peripheral driver level is the last level in the EDT,
2410 * so if we're here, we should execute the function in question.
2412 return(tr_func(periph, tr_config->tr_arg));
2416 * Execute the given function for every bus in the EDT.
2419 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2421 struct xpt_traverse_config tr_config;
2423 tr_config.depth = XPT_DEPTH_BUS;
2424 tr_config.tr_func = tr_func;
2425 tr_config.tr_arg = arg;
2427 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2431 * Execute the given function for every device in the EDT.
2434 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2436 struct xpt_traverse_config tr_config;
2438 tr_config.depth = XPT_DEPTH_DEVICE;
2439 tr_config.tr_func = tr_func;
2440 tr_config.tr_arg = arg;
2442 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2446 xptsetasyncfunc(struct cam_ed *device, void *arg)
2448 struct cam_path path;
2449 struct ccb_getdev cgd;
2450 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2453 * Don't report unconfigured devices (Wildcard devs,
2454 * devices only for target mode, device instances
2455 * that have been invalidated but are waiting for
2456 * their last reference count to be released).
2458 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2461 xpt_compile_path(&path,
2463 device->target->bus->path_id,
2464 device->target->target_id,
2466 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2467 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2468 xpt_action((union ccb *)&cgd);
2469 csa->callback(csa->callback_arg,
2472 xpt_release_path(&path);
2478 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2480 struct cam_path path;
2481 struct ccb_pathinq cpi;
2482 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2484 xpt_compile_path(&path, /*periph*/NULL,
2486 CAM_TARGET_WILDCARD,
2488 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2489 cpi.ccb_h.func_code = XPT_PATH_INQ;
2490 xpt_action((union ccb *)&cpi);
2491 csa->callback(csa->callback_arg,
2494 xpt_release_path(&path);
2500 xpt_action(union ccb *start_ccb)
2503 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2505 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2506 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2510 xpt_action_default(union ccb *start_ccb)
2512 struct cam_path *path;
2514 path = start_ccb->ccb_h.path;
2515 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2517 switch (start_ccb->ccb_h.func_code) {
2520 struct cam_ed *device;
2523 * For the sake of compatibility with SCSI-1
2524 * devices that may not understand the identify
2525 * message, we include lun information in the
2526 * second byte of all commands. SCSI-1 specifies
2527 * that luns are a 3 bit value and reserves only 3
2528 * bits for lun information in the CDB. Later
2529 * revisions of the SCSI spec allow for more than 8
2530 * luns, but have deprecated lun information in the
2531 * CDB. So, if the lun won't fit, we must omit.
2533 * Also be aware that during initial probing for devices,
2534 * the inquiry information is unknown but initialized to 0.
2535 * This means that this code will be exercised while probing
2536 * devices with an ANSI revision greater than 2.
2538 device = path->device;
2539 if (device->protocol_version <= SCSI_REV_2
2540 && start_ccb->ccb_h.target_lun < 8
2541 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2543 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2544 start_ccb->ccb_h.target_lun << 5;
2546 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2550 case XPT_CONT_TARGET_IO:
2551 start_ccb->csio.sense_resid = 0;
2552 start_ccb->csio.resid = 0;
2555 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2556 start_ccb->ataio.resid = 0;
2561 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2562 if (xpt_schedule_devq(path->bus->sim->devq, path->device))
2563 xpt_run_devq(path->bus->sim->devq);
2565 case XPT_CALC_GEOMETRY:
2567 struct cam_sim *sim;
2569 /* Filter out garbage */
2570 if (start_ccb->ccg.block_size == 0
2571 || start_ccb->ccg.volume_size == 0) {
2572 start_ccb->ccg.cylinders = 0;
2573 start_ccb->ccg.heads = 0;
2574 start_ccb->ccg.secs_per_track = 0;
2575 start_ccb->ccb_h.status = CAM_REQ_CMP;
2578 #if defined(PC98) || defined(__sparc64__)
2580 * In a PC-98 system, geometry translation depens on
2581 * the "real" device geometry obtained from mode page 4.
2582 * SCSI geometry translation is performed in the
2583 * initialization routine of the SCSI BIOS and the result
2584 * stored in host memory. If the translation is available
2585 * in host memory, use it. If not, rely on the default
2586 * translation the device driver performs.
2587 * For sparc64, we may need adjust the geometry of large
2588 * disks in order to fit the limitations of the 16-bit
2589 * fields of the VTOC8 disk label.
2591 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2592 start_ccb->ccb_h.status = CAM_REQ_CMP;
2596 sim = path->bus->sim;
2597 (*(sim->sim_action))(sim, start_ccb);
2602 union ccb* abort_ccb;
2604 abort_ccb = start_ccb->cab.abort_ccb;
2605 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2607 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2608 struct cam_ccbq *ccbq;
2609 struct cam_ed *device;
2611 device = abort_ccb->ccb_h.path->device;
2612 ccbq = &device->ccbq;
2613 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2614 abort_ccb->ccb_h.status =
2615 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2616 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2617 xpt_done(abort_ccb);
2618 start_ccb->ccb_h.status = CAM_REQ_CMP;
2621 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2622 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2624 * We've caught this ccb en route to
2625 * the SIM. Flag it for abort and the
2626 * SIM will do so just before starting
2627 * real work on the CCB.
2629 abort_ccb->ccb_h.status =
2630 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2631 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2632 start_ccb->ccb_h.status = CAM_REQ_CMP;
2636 if (XPT_FC_IS_QUEUED(abort_ccb)
2637 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2639 * It's already completed but waiting
2640 * for our SWI to get to it.
2642 start_ccb->ccb_h.status = CAM_UA_ABORT;
2646 * If we weren't able to take care of the abort request
2647 * in the XPT, pass the request down to the SIM for processing.
2651 case XPT_ACCEPT_TARGET_IO:
2653 case XPT_IMMED_NOTIFY:
2654 case XPT_NOTIFY_ACK:
2656 case XPT_IMMEDIATE_NOTIFY:
2657 case XPT_NOTIFY_ACKNOWLEDGE:
2658 case XPT_GET_SIM_KNOB:
2659 case XPT_SET_SIM_KNOB:
2661 struct cam_sim *sim;
2663 sim = path->bus->sim;
2664 (*(sim->sim_action))(sim, start_ccb);
2669 struct cam_sim *sim;
2671 sim = path->bus->sim;
2672 (*(sim->sim_action))(sim, start_ccb);
2675 case XPT_PATH_STATS:
2676 start_ccb->cpis.last_reset = path->bus->last_reset;
2677 start_ccb->ccb_h.status = CAM_REQ_CMP;
2684 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2685 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2687 struct ccb_getdev *cgd;
2689 cgd = &start_ccb->cgd;
2690 cgd->protocol = dev->protocol;
2691 cgd->inq_data = dev->inq_data;
2692 cgd->ident_data = dev->ident_data;
2693 cgd->inq_flags = dev->inq_flags;
2694 cgd->ccb_h.status = CAM_REQ_CMP;
2695 cgd->serial_num_len = dev->serial_num_len;
2696 if ((dev->serial_num_len > 0)
2697 && (dev->serial_num != NULL))
2698 bcopy(dev->serial_num, cgd->serial_num,
2699 dev->serial_num_len);
2703 case XPT_GDEV_STATS:
2708 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2709 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2711 struct ccb_getdevstats *cgds;
2715 cgds = &start_ccb->cgds;
2718 cgds->dev_openings = dev->ccbq.dev_openings;
2719 cgds->dev_active = dev->ccbq.dev_active;
2720 cgds->devq_openings = dev->ccbq.devq_openings;
2721 cgds->devq_queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2722 cgds->held = dev->ccbq.held;
2723 cgds->last_reset = tar->last_reset;
2724 cgds->maxtags = dev->maxtags;
2725 cgds->mintags = dev->mintags;
2726 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2727 cgds->last_reset = bus->last_reset;
2728 cgds->ccb_h.status = CAM_REQ_CMP;
2734 struct cam_periph *nperiph;
2735 struct periph_list *periph_head;
2736 struct ccb_getdevlist *cgdl;
2738 struct cam_ed *device;
2745 * Don't want anyone mucking with our data.
2747 device = path->device;
2748 periph_head = &device->periphs;
2749 cgdl = &start_ccb->cgdl;
2752 * Check and see if the list has changed since the user
2753 * last requested a list member. If so, tell them that the
2754 * list has changed, and therefore they need to start over
2755 * from the beginning.
2757 if ((cgdl->index != 0) &&
2758 (cgdl->generation != device->generation)) {
2759 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2764 * Traverse the list of peripherals and attempt to find
2765 * the requested peripheral.
2767 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2768 (nperiph != NULL) && (i <= cgdl->index);
2769 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2770 if (i == cgdl->index) {
2771 strncpy(cgdl->periph_name,
2772 nperiph->periph_name,
2774 cgdl->unit_number = nperiph->unit_number;
2779 cgdl->status = CAM_GDEVLIST_ERROR;
2783 if (nperiph == NULL)
2784 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2786 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2789 cgdl->generation = device->generation;
2791 cgdl->ccb_h.status = CAM_REQ_CMP;
2796 dev_pos_type position_type;
2797 struct ccb_dev_match *cdm;
2799 cdm = &start_ccb->cdm;
2802 * There are two ways of getting at information in the EDT.
2803 * The first way is via the primary EDT tree. It starts
2804 * with a list of busses, then a list of targets on a bus,
2805 * then devices/luns on a target, and then peripherals on a
2806 * device/lun. The "other" way is by the peripheral driver
2807 * lists. The peripheral driver lists are organized by
2808 * peripheral driver. (obviously) So it makes sense to
2809 * use the peripheral driver list if the user is looking
2810 * for something like "da1", or all "da" devices. If the
2811 * user is looking for something on a particular bus/target
2812 * or lun, it's generally better to go through the EDT tree.
2815 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2816 position_type = cdm->pos.position_type;
2820 position_type = CAM_DEV_POS_NONE;
2822 for (i = 0; i < cdm->num_patterns; i++) {
2823 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2824 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2825 position_type = CAM_DEV_POS_EDT;
2830 if (cdm->num_patterns == 0)
2831 position_type = CAM_DEV_POS_EDT;
2832 else if (position_type == CAM_DEV_POS_NONE)
2833 position_type = CAM_DEV_POS_PDRV;
2837 * Note that we drop the SIM lock here, because the EDT
2838 * traversal code needs to do its own locking.
2840 CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
2841 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2842 case CAM_DEV_POS_EDT:
2845 case CAM_DEV_POS_PDRV:
2846 xptperiphlistmatch(cdm);
2849 cdm->status = CAM_DEV_MATCH_ERROR;
2852 CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
2854 if (cdm->status == CAM_DEV_MATCH_ERROR)
2855 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2857 start_ccb->ccb_h.status = CAM_REQ_CMP;
2863 struct ccb_setasync *csa;
2864 struct async_node *cur_entry;
2865 struct async_list *async_head;
2868 csa = &start_ccb->csa;
2869 added = csa->event_enable;
2870 async_head = &path->device->asyncs;
2873 * If there is already an entry for us, simply
2876 cur_entry = SLIST_FIRST(async_head);
2877 while (cur_entry != NULL) {
2878 if ((cur_entry->callback_arg == csa->callback_arg)
2879 && (cur_entry->callback == csa->callback))
2881 cur_entry = SLIST_NEXT(cur_entry, links);
2884 if (cur_entry != NULL) {
2886 * If the request has no flags set,
2889 added &= ~cur_entry->event_enable;
2890 if (csa->event_enable == 0) {
2891 SLIST_REMOVE(async_head, cur_entry,
2893 xpt_release_device(path->device);
2894 free(cur_entry, M_CAMXPT);
2896 cur_entry->event_enable = csa->event_enable;
2898 csa->event_enable = added;
2900 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2902 if (cur_entry == NULL) {
2903 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2906 cur_entry->event_enable = csa->event_enable;
2907 cur_entry->callback_arg = csa->callback_arg;
2908 cur_entry->callback = csa->callback;
2909 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2910 xpt_acquire_device(path->device);
2912 start_ccb->ccb_h.status = CAM_REQ_CMP;
2917 struct ccb_relsim *crs;
2920 crs = &start_ccb->crs;
2924 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2928 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2930 /* Don't ever go below one opening */
2931 if (crs->openings > 0) {
2932 xpt_dev_ccbq_resize(path, crs->openings);
2935 "number of openings is now %d\n",
2941 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2943 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2946 * Just extend the old timeout and decrement
2947 * the freeze count so that a single timeout
2948 * is sufficient for releasing the queue.
2950 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2951 callout_stop(&dev->callout);
2954 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2957 callout_reset(&dev->callout,
2958 (crs->release_timeout * hz) / 1000,
2959 xpt_release_devq_timeout, dev);
2961 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2965 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2967 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2969 * Decrement the freeze count so that a single
2970 * completion is still sufficient to unfreeze
2973 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2976 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2977 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2981 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2983 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2984 || (dev->ccbq.dev_active == 0)) {
2986 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2989 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2990 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2994 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2995 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2996 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2997 start_ccb->ccb_h.status = CAM_REQ_CMP;
3001 struct cam_path *oldpath;
3002 struct cam_sim *oldsim;
3004 /* Check that all request bits are supported. */
3005 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3006 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3010 cam_dflags = CAM_DEBUG_NONE;
3011 if (cam_dpath != NULL) {
3012 /* To release the old path we must hold proper lock. */
3013 oldpath = cam_dpath;
3015 oldsim = xpt_path_sim(oldpath);
3016 CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
3017 CAM_SIM_LOCK(oldsim);
3018 xpt_free_path(oldpath);
3019 CAM_SIM_UNLOCK(oldsim);
3020 CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
3022 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3023 if (xpt_create_path(&cam_dpath, NULL,
3024 start_ccb->ccb_h.path_id,
3025 start_ccb->ccb_h.target_id,
3026 start_ccb->ccb_h.target_lun) !=
3028 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3030 cam_dflags = start_ccb->cdbg.flags;
3031 start_ccb->ccb_h.status = CAM_REQ_CMP;
3032 xpt_print(cam_dpath, "debugging flags now %x\n",
3036 start_ccb->ccb_h.status = CAM_REQ_CMP;
3040 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3041 xpt_freeze_devq(path, 1);
3042 start_ccb->ccb_h.status = CAM_REQ_CMP;
3049 printf("%s: CCB type %#x not supported\n", __func__,
3050 start_ccb->ccb_h.func_code);
3051 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3052 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3053 xpt_done(start_ccb);
3060 xpt_polled_action(union ccb *start_ccb)
3063 struct cam_sim *sim;
3064 struct cam_devq *devq;
3068 timeout = start_ccb->ccb_h.timeout * 10;
3069 sim = start_ccb->ccb_h.path->bus->sim;
3071 dev = start_ccb->ccb_h.path->device;
3073 mtx_assert(sim->mtx, MA_OWNED);
3075 /* Don't use ISR for this SIM while polling. */
3076 sim->flags |= CAM_SIM_POLLED;
3079 * Steal an opening so that no other queued requests
3080 * can get it before us while we simulate interrupts.
3082 dev->ccbq.devq_openings--;
3083 dev->ccbq.dev_openings--;
3085 while(((devq != NULL && devq->send_openings <= 0) ||
3086 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3088 (*(sim->sim_poll))(sim);
3089 camisr_runqueue(sim);
3092 dev->ccbq.devq_openings++;
3093 dev->ccbq.dev_openings++;
3096 xpt_action(start_ccb);
3097 while(--timeout > 0) {
3098 (*(sim->sim_poll))(sim);
3099 camisr_runqueue(sim);
3100 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3107 * XXX Is it worth adding a sim_timeout entry
3108 * point so we can attempt recovery? If
3109 * this is only used for dumps, I don't think
3112 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3115 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3118 /* We will use CAM ISR for this SIM again. */
3119 sim->flags &= ~CAM_SIM_POLLED;
3123 * Schedule a peripheral driver to receive a ccb when it's
3124 * target device has space for more transactions.
3127 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3129 struct cam_ed *device;
3132 mtx_assert(perph->sim->mtx, MA_OWNED);
3134 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3135 device = perph->path->device;
3136 if (periph_is_queued(perph)) {
3137 /* Simply reorder based on new priority */
3138 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3139 (" change priority to %d\n", new_priority));
3140 if (new_priority < perph->pinfo.priority) {
3141 camq_change_priority(&device->drvq,
3147 /* New entry on the queue */
3148 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3149 (" added periph to queue\n"));
3150 perph->pinfo.priority = new_priority;
3151 perph->pinfo.generation = ++device->drvq.generation;
3152 camq_insert(&device->drvq, &perph->pinfo);
3156 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3157 (" calling xpt_run_dev_allocq\n"));
3158 xpt_run_dev_allocq(device);
3164 * Schedule a device to run on a given queue.
3165 * If the device was inserted as a new entry on the queue,
3166 * return 1 meaning the device queue should be run. If we
3167 * were already queued, implying someone else has already
3168 * started the queue, return 0 so the caller doesn't attempt
3172 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3173 u_int32_t new_priority)
3176 u_int32_t old_priority;
3178 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3180 old_priority = pinfo->priority;
3183 * Are we already queued?
3185 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3186 /* Simply reorder based on new priority */
3187 if (new_priority < old_priority) {
3188 camq_change_priority(queue, pinfo->index,
3190 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3191 ("changed priority to %d\n",
3197 /* New entry on the queue */
3198 if (new_priority < old_priority)
3199 pinfo->priority = new_priority;
3201 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3202 ("Inserting onto queue\n"));
3203 pinfo->generation = ++queue->generation;
3204 camq_insert(queue, pinfo);
3211 xpt_run_dev_allocq(struct cam_ed *device)
3215 if (device->ccbq.devq_allocating)
3217 device->ccbq.devq_allocating = 1;
3218 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq(%p)\n", device));
3219 drvq = &device->drvq;
3220 while ((drvq->entries > 0) &&
3221 (device->ccbq.devq_openings > 0 ||
3222 CAMQ_GET_PRIO(drvq) <= CAM_PRIORITY_OOB) &&
3223 (device->ccbq.queue.qfrozen_cnt == 0)) {
3224 union ccb *work_ccb;
3225 struct cam_periph *drv;
3227 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3228 "Device on queue without any work to do"));
3229 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3230 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3231 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3232 drv->pinfo.priority);
3233 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3234 ("calling periph start\n"));
3235 drv->periph_start(drv, work_ccb);
3238 * Malloc failure in alloc_ccb
3241 * XXX add us to a list to be run from free_ccb
3242 * if we don't have any ccbs active on this
3243 * device queue otherwise we may never get run
3249 device->ccbq.devq_allocating = 0;
3253 xpt_run_devq(struct cam_devq *devq)
3255 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3257 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3259 devq->send_queue.qfrozen_cnt++;
3260 while ((devq->send_queue.entries > 0)
3261 && (devq->send_openings > 0)
3262 && (devq->send_queue.qfrozen_cnt <= 1)) {
3263 struct cam_ed_qinfo *qinfo;
3264 struct cam_ed *device;
3265 union ccb *work_ccb;
3266 struct cam_sim *sim;
3268 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3270 device = qinfo->device;
3271 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3272 ("running device %p\n", device));
3274 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3275 if (work_ccb == NULL) {
3276 printf("device on run queue with no ccbs???\n");
3280 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3282 mtx_lock(&xsoftc.xpt_lock);
3283 if (xsoftc.num_highpower <= 0) {
3285 * We got a high power command, but we
3286 * don't have any available slots. Freeze
3287 * the device queue until we have a slot
3290 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3291 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3292 work_ccb->ccb_h.path->device,
3295 mtx_unlock(&xsoftc.xpt_lock);
3299 * Consume a high power slot while
3302 xsoftc.num_highpower--;
3304 mtx_unlock(&xsoftc.xpt_lock);
3306 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3307 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3309 devq->send_openings--;
3310 devq->send_active++;
3312 xpt_schedule_devq(devq, device);
3314 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3316 * The client wants to freeze the queue
3317 * after this CCB is sent.
3319 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3322 /* In Target mode, the peripheral driver knows best... */
3323 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3324 if ((device->inq_flags & SID_CmdQue) != 0
3325 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3326 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3329 * Clear this in case of a retried CCB that
3330 * failed due to a rejected tag.
3332 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3335 switch (work_ccb->ccb_h.func_code) {
3337 CAM_DEBUG(work_ccb->ccb_h.path,
3338 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3339 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3341 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3342 cdb_str, sizeof(cdb_str))));
3345 CAM_DEBUG(work_ccb->ccb_h.path,
3346 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3347 ata_op_string(&work_ccb->ataio.cmd),
3348 ata_cmd_string(&work_ccb->ataio.cmd,
3349 cdb_str, sizeof(cdb_str))));
3356 * Device queues can be shared among multiple sim instances
3357 * that reside on different busses. Use the SIM in the queue
3358 * CCB's path, rather than the one in the bus that was passed
3359 * into this function.
3361 sim = work_ccb->ccb_h.path->bus->sim;
3362 (*(sim->sim_action))(sim, work_ccb);
3364 devq->send_queue.qfrozen_cnt--;
3368 * This function merges stuff from the slave ccb into the master ccb, while
3369 * keeping important fields in the master ccb constant.
3372 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3376 * Pull fields that are valid for peripheral drivers to set
3377 * into the master CCB along with the CCB "payload".
3379 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3380 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3381 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3382 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3383 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3384 sizeof(union ccb) - sizeof(struct ccb_hdr));
3388 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3391 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3392 ccb_h->pinfo.priority = priority;
3394 ccb_h->path_id = path->bus->path_id;
3396 ccb_h->target_id = path->target->target_id;
3398 ccb_h->target_id = CAM_TARGET_WILDCARD;
3400 ccb_h->target_lun = path->device->lun_id;
3401 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3403 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3405 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3409 /* Path manipulation functions */
3411 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3412 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3414 struct cam_path *path;
3417 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3420 status = CAM_RESRC_UNAVAIL;
3423 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3424 if (status != CAM_REQ_CMP) {
3425 free(path, M_CAMPATH);
3428 *new_path_ptr = path;
3433 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3434 struct cam_periph *periph, path_id_t path_id,
3435 target_id_t target_id, lun_id_t lun_id)
3437 struct cam_path *path;
3438 struct cam_eb *bus = NULL;
3441 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
3443 bus = xpt_find_bus(path_id);
3445 CAM_SIM_LOCK(bus->sim);
3446 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3448 CAM_SIM_UNLOCK(bus->sim);
3449 xpt_release_bus(bus);
3451 if (status != CAM_REQ_CMP) {
3452 free(path, M_CAMPATH);
3455 *new_path_ptr = path;
3460 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3461 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3464 struct cam_et *target;
3465 struct cam_ed *device;
3468 status = CAM_REQ_CMP; /* Completed without error */
3469 target = NULL; /* Wildcarded */
3470 device = NULL; /* Wildcarded */
3473 * We will potentially modify the EDT, so block interrupts
3474 * that may attempt to create cam paths.
3476 bus = xpt_find_bus(path_id);
3478 status = CAM_PATH_INVALID;
3480 target = xpt_find_target(bus, target_id);
3481 if (target == NULL) {
3483 struct cam_et *new_target;
3485 new_target = xpt_alloc_target(bus, target_id);
3486 if (new_target == NULL) {
3487 status = CAM_RESRC_UNAVAIL;
3489 target = new_target;
3492 if (target != NULL) {
3493 device = xpt_find_device(target, lun_id);
3494 if (device == NULL) {
3496 struct cam_ed *new_device;
3499 (*(bus->xport->alloc_device))(bus,
3502 if (new_device == NULL) {
3503 status = CAM_RESRC_UNAVAIL;
3505 device = new_device;
3512 * Only touch the user's data if we are successful.
3514 if (status == CAM_REQ_CMP) {
3515 new_path->periph = perph;
3516 new_path->bus = bus;
3517 new_path->target = target;
3518 new_path->device = device;
3519 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3522 xpt_release_device(device);
3524 xpt_release_target(target);
3526 xpt_release_bus(bus);
3532 xpt_release_path(struct cam_path *path)
3534 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3535 if (path->device != NULL) {
3536 xpt_release_device(path->device);
3537 path->device = NULL;
3539 if (path->target != NULL) {
3540 xpt_release_target(path->target);
3541 path->target = NULL;
3543 if (path->bus != NULL) {
3544 xpt_release_bus(path->bus);
3550 xpt_free_path(struct cam_path *path)
3553 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3554 xpt_release_path(path);
3555 free(path, M_CAMPATH);
3559 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3560 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3566 *bus_ref = path->bus->refcount;
3572 *periph_ref = path->periph->refcount;
3579 *target_ref = path->target->refcount;
3585 *device_ref = path->device->refcount;
3592 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3593 * in path1, 2 for match with wildcards in path2.
3596 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3600 if (path1->bus != path2->bus) {
3601 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3603 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3608 if (path1->target != path2->target) {
3609 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3612 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3617 if (path1->device != path2->device) {
3618 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3621 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3630 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3634 if (path->bus != dev->target->bus) {
3635 if (path->bus->path_id == CAM_BUS_WILDCARD)
3637 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3642 if (path->target != dev->target) {
3643 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3646 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3651 if (path->device != dev) {
3652 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3655 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3664 xpt_print_path(struct cam_path *path)
3668 printf("(nopath): ");
3670 if (path->periph != NULL)
3671 printf("(%s%d:", path->periph->periph_name,
3672 path->periph->unit_number);
3674 printf("(noperiph:");
3676 if (path->bus != NULL)
3677 printf("%s%d:%d:", path->bus->sim->sim_name,
3678 path->bus->sim->unit_number,
3679 path->bus->sim->bus_id);
3683 if (path->target != NULL)
3684 printf("%d:", path->target->target_id);
3688 if (path->device != NULL)
3689 printf("%d): ", path->device->lun_id);
3696 xpt_print_device(struct cam_ed *device)
3700 printf("(nopath): ");
3702 printf("(noperiph:%s%d:%d:%d:%d): ", device->sim->sim_name,
3703 device->sim->unit_number,
3704 device->sim->bus_id,
3705 device->target->target_id,
3711 xpt_print(struct cam_path *path, const char *fmt, ...)
3714 xpt_print_path(path);
3721 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3726 if (path != NULL && path->bus != NULL)
3727 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3730 sbuf_new(&sb, str, str_len, 0);
3733 sbuf_printf(&sb, "(nopath): ");
3735 if (path->periph != NULL)
3736 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3737 path->periph->unit_number);
3739 sbuf_printf(&sb, "(noperiph:");
3741 if (path->bus != NULL)
3742 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3743 path->bus->sim->unit_number,
3744 path->bus->sim->bus_id);
3746 sbuf_printf(&sb, "nobus:");
3748 if (path->target != NULL)
3749 sbuf_printf(&sb, "%d:", path->target->target_id);
3751 sbuf_printf(&sb, "X:");
3753 if (path->device != NULL)
3754 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3756 sbuf_printf(&sb, "X): ");
3760 return(sbuf_len(&sb));
3764 xpt_path_path_id(struct cam_path *path)
3766 return(path->bus->path_id);
3770 xpt_path_target_id(struct cam_path *path)
3772 if (path->target != NULL)
3773 return (path->target->target_id);
3775 return (CAM_TARGET_WILDCARD);
3779 xpt_path_lun_id(struct cam_path *path)
3781 if (path->device != NULL)
3782 return (path->device->lun_id);
3784 return (CAM_LUN_WILDCARD);
3788 xpt_path_sim(struct cam_path *path)
3791 return (path->bus->sim);
3795 xpt_path_periph(struct cam_path *path)
3797 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3799 return (path->periph);
3803 xpt_path_legacy_ata_id(struct cam_path *path)
3808 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3809 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3810 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3811 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3814 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3815 path->bus->sim->unit_number < 2) {
3816 bus_id = path->bus->sim->unit_number;
3820 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3821 if (bus == path->bus)
3823 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3824 bus->sim->unit_number >= 2) ||
3825 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3826 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3827 strcmp(bus->sim->sim_name, "siisch") == 0)
3832 if (path->target != NULL) {
3833 if (path->target->target_id < 2)
3834 return (bus_id * 2 + path->target->target_id);
3838 return (bus_id * 2);
3842 * Release a CAM control block for the caller. Remit the cost of the structure
3843 * to the device referenced by the path. If the this device had no 'credits'
3844 * and peripheral drivers have registered async callbacks for this notification
3848 xpt_release_ccb(union ccb *free_ccb)
3850 struct cam_path *path;
3851 struct cam_ed *device;
3853 struct cam_sim *sim;
3855 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3856 path = free_ccb->ccb_h.path;
3857 device = path->device;
3861 mtx_assert(sim->mtx, MA_OWNED);
3863 cam_ccbq_release_opening(&device->ccbq);
3864 if (sim->ccb_count > sim->max_ccbs) {
3865 xpt_free_ccb(free_ccb);
3868 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3871 xpt_run_dev_allocq(device);
3874 /* Functions accessed by SIM drivers */
3876 static struct xpt_xport xport_default = {
3877 .alloc_device = xpt_alloc_device_default,
3878 .action = xpt_action_default,
3879 .async = xpt_dev_async_default,
3883 * A sim structure, listing the SIM entry points and instance
3884 * identification info is passed to xpt_bus_register to hook the SIM
3885 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3886 * for this new bus and places it in the array of busses and assigns
3887 * it a path_id. The path_id may be influenced by "hard wiring"
3888 * information specified by the user. Once interrupt services are
3889 * available, the bus will be probed.
3892 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3894 struct cam_eb *new_bus;
3895 struct cam_eb *old_bus;
3896 struct ccb_pathinq cpi;
3897 struct cam_path *path;
3900 mtx_assert(sim->mtx, MA_OWNED);
3903 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3904 M_CAMXPT, M_NOWAIT);
3905 if (new_bus == NULL) {
3906 /* Couldn't satisfy request */
3907 return (CAM_RESRC_UNAVAIL);
3910 TAILQ_INIT(&new_bus->et_entries);
3913 timevalclear(&new_bus->last_reset);
3915 new_bus->refcount = 1; /* Held until a bus_deregister event */
3916 new_bus->generation = 0;
3919 sim->path_id = new_bus->path_id =
3920 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3921 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3922 while (old_bus != NULL
3923 && old_bus->path_id < new_bus->path_id)
3924 old_bus = TAILQ_NEXT(old_bus, links);
3925 if (old_bus != NULL)
3926 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3928 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3929 xsoftc.bus_generation++;
3933 * Set a default transport so that a PATH_INQ can be issued to
3934 * the SIM. This will then allow for probing and attaching of
3935 * a more appropriate transport.
3937 new_bus->xport = &xport_default;
3939 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3940 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3941 if (status != CAM_REQ_CMP) {
3942 xpt_release_bus(new_bus);
3943 free(path, M_CAMXPT);
3944 return (CAM_RESRC_UNAVAIL);
3947 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3948 cpi.ccb_h.func_code = XPT_PATH_INQ;
3949 xpt_action((union ccb *)&cpi);
3951 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3952 switch (cpi.transport) {
3959 new_bus->xport = scsi_get_xport();
3963 new_bus->xport = ata_get_xport();
3966 new_bus->xport = &xport_default;
3971 /* Notify interested parties */
3972 if (sim->path_id != CAM_XPT_PATH_ID) {
3974 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3975 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3976 union ccb *scan_ccb;
3978 /* Initiate bus rescan. */
3979 scan_ccb = xpt_alloc_ccb_nowait();
3980 if (scan_ccb != NULL) {
3981 scan_ccb->ccb_h.path = path;
3982 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3983 scan_ccb->crcn.flags = 0;
3984 xpt_rescan(scan_ccb);
3987 "Can't allocate CCB to scan bus\n");
3989 xpt_free_path(path);
3991 xpt_free_path(path);
3992 return (CAM_SUCCESS);
3996 xpt_bus_deregister(path_id_t pathid)
3998 struct cam_path bus_path;
4001 status = xpt_compile_path(&bus_path, NULL, pathid,
4002 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4003 if (status != CAM_REQ_CMP)
4006 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4007 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4009 /* Release the reference count held while registered. */
4010 xpt_release_bus(bus_path.bus);
4011 xpt_release_path(&bus_path);
4013 return (CAM_REQ_CMP);
4017 xptnextfreepathid(void)
4023 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4025 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4027 /* Find an unoccupied pathid */
4028 while (bus != NULL && bus->path_id <= pathid) {
4029 if (bus->path_id == pathid)
4031 bus = TAILQ_NEXT(bus, links);
4035 * Ensure that this pathid is not reserved for
4036 * a bus that may be registered in the future.
4038 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4040 /* Start the search over */
4047 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4054 pathid = CAM_XPT_PATH_ID;
4055 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4056 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4059 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4060 if (strcmp(dname, "scbus")) {
4061 /* Avoid a bit of foot shooting. */
4064 if (dunit < 0) /* unwired?! */
4066 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4067 if (sim_bus == val) {
4071 } else if (sim_bus == 0) {
4072 /* Unspecified matches bus 0 */
4076 printf("Ambiguous scbus configuration for %s%d "
4077 "bus %d, cannot wire down. The kernel "
4078 "config entry for scbus%d should "
4079 "specify a controller bus.\n"
4080 "Scbus will be assigned dynamically.\n",
4081 sim_name, sim_unit, sim_bus, dunit);
4086 if (pathid == CAM_XPT_PATH_ID)
4087 pathid = xptnextfreepathid();
4092 xpt_async_string(u_int32_t async_code)
4095 switch (async_code) {
4096 case AC_BUS_RESET: return ("AC_BUS_RESET");
4097 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4098 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4099 case AC_SENT_BDR: return ("AC_SENT_BDR");
4100 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4101 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4102 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4103 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4104 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4105 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4106 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4107 case AC_CONTRACT: return ("AC_CONTRACT");
4108 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4109 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4111 return ("AC_UNKNOWN");
4115 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4118 struct cam_et *target, *next_target;
4119 struct cam_ed *device, *next_device;
4121 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4122 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4123 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4126 * Most async events come from a CAM interrupt context. In
4127 * a few cases, the error recovery code at the peripheral layer,
4128 * which may run from our SWI or a process context, may signal
4129 * deferred events with a call to xpt_async.
4134 if (async_code == AC_BUS_RESET) {
4135 /* Update our notion of when the last reset occurred */
4136 microtime(&bus->last_reset);
4139 for (target = TAILQ_FIRST(&bus->et_entries);
4141 target = next_target) {
4143 next_target = TAILQ_NEXT(target, links);
4145 if (path->target != target
4146 && path->target->target_id != CAM_TARGET_WILDCARD
4147 && target->target_id != CAM_TARGET_WILDCARD)
4150 if (async_code == AC_SENT_BDR) {
4151 /* Update our notion of when the last reset occurred */
4152 microtime(&path->target->last_reset);
4155 for (device = TAILQ_FIRST(&target->ed_entries);
4157 device = next_device) {
4159 next_device = TAILQ_NEXT(device, links);
4161 if (path->device != device
4162 && path->device->lun_id != CAM_LUN_WILDCARD
4163 && device->lun_id != CAM_LUN_WILDCARD)
4166 * The async callback could free the device.
4167 * If it is a broadcast async, it doesn't hold
4168 * device reference, so take our own reference.
4170 xpt_acquire_device(device);
4171 (*(bus->xport->async))(async_code, bus,
4175 xpt_async_bcast(&device->asyncs, async_code,
4177 xpt_release_device(device);
4182 * If this wasn't a fully wildcarded async, tell all
4183 * clients that want all async events.
4185 if (bus != xpt_periph->path->bus)
4186 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4191 xpt_async_bcast(struct async_list *async_head,
4192 u_int32_t async_code,
4193 struct cam_path *path, void *async_arg)
4195 struct async_node *cur_entry;
4197 cur_entry = SLIST_FIRST(async_head);
4198 while (cur_entry != NULL) {
4199 struct async_node *next_entry;
4201 * Grab the next list entry before we call the current
4202 * entry's callback. This is because the callback function
4203 * can delete its async callback entry.
4205 next_entry = SLIST_NEXT(cur_entry, links);
4206 if ((cur_entry->event_enable & async_code) != 0)
4207 cur_entry->callback(cur_entry->callback_arg,
4210 cur_entry = next_entry;
4215 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4216 struct cam_et *target, struct cam_ed *device,
4219 printf("%s called\n", __func__);
4223 xpt_freeze_devq(struct cam_path *path, u_int count)
4225 struct cam_ed *dev = path->device;
4227 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4228 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq() %u->%u\n",
4229 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4230 dev->ccbq.queue.qfrozen_cnt += count;
4231 /* Remove frozen device from sendq. */
4232 if (device_is_queued(dev)) {
4233 camq_remove(&dev->sim->devq->send_queue,
4234 dev->devq_entry.pinfo.index);
4236 return (dev->ccbq.queue.qfrozen_cnt);
4240 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4243 mtx_assert(sim->mtx, MA_OWNED);
4244 sim->devq->send_queue.qfrozen_cnt += count;
4245 return (sim->devq->send_queue.qfrozen_cnt);
4249 xpt_release_devq_timeout(void *arg)
4251 struct cam_ed *device;
4253 device = (struct cam_ed *)arg;
4254 CAM_DEBUG_DEV(device, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4255 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4259 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4262 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4263 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4265 xpt_release_devq_device(path->device, count, run_queue);
4269 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4272 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4273 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4274 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4275 if (count > dev->ccbq.queue.qfrozen_cnt) {
4277 printf("xpt_release_devq(): requested %u > present %u\n",
4278 count, dev->ccbq.queue.qfrozen_cnt);
4280 count = dev->ccbq.queue.qfrozen_cnt;
4282 dev->ccbq.queue.qfrozen_cnt -= count;
4283 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4285 * No longer need to wait for a successful
4286 * command completion.
4288 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4290 * Remove any timeouts that might be scheduled
4291 * to release this queue.
4293 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4294 callout_stop(&dev->callout);
4295 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4297 xpt_run_dev_allocq(dev);
4301 * Now that we are unfrozen schedule the
4302 * device so any pending transactions are
4305 if (xpt_schedule_devq(dev->sim->devq, dev))
4306 xpt_run_devq(dev->sim->devq);
4311 xpt_release_simq(struct cam_sim *sim, int run_queue)
4315 mtx_assert(sim->mtx, MA_OWNED);
4316 sendq = &(sim->devq->send_queue);
4317 if (sendq->qfrozen_cnt <= 0) {
4319 printf("xpt_release_simq: requested 1 > present %u\n",
4320 sendq->qfrozen_cnt);
4323 sendq->qfrozen_cnt--;
4324 if (sendq->qfrozen_cnt == 0) {
4326 * If there is a timeout scheduled to release this
4327 * sim queue, remove it. The queue frozen count is
4330 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4331 callout_stop(&sim->callout);
4332 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4336 * Now that we are unfrozen run the send queue.
4338 xpt_run_devq(sim->devq);
4344 * XXX Appears to be unused.
4347 xpt_release_simq_timeout(void *arg)
4349 struct cam_sim *sim;
4351 sim = (struct cam_sim *)arg;
4352 xpt_release_simq(sim, /* run_queue */ TRUE);
4356 xpt_done(union ccb *done_ccb)
4358 struct cam_sim *sim;
4361 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4362 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4364 * Queue up the request for handling by our SWI handler
4365 * any of the "non-immediate" type of ccbs.
4367 sim = done_ccb->ccb_h.path->bus->sim;
4368 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4370 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4371 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4372 CAM_SIM_BATCH)) == 0) {
4373 mtx_lock(&cam_simq_lock);
4374 first = TAILQ_EMPTY(&cam_simq);
4375 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4376 mtx_unlock(&cam_simq_lock);
4377 sim->flags |= CAM_SIM_ON_DONEQ;
4379 swi_sched(cambio_ih, 0);
4385 xpt_batch_start(struct cam_sim *sim)
4388 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4389 sim->flags |= CAM_SIM_BATCH;
4393 xpt_batch_done(struct cam_sim *sim)
4396 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4397 sim->flags &= ~CAM_SIM_BATCH;
4398 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4399 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4400 camisr_runqueue(sim);
4408 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4413 xpt_alloc_ccb_nowait()
4417 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4422 xpt_free_ccb(union ccb *free_ccb)
4424 free(free_ccb, M_CAMCCB);
4429 /* Private XPT functions */
4432 * Get a CAM control block for the caller. Charge the structure to the device
4433 * referenced by the path. If the this device has no 'credits' then the
4434 * device already has the maximum number of outstanding operations under way
4435 * and we return NULL. If we don't have sufficient resources to allocate more
4436 * ccbs, we also return NULL.
4439 xpt_get_ccb(struct cam_ed *device)
4442 struct cam_sim *sim;
4445 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4446 new_ccb = xpt_alloc_ccb_nowait();
4447 if (new_ccb == NULL) {
4450 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4451 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4452 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4456 cam_ccbq_take_opening(&device->ccbq);
4457 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4462 xpt_release_bus(struct cam_eb *bus)
4466 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4467 if (--bus->refcount > 0) {
4471 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4472 ("refcount is zero, but target list is not empty"));
4473 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4474 xsoftc.bus_generation++;
4476 cam_sim_release(bus->sim);
4477 free(bus, M_CAMXPT);
4480 static struct cam_et *
4481 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4483 struct cam_et *cur_target, *target;
4485 mtx_assert(bus->sim->mtx, MA_OWNED);
4486 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4491 TAILQ_INIT(&target->ed_entries);
4493 target->target_id = target_id;
4494 target->refcount = 1;
4495 target->generation = 0;
4496 target->luns = NULL;
4497 timevalclear(&target->last_reset);
4499 * Hold a reference to our parent bus so it
4500 * will not go away before we do.
4506 /* Insertion sort into our bus's target list */
4507 cur_target = TAILQ_FIRST(&bus->et_entries);
4508 while (cur_target != NULL && cur_target->target_id < target_id)
4509 cur_target = TAILQ_NEXT(cur_target, links);
4510 if (cur_target != NULL) {
4511 TAILQ_INSERT_BEFORE(cur_target, target, links);
4513 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4520 xpt_release_target(struct cam_et *target)
4523 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4524 if (--target->refcount > 0)
4526 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4527 ("refcount is zero, but device list is not empty"));
4528 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4529 target->bus->generation++;
4530 xpt_release_bus(target->bus);
4532 free(target->luns, M_CAMXPT);
4533 free(target, M_CAMXPT);
4536 static struct cam_ed *
4537 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4540 struct cam_ed *device;
4542 device = xpt_alloc_device(bus, target, lun_id);
4546 device->mintags = 1;
4547 device->maxtags = 1;
4548 bus->sim->max_ccbs += device->ccbq.devq_openings;
4553 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4555 struct cam_ed *cur_device, *device;
4556 struct cam_devq *devq;
4559 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4560 /* Make space for us in the device queue on our bus */
4561 devq = bus->sim->devq;
4562 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4563 if (status != CAM_REQ_CMP)
4566 device = (struct cam_ed *)malloc(sizeof(*device),
4567 M_CAMDEV, M_NOWAIT|M_ZERO);
4571 cam_init_pinfo(&device->devq_entry.pinfo);
4572 device->devq_entry.device = device;
4573 device->target = target;
4574 device->lun_id = lun_id;
4575 device->sim = bus->sim;
4576 /* Initialize our queues */
4577 if (camq_init(&device->drvq, 0) != 0) {
4578 free(device, M_CAMDEV);
4581 if (cam_ccbq_init(&device->ccbq,
4582 bus->sim->max_dev_openings) != 0) {
4583 camq_fini(&device->drvq);
4584 free(device, M_CAMDEV);
4587 SLIST_INIT(&device->asyncs);
4588 SLIST_INIT(&device->periphs);
4589 device->generation = 0;
4590 device->owner = NULL;
4591 device->flags = CAM_DEV_UNCONFIGURED;
4592 device->tag_delay_count = 0;
4593 device->tag_saved_openings = 0;
4594 device->refcount = 1;
4595 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4597 cur_device = TAILQ_FIRST(&target->ed_entries);
4598 while (cur_device != NULL && cur_device->lun_id < lun_id)
4599 cur_device = TAILQ_NEXT(cur_device, links);
4600 if (cur_device != NULL)
4601 TAILQ_INSERT_BEFORE(cur_device, device, links);
4603 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4605 target->generation++;
4610 xpt_acquire_device(struct cam_ed *device)
4613 mtx_assert(device->sim->mtx, MA_OWNED);
4618 xpt_release_device(struct cam_ed *device)
4620 struct cam_devq *devq;
4622 mtx_assert(device->sim->mtx, MA_OWNED);
4623 if (--device->refcount > 0)
4626 KASSERT(SLIST_EMPTY(&device->periphs),
4627 ("refcount is zero, but periphs list is not empty"));
4628 if (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4629 panic("Removing device while still queued for ccbs");
4631 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4632 callout_stop(&device->callout);
4634 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4635 device->target->generation++;
4636 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4637 /* Release our slot in the devq */
4638 devq = device->target->bus->sim->devq;
4639 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4640 camq_fini(&device->drvq);
4641 cam_ccbq_fini(&device->ccbq);
4643 * Free allocated memory. free(9) does nothing if the
4644 * supplied pointer is NULL, so it is safe to call without
4647 free(device->supported_vpds, M_CAMXPT);
4648 free(device->device_id, M_CAMXPT);
4649 free(device->physpath, M_CAMXPT);
4650 free(device->rcap_buf, M_CAMXPT);
4651 free(device->serial_num, M_CAMXPT);
4653 xpt_release_target(device->target);
4654 free(device, M_CAMDEV);
4658 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4666 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4667 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4668 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4669 || (dev->inq_flags & SID_CmdQue) != 0)
4670 dev->tag_saved_openings = newopenings;
4671 /* Adjust the global limit */
4672 dev->sim->max_ccbs += diff;
4676 static struct cam_eb *
4677 xpt_find_bus(path_id_t path_id)
4682 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4684 bus = TAILQ_NEXT(bus, links)) {
4685 if (bus->path_id == path_id) {
4694 static struct cam_et *
4695 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4697 struct cam_et *target;
4699 mtx_assert(bus->sim->mtx, MA_OWNED);
4700 for (target = TAILQ_FIRST(&bus->et_entries);
4702 target = TAILQ_NEXT(target, links)) {
4703 if (target->target_id == target_id) {
4711 static struct cam_ed *
4712 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4714 struct cam_ed *device;
4716 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4717 for (device = TAILQ_FIRST(&target->ed_entries);
4719 device = TAILQ_NEXT(device, links)) {
4720 if (device->lun_id == lun_id) {
4729 xpt_start_tags(struct cam_path *path)
4731 struct ccb_relsim crs;
4732 struct cam_ed *device;
4733 struct cam_sim *sim;
4736 device = path->device;
4737 sim = path->bus->sim;
4738 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4739 xpt_freeze_devq(path, /*count*/1);
4740 device->inq_flags |= SID_CmdQue;
4741 if (device->tag_saved_openings != 0)
4742 newopenings = device->tag_saved_openings;
4744 newopenings = min(device->maxtags,
4745 sim->max_tagged_dev_openings);
4746 xpt_dev_ccbq_resize(path, newopenings);
4747 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4748 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4749 crs.ccb_h.func_code = XPT_REL_SIMQ;
4750 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4752 = crs.release_timeout
4755 xpt_action((union ccb *)&crs);
4759 xpt_stop_tags(struct cam_path *path)
4761 struct ccb_relsim crs;
4762 struct cam_ed *device;
4763 struct cam_sim *sim;
4765 device = path->device;
4766 sim = path->bus->sim;
4767 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4768 device->tag_delay_count = 0;
4769 xpt_freeze_devq(path, /*count*/1);
4770 device->inq_flags &= ~SID_CmdQue;
4771 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4772 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4773 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4774 crs.ccb_h.func_code = XPT_REL_SIMQ;
4775 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4777 = crs.release_timeout
4780 xpt_action((union ccb *)&crs);
4784 xpt_boot_delay(void *arg)
4791 xpt_config(void *arg)
4794 * Now that interrupts are enabled, go find our devices
4797 /* Setup debugging path */
4798 if (cam_dflags != CAM_DEBUG_NONE) {
4799 if (xpt_create_path_unlocked(&cam_dpath, NULL,
4800 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4801 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4802 printf("xpt_config: xpt_create_path() failed for debug"
4803 " target %d:%d:%d, debugging disabled\n",
4804 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4805 cam_dflags = CAM_DEBUG_NONE;
4810 periphdriver_init(1);
4812 callout_init(&xsoftc.boot_callout, 1);
4813 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4814 xpt_boot_delay, NULL);
4815 /* Fire up rescan thread. */
4816 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4817 printf("xpt_config: failed to create rescan thread.\n");
4825 xsoftc.buses_to_config++;
4830 xpt_release_boot(void)
4833 xsoftc.buses_to_config--;
4834 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4835 struct xpt_task *task;
4837 xsoftc.buses_config_done = 1;
4839 /* Call manually because we don't have any busses */
4840 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4842 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4843 taskqueue_enqueue(taskqueue_thread, &task->task);
4850 * If the given device only has one peripheral attached to it, and if that
4851 * peripheral is the passthrough driver, announce it. This insures that the
4852 * user sees some sort of announcement for every peripheral in their system.
4855 xptpassannouncefunc(struct cam_ed *device, void *arg)
4857 struct cam_periph *periph;
4860 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4861 periph = SLIST_NEXT(periph, periph_links), i++);
4863 periph = SLIST_FIRST(&device->periphs);
4865 && (strncmp(periph->periph_name, "pass", 4) == 0))
4866 xpt_announce_periph(periph, NULL);
4872 xpt_finishconfig_task(void *context, int pending)
4875 periphdriver_init(2);
4877 * Check for devices with no "standard" peripheral driver
4878 * attached. For any devices like that, announce the
4879 * passthrough driver so the user will see something.
4882 xpt_for_all_devices(xptpassannouncefunc, NULL);
4884 /* Release our hook so that the boot can continue. */
4885 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4886 free(xsoftc.xpt_config_hook, M_CAMXPT);
4887 xsoftc.xpt_config_hook = NULL;
4889 free(context, M_CAMXPT);
4893 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4894 struct cam_path *path)
4896 struct ccb_setasync csa;
4901 mtx_lock(&xsoftc.xpt_lock);
4902 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4903 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4904 if (status != CAM_REQ_CMP) {
4905 mtx_unlock(&xsoftc.xpt_lock);
4911 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4912 csa.ccb_h.func_code = XPT_SASYNC_CB;
4913 csa.event_enable = event;
4914 csa.callback = cbfunc;
4915 csa.callback_arg = cbarg;
4916 xpt_action((union ccb *)&csa);
4917 status = csa.ccb_h.status;
4920 xpt_free_path(path);
4921 mtx_unlock(&xsoftc.xpt_lock);
4924 if ((status == CAM_REQ_CMP) &&
4925 (csa.event_enable & AC_FOUND_DEVICE)) {
4927 * Get this peripheral up to date with all
4928 * the currently existing devices.
4930 xpt_for_all_devices(xptsetasyncfunc, &csa);
4932 if ((status == CAM_REQ_CMP) &&
4933 (csa.event_enable & AC_PATH_REGISTERED)) {
4935 * Get this peripheral up to date with all
4936 * the currently existing busses.
4938 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4945 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4947 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4949 switch (work_ccb->ccb_h.func_code) {
4950 /* Common cases first */
4951 case XPT_PATH_INQ: /* Path routing inquiry */
4953 struct ccb_pathinq *cpi;
4955 cpi = &work_ccb->cpi;
4956 cpi->version_num = 1; /* XXX??? */
4957 cpi->hba_inquiry = 0;
4958 cpi->target_sprt = 0;
4960 cpi->hba_eng_cnt = 0;
4961 cpi->max_target = 0;
4963 cpi->initiator_id = 0;
4964 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4965 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4966 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4967 cpi->unit_number = sim->unit_number;
4968 cpi->bus_id = sim->bus_id;
4969 cpi->base_transfer_speed = 0;
4970 cpi->protocol = PROTO_UNSPECIFIED;
4971 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4972 cpi->transport = XPORT_UNSPECIFIED;
4973 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4974 cpi->ccb_h.status = CAM_REQ_CMP;
4979 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4986 * The xpt as a "controller" has no interrupt sources, so polling
4990 xptpoll(struct cam_sim *sim)
4995 xpt_lock_buses(void)
4997 mtx_lock(&xsoftc.xpt_topo_lock);
5001 xpt_unlock_buses(void)
5003 mtx_unlock(&xsoftc.xpt_topo_lock);
5010 struct cam_sim *sim;
5012 mtx_lock(&cam_simq_lock);
5014 while (!TAILQ_EMPTY(&cam_simq)) {
5015 TAILQ_CONCAT(&queue, &cam_simq, links);
5016 mtx_unlock(&cam_simq_lock);
5018 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
5019 TAILQ_REMOVE(&queue, sim, links);
5021 camisr_runqueue(sim);
5022 sim->flags &= ~CAM_SIM_ON_DONEQ;
5023 CAM_SIM_UNLOCK(sim);
5025 mtx_lock(&cam_simq_lock);
5027 mtx_unlock(&cam_simq_lock);
5031 camisr_runqueue(struct cam_sim *sim)
5033 struct ccb_hdr *ccb_h;
5035 while ((ccb_h = TAILQ_FIRST(&sim->sim_doneq)) != NULL) {
5038 TAILQ_REMOVE(&sim->sim_doneq, ccb_h, sim_links.tqe);
5039 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5041 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5046 if (ccb_h->flags & CAM_HIGH_POWER) {
5047 struct highpowerlist *hphead;
5048 struct cam_ed *device;
5050 mtx_lock(&xsoftc.xpt_lock);
5051 hphead = &xsoftc.highpowerq;
5053 device = STAILQ_FIRST(hphead);
5056 * Increment the count since this command is done.
5058 xsoftc.num_highpower++;
5061 * Any high powered commands queued up?
5063 if (device != NULL) {
5065 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5066 mtx_unlock(&xsoftc.xpt_lock);
5068 xpt_release_devq_device(device,
5069 /*count*/1, /*runqueue*/TRUE);
5071 mtx_unlock(&xsoftc.xpt_lock);
5074 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5077 dev = ccb_h->path->device;
5079 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5080 sim->devq->send_active--;
5081 sim->devq->send_openings++;
5084 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5085 && (dev->ccbq.dev_active == 0))) {
5086 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5087 xpt_release_devq(ccb_h->path, /*count*/1,
5088 /*run_queue*/FALSE);
5091 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5092 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5093 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5094 xpt_release_devq(ccb_h->path, /*count*/1,
5095 /*run_queue*/FALSE);
5098 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5099 && (--dev->tag_delay_count == 0))
5100 xpt_start_tags(ccb_h->path);
5101 if (!device_is_queued(dev)) {
5102 (void)xpt_schedule_devq(sim->devq, dev);
5106 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5107 xpt_release_simq(sim, /*run_queue*/TRUE);
5108 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5112 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5113 && (ccb_h->status & CAM_DEV_QFRZN)) {
5114 xpt_release_devq(ccb_h->path, /*count*/1,
5116 ccb_h->status &= ~CAM_DEV_QFRZN;
5118 xpt_run_devq(sim->devq);
5121 /* Call the peripheral driver's callback */
5122 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);