2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_xpt_internal.h>
60 #include <cam/cam_debug.h>
61 #include <cam/cam_compat.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
67 #include <machine/md_var.h> /* geometry translation */
68 #include <machine/stdarg.h> /* for xpt_print below */
73 * This is the maximum number of high powered commands (e.g. start unit)
74 * that can be outstanding at a particular time.
76 #ifndef CAM_MAX_HIGHPOWER
77 #define CAM_MAX_HIGHPOWER 4
80 /* Datastructures internal to the xpt layer */
81 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
82 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
83 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
84 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
86 /* Object for defering XPT actions to a taskqueue */
99 u_int32_t xpt_generation;
101 /* number of high powered commands that can go through right now */
102 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
105 /* queue for handling async rescan requests. */
106 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
108 int buses_config_done;
110 /* Registered busses */
111 TAILQ_HEAD(,cam_eb) xpt_busses;
112 u_int bus_generation;
114 struct intr_config_hook *xpt_config_hook;
117 struct callout boot_callout;
119 struct mtx xpt_topo_lock;
125 DM_RET_FLAG_MASK = 0x0f,
128 DM_RET_DESCEND = 0x20,
130 DM_RET_ACTION_MASK = 0xf0
138 } xpt_traverse_depth;
140 struct xpt_traverse_config {
141 xpt_traverse_depth depth;
146 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
147 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
148 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
149 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
150 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
152 /* Transport layer configuration information */
153 static struct xpt_softc xsoftc;
155 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
159 /* Queues for our software interrupt handler */
160 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
161 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
162 static cam_simq_t cam_simq;
163 static struct mtx cam_simq_lock;
165 /* Pointers to software interrupt handlers */
166 static void *cambio_ih;
168 struct cam_periph *xpt_periph;
170 static periph_init_t xpt_periph_init;
172 static struct periph_driver xpt_driver =
174 xpt_periph_init, "xpt",
175 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
179 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
181 static d_open_t xptopen;
182 static d_close_t xptclose;
183 static d_ioctl_t xptioctl;
184 static d_ioctl_t xptdoioctl;
186 static struct cdevsw xpt_cdevsw = {
187 .d_version = D_VERSION,
195 /* Storage for debugging datastructures */
196 struct cam_path *cam_dpath;
197 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
198 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
199 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
200 &cam_dflags, 0, "Enabled debug flags");
201 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
202 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
203 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
204 &cam_debug_delay, 0, "Delay in us after each debug message");
206 /* Our boot-time initialization hook */
207 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
209 static moduledata_t cam_moduledata = {
211 cam_module_event_handler,
215 static int xpt_init(void *);
217 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
218 MODULE_VERSION(cam, 1);
221 static void xpt_async_bcast(struct async_list *async_head,
222 u_int32_t async_code,
223 struct cam_path *path,
225 static path_id_t xptnextfreepathid(void);
226 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
227 static union ccb *xpt_get_ccb(struct cam_ed *device);
228 static void xpt_run_dev_allocq(struct cam_ed *device);
229 static void xpt_run_devq(struct cam_devq *devq);
230 static timeout_t xpt_release_devq_timeout;
231 static void xpt_release_simq_timeout(void *arg) __unused;
232 static void xpt_release_bus(struct cam_eb *bus);
233 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
235 static struct cam_et*
236 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
237 static void xpt_release_target(struct cam_et *target);
238 static struct cam_eb*
239 xpt_find_bus(path_id_t path_id);
240 static struct cam_et*
241 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
242 static struct cam_ed*
243 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
244 static void xpt_config(void *arg);
245 static xpt_devicefunc_t xptpassannouncefunc;
246 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
247 static void xptpoll(struct cam_sim *sim);
248 static void camisr(void *);
249 static void camisr_runqueue(void *);
250 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
251 u_int num_patterns, struct cam_eb *bus);
252 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
254 struct cam_ed *device);
255 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
257 struct cam_periph *periph);
258 static xpt_busfunc_t xptedtbusfunc;
259 static xpt_targetfunc_t xptedttargetfunc;
260 static xpt_devicefunc_t xptedtdevicefunc;
261 static xpt_periphfunc_t xptedtperiphfunc;
262 static xpt_pdrvfunc_t xptplistpdrvfunc;
263 static xpt_periphfunc_t xptplistperiphfunc;
264 static int xptedtmatch(struct ccb_dev_match *cdm);
265 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
266 static int xptbustraverse(struct cam_eb *start_bus,
267 xpt_busfunc_t *tr_func, void *arg);
268 static int xpttargettraverse(struct cam_eb *bus,
269 struct cam_et *start_target,
270 xpt_targetfunc_t *tr_func, void *arg);
271 static int xptdevicetraverse(struct cam_et *target,
272 struct cam_ed *start_device,
273 xpt_devicefunc_t *tr_func, void *arg);
274 static int xptperiphtraverse(struct cam_ed *device,
275 struct cam_periph *start_periph,
276 xpt_periphfunc_t *tr_func, void *arg);
277 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
278 xpt_pdrvfunc_t *tr_func, void *arg);
279 static int xptpdperiphtraverse(struct periph_driver **pdrv,
280 struct cam_periph *start_periph,
281 xpt_periphfunc_t *tr_func,
283 static xpt_busfunc_t xptdefbusfunc;
284 static xpt_targetfunc_t xptdeftargetfunc;
285 static xpt_devicefunc_t xptdefdevicefunc;
286 static xpt_periphfunc_t xptdefperiphfunc;
287 static void xpt_finishconfig_task(void *context, int pending);
288 static void xpt_dev_async_default(u_int32_t async_code,
290 struct cam_et *target,
291 struct cam_ed *device,
293 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
294 struct cam_et *target,
296 static xpt_devicefunc_t xptsetasyncfunc;
297 static xpt_busfunc_t xptsetasyncbusfunc;
298 static cam_status xptregister(struct cam_periph *periph,
300 static __inline int periph_is_queued(struct cam_periph *periph);
301 static __inline int device_is_queued(struct cam_ed *device);
304 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
308 if ((dev->ccbq.queue.entries > 0) &&
309 (dev->ccbq.dev_openings > 0) &&
310 (dev->ccbq.queue.qfrozen_cnt == 0)) {
312 * The priority of a device waiting for controller
313 * resources is that of the highest priority CCB
317 xpt_schedule_dev(&devq->send_queue,
318 &dev->devq_entry.pinfo,
319 CAMQ_GET_PRIO(&dev->ccbq.queue));
327 periph_is_queued(struct cam_periph *periph)
329 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
333 device_is_queued(struct cam_ed *device)
335 return (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX);
341 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
345 xptdone(struct cam_periph *periph, union ccb *done_ccb)
347 /* Caller will release the CCB */
348 wakeup(&done_ccb->ccb_h.cbfcnp);
352 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
356 * Only allow read-write access.
358 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
362 * We don't allow nonblocking access.
364 if ((flags & O_NONBLOCK) != 0) {
365 printf("%s: can't do nonblocking access\n", devtoname(dev));
369 /* Mark ourselves open */
370 mtx_lock(&xsoftc.xpt_lock);
371 xsoftc.flags |= XPT_FLAG_OPEN;
372 mtx_unlock(&xsoftc.xpt_lock);
378 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
381 /* Mark ourselves closed */
382 mtx_lock(&xsoftc.xpt_lock);
383 xsoftc.flags &= ~XPT_FLAG_OPEN;
384 mtx_unlock(&xsoftc.xpt_lock);
390 * Don't automatically grab the xpt softc lock here even though this is going
391 * through the xpt device. The xpt device is really just a back door for
392 * accessing other devices and SIMs, so the right thing to do is to grab
393 * the appropriate SIM lock once the bus/SIM is located.
396 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
400 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
401 error = cam_compat_ioctl(dev, &cmd, &addr, &flag, td);
403 return (xptdoioctl(dev, cmd, addr, flag, td));
409 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
417 * For the transport layer CAMIOCOMMAND ioctl, we really only want
418 * to accept CCB types that don't quite make sense to send through a
419 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
427 inccb = (union ccb *)addr;
429 bus = xpt_find_bus(inccb->ccb_h.path_id);
433 switch (inccb->ccb_h.func_code) {
436 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
437 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
438 xpt_release_bus(bus);
443 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
444 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
445 xpt_release_bus(bus);
453 switch(inccb->ccb_h.func_code) {
461 ccb = xpt_alloc_ccb();
463 CAM_SIM_LOCK(bus->sim);
466 * Create a path using the bus, target, and lun the
469 if (xpt_create_path(&ccb->ccb_h.path, NULL,
470 inccb->ccb_h.path_id,
471 inccb->ccb_h.target_id,
472 inccb->ccb_h.target_lun) !=
475 CAM_SIM_UNLOCK(bus->sim);
479 /* Ensure all of our fields are correct */
480 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
481 inccb->ccb_h.pinfo.priority);
482 xpt_merge_ccb(ccb, inccb);
483 ccb->ccb_h.cbfcnp = xptdone;
484 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
485 bcopy(ccb, inccb, sizeof(union ccb));
486 xpt_free_path(ccb->ccb_h.path);
488 CAM_SIM_UNLOCK(bus->sim);
495 * This is an immediate CCB, so it's okay to
496 * allocate it on the stack.
499 CAM_SIM_LOCK(bus->sim);
502 * Create a path using the bus, target, and lun the
505 if (xpt_create_path(&ccb.ccb_h.path, NULL,
506 inccb->ccb_h.path_id,
507 inccb->ccb_h.target_id,
508 inccb->ccb_h.target_lun) !=
511 CAM_SIM_UNLOCK(bus->sim);
514 /* Ensure all of our fields are correct */
515 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
516 inccb->ccb_h.pinfo.priority);
517 xpt_merge_ccb(&ccb, inccb);
518 ccb.ccb_h.cbfcnp = xptdone;
520 bcopy(&ccb, inccb, sizeof(union ccb));
521 xpt_free_path(ccb.ccb_h.path);
522 CAM_SIM_UNLOCK(bus->sim);
526 case XPT_DEV_MATCH: {
527 struct cam_periph_map_info mapinfo;
528 struct cam_path *old_path;
531 * We can't deal with physical addresses for this
532 * type of transaction.
534 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
541 * Save this in case the caller had it set to
542 * something in particular.
544 old_path = inccb->ccb_h.path;
547 * We really don't need a path for the matching
548 * code. The path is needed because of the
549 * debugging statements in xpt_action(). They
550 * assume that the CCB has a valid path.
552 inccb->ccb_h.path = xpt_periph->path;
554 bzero(&mapinfo, sizeof(mapinfo));
557 * Map the pattern and match buffers into kernel
558 * virtual address space.
560 error = cam_periph_mapmem(inccb, &mapinfo);
563 inccb->ccb_h.path = old_path;
568 * This is an immediate CCB, we can send it on directly.
570 CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
572 CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
575 * Map the buffers back into user space.
577 cam_periph_unmapmem(inccb, &mapinfo);
579 inccb->ccb_h.path = old_path;
588 xpt_release_bus(bus);
592 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
593 * with the periphal driver name and unit name filled in. The other
594 * fields don't really matter as input. The passthrough driver name
595 * ("pass"), and unit number are passed back in the ccb. The current
596 * device generation number, and the index into the device peripheral
597 * driver list, and the status are also passed back. Note that
598 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
599 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
600 * (or rather should be) impossible for the device peripheral driver
601 * list to change since we look at the whole thing in one pass, and
602 * we do it with lock protection.
605 case CAMGETPASSTHRU: {
607 struct cam_periph *periph;
608 struct periph_driver **p_drv;
611 u_int cur_generation;
612 int base_periph_found;
615 ccb = (union ccb *)addr;
616 unit = ccb->cgdl.unit_number;
617 name = ccb->cgdl.periph_name;
619 * Every 100 devices, we want to drop our lock protection to
620 * give the software interrupt handler a chance to run.
621 * Most systems won't run into this check, but this should
622 * avoid starvation in the software interrupt handler in
627 ccb = (union ccb *)addr;
629 base_periph_found = 0;
632 * Sanity check -- make sure we don't get a null peripheral
635 if (*ccb->cgdl.periph_name == '\0') {
640 /* Keep the list from changing while we traverse it */
643 cur_generation = xsoftc.xpt_generation;
645 /* first find our driver in the list of drivers */
646 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
647 if (strcmp((*p_drv)->driver_name, name) == 0)
650 if (*p_drv == NULL) {
652 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
653 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
654 *ccb->cgdl.periph_name = '\0';
655 ccb->cgdl.unit_number = 0;
661 * Run through every peripheral instance of this driver
662 * and check to see whether it matches the unit passed
663 * in by the user. If it does, get out of the loops and
664 * find the passthrough driver associated with that
667 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
668 periph = TAILQ_NEXT(periph, unit_links)) {
670 if (periph->unit_number == unit) {
672 } else if (--splbreaknum == 0) {
676 if (cur_generation != xsoftc.xpt_generation)
681 * If we found the peripheral driver that the user passed
682 * in, go through all of the peripheral drivers for that
683 * particular device and look for a passthrough driver.
685 if (periph != NULL) {
686 struct cam_ed *device;
689 base_periph_found = 1;
690 device = periph->path->device;
691 for (i = 0, periph = SLIST_FIRST(&device->periphs);
693 periph = SLIST_NEXT(periph, periph_links), i++) {
695 * Check to see whether we have a
696 * passthrough device or not.
698 if (strcmp(periph->periph_name, "pass") == 0) {
700 * Fill in the getdevlist fields.
702 strcpy(ccb->cgdl.periph_name,
703 periph->periph_name);
704 ccb->cgdl.unit_number =
706 if (SLIST_NEXT(periph, periph_links))
708 CAM_GDEVLIST_MORE_DEVS;
711 CAM_GDEVLIST_LAST_DEVICE;
712 ccb->cgdl.generation =
716 * Fill in some CCB header fields
717 * that the user may want.
720 periph->path->bus->path_id;
721 ccb->ccb_h.target_id =
722 periph->path->target->target_id;
723 ccb->ccb_h.target_lun =
724 periph->path->device->lun_id;
725 ccb->ccb_h.status = CAM_REQ_CMP;
732 * If the periph is null here, one of two things has
733 * happened. The first possibility is that we couldn't
734 * find the unit number of the particular peripheral driver
735 * that the user is asking about. e.g. the user asks for
736 * the passthrough driver for "da11". We find the list of
737 * "da" peripherals all right, but there is no unit 11.
738 * The other possibility is that we went through the list
739 * of peripheral drivers attached to the device structure,
740 * but didn't find one with the name "pass". Either way,
741 * we return ENOENT, since we couldn't find something.
743 if (periph == NULL) {
744 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
745 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
746 *ccb->cgdl.periph_name = '\0';
747 ccb->cgdl.unit_number = 0;
750 * It is unfortunate that this is even necessary,
751 * but there are many, many clueless users out there.
752 * If this is true, the user is looking for the
753 * passthrough driver, but doesn't have one in his
756 if (base_periph_found == 1) {
757 printf("xptioctl: pass driver is not in the "
759 printf("xptioctl: put \"device pass\" in "
760 "your kernel config file\n");
775 cam_module_event_handler(module_t mod, int what, void *arg)
781 if ((error = xpt_init(NULL)) != 0)
794 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
797 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
798 xpt_free_path(done_ccb->ccb_h.path);
799 xpt_free_ccb(done_ccb);
801 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
802 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
807 /* thread to handle bus rescans */
809 xpt_scanner_thread(void *dummy)
816 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
817 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
819 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
820 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
823 sim = ccb->ccb_h.path->bus->sim;
834 xpt_rescan(union ccb *ccb)
838 /* Prepare request */
839 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
840 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
841 ccb->ccb_h.func_code = XPT_SCAN_BUS;
842 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
843 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
844 ccb->ccb_h.func_code = XPT_SCAN_TGT;
845 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
846 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
847 ccb->ccb_h.func_code = XPT_SCAN_LUN;
849 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
850 xpt_free_path(ccb->ccb_h.path);
854 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
855 ccb->ccb_h.cbfcnp = xpt_rescan_done;
856 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
857 /* Don't make duplicate entries for the same paths. */
859 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
860 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
861 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
862 wakeup(&xsoftc.ccb_scanq);
864 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
865 xpt_free_path(ccb->ccb_h.path);
871 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
872 xsoftc.buses_to_config++;
873 wakeup(&xsoftc.ccb_scanq);
877 /* Functions accessed by the peripheral drivers */
879 xpt_init(void *dummy)
881 struct cam_sim *xpt_sim;
882 struct cam_path *path;
883 struct cam_devq *devq;
886 TAILQ_INIT(&xsoftc.xpt_busses);
887 TAILQ_INIT(&cam_simq);
888 TAILQ_INIT(&xsoftc.ccb_scanq);
889 STAILQ_INIT(&xsoftc.highpowerq);
890 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
892 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
893 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
894 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
896 #ifdef CAM_BOOT_DELAY
898 * Override this value at compile time to assist our users
899 * who don't use loader to boot a kernel.
901 xsoftc.boot_delay = CAM_BOOT_DELAY;
904 * The xpt layer is, itself, the equivelent of a SIM.
905 * Allow 16 ccbs in the ccb pool for it. This should
906 * give decent parallelism when we probe busses and
907 * perform other XPT functions.
909 devq = cam_simq_alloc(16);
910 xpt_sim = cam_sim_alloc(xptaction,
915 /*mtx*/&xsoftc.xpt_lock,
916 /*max_dev_transactions*/0,
917 /*max_tagged_dev_transactions*/0,
922 mtx_lock(&xsoftc.xpt_lock);
923 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
924 mtx_unlock(&xsoftc.xpt_lock);
925 printf("xpt_init: xpt_bus_register failed with status %#x,"
926 " failing attach\n", status);
931 * Looking at the XPT from the SIM layer, the XPT is
932 * the equivelent of a peripheral driver. Allocate
933 * a peripheral driver entry for us.
935 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
937 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
938 mtx_unlock(&xsoftc.xpt_lock);
939 printf("xpt_init: xpt_create_path failed with status %#x,"
940 " failing attach\n", status);
944 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
945 path, NULL, 0, xpt_sim);
947 mtx_unlock(&xsoftc.xpt_lock);
948 /* Install our software interrupt handlers */
949 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
951 * Register a callback for when interrupts are enabled.
953 xsoftc.xpt_config_hook =
954 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
955 M_CAMXPT, M_NOWAIT | M_ZERO);
956 if (xsoftc.xpt_config_hook == NULL) {
957 printf("xpt_init: Cannot malloc config hook "
958 "- failing attach\n");
961 xsoftc.xpt_config_hook->ich_func = xpt_config;
962 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
963 free (xsoftc.xpt_config_hook, M_CAMXPT);
964 printf("xpt_init: config_intrhook_establish failed "
965 "- failing attach\n");
972 xptregister(struct cam_periph *periph, void *arg)
974 struct cam_sim *xpt_sim;
976 if (periph == NULL) {
977 printf("xptregister: periph was NULL!!\n");
978 return(CAM_REQ_CMP_ERR);
981 xpt_sim = (struct cam_sim *)arg;
982 xpt_sim->softc = periph;
984 periph->softc = NULL;
990 xpt_add_periph(struct cam_periph *periph)
992 struct cam_ed *device;
994 struct periph_list *periph_head;
996 mtx_assert(periph->sim->mtx, MA_OWNED);
998 device = periph->path->device;
1000 periph_head = &device->periphs;
1002 status = CAM_REQ_CMP;
1004 if (device != NULL) {
1006 * Make room for this peripheral
1007 * so it will fit in the queue
1008 * when it's scheduled to run
1010 status = camq_resize(&device->drvq,
1011 device->drvq.array_size + 1);
1013 device->generation++;
1015 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1019 xsoftc.xpt_generation++;
1026 xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
1028 struct cam_ed *device;
1030 mtx_assert(periph->sim->mtx, MA_OWNED);
1032 device = periph->path->device;
1034 if (device != NULL) {
1035 struct periph_list *periph_head;
1037 periph_head = &device->periphs;
1039 /* Release the slot for this peripheral */
1040 camq_resize(&device->drvq, device->drvq.array_size - 1);
1042 device->generation++;
1044 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1047 if (topology_lock_held == 0)
1050 xsoftc.xpt_generation++;
1052 if (topology_lock_held == 0)
1058 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1060 struct cam_path *path = periph->path;
1062 mtx_assert(periph->sim->mtx, MA_OWNED);
1064 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1065 periph->periph_name, periph->unit_number,
1066 path->bus->sim->sim_name,
1067 path->bus->sim->unit_number,
1068 path->bus->sim->bus_id,
1070 path->target->target_id,
1071 path->device->lun_id);
1072 printf("%s%d: ", periph->periph_name, periph->unit_number);
1073 if (path->device->protocol == PROTO_SCSI)
1074 scsi_print_inquiry(&path->device->inq_data);
1075 else if (path->device->protocol == PROTO_ATA ||
1076 path->device->protocol == PROTO_SATAPM)
1077 ata_print_ident(&path->device->ident_data);
1078 else if (path->device->protocol == PROTO_SEMB)
1080 (struct sep_identify_data *)&path->device->ident_data);
1082 printf("Unknown protocol device\n");
1083 if (bootverbose && path->device->serial_num_len > 0) {
1084 /* Don't wrap the screen - print only the first 60 chars */
1085 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1086 periph->unit_number, path->device->serial_num);
1088 /* Announce transport details. */
1089 (*(path->bus->xport->announce))(periph);
1090 /* Announce command queueing. */
1091 if (path->device->inq_flags & SID_CmdQue
1092 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1093 printf("%s%d: Command Queueing enabled\n",
1094 periph->periph_name, periph->unit_number);
1096 /* Announce caller's details if they've passed in. */
1097 if (announce_string != NULL)
1098 printf("%s%d: %s\n", periph->periph_name,
1099 periph->unit_number, announce_string);
1103 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1106 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1107 periph->unit_number, quirks, bit_string);
1112 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1115 struct ccb_dev_advinfo cdai;
1116 struct scsi_vpd_id_descriptor *idd;
1118 mtx_assert(path->bus->sim->mtx, MA_OWNED);
1120 memset(&cdai, 0, sizeof(cdai));
1121 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1122 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1125 if (!strcmp(attr, "GEOM::ident"))
1126 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1127 else if (!strcmp(attr, "GEOM::physpath"))
1128 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1129 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1130 strcmp(attr, "GEOM::lunname") == 0) {
1131 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1132 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1136 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1137 if (cdai.buf == NULL) {
1141 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1142 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1143 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1144 if (cdai.provsiz == 0)
1146 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1147 if (strcmp(attr, "GEOM::lunid") == 0) {
1148 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1149 cdai.provsiz, scsi_devid_is_lun_naa);
1151 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1152 cdai.provsiz, scsi_devid_is_lun_eui64);
1156 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1157 cdai.provsiz, scsi_devid_is_lun_t10);
1159 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1160 cdai.provsiz, scsi_devid_is_lun_name);
1164 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1165 (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1166 l = strnlen(idd->identifier, idd->length);
1168 bcopy(idd->identifier, buf, l);
1173 if (idd->length * 2 < len) {
1174 for (l = 0; l < idd->length; l++)
1175 sprintf(buf + l * 2, "%02x",
1176 idd->identifier[l]);
1182 if (strlcpy(buf, cdai.buf, len) >= len)
1187 if (cdai.buf != NULL)
1188 free(cdai.buf, M_CAMXPT);
1192 static dev_match_ret
1193 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1196 dev_match_ret retval;
1199 retval = DM_RET_NONE;
1202 * If we aren't given something to match against, that's an error.
1205 return(DM_RET_ERROR);
1208 * If there are no match entries, then this bus matches no
1211 if ((patterns == NULL) || (num_patterns == 0))
1212 return(DM_RET_DESCEND | DM_RET_COPY);
1214 for (i = 0; i < num_patterns; i++) {
1215 struct bus_match_pattern *cur_pattern;
1218 * If the pattern in question isn't for a bus node, we
1219 * aren't interested. However, we do indicate to the
1220 * calling routine that we should continue descending the
1221 * tree, since the user wants to match against lower-level
1224 if (patterns[i].type != DEV_MATCH_BUS) {
1225 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1226 retval |= DM_RET_DESCEND;
1230 cur_pattern = &patterns[i].pattern.bus_pattern;
1233 * If they want to match any bus node, we give them any
1236 if (cur_pattern->flags == BUS_MATCH_ANY) {
1237 /* set the copy flag */
1238 retval |= DM_RET_COPY;
1241 * If we've already decided on an action, go ahead
1244 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1249 * Not sure why someone would do this...
1251 if (cur_pattern->flags == BUS_MATCH_NONE)
1254 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1255 && (cur_pattern->path_id != bus->path_id))
1258 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1259 && (cur_pattern->bus_id != bus->sim->bus_id))
1262 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1263 && (cur_pattern->unit_number != bus->sim->unit_number))
1266 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1267 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1272 * If we get to this point, the user definitely wants
1273 * information on this bus. So tell the caller to copy the
1276 retval |= DM_RET_COPY;
1279 * If the return action has been set to descend, then we
1280 * know that we've already seen a non-bus matching
1281 * expression, therefore we need to further descend the tree.
1282 * This won't change by continuing around the loop, so we
1283 * go ahead and return. If we haven't seen a non-bus
1284 * matching expression, we keep going around the loop until
1285 * we exhaust the matching expressions. We'll set the stop
1286 * flag once we fall out of the loop.
1288 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1293 * If the return action hasn't been set to descend yet, that means
1294 * we haven't seen anything other than bus matching patterns. So
1295 * tell the caller to stop descending the tree -- the user doesn't
1296 * want to match against lower level tree elements.
1298 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1299 retval |= DM_RET_STOP;
1304 static dev_match_ret
1305 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1306 struct cam_ed *device)
1308 dev_match_ret retval;
1311 retval = DM_RET_NONE;
1314 * If we aren't given something to match against, that's an error.
1317 return(DM_RET_ERROR);
1320 * If there are no match entries, then this device matches no
1323 if ((patterns == NULL) || (num_patterns == 0))
1324 return(DM_RET_DESCEND | DM_RET_COPY);
1326 for (i = 0; i < num_patterns; i++) {
1327 struct device_match_pattern *cur_pattern;
1328 struct scsi_vpd_device_id *device_id_page;
1331 * If the pattern in question isn't for a device node, we
1332 * aren't interested.
1334 if (patterns[i].type != DEV_MATCH_DEVICE) {
1335 if ((patterns[i].type == DEV_MATCH_PERIPH)
1336 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1337 retval |= DM_RET_DESCEND;
1341 cur_pattern = &patterns[i].pattern.device_pattern;
1343 /* Error out if mutually exclusive options are specified. */
1344 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1345 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1346 return(DM_RET_ERROR);
1349 * If they want to match any device node, we give them any
1352 if (cur_pattern->flags == DEV_MATCH_ANY)
1356 * Not sure why someone would do this...
1358 if (cur_pattern->flags == DEV_MATCH_NONE)
1361 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1362 && (cur_pattern->path_id != device->target->bus->path_id))
1365 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1366 && (cur_pattern->target_id != device->target->target_id))
1369 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1370 && (cur_pattern->target_lun != device->lun_id))
1373 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1374 && (cam_quirkmatch((caddr_t)&device->inq_data,
1375 (caddr_t)&cur_pattern->data.inq_pat,
1376 1, sizeof(cur_pattern->data.inq_pat),
1377 scsi_static_inquiry_match) == NULL))
1380 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1381 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1382 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1383 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1384 device->device_id_len
1385 - SVPD_DEVICE_ID_HDR_LEN,
1386 cur_pattern->data.devid_pat.id,
1387 cur_pattern->data.devid_pat.id_len) != 0))
1392 * If we get to this point, the user definitely wants
1393 * information on this device. So tell the caller to copy
1396 retval |= DM_RET_COPY;
1399 * If the return action has been set to descend, then we
1400 * know that we've already seen a peripheral matching
1401 * expression, therefore we need to further descend the tree.
1402 * This won't change by continuing around the loop, so we
1403 * go ahead and return. If we haven't seen a peripheral
1404 * matching expression, we keep going around the loop until
1405 * we exhaust the matching expressions. We'll set the stop
1406 * flag once we fall out of the loop.
1408 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1413 * If the return action hasn't been set to descend yet, that means
1414 * we haven't seen any peripheral matching patterns. So tell the
1415 * caller to stop descending the tree -- the user doesn't want to
1416 * match against lower level tree elements.
1418 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1419 retval |= DM_RET_STOP;
1425 * Match a single peripheral against any number of match patterns.
1427 static dev_match_ret
1428 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1429 struct cam_periph *periph)
1431 dev_match_ret retval;
1435 * If we aren't given something to match against, that's an error.
1438 return(DM_RET_ERROR);
1441 * If there are no match entries, then this peripheral matches no
1444 if ((patterns == NULL) || (num_patterns == 0))
1445 return(DM_RET_STOP | DM_RET_COPY);
1448 * There aren't any nodes below a peripheral node, so there's no
1449 * reason to descend the tree any further.
1451 retval = DM_RET_STOP;
1453 for (i = 0; i < num_patterns; i++) {
1454 struct periph_match_pattern *cur_pattern;
1457 * If the pattern in question isn't for a peripheral, we
1458 * aren't interested.
1460 if (patterns[i].type != DEV_MATCH_PERIPH)
1463 cur_pattern = &patterns[i].pattern.periph_pattern;
1466 * If they want to match on anything, then we will do so.
1468 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1469 /* set the copy flag */
1470 retval |= DM_RET_COPY;
1473 * We've already set the return action to stop,
1474 * since there are no nodes below peripherals in
1481 * Not sure why someone would do this...
1483 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1486 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1487 && (cur_pattern->path_id != periph->path->bus->path_id))
1491 * For the target and lun id's, we have to make sure the
1492 * target and lun pointers aren't NULL. The xpt peripheral
1493 * has a wildcard target and device.
1495 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1496 && ((periph->path->target == NULL)
1497 ||(cur_pattern->target_id != periph->path->target->target_id)))
1500 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1501 && ((periph->path->device == NULL)
1502 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1505 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1506 && (cur_pattern->unit_number != periph->unit_number))
1509 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1510 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1515 * If we get to this point, the user definitely wants
1516 * information on this peripheral. So tell the caller to
1517 * copy the data out.
1519 retval |= DM_RET_COPY;
1522 * The return action has already been set to stop, since
1523 * peripherals don't have any nodes below them in the EDT.
1529 * If we get to this point, the peripheral that was passed in
1530 * doesn't match any of the patterns.
1536 xptedtbusfunc(struct cam_eb *bus, void *arg)
1538 struct ccb_dev_match *cdm;
1539 dev_match_ret retval;
1541 cdm = (struct ccb_dev_match *)arg;
1544 * If our position is for something deeper in the tree, that means
1545 * that we've already seen this node. So, we keep going down.
1547 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1548 && (cdm->pos.cookie.bus == bus)
1549 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1550 && (cdm->pos.cookie.target != NULL))
1551 retval = DM_RET_DESCEND;
1553 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1556 * If we got an error, bail out of the search.
1558 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1559 cdm->status = CAM_DEV_MATCH_ERROR;
1564 * If the copy flag is set, copy this bus out.
1566 if (retval & DM_RET_COPY) {
1569 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1570 sizeof(struct dev_match_result));
1573 * If we don't have enough space to put in another
1574 * match result, save our position and tell the
1575 * user there are more devices to check.
1577 if (spaceleft < sizeof(struct dev_match_result)) {
1578 bzero(&cdm->pos, sizeof(cdm->pos));
1579 cdm->pos.position_type =
1580 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1582 cdm->pos.cookie.bus = bus;
1583 cdm->pos.generations[CAM_BUS_GENERATION]=
1584 xsoftc.bus_generation;
1585 cdm->status = CAM_DEV_MATCH_MORE;
1588 j = cdm->num_matches;
1590 cdm->matches[j].type = DEV_MATCH_BUS;
1591 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1592 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1593 cdm->matches[j].result.bus_result.unit_number =
1594 bus->sim->unit_number;
1595 strncpy(cdm->matches[j].result.bus_result.dev_name,
1596 bus->sim->sim_name, DEV_IDLEN);
1600 * If the user is only interested in busses, there's no
1601 * reason to descend to the next level in the tree.
1603 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1607 * If there is a target generation recorded, check it to
1608 * make sure the target list hasn't changed.
1610 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1611 && (bus == cdm->pos.cookie.bus)
1612 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1613 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1614 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1616 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1620 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1621 && (cdm->pos.cookie.bus == bus)
1622 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1623 && (cdm->pos.cookie.target != NULL))
1624 return(xpttargettraverse(bus,
1625 (struct cam_et *)cdm->pos.cookie.target,
1626 xptedttargetfunc, arg));
1628 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1632 xptedttargetfunc(struct cam_et *target, void *arg)
1634 struct ccb_dev_match *cdm;
1636 cdm = (struct ccb_dev_match *)arg;
1639 * If there is a device list generation recorded, check it to
1640 * make sure the device list hasn't changed.
1642 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1643 && (cdm->pos.cookie.bus == target->bus)
1644 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1645 && (cdm->pos.cookie.target == target)
1646 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1647 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1648 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1649 target->generation)) {
1650 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1654 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1655 && (cdm->pos.cookie.bus == target->bus)
1656 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1657 && (cdm->pos.cookie.target == target)
1658 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1659 && (cdm->pos.cookie.device != NULL))
1660 return(xptdevicetraverse(target,
1661 (struct cam_ed *)cdm->pos.cookie.device,
1662 xptedtdevicefunc, arg));
1664 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1668 xptedtdevicefunc(struct cam_ed *device, void *arg)
1671 struct ccb_dev_match *cdm;
1672 dev_match_ret retval;
1674 cdm = (struct ccb_dev_match *)arg;
1677 * If our position is for something deeper in the tree, that means
1678 * that we've already seen this node. So, we keep going down.
1680 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1681 && (cdm->pos.cookie.device == device)
1682 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1683 && (cdm->pos.cookie.periph != NULL))
1684 retval = DM_RET_DESCEND;
1686 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1689 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1690 cdm->status = CAM_DEV_MATCH_ERROR;
1695 * If the copy flag is set, copy this device out.
1697 if (retval & DM_RET_COPY) {
1700 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1701 sizeof(struct dev_match_result));
1704 * If we don't have enough space to put in another
1705 * match result, save our position and tell the
1706 * user there are more devices to check.
1708 if (spaceleft < sizeof(struct dev_match_result)) {
1709 bzero(&cdm->pos, sizeof(cdm->pos));
1710 cdm->pos.position_type =
1711 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1712 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1714 cdm->pos.cookie.bus = device->target->bus;
1715 cdm->pos.generations[CAM_BUS_GENERATION]=
1716 xsoftc.bus_generation;
1717 cdm->pos.cookie.target = device->target;
1718 cdm->pos.generations[CAM_TARGET_GENERATION] =
1719 device->target->bus->generation;
1720 cdm->pos.cookie.device = device;
1721 cdm->pos.generations[CAM_DEV_GENERATION] =
1722 device->target->generation;
1723 cdm->status = CAM_DEV_MATCH_MORE;
1726 j = cdm->num_matches;
1728 cdm->matches[j].type = DEV_MATCH_DEVICE;
1729 cdm->matches[j].result.device_result.path_id =
1730 device->target->bus->path_id;
1731 cdm->matches[j].result.device_result.target_id =
1732 device->target->target_id;
1733 cdm->matches[j].result.device_result.target_lun =
1735 cdm->matches[j].result.device_result.protocol =
1737 bcopy(&device->inq_data,
1738 &cdm->matches[j].result.device_result.inq_data,
1739 sizeof(struct scsi_inquiry_data));
1740 bcopy(&device->ident_data,
1741 &cdm->matches[j].result.device_result.ident_data,
1742 sizeof(struct ata_params));
1744 /* Let the user know whether this device is unconfigured */
1745 if (device->flags & CAM_DEV_UNCONFIGURED)
1746 cdm->matches[j].result.device_result.flags =
1747 DEV_RESULT_UNCONFIGURED;
1749 cdm->matches[j].result.device_result.flags =
1754 * If the user isn't interested in peripherals, don't descend
1755 * the tree any further.
1757 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1761 * If there is a peripheral list generation recorded, make sure
1762 * it hasn't changed.
1764 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1765 && (device->target->bus == cdm->pos.cookie.bus)
1766 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1767 && (device->target == cdm->pos.cookie.target)
1768 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1769 && (device == cdm->pos.cookie.device)
1770 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1771 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1772 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1773 device->generation)){
1774 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1778 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1779 && (cdm->pos.cookie.bus == device->target->bus)
1780 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1781 && (cdm->pos.cookie.target == device->target)
1782 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1783 && (cdm->pos.cookie.device == device)
1784 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1785 && (cdm->pos.cookie.periph != NULL))
1786 return(xptperiphtraverse(device,
1787 (struct cam_periph *)cdm->pos.cookie.periph,
1788 xptedtperiphfunc, arg));
1790 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1794 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1796 struct ccb_dev_match *cdm;
1797 dev_match_ret retval;
1799 cdm = (struct ccb_dev_match *)arg;
1801 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1803 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1804 cdm->status = CAM_DEV_MATCH_ERROR;
1809 * If the copy flag is set, copy this peripheral out.
1811 if (retval & DM_RET_COPY) {
1814 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1815 sizeof(struct dev_match_result));
1818 * If we don't have enough space to put in another
1819 * match result, save our position and tell the
1820 * user there are more devices to check.
1822 if (spaceleft < sizeof(struct dev_match_result)) {
1823 bzero(&cdm->pos, sizeof(cdm->pos));
1824 cdm->pos.position_type =
1825 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1826 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1829 cdm->pos.cookie.bus = periph->path->bus;
1830 cdm->pos.generations[CAM_BUS_GENERATION]=
1831 xsoftc.bus_generation;
1832 cdm->pos.cookie.target = periph->path->target;
1833 cdm->pos.generations[CAM_TARGET_GENERATION] =
1834 periph->path->bus->generation;
1835 cdm->pos.cookie.device = periph->path->device;
1836 cdm->pos.generations[CAM_DEV_GENERATION] =
1837 periph->path->target->generation;
1838 cdm->pos.cookie.periph = periph;
1839 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1840 periph->path->device->generation;
1841 cdm->status = CAM_DEV_MATCH_MORE;
1845 j = cdm->num_matches;
1847 cdm->matches[j].type = DEV_MATCH_PERIPH;
1848 cdm->matches[j].result.periph_result.path_id =
1849 periph->path->bus->path_id;
1850 cdm->matches[j].result.periph_result.target_id =
1851 periph->path->target->target_id;
1852 cdm->matches[j].result.periph_result.target_lun =
1853 periph->path->device->lun_id;
1854 cdm->matches[j].result.periph_result.unit_number =
1855 periph->unit_number;
1856 strncpy(cdm->matches[j].result.periph_result.periph_name,
1857 periph->periph_name, DEV_IDLEN);
1864 xptedtmatch(struct ccb_dev_match *cdm)
1868 cdm->num_matches = 0;
1871 * Check the bus list generation. If it has changed, the user
1872 * needs to reset everything and start over.
1874 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1875 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1876 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1877 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1881 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1882 && (cdm->pos.cookie.bus != NULL))
1883 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1884 xptedtbusfunc, cdm);
1886 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1889 * If we get back 0, that means that we had to stop before fully
1890 * traversing the EDT. It also means that one of the subroutines
1891 * has set the status field to the proper value. If we get back 1,
1892 * we've fully traversed the EDT and copied out any matching entries.
1895 cdm->status = CAM_DEV_MATCH_LAST;
1901 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1903 struct ccb_dev_match *cdm;
1905 cdm = (struct ccb_dev_match *)arg;
1907 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1908 && (cdm->pos.cookie.pdrv == pdrv)
1909 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1910 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1911 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1912 (*pdrv)->generation)) {
1913 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1917 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1918 && (cdm->pos.cookie.pdrv == pdrv)
1919 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1920 && (cdm->pos.cookie.periph != NULL))
1921 return(xptpdperiphtraverse(pdrv,
1922 (struct cam_periph *)cdm->pos.cookie.periph,
1923 xptplistperiphfunc, arg));
1925 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1929 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1931 struct ccb_dev_match *cdm;
1932 dev_match_ret retval;
1934 cdm = (struct ccb_dev_match *)arg;
1936 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1938 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1939 cdm->status = CAM_DEV_MATCH_ERROR;
1944 * If the copy flag is set, copy this peripheral out.
1946 if (retval & DM_RET_COPY) {
1949 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1950 sizeof(struct dev_match_result));
1953 * If we don't have enough space to put in another
1954 * match result, save our position and tell the
1955 * user there are more devices to check.
1957 if (spaceleft < sizeof(struct dev_match_result)) {
1958 struct periph_driver **pdrv;
1961 bzero(&cdm->pos, sizeof(cdm->pos));
1962 cdm->pos.position_type =
1963 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1967 * This may look a bit non-sensical, but it is
1968 * actually quite logical. There are very few
1969 * peripheral drivers, and bloating every peripheral
1970 * structure with a pointer back to its parent
1971 * peripheral driver linker set entry would cost
1972 * more in the long run than doing this quick lookup.
1974 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1975 if (strcmp((*pdrv)->driver_name,
1976 periph->periph_name) == 0)
1980 if (*pdrv == NULL) {
1981 cdm->status = CAM_DEV_MATCH_ERROR;
1985 cdm->pos.cookie.pdrv = pdrv;
1987 * The periph generation slot does double duty, as
1988 * does the periph pointer slot. They are used for
1989 * both edt and pdrv lookups and positioning.
1991 cdm->pos.cookie.periph = periph;
1992 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1993 (*pdrv)->generation;
1994 cdm->status = CAM_DEV_MATCH_MORE;
1998 j = cdm->num_matches;
2000 cdm->matches[j].type = DEV_MATCH_PERIPH;
2001 cdm->matches[j].result.periph_result.path_id =
2002 periph->path->bus->path_id;
2005 * The transport layer peripheral doesn't have a target or
2008 if (periph->path->target)
2009 cdm->matches[j].result.periph_result.target_id =
2010 periph->path->target->target_id;
2012 cdm->matches[j].result.periph_result.target_id = -1;
2014 if (periph->path->device)
2015 cdm->matches[j].result.periph_result.target_lun =
2016 periph->path->device->lun_id;
2018 cdm->matches[j].result.periph_result.target_lun = -1;
2020 cdm->matches[j].result.periph_result.unit_number =
2021 periph->unit_number;
2022 strncpy(cdm->matches[j].result.periph_result.periph_name,
2023 periph->periph_name, DEV_IDLEN);
2030 xptperiphlistmatch(struct ccb_dev_match *cdm)
2034 cdm->num_matches = 0;
2037 * At this point in the edt traversal function, we check the bus
2038 * list generation to make sure that no busses have been added or
2039 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2040 * For the peripheral driver list traversal function, however, we
2041 * don't have to worry about new peripheral driver types coming or
2042 * going; they're in a linker set, and therefore can't change
2043 * without a recompile.
2046 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2047 && (cdm->pos.cookie.pdrv != NULL))
2048 ret = xptpdrvtraverse(
2049 (struct periph_driver **)cdm->pos.cookie.pdrv,
2050 xptplistpdrvfunc, cdm);
2052 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2055 * If we get back 0, that means that we had to stop before fully
2056 * traversing the peripheral driver tree. It also means that one of
2057 * the subroutines has set the status field to the proper value. If
2058 * we get back 1, we've fully traversed the EDT and copied out any
2062 cdm->status = CAM_DEV_MATCH_LAST;
2068 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2070 struct cam_eb *bus, *next_bus;
2076 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2083 * XXX The locking here is obviously very complex. We
2084 * should work to simplify it.
2087 CAM_SIM_LOCK(bus->sim);
2088 retval = tr_func(bus, arg);
2089 CAM_SIM_UNLOCK(bus->sim);
2092 next_bus = TAILQ_NEXT(bus, links);
2095 xpt_release_bus(bus);
2107 xpt_sim_opened(struct cam_sim *sim)
2110 struct cam_et *target;
2111 struct cam_ed *device;
2112 struct cam_periph *periph;
2114 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2115 mtx_assert(sim->mtx, MA_OWNED);
2118 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2119 if (bus->sim != sim)
2122 TAILQ_FOREACH(target, &bus->et_entries, links) {
2123 TAILQ_FOREACH(device, &target->ed_entries, links) {
2124 SLIST_FOREACH(periph, &device->periphs,
2126 if (periph->refcount > 0) {
2140 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2141 xpt_targetfunc_t *tr_func, void *arg)
2143 struct cam_et *target, *next_target;
2146 mtx_assert(bus->sim->mtx, MA_OWNED);
2148 for (target = (start_target ? start_target :
2149 TAILQ_FIRST(&bus->et_entries));
2150 target != NULL; target = next_target) {
2154 retval = tr_func(target, arg);
2156 next_target = TAILQ_NEXT(target, links);
2158 xpt_release_target(target);
2168 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2169 xpt_devicefunc_t *tr_func, void *arg)
2171 struct cam_ed *device, *next_device;
2174 mtx_assert(target->bus->sim->mtx, MA_OWNED);
2176 for (device = (start_device ? start_device :
2177 TAILQ_FIRST(&target->ed_entries));
2179 device = next_device) {
2182 * Hold a reference so the current device does not go away
2187 retval = tr_func(device, arg);
2190 * Grab our next pointer before we release the current
2193 next_device = TAILQ_NEXT(device, links);
2195 xpt_release_device(device);
2205 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2206 xpt_periphfunc_t *tr_func, void *arg)
2208 struct cam_periph *periph, *next_periph;
2213 mtx_assert(device->sim->mtx, MA_OWNED);
2215 for (periph = (start_periph ? start_periph :
2216 SLIST_FIRST(&device->periphs));
2218 periph = next_periph) {
2222 * In this case, we want to show peripherals that have been
2223 * invalidated, but not peripherals that are scheduled to
2224 * be freed. So instead of calling cam_periph_acquire(),
2225 * which will fail if the periph has been invalidated, we
2226 * just check for the free flag here. If it is in the
2227 * process of being freed, we skip to the next periph.
2229 if (periph->flags & CAM_PERIPH_FREE) {
2230 next_periph = SLIST_NEXT(periph, periph_links);
2235 * Acquire a reference to this periph while we call the
2236 * traversal function, so it can't go away.
2240 retval = tr_func(periph, arg);
2243 * Grab the next peripheral before we release this one, so
2244 * our next pointer is still valid.
2246 next_periph = SLIST_NEXT(periph, periph_links);
2248 cam_periph_release_locked_buses(periph);
2262 xptpdrvtraverse(struct periph_driver **start_pdrv,
2263 xpt_pdrvfunc_t *tr_func, void *arg)
2265 struct periph_driver **pdrv;
2271 * We don't traverse the peripheral driver list like we do the
2272 * other lists, because it is a linker set, and therefore cannot be
2273 * changed during runtime. If the peripheral driver list is ever
2274 * re-done to be something other than a linker set (i.e. it can
2275 * change while the system is running), the list traversal should
2276 * be modified to work like the other traversal functions.
2278 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2279 *pdrv != NULL; pdrv++) {
2280 retval = tr_func(pdrv, arg);
2290 xptpdperiphtraverse(struct periph_driver **pdrv,
2291 struct cam_periph *start_periph,
2292 xpt_periphfunc_t *tr_func, void *arg)
2294 struct cam_periph *periph, *next_periph;
2295 struct cam_sim *sim;
2301 for (periph = (start_periph ? start_periph :
2302 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2303 periph = next_periph) {
2307 * In this case, we want to show peripherals that have been
2308 * invalidated, but not peripherals that are scheduled to
2309 * be freed. So instead of calling cam_periph_acquire(),
2310 * which will fail if the periph has been invalidated, we
2311 * just check for the free flag here. If it is free, we
2312 * skip to the next periph.
2314 if (periph->flags & CAM_PERIPH_FREE) {
2315 next_periph = TAILQ_NEXT(periph, unit_links);
2320 * Acquire a reference to this periph while we call the
2321 * traversal function, so it can't go away.
2328 retval = tr_func(periph, arg);
2331 * Grab the next peripheral before we release this one, so
2332 * our next pointer is still valid.
2334 next_periph = TAILQ_NEXT(periph, unit_links);
2336 cam_periph_release_locked_buses(periph);
2337 CAM_SIM_UNLOCK(sim);
2350 xptdefbusfunc(struct cam_eb *bus, void *arg)
2352 struct xpt_traverse_config *tr_config;
2354 tr_config = (struct xpt_traverse_config *)arg;
2356 if (tr_config->depth == XPT_DEPTH_BUS) {
2357 xpt_busfunc_t *tr_func;
2359 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2361 return(tr_func(bus, tr_config->tr_arg));
2363 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2367 xptdeftargetfunc(struct cam_et *target, void *arg)
2369 struct xpt_traverse_config *tr_config;
2371 tr_config = (struct xpt_traverse_config *)arg;
2373 if (tr_config->depth == XPT_DEPTH_TARGET) {
2374 xpt_targetfunc_t *tr_func;
2376 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2378 return(tr_func(target, tr_config->tr_arg));
2380 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2384 xptdefdevicefunc(struct cam_ed *device, void *arg)
2386 struct xpt_traverse_config *tr_config;
2388 tr_config = (struct xpt_traverse_config *)arg;
2390 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2391 xpt_devicefunc_t *tr_func;
2393 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2395 return(tr_func(device, tr_config->tr_arg));
2397 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2401 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2403 struct xpt_traverse_config *tr_config;
2404 xpt_periphfunc_t *tr_func;
2406 tr_config = (struct xpt_traverse_config *)arg;
2408 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2411 * Unlike the other default functions, we don't check for depth
2412 * here. The peripheral driver level is the last level in the EDT,
2413 * so if we're here, we should execute the function in question.
2415 return(tr_func(periph, tr_config->tr_arg));
2419 * Execute the given function for every bus in the EDT.
2422 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2424 struct xpt_traverse_config tr_config;
2426 tr_config.depth = XPT_DEPTH_BUS;
2427 tr_config.tr_func = tr_func;
2428 tr_config.tr_arg = arg;
2430 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2434 * Execute the given function for every device in the EDT.
2437 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2439 struct xpt_traverse_config tr_config;
2441 tr_config.depth = XPT_DEPTH_DEVICE;
2442 tr_config.tr_func = tr_func;
2443 tr_config.tr_arg = arg;
2445 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2449 xptsetasyncfunc(struct cam_ed *device, void *arg)
2451 struct cam_path path;
2452 struct ccb_getdev cgd;
2453 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2456 * Don't report unconfigured devices (Wildcard devs,
2457 * devices only for target mode, device instances
2458 * that have been invalidated but are waiting for
2459 * their last reference count to be released).
2461 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2464 xpt_compile_path(&path,
2466 device->target->bus->path_id,
2467 device->target->target_id,
2469 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2470 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2471 xpt_action((union ccb *)&cgd);
2472 csa->callback(csa->callback_arg,
2475 xpt_release_path(&path);
2481 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2483 struct cam_path path;
2484 struct ccb_pathinq cpi;
2485 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2487 xpt_compile_path(&path, /*periph*/NULL,
2489 CAM_TARGET_WILDCARD,
2491 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2492 cpi.ccb_h.func_code = XPT_PATH_INQ;
2493 xpt_action((union ccb *)&cpi);
2494 csa->callback(csa->callback_arg,
2497 xpt_release_path(&path);
2503 xpt_action(union ccb *start_ccb)
2506 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2508 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2509 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2513 xpt_action_default(union ccb *start_ccb)
2515 struct cam_path *path;
2517 path = start_ccb->ccb_h.path;
2518 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2520 switch (start_ccb->ccb_h.func_code) {
2523 struct cam_ed *device;
2526 * For the sake of compatibility with SCSI-1
2527 * devices that may not understand the identify
2528 * message, we include lun information in the
2529 * second byte of all commands. SCSI-1 specifies
2530 * that luns are a 3 bit value and reserves only 3
2531 * bits for lun information in the CDB. Later
2532 * revisions of the SCSI spec allow for more than 8
2533 * luns, but have deprecated lun information in the
2534 * CDB. So, if the lun won't fit, we must omit.
2536 * Also be aware that during initial probing for devices,
2537 * the inquiry information is unknown but initialized to 0.
2538 * This means that this code will be exercised while probing
2539 * devices with an ANSI revision greater than 2.
2541 device = path->device;
2542 if (device->protocol_version <= SCSI_REV_2
2543 && start_ccb->ccb_h.target_lun < 8
2544 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2546 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2547 start_ccb->ccb_h.target_lun << 5;
2549 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2553 case XPT_CONT_TARGET_IO:
2554 start_ccb->csio.sense_resid = 0;
2555 start_ccb->csio.resid = 0;
2558 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2559 start_ccb->ataio.resid = 0;
2564 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2565 if (xpt_schedule_devq(path->bus->sim->devq, path->device))
2566 xpt_run_devq(path->bus->sim->devq);
2568 case XPT_CALC_GEOMETRY:
2570 struct cam_sim *sim;
2572 /* Filter out garbage */
2573 if (start_ccb->ccg.block_size == 0
2574 || start_ccb->ccg.volume_size == 0) {
2575 start_ccb->ccg.cylinders = 0;
2576 start_ccb->ccg.heads = 0;
2577 start_ccb->ccg.secs_per_track = 0;
2578 start_ccb->ccb_h.status = CAM_REQ_CMP;
2581 #if defined(PC98) || defined(__sparc64__)
2583 * In a PC-98 system, geometry translation depens on
2584 * the "real" device geometry obtained from mode page 4.
2585 * SCSI geometry translation is performed in the
2586 * initialization routine of the SCSI BIOS and the result
2587 * stored in host memory. If the translation is available
2588 * in host memory, use it. If not, rely on the default
2589 * translation the device driver performs.
2590 * For sparc64, we may need adjust the geometry of large
2591 * disks in order to fit the limitations of the 16-bit
2592 * fields of the VTOC8 disk label.
2594 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2595 start_ccb->ccb_h.status = CAM_REQ_CMP;
2599 sim = path->bus->sim;
2600 (*(sim->sim_action))(sim, start_ccb);
2605 union ccb* abort_ccb;
2607 abort_ccb = start_ccb->cab.abort_ccb;
2608 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2610 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2611 struct cam_ccbq *ccbq;
2612 struct cam_ed *device;
2614 device = abort_ccb->ccb_h.path->device;
2615 ccbq = &device->ccbq;
2616 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2617 abort_ccb->ccb_h.status =
2618 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2619 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2620 xpt_done(abort_ccb);
2621 start_ccb->ccb_h.status = CAM_REQ_CMP;
2624 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2625 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2627 * We've caught this ccb en route to
2628 * the SIM. Flag it for abort and the
2629 * SIM will do so just before starting
2630 * real work on the CCB.
2632 abort_ccb->ccb_h.status =
2633 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2634 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2635 start_ccb->ccb_h.status = CAM_REQ_CMP;
2639 if (XPT_FC_IS_QUEUED(abort_ccb)
2640 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2642 * It's already completed but waiting
2643 * for our SWI to get to it.
2645 start_ccb->ccb_h.status = CAM_UA_ABORT;
2649 * If we weren't able to take care of the abort request
2650 * in the XPT, pass the request down to the SIM for processing.
2654 case XPT_ACCEPT_TARGET_IO:
2656 case XPT_IMMED_NOTIFY:
2657 case XPT_NOTIFY_ACK:
2659 case XPT_IMMEDIATE_NOTIFY:
2660 case XPT_NOTIFY_ACKNOWLEDGE:
2661 case XPT_GET_SIM_KNOB:
2662 case XPT_SET_SIM_KNOB:
2664 struct cam_sim *sim;
2666 sim = path->bus->sim;
2667 (*(sim->sim_action))(sim, start_ccb);
2672 struct cam_sim *sim;
2674 sim = path->bus->sim;
2675 (*(sim->sim_action))(sim, start_ccb);
2678 case XPT_PATH_STATS:
2679 start_ccb->cpis.last_reset = path->bus->last_reset;
2680 start_ccb->ccb_h.status = CAM_REQ_CMP;
2687 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2688 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2690 struct ccb_getdev *cgd;
2692 cgd = &start_ccb->cgd;
2693 cgd->protocol = dev->protocol;
2694 cgd->inq_data = dev->inq_data;
2695 cgd->ident_data = dev->ident_data;
2696 cgd->inq_flags = dev->inq_flags;
2697 cgd->ccb_h.status = CAM_REQ_CMP;
2698 cgd->serial_num_len = dev->serial_num_len;
2699 if ((dev->serial_num_len > 0)
2700 && (dev->serial_num != NULL))
2701 bcopy(dev->serial_num, cgd->serial_num,
2702 dev->serial_num_len);
2706 case XPT_GDEV_STATS:
2711 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2712 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2714 struct ccb_getdevstats *cgds;
2718 cgds = &start_ccb->cgds;
2721 cgds->dev_openings = dev->ccbq.dev_openings;
2722 cgds->dev_active = dev->ccbq.dev_active;
2723 cgds->devq_openings = dev->ccbq.devq_openings;
2724 cgds->devq_queued = dev->ccbq.queue.entries;
2725 cgds->held = dev->ccbq.held;
2726 cgds->last_reset = tar->last_reset;
2727 cgds->maxtags = dev->maxtags;
2728 cgds->mintags = dev->mintags;
2729 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2730 cgds->last_reset = bus->last_reset;
2731 cgds->ccb_h.status = CAM_REQ_CMP;
2737 struct cam_periph *nperiph;
2738 struct periph_list *periph_head;
2739 struct ccb_getdevlist *cgdl;
2741 struct cam_ed *device;
2748 * Don't want anyone mucking with our data.
2750 device = path->device;
2751 periph_head = &device->periphs;
2752 cgdl = &start_ccb->cgdl;
2755 * Check and see if the list has changed since the user
2756 * last requested a list member. If so, tell them that the
2757 * list has changed, and therefore they need to start over
2758 * from the beginning.
2760 if ((cgdl->index != 0) &&
2761 (cgdl->generation != device->generation)) {
2762 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2767 * Traverse the list of peripherals and attempt to find
2768 * the requested peripheral.
2770 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2771 (nperiph != NULL) && (i <= cgdl->index);
2772 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2773 if (i == cgdl->index) {
2774 strncpy(cgdl->periph_name,
2775 nperiph->periph_name,
2777 cgdl->unit_number = nperiph->unit_number;
2782 cgdl->status = CAM_GDEVLIST_ERROR;
2786 if (nperiph == NULL)
2787 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2789 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2792 cgdl->generation = device->generation;
2794 cgdl->ccb_h.status = CAM_REQ_CMP;
2799 dev_pos_type position_type;
2800 struct ccb_dev_match *cdm;
2802 cdm = &start_ccb->cdm;
2805 * There are two ways of getting at information in the EDT.
2806 * The first way is via the primary EDT tree. It starts
2807 * with a list of busses, then a list of targets on a bus,
2808 * then devices/luns on a target, and then peripherals on a
2809 * device/lun. The "other" way is by the peripheral driver
2810 * lists. The peripheral driver lists are organized by
2811 * peripheral driver. (obviously) So it makes sense to
2812 * use the peripheral driver list if the user is looking
2813 * for something like "da1", or all "da" devices. If the
2814 * user is looking for something on a particular bus/target
2815 * or lun, it's generally better to go through the EDT tree.
2818 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2819 position_type = cdm->pos.position_type;
2823 position_type = CAM_DEV_POS_NONE;
2825 for (i = 0; i < cdm->num_patterns; i++) {
2826 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2827 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2828 position_type = CAM_DEV_POS_EDT;
2833 if (cdm->num_patterns == 0)
2834 position_type = CAM_DEV_POS_EDT;
2835 else if (position_type == CAM_DEV_POS_NONE)
2836 position_type = CAM_DEV_POS_PDRV;
2840 * Note that we drop the SIM lock here, because the EDT
2841 * traversal code needs to do its own locking.
2843 CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
2844 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2845 case CAM_DEV_POS_EDT:
2848 case CAM_DEV_POS_PDRV:
2849 xptperiphlistmatch(cdm);
2852 cdm->status = CAM_DEV_MATCH_ERROR;
2855 CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
2857 if (cdm->status == CAM_DEV_MATCH_ERROR)
2858 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2860 start_ccb->ccb_h.status = CAM_REQ_CMP;
2866 struct ccb_setasync *csa;
2867 struct async_node *cur_entry;
2868 struct async_list *async_head;
2871 csa = &start_ccb->csa;
2872 added = csa->event_enable;
2873 async_head = &path->device->asyncs;
2876 * If there is already an entry for us, simply
2879 cur_entry = SLIST_FIRST(async_head);
2880 while (cur_entry != NULL) {
2881 if ((cur_entry->callback_arg == csa->callback_arg)
2882 && (cur_entry->callback == csa->callback))
2884 cur_entry = SLIST_NEXT(cur_entry, links);
2887 if (cur_entry != NULL) {
2889 * If the request has no flags set,
2892 added &= ~cur_entry->event_enable;
2893 if (csa->event_enable == 0) {
2894 SLIST_REMOVE(async_head, cur_entry,
2896 xpt_release_device(path->device);
2897 free(cur_entry, M_CAMXPT);
2899 cur_entry->event_enable = csa->event_enable;
2901 csa->event_enable = added;
2903 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2905 if (cur_entry == NULL) {
2906 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2909 cur_entry->event_enable = csa->event_enable;
2910 cur_entry->callback_arg = csa->callback_arg;
2911 cur_entry->callback = csa->callback;
2912 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2913 xpt_acquire_device(path->device);
2915 start_ccb->ccb_h.status = CAM_REQ_CMP;
2920 struct ccb_relsim *crs;
2923 crs = &start_ccb->crs;
2927 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2931 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2933 /* Don't ever go below one opening */
2934 if (crs->openings > 0) {
2935 xpt_dev_ccbq_resize(path, crs->openings);
2938 "number of openings is now %d\n",
2944 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2946 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2949 * Just extend the old timeout and decrement
2950 * the freeze count so that a single timeout
2951 * is sufficient for releasing the queue.
2953 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2954 callout_stop(&dev->callout);
2957 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2960 callout_reset(&dev->callout,
2961 (crs->release_timeout * hz) / 1000,
2962 xpt_release_devq_timeout, dev);
2964 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2968 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2970 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2972 * Decrement the freeze count so that a single
2973 * completion is still sufficient to unfreeze
2976 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2979 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2980 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2984 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2986 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2987 || (dev->ccbq.dev_active == 0)) {
2989 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2992 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2993 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2997 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2998 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2999 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3000 start_ccb->ccb_h.status = CAM_REQ_CMP;
3004 struct cam_path *oldpath;
3005 struct cam_sim *oldsim;
3007 /* Check that all request bits are supported. */
3008 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3009 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3013 cam_dflags = CAM_DEBUG_NONE;
3014 if (cam_dpath != NULL) {
3015 /* To release the old path we must hold proper lock. */
3016 oldpath = cam_dpath;
3018 oldsim = xpt_path_sim(oldpath);
3019 CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
3020 CAM_SIM_LOCK(oldsim);
3021 xpt_free_path(oldpath);
3022 CAM_SIM_UNLOCK(oldsim);
3023 CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
3025 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3026 if (xpt_create_path(&cam_dpath, NULL,
3027 start_ccb->ccb_h.path_id,
3028 start_ccb->ccb_h.target_id,
3029 start_ccb->ccb_h.target_lun) !=
3031 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3033 cam_dflags = start_ccb->cdbg.flags;
3034 start_ccb->ccb_h.status = CAM_REQ_CMP;
3035 xpt_print(cam_dpath, "debugging flags now %x\n",
3039 start_ccb->ccb_h.status = CAM_REQ_CMP;
3043 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3044 xpt_freeze_devq(path, 1);
3045 start_ccb->ccb_h.status = CAM_REQ_CMP;
3052 printf("%s: CCB type %#x not supported\n", __func__,
3053 start_ccb->ccb_h.func_code);
3054 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3055 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3056 xpt_done(start_ccb);
3063 xpt_polled_action(union ccb *start_ccb)
3066 struct cam_sim *sim;
3067 struct cam_devq *devq;
3071 timeout = start_ccb->ccb_h.timeout * 10;
3072 sim = start_ccb->ccb_h.path->bus->sim;
3074 dev = start_ccb->ccb_h.path->device;
3076 mtx_assert(sim->mtx, MA_OWNED);
3078 /* Don't use ISR for this SIM while polling. */
3079 sim->flags |= CAM_SIM_POLLED;
3082 * Steal an opening so that no other queued requests
3083 * can get it before us while we simulate interrupts.
3085 dev->ccbq.devq_openings--;
3086 dev->ccbq.dev_openings--;
3088 while(((devq != NULL && devq->send_openings <= 0) ||
3089 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3091 (*(sim->sim_poll))(sim);
3092 camisr_runqueue(&sim->sim_doneq);
3095 dev->ccbq.devq_openings++;
3096 dev->ccbq.dev_openings++;
3099 xpt_action(start_ccb);
3100 while(--timeout > 0) {
3101 (*(sim->sim_poll))(sim);
3102 camisr_runqueue(&sim->sim_doneq);
3103 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3110 * XXX Is it worth adding a sim_timeout entry
3111 * point so we can attempt recovery? If
3112 * this is only used for dumps, I don't think
3115 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3118 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3121 /* We will use CAM ISR for this SIM again. */
3122 sim->flags &= ~CAM_SIM_POLLED;
3126 * Schedule a peripheral driver to receive a ccb when it's
3127 * target device has space for more transactions.
3130 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3132 struct cam_ed *device;
3135 mtx_assert(perph->sim->mtx, MA_OWNED);
3137 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3138 device = perph->path->device;
3139 if (periph_is_queued(perph)) {
3140 /* Simply reorder based on new priority */
3141 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3142 (" change priority to %d\n", new_priority));
3143 if (new_priority < perph->pinfo.priority) {
3144 camq_change_priority(&device->drvq,
3150 /* New entry on the queue */
3151 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3152 (" added periph to queue\n"));
3153 perph->pinfo.priority = new_priority;
3154 perph->pinfo.generation = ++device->drvq.generation;
3155 camq_insert(&device->drvq, &perph->pinfo);
3159 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3160 (" calling xpt_run_dev_allocq\n"));
3161 xpt_run_dev_allocq(device);
3167 * Schedule a device to run on a given queue.
3168 * If the device was inserted as a new entry on the queue,
3169 * return 1 meaning the device queue should be run. If we
3170 * were already queued, implying someone else has already
3171 * started the queue, return 0 so the caller doesn't attempt
3175 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3176 u_int32_t new_priority)
3179 u_int32_t old_priority;
3181 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3183 old_priority = pinfo->priority;
3186 * Are we already queued?
3188 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3189 /* Simply reorder based on new priority */
3190 if (new_priority < old_priority) {
3191 camq_change_priority(queue, pinfo->index,
3193 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3194 ("changed priority to %d\n",
3200 /* New entry on the queue */
3201 if (new_priority < old_priority)
3202 pinfo->priority = new_priority;
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3205 ("Inserting onto queue\n"));
3206 pinfo->generation = ++queue->generation;
3207 camq_insert(queue, pinfo);
3214 xpt_run_dev_allocq(struct cam_ed *device)
3218 if (device->ccbq.devq_allocating)
3220 device->ccbq.devq_allocating = 1;
3221 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq(%p)\n", device));
3222 drvq = &device->drvq;
3223 while ((drvq->entries > 0) &&
3224 (device->ccbq.devq_openings > 0 ||
3225 CAMQ_GET_PRIO(drvq) <= CAM_PRIORITY_OOB) &&
3226 (device->ccbq.queue.qfrozen_cnt == 0)) {
3227 union ccb *work_ccb;
3228 struct cam_periph *drv;
3230 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3231 "Device on queue without any work to do"));
3232 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3233 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3234 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3235 drv->pinfo.priority);
3236 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3237 ("calling periph start\n"));
3238 drv->periph_start(drv, work_ccb);
3241 * Malloc failure in alloc_ccb
3244 * XXX add us to a list to be run from free_ccb
3245 * if we don't have any ccbs active on this
3246 * device queue otherwise we may never get run
3252 device->ccbq.devq_allocating = 0;
3256 xpt_run_devq(struct cam_devq *devq)
3258 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3260 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3262 devq->send_queue.qfrozen_cnt++;
3263 while ((devq->send_queue.entries > 0)
3264 && (devq->send_openings > 0)
3265 && (devq->send_queue.qfrozen_cnt <= 1)) {
3266 struct cam_ed_qinfo *qinfo;
3267 struct cam_ed *device;
3268 union ccb *work_ccb;
3269 struct cam_sim *sim;
3271 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3273 device = qinfo->device;
3274 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3275 ("running device %p\n", device));
3277 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3278 if (work_ccb == NULL) {
3279 printf("device on run queue with no ccbs???\n");
3283 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3285 mtx_lock(&xsoftc.xpt_lock);
3286 if (xsoftc.num_highpower <= 0) {
3288 * We got a high power command, but we
3289 * don't have any available slots. Freeze
3290 * the device queue until we have a slot
3293 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3294 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3298 mtx_unlock(&xsoftc.xpt_lock);
3302 * Consume a high power slot while
3305 xsoftc.num_highpower--;
3307 mtx_unlock(&xsoftc.xpt_lock);
3309 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3310 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3312 devq->send_openings--;
3313 devq->send_active++;
3315 xpt_schedule_devq(devq, device);
3317 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3319 * The client wants to freeze the queue
3320 * after this CCB is sent.
3322 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3325 /* In Target mode, the peripheral driver knows best... */
3326 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3327 if ((device->inq_flags & SID_CmdQue) != 0
3328 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3329 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3332 * Clear this in case of a retried CCB that
3333 * failed due to a rejected tag.
3335 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3338 switch (work_ccb->ccb_h.func_code) {
3340 CAM_DEBUG(work_ccb->ccb_h.path,
3341 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3342 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3344 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3345 cdb_str, sizeof(cdb_str))));
3348 CAM_DEBUG(work_ccb->ccb_h.path,
3349 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3350 ata_op_string(&work_ccb->ataio.cmd),
3351 ata_cmd_string(&work_ccb->ataio.cmd,
3352 cdb_str, sizeof(cdb_str))));
3359 * Device queues can be shared among multiple sim instances
3360 * that reside on different busses. Use the SIM in the queue
3361 * CCB's path, rather than the one in the bus that was passed
3362 * into this function.
3364 sim = work_ccb->ccb_h.path->bus->sim;
3365 (*(sim->sim_action))(sim, work_ccb);
3367 devq->send_queue.qfrozen_cnt--;
3371 * This function merges stuff from the slave ccb into the master ccb, while
3372 * keeping important fields in the master ccb constant.
3375 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3379 * Pull fields that are valid for peripheral drivers to set
3380 * into the master CCB along with the CCB "payload".
3382 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3383 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3384 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3385 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3386 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3387 sizeof(union ccb) - sizeof(struct ccb_hdr));
3391 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3394 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3395 ccb_h->pinfo.priority = priority;
3397 ccb_h->path_id = path->bus->path_id;
3399 ccb_h->target_id = path->target->target_id;
3401 ccb_h->target_id = CAM_TARGET_WILDCARD;
3403 ccb_h->target_lun = path->device->lun_id;
3404 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3406 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3408 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3412 /* Path manipulation functions */
3414 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3415 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3417 struct cam_path *path;
3420 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3423 status = CAM_RESRC_UNAVAIL;
3426 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3427 if (status != CAM_REQ_CMP) {
3428 free(path, M_CAMPATH);
3431 *new_path_ptr = path;
3436 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3437 struct cam_periph *periph, path_id_t path_id,
3438 target_id_t target_id, lun_id_t lun_id)
3440 struct cam_path *path;
3441 struct cam_eb *bus = NULL;
3444 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
3446 bus = xpt_find_bus(path_id);
3448 CAM_SIM_LOCK(bus->sim);
3449 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3451 CAM_SIM_UNLOCK(bus->sim);
3452 xpt_release_bus(bus);
3454 if (status != CAM_REQ_CMP) {
3455 free(path, M_CAMPATH);
3458 *new_path_ptr = path;
3463 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3464 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3467 struct cam_et *target;
3468 struct cam_ed *device;
3471 status = CAM_REQ_CMP; /* Completed without error */
3472 target = NULL; /* Wildcarded */
3473 device = NULL; /* Wildcarded */
3476 * We will potentially modify the EDT, so block interrupts
3477 * that may attempt to create cam paths.
3479 bus = xpt_find_bus(path_id);
3481 status = CAM_PATH_INVALID;
3483 target = xpt_find_target(bus, target_id);
3484 if (target == NULL) {
3486 struct cam_et *new_target;
3488 new_target = xpt_alloc_target(bus, target_id);
3489 if (new_target == NULL) {
3490 status = CAM_RESRC_UNAVAIL;
3492 target = new_target;
3495 if (target != NULL) {
3496 device = xpt_find_device(target, lun_id);
3497 if (device == NULL) {
3499 struct cam_ed *new_device;
3502 (*(bus->xport->alloc_device))(bus,
3505 if (new_device == NULL) {
3506 status = CAM_RESRC_UNAVAIL;
3508 device = new_device;
3515 * Only touch the user's data if we are successful.
3517 if (status == CAM_REQ_CMP) {
3518 new_path->periph = perph;
3519 new_path->bus = bus;
3520 new_path->target = target;
3521 new_path->device = device;
3522 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3525 xpt_release_device(device);
3527 xpt_release_target(target);
3529 xpt_release_bus(bus);
3535 xpt_release_path(struct cam_path *path)
3537 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3538 if (path->device != NULL) {
3539 xpt_release_device(path->device);
3540 path->device = NULL;
3542 if (path->target != NULL) {
3543 xpt_release_target(path->target);
3544 path->target = NULL;
3546 if (path->bus != NULL) {
3547 xpt_release_bus(path->bus);
3553 xpt_free_path(struct cam_path *path)
3556 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3557 xpt_release_path(path);
3558 free(path, M_CAMPATH);
3562 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3563 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3569 *bus_ref = path->bus->refcount;
3575 *periph_ref = path->periph->refcount;
3582 *target_ref = path->target->refcount;
3588 *device_ref = path->device->refcount;
3595 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3596 * in path1, 2 for match with wildcards in path2.
3599 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3603 if (path1->bus != path2->bus) {
3604 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3606 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3611 if (path1->target != path2->target) {
3612 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3615 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3620 if (path1->device != path2->device) {
3621 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3624 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3633 xpt_print_path(struct cam_path *path)
3637 printf("(nopath): ");
3639 if (path->periph != NULL)
3640 printf("(%s%d:", path->periph->periph_name,
3641 path->periph->unit_number);
3643 printf("(noperiph:");
3645 if (path->bus != NULL)
3646 printf("%s%d:%d:", path->bus->sim->sim_name,
3647 path->bus->sim->unit_number,
3648 path->bus->sim->bus_id);
3652 if (path->target != NULL)
3653 printf("%d:", path->target->target_id);
3657 if (path->device != NULL)
3658 printf("%d): ", path->device->lun_id);
3665 xpt_print(struct cam_path *path, const char *fmt, ...)
3668 xpt_print_path(path);
3675 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3680 if (path != NULL && path->bus != NULL)
3681 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3684 sbuf_new(&sb, str, str_len, 0);
3687 sbuf_printf(&sb, "(nopath): ");
3689 if (path->periph != NULL)
3690 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3691 path->periph->unit_number);
3693 sbuf_printf(&sb, "(noperiph:");
3695 if (path->bus != NULL)
3696 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3697 path->bus->sim->unit_number,
3698 path->bus->sim->bus_id);
3700 sbuf_printf(&sb, "nobus:");
3702 if (path->target != NULL)
3703 sbuf_printf(&sb, "%d:", path->target->target_id);
3705 sbuf_printf(&sb, "X:");
3707 if (path->device != NULL)
3708 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3710 sbuf_printf(&sb, "X): ");
3714 return(sbuf_len(&sb));
3718 xpt_path_path_id(struct cam_path *path)
3720 return(path->bus->path_id);
3724 xpt_path_target_id(struct cam_path *path)
3726 if (path->target != NULL)
3727 return (path->target->target_id);
3729 return (CAM_TARGET_WILDCARD);
3733 xpt_path_lun_id(struct cam_path *path)
3735 if (path->device != NULL)
3736 return (path->device->lun_id);
3738 return (CAM_LUN_WILDCARD);
3742 xpt_path_sim(struct cam_path *path)
3745 return (path->bus->sim);
3749 xpt_path_periph(struct cam_path *path)
3751 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3753 return (path->periph);
3757 xpt_path_legacy_ata_id(struct cam_path *path)
3762 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3763 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3764 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3765 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3768 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3769 path->bus->sim->unit_number < 2) {
3770 bus_id = path->bus->sim->unit_number;
3774 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3775 if (bus == path->bus)
3777 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3778 bus->sim->unit_number >= 2) ||
3779 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3780 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3781 strcmp(bus->sim->sim_name, "siisch") == 0)
3786 if (path->target != NULL) {
3787 if (path->target->target_id < 2)
3788 return (bus_id * 2 + path->target->target_id);
3792 return (bus_id * 2);
3796 * Release a CAM control block for the caller. Remit the cost of the structure
3797 * to the device referenced by the path. If the this device had no 'credits'
3798 * and peripheral drivers have registered async callbacks for this notification
3802 xpt_release_ccb(union ccb *free_ccb)
3804 struct cam_path *path;
3805 struct cam_ed *device;
3807 struct cam_sim *sim;
3809 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3810 path = free_ccb->ccb_h.path;
3811 device = path->device;
3815 mtx_assert(sim->mtx, MA_OWNED);
3817 cam_ccbq_release_opening(&device->ccbq);
3818 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3819 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3820 cam_ccbq_resize(&device->ccbq,
3821 device->ccbq.dev_openings + device->ccbq.dev_active);
3823 if (sim->ccb_count > sim->max_ccbs) {
3824 xpt_free_ccb(free_ccb);
3827 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3830 xpt_run_dev_allocq(device);
3833 /* Functions accessed by SIM drivers */
3835 static struct xpt_xport xport_default = {
3836 .alloc_device = xpt_alloc_device_default,
3837 .action = xpt_action_default,
3838 .async = xpt_dev_async_default,
3842 * A sim structure, listing the SIM entry points and instance
3843 * identification info is passed to xpt_bus_register to hook the SIM
3844 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3845 * for this new bus and places it in the array of busses and assigns
3846 * it a path_id. The path_id may be influenced by "hard wiring"
3847 * information specified by the user. Once interrupt services are
3848 * available, the bus will be probed.
3851 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3853 struct cam_eb *new_bus;
3854 struct cam_eb *old_bus;
3855 struct ccb_pathinq cpi;
3856 struct cam_path *path;
3859 mtx_assert(sim->mtx, MA_OWNED);
3862 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3863 M_CAMXPT, M_NOWAIT);
3864 if (new_bus == NULL) {
3865 /* Couldn't satisfy request */
3866 return (CAM_RESRC_UNAVAIL);
3868 if (strcmp(sim->sim_name, "xpt") != 0) {
3870 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3873 TAILQ_INIT(&new_bus->et_entries);
3874 new_bus->path_id = sim->path_id;
3877 timevalclear(&new_bus->last_reset);
3879 new_bus->refcount = 1; /* Held until a bus_deregister event */
3880 new_bus->generation = 0;
3883 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3884 while (old_bus != NULL
3885 && old_bus->path_id < new_bus->path_id)
3886 old_bus = TAILQ_NEXT(old_bus, links);
3887 if (old_bus != NULL)
3888 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3890 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3891 xsoftc.bus_generation++;
3895 * Set a default transport so that a PATH_INQ can be issued to
3896 * the SIM. This will then allow for probing and attaching of
3897 * a more appropriate transport.
3899 new_bus->xport = &xport_default;
3901 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3902 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3903 if (status != CAM_REQ_CMP) {
3904 xpt_release_bus(new_bus);
3905 free(path, M_CAMXPT);
3906 return (CAM_RESRC_UNAVAIL);
3909 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3910 cpi.ccb_h.func_code = XPT_PATH_INQ;
3911 xpt_action((union ccb *)&cpi);
3913 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3914 switch (cpi.transport) {
3921 new_bus->xport = scsi_get_xport();
3925 new_bus->xport = ata_get_xport();
3928 new_bus->xport = &xport_default;
3933 /* Notify interested parties */
3934 if (sim->path_id != CAM_XPT_PATH_ID) {
3936 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3937 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3938 union ccb *scan_ccb;
3940 /* Initiate bus rescan. */
3941 scan_ccb = xpt_alloc_ccb_nowait();
3942 if (scan_ccb != NULL) {
3943 scan_ccb->ccb_h.path = path;
3944 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3945 scan_ccb->crcn.flags = 0;
3946 xpt_rescan(scan_ccb);
3949 "Can't allocate CCB to scan bus\n");
3951 xpt_free_path(path);
3953 xpt_free_path(path);
3954 return (CAM_SUCCESS);
3958 xpt_bus_deregister(path_id_t pathid)
3960 struct cam_path bus_path;
3963 status = xpt_compile_path(&bus_path, NULL, pathid,
3964 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3965 if (status != CAM_REQ_CMP)
3968 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3969 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3971 /* Release the reference count held while registered. */
3972 xpt_release_bus(bus_path.bus);
3973 xpt_release_path(&bus_path);
3975 return (CAM_REQ_CMP);
3979 xptnextfreepathid(void)
3987 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3989 /* Find an unoccupied pathid */
3990 while (bus != NULL && bus->path_id <= pathid) {
3991 if (bus->path_id == pathid)
3993 bus = TAILQ_NEXT(bus, links);
3998 * Ensure that this pathid is not reserved for
3999 * a bus that may be registered in the future.
4001 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4003 /* Start the search over */
4011 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4018 pathid = CAM_XPT_PATH_ID;
4019 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4021 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4022 if (strcmp(dname, "scbus")) {
4023 /* Avoid a bit of foot shooting. */
4026 if (dunit < 0) /* unwired?! */
4028 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4029 if (sim_bus == val) {
4033 } else if (sim_bus == 0) {
4034 /* Unspecified matches bus 0 */
4038 printf("Ambiguous scbus configuration for %s%d "
4039 "bus %d, cannot wire down. The kernel "
4040 "config entry for scbus%d should "
4041 "specify a controller bus.\n"
4042 "Scbus will be assigned dynamically.\n",
4043 sim_name, sim_unit, sim_bus, dunit);
4048 if (pathid == CAM_XPT_PATH_ID)
4049 pathid = xptnextfreepathid();
4054 xpt_async_string(u_int32_t async_code)
4057 switch (async_code) {
4058 case AC_BUS_RESET: return ("AC_BUS_RESET");
4059 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4060 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4061 case AC_SENT_BDR: return ("AC_SENT_BDR");
4062 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4063 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4064 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4065 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4066 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4067 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4068 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4069 case AC_CONTRACT: return ("AC_CONTRACT");
4070 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4071 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4073 return ("AC_UNKNOWN");
4077 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4080 struct cam_et *target, *next_target;
4081 struct cam_ed *device, *next_device;
4083 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4084 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4085 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4088 * Most async events come from a CAM interrupt context. In
4089 * a few cases, the error recovery code at the peripheral layer,
4090 * which may run from our SWI or a process context, may signal
4091 * deferred events with a call to xpt_async.
4096 if (async_code == AC_BUS_RESET) {
4097 /* Update our notion of when the last reset occurred */
4098 microtime(&bus->last_reset);
4101 for (target = TAILQ_FIRST(&bus->et_entries);
4103 target = next_target) {
4105 next_target = TAILQ_NEXT(target, links);
4107 if (path->target != target
4108 && path->target->target_id != CAM_TARGET_WILDCARD
4109 && target->target_id != CAM_TARGET_WILDCARD)
4112 if (async_code == AC_SENT_BDR) {
4113 /* Update our notion of when the last reset occurred */
4114 microtime(&path->target->last_reset);
4117 for (device = TAILQ_FIRST(&target->ed_entries);
4119 device = next_device) {
4121 next_device = TAILQ_NEXT(device, links);
4123 if (path->device != device
4124 && path->device->lun_id != CAM_LUN_WILDCARD
4125 && device->lun_id != CAM_LUN_WILDCARD)
4128 * The async callback could free the device.
4129 * If it is a broadcast async, it doesn't hold
4130 * device reference, so take our own reference.
4132 xpt_acquire_device(device);
4133 (*(bus->xport->async))(async_code, bus,
4137 xpt_async_bcast(&device->asyncs, async_code,
4139 xpt_release_device(device);
4144 * If this wasn't a fully wildcarded async, tell all
4145 * clients that want all async events.
4147 if (bus != xpt_periph->path->bus)
4148 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4153 xpt_async_bcast(struct async_list *async_head,
4154 u_int32_t async_code,
4155 struct cam_path *path, void *async_arg)
4157 struct async_node *cur_entry;
4159 cur_entry = SLIST_FIRST(async_head);
4160 while (cur_entry != NULL) {
4161 struct async_node *next_entry;
4163 * Grab the next list entry before we call the current
4164 * entry's callback. This is because the callback function
4165 * can delete its async callback entry.
4167 next_entry = SLIST_NEXT(cur_entry, links);
4168 if ((cur_entry->event_enable & async_code) != 0)
4169 cur_entry->callback(cur_entry->callback_arg,
4172 cur_entry = next_entry;
4177 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4178 struct cam_et *target, struct cam_ed *device,
4181 printf("%s called\n", __func__);
4185 xpt_freeze_devq(struct cam_path *path, u_int count)
4187 struct cam_ed *dev = path->device;
4189 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4190 dev->ccbq.queue.qfrozen_cnt += count;
4191 /* Remove frozen device from sendq. */
4192 if (device_is_queued(dev)) {
4193 camq_remove(&dev->sim->devq->send_queue,
4194 dev->devq_entry.pinfo.index);
4196 return (dev->ccbq.queue.qfrozen_cnt);
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 += count;
4205 return (sim->devq->send_queue.qfrozen_cnt);
4209 xpt_release_devq_timeout(void *arg)
4211 struct cam_ed *device;
4213 device = (struct cam_ed *)arg;
4214 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4218 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4221 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4222 xpt_release_devq_device(path->device, count, run_queue);
4226 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4229 if (count > dev->ccbq.queue.qfrozen_cnt) {
4231 printf("xpt_release_devq(): requested %u > present %u\n",
4232 count, dev->ccbq.queue.qfrozen_cnt);
4234 count = dev->ccbq.queue.qfrozen_cnt;
4236 dev->ccbq.queue.qfrozen_cnt -= count;
4237 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4239 * No longer need to wait for a successful
4240 * command completion.
4242 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4244 * Remove any timeouts that might be scheduled
4245 * to release this queue.
4247 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4248 callout_stop(&dev->callout);
4249 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4251 xpt_run_dev_allocq(dev);
4255 * Now that we are unfrozen schedule the
4256 * device so any pending transactions are
4259 if (xpt_schedule_devq(dev->sim->devq, dev))
4260 xpt_run_devq(dev->sim->devq);
4265 xpt_release_simq(struct cam_sim *sim, int run_queue)
4269 mtx_assert(sim->mtx, MA_OWNED);
4270 sendq = &(sim->devq->send_queue);
4271 if (sendq->qfrozen_cnt <= 0) {
4273 printf("xpt_release_simq: requested 1 > present %u\n",
4274 sendq->qfrozen_cnt);
4277 sendq->qfrozen_cnt--;
4278 if (sendq->qfrozen_cnt == 0) {
4280 * If there is a timeout scheduled to release this
4281 * sim queue, remove it. The queue frozen count is
4284 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4285 callout_stop(&sim->callout);
4286 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4290 * Now that we are unfrozen run the send queue.
4292 xpt_run_devq(sim->devq);
4298 * XXX Appears to be unused.
4301 xpt_release_simq_timeout(void *arg)
4303 struct cam_sim *sim;
4305 sim = (struct cam_sim *)arg;
4306 xpt_release_simq(sim, /* run_queue */ TRUE);
4310 xpt_done(union ccb *done_ccb)
4312 struct cam_sim *sim;
4315 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4316 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4318 * Queue up the request for handling by our SWI handler
4319 * any of the "non-immediate" type of ccbs.
4321 sim = done_ccb->ccb_h.path->bus->sim;
4322 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4324 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4325 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4326 CAM_SIM_BATCH)) == 0) {
4327 mtx_lock(&cam_simq_lock);
4328 first = TAILQ_EMPTY(&cam_simq);
4329 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4330 mtx_unlock(&cam_simq_lock);
4331 sim->flags |= CAM_SIM_ON_DONEQ;
4333 swi_sched(cambio_ih, 0);
4339 xpt_batch_start(struct cam_sim *sim)
4342 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4343 sim->flags |= CAM_SIM_BATCH;
4347 xpt_batch_done(struct cam_sim *sim)
4350 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4351 sim->flags &= ~CAM_SIM_BATCH;
4352 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4353 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4354 camisr_runqueue(&sim->sim_doneq);
4362 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4367 xpt_alloc_ccb_nowait()
4371 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4376 xpt_free_ccb(union ccb *free_ccb)
4378 free(free_ccb, M_CAMCCB);
4383 /* Private XPT functions */
4386 * Get a CAM control block for the caller. Charge the structure to the device
4387 * referenced by the path. If the this device has no 'credits' then the
4388 * device already has the maximum number of outstanding operations under way
4389 * and we return NULL. If we don't have sufficient resources to allocate more
4390 * ccbs, we also return NULL.
4393 xpt_get_ccb(struct cam_ed *device)
4396 struct cam_sim *sim;
4399 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4400 new_ccb = xpt_alloc_ccb_nowait();
4401 if (new_ccb == NULL) {
4404 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4405 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4406 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4410 cam_ccbq_take_opening(&device->ccbq);
4411 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4416 xpt_release_bus(struct cam_eb *bus)
4420 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4421 if (--bus->refcount > 0) {
4425 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4426 ("refcount is zero, but target list is not empty"));
4427 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4428 xsoftc.bus_generation++;
4430 cam_sim_release(bus->sim);
4431 free(bus, M_CAMXPT);
4434 static struct cam_et *
4435 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4437 struct cam_et *cur_target, *target;
4439 mtx_assert(bus->sim->mtx, MA_OWNED);
4440 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4445 TAILQ_INIT(&target->ed_entries);
4447 target->target_id = target_id;
4448 target->refcount = 1;
4449 target->generation = 0;
4450 target->luns = NULL;
4451 timevalclear(&target->last_reset);
4453 * Hold a reference to our parent bus so it
4454 * will not go away before we do.
4460 /* Insertion sort into our bus's target list */
4461 cur_target = TAILQ_FIRST(&bus->et_entries);
4462 while (cur_target != NULL && cur_target->target_id < target_id)
4463 cur_target = TAILQ_NEXT(cur_target, links);
4464 if (cur_target != NULL) {
4465 TAILQ_INSERT_BEFORE(cur_target, target, links);
4467 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4474 xpt_release_target(struct cam_et *target)
4477 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4478 if (--target->refcount > 0)
4480 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4481 ("refcount is zero, but device list is not empty"));
4482 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4483 target->bus->generation++;
4484 xpt_release_bus(target->bus);
4486 free(target->luns, M_CAMXPT);
4487 free(target, M_CAMXPT);
4490 static struct cam_ed *
4491 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4494 struct cam_ed *device;
4496 device = xpt_alloc_device(bus, target, lun_id);
4500 device->mintags = 1;
4501 device->maxtags = 1;
4502 bus->sim->max_ccbs += device->ccbq.devq_openings;
4507 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4509 struct cam_ed *cur_device, *device;
4510 struct cam_devq *devq;
4513 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4514 /* Make space for us in the device queue on our bus */
4515 devq = bus->sim->devq;
4516 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4517 if (status != CAM_REQ_CMP)
4520 device = (struct cam_ed *)malloc(sizeof(*device),
4521 M_CAMDEV, M_NOWAIT|M_ZERO);
4525 cam_init_pinfo(&device->devq_entry.pinfo);
4526 device->devq_entry.device = device;
4527 device->target = target;
4528 device->lun_id = lun_id;
4529 device->sim = bus->sim;
4530 /* Initialize our queues */
4531 if (camq_init(&device->drvq, 0) != 0) {
4532 free(device, M_CAMDEV);
4535 if (cam_ccbq_init(&device->ccbq,
4536 bus->sim->max_dev_openings) != 0) {
4537 camq_fini(&device->drvq);
4538 free(device, M_CAMDEV);
4541 SLIST_INIT(&device->asyncs);
4542 SLIST_INIT(&device->periphs);
4543 device->generation = 0;
4544 device->owner = NULL;
4545 device->flags = CAM_DEV_UNCONFIGURED;
4546 device->tag_delay_count = 0;
4547 device->tag_saved_openings = 0;
4548 device->refcount = 1;
4549 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4551 cur_device = TAILQ_FIRST(&target->ed_entries);
4552 while (cur_device != NULL && cur_device->lun_id < lun_id)
4553 cur_device = TAILQ_NEXT(cur_device, links);
4554 if (cur_device != NULL)
4555 TAILQ_INSERT_BEFORE(cur_device, device, links);
4557 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4559 target->generation++;
4564 xpt_acquire_device(struct cam_ed *device)
4567 mtx_assert(device->sim->mtx, MA_OWNED);
4572 xpt_release_device(struct cam_ed *device)
4574 struct cam_devq *devq;
4576 mtx_assert(device->sim->mtx, MA_OWNED);
4577 if (--device->refcount > 0)
4580 KASSERT(SLIST_EMPTY(&device->periphs),
4581 ("refcount is zero, but periphs list is not empty"));
4582 if (device->devq_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4583 panic("Removing device while still queued for ccbs");
4585 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4586 callout_stop(&device->callout);
4588 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4589 device->target->generation++;
4590 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4591 /* Release our slot in the devq */
4592 devq = device->target->bus->sim->devq;
4593 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4594 camq_fini(&device->drvq);
4595 cam_ccbq_fini(&device->ccbq);
4597 * Free allocated memory. free(9) does nothing if the
4598 * supplied pointer is NULL, so it is safe to call without
4601 free(device->supported_vpds, M_CAMXPT);
4602 free(device->device_id, M_CAMXPT);
4603 free(device->physpath, M_CAMXPT);
4604 free(device->rcap_buf, M_CAMXPT);
4605 free(device->serial_num, M_CAMXPT);
4607 xpt_release_target(device->target);
4608 free(device, M_CAMDEV);
4612 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4620 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4621 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4622 if (result == CAM_REQ_CMP && (diff < 0)) {
4623 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4625 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4626 || (dev->inq_flags & SID_CmdQue) != 0)
4627 dev->tag_saved_openings = newopenings;
4628 /* Adjust the global limit */
4629 dev->sim->max_ccbs += diff;
4633 static struct cam_eb *
4634 xpt_find_bus(path_id_t path_id)
4639 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4641 bus = TAILQ_NEXT(bus, links)) {
4642 if (bus->path_id == path_id) {
4651 static struct cam_et *
4652 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4654 struct cam_et *target;
4656 mtx_assert(bus->sim->mtx, MA_OWNED);
4657 for (target = TAILQ_FIRST(&bus->et_entries);
4659 target = TAILQ_NEXT(target, links)) {
4660 if (target->target_id == target_id) {
4668 static struct cam_ed *
4669 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4671 struct cam_ed *device;
4673 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4674 for (device = TAILQ_FIRST(&target->ed_entries);
4676 device = TAILQ_NEXT(device, links)) {
4677 if (device->lun_id == lun_id) {
4686 xpt_start_tags(struct cam_path *path)
4688 struct ccb_relsim crs;
4689 struct cam_ed *device;
4690 struct cam_sim *sim;
4693 device = path->device;
4694 sim = path->bus->sim;
4695 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4696 xpt_freeze_devq(path, /*count*/1);
4697 device->inq_flags |= SID_CmdQue;
4698 if (device->tag_saved_openings != 0)
4699 newopenings = device->tag_saved_openings;
4701 newopenings = min(device->maxtags,
4702 sim->max_tagged_dev_openings);
4703 xpt_dev_ccbq_resize(path, newopenings);
4704 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4705 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4706 crs.ccb_h.func_code = XPT_REL_SIMQ;
4707 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4709 = crs.release_timeout
4712 xpt_action((union ccb *)&crs);
4716 xpt_stop_tags(struct cam_path *path)
4718 struct ccb_relsim crs;
4719 struct cam_ed *device;
4720 struct cam_sim *sim;
4722 device = path->device;
4723 sim = path->bus->sim;
4724 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4725 device->tag_delay_count = 0;
4726 xpt_freeze_devq(path, /*count*/1);
4727 device->inq_flags &= ~SID_CmdQue;
4728 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4729 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4730 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4731 crs.ccb_h.func_code = XPT_REL_SIMQ;
4732 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4734 = crs.release_timeout
4737 xpt_action((union ccb *)&crs);
4741 xpt_boot_delay(void *arg)
4748 xpt_config(void *arg)
4751 * Now that interrupts are enabled, go find our devices
4754 /* Setup debugging path */
4755 if (cam_dflags != CAM_DEBUG_NONE) {
4756 if (xpt_create_path_unlocked(&cam_dpath, NULL,
4757 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4758 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4759 printf("xpt_config: xpt_create_path() failed for debug"
4760 " target %d:%d:%d, debugging disabled\n",
4761 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4762 cam_dflags = CAM_DEBUG_NONE;
4767 periphdriver_init(1);
4769 callout_init(&xsoftc.boot_callout, 1);
4770 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4771 xpt_boot_delay, NULL);
4772 /* Fire up rescan thread. */
4773 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4774 printf("xpt_config: failed to create rescan thread.\n");
4782 xsoftc.buses_to_config++;
4787 xpt_release_boot(void)
4790 xsoftc.buses_to_config--;
4791 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4792 struct xpt_task *task;
4794 xsoftc.buses_config_done = 1;
4796 /* Call manually because we don't have any busses */
4797 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4799 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4800 taskqueue_enqueue(taskqueue_thread, &task->task);
4807 * If the given device only has one peripheral attached to it, and if that
4808 * peripheral is the passthrough driver, announce it. This insures that the
4809 * user sees some sort of announcement for every peripheral in their system.
4812 xptpassannouncefunc(struct cam_ed *device, void *arg)
4814 struct cam_periph *periph;
4817 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4818 periph = SLIST_NEXT(periph, periph_links), i++);
4820 periph = SLIST_FIRST(&device->periphs);
4822 && (strncmp(periph->periph_name, "pass", 4) == 0))
4823 xpt_announce_periph(periph, NULL);
4829 xpt_finishconfig_task(void *context, int pending)
4832 periphdriver_init(2);
4834 * Check for devices with no "standard" peripheral driver
4835 * attached. For any devices like that, announce the
4836 * passthrough driver so the user will see something.
4839 xpt_for_all_devices(xptpassannouncefunc, NULL);
4841 /* Release our hook so that the boot can continue. */
4842 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4843 free(xsoftc.xpt_config_hook, M_CAMXPT);
4844 xsoftc.xpt_config_hook = NULL;
4846 free(context, M_CAMXPT);
4850 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4851 struct cam_path *path)
4853 struct ccb_setasync csa;
4858 mtx_lock(&xsoftc.xpt_lock);
4859 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4860 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4861 if (status != CAM_REQ_CMP) {
4862 mtx_unlock(&xsoftc.xpt_lock);
4868 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4869 csa.ccb_h.func_code = XPT_SASYNC_CB;
4870 csa.event_enable = event;
4871 csa.callback = cbfunc;
4872 csa.callback_arg = cbarg;
4873 xpt_action((union ccb *)&csa);
4874 status = csa.ccb_h.status;
4877 xpt_free_path(path);
4878 mtx_unlock(&xsoftc.xpt_lock);
4881 if ((status == CAM_REQ_CMP) &&
4882 (csa.event_enable & AC_FOUND_DEVICE)) {
4884 * Get this peripheral up to date with all
4885 * the currently existing devices.
4887 xpt_for_all_devices(xptsetasyncfunc, &csa);
4889 if ((status == CAM_REQ_CMP) &&
4890 (csa.event_enable & AC_PATH_REGISTERED)) {
4892 * Get this peripheral up to date with all
4893 * the currently existing busses.
4895 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4902 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4904 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4906 switch (work_ccb->ccb_h.func_code) {
4907 /* Common cases first */
4908 case XPT_PATH_INQ: /* Path routing inquiry */
4910 struct ccb_pathinq *cpi;
4912 cpi = &work_ccb->cpi;
4913 cpi->version_num = 1; /* XXX??? */
4914 cpi->hba_inquiry = 0;
4915 cpi->target_sprt = 0;
4917 cpi->hba_eng_cnt = 0;
4918 cpi->max_target = 0;
4920 cpi->initiator_id = 0;
4921 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4922 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4923 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4924 cpi->unit_number = sim->unit_number;
4925 cpi->bus_id = sim->bus_id;
4926 cpi->base_transfer_speed = 0;
4927 cpi->protocol = PROTO_UNSPECIFIED;
4928 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4929 cpi->transport = XPORT_UNSPECIFIED;
4930 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4931 cpi->ccb_h.status = CAM_REQ_CMP;
4936 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4943 * The xpt as a "controller" has no interrupt sources, so polling
4947 xptpoll(struct cam_sim *sim)
4952 xpt_lock_buses(void)
4954 mtx_lock(&xsoftc.xpt_topo_lock);
4958 xpt_unlock_buses(void)
4960 mtx_unlock(&xsoftc.xpt_topo_lock);
4967 struct cam_sim *sim;
4969 mtx_lock(&cam_simq_lock);
4971 while (!TAILQ_EMPTY(&cam_simq)) {
4972 TAILQ_CONCAT(&queue, &cam_simq, links);
4973 mtx_unlock(&cam_simq_lock);
4975 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4976 TAILQ_REMOVE(&queue, sim, links);
4978 camisr_runqueue(&sim->sim_doneq);
4979 sim->flags &= ~CAM_SIM_ON_DONEQ;
4980 CAM_SIM_UNLOCK(sim);
4982 mtx_lock(&cam_simq_lock);
4984 mtx_unlock(&cam_simq_lock);
4988 camisr_runqueue(void *V_queue)
4990 cam_isrq_t *queue = V_queue;
4991 struct ccb_hdr *ccb_h;
4993 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4996 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4997 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4999 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5004 if (ccb_h->flags & CAM_HIGH_POWER) {
5005 struct highpowerlist *hphead;
5006 union ccb *send_ccb;
5008 mtx_lock(&xsoftc.xpt_lock);
5009 hphead = &xsoftc.highpowerq;
5011 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5014 * Increment the count since this command is done.
5016 xsoftc.num_highpower++;
5019 * Any high powered commands queued up?
5021 if (send_ccb != NULL) {
5023 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5024 mtx_unlock(&xsoftc.xpt_lock);
5026 xpt_release_devq(send_ccb->ccb_h.path,
5027 /*count*/1, /*runqueue*/TRUE);
5029 mtx_unlock(&xsoftc.xpt_lock);
5032 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5035 dev = ccb_h->path->device;
5037 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5038 ccb_h->path->bus->sim->devq->send_active--;
5039 ccb_h->path->bus->sim->devq->send_openings++;
5042 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5043 && (dev->ccbq.dev_active == 0))) {
5044 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5045 xpt_release_devq(ccb_h->path, /*count*/1,
5046 /*run_queue*/FALSE);
5049 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5050 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5051 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5052 xpt_release_devq(ccb_h->path, /*count*/1,
5053 /*run_queue*/FALSE);
5056 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5057 && (--dev->tag_delay_count == 0))
5058 xpt_start_tags(ccb_h->path);
5059 if (!device_is_queued(dev)) {
5060 (void)xpt_schedule_devq(
5061 ccb_h->path->bus->sim->devq, dev);
5065 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5066 xpt_release_simq(ccb_h->path->bus->sim,
5068 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5072 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5073 && (ccb_h->status & CAM_DEV_QFRZN)) {
5074 xpt_release_devq(ccb_h->path, /*count*/1,
5076 ccb_h->status &= ~CAM_DEV_QFRZN;
5078 xpt_run_devq(ccb_h->path->bus->sim->devq);
5081 /* Call the peripheral driver's callback */
5082 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);