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>
43 #include <sys/interrupt.h>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_queue.h>
60 #include <cam/cam_sim.h>
61 #include <cam/cam_xpt.h>
62 #include <cam/cam_xpt_sim.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_xpt_internal.h>
65 #include <cam/cam_debug.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69 #include <cam/scsi/scsi_pass.h>
70 #include <machine/stdarg.h> /* for xpt_print below */
74 * This is the maximum number of high powered commands (e.g. start unit)
75 * that can be outstanding at a particular time.
77 #ifndef CAM_MAX_HIGHPOWER
78 #define CAM_MAX_HIGHPOWER 4
81 /* Datastructures internal to the xpt layer */
82 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 /* Object for defering XPT actions to a taskqueue */
97 u_int32_t xpt_generation;
99 /* number of high powered commands that can go through right now */
100 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 /* Registered busses */
107 TAILQ_HEAD(,cam_eb) xpt_busses;
108 u_int bus_generation;
110 struct intr_config_hook *xpt_config_hook;
112 struct mtx xpt_topo_lock;
118 DM_RET_FLAG_MASK = 0x0f,
121 DM_RET_DESCEND = 0x20,
123 DM_RET_ACTION_MASK = 0xf0
131 } xpt_traverse_depth;
133 struct xpt_traverse_config {
134 xpt_traverse_depth depth;
139 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
140 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
141 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
142 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
143 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
145 /* Transport layer configuration information */
146 static struct xpt_softc xsoftc;
148 /* Queues for our software interrupt handler */
149 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
150 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
151 static cam_simq_t cam_simq;
152 static struct mtx cam_simq_lock;
154 /* Pointers to software interrupt handlers */
155 static void *cambio_ih;
157 struct cam_periph *xpt_periph;
159 static periph_init_t xpt_periph_init;
161 static struct periph_driver xpt_driver =
163 xpt_periph_init, "xpt",
164 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
167 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
169 static d_open_t xptopen;
170 static d_close_t xptclose;
171 static d_ioctl_t xptioctl;
173 static struct cdevsw xpt_cdevsw = {
174 .d_version = D_VERSION,
182 /* Storage for debugging datastructures */
184 struct cam_path *cam_dpath;
185 u_int32_t cam_dflags;
186 u_int32_t cam_debug_delay;
189 /* Our boot-time initialization hook */
190 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
192 static moduledata_t cam_moduledata = {
194 cam_module_event_handler,
198 static int xpt_init(void *);
200 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
201 MODULE_VERSION(cam, 1);
204 static void xpt_async_bcast(struct async_list *async_head,
205 u_int32_t async_code,
206 struct cam_path *path,
208 static path_id_t xptnextfreepathid(void);
209 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
210 static union ccb *xpt_get_ccb(struct cam_ed *device);
211 static void xpt_run_dev_allocq(struct cam_eb *bus);
212 static timeout_t xpt_release_devq_timeout;
213 static void xpt_release_simq_timeout(void *arg) __unused;
214 static void xpt_release_bus(struct cam_eb *bus);
215 static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
217 static struct cam_et*
218 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
219 static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
220 static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
221 struct cam_ed *device);
222 static struct cam_eb*
223 xpt_find_bus(path_id_t path_id);
224 static struct cam_et*
225 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
226 static struct cam_ed*
227 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
228 static xpt_busfunc_t xptconfigbuscountfunc;
229 static xpt_busfunc_t xptconfigfunc;
230 static void xpt_config(void *arg);
231 static xpt_devicefunc_t xptpassannouncefunc;
232 static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
233 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
234 static void xptpoll(struct cam_sim *sim);
235 static void camisr(void *);
236 static void camisr_runqueue(void *);
237 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
238 u_int num_patterns, struct cam_eb *bus);
239 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
241 struct cam_ed *device);
242 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
244 struct cam_periph *periph);
245 static xpt_busfunc_t xptedtbusfunc;
246 static xpt_targetfunc_t xptedttargetfunc;
247 static xpt_devicefunc_t xptedtdevicefunc;
248 static xpt_periphfunc_t xptedtperiphfunc;
249 static xpt_pdrvfunc_t xptplistpdrvfunc;
250 static xpt_periphfunc_t xptplistperiphfunc;
251 static int xptedtmatch(struct ccb_dev_match *cdm);
252 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
253 static int xptbustraverse(struct cam_eb *start_bus,
254 xpt_busfunc_t *tr_func, void *arg);
255 static int xpttargettraverse(struct cam_eb *bus,
256 struct cam_et *start_target,
257 xpt_targetfunc_t *tr_func, void *arg);
258 static int xptdevicetraverse(struct cam_et *target,
259 struct cam_ed *start_device,
260 xpt_devicefunc_t *tr_func, void *arg);
261 static int xptperiphtraverse(struct cam_ed *device,
262 struct cam_periph *start_periph,
263 xpt_periphfunc_t *tr_func, void *arg);
264 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
265 xpt_pdrvfunc_t *tr_func, void *arg);
266 static int xptpdperiphtraverse(struct periph_driver **pdrv,
267 struct cam_periph *start_periph,
268 xpt_periphfunc_t *tr_func,
270 static xpt_busfunc_t xptdefbusfunc;
271 static xpt_targetfunc_t xptdeftargetfunc;
272 static xpt_devicefunc_t xptdefdevicefunc;
273 static xpt_periphfunc_t xptdefperiphfunc;
274 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
275 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
277 static void xpt_dev_async_default(u_int32_t async_code,
279 struct cam_et *target,
280 struct cam_ed *device,
282 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
283 struct cam_et *target,
285 static xpt_devicefunc_t xptsetasyncfunc;
286 static xpt_busfunc_t xptsetasyncbusfunc;
287 static cam_status xptregister(struct cam_periph *periph,
289 static void xpt_start_tags(struct cam_path *path);
290 static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
292 static __inline int periph_is_queued(struct cam_periph *periph);
293 static __inline int device_is_alloc_queued(struct cam_ed *device);
294 static __inline int device_is_send_queued(struct cam_ed *device);
295 static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
298 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
302 if (dev->ccbq.devq_openings > 0) {
303 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
304 cam_ccbq_resize(&dev->ccbq,
305 dev->ccbq.dev_openings
306 + dev->ccbq.dev_active);
307 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
310 * The priority of a device waiting for CCB resources
311 * is that of the the highest priority peripheral driver
314 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
315 &dev->alloc_ccb_entry.pinfo,
316 CAMQ_GET_HEAD(&dev->drvq)->priority);
325 periph_is_queued(struct cam_periph *periph)
327 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
331 device_is_alloc_queued(struct cam_ed *device)
333 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
337 device_is_send_queued(struct cam_ed *device)
339 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
343 dev_allocq_is_runnable(struct cam_devq *devq)
347 * Have space to do more work.
348 * Allowed to do work.
350 return ((devq->alloc_queue.qfrozen_cnt == 0)
351 && (devq->alloc_queue.entries > 0)
352 && (devq->alloc_openings > 0));
358 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
362 xptdone(struct cam_periph *periph, union ccb *done_ccb)
364 /* Caller will release the CCB */
365 wakeup(&done_ccb->ccb_h.cbfcnp);
369 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
373 * Only allow read-write access.
375 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
379 * We don't allow nonblocking access.
381 if ((flags & O_NONBLOCK) != 0) {
382 printf("%s: can't do nonblocking access\n", devtoname(dev));
386 /* Mark ourselves open */
387 mtx_lock(&xsoftc.xpt_lock);
388 xsoftc.flags |= XPT_FLAG_OPEN;
389 mtx_unlock(&xsoftc.xpt_lock);
395 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
398 /* Mark ourselves closed */
399 mtx_lock(&xsoftc.xpt_lock);
400 xsoftc.flags &= ~XPT_FLAG_OPEN;
401 mtx_unlock(&xsoftc.xpt_lock);
407 * Don't automatically grab the xpt softc lock here even though this is going
408 * through the xpt device. The xpt device is really just a back door for
409 * accessing other devices and SIMs, so the right thing to do is to grab
410 * the appropriate SIM lock once the bus/SIM is located.
413 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
421 * For the transport layer CAMIOCOMMAND ioctl, we really only want
422 * to accept CCB types that don't quite make sense to send through a
423 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
431 inccb = (union ccb *)addr;
433 bus = xpt_find_bus(inccb->ccb_h.path_id);
439 switch(inccb->ccb_h.func_code) {
442 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
443 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
452 ccb = xpt_alloc_ccb();
454 CAM_SIM_LOCK(bus->sim);
457 * Create a path using the bus, target, and lun the
460 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
461 inccb->ccb_h.path_id,
462 inccb->ccb_h.target_id,
463 inccb->ccb_h.target_lun) !=
466 CAM_SIM_UNLOCK(bus->sim);
470 /* Ensure all of our fields are correct */
471 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
472 inccb->ccb_h.pinfo.priority);
473 xpt_merge_ccb(ccb, inccb);
474 ccb->ccb_h.cbfcnp = xptdone;
475 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
476 bcopy(ccb, inccb, sizeof(union ccb));
477 xpt_free_path(ccb->ccb_h.path);
479 CAM_SIM_UNLOCK(bus->sim);
486 * This is an immediate CCB, so it's okay to
487 * allocate it on the stack.
490 CAM_SIM_LOCK(bus->sim);
493 * Create a path using the bus, target, and lun the
496 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
497 inccb->ccb_h.path_id,
498 inccb->ccb_h.target_id,
499 inccb->ccb_h.target_lun) !=
502 CAM_SIM_UNLOCK(bus->sim);
505 /* Ensure all of our fields are correct */
506 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
507 inccb->ccb_h.pinfo.priority);
508 xpt_merge_ccb(&ccb, inccb);
509 ccb.ccb_h.cbfcnp = xptdone;
511 CAM_SIM_UNLOCK(bus->sim);
512 bcopy(&ccb, inccb, sizeof(union ccb));
513 xpt_free_path(ccb.ccb_h.path);
517 case XPT_DEV_MATCH: {
518 struct cam_periph_map_info mapinfo;
519 struct cam_path *old_path;
522 * We can't deal with physical addresses for this
523 * type of transaction.
525 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
531 * Save this in case the caller had it set to
532 * something in particular.
534 old_path = inccb->ccb_h.path;
537 * We really don't need a path for the matching
538 * code. The path is needed because of the
539 * debugging statements in xpt_action(). They
540 * assume that the CCB has a valid path.
542 inccb->ccb_h.path = xpt_periph->path;
544 bzero(&mapinfo, sizeof(mapinfo));
547 * Map the pattern and match buffers into kernel
548 * virtual address space.
550 error = cam_periph_mapmem(inccb, &mapinfo);
553 inccb->ccb_h.path = old_path;
558 * This is an immediate CCB, we can send it on directly.
563 * Map the buffers back into user space.
565 cam_periph_unmapmem(inccb, &mapinfo);
567 inccb->ccb_h.path = old_path;
576 xpt_release_bus(bus);
580 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
581 * with the periphal driver name and unit name filled in. The other
582 * fields don't really matter as input. The passthrough driver name
583 * ("pass"), and unit number are passed back in the ccb. The current
584 * device generation number, and the index into the device peripheral
585 * driver list, and the status are also passed back. Note that
586 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
587 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
588 * (or rather should be) impossible for the device peripheral driver
589 * list to change since we look at the whole thing in one pass, and
590 * we do it with lock protection.
593 case CAMGETPASSTHRU: {
595 struct cam_periph *periph;
596 struct periph_driver **p_drv;
599 u_int cur_generation;
600 int base_periph_found;
603 ccb = (union ccb *)addr;
604 unit = ccb->cgdl.unit_number;
605 name = ccb->cgdl.periph_name;
607 * Every 100 devices, we want to drop our lock protection to
608 * give the software interrupt handler a chance to run.
609 * Most systems won't run into this check, but this should
610 * avoid starvation in the software interrupt handler in
615 ccb = (union ccb *)addr;
617 base_periph_found = 0;
620 * Sanity check -- make sure we don't get a null peripheral
623 if (*ccb->cgdl.periph_name == '\0') {
628 /* Keep the list from changing while we traverse it */
629 mtx_lock(&xsoftc.xpt_topo_lock);
631 cur_generation = xsoftc.xpt_generation;
633 /* first find our driver in the list of drivers */
634 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
635 if (strcmp((*p_drv)->driver_name, name) == 0)
638 if (*p_drv == NULL) {
639 mtx_unlock(&xsoftc.xpt_topo_lock);
640 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
641 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
642 *ccb->cgdl.periph_name = '\0';
643 ccb->cgdl.unit_number = 0;
649 * Run through every peripheral instance of this driver
650 * and check to see whether it matches the unit passed
651 * in by the user. If it does, get out of the loops and
652 * find the passthrough driver associated with that
655 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
656 periph = TAILQ_NEXT(periph, unit_links)) {
658 if (periph->unit_number == unit) {
660 } else if (--splbreaknum == 0) {
661 mtx_unlock(&xsoftc.xpt_topo_lock);
662 mtx_lock(&xsoftc.xpt_topo_lock);
664 if (cur_generation != xsoftc.xpt_generation)
669 * If we found the peripheral driver that the user passed
670 * in, go through all of the peripheral drivers for that
671 * particular device and look for a passthrough driver.
673 if (periph != NULL) {
674 struct cam_ed *device;
677 base_periph_found = 1;
678 device = periph->path->device;
679 for (i = 0, periph = SLIST_FIRST(&device->periphs);
681 periph = SLIST_NEXT(periph, periph_links), i++) {
683 * Check to see whether we have a
684 * passthrough device or not.
686 if (strcmp(periph->periph_name, "pass") == 0) {
688 * Fill in the getdevlist fields.
690 strcpy(ccb->cgdl.periph_name,
691 periph->periph_name);
692 ccb->cgdl.unit_number =
694 if (SLIST_NEXT(periph, periph_links))
696 CAM_GDEVLIST_MORE_DEVS;
699 CAM_GDEVLIST_LAST_DEVICE;
700 ccb->cgdl.generation =
704 * Fill in some CCB header fields
705 * that the user may want.
708 periph->path->bus->path_id;
709 ccb->ccb_h.target_id =
710 periph->path->target->target_id;
711 ccb->ccb_h.target_lun =
712 periph->path->device->lun_id;
713 ccb->ccb_h.status = CAM_REQ_CMP;
720 * If the periph is null here, one of two things has
721 * happened. The first possibility is that we couldn't
722 * find the unit number of the particular peripheral driver
723 * that the user is asking about. e.g. the user asks for
724 * the passthrough driver for "da11". We find the list of
725 * "da" peripherals all right, but there is no unit 11.
726 * The other possibility is that we went through the list
727 * of peripheral drivers attached to the device structure,
728 * but didn't find one with the name "pass". Either way,
729 * we return ENOENT, since we couldn't find something.
731 if (periph == NULL) {
732 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
733 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
734 *ccb->cgdl.periph_name = '\0';
735 ccb->cgdl.unit_number = 0;
738 * It is unfortunate that this is even necessary,
739 * but there are many, many clueless users out there.
740 * If this is true, the user is looking for the
741 * passthrough driver, but doesn't have one in his
744 if (base_periph_found == 1) {
745 printf("xptioctl: pass driver is not in the "
747 printf("xptioctl: put \"device pass\" in "
748 "your kernel config file\n");
751 mtx_unlock(&xsoftc.xpt_topo_lock);
763 cam_module_event_handler(module_t mod, int what, void *arg)
769 if ((error = xpt_init(NULL)) != 0)
781 /* thread to handle bus rescans */
783 xpt_scanner_thread(void *dummy)
791 * Wait for a rescan request to come in. When it does, splice
792 * it onto a queue from local storage so that the xpt lock
793 * doesn't need to be held while the requests are being
797 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
798 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
801 TAILQ_CONCAT(&queue, &xsoftc.ccb_scanq, sim_links.tqe);
804 while ((ccb = (union ccb *)TAILQ_FIRST(&queue)) != NULL) {
805 TAILQ_REMOVE(&queue, &ccb->ccb_h, sim_links.tqe);
807 sim = ccb->ccb_h.path->bus->sim;
810 if( ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD )
811 ccb->ccb_h.func_code = XPT_SCAN_BUS;
813 ccb->ccb_h.func_code = XPT_SCAN_LUN;
814 ccb->ccb_h.cbfcnp = xptdone;
815 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 1);
816 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
817 xpt_free_path(ccb->ccb_h.path);
825 xpt_rescan(union ccb *ccb)
830 * Don't make duplicate entries for the same paths.
833 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
834 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
835 wakeup(&xsoftc.ccb_scanq);
837 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
838 xpt_free_path(ccb->ccb_h.path);
843 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
844 wakeup(&xsoftc.ccb_scanq);
848 /* Functions accessed by the peripheral drivers */
850 xpt_init(void *dummy)
852 struct cam_sim *xpt_sim;
853 struct cam_path *path;
854 struct cam_devq *devq;
857 TAILQ_INIT(&xsoftc.xpt_busses);
858 TAILQ_INIT(&cam_simq);
859 TAILQ_INIT(&xsoftc.ccb_scanq);
860 STAILQ_INIT(&xsoftc.highpowerq);
861 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
863 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
864 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
865 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
868 * The xpt layer is, itself, the equivelent of a SIM.
869 * Allow 16 ccbs in the ccb pool for it. This should
870 * give decent parallelism when we probe busses and
871 * perform other XPT functions.
873 devq = cam_simq_alloc(16);
874 xpt_sim = cam_sim_alloc(xptaction,
879 /*mtx*/&xsoftc.xpt_lock,
880 /*max_dev_transactions*/0,
881 /*max_tagged_dev_transactions*/0,
886 xpt_sim->max_ccbs = 16;
888 mtx_lock(&xsoftc.xpt_lock);
889 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
890 printf("xpt_init: xpt_bus_register failed with status %#x,"
891 " failing attach\n", status);
896 * Looking at the XPT from the SIM layer, the XPT is
897 * the equivelent of a peripheral driver. Allocate
898 * a peripheral driver entry for us.
900 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
902 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
903 printf("xpt_init: xpt_create_path failed with status %#x,"
904 " failing attach\n", status);
908 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
909 path, NULL, 0, xpt_sim);
911 mtx_unlock(&xsoftc.xpt_lock);
914 * Register a callback for when interrupts are enabled.
916 xsoftc.xpt_config_hook =
917 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
918 M_CAMXPT, M_NOWAIT | M_ZERO);
919 if (xsoftc.xpt_config_hook == NULL) {
920 printf("xpt_init: Cannot malloc config hook "
921 "- failing attach\n");
925 xsoftc.xpt_config_hook->ich_func = xpt_config;
926 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
927 free (xsoftc.xpt_config_hook, M_CAMXPT);
928 printf("xpt_init: config_intrhook_establish failed "
929 "- failing attach\n");
932 /* fire up rescan thread */
933 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
934 printf("xpt_init: failed to create rescan thread\n");
936 /* Install our software interrupt handlers */
937 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
943 xptregister(struct cam_periph *periph, void *arg)
945 struct cam_sim *xpt_sim;
947 if (periph == NULL) {
948 printf("xptregister: periph was NULL!!\n");
949 return(CAM_REQ_CMP_ERR);
952 xpt_sim = (struct cam_sim *)arg;
953 xpt_sim->softc = periph;
955 periph->softc = NULL;
961 xpt_add_periph(struct cam_periph *periph)
963 struct cam_ed *device;
965 struct periph_list *periph_head;
967 mtx_assert(periph->sim->mtx, MA_OWNED);
969 device = periph->path->device;
971 periph_head = &device->periphs;
973 status = CAM_REQ_CMP;
975 if (device != NULL) {
977 * Make room for this peripheral
978 * so it will fit in the queue
979 * when it's scheduled to run
981 status = camq_resize(&device->drvq,
982 device->drvq.array_size + 1);
984 device->generation++;
986 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
989 mtx_lock(&xsoftc.xpt_topo_lock);
990 xsoftc.xpt_generation++;
991 mtx_unlock(&xsoftc.xpt_topo_lock);
997 xpt_remove_periph(struct cam_periph *periph)
999 struct cam_ed *device;
1001 mtx_assert(periph->sim->mtx, MA_OWNED);
1003 device = periph->path->device;
1005 if (device != NULL) {
1006 struct periph_list *periph_head;
1008 periph_head = &device->periphs;
1010 /* Release the slot for this peripheral */
1011 camq_resize(&device->drvq, device->drvq.array_size - 1);
1013 device->generation++;
1015 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1018 mtx_lock(&xsoftc.xpt_topo_lock);
1019 xsoftc.xpt_generation++;
1020 mtx_unlock(&xsoftc.xpt_topo_lock);
1025 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1027 struct ccb_pathinq cpi;
1028 struct ccb_trans_settings cts;
1029 struct cam_path *path;
1034 mtx_assert(periph->sim->mtx, MA_OWNED);
1036 path = periph->path;
1038 * To ensure that this is printed in one piece,
1039 * mask out CAM interrupts.
1041 printf("%s%d at %s%d bus %d target %d lun %d\n",
1042 periph->periph_name, periph->unit_number,
1043 path->bus->sim->sim_name,
1044 path->bus->sim->unit_number,
1045 path->bus->sim->bus_id,
1046 path->target->target_id,
1047 path->device->lun_id);
1048 printf("%s%d: ", periph->periph_name, periph->unit_number);
1049 if (path->device->protocol == PROTO_SCSI)
1050 scsi_print_inquiry(&path->device->inq_data);
1051 else if (path->device->protocol == PROTO_ATA ||
1052 path->device->protocol == PROTO_SATAPM)
1053 ata_print_ident(&path->device->ident_data);
1055 printf("Unknown protocol device\n");
1056 if (bootverbose && path->device->serial_num_len > 0) {
1057 /* Don't wrap the screen - print only the first 60 chars */
1058 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1059 periph->unit_number, path->device->serial_num);
1061 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1062 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1063 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1064 xpt_action((union ccb*)&cts);
1065 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1069 /* Ask the SIM for its base transfer speed */
1070 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1071 cpi.ccb_h.func_code = XPT_PATH_INQ;
1072 xpt_action((union ccb *)&cpi);
1074 speed = cpi.base_transfer_speed;
1076 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1077 struct ccb_trans_settings_spi *spi;
1079 spi = &cts.xport_specific.spi;
1080 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1081 && spi->sync_offset != 0) {
1082 freq = scsi_calc_syncsrate(spi->sync_period);
1086 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1087 speed *= (0x01 << spi->bus_width);
1089 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1090 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1091 if (fc->valid & CTS_FC_VALID_SPEED)
1092 speed = fc->bitrate;
1094 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1095 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1096 if (sas->valid & CTS_SAS_VALID_SPEED)
1097 speed = sas->bitrate;
1099 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1100 struct ccb_trans_settings_sata *sata = &cts.xport_specific.sata;
1101 if (sata->valid & CTS_SATA_VALID_SPEED)
1102 speed = sata->bitrate;
1107 printf("%s%d: %d.%03dMB/s transfers",
1108 periph->periph_name, periph->unit_number,
1111 printf("%s%d: %dKB/s transfers", periph->periph_name,
1112 periph->unit_number, speed);
1113 /* Report additional information about SPI connections */
1114 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1115 struct ccb_trans_settings_spi *spi;
1117 spi = &cts.xport_specific.spi;
1119 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1121 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1125 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1126 && spi->bus_width > 0) {
1132 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1133 } else if (freq != 0) {
1137 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1138 struct ccb_trans_settings_fc *fc;
1140 fc = &cts.xport_specific.fc;
1141 if (fc->valid & CTS_FC_VALID_WWNN)
1142 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1143 if (fc->valid & CTS_FC_VALID_WWPN)
1144 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1145 if (fc->valid & CTS_FC_VALID_PORT)
1146 printf(" PortID 0x%x", fc->port);
1149 if (path->device->inq_flags & SID_CmdQue
1150 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1151 printf("\n%s%d: Command Queueing enabled",
1152 periph->periph_name, periph->unit_number);
1157 * We only want to print the caller's announce string if they've
1160 if (announce_string != NULL)
1161 printf("%s%d: %s\n", periph->periph_name,
1162 periph->unit_number, announce_string);
1165 static dev_match_ret
1166 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1169 dev_match_ret retval;
1172 retval = DM_RET_NONE;
1175 * If we aren't given something to match against, that's an error.
1178 return(DM_RET_ERROR);
1181 * If there are no match entries, then this bus matches no
1184 if ((patterns == NULL) || (num_patterns == 0))
1185 return(DM_RET_DESCEND | DM_RET_COPY);
1187 for (i = 0; i < num_patterns; i++) {
1188 struct bus_match_pattern *cur_pattern;
1191 * If the pattern in question isn't for a bus node, we
1192 * aren't interested. However, we do indicate to the
1193 * calling routine that we should continue descending the
1194 * tree, since the user wants to match against lower-level
1197 if (patterns[i].type != DEV_MATCH_BUS) {
1198 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1199 retval |= DM_RET_DESCEND;
1203 cur_pattern = &patterns[i].pattern.bus_pattern;
1206 * If they want to match any bus node, we give them any
1209 if (cur_pattern->flags == BUS_MATCH_ANY) {
1210 /* set the copy flag */
1211 retval |= DM_RET_COPY;
1214 * If we've already decided on an action, go ahead
1217 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1222 * Not sure why someone would do this...
1224 if (cur_pattern->flags == BUS_MATCH_NONE)
1227 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1228 && (cur_pattern->path_id != bus->path_id))
1231 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1232 && (cur_pattern->bus_id != bus->sim->bus_id))
1235 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1236 && (cur_pattern->unit_number != bus->sim->unit_number))
1239 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1240 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1245 * If we get to this point, the user definitely wants
1246 * information on this bus. So tell the caller to copy the
1249 retval |= DM_RET_COPY;
1252 * If the return action has been set to descend, then we
1253 * know that we've already seen a non-bus matching
1254 * expression, therefore we need to further descend the tree.
1255 * This won't change by continuing around the loop, so we
1256 * go ahead and return. If we haven't seen a non-bus
1257 * matching expression, we keep going around the loop until
1258 * we exhaust the matching expressions. We'll set the stop
1259 * flag once we fall out of the loop.
1261 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1266 * If the return action hasn't been set to descend yet, that means
1267 * we haven't seen anything other than bus matching patterns. So
1268 * tell the caller to stop descending the tree -- the user doesn't
1269 * want to match against lower level tree elements.
1271 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1272 retval |= DM_RET_STOP;
1277 static dev_match_ret
1278 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1279 struct cam_ed *device)
1281 dev_match_ret retval;
1284 retval = DM_RET_NONE;
1287 * If we aren't given something to match against, that's an error.
1290 return(DM_RET_ERROR);
1293 * If there are no match entries, then this device matches no
1296 if ((patterns == NULL) || (num_patterns == 0))
1297 return(DM_RET_DESCEND | DM_RET_COPY);
1299 for (i = 0; i < num_patterns; i++) {
1300 struct device_match_pattern *cur_pattern;
1303 * If the pattern in question isn't for a device node, we
1304 * aren't interested.
1306 if (patterns[i].type != DEV_MATCH_DEVICE) {
1307 if ((patterns[i].type == DEV_MATCH_PERIPH)
1308 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1309 retval |= DM_RET_DESCEND;
1313 cur_pattern = &patterns[i].pattern.device_pattern;
1316 * If they want to match any device node, we give them any
1319 if (cur_pattern->flags == DEV_MATCH_ANY) {
1320 /* set the copy flag */
1321 retval |= DM_RET_COPY;
1325 * If we've already decided on an action, go ahead
1328 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1333 * Not sure why someone would do this...
1335 if (cur_pattern->flags == DEV_MATCH_NONE)
1338 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1339 && (cur_pattern->path_id != device->target->bus->path_id))
1342 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1343 && (cur_pattern->target_id != device->target->target_id))
1346 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1347 && (cur_pattern->target_lun != device->lun_id))
1350 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1351 && (cam_quirkmatch((caddr_t)&device->inq_data,
1352 (caddr_t)&cur_pattern->inq_pat,
1353 1, sizeof(cur_pattern->inq_pat),
1354 scsi_static_inquiry_match) == NULL))
1358 * If we get to this point, the user definitely wants
1359 * information on this device. So tell the caller to copy
1362 retval |= DM_RET_COPY;
1365 * If the return action has been set to descend, then we
1366 * know that we've already seen a peripheral matching
1367 * expression, therefore we need to further descend the tree.
1368 * This won't change by continuing around the loop, so we
1369 * go ahead and return. If we haven't seen a peripheral
1370 * matching expression, we keep going around the loop until
1371 * we exhaust the matching expressions. We'll set the stop
1372 * flag once we fall out of the loop.
1374 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1379 * If the return action hasn't been set to descend yet, that means
1380 * we haven't seen any peripheral matching patterns. So tell the
1381 * caller to stop descending the tree -- the user doesn't want to
1382 * match against lower level tree elements.
1384 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1385 retval |= DM_RET_STOP;
1391 * Match a single peripheral against any number of match patterns.
1393 static dev_match_ret
1394 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1395 struct cam_periph *periph)
1397 dev_match_ret retval;
1401 * If we aren't given something to match against, that's an error.
1404 return(DM_RET_ERROR);
1407 * If there are no match entries, then this peripheral matches no
1410 if ((patterns == NULL) || (num_patterns == 0))
1411 return(DM_RET_STOP | DM_RET_COPY);
1414 * There aren't any nodes below a peripheral node, so there's no
1415 * reason to descend the tree any further.
1417 retval = DM_RET_STOP;
1419 for (i = 0; i < num_patterns; i++) {
1420 struct periph_match_pattern *cur_pattern;
1423 * If the pattern in question isn't for a peripheral, we
1424 * aren't interested.
1426 if (patterns[i].type != DEV_MATCH_PERIPH)
1429 cur_pattern = &patterns[i].pattern.periph_pattern;
1432 * If they want to match on anything, then we will do so.
1434 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1435 /* set the copy flag */
1436 retval |= DM_RET_COPY;
1439 * We've already set the return action to stop,
1440 * since there are no nodes below peripherals in
1447 * Not sure why someone would do this...
1449 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1452 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1453 && (cur_pattern->path_id != periph->path->bus->path_id))
1457 * For the target and lun id's, we have to make sure the
1458 * target and lun pointers aren't NULL. The xpt peripheral
1459 * has a wildcard target and device.
1461 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1462 && ((periph->path->target == NULL)
1463 ||(cur_pattern->target_id != periph->path->target->target_id)))
1466 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1467 && ((periph->path->device == NULL)
1468 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1471 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1472 && (cur_pattern->unit_number != periph->unit_number))
1475 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1476 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1481 * If we get to this point, the user definitely wants
1482 * information on this peripheral. So tell the caller to
1483 * copy the data out.
1485 retval |= DM_RET_COPY;
1488 * The return action has already been set to stop, since
1489 * peripherals don't have any nodes below them in the EDT.
1495 * If we get to this point, the peripheral that was passed in
1496 * doesn't match any of the patterns.
1502 xptedtbusfunc(struct cam_eb *bus, void *arg)
1504 struct ccb_dev_match *cdm;
1505 dev_match_ret retval;
1507 cdm = (struct ccb_dev_match *)arg;
1510 * If our position is for something deeper in the tree, that means
1511 * that we've already seen this node. So, we keep going down.
1513 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1514 && (cdm->pos.cookie.bus == bus)
1515 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1516 && (cdm->pos.cookie.target != NULL))
1517 retval = DM_RET_DESCEND;
1519 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1522 * If we got an error, bail out of the search.
1524 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1525 cdm->status = CAM_DEV_MATCH_ERROR;
1530 * If the copy flag is set, copy this bus out.
1532 if (retval & DM_RET_COPY) {
1535 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1536 sizeof(struct dev_match_result));
1539 * If we don't have enough space to put in another
1540 * match result, save our position and tell the
1541 * user there are more devices to check.
1543 if (spaceleft < sizeof(struct dev_match_result)) {
1544 bzero(&cdm->pos, sizeof(cdm->pos));
1545 cdm->pos.position_type =
1546 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1548 cdm->pos.cookie.bus = bus;
1549 cdm->pos.generations[CAM_BUS_GENERATION]=
1550 xsoftc.bus_generation;
1551 cdm->status = CAM_DEV_MATCH_MORE;
1554 j = cdm->num_matches;
1556 cdm->matches[j].type = DEV_MATCH_BUS;
1557 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1558 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1559 cdm->matches[j].result.bus_result.unit_number =
1560 bus->sim->unit_number;
1561 strncpy(cdm->matches[j].result.bus_result.dev_name,
1562 bus->sim->sim_name, DEV_IDLEN);
1566 * If the user is only interested in busses, there's no
1567 * reason to descend to the next level in the tree.
1569 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1573 * If there is a target generation recorded, check it to
1574 * make sure the target list hasn't changed.
1576 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1577 && (bus == cdm->pos.cookie.bus)
1578 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1579 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1580 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1582 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1586 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1587 && (cdm->pos.cookie.bus == bus)
1588 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1589 && (cdm->pos.cookie.target != NULL))
1590 return(xpttargettraverse(bus,
1591 (struct cam_et *)cdm->pos.cookie.target,
1592 xptedttargetfunc, arg));
1594 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1598 xptedttargetfunc(struct cam_et *target, void *arg)
1600 struct ccb_dev_match *cdm;
1602 cdm = (struct ccb_dev_match *)arg;
1605 * If there is a device list generation recorded, check it to
1606 * make sure the device list hasn't changed.
1608 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1609 && (cdm->pos.cookie.bus == target->bus)
1610 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1611 && (cdm->pos.cookie.target == target)
1612 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1613 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1614 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1615 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 == target->bus)
1622 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1623 && (cdm->pos.cookie.target == target)
1624 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1625 && (cdm->pos.cookie.device != NULL))
1626 return(xptdevicetraverse(target,
1627 (struct cam_ed *)cdm->pos.cookie.device,
1628 xptedtdevicefunc, arg));
1630 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1634 xptedtdevicefunc(struct cam_ed *device, void *arg)
1637 struct ccb_dev_match *cdm;
1638 dev_match_ret retval;
1640 cdm = (struct ccb_dev_match *)arg;
1643 * If our position is for something deeper in the tree, that means
1644 * that we've already seen this node. So, we keep going down.
1646 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1647 && (cdm->pos.cookie.device == device)
1648 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1649 && (cdm->pos.cookie.periph != NULL))
1650 retval = DM_RET_DESCEND;
1652 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1655 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1656 cdm->status = CAM_DEV_MATCH_ERROR;
1661 * If the copy flag is set, copy this device out.
1663 if (retval & DM_RET_COPY) {
1666 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1667 sizeof(struct dev_match_result));
1670 * If we don't have enough space to put in another
1671 * match result, save our position and tell the
1672 * user there are more devices to check.
1674 if (spaceleft < sizeof(struct dev_match_result)) {
1675 bzero(&cdm->pos, sizeof(cdm->pos));
1676 cdm->pos.position_type =
1677 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1678 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1680 cdm->pos.cookie.bus = device->target->bus;
1681 cdm->pos.generations[CAM_BUS_GENERATION]=
1682 xsoftc.bus_generation;
1683 cdm->pos.cookie.target = device->target;
1684 cdm->pos.generations[CAM_TARGET_GENERATION] =
1685 device->target->bus->generation;
1686 cdm->pos.cookie.device = device;
1687 cdm->pos.generations[CAM_DEV_GENERATION] =
1688 device->target->generation;
1689 cdm->status = CAM_DEV_MATCH_MORE;
1692 j = cdm->num_matches;
1694 cdm->matches[j].type = DEV_MATCH_DEVICE;
1695 cdm->matches[j].result.device_result.path_id =
1696 device->target->bus->path_id;
1697 cdm->matches[j].result.device_result.target_id =
1698 device->target->target_id;
1699 cdm->matches[j].result.device_result.target_lun =
1701 cdm->matches[j].result.device_result.protocol =
1703 bcopy(&device->inq_data,
1704 &cdm->matches[j].result.device_result.inq_data,
1705 sizeof(struct scsi_inquiry_data));
1706 bcopy(&device->ident_data,
1707 &cdm->matches[j].result.device_result.ident_data,
1708 sizeof(struct ata_params));
1710 /* Let the user know whether this device is unconfigured */
1711 if (device->flags & CAM_DEV_UNCONFIGURED)
1712 cdm->matches[j].result.device_result.flags =
1713 DEV_RESULT_UNCONFIGURED;
1715 cdm->matches[j].result.device_result.flags =
1720 * If the user isn't interested in peripherals, don't descend
1721 * the tree any further.
1723 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1727 * If there is a peripheral list generation recorded, make sure
1728 * it hasn't changed.
1730 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1731 && (device->target->bus == cdm->pos.cookie.bus)
1732 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1733 && (device->target == cdm->pos.cookie.target)
1734 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1735 && (device == cdm->pos.cookie.device)
1736 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1737 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1738 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1739 device->generation)){
1740 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1744 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1745 && (cdm->pos.cookie.bus == device->target->bus)
1746 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1747 && (cdm->pos.cookie.target == device->target)
1748 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1749 && (cdm->pos.cookie.device == device)
1750 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1751 && (cdm->pos.cookie.periph != NULL))
1752 return(xptperiphtraverse(device,
1753 (struct cam_periph *)cdm->pos.cookie.periph,
1754 xptedtperiphfunc, arg));
1756 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1760 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1762 struct ccb_dev_match *cdm;
1763 dev_match_ret retval;
1765 cdm = (struct ccb_dev_match *)arg;
1767 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1769 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1770 cdm->status = CAM_DEV_MATCH_ERROR;
1775 * If the copy flag is set, copy this peripheral out.
1777 if (retval & DM_RET_COPY) {
1780 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1781 sizeof(struct dev_match_result));
1784 * If we don't have enough space to put in another
1785 * match result, save our position and tell the
1786 * user there are more devices to check.
1788 if (spaceleft < sizeof(struct dev_match_result)) {
1789 bzero(&cdm->pos, sizeof(cdm->pos));
1790 cdm->pos.position_type =
1791 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1792 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1795 cdm->pos.cookie.bus = periph->path->bus;
1796 cdm->pos.generations[CAM_BUS_GENERATION]=
1797 xsoftc.bus_generation;
1798 cdm->pos.cookie.target = periph->path->target;
1799 cdm->pos.generations[CAM_TARGET_GENERATION] =
1800 periph->path->bus->generation;
1801 cdm->pos.cookie.device = periph->path->device;
1802 cdm->pos.generations[CAM_DEV_GENERATION] =
1803 periph->path->target->generation;
1804 cdm->pos.cookie.periph = periph;
1805 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1806 periph->path->device->generation;
1807 cdm->status = CAM_DEV_MATCH_MORE;
1811 j = cdm->num_matches;
1813 cdm->matches[j].type = DEV_MATCH_PERIPH;
1814 cdm->matches[j].result.periph_result.path_id =
1815 periph->path->bus->path_id;
1816 cdm->matches[j].result.periph_result.target_id =
1817 periph->path->target->target_id;
1818 cdm->matches[j].result.periph_result.target_lun =
1819 periph->path->device->lun_id;
1820 cdm->matches[j].result.periph_result.unit_number =
1821 periph->unit_number;
1822 strncpy(cdm->matches[j].result.periph_result.periph_name,
1823 periph->periph_name, DEV_IDLEN);
1830 xptedtmatch(struct ccb_dev_match *cdm)
1834 cdm->num_matches = 0;
1837 * Check the bus list generation. If it has changed, the user
1838 * needs to reset everything and start over.
1840 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1841 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1842 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1843 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1847 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1848 && (cdm->pos.cookie.bus != NULL))
1849 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1850 xptedtbusfunc, cdm);
1852 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1855 * If we get back 0, that means that we had to stop before fully
1856 * traversing the EDT. It also means that one of the subroutines
1857 * has set the status field to the proper value. If we get back 1,
1858 * we've fully traversed the EDT and copied out any matching entries.
1861 cdm->status = CAM_DEV_MATCH_LAST;
1867 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1869 struct ccb_dev_match *cdm;
1871 cdm = (struct ccb_dev_match *)arg;
1873 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1874 && (cdm->pos.cookie.pdrv == pdrv)
1875 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1876 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1877 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1878 (*pdrv)->generation)) {
1879 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1883 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1884 && (cdm->pos.cookie.pdrv == pdrv)
1885 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1886 && (cdm->pos.cookie.periph != NULL))
1887 return(xptpdperiphtraverse(pdrv,
1888 (struct cam_periph *)cdm->pos.cookie.periph,
1889 xptplistperiphfunc, arg));
1891 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1895 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1897 struct ccb_dev_match *cdm;
1898 dev_match_ret retval;
1900 cdm = (struct ccb_dev_match *)arg;
1902 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1904 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1905 cdm->status = CAM_DEV_MATCH_ERROR;
1910 * If the copy flag is set, copy this peripheral out.
1912 if (retval & DM_RET_COPY) {
1915 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1916 sizeof(struct dev_match_result));
1919 * If we don't have enough space to put in another
1920 * match result, save our position and tell the
1921 * user there are more devices to check.
1923 if (spaceleft < sizeof(struct dev_match_result)) {
1924 struct periph_driver **pdrv;
1927 bzero(&cdm->pos, sizeof(cdm->pos));
1928 cdm->pos.position_type =
1929 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1933 * This may look a bit non-sensical, but it is
1934 * actually quite logical. There are very few
1935 * peripheral drivers, and bloating every peripheral
1936 * structure with a pointer back to its parent
1937 * peripheral driver linker set entry would cost
1938 * more in the long run than doing this quick lookup.
1940 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1941 if (strcmp((*pdrv)->driver_name,
1942 periph->periph_name) == 0)
1946 if (*pdrv == NULL) {
1947 cdm->status = CAM_DEV_MATCH_ERROR;
1951 cdm->pos.cookie.pdrv = pdrv;
1953 * The periph generation slot does double duty, as
1954 * does the periph pointer slot. They are used for
1955 * both edt and pdrv lookups and positioning.
1957 cdm->pos.cookie.periph = periph;
1958 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1959 (*pdrv)->generation;
1960 cdm->status = CAM_DEV_MATCH_MORE;
1964 j = cdm->num_matches;
1966 cdm->matches[j].type = DEV_MATCH_PERIPH;
1967 cdm->matches[j].result.periph_result.path_id =
1968 periph->path->bus->path_id;
1971 * The transport layer peripheral doesn't have a target or
1974 if (periph->path->target)
1975 cdm->matches[j].result.periph_result.target_id =
1976 periph->path->target->target_id;
1978 cdm->matches[j].result.periph_result.target_id = -1;
1980 if (periph->path->device)
1981 cdm->matches[j].result.periph_result.target_lun =
1982 periph->path->device->lun_id;
1984 cdm->matches[j].result.periph_result.target_lun = -1;
1986 cdm->matches[j].result.periph_result.unit_number =
1987 periph->unit_number;
1988 strncpy(cdm->matches[j].result.periph_result.periph_name,
1989 periph->periph_name, DEV_IDLEN);
1996 xptperiphlistmatch(struct ccb_dev_match *cdm)
2000 cdm->num_matches = 0;
2003 * At this point in the edt traversal function, we check the bus
2004 * list generation to make sure that no busses have been added or
2005 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2006 * For the peripheral driver list traversal function, however, we
2007 * don't have to worry about new peripheral driver types coming or
2008 * going; they're in a linker set, and therefore can't change
2009 * without a recompile.
2012 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2013 && (cdm->pos.cookie.pdrv != NULL))
2014 ret = xptpdrvtraverse(
2015 (struct periph_driver **)cdm->pos.cookie.pdrv,
2016 xptplistpdrvfunc, cdm);
2018 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2021 * If we get back 0, that means that we had to stop before fully
2022 * traversing the peripheral driver tree. It also means that one of
2023 * the subroutines has set the status field to the proper value. If
2024 * we get back 1, we've fully traversed the EDT and copied out any
2028 cdm->status = CAM_DEV_MATCH_LAST;
2034 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2036 struct cam_eb *bus, *next_bus;
2041 mtx_lock(&xsoftc.xpt_topo_lock);
2042 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2045 next_bus = TAILQ_NEXT(bus, links);
2047 mtx_unlock(&xsoftc.xpt_topo_lock);
2048 CAM_SIM_LOCK(bus->sim);
2049 retval = tr_func(bus, arg);
2050 CAM_SIM_UNLOCK(bus->sim);
2053 mtx_lock(&xsoftc.xpt_topo_lock);
2055 mtx_unlock(&xsoftc.xpt_topo_lock);
2061 xpt_sim_opened(struct cam_sim *sim)
2064 struct cam_et *target;
2065 struct cam_ed *device;
2066 struct cam_periph *periph;
2068 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2069 mtx_assert(sim->mtx, MA_OWNED);
2071 mtx_lock(&xsoftc.xpt_topo_lock);
2072 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2073 if (bus->sim != sim)
2076 TAILQ_FOREACH(target, &bus->et_entries, links) {
2077 TAILQ_FOREACH(device, &target->ed_entries, links) {
2078 SLIST_FOREACH(periph, &device->periphs,
2080 if (periph->refcount > 0) {
2081 mtx_unlock(&xsoftc.xpt_topo_lock);
2089 mtx_unlock(&xsoftc.xpt_topo_lock);
2094 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2095 xpt_targetfunc_t *tr_func, void *arg)
2097 struct cam_et *target, *next_target;
2101 for (target = (start_target ? start_target :
2102 TAILQ_FIRST(&bus->et_entries));
2103 target != NULL; target = next_target) {
2105 next_target = TAILQ_NEXT(target, links);
2107 retval = tr_func(target, arg);
2117 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2118 xpt_devicefunc_t *tr_func, void *arg)
2120 struct cam_ed *device, *next_device;
2124 for (device = (start_device ? start_device :
2125 TAILQ_FIRST(&target->ed_entries));
2127 device = next_device) {
2129 next_device = TAILQ_NEXT(device, links);
2131 retval = tr_func(device, arg);
2141 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2142 xpt_periphfunc_t *tr_func, void *arg)
2144 struct cam_periph *periph, *next_periph;
2149 for (periph = (start_periph ? start_periph :
2150 SLIST_FIRST(&device->periphs));
2152 periph = next_periph) {
2154 next_periph = SLIST_NEXT(periph, periph_links);
2156 retval = tr_func(periph, arg);
2165 xptpdrvtraverse(struct periph_driver **start_pdrv,
2166 xpt_pdrvfunc_t *tr_func, void *arg)
2168 struct periph_driver **pdrv;
2174 * We don't traverse the peripheral driver list like we do the
2175 * other lists, because it is a linker set, and therefore cannot be
2176 * changed during runtime. If the peripheral driver list is ever
2177 * re-done to be something other than a linker set (i.e. it can
2178 * change while the system is running), the list traversal should
2179 * be modified to work like the other traversal functions.
2181 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2182 *pdrv != NULL; pdrv++) {
2183 retval = tr_func(pdrv, arg);
2193 xptpdperiphtraverse(struct periph_driver **pdrv,
2194 struct cam_periph *start_periph,
2195 xpt_periphfunc_t *tr_func, void *arg)
2197 struct cam_periph *periph, *next_periph;
2202 for (periph = (start_periph ? start_periph :
2203 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2204 periph = next_periph) {
2206 next_periph = TAILQ_NEXT(periph, unit_links);
2208 retval = tr_func(periph, arg);
2216 xptdefbusfunc(struct cam_eb *bus, void *arg)
2218 struct xpt_traverse_config *tr_config;
2220 tr_config = (struct xpt_traverse_config *)arg;
2222 if (tr_config->depth == XPT_DEPTH_BUS) {
2223 xpt_busfunc_t *tr_func;
2225 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2227 return(tr_func(bus, tr_config->tr_arg));
2229 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2233 xptdeftargetfunc(struct cam_et *target, void *arg)
2235 struct xpt_traverse_config *tr_config;
2237 tr_config = (struct xpt_traverse_config *)arg;
2239 if (tr_config->depth == XPT_DEPTH_TARGET) {
2240 xpt_targetfunc_t *tr_func;
2242 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2244 return(tr_func(target, tr_config->tr_arg));
2246 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2250 xptdefdevicefunc(struct cam_ed *device, void *arg)
2252 struct xpt_traverse_config *tr_config;
2254 tr_config = (struct xpt_traverse_config *)arg;
2256 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2257 xpt_devicefunc_t *tr_func;
2259 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2261 return(tr_func(device, tr_config->tr_arg));
2263 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2267 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2269 struct xpt_traverse_config *tr_config;
2270 xpt_periphfunc_t *tr_func;
2272 tr_config = (struct xpt_traverse_config *)arg;
2274 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2277 * Unlike the other default functions, we don't check for depth
2278 * here. The peripheral driver level is the last level in the EDT,
2279 * so if we're here, we should execute the function in question.
2281 return(tr_func(periph, tr_config->tr_arg));
2285 * Execute the given function for every bus in the EDT.
2288 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2290 struct xpt_traverse_config tr_config;
2292 tr_config.depth = XPT_DEPTH_BUS;
2293 tr_config.tr_func = tr_func;
2294 tr_config.tr_arg = arg;
2296 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2300 * Execute the given function for every device in the EDT.
2303 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2305 struct xpt_traverse_config tr_config;
2307 tr_config.depth = XPT_DEPTH_DEVICE;
2308 tr_config.tr_func = tr_func;
2309 tr_config.tr_arg = arg;
2311 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2315 xptsetasyncfunc(struct cam_ed *device, void *arg)
2317 struct cam_path path;
2318 struct ccb_getdev cgd;
2319 struct async_node *cur_entry;
2321 cur_entry = (struct async_node *)arg;
2324 * Don't report unconfigured devices (Wildcard devs,
2325 * devices only for target mode, device instances
2326 * that have been invalidated but are waiting for
2327 * their last reference count to be released).
2329 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2332 xpt_compile_path(&path,
2334 device->target->bus->path_id,
2335 device->target->target_id,
2337 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2338 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2339 xpt_action((union ccb *)&cgd);
2340 cur_entry->callback(cur_entry->callback_arg,
2343 xpt_release_path(&path);
2349 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2351 struct cam_path path;
2352 struct ccb_pathinq cpi;
2353 struct async_node *cur_entry;
2355 cur_entry = (struct async_node *)arg;
2357 xpt_compile_path(&path, /*periph*/NULL,
2359 CAM_TARGET_WILDCARD,
2361 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2362 cpi.ccb_h.func_code = XPT_PATH_INQ;
2363 xpt_action((union ccb *)&cpi);
2364 cur_entry->callback(cur_entry->callback_arg,
2367 xpt_release_path(&path);
2373 xpt_action_sasync_cb(void *context, int pending)
2375 struct async_node *cur_entry;
2376 struct xpt_task *task;
2379 task = (struct xpt_task *)context;
2380 cur_entry = (struct async_node *)task->data1;
2381 added = task->data2;
2383 if ((added & AC_FOUND_DEVICE) != 0) {
2385 * Get this peripheral up to date with all
2386 * the currently existing devices.
2388 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
2390 if ((added & AC_PATH_REGISTERED) != 0) {
2392 * Get this peripheral up to date with all
2393 * the currently existing busses.
2395 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
2398 free(task, M_CAMXPT);
2402 xpt_action(union ccb *start_ccb)
2405 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2407 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2408 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2412 xpt_action_default(union ccb *start_ccb)
2415 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2418 switch (start_ccb->ccb_h.func_code) {
2421 struct cam_ed *device;
2423 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2424 struct cam_path *path;
2426 path = start_ccb->ccb_h.path;
2430 * For the sake of compatibility with SCSI-1
2431 * devices that may not understand the identify
2432 * message, we include lun information in the
2433 * second byte of all commands. SCSI-1 specifies
2434 * that luns are a 3 bit value and reserves only 3
2435 * bits for lun information in the CDB. Later
2436 * revisions of the SCSI spec allow for more than 8
2437 * luns, but have deprecated lun information in the
2438 * CDB. So, if the lun won't fit, we must omit.
2440 * Also be aware that during initial probing for devices,
2441 * the inquiry information is unknown but initialized to 0.
2442 * This means that this code will be exercised while probing
2443 * devices with an ANSI revision greater than 2.
2445 device = start_ccb->ccb_h.path->device;
2446 if (device->protocol_version <= SCSI_REV_2
2447 && start_ccb->ccb_h.target_lun < 8
2448 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2450 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2451 start_ccb->ccb_h.target_lun << 5;
2453 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2454 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2455 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2456 &path->device->inq_data),
2457 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2458 cdb_str, sizeof(cdb_str))));
2462 case XPT_CONT_TARGET_IO:
2463 start_ccb->csio.sense_resid = 0;
2464 start_ccb->csio.resid = 0;
2467 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2468 start_ccb->ataio.resid = 0;
2473 struct cam_path *path;
2476 path = start_ccb->ccb_h.path;
2478 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2479 if (path->device->qfrozen_cnt == 0)
2480 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2484 xpt_run_dev_sendq(path->bus);
2487 case XPT_CALC_GEOMETRY:
2489 struct cam_sim *sim;
2491 /* Filter out garbage */
2492 if (start_ccb->ccg.block_size == 0
2493 || start_ccb->ccg.volume_size == 0) {
2494 start_ccb->ccg.cylinders = 0;
2495 start_ccb->ccg.heads = 0;
2496 start_ccb->ccg.secs_per_track = 0;
2497 start_ccb->ccb_h.status = CAM_REQ_CMP;
2502 * In a PC-98 system, geometry translation depens on
2503 * the "real" device geometry obtained from mode page 4.
2504 * SCSI geometry translation is performed in the
2505 * initialization routine of the SCSI BIOS and the result
2506 * stored in host memory. If the translation is available
2507 * in host memory, use it. If not, rely on the default
2508 * translation the device driver performs.
2510 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2511 start_ccb->ccb_h.status = CAM_REQ_CMP;
2515 sim = start_ccb->ccb_h.path->bus->sim;
2516 (*(sim->sim_action))(sim, start_ccb);
2521 union ccb* abort_ccb;
2523 abort_ccb = start_ccb->cab.abort_ccb;
2524 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2526 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2527 struct cam_ccbq *ccbq;
2529 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2530 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2531 abort_ccb->ccb_h.status =
2532 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2533 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2534 xpt_done(abort_ccb);
2535 start_ccb->ccb_h.status = CAM_REQ_CMP;
2538 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2539 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2541 * We've caught this ccb en route to
2542 * the SIM. Flag it for abort and the
2543 * SIM will do so just before starting
2544 * real work on the CCB.
2546 abort_ccb->ccb_h.status =
2547 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2548 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2549 start_ccb->ccb_h.status = CAM_REQ_CMP;
2553 if (XPT_FC_IS_QUEUED(abort_ccb)
2554 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2556 * It's already completed but waiting
2557 * for our SWI to get to it.
2559 start_ccb->ccb_h.status = CAM_UA_ABORT;
2563 * If we weren't able to take care of the abort request
2564 * in the XPT, pass the request down to the SIM for processing.
2568 case XPT_ACCEPT_TARGET_IO:
2570 case XPT_IMMED_NOTIFY:
2571 case XPT_NOTIFY_ACK:
2573 case XPT_IMMEDIATE_NOTIFY:
2574 case XPT_NOTIFY_ACKNOWLEDGE:
2575 case XPT_GET_SIM_KNOB:
2576 case XPT_SET_SIM_KNOB:
2578 struct cam_sim *sim;
2580 sim = start_ccb->ccb_h.path->bus->sim;
2581 (*(sim->sim_action))(sim, start_ccb);
2586 struct cam_sim *sim;
2588 sim = start_ccb->ccb_h.path->bus->sim;
2589 (*(sim->sim_action))(sim, start_ccb);
2592 case XPT_PATH_STATS:
2593 start_ccb->cpis.last_reset =
2594 start_ccb->ccb_h.path->bus->last_reset;
2595 start_ccb->ccb_h.status = CAM_REQ_CMP;
2601 dev = start_ccb->ccb_h.path->device;
2602 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2603 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2605 struct ccb_getdev *cgd;
2609 cgd = &start_ccb->cgd;
2610 bus = cgd->ccb_h.path->bus;
2611 tar = cgd->ccb_h.path->target;
2612 cgd->protocol = dev->protocol;
2613 cgd->inq_data = dev->inq_data;
2614 cgd->ident_data = dev->ident_data;
2615 cgd->ccb_h.status = CAM_REQ_CMP;
2616 cgd->serial_num_len = dev->serial_num_len;
2617 if ((dev->serial_num_len > 0)
2618 && (dev->serial_num != NULL))
2619 bcopy(dev->serial_num, cgd->serial_num,
2620 dev->serial_num_len);
2624 case XPT_GDEV_STATS:
2628 dev = start_ccb->ccb_h.path->device;
2629 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2630 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2632 struct ccb_getdevstats *cgds;
2636 cgds = &start_ccb->cgds;
2637 bus = cgds->ccb_h.path->bus;
2638 tar = cgds->ccb_h.path->target;
2639 cgds->dev_openings = dev->ccbq.dev_openings;
2640 cgds->dev_active = dev->ccbq.dev_active;
2641 cgds->devq_openings = dev->ccbq.devq_openings;
2642 cgds->devq_queued = dev->ccbq.queue.entries;
2643 cgds->held = dev->ccbq.held;
2644 cgds->last_reset = tar->last_reset;
2645 cgds->maxtags = dev->maxtags;
2646 cgds->mintags = dev->mintags;
2647 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2648 cgds->last_reset = bus->last_reset;
2649 cgds->ccb_h.status = CAM_REQ_CMP;
2655 struct cam_periph *nperiph;
2656 struct periph_list *periph_head;
2657 struct ccb_getdevlist *cgdl;
2659 struct cam_ed *device;
2666 * Don't want anyone mucking with our data.
2668 device = start_ccb->ccb_h.path->device;
2669 periph_head = &device->periphs;
2670 cgdl = &start_ccb->cgdl;
2673 * Check and see if the list has changed since the user
2674 * last requested a list member. If so, tell them that the
2675 * list has changed, and therefore they need to start over
2676 * from the beginning.
2678 if ((cgdl->index != 0) &&
2679 (cgdl->generation != device->generation)) {
2680 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2685 * Traverse the list of peripherals and attempt to find
2686 * the requested peripheral.
2688 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2689 (nperiph != NULL) && (i <= cgdl->index);
2690 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2691 if (i == cgdl->index) {
2692 strncpy(cgdl->periph_name,
2693 nperiph->periph_name,
2695 cgdl->unit_number = nperiph->unit_number;
2700 cgdl->status = CAM_GDEVLIST_ERROR;
2704 if (nperiph == NULL)
2705 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2707 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2710 cgdl->generation = device->generation;
2712 cgdl->ccb_h.status = CAM_REQ_CMP;
2717 dev_pos_type position_type;
2718 struct ccb_dev_match *cdm;
2720 cdm = &start_ccb->cdm;
2723 * There are two ways of getting at information in the EDT.
2724 * The first way is via the primary EDT tree. It starts
2725 * with a list of busses, then a list of targets on a bus,
2726 * then devices/luns on a target, and then peripherals on a
2727 * device/lun. The "other" way is by the peripheral driver
2728 * lists. The peripheral driver lists are organized by
2729 * peripheral driver. (obviously) So it makes sense to
2730 * use the peripheral driver list if the user is looking
2731 * for something like "da1", or all "da" devices. If the
2732 * user is looking for something on a particular bus/target
2733 * or lun, it's generally better to go through the EDT tree.
2736 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2737 position_type = cdm->pos.position_type;
2741 position_type = CAM_DEV_POS_NONE;
2743 for (i = 0; i < cdm->num_patterns; i++) {
2744 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2745 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2746 position_type = CAM_DEV_POS_EDT;
2751 if (cdm->num_patterns == 0)
2752 position_type = CAM_DEV_POS_EDT;
2753 else if (position_type == CAM_DEV_POS_NONE)
2754 position_type = CAM_DEV_POS_PDRV;
2757 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2758 case CAM_DEV_POS_EDT:
2761 case CAM_DEV_POS_PDRV:
2762 xptperiphlistmatch(cdm);
2765 cdm->status = CAM_DEV_MATCH_ERROR;
2769 if (cdm->status == CAM_DEV_MATCH_ERROR)
2770 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2772 start_ccb->ccb_h.status = CAM_REQ_CMP;
2778 struct ccb_setasync *csa;
2779 struct async_node *cur_entry;
2780 struct async_list *async_head;
2783 csa = &start_ccb->csa;
2784 added = csa->event_enable;
2785 async_head = &csa->ccb_h.path->device->asyncs;
2788 * If there is already an entry for us, simply
2791 cur_entry = SLIST_FIRST(async_head);
2792 while (cur_entry != NULL) {
2793 if ((cur_entry->callback_arg == csa->callback_arg)
2794 && (cur_entry->callback == csa->callback))
2796 cur_entry = SLIST_NEXT(cur_entry, links);
2799 if (cur_entry != NULL) {
2801 * If the request has no flags set,
2804 added &= ~cur_entry->event_enable;
2805 if (csa->event_enable == 0) {
2806 SLIST_REMOVE(async_head, cur_entry,
2808 csa->ccb_h.path->device->refcount--;
2809 free(cur_entry, M_CAMXPT);
2811 cur_entry->event_enable = csa->event_enable;
2814 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2816 if (cur_entry == NULL) {
2817 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2820 cur_entry->event_enable = csa->event_enable;
2821 cur_entry->callback_arg = csa->callback_arg;
2822 cur_entry->callback = csa->callback;
2823 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2824 csa->ccb_h.path->device->refcount++;
2828 * Need to decouple this operation via a taqskqueue so that
2829 * the locking doesn't become a mess.
2831 if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) {
2832 struct xpt_task *task;
2834 task = malloc(sizeof(struct xpt_task), M_CAMXPT,
2837 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2841 TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task);
2842 task->data1 = cur_entry;
2843 task->data2 = added;
2844 taskqueue_enqueue(taskqueue_thread, &task->task);
2847 start_ccb->ccb_h.status = CAM_REQ_CMP;
2852 struct ccb_relsim *crs;
2855 crs = &start_ccb->crs;
2856 dev = crs->ccb_h.path->device;
2859 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2863 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2865 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2866 /* Don't ever go below one opening */
2867 if (crs->openings > 0) {
2868 xpt_dev_ccbq_resize(crs->ccb_h.path,
2872 xpt_print(crs->ccb_h.path,
2873 "tagged openings now %d\n",
2880 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2882 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2885 * Just extend the old timeout and decrement
2886 * the freeze count so that a single timeout
2887 * is sufficient for releasing the queue.
2889 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2890 callout_stop(&dev->callout);
2893 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2896 callout_reset(&dev->callout,
2897 (crs->release_timeout * hz) / 1000,
2898 xpt_release_devq_timeout, dev);
2900 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2904 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2906 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2908 * Decrement the freeze count so that a single
2909 * completion is still sufficient to unfreeze
2912 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2915 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2916 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2920 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2922 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2923 || (dev->ccbq.dev_active == 0)) {
2925 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2928 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2929 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2933 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2935 xpt_release_devq(crs->ccb_h.path, /*count*/1,
2938 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
2939 start_ccb->ccb_h.status = CAM_REQ_CMP;
2944 #ifdef CAM_DEBUG_DELAY
2945 cam_debug_delay = CAM_DEBUG_DELAY;
2947 cam_dflags = start_ccb->cdbg.flags;
2948 if (cam_dpath != NULL) {
2949 xpt_free_path(cam_dpath);
2953 if (cam_dflags != CAM_DEBUG_NONE) {
2954 if (xpt_create_path(&cam_dpath, xpt_periph,
2955 start_ccb->ccb_h.path_id,
2956 start_ccb->ccb_h.target_id,
2957 start_ccb->ccb_h.target_lun) !=
2959 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2960 cam_dflags = CAM_DEBUG_NONE;
2962 start_ccb->ccb_h.status = CAM_REQ_CMP;
2963 xpt_print(cam_dpath, "debugging flags now %x\n",
2968 start_ccb->ccb_h.status = CAM_REQ_CMP;
2970 #else /* !CAMDEBUG */
2971 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2972 #endif /* CAMDEBUG */
2976 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2977 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
2978 start_ccb->ccb_h.status = CAM_REQ_CMP;
2985 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2991 xpt_polled_action(union ccb *start_ccb)
2994 struct cam_sim *sim;
2995 struct cam_devq *devq;
2999 timeout = start_ccb->ccb_h.timeout;
3000 sim = start_ccb->ccb_h.path->bus->sim;
3002 dev = start_ccb->ccb_h.path->device;
3004 mtx_assert(sim->mtx, MA_OWNED);
3007 * Steal an opening so that no other queued requests
3008 * can get it before us while we simulate interrupts.
3010 dev->ccbq.devq_openings--;
3011 dev->ccbq.dev_openings--;
3013 while(((devq != NULL && devq->send_openings <= 0) ||
3014 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3016 (*(sim->sim_poll))(sim);
3017 camisr_runqueue(&sim->sim_doneq);
3020 dev->ccbq.devq_openings++;
3021 dev->ccbq.dev_openings++;
3024 xpt_action(start_ccb);
3025 while(--timeout > 0) {
3026 (*(sim->sim_poll))(sim);
3027 camisr_runqueue(&sim->sim_doneq);
3028 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3035 * XXX Is it worth adding a sim_timeout entry
3036 * point so we can attempt recovery? If
3037 * this is only used for dumps, I don't think
3040 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3043 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3048 * Schedule a peripheral driver to receive a ccb when it's
3049 * target device has space for more transactions.
3052 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3054 struct cam_ed *device;
3057 mtx_assert(perph->sim->mtx, MA_OWNED);
3059 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3060 device = perph->path->device;
3061 if (periph_is_queued(perph)) {
3062 /* Simply reorder based on new priority */
3063 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3064 (" change priority to %d\n", new_priority));
3065 if (new_priority < perph->pinfo.priority) {
3066 camq_change_priority(&device->drvq,
3072 /* New entry on the queue */
3073 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3074 (" added periph to queue\n"));
3075 perph->pinfo.priority = new_priority;
3076 perph->pinfo.generation = ++device->drvq.generation;
3077 camq_insert(&device->drvq, &perph->pinfo);
3078 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3081 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3082 (" calling xpt_run_devq\n"));
3083 xpt_run_dev_allocq(perph->path->bus);
3089 * Schedule a device to run on a given queue.
3090 * If the device was inserted as a new entry on the queue,
3091 * return 1 meaning the device queue should be run. If we
3092 * were already queued, implying someone else has already
3093 * started the queue, return 0 so the caller doesn't attempt
3097 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3098 u_int32_t new_priority)
3101 u_int32_t old_priority;
3103 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3105 old_priority = pinfo->priority;
3108 * Are we already queued?
3110 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3111 /* Simply reorder based on new priority */
3112 if (new_priority < old_priority) {
3113 camq_change_priority(queue, pinfo->index,
3115 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3116 ("changed priority to %d\n",
3121 /* New entry on the queue */
3122 if (new_priority < old_priority)
3123 pinfo->priority = new_priority;
3125 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3126 ("Inserting onto queue\n"));
3127 pinfo->generation = ++queue->generation;
3128 camq_insert(queue, pinfo);
3135 xpt_run_dev_allocq(struct cam_eb *bus)
3137 struct cam_devq *devq;
3139 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3140 devq = bus->sim->devq;
3142 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3143 (" qfrozen_cnt == 0x%x, entries == %d, "
3144 "openings == %d, active == %d\n",
3145 devq->alloc_queue.qfrozen_cnt,
3146 devq->alloc_queue.entries,
3147 devq->alloc_openings,
3148 devq->alloc_active));
3150 devq->alloc_queue.qfrozen_cnt++;
3151 while ((devq->alloc_queue.entries > 0)
3152 && (devq->alloc_openings > 0)
3153 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3154 struct cam_ed_qinfo *qinfo;
3155 struct cam_ed *device;
3156 union ccb *work_ccb;
3157 struct cam_periph *drv;
3160 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3162 device = qinfo->device;
3164 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3165 ("running device %p\n", device));
3167 drvq = &device->drvq;
3170 if (drvq->entries <= 0) {
3171 panic("xpt_run_dev_allocq: "
3172 "Device on queue without any work to do");
3175 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3176 devq->alloc_openings--;
3177 devq->alloc_active++;
3178 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3179 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3180 drv->pinfo.priority);
3181 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3182 ("calling periph start\n"));
3183 drv->periph_start(drv, work_ccb);
3186 * Malloc failure in alloc_ccb
3189 * XXX add us to a list to be run from free_ccb
3190 * if we don't have any ccbs active on this
3191 * device queue otherwise we may never get run
3197 if (drvq->entries > 0) {
3198 /* We have more work. Attempt to reschedule */
3199 xpt_schedule_dev_allocq(bus, device);
3202 devq->alloc_queue.qfrozen_cnt--;
3206 xpt_run_dev_sendq(struct cam_eb *bus)
3208 struct cam_devq *devq;
3210 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3212 devq = bus->sim->devq;
3214 devq->send_queue.qfrozen_cnt++;
3215 while ((devq->send_queue.entries > 0)
3216 && (devq->send_openings > 0)) {
3217 struct cam_ed_qinfo *qinfo;
3218 struct cam_ed *device;
3219 union ccb *work_ccb;
3220 struct cam_sim *sim;
3222 if (devq->send_queue.qfrozen_cnt > 1) {
3226 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3228 device = qinfo->device;
3231 * If the device has been "frozen", don't attempt
3234 if (device->qfrozen_cnt > 0) {
3238 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3239 ("running device %p\n", device));
3241 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3242 if (work_ccb == NULL) {
3243 printf("device on run queue with no ccbs???\n");
3247 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3249 mtx_lock(&xsoftc.xpt_lock);
3250 if (xsoftc.num_highpower <= 0) {
3252 * We got a high power command, but we
3253 * don't have any available slots. Freeze
3254 * the device queue until we have a slot
3257 device->qfrozen_cnt++;
3258 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3262 mtx_unlock(&xsoftc.xpt_lock);
3266 * Consume a high power slot while
3269 xsoftc.num_highpower--;
3271 mtx_unlock(&xsoftc.xpt_lock);
3273 devq->active_dev = device;
3274 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3276 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3278 devq->send_openings--;
3279 devq->send_active++;
3281 if (device->ccbq.queue.entries > 0)
3282 xpt_schedule_dev_sendq(bus, device);
3284 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3286 * The client wants to freeze the queue
3287 * after this CCB is sent.
3289 device->qfrozen_cnt++;
3292 /* In Target mode, the peripheral driver knows best... */
3293 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3294 if ((device->inq_flags & SID_CmdQue) != 0
3295 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3296 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3299 * Clear this in case of a retried CCB that
3300 * failed due to a rejected tag.
3302 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3306 * Device queues can be shared among multiple sim instances
3307 * that reside on different busses. Use the SIM in the queue
3308 * CCB's path, rather than the one in the bus that was passed
3309 * into this function.
3311 sim = work_ccb->ccb_h.path->bus->sim;
3312 (*(sim->sim_action))(sim, work_ccb);
3314 devq->active_dev = NULL;
3316 devq->send_queue.qfrozen_cnt--;
3320 * This function merges stuff from the slave ccb into the master ccb, while
3321 * keeping important fields in the master ccb constant.
3324 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3328 * Pull fields that are valid for peripheral drivers to set
3329 * into the master CCB along with the CCB "payload".
3331 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3332 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3333 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3334 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3335 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3336 sizeof(union ccb) - sizeof(struct ccb_hdr));
3340 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3343 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3344 ccb_h->pinfo.priority = priority;
3346 ccb_h->path_id = path->bus->path_id;
3348 ccb_h->target_id = path->target->target_id;
3350 ccb_h->target_id = CAM_TARGET_WILDCARD;
3352 ccb_h->target_lun = path->device->lun_id;
3353 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3355 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3357 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3361 /* Path manipulation functions */
3363 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3364 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3366 struct cam_path *path;
3369 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3372 status = CAM_RESRC_UNAVAIL;
3375 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3376 if (status != CAM_REQ_CMP) {
3377 free(path, M_CAMXPT);
3380 *new_path_ptr = path;
3385 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3386 struct cam_periph *periph, path_id_t path_id,
3387 target_id_t target_id, lun_id_t lun_id)
3389 struct cam_path *path;
3390 struct cam_eb *bus = NULL;
3392 int need_unlock = 0;
3394 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3396 if (path_id != CAM_BUS_WILDCARD) {
3397 bus = xpt_find_bus(path_id);
3400 CAM_SIM_LOCK(bus->sim);
3403 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3405 CAM_SIM_UNLOCK(bus->sim);
3406 if (status != CAM_REQ_CMP) {
3407 free(path, M_CAMXPT);
3410 *new_path_ptr = path;
3415 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3416 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3419 struct cam_et *target;
3420 struct cam_ed *device;
3423 status = CAM_REQ_CMP; /* Completed without error */
3424 target = NULL; /* Wildcarded */
3425 device = NULL; /* Wildcarded */
3428 * We will potentially modify the EDT, so block interrupts
3429 * that may attempt to create cam paths.
3431 bus = xpt_find_bus(path_id);
3433 status = CAM_PATH_INVALID;
3435 target = xpt_find_target(bus, target_id);
3436 if (target == NULL) {
3438 struct cam_et *new_target;
3440 new_target = xpt_alloc_target(bus, target_id);
3441 if (new_target == NULL) {
3442 status = CAM_RESRC_UNAVAIL;
3444 target = new_target;
3447 if (target != NULL) {
3448 device = xpt_find_device(target, lun_id);
3449 if (device == NULL) {
3451 struct cam_ed *new_device;
3454 (*(bus->xport->alloc_device))(bus,
3457 if (new_device == NULL) {
3458 status = CAM_RESRC_UNAVAIL;
3460 device = new_device;
3467 * Only touch the user's data if we are successful.
3469 if (status == CAM_REQ_CMP) {
3470 new_path->periph = perph;
3471 new_path->bus = bus;
3472 new_path->target = target;
3473 new_path->device = device;
3474 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3477 xpt_release_device(bus, target, device);
3479 xpt_release_target(bus, target);
3481 xpt_release_bus(bus);
3487 xpt_release_path(struct cam_path *path)
3489 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3490 if (path->device != NULL) {
3491 xpt_release_device(path->bus, path->target, path->device);
3492 path->device = NULL;
3494 if (path->target != NULL) {
3495 xpt_release_target(path->bus, path->target);
3496 path->target = NULL;
3498 if (path->bus != NULL) {
3499 xpt_release_bus(path->bus);
3505 xpt_free_path(struct cam_path *path)
3508 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3509 xpt_release_path(path);
3510 free(path, M_CAMXPT);
3515 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3516 * in path1, 2 for match with wildcards in path2.
3519 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3523 if (path1->bus != path2->bus) {
3524 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3526 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3531 if (path1->target != path2->target) {
3532 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3535 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3540 if (path1->device != path2->device) {
3541 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3544 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3553 xpt_print_path(struct cam_path *path)
3557 printf("(nopath): ");
3559 if (path->periph != NULL)
3560 printf("(%s%d:", path->periph->periph_name,
3561 path->periph->unit_number);
3563 printf("(noperiph:");
3565 if (path->bus != NULL)
3566 printf("%s%d:%d:", path->bus->sim->sim_name,
3567 path->bus->sim->unit_number,
3568 path->bus->sim->bus_id);
3572 if (path->target != NULL)
3573 printf("%d:", path->target->target_id);
3577 if (path->device != NULL)
3578 printf("%d): ", path->device->lun_id);
3585 xpt_print(struct cam_path *path, const char *fmt, ...)
3588 xpt_print_path(path);
3595 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3600 if (path != NULL && path->bus != NULL)
3601 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3604 sbuf_new(&sb, str, str_len, 0);
3607 sbuf_printf(&sb, "(nopath): ");
3609 if (path->periph != NULL)
3610 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3611 path->periph->unit_number);
3613 sbuf_printf(&sb, "(noperiph:");
3615 if (path->bus != NULL)
3616 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3617 path->bus->sim->unit_number,
3618 path->bus->sim->bus_id);
3620 sbuf_printf(&sb, "nobus:");
3622 if (path->target != NULL)
3623 sbuf_printf(&sb, "%d:", path->target->target_id);
3625 sbuf_printf(&sb, "X:");
3627 if (path->device != NULL)
3628 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3630 sbuf_printf(&sb, "X): ");
3634 return(sbuf_len(&sb));
3638 xpt_path_path_id(struct cam_path *path)
3640 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3642 return(path->bus->path_id);
3646 xpt_path_target_id(struct cam_path *path)
3648 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3650 if (path->target != NULL)
3651 return (path->target->target_id);
3653 return (CAM_TARGET_WILDCARD);
3657 xpt_path_lun_id(struct cam_path *path)
3659 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3661 if (path->device != NULL)
3662 return (path->device->lun_id);
3664 return (CAM_LUN_WILDCARD);
3668 xpt_path_sim(struct cam_path *path)
3671 return (path->bus->sim);
3675 xpt_path_periph(struct cam_path *path)
3677 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3679 return (path->periph);
3683 * Release a CAM control block for the caller. Remit the cost of the structure
3684 * to the device referenced by the path. If the this device had no 'credits'
3685 * and peripheral drivers have registered async callbacks for this notification
3689 xpt_release_ccb(union ccb *free_ccb)
3691 struct cam_path *path;
3692 struct cam_ed *device;
3694 struct cam_sim *sim;
3696 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3697 path = free_ccb->ccb_h.path;
3698 device = path->device;
3702 mtx_assert(sim->mtx, MA_OWNED);
3704 cam_ccbq_release_opening(&device->ccbq);
3705 if (sim->ccb_count > sim->max_ccbs) {
3706 xpt_free_ccb(free_ccb);
3709 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3712 if (sim->devq == NULL) {
3715 sim->devq->alloc_openings++;
3716 sim->devq->alloc_active--;
3717 /* XXX Turn this into an inline function - xpt_run_device?? */
3718 if ((device_is_alloc_queued(device) == 0)
3719 && (device->drvq.entries > 0)) {
3720 xpt_schedule_dev_allocq(bus, device);
3722 if (dev_allocq_is_runnable(sim->devq))
3723 xpt_run_dev_allocq(bus);
3726 /* Functions accessed by SIM drivers */
3728 static struct xpt_xport xport_default = {
3729 .alloc_device = xpt_alloc_device_default,
3730 .action = xpt_action_default,
3731 .async = xpt_dev_async_default,
3735 * A sim structure, listing the SIM entry points and instance
3736 * identification info is passed to xpt_bus_register to hook the SIM
3737 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3738 * for this new bus and places it in the array of busses and assigns
3739 * it a path_id. The path_id may be influenced by "hard wiring"
3740 * information specified by the user. Once interrupt services are
3741 * available, the bus will be probed.
3744 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3746 struct cam_eb *new_bus;
3747 struct cam_eb *old_bus;
3748 struct ccb_pathinq cpi;
3749 struct cam_path path;
3752 mtx_assert(sim->mtx, MA_OWNED);
3755 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3756 M_CAMXPT, M_NOWAIT);
3757 if (new_bus == NULL) {
3758 /* Couldn't satisfy request */
3759 return (CAM_RESRC_UNAVAIL);
3762 if (strcmp(sim->sim_name, "xpt") != 0) {
3764 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3767 TAILQ_INIT(&new_bus->et_entries);
3768 new_bus->path_id = sim->path_id;
3771 timevalclear(&new_bus->last_reset);
3773 new_bus->refcount = 1; /* Held until a bus_deregister event */
3774 new_bus->generation = 0;
3776 mtx_lock(&xsoftc.xpt_topo_lock);
3777 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3778 while (old_bus != NULL
3779 && old_bus->path_id < new_bus->path_id)
3780 old_bus = TAILQ_NEXT(old_bus, links);
3781 if (old_bus != NULL)
3782 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3784 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3785 xsoftc.bus_generation++;
3786 mtx_unlock(&xsoftc.xpt_topo_lock);
3789 * Set a default transport so that a PATH_INQ can be issued to
3790 * the SIM. This will then allow for probing and attaching of
3791 * a more appropriate transport.
3793 new_bus->xport = &xport_default;
3795 bzero(&path, sizeof(path));
3796 status = xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
3797 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3798 if (status != CAM_REQ_CMP)
3799 printf("xpt_compile_path returned %d\n", status);
3801 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
3802 cpi.ccb_h.func_code = XPT_PATH_INQ;
3803 xpt_action((union ccb *)&cpi);
3805 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3806 switch (cpi.transport) {
3813 new_bus->xport = scsi_get_xport();
3817 new_bus->xport = ata_get_xport();
3820 new_bus->xport = &xport_default;
3825 /* Notify interested parties */
3826 if (sim->path_id != CAM_XPT_PATH_ID) {
3827 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
3829 xpt_release_path(&path);
3830 return (CAM_SUCCESS);
3834 xpt_bus_deregister(path_id_t pathid)
3836 struct cam_path bus_path;
3839 status = xpt_compile_path(&bus_path, NULL, pathid,
3840 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3841 if (status != CAM_REQ_CMP)
3844 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3845 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3847 /* Release the reference count held while registered. */
3848 xpt_release_bus(bus_path.bus);
3849 xpt_release_path(&bus_path);
3851 return (CAM_REQ_CMP);
3855 xptnextfreepathid(void)
3862 mtx_lock(&xsoftc.xpt_topo_lock);
3863 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3865 /* Find an unoccupied pathid */
3866 while (bus != NULL && bus->path_id <= pathid) {
3867 if (bus->path_id == pathid)
3869 bus = TAILQ_NEXT(bus, links);
3871 mtx_unlock(&xsoftc.xpt_topo_lock);
3874 * Ensure that this pathid is not reserved for
3875 * a bus that may be registered in the future.
3877 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3879 /* Start the search over */
3880 mtx_lock(&xsoftc.xpt_topo_lock);
3887 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3894 pathid = CAM_XPT_PATH_ID;
3895 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3897 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3898 if (strcmp(dname, "scbus")) {
3899 /* Avoid a bit of foot shooting. */
3902 if (dunit < 0) /* unwired?! */
3904 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3905 if (sim_bus == val) {
3909 } else if (sim_bus == 0) {
3910 /* Unspecified matches bus 0 */
3914 printf("Ambiguous scbus configuration for %s%d "
3915 "bus %d, cannot wire down. The kernel "
3916 "config entry for scbus%d should "
3917 "specify a controller bus.\n"
3918 "Scbus will be assigned dynamically.\n",
3919 sim_name, sim_unit, sim_bus, dunit);
3924 if (pathid == CAM_XPT_PATH_ID)
3925 pathid = xptnextfreepathid();
3930 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
3933 struct cam_et *target, *next_target;
3934 struct cam_ed *device, *next_device;
3936 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3938 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
3941 * Most async events come from a CAM interrupt context. In
3942 * a few cases, the error recovery code at the peripheral layer,
3943 * which may run from our SWI or a process context, may signal
3944 * deferred events with a call to xpt_async.
3949 if (async_code == AC_BUS_RESET) {
3950 /* Update our notion of when the last reset occurred */
3951 microtime(&bus->last_reset);
3954 for (target = TAILQ_FIRST(&bus->et_entries);
3956 target = next_target) {
3958 next_target = TAILQ_NEXT(target, links);
3960 if (path->target != target
3961 && path->target->target_id != CAM_TARGET_WILDCARD
3962 && target->target_id != CAM_TARGET_WILDCARD)
3965 if (async_code == AC_SENT_BDR) {
3966 /* Update our notion of when the last reset occurred */
3967 microtime(&path->target->last_reset);
3970 for (device = TAILQ_FIRST(&target->ed_entries);
3972 device = next_device) {
3974 next_device = TAILQ_NEXT(device, links);
3976 if (path->device != device
3977 && path->device->lun_id != CAM_LUN_WILDCARD
3978 && device->lun_id != CAM_LUN_WILDCARD)
3981 (*(bus->xport->async))(async_code, bus,
3985 xpt_async_bcast(&device->asyncs, async_code,
3991 * If this wasn't a fully wildcarded async, tell all
3992 * clients that want all async events.
3994 if (bus != xpt_periph->path->bus)
3995 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4000 xpt_async_bcast(struct async_list *async_head,
4001 u_int32_t async_code,
4002 struct cam_path *path, void *async_arg)
4004 struct async_node *cur_entry;
4006 cur_entry = SLIST_FIRST(async_head);
4007 while (cur_entry != NULL) {
4008 struct async_node *next_entry;
4010 * Grab the next list entry before we call the current
4011 * entry's callback. This is because the callback function
4012 * can delete its async callback entry.
4014 next_entry = SLIST_NEXT(cur_entry, links);
4015 if ((cur_entry->event_enable & async_code) != 0)
4016 cur_entry->callback(cur_entry->callback_arg,
4019 cur_entry = next_entry;
4024 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4025 struct cam_et *target, struct cam_ed *device,
4028 printf("xpt_dev_async called\n");
4032 xpt_freeze_devq(struct cam_path *path, u_int count)
4034 struct ccb_hdr *ccbh;
4036 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4038 path->device->qfrozen_cnt += count;
4041 * Mark the last CCB in the queue as needing
4042 * to be requeued if the driver hasn't
4043 * changed it's state yet. This fixes a race
4044 * where a ccb is just about to be queued to
4045 * a controller driver when it's interrupt routine
4046 * freezes the queue. To completly close the
4047 * hole, controller drives must check to see
4048 * if a ccb's status is still CAM_REQ_INPROG
4049 * just before they queue
4050 * the CCB. See ahc_action/ahc_freeze_devq for
4053 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4054 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4055 ccbh->status = CAM_REQUEUE_REQ;
4056 return (path->device->qfrozen_cnt);
4060 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4062 mtx_assert(sim->mtx, MA_OWNED);
4064 sim->devq->send_queue.qfrozen_cnt += count;
4065 if (sim->devq->active_dev != NULL) {
4066 struct ccb_hdr *ccbh;
4068 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4070 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4071 ccbh->status = CAM_REQUEUE_REQ;
4073 return (sim->devq->send_queue.qfrozen_cnt);
4077 xpt_release_devq_timeout(void *arg)
4079 struct cam_ed *device;
4081 device = (struct cam_ed *)arg;
4083 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4087 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4089 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4091 xpt_release_devq_device(path->device, count, run_queue);
4095 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4100 if (dev->qfrozen_cnt > 0) {
4102 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4103 dev->qfrozen_cnt -= count;
4104 if (dev->qfrozen_cnt == 0) {
4107 * No longer need to wait for a successful
4108 * command completion.
4110 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4113 * Remove any timeouts that might be scheduled
4114 * to release this queue.
4116 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4117 callout_stop(&dev->callout);
4118 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4122 * Now that we are unfrozen schedule the
4123 * device so any pending transactions are
4126 if ((dev->ccbq.queue.entries > 0)
4127 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4128 && (run_queue != 0)) {
4134 xpt_run_dev_sendq(dev->target->bus);
4138 xpt_release_simq(struct cam_sim *sim, int run_queue)
4142 mtx_assert(sim->mtx, MA_OWNED);
4144 sendq = &(sim->devq->send_queue);
4145 if (sendq->qfrozen_cnt > 0) {
4147 sendq->qfrozen_cnt--;
4148 if (sendq->qfrozen_cnt == 0) {
4152 * If there is a timeout scheduled to release this
4153 * sim queue, remove it. The queue frozen count is
4156 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4157 callout_stop(&sim->callout);
4158 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4160 bus = xpt_find_bus(sim->path_id);
4164 * Now that we are unfrozen run the send queue.
4166 xpt_run_dev_sendq(bus);
4168 xpt_release_bus(bus);
4174 * XXX Appears to be unused.
4177 xpt_release_simq_timeout(void *arg)
4179 struct cam_sim *sim;
4181 sim = (struct cam_sim *)arg;
4182 xpt_release_simq(sim, /* run_queue */ TRUE);
4186 xpt_done(union ccb *done_ccb)
4188 struct cam_sim *sim;
4190 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4191 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4193 * Queue up the request for handling by our SWI handler
4194 * any of the "non-immediate" type of ccbs.
4196 sim = done_ccb->ccb_h.path->bus->sim;
4197 switch (done_ccb->ccb_h.path->periph->type) {
4198 case CAM_PERIPH_BIO:
4199 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4201 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4202 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4203 mtx_lock(&cam_simq_lock);
4204 TAILQ_INSERT_TAIL(&cam_simq, sim,
4206 sim->flags |= CAM_SIM_ON_DONEQ;
4207 mtx_unlock(&cam_simq_lock);
4209 if ((done_ccb->ccb_h.path->periph->flags &
4210 CAM_PERIPH_POLLED) == 0)
4211 swi_sched(cambio_ih, 0);
4214 panic("unknown periph type %d",
4215 done_ccb->ccb_h.path->periph->type);
4225 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4230 xpt_alloc_ccb_nowait()
4234 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4239 xpt_free_ccb(union ccb *free_ccb)
4241 free(free_ccb, M_CAMXPT);
4246 /* Private XPT functions */
4249 * Get a CAM control block for the caller. Charge the structure to the device
4250 * referenced by the path. If the this device has no 'credits' then the
4251 * device already has the maximum number of outstanding operations under way
4252 * and we return NULL. If we don't have sufficient resources to allocate more
4253 * ccbs, we also return NULL.
4256 xpt_get_ccb(struct cam_ed *device)
4259 struct cam_sim *sim;
4262 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4263 new_ccb = xpt_alloc_ccb_nowait();
4264 if (new_ccb == NULL) {
4267 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4268 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4269 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4273 cam_ccbq_take_opening(&device->ccbq);
4274 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4279 xpt_release_bus(struct cam_eb *bus)
4282 if ((--bus->refcount == 0)
4283 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4284 mtx_lock(&xsoftc.xpt_topo_lock);
4285 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4286 xsoftc.bus_generation++;
4287 mtx_unlock(&xsoftc.xpt_topo_lock);
4288 cam_sim_release(bus->sim);
4289 free(bus, M_CAMXPT);
4293 static struct cam_et *
4294 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4296 struct cam_et *target;
4298 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4299 if (target != NULL) {
4300 struct cam_et *cur_target;
4302 TAILQ_INIT(&target->ed_entries);
4304 target->target_id = target_id;
4305 target->refcount = 1;
4306 target->generation = 0;
4307 timevalclear(&target->last_reset);
4309 * Hold a reference to our parent bus so it
4310 * will not go away before we do.
4314 /* Insertion sort into our bus's target list */
4315 cur_target = TAILQ_FIRST(&bus->et_entries);
4316 while (cur_target != NULL && cur_target->target_id < target_id)
4317 cur_target = TAILQ_NEXT(cur_target, links);
4319 if (cur_target != NULL) {
4320 TAILQ_INSERT_BEFORE(cur_target, target, links);
4322 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4330 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4333 if ((--target->refcount == 0)
4334 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4335 TAILQ_REMOVE(&bus->et_entries, target, links);
4337 free(target, M_CAMXPT);
4338 xpt_release_bus(bus);
4342 static struct cam_ed *
4343 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4346 struct cam_ed *device, *cur_device;
4348 device = xpt_alloc_device(bus, target, lun_id);
4352 device->mintags = 1;
4353 device->maxtags = 1;
4354 bus->sim->max_ccbs = device->ccbq.devq_openings;
4355 cur_device = TAILQ_FIRST(&target->ed_entries);
4356 while (cur_device != NULL && cur_device->lun_id < lun_id)
4357 cur_device = TAILQ_NEXT(cur_device, links);
4358 if (cur_device != NULL) {
4359 TAILQ_INSERT_BEFORE(cur_device, device, links);
4361 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4363 target->generation++;
4369 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4371 struct cam_ed *device;
4372 struct cam_devq *devq;
4375 /* Make space for us in the device queue on our bus */
4376 devq = bus->sim->devq;
4377 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4379 if (status != CAM_REQ_CMP) {
4382 device = (struct cam_ed *)malloc(sizeof(*device),
4383 M_CAMXPT, M_NOWAIT);
4386 if (device != NULL) {
4387 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4388 device->alloc_ccb_entry.device = device;
4389 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4390 device->send_ccb_entry.device = device;
4391 device->target = target;
4392 device->lun_id = lun_id;
4393 device->sim = bus->sim;
4394 /* Initialize our queues */
4395 if (camq_init(&device->drvq, 0) != 0) {
4396 free(device, M_CAMXPT);
4399 if (cam_ccbq_init(&device->ccbq,
4400 bus->sim->max_dev_openings) != 0) {
4401 camq_fini(&device->drvq);
4402 free(device, M_CAMXPT);
4405 SLIST_INIT(&device->asyncs);
4406 SLIST_INIT(&device->periphs);
4407 device->generation = 0;
4408 device->owner = NULL;
4409 device->qfrozen_cnt = 0;
4410 device->flags = CAM_DEV_UNCONFIGURED;
4411 device->tag_delay_count = 0;
4412 device->tag_saved_openings = 0;
4413 device->refcount = 1;
4414 if (bus->sim->flags & CAM_SIM_MPSAFE)
4415 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4417 callout_init_mtx(&device->callout, &Giant, 0);
4420 * Hold a reference to our parent target so it
4421 * will not go away before we do.
4430 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4431 struct cam_ed *device)
4434 if ((--device->refcount == 0)
4435 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4436 struct cam_devq *devq;
4438 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4439 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4440 panic("Removing device while still queued for ccbs");
4442 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4443 callout_stop(&device->callout);
4445 TAILQ_REMOVE(&target->ed_entries, device,links);
4446 target->generation++;
4447 bus->sim->max_ccbs -= device->ccbq.devq_openings;
4448 /* Release our slot in the devq */
4449 devq = bus->sim->devq;
4450 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4451 camq_fini(&device->drvq);
4452 camq_fini(&device->ccbq.queue);
4453 free(device, M_CAMXPT);
4454 xpt_release_target(bus, target);
4459 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4467 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4468 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4469 if (result == CAM_REQ_CMP && (diff < 0)) {
4470 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4472 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4473 || (dev->inq_flags & SID_CmdQue) != 0)
4474 dev->tag_saved_openings = newopenings;
4475 /* Adjust the global limit */
4476 dev->sim->max_ccbs += diff;
4480 static struct cam_eb *
4481 xpt_find_bus(path_id_t path_id)
4485 mtx_lock(&xsoftc.xpt_topo_lock);
4486 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4488 bus = TAILQ_NEXT(bus, links)) {
4489 if (bus->path_id == path_id) {
4494 mtx_unlock(&xsoftc.xpt_topo_lock);
4498 static struct cam_et *
4499 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4501 struct cam_et *target;
4503 for (target = TAILQ_FIRST(&bus->et_entries);
4505 target = TAILQ_NEXT(target, links)) {
4506 if (target->target_id == target_id) {
4514 static struct cam_ed *
4515 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4517 struct cam_ed *device;
4519 for (device = TAILQ_FIRST(&target->ed_entries);
4521 device = TAILQ_NEXT(device, links)) {
4522 if (device->lun_id == lun_id) {
4531 xpt_start_tags(struct cam_path *path)
4533 struct ccb_relsim crs;
4534 struct cam_ed *device;
4535 struct cam_sim *sim;
4538 device = path->device;
4539 sim = path->bus->sim;
4540 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4541 xpt_freeze_devq(path, /*count*/1);
4542 device->inq_flags |= SID_CmdQue;
4543 if (device->tag_saved_openings != 0)
4544 newopenings = device->tag_saved_openings;
4546 newopenings = min(device->maxtags,
4547 sim->max_tagged_dev_openings);
4548 xpt_dev_ccbq_resize(path, newopenings);
4549 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
4550 crs.ccb_h.func_code = XPT_REL_SIMQ;
4551 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4553 = crs.release_timeout
4556 xpt_action((union ccb *)&crs);
4559 static int busses_to_config;
4560 static int busses_to_reset;
4563 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
4566 mtx_assert(bus->sim->mtx, MA_OWNED);
4568 if (bus->path_id != CAM_XPT_PATH_ID) {
4569 struct cam_path path;
4570 struct ccb_pathinq cpi;
4574 xpt_compile_path(&path, NULL, bus->path_id,
4575 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4576 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4577 cpi.ccb_h.func_code = XPT_PATH_INQ;
4578 xpt_action((union ccb *)&cpi);
4579 can_negotiate = cpi.hba_inquiry;
4580 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
4581 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
4584 xpt_release_path(&path);
4591 xptconfigfunc(struct cam_eb *bus, void *arg)
4593 struct cam_path *path;
4594 union ccb *work_ccb;
4596 mtx_assert(bus->sim->mtx, MA_OWNED);
4598 if (bus->path_id != CAM_XPT_PATH_ID) {
4602 work_ccb = xpt_alloc_ccb_nowait();
4603 if (work_ccb == NULL) {
4605 xpt_finishconfig(xpt_periph, NULL);
4608 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
4609 CAM_TARGET_WILDCARD,
4610 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
4611 printf("xptconfigfunc: xpt_create_path failed with "
4612 "status %#x for bus %d\n", status, bus->path_id);
4613 printf("xptconfigfunc: halting bus configuration\n");
4614 xpt_free_ccb(work_ccb);
4616 xpt_finishconfig(xpt_periph, NULL);
4619 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
4620 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4621 xpt_action(work_ccb);
4622 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4623 printf("xptconfigfunc: CPI failed on bus %d "
4624 "with status %d\n", bus->path_id,
4625 work_ccb->ccb_h.status);
4626 xpt_finishconfig(xpt_periph, work_ccb);
4630 can_negotiate = work_ccb->cpi.hba_inquiry;
4631 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
4632 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
4633 && (can_negotiate != 0)) {
4634 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
4635 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
4636 work_ccb->ccb_h.cbfcnp = NULL;
4637 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
4638 ("Resetting Bus\n"));
4639 xpt_action(work_ccb);
4640 xpt_finishconfig(xpt_periph, work_ccb);
4642 /* Act as though we performed a successful BUS RESET */
4643 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
4644 xpt_finishconfig(xpt_periph, work_ccb);
4652 xpt_config(void *arg)
4655 * Now that interrupts are enabled, go find our devices
4659 /* Setup debugging flags and path */
4660 #ifdef CAM_DEBUG_FLAGS
4661 cam_dflags = CAM_DEBUG_FLAGS;
4662 #else /* !CAM_DEBUG_FLAGS */
4663 cam_dflags = CAM_DEBUG_NONE;
4664 #endif /* CAM_DEBUG_FLAGS */
4665 #ifdef CAM_DEBUG_BUS
4666 if (cam_dflags != CAM_DEBUG_NONE) {
4668 * Locking is specifically omitted here. No SIMs have
4669 * registered yet, so xpt_create_path will only be searching
4670 * empty lists of targets and devices.
4672 if (xpt_create_path(&cam_dpath, xpt_periph,
4673 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4674 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4675 printf("xpt_config: xpt_create_path() failed for debug"
4676 " target %d:%d:%d, debugging disabled\n",
4677 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4678 cam_dflags = CAM_DEBUG_NONE;
4682 #else /* !CAM_DEBUG_BUS */
4684 #endif /* CAM_DEBUG_BUS */
4685 #endif /* CAMDEBUG */
4688 * Scan all installed busses.
4690 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
4692 if (busses_to_config == 0) {
4693 /* Call manually because we don't have any busses */
4694 xpt_finishconfig(xpt_periph, NULL);
4696 if (busses_to_reset > 0 && scsi_delay >= 2000) {
4697 printf("Waiting %d seconds for SCSI "
4698 "devices to settle\n", scsi_delay/1000);
4700 xpt_for_all_busses(xptconfigfunc, NULL);
4705 * If the given device only has one peripheral attached to it, and if that
4706 * peripheral is the passthrough driver, announce it. This insures that the
4707 * user sees some sort of announcement for every peripheral in their system.
4710 xptpassannouncefunc(struct cam_ed *device, void *arg)
4712 struct cam_periph *periph;
4715 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4716 periph = SLIST_NEXT(periph, periph_links), i++);
4718 periph = SLIST_FIRST(&device->periphs);
4720 && (strncmp(periph->periph_name, "pass", 4) == 0))
4721 xpt_announce_periph(periph, NULL);
4727 xpt_finishconfig_task(void *context, int pending)
4729 struct periph_driver **p_drv;
4732 if (busses_to_config == 0) {
4733 /* Register all the peripheral drivers */
4734 /* XXX This will have to change when we have loadable modules */
4735 p_drv = periph_drivers;
4736 for (i = 0; p_drv[i] != NULL; i++) {
4737 (*p_drv[i]->init)();
4741 * Check for devices with no "standard" peripheral driver
4742 * attached. For any devices like that, announce the
4743 * passthrough driver so the user will see something.
4745 xpt_for_all_devices(xptpassannouncefunc, NULL);
4747 /* Release our hook so that the boot can continue. */
4748 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4749 free(xsoftc.xpt_config_hook, M_CAMXPT);
4750 xsoftc.xpt_config_hook = NULL;
4753 free(context, M_CAMXPT);
4757 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
4759 struct xpt_task *task;
4761 if (done_ccb != NULL) {
4762 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4763 ("xpt_finishconfig\n"));
4764 switch(done_ccb->ccb_h.func_code) {
4766 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
4767 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4768 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
4769 done_ccb->crcn.flags = 0;
4770 xpt_action(done_ccb);
4776 xpt_free_path(done_ccb->ccb_h.path);
4782 if (busses_to_config == 0) {
4783 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4785 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4786 taskqueue_enqueue(taskqueue_thread, &task->task);
4790 if (done_ccb != NULL)
4791 xpt_free_ccb(done_ccb);
4795 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4796 struct cam_path *path)
4798 struct ccb_setasync csa;
4803 mtx_lock(&xsoftc.xpt_lock);
4804 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4805 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4806 if (status != CAM_REQ_CMP) {
4807 mtx_unlock(&xsoftc.xpt_lock);
4813 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
4814 csa.ccb_h.func_code = XPT_SASYNC_CB;
4815 csa.event_enable = event;
4816 csa.callback = cbfunc;
4817 csa.callback_arg = cbarg;
4818 xpt_action((union ccb *)&csa);
4819 status = csa.ccb_h.status;
4821 xpt_free_path(path);
4822 mtx_unlock(&xsoftc.xpt_lock);
4828 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4830 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4832 switch (work_ccb->ccb_h.func_code) {
4833 /* Common cases first */
4834 case XPT_PATH_INQ: /* Path routing inquiry */
4836 struct ccb_pathinq *cpi;
4838 cpi = &work_ccb->cpi;
4839 cpi->version_num = 1; /* XXX??? */
4840 cpi->hba_inquiry = 0;
4841 cpi->target_sprt = 0;
4843 cpi->hba_eng_cnt = 0;
4844 cpi->max_target = 0;
4846 cpi->initiator_id = 0;
4847 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4848 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4849 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4850 cpi->unit_number = sim->unit_number;
4851 cpi->bus_id = sim->bus_id;
4852 cpi->base_transfer_speed = 0;
4853 cpi->protocol = PROTO_UNSPECIFIED;
4854 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4855 cpi->transport = XPORT_UNSPECIFIED;
4856 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4857 cpi->ccb_h.status = CAM_REQ_CMP;
4862 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4869 * The xpt as a "controller" has no interrupt sources, so polling
4873 xptpoll(struct cam_sim *sim)
4878 xpt_lock_buses(void)
4880 mtx_lock(&xsoftc.xpt_topo_lock);
4884 xpt_unlock_buses(void)
4886 mtx_unlock(&xsoftc.xpt_topo_lock);
4893 struct cam_sim *sim;
4895 mtx_lock(&cam_simq_lock);
4897 TAILQ_CONCAT(&queue, &cam_simq, links);
4898 mtx_unlock(&cam_simq_lock);
4900 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4901 TAILQ_REMOVE(&queue, sim, links);
4903 sim->flags &= ~CAM_SIM_ON_DONEQ;
4904 camisr_runqueue(&sim->sim_doneq);
4905 CAM_SIM_UNLOCK(sim);
4910 camisr_runqueue(void *V_queue)
4912 cam_isrq_t *queue = V_queue;
4913 struct ccb_hdr *ccb_h;
4915 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4918 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4919 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4921 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4926 if (ccb_h->flags & CAM_HIGH_POWER) {
4927 struct highpowerlist *hphead;
4928 union ccb *send_ccb;
4930 mtx_lock(&xsoftc.xpt_lock);
4931 hphead = &xsoftc.highpowerq;
4933 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4936 * Increment the count since this command is done.
4938 xsoftc.num_highpower++;
4941 * Any high powered commands queued up?
4943 if (send_ccb != NULL) {
4945 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4946 mtx_unlock(&xsoftc.xpt_lock);
4948 xpt_release_devq(send_ccb->ccb_h.path,
4949 /*count*/1, /*runqueue*/TRUE);
4951 mtx_unlock(&xsoftc.xpt_lock);
4954 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4957 dev = ccb_h->path->device;
4959 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
4960 ccb_h->path->bus->sim->devq->send_active--;
4961 ccb_h->path->bus->sim->devq->send_openings++;
4963 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4964 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
4965 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
4966 && (dev->ccbq.dev_active == 0))) {
4968 xpt_release_devq(ccb_h->path, /*count*/1,
4972 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4973 && (--dev->tag_delay_count == 0))
4974 xpt_start_tags(ccb_h->path);
4976 if ((dev->ccbq.queue.entries > 0)
4977 && (dev->qfrozen_cnt == 0)
4978 && (device_is_send_queued(dev) == 0)) {
4979 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
4984 if (ccb_h->status & CAM_RELEASE_SIMQ) {
4985 xpt_release_simq(ccb_h->path->bus->sim,
4987 ccb_h->status &= ~CAM_RELEASE_SIMQ;
4991 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
4992 && (ccb_h->status & CAM_DEV_QFRZN)) {
4993 xpt_release_devq(ccb_h->path, /*count*/1,
4995 ccb_h->status &= ~CAM_DEV_QFRZN;
4997 xpt_run_dev_sendq(ccb_h->path->bus);
5000 /* Call the peripheral driver's callback */
5001 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);