2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_xpt_internal.h>
60 #include <cam/cam_debug.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
66 #include <machine/md_var.h> /* geometry translation */
67 #include <machine/stdarg.h> /* for xpt_print below */
72 * This is the maximum number of high powered commands (e.g. start unit)
73 * that can be outstanding at a particular time.
75 #ifndef CAM_MAX_HIGHPOWER
76 #define CAM_MAX_HIGHPOWER 4
79 /* Datastructures internal to the xpt layer */
80 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
81 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
82 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
83 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
85 /* Object for defering XPT actions to a taskqueue */
98 u_int32_t xpt_generation;
100 /* number of high powered commands that can go through right now */
101 STAILQ_HEAD(highpowerlist, ccb_hdr) 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;
184 static struct cdevsw xpt_cdevsw = {
185 .d_version = D_VERSION,
193 /* Storage for debugging datastructures */
194 struct cam_path *cam_dpath;
195 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
196 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
197 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
198 &cam_dflags, 0, "Enabled debug flags");
199 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
200 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
201 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
202 &cam_debug_delay, 0, "Delay in us after each debug message");
204 /* Our boot-time initialization hook */
205 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
207 static moduledata_t cam_moduledata = {
209 cam_module_event_handler,
213 static int xpt_init(void *);
215 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
216 MODULE_VERSION(cam, 1);
219 static void xpt_async_bcast(struct async_list *async_head,
220 u_int32_t async_code,
221 struct cam_path *path,
223 static path_id_t xptnextfreepathid(void);
224 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
225 static union ccb *xpt_get_ccb(struct cam_ed *device);
226 static void xpt_run_dev_allocq(struct cam_eb *bus);
227 static void xpt_run_dev_sendq(struct cam_eb *bus);
228 static timeout_t xpt_release_devq_timeout;
229 static void xpt_release_simq_timeout(void *arg) __unused;
230 static void xpt_release_bus(struct cam_eb *bus);
231 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
232 u_int count, int run_queue);
233 static struct cam_et*
234 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
235 static void xpt_release_target(struct cam_et *target);
236 static struct cam_eb*
237 xpt_find_bus(path_id_t path_id);
238 static struct cam_et*
239 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
240 static struct cam_ed*
241 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
242 static void xpt_config(void *arg);
243 static xpt_devicefunc_t xptpassannouncefunc;
244 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
245 static void xptpoll(struct cam_sim *sim);
246 static void camisr(void *);
247 static void camisr_runqueue(void *);
248 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
249 u_int num_patterns, struct cam_eb *bus);
250 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
252 struct cam_ed *device);
253 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
255 struct cam_periph *periph);
256 static xpt_busfunc_t xptedtbusfunc;
257 static xpt_targetfunc_t xptedttargetfunc;
258 static xpt_devicefunc_t xptedtdevicefunc;
259 static xpt_periphfunc_t xptedtperiphfunc;
260 static xpt_pdrvfunc_t xptplistpdrvfunc;
261 static xpt_periphfunc_t xptplistperiphfunc;
262 static int xptedtmatch(struct ccb_dev_match *cdm);
263 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
264 static int xptbustraverse(struct cam_eb *start_bus,
265 xpt_busfunc_t *tr_func, void *arg);
266 static int xpttargettraverse(struct cam_eb *bus,
267 struct cam_et *start_target,
268 xpt_targetfunc_t *tr_func, void *arg);
269 static int xptdevicetraverse(struct cam_et *target,
270 struct cam_ed *start_device,
271 xpt_devicefunc_t *tr_func, void *arg);
272 static int xptperiphtraverse(struct cam_ed *device,
273 struct cam_periph *start_periph,
274 xpt_periphfunc_t *tr_func, void *arg);
275 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
276 xpt_pdrvfunc_t *tr_func, void *arg);
277 static int xptpdperiphtraverse(struct periph_driver **pdrv,
278 struct cam_periph *start_periph,
279 xpt_periphfunc_t *tr_func,
281 static xpt_busfunc_t xptdefbusfunc;
282 static xpt_targetfunc_t xptdeftargetfunc;
283 static xpt_devicefunc_t xptdefdevicefunc;
284 static xpt_periphfunc_t xptdefperiphfunc;
285 static void xpt_finishconfig_task(void *context, int pending);
286 static void xpt_dev_async_default(u_int32_t async_code,
288 struct cam_et *target,
289 struct cam_ed *device,
291 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
292 struct cam_et *target,
294 static xpt_devicefunc_t xptsetasyncfunc;
295 static xpt_busfunc_t xptsetasyncbusfunc;
296 static cam_status xptregister(struct cam_periph *periph,
298 static __inline int periph_is_queued(struct cam_periph *periph);
299 static __inline int device_is_alloc_queued(struct cam_ed *device);
300 static __inline int device_is_send_queued(struct cam_ed *device);
303 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
307 if ((dev->drvq.entries > 0) &&
308 (dev->ccbq.devq_openings > 0) &&
309 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
310 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
312 * The priority of a device waiting for CCB resources
313 * is that of the highest priority peripheral driver
316 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
317 &dev->alloc_ccb_entry.pinfo,
318 CAMQ_GET_PRIO(&dev->drvq));
327 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
331 if ((dev->ccbq.queue.entries > 0) &&
332 (dev->ccbq.dev_openings > 0) &&
333 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
335 * The priority of a device waiting for controller
336 * resources is that of the highest priority CCB
340 xpt_schedule_dev(&bus->sim->devq->send_queue,
341 &dev->send_ccb_entry.pinfo,
342 CAMQ_GET_PRIO(&dev->ccbq.queue));
350 periph_is_queued(struct cam_periph *periph)
352 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
356 device_is_alloc_queued(struct cam_ed *device)
358 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
362 device_is_send_queued(struct cam_ed *device)
364 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
370 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
374 xptdone(struct cam_periph *periph, union ccb *done_ccb)
376 /* Caller will release the CCB */
377 wakeup(&done_ccb->ccb_h.cbfcnp);
381 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
385 * Only allow read-write access.
387 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
391 * We don't allow nonblocking access.
393 if ((flags & O_NONBLOCK) != 0) {
394 printf("%s: can't do nonblocking access\n", devtoname(dev));
398 /* Mark ourselves open */
399 mtx_lock(&xsoftc.xpt_lock);
400 xsoftc.flags |= XPT_FLAG_OPEN;
401 mtx_unlock(&xsoftc.xpt_lock);
407 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
410 /* Mark ourselves closed */
411 mtx_lock(&xsoftc.xpt_lock);
412 xsoftc.flags &= ~XPT_FLAG_OPEN;
413 mtx_unlock(&xsoftc.xpt_lock);
419 * Don't automatically grab the xpt softc lock here even though this is going
420 * through the xpt device. The xpt device is really just a back door for
421 * accessing other devices and SIMs, so the right thing to do is to grab
422 * the appropriate SIM lock once the bus/SIM is located.
425 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
433 * For the transport layer CAMIOCOMMAND ioctl, we really only want
434 * to accept CCB types that don't quite make sense to send through a
435 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
443 inccb = (union ccb *)addr;
445 bus = xpt_find_bus(inccb->ccb_h.path_id);
449 switch (inccb->ccb_h.func_code) {
452 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
453 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
454 xpt_release_bus(bus);
459 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
460 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
461 xpt_release_bus(bus);
469 switch(inccb->ccb_h.func_code) {
477 ccb = xpt_alloc_ccb();
479 CAM_SIM_LOCK(bus->sim);
482 * Create a path using the bus, target, and lun the
485 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
486 inccb->ccb_h.path_id,
487 inccb->ccb_h.target_id,
488 inccb->ccb_h.target_lun) !=
491 CAM_SIM_UNLOCK(bus->sim);
495 /* Ensure all of our fields are correct */
496 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
497 inccb->ccb_h.pinfo.priority);
498 xpt_merge_ccb(ccb, inccb);
499 ccb->ccb_h.cbfcnp = xptdone;
500 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
501 bcopy(ccb, inccb, sizeof(union ccb));
502 xpt_free_path(ccb->ccb_h.path);
504 CAM_SIM_UNLOCK(bus->sim);
511 * This is an immediate CCB, so it's okay to
512 * allocate it on the stack.
515 CAM_SIM_LOCK(bus->sim);
518 * Create a path using the bus, target, and lun the
521 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
522 inccb->ccb_h.path_id,
523 inccb->ccb_h.target_id,
524 inccb->ccb_h.target_lun) !=
527 CAM_SIM_UNLOCK(bus->sim);
530 /* Ensure all of our fields are correct */
531 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
532 inccb->ccb_h.pinfo.priority);
533 xpt_merge_ccb(&ccb, inccb);
534 ccb.ccb_h.cbfcnp = xptdone;
536 bcopy(&ccb, inccb, sizeof(union ccb));
537 xpt_free_path(ccb.ccb_h.path);
538 CAM_SIM_UNLOCK(bus->sim);
542 case XPT_DEV_MATCH: {
543 struct cam_periph_map_info mapinfo;
544 struct cam_path *old_path;
547 * We can't deal with physical addresses for this
548 * type of transaction.
550 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
556 * Save this in case the caller had it set to
557 * something in particular.
559 old_path = inccb->ccb_h.path;
562 * We really don't need a path for the matching
563 * code. The path is needed because of the
564 * debugging statements in xpt_action(). They
565 * assume that the CCB has a valid path.
567 inccb->ccb_h.path = xpt_periph->path;
569 bzero(&mapinfo, sizeof(mapinfo));
572 * Map the pattern and match buffers into kernel
573 * virtual address space.
575 error = cam_periph_mapmem(inccb, &mapinfo);
578 inccb->ccb_h.path = old_path;
583 * This is an immediate CCB, we can send it on directly.
585 CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
587 CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
590 * Map the buffers back into user space.
592 cam_periph_unmapmem(inccb, &mapinfo);
594 inccb->ccb_h.path = old_path;
603 xpt_release_bus(bus);
607 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
608 * with the periphal driver name and unit name filled in. The other
609 * fields don't really matter as input. The passthrough driver name
610 * ("pass"), and unit number are passed back in the ccb. The current
611 * device generation number, and the index into the device peripheral
612 * driver list, and the status are also passed back. Note that
613 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
614 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
615 * (or rather should be) impossible for the device peripheral driver
616 * list to change since we look at the whole thing in one pass, and
617 * we do it with lock protection.
620 case CAMGETPASSTHRU: {
622 struct cam_periph *periph;
623 struct periph_driver **p_drv;
626 u_int cur_generation;
627 int base_periph_found;
630 ccb = (union ccb *)addr;
631 unit = ccb->cgdl.unit_number;
632 name = ccb->cgdl.periph_name;
634 * Every 100 devices, we want to drop our lock protection to
635 * give the software interrupt handler a chance to run.
636 * Most systems won't run into this check, but this should
637 * avoid starvation in the software interrupt handler in
642 ccb = (union ccb *)addr;
644 base_periph_found = 0;
647 * Sanity check -- make sure we don't get a null peripheral
650 if (*ccb->cgdl.periph_name == '\0') {
655 /* Keep the list from changing while we traverse it */
656 mtx_lock(&xsoftc.xpt_topo_lock);
658 cur_generation = xsoftc.xpt_generation;
660 /* first find our driver in the list of drivers */
661 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
662 if (strcmp((*p_drv)->driver_name, name) == 0)
665 if (*p_drv == NULL) {
666 mtx_unlock(&xsoftc.xpt_topo_lock);
667 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
668 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
669 *ccb->cgdl.periph_name = '\0';
670 ccb->cgdl.unit_number = 0;
676 * Run through every peripheral instance of this driver
677 * and check to see whether it matches the unit passed
678 * in by the user. If it does, get out of the loops and
679 * find the passthrough driver associated with that
682 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
683 periph = TAILQ_NEXT(periph, unit_links)) {
685 if (periph->unit_number == unit) {
687 } else if (--splbreaknum == 0) {
688 mtx_unlock(&xsoftc.xpt_topo_lock);
689 mtx_lock(&xsoftc.xpt_topo_lock);
691 if (cur_generation != xsoftc.xpt_generation)
696 * If we found the peripheral driver that the user passed
697 * in, go through all of the peripheral drivers for that
698 * particular device and look for a passthrough driver.
700 if (periph != NULL) {
701 struct cam_ed *device;
704 base_periph_found = 1;
705 device = periph->path->device;
706 for (i = 0, periph = SLIST_FIRST(&device->periphs);
708 periph = SLIST_NEXT(periph, periph_links), i++) {
710 * Check to see whether we have a
711 * passthrough device or not.
713 if (strcmp(periph->periph_name, "pass") == 0) {
715 * Fill in the getdevlist fields.
717 strcpy(ccb->cgdl.periph_name,
718 periph->periph_name);
719 ccb->cgdl.unit_number =
721 if (SLIST_NEXT(periph, periph_links))
723 CAM_GDEVLIST_MORE_DEVS;
726 CAM_GDEVLIST_LAST_DEVICE;
727 ccb->cgdl.generation =
731 * Fill in some CCB header fields
732 * that the user may want.
735 periph->path->bus->path_id;
736 ccb->ccb_h.target_id =
737 periph->path->target->target_id;
738 ccb->ccb_h.target_lun =
739 periph->path->device->lun_id;
740 ccb->ccb_h.status = CAM_REQ_CMP;
747 * If the periph is null here, one of two things has
748 * happened. The first possibility is that we couldn't
749 * find the unit number of the particular peripheral driver
750 * that the user is asking about. e.g. the user asks for
751 * the passthrough driver for "da11". We find the list of
752 * "da" peripherals all right, but there is no unit 11.
753 * The other possibility is that we went through the list
754 * of peripheral drivers attached to the device structure,
755 * but didn't find one with the name "pass". Either way,
756 * we return ENOENT, since we couldn't find something.
758 if (periph == NULL) {
759 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
760 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
761 *ccb->cgdl.periph_name = '\0';
762 ccb->cgdl.unit_number = 0;
765 * It is unfortunate that this is even necessary,
766 * but there are many, many clueless users out there.
767 * If this is true, the user is looking for the
768 * passthrough driver, but doesn't have one in his
771 if (base_periph_found == 1) {
772 printf("xptioctl: pass driver is not in the "
774 printf("xptioctl: put \"device pass\" in "
775 "your kernel config file\n");
778 mtx_unlock(&xsoftc.xpt_topo_lock);
790 cam_module_event_handler(module_t mod, int what, void *arg)
796 if ((error = xpt_init(NULL)) != 0)
809 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
812 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
813 xpt_free_path(done_ccb->ccb_h.path);
814 xpt_free_ccb(done_ccb);
816 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
817 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
822 /* thread to handle bus rescans */
824 xpt_scanner_thread(void *dummy)
831 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
832 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
834 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
835 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
838 sim = ccb->ccb_h.path->bus->sim;
849 xpt_rescan(union ccb *ccb)
853 /* Prepare request */
854 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
855 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
856 ccb->ccb_h.func_code = XPT_SCAN_BUS;
857 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
858 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
859 ccb->ccb_h.func_code = XPT_SCAN_TGT;
860 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
861 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
862 ccb->ccb_h.func_code = XPT_SCAN_LUN;
864 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
865 xpt_free_path(ccb->ccb_h.path);
869 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
870 ccb->ccb_h.cbfcnp = xpt_rescan_done;
871 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
872 /* Don't make duplicate entries for the same paths. */
874 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
875 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
876 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
877 wakeup(&xsoftc.ccb_scanq);
879 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
880 xpt_free_path(ccb->ccb_h.path);
886 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
887 xsoftc.buses_to_config++;
888 wakeup(&xsoftc.ccb_scanq);
892 /* Functions accessed by the peripheral drivers */
894 xpt_init(void *dummy)
896 struct cam_sim *xpt_sim;
897 struct cam_path *path;
898 struct cam_devq *devq;
901 TAILQ_INIT(&xsoftc.xpt_busses);
902 TAILQ_INIT(&cam_simq);
903 TAILQ_INIT(&xsoftc.ccb_scanq);
904 STAILQ_INIT(&xsoftc.highpowerq);
905 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
907 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
908 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
909 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
912 * The xpt layer is, itself, the equivelent of a SIM.
913 * Allow 16 ccbs in the ccb pool for it. This should
914 * give decent parallelism when we probe busses and
915 * perform other XPT functions.
917 devq = cam_simq_alloc(16);
918 xpt_sim = cam_sim_alloc(xptaction,
923 /*mtx*/&xsoftc.xpt_lock,
924 /*max_dev_transactions*/0,
925 /*max_tagged_dev_transactions*/0,
930 mtx_lock(&xsoftc.xpt_lock);
931 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
932 mtx_unlock(&xsoftc.xpt_lock);
933 printf("xpt_init: xpt_bus_register failed with status %#x,"
934 " failing attach\n", status);
939 * Looking at the XPT from the SIM layer, the XPT is
940 * the equivelent of a peripheral driver. Allocate
941 * a peripheral driver entry for us.
943 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
945 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
946 mtx_unlock(&xsoftc.xpt_lock);
947 printf("xpt_init: xpt_create_path failed with status %#x,"
948 " failing attach\n", status);
952 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
953 path, NULL, 0, xpt_sim);
955 mtx_unlock(&xsoftc.xpt_lock);
956 /* Install our software interrupt handlers */
957 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
959 * Register a callback for when interrupts are enabled.
961 xsoftc.xpt_config_hook =
962 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
963 M_CAMXPT, M_NOWAIT | M_ZERO);
964 if (xsoftc.xpt_config_hook == NULL) {
965 printf("xpt_init: Cannot malloc config hook "
966 "- failing attach\n");
969 xsoftc.xpt_config_hook->ich_func = xpt_config;
970 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
971 free (xsoftc.xpt_config_hook, M_CAMXPT);
972 printf("xpt_init: config_intrhook_establish failed "
973 "- failing attach\n");
980 xptregister(struct cam_periph *periph, void *arg)
982 struct cam_sim *xpt_sim;
984 if (periph == NULL) {
985 printf("xptregister: periph was NULL!!\n");
986 return(CAM_REQ_CMP_ERR);
989 xpt_sim = (struct cam_sim *)arg;
990 xpt_sim->softc = periph;
992 periph->softc = NULL;
998 xpt_add_periph(struct cam_periph *periph)
1000 struct cam_ed *device;
1002 struct periph_list *periph_head;
1004 mtx_assert(periph->sim->mtx, MA_OWNED);
1006 device = periph->path->device;
1008 periph_head = &device->periphs;
1010 status = CAM_REQ_CMP;
1012 if (device != NULL) {
1014 * Make room for this peripheral
1015 * so it will fit in the queue
1016 * when it's scheduled to run
1018 status = camq_resize(&device->drvq,
1019 device->drvq.array_size + 1);
1021 device->generation++;
1023 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1026 mtx_lock(&xsoftc.xpt_topo_lock);
1027 xsoftc.xpt_generation++;
1028 mtx_unlock(&xsoftc.xpt_topo_lock);
1034 xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
1036 struct cam_ed *device;
1038 mtx_assert(periph->sim->mtx, MA_OWNED);
1040 device = periph->path->device;
1042 if (device != NULL) {
1043 struct periph_list *periph_head;
1045 periph_head = &device->periphs;
1047 /* Release the slot for this peripheral */
1048 camq_resize(&device->drvq, device->drvq.array_size - 1);
1050 device->generation++;
1052 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1055 if (topology_lock_held == 0)
1056 mtx_lock(&xsoftc.xpt_topo_lock);
1058 xsoftc.xpt_generation++;
1060 if (topology_lock_held == 0)
1061 mtx_unlock(&xsoftc.xpt_topo_lock);
1066 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1068 struct cam_path *path = periph->path;
1070 mtx_assert(periph->sim->mtx, MA_OWNED);
1072 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1073 periph->periph_name, periph->unit_number,
1074 path->bus->sim->sim_name,
1075 path->bus->sim->unit_number,
1076 path->bus->sim->bus_id,
1078 path->target->target_id,
1079 path->device->lun_id);
1080 printf("%s%d: ", periph->periph_name, periph->unit_number);
1081 if (path->device->protocol == PROTO_SCSI)
1082 scsi_print_inquiry(&path->device->inq_data);
1083 else if (path->device->protocol == PROTO_ATA ||
1084 path->device->protocol == PROTO_SATAPM)
1085 ata_print_ident(&path->device->ident_data);
1086 else if (path->device->protocol == PROTO_SEMB)
1088 (struct sep_identify_data *)&path->device->ident_data);
1090 printf("Unknown protocol device\n");
1091 if (bootverbose && path->device->serial_num_len > 0) {
1092 /* Don't wrap the screen - print only the first 60 chars */
1093 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1094 periph->unit_number, path->device->serial_num);
1096 /* Announce transport details. */
1097 (*(path->bus->xport->announce))(periph);
1098 /* Announce command queueing. */
1099 if (path->device->inq_flags & SID_CmdQue
1100 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1101 printf("%s%d: Command Queueing enabled\n",
1102 periph->periph_name, periph->unit_number);
1104 /* Announce caller's details if they've passed in. */
1105 if (announce_string != NULL)
1106 printf("%s%d: %s\n", periph->periph_name,
1107 periph->unit_number, announce_string);
1111 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1114 struct ccb_dev_advinfo cdai;
1116 mtx_assert(path->bus->sim->mtx, MA_OWNED);
1118 memset(&cdai, 0, sizeof(cdai));
1119 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1120 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1123 if (!strcmp(attr, "GEOM::ident"))
1124 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1125 else if (!strcmp(attr, "GEOM::physpath"))
1126 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1130 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1131 if (cdai.buf == NULL) {
1135 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1136 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1137 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1138 if (cdai.provsiz == 0)
1141 if (strlcpy(buf, cdai.buf, len) >= len)
1145 if (cdai.buf != NULL)
1146 free(cdai.buf, M_CAMXPT);
1150 static dev_match_ret
1151 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1154 dev_match_ret retval;
1157 retval = DM_RET_NONE;
1160 * If we aren't given something to match against, that's an error.
1163 return(DM_RET_ERROR);
1166 * If there are no match entries, then this bus matches no
1169 if ((patterns == NULL) || (num_patterns == 0))
1170 return(DM_RET_DESCEND | DM_RET_COPY);
1172 for (i = 0; i < num_patterns; i++) {
1173 struct bus_match_pattern *cur_pattern;
1176 * If the pattern in question isn't for a bus node, we
1177 * aren't interested. However, we do indicate to the
1178 * calling routine that we should continue descending the
1179 * tree, since the user wants to match against lower-level
1182 if (patterns[i].type != DEV_MATCH_BUS) {
1183 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1184 retval |= DM_RET_DESCEND;
1188 cur_pattern = &patterns[i].pattern.bus_pattern;
1191 * If they want to match any bus node, we give them any
1194 if (cur_pattern->flags == BUS_MATCH_ANY) {
1195 /* set the copy flag */
1196 retval |= DM_RET_COPY;
1199 * If we've already decided on an action, go ahead
1202 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1207 * Not sure why someone would do this...
1209 if (cur_pattern->flags == BUS_MATCH_NONE)
1212 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1213 && (cur_pattern->path_id != bus->path_id))
1216 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1217 && (cur_pattern->bus_id != bus->sim->bus_id))
1220 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1221 && (cur_pattern->unit_number != bus->sim->unit_number))
1224 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1225 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1230 * If we get to this point, the user definitely wants
1231 * information on this bus. So tell the caller to copy the
1234 retval |= DM_RET_COPY;
1237 * If the return action has been set to descend, then we
1238 * know that we've already seen a non-bus matching
1239 * expression, therefore we need to further descend the tree.
1240 * This won't change by continuing around the loop, so we
1241 * go ahead and return. If we haven't seen a non-bus
1242 * matching expression, we keep going around the loop until
1243 * we exhaust the matching expressions. We'll set the stop
1244 * flag once we fall out of the loop.
1246 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1251 * If the return action hasn't been set to descend yet, that means
1252 * we haven't seen anything other than bus matching patterns. So
1253 * tell the caller to stop descending the tree -- the user doesn't
1254 * want to match against lower level tree elements.
1256 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1257 retval |= DM_RET_STOP;
1262 static dev_match_ret
1263 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1264 struct cam_ed *device)
1266 dev_match_ret retval;
1269 retval = DM_RET_NONE;
1272 * If we aren't given something to match against, that's an error.
1275 return(DM_RET_ERROR);
1278 * If there are no match entries, then this device matches no
1281 if ((patterns == NULL) || (num_patterns == 0))
1282 return(DM_RET_DESCEND | DM_RET_COPY);
1284 for (i = 0; i < num_patterns; i++) {
1285 struct device_match_pattern *cur_pattern;
1286 struct scsi_vpd_device_id *device_id_page;
1289 * If the pattern in question isn't for a device node, we
1290 * aren't interested.
1292 if (patterns[i].type != DEV_MATCH_DEVICE) {
1293 if ((patterns[i].type == DEV_MATCH_PERIPH)
1294 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1295 retval |= DM_RET_DESCEND;
1299 cur_pattern = &patterns[i].pattern.device_pattern;
1301 /* Error out if mutually exclusive options are specified. */
1302 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1303 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1304 return(DM_RET_ERROR);
1307 * If they want to match any device node, we give them any
1310 if (cur_pattern->flags == DEV_MATCH_ANY)
1314 * Not sure why someone would do this...
1316 if (cur_pattern->flags == DEV_MATCH_NONE)
1319 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1320 && (cur_pattern->path_id != device->target->bus->path_id))
1323 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1324 && (cur_pattern->target_id != device->target->target_id))
1327 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1328 && (cur_pattern->target_lun != device->lun_id))
1331 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1332 && (cam_quirkmatch((caddr_t)&device->inq_data,
1333 (caddr_t)&cur_pattern->data.inq_pat,
1334 1, sizeof(cur_pattern->data.inq_pat),
1335 scsi_static_inquiry_match) == NULL))
1338 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1339 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1340 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1341 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1342 device->device_id_len
1343 - SVPD_DEVICE_ID_HDR_LEN,
1344 cur_pattern->data.devid_pat.id,
1345 cur_pattern->data.devid_pat.id_len) != 0))
1350 * If we get to this point, the user definitely wants
1351 * information on this device. So tell the caller to copy
1354 retval |= DM_RET_COPY;
1357 * If the return action has been set to descend, then we
1358 * know that we've already seen a peripheral matching
1359 * expression, therefore we need to further descend the tree.
1360 * This won't change by continuing around the loop, so we
1361 * go ahead and return. If we haven't seen a peripheral
1362 * matching expression, we keep going around the loop until
1363 * we exhaust the matching expressions. We'll set the stop
1364 * flag once we fall out of the loop.
1366 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1371 * If the return action hasn't been set to descend yet, that means
1372 * we haven't seen any peripheral matching patterns. So tell the
1373 * caller to stop descending the tree -- the user doesn't want to
1374 * match against lower level tree elements.
1376 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1377 retval |= DM_RET_STOP;
1383 * Match a single peripheral against any number of match patterns.
1385 static dev_match_ret
1386 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1387 struct cam_periph *periph)
1389 dev_match_ret retval;
1393 * If we aren't given something to match against, that's an error.
1396 return(DM_RET_ERROR);
1399 * If there are no match entries, then this peripheral matches no
1402 if ((patterns == NULL) || (num_patterns == 0))
1403 return(DM_RET_STOP | DM_RET_COPY);
1406 * There aren't any nodes below a peripheral node, so there's no
1407 * reason to descend the tree any further.
1409 retval = DM_RET_STOP;
1411 for (i = 0; i < num_patterns; i++) {
1412 struct periph_match_pattern *cur_pattern;
1415 * If the pattern in question isn't for a peripheral, we
1416 * aren't interested.
1418 if (patterns[i].type != DEV_MATCH_PERIPH)
1421 cur_pattern = &patterns[i].pattern.periph_pattern;
1424 * If they want to match on anything, then we will do so.
1426 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1427 /* set the copy flag */
1428 retval |= DM_RET_COPY;
1431 * We've already set the return action to stop,
1432 * since there are no nodes below peripherals in
1439 * Not sure why someone would do this...
1441 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1444 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1445 && (cur_pattern->path_id != periph->path->bus->path_id))
1449 * For the target and lun id's, we have to make sure the
1450 * target and lun pointers aren't NULL. The xpt peripheral
1451 * has a wildcard target and device.
1453 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1454 && ((periph->path->target == NULL)
1455 ||(cur_pattern->target_id != periph->path->target->target_id)))
1458 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1459 && ((periph->path->device == NULL)
1460 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1463 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1464 && (cur_pattern->unit_number != periph->unit_number))
1467 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1468 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1473 * If we get to this point, the user definitely wants
1474 * information on this peripheral. So tell the caller to
1475 * copy the data out.
1477 retval |= DM_RET_COPY;
1480 * The return action has already been set to stop, since
1481 * peripherals don't have any nodes below them in the EDT.
1487 * If we get to this point, the peripheral that was passed in
1488 * doesn't match any of the patterns.
1494 xptedtbusfunc(struct cam_eb *bus, void *arg)
1496 struct ccb_dev_match *cdm;
1497 dev_match_ret retval;
1499 cdm = (struct ccb_dev_match *)arg;
1502 * If our position is for something deeper in the tree, that means
1503 * that we've already seen this node. So, we keep going down.
1505 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1506 && (cdm->pos.cookie.bus == bus)
1507 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1508 && (cdm->pos.cookie.target != NULL))
1509 retval = DM_RET_DESCEND;
1511 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1514 * If we got an error, bail out of the search.
1516 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1517 cdm->status = CAM_DEV_MATCH_ERROR;
1522 * If the copy flag is set, copy this bus out.
1524 if (retval & DM_RET_COPY) {
1527 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1528 sizeof(struct dev_match_result));
1531 * If we don't have enough space to put in another
1532 * match result, save our position and tell the
1533 * user there are more devices to check.
1535 if (spaceleft < sizeof(struct dev_match_result)) {
1536 bzero(&cdm->pos, sizeof(cdm->pos));
1537 cdm->pos.position_type =
1538 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1540 cdm->pos.cookie.bus = bus;
1541 cdm->pos.generations[CAM_BUS_GENERATION]=
1542 xsoftc.bus_generation;
1543 cdm->status = CAM_DEV_MATCH_MORE;
1546 j = cdm->num_matches;
1548 cdm->matches[j].type = DEV_MATCH_BUS;
1549 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1550 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1551 cdm->matches[j].result.bus_result.unit_number =
1552 bus->sim->unit_number;
1553 strncpy(cdm->matches[j].result.bus_result.dev_name,
1554 bus->sim->sim_name, DEV_IDLEN);
1558 * If the user is only interested in busses, there's no
1559 * reason to descend to the next level in the tree.
1561 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1565 * If there is a target generation recorded, check it to
1566 * make sure the target list hasn't changed.
1568 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1569 && (bus == cdm->pos.cookie.bus)
1570 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1571 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1572 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1574 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1578 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1579 && (cdm->pos.cookie.bus == bus)
1580 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1581 && (cdm->pos.cookie.target != NULL))
1582 return(xpttargettraverse(bus,
1583 (struct cam_et *)cdm->pos.cookie.target,
1584 xptedttargetfunc, arg));
1586 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1590 xptedttargetfunc(struct cam_et *target, void *arg)
1592 struct ccb_dev_match *cdm;
1594 cdm = (struct ccb_dev_match *)arg;
1597 * If there is a device list generation recorded, check it to
1598 * make sure the device list hasn't changed.
1600 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1601 && (cdm->pos.cookie.bus == target->bus)
1602 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1603 && (cdm->pos.cookie.target == target)
1604 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1605 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1606 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1607 target->generation)) {
1608 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1612 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1613 && (cdm->pos.cookie.bus == target->bus)
1614 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1615 && (cdm->pos.cookie.target == target)
1616 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1617 && (cdm->pos.cookie.device != NULL))
1618 return(xptdevicetraverse(target,
1619 (struct cam_ed *)cdm->pos.cookie.device,
1620 xptedtdevicefunc, arg));
1622 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1626 xptedtdevicefunc(struct cam_ed *device, void *arg)
1629 struct ccb_dev_match *cdm;
1630 dev_match_ret retval;
1632 cdm = (struct ccb_dev_match *)arg;
1635 * If our position is for something deeper in the tree, that means
1636 * that we've already seen this node. So, we keep going down.
1638 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1639 && (cdm->pos.cookie.device == device)
1640 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1641 && (cdm->pos.cookie.periph != NULL))
1642 retval = DM_RET_DESCEND;
1644 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1647 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1648 cdm->status = CAM_DEV_MATCH_ERROR;
1653 * If the copy flag is set, copy this device out.
1655 if (retval & DM_RET_COPY) {
1658 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1659 sizeof(struct dev_match_result));
1662 * If we don't have enough space to put in another
1663 * match result, save our position and tell the
1664 * user there are more devices to check.
1666 if (spaceleft < sizeof(struct dev_match_result)) {
1667 bzero(&cdm->pos, sizeof(cdm->pos));
1668 cdm->pos.position_type =
1669 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1670 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1672 cdm->pos.cookie.bus = device->target->bus;
1673 cdm->pos.generations[CAM_BUS_GENERATION]=
1674 xsoftc.bus_generation;
1675 cdm->pos.cookie.target = device->target;
1676 cdm->pos.generations[CAM_TARGET_GENERATION] =
1677 device->target->bus->generation;
1678 cdm->pos.cookie.device = device;
1679 cdm->pos.generations[CAM_DEV_GENERATION] =
1680 device->target->generation;
1681 cdm->status = CAM_DEV_MATCH_MORE;
1684 j = cdm->num_matches;
1686 cdm->matches[j].type = DEV_MATCH_DEVICE;
1687 cdm->matches[j].result.device_result.path_id =
1688 device->target->bus->path_id;
1689 cdm->matches[j].result.device_result.target_id =
1690 device->target->target_id;
1691 cdm->matches[j].result.device_result.target_lun =
1693 cdm->matches[j].result.device_result.protocol =
1695 bcopy(&device->inq_data,
1696 &cdm->matches[j].result.device_result.inq_data,
1697 sizeof(struct scsi_inquiry_data));
1698 bcopy(&device->ident_data,
1699 &cdm->matches[j].result.device_result.ident_data,
1700 sizeof(struct ata_params));
1702 /* Let the user know whether this device is unconfigured */
1703 if (device->flags & CAM_DEV_UNCONFIGURED)
1704 cdm->matches[j].result.device_result.flags =
1705 DEV_RESULT_UNCONFIGURED;
1707 cdm->matches[j].result.device_result.flags =
1712 * If the user isn't interested in peripherals, don't descend
1713 * the tree any further.
1715 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1719 * If there is a peripheral list generation recorded, make sure
1720 * it hasn't changed.
1722 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1723 && (device->target->bus == cdm->pos.cookie.bus)
1724 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1725 && (device->target == cdm->pos.cookie.target)
1726 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1727 && (device == cdm->pos.cookie.device)
1728 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1729 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1730 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1731 device->generation)){
1732 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1736 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1737 && (cdm->pos.cookie.bus == device->target->bus)
1738 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1739 && (cdm->pos.cookie.target == device->target)
1740 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1741 && (cdm->pos.cookie.device == device)
1742 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1743 && (cdm->pos.cookie.periph != NULL))
1744 return(xptperiphtraverse(device,
1745 (struct cam_periph *)cdm->pos.cookie.periph,
1746 xptedtperiphfunc, arg));
1748 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1752 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1754 struct ccb_dev_match *cdm;
1755 dev_match_ret retval;
1757 cdm = (struct ccb_dev_match *)arg;
1759 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1761 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1762 cdm->status = CAM_DEV_MATCH_ERROR;
1767 * If the copy flag is set, copy this peripheral out.
1769 if (retval & DM_RET_COPY) {
1772 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1773 sizeof(struct dev_match_result));
1776 * If we don't have enough space to put in another
1777 * match result, save our position and tell the
1778 * user there are more devices to check.
1780 if (spaceleft < sizeof(struct dev_match_result)) {
1781 bzero(&cdm->pos, sizeof(cdm->pos));
1782 cdm->pos.position_type =
1783 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1784 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1787 cdm->pos.cookie.bus = periph->path->bus;
1788 cdm->pos.generations[CAM_BUS_GENERATION]=
1789 xsoftc.bus_generation;
1790 cdm->pos.cookie.target = periph->path->target;
1791 cdm->pos.generations[CAM_TARGET_GENERATION] =
1792 periph->path->bus->generation;
1793 cdm->pos.cookie.device = periph->path->device;
1794 cdm->pos.generations[CAM_DEV_GENERATION] =
1795 periph->path->target->generation;
1796 cdm->pos.cookie.periph = periph;
1797 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1798 periph->path->device->generation;
1799 cdm->status = CAM_DEV_MATCH_MORE;
1803 j = cdm->num_matches;
1805 cdm->matches[j].type = DEV_MATCH_PERIPH;
1806 cdm->matches[j].result.periph_result.path_id =
1807 periph->path->bus->path_id;
1808 cdm->matches[j].result.periph_result.target_id =
1809 periph->path->target->target_id;
1810 cdm->matches[j].result.periph_result.target_lun =
1811 periph->path->device->lun_id;
1812 cdm->matches[j].result.periph_result.unit_number =
1813 periph->unit_number;
1814 strncpy(cdm->matches[j].result.periph_result.periph_name,
1815 periph->periph_name, DEV_IDLEN);
1822 xptedtmatch(struct ccb_dev_match *cdm)
1826 cdm->num_matches = 0;
1829 * Check the bus list generation. If it has changed, the user
1830 * needs to reset everything and start over.
1832 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1833 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1834 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1835 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1839 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1840 && (cdm->pos.cookie.bus != NULL))
1841 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1842 xptedtbusfunc, cdm);
1844 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1847 * If we get back 0, that means that we had to stop before fully
1848 * traversing the EDT. It also means that one of the subroutines
1849 * has set the status field to the proper value. If we get back 1,
1850 * we've fully traversed the EDT and copied out any matching entries.
1853 cdm->status = CAM_DEV_MATCH_LAST;
1859 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1861 struct ccb_dev_match *cdm;
1863 cdm = (struct ccb_dev_match *)arg;
1865 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1866 && (cdm->pos.cookie.pdrv == pdrv)
1867 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1868 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1869 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1870 (*pdrv)->generation)) {
1871 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1875 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1876 && (cdm->pos.cookie.pdrv == pdrv)
1877 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1878 && (cdm->pos.cookie.periph != NULL))
1879 return(xptpdperiphtraverse(pdrv,
1880 (struct cam_periph *)cdm->pos.cookie.periph,
1881 xptplistperiphfunc, arg));
1883 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1887 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1889 struct ccb_dev_match *cdm;
1890 dev_match_ret retval;
1892 cdm = (struct ccb_dev_match *)arg;
1894 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1896 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1897 cdm->status = CAM_DEV_MATCH_ERROR;
1902 * If the copy flag is set, copy this peripheral out.
1904 if (retval & DM_RET_COPY) {
1907 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1908 sizeof(struct dev_match_result));
1911 * If we don't have enough space to put in another
1912 * match result, save our position and tell the
1913 * user there are more devices to check.
1915 if (spaceleft < sizeof(struct dev_match_result)) {
1916 struct periph_driver **pdrv;
1919 bzero(&cdm->pos, sizeof(cdm->pos));
1920 cdm->pos.position_type =
1921 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1925 * This may look a bit non-sensical, but it is
1926 * actually quite logical. There are very few
1927 * peripheral drivers, and bloating every peripheral
1928 * structure with a pointer back to its parent
1929 * peripheral driver linker set entry would cost
1930 * more in the long run than doing this quick lookup.
1932 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1933 if (strcmp((*pdrv)->driver_name,
1934 periph->periph_name) == 0)
1938 if (*pdrv == NULL) {
1939 cdm->status = CAM_DEV_MATCH_ERROR;
1943 cdm->pos.cookie.pdrv = pdrv;
1945 * The periph generation slot does double duty, as
1946 * does the periph pointer slot. They are used for
1947 * both edt and pdrv lookups and positioning.
1949 cdm->pos.cookie.periph = periph;
1950 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1951 (*pdrv)->generation;
1952 cdm->status = CAM_DEV_MATCH_MORE;
1956 j = cdm->num_matches;
1958 cdm->matches[j].type = DEV_MATCH_PERIPH;
1959 cdm->matches[j].result.periph_result.path_id =
1960 periph->path->bus->path_id;
1963 * The transport layer peripheral doesn't have a target or
1966 if (periph->path->target)
1967 cdm->matches[j].result.periph_result.target_id =
1968 periph->path->target->target_id;
1970 cdm->matches[j].result.periph_result.target_id = -1;
1972 if (periph->path->device)
1973 cdm->matches[j].result.periph_result.target_lun =
1974 periph->path->device->lun_id;
1976 cdm->matches[j].result.periph_result.target_lun = -1;
1978 cdm->matches[j].result.periph_result.unit_number =
1979 periph->unit_number;
1980 strncpy(cdm->matches[j].result.periph_result.periph_name,
1981 periph->periph_name, DEV_IDLEN);
1988 xptperiphlistmatch(struct ccb_dev_match *cdm)
1992 cdm->num_matches = 0;
1995 * At this point in the edt traversal function, we check the bus
1996 * list generation to make sure that no busses have been added or
1997 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1998 * For the peripheral driver list traversal function, however, we
1999 * don't have to worry about new peripheral driver types coming or
2000 * going; they're in a linker set, and therefore can't change
2001 * without a recompile.
2004 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2005 && (cdm->pos.cookie.pdrv != NULL))
2006 ret = xptpdrvtraverse(
2007 (struct periph_driver **)cdm->pos.cookie.pdrv,
2008 xptplistpdrvfunc, cdm);
2010 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2013 * If we get back 0, that means that we had to stop before fully
2014 * traversing the peripheral driver tree. It also means that one of
2015 * the subroutines has set the status field to the proper value. If
2016 * we get back 1, we've fully traversed the EDT and copied out any
2020 cdm->status = CAM_DEV_MATCH_LAST;
2026 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2028 struct cam_eb *bus, *next_bus;
2033 mtx_lock(&xsoftc.xpt_topo_lock);
2034 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2041 * XXX The locking here is obviously very complex. We
2042 * should work to simplify it.
2044 mtx_unlock(&xsoftc.xpt_topo_lock);
2045 CAM_SIM_LOCK(bus->sim);
2046 retval = tr_func(bus, arg);
2047 CAM_SIM_UNLOCK(bus->sim);
2049 mtx_lock(&xsoftc.xpt_topo_lock);
2050 next_bus = TAILQ_NEXT(bus, links);
2051 mtx_unlock(&xsoftc.xpt_topo_lock);
2053 xpt_release_bus(bus);
2057 mtx_lock(&xsoftc.xpt_topo_lock);
2059 mtx_unlock(&xsoftc.xpt_topo_lock);
2065 xpt_sim_opened(struct cam_sim *sim)
2068 struct cam_et *target;
2069 struct cam_ed *device;
2070 struct cam_periph *periph;
2072 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2073 mtx_assert(sim->mtx, MA_OWNED);
2075 mtx_lock(&xsoftc.xpt_topo_lock);
2076 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2077 if (bus->sim != sim)
2080 TAILQ_FOREACH(target, &bus->et_entries, links) {
2081 TAILQ_FOREACH(device, &target->ed_entries, links) {
2082 SLIST_FOREACH(periph, &device->periphs,
2084 if (periph->refcount > 0) {
2085 mtx_unlock(&xsoftc.xpt_topo_lock);
2093 mtx_unlock(&xsoftc.xpt_topo_lock);
2098 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2099 xpt_targetfunc_t *tr_func, void *arg)
2101 struct cam_et *target, *next_target;
2105 for (target = (start_target ? start_target :
2106 TAILQ_FIRST(&bus->et_entries));
2107 target != NULL; target = next_target) {
2111 retval = tr_func(target, arg);
2113 next_target = TAILQ_NEXT(target, links);
2115 xpt_release_target(target);
2125 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2126 xpt_devicefunc_t *tr_func, void *arg)
2128 struct cam_ed *device, *next_device;
2132 for (device = (start_device ? start_device :
2133 TAILQ_FIRST(&target->ed_entries));
2135 device = next_device) {
2138 * Hold a reference so the current device does not go away
2143 retval = tr_func(device, arg);
2146 * Grab our next pointer before we release the current
2149 next_device = TAILQ_NEXT(device, links);
2151 xpt_release_device(device);
2161 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2162 xpt_periphfunc_t *tr_func, void *arg)
2164 struct cam_periph *periph, *next_periph;
2170 for (periph = (start_periph ? start_periph :
2171 SLIST_FIRST(&device->periphs));
2173 periph = next_periph) {
2177 * In this case, we want to show peripherals that have been
2178 * invalidated, but not peripherals that are scheduled to
2179 * be freed. So instead of calling cam_periph_acquire(),
2180 * which will fail if the periph has been invalidated, we
2181 * just check for the free flag here. If it is in the
2182 * process of being freed, we skip to the next periph.
2184 if (periph->flags & CAM_PERIPH_FREE) {
2185 next_periph = SLIST_NEXT(periph, periph_links);
2190 * Acquire a reference to this periph while we call the
2191 * traversal function, so it can't go away.
2195 retval = tr_func(periph, arg);
2198 * Grab the next peripheral before we release this one, so
2199 * our next pointer is still valid.
2201 next_periph = SLIST_NEXT(periph, periph_links);
2203 cam_periph_release_locked_buses(periph);
2217 xptpdrvtraverse(struct periph_driver **start_pdrv,
2218 xpt_pdrvfunc_t *tr_func, void *arg)
2220 struct periph_driver **pdrv;
2226 * We don't traverse the peripheral driver list like we do the
2227 * other lists, because it is a linker set, and therefore cannot be
2228 * changed during runtime. If the peripheral driver list is ever
2229 * re-done to be something other than a linker set (i.e. it can
2230 * change while the system is running), the list traversal should
2231 * be modified to work like the other traversal functions.
2233 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2234 *pdrv != NULL; pdrv++) {
2235 retval = tr_func(pdrv, arg);
2245 xptpdperiphtraverse(struct periph_driver **pdrv,
2246 struct cam_periph *start_periph,
2247 xpt_periphfunc_t *tr_func, void *arg)
2249 struct cam_periph *periph, *next_periph;
2255 for (periph = (start_periph ? start_periph :
2256 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2257 periph = next_periph) {
2261 * In this case, we want to show peripherals that have been
2262 * invalidated, but not peripherals that are scheduled to
2263 * be freed. So instead of calling cam_periph_acquire(),
2264 * which will fail if the periph has been invalidated, we
2265 * just check for the free flag here. If it is free, we
2266 * skip to the next periph.
2268 if (periph->flags & CAM_PERIPH_FREE) {
2269 next_periph = TAILQ_NEXT(periph, unit_links);
2274 * Acquire a reference to this periph while we call the
2275 * traversal function, so it can't go away.
2279 retval = tr_func(periph, arg);
2282 * Grab the next peripheral before we release this one, so
2283 * our next pointer is still valid.
2285 next_periph = TAILQ_NEXT(periph, unit_links);
2287 cam_periph_release_locked_buses(periph);
2300 xptdefbusfunc(struct cam_eb *bus, void *arg)
2302 struct xpt_traverse_config *tr_config;
2304 tr_config = (struct xpt_traverse_config *)arg;
2306 if (tr_config->depth == XPT_DEPTH_BUS) {
2307 xpt_busfunc_t *tr_func;
2309 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2311 return(tr_func(bus, tr_config->tr_arg));
2313 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2317 xptdeftargetfunc(struct cam_et *target, void *arg)
2319 struct xpt_traverse_config *tr_config;
2321 tr_config = (struct xpt_traverse_config *)arg;
2323 if (tr_config->depth == XPT_DEPTH_TARGET) {
2324 xpt_targetfunc_t *tr_func;
2326 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2328 return(tr_func(target, tr_config->tr_arg));
2330 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2334 xptdefdevicefunc(struct cam_ed *device, void *arg)
2336 struct xpt_traverse_config *tr_config;
2338 tr_config = (struct xpt_traverse_config *)arg;
2340 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2341 xpt_devicefunc_t *tr_func;
2343 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2345 return(tr_func(device, tr_config->tr_arg));
2347 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2351 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2353 struct xpt_traverse_config *tr_config;
2354 xpt_periphfunc_t *tr_func;
2356 tr_config = (struct xpt_traverse_config *)arg;
2358 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2361 * Unlike the other default functions, we don't check for depth
2362 * here. The peripheral driver level is the last level in the EDT,
2363 * so if we're here, we should execute the function in question.
2365 return(tr_func(periph, tr_config->tr_arg));
2369 * Execute the given function for every bus in the EDT.
2372 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2374 struct xpt_traverse_config tr_config;
2376 tr_config.depth = XPT_DEPTH_BUS;
2377 tr_config.tr_func = tr_func;
2378 tr_config.tr_arg = arg;
2380 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2384 * Execute the given function for every device in the EDT.
2387 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2389 struct xpt_traverse_config tr_config;
2391 tr_config.depth = XPT_DEPTH_DEVICE;
2392 tr_config.tr_func = tr_func;
2393 tr_config.tr_arg = arg;
2395 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2399 xptsetasyncfunc(struct cam_ed *device, void *arg)
2401 struct cam_path path;
2402 struct ccb_getdev cgd;
2403 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2406 * Don't report unconfigured devices (Wildcard devs,
2407 * devices only for target mode, device instances
2408 * that have been invalidated but are waiting for
2409 * their last reference count to be released).
2411 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2414 xpt_compile_path(&path,
2416 device->target->bus->path_id,
2417 device->target->target_id,
2419 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2420 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2421 xpt_action((union ccb *)&cgd);
2422 csa->callback(csa->callback_arg,
2425 xpt_release_path(&path);
2431 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2433 struct cam_path path;
2434 struct ccb_pathinq cpi;
2435 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2437 xpt_compile_path(&path, /*periph*/NULL,
2439 CAM_TARGET_WILDCARD,
2441 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2442 cpi.ccb_h.func_code = XPT_PATH_INQ;
2443 xpt_action((union ccb *)&cpi);
2444 csa->callback(csa->callback_arg,
2447 xpt_release_path(&path);
2453 xpt_action(union ccb *start_ccb)
2456 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2458 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2459 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2463 xpt_action_default(union ccb *start_ccb)
2465 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2466 struct cam_path *path;
2468 path = start_ccb->ccb_h.path;
2469 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2471 switch (start_ccb->ccb_h.func_code) {
2474 struct cam_ed *device;
2477 * For the sake of compatibility with SCSI-1
2478 * devices that may not understand the identify
2479 * message, we include lun information in the
2480 * second byte of all commands. SCSI-1 specifies
2481 * that luns are a 3 bit value and reserves only 3
2482 * bits for lun information in the CDB. Later
2483 * revisions of the SCSI spec allow for more than 8
2484 * luns, but have deprecated lun information in the
2485 * CDB. So, if the lun won't fit, we must omit.
2487 * Also be aware that during initial probing for devices,
2488 * the inquiry information is unknown but initialized to 0.
2489 * This means that this code will be exercised while probing
2490 * devices with an ANSI revision greater than 2.
2492 device = path->device;
2493 if (device->protocol_version <= SCSI_REV_2
2494 && start_ccb->ccb_h.target_lun < 8
2495 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2497 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2498 start_ccb->ccb_h.target_lun << 5;
2500 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2501 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2502 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2503 &path->device->inq_data),
2504 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2505 cdb_str, sizeof(cdb_str))));
2509 case XPT_CONT_TARGET_IO:
2510 start_ccb->csio.sense_resid = 0;
2511 start_ccb->csio.resid = 0;
2514 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2515 start_ccb->ataio.resid = 0;
2516 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2517 ata_op_string(&start_ccb->ataio.cmd),
2518 ata_cmd_string(&start_ccb->ataio.cmd,
2519 cdb_str, sizeof(cdb_str))));
2528 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2529 path->device->sim->devq->alloc_openings += frozen;
2531 xpt_run_dev_allocq(path->bus);
2532 if (xpt_schedule_dev_sendq(path->bus, path->device))
2533 xpt_run_dev_sendq(path->bus);
2536 case XPT_CALC_GEOMETRY:
2538 struct cam_sim *sim;
2540 /* Filter out garbage */
2541 if (start_ccb->ccg.block_size == 0
2542 || start_ccb->ccg.volume_size == 0) {
2543 start_ccb->ccg.cylinders = 0;
2544 start_ccb->ccg.heads = 0;
2545 start_ccb->ccg.secs_per_track = 0;
2546 start_ccb->ccb_h.status = CAM_REQ_CMP;
2549 #if defined(PC98) || defined(__sparc64__)
2551 * In a PC-98 system, geometry translation depens on
2552 * the "real" device geometry obtained from mode page 4.
2553 * SCSI geometry translation is performed in the
2554 * initialization routine of the SCSI BIOS and the result
2555 * stored in host memory. If the translation is available
2556 * in host memory, use it. If not, rely on the default
2557 * translation the device driver performs.
2558 * For sparc64, we may need adjust the geometry of large
2559 * disks in order to fit the limitations of the 16-bit
2560 * fields of the VTOC8 disk label.
2562 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2563 start_ccb->ccb_h.status = CAM_REQ_CMP;
2567 sim = path->bus->sim;
2568 (*(sim->sim_action))(sim, start_ccb);
2573 union ccb* abort_ccb;
2575 abort_ccb = start_ccb->cab.abort_ccb;
2576 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2578 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2579 struct cam_ccbq *ccbq;
2580 struct cam_ed *device;
2582 device = abort_ccb->ccb_h.path->device;
2583 ccbq = &device->ccbq;
2584 device->sim->devq->alloc_openings -=
2585 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2586 abort_ccb->ccb_h.status =
2587 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2588 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2589 xpt_done(abort_ccb);
2590 start_ccb->ccb_h.status = CAM_REQ_CMP;
2593 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2594 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2596 * We've caught this ccb en route to
2597 * the SIM. Flag it for abort and the
2598 * SIM will do so just before starting
2599 * real work on the CCB.
2601 abort_ccb->ccb_h.status =
2602 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2603 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2604 start_ccb->ccb_h.status = CAM_REQ_CMP;
2608 if (XPT_FC_IS_QUEUED(abort_ccb)
2609 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2611 * It's already completed but waiting
2612 * for our SWI to get to it.
2614 start_ccb->ccb_h.status = CAM_UA_ABORT;
2618 * If we weren't able to take care of the abort request
2619 * in the XPT, pass the request down to the SIM for processing.
2623 case XPT_ACCEPT_TARGET_IO:
2625 case XPT_IMMED_NOTIFY:
2626 case XPT_NOTIFY_ACK:
2628 case XPT_IMMEDIATE_NOTIFY:
2629 case XPT_NOTIFY_ACKNOWLEDGE:
2630 case XPT_GET_SIM_KNOB:
2631 case XPT_SET_SIM_KNOB:
2633 struct cam_sim *sim;
2635 sim = path->bus->sim;
2636 (*(sim->sim_action))(sim, start_ccb);
2641 struct cam_sim *sim;
2643 sim = path->bus->sim;
2644 (*(sim->sim_action))(sim, start_ccb);
2647 case XPT_PATH_STATS:
2648 start_ccb->cpis.last_reset = path->bus->last_reset;
2649 start_ccb->ccb_h.status = CAM_REQ_CMP;
2656 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2657 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2659 struct ccb_getdev *cgd;
2661 cgd = &start_ccb->cgd;
2662 cgd->protocol = dev->protocol;
2663 cgd->inq_data = dev->inq_data;
2664 cgd->ident_data = dev->ident_data;
2665 cgd->inq_flags = dev->inq_flags;
2666 cgd->ccb_h.status = CAM_REQ_CMP;
2667 cgd->serial_num_len = dev->serial_num_len;
2668 if ((dev->serial_num_len > 0)
2669 && (dev->serial_num != NULL))
2670 bcopy(dev->serial_num, cgd->serial_num,
2671 dev->serial_num_len);
2675 case XPT_GDEV_STATS:
2680 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2681 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2683 struct ccb_getdevstats *cgds;
2687 cgds = &start_ccb->cgds;
2690 cgds->dev_openings = dev->ccbq.dev_openings;
2691 cgds->dev_active = dev->ccbq.dev_active;
2692 cgds->devq_openings = dev->ccbq.devq_openings;
2693 cgds->devq_queued = dev->ccbq.queue.entries;
2694 cgds->held = dev->ccbq.held;
2695 cgds->last_reset = tar->last_reset;
2696 cgds->maxtags = dev->maxtags;
2697 cgds->mintags = dev->mintags;
2698 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2699 cgds->last_reset = bus->last_reset;
2700 cgds->ccb_h.status = CAM_REQ_CMP;
2706 struct cam_periph *nperiph;
2707 struct periph_list *periph_head;
2708 struct ccb_getdevlist *cgdl;
2710 struct cam_ed *device;
2717 * Don't want anyone mucking with our data.
2719 device = path->device;
2720 periph_head = &device->periphs;
2721 cgdl = &start_ccb->cgdl;
2724 * Check and see if the list has changed since the user
2725 * last requested a list member. If so, tell them that the
2726 * list has changed, and therefore they need to start over
2727 * from the beginning.
2729 if ((cgdl->index != 0) &&
2730 (cgdl->generation != device->generation)) {
2731 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2736 * Traverse the list of peripherals and attempt to find
2737 * the requested peripheral.
2739 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2740 (nperiph != NULL) && (i <= cgdl->index);
2741 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2742 if (i == cgdl->index) {
2743 strncpy(cgdl->periph_name,
2744 nperiph->periph_name,
2746 cgdl->unit_number = nperiph->unit_number;
2751 cgdl->status = CAM_GDEVLIST_ERROR;
2755 if (nperiph == NULL)
2756 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2758 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2761 cgdl->generation = device->generation;
2763 cgdl->ccb_h.status = CAM_REQ_CMP;
2768 dev_pos_type position_type;
2769 struct ccb_dev_match *cdm;
2771 cdm = &start_ccb->cdm;
2774 * There are two ways of getting at information in the EDT.
2775 * The first way is via the primary EDT tree. It starts
2776 * with a list of busses, then a list of targets on a bus,
2777 * then devices/luns on a target, and then peripherals on a
2778 * device/lun. The "other" way is by the peripheral driver
2779 * lists. The peripheral driver lists are organized by
2780 * peripheral driver. (obviously) So it makes sense to
2781 * use the peripheral driver list if the user is looking
2782 * for something like "da1", or all "da" devices. If the
2783 * user is looking for something on a particular bus/target
2784 * or lun, it's generally better to go through the EDT tree.
2787 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2788 position_type = cdm->pos.position_type;
2792 position_type = CAM_DEV_POS_NONE;
2794 for (i = 0; i < cdm->num_patterns; i++) {
2795 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2796 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2797 position_type = CAM_DEV_POS_EDT;
2802 if (cdm->num_patterns == 0)
2803 position_type = CAM_DEV_POS_EDT;
2804 else if (position_type == CAM_DEV_POS_NONE)
2805 position_type = CAM_DEV_POS_PDRV;
2809 * Note that we drop the SIM lock here, because the EDT
2810 * traversal code needs to do its own locking.
2812 CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
2813 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2814 case CAM_DEV_POS_EDT:
2817 case CAM_DEV_POS_PDRV:
2818 xptperiphlistmatch(cdm);
2821 cdm->status = CAM_DEV_MATCH_ERROR;
2824 CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
2826 if (cdm->status == CAM_DEV_MATCH_ERROR)
2827 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2829 start_ccb->ccb_h.status = CAM_REQ_CMP;
2835 struct ccb_setasync *csa;
2836 struct async_node *cur_entry;
2837 struct async_list *async_head;
2840 csa = &start_ccb->csa;
2841 added = csa->event_enable;
2842 async_head = &path->device->asyncs;
2845 * If there is already an entry for us, simply
2848 cur_entry = SLIST_FIRST(async_head);
2849 while (cur_entry != NULL) {
2850 if ((cur_entry->callback_arg == csa->callback_arg)
2851 && (cur_entry->callback == csa->callback))
2853 cur_entry = SLIST_NEXT(cur_entry, links);
2856 if (cur_entry != NULL) {
2858 * If the request has no flags set,
2861 added &= ~cur_entry->event_enable;
2862 if (csa->event_enable == 0) {
2863 SLIST_REMOVE(async_head, cur_entry,
2865 xpt_release_device(path->device);
2866 free(cur_entry, M_CAMXPT);
2868 cur_entry->event_enable = csa->event_enable;
2870 csa->event_enable = added;
2872 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2874 if (cur_entry == NULL) {
2875 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2878 cur_entry->event_enable = csa->event_enable;
2879 cur_entry->callback_arg = csa->callback_arg;
2880 cur_entry->callback = csa->callback;
2881 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2882 xpt_acquire_device(path->device);
2884 start_ccb->ccb_h.status = CAM_REQ_CMP;
2889 struct ccb_relsim *crs;
2892 crs = &start_ccb->crs;
2896 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2900 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2902 /* Don't ever go below one opening */
2903 if (crs->openings > 0) {
2904 xpt_dev_ccbq_resize(path, crs->openings);
2907 "number of openings is now %d\n",
2913 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2915 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2918 * Just extend the old timeout and decrement
2919 * the freeze count so that a single timeout
2920 * is sufficient for releasing the queue.
2922 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2923 callout_stop(&dev->callout);
2926 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2929 callout_reset(&dev->callout,
2930 (crs->release_timeout * hz) / 1000,
2931 xpt_release_devq_timeout, dev);
2933 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2937 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2939 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2941 * Decrement the freeze count so that a single
2942 * completion is still sufficient to unfreeze
2945 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2948 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2949 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2953 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2955 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2956 || (dev->ccbq.dev_active == 0)) {
2958 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2961 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2962 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2966 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2967 xpt_release_devq_rl(path, /*runlevel*/
2968 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2969 crs->release_timeout : 0,
2970 /*count*/1, /*run_queue*/TRUE);
2972 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2973 start_ccb->ccb_h.status = CAM_REQ_CMP;
2977 struct cam_path *oldpath;
2978 struct cam_sim *oldsim;
2980 /* Check that all request bits are supported. */
2981 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2982 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2986 cam_dflags = CAM_DEBUG_NONE;
2987 if (cam_dpath != NULL) {
2988 /* To release the old path we must hold proper lock. */
2989 oldpath = cam_dpath;
2991 oldsim = xpt_path_sim(oldpath);
2992 CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
2993 CAM_SIM_LOCK(oldsim);
2994 xpt_free_path(oldpath);
2995 CAM_SIM_UNLOCK(oldsim);
2996 CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
2998 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2999 if (xpt_create_path(&cam_dpath, xpt_periph,
3000 start_ccb->ccb_h.path_id,
3001 start_ccb->ccb_h.target_id,
3002 start_ccb->ccb_h.target_lun) !=
3004 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3006 cam_dflags = start_ccb->cdbg.flags;
3007 start_ccb->ccb_h.status = CAM_REQ_CMP;
3008 xpt_print(cam_dpath, "debugging flags now %x\n",
3012 start_ccb->ccb_h.status = CAM_REQ_CMP;
3015 case XPT_FREEZE_QUEUE:
3017 struct ccb_relsim *crs = &start_ccb->crs;
3019 xpt_freeze_devq_rl(path, /*runlevel*/
3020 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3021 crs->release_timeout : 0, /*count*/1);
3022 start_ccb->ccb_h.status = CAM_REQ_CMP;
3026 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3027 xpt_freeze_devq(path, 1);
3028 start_ccb->ccb_h.status = CAM_REQ_CMP;
3035 printf("%s: CCB type %#x not supported\n", __func__,
3036 start_ccb->ccb_h.func_code);
3037 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3038 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3039 xpt_done(start_ccb);
3046 xpt_polled_action(union ccb *start_ccb)
3049 struct cam_sim *sim;
3050 struct cam_devq *devq;
3054 timeout = start_ccb->ccb_h.timeout * 10;
3055 sim = start_ccb->ccb_h.path->bus->sim;
3057 dev = start_ccb->ccb_h.path->device;
3059 mtx_assert(sim->mtx, MA_OWNED);
3061 /* Don't use ISR for this SIM while polling. */
3062 sim->flags |= CAM_SIM_POLLED;
3065 * Steal an opening so that no other queued requests
3066 * can get it before us while we simulate interrupts.
3068 dev->ccbq.devq_openings--;
3069 dev->ccbq.dev_openings--;
3071 while(((devq != NULL && devq->send_openings <= 0) ||
3072 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3074 (*(sim->sim_poll))(sim);
3075 camisr_runqueue(&sim->sim_doneq);
3078 dev->ccbq.devq_openings++;
3079 dev->ccbq.dev_openings++;
3082 xpt_action(start_ccb);
3083 while(--timeout > 0) {
3084 (*(sim->sim_poll))(sim);
3085 camisr_runqueue(&sim->sim_doneq);
3086 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3093 * XXX Is it worth adding a sim_timeout entry
3094 * point so we can attempt recovery? If
3095 * this is only used for dumps, I don't think
3098 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3101 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3104 /* We will use CAM ISR for this SIM again. */
3105 sim->flags &= ~CAM_SIM_POLLED;
3109 * Schedule a peripheral driver to receive a ccb when it's
3110 * target device has space for more transactions.
3113 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3115 struct cam_ed *device;
3118 mtx_assert(perph->sim->mtx, MA_OWNED);
3120 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3121 device = perph->path->device;
3122 if (periph_is_queued(perph)) {
3123 /* Simply reorder based on new priority */
3124 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3125 (" change priority to %d\n", new_priority));
3126 if (new_priority < perph->pinfo.priority) {
3127 camq_change_priority(&device->drvq,
3130 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3133 /* New entry on the queue */
3134 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3135 (" added periph to queue\n"));
3136 perph->pinfo.priority = new_priority;
3137 perph->pinfo.generation = ++device->drvq.generation;
3138 camq_insert(&device->drvq, &perph->pinfo);
3139 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3142 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3143 (" calling xpt_run_devq\n"));
3144 xpt_run_dev_allocq(perph->path->bus);
3150 * Schedule a device to run on a given queue.
3151 * If the device was inserted as a new entry on the queue,
3152 * return 1 meaning the device queue should be run. If we
3153 * were already queued, implying someone else has already
3154 * started the queue, return 0 so the caller doesn't attempt
3158 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3159 u_int32_t new_priority)
3162 u_int32_t old_priority;
3164 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3166 old_priority = pinfo->priority;
3169 * Are we already queued?
3171 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3172 /* Simply reorder based on new priority */
3173 if (new_priority < old_priority) {
3174 camq_change_priority(queue, pinfo->index,
3176 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3177 ("changed priority to %d\n",
3183 /* New entry on the queue */
3184 if (new_priority < old_priority)
3185 pinfo->priority = new_priority;
3187 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3188 ("Inserting onto queue\n"));
3189 pinfo->generation = ++queue->generation;
3190 camq_insert(queue, pinfo);
3197 xpt_run_dev_allocq(struct cam_eb *bus)
3199 struct cam_devq *devq;
3201 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3202 devq = bus->sim->devq;
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3205 (" qfrozen_cnt == 0x%x, entries == %d, "
3206 "openings == %d, active == %d\n",
3207 devq->alloc_queue.qfrozen_cnt[0],
3208 devq->alloc_queue.entries,
3209 devq->alloc_openings,
3210 devq->alloc_active));
3212 devq->alloc_queue.qfrozen_cnt[0]++;
3213 while ((devq->alloc_queue.entries > 0)
3214 && (devq->alloc_openings > 0)
3215 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3216 struct cam_ed_qinfo *qinfo;
3217 struct cam_ed *device;
3218 union ccb *work_ccb;
3219 struct cam_periph *drv;
3222 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3224 device = qinfo->device;
3225 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3226 ("running device %p\n", device));
3228 drvq = &device->drvq;
3229 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3230 "Device on queue without any work to do"));
3231 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3232 devq->alloc_openings--;
3233 devq->alloc_active++;
3234 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3235 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3236 drv->pinfo.priority);
3237 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3238 ("calling periph start\n"));
3239 drv->periph_start(drv, work_ccb);
3242 * Malloc failure in alloc_ccb
3245 * XXX add us to a list to be run from free_ccb
3246 * if we don't have any ccbs active on this
3247 * device queue otherwise we may never get run
3253 /* We may have more work. Attempt to reschedule. */
3254 xpt_schedule_dev_allocq(bus, device);
3256 devq->alloc_queue.qfrozen_cnt[0]--;
3260 xpt_run_dev_sendq(struct cam_eb *bus)
3262 struct cam_devq *devq;
3264 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3266 devq = bus->sim->devq;
3268 devq->send_queue.qfrozen_cnt[0]++;
3269 while ((devq->send_queue.entries > 0)
3270 && (devq->send_openings > 0)
3271 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3272 struct cam_ed_qinfo *qinfo;
3273 struct cam_ed *device;
3274 union ccb *work_ccb;
3275 struct cam_sim *sim;
3277 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3279 device = qinfo->device;
3280 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3281 ("running device %p\n", device));
3283 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3284 if (work_ccb == NULL) {
3285 printf("device on run queue with no ccbs???\n");
3289 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3291 mtx_lock(&xsoftc.xpt_lock);
3292 if (xsoftc.num_highpower <= 0) {
3294 * We got a high power command, but we
3295 * don't have any available slots. Freeze
3296 * the device queue until we have a slot
3299 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3300 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3304 mtx_unlock(&xsoftc.xpt_lock);
3308 * Consume a high power slot while
3311 xsoftc.num_highpower--;
3313 mtx_unlock(&xsoftc.xpt_lock);
3315 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3316 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3318 devq->send_openings--;
3319 devq->send_active++;
3321 xpt_schedule_dev_sendq(bus, device);
3323 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3325 * The client wants to freeze the queue
3326 * after this CCB is sent.
3328 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3331 /* In Target mode, the peripheral driver knows best... */
3332 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3333 if ((device->inq_flags & SID_CmdQue) != 0
3334 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3335 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3338 * Clear this in case of a retried CCB that
3339 * failed due to a rejected tag.
3341 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3345 * Device queues can be shared among multiple sim instances
3346 * that reside on different busses. Use the SIM in the queue
3347 * CCB's path, rather than the one in the bus that was passed
3348 * into this function.
3350 sim = work_ccb->ccb_h.path->bus->sim;
3351 (*(sim->sim_action))(sim, work_ccb);
3353 devq->send_queue.qfrozen_cnt[0]--;
3357 * This function merges stuff from the slave ccb into the master ccb, while
3358 * keeping important fields in the master ccb constant.
3361 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3365 * Pull fields that are valid for peripheral drivers to set
3366 * into the master CCB along with the CCB "payload".
3368 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3369 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3370 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3371 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3372 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3373 sizeof(union ccb) - sizeof(struct ccb_hdr));
3377 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3380 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3381 ccb_h->pinfo.priority = priority;
3383 ccb_h->path_id = path->bus->path_id;
3385 ccb_h->target_id = path->target->target_id;
3387 ccb_h->target_id = CAM_TARGET_WILDCARD;
3389 ccb_h->target_lun = path->device->lun_id;
3390 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3392 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3394 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3398 /* Path manipulation functions */
3400 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3401 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3403 struct cam_path *path;
3406 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3409 status = CAM_RESRC_UNAVAIL;
3412 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3413 if (status != CAM_REQ_CMP) {
3414 free(path, M_CAMPATH);
3417 *new_path_ptr = path;
3422 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3423 struct cam_periph *periph, path_id_t path_id,
3424 target_id_t target_id, lun_id_t lun_id)
3426 struct cam_path *path;
3427 struct cam_eb *bus = NULL;
3430 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
3432 bus = xpt_find_bus(path_id);
3434 CAM_SIM_LOCK(bus->sim);
3435 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3437 CAM_SIM_UNLOCK(bus->sim);
3438 xpt_release_bus(bus);
3440 if (status != CAM_REQ_CMP) {
3441 free(path, M_CAMPATH);
3444 *new_path_ptr = path;
3449 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3450 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3453 struct cam_et *target;
3454 struct cam_ed *device;
3457 status = CAM_REQ_CMP; /* Completed without error */
3458 target = NULL; /* Wildcarded */
3459 device = NULL; /* Wildcarded */
3462 * We will potentially modify the EDT, so block interrupts
3463 * that may attempt to create cam paths.
3465 bus = xpt_find_bus(path_id);
3467 status = CAM_PATH_INVALID;
3469 target = xpt_find_target(bus, target_id);
3470 if (target == NULL) {
3472 struct cam_et *new_target;
3474 new_target = xpt_alloc_target(bus, target_id);
3475 if (new_target == NULL) {
3476 status = CAM_RESRC_UNAVAIL;
3478 target = new_target;
3481 if (target != NULL) {
3482 device = xpt_find_device(target, lun_id);
3483 if (device == NULL) {
3485 struct cam_ed *new_device;
3488 (*(bus->xport->alloc_device))(bus,
3491 if (new_device == NULL) {
3492 status = CAM_RESRC_UNAVAIL;
3494 device = new_device;
3501 * Only touch the user's data if we are successful.
3503 if (status == CAM_REQ_CMP) {
3504 new_path->periph = perph;
3505 new_path->bus = bus;
3506 new_path->target = target;
3507 new_path->device = device;
3508 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3511 xpt_release_device(device);
3513 xpt_release_target(target);
3515 xpt_release_bus(bus);
3521 xpt_release_path(struct cam_path *path)
3523 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3524 if (path->device != NULL) {
3525 xpt_release_device(path->device);
3526 path->device = NULL;
3528 if (path->target != NULL) {
3529 xpt_release_target(path->target);
3530 path->target = NULL;
3532 if (path->bus != NULL) {
3533 xpt_release_bus(path->bus);
3539 xpt_free_path(struct cam_path *path)
3542 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3543 xpt_release_path(path);
3544 free(path, M_CAMPATH);
3548 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3549 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3552 mtx_lock(&xsoftc.xpt_topo_lock);
3555 *bus_ref = path->bus->refcount;
3559 mtx_unlock(&xsoftc.xpt_topo_lock);
3562 *periph_ref = path->periph->refcount;
3568 *target_ref = path->target->refcount;
3574 *device_ref = path->device->refcount;
3581 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3582 * in path1, 2 for match with wildcards in path2.
3585 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3589 if (path1->bus != path2->bus) {
3590 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3592 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3597 if (path1->target != path2->target) {
3598 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3601 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3606 if (path1->device != path2->device) {
3607 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3610 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3619 xpt_print_path(struct cam_path *path)
3623 printf("(nopath): ");
3625 if (path->periph != NULL)
3626 printf("(%s%d:", path->periph->periph_name,
3627 path->periph->unit_number);
3629 printf("(noperiph:");
3631 if (path->bus != NULL)
3632 printf("%s%d:%d:", path->bus->sim->sim_name,
3633 path->bus->sim->unit_number,
3634 path->bus->sim->bus_id);
3638 if (path->target != NULL)
3639 printf("%d:", path->target->target_id);
3643 if (path->device != NULL)
3644 printf("%d): ", path->device->lun_id);
3651 xpt_print(struct cam_path *path, const char *fmt, ...)
3654 xpt_print_path(path);
3661 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3666 if (path != NULL && path->bus != NULL)
3667 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3670 sbuf_new(&sb, str, str_len, 0);
3673 sbuf_printf(&sb, "(nopath): ");
3675 if (path->periph != NULL)
3676 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3677 path->periph->unit_number);
3679 sbuf_printf(&sb, "(noperiph:");
3681 if (path->bus != NULL)
3682 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3683 path->bus->sim->unit_number,
3684 path->bus->sim->bus_id);
3686 sbuf_printf(&sb, "nobus:");
3688 if (path->target != NULL)
3689 sbuf_printf(&sb, "%d:", path->target->target_id);
3691 sbuf_printf(&sb, "X:");
3693 if (path->device != NULL)
3694 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3696 sbuf_printf(&sb, "X): ");
3700 return(sbuf_len(&sb));
3704 xpt_path_path_id(struct cam_path *path)
3706 return(path->bus->path_id);
3710 xpt_path_target_id(struct cam_path *path)
3712 if (path->target != NULL)
3713 return (path->target->target_id);
3715 return (CAM_TARGET_WILDCARD);
3719 xpt_path_lun_id(struct cam_path *path)
3721 if (path->device != NULL)
3722 return (path->device->lun_id);
3724 return (CAM_LUN_WILDCARD);
3728 xpt_path_sim(struct cam_path *path)
3731 return (path->bus->sim);
3735 xpt_path_periph(struct cam_path *path)
3737 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3739 return (path->periph);
3743 xpt_path_legacy_ata_id(struct cam_path *path)
3748 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3749 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3750 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3751 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3754 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3755 path->bus->sim->unit_number < 2) {
3756 bus_id = path->bus->sim->unit_number;
3760 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3761 if (bus == path->bus)
3763 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3764 bus->sim->unit_number >= 2) ||
3765 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3766 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3767 strcmp(bus->sim->sim_name, "siisch") == 0)
3772 if (path->target != NULL) {
3773 if (path->target->target_id < 2)
3774 return (bus_id * 2 + path->target->target_id);
3778 return (bus_id * 2);
3782 * Release a CAM control block for the caller. Remit the cost of the structure
3783 * to the device referenced by the path. If the this device had no 'credits'
3784 * and peripheral drivers have registered async callbacks for this notification
3788 xpt_release_ccb(union ccb *free_ccb)
3790 struct cam_path *path;
3791 struct cam_ed *device;
3793 struct cam_sim *sim;
3795 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3796 path = free_ccb->ccb_h.path;
3797 device = path->device;
3801 mtx_assert(sim->mtx, MA_OWNED);
3803 cam_ccbq_release_opening(&device->ccbq);
3804 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3805 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3806 cam_ccbq_resize(&device->ccbq,
3807 device->ccbq.dev_openings + device->ccbq.dev_active);
3809 if (sim->ccb_count > sim->max_ccbs) {
3810 xpt_free_ccb(free_ccb);
3813 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3816 if (sim->devq == NULL) {
3819 sim->devq->alloc_openings++;
3820 sim->devq->alloc_active--;
3821 if (device_is_alloc_queued(device) == 0)
3822 xpt_schedule_dev_allocq(bus, device);
3823 xpt_run_dev_allocq(bus);
3826 /* Functions accessed by SIM drivers */
3828 static struct xpt_xport xport_default = {
3829 .alloc_device = xpt_alloc_device_default,
3830 .action = xpt_action_default,
3831 .async = xpt_dev_async_default,
3835 * A sim structure, listing the SIM entry points and instance
3836 * identification info is passed to xpt_bus_register to hook the SIM
3837 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3838 * for this new bus and places it in the array of busses and assigns
3839 * it a path_id. The path_id may be influenced by "hard wiring"
3840 * information specified by the user. Once interrupt services are
3841 * available, the bus will be probed.
3844 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3846 struct cam_eb *new_bus;
3847 struct cam_eb *old_bus;
3848 struct ccb_pathinq cpi;
3849 struct cam_path *path;
3852 mtx_assert(sim->mtx, MA_OWNED);
3855 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3856 M_CAMXPT, M_NOWAIT);
3857 if (new_bus == NULL) {
3858 /* Couldn't satisfy request */
3859 return (CAM_RESRC_UNAVAIL);
3861 if (strcmp(sim->sim_name, "xpt") != 0) {
3863 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3866 TAILQ_INIT(&new_bus->et_entries);
3867 new_bus->path_id = sim->path_id;
3870 timevalclear(&new_bus->last_reset);
3872 new_bus->refcount = 1; /* Held until a bus_deregister event */
3873 new_bus->generation = 0;
3875 mtx_lock(&xsoftc.xpt_topo_lock);
3876 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3877 while (old_bus != NULL
3878 && old_bus->path_id < new_bus->path_id)
3879 old_bus = TAILQ_NEXT(old_bus, links);
3880 if (old_bus != NULL)
3881 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3883 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3884 xsoftc.bus_generation++;
3885 mtx_unlock(&xsoftc.xpt_topo_lock);
3888 * Set a default transport so that a PATH_INQ can be issued to
3889 * the SIM. This will then allow for probing and attaching of
3890 * a more appropriate transport.
3892 new_bus->xport = &xport_default;
3894 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3895 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3896 if (status != CAM_REQ_CMP) {
3897 xpt_release_bus(new_bus);
3898 free(path, M_CAMXPT);
3899 return (CAM_RESRC_UNAVAIL);
3902 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3903 cpi.ccb_h.func_code = XPT_PATH_INQ;
3904 xpt_action((union ccb *)&cpi);
3906 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3907 switch (cpi.transport) {
3914 new_bus->xport = scsi_get_xport();
3918 new_bus->xport = ata_get_xport();
3921 new_bus->xport = &xport_default;
3926 /* Notify interested parties */
3927 if (sim->path_id != CAM_XPT_PATH_ID) {
3928 union ccb *scan_ccb;
3930 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3931 /* Initiate bus rescan. */
3932 scan_ccb = xpt_alloc_ccb_nowait();
3933 scan_ccb->ccb_h.path = path;
3934 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3935 scan_ccb->crcn.flags = 0;
3936 xpt_rescan(scan_ccb);
3938 xpt_free_path(path);
3939 return (CAM_SUCCESS);
3943 xpt_bus_deregister(path_id_t pathid)
3945 struct cam_path bus_path;
3948 status = xpt_compile_path(&bus_path, NULL, pathid,
3949 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3950 if (status != CAM_REQ_CMP)
3953 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3954 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3956 /* Release the reference count held while registered. */
3957 xpt_release_bus(bus_path.bus);
3958 xpt_release_path(&bus_path);
3960 return (CAM_REQ_CMP);
3964 xptnextfreepathid(void)
3971 mtx_lock(&xsoftc.xpt_topo_lock);
3972 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3974 /* Find an unoccupied pathid */
3975 while (bus != NULL && bus->path_id <= pathid) {
3976 if (bus->path_id == pathid)
3978 bus = TAILQ_NEXT(bus, links);
3980 mtx_unlock(&xsoftc.xpt_topo_lock);
3983 * Ensure that this pathid is not reserved for
3984 * a bus that may be registered in the future.
3986 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3988 /* Start the search over */
3989 mtx_lock(&xsoftc.xpt_topo_lock);
3996 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4003 pathid = CAM_XPT_PATH_ID;
4004 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4006 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4007 if (strcmp(dname, "scbus")) {
4008 /* Avoid a bit of foot shooting. */
4011 if (dunit < 0) /* unwired?! */
4013 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4014 if (sim_bus == val) {
4018 } else if (sim_bus == 0) {
4019 /* Unspecified matches bus 0 */
4023 printf("Ambiguous scbus configuration for %s%d "
4024 "bus %d, cannot wire down. The kernel "
4025 "config entry for scbus%d should "
4026 "specify a controller bus.\n"
4027 "Scbus will be assigned dynamically.\n",
4028 sim_name, sim_unit, sim_bus, dunit);
4033 if (pathid == CAM_XPT_PATH_ID)
4034 pathid = xptnextfreepathid();
4039 xpt_async_string(u_int32_t async_code)
4042 switch (async_code) {
4043 case AC_BUS_RESET: return ("AC_BUS_RESET");
4044 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4045 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4046 case AC_SENT_BDR: return ("AC_SENT_BDR");
4047 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4048 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4049 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4050 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4051 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4052 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4053 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4054 case AC_CONTRACT: return ("AC_CONTRACT");
4055 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4057 return ("AC_UNKNOWN");
4061 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4064 struct cam_et *target, *next_target;
4065 struct cam_ed *device, *next_device;
4067 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4068 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4069 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4072 * Most async events come from a CAM interrupt context. In
4073 * a few cases, the error recovery code at the peripheral layer,
4074 * which may run from our SWI or a process context, may signal
4075 * deferred events with a call to xpt_async.
4080 if (async_code == AC_BUS_RESET) {
4081 /* Update our notion of when the last reset occurred */
4082 microtime(&bus->last_reset);
4085 for (target = TAILQ_FIRST(&bus->et_entries);
4087 target = next_target) {
4089 next_target = TAILQ_NEXT(target, links);
4091 if (path->target != target
4092 && path->target->target_id != CAM_TARGET_WILDCARD
4093 && target->target_id != CAM_TARGET_WILDCARD)
4096 if (async_code == AC_SENT_BDR) {
4097 /* Update our notion of when the last reset occurred */
4098 microtime(&path->target->last_reset);
4101 for (device = TAILQ_FIRST(&target->ed_entries);
4103 device = next_device) {
4105 next_device = TAILQ_NEXT(device, links);
4107 if (path->device != device
4108 && path->device->lun_id != CAM_LUN_WILDCARD
4109 && device->lun_id != CAM_LUN_WILDCARD)
4112 * The async callback could free the device.
4113 * If it is a broadcast async, it doesn't hold
4114 * device reference, so take our own reference.
4116 xpt_acquire_device(device);
4117 (*(bus->xport->async))(async_code, bus,
4121 xpt_async_bcast(&device->asyncs, async_code,
4123 xpt_release_device(device);
4128 * If this wasn't a fully wildcarded async, tell all
4129 * clients that want all async events.
4131 if (bus != xpt_periph->path->bus)
4132 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4137 xpt_async_bcast(struct async_list *async_head,
4138 u_int32_t async_code,
4139 struct cam_path *path, void *async_arg)
4141 struct async_node *cur_entry;
4143 cur_entry = SLIST_FIRST(async_head);
4144 while (cur_entry != NULL) {
4145 struct async_node *next_entry;
4147 * Grab the next list entry before we call the current
4148 * entry's callback. This is because the callback function
4149 * can delete its async callback entry.
4151 next_entry = SLIST_NEXT(cur_entry, links);
4152 if ((cur_entry->event_enable & async_code) != 0)
4153 cur_entry->callback(cur_entry->callback_arg,
4156 cur_entry = next_entry;
4161 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4162 struct cam_et *target, struct cam_ed *device,
4165 printf("%s called\n", __func__);
4169 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4171 struct cam_ed *dev = path->device;
4173 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4174 dev->sim->devq->alloc_openings +=
4175 cam_ccbq_freeze(&dev->ccbq, rl, count);
4176 /* Remove frozen device from allocq. */
4177 if (device_is_alloc_queued(dev) &&
4178 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4179 CAMQ_GET_PRIO(&dev->drvq)))) {
4180 camq_remove(&dev->sim->devq->alloc_queue,
4181 dev->alloc_ccb_entry.pinfo.index);
4183 /* Remove frozen device from sendq. */
4184 if (device_is_send_queued(dev) &&
4185 cam_ccbq_frozen_top(&dev->ccbq)) {
4186 camq_remove(&dev->sim->devq->send_queue,
4187 dev->send_ccb_entry.pinfo.index);
4189 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4193 xpt_freeze_devq(struct cam_path *path, u_int count)
4196 return (xpt_freeze_devq_rl(path, 0, count));
4200 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4203 mtx_assert(sim->mtx, MA_OWNED);
4204 sim->devq->send_queue.qfrozen_cnt[0] += count;
4205 return (sim->devq->send_queue.qfrozen_cnt[0]);
4209 xpt_release_devq_timeout(void *arg)
4211 struct cam_ed *device;
4213 device = (struct cam_ed *)arg;
4215 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4219 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4221 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4223 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4227 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4229 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4231 xpt_release_devq_device(path->device, rl, count, run_queue);
4235 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4238 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4240 printf("xpt_release_devq(%d): requested %u > present %u\n",
4241 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4243 count = dev->ccbq.queue.qfrozen_cnt[rl];
4245 dev->sim->devq->alloc_openings -=
4246 cam_ccbq_release(&dev->ccbq, rl, count);
4247 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4248 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4249 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4250 xpt_run_dev_allocq(dev->target->bus);
4252 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4254 * No longer need to wait for a successful
4255 * command completion.
4257 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4259 * Remove any timeouts that might be scheduled
4260 * to release this queue.
4262 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4263 callout_stop(&dev->callout);
4264 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4269 * Now that we are unfrozen schedule the
4270 * device so any pending transactions are
4273 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4274 xpt_run_dev_sendq(dev->target->bus);
4279 xpt_release_simq(struct cam_sim *sim, int run_queue)
4283 mtx_assert(sim->mtx, MA_OWNED);
4284 sendq = &(sim->devq->send_queue);
4285 if (sendq->qfrozen_cnt[0] <= 0) {
4287 printf("xpt_release_simq: requested 1 > present %u\n",
4288 sendq->qfrozen_cnt[0]);
4291 sendq->qfrozen_cnt[0]--;
4292 if (sendq->qfrozen_cnt[0] == 0) {
4294 * If there is a timeout scheduled to release this
4295 * sim queue, remove it. The queue frozen count is
4298 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4299 callout_stop(&sim->callout);
4300 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4306 * Now that we are unfrozen run the send queue.
4308 bus = xpt_find_bus(sim->path_id);
4309 xpt_run_dev_sendq(bus);
4310 xpt_release_bus(bus);
4316 * XXX Appears to be unused.
4319 xpt_release_simq_timeout(void *arg)
4321 struct cam_sim *sim;
4323 sim = (struct cam_sim *)arg;
4324 xpt_release_simq(sim, /* run_queue */ TRUE);
4328 xpt_done(union ccb *done_ccb)
4330 struct cam_sim *sim;
4333 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4334 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4336 * Queue up the request for handling by our SWI handler
4337 * any of the "non-immediate" type of ccbs.
4339 sim = done_ccb->ccb_h.path->bus->sim;
4340 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4342 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4343 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4344 CAM_SIM_BATCH)) == 0) {
4345 mtx_lock(&cam_simq_lock);
4346 first = TAILQ_EMPTY(&cam_simq);
4347 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4348 mtx_unlock(&cam_simq_lock);
4349 sim->flags |= CAM_SIM_ON_DONEQ;
4351 swi_sched(cambio_ih, 0);
4357 xpt_batch_start(struct cam_sim *sim)
4360 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4361 sim->flags |= CAM_SIM_BATCH;
4365 xpt_batch_done(struct cam_sim *sim)
4368 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4369 sim->flags &= ~CAM_SIM_BATCH;
4370 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4371 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4372 camisr_runqueue(&sim->sim_doneq);
4380 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4385 xpt_alloc_ccb_nowait()
4389 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4394 xpt_free_ccb(union ccb *free_ccb)
4396 free(free_ccb, M_CAMCCB);
4401 /* Private XPT functions */
4404 * Get a CAM control block for the caller. Charge the structure to the device
4405 * referenced by the path. If the this device has no 'credits' then the
4406 * device already has the maximum number of outstanding operations under way
4407 * and we return NULL. If we don't have sufficient resources to allocate more
4408 * ccbs, we also return NULL.
4411 xpt_get_ccb(struct cam_ed *device)
4414 struct cam_sim *sim;
4417 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4418 new_ccb = xpt_alloc_ccb_nowait();
4419 if (new_ccb == NULL) {
4422 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4423 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4424 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4428 cam_ccbq_take_opening(&device->ccbq);
4429 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4434 xpt_release_bus(struct cam_eb *bus)
4437 mtx_lock(&xsoftc.xpt_topo_lock);
4438 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4439 if ((--bus->refcount == 0)
4440 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4441 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4442 xsoftc.bus_generation++;
4443 mtx_unlock(&xsoftc.xpt_topo_lock);
4444 cam_sim_release(bus->sim);
4445 free(bus, M_CAMXPT);
4447 mtx_unlock(&xsoftc.xpt_topo_lock);
4450 static struct cam_et *
4451 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4453 struct cam_et *target;
4455 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4457 if (target != NULL) {
4458 struct cam_et *cur_target;
4460 TAILQ_INIT(&target->ed_entries);
4462 target->target_id = target_id;
4463 target->refcount = 1;
4464 target->generation = 0;
4465 target->luns = NULL;
4466 timevalclear(&target->last_reset);
4468 * Hold a reference to our parent bus so it
4469 * will not go away before we do.
4471 mtx_lock(&xsoftc.xpt_topo_lock);
4473 mtx_unlock(&xsoftc.xpt_topo_lock);
4475 /* Insertion sort into our bus's target list */
4476 cur_target = TAILQ_FIRST(&bus->et_entries);
4477 while (cur_target != NULL && cur_target->target_id < target_id)
4478 cur_target = TAILQ_NEXT(cur_target, links);
4480 if (cur_target != NULL) {
4481 TAILQ_INSERT_BEFORE(cur_target, target, links);
4483 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4491 xpt_release_target(struct cam_et *target)
4494 if (target->refcount == 1) {
4495 if (TAILQ_FIRST(&target->ed_entries) == NULL) {
4496 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4497 target->bus->generation++;
4498 xpt_release_bus(target->bus);
4500 free(target->luns, M_CAMXPT);
4501 free(target, M_CAMXPT);
4507 static struct cam_ed *
4508 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4511 struct cam_ed *device, *cur_device;
4513 device = xpt_alloc_device(bus, target, lun_id);
4517 device->mintags = 1;
4518 device->maxtags = 1;
4519 bus->sim->max_ccbs += device->ccbq.devq_openings;
4520 cur_device = TAILQ_FIRST(&target->ed_entries);
4521 while (cur_device != NULL && cur_device->lun_id < lun_id)
4522 cur_device = TAILQ_NEXT(cur_device, links);
4523 if (cur_device != NULL) {
4524 TAILQ_INSERT_BEFORE(cur_device, device, links);
4526 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4528 target->generation++;
4534 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4536 struct cam_ed *device;
4537 struct cam_devq *devq;
4540 /* Make space for us in the device queue on our bus */
4541 devq = bus->sim->devq;
4542 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4544 if (status != CAM_REQ_CMP) {
4547 device = (struct cam_ed *)malloc(sizeof(*device),
4548 M_CAMDEV, M_NOWAIT|M_ZERO);
4551 if (device != NULL) {
4552 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4553 device->alloc_ccb_entry.device = device;
4554 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4555 device->send_ccb_entry.device = device;
4556 device->target = target;
4557 device->lun_id = lun_id;
4558 device->sim = bus->sim;
4559 /* Initialize our queues */
4560 if (camq_init(&device->drvq, 0) != 0) {
4561 free(device, M_CAMDEV);
4564 if (cam_ccbq_init(&device->ccbq,
4565 bus->sim->max_dev_openings) != 0) {
4566 camq_fini(&device->drvq);
4567 free(device, M_CAMDEV);
4570 SLIST_INIT(&device->asyncs);
4571 SLIST_INIT(&device->periphs);
4572 device->generation = 0;
4573 device->owner = NULL;
4574 device->flags = CAM_DEV_UNCONFIGURED;
4575 device->tag_delay_count = 0;
4576 device->tag_saved_openings = 0;
4577 device->refcount = 1;
4578 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4581 * Hold a reference to our parent target so it
4582 * will not go away before we do.
4591 xpt_acquire_device(struct cam_ed *device)
4598 xpt_release_device(struct cam_ed *device)
4601 if (device->refcount == 1) {
4602 struct cam_devq *devq;
4604 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4605 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4606 panic("Removing device while still queued for ccbs");
4608 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4609 callout_stop(&device->callout);
4611 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4612 device->target->generation++;
4613 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4614 /* Release our slot in the devq */
4615 devq = device->target->bus->sim->devq;
4616 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4617 camq_fini(&device->drvq);
4618 cam_ccbq_fini(&device->ccbq);
4620 * Free allocated memory. free(9) does nothing if the
4621 * supplied pointer is NULL, so it is safe to call without
4624 free(device->supported_vpds, M_CAMXPT);
4625 free(device->device_id, M_CAMXPT);
4626 free(device->physpath, M_CAMXPT);
4627 free(device->rcap_buf, M_CAMXPT);
4628 free(device->serial_num, M_CAMXPT);
4630 xpt_release_target(device->target);
4631 free(device, M_CAMDEV);
4637 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4645 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4646 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4647 if (result == CAM_REQ_CMP && (diff < 0)) {
4648 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4650 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4651 || (dev->inq_flags & SID_CmdQue) != 0)
4652 dev->tag_saved_openings = newopenings;
4653 /* Adjust the global limit */
4654 dev->sim->max_ccbs += diff;
4658 static struct cam_eb *
4659 xpt_find_bus(path_id_t path_id)
4663 mtx_lock(&xsoftc.xpt_topo_lock);
4664 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4666 bus = TAILQ_NEXT(bus, links)) {
4667 if (bus->path_id == path_id) {
4672 mtx_unlock(&xsoftc.xpt_topo_lock);
4676 static struct cam_et *
4677 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4679 struct cam_et *target;
4681 for (target = TAILQ_FIRST(&bus->et_entries);
4683 target = TAILQ_NEXT(target, links)) {
4684 if (target->target_id == target_id) {
4692 static struct cam_ed *
4693 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4695 struct cam_ed *device;
4697 for (device = TAILQ_FIRST(&target->ed_entries);
4699 device = TAILQ_NEXT(device, links)) {
4700 if (device->lun_id == lun_id) {
4709 xpt_start_tags(struct cam_path *path)
4711 struct ccb_relsim crs;
4712 struct cam_ed *device;
4713 struct cam_sim *sim;
4716 device = path->device;
4717 sim = path->bus->sim;
4718 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4719 xpt_freeze_devq(path, /*count*/1);
4720 device->inq_flags |= SID_CmdQue;
4721 if (device->tag_saved_openings != 0)
4722 newopenings = device->tag_saved_openings;
4724 newopenings = min(device->maxtags,
4725 sim->max_tagged_dev_openings);
4726 xpt_dev_ccbq_resize(path, newopenings);
4727 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4728 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4729 crs.ccb_h.func_code = XPT_REL_SIMQ;
4730 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4732 = crs.release_timeout
4735 xpt_action((union ccb *)&crs);
4739 xpt_stop_tags(struct cam_path *path)
4741 struct ccb_relsim crs;
4742 struct cam_ed *device;
4743 struct cam_sim *sim;
4745 device = path->device;
4746 sim = path->bus->sim;
4747 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4748 device->tag_delay_count = 0;
4749 xpt_freeze_devq(path, /*count*/1);
4750 device->inq_flags &= ~SID_CmdQue;
4751 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4752 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4753 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4754 crs.ccb_h.func_code = XPT_REL_SIMQ;
4755 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4757 = crs.release_timeout
4760 xpt_action((union ccb *)&crs);
4764 xpt_boot_delay(void *arg)
4771 xpt_config(void *arg)
4774 * Now that interrupts are enabled, go find our devices
4777 /* Setup debugging path */
4778 if (cam_dflags != CAM_DEBUG_NONE) {
4779 if (xpt_create_path_unlocked(&cam_dpath, xpt_periph,
4780 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4781 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4782 printf("xpt_config: xpt_create_path() failed for debug"
4783 " target %d:%d:%d, debugging disabled\n",
4784 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4785 cam_dflags = CAM_DEBUG_NONE;
4790 periphdriver_init(1);
4792 callout_init(&xsoftc.boot_callout, 1);
4793 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4794 xpt_boot_delay, NULL);
4795 /* Fire up rescan thread. */
4796 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4797 printf("xpt_config: failed to create rescan thread.\n");
4805 xsoftc.buses_to_config++;
4810 xpt_release_boot(void)
4813 xsoftc.buses_to_config--;
4814 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4815 struct xpt_task *task;
4817 xsoftc.buses_config_done = 1;
4819 /* Call manually because we don't have any busses */
4820 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4822 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4823 taskqueue_enqueue(taskqueue_thread, &task->task);
4830 * If the given device only has one peripheral attached to it, and if that
4831 * peripheral is the passthrough driver, announce it. This insures that the
4832 * user sees some sort of announcement for every peripheral in their system.
4835 xptpassannouncefunc(struct cam_ed *device, void *arg)
4837 struct cam_periph *periph;
4840 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4841 periph = SLIST_NEXT(periph, periph_links), i++);
4843 periph = SLIST_FIRST(&device->periphs);
4845 && (strncmp(periph->periph_name, "pass", 4) == 0))
4846 xpt_announce_periph(periph, NULL);
4852 xpt_finishconfig_task(void *context, int pending)
4855 periphdriver_init(2);
4857 * Check for devices with no "standard" peripheral driver
4858 * attached. For any devices like that, announce the
4859 * passthrough driver so the user will see something.
4862 xpt_for_all_devices(xptpassannouncefunc, NULL);
4864 /* Release our hook so that the boot can continue. */
4865 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4866 free(xsoftc.xpt_config_hook, M_CAMXPT);
4867 xsoftc.xpt_config_hook = NULL;
4869 free(context, M_CAMXPT);
4873 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4874 struct cam_path *path)
4876 struct ccb_setasync csa;
4881 mtx_lock(&xsoftc.xpt_lock);
4882 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4883 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4884 if (status != CAM_REQ_CMP) {
4885 mtx_unlock(&xsoftc.xpt_lock);
4891 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4892 csa.ccb_h.func_code = XPT_SASYNC_CB;
4893 csa.event_enable = event;
4894 csa.callback = cbfunc;
4895 csa.callback_arg = cbarg;
4896 xpt_action((union ccb *)&csa);
4897 status = csa.ccb_h.status;
4900 xpt_free_path(path);
4901 mtx_unlock(&xsoftc.xpt_lock);
4904 if ((status == CAM_REQ_CMP) &&
4905 (csa.event_enable & AC_FOUND_DEVICE)) {
4907 * Get this peripheral up to date with all
4908 * the currently existing devices.
4910 xpt_for_all_devices(xptsetasyncfunc, &csa);
4912 if ((status == CAM_REQ_CMP) &&
4913 (csa.event_enable & AC_PATH_REGISTERED)) {
4915 * Get this peripheral up to date with all
4916 * the currently existing busses.
4918 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4925 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4927 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4929 switch (work_ccb->ccb_h.func_code) {
4930 /* Common cases first */
4931 case XPT_PATH_INQ: /* Path routing inquiry */
4933 struct ccb_pathinq *cpi;
4935 cpi = &work_ccb->cpi;
4936 cpi->version_num = 1; /* XXX??? */
4937 cpi->hba_inquiry = 0;
4938 cpi->target_sprt = 0;
4940 cpi->hba_eng_cnt = 0;
4941 cpi->max_target = 0;
4943 cpi->initiator_id = 0;
4944 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4945 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4946 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4947 cpi->unit_number = sim->unit_number;
4948 cpi->bus_id = sim->bus_id;
4949 cpi->base_transfer_speed = 0;
4950 cpi->protocol = PROTO_UNSPECIFIED;
4951 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4952 cpi->transport = XPORT_UNSPECIFIED;
4953 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4954 cpi->ccb_h.status = CAM_REQ_CMP;
4959 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4966 * The xpt as a "controller" has no interrupt sources, so polling
4970 xptpoll(struct cam_sim *sim)
4975 xpt_lock_buses(void)
4977 mtx_lock(&xsoftc.xpt_topo_lock);
4981 xpt_unlock_buses(void)
4983 mtx_unlock(&xsoftc.xpt_topo_lock);
4990 struct cam_sim *sim;
4992 mtx_lock(&cam_simq_lock);
4994 while (!TAILQ_EMPTY(&cam_simq)) {
4995 TAILQ_CONCAT(&queue, &cam_simq, links);
4996 mtx_unlock(&cam_simq_lock);
4998 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4999 TAILQ_REMOVE(&queue, sim, links);
5001 camisr_runqueue(&sim->sim_doneq);
5002 sim->flags &= ~CAM_SIM_ON_DONEQ;
5003 CAM_SIM_UNLOCK(sim);
5005 mtx_lock(&cam_simq_lock);
5007 mtx_unlock(&cam_simq_lock);
5011 camisr_runqueue(void *V_queue)
5013 cam_isrq_t *queue = V_queue;
5014 struct ccb_hdr *ccb_h;
5016 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5019 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5020 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5022 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5027 if (ccb_h->flags & CAM_HIGH_POWER) {
5028 struct highpowerlist *hphead;
5029 union ccb *send_ccb;
5031 mtx_lock(&xsoftc.xpt_lock);
5032 hphead = &xsoftc.highpowerq;
5034 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5037 * Increment the count since this command is done.
5039 xsoftc.num_highpower++;
5042 * Any high powered commands queued up?
5044 if (send_ccb != NULL) {
5046 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5047 mtx_unlock(&xsoftc.xpt_lock);
5049 xpt_release_devq(send_ccb->ccb_h.path,
5050 /*count*/1, /*runqueue*/TRUE);
5052 mtx_unlock(&xsoftc.xpt_lock);
5055 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5058 dev = ccb_h->path->device;
5060 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5061 ccb_h->path->bus->sim->devq->send_active--;
5062 ccb_h->path->bus->sim->devq->send_openings++;
5065 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5066 && (dev->ccbq.dev_active == 0))) {
5067 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5068 xpt_release_devq(ccb_h->path, /*count*/1,
5069 /*run_queue*/FALSE);
5072 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5073 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5074 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5075 xpt_release_devq(ccb_h->path, /*count*/1,
5076 /*run_queue*/FALSE);
5079 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5080 && (--dev->tag_delay_count == 0))
5081 xpt_start_tags(ccb_h->path);
5082 if (!device_is_send_queued(dev)) {
5083 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5088 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5089 xpt_release_simq(ccb_h->path->bus->sim,
5091 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5095 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5096 && (ccb_h->status & CAM_DEV_QFRZN)) {
5097 xpt_release_devq(ccb_h->path, /*count*/1,
5099 ccb_h->status &= ~CAM_DEV_QFRZN;
5101 xpt_run_dev_sendq(ccb_h->path->bus);
5104 /* Call the peripheral driver's callback */
5105 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);