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, CAM_PRIORITY_NORMAL);
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 scbus%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,
1047 path->target->target_id,
1048 path->device->lun_id);
1049 printf("%s%d: ", periph->periph_name, periph->unit_number);
1050 if (path->device->protocol == PROTO_SCSI)
1051 scsi_print_inquiry(&path->device->inq_data);
1052 else if (path->device->protocol == PROTO_ATA ||
1053 path->device->protocol == PROTO_SATAPM)
1054 ata_print_ident(&path->device->ident_data);
1056 printf("Unknown protocol device\n");
1057 if (bootverbose && path->device->serial_num_len > 0) {
1058 /* Don't wrap the screen - print only the first 60 chars */
1059 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1060 periph->unit_number, path->device->serial_num);
1062 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
1063 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1064 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1065 xpt_action((union ccb*)&cts);
1066 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1070 /* Ask the SIM for its base transfer speed */
1071 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
1072 cpi.ccb_h.func_code = XPT_PATH_INQ;
1073 xpt_action((union ccb *)&cpi);
1075 speed = cpi.base_transfer_speed;
1077 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1078 struct ccb_trans_settings_spi *spi;
1080 spi = &cts.xport_specific.spi;
1081 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1082 && spi->sync_offset != 0) {
1083 freq = scsi_calc_syncsrate(spi->sync_period);
1087 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1088 speed *= (0x01 << spi->bus_width);
1090 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1091 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1092 if (fc->valid & CTS_FC_VALID_SPEED)
1093 speed = fc->bitrate;
1095 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SAS) {
1096 struct ccb_trans_settings_sas *sas = &cts.xport_specific.sas;
1097 if (sas->valid & CTS_SAS_VALID_SPEED)
1098 speed = sas->bitrate;
1100 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SATA) {
1101 struct ccb_trans_settings_sata *sata = &cts.xport_specific.sata;
1102 if (sata->valid & CTS_SATA_VALID_SPEED)
1103 speed = sata->bitrate;
1108 printf("%s%d: %d.%03dMB/s transfers",
1109 periph->periph_name, periph->unit_number,
1112 printf("%s%d: %dKB/s transfers", periph->periph_name,
1113 periph->unit_number, speed);
1114 /* Report additional information about SPI connections */
1115 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1116 struct ccb_trans_settings_spi *spi;
1118 spi = &cts.xport_specific.spi;
1120 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1122 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1126 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1127 && spi->bus_width > 0) {
1133 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1134 } else if (freq != 0) {
1138 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1139 struct ccb_trans_settings_fc *fc;
1141 fc = &cts.xport_specific.fc;
1142 if (fc->valid & CTS_FC_VALID_WWNN)
1143 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1144 if (fc->valid & CTS_FC_VALID_WWPN)
1145 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1146 if (fc->valid & CTS_FC_VALID_PORT)
1147 printf(" PortID 0x%x", fc->port);
1150 if (path->device->inq_flags & SID_CmdQue
1151 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1152 printf("\n%s%d: Command Queueing enabled",
1153 periph->periph_name, periph->unit_number);
1158 * We only want to print the caller's announce string if they've
1161 if (announce_string != NULL)
1162 printf("%s%d: %s\n", periph->periph_name,
1163 periph->unit_number, announce_string);
1166 static dev_match_ret
1167 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1170 dev_match_ret retval;
1173 retval = DM_RET_NONE;
1176 * If we aren't given something to match against, that's an error.
1179 return(DM_RET_ERROR);
1182 * If there are no match entries, then this bus matches no
1185 if ((patterns == NULL) || (num_patterns == 0))
1186 return(DM_RET_DESCEND | DM_RET_COPY);
1188 for (i = 0; i < num_patterns; i++) {
1189 struct bus_match_pattern *cur_pattern;
1192 * If the pattern in question isn't for a bus node, we
1193 * aren't interested. However, we do indicate to the
1194 * calling routine that we should continue descending the
1195 * tree, since the user wants to match against lower-level
1198 if (patterns[i].type != DEV_MATCH_BUS) {
1199 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1200 retval |= DM_RET_DESCEND;
1204 cur_pattern = &patterns[i].pattern.bus_pattern;
1207 * If they want to match any bus node, we give them any
1210 if (cur_pattern->flags == BUS_MATCH_ANY) {
1211 /* set the copy flag */
1212 retval |= DM_RET_COPY;
1215 * If we've already decided on an action, go ahead
1218 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1223 * Not sure why someone would do this...
1225 if (cur_pattern->flags == BUS_MATCH_NONE)
1228 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1229 && (cur_pattern->path_id != bus->path_id))
1232 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1233 && (cur_pattern->bus_id != bus->sim->bus_id))
1236 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1237 && (cur_pattern->unit_number != bus->sim->unit_number))
1240 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1241 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1246 * If we get to this point, the user definitely wants
1247 * information on this bus. So tell the caller to copy the
1250 retval |= DM_RET_COPY;
1253 * If the return action has been set to descend, then we
1254 * know that we've already seen a non-bus matching
1255 * expression, therefore we need to further descend the tree.
1256 * This won't change by continuing around the loop, so we
1257 * go ahead and return. If we haven't seen a non-bus
1258 * matching expression, we keep going around the loop until
1259 * we exhaust the matching expressions. We'll set the stop
1260 * flag once we fall out of the loop.
1262 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1267 * If the return action hasn't been set to descend yet, that means
1268 * we haven't seen anything other than bus matching patterns. So
1269 * tell the caller to stop descending the tree -- the user doesn't
1270 * want to match against lower level tree elements.
1272 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1273 retval |= DM_RET_STOP;
1278 static dev_match_ret
1279 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1280 struct cam_ed *device)
1282 dev_match_ret retval;
1285 retval = DM_RET_NONE;
1288 * If we aren't given something to match against, that's an error.
1291 return(DM_RET_ERROR);
1294 * If there are no match entries, then this device matches no
1297 if ((patterns == NULL) || (num_patterns == 0))
1298 return(DM_RET_DESCEND | DM_RET_COPY);
1300 for (i = 0; i < num_patterns; i++) {
1301 struct device_match_pattern *cur_pattern;
1304 * If the pattern in question isn't for a device node, we
1305 * aren't interested.
1307 if (patterns[i].type != DEV_MATCH_DEVICE) {
1308 if ((patterns[i].type == DEV_MATCH_PERIPH)
1309 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1310 retval |= DM_RET_DESCEND;
1314 cur_pattern = &patterns[i].pattern.device_pattern;
1317 * If they want to match any device node, we give them any
1320 if (cur_pattern->flags == DEV_MATCH_ANY) {
1321 /* set the copy flag */
1322 retval |= DM_RET_COPY;
1326 * If we've already decided on an action, go ahead
1329 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1334 * Not sure why someone would do this...
1336 if (cur_pattern->flags == DEV_MATCH_NONE)
1339 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1340 && (cur_pattern->path_id != device->target->bus->path_id))
1343 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1344 && (cur_pattern->target_id != device->target->target_id))
1347 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1348 && (cur_pattern->target_lun != device->lun_id))
1351 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1352 && (cam_quirkmatch((caddr_t)&device->inq_data,
1353 (caddr_t)&cur_pattern->inq_pat,
1354 1, sizeof(cur_pattern->inq_pat),
1355 scsi_static_inquiry_match) == NULL))
1359 * If we get to this point, the user definitely wants
1360 * information on this device. So tell the caller to copy
1363 retval |= DM_RET_COPY;
1366 * If the return action has been set to descend, then we
1367 * know that we've already seen a peripheral matching
1368 * expression, therefore we need to further descend the tree.
1369 * This won't change by continuing around the loop, so we
1370 * go ahead and return. If we haven't seen a peripheral
1371 * matching expression, we keep going around the loop until
1372 * we exhaust the matching expressions. We'll set the stop
1373 * flag once we fall out of the loop.
1375 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1380 * If the return action hasn't been set to descend yet, that means
1381 * we haven't seen any peripheral matching patterns. So tell the
1382 * caller to stop descending the tree -- the user doesn't want to
1383 * match against lower level tree elements.
1385 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1386 retval |= DM_RET_STOP;
1392 * Match a single peripheral against any number of match patterns.
1394 static dev_match_ret
1395 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1396 struct cam_periph *periph)
1398 dev_match_ret retval;
1402 * If we aren't given something to match against, that's an error.
1405 return(DM_RET_ERROR);
1408 * If there are no match entries, then this peripheral matches no
1411 if ((patterns == NULL) || (num_patterns == 0))
1412 return(DM_RET_STOP | DM_RET_COPY);
1415 * There aren't any nodes below a peripheral node, so there's no
1416 * reason to descend the tree any further.
1418 retval = DM_RET_STOP;
1420 for (i = 0; i < num_patterns; i++) {
1421 struct periph_match_pattern *cur_pattern;
1424 * If the pattern in question isn't for a peripheral, we
1425 * aren't interested.
1427 if (patterns[i].type != DEV_MATCH_PERIPH)
1430 cur_pattern = &patterns[i].pattern.periph_pattern;
1433 * If they want to match on anything, then we will do so.
1435 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1436 /* set the copy flag */
1437 retval |= DM_RET_COPY;
1440 * We've already set the return action to stop,
1441 * since there are no nodes below peripherals in
1448 * Not sure why someone would do this...
1450 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1453 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1454 && (cur_pattern->path_id != periph->path->bus->path_id))
1458 * For the target and lun id's, we have to make sure the
1459 * target and lun pointers aren't NULL. The xpt peripheral
1460 * has a wildcard target and device.
1462 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1463 && ((periph->path->target == NULL)
1464 ||(cur_pattern->target_id != periph->path->target->target_id)))
1467 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1468 && ((periph->path->device == NULL)
1469 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1472 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1473 && (cur_pattern->unit_number != periph->unit_number))
1476 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1477 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1482 * If we get to this point, the user definitely wants
1483 * information on this peripheral. So tell the caller to
1484 * copy the data out.
1486 retval |= DM_RET_COPY;
1489 * The return action has already been set to stop, since
1490 * peripherals don't have any nodes below them in the EDT.
1496 * If we get to this point, the peripheral that was passed in
1497 * doesn't match any of the patterns.
1503 xptedtbusfunc(struct cam_eb *bus, void *arg)
1505 struct ccb_dev_match *cdm;
1506 dev_match_ret retval;
1508 cdm = (struct ccb_dev_match *)arg;
1511 * If our position is for something deeper in the tree, that means
1512 * that we've already seen this node. So, we keep going down.
1514 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1515 && (cdm->pos.cookie.bus == bus)
1516 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1517 && (cdm->pos.cookie.target != NULL))
1518 retval = DM_RET_DESCEND;
1520 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1523 * If we got an error, bail out of the search.
1525 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1526 cdm->status = CAM_DEV_MATCH_ERROR;
1531 * If the copy flag is set, copy this bus out.
1533 if (retval & DM_RET_COPY) {
1536 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1537 sizeof(struct dev_match_result));
1540 * If we don't have enough space to put in another
1541 * match result, save our position and tell the
1542 * user there are more devices to check.
1544 if (spaceleft < sizeof(struct dev_match_result)) {
1545 bzero(&cdm->pos, sizeof(cdm->pos));
1546 cdm->pos.position_type =
1547 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1549 cdm->pos.cookie.bus = bus;
1550 cdm->pos.generations[CAM_BUS_GENERATION]=
1551 xsoftc.bus_generation;
1552 cdm->status = CAM_DEV_MATCH_MORE;
1555 j = cdm->num_matches;
1557 cdm->matches[j].type = DEV_MATCH_BUS;
1558 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1559 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1560 cdm->matches[j].result.bus_result.unit_number =
1561 bus->sim->unit_number;
1562 strncpy(cdm->matches[j].result.bus_result.dev_name,
1563 bus->sim->sim_name, DEV_IDLEN);
1567 * If the user is only interested in busses, there's no
1568 * reason to descend to the next level in the tree.
1570 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1574 * If there is a target generation recorded, check it to
1575 * make sure the target list hasn't changed.
1577 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1578 && (bus == cdm->pos.cookie.bus)
1579 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1580 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1581 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1583 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1587 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1588 && (cdm->pos.cookie.bus == bus)
1589 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1590 && (cdm->pos.cookie.target != NULL))
1591 return(xpttargettraverse(bus,
1592 (struct cam_et *)cdm->pos.cookie.target,
1593 xptedttargetfunc, arg));
1595 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1599 xptedttargetfunc(struct cam_et *target, void *arg)
1601 struct ccb_dev_match *cdm;
1603 cdm = (struct ccb_dev_match *)arg;
1606 * If there is a device list generation recorded, check it to
1607 * make sure the device list hasn't changed.
1609 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1610 && (cdm->pos.cookie.bus == target->bus)
1611 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1612 && (cdm->pos.cookie.target == target)
1613 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1614 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1615 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1616 target->generation)) {
1617 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1621 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1622 && (cdm->pos.cookie.bus == target->bus)
1623 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1624 && (cdm->pos.cookie.target == target)
1625 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1626 && (cdm->pos.cookie.device != NULL))
1627 return(xptdevicetraverse(target,
1628 (struct cam_ed *)cdm->pos.cookie.device,
1629 xptedtdevicefunc, arg));
1631 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1635 xptedtdevicefunc(struct cam_ed *device, void *arg)
1638 struct ccb_dev_match *cdm;
1639 dev_match_ret retval;
1641 cdm = (struct ccb_dev_match *)arg;
1644 * If our position is for something deeper in the tree, that means
1645 * that we've already seen this node. So, we keep going down.
1647 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1648 && (cdm->pos.cookie.device == device)
1649 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1650 && (cdm->pos.cookie.periph != NULL))
1651 retval = DM_RET_DESCEND;
1653 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1656 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1657 cdm->status = CAM_DEV_MATCH_ERROR;
1662 * If the copy flag is set, copy this device out.
1664 if (retval & DM_RET_COPY) {
1667 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1668 sizeof(struct dev_match_result));
1671 * If we don't have enough space to put in another
1672 * match result, save our position and tell the
1673 * user there are more devices to check.
1675 if (spaceleft < sizeof(struct dev_match_result)) {
1676 bzero(&cdm->pos, sizeof(cdm->pos));
1677 cdm->pos.position_type =
1678 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1679 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1681 cdm->pos.cookie.bus = device->target->bus;
1682 cdm->pos.generations[CAM_BUS_GENERATION]=
1683 xsoftc.bus_generation;
1684 cdm->pos.cookie.target = device->target;
1685 cdm->pos.generations[CAM_TARGET_GENERATION] =
1686 device->target->bus->generation;
1687 cdm->pos.cookie.device = device;
1688 cdm->pos.generations[CAM_DEV_GENERATION] =
1689 device->target->generation;
1690 cdm->status = CAM_DEV_MATCH_MORE;
1693 j = cdm->num_matches;
1695 cdm->matches[j].type = DEV_MATCH_DEVICE;
1696 cdm->matches[j].result.device_result.path_id =
1697 device->target->bus->path_id;
1698 cdm->matches[j].result.device_result.target_id =
1699 device->target->target_id;
1700 cdm->matches[j].result.device_result.target_lun =
1702 cdm->matches[j].result.device_result.protocol =
1704 bcopy(&device->inq_data,
1705 &cdm->matches[j].result.device_result.inq_data,
1706 sizeof(struct scsi_inquiry_data));
1707 bcopy(&device->ident_data,
1708 &cdm->matches[j].result.device_result.ident_data,
1709 sizeof(struct ata_params));
1711 /* Let the user know whether this device is unconfigured */
1712 if (device->flags & CAM_DEV_UNCONFIGURED)
1713 cdm->matches[j].result.device_result.flags =
1714 DEV_RESULT_UNCONFIGURED;
1716 cdm->matches[j].result.device_result.flags =
1721 * If the user isn't interested in peripherals, don't descend
1722 * the tree any further.
1724 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1728 * If there is a peripheral list generation recorded, make sure
1729 * it hasn't changed.
1731 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1732 && (device->target->bus == cdm->pos.cookie.bus)
1733 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1734 && (device->target == cdm->pos.cookie.target)
1735 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1736 && (device == cdm->pos.cookie.device)
1737 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1738 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1739 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1740 device->generation)){
1741 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1745 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1746 && (cdm->pos.cookie.bus == device->target->bus)
1747 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1748 && (cdm->pos.cookie.target == device->target)
1749 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1750 && (cdm->pos.cookie.device == device)
1751 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1752 && (cdm->pos.cookie.periph != NULL))
1753 return(xptperiphtraverse(device,
1754 (struct cam_periph *)cdm->pos.cookie.periph,
1755 xptedtperiphfunc, arg));
1757 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1761 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1763 struct ccb_dev_match *cdm;
1764 dev_match_ret retval;
1766 cdm = (struct ccb_dev_match *)arg;
1768 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1770 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1771 cdm->status = CAM_DEV_MATCH_ERROR;
1776 * If the copy flag is set, copy this peripheral out.
1778 if (retval & DM_RET_COPY) {
1781 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1782 sizeof(struct dev_match_result));
1785 * If we don't have enough space to put in another
1786 * match result, save our position and tell the
1787 * user there are more devices to check.
1789 if (spaceleft < sizeof(struct dev_match_result)) {
1790 bzero(&cdm->pos, sizeof(cdm->pos));
1791 cdm->pos.position_type =
1792 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1793 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1796 cdm->pos.cookie.bus = periph->path->bus;
1797 cdm->pos.generations[CAM_BUS_GENERATION]=
1798 xsoftc.bus_generation;
1799 cdm->pos.cookie.target = periph->path->target;
1800 cdm->pos.generations[CAM_TARGET_GENERATION] =
1801 periph->path->bus->generation;
1802 cdm->pos.cookie.device = periph->path->device;
1803 cdm->pos.generations[CAM_DEV_GENERATION] =
1804 periph->path->target->generation;
1805 cdm->pos.cookie.periph = periph;
1806 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1807 periph->path->device->generation;
1808 cdm->status = CAM_DEV_MATCH_MORE;
1812 j = cdm->num_matches;
1814 cdm->matches[j].type = DEV_MATCH_PERIPH;
1815 cdm->matches[j].result.periph_result.path_id =
1816 periph->path->bus->path_id;
1817 cdm->matches[j].result.periph_result.target_id =
1818 periph->path->target->target_id;
1819 cdm->matches[j].result.periph_result.target_lun =
1820 periph->path->device->lun_id;
1821 cdm->matches[j].result.periph_result.unit_number =
1822 periph->unit_number;
1823 strncpy(cdm->matches[j].result.periph_result.periph_name,
1824 periph->periph_name, DEV_IDLEN);
1831 xptedtmatch(struct ccb_dev_match *cdm)
1835 cdm->num_matches = 0;
1838 * Check the bus list generation. If it has changed, the user
1839 * needs to reset everything and start over.
1841 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1842 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1843 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1844 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1848 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1849 && (cdm->pos.cookie.bus != NULL))
1850 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1851 xptedtbusfunc, cdm);
1853 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1856 * If we get back 0, that means that we had to stop before fully
1857 * traversing the EDT. It also means that one of the subroutines
1858 * has set the status field to the proper value. If we get back 1,
1859 * we've fully traversed the EDT and copied out any matching entries.
1862 cdm->status = CAM_DEV_MATCH_LAST;
1868 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1870 struct ccb_dev_match *cdm;
1872 cdm = (struct ccb_dev_match *)arg;
1874 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1875 && (cdm->pos.cookie.pdrv == pdrv)
1876 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1877 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1878 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1879 (*pdrv)->generation)) {
1880 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1884 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1885 && (cdm->pos.cookie.pdrv == pdrv)
1886 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1887 && (cdm->pos.cookie.periph != NULL))
1888 return(xptpdperiphtraverse(pdrv,
1889 (struct cam_periph *)cdm->pos.cookie.periph,
1890 xptplistperiphfunc, arg));
1892 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1896 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1898 struct ccb_dev_match *cdm;
1899 dev_match_ret retval;
1901 cdm = (struct ccb_dev_match *)arg;
1903 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1905 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1906 cdm->status = CAM_DEV_MATCH_ERROR;
1911 * If the copy flag is set, copy this peripheral out.
1913 if (retval & DM_RET_COPY) {
1916 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1917 sizeof(struct dev_match_result));
1920 * If we don't have enough space to put in another
1921 * match result, save our position and tell the
1922 * user there are more devices to check.
1924 if (spaceleft < sizeof(struct dev_match_result)) {
1925 struct periph_driver **pdrv;
1928 bzero(&cdm->pos, sizeof(cdm->pos));
1929 cdm->pos.position_type =
1930 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1934 * This may look a bit non-sensical, but it is
1935 * actually quite logical. There are very few
1936 * peripheral drivers, and bloating every peripheral
1937 * structure with a pointer back to its parent
1938 * peripheral driver linker set entry would cost
1939 * more in the long run than doing this quick lookup.
1941 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1942 if (strcmp((*pdrv)->driver_name,
1943 periph->periph_name) == 0)
1947 if (*pdrv == NULL) {
1948 cdm->status = CAM_DEV_MATCH_ERROR;
1952 cdm->pos.cookie.pdrv = pdrv;
1954 * The periph generation slot does double duty, as
1955 * does the periph pointer slot. They are used for
1956 * both edt and pdrv lookups and positioning.
1958 cdm->pos.cookie.periph = periph;
1959 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1960 (*pdrv)->generation;
1961 cdm->status = CAM_DEV_MATCH_MORE;
1965 j = cdm->num_matches;
1967 cdm->matches[j].type = DEV_MATCH_PERIPH;
1968 cdm->matches[j].result.periph_result.path_id =
1969 periph->path->bus->path_id;
1972 * The transport layer peripheral doesn't have a target or
1975 if (periph->path->target)
1976 cdm->matches[j].result.periph_result.target_id =
1977 periph->path->target->target_id;
1979 cdm->matches[j].result.periph_result.target_id = -1;
1981 if (periph->path->device)
1982 cdm->matches[j].result.periph_result.target_lun =
1983 periph->path->device->lun_id;
1985 cdm->matches[j].result.periph_result.target_lun = -1;
1987 cdm->matches[j].result.periph_result.unit_number =
1988 periph->unit_number;
1989 strncpy(cdm->matches[j].result.periph_result.periph_name,
1990 periph->periph_name, DEV_IDLEN);
1997 xptperiphlistmatch(struct ccb_dev_match *cdm)
2001 cdm->num_matches = 0;
2004 * At this point in the edt traversal function, we check the bus
2005 * list generation to make sure that no busses have been added or
2006 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2007 * For the peripheral driver list traversal function, however, we
2008 * don't have to worry about new peripheral driver types coming or
2009 * going; they're in a linker set, and therefore can't change
2010 * without a recompile.
2013 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2014 && (cdm->pos.cookie.pdrv != NULL))
2015 ret = xptpdrvtraverse(
2016 (struct periph_driver **)cdm->pos.cookie.pdrv,
2017 xptplistpdrvfunc, cdm);
2019 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2022 * If we get back 0, that means that we had to stop before fully
2023 * traversing the peripheral driver tree. It also means that one of
2024 * the subroutines has set the status field to the proper value. If
2025 * we get back 1, we've fully traversed the EDT and copied out any
2029 cdm->status = CAM_DEV_MATCH_LAST;
2035 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2037 struct cam_eb *bus, *next_bus;
2042 mtx_lock(&xsoftc.xpt_topo_lock);
2043 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2046 next_bus = TAILQ_NEXT(bus, links);
2048 mtx_unlock(&xsoftc.xpt_topo_lock);
2049 CAM_SIM_LOCK(bus->sim);
2050 retval = tr_func(bus, arg);
2051 CAM_SIM_UNLOCK(bus->sim);
2054 mtx_lock(&xsoftc.xpt_topo_lock);
2056 mtx_unlock(&xsoftc.xpt_topo_lock);
2062 xpt_sim_opened(struct cam_sim *sim)
2065 struct cam_et *target;
2066 struct cam_ed *device;
2067 struct cam_periph *periph;
2069 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2070 mtx_assert(sim->mtx, MA_OWNED);
2072 mtx_lock(&xsoftc.xpt_topo_lock);
2073 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2074 if (bus->sim != sim)
2077 TAILQ_FOREACH(target, &bus->et_entries, links) {
2078 TAILQ_FOREACH(device, &target->ed_entries, links) {
2079 SLIST_FOREACH(periph, &device->periphs,
2081 if (periph->refcount > 0) {
2082 mtx_unlock(&xsoftc.xpt_topo_lock);
2090 mtx_unlock(&xsoftc.xpt_topo_lock);
2095 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2096 xpt_targetfunc_t *tr_func, void *arg)
2098 struct cam_et *target, *next_target;
2102 for (target = (start_target ? start_target :
2103 TAILQ_FIRST(&bus->et_entries));
2104 target != NULL; target = next_target) {
2106 next_target = TAILQ_NEXT(target, links);
2108 retval = tr_func(target, arg);
2118 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2119 xpt_devicefunc_t *tr_func, void *arg)
2121 struct cam_ed *device, *next_device;
2125 for (device = (start_device ? start_device :
2126 TAILQ_FIRST(&target->ed_entries));
2128 device = next_device) {
2130 next_device = TAILQ_NEXT(device, links);
2132 retval = tr_func(device, arg);
2142 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2143 xpt_periphfunc_t *tr_func, void *arg)
2145 struct cam_periph *periph, *next_periph;
2150 for (periph = (start_periph ? start_periph :
2151 SLIST_FIRST(&device->periphs));
2153 periph = next_periph) {
2155 next_periph = SLIST_NEXT(periph, periph_links);
2157 retval = tr_func(periph, arg);
2166 xptpdrvtraverse(struct periph_driver **start_pdrv,
2167 xpt_pdrvfunc_t *tr_func, void *arg)
2169 struct periph_driver **pdrv;
2175 * We don't traverse the peripheral driver list like we do the
2176 * other lists, because it is a linker set, and therefore cannot be
2177 * changed during runtime. If the peripheral driver list is ever
2178 * re-done to be something other than a linker set (i.e. it can
2179 * change while the system is running), the list traversal should
2180 * be modified to work like the other traversal functions.
2182 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2183 *pdrv != NULL; pdrv++) {
2184 retval = tr_func(pdrv, arg);
2194 xptpdperiphtraverse(struct periph_driver **pdrv,
2195 struct cam_periph *start_periph,
2196 xpt_periphfunc_t *tr_func, void *arg)
2198 struct cam_periph *periph, *next_periph;
2203 for (periph = (start_periph ? start_periph :
2204 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2205 periph = next_periph) {
2207 next_periph = TAILQ_NEXT(periph, unit_links);
2209 retval = tr_func(periph, arg);
2217 xptdefbusfunc(struct cam_eb *bus, void *arg)
2219 struct xpt_traverse_config *tr_config;
2221 tr_config = (struct xpt_traverse_config *)arg;
2223 if (tr_config->depth == XPT_DEPTH_BUS) {
2224 xpt_busfunc_t *tr_func;
2226 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2228 return(tr_func(bus, tr_config->tr_arg));
2230 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2234 xptdeftargetfunc(struct cam_et *target, void *arg)
2236 struct xpt_traverse_config *tr_config;
2238 tr_config = (struct xpt_traverse_config *)arg;
2240 if (tr_config->depth == XPT_DEPTH_TARGET) {
2241 xpt_targetfunc_t *tr_func;
2243 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2245 return(tr_func(target, tr_config->tr_arg));
2247 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2251 xptdefdevicefunc(struct cam_ed *device, void *arg)
2253 struct xpt_traverse_config *tr_config;
2255 tr_config = (struct xpt_traverse_config *)arg;
2257 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2258 xpt_devicefunc_t *tr_func;
2260 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2262 return(tr_func(device, tr_config->tr_arg));
2264 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2268 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2270 struct xpt_traverse_config *tr_config;
2271 xpt_periphfunc_t *tr_func;
2273 tr_config = (struct xpt_traverse_config *)arg;
2275 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2278 * Unlike the other default functions, we don't check for depth
2279 * here. The peripheral driver level is the last level in the EDT,
2280 * so if we're here, we should execute the function in question.
2282 return(tr_func(periph, tr_config->tr_arg));
2286 * Execute the given function for every bus in the EDT.
2289 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2291 struct xpt_traverse_config tr_config;
2293 tr_config.depth = XPT_DEPTH_BUS;
2294 tr_config.tr_func = tr_func;
2295 tr_config.tr_arg = arg;
2297 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2301 * Execute the given function for every device in the EDT.
2304 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2306 struct xpt_traverse_config tr_config;
2308 tr_config.depth = XPT_DEPTH_DEVICE;
2309 tr_config.tr_func = tr_func;
2310 tr_config.tr_arg = arg;
2312 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2316 xptsetasyncfunc(struct cam_ed *device, void *arg)
2318 struct cam_path path;
2319 struct ccb_getdev cgd;
2320 struct async_node *cur_entry;
2322 cur_entry = (struct async_node *)arg;
2325 * Don't report unconfigured devices (Wildcard devs,
2326 * devices only for target mode, device instances
2327 * that have been invalidated but are waiting for
2328 * their last reference count to be released).
2330 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2333 xpt_compile_path(&path,
2335 device->target->bus->path_id,
2336 device->target->target_id,
2338 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2339 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2340 xpt_action((union ccb *)&cgd);
2341 cur_entry->callback(cur_entry->callback_arg,
2344 xpt_release_path(&path);
2350 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2352 struct cam_path path;
2353 struct ccb_pathinq cpi;
2354 struct async_node *cur_entry;
2356 cur_entry = (struct async_node *)arg;
2358 xpt_compile_path(&path, /*periph*/NULL,
2360 CAM_TARGET_WILDCARD,
2362 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2363 cpi.ccb_h.func_code = XPT_PATH_INQ;
2364 xpt_action((union ccb *)&cpi);
2365 cur_entry->callback(cur_entry->callback_arg,
2368 xpt_release_path(&path);
2374 xpt_action_sasync_cb(void *context, int pending)
2376 struct async_node *cur_entry;
2377 struct xpt_task *task;
2380 task = (struct xpt_task *)context;
2381 cur_entry = (struct async_node *)task->data1;
2382 added = task->data2;
2384 if ((added & AC_FOUND_DEVICE) != 0) {
2386 * Get this peripheral up to date with all
2387 * the currently existing devices.
2389 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
2391 if ((added & AC_PATH_REGISTERED) != 0) {
2393 * Get this peripheral up to date with all
2394 * the currently existing busses.
2396 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
2399 free(task, M_CAMXPT);
2403 xpt_action(union ccb *start_ccb)
2406 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2408 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2409 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2413 xpt_action_default(union ccb *start_ccb)
2416 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2419 switch (start_ccb->ccb_h.func_code) {
2422 struct cam_ed *device;
2424 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2425 struct cam_path *path;
2427 path = start_ccb->ccb_h.path;
2431 * For the sake of compatibility with SCSI-1
2432 * devices that may not understand the identify
2433 * message, we include lun information in the
2434 * second byte of all commands. SCSI-1 specifies
2435 * that luns are a 3 bit value and reserves only 3
2436 * bits for lun information in the CDB. Later
2437 * revisions of the SCSI spec allow for more than 8
2438 * luns, but have deprecated lun information in the
2439 * CDB. So, if the lun won't fit, we must omit.
2441 * Also be aware that during initial probing for devices,
2442 * the inquiry information is unknown but initialized to 0.
2443 * This means that this code will be exercised while probing
2444 * devices with an ANSI revision greater than 2.
2446 device = start_ccb->ccb_h.path->device;
2447 if (device->protocol_version <= SCSI_REV_2
2448 && start_ccb->ccb_h.target_lun < 8
2449 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2451 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2452 start_ccb->ccb_h.target_lun << 5;
2454 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2455 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2456 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2457 &path->device->inq_data),
2458 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2459 cdb_str, sizeof(cdb_str))));
2463 case XPT_CONT_TARGET_IO:
2464 start_ccb->csio.sense_resid = 0;
2465 start_ccb->csio.resid = 0;
2468 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2469 start_ccb->ataio.resid = 0;
2474 struct cam_path *path;
2477 path = start_ccb->ccb_h.path;
2479 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2480 if (path->device->ccbq.queue.qfrozen_cnt == 0)
2481 runq = xpt_schedule_dev_sendq(path->bus, path->device);
2485 xpt_run_dev_sendq(path->bus);
2488 case XPT_CALC_GEOMETRY:
2490 struct cam_sim *sim;
2492 /* Filter out garbage */
2493 if (start_ccb->ccg.block_size == 0
2494 || start_ccb->ccg.volume_size == 0) {
2495 start_ccb->ccg.cylinders = 0;
2496 start_ccb->ccg.heads = 0;
2497 start_ccb->ccg.secs_per_track = 0;
2498 start_ccb->ccb_h.status = CAM_REQ_CMP;
2503 * In a PC-98 system, geometry translation depens on
2504 * the "real" device geometry obtained from mode page 4.
2505 * SCSI geometry translation is performed in the
2506 * initialization routine of the SCSI BIOS and the result
2507 * stored in host memory. If the translation is available
2508 * in host memory, use it. If not, rely on the default
2509 * translation the device driver performs.
2511 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2512 start_ccb->ccb_h.status = CAM_REQ_CMP;
2516 sim = start_ccb->ccb_h.path->bus->sim;
2517 (*(sim->sim_action))(sim, start_ccb);
2522 union ccb* abort_ccb;
2524 abort_ccb = start_ccb->cab.abort_ccb;
2525 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2527 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2528 struct cam_ccbq *ccbq;
2530 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
2531 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2532 abort_ccb->ccb_h.status =
2533 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2534 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2535 xpt_done(abort_ccb);
2536 start_ccb->ccb_h.status = CAM_REQ_CMP;
2539 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2540 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2542 * We've caught this ccb en route to
2543 * the SIM. Flag it for abort and the
2544 * SIM will do so just before starting
2545 * real work on the CCB.
2547 abort_ccb->ccb_h.status =
2548 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2549 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2550 start_ccb->ccb_h.status = CAM_REQ_CMP;
2554 if (XPT_FC_IS_QUEUED(abort_ccb)
2555 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2557 * It's already completed but waiting
2558 * for our SWI to get to it.
2560 start_ccb->ccb_h.status = CAM_UA_ABORT;
2564 * If we weren't able to take care of the abort request
2565 * in the XPT, pass the request down to the SIM for processing.
2569 case XPT_ACCEPT_TARGET_IO:
2571 case XPT_IMMED_NOTIFY:
2572 case XPT_NOTIFY_ACK:
2574 case XPT_IMMEDIATE_NOTIFY:
2575 case XPT_NOTIFY_ACKNOWLEDGE:
2576 case XPT_GET_SIM_KNOB:
2577 case XPT_SET_SIM_KNOB:
2579 struct cam_sim *sim;
2581 sim = start_ccb->ccb_h.path->bus->sim;
2582 (*(sim->sim_action))(sim, start_ccb);
2587 struct cam_sim *sim;
2589 sim = start_ccb->ccb_h.path->bus->sim;
2590 (*(sim->sim_action))(sim, start_ccb);
2593 case XPT_PATH_STATS:
2594 start_ccb->cpis.last_reset =
2595 start_ccb->ccb_h.path->bus->last_reset;
2596 start_ccb->ccb_h.status = CAM_REQ_CMP;
2602 dev = start_ccb->ccb_h.path->device;
2603 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2604 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2606 struct ccb_getdev *cgd;
2610 cgd = &start_ccb->cgd;
2611 bus = cgd->ccb_h.path->bus;
2612 tar = cgd->ccb_h.path->target;
2613 cgd->protocol = dev->protocol;
2614 cgd->inq_data = dev->inq_data;
2615 cgd->ident_data = dev->ident_data;
2616 cgd->ccb_h.status = CAM_REQ_CMP;
2617 cgd->serial_num_len = dev->serial_num_len;
2618 if ((dev->serial_num_len > 0)
2619 && (dev->serial_num != NULL))
2620 bcopy(dev->serial_num, cgd->serial_num,
2621 dev->serial_num_len);
2625 case XPT_GDEV_STATS:
2629 dev = start_ccb->ccb_h.path->device;
2630 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2631 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2633 struct ccb_getdevstats *cgds;
2637 cgds = &start_ccb->cgds;
2638 bus = cgds->ccb_h.path->bus;
2639 tar = cgds->ccb_h.path->target;
2640 cgds->dev_openings = dev->ccbq.dev_openings;
2641 cgds->dev_active = dev->ccbq.dev_active;
2642 cgds->devq_openings = dev->ccbq.devq_openings;
2643 cgds->devq_queued = dev->ccbq.queue.entries;
2644 cgds->held = dev->ccbq.held;
2645 cgds->last_reset = tar->last_reset;
2646 cgds->maxtags = dev->maxtags;
2647 cgds->mintags = dev->mintags;
2648 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2649 cgds->last_reset = bus->last_reset;
2650 cgds->ccb_h.status = CAM_REQ_CMP;
2656 struct cam_periph *nperiph;
2657 struct periph_list *periph_head;
2658 struct ccb_getdevlist *cgdl;
2660 struct cam_ed *device;
2667 * Don't want anyone mucking with our data.
2669 device = start_ccb->ccb_h.path->device;
2670 periph_head = &device->periphs;
2671 cgdl = &start_ccb->cgdl;
2674 * Check and see if the list has changed since the user
2675 * last requested a list member. If so, tell them that the
2676 * list has changed, and therefore they need to start over
2677 * from the beginning.
2679 if ((cgdl->index != 0) &&
2680 (cgdl->generation != device->generation)) {
2681 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2686 * Traverse the list of peripherals and attempt to find
2687 * the requested peripheral.
2689 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2690 (nperiph != NULL) && (i <= cgdl->index);
2691 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2692 if (i == cgdl->index) {
2693 strncpy(cgdl->periph_name,
2694 nperiph->periph_name,
2696 cgdl->unit_number = nperiph->unit_number;
2701 cgdl->status = CAM_GDEVLIST_ERROR;
2705 if (nperiph == NULL)
2706 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2708 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2711 cgdl->generation = device->generation;
2713 cgdl->ccb_h.status = CAM_REQ_CMP;
2718 dev_pos_type position_type;
2719 struct ccb_dev_match *cdm;
2721 cdm = &start_ccb->cdm;
2724 * There are two ways of getting at information in the EDT.
2725 * The first way is via the primary EDT tree. It starts
2726 * with a list of busses, then a list of targets on a bus,
2727 * then devices/luns on a target, and then peripherals on a
2728 * device/lun. The "other" way is by the peripheral driver
2729 * lists. The peripheral driver lists are organized by
2730 * peripheral driver. (obviously) So it makes sense to
2731 * use the peripheral driver list if the user is looking
2732 * for something like "da1", or all "da" devices. If the
2733 * user is looking for something on a particular bus/target
2734 * or lun, it's generally better to go through the EDT tree.
2737 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2738 position_type = cdm->pos.position_type;
2742 position_type = CAM_DEV_POS_NONE;
2744 for (i = 0; i < cdm->num_patterns; i++) {
2745 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2746 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2747 position_type = CAM_DEV_POS_EDT;
2752 if (cdm->num_patterns == 0)
2753 position_type = CAM_DEV_POS_EDT;
2754 else if (position_type == CAM_DEV_POS_NONE)
2755 position_type = CAM_DEV_POS_PDRV;
2758 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2759 case CAM_DEV_POS_EDT:
2762 case CAM_DEV_POS_PDRV:
2763 xptperiphlistmatch(cdm);
2766 cdm->status = CAM_DEV_MATCH_ERROR;
2770 if (cdm->status == CAM_DEV_MATCH_ERROR)
2771 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2773 start_ccb->ccb_h.status = CAM_REQ_CMP;
2779 struct ccb_setasync *csa;
2780 struct async_node *cur_entry;
2781 struct async_list *async_head;
2784 csa = &start_ccb->csa;
2785 added = csa->event_enable;
2786 async_head = &csa->ccb_h.path->device->asyncs;
2789 * If there is already an entry for us, simply
2792 cur_entry = SLIST_FIRST(async_head);
2793 while (cur_entry != NULL) {
2794 if ((cur_entry->callback_arg == csa->callback_arg)
2795 && (cur_entry->callback == csa->callback))
2797 cur_entry = SLIST_NEXT(cur_entry, links);
2800 if (cur_entry != NULL) {
2802 * If the request has no flags set,
2805 added &= ~cur_entry->event_enable;
2806 if (csa->event_enable == 0) {
2807 SLIST_REMOVE(async_head, cur_entry,
2809 csa->ccb_h.path->device->refcount--;
2810 free(cur_entry, M_CAMXPT);
2812 cur_entry->event_enable = csa->event_enable;
2815 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2817 if (cur_entry == NULL) {
2818 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2821 cur_entry->event_enable = csa->event_enable;
2822 cur_entry->callback_arg = csa->callback_arg;
2823 cur_entry->callback = csa->callback;
2824 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2825 csa->ccb_h.path->device->refcount++;
2829 * Need to decouple this operation via a taqskqueue so that
2830 * the locking doesn't become a mess.
2832 if ((added & (AC_FOUND_DEVICE | AC_PATH_REGISTERED)) != 0) {
2833 struct xpt_task *task;
2835 task = malloc(sizeof(struct xpt_task), M_CAMXPT,
2838 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2842 TASK_INIT(&task->task, 0, xpt_action_sasync_cb, task);
2843 task->data1 = cur_entry;
2844 task->data2 = added;
2845 taskqueue_enqueue(taskqueue_thread, &task->task);
2848 start_ccb->ccb_h.status = CAM_REQ_CMP;
2853 struct ccb_relsim *crs;
2856 crs = &start_ccb->crs;
2857 dev = crs->ccb_h.path->device;
2860 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2864 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2866 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2867 /* Don't ever go below one opening */
2868 if (crs->openings > 0) {
2869 xpt_dev_ccbq_resize(crs->ccb_h.path,
2873 xpt_print(crs->ccb_h.path,
2874 "tagged openings now %d\n",
2881 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2883 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2886 * Just extend the old timeout and decrement
2887 * the freeze count so that a single timeout
2888 * is sufficient for releasing the queue.
2890 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2891 callout_stop(&dev->callout);
2894 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2897 callout_reset(&dev->callout,
2898 (crs->release_timeout * hz) / 1000,
2899 xpt_release_devq_timeout, dev);
2901 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2905 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2907 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2909 * Decrement the freeze count so that a single
2910 * completion is still sufficient to unfreeze
2913 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2916 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2917 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2921 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2923 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2924 || (dev->ccbq.dev_active == 0)) {
2926 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2929 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2930 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2934 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2936 xpt_release_devq(crs->ccb_h.path, /*count*/1,
2939 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2940 start_ccb->ccb_h.status = CAM_REQ_CMP;
2945 #ifdef CAM_DEBUG_DELAY
2946 cam_debug_delay = CAM_DEBUG_DELAY;
2948 cam_dflags = start_ccb->cdbg.flags;
2949 if (cam_dpath != NULL) {
2950 xpt_free_path(cam_dpath);
2954 if (cam_dflags != CAM_DEBUG_NONE) {
2955 if (xpt_create_path(&cam_dpath, xpt_periph,
2956 start_ccb->ccb_h.path_id,
2957 start_ccb->ccb_h.target_id,
2958 start_ccb->ccb_h.target_lun) !=
2960 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2961 cam_dflags = CAM_DEBUG_NONE;
2963 start_ccb->ccb_h.status = CAM_REQ_CMP;
2964 xpt_print(cam_dpath, "debugging flags now %x\n",
2969 start_ccb->ccb_h.status = CAM_REQ_CMP;
2971 #else /* !CAMDEBUG */
2972 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2973 #endif /* CAMDEBUG */
2977 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2978 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
2979 start_ccb->ccb_h.status = CAM_REQ_CMP;
2986 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2992 xpt_polled_action(union ccb *start_ccb)
2995 struct cam_sim *sim;
2996 struct cam_devq *devq;
3000 timeout = start_ccb->ccb_h.timeout;
3001 sim = start_ccb->ccb_h.path->bus->sim;
3003 dev = start_ccb->ccb_h.path->device;
3005 mtx_assert(sim->mtx, MA_OWNED);
3008 * Steal an opening so that no other queued requests
3009 * can get it before us while we simulate interrupts.
3011 dev->ccbq.devq_openings--;
3012 dev->ccbq.dev_openings--;
3014 while(((devq != NULL && devq->send_openings <= 0) ||
3015 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3017 (*(sim->sim_poll))(sim);
3018 camisr_runqueue(&sim->sim_doneq);
3021 dev->ccbq.devq_openings++;
3022 dev->ccbq.dev_openings++;
3025 xpt_action(start_ccb);
3026 while(--timeout > 0) {
3027 (*(sim->sim_poll))(sim);
3028 camisr_runqueue(&sim->sim_doneq);
3029 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3036 * XXX Is it worth adding a sim_timeout entry
3037 * point so we can attempt recovery? If
3038 * this is only used for dumps, I don't think
3041 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3044 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3049 * Schedule a peripheral driver to receive a ccb when it's
3050 * target device has space for more transactions.
3053 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3055 struct cam_ed *device;
3058 mtx_assert(perph->sim->mtx, MA_OWNED);
3060 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3061 device = perph->path->device;
3062 if (periph_is_queued(perph)) {
3063 /* Simply reorder based on new priority */
3064 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3065 (" change priority to %d\n", new_priority));
3066 if (new_priority < perph->pinfo.priority) {
3067 camq_change_priority(&device->drvq,
3073 /* New entry on the queue */
3074 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3075 (" added periph to queue\n"));
3076 perph->pinfo.priority = new_priority;
3077 perph->pinfo.generation = ++device->drvq.generation;
3078 camq_insert(&device->drvq, &perph->pinfo);
3079 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3082 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3083 (" calling xpt_run_devq\n"));
3084 xpt_run_dev_allocq(perph->path->bus);
3090 * Schedule a device to run on a given queue.
3091 * If the device was inserted as a new entry on the queue,
3092 * return 1 meaning the device queue should be run. If we
3093 * were already queued, implying someone else has already
3094 * started the queue, return 0 so the caller doesn't attempt
3098 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3099 u_int32_t new_priority)
3102 u_int32_t old_priority;
3104 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3106 old_priority = pinfo->priority;
3109 * Are we already queued?
3111 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3112 /* Simply reorder based on new priority */
3113 if (new_priority < old_priority) {
3114 camq_change_priority(queue, pinfo->index,
3116 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3117 ("changed priority to %d\n",
3122 /* New entry on the queue */
3123 if (new_priority < old_priority)
3124 pinfo->priority = new_priority;
3126 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3127 ("Inserting onto queue\n"));
3128 pinfo->generation = ++queue->generation;
3129 camq_insert(queue, pinfo);
3136 xpt_run_dev_allocq(struct cam_eb *bus)
3138 struct cam_devq *devq;
3140 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3141 devq = bus->sim->devq;
3143 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3144 (" qfrozen_cnt == 0x%x, entries == %d, "
3145 "openings == %d, active == %d\n",
3146 devq->alloc_queue.qfrozen_cnt,
3147 devq->alloc_queue.entries,
3148 devq->alloc_openings,
3149 devq->alloc_active));
3151 devq->alloc_queue.qfrozen_cnt++;
3152 while ((devq->alloc_queue.entries > 0)
3153 && (devq->alloc_openings > 0)
3154 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3155 struct cam_ed_qinfo *qinfo;
3156 struct cam_ed *device;
3157 union ccb *work_ccb;
3158 struct cam_periph *drv;
3161 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3163 device = qinfo->device;
3165 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3166 ("running device %p\n", device));
3168 drvq = &device->drvq;
3171 if (drvq->entries <= 0) {
3172 panic("xpt_run_dev_allocq: "
3173 "Device on queue without any work to do");
3176 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3177 devq->alloc_openings--;
3178 devq->alloc_active++;
3179 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3180 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3181 drv->pinfo.priority);
3182 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3183 ("calling periph start\n"));
3184 drv->periph_start(drv, work_ccb);
3187 * Malloc failure in alloc_ccb
3190 * XXX add us to a list to be run from free_ccb
3191 * if we don't have any ccbs active on this
3192 * device queue otherwise we may never get run
3198 if (drvq->entries > 0) {
3199 /* We have more work. Attempt to reschedule */
3200 xpt_schedule_dev_allocq(bus, device);
3203 devq->alloc_queue.qfrozen_cnt--;
3207 xpt_run_dev_sendq(struct cam_eb *bus)
3209 struct cam_devq *devq;
3211 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3213 devq = bus->sim->devq;
3215 devq->send_queue.qfrozen_cnt++;
3216 while ((devq->send_queue.entries > 0)
3217 && (devq->send_openings > 0)) {
3218 struct cam_ed_qinfo *qinfo;
3219 struct cam_ed *device;
3220 union ccb *work_ccb;
3221 struct cam_sim *sim;
3223 if (devq->send_queue.qfrozen_cnt > 1) {
3227 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3229 device = qinfo->device;
3232 * If the device has been "frozen", don't attempt
3235 if (device->ccbq.queue.qfrozen_cnt > 0) {
3239 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3240 ("running device %p\n", device));
3242 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3243 if (work_ccb == NULL) {
3244 printf("device on run queue with no ccbs???\n");
3248 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3250 mtx_lock(&xsoftc.xpt_lock);
3251 if (xsoftc.num_highpower <= 0) {
3253 * We got a high power command, but we
3254 * don't have any available slots. Freeze
3255 * the device queue until we have a slot
3258 device->ccbq.queue.qfrozen_cnt++;
3259 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3263 mtx_unlock(&xsoftc.xpt_lock);
3267 * Consume a high power slot while
3270 xsoftc.num_highpower--;
3272 mtx_unlock(&xsoftc.xpt_lock);
3274 devq->active_dev = device;
3275 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3277 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3279 devq->send_openings--;
3280 devq->send_active++;
3282 if (device->ccbq.queue.entries > 0)
3283 xpt_schedule_dev_sendq(bus, device);
3285 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3287 * The client wants to freeze the queue
3288 * after this CCB is sent.
3290 device->ccbq.queue.qfrozen_cnt++;
3293 /* In Target mode, the peripheral driver knows best... */
3294 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3295 if ((device->inq_flags & SID_CmdQue) != 0
3296 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3297 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3300 * Clear this in case of a retried CCB that
3301 * failed due to a rejected tag.
3303 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3307 * Device queues can be shared among multiple sim instances
3308 * that reside on different busses. Use the SIM in the queue
3309 * CCB's path, rather than the one in the bus that was passed
3310 * into this function.
3312 sim = work_ccb->ccb_h.path->bus->sim;
3313 (*(sim->sim_action))(sim, work_ccb);
3315 devq->active_dev = NULL;
3317 devq->send_queue.qfrozen_cnt--;
3321 * This function merges stuff from the slave ccb into the master ccb, while
3322 * keeping important fields in the master ccb constant.
3325 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3329 * Pull fields that are valid for peripheral drivers to set
3330 * into the master CCB along with the CCB "payload".
3332 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3333 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3334 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3335 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3336 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3337 sizeof(union ccb) - sizeof(struct ccb_hdr));
3341 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3344 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3345 ccb_h->pinfo.priority = priority;
3347 ccb_h->path_id = path->bus->path_id;
3349 ccb_h->target_id = path->target->target_id;
3351 ccb_h->target_id = CAM_TARGET_WILDCARD;
3353 ccb_h->target_lun = path->device->lun_id;
3354 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3356 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3358 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3362 /* Path manipulation functions */
3364 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3365 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3367 struct cam_path *path;
3370 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3373 status = CAM_RESRC_UNAVAIL;
3376 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3377 if (status != CAM_REQ_CMP) {
3378 free(path, M_CAMXPT);
3381 *new_path_ptr = path;
3386 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3387 struct cam_periph *periph, path_id_t path_id,
3388 target_id_t target_id, lun_id_t lun_id)
3390 struct cam_path *path;
3391 struct cam_eb *bus = NULL;
3393 int need_unlock = 0;
3395 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3397 if (path_id != CAM_BUS_WILDCARD) {
3398 bus = xpt_find_bus(path_id);
3401 CAM_SIM_LOCK(bus->sim);
3404 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3406 CAM_SIM_UNLOCK(bus->sim);
3407 if (status != CAM_REQ_CMP) {
3408 free(path, M_CAMXPT);
3411 *new_path_ptr = path;
3416 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3417 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3420 struct cam_et *target;
3421 struct cam_ed *device;
3424 status = CAM_REQ_CMP; /* Completed without error */
3425 target = NULL; /* Wildcarded */
3426 device = NULL; /* Wildcarded */
3429 * We will potentially modify the EDT, so block interrupts
3430 * that may attempt to create cam paths.
3432 bus = xpt_find_bus(path_id);
3434 status = CAM_PATH_INVALID;
3436 target = xpt_find_target(bus, target_id);
3437 if (target == NULL) {
3439 struct cam_et *new_target;
3441 new_target = xpt_alloc_target(bus, target_id);
3442 if (new_target == NULL) {
3443 status = CAM_RESRC_UNAVAIL;
3445 target = new_target;
3448 if (target != NULL) {
3449 device = xpt_find_device(target, lun_id);
3450 if (device == NULL) {
3452 struct cam_ed *new_device;
3455 (*(bus->xport->alloc_device))(bus,
3458 if (new_device == NULL) {
3459 status = CAM_RESRC_UNAVAIL;
3461 device = new_device;
3468 * Only touch the user's data if we are successful.
3470 if (status == CAM_REQ_CMP) {
3471 new_path->periph = perph;
3472 new_path->bus = bus;
3473 new_path->target = target;
3474 new_path->device = device;
3475 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3478 xpt_release_device(bus, target, device);
3480 xpt_release_target(bus, target);
3482 xpt_release_bus(bus);
3488 xpt_release_path(struct cam_path *path)
3490 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3491 if (path->device != NULL) {
3492 xpt_release_device(path->bus, path->target, path->device);
3493 path->device = NULL;
3495 if (path->target != NULL) {
3496 xpt_release_target(path->bus, path->target);
3497 path->target = NULL;
3499 if (path->bus != NULL) {
3500 xpt_release_bus(path->bus);
3506 xpt_free_path(struct cam_path *path)
3509 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3510 xpt_release_path(path);
3511 free(path, M_CAMXPT);
3516 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3517 * in path1, 2 for match with wildcards in path2.
3520 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3524 if (path1->bus != path2->bus) {
3525 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3527 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3532 if (path1->target != path2->target) {
3533 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3536 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3541 if (path1->device != path2->device) {
3542 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3545 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3554 xpt_print_path(struct cam_path *path)
3558 printf("(nopath): ");
3560 if (path->periph != NULL)
3561 printf("(%s%d:", path->periph->periph_name,
3562 path->periph->unit_number);
3564 printf("(noperiph:");
3566 if (path->bus != NULL)
3567 printf("%s%d:%d:", path->bus->sim->sim_name,
3568 path->bus->sim->unit_number,
3569 path->bus->sim->bus_id);
3573 if (path->target != NULL)
3574 printf("%d:", path->target->target_id);
3578 if (path->device != NULL)
3579 printf("%d): ", path->device->lun_id);
3586 xpt_print(struct cam_path *path, const char *fmt, ...)
3589 xpt_print_path(path);
3596 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3601 if (path != NULL && path->bus != NULL)
3602 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3605 sbuf_new(&sb, str, str_len, 0);
3608 sbuf_printf(&sb, "(nopath): ");
3610 if (path->periph != NULL)
3611 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3612 path->periph->unit_number);
3614 sbuf_printf(&sb, "(noperiph:");
3616 if (path->bus != NULL)
3617 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3618 path->bus->sim->unit_number,
3619 path->bus->sim->bus_id);
3621 sbuf_printf(&sb, "nobus:");
3623 if (path->target != NULL)
3624 sbuf_printf(&sb, "%d:", path->target->target_id);
3626 sbuf_printf(&sb, "X:");
3628 if (path->device != NULL)
3629 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3631 sbuf_printf(&sb, "X): ");
3635 return(sbuf_len(&sb));
3639 xpt_path_path_id(struct cam_path *path)
3641 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3643 return(path->bus->path_id);
3647 xpt_path_target_id(struct cam_path *path)
3649 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3651 if (path->target != NULL)
3652 return (path->target->target_id);
3654 return (CAM_TARGET_WILDCARD);
3658 xpt_path_lun_id(struct cam_path *path)
3660 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3662 if (path->device != NULL)
3663 return (path->device->lun_id);
3665 return (CAM_LUN_WILDCARD);
3669 xpt_path_sim(struct cam_path *path)
3672 return (path->bus->sim);
3676 xpt_path_periph(struct cam_path *path)
3678 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3680 return (path->periph);
3684 * Release a CAM control block for the caller. Remit the cost of the structure
3685 * to the device referenced by the path. If the this device had no 'credits'
3686 * and peripheral drivers have registered async callbacks for this notification
3690 xpt_release_ccb(union ccb *free_ccb)
3692 struct cam_path *path;
3693 struct cam_ed *device;
3695 struct cam_sim *sim;
3697 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3698 path = free_ccb->ccb_h.path;
3699 device = path->device;
3703 mtx_assert(sim->mtx, MA_OWNED);
3705 cam_ccbq_release_opening(&device->ccbq);
3706 if (sim->ccb_count > sim->max_ccbs) {
3707 xpt_free_ccb(free_ccb);
3710 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3713 if (sim->devq == NULL) {
3716 sim->devq->alloc_openings++;
3717 sim->devq->alloc_active--;
3718 /* XXX Turn this into an inline function - xpt_run_device?? */
3719 if ((device_is_alloc_queued(device) == 0)
3720 && (device->drvq.entries > 0)) {
3721 xpt_schedule_dev_allocq(bus, device);
3723 if (dev_allocq_is_runnable(sim->devq))
3724 xpt_run_dev_allocq(bus);
3727 /* Functions accessed by SIM drivers */
3729 static struct xpt_xport xport_default = {
3730 .alloc_device = xpt_alloc_device_default,
3731 .action = xpt_action_default,
3732 .async = xpt_dev_async_default,
3736 * A sim structure, listing the SIM entry points and instance
3737 * identification info is passed to xpt_bus_register to hook the SIM
3738 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3739 * for this new bus and places it in the array of busses and assigns
3740 * it a path_id. The path_id may be influenced by "hard wiring"
3741 * information specified by the user. Once interrupt services are
3742 * available, the bus will be probed.
3745 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3747 struct cam_eb *new_bus;
3748 struct cam_eb *old_bus;
3749 struct ccb_pathinq cpi;
3750 struct cam_path path;
3753 mtx_assert(sim->mtx, MA_OWNED);
3756 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3757 M_CAMXPT, M_NOWAIT);
3758 if (new_bus == NULL) {
3759 /* Couldn't satisfy request */
3760 return (CAM_RESRC_UNAVAIL);
3763 if (strcmp(sim->sim_name, "xpt") != 0) {
3765 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3768 TAILQ_INIT(&new_bus->et_entries);
3769 new_bus->path_id = sim->path_id;
3772 timevalclear(&new_bus->last_reset);
3774 new_bus->refcount = 1; /* Held until a bus_deregister event */
3775 new_bus->generation = 0;
3777 mtx_lock(&xsoftc.xpt_topo_lock);
3778 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3779 while (old_bus != NULL
3780 && old_bus->path_id < new_bus->path_id)
3781 old_bus = TAILQ_NEXT(old_bus, links);
3782 if (old_bus != NULL)
3783 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3785 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3786 xsoftc.bus_generation++;
3787 mtx_unlock(&xsoftc.xpt_topo_lock);
3790 * Set a default transport so that a PATH_INQ can be issued to
3791 * the SIM. This will then allow for probing and attaching of
3792 * a more appropriate transport.
3794 new_bus->xport = &xport_default;
3796 bzero(&path, sizeof(path));
3797 status = xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
3798 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3799 if (status != CAM_REQ_CMP)
3800 printf("xpt_compile_path returned %d\n", status);
3802 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
3803 cpi.ccb_h.func_code = XPT_PATH_INQ;
3804 xpt_action((union ccb *)&cpi);
3806 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3807 switch (cpi.transport) {
3814 new_bus->xport = scsi_get_xport();
3818 new_bus->xport = ata_get_xport();
3821 new_bus->xport = &xport_default;
3826 /* Notify interested parties */
3827 if (sim->path_id != CAM_XPT_PATH_ID) {
3828 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
3830 xpt_release_path(&path);
3831 return (CAM_SUCCESS);
3835 xpt_bus_deregister(path_id_t pathid)
3837 struct cam_path bus_path;
3840 status = xpt_compile_path(&bus_path, NULL, pathid,
3841 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3842 if (status != CAM_REQ_CMP)
3845 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3846 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3848 /* Release the reference count held while registered. */
3849 xpt_release_bus(bus_path.bus);
3850 xpt_release_path(&bus_path);
3852 return (CAM_REQ_CMP);
3856 xptnextfreepathid(void)
3863 mtx_lock(&xsoftc.xpt_topo_lock);
3864 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3866 /* Find an unoccupied pathid */
3867 while (bus != NULL && bus->path_id <= pathid) {
3868 if (bus->path_id == pathid)
3870 bus = TAILQ_NEXT(bus, links);
3872 mtx_unlock(&xsoftc.xpt_topo_lock);
3875 * Ensure that this pathid is not reserved for
3876 * a bus that may be registered in the future.
3878 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3880 /* Start the search over */
3881 mtx_lock(&xsoftc.xpt_topo_lock);
3888 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3895 pathid = CAM_XPT_PATH_ID;
3896 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3898 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3899 if (strcmp(dname, "scbus")) {
3900 /* Avoid a bit of foot shooting. */
3903 if (dunit < 0) /* unwired?! */
3905 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3906 if (sim_bus == val) {
3910 } else if (sim_bus == 0) {
3911 /* Unspecified matches bus 0 */
3915 printf("Ambiguous scbus configuration for %s%d "
3916 "bus %d, cannot wire down. The kernel "
3917 "config entry for scbus%d should "
3918 "specify a controller bus.\n"
3919 "Scbus will be assigned dynamically.\n",
3920 sim_name, sim_unit, sim_bus, dunit);
3925 if (pathid == CAM_XPT_PATH_ID)
3926 pathid = xptnextfreepathid();
3931 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
3934 struct cam_et *target, *next_target;
3935 struct cam_ed *device, *next_device;
3937 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3939 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
3942 * Most async events come from a CAM interrupt context. In
3943 * a few cases, the error recovery code at the peripheral layer,
3944 * which may run from our SWI or a process context, may signal
3945 * deferred events with a call to xpt_async.
3950 if (async_code == AC_BUS_RESET) {
3951 /* Update our notion of when the last reset occurred */
3952 microtime(&bus->last_reset);
3955 for (target = TAILQ_FIRST(&bus->et_entries);
3957 target = next_target) {
3959 next_target = TAILQ_NEXT(target, links);
3961 if (path->target != target
3962 && path->target->target_id != CAM_TARGET_WILDCARD
3963 && target->target_id != CAM_TARGET_WILDCARD)
3966 if (async_code == AC_SENT_BDR) {
3967 /* Update our notion of when the last reset occurred */
3968 microtime(&path->target->last_reset);
3971 for (device = TAILQ_FIRST(&target->ed_entries);
3973 device = next_device) {
3975 next_device = TAILQ_NEXT(device, links);
3977 if (path->device != device
3978 && path->device->lun_id != CAM_LUN_WILDCARD
3979 && device->lun_id != CAM_LUN_WILDCARD)
3982 (*(bus->xport->async))(async_code, bus,
3986 xpt_async_bcast(&device->asyncs, async_code,
3992 * If this wasn't a fully wildcarded async, tell all
3993 * clients that want all async events.
3995 if (bus != xpt_periph->path->bus)
3996 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4001 xpt_async_bcast(struct async_list *async_head,
4002 u_int32_t async_code,
4003 struct cam_path *path, void *async_arg)
4005 struct async_node *cur_entry;
4007 cur_entry = SLIST_FIRST(async_head);
4008 while (cur_entry != NULL) {
4009 struct async_node *next_entry;
4011 * Grab the next list entry before we call the current
4012 * entry's callback. This is because the callback function
4013 * can delete its async callback entry.
4015 next_entry = SLIST_NEXT(cur_entry, links);
4016 if ((cur_entry->event_enable & async_code) != 0)
4017 cur_entry->callback(cur_entry->callback_arg,
4020 cur_entry = next_entry;
4025 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4026 struct cam_et *target, struct cam_ed *device,
4029 printf("xpt_dev_async called\n");
4033 xpt_freeze_devq(struct cam_path *path, u_int count)
4035 struct ccb_hdr *ccbh;
4037 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4039 path->device->ccbq.queue.qfrozen_cnt += count;
4042 * Mark the last CCB in the queue as needing
4043 * to be requeued if the driver hasn't
4044 * changed it's state yet. This fixes a race
4045 * where a ccb is just about to be queued to
4046 * a controller driver when it's interrupt routine
4047 * freezes the queue. To completly close the
4048 * hole, controller drives must check to see
4049 * if a ccb's status is still CAM_REQ_INPROG
4050 * just before they queue
4051 * the CCB. See ahc_action/ahc_freeze_devq for
4054 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4055 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4056 ccbh->status = CAM_REQUEUE_REQ;
4057 return (path->device->ccbq.queue.qfrozen_cnt);
4061 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4063 mtx_assert(sim->mtx, MA_OWNED);
4065 sim->devq->send_queue.qfrozen_cnt += count;
4066 if (sim->devq->active_dev != NULL) {
4067 struct ccb_hdr *ccbh;
4069 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4071 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4072 ccbh->status = CAM_REQUEUE_REQ;
4074 return (sim->devq->send_queue.qfrozen_cnt);
4078 xpt_release_devq_timeout(void *arg)
4080 struct cam_ed *device;
4082 device = (struct cam_ed *)arg;
4084 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4088 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4090 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4092 xpt_release_devq_device(path->device, count, run_queue);
4096 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4101 if (dev->ccbq.queue.qfrozen_cnt > 0) {
4103 count = (count > dev->ccbq.queue.qfrozen_cnt) ?
4104 dev->ccbq.queue.qfrozen_cnt : count;
4105 dev->ccbq.queue.qfrozen_cnt -= count;
4106 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4109 * No longer need to wait for a successful
4110 * command completion.
4112 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4115 * Remove any timeouts that might be scheduled
4116 * to release this queue.
4118 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4119 callout_stop(&dev->callout);
4120 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4124 * Now that we are unfrozen schedule the
4125 * device so any pending transactions are
4128 if ((dev->ccbq.queue.entries > 0)
4129 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4130 && (run_queue != 0)) {
4136 xpt_run_dev_sendq(dev->target->bus);
4140 xpt_release_simq(struct cam_sim *sim, int run_queue)
4144 mtx_assert(sim->mtx, MA_OWNED);
4146 sendq = &(sim->devq->send_queue);
4147 if (sendq->qfrozen_cnt > 0) {
4149 sendq->qfrozen_cnt--;
4150 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;
4165 * Now that we are unfrozen run the send queue.
4167 bus = xpt_find_bus(sim->path_id);
4168 xpt_run_dev_sendq(bus);
4169 xpt_release_bus(bus);
4176 * XXX Appears to be unused.
4179 xpt_release_simq_timeout(void *arg)
4181 struct cam_sim *sim;
4183 sim = (struct cam_sim *)arg;
4184 xpt_release_simq(sim, /* run_queue */ TRUE);
4188 xpt_done(union ccb *done_ccb)
4190 struct cam_sim *sim;
4192 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4193 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4195 * Queue up the request for handling by our SWI handler
4196 * any of the "non-immediate" type of ccbs.
4198 sim = done_ccb->ccb_h.path->bus->sim;
4199 switch (done_ccb->ccb_h.path->periph->type) {
4200 case CAM_PERIPH_BIO:
4201 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4203 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4204 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4205 mtx_lock(&cam_simq_lock);
4206 TAILQ_INSERT_TAIL(&cam_simq, sim,
4208 mtx_unlock(&cam_simq_lock);
4209 sim->flags |= CAM_SIM_ON_DONEQ;
4210 if ((done_ccb->ccb_h.path->periph->flags &
4211 CAM_PERIPH_POLLED) == 0)
4212 swi_sched(cambio_ih, 0);
4216 panic("unknown periph type %d",
4217 done_ccb->ccb_h.path->periph->type);
4227 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4232 xpt_alloc_ccb_nowait()
4236 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4241 xpt_free_ccb(union ccb *free_ccb)
4243 free(free_ccb, M_CAMXPT);
4248 /* Private XPT functions */
4251 * Get a CAM control block for the caller. Charge the structure to the device
4252 * referenced by the path. If the this device has no 'credits' then the
4253 * device already has the maximum number of outstanding operations under way
4254 * and we return NULL. If we don't have sufficient resources to allocate more
4255 * ccbs, we also return NULL.
4258 xpt_get_ccb(struct cam_ed *device)
4261 struct cam_sim *sim;
4264 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4265 new_ccb = xpt_alloc_ccb_nowait();
4266 if (new_ccb == NULL) {
4269 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4270 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4271 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4275 cam_ccbq_take_opening(&device->ccbq);
4276 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4281 xpt_release_bus(struct cam_eb *bus)
4284 if ((--bus->refcount == 0)
4285 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4286 mtx_lock(&xsoftc.xpt_topo_lock);
4287 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4288 xsoftc.bus_generation++;
4289 mtx_unlock(&xsoftc.xpt_topo_lock);
4290 cam_sim_release(bus->sim);
4291 free(bus, M_CAMXPT);
4295 static struct cam_et *
4296 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4298 struct cam_et *target;
4300 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4301 if (target != NULL) {
4302 struct cam_et *cur_target;
4304 TAILQ_INIT(&target->ed_entries);
4306 target->target_id = target_id;
4307 target->refcount = 1;
4308 target->generation = 0;
4309 timevalclear(&target->last_reset);
4311 * Hold a reference to our parent bus so it
4312 * will not go away before we do.
4316 /* Insertion sort into our bus's target list */
4317 cur_target = TAILQ_FIRST(&bus->et_entries);
4318 while (cur_target != NULL && cur_target->target_id < target_id)
4319 cur_target = TAILQ_NEXT(cur_target, links);
4321 if (cur_target != NULL) {
4322 TAILQ_INSERT_BEFORE(cur_target, target, links);
4324 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4332 xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4335 if ((--target->refcount == 0)
4336 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4337 TAILQ_REMOVE(&bus->et_entries, target, links);
4339 free(target, M_CAMXPT);
4340 xpt_release_bus(bus);
4344 static struct cam_ed *
4345 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4348 struct cam_ed *device, *cur_device;
4350 device = xpt_alloc_device(bus, target, lun_id);
4354 device->mintags = 1;
4355 device->maxtags = 1;
4356 bus->sim->max_ccbs = device->ccbq.devq_openings;
4357 cur_device = TAILQ_FIRST(&target->ed_entries);
4358 while (cur_device != NULL && cur_device->lun_id < lun_id)
4359 cur_device = TAILQ_NEXT(cur_device, links);
4360 if (cur_device != NULL) {
4361 TAILQ_INSERT_BEFORE(cur_device, device, links);
4363 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4365 target->generation++;
4371 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4373 struct cam_ed *device;
4374 struct cam_devq *devq;
4377 /* Make space for us in the device queue on our bus */
4378 devq = bus->sim->devq;
4379 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4381 if (status != CAM_REQ_CMP) {
4384 device = (struct cam_ed *)malloc(sizeof(*device),
4385 M_CAMXPT, M_NOWAIT);
4388 if (device != NULL) {
4389 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4390 device->alloc_ccb_entry.device = device;
4391 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4392 device->send_ccb_entry.device = device;
4393 device->target = target;
4394 device->lun_id = lun_id;
4395 device->sim = bus->sim;
4396 /* Initialize our queues */
4397 if (camq_init(&device->drvq, 0) != 0) {
4398 free(device, M_CAMXPT);
4401 if (cam_ccbq_init(&device->ccbq,
4402 bus->sim->max_dev_openings) != 0) {
4403 camq_fini(&device->drvq);
4404 free(device, M_CAMXPT);
4407 SLIST_INIT(&device->asyncs);
4408 SLIST_INIT(&device->periphs);
4409 device->generation = 0;
4410 device->owner = NULL;
4411 device->flags = CAM_DEV_UNCONFIGURED;
4412 device->tag_delay_count = 0;
4413 device->tag_saved_openings = 0;
4414 device->refcount = 1;
4415 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4418 * Hold a reference to our parent target so it
4419 * will not go away before we do.
4428 xpt_release_device(struct cam_eb *bus, struct cam_et *target,
4429 struct cam_ed *device)
4432 if ((--device->refcount == 0)
4433 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
4434 struct cam_devq *devq;
4436 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4437 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4438 panic("Removing device while still queued for ccbs");
4440 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4441 callout_stop(&device->callout);
4443 TAILQ_REMOVE(&target->ed_entries, device,links);
4444 target->generation++;
4445 bus->sim->max_ccbs -= device->ccbq.devq_openings;
4446 /* Release our slot in the devq */
4447 devq = bus->sim->devq;
4448 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4449 camq_fini(&device->drvq);
4450 cam_ccbq_fini(&device->ccbq);
4451 free(device, M_CAMXPT);
4452 xpt_release_target(bus, target);
4457 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4465 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4466 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4467 if (result == CAM_REQ_CMP && (diff < 0)) {
4468 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4470 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4471 || (dev->inq_flags & SID_CmdQue) != 0)
4472 dev->tag_saved_openings = newopenings;
4473 /* Adjust the global limit */
4474 dev->sim->max_ccbs += diff;
4478 static struct cam_eb *
4479 xpt_find_bus(path_id_t path_id)
4483 mtx_lock(&xsoftc.xpt_topo_lock);
4484 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4486 bus = TAILQ_NEXT(bus, links)) {
4487 if (bus->path_id == path_id) {
4492 mtx_unlock(&xsoftc.xpt_topo_lock);
4496 static struct cam_et *
4497 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4499 struct cam_et *target;
4501 for (target = TAILQ_FIRST(&bus->et_entries);
4503 target = TAILQ_NEXT(target, links)) {
4504 if (target->target_id == target_id) {
4512 static struct cam_ed *
4513 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4515 struct cam_ed *device;
4517 for (device = TAILQ_FIRST(&target->ed_entries);
4519 device = TAILQ_NEXT(device, links)) {
4520 if (device->lun_id == lun_id) {
4529 xpt_start_tags(struct cam_path *path)
4531 struct ccb_relsim crs;
4532 struct cam_ed *device;
4533 struct cam_sim *sim;
4536 device = path->device;
4537 sim = path->bus->sim;
4538 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4539 xpt_freeze_devq(path, /*count*/1);
4540 device->inq_flags |= SID_CmdQue;
4541 if (device->tag_saved_openings != 0)
4542 newopenings = device->tag_saved_openings;
4544 newopenings = min(device->maxtags,
4545 sim->max_tagged_dev_openings);
4546 xpt_dev_ccbq_resize(path, newopenings);
4547 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4548 crs.ccb_h.func_code = XPT_REL_SIMQ;
4549 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4551 = crs.release_timeout
4554 xpt_action((union ccb *)&crs);
4557 static int busses_to_config;
4558 static int busses_to_reset;
4561 xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
4564 mtx_assert(bus->sim->mtx, MA_OWNED);
4566 if (bus->path_id != CAM_XPT_PATH_ID) {
4567 struct cam_path path;
4568 struct ccb_pathinq cpi;
4572 xpt_compile_path(&path, NULL, bus->path_id,
4573 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4574 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
4575 cpi.ccb_h.func_code = XPT_PATH_INQ;
4576 xpt_action((union ccb *)&cpi);
4577 can_negotiate = cpi.hba_inquiry;
4578 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
4579 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
4582 xpt_release_path(&path);
4589 xptconfigfunc(struct cam_eb *bus, void *arg)
4591 struct cam_path *path;
4592 union ccb *work_ccb;
4594 mtx_assert(bus->sim->mtx, MA_OWNED);
4596 if (bus->path_id != CAM_XPT_PATH_ID) {
4600 work_ccb = xpt_alloc_ccb_nowait();
4601 if (work_ccb == NULL) {
4603 xpt_finishconfig(xpt_periph, NULL);
4606 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
4607 CAM_TARGET_WILDCARD,
4608 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
4609 printf("xptconfigfunc: xpt_create_path failed with "
4610 "status %#x for scbus%d\n", status, bus->path_id);
4611 printf("xptconfigfunc: halting bus configuration\n");
4612 xpt_free_ccb(work_ccb);
4614 xpt_finishconfig(xpt_periph, NULL);
4617 xpt_setup_ccb(&work_ccb->ccb_h, path, CAM_PRIORITY_NORMAL);
4618 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
4619 xpt_action(work_ccb);
4620 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
4621 printf("xptconfigfunc: CPI failed on scbus%d "
4622 "with status %d\n", bus->path_id,
4623 work_ccb->ccb_h.status);
4624 xpt_finishconfig(xpt_periph, work_ccb);
4628 can_negotiate = work_ccb->cpi.hba_inquiry;
4629 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
4630 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
4631 && (can_negotiate != 0)) {
4632 xpt_setup_ccb(&work_ccb->ccb_h, path, CAM_PRIORITY_NORMAL);
4633 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
4634 work_ccb->ccb_h.cbfcnp = NULL;
4635 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
4636 ("Resetting Bus\n"));
4637 xpt_action(work_ccb);
4638 xpt_finishconfig(xpt_periph, work_ccb);
4640 /* Act as though we performed a successful BUS RESET */
4641 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
4642 xpt_finishconfig(xpt_periph, work_ccb);
4650 xpt_config(void *arg)
4653 * Now that interrupts are enabled, go find our devices
4657 /* Setup debugging flags and path */
4658 #ifdef CAM_DEBUG_FLAGS
4659 cam_dflags = CAM_DEBUG_FLAGS;
4660 #else /* !CAM_DEBUG_FLAGS */
4661 cam_dflags = CAM_DEBUG_NONE;
4662 #endif /* CAM_DEBUG_FLAGS */
4663 #ifdef CAM_DEBUG_BUS
4664 if (cam_dflags != CAM_DEBUG_NONE) {
4666 * Locking is specifically omitted here. No SIMs have
4667 * registered yet, so xpt_create_path will only be searching
4668 * empty lists of targets and devices.
4670 if (xpt_create_path(&cam_dpath, xpt_periph,
4671 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4672 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4673 printf("xpt_config: xpt_create_path() failed for debug"
4674 " target %d:%d:%d, debugging disabled\n",
4675 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4676 cam_dflags = CAM_DEBUG_NONE;
4680 #else /* !CAM_DEBUG_BUS */
4682 #endif /* CAM_DEBUG_BUS */
4683 #endif /* CAMDEBUG */
4686 * Scan all installed busses.
4688 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
4690 if (busses_to_config == 0) {
4691 /* Call manually because we don't have any busses */
4692 xpt_finishconfig(xpt_periph, NULL);
4694 if (busses_to_reset > 0 && scsi_delay >= 2000) {
4695 printf("Waiting %d seconds for SCSI "
4696 "devices to settle\n", scsi_delay/1000);
4698 xpt_for_all_busses(xptconfigfunc, NULL);
4703 * If the given device only has one peripheral attached to it, and if that
4704 * peripheral is the passthrough driver, announce it. This insures that the
4705 * user sees some sort of announcement for every peripheral in their system.
4708 xptpassannouncefunc(struct cam_ed *device, void *arg)
4710 struct cam_periph *periph;
4713 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4714 periph = SLIST_NEXT(periph, periph_links), i++);
4716 periph = SLIST_FIRST(&device->periphs);
4718 && (strncmp(periph->periph_name, "pass", 4) == 0))
4719 xpt_announce_periph(periph, NULL);
4725 xpt_finishconfig_task(void *context, int pending)
4727 struct periph_driver **p_drv;
4730 if (busses_to_config == 0) {
4731 /* Register all the peripheral drivers */
4732 /* XXX This will have to change when we have loadable modules */
4733 p_drv = periph_drivers;
4734 for (i = 0; p_drv[i] != NULL; i++) {
4735 (*p_drv[i]->init)();
4739 * Check for devices with no "standard" peripheral driver
4740 * attached. For any devices like that, announce the
4741 * passthrough driver so the user will see something.
4743 xpt_for_all_devices(xptpassannouncefunc, NULL);
4745 /* Release our hook so that the boot can continue. */
4746 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4747 free(xsoftc.xpt_config_hook, M_CAMXPT);
4748 xsoftc.xpt_config_hook = NULL;
4751 free(context, M_CAMXPT);
4755 xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
4757 struct xpt_task *task;
4759 if (done_ccb != NULL) {
4760 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4761 ("xpt_finishconfig\n"));
4762 switch(done_ccb->ccb_h.func_code) {
4764 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
4765 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4766 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
4767 done_ccb->crcn.flags = 0;
4768 xpt_action(done_ccb);
4774 xpt_free_path(done_ccb->ccb_h.path);
4780 if (busses_to_config == 0) {
4781 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4783 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4784 taskqueue_enqueue(taskqueue_thread, &task->task);
4788 if (done_ccb != NULL)
4789 xpt_free_ccb(done_ccb);
4793 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4794 struct cam_path *path)
4796 struct ccb_setasync csa;
4801 mtx_lock(&xsoftc.xpt_lock);
4802 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4803 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4804 if (status != CAM_REQ_CMP) {
4805 mtx_unlock(&xsoftc.xpt_lock);
4811 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
4812 csa.ccb_h.func_code = XPT_SASYNC_CB;
4813 csa.event_enable = event;
4814 csa.callback = cbfunc;
4815 csa.callback_arg = cbarg;
4816 xpt_action((union ccb *)&csa);
4817 status = csa.ccb_h.status;
4819 xpt_free_path(path);
4820 mtx_unlock(&xsoftc.xpt_lock);
4826 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4828 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4830 switch (work_ccb->ccb_h.func_code) {
4831 /* Common cases first */
4832 case XPT_PATH_INQ: /* Path routing inquiry */
4834 struct ccb_pathinq *cpi;
4836 cpi = &work_ccb->cpi;
4837 cpi->version_num = 1; /* XXX??? */
4838 cpi->hba_inquiry = 0;
4839 cpi->target_sprt = 0;
4841 cpi->hba_eng_cnt = 0;
4842 cpi->max_target = 0;
4844 cpi->initiator_id = 0;
4845 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4846 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4847 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4848 cpi->unit_number = sim->unit_number;
4849 cpi->bus_id = sim->bus_id;
4850 cpi->base_transfer_speed = 0;
4851 cpi->protocol = PROTO_UNSPECIFIED;
4852 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4853 cpi->transport = XPORT_UNSPECIFIED;
4854 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4855 cpi->ccb_h.status = CAM_REQ_CMP;
4860 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4867 * The xpt as a "controller" has no interrupt sources, so polling
4871 xptpoll(struct cam_sim *sim)
4876 xpt_lock_buses(void)
4878 mtx_lock(&xsoftc.xpt_topo_lock);
4882 xpt_unlock_buses(void)
4884 mtx_unlock(&xsoftc.xpt_topo_lock);
4891 struct cam_sim *sim;
4893 mtx_lock(&cam_simq_lock);
4895 while (!TAILQ_EMPTY(&cam_simq)) {
4896 TAILQ_CONCAT(&queue, &cam_simq, links);
4897 mtx_unlock(&cam_simq_lock);
4899 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4900 TAILQ_REMOVE(&queue, sim, links);
4902 sim->flags &= ~CAM_SIM_ON_DONEQ;
4903 camisr_runqueue(&sim->sim_doneq);
4904 CAM_SIM_UNLOCK(sim);
4906 mtx_lock(&cam_simq_lock);
4908 mtx_unlock(&cam_simq_lock);
4912 camisr_runqueue(void *V_queue)
4914 cam_isrq_t *queue = V_queue;
4915 struct ccb_hdr *ccb_h;
4917 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4920 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4921 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4923 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4928 if (ccb_h->flags & CAM_HIGH_POWER) {
4929 struct highpowerlist *hphead;
4930 union ccb *send_ccb;
4932 mtx_lock(&xsoftc.xpt_lock);
4933 hphead = &xsoftc.highpowerq;
4935 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4938 * Increment the count since this command is done.
4940 xsoftc.num_highpower++;
4943 * Any high powered commands queued up?
4945 if (send_ccb != NULL) {
4947 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4948 mtx_unlock(&xsoftc.xpt_lock);
4950 xpt_release_devq(send_ccb->ccb_h.path,
4951 /*count*/1, /*runqueue*/TRUE);
4953 mtx_unlock(&xsoftc.xpt_lock);
4956 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4959 dev = ccb_h->path->device;
4961 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
4962 ccb_h->path->bus->sim->devq->send_active--;
4963 ccb_h->path->bus->sim->devq->send_openings++;
4965 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4966 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
4967 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
4968 && (dev->ccbq.dev_active == 0))) {
4970 xpt_release_devq(ccb_h->path, /*count*/1,
4974 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4975 && (--dev->tag_delay_count == 0))
4976 xpt_start_tags(ccb_h->path);
4978 if ((dev->ccbq.queue.entries > 0)
4979 && (dev->ccbq.queue.qfrozen_cnt == 0)
4980 && (device_is_send_queued(dev) == 0)) {
4981 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
4986 if (ccb_h->status & CAM_RELEASE_SIMQ) {
4987 xpt_release_simq(ccb_h->path->bus->sim,
4989 ccb_h->status &= ~CAM_RELEASE_SIMQ;
4993 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
4994 && (ccb_h->status & CAM_DEV_QFRZN)) {
4995 xpt_release_devq(ccb_h->path, /*count*/1,
4997 ccb_h->status &= ~CAM_DEV_QFRZN;
4999 xpt_run_dev_sendq(ccb_h->path->bus);
5002 /* Call the peripheral driver's callback */
5003 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);