2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_compat.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
69 #include <machine/md_var.h> /* geometry translation */
70 #include <machine/stdarg.h> /* for xpt_print below */
75 * This is the maximum number of high powered commands (e.g. start unit)
76 * that can be outstanding at a particular time.
78 #ifndef CAM_MAX_HIGHPOWER
79 #define CAM_MAX_HIGHPOWER 4
82 /* Datastructures internal to the xpt layer */
83 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
88 /* Object for defering XPT actions to a taskqueue */
96 /* number of high powered commands that can go through right now */
97 struct mtx xpt_highpower_lock;
98 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
101 /* queue for handling async rescan requests. */
102 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
104 int buses_config_done;
106 /* Registered busses */
107 TAILQ_HEAD(,cam_eb) xpt_busses;
108 u_int bus_generation;
110 struct intr_config_hook *xpt_config_hook;
113 struct callout boot_callout;
115 struct mtx xpt_topo_lock;
117 struct taskqueue *xpt_taskq;
122 DM_RET_FLAG_MASK = 0x0f,
125 DM_RET_DESCEND = 0x20,
127 DM_RET_ACTION_MASK = 0xf0
135 } xpt_traverse_depth;
137 struct xpt_traverse_config {
138 xpt_traverse_depth depth;
143 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
144 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
145 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
146 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
147 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
149 /* Transport layer configuration information */
150 static struct xpt_softc xsoftc;
152 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
153 &xsoftc.boot_delay, 0, "Bus registration wait time");
156 struct mtx_padalign cam_doneq_mtx;
157 STAILQ_HEAD(, ccb_hdr) cam_doneq;
161 static struct cam_doneq cam_doneqs[MAXCPU];
162 static int cam_num_doneqs;
163 static struct proc *cam_proc;
165 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
166 &cam_num_doneqs, 0, "Number of completion queues/threads");
168 struct cam_periph *xpt_periph;
170 static periph_init_t xpt_periph_init;
172 static struct periph_driver xpt_driver =
174 xpt_periph_init, "xpt",
175 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
179 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
181 static d_open_t xptopen;
182 static d_close_t xptclose;
183 static d_ioctl_t xptioctl;
184 static d_ioctl_t xptdoioctl;
186 static struct cdevsw xpt_cdevsw = {
187 .d_version = D_VERSION,
195 /* Storage for debugging datastructures */
196 struct cam_path *cam_dpath;
197 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
198 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
199 &cam_dflags, 0, "Enabled debug flags");
200 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
201 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
202 &cam_debug_delay, 0, "Delay in us after each debug message");
204 /* Our boot-time initialization hook */
205 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
207 static moduledata_t cam_moduledata = {
209 cam_module_event_handler,
213 static int xpt_init(void *);
215 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
216 MODULE_VERSION(cam, 1);
219 static void xpt_async_bcast(struct async_list *async_head,
220 u_int32_t async_code,
221 struct cam_path *path,
223 static path_id_t xptnextfreepathid(void);
224 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
225 static union ccb *xpt_get_ccb(struct cam_periph *periph);
226 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
227 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
228 static void xpt_run_allocq_task(void *context, int pending);
229 static void xpt_run_devq(struct cam_devq *devq);
230 static timeout_t xpt_release_devq_timeout;
231 static void xpt_release_simq_timeout(void *arg) __unused;
232 static void xpt_acquire_bus(struct cam_eb *bus);
233 static void xpt_release_bus(struct cam_eb *bus);
234 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
235 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
237 static struct cam_et*
238 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
239 static void xpt_acquire_target(struct cam_et *target);
240 static void xpt_release_target(struct cam_et *target);
241 static struct cam_eb*
242 xpt_find_bus(path_id_t path_id);
243 static struct cam_et*
244 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
245 static struct cam_ed*
246 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
247 static void xpt_config(void *arg);
248 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
249 u_int32_t new_priority);
250 static xpt_devicefunc_t xptpassannouncefunc;
251 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
252 static void xptpoll(struct cam_sim *sim);
253 static void camisr_runqueue(void);
254 static void xpt_done_process(struct ccb_hdr *ccb_h);
255 static void xpt_done_td(void *);
256 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
257 u_int num_patterns, struct cam_eb *bus);
258 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
260 struct cam_ed *device);
261 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
263 struct cam_periph *periph);
264 static xpt_busfunc_t xptedtbusfunc;
265 static xpt_targetfunc_t xptedttargetfunc;
266 static xpt_devicefunc_t xptedtdevicefunc;
267 static xpt_periphfunc_t xptedtperiphfunc;
268 static xpt_pdrvfunc_t xptplistpdrvfunc;
269 static xpt_periphfunc_t xptplistperiphfunc;
270 static int xptedtmatch(struct ccb_dev_match *cdm);
271 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
272 static int xptbustraverse(struct cam_eb *start_bus,
273 xpt_busfunc_t *tr_func, void *arg);
274 static int xpttargettraverse(struct cam_eb *bus,
275 struct cam_et *start_target,
276 xpt_targetfunc_t *tr_func, void *arg);
277 static int xptdevicetraverse(struct cam_et *target,
278 struct cam_ed *start_device,
279 xpt_devicefunc_t *tr_func, void *arg);
280 static int xptperiphtraverse(struct cam_ed *device,
281 struct cam_periph *start_periph,
282 xpt_periphfunc_t *tr_func, void *arg);
283 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
284 xpt_pdrvfunc_t *tr_func, void *arg);
285 static int xptpdperiphtraverse(struct periph_driver **pdrv,
286 struct cam_periph *start_periph,
287 xpt_periphfunc_t *tr_func,
289 static xpt_busfunc_t xptdefbusfunc;
290 static xpt_targetfunc_t xptdeftargetfunc;
291 static xpt_devicefunc_t xptdefdevicefunc;
292 static xpt_periphfunc_t xptdefperiphfunc;
293 static void xpt_finishconfig_task(void *context, int pending);
294 static void xpt_dev_async_default(u_int32_t async_code,
296 struct cam_et *target,
297 struct cam_ed *device,
299 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
300 struct cam_et *target,
302 static xpt_devicefunc_t xptsetasyncfunc;
303 static xpt_busfunc_t xptsetasyncbusfunc;
304 static cam_status xptregister(struct cam_periph *periph,
306 static __inline int device_is_queued(struct cam_ed *device);
309 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
313 mtx_assert(&devq->send_mtx, MA_OWNED);
314 if ((dev->ccbq.queue.entries > 0) &&
315 (dev->ccbq.dev_openings > 0) &&
316 (dev->ccbq.queue.qfrozen_cnt == 0)) {
318 * The priority of a device waiting for controller
319 * resources is that of the highest priority CCB
323 xpt_schedule_dev(&devq->send_queue,
325 CAMQ_GET_PRIO(&dev->ccbq.queue));
333 device_is_queued(struct cam_ed *device)
335 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
341 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
345 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
349 * Only allow read-write access.
351 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
355 * We don't allow nonblocking access.
357 if ((flags & O_NONBLOCK) != 0) {
358 printf("%s: can't do nonblocking access\n", devtoname(dev));
366 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
373 * Don't automatically grab the xpt softc lock here even though this is going
374 * through the xpt device. The xpt device is really just a back door for
375 * accessing other devices and SIMs, so the right thing to do is to grab
376 * the appropriate SIM lock once the bus/SIM is located.
379 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
383 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
384 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
390 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
398 * For the transport layer CAMIOCOMMAND ioctl, we really only want
399 * to accept CCB types that don't quite make sense to send through a
400 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
408 inccb = (union ccb *)addr;
410 bus = xpt_find_bus(inccb->ccb_h.path_id);
414 switch (inccb->ccb_h.func_code) {
417 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
418 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
419 xpt_release_bus(bus);
424 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
425 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
426 xpt_release_bus(bus);
434 switch(inccb->ccb_h.func_code) {
442 ccb = xpt_alloc_ccb();
445 * Create a path using the bus, target, and lun the
448 if (xpt_create_path(&ccb->ccb_h.path, NULL,
449 inccb->ccb_h.path_id,
450 inccb->ccb_h.target_id,
451 inccb->ccb_h.target_lun) !=
457 /* Ensure all of our fields are correct */
458 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
459 inccb->ccb_h.pinfo.priority);
460 xpt_merge_ccb(ccb, inccb);
461 xpt_path_lock(ccb->ccb_h.path);
462 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
463 xpt_path_unlock(ccb->ccb_h.path);
464 bcopy(ccb, inccb, sizeof(union ccb));
465 xpt_free_path(ccb->ccb_h.path);
473 * This is an immediate CCB, so it's okay to
474 * allocate it on the stack.
478 * Create a path using the bus, target, and lun the
481 if (xpt_create_path(&ccb.ccb_h.path, NULL,
482 inccb->ccb_h.path_id,
483 inccb->ccb_h.target_id,
484 inccb->ccb_h.target_lun) !=
489 /* Ensure all of our fields are correct */
490 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
491 inccb->ccb_h.pinfo.priority);
492 xpt_merge_ccb(&ccb, inccb);
494 bcopy(&ccb, inccb, sizeof(union ccb));
495 xpt_free_path(ccb.ccb_h.path);
499 case XPT_DEV_MATCH: {
500 struct cam_periph_map_info mapinfo;
501 struct cam_path *old_path;
504 * We can't deal with physical addresses for this
505 * type of transaction.
507 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
514 * Save this in case the caller had it set to
515 * something in particular.
517 old_path = inccb->ccb_h.path;
520 * We really don't need a path for the matching
521 * code. The path is needed because of the
522 * debugging statements in xpt_action(). They
523 * assume that the CCB has a valid path.
525 inccb->ccb_h.path = xpt_periph->path;
527 bzero(&mapinfo, sizeof(mapinfo));
530 * Map the pattern and match buffers into kernel
531 * virtual address space.
533 error = cam_periph_mapmem(inccb, &mapinfo);
536 inccb->ccb_h.path = old_path;
541 * This is an immediate CCB, we can send it on directly.
546 * Map the buffers back into user space.
548 cam_periph_unmapmem(inccb, &mapinfo);
550 inccb->ccb_h.path = old_path;
559 xpt_release_bus(bus);
563 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
564 * with the periphal driver name and unit name filled in. The other
565 * fields don't really matter as input. The passthrough driver name
566 * ("pass"), and unit number are passed back in the ccb. The current
567 * device generation number, and the index into the device peripheral
568 * driver list, and the status are also passed back. Note that
569 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
570 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
571 * (or rather should be) impossible for the device peripheral driver
572 * list to change since we look at the whole thing in one pass, and
573 * we do it with lock protection.
576 case CAMGETPASSTHRU: {
578 struct cam_periph *periph;
579 struct periph_driver **p_drv;
582 int base_periph_found;
584 ccb = (union ccb *)addr;
585 unit = ccb->cgdl.unit_number;
586 name = ccb->cgdl.periph_name;
587 base_periph_found = 0;
590 * Sanity check -- make sure we don't get a null peripheral
593 if (*ccb->cgdl.periph_name == '\0') {
598 /* Keep the list from changing while we traverse it */
601 /* first find our driver in the list of drivers */
602 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
603 if (strcmp((*p_drv)->driver_name, name) == 0)
606 if (*p_drv == NULL) {
608 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
609 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
610 *ccb->cgdl.periph_name = '\0';
611 ccb->cgdl.unit_number = 0;
617 * Run through every peripheral instance of this driver
618 * and check to see whether it matches the unit passed
619 * in by the user. If it does, get out of the loops and
620 * find the passthrough driver associated with that
623 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
624 periph = TAILQ_NEXT(periph, unit_links)) {
626 if (periph->unit_number == unit)
630 * If we found the peripheral driver that the user passed
631 * in, go through all of the peripheral drivers for that
632 * particular device and look for a passthrough driver.
634 if (periph != NULL) {
635 struct cam_ed *device;
638 base_periph_found = 1;
639 device = periph->path->device;
640 for (i = 0, periph = SLIST_FIRST(&device->periphs);
642 periph = SLIST_NEXT(periph, periph_links), i++) {
644 * Check to see whether we have a
645 * passthrough device or not.
647 if (strcmp(periph->periph_name, "pass") == 0) {
649 * Fill in the getdevlist fields.
651 strcpy(ccb->cgdl.periph_name,
652 periph->periph_name);
653 ccb->cgdl.unit_number =
655 if (SLIST_NEXT(periph, periph_links))
657 CAM_GDEVLIST_MORE_DEVS;
660 CAM_GDEVLIST_LAST_DEVICE;
661 ccb->cgdl.generation =
665 * Fill in some CCB header fields
666 * that the user may want.
669 periph->path->bus->path_id;
670 ccb->ccb_h.target_id =
671 periph->path->target->target_id;
672 ccb->ccb_h.target_lun =
673 periph->path->device->lun_id;
674 ccb->ccb_h.status = CAM_REQ_CMP;
681 * If the periph is null here, one of two things has
682 * happened. The first possibility is that we couldn't
683 * find the unit number of the particular peripheral driver
684 * that the user is asking about. e.g. the user asks for
685 * the passthrough driver for "da11". We find the list of
686 * "da" peripherals all right, but there is no unit 11.
687 * The other possibility is that we went through the list
688 * of peripheral drivers attached to the device structure,
689 * but didn't find one with the name "pass". Either way,
690 * we return ENOENT, since we couldn't find something.
692 if (periph == NULL) {
693 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
694 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
695 *ccb->cgdl.periph_name = '\0';
696 ccb->cgdl.unit_number = 0;
699 * It is unfortunate that this is even necessary,
700 * but there are many, many clueless users out there.
701 * If this is true, the user is looking for the
702 * passthrough driver, but doesn't have one in his
705 if (base_periph_found == 1) {
706 printf("xptioctl: pass driver is not in the "
708 printf("xptioctl: put \"device pass\" in "
709 "your kernel config file\n");
724 cam_module_event_handler(module_t mod, int what, void *arg)
730 if ((error = xpt_init(NULL)) != 0)
743 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
746 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
747 xpt_free_path(done_ccb->ccb_h.path);
748 xpt_free_ccb(done_ccb);
750 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
751 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
756 /* thread to handle bus rescans */
758 xpt_scanner_thread(void *dummy)
761 struct cam_path path;
765 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
766 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
768 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
769 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
773 * Since lock can be dropped inside and path freed
774 * by completion callback even before return here,
775 * take our own path copy for reference.
777 xpt_copy_path(&path, ccb->ccb_h.path);
778 xpt_path_lock(&path);
780 xpt_path_unlock(&path);
781 xpt_release_path(&path);
789 xpt_rescan(union ccb *ccb)
793 /* Prepare request */
794 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
795 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
796 ccb->ccb_h.func_code = XPT_SCAN_BUS;
797 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
798 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
799 ccb->ccb_h.func_code = XPT_SCAN_TGT;
800 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
801 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
802 ccb->ccb_h.func_code = XPT_SCAN_LUN;
804 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
805 xpt_free_path(ccb->ccb_h.path);
809 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
810 ccb->ccb_h.cbfcnp = xpt_rescan_done;
811 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
812 /* Don't make duplicate entries for the same paths. */
814 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
815 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
816 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
817 wakeup(&xsoftc.ccb_scanq);
819 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
820 xpt_free_path(ccb->ccb_h.path);
826 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
827 xsoftc.buses_to_config++;
828 wakeup(&xsoftc.ccb_scanq);
832 /* Functions accessed by the peripheral drivers */
834 xpt_init(void *dummy)
836 struct cam_sim *xpt_sim;
837 struct cam_path *path;
838 struct cam_devq *devq;
842 TAILQ_INIT(&xsoftc.xpt_busses);
843 TAILQ_INIT(&xsoftc.ccb_scanq);
844 STAILQ_INIT(&xsoftc.highpowerq);
845 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
847 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
848 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
849 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
850 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
851 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
853 #ifdef CAM_BOOT_DELAY
855 * Override this value at compile time to assist our users
856 * who don't use loader to boot a kernel.
858 xsoftc.boot_delay = CAM_BOOT_DELAY;
861 * The xpt layer is, itself, the equivelent of a SIM.
862 * Allow 16 ccbs in the ccb pool for it. This should
863 * give decent parallelism when we probe busses and
864 * perform other XPT functions.
866 devq = cam_simq_alloc(16);
867 xpt_sim = cam_sim_alloc(xptaction,
872 /*mtx*/&xsoftc.xpt_lock,
873 /*max_dev_transactions*/0,
874 /*max_tagged_dev_transactions*/0,
879 mtx_lock(&xsoftc.xpt_lock);
880 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
881 mtx_unlock(&xsoftc.xpt_lock);
882 printf("xpt_init: xpt_bus_register failed with status %#x,"
883 " failing attach\n", status);
886 mtx_unlock(&xsoftc.xpt_lock);
889 * Looking at the XPT from the SIM layer, the XPT is
890 * the equivelent of a peripheral driver. Allocate
891 * a peripheral driver entry for us.
893 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
895 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
896 mtx_unlock(&xsoftc.xpt_lock);
897 printf("xpt_init: xpt_create_path failed with status %#x,"
898 " failing attach\n", status);
902 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
903 path, NULL, 0, xpt_sim);
904 xpt_path_unlock(path);
907 if (cam_num_doneqs < 1)
908 cam_num_doneqs = 1 + mp_ncpus / 6;
909 else if (cam_num_doneqs > MAXCPU)
910 cam_num_doneqs = MAXCPU;
911 for (i = 0; i < cam_num_doneqs; i++) {
912 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
914 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
915 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
916 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
922 if (cam_num_doneqs < 1) {
923 printf("xpt_init: Cannot init completion queues "
924 "- failing attach\n");
928 * Register a callback for when interrupts are enabled.
930 xsoftc.xpt_config_hook =
931 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
932 M_CAMXPT, M_NOWAIT | M_ZERO);
933 if (xsoftc.xpt_config_hook == NULL) {
934 printf("xpt_init: Cannot malloc config hook "
935 "- failing attach\n");
938 xsoftc.xpt_config_hook->ich_func = xpt_config;
939 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
940 free (xsoftc.xpt_config_hook, M_CAMXPT);
941 printf("xpt_init: config_intrhook_establish failed "
942 "- failing attach\n");
949 xptregister(struct cam_periph *periph, void *arg)
951 struct cam_sim *xpt_sim;
953 if (periph == NULL) {
954 printf("xptregister: periph was NULL!!\n");
955 return(CAM_REQ_CMP_ERR);
958 xpt_sim = (struct cam_sim *)arg;
959 xpt_sim->softc = periph;
961 periph->softc = NULL;
967 xpt_add_periph(struct cam_periph *periph)
969 struct cam_ed *device;
972 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
973 device = periph->path->device;
974 status = CAM_REQ_CMP;
975 if (device != NULL) {
976 mtx_lock(&device->target->bus->eb_mtx);
977 device->generation++;
978 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
979 mtx_unlock(&device->target->bus->eb_mtx);
986 xpt_remove_periph(struct cam_periph *periph)
988 struct cam_ed *device;
990 device = periph->path->device;
991 if (device != NULL) {
992 mtx_lock(&device->target->bus->eb_mtx);
993 device->generation++;
994 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
995 mtx_unlock(&device->target->bus->eb_mtx);
1001 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1003 struct cam_path *path = periph->path;
1005 cam_periph_assert(periph, MA_OWNED);
1006 periph->flags |= CAM_PERIPH_ANNOUNCED;
1008 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1009 periph->periph_name, periph->unit_number,
1010 path->bus->sim->sim_name,
1011 path->bus->sim->unit_number,
1012 path->bus->sim->bus_id,
1014 path->target->target_id,
1015 (uintmax_t)path->device->lun_id);
1016 printf("%s%d: ", periph->periph_name, periph->unit_number);
1017 if (path->device->protocol == PROTO_SCSI)
1018 scsi_print_inquiry(&path->device->inq_data);
1019 else if (path->device->protocol == PROTO_ATA ||
1020 path->device->protocol == PROTO_SATAPM)
1021 ata_print_ident(&path->device->ident_data);
1022 else if (path->device->protocol == PROTO_SEMB)
1024 (struct sep_identify_data *)&path->device->ident_data);
1026 printf("Unknown protocol device\n");
1027 if (path->device->serial_num_len > 0) {
1028 /* Don't wrap the screen - print only the first 60 chars */
1029 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1030 periph->unit_number, path->device->serial_num);
1032 /* Announce transport details. */
1033 (*(path->bus->xport->announce))(periph);
1034 /* Announce command queueing. */
1035 if (path->device->inq_flags & SID_CmdQue
1036 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1037 printf("%s%d: Command Queueing enabled\n",
1038 periph->periph_name, periph->unit_number);
1040 /* Announce caller's details if they've passed in. */
1041 if (announce_string != NULL)
1042 printf("%s%d: %s\n", periph->periph_name,
1043 periph->unit_number, announce_string);
1047 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1050 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1051 periph->unit_number, quirks, bit_string);
1056 xpt_denounce_periph(struct cam_periph *periph)
1058 struct cam_path *path = periph->path;
1060 cam_periph_assert(periph, MA_OWNED);
1061 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1062 periph->periph_name, periph->unit_number,
1063 path->bus->sim->sim_name,
1064 path->bus->sim->unit_number,
1065 path->bus->sim->bus_id,
1067 path->target->target_id,
1068 (uintmax_t)path->device->lun_id);
1069 printf("%s%d: ", periph->periph_name, periph->unit_number);
1070 if (path->device->protocol == PROTO_SCSI)
1071 scsi_print_inquiry_short(&path->device->inq_data);
1072 else if (path->device->protocol == PROTO_ATA ||
1073 path->device->protocol == PROTO_SATAPM)
1074 ata_print_ident_short(&path->device->ident_data);
1075 else if (path->device->protocol == PROTO_SEMB)
1076 semb_print_ident_short(
1077 (struct sep_identify_data *)&path->device->ident_data);
1079 printf("Unknown protocol device");
1080 if (path->device->serial_num_len > 0)
1081 printf(" s/n %.60s", path->device->serial_num);
1082 printf(" detached\n");
1087 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1090 struct ccb_dev_advinfo cdai;
1091 struct scsi_vpd_id_descriptor *idd;
1093 xpt_path_assert(path, MA_OWNED);
1095 memset(&cdai, 0, sizeof(cdai));
1096 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1097 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1100 if (!strcmp(attr, "GEOM::ident"))
1101 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1102 else if (!strcmp(attr, "GEOM::physpath"))
1103 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1104 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1105 strcmp(attr, "GEOM::lunname") == 0) {
1106 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1107 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1111 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1112 if (cdai.buf == NULL) {
1116 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1117 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1118 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1119 if (cdai.provsiz == 0)
1121 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1122 if (strcmp(attr, "GEOM::lunid") == 0) {
1123 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1124 cdai.provsiz, scsi_devid_is_lun_naa);
1126 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1127 cdai.provsiz, scsi_devid_is_lun_eui64);
1131 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1132 cdai.provsiz, scsi_devid_is_lun_t10);
1134 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1135 cdai.provsiz, scsi_devid_is_lun_name);
1139 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1140 if (idd->length < len) {
1141 for (l = 0; l < idd->length; l++)
1142 buf[l] = idd->identifier[l] ?
1143 idd->identifier[l] : ' ';
1147 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1148 l = strnlen(idd->identifier, idd->length);
1150 bcopy(idd->identifier, buf, l);
1155 if (idd->length * 2 < len) {
1156 for (l = 0; l < idd->length; l++)
1157 sprintf(buf + l * 2, "%02x",
1158 idd->identifier[l]);
1164 if (strlcpy(buf, cdai.buf, len) >= len)
1169 if (cdai.buf != NULL)
1170 free(cdai.buf, M_CAMXPT);
1174 static dev_match_ret
1175 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1178 dev_match_ret retval;
1181 retval = DM_RET_NONE;
1184 * If we aren't given something to match against, that's an error.
1187 return(DM_RET_ERROR);
1190 * If there are no match entries, then this bus matches no
1193 if ((patterns == NULL) || (num_patterns == 0))
1194 return(DM_RET_DESCEND | DM_RET_COPY);
1196 for (i = 0; i < num_patterns; i++) {
1197 struct bus_match_pattern *cur_pattern;
1200 * If the pattern in question isn't for a bus node, we
1201 * aren't interested. However, we do indicate to the
1202 * calling routine that we should continue descending the
1203 * tree, since the user wants to match against lower-level
1206 if (patterns[i].type != DEV_MATCH_BUS) {
1207 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1208 retval |= DM_RET_DESCEND;
1212 cur_pattern = &patterns[i].pattern.bus_pattern;
1215 * If they want to match any bus node, we give them any
1218 if (cur_pattern->flags == BUS_MATCH_ANY) {
1219 /* set the copy flag */
1220 retval |= DM_RET_COPY;
1223 * If we've already decided on an action, go ahead
1226 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1231 * Not sure why someone would do this...
1233 if (cur_pattern->flags == BUS_MATCH_NONE)
1236 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1237 && (cur_pattern->path_id != bus->path_id))
1240 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1241 && (cur_pattern->bus_id != bus->sim->bus_id))
1244 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1245 && (cur_pattern->unit_number != bus->sim->unit_number))
1248 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1249 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1254 * If we get to this point, the user definitely wants
1255 * information on this bus. So tell the caller to copy the
1258 retval |= DM_RET_COPY;
1261 * If the return action has been set to descend, then we
1262 * know that we've already seen a non-bus matching
1263 * expression, therefore we need to further descend the tree.
1264 * This won't change by continuing around the loop, so we
1265 * go ahead and return. If we haven't seen a non-bus
1266 * matching expression, we keep going around the loop until
1267 * we exhaust the matching expressions. We'll set the stop
1268 * flag once we fall out of the loop.
1270 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1275 * If the return action hasn't been set to descend yet, that means
1276 * we haven't seen anything other than bus matching patterns. So
1277 * tell the caller to stop descending the tree -- the user doesn't
1278 * want to match against lower level tree elements.
1280 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1281 retval |= DM_RET_STOP;
1286 static dev_match_ret
1287 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1288 struct cam_ed *device)
1290 dev_match_ret retval;
1293 retval = DM_RET_NONE;
1296 * If we aren't given something to match against, that's an error.
1299 return(DM_RET_ERROR);
1302 * If there are no match entries, then this device matches no
1305 if ((patterns == NULL) || (num_patterns == 0))
1306 return(DM_RET_DESCEND | DM_RET_COPY);
1308 for (i = 0; i < num_patterns; i++) {
1309 struct device_match_pattern *cur_pattern;
1310 struct scsi_vpd_device_id *device_id_page;
1313 * If the pattern in question isn't for a device node, we
1314 * aren't interested.
1316 if (patterns[i].type != DEV_MATCH_DEVICE) {
1317 if ((patterns[i].type == DEV_MATCH_PERIPH)
1318 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1319 retval |= DM_RET_DESCEND;
1323 cur_pattern = &patterns[i].pattern.device_pattern;
1325 /* Error out if mutually exclusive options are specified. */
1326 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1327 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1328 return(DM_RET_ERROR);
1331 * If they want to match any device node, we give them any
1334 if (cur_pattern->flags == DEV_MATCH_ANY)
1338 * Not sure why someone would do this...
1340 if (cur_pattern->flags == DEV_MATCH_NONE)
1343 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1344 && (cur_pattern->path_id != device->target->bus->path_id))
1347 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1348 && (cur_pattern->target_id != device->target->target_id))
1351 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1352 && (cur_pattern->target_lun != device->lun_id))
1355 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1356 && (cam_quirkmatch((caddr_t)&device->inq_data,
1357 (caddr_t)&cur_pattern->data.inq_pat,
1358 1, sizeof(cur_pattern->data.inq_pat),
1359 scsi_static_inquiry_match) == NULL))
1362 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1363 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1364 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1365 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1366 device->device_id_len
1367 - SVPD_DEVICE_ID_HDR_LEN,
1368 cur_pattern->data.devid_pat.id,
1369 cur_pattern->data.devid_pat.id_len) != 0))
1374 * If we get to this point, the user definitely wants
1375 * information on this device. So tell the caller to copy
1378 retval |= DM_RET_COPY;
1381 * If the return action has been set to descend, then we
1382 * know that we've already seen a peripheral matching
1383 * expression, therefore we need to further descend the tree.
1384 * This won't change by continuing around the loop, so we
1385 * go ahead and return. If we haven't seen a peripheral
1386 * matching expression, we keep going around the loop until
1387 * we exhaust the matching expressions. We'll set the stop
1388 * flag once we fall out of the loop.
1390 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1395 * If the return action hasn't been set to descend yet, that means
1396 * we haven't seen any peripheral matching patterns. So tell the
1397 * caller to stop descending the tree -- the user doesn't want to
1398 * match against lower level tree elements.
1400 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1401 retval |= DM_RET_STOP;
1407 * Match a single peripheral against any number of match patterns.
1409 static dev_match_ret
1410 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1411 struct cam_periph *periph)
1413 dev_match_ret retval;
1417 * If we aren't given something to match against, that's an error.
1420 return(DM_RET_ERROR);
1423 * If there are no match entries, then this peripheral matches no
1426 if ((patterns == NULL) || (num_patterns == 0))
1427 return(DM_RET_STOP | DM_RET_COPY);
1430 * There aren't any nodes below a peripheral node, so there's no
1431 * reason to descend the tree any further.
1433 retval = DM_RET_STOP;
1435 for (i = 0; i < num_patterns; i++) {
1436 struct periph_match_pattern *cur_pattern;
1439 * If the pattern in question isn't for a peripheral, we
1440 * aren't interested.
1442 if (patterns[i].type != DEV_MATCH_PERIPH)
1445 cur_pattern = &patterns[i].pattern.periph_pattern;
1448 * If they want to match on anything, then we will do so.
1450 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1451 /* set the copy flag */
1452 retval |= DM_RET_COPY;
1455 * We've already set the return action to stop,
1456 * since there are no nodes below peripherals in
1463 * Not sure why someone would do this...
1465 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1468 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1469 && (cur_pattern->path_id != periph->path->bus->path_id))
1473 * For the target and lun id's, we have to make sure the
1474 * target and lun pointers aren't NULL. The xpt peripheral
1475 * has a wildcard target and device.
1477 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1478 && ((periph->path->target == NULL)
1479 ||(cur_pattern->target_id != periph->path->target->target_id)))
1482 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1483 && ((periph->path->device == NULL)
1484 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1487 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1488 && (cur_pattern->unit_number != periph->unit_number))
1491 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1492 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1497 * If we get to this point, the user definitely wants
1498 * information on this peripheral. So tell the caller to
1499 * copy the data out.
1501 retval |= DM_RET_COPY;
1504 * The return action has already been set to stop, since
1505 * peripherals don't have any nodes below them in the EDT.
1511 * If we get to this point, the peripheral that was passed in
1512 * doesn't match any of the patterns.
1518 xptedtbusfunc(struct cam_eb *bus, void *arg)
1520 struct ccb_dev_match *cdm;
1521 struct cam_et *target;
1522 dev_match_ret retval;
1524 cdm = (struct ccb_dev_match *)arg;
1527 * If our position is for something deeper in the tree, that means
1528 * that we've already seen this node. So, we keep going down.
1530 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1531 && (cdm->pos.cookie.bus == bus)
1532 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1533 && (cdm->pos.cookie.target != NULL))
1534 retval = DM_RET_DESCEND;
1536 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1539 * If we got an error, bail out of the search.
1541 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1542 cdm->status = CAM_DEV_MATCH_ERROR;
1547 * If the copy flag is set, copy this bus out.
1549 if (retval & DM_RET_COPY) {
1552 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1553 sizeof(struct dev_match_result));
1556 * If we don't have enough space to put in another
1557 * match result, save our position and tell the
1558 * user there are more devices to check.
1560 if (spaceleft < sizeof(struct dev_match_result)) {
1561 bzero(&cdm->pos, sizeof(cdm->pos));
1562 cdm->pos.position_type =
1563 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1565 cdm->pos.cookie.bus = bus;
1566 cdm->pos.generations[CAM_BUS_GENERATION]=
1567 xsoftc.bus_generation;
1568 cdm->status = CAM_DEV_MATCH_MORE;
1571 j = cdm->num_matches;
1573 cdm->matches[j].type = DEV_MATCH_BUS;
1574 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1575 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1576 cdm->matches[j].result.bus_result.unit_number =
1577 bus->sim->unit_number;
1578 strncpy(cdm->matches[j].result.bus_result.dev_name,
1579 bus->sim->sim_name, DEV_IDLEN);
1583 * If the user is only interested in busses, there's no
1584 * reason to descend to the next level in the tree.
1586 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1590 * If there is a target generation recorded, check it to
1591 * make sure the target list hasn't changed.
1593 mtx_lock(&bus->eb_mtx);
1594 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1595 && (cdm->pos.cookie.bus == bus)
1596 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1597 && (cdm->pos.cookie.target != NULL)) {
1598 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1600 mtx_unlock(&bus->eb_mtx);
1601 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1604 target = (struct cam_et *)cdm->pos.cookie.target;
1608 mtx_unlock(&bus->eb_mtx);
1610 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1614 xptedttargetfunc(struct cam_et *target, void *arg)
1616 struct ccb_dev_match *cdm;
1618 struct cam_ed *device;
1620 cdm = (struct ccb_dev_match *)arg;
1624 * If there is a device list generation recorded, check it to
1625 * make sure the device list hasn't changed.
1627 mtx_lock(&bus->eb_mtx);
1628 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1629 && (cdm->pos.cookie.bus == bus)
1630 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1631 && (cdm->pos.cookie.target == target)
1632 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1633 && (cdm->pos.cookie.device != NULL)) {
1634 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1635 target->generation) {
1636 mtx_unlock(&bus->eb_mtx);
1637 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1640 device = (struct cam_ed *)cdm->pos.cookie.device;
1644 mtx_unlock(&bus->eb_mtx);
1646 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1650 xptedtdevicefunc(struct cam_ed *device, void *arg)
1653 struct cam_periph *periph;
1654 struct ccb_dev_match *cdm;
1655 dev_match_ret retval;
1657 cdm = (struct ccb_dev_match *)arg;
1658 bus = device->target->bus;
1661 * If our position is for something deeper in the tree, that means
1662 * that we've already seen this node. So, we keep going down.
1664 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1665 && (cdm->pos.cookie.device == device)
1666 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1667 && (cdm->pos.cookie.periph != NULL))
1668 retval = DM_RET_DESCEND;
1670 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1673 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1674 cdm->status = CAM_DEV_MATCH_ERROR;
1679 * If the copy flag is set, copy this device out.
1681 if (retval & DM_RET_COPY) {
1684 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1685 sizeof(struct dev_match_result));
1688 * If we don't have enough space to put in another
1689 * match result, save our position and tell the
1690 * user there are more devices to check.
1692 if (spaceleft < sizeof(struct dev_match_result)) {
1693 bzero(&cdm->pos, sizeof(cdm->pos));
1694 cdm->pos.position_type =
1695 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1696 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1698 cdm->pos.cookie.bus = device->target->bus;
1699 cdm->pos.generations[CAM_BUS_GENERATION]=
1700 xsoftc.bus_generation;
1701 cdm->pos.cookie.target = device->target;
1702 cdm->pos.generations[CAM_TARGET_GENERATION] =
1703 device->target->bus->generation;
1704 cdm->pos.cookie.device = device;
1705 cdm->pos.generations[CAM_DEV_GENERATION] =
1706 device->target->generation;
1707 cdm->status = CAM_DEV_MATCH_MORE;
1710 j = cdm->num_matches;
1712 cdm->matches[j].type = DEV_MATCH_DEVICE;
1713 cdm->matches[j].result.device_result.path_id =
1714 device->target->bus->path_id;
1715 cdm->matches[j].result.device_result.target_id =
1716 device->target->target_id;
1717 cdm->matches[j].result.device_result.target_lun =
1719 cdm->matches[j].result.device_result.protocol =
1721 bcopy(&device->inq_data,
1722 &cdm->matches[j].result.device_result.inq_data,
1723 sizeof(struct scsi_inquiry_data));
1724 bcopy(&device->ident_data,
1725 &cdm->matches[j].result.device_result.ident_data,
1726 sizeof(struct ata_params));
1728 /* Let the user know whether this device is unconfigured */
1729 if (device->flags & CAM_DEV_UNCONFIGURED)
1730 cdm->matches[j].result.device_result.flags =
1731 DEV_RESULT_UNCONFIGURED;
1733 cdm->matches[j].result.device_result.flags =
1738 * If the user isn't interested in peripherals, don't descend
1739 * the tree any further.
1741 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1745 * If there is a peripheral list generation recorded, make sure
1746 * it hasn't changed.
1749 mtx_lock(&bus->eb_mtx);
1750 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1751 && (cdm->pos.cookie.bus == bus)
1752 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1753 && (cdm->pos.cookie.target == device->target)
1754 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1755 && (cdm->pos.cookie.device == device)
1756 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1757 && (cdm->pos.cookie.periph != NULL)) {
1758 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1759 device->generation) {
1760 mtx_unlock(&bus->eb_mtx);
1762 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1765 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1769 mtx_unlock(&bus->eb_mtx);
1772 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1776 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1778 struct ccb_dev_match *cdm;
1779 dev_match_ret retval;
1781 cdm = (struct ccb_dev_match *)arg;
1783 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1785 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1786 cdm->status = CAM_DEV_MATCH_ERROR;
1791 * If the copy flag is set, copy this peripheral out.
1793 if (retval & DM_RET_COPY) {
1796 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1797 sizeof(struct dev_match_result));
1800 * If we don't have enough space to put in another
1801 * match result, save our position and tell the
1802 * user there are more devices to check.
1804 if (spaceleft < sizeof(struct dev_match_result)) {
1805 bzero(&cdm->pos, sizeof(cdm->pos));
1806 cdm->pos.position_type =
1807 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1808 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1811 cdm->pos.cookie.bus = periph->path->bus;
1812 cdm->pos.generations[CAM_BUS_GENERATION]=
1813 xsoftc.bus_generation;
1814 cdm->pos.cookie.target = periph->path->target;
1815 cdm->pos.generations[CAM_TARGET_GENERATION] =
1816 periph->path->bus->generation;
1817 cdm->pos.cookie.device = periph->path->device;
1818 cdm->pos.generations[CAM_DEV_GENERATION] =
1819 periph->path->target->generation;
1820 cdm->pos.cookie.periph = periph;
1821 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1822 periph->path->device->generation;
1823 cdm->status = CAM_DEV_MATCH_MORE;
1827 j = cdm->num_matches;
1829 cdm->matches[j].type = DEV_MATCH_PERIPH;
1830 cdm->matches[j].result.periph_result.path_id =
1831 periph->path->bus->path_id;
1832 cdm->matches[j].result.periph_result.target_id =
1833 periph->path->target->target_id;
1834 cdm->matches[j].result.periph_result.target_lun =
1835 periph->path->device->lun_id;
1836 cdm->matches[j].result.periph_result.unit_number =
1837 periph->unit_number;
1838 strncpy(cdm->matches[j].result.periph_result.periph_name,
1839 periph->periph_name, DEV_IDLEN);
1846 xptedtmatch(struct ccb_dev_match *cdm)
1851 cdm->num_matches = 0;
1854 * Check the bus list generation. If it has changed, the user
1855 * needs to reset everything and start over.
1858 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1859 && (cdm->pos.cookie.bus != NULL)) {
1860 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1861 xsoftc.bus_generation) {
1863 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1866 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1872 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1875 * If we get back 0, that means that we had to stop before fully
1876 * traversing the EDT. It also means that one of the subroutines
1877 * has set the status field to the proper value. If we get back 1,
1878 * we've fully traversed the EDT and copied out any matching entries.
1881 cdm->status = CAM_DEV_MATCH_LAST;
1887 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1889 struct cam_periph *periph;
1890 struct ccb_dev_match *cdm;
1892 cdm = (struct ccb_dev_match *)arg;
1895 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1896 && (cdm->pos.cookie.pdrv == pdrv)
1897 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1898 && (cdm->pos.cookie.periph != NULL)) {
1899 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1900 (*pdrv)->generation) {
1902 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1905 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1911 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1915 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1917 struct ccb_dev_match *cdm;
1918 dev_match_ret retval;
1920 cdm = (struct ccb_dev_match *)arg;
1922 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1924 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1925 cdm->status = CAM_DEV_MATCH_ERROR;
1930 * If the copy flag is set, copy this peripheral out.
1932 if (retval & DM_RET_COPY) {
1935 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1936 sizeof(struct dev_match_result));
1939 * If we don't have enough space to put in another
1940 * match result, save our position and tell the
1941 * user there are more devices to check.
1943 if (spaceleft < sizeof(struct dev_match_result)) {
1944 struct periph_driver **pdrv;
1947 bzero(&cdm->pos, sizeof(cdm->pos));
1948 cdm->pos.position_type =
1949 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1953 * This may look a bit non-sensical, but it is
1954 * actually quite logical. There are very few
1955 * peripheral drivers, and bloating every peripheral
1956 * structure with a pointer back to its parent
1957 * peripheral driver linker set entry would cost
1958 * more in the long run than doing this quick lookup.
1960 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1961 if (strcmp((*pdrv)->driver_name,
1962 periph->periph_name) == 0)
1966 if (*pdrv == NULL) {
1967 cdm->status = CAM_DEV_MATCH_ERROR;
1971 cdm->pos.cookie.pdrv = pdrv;
1973 * The periph generation slot does double duty, as
1974 * does the periph pointer slot. They are used for
1975 * both edt and pdrv lookups and positioning.
1977 cdm->pos.cookie.periph = periph;
1978 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1979 (*pdrv)->generation;
1980 cdm->status = CAM_DEV_MATCH_MORE;
1984 j = cdm->num_matches;
1986 cdm->matches[j].type = DEV_MATCH_PERIPH;
1987 cdm->matches[j].result.periph_result.path_id =
1988 periph->path->bus->path_id;
1991 * The transport layer peripheral doesn't have a target or
1994 if (periph->path->target)
1995 cdm->matches[j].result.periph_result.target_id =
1996 periph->path->target->target_id;
1998 cdm->matches[j].result.periph_result.target_id =
1999 CAM_TARGET_WILDCARD;
2001 if (periph->path->device)
2002 cdm->matches[j].result.periph_result.target_lun =
2003 periph->path->device->lun_id;
2005 cdm->matches[j].result.periph_result.target_lun =
2008 cdm->matches[j].result.periph_result.unit_number =
2009 periph->unit_number;
2010 strncpy(cdm->matches[j].result.periph_result.periph_name,
2011 periph->periph_name, DEV_IDLEN);
2018 xptperiphlistmatch(struct ccb_dev_match *cdm)
2022 cdm->num_matches = 0;
2025 * At this point in the edt traversal function, we check the bus
2026 * list generation to make sure that no busses have been added or
2027 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2028 * For the peripheral driver list traversal function, however, we
2029 * don't have to worry about new peripheral driver types coming or
2030 * going; they're in a linker set, and therefore can't change
2031 * without a recompile.
2034 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2035 && (cdm->pos.cookie.pdrv != NULL))
2036 ret = xptpdrvtraverse(
2037 (struct periph_driver **)cdm->pos.cookie.pdrv,
2038 xptplistpdrvfunc, cdm);
2040 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2043 * If we get back 0, that means that we had to stop before fully
2044 * traversing the peripheral driver tree. It also means that one of
2045 * the subroutines has set the status field to the proper value. If
2046 * we get back 1, we've fully traversed the EDT and copied out any
2050 cdm->status = CAM_DEV_MATCH_LAST;
2056 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2058 struct cam_eb *bus, *next_bus;
2066 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2074 for (; bus != NULL; bus = next_bus) {
2075 retval = tr_func(bus, arg);
2077 xpt_release_bus(bus);
2081 next_bus = TAILQ_NEXT(bus, links);
2083 next_bus->refcount++;
2085 xpt_release_bus(bus);
2091 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2092 xpt_targetfunc_t *tr_func, void *arg)
2094 struct cam_et *target, *next_target;
2099 target = start_target;
2101 mtx_lock(&bus->eb_mtx);
2102 target = TAILQ_FIRST(&bus->et_entries);
2103 if (target == NULL) {
2104 mtx_unlock(&bus->eb_mtx);
2108 mtx_unlock(&bus->eb_mtx);
2110 for (; target != NULL; target = next_target) {
2111 retval = tr_func(target, arg);
2113 xpt_release_target(target);
2116 mtx_lock(&bus->eb_mtx);
2117 next_target = TAILQ_NEXT(target, links);
2119 next_target->refcount++;
2120 mtx_unlock(&bus->eb_mtx);
2121 xpt_release_target(target);
2127 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2128 xpt_devicefunc_t *tr_func, void *arg)
2131 struct cam_ed *device, *next_device;
2137 device = start_device;
2139 mtx_lock(&bus->eb_mtx);
2140 device = TAILQ_FIRST(&target->ed_entries);
2141 if (device == NULL) {
2142 mtx_unlock(&bus->eb_mtx);
2146 mtx_unlock(&bus->eb_mtx);
2148 for (; device != NULL; device = next_device) {
2149 mtx_lock(&device->device_mtx);
2150 retval = tr_func(device, arg);
2151 mtx_unlock(&device->device_mtx);
2153 xpt_release_device(device);
2156 mtx_lock(&bus->eb_mtx);
2157 next_device = TAILQ_NEXT(device, links);
2159 next_device->refcount++;
2160 mtx_unlock(&bus->eb_mtx);
2161 xpt_release_device(device);
2167 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2168 xpt_periphfunc_t *tr_func, void *arg)
2171 struct cam_periph *periph, *next_periph;
2176 bus = device->target->bus;
2178 periph = start_periph;
2181 mtx_lock(&bus->eb_mtx);
2182 periph = SLIST_FIRST(&device->periphs);
2183 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2184 periph = SLIST_NEXT(periph, periph_links);
2185 if (periph == NULL) {
2186 mtx_unlock(&bus->eb_mtx);
2191 mtx_unlock(&bus->eb_mtx);
2194 for (; periph != NULL; periph = next_periph) {
2195 retval = tr_func(periph, arg);
2197 cam_periph_release_locked(periph);
2201 mtx_lock(&bus->eb_mtx);
2202 next_periph = SLIST_NEXT(periph, periph_links);
2203 while (next_periph != NULL &&
2204 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2205 next_periph = SLIST_NEXT(next_periph, periph_links);
2207 next_periph->refcount++;
2208 mtx_unlock(&bus->eb_mtx);
2210 cam_periph_release_locked(periph);
2216 xptpdrvtraverse(struct periph_driver **start_pdrv,
2217 xpt_pdrvfunc_t *tr_func, void *arg)
2219 struct periph_driver **pdrv;
2225 * We don't traverse the peripheral driver list like we do the
2226 * other lists, because it is a linker set, and therefore cannot be
2227 * changed during runtime. If the peripheral driver list is ever
2228 * re-done to be something other than a linker set (i.e. it can
2229 * change while the system is running), the list traversal should
2230 * be modified to work like the other traversal functions.
2232 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2233 *pdrv != NULL; pdrv++) {
2234 retval = tr_func(pdrv, arg);
2244 xptpdperiphtraverse(struct periph_driver **pdrv,
2245 struct cam_periph *start_periph,
2246 xpt_periphfunc_t *tr_func, void *arg)
2248 struct cam_periph *periph, *next_periph;
2254 periph = start_periph;
2257 periph = TAILQ_FIRST(&(*pdrv)->units);
2258 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2259 periph = TAILQ_NEXT(periph, unit_links);
2260 if (periph == NULL) {
2267 for (; periph != NULL; periph = next_periph) {
2268 cam_periph_lock(periph);
2269 retval = tr_func(periph, arg);
2270 cam_periph_unlock(periph);
2272 cam_periph_release(periph);
2276 next_periph = TAILQ_NEXT(periph, unit_links);
2277 while (next_periph != NULL &&
2278 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2279 next_periph = TAILQ_NEXT(next_periph, unit_links);
2281 next_periph->refcount++;
2283 cam_periph_release(periph);
2289 xptdefbusfunc(struct cam_eb *bus, void *arg)
2291 struct xpt_traverse_config *tr_config;
2293 tr_config = (struct xpt_traverse_config *)arg;
2295 if (tr_config->depth == XPT_DEPTH_BUS) {
2296 xpt_busfunc_t *tr_func;
2298 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2300 return(tr_func(bus, tr_config->tr_arg));
2302 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2306 xptdeftargetfunc(struct cam_et *target, void *arg)
2308 struct xpt_traverse_config *tr_config;
2310 tr_config = (struct xpt_traverse_config *)arg;
2312 if (tr_config->depth == XPT_DEPTH_TARGET) {
2313 xpt_targetfunc_t *tr_func;
2315 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2317 return(tr_func(target, tr_config->tr_arg));
2319 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2323 xptdefdevicefunc(struct cam_ed *device, void *arg)
2325 struct xpt_traverse_config *tr_config;
2327 tr_config = (struct xpt_traverse_config *)arg;
2329 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2330 xpt_devicefunc_t *tr_func;
2332 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2334 return(tr_func(device, tr_config->tr_arg));
2336 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2340 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2342 struct xpt_traverse_config *tr_config;
2343 xpt_periphfunc_t *tr_func;
2345 tr_config = (struct xpt_traverse_config *)arg;
2347 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2350 * Unlike the other default functions, we don't check for depth
2351 * here. The peripheral driver level is the last level in the EDT,
2352 * so if we're here, we should execute the function in question.
2354 return(tr_func(periph, tr_config->tr_arg));
2358 * Execute the given function for every bus in the EDT.
2361 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2363 struct xpt_traverse_config tr_config;
2365 tr_config.depth = XPT_DEPTH_BUS;
2366 tr_config.tr_func = tr_func;
2367 tr_config.tr_arg = arg;
2369 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2373 * Execute the given function for every device in the EDT.
2376 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2378 struct xpt_traverse_config tr_config;
2380 tr_config.depth = XPT_DEPTH_DEVICE;
2381 tr_config.tr_func = tr_func;
2382 tr_config.tr_arg = arg;
2384 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2388 xptsetasyncfunc(struct cam_ed *device, void *arg)
2390 struct cam_path path;
2391 struct ccb_getdev cgd;
2392 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2395 * Don't report unconfigured devices (Wildcard devs,
2396 * devices only for target mode, device instances
2397 * that have been invalidated but are waiting for
2398 * their last reference count to be released).
2400 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2403 xpt_compile_path(&path,
2405 device->target->bus->path_id,
2406 device->target->target_id,
2408 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2409 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2410 xpt_action((union ccb *)&cgd);
2411 csa->callback(csa->callback_arg,
2414 xpt_release_path(&path);
2420 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2422 struct cam_path path;
2423 struct ccb_pathinq cpi;
2424 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2426 xpt_compile_path(&path, /*periph*/NULL,
2428 CAM_TARGET_WILDCARD,
2430 xpt_path_lock(&path);
2431 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2432 cpi.ccb_h.func_code = XPT_PATH_INQ;
2433 xpt_action((union ccb *)&cpi);
2434 csa->callback(csa->callback_arg,
2437 xpt_path_unlock(&path);
2438 xpt_release_path(&path);
2444 xpt_action(union ccb *start_ccb)
2447 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2449 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2450 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2454 xpt_action_default(union ccb *start_ccb)
2456 struct cam_path *path;
2457 struct cam_sim *sim;
2460 path = start_ccb->ccb_h.path;
2461 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2463 switch (start_ccb->ccb_h.func_code) {
2466 struct cam_ed *device;
2469 * For the sake of compatibility with SCSI-1
2470 * devices that may not understand the identify
2471 * message, we include lun information in the
2472 * second byte of all commands. SCSI-1 specifies
2473 * that luns are a 3 bit value and reserves only 3
2474 * bits for lun information in the CDB. Later
2475 * revisions of the SCSI spec allow for more than 8
2476 * luns, but have deprecated lun information in the
2477 * CDB. So, if the lun won't fit, we must omit.
2479 * Also be aware that during initial probing for devices,
2480 * the inquiry information is unknown but initialized to 0.
2481 * This means that this code will be exercised while probing
2482 * devices with an ANSI revision greater than 2.
2484 device = path->device;
2485 if (device->protocol_version <= SCSI_REV_2
2486 && start_ccb->ccb_h.target_lun < 8
2487 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2489 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2490 start_ccb->ccb_h.target_lun << 5;
2492 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2496 case XPT_CONT_TARGET_IO:
2497 start_ccb->csio.sense_resid = 0;
2498 start_ccb->csio.resid = 0;
2501 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2502 start_ccb->ataio.resid = 0;
2508 struct cam_devq *devq;
2510 devq = path->bus->sim->devq;
2511 mtx_lock(&devq->send_mtx);
2512 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2513 if (xpt_schedule_devq(devq, path->device) != 0)
2515 mtx_unlock(&devq->send_mtx);
2518 case XPT_CALC_GEOMETRY:
2519 /* Filter out garbage */
2520 if (start_ccb->ccg.block_size == 0
2521 || start_ccb->ccg.volume_size == 0) {
2522 start_ccb->ccg.cylinders = 0;
2523 start_ccb->ccg.heads = 0;
2524 start_ccb->ccg.secs_per_track = 0;
2525 start_ccb->ccb_h.status = CAM_REQ_CMP;
2528 #if defined(PC98) || defined(__sparc64__)
2530 * In a PC-98 system, geometry translation depens on
2531 * the "real" device geometry obtained from mode page 4.
2532 * SCSI geometry translation is performed in the
2533 * initialization routine of the SCSI BIOS and the result
2534 * stored in host memory. If the translation is available
2535 * in host memory, use it. If not, rely on the default
2536 * translation the device driver performs.
2537 * For sparc64, we may need adjust the geometry of large
2538 * disks in order to fit the limitations of the 16-bit
2539 * fields of the VTOC8 disk label.
2541 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2542 start_ccb->ccb_h.status = CAM_REQ_CMP;
2549 union ccb* abort_ccb;
2551 abort_ccb = start_ccb->cab.abort_ccb;
2552 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2554 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2555 struct cam_ccbq *ccbq;
2556 struct cam_ed *device;
2558 device = abort_ccb->ccb_h.path->device;
2559 ccbq = &device->ccbq;
2560 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2561 abort_ccb->ccb_h.status =
2562 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2563 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2564 xpt_done(abort_ccb);
2565 start_ccb->ccb_h.status = CAM_REQ_CMP;
2568 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2569 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2571 * We've caught this ccb en route to
2572 * the SIM. Flag it for abort and the
2573 * SIM will do so just before starting
2574 * real work on the CCB.
2576 abort_ccb->ccb_h.status =
2577 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2578 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2579 start_ccb->ccb_h.status = CAM_REQ_CMP;
2583 if (XPT_FC_IS_QUEUED(abort_ccb)
2584 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2586 * It's already completed but waiting
2587 * for our SWI to get to it.
2589 start_ccb->ccb_h.status = CAM_UA_ABORT;
2593 * If we weren't able to take care of the abort request
2594 * in the XPT, pass the request down to the SIM for processing.
2598 case XPT_ACCEPT_TARGET_IO:
2600 case XPT_IMMED_NOTIFY:
2601 case XPT_NOTIFY_ACK:
2603 case XPT_IMMEDIATE_NOTIFY:
2604 case XPT_NOTIFY_ACKNOWLEDGE:
2605 case XPT_GET_SIM_KNOB:
2606 case XPT_SET_SIM_KNOB:
2607 case XPT_GET_TRAN_SETTINGS:
2608 case XPT_SET_TRAN_SETTINGS:
2611 sim = path->bus->sim;
2612 lock = (mtx_owned(sim->mtx) == 0);
2615 (*(sim->sim_action))(sim, start_ccb);
2617 CAM_SIM_UNLOCK(sim);
2619 case XPT_PATH_STATS:
2620 start_ccb->cpis.last_reset = path->bus->last_reset;
2621 start_ccb->ccb_h.status = CAM_REQ_CMP;
2628 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2629 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2631 struct ccb_getdev *cgd;
2633 cgd = &start_ccb->cgd;
2634 cgd->protocol = dev->protocol;
2635 cgd->inq_data = dev->inq_data;
2636 cgd->ident_data = dev->ident_data;
2637 cgd->inq_flags = dev->inq_flags;
2638 cgd->ccb_h.status = CAM_REQ_CMP;
2639 cgd->serial_num_len = dev->serial_num_len;
2640 if ((dev->serial_num_len > 0)
2641 && (dev->serial_num != NULL))
2642 bcopy(dev->serial_num, cgd->serial_num,
2643 dev->serial_num_len);
2647 case XPT_GDEV_STATS:
2652 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2653 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2655 struct ccb_getdevstats *cgds;
2658 struct cam_devq *devq;
2660 cgds = &start_ccb->cgds;
2663 devq = bus->sim->devq;
2664 mtx_lock(&devq->send_mtx);
2665 cgds->dev_openings = dev->ccbq.dev_openings;
2666 cgds->dev_active = dev->ccbq.dev_active;
2667 cgds->allocated = dev->ccbq.allocated;
2668 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2669 cgds->held = cgds->allocated - cgds->dev_active -
2671 cgds->last_reset = tar->last_reset;
2672 cgds->maxtags = dev->maxtags;
2673 cgds->mintags = dev->mintags;
2674 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2675 cgds->last_reset = bus->last_reset;
2676 mtx_unlock(&devq->send_mtx);
2677 cgds->ccb_h.status = CAM_REQ_CMP;
2683 struct cam_periph *nperiph;
2684 struct periph_list *periph_head;
2685 struct ccb_getdevlist *cgdl;
2687 struct cam_ed *device;
2694 * Don't want anyone mucking with our data.
2696 device = path->device;
2697 periph_head = &device->periphs;
2698 cgdl = &start_ccb->cgdl;
2701 * Check and see if the list has changed since the user
2702 * last requested a list member. If so, tell them that the
2703 * list has changed, and therefore they need to start over
2704 * from the beginning.
2706 if ((cgdl->index != 0) &&
2707 (cgdl->generation != device->generation)) {
2708 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2713 * Traverse the list of peripherals and attempt to find
2714 * the requested peripheral.
2716 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2717 (nperiph != NULL) && (i <= cgdl->index);
2718 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2719 if (i == cgdl->index) {
2720 strncpy(cgdl->periph_name,
2721 nperiph->periph_name,
2723 cgdl->unit_number = nperiph->unit_number;
2728 cgdl->status = CAM_GDEVLIST_ERROR;
2732 if (nperiph == NULL)
2733 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2735 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2738 cgdl->generation = device->generation;
2740 cgdl->ccb_h.status = CAM_REQ_CMP;
2745 dev_pos_type position_type;
2746 struct ccb_dev_match *cdm;
2748 cdm = &start_ccb->cdm;
2751 * There are two ways of getting at information in the EDT.
2752 * The first way is via the primary EDT tree. It starts
2753 * with a list of busses, then a list of targets on a bus,
2754 * then devices/luns on a target, and then peripherals on a
2755 * device/lun. The "other" way is by the peripheral driver
2756 * lists. The peripheral driver lists are organized by
2757 * peripheral driver. (obviously) So it makes sense to
2758 * use the peripheral driver list if the user is looking
2759 * for something like "da1", or all "da" devices. If the
2760 * user is looking for something on a particular bus/target
2761 * or lun, it's generally better to go through the EDT tree.
2764 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2765 position_type = cdm->pos.position_type;
2769 position_type = CAM_DEV_POS_NONE;
2771 for (i = 0; i < cdm->num_patterns; i++) {
2772 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2773 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2774 position_type = CAM_DEV_POS_EDT;
2779 if (cdm->num_patterns == 0)
2780 position_type = CAM_DEV_POS_EDT;
2781 else if (position_type == CAM_DEV_POS_NONE)
2782 position_type = CAM_DEV_POS_PDRV;
2785 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2786 case CAM_DEV_POS_EDT:
2789 case CAM_DEV_POS_PDRV:
2790 xptperiphlistmatch(cdm);
2793 cdm->status = CAM_DEV_MATCH_ERROR;
2797 if (cdm->status == CAM_DEV_MATCH_ERROR)
2798 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2800 start_ccb->ccb_h.status = CAM_REQ_CMP;
2806 struct ccb_setasync *csa;
2807 struct async_node *cur_entry;
2808 struct async_list *async_head;
2811 csa = &start_ccb->csa;
2812 added = csa->event_enable;
2813 async_head = &path->device->asyncs;
2816 * If there is already an entry for us, simply
2819 cur_entry = SLIST_FIRST(async_head);
2820 while (cur_entry != NULL) {
2821 if ((cur_entry->callback_arg == csa->callback_arg)
2822 && (cur_entry->callback == csa->callback))
2824 cur_entry = SLIST_NEXT(cur_entry, links);
2827 if (cur_entry != NULL) {
2829 * If the request has no flags set,
2832 added &= ~cur_entry->event_enable;
2833 if (csa->event_enable == 0) {
2834 SLIST_REMOVE(async_head, cur_entry,
2836 xpt_release_device(path->device);
2837 free(cur_entry, M_CAMXPT);
2839 cur_entry->event_enable = csa->event_enable;
2841 csa->event_enable = added;
2843 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2845 if (cur_entry == NULL) {
2846 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2849 cur_entry->event_enable = csa->event_enable;
2850 cur_entry->event_lock =
2851 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2852 cur_entry->callback_arg = csa->callback_arg;
2853 cur_entry->callback = csa->callback;
2854 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2855 xpt_acquire_device(path->device);
2857 start_ccb->ccb_h.status = CAM_REQ_CMP;
2862 struct ccb_relsim *crs;
2865 crs = &start_ccb->crs;
2869 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2873 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2875 /* Don't ever go below one opening */
2876 if (crs->openings > 0) {
2877 xpt_dev_ccbq_resize(path, crs->openings);
2880 "number of openings is now %d\n",
2886 mtx_lock(&dev->sim->devq->send_mtx);
2887 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2889 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2892 * Just extend the old timeout and decrement
2893 * the freeze count so that a single timeout
2894 * is sufficient for releasing the queue.
2896 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2897 callout_stop(&dev->callout);
2900 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2903 callout_reset_sbt(&dev->callout,
2904 SBT_1MS * crs->release_timeout, 0,
2905 xpt_release_devq_timeout, dev, 0);
2907 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2911 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2913 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2915 * Decrement the freeze count so that a single
2916 * completion is still sufficient to unfreeze
2919 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2922 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2923 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2927 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2929 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2930 || (dev->ccbq.dev_active == 0)) {
2932 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2935 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2936 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2939 mtx_unlock(&dev->sim->devq->send_mtx);
2941 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2942 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2943 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2944 start_ccb->ccb_h.status = CAM_REQ_CMP;
2948 struct cam_path *oldpath;
2950 /* Check that all request bits are supported. */
2951 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2952 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2956 cam_dflags = CAM_DEBUG_NONE;
2957 if (cam_dpath != NULL) {
2958 oldpath = cam_dpath;
2960 xpt_free_path(oldpath);
2962 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2963 if (xpt_create_path(&cam_dpath, NULL,
2964 start_ccb->ccb_h.path_id,
2965 start_ccb->ccb_h.target_id,
2966 start_ccb->ccb_h.target_lun) !=
2968 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2970 cam_dflags = start_ccb->cdbg.flags;
2971 start_ccb->ccb_h.status = CAM_REQ_CMP;
2972 xpt_print(cam_dpath, "debugging flags now %x\n",
2976 start_ccb->ccb_h.status = CAM_REQ_CMP;
2980 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2981 xpt_freeze_devq(path, 1);
2982 start_ccb->ccb_h.status = CAM_REQ_CMP;
2989 printf("%s: CCB type %#x not supported\n", __func__,
2990 start_ccb->ccb_h.func_code);
2991 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2992 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2993 xpt_done(start_ccb);
3000 xpt_polled_action(union ccb *start_ccb)
3003 struct cam_sim *sim;
3004 struct cam_devq *devq;
3007 timeout = start_ccb->ccb_h.timeout * 10;
3008 sim = start_ccb->ccb_h.path->bus->sim;
3010 dev = start_ccb->ccb_h.path->device;
3012 mtx_unlock(&dev->device_mtx);
3015 * Steal an opening so that no other queued requests
3016 * can get it before us while we simulate interrupts.
3018 mtx_lock(&devq->send_mtx);
3019 dev->ccbq.dev_openings--;
3020 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3022 mtx_unlock(&devq->send_mtx);
3025 (*(sim->sim_poll))(sim);
3026 CAM_SIM_UNLOCK(sim);
3028 mtx_lock(&devq->send_mtx);
3030 dev->ccbq.dev_openings++;
3031 mtx_unlock(&devq->send_mtx);
3034 xpt_action(start_ccb);
3035 while(--timeout > 0) {
3037 (*(sim->sim_poll))(sim);
3038 CAM_SIM_UNLOCK(sim);
3040 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3047 * XXX Is it worth adding a sim_timeout entry
3048 * point so we can attempt recovery? If
3049 * this is only used for dumps, I don't think
3052 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3055 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3058 mtx_lock(&dev->device_mtx);
3062 * Schedule a peripheral driver to receive a ccb when its
3063 * target device has space for more transactions.
3066 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3069 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3070 cam_periph_assert(periph, MA_OWNED);
3071 if (new_priority < periph->scheduled_priority) {
3072 periph->scheduled_priority = new_priority;
3073 xpt_run_allocq(periph, 0);
3079 * Schedule a device to run on a given queue.
3080 * If the device was inserted as a new entry on the queue,
3081 * return 1 meaning the device queue should be run. If we
3082 * were already queued, implying someone else has already
3083 * started the queue, return 0 so the caller doesn't attempt
3087 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3088 u_int32_t new_priority)
3091 u_int32_t old_priority;
3093 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3095 old_priority = pinfo->priority;
3098 * Are we already queued?
3100 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3101 /* Simply reorder based on new priority */
3102 if (new_priority < old_priority) {
3103 camq_change_priority(queue, pinfo->index,
3105 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3106 ("changed priority to %d\n",
3112 /* New entry on the queue */
3113 if (new_priority < old_priority)
3114 pinfo->priority = new_priority;
3116 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3117 ("Inserting onto queue\n"));
3118 pinfo->generation = ++queue->generation;
3119 camq_insert(queue, pinfo);
3126 xpt_run_allocq_task(void *context, int pending)
3128 struct cam_periph *periph = context;
3130 cam_periph_lock(periph);
3131 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3132 xpt_run_allocq(periph, 1);
3133 cam_periph_unlock(periph);
3134 cam_periph_release(periph);
3138 xpt_run_allocq(struct cam_periph *periph, int sleep)
3140 struct cam_ed *device;
3144 cam_periph_assert(periph, MA_OWNED);
3145 if (periph->periph_allocating)
3147 periph->periph_allocating = 1;
3148 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3149 device = periph->path->device;
3152 while ((prio = min(periph->scheduled_priority,
3153 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3154 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3155 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3158 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3160 ccb = xpt_get_ccb(periph);
3163 if (periph->flags & CAM_PERIPH_RUN_TASK)
3165 cam_periph_doacquire(periph);
3166 periph->flags |= CAM_PERIPH_RUN_TASK;
3167 taskqueue_enqueue(xsoftc.xpt_taskq,
3168 &periph->periph_run_task);
3171 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3172 if (prio == periph->immediate_priority) {
3173 periph->immediate_priority = CAM_PRIORITY_NONE;
3174 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3175 ("waking cam_periph_getccb()\n"));
3176 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3178 wakeup(&periph->ccb_list);
3180 periph->scheduled_priority = CAM_PRIORITY_NONE;
3181 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3182 ("calling periph_start()\n"));
3183 periph->periph_start(periph, ccb);
3188 xpt_release_ccb(ccb);
3189 periph->periph_allocating = 0;
3193 xpt_run_devq(struct cam_devq *devq)
3195 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3198 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3200 devq->send_queue.qfrozen_cnt++;
3201 while ((devq->send_queue.entries > 0)
3202 && (devq->send_openings > 0)
3203 && (devq->send_queue.qfrozen_cnt <= 1)) {
3204 struct cam_ed *device;
3205 union ccb *work_ccb;
3206 struct cam_sim *sim;
3208 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3210 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3211 ("running device %p\n", device));
3213 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3214 if (work_ccb == NULL) {
3215 printf("device on run queue with no ccbs???\n");
3219 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3221 mtx_lock(&xsoftc.xpt_highpower_lock);
3222 if (xsoftc.num_highpower <= 0) {
3224 * We got a high power command, but we
3225 * don't have any available slots. Freeze
3226 * the device queue until we have a slot
3229 xpt_freeze_devq_device(device, 1);
3230 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3233 mtx_unlock(&xsoftc.xpt_highpower_lock);
3237 * Consume a high power slot while
3240 xsoftc.num_highpower--;
3242 mtx_unlock(&xsoftc.xpt_highpower_lock);
3244 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3245 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3246 devq->send_openings--;
3247 devq->send_active++;
3248 xpt_schedule_devq(devq, device);
3249 mtx_unlock(&devq->send_mtx);
3251 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3253 * The client wants to freeze the queue
3254 * after this CCB is sent.
3256 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3259 /* In Target mode, the peripheral driver knows best... */
3260 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3261 if ((device->inq_flags & SID_CmdQue) != 0
3262 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3263 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3266 * Clear this in case of a retried CCB that
3267 * failed due to a rejected tag.
3269 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3272 switch (work_ccb->ccb_h.func_code) {
3274 CAM_DEBUG(work_ccb->ccb_h.path,
3275 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3276 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3278 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3279 cdb_str, sizeof(cdb_str))));
3282 CAM_DEBUG(work_ccb->ccb_h.path,
3283 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3284 ata_op_string(&work_ccb->ataio.cmd),
3285 ata_cmd_string(&work_ccb->ataio.cmd,
3286 cdb_str, sizeof(cdb_str))));
3293 * Device queues can be shared among multiple SIM instances
3294 * that reside on different busses. Use the SIM from the
3295 * queued device, rather than the one from the calling bus.
3298 lock = (mtx_owned(sim->mtx) == 0);
3301 (*(sim->sim_action))(sim, work_ccb);
3303 CAM_SIM_UNLOCK(sim);
3304 mtx_lock(&devq->send_mtx);
3306 devq->send_queue.qfrozen_cnt--;
3310 * This function merges stuff from the slave ccb into the master ccb, while
3311 * keeping important fields in the master ccb constant.
3314 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3318 * Pull fields that are valid for peripheral drivers to set
3319 * into the master CCB along with the CCB "payload".
3321 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3322 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3323 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3324 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3325 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3326 sizeof(union ccb) - sizeof(struct ccb_hdr));
3330 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3333 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3334 ccb_h->pinfo.priority = priority;
3336 ccb_h->path_id = path->bus->path_id;
3338 ccb_h->target_id = path->target->target_id;
3340 ccb_h->target_id = CAM_TARGET_WILDCARD;
3342 ccb_h->target_lun = path->device->lun_id;
3343 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3345 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3347 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3352 /* Path manipulation functions */
3354 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3355 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3357 struct cam_path *path;
3360 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3363 status = CAM_RESRC_UNAVAIL;
3366 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3367 if (status != CAM_REQ_CMP) {
3368 free(path, M_CAMPATH);
3371 *new_path_ptr = path;
3376 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3377 struct cam_periph *periph, path_id_t path_id,
3378 target_id_t target_id, lun_id_t lun_id)
3381 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3386 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3387 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3390 struct cam_et *target;
3391 struct cam_ed *device;
3394 status = CAM_REQ_CMP; /* Completed without error */
3395 target = NULL; /* Wildcarded */
3396 device = NULL; /* Wildcarded */
3399 * We will potentially modify the EDT, so block interrupts
3400 * that may attempt to create cam paths.
3402 bus = xpt_find_bus(path_id);
3404 status = CAM_PATH_INVALID;
3407 mtx_lock(&bus->eb_mtx);
3408 target = xpt_find_target(bus, target_id);
3409 if (target == NULL) {
3411 struct cam_et *new_target;
3413 new_target = xpt_alloc_target(bus, target_id);
3414 if (new_target == NULL) {
3415 status = CAM_RESRC_UNAVAIL;
3417 target = new_target;
3421 if (target != NULL) {
3422 device = xpt_find_device(target, lun_id);
3423 if (device == NULL) {
3425 struct cam_ed *new_device;
3428 (*(bus->xport->alloc_device))(bus,
3431 if (new_device == NULL) {
3432 status = CAM_RESRC_UNAVAIL;
3434 device = new_device;
3438 mtx_unlock(&bus->eb_mtx);
3442 * Only touch the user's data if we are successful.
3444 if (status == CAM_REQ_CMP) {
3445 new_path->periph = perph;
3446 new_path->bus = bus;
3447 new_path->target = target;
3448 new_path->device = device;
3449 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3452 xpt_release_device(device);
3454 xpt_release_target(target);
3456 xpt_release_bus(bus);
3462 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3464 struct cam_path *new_path;
3466 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3467 if (new_path == NULL)
3468 return(CAM_RESRC_UNAVAIL);
3469 xpt_copy_path(new_path, path);
3470 *new_path_ptr = new_path;
3471 return (CAM_REQ_CMP);
3475 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3479 if (path->bus != NULL)
3480 xpt_acquire_bus(path->bus);
3481 if (path->target != NULL)
3482 xpt_acquire_target(path->target);
3483 if (path->device != NULL)
3484 xpt_acquire_device(path->device);
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->device);
3493 path->device = NULL;
3495 if (path->target != NULL) {
3496 xpt_release_target(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_CAMPATH);
3515 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3516 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3522 *bus_ref = path->bus->refcount;
3528 *periph_ref = path->periph->refcount;
3535 *target_ref = path->target->refcount;
3541 *device_ref = path->device->refcount;
3548 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3549 * in path1, 2 for match with wildcards in path2.
3552 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3556 if (path1->bus != path2->bus) {
3557 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3559 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3564 if (path1->target != path2->target) {
3565 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3568 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3573 if (path1->device != path2->device) {
3574 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3577 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3586 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3590 if (path->bus != dev->target->bus) {
3591 if (path->bus->path_id == CAM_BUS_WILDCARD)
3593 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3598 if (path->target != dev->target) {
3599 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3602 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3607 if (path->device != dev) {
3608 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3611 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3620 xpt_print_path(struct cam_path *path)
3624 printf("(nopath): ");
3626 if (path->periph != NULL)
3627 printf("(%s%d:", path->periph->periph_name,
3628 path->periph->unit_number);
3630 printf("(noperiph:");
3632 if (path->bus != NULL)
3633 printf("%s%d:%d:", path->bus->sim->sim_name,
3634 path->bus->sim->unit_number,
3635 path->bus->sim->bus_id);
3639 if (path->target != NULL)
3640 printf("%d:", path->target->target_id);
3644 if (path->device != NULL)
3645 printf("%jx): ", (uintmax_t)path->device->lun_id);
3652 xpt_print_device(struct cam_ed *device)
3656 printf("(nopath): ");
3658 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3659 device->sim->unit_number,
3660 device->sim->bus_id,
3661 device->target->target_id,
3662 (uintmax_t)device->lun_id);
3667 xpt_print(struct cam_path *path, const char *fmt, ...)
3670 xpt_print_path(path);
3677 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3681 sbuf_new(&sb, str, str_len, 0);
3684 sbuf_printf(&sb, "(nopath): ");
3686 if (path->periph != NULL)
3687 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3688 path->periph->unit_number);
3690 sbuf_printf(&sb, "(noperiph:");
3692 if (path->bus != NULL)
3693 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3694 path->bus->sim->unit_number,
3695 path->bus->sim->bus_id);
3697 sbuf_printf(&sb, "nobus:");
3699 if (path->target != NULL)
3700 sbuf_printf(&sb, "%d:", path->target->target_id);
3702 sbuf_printf(&sb, "X:");
3704 if (path->device != NULL)
3705 sbuf_printf(&sb, "%jx): ",
3706 (uintmax_t)path->device->lun_id);
3708 sbuf_printf(&sb, "X): ");
3712 return(sbuf_len(&sb));
3716 xpt_path_path_id(struct cam_path *path)
3718 return(path->bus->path_id);
3722 xpt_path_target_id(struct cam_path *path)
3724 if (path->target != NULL)
3725 return (path->target->target_id);
3727 return (CAM_TARGET_WILDCARD);
3731 xpt_path_lun_id(struct cam_path *path)
3733 if (path->device != NULL)
3734 return (path->device->lun_id);
3736 return (CAM_LUN_WILDCARD);
3740 xpt_path_sim(struct cam_path *path)
3743 return (path->bus->sim);
3747 xpt_path_periph(struct cam_path *path)
3750 return (path->periph);
3754 xpt_path_legacy_ata_id(struct cam_path *path)
3759 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3760 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3761 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3762 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3765 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3766 path->bus->sim->unit_number < 2) {
3767 bus_id = path->bus->sim->unit_number;
3771 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3772 if (bus == path->bus)
3774 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3775 bus->sim->unit_number >= 2) ||
3776 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3777 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3778 strcmp(bus->sim->sim_name, "siisch") == 0)
3783 if (path->target != NULL) {
3784 if (path->target->target_id < 2)
3785 return (bus_id * 2 + path->target->target_id);
3789 return (bus_id * 2);
3793 * Release a CAM control block for the caller. Remit the cost of the structure
3794 * to the device referenced by the path. If the this device had no 'credits'
3795 * and peripheral drivers have registered async callbacks for this notification
3799 xpt_release_ccb(union ccb *free_ccb)
3801 struct cam_ed *device;
3802 struct cam_periph *periph;
3804 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3805 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3806 device = free_ccb->ccb_h.path->device;
3807 periph = free_ccb->ccb_h.path->periph;
3809 xpt_free_ccb(free_ccb);
3810 periph->periph_allocated--;
3811 cam_ccbq_release_opening(&device->ccbq);
3812 xpt_run_allocq(periph, 0);
3815 /* Functions accessed by SIM drivers */
3817 static struct xpt_xport xport_default = {
3818 .alloc_device = xpt_alloc_device_default,
3819 .action = xpt_action_default,
3820 .async = xpt_dev_async_default,
3824 * A sim structure, listing the SIM entry points and instance
3825 * identification info is passed to xpt_bus_register to hook the SIM
3826 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3827 * for this new bus and places it in the array of busses and assigns
3828 * it a path_id. The path_id may be influenced by "hard wiring"
3829 * information specified by the user. Once interrupt services are
3830 * available, the bus will be probed.
3833 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3835 struct cam_eb *new_bus;
3836 struct cam_eb *old_bus;
3837 struct ccb_pathinq cpi;
3838 struct cam_path *path;
3841 mtx_assert(sim->mtx, MA_OWNED);
3844 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3845 M_CAMXPT, M_NOWAIT|M_ZERO);
3846 if (new_bus == NULL) {
3847 /* Couldn't satisfy request */
3848 return (CAM_RESRC_UNAVAIL);
3851 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3852 TAILQ_INIT(&new_bus->et_entries);
3855 timevalclear(&new_bus->last_reset);
3857 new_bus->refcount = 1; /* Held until a bus_deregister event */
3858 new_bus->generation = 0;
3861 sim->path_id = new_bus->path_id =
3862 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3863 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3864 while (old_bus != NULL
3865 && old_bus->path_id < new_bus->path_id)
3866 old_bus = TAILQ_NEXT(old_bus, links);
3867 if (old_bus != NULL)
3868 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3870 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3871 xsoftc.bus_generation++;
3875 * Set a default transport so that a PATH_INQ can be issued to
3876 * the SIM. This will then allow for probing and attaching of
3877 * a more appropriate transport.
3879 new_bus->xport = &xport_default;
3881 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3882 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3883 if (status != CAM_REQ_CMP) {
3884 xpt_release_bus(new_bus);
3885 free(path, M_CAMXPT);
3886 return (CAM_RESRC_UNAVAIL);
3889 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3890 cpi.ccb_h.func_code = XPT_PATH_INQ;
3891 xpt_action((union ccb *)&cpi);
3893 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3894 switch (cpi.transport) {
3902 new_bus->xport = scsi_get_xport();
3906 new_bus->xport = ata_get_xport();
3909 new_bus->xport = &xport_default;
3914 /* Notify interested parties */
3915 if (sim->path_id != CAM_XPT_PATH_ID) {
3917 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3918 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3919 union ccb *scan_ccb;
3921 /* Initiate bus rescan. */
3922 scan_ccb = xpt_alloc_ccb_nowait();
3923 if (scan_ccb != NULL) {
3924 scan_ccb->ccb_h.path = path;
3925 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3926 scan_ccb->crcn.flags = 0;
3927 xpt_rescan(scan_ccb);
3930 "Can't allocate CCB to scan bus\n");
3931 xpt_free_path(path);
3934 xpt_free_path(path);
3936 xpt_free_path(path);
3937 return (CAM_SUCCESS);
3941 xpt_bus_deregister(path_id_t pathid)
3943 struct cam_path bus_path;
3946 status = xpt_compile_path(&bus_path, NULL, pathid,
3947 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3948 if (status != CAM_REQ_CMP)
3951 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3952 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3954 /* Release the reference count held while registered. */
3955 xpt_release_bus(bus_path.bus);
3956 xpt_release_path(&bus_path);
3958 return (CAM_REQ_CMP);
3962 xptnextfreepathid(void)
3968 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3970 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3972 /* Find an unoccupied pathid */
3973 while (bus != NULL && bus->path_id <= pathid) {
3974 if (bus->path_id == pathid)
3976 bus = TAILQ_NEXT(bus, links);
3980 * Ensure that this pathid is not reserved for
3981 * a bus that may be registered in the future.
3983 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3985 /* Start the search over */
3992 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3999 pathid = CAM_XPT_PATH_ID;
4000 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4001 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4004 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4005 if (strcmp(dname, "scbus")) {
4006 /* Avoid a bit of foot shooting. */
4009 if (dunit < 0) /* unwired?! */
4011 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4012 if (sim_bus == val) {
4016 } else if (sim_bus == 0) {
4017 /* Unspecified matches bus 0 */
4021 printf("Ambiguous scbus configuration for %s%d "
4022 "bus %d, cannot wire down. The kernel "
4023 "config entry for scbus%d should "
4024 "specify a controller bus.\n"
4025 "Scbus will be assigned dynamically.\n",
4026 sim_name, sim_unit, sim_bus, dunit);
4031 if (pathid == CAM_XPT_PATH_ID)
4032 pathid = xptnextfreepathid();
4037 xpt_async_string(u_int32_t async_code)
4040 switch (async_code) {
4041 case AC_BUS_RESET: return ("AC_BUS_RESET");
4042 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4043 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4044 case AC_SENT_BDR: return ("AC_SENT_BDR");
4045 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4046 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4047 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4048 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4049 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4050 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4051 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4052 case AC_CONTRACT: return ("AC_CONTRACT");
4053 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4054 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4056 return ("AC_UNKNOWN");
4060 xpt_async_size(u_int32_t async_code)
4063 switch (async_code) {
4064 case AC_BUS_RESET: return (0);
4065 case AC_UNSOL_RESEL: return (0);
4066 case AC_SCSI_AEN: return (0);
4067 case AC_SENT_BDR: return (0);
4068 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4069 case AC_PATH_DEREGISTERED: return (0);
4070 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4071 case AC_LOST_DEVICE: return (0);
4072 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4073 case AC_INQ_CHANGED: return (0);
4074 case AC_GETDEV_CHANGED: return (0);
4075 case AC_CONTRACT: return (sizeof(struct ac_contract));
4076 case AC_ADVINFO_CHANGED: return (-1);
4077 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4083 xpt_async_process_dev(struct cam_ed *device, void *arg)
4085 union ccb *ccb = arg;
4086 struct cam_path *path = ccb->ccb_h.path;
4087 void *async_arg = ccb->casync.async_arg_ptr;
4088 u_int32_t async_code = ccb->casync.async_code;
4091 if (path->device != device
4092 && path->device->lun_id != CAM_LUN_WILDCARD
4093 && device->lun_id != CAM_LUN_WILDCARD)
4097 * The async callback could free the device.
4098 * If it is a broadcast async, it doesn't hold
4099 * device reference, so take our own reference.
4101 xpt_acquire_device(device);
4104 * If async for specific device is to be delivered to
4105 * the wildcard client, take the specific device lock.
4106 * XXX: We may need a way for client to specify it.
4108 if ((device->lun_id == CAM_LUN_WILDCARD &&
4109 path->device->lun_id != CAM_LUN_WILDCARD) ||
4110 (device->target->target_id == CAM_TARGET_WILDCARD &&
4111 path->target->target_id != CAM_TARGET_WILDCARD) ||
4112 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4113 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4114 mtx_unlock(&device->device_mtx);
4115 xpt_path_lock(path);
4120 (*(device->target->bus->xport->async))(async_code,
4121 device->target->bus, device->target, device, async_arg);
4122 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4125 xpt_path_unlock(path);
4126 mtx_lock(&device->device_mtx);
4128 xpt_release_device(device);
4133 xpt_async_process_tgt(struct cam_et *target, void *arg)
4135 union ccb *ccb = arg;
4136 struct cam_path *path = ccb->ccb_h.path;
4138 if (path->target != target
4139 && path->target->target_id != CAM_TARGET_WILDCARD
4140 && target->target_id != CAM_TARGET_WILDCARD)
4143 if (ccb->casync.async_code == AC_SENT_BDR) {
4144 /* Update our notion of when the last reset occurred */
4145 microtime(&target->last_reset);
4148 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4152 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4155 struct cam_path *path;
4157 u_int32_t async_code;
4159 path = ccb->ccb_h.path;
4160 async_code = ccb->casync.async_code;
4161 async_arg = ccb->casync.async_arg_ptr;
4162 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4163 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4166 if (async_code == AC_BUS_RESET) {
4167 /* Update our notion of when the last reset occurred */
4168 microtime(&bus->last_reset);
4171 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4174 * If this wasn't a fully wildcarded async, tell all
4175 * clients that want all async events.
4177 if (bus != xpt_periph->path->bus) {
4178 xpt_path_lock(xpt_periph->path);
4179 xpt_async_process_dev(xpt_periph->path->device, ccb);
4180 xpt_path_unlock(xpt_periph->path);
4183 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4184 xpt_release_devq(path, 1, TRUE);
4186 xpt_release_simq(path->bus->sim, TRUE);
4187 if (ccb->casync.async_arg_size > 0)
4188 free(async_arg, M_CAMXPT);
4189 xpt_free_path(path);
4194 xpt_async_bcast(struct async_list *async_head,
4195 u_int32_t async_code,
4196 struct cam_path *path, void *async_arg)
4198 struct async_node *cur_entry;
4201 cur_entry = SLIST_FIRST(async_head);
4202 while (cur_entry != NULL) {
4203 struct async_node *next_entry;
4205 * Grab the next list entry before we call the current
4206 * entry's callback. This is because the callback function
4207 * can delete its async callback entry.
4209 next_entry = SLIST_NEXT(cur_entry, links);
4210 if ((cur_entry->event_enable & async_code) != 0) {
4211 lock = cur_entry->event_lock;
4213 CAM_SIM_LOCK(path->device->sim);
4214 cur_entry->callback(cur_entry->callback_arg,
4218 CAM_SIM_UNLOCK(path->device->sim);
4220 cur_entry = next_entry;
4225 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4230 ccb = xpt_alloc_ccb_nowait();
4232 xpt_print(path, "Can't allocate CCB to send %s\n",
4233 xpt_async_string(async_code));
4237 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4238 xpt_print(path, "Can't allocate path to send %s\n",
4239 xpt_async_string(async_code));
4243 ccb->ccb_h.path->periph = NULL;
4244 ccb->ccb_h.func_code = XPT_ASYNC;
4245 ccb->ccb_h.cbfcnp = xpt_async_process;
4246 ccb->ccb_h.flags |= CAM_UNLOCKED;
4247 ccb->casync.async_code = async_code;
4248 ccb->casync.async_arg_size = 0;
4249 size = xpt_async_size(async_code);
4250 if (size > 0 && async_arg != NULL) {
4251 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4252 if (ccb->casync.async_arg_ptr == NULL) {
4253 xpt_print(path, "Can't allocate argument to send %s\n",
4254 xpt_async_string(async_code));
4255 xpt_free_path(ccb->ccb_h.path);
4259 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4260 ccb->casync.async_arg_size = size;
4261 } else if (size < 0)
4262 ccb->casync.async_arg_size = size;
4263 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4264 xpt_freeze_devq(path, 1);
4266 xpt_freeze_simq(path->bus->sim, 1);
4271 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4272 struct cam_et *target, struct cam_ed *device,
4277 * We only need to handle events for real devices.
4279 if (target->target_id == CAM_TARGET_WILDCARD
4280 || device->lun_id == CAM_LUN_WILDCARD)
4283 printf("%s called\n", __func__);
4287 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4289 struct cam_devq *devq;
4292 devq = dev->sim->devq;
4293 mtx_assert(&devq->send_mtx, MA_OWNED);
4294 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4295 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4296 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4297 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4298 /* Remove frozen device from sendq. */
4299 if (device_is_queued(dev))
4300 camq_remove(&devq->send_queue, dev->devq_entry.index);
4305 xpt_freeze_devq(struct cam_path *path, u_int count)
4307 struct cam_ed *dev = path->device;
4308 struct cam_devq *devq;
4311 devq = dev->sim->devq;
4312 mtx_lock(&devq->send_mtx);
4313 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4314 freeze = xpt_freeze_devq_device(dev, count);
4315 mtx_unlock(&devq->send_mtx);
4320 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4322 struct cam_devq *devq;
4326 mtx_lock(&devq->send_mtx);
4327 freeze = (devq->send_queue.qfrozen_cnt += count);
4328 mtx_unlock(&devq->send_mtx);
4333 xpt_release_devq_timeout(void *arg)
4336 struct cam_devq *devq;
4338 dev = (struct cam_ed *)arg;
4339 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4340 devq = dev->sim->devq;
4341 mtx_assert(&devq->send_mtx, MA_OWNED);
4342 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4347 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4350 struct cam_devq *devq;
4352 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4355 devq = dev->sim->devq;
4356 mtx_lock(&devq->send_mtx);
4357 if (xpt_release_devq_device(dev, count, run_queue))
4358 xpt_run_devq(dev->sim->devq);
4359 mtx_unlock(&devq->send_mtx);
4363 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4366 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4367 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4368 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4369 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4370 if (count > dev->ccbq.queue.qfrozen_cnt) {
4372 printf("xpt_release_devq(): requested %u > present %u\n",
4373 count, dev->ccbq.queue.qfrozen_cnt);
4375 count = dev->ccbq.queue.qfrozen_cnt;
4377 dev->ccbq.queue.qfrozen_cnt -= count;
4378 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4380 * No longer need to wait for a successful
4381 * command completion.
4383 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4385 * Remove any timeouts that might be scheduled
4386 * to release this queue.
4388 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4389 callout_stop(&dev->callout);
4390 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4393 * Now that we are unfrozen schedule the
4394 * device so any pending transactions are
4397 xpt_schedule_devq(dev->sim->devq, dev);
4404 xpt_release_simq(struct cam_sim *sim, int run_queue)
4406 struct cam_devq *devq;
4409 mtx_lock(&devq->send_mtx);
4410 if (devq->send_queue.qfrozen_cnt <= 0) {
4412 printf("xpt_release_simq: requested 1 > present %u\n",
4413 devq->send_queue.qfrozen_cnt);
4416 devq->send_queue.qfrozen_cnt--;
4417 if (devq->send_queue.qfrozen_cnt == 0) {
4419 * If there is a timeout scheduled to release this
4420 * sim queue, remove it. The queue frozen count is
4423 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4424 callout_stop(&sim->callout);
4425 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4429 * Now that we are unfrozen run the send queue.
4431 xpt_run_devq(sim->devq);
4434 mtx_unlock(&devq->send_mtx);
4438 * XXX Appears to be unused.
4441 xpt_release_simq_timeout(void *arg)
4443 struct cam_sim *sim;
4445 sim = (struct cam_sim *)arg;
4446 xpt_release_simq(sim, /* run_queue */ TRUE);
4450 xpt_done(union ccb *done_ccb)
4452 struct cam_doneq *queue;
4455 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4456 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4459 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4460 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4461 queue = &cam_doneqs[hash];
4462 mtx_lock(&queue->cam_doneq_mtx);
4463 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4464 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4465 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4466 mtx_unlock(&queue->cam_doneq_mtx);
4468 wakeup(&queue->cam_doneq);
4472 xpt_done_direct(union ccb *done_ccb)
4475 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done_direct\n"));
4476 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4479 xpt_done_process(&done_ccb->ccb_h);
4487 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4492 xpt_alloc_ccb_nowait()
4496 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4501 xpt_free_ccb(union ccb *free_ccb)
4503 free(free_ccb, M_CAMCCB);
4508 /* Private XPT functions */
4511 * Get a CAM control block for the caller. Charge the structure to the device
4512 * referenced by the path. If we don't have sufficient resources to allocate
4513 * more ccbs, we return NULL.
4516 xpt_get_ccb_nowait(struct cam_periph *periph)
4520 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_NOWAIT);
4521 if (new_ccb == NULL)
4523 periph->periph_allocated++;
4524 cam_ccbq_take_opening(&periph->path->device->ccbq);
4529 xpt_get_ccb(struct cam_periph *periph)
4533 cam_periph_unlock(periph);
4534 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_WAITOK);
4535 cam_periph_lock(periph);
4536 periph->periph_allocated++;
4537 cam_ccbq_take_opening(&periph->path->device->ccbq);
4542 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4544 struct ccb_hdr *ccb_h;
4546 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4547 cam_periph_assert(periph, MA_OWNED);
4548 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4549 ccb_h->pinfo.priority != priority) {
4550 if (priority < periph->immediate_priority) {
4551 periph->immediate_priority = priority;
4552 xpt_run_allocq(periph, 0);
4554 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4557 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4558 return ((union ccb *)ccb_h);
4562 xpt_acquire_bus(struct cam_eb *bus)
4571 xpt_release_bus(struct cam_eb *bus)
4575 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4576 if (--bus->refcount > 0) {
4580 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4581 xsoftc.bus_generation++;
4583 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4584 ("destroying bus, but target list is not empty"));
4585 cam_sim_release(bus->sim);
4586 mtx_destroy(&bus->eb_mtx);
4587 free(bus, M_CAMXPT);
4590 static struct cam_et *
4591 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4593 struct cam_et *cur_target, *target;
4595 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4596 mtx_assert(&bus->eb_mtx, MA_OWNED);
4597 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4602 TAILQ_INIT(&target->ed_entries);
4604 target->target_id = target_id;
4605 target->refcount = 1;
4606 target->generation = 0;
4607 target->luns = NULL;
4608 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4609 timevalclear(&target->last_reset);
4611 * Hold a reference to our parent bus so it
4612 * will not go away before we do.
4616 /* Insertion sort into our bus's target list */
4617 cur_target = TAILQ_FIRST(&bus->et_entries);
4618 while (cur_target != NULL && cur_target->target_id < target_id)
4619 cur_target = TAILQ_NEXT(cur_target, links);
4620 if (cur_target != NULL) {
4621 TAILQ_INSERT_BEFORE(cur_target, target, links);
4623 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4630 xpt_acquire_target(struct cam_et *target)
4632 struct cam_eb *bus = target->bus;
4634 mtx_lock(&bus->eb_mtx);
4636 mtx_unlock(&bus->eb_mtx);
4640 xpt_release_target(struct cam_et *target)
4642 struct cam_eb *bus = target->bus;
4644 mtx_lock(&bus->eb_mtx);
4645 if (--target->refcount > 0) {
4646 mtx_unlock(&bus->eb_mtx);
4649 TAILQ_REMOVE(&bus->et_entries, target, links);
4651 mtx_unlock(&bus->eb_mtx);
4652 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4653 ("destroying target, but device list is not empty"));
4654 xpt_release_bus(bus);
4655 mtx_destroy(&target->luns_mtx);
4657 free(target->luns, M_CAMXPT);
4658 free(target, M_CAMXPT);
4661 static struct cam_ed *
4662 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4665 struct cam_ed *device;
4667 device = xpt_alloc_device(bus, target, lun_id);
4671 device->mintags = 1;
4672 device->maxtags = 1;
4677 xpt_destroy_device(void *context, int pending)
4679 struct cam_ed *device = context;
4681 mtx_lock(&device->device_mtx);
4682 mtx_destroy(&device->device_mtx);
4683 free(device, M_CAMDEV);
4687 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4689 struct cam_ed *cur_device, *device;
4690 struct cam_devq *devq;
4693 mtx_assert(&bus->eb_mtx, MA_OWNED);
4694 /* Make space for us in the device queue on our bus */
4695 devq = bus->sim->devq;
4696 mtx_lock(&devq->send_mtx);
4697 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4698 mtx_unlock(&devq->send_mtx);
4699 if (status != CAM_REQ_CMP)
4702 device = (struct cam_ed *)malloc(sizeof(*device),
4703 M_CAMDEV, M_NOWAIT|M_ZERO);
4707 cam_init_pinfo(&device->devq_entry);
4708 device->target = target;
4709 device->lun_id = lun_id;
4710 device->sim = bus->sim;
4711 if (cam_ccbq_init(&device->ccbq,
4712 bus->sim->max_dev_openings) != 0) {
4713 free(device, M_CAMDEV);
4716 SLIST_INIT(&device->asyncs);
4717 SLIST_INIT(&device->periphs);
4718 device->generation = 0;
4719 device->flags = CAM_DEV_UNCONFIGURED;
4720 device->tag_delay_count = 0;
4721 device->tag_saved_openings = 0;
4722 device->refcount = 1;
4723 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4724 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4725 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4727 * Hold a reference to our parent bus so it
4728 * will not go away before we do.
4732 cur_device = TAILQ_FIRST(&target->ed_entries);
4733 while (cur_device != NULL && cur_device->lun_id < lun_id)
4734 cur_device = TAILQ_NEXT(cur_device, links);
4735 if (cur_device != NULL)
4736 TAILQ_INSERT_BEFORE(cur_device, device, links);
4738 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4739 target->generation++;
4744 xpt_acquire_device(struct cam_ed *device)
4746 struct cam_eb *bus = device->target->bus;
4748 mtx_lock(&bus->eb_mtx);
4750 mtx_unlock(&bus->eb_mtx);
4754 xpt_release_device(struct cam_ed *device)
4756 struct cam_eb *bus = device->target->bus;
4757 struct cam_devq *devq;
4759 mtx_lock(&bus->eb_mtx);
4760 if (--device->refcount > 0) {
4761 mtx_unlock(&bus->eb_mtx);
4765 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4766 device->target->generation++;
4767 mtx_unlock(&bus->eb_mtx);
4769 /* Release our slot in the devq */
4770 devq = bus->sim->devq;
4771 mtx_lock(&devq->send_mtx);
4772 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4773 mtx_unlock(&devq->send_mtx);
4775 KASSERT(SLIST_EMPTY(&device->periphs),
4776 ("destroying device, but periphs list is not empty"));
4777 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4778 ("destroying device while still queued for ccbs"));
4780 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4781 callout_stop(&device->callout);
4783 xpt_release_target(device->target);
4785 cam_ccbq_fini(&device->ccbq);
4787 * Free allocated memory. free(9) does nothing if the
4788 * supplied pointer is NULL, so it is safe to call without
4791 free(device->supported_vpds, M_CAMXPT);
4792 free(device->device_id, M_CAMXPT);
4793 free(device->physpath, M_CAMXPT);
4794 free(device->rcap_buf, M_CAMXPT);
4795 free(device->serial_num, M_CAMXPT);
4796 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4800 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4806 mtx_lock(&dev->sim->devq->send_mtx);
4807 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4808 mtx_unlock(&dev->sim->devq->send_mtx);
4809 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4810 || (dev->inq_flags & SID_CmdQue) != 0)
4811 dev->tag_saved_openings = newopenings;
4815 static struct cam_eb *
4816 xpt_find_bus(path_id_t path_id)
4821 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4823 bus = TAILQ_NEXT(bus, links)) {
4824 if (bus->path_id == path_id) {
4833 static struct cam_et *
4834 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4836 struct cam_et *target;
4838 mtx_assert(&bus->eb_mtx, MA_OWNED);
4839 for (target = TAILQ_FIRST(&bus->et_entries);
4841 target = TAILQ_NEXT(target, links)) {
4842 if (target->target_id == target_id) {
4850 static struct cam_ed *
4851 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4853 struct cam_ed *device;
4855 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4856 for (device = TAILQ_FIRST(&target->ed_entries);
4858 device = TAILQ_NEXT(device, links)) {
4859 if (device->lun_id == lun_id) {
4868 xpt_start_tags(struct cam_path *path)
4870 struct ccb_relsim crs;
4871 struct cam_ed *device;
4872 struct cam_sim *sim;
4875 device = path->device;
4876 sim = path->bus->sim;
4877 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4878 xpt_freeze_devq(path, /*count*/1);
4879 device->inq_flags |= SID_CmdQue;
4880 if (device->tag_saved_openings != 0)
4881 newopenings = device->tag_saved_openings;
4883 newopenings = min(device->maxtags,
4884 sim->max_tagged_dev_openings);
4885 xpt_dev_ccbq_resize(path, newopenings);
4886 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4887 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4888 crs.ccb_h.func_code = XPT_REL_SIMQ;
4889 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4891 = crs.release_timeout
4894 xpt_action((union ccb *)&crs);
4898 xpt_stop_tags(struct cam_path *path)
4900 struct ccb_relsim crs;
4901 struct cam_ed *device;
4902 struct cam_sim *sim;
4904 device = path->device;
4905 sim = path->bus->sim;
4906 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4907 device->tag_delay_count = 0;
4908 xpt_freeze_devq(path, /*count*/1);
4909 device->inq_flags &= ~SID_CmdQue;
4910 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4911 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4912 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4913 crs.ccb_h.func_code = XPT_REL_SIMQ;
4914 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4916 = crs.release_timeout
4919 xpt_action((union ccb *)&crs);
4923 xpt_boot_delay(void *arg)
4930 xpt_config(void *arg)
4933 * Now that interrupts are enabled, go find our devices
4935 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4936 printf("xpt_config: failed to create taskqueue thread.\n");
4938 /* Setup debugging path */
4939 if (cam_dflags != CAM_DEBUG_NONE) {
4940 if (xpt_create_path(&cam_dpath, NULL,
4941 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4942 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4943 printf("xpt_config: xpt_create_path() failed for debug"
4944 " target %d:%d:%d, debugging disabled\n",
4945 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4946 cam_dflags = CAM_DEBUG_NONE;
4951 periphdriver_init(1);
4953 callout_init(&xsoftc.boot_callout, 1);
4954 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
4955 xpt_boot_delay, NULL, 0);
4956 /* Fire up rescan thread. */
4957 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4958 "cam", "scanner")) {
4959 printf("xpt_config: failed to create rescan thread.\n");
4967 xsoftc.buses_to_config++;
4972 xpt_release_boot(void)
4975 xsoftc.buses_to_config--;
4976 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4977 struct xpt_task *task;
4979 xsoftc.buses_config_done = 1;
4981 /* Call manually because we don't have any busses */
4982 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4984 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4985 taskqueue_enqueue(taskqueue_thread, &task->task);
4992 * If the given device only has one peripheral attached to it, and if that
4993 * peripheral is the passthrough driver, announce it. This insures that the
4994 * user sees some sort of announcement for every peripheral in their system.
4997 xptpassannouncefunc(struct cam_ed *device, void *arg)
4999 struct cam_periph *periph;
5002 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5003 periph = SLIST_NEXT(periph, periph_links), i++);
5005 periph = SLIST_FIRST(&device->periphs);
5007 && (strncmp(periph->periph_name, "pass", 4) == 0))
5008 xpt_announce_periph(periph, NULL);
5014 xpt_finishconfig_task(void *context, int pending)
5017 periphdriver_init(2);
5019 * Check for devices with no "standard" peripheral driver
5020 * attached. For any devices like that, announce the
5021 * passthrough driver so the user will see something.
5024 xpt_for_all_devices(xptpassannouncefunc, NULL);
5026 /* Release our hook so that the boot can continue. */
5027 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5028 free(xsoftc.xpt_config_hook, M_CAMXPT);
5029 xsoftc.xpt_config_hook = NULL;
5031 free(context, M_CAMXPT);
5035 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5036 struct cam_path *path)
5038 struct ccb_setasync csa;
5043 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5044 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5045 if (status != CAM_REQ_CMP)
5047 xpt_path_lock(path);
5051 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5052 csa.ccb_h.func_code = XPT_SASYNC_CB;
5053 csa.event_enable = event;
5054 csa.callback = cbfunc;
5055 csa.callback_arg = cbarg;
5056 xpt_action((union ccb *)&csa);
5057 status = csa.ccb_h.status;
5060 xpt_path_unlock(path);
5061 xpt_free_path(path);
5064 if ((status == CAM_REQ_CMP) &&
5065 (csa.event_enable & AC_FOUND_DEVICE)) {
5067 * Get this peripheral up to date with all
5068 * the currently existing devices.
5070 xpt_for_all_devices(xptsetasyncfunc, &csa);
5072 if ((status == CAM_REQ_CMP) &&
5073 (csa.event_enable & AC_PATH_REGISTERED)) {
5075 * Get this peripheral up to date with all
5076 * the currently existing busses.
5078 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5085 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5087 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5089 switch (work_ccb->ccb_h.func_code) {
5090 /* Common cases first */
5091 case XPT_PATH_INQ: /* Path routing inquiry */
5093 struct ccb_pathinq *cpi;
5095 cpi = &work_ccb->cpi;
5096 cpi->version_num = 1; /* XXX??? */
5097 cpi->hba_inquiry = 0;
5098 cpi->target_sprt = 0;
5100 cpi->hba_eng_cnt = 0;
5101 cpi->max_target = 0;
5103 cpi->initiator_id = 0;
5104 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5105 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5106 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5107 cpi->unit_number = sim->unit_number;
5108 cpi->bus_id = sim->bus_id;
5109 cpi->base_transfer_speed = 0;
5110 cpi->protocol = PROTO_UNSPECIFIED;
5111 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5112 cpi->transport = XPORT_UNSPECIFIED;
5113 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5114 cpi->ccb_h.status = CAM_REQ_CMP;
5119 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5126 * The xpt as a "controller" has no interrupt sources, so polling
5130 xptpoll(struct cam_sim *sim)
5135 xpt_lock_buses(void)
5137 mtx_lock(&xsoftc.xpt_topo_lock);
5141 xpt_unlock_buses(void)
5143 mtx_unlock(&xsoftc.xpt_topo_lock);
5147 xpt_path_mtx(struct cam_path *path)
5150 return (&path->device->device_mtx);
5154 xpt_done_process(struct ccb_hdr *ccb_h)
5156 struct cam_sim *sim;
5157 struct cam_devq *devq;
5158 struct mtx *mtx = NULL;
5160 if (ccb_h->flags & CAM_HIGH_POWER) {
5161 struct highpowerlist *hphead;
5162 struct cam_ed *device;
5164 mtx_lock(&xsoftc.xpt_highpower_lock);
5165 hphead = &xsoftc.highpowerq;
5167 device = STAILQ_FIRST(hphead);
5170 * Increment the count since this command is done.
5172 xsoftc.num_highpower++;
5175 * Any high powered commands queued up?
5177 if (device != NULL) {
5179 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5180 mtx_unlock(&xsoftc.xpt_highpower_lock);
5182 mtx_lock(&device->sim->devq->send_mtx);
5183 xpt_release_devq_device(device,
5184 /*count*/1, /*runqueue*/TRUE);
5185 mtx_unlock(&device->sim->devq->send_mtx);
5187 mtx_unlock(&xsoftc.xpt_highpower_lock);
5190 sim = ccb_h->path->bus->sim;
5192 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5193 xpt_release_simq(sim, /*run_queue*/FALSE);
5194 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5197 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5198 && (ccb_h->status & CAM_DEV_QFRZN)) {
5199 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5200 ccb_h->status &= ~CAM_DEV_QFRZN;
5204 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5205 struct cam_ed *dev = ccb_h->path->device;
5207 mtx_lock(&devq->send_mtx);
5208 devq->send_active--;
5209 devq->send_openings++;
5210 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5212 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5213 && (dev->ccbq.dev_active == 0))) {
5214 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5215 xpt_release_devq_device(dev, /*count*/1,
5216 /*run_queue*/FALSE);
5219 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5220 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5221 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5222 xpt_release_devq_device(dev, /*count*/1,
5223 /*run_queue*/FALSE);
5226 if (!device_is_queued(dev))
5227 (void)xpt_schedule_devq(devq, dev);
5229 mtx_unlock(&devq->send_mtx);
5231 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5232 mtx = xpt_path_mtx(ccb_h->path);
5235 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5236 && (--dev->tag_delay_count == 0))
5237 xpt_start_tags(ccb_h->path);
5241 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5243 mtx = xpt_path_mtx(ccb_h->path);
5253 /* Call the peripheral driver's callback */
5254 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5255 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5261 xpt_done_td(void *arg)
5263 struct cam_doneq *queue = arg;
5264 struct ccb_hdr *ccb_h;
5265 STAILQ_HEAD(, ccb_hdr) doneq;
5267 STAILQ_INIT(&doneq);
5268 mtx_lock(&queue->cam_doneq_mtx);
5270 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5271 queue->cam_doneq_sleep = 1;
5272 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5274 queue->cam_doneq_sleep = 0;
5276 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5277 mtx_unlock(&queue->cam_doneq_mtx);
5279 THREAD_NO_SLEEPING();
5280 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5281 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5282 xpt_done_process(ccb_h);
5284 THREAD_SLEEPING_OK();
5286 mtx_lock(&queue->cam_doneq_mtx);
5291 camisr_runqueue(void)
5293 struct ccb_hdr *ccb_h;
5294 struct cam_doneq *queue;
5297 /* Process global queues. */
5298 for (i = 0; i < cam_num_doneqs; i++) {
5299 queue = &cam_doneqs[i];
5300 mtx_lock(&queue->cam_doneq_mtx);
5301 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5302 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5303 mtx_unlock(&queue->cam_doneq_mtx);
5304 xpt_done_process(ccb_h);
5305 mtx_lock(&queue->cam_doneq_mtx);
5307 mtx_unlock(&queue->cam_doneq_mtx);