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 "opt_printf.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/interrupt.h>
49 #include <sys/taskqueue.h>
52 #include <sys/mutex.h>
53 #include <sys/sysctl.h>
54 #include <sys/kthread.h>
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_queue.h>
60 #include <cam/cam_sim.h>
61 #include <cam/cam_xpt.h>
62 #include <cam/cam_xpt_sim.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_xpt_internal.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_compat.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_message.h>
70 #include <cam/scsi/scsi_pass.h>
72 #include <machine/md_var.h> /* geometry translation */
73 #include <machine/stdarg.h> /* for xpt_print below */
77 /* Wild guess based on not wanting to grow the stack too much */
78 #define XPT_PRINT_MAXLEN 512
79 #ifdef PRINTF_BUFR_SIZE
80 #define XPT_PRINT_LEN PRINTF_BUFR_SIZE
82 #define XPT_PRINT_LEN 128
84 _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
87 * This is the maximum number of high powered commands (e.g. start unit)
88 * that can be outstanding at a particular time.
90 #ifndef CAM_MAX_HIGHPOWER
91 #define CAM_MAX_HIGHPOWER 4
94 /* Datastructures internal to the xpt layer */
95 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
96 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
97 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
98 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
100 /* Object for defering XPT actions to a taskqueue */
108 uint32_t xpt_generation;
110 /* number of high powered commands that can go through right now */
111 struct mtx xpt_highpower_lock;
112 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
115 /* queue for handling async rescan requests. */
116 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
118 int buses_config_done;
123 * N.B., "busses" is an archaic spelling of "buses". In new code
124 * "buses" is preferred.
126 TAILQ_HEAD(,cam_eb) xpt_busses;
127 u_int bus_generation;
129 struct intr_config_hook *xpt_config_hook;
132 struct callout boot_callout;
134 struct mtx xpt_topo_lock;
136 struct taskqueue *xpt_taskq;
141 DM_RET_FLAG_MASK = 0x0f,
144 DM_RET_DESCEND = 0x20,
146 DM_RET_ACTION_MASK = 0xf0
154 } xpt_traverse_depth;
156 struct xpt_traverse_config {
157 xpt_traverse_depth depth;
162 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
163 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
164 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
165 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
166 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
168 /* Transport layer configuration information */
169 static struct xpt_softc xsoftc;
171 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
173 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
174 &xsoftc.boot_delay, 0, "Bus registration wait time");
175 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
176 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
179 struct mtx_padalign cam_doneq_mtx;
180 STAILQ_HEAD(, ccb_hdr) cam_doneq;
184 static struct cam_doneq cam_doneqs[MAXCPU];
185 static int cam_num_doneqs;
186 static struct proc *cam_proc;
188 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
189 &cam_num_doneqs, 0, "Number of completion queues/threads");
191 struct cam_periph *xpt_periph;
193 static periph_init_t xpt_periph_init;
195 static struct periph_driver xpt_driver =
197 xpt_periph_init, "xpt",
198 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
202 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
204 static d_open_t xptopen;
205 static d_close_t xptclose;
206 static d_ioctl_t xptioctl;
207 static d_ioctl_t xptdoioctl;
209 static struct cdevsw xpt_cdevsw = {
210 .d_version = D_VERSION,
218 /* Storage for debugging datastructures */
219 struct cam_path *cam_dpath;
220 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
221 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
222 &cam_dflags, 0, "Enabled debug flags");
223 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
224 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
225 &cam_debug_delay, 0, "Delay in us after each debug message");
227 /* Our boot-time initialization hook */
228 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
230 static moduledata_t cam_moduledata = {
232 cam_module_event_handler,
236 static int xpt_init(void *);
238 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
239 MODULE_VERSION(cam, 1);
242 static void xpt_async_bcast(struct async_list *async_head,
243 u_int32_t async_code,
244 struct cam_path *path,
246 static path_id_t xptnextfreepathid(void);
247 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
248 static union ccb *xpt_get_ccb(struct cam_periph *periph);
249 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
250 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
251 static void xpt_run_allocq_task(void *context, int pending);
252 static void xpt_run_devq(struct cam_devq *devq);
253 static timeout_t xpt_release_devq_timeout;
254 static void xpt_release_simq_timeout(void *arg) __unused;
255 static void xpt_acquire_bus(struct cam_eb *bus);
256 static void xpt_release_bus(struct cam_eb *bus);
257 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
258 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
260 static struct cam_et*
261 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
262 static void xpt_acquire_target(struct cam_et *target);
263 static void xpt_release_target(struct cam_et *target);
264 static struct cam_eb*
265 xpt_find_bus(path_id_t path_id);
266 static struct cam_et*
267 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
268 static struct cam_ed*
269 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
270 static void xpt_config(void *arg);
271 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
272 u_int32_t new_priority);
273 static xpt_devicefunc_t xptpassannouncefunc;
274 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
275 static void xptpoll(struct cam_sim *sim);
276 static void camisr_runqueue(void);
277 static void xpt_done_process(struct ccb_hdr *ccb_h);
278 static void xpt_done_td(void *);
279 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
280 u_int num_patterns, struct cam_eb *bus);
281 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
283 struct cam_ed *device);
284 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
286 struct cam_periph *periph);
287 static xpt_busfunc_t xptedtbusfunc;
288 static xpt_targetfunc_t xptedttargetfunc;
289 static xpt_devicefunc_t xptedtdevicefunc;
290 static xpt_periphfunc_t xptedtperiphfunc;
291 static xpt_pdrvfunc_t xptplistpdrvfunc;
292 static xpt_periphfunc_t xptplistperiphfunc;
293 static int xptedtmatch(struct ccb_dev_match *cdm);
294 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
295 static int xptbustraverse(struct cam_eb *start_bus,
296 xpt_busfunc_t *tr_func, void *arg);
297 static int xpttargettraverse(struct cam_eb *bus,
298 struct cam_et *start_target,
299 xpt_targetfunc_t *tr_func, void *arg);
300 static int xptdevicetraverse(struct cam_et *target,
301 struct cam_ed *start_device,
302 xpt_devicefunc_t *tr_func, void *arg);
303 static int xptperiphtraverse(struct cam_ed *device,
304 struct cam_periph *start_periph,
305 xpt_periphfunc_t *tr_func, void *arg);
306 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
307 xpt_pdrvfunc_t *tr_func, void *arg);
308 static int xptpdperiphtraverse(struct periph_driver **pdrv,
309 struct cam_periph *start_periph,
310 xpt_periphfunc_t *tr_func,
312 static xpt_busfunc_t xptdefbusfunc;
313 static xpt_targetfunc_t xptdeftargetfunc;
314 static xpt_devicefunc_t xptdefdevicefunc;
315 static xpt_periphfunc_t xptdefperiphfunc;
316 static void xpt_finishconfig_task(void *context, int pending);
317 static void xpt_dev_async_default(u_int32_t async_code,
319 struct cam_et *target,
320 struct cam_ed *device,
322 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
323 struct cam_et *target,
325 static xpt_devicefunc_t xptsetasyncfunc;
326 static xpt_busfunc_t xptsetasyncbusfunc;
327 static cam_status xptregister(struct cam_periph *periph,
329 static const char * xpt_action_name(uint32_t action);
330 static __inline int device_is_queued(struct cam_ed *device);
333 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
337 mtx_assert(&devq->send_mtx, MA_OWNED);
338 if ((dev->ccbq.queue.entries > 0) &&
339 (dev->ccbq.dev_openings > 0) &&
340 (dev->ccbq.queue.qfrozen_cnt == 0)) {
342 * The priority of a device waiting for controller
343 * resources is that of the highest priority CCB
347 xpt_schedule_dev(&devq->send_queue,
349 CAMQ_GET_PRIO(&dev->ccbq.queue));
357 device_is_queued(struct cam_ed *device)
359 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
365 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
369 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
373 * Only allow read-write access.
375 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
379 * We don't allow nonblocking access.
381 if ((flags & O_NONBLOCK) != 0) {
382 printf("%s: can't do nonblocking access\n", devtoname(dev));
390 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
397 * Don't automatically grab the xpt softc lock here even though this is going
398 * through the xpt device. The xpt device is really just a back door for
399 * accessing other devices and SIMs, so the right thing to do is to grab
400 * the appropriate SIM lock once the bus/SIM is located.
403 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
407 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
408 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
414 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
422 * For the transport layer CAMIOCOMMAND ioctl, we really only want
423 * to accept CCB types that don't quite make sense to send through a
424 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
432 inccb = (union ccb *)addr;
433 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
434 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
435 inccb->csio.bio = NULL;
438 if (inccb->ccb_h.flags & CAM_UNLOCKED)
441 bus = xpt_find_bus(inccb->ccb_h.path_id);
445 switch (inccb->ccb_h.func_code) {
448 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
449 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
450 xpt_release_bus(bus);
455 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
456 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
457 xpt_release_bus(bus);
465 switch(inccb->ccb_h.func_code) {
473 ccb = xpt_alloc_ccb();
476 * Create a path using the bus, target, and lun the
479 if (xpt_create_path(&ccb->ccb_h.path, NULL,
480 inccb->ccb_h.path_id,
481 inccb->ccb_h.target_id,
482 inccb->ccb_h.target_lun) !=
488 /* Ensure all of our fields are correct */
489 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
490 inccb->ccb_h.pinfo.priority);
491 xpt_merge_ccb(ccb, inccb);
492 xpt_path_lock(ccb->ccb_h.path);
493 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
494 xpt_path_unlock(ccb->ccb_h.path);
495 bcopy(ccb, inccb, sizeof(union ccb));
496 xpt_free_path(ccb->ccb_h.path);
504 * This is an immediate CCB, so it's okay to
505 * allocate it on the stack.
509 * Create a path using the bus, target, and lun the
512 if (xpt_create_path(&ccb.ccb_h.path, NULL,
513 inccb->ccb_h.path_id,
514 inccb->ccb_h.target_id,
515 inccb->ccb_h.target_lun) !=
520 /* Ensure all of our fields are correct */
521 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
522 inccb->ccb_h.pinfo.priority);
523 xpt_merge_ccb(&ccb, inccb);
525 bcopy(&ccb, inccb, sizeof(union ccb));
526 xpt_free_path(ccb.ccb_h.path);
530 case XPT_DEV_MATCH: {
531 struct cam_periph_map_info mapinfo;
532 struct cam_path *old_path;
535 * We can't deal with physical addresses for this
536 * type of transaction.
538 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
545 * Save this in case the caller had it set to
546 * something in particular.
548 old_path = inccb->ccb_h.path;
551 * We really don't need a path for the matching
552 * code. The path is needed because of the
553 * debugging statements in xpt_action(). They
554 * assume that the CCB has a valid path.
556 inccb->ccb_h.path = xpt_periph->path;
558 bzero(&mapinfo, sizeof(mapinfo));
561 * Map the pattern and match buffers into kernel
562 * virtual address space.
564 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
567 inccb->ccb_h.path = old_path;
572 * This is an immediate CCB, we can send it on directly.
577 * Map the buffers back into user space.
579 cam_periph_unmapmem(inccb, &mapinfo);
581 inccb->ccb_h.path = old_path;
590 xpt_release_bus(bus);
594 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
595 * with the periphal driver name and unit name filled in. The other
596 * fields don't really matter as input. The passthrough driver name
597 * ("pass"), and unit number are passed back in the ccb. The current
598 * device generation number, and the index into the device peripheral
599 * driver list, and the status are also passed back. Note that
600 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
601 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
602 * (or rather should be) impossible for the device peripheral driver
603 * list to change since we look at the whole thing in one pass, and
604 * we do it with lock protection.
607 case CAMGETPASSTHRU: {
609 struct cam_periph *periph;
610 struct periph_driver **p_drv;
613 int base_periph_found;
615 ccb = (union ccb *)addr;
616 unit = ccb->cgdl.unit_number;
617 name = ccb->cgdl.periph_name;
618 base_periph_found = 0;
619 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
620 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
621 ccb->csio.bio = NULL;
625 * Sanity check -- make sure we don't get a null peripheral
628 if (*ccb->cgdl.periph_name == '\0') {
633 /* Keep the list from changing while we traverse it */
636 /* first find our driver in the list of drivers */
637 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
638 if (strcmp((*p_drv)->driver_name, name) == 0)
641 if (*p_drv == NULL) {
643 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
644 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
645 *ccb->cgdl.periph_name = '\0';
646 ccb->cgdl.unit_number = 0;
652 * Run through every peripheral instance of this driver
653 * and check to see whether it matches the unit passed
654 * in by the user. If it does, get out of the loops and
655 * find the passthrough driver associated with that
658 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
659 periph = TAILQ_NEXT(periph, unit_links)) {
661 if (periph->unit_number == unit)
665 * If we found the peripheral driver that the user passed
666 * in, go through all of the peripheral drivers for that
667 * particular device and look for a passthrough driver.
669 if (periph != NULL) {
670 struct cam_ed *device;
673 base_periph_found = 1;
674 device = periph->path->device;
675 for (i = 0, periph = SLIST_FIRST(&device->periphs);
677 periph = SLIST_NEXT(periph, periph_links), i++) {
679 * Check to see whether we have a
680 * passthrough device or not.
682 if (strcmp(periph->periph_name, "pass") == 0) {
684 * Fill in the getdevlist fields.
686 strcpy(ccb->cgdl.periph_name,
687 periph->periph_name);
688 ccb->cgdl.unit_number =
690 if (SLIST_NEXT(periph, periph_links))
692 CAM_GDEVLIST_MORE_DEVS;
695 CAM_GDEVLIST_LAST_DEVICE;
696 ccb->cgdl.generation =
700 * Fill in some CCB header fields
701 * that the user may want.
704 periph->path->bus->path_id;
705 ccb->ccb_h.target_id =
706 periph->path->target->target_id;
707 ccb->ccb_h.target_lun =
708 periph->path->device->lun_id;
709 ccb->ccb_h.status = CAM_REQ_CMP;
716 * If the periph is null here, one of two things has
717 * happened. The first possibility is that we couldn't
718 * find the unit number of the particular peripheral driver
719 * that the user is asking about. e.g. the user asks for
720 * the passthrough driver for "da11". We find the list of
721 * "da" peripherals all right, but there is no unit 11.
722 * The other possibility is that we went through the list
723 * of peripheral drivers attached to the device structure,
724 * but didn't find one with the name "pass". Either way,
725 * we return ENOENT, since we couldn't find something.
727 if (periph == NULL) {
728 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
729 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
730 *ccb->cgdl.periph_name = '\0';
731 ccb->cgdl.unit_number = 0;
734 * It is unfortunate that this is even necessary,
735 * but there are many, many clueless users out there.
736 * If this is true, the user is looking for the
737 * passthrough driver, but doesn't have one in his
740 if (base_periph_found == 1) {
741 printf("xptioctl: pass driver is not in the "
743 printf("xptioctl: put \"device pass\" in "
744 "your kernel config file\n");
759 cam_module_event_handler(module_t mod, int what, void *arg)
765 if ((error = xpt_init(NULL)) != 0)
777 static struct xpt_proto *
778 xpt_proto_find(cam_proto proto)
780 struct xpt_proto **pp;
782 SET_FOREACH(pp, cam_xpt_proto_set) {
783 if ((*pp)->proto == proto)
791 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
794 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
795 xpt_free_path(done_ccb->ccb_h.path);
796 xpt_free_ccb(done_ccb);
798 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
799 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
804 /* thread to handle bus rescans */
806 xpt_scanner_thread(void *dummy)
809 struct cam_path path;
813 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
814 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
816 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
817 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
821 * Since lock can be dropped inside and path freed
822 * by completion callback even before return here,
823 * take our own path copy for reference.
825 xpt_copy_path(&path, ccb->ccb_h.path);
826 xpt_path_lock(&path);
828 xpt_path_unlock(&path);
829 xpt_release_path(&path);
837 xpt_rescan(union ccb *ccb)
841 /* Prepare request */
842 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
843 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
844 ccb->ccb_h.func_code = XPT_SCAN_BUS;
845 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
846 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
847 ccb->ccb_h.func_code = XPT_SCAN_TGT;
848 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
849 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
850 ccb->ccb_h.func_code = XPT_SCAN_LUN;
852 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
853 xpt_free_path(ccb->ccb_h.path);
857 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
858 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
859 xpt_action_name(ccb->ccb_h.func_code)));
861 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
862 ccb->ccb_h.cbfcnp = xpt_rescan_done;
863 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
864 /* Don't make duplicate entries for the same paths. */
866 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
867 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
868 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
869 wakeup(&xsoftc.ccb_scanq);
871 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
872 xpt_free_path(ccb->ccb_h.path);
878 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
879 xsoftc.buses_to_config++;
880 wakeup(&xsoftc.ccb_scanq);
884 /* Functions accessed by the peripheral drivers */
886 xpt_init(void *dummy)
888 struct cam_sim *xpt_sim;
889 struct cam_path *path;
890 struct cam_devq *devq;
894 TAILQ_INIT(&xsoftc.xpt_busses);
895 TAILQ_INIT(&xsoftc.ccb_scanq);
896 STAILQ_INIT(&xsoftc.highpowerq);
897 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
899 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
900 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
901 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
902 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
904 #ifdef CAM_BOOT_DELAY
906 * Override this value at compile time to assist our users
907 * who don't use loader to boot a kernel.
909 xsoftc.boot_delay = CAM_BOOT_DELAY;
912 * The xpt layer is, itself, the equivalent of a SIM.
913 * Allow 16 ccbs in the ccb pool for it. This should
914 * give decent parallelism when we probe buses and
915 * perform other XPT functions.
917 devq = cam_simq_alloc(16);
918 xpt_sim = cam_sim_alloc(xptaction,
923 /*mtx*/&xsoftc.xpt_lock,
924 /*max_dev_transactions*/0,
925 /*max_tagged_dev_transactions*/0,
930 mtx_lock(&xsoftc.xpt_lock);
931 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
932 mtx_unlock(&xsoftc.xpt_lock);
933 printf("xpt_init: xpt_bus_register failed with status %#x,"
934 " failing attach\n", status);
937 mtx_unlock(&xsoftc.xpt_lock);
940 * Looking at the XPT from the SIM layer, the XPT is
941 * the equivalent of a peripheral driver. Allocate
942 * a peripheral driver entry for us.
944 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
946 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
947 printf("xpt_init: xpt_create_path failed with status %#x,"
948 " failing attach\n", status);
952 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
953 path, NULL, 0, xpt_sim);
954 xpt_path_unlock(path);
957 if (cam_num_doneqs < 1)
958 cam_num_doneqs = 1 + mp_ncpus / 6;
959 else if (cam_num_doneqs > MAXCPU)
960 cam_num_doneqs = MAXCPU;
961 for (i = 0; i < cam_num_doneqs; i++) {
962 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
964 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
965 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
966 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
972 if (cam_num_doneqs < 1) {
973 printf("xpt_init: Cannot init completion queues "
974 "- failing attach\n");
978 * Register a callback for when interrupts are enabled.
980 xsoftc.xpt_config_hook =
981 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
982 M_CAMXPT, M_NOWAIT | M_ZERO);
983 if (xsoftc.xpt_config_hook == NULL) {
984 printf("xpt_init: Cannot malloc config hook "
985 "- failing attach\n");
988 xsoftc.xpt_config_hook->ich_func = xpt_config;
989 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
990 free (xsoftc.xpt_config_hook, M_CAMXPT);
991 printf("xpt_init: config_intrhook_establish failed "
992 "- failing attach\n");
999 xptregister(struct cam_periph *periph, void *arg)
1001 struct cam_sim *xpt_sim;
1003 if (periph == NULL) {
1004 printf("xptregister: periph was NULL!!\n");
1005 return(CAM_REQ_CMP_ERR);
1008 xpt_sim = (struct cam_sim *)arg;
1009 xpt_sim->softc = periph;
1010 xpt_periph = periph;
1011 periph->softc = NULL;
1013 return(CAM_REQ_CMP);
1017 xpt_add_periph(struct cam_periph *periph)
1019 struct cam_ed *device;
1022 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1023 device = periph->path->device;
1024 status = CAM_REQ_CMP;
1025 if (device != NULL) {
1026 mtx_lock(&device->target->bus->eb_mtx);
1027 device->generation++;
1028 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1029 mtx_unlock(&device->target->bus->eb_mtx);
1030 atomic_add_32(&xsoftc.xpt_generation, 1);
1037 xpt_remove_periph(struct cam_periph *periph)
1039 struct cam_ed *device;
1041 device = periph->path->device;
1042 if (device != NULL) {
1043 mtx_lock(&device->target->bus->eb_mtx);
1044 device->generation++;
1045 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1046 mtx_unlock(&device->target->bus->eb_mtx);
1047 atomic_add_32(&xsoftc.xpt_generation, 1);
1053 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1055 struct cam_path *path = periph->path;
1056 struct xpt_proto *proto;
1058 cam_periph_assert(periph, MA_OWNED);
1059 periph->flags |= CAM_PERIPH_ANNOUNCED;
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 proto = xpt_proto_find(path->device->protocol);
1072 proto->ops->announce(path->device);
1074 printf("%s%d: Unknown protocol device %d\n",
1075 periph->periph_name, periph->unit_number,
1076 path->device->protocol);
1077 if (path->device->serial_num_len > 0) {
1078 /* Don't wrap the screen - print only the first 60 chars */
1079 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1080 periph->unit_number, path->device->serial_num);
1082 /* Announce transport details. */
1083 path->bus->xport->ops->announce(periph);
1084 /* Announce command queueing. */
1085 if (path->device->inq_flags & SID_CmdQue
1086 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1087 printf("%s%d: Command Queueing enabled\n",
1088 periph->periph_name, periph->unit_number);
1090 /* Announce caller's details if they've passed in. */
1091 if (announce_string != NULL)
1092 printf("%s%d: %s\n", periph->periph_name,
1093 periph->unit_number, announce_string);
1097 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1100 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1101 periph->unit_number, quirks, bit_string);
1106 xpt_denounce_periph(struct cam_periph *periph)
1108 struct cam_path *path = periph->path;
1109 struct xpt_proto *proto;
1111 cam_periph_assert(periph, MA_OWNED);
1112 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1113 periph->periph_name, periph->unit_number,
1114 path->bus->sim->sim_name,
1115 path->bus->sim->unit_number,
1116 path->bus->sim->bus_id,
1118 path->target->target_id,
1119 (uintmax_t)path->device->lun_id);
1120 printf("%s%d: ", periph->periph_name, periph->unit_number);
1121 proto = xpt_proto_find(path->device->protocol);
1123 proto->ops->denounce(path->device);
1125 printf("%s%d: Unknown protocol device %d\n",
1126 periph->periph_name, periph->unit_number,
1127 path->device->protocol);
1128 if (path->device->serial_num_len > 0)
1129 printf(" s/n %.60s", path->device->serial_num);
1130 printf(" detached\n");
1135 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1138 struct ccb_dev_advinfo cdai;
1139 struct scsi_vpd_id_descriptor *idd;
1141 xpt_path_assert(path, MA_OWNED);
1143 memset(&cdai, 0, sizeof(cdai));
1144 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1145 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1146 cdai.flags = CDAI_FLAG_NONE;
1149 if (!strcmp(attr, "GEOM::ident"))
1150 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1151 else if (!strcmp(attr, "GEOM::physpath"))
1152 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1153 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1154 strcmp(attr, "GEOM::lunname") == 0) {
1155 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1156 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1160 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1161 if (cdai.buf == NULL) {
1165 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1166 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1167 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1168 if (cdai.provsiz == 0)
1170 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1171 if (strcmp(attr, "GEOM::lunid") == 0) {
1172 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1173 cdai.provsiz, scsi_devid_is_lun_naa);
1175 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1176 cdai.provsiz, scsi_devid_is_lun_eui64);
1178 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1179 cdai.provsiz, scsi_devid_is_lun_uuid);
1181 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1182 cdai.provsiz, scsi_devid_is_lun_md5);
1186 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1187 cdai.provsiz, scsi_devid_is_lun_t10);
1189 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1190 cdai.provsiz, scsi_devid_is_lun_name);
1194 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1195 if (idd->length < len) {
1196 for (l = 0; l < idd->length; l++)
1197 buf[l] = idd->identifier[l] ?
1198 idd->identifier[l] : ' ';
1202 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1203 l = strnlen(idd->identifier, idd->length);
1205 bcopy(idd->identifier, buf, l);
1209 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1210 && idd->identifier[0] == 0x10) {
1211 if ((idd->length - 2) * 2 + 4 < len) {
1212 for (l = 2, o = 0; l < idd->length; l++) {
1213 if (l == 6 || l == 8 || l == 10 || l == 12)
1214 o += sprintf(buf + o, "-");
1215 o += sprintf(buf + o, "%02x",
1216 idd->identifier[l]);
1221 if (idd->length * 2 < len) {
1222 for (l = 0; l < idd->length; l++)
1223 sprintf(buf + l * 2, "%02x",
1224 idd->identifier[l]);
1230 if (strlcpy(buf, cdai.buf, len) >= len)
1235 if (cdai.buf != NULL)
1236 free(cdai.buf, M_CAMXPT);
1240 static dev_match_ret
1241 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1244 dev_match_ret retval;
1247 retval = DM_RET_NONE;
1250 * If we aren't given something to match against, that's an error.
1253 return(DM_RET_ERROR);
1256 * If there are no match entries, then this bus matches no
1259 if ((patterns == NULL) || (num_patterns == 0))
1260 return(DM_RET_DESCEND | DM_RET_COPY);
1262 for (i = 0; i < num_patterns; i++) {
1263 struct bus_match_pattern *cur_pattern;
1266 * If the pattern in question isn't for a bus node, we
1267 * aren't interested. However, we do indicate to the
1268 * calling routine that we should continue descending the
1269 * tree, since the user wants to match against lower-level
1272 if (patterns[i].type != DEV_MATCH_BUS) {
1273 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1274 retval |= DM_RET_DESCEND;
1278 cur_pattern = &patterns[i].pattern.bus_pattern;
1281 * If they want to match any bus node, we give them any
1284 if (cur_pattern->flags == BUS_MATCH_ANY) {
1285 /* set the copy flag */
1286 retval |= DM_RET_COPY;
1289 * If we've already decided on an action, go ahead
1292 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1297 * Not sure why someone would do this...
1299 if (cur_pattern->flags == BUS_MATCH_NONE)
1302 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1303 && (cur_pattern->path_id != bus->path_id))
1306 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1307 && (cur_pattern->bus_id != bus->sim->bus_id))
1310 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1311 && (cur_pattern->unit_number != bus->sim->unit_number))
1314 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1315 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1320 * If we get to this point, the user definitely wants
1321 * information on this bus. So tell the caller to copy the
1324 retval |= DM_RET_COPY;
1327 * If the return action has been set to descend, then we
1328 * know that we've already seen a non-bus matching
1329 * expression, therefore we need to further descend the tree.
1330 * This won't change by continuing around the loop, so we
1331 * go ahead and return. If we haven't seen a non-bus
1332 * matching expression, we keep going around the loop until
1333 * we exhaust the matching expressions. We'll set the stop
1334 * flag once we fall out of the loop.
1336 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1341 * If the return action hasn't been set to descend yet, that means
1342 * we haven't seen anything other than bus matching patterns. So
1343 * tell the caller to stop descending the tree -- the user doesn't
1344 * want to match against lower level tree elements.
1346 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1347 retval |= DM_RET_STOP;
1352 static dev_match_ret
1353 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1354 struct cam_ed *device)
1356 dev_match_ret retval;
1359 retval = DM_RET_NONE;
1362 * If we aren't given something to match against, that's an error.
1365 return(DM_RET_ERROR);
1368 * If there are no match entries, then this device matches no
1371 if ((patterns == NULL) || (num_patterns == 0))
1372 return(DM_RET_DESCEND | DM_RET_COPY);
1374 for (i = 0; i < num_patterns; i++) {
1375 struct device_match_pattern *cur_pattern;
1376 struct scsi_vpd_device_id *device_id_page;
1379 * If the pattern in question isn't for a device node, we
1380 * aren't interested.
1382 if (patterns[i].type != DEV_MATCH_DEVICE) {
1383 if ((patterns[i].type == DEV_MATCH_PERIPH)
1384 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1385 retval |= DM_RET_DESCEND;
1389 cur_pattern = &patterns[i].pattern.device_pattern;
1391 /* Error out if mutually exclusive options are specified. */
1392 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1393 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1394 return(DM_RET_ERROR);
1397 * If they want to match any device node, we give them any
1400 if (cur_pattern->flags == DEV_MATCH_ANY)
1404 * Not sure why someone would do this...
1406 if (cur_pattern->flags == DEV_MATCH_NONE)
1409 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1410 && (cur_pattern->path_id != device->target->bus->path_id))
1413 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1414 && (cur_pattern->target_id != device->target->target_id))
1417 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1418 && (cur_pattern->target_lun != device->lun_id))
1421 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1422 && (cam_quirkmatch((caddr_t)&device->inq_data,
1423 (caddr_t)&cur_pattern->data.inq_pat,
1424 1, sizeof(cur_pattern->data.inq_pat),
1425 scsi_static_inquiry_match) == NULL))
1428 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1429 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1430 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1431 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1432 device->device_id_len
1433 - SVPD_DEVICE_ID_HDR_LEN,
1434 cur_pattern->data.devid_pat.id,
1435 cur_pattern->data.devid_pat.id_len) != 0))
1440 * If we get to this point, the user definitely wants
1441 * information on this device. So tell the caller to copy
1444 retval |= DM_RET_COPY;
1447 * If the return action has been set to descend, then we
1448 * know that we've already seen a peripheral matching
1449 * expression, therefore we need to further descend the tree.
1450 * This won't change by continuing around the loop, so we
1451 * go ahead and return. If we haven't seen a peripheral
1452 * matching expression, we keep going around the loop until
1453 * we exhaust the matching expressions. We'll set the stop
1454 * flag once we fall out of the loop.
1456 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1461 * If the return action hasn't been set to descend yet, that means
1462 * we haven't seen any peripheral matching patterns. So tell the
1463 * caller to stop descending the tree -- the user doesn't want to
1464 * match against lower level tree elements.
1466 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1467 retval |= DM_RET_STOP;
1473 * Match a single peripheral against any number of match patterns.
1475 static dev_match_ret
1476 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1477 struct cam_periph *periph)
1479 dev_match_ret retval;
1483 * If we aren't given something to match against, that's an error.
1486 return(DM_RET_ERROR);
1489 * If there are no match entries, then this peripheral matches no
1492 if ((patterns == NULL) || (num_patterns == 0))
1493 return(DM_RET_STOP | DM_RET_COPY);
1496 * There aren't any nodes below a peripheral node, so there's no
1497 * reason to descend the tree any further.
1499 retval = DM_RET_STOP;
1501 for (i = 0; i < num_patterns; i++) {
1502 struct periph_match_pattern *cur_pattern;
1505 * If the pattern in question isn't for a peripheral, we
1506 * aren't interested.
1508 if (patterns[i].type != DEV_MATCH_PERIPH)
1511 cur_pattern = &patterns[i].pattern.periph_pattern;
1514 * If they want to match on anything, then we will do so.
1516 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1517 /* set the copy flag */
1518 retval |= DM_RET_COPY;
1521 * We've already set the return action to stop,
1522 * since there are no nodes below peripherals in
1529 * Not sure why someone would do this...
1531 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1534 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1535 && (cur_pattern->path_id != periph->path->bus->path_id))
1539 * For the target and lun id's, we have to make sure the
1540 * target and lun pointers aren't NULL. The xpt peripheral
1541 * has a wildcard target and device.
1543 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1544 && ((periph->path->target == NULL)
1545 ||(cur_pattern->target_id != periph->path->target->target_id)))
1548 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1549 && ((periph->path->device == NULL)
1550 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1553 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1554 && (cur_pattern->unit_number != periph->unit_number))
1557 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1558 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1563 * If we get to this point, the user definitely wants
1564 * information on this peripheral. So tell the caller to
1565 * copy the data out.
1567 retval |= DM_RET_COPY;
1570 * The return action has already been set to stop, since
1571 * peripherals don't have any nodes below them in the EDT.
1577 * If we get to this point, the peripheral that was passed in
1578 * doesn't match any of the patterns.
1584 xptedtbusfunc(struct cam_eb *bus, void *arg)
1586 struct ccb_dev_match *cdm;
1587 struct cam_et *target;
1588 dev_match_ret retval;
1590 cdm = (struct ccb_dev_match *)arg;
1593 * If our position is for something deeper in the tree, that means
1594 * that we've already seen this node. So, we keep going down.
1596 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1597 && (cdm->pos.cookie.bus == bus)
1598 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1599 && (cdm->pos.cookie.target != NULL))
1600 retval = DM_RET_DESCEND;
1602 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1605 * If we got an error, bail out of the search.
1607 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1608 cdm->status = CAM_DEV_MATCH_ERROR;
1613 * If the copy flag is set, copy this bus out.
1615 if (retval & DM_RET_COPY) {
1618 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1619 sizeof(struct dev_match_result));
1622 * If we don't have enough space to put in another
1623 * match result, save our position and tell the
1624 * user there are more devices to check.
1626 if (spaceleft < sizeof(struct dev_match_result)) {
1627 bzero(&cdm->pos, sizeof(cdm->pos));
1628 cdm->pos.position_type =
1629 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1631 cdm->pos.cookie.bus = bus;
1632 cdm->pos.generations[CAM_BUS_GENERATION]=
1633 xsoftc.bus_generation;
1634 cdm->status = CAM_DEV_MATCH_MORE;
1637 j = cdm->num_matches;
1639 cdm->matches[j].type = DEV_MATCH_BUS;
1640 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1641 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1642 cdm->matches[j].result.bus_result.unit_number =
1643 bus->sim->unit_number;
1644 strncpy(cdm->matches[j].result.bus_result.dev_name,
1645 bus->sim->sim_name, DEV_IDLEN);
1649 * If the user is only interested in buses, there's no
1650 * reason to descend to the next level in the tree.
1652 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1656 * If there is a target generation recorded, check it to
1657 * make sure the target list hasn't changed.
1659 mtx_lock(&bus->eb_mtx);
1660 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1661 && (cdm->pos.cookie.bus == bus)
1662 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1663 && (cdm->pos.cookie.target != NULL)) {
1664 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1666 mtx_unlock(&bus->eb_mtx);
1667 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1670 target = (struct cam_et *)cdm->pos.cookie.target;
1674 mtx_unlock(&bus->eb_mtx);
1676 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1680 xptedttargetfunc(struct cam_et *target, void *arg)
1682 struct ccb_dev_match *cdm;
1684 struct cam_ed *device;
1686 cdm = (struct ccb_dev_match *)arg;
1690 * If there is a device list generation recorded, check it to
1691 * make sure the device list hasn't changed.
1693 mtx_lock(&bus->eb_mtx);
1694 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1695 && (cdm->pos.cookie.bus == bus)
1696 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1697 && (cdm->pos.cookie.target == target)
1698 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1699 && (cdm->pos.cookie.device != NULL)) {
1700 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1701 target->generation) {
1702 mtx_unlock(&bus->eb_mtx);
1703 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1706 device = (struct cam_ed *)cdm->pos.cookie.device;
1710 mtx_unlock(&bus->eb_mtx);
1712 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1716 xptedtdevicefunc(struct cam_ed *device, void *arg)
1719 struct cam_periph *periph;
1720 struct ccb_dev_match *cdm;
1721 dev_match_ret retval;
1723 cdm = (struct ccb_dev_match *)arg;
1724 bus = device->target->bus;
1727 * If our position is for something deeper in the tree, that means
1728 * that we've already seen this node. So, we keep going down.
1730 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1731 && (cdm->pos.cookie.device == device)
1732 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1733 && (cdm->pos.cookie.periph != NULL))
1734 retval = DM_RET_DESCEND;
1736 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1739 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1740 cdm->status = CAM_DEV_MATCH_ERROR;
1745 * If the copy flag is set, copy this device out.
1747 if (retval & DM_RET_COPY) {
1750 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1751 sizeof(struct dev_match_result));
1754 * If we don't have enough space to put in another
1755 * match result, save our position and tell the
1756 * user there are more devices to check.
1758 if (spaceleft < sizeof(struct dev_match_result)) {
1759 bzero(&cdm->pos, sizeof(cdm->pos));
1760 cdm->pos.position_type =
1761 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1762 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1764 cdm->pos.cookie.bus = device->target->bus;
1765 cdm->pos.generations[CAM_BUS_GENERATION]=
1766 xsoftc.bus_generation;
1767 cdm->pos.cookie.target = device->target;
1768 cdm->pos.generations[CAM_TARGET_GENERATION] =
1769 device->target->bus->generation;
1770 cdm->pos.cookie.device = device;
1771 cdm->pos.generations[CAM_DEV_GENERATION] =
1772 device->target->generation;
1773 cdm->status = CAM_DEV_MATCH_MORE;
1776 j = cdm->num_matches;
1778 cdm->matches[j].type = DEV_MATCH_DEVICE;
1779 cdm->matches[j].result.device_result.path_id =
1780 device->target->bus->path_id;
1781 cdm->matches[j].result.device_result.target_id =
1782 device->target->target_id;
1783 cdm->matches[j].result.device_result.target_lun =
1785 cdm->matches[j].result.device_result.protocol =
1787 bcopy(&device->inq_data,
1788 &cdm->matches[j].result.device_result.inq_data,
1789 sizeof(struct scsi_inquiry_data));
1790 bcopy(&device->ident_data,
1791 &cdm->matches[j].result.device_result.ident_data,
1792 sizeof(struct ata_params));
1794 /* Let the user know whether this device is unconfigured */
1795 if (device->flags & CAM_DEV_UNCONFIGURED)
1796 cdm->matches[j].result.device_result.flags =
1797 DEV_RESULT_UNCONFIGURED;
1799 cdm->matches[j].result.device_result.flags =
1804 * If the user isn't interested in peripherals, don't descend
1805 * the tree any further.
1807 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1811 * If there is a peripheral list generation recorded, make sure
1812 * it hasn't changed.
1815 mtx_lock(&bus->eb_mtx);
1816 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1817 && (cdm->pos.cookie.bus == bus)
1818 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1819 && (cdm->pos.cookie.target == device->target)
1820 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1821 && (cdm->pos.cookie.device == device)
1822 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1823 && (cdm->pos.cookie.periph != NULL)) {
1824 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1825 device->generation) {
1826 mtx_unlock(&bus->eb_mtx);
1828 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1831 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1835 mtx_unlock(&bus->eb_mtx);
1838 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1842 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1844 struct ccb_dev_match *cdm;
1845 dev_match_ret retval;
1847 cdm = (struct ccb_dev_match *)arg;
1849 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1851 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1852 cdm->status = CAM_DEV_MATCH_ERROR;
1857 * If the copy flag is set, copy this peripheral out.
1859 if (retval & DM_RET_COPY) {
1862 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1863 sizeof(struct dev_match_result));
1866 * If we don't have enough space to put in another
1867 * match result, save our position and tell the
1868 * user there are more devices to check.
1870 if (spaceleft < sizeof(struct dev_match_result)) {
1871 bzero(&cdm->pos, sizeof(cdm->pos));
1872 cdm->pos.position_type =
1873 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1874 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1877 cdm->pos.cookie.bus = periph->path->bus;
1878 cdm->pos.generations[CAM_BUS_GENERATION]=
1879 xsoftc.bus_generation;
1880 cdm->pos.cookie.target = periph->path->target;
1881 cdm->pos.generations[CAM_TARGET_GENERATION] =
1882 periph->path->bus->generation;
1883 cdm->pos.cookie.device = periph->path->device;
1884 cdm->pos.generations[CAM_DEV_GENERATION] =
1885 periph->path->target->generation;
1886 cdm->pos.cookie.periph = periph;
1887 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1888 periph->path->device->generation;
1889 cdm->status = CAM_DEV_MATCH_MORE;
1893 j = cdm->num_matches;
1895 cdm->matches[j].type = DEV_MATCH_PERIPH;
1896 cdm->matches[j].result.periph_result.path_id =
1897 periph->path->bus->path_id;
1898 cdm->matches[j].result.periph_result.target_id =
1899 periph->path->target->target_id;
1900 cdm->matches[j].result.periph_result.target_lun =
1901 periph->path->device->lun_id;
1902 cdm->matches[j].result.periph_result.unit_number =
1903 periph->unit_number;
1904 strncpy(cdm->matches[j].result.periph_result.periph_name,
1905 periph->periph_name, DEV_IDLEN);
1912 xptedtmatch(struct ccb_dev_match *cdm)
1917 cdm->num_matches = 0;
1920 * Check the bus list generation. If it has changed, the user
1921 * needs to reset everything and start over.
1924 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1925 && (cdm->pos.cookie.bus != NULL)) {
1926 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1927 xsoftc.bus_generation) {
1929 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1932 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1938 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1941 * If we get back 0, that means that we had to stop before fully
1942 * traversing the EDT. It also means that one of the subroutines
1943 * has set the status field to the proper value. If we get back 1,
1944 * we've fully traversed the EDT and copied out any matching entries.
1947 cdm->status = CAM_DEV_MATCH_LAST;
1953 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1955 struct cam_periph *periph;
1956 struct ccb_dev_match *cdm;
1958 cdm = (struct ccb_dev_match *)arg;
1961 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1962 && (cdm->pos.cookie.pdrv == pdrv)
1963 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1964 && (cdm->pos.cookie.periph != NULL)) {
1965 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1966 (*pdrv)->generation) {
1968 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1971 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1977 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1981 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1983 struct ccb_dev_match *cdm;
1984 dev_match_ret retval;
1986 cdm = (struct ccb_dev_match *)arg;
1988 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1990 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1991 cdm->status = CAM_DEV_MATCH_ERROR;
1996 * If the copy flag is set, copy this peripheral out.
1998 if (retval & DM_RET_COPY) {
2001 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2002 sizeof(struct dev_match_result));
2005 * If we don't have enough space to put in another
2006 * match result, save our position and tell the
2007 * user there are more devices to check.
2009 if (spaceleft < sizeof(struct dev_match_result)) {
2010 struct periph_driver **pdrv;
2013 bzero(&cdm->pos, sizeof(cdm->pos));
2014 cdm->pos.position_type =
2015 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2019 * This may look a bit non-sensical, but it is
2020 * actually quite logical. There are very few
2021 * peripheral drivers, and bloating every peripheral
2022 * structure with a pointer back to its parent
2023 * peripheral driver linker set entry would cost
2024 * more in the long run than doing this quick lookup.
2026 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2027 if (strcmp((*pdrv)->driver_name,
2028 periph->periph_name) == 0)
2032 if (*pdrv == NULL) {
2033 cdm->status = CAM_DEV_MATCH_ERROR;
2037 cdm->pos.cookie.pdrv = pdrv;
2039 * The periph generation slot does double duty, as
2040 * does the periph pointer slot. They are used for
2041 * both edt and pdrv lookups and positioning.
2043 cdm->pos.cookie.periph = periph;
2044 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2045 (*pdrv)->generation;
2046 cdm->status = CAM_DEV_MATCH_MORE;
2050 j = cdm->num_matches;
2052 cdm->matches[j].type = DEV_MATCH_PERIPH;
2053 cdm->matches[j].result.periph_result.path_id =
2054 periph->path->bus->path_id;
2057 * The transport layer peripheral doesn't have a target or
2060 if (periph->path->target)
2061 cdm->matches[j].result.periph_result.target_id =
2062 periph->path->target->target_id;
2064 cdm->matches[j].result.periph_result.target_id =
2065 CAM_TARGET_WILDCARD;
2067 if (periph->path->device)
2068 cdm->matches[j].result.periph_result.target_lun =
2069 periph->path->device->lun_id;
2071 cdm->matches[j].result.periph_result.target_lun =
2074 cdm->matches[j].result.periph_result.unit_number =
2075 periph->unit_number;
2076 strncpy(cdm->matches[j].result.periph_result.periph_name,
2077 periph->periph_name, DEV_IDLEN);
2084 xptperiphlistmatch(struct ccb_dev_match *cdm)
2088 cdm->num_matches = 0;
2091 * At this point in the edt traversal function, we check the bus
2092 * list generation to make sure that no buses have been added or
2093 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2094 * For the peripheral driver list traversal function, however, we
2095 * don't have to worry about new peripheral driver types coming or
2096 * going; they're in a linker set, and therefore can't change
2097 * without a recompile.
2100 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2101 && (cdm->pos.cookie.pdrv != NULL))
2102 ret = xptpdrvtraverse(
2103 (struct periph_driver **)cdm->pos.cookie.pdrv,
2104 xptplistpdrvfunc, cdm);
2106 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2109 * If we get back 0, that means that we had to stop before fully
2110 * traversing the peripheral driver tree. It also means that one of
2111 * the subroutines has set the status field to the proper value. If
2112 * we get back 1, we've fully traversed the EDT and copied out any
2116 cdm->status = CAM_DEV_MATCH_LAST;
2122 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2124 struct cam_eb *bus, *next_bus;
2132 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2140 for (; bus != NULL; bus = next_bus) {
2141 retval = tr_func(bus, arg);
2143 xpt_release_bus(bus);
2147 next_bus = TAILQ_NEXT(bus, links);
2149 next_bus->refcount++;
2151 xpt_release_bus(bus);
2157 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2158 xpt_targetfunc_t *tr_func, void *arg)
2160 struct cam_et *target, *next_target;
2165 target = start_target;
2167 mtx_lock(&bus->eb_mtx);
2168 target = TAILQ_FIRST(&bus->et_entries);
2169 if (target == NULL) {
2170 mtx_unlock(&bus->eb_mtx);
2174 mtx_unlock(&bus->eb_mtx);
2176 for (; target != NULL; target = next_target) {
2177 retval = tr_func(target, arg);
2179 xpt_release_target(target);
2182 mtx_lock(&bus->eb_mtx);
2183 next_target = TAILQ_NEXT(target, links);
2185 next_target->refcount++;
2186 mtx_unlock(&bus->eb_mtx);
2187 xpt_release_target(target);
2193 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2194 xpt_devicefunc_t *tr_func, void *arg)
2197 struct cam_ed *device, *next_device;
2203 device = start_device;
2205 mtx_lock(&bus->eb_mtx);
2206 device = TAILQ_FIRST(&target->ed_entries);
2207 if (device == NULL) {
2208 mtx_unlock(&bus->eb_mtx);
2212 mtx_unlock(&bus->eb_mtx);
2214 for (; device != NULL; device = next_device) {
2215 mtx_lock(&device->device_mtx);
2216 retval = tr_func(device, arg);
2217 mtx_unlock(&device->device_mtx);
2219 xpt_release_device(device);
2222 mtx_lock(&bus->eb_mtx);
2223 next_device = TAILQ_NEXT(device, links);
2225 next_device->refcount++;
2226 mtx_unlock(&bus->eb_mtx);
2227 xpt_release_device(device);
2233 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2234 xpt_periphfunc_t *tr_func, void *arg)
2237 struct cam_periph *periph, *next_periph;
2242 bus = device->target->bus;
2244 periph = start_periph;
2247 mtx_lock(&bus->eb_mtx);
2248 periph = SLIST_FIRST(&device->periphs);
2249 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2250 periph = SLIST_NEXT(periph, periph_links);
2251 if (periph == NULL) {
2252 mtx_unlock(&bus->eb_mtx);
2257 mtx_unlock(&bus->eb_mtx);
2260 for (; periph != NULL; periph = next_periph) {
2261 retval = tr_func(periph, arg);
2263 cam_periph_release_locked(periph);
2267 mtx_lock(&bus->eb_mtx);
2268 next_periph = SLIST_NEXT(periph, periph_links);
2269 while (next_periph != NULL &&
2270 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2271 next_periph = SLIST_NEXT(next_periph, periph_links);
2273 next_periph->refcount++;
2274 mtx_unlock(&bus->eb_mtx);
2276 cam_periph_release_locked(periph);
2282 xptpdrvtraverse(struct periph_driver **start_pdrv,
2283 xpt_pdrvfunc_t *tr_func, void *arg)
2285 struct periph_driver **pdrv;
2291 * We don't traverse the peripheral driver list like we do the
2292 * other lists, because it is a linker set, and therefore cannot be
2293 * changed during runtime. If the peripheral driver list is ever
2294 * re-done to be something other than a linker set (i.e. it can
2295 * change while the system is running), the list traversal should
2296 * be modified to work like the other traversal functions.
2298 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2299 *pdrv != NULL; pdrv++) {
2300 retval = tr_func(pdrv, arg);
2310 xptpdperiphtraverse(struct periph_driver **pdrv,
2311 struct cam_periph *start_periph,
2312 xpt_periphfunc_t *tr_func, void *arg)
2314 struct cam_periph *periph, *next_periph;
2320 periph = start_periph;
2323 periph = TAILQ_FIRST(&(*pdrv)->units);
2324 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2325 periph = TAILQ_NEXT(periph, unit_links);
2326 if (periph == NULL) {
2333 for (; periph != NULL; periph = next_periph) {
2334 cam_periph_lock(periph);
2335 retval = tr_func(periph, arg);
2336 cam_periph_unlock(periph);
2338 cam_periph_release(periph);
2342 next_periph = TAILQ_NEXT(periph, unit_links);
2343 while (next_periph != NULL &&
2344 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2345 next_periph = TAILQ_NEXT(next_periph, unit_links);
2347 next_periph->refcount++;
2349 cam_periph_release(periph);
2355 xptdefbusfunc(struct cam_eb *bus, void *arg)
2357 struct xpt_traverse_config *tr_config;
2359 tr_config = (struct xpt_traverse_config *)arg;
2361 if (tr_config->depth == XPT_DEPTH_BUS) {
2362 xpt_busfunc_t *tr_func;
2364 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2366 return(tr_func(bus, tr_config->tr_arg));
2368 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2372 xptdeftargetfunc(struct cam_et *target, void *arg)
2374 struct xpt_traverse_config *tr_config;
2376 tr_config = (struct xpt_traverse_config *)arg;
2378 if (tr_config->depth == XPT_DEPTH_TARGET) {
2379 xpt_targetfunc_t *tr_func;
2381 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2383 return(tr_func(target, tr_config->tr_arg));
2385 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2389 xptdefdevicefunc(struct cam_ed *device, void *arg)
2391 struct xpt_traverse_config *tr_config;
2393 tr_config = (struct xpt_traverse_config *)arg;
2395 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2396 xpt_devicefunc_t *tr_func;
2398 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2400 return(tr_func(device, tr_config->tr_arg));
2402 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2406 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2408 struct xpt_traverse_config *tr_config;
2409 xpt_periphfunc_t *tr_func;
2411 tr_config = (struct xpt_traverse_config *)arg;
2413 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2416 * Unlike the other default functions, we don't check for depth
2417 * here. The peripheral driver level is the last level in the EDT,
2418 * so if we're here, we should execute the function in question.
2420 return(tr_func(periph, tr_config->tr_arg));
2424 * Execute the given function for every bus in the EDT.
2427 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2429 struct xpt_traverse_config tr_config;
2431 tr_config.depth = XPT_DEPTH_BUS;
2432 tr_config.tr_func = tr_func;
2433 tr_config.tr_arg = arg;
2435 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2439 * Execute the given function for every device in the EDT.
2442 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2444 struct xpt_traverse_config tr_config;
2446 tr_config.depth = XPT_DEPTH_DEVICE;
2447 tr_config.tr_func = tr_func;
2448 tr_config.tr_arg = arg;
2450 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2454 xptsetasyncfunc(struct cam_ed *device, void *arg)
2456 struct cam_path path;
2457 struct ccb_getdev cgd;
2458 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2461 * Don't report unconfigured devices (Wildcard devs,
2462 * devices only for target mode, device instances
2463 * that have been invalidated but are waiting for
2464 * their last reference count to be released).
2466 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2469 xpt_compile_path(&path,
2471 device->target->bus->path_id,
2472 device->target->target_id,
2474 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2475 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2476 xpt_action((union ccb *)&cgd);
2477 csa->callback(csa->callback_arg,
2480 xpt_release_path(&path);
2486 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2488 struct cam_path path;
2489 struct ccb_pathinq cpi;
2490 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2492 xpt_compile_path(&path, /*periph*/NULL,
2494 CAM_TARGET_WILDCARD,
2496 xpt_path_lock(&path);
2497 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2498 cpi.ccb_h.func_code = XPT_PATH_INQ;
2499 xpt_action((union ccb *)&cpi);
2500 csa->callback(csa->callback_arg,
2503 xpt_path_unlock(&path);
2504 xpt_release_path(&path);
2510 xpt_action(union ccb *start_ccb)
2513 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2514 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2515 xpt_action_name(start_ccb->ccb_h.func_code)));
2517 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2518 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2522 xpt_action_default(union ccb *start_ccb)
2524 struct cam_path *path;
2525 struct cam_sim *sim;
2528 path = start_ccb->ccb_h.path;
2529 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2530 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2531 xpt_action_name(start_ccb->ccb_h.func_code)));
2533 switch (start_ccb->ccb_h.func_code) {
2536 struct cam_ed *device;
2539 * For the sake of compatibility with SCSI-1
2540 * devices that may not understand the identify
2541 * message, we include lun information in the
2542 * second byte of all commands. SCSI-1 specifies
2543 * that luns are a 3 bit value and reserves only 3
2544 * bits for lun information in the CDB. Later
2545 * revisions of the SCSI spec allow for more than 8
2546 * luns, but have deprecated lun information in the
2547 * CDB. So, if the lun won't fit, we must omit.
2549 * Also be aware that during initial probing for devices,
2550 * the inquiry information is unknown but initialized to 0.
2551 * This means that this code will be exercised while probing
2552 * devices with an ANSI revision greater than 2.
2554 device = path->device;
2555 if (device->protocol_version <= SCSI_REV_2
2556 && start_ccb->ccb_h.target_lun < 8
2557 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2559 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2560 start_ccb->ccb_h.target_lun << 5;
2562 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2566 case XPT_CONT_TARGET_IO:
2567 start_ccb->csio.sense_resid = 0;
2568 start_ccb->csio.resid = 0;
2571 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2572 start_ccb->ataio.resid = 0;
2575 if (start_ccb->ccb_h.func_code == XPT_NVME_IO)
2576 start_ccb->nvmeio.resid = 0;
2582 struct cam_devq *devq;
2584 devq = path->bus->sim->devq;
2585 mtx_lock(&devq->send_mtx);
2586 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2587 if (xpt_schedule_devq(devq, path->device) != 0)
2589 mtx_unlock(&devq->send_mtx);
2592 case XPT_CALC_GEOMETRY:
2593 /* Filter out garbage */
2594 if (start_ccb->ccg.block_size == 0
2595 || start_ccb->ccg.volume_size == 0) {
2596 start_ccb->ccg.cylinders = 0;
2597 start_ccb->ccg.heads = 0;
2598 start_ccb->ccg.secs_per_track = 0;
2599 start_ccb->ccb_h.status = CAM_REQ_CMP;
2602 #if defined(__sparc64__)
2604 * For sparc64, we may need adjust the geometry of large
2605 * disks in order to fit the limitations of the 16-bit
2606 * fields of the VTOC8 disk label.
2608 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2609 start_ccb->ccb_h.status = CAM_REQ_CMP;
2616 union ccb* abort_ccb;
2618 abort_ccb = start_ccb->cab.abort_ccb;
2619 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2620 struct cam_ed *device;
2621 struct cam_devq *devq;
2623 device = abort_ccb->ccb_h.path->device;
2624 devq = device->sim->devq;
2626 mtx_lock(&devq->send_mtx);
2627 if (abort_ccb->ccb_h.pinfo.index > 0) {
2628 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2629 abort_ccb->ccb_h.status =
2630 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2631 xpt_freeze_devq_device(device, 1);
2632 mtx_unlock(&devq->send_mtx);
2633 xpt_done(abort_ccb);
2634 start_ccb->ccb_h.status = CAM_REQ_CMP;
2637 mtx_unlock(&devq->send_mtx);
2639 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2640 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2642 * We've caught this ccb en route to
2643 * the SIM. Flag it for abort and the
2644 * SIM will do so just before starting
2645 * real work on the CCB.
2647 abort_ccb->ccb_h.status =
2648 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2649 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2650 start_ccb->ccb_h.status = CAM_REQ_CMP;
2654 if (XPT_FC_IS_QUEUED(abort_ccb)
2655 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2657 * It's already completed but waiting
2658 * for our SWI to get to it.
2660 start_ccb->ccb_h.status = CAM_UA_ABORT;
2664 * If we weren't able to take care of the abort request
2665 * in the XPT, pass the request down to the SIM for processing.
2669 case XPT_ACCEPT_TARGET_IO:
2671 case XPT_IMMED_NOTIFY:
2672 case XPT_NOTIFY_ACK:
2674 case XPT_IMMEDIATE_NOTIFY:
2675 case XPT_NOTIFY_ACKNOWLEDGE:
2676 case XPT_GET_SIM_KNOB_OLD:
2677 case XPT_GET_SIM_KNOB:
2678 case XPT_SET_SIM_KNOB:
2679 case XPT_GET_TRAN_SETTINGS:
2680 case XPT_SET_TRAN_SETTINGS:
2683 sim = path->bus->sim;
2685 if (mtx && !mtx_owned(mtx))
2689 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2690 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2691 (*(sim->sim_action))(sim, start_ccb);
2692 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2693 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2697 case XPT_PATH_STATS:
2698 start_ccb->cpis.last_reset = path->bus->last_reset;
2699 start_ccb->ccb_h.status = CAM_REQ_CMP;
2706 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2707 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2709 struct ccb_getdev *cgd;
2711 cgd = &start_ccb->cgd;
2712 cgd->protocol = dev->protocol;
2713 cgd->inq_data = dev->inq_data;
2714 cgd->ident_data = dev->ident_data;
2715 cgd->inq_flags = dev->inq_flags;
2716 cgd->nvme_data = dev->nvme_data;
2717 cgd->nvme_cdata = dev->nvme_cdata;
2718 cgd->ccb_h.status = CAM_REQ_CMP;
2719 cgd->serial_num_len = dev->serial_num_len;
2720 if ((dev->serial_num_len > 0)
2721 && (dev->serial_num != NULL))
2722 bcopy(dev->serial_num, cgd->serial_num,
2723 dev->serial_num_len);
2727 case XPT_GDEV_STATS:
2729 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2730 struct cam_ed *dev = path->device;
2731 struct cam_eb *bus = path->bus;
2732 struct cam_et *tar = path->target;
2733 struct cam_devq *devq = bus->sim->devq;
2735 mtx_lock(&devq->send_mtx);
2736 cgds->dev_openings = dev->ccbq.dev_openings;
2737 cgds->dev_active = dev->ccbq.dev_active;
2738 cgds->allocated = dev->ccbq.allocated;
2739 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2740 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2741 cgds->last_reset = tar->last_reset;
2742 cgds->maxtags = dev->maxtags;
2743 cgds->mintags = dev->mintags;
2744 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2745 cgds->last_reset = bus->last_reset;
2746 mtx_unlock(&devq->send_mtx);
2747 cgds->ccb_h.status = CAM_REQ_CMP;
2752 struct cam_periph *nperiph;
2753 struct periph_list *periph_head;
2754 struct ccb_getdevlist *cgdl;
2756 struct cam_ed *device;
2763 * Don't want anyone mucking with our data.
2765 device = path->device;
2766 periph_head = &device->periphs;
2767 cgdl = &start_ccb->cgdl;
2770 * Check and see if the list has changed since the user
2771 * last requested a list member. If so, tell them that the
2772 * list has changed, and therefore they need to start over
2773 * from the beginning.
2775 if ((cgdl->index != 0) &&
2776 (cgdl->generation != device->generation)) {
2777 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2782 * Traverse the list of peripherals and attempt to find
2783 * the requested peripheral.
2785 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2786 (nperiph != NULL) && (i <= cgdl->index);
2787 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2788 if (i == cgdl->index) {
2789 strncpy(cgdl->periph_name,
2790 nperiph->periph_name,
2792 cgdl->unit_number = nperiph->unit_number;
2797 cgdl->status = CAM_GDEVLIST_ERROR;
2801 if (nperiph == NULL)
2802 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2804 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2807 cgdl->generation = device->generation;
2809 cgdl->ccb_h.status = CAM_REQ_CMP;
2814 dev_pos_type position_type;
2815 struct ccb_dev_match *cdm;
2817 cdm = &start_ccb->cdm;
2820 * There are two ways of getting at information in the EDT.
2821 * The first way is via the primary EDT tree. It starts
2822 * with a list of buses, then a list of targets on a bus,
2823 * then devices/luns on a target, and then peripherals on a
2824 * device/lun. The "other" way is by the peripheral driver
2825 * lists. The peripheral driver lists are organized by
2826 * peripheral driver. (obviously) So it makes sense to
2827 * use the peripheral driver list if the user is looking
2828 * for something like "da1", or all "da" devices. If the
2829 * user is looking for something on a particular bus/target
2830 * or lun, it's generally better to go through the EDT tree.
2833 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2834 position_type = cdm->pos.position_type;
2838 position_type = CAM_DEV_POS_NONE;
2840 for (i = 0; i < cdm->num_patterns; i++) {
2841 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2842 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2843 position_type = CAM_DEV_POS_EDT;
2848 if (cdm->num_patterns == 0)
2849 position_type = CAM_DEV_POS_EDT;
2850 else if (position_type == CAM_DEV_POS_NONE)
2851 position_type = CAM_DEV_POS_PDRV;
2854 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2855 case CAM_DEV_POS_EDT:
2858 case CAM_DEV_POS_PDRV:
2859 xptperiphlistmatch(cdm);
2862 cdm->status = CAM_DEV_MATCH_ERROR;
2866 if (cdm->status == CAM_DEV_MATCH_ERROR)
2867 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2869 start_ccb->ccb_h.status = CAM_REQ_CMP;
2875 struct ccb_setasync *csa;
2876 struct async_node *cur_entry;
2877 struct async_list *async_head;
2880 csa = &start_ccb->csa;
2881 added = csa->event_enable;
2882 async_head = &path->device->asyncs;
2885 * If there is already an entry for us, simply
2888 cur_entry = SLIST_FIRST(async_head);
2889 while (cur_entry != NULL) {
2890 if ((cur_entry->callback_arg == csa->callback_arg)
2891 && (cur_entry->callback == csa->callback))
2893 cur_entry = SLIST_NEXT(cur_entry, links);
2896 if (cur_entry != NULL) {
2898 * If the request has no flags set,
2901 added &= ~cur_entry->event_enable;
2902 if (csa->event_enable == 0) {
2903 SLIST_REMOVE(async_head, cur_entry,
2905 xpt_release_device(path->device);
2906 free(cur_entry, M_CAMXPT);
2908 cur_entry->event_enable = csa->event_enable;
2910 csa->event_enable = added;
2912 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2914 if (cur_entry == NULL) {
2915 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2918 cur_entry->event_enable = csa->event_enable;
2919 cur_entry->event_lock = (path->bus->sim->mtx &&
2920 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
2921 cur_entry->callback_arg = csa->callback_arg;
2922 cur_entry->callback = csa->callback;
2923 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2924 xpt_acquire_device(path->device);
2926 start_ccb->ccb_h.status = CAM_REQ_CMP;
2931 struct ccb_relsim *crs;
2934 crs = &start_ccb->crs;
2938 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2942 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2944 /* Don't ever go below one opening */
2945 if (crs->openings > 0) {
2946 xpt_dev_ccbq_resize(path, crs->openings);
2949 "number of openings is now %d\n",
2955 mtx_lock(&dev->sim->devq->send_mtx);
2956 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2958 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2961 * Just extend the old timeout and decrement
2962 * the freeze count so that a single timeout
2963 * is sufficient for releasing the queue.
2965 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2966 callout_stop(&dev->callout);
2969 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2972 callout_reset_sbt(&dev->callout,
2973 SBT_1MS * crs->release_timeout, 0,
2974 xpt_release_devq_timeout, dev, 0);
2976 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2980 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2982 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2984 * Decrement the freeze count so that a single
2985 * completion is still sufficient to unfreeze
2988 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2991 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2992 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2996 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2998 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2999 || (dev->ccbq.dev_active == 0)) {
3001 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3004 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3005 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3008 mtx_unlock(&dev->sim->devq->send_mtx);
3010 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3011 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3012 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3013 start_ccb->ccb_h.status = CAM_REQ_CMP;
3017 struct cam_path *oldpath;
3019 /* Check that all request bits are supported. */
3020 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3021 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3025 cam_dflags = CAM_DEBUG_NONE;
3026 if (cam_dpath != NULL) {
3027 oldpath = cam_dpath;
3029 xpt_free_path(oldpath);
3031 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3032 if (xpt_create_path(&cam_dpath, NULL,
3033 start_ccb->ccb_h.path_id,
3034 start_ccb->ccb_h.target_id,
3035 start_ccb->ccb_h.target_lun) !=
3037 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3039 cam_dflags = start_ccb->cdbg.flags;
3040 start_ccb->ccb_h.status = CAM_REQ_CMP;
3041 xpt_print(cam_dpath, "debugging flags now %x\n",
3045 start_ccb->ccb_h.status = CAM_REQ_CMP;
3049 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3050 xpt_freeze_devq(path, 1);
3051 start_ccb->ccb_h.status = CAM_REQ_CMP;
3053 case XPT_REPROBE_LUN:
3054 xpt_async(AC_INQ_CHANGED, path, NULL);
3055 start_ccb->ccb_h.status = CAM_REQ_CMP;
3056 xpt_done(start_ccb);
3063 xpt_print(start_ccb->ccb_h.path,
3064 "%s: CCB type %#x %s not supported\n", __func__,
3065 start_ccb->ccb_h.func_code,
3066 xpt_action_name(start_ccb->ccb_h.func_code));
3067 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3068 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3069 xpt_done(start_ccb);
3073 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3074 ("xpt_action_default: func= %#x %s status %#x\n",
3075 start_ccb->ccb_h.func_code,
3076 xpt_action_name(start_ccb->ccb_h.func_code),
3077 start_ccb->ccb_h.status));
3081 xpt_polled_action(union ccb *start_ccb)
3084 struct cam_sim *sim;
3085 struct cam_devq *devq;
3089 timeout = start_ccb->ccb_h.timeout * 10;
3090 sim = start_ccb->ccb_h.path->bus->sim;
3093 dev = start_ccb->ccb_h.path->device;
3095 mtx_unlock(&dev->device_mtx);
3098 * Steal an opening so that no other queued requests
3099 * can get it before us while we simulate interrupts.
3101 mtx_lock(&devq->send_mtx);
3102 dev->ccbq.dev_openings--;
3103 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3105 mtx_unlock(&devq->send_mtx);
3109 (*(sim->sim_poll))(sim);
3113 mtx_lock(&devq->send_mtx);
3115 dev->ccbq.dev_openings++;
3116 mtx_unlock(&devq->send_mtx);
3119 xpt_action(start_ccb);
3120 while(--timeout > 0) {
3123 (*(sim->sim_poll))(sim);
3127 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3134 * XXX Is it worth adding a sim_timeout entry
3135 * point so we can attempt recovery? If
3136 * this is only used for dumps, I don't think
3139 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3142 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3145 mtx_lock(&dev->device_mtx);
3149 * Schedule a peripheral driver to receive a ccb when its
3150 * target device has space for more transactions.
3153 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3156 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3157 cam_periph_assert(periph, MA_OWNED);
3158 if (new_priority < periph->scheduled_priority) {
3159 periph->scheduled_priority = new_priority;
3160 xpt_run_allocq(periph, 0);
3166 * Schedule a device to run on a given queue.
3167 * If the device was inserted as a new entry on the queue,
3168 * return 1 meaning the device queue should be run. If we
3169 * were already queued, implying someone else has already
3170 * started the queue, return 0 so the caller doesn't attempt
3174 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3175 u_int32_t new_priority)
3178 u_int32_t old_priority;
3180 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3182 old_priority = pinfo->priority;
3185 * Are we already queued?
3187 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3188 /* Simply reorder based on new priority */
3189 if (new_priority < old_priority) {
3190 camq_change_priority(queue, pinfo->index,
3192 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3193 ("changed priority to %d\n",
3199 /* New entry on the queue */
3200 if (new_priority < old_priority)
3201 pinfo->priority = new_priority;
3203 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3204 ("Inserting onto queue\n"));
3205 pinfo->generation = ++queue->generation;
3206 camq_insert(queue, pinfo);
3213 xpt_run_allocq_task(void *context, int pending)
3215 struct cam_periph *periph = context;
3217 cam_periph_lock(periph);
3218 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3219 xpt_run_allocq(periph, 1);
3220 cam_periph_unlock(periph);
3221 cam_periph_release(periph);
3225 xpt_run_allocq(struct cam_periph *periph, int sleep)
3227 struct cam_ed *device;
3231 cam_periph_assert(periph, MA_OWNED);
3232 if (periph->periph_allocating)
3234 periph->periph_allocating = 1;
3235 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3236 device = periph->path->device;
3239 while ((prio = min(periph->scheduled_priority,
3240 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3241 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3242 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3245 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3247 ccb = xpt_get_ccb(periph);
3250 if (periph->flags & CAM_PERIPH_RUN_TASK)
3252 cam_periph_doacquire(periph);
3253 periph->flags |= CAM_PERIPH_RUN_TASK;
3254 taskqueue_enqueue(xsoftc.xpt_taskq,
3255 &periph->periph_run_task);
3258 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3259 if (prio == periph->immediate_priority) {
3260 periph->immediate_priority = CAM_PRIORITY_NONE;
3261 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3262 ("waking cam_periph_getccb()\n"));
3263 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3265 wakeup(&periph->ccb_list);
3267 periph->scheduled_priority = CAM_PRIORITY_NONE;
3268 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3269 ("calling periph_start()\n"));
3270 periph->periph_start(periph, ccb);
3275 xpt_release_ccb(ccb);
3276 periph->periph_allocating = 0;
3280 xpt_run_devq(struct cam_devq *devq)
3284 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3286 devq->send_queue.qfrozen_cnt++;
3287 while ((devq->send_queue.entries > 0)
3288 && (devq->send_openings > 0)
3289 && (devq->send_queue.qfrozen_cnt <= 1)) {
3290 struct cam_ed *device;
3291 union ccb *work_ccb;
3292 struct cam_sim *sim;
3293 struct xpt_proto *proto;
3295 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3297 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3298 ("running device %p\n", device));
3300 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3301 if (work_ccb == NULL) {
3302 printf("device on run queue with no ccbs???\n");
3306 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3308 mtx_lock(&xsoftc.xpt_highpower_lock);
3309 if (xsoftc.num_highpower <= 0) {
3311 * We got a high power command, but we
3312 * don't have any available slots. Freeze
3313 * the device queue until we have a slot
3316 xpt_freeze_devq_device(device, 1);
3317 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3320 mtx_unlock(&xsoftc.xpt_highpower_lock);
3324 * Consume a high power slot while
3327 xsoftc.num_highpower--;
3329 mtx_unlock(&xsoftc.xpt_highpower_lock);
3331 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3332 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3333 devq->send_openings--;
3334 devq->send_active++;
3335 xpt_schedule_devq(devq, device);
3336 mtx_unlock(&devq->send_mtx);
3338 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3340 * The client wants to freeze the queue
3341 * after this CCB is sent.
3343 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3346 /* In Target mode, the peripheral driver knows best... */
3347 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3348 if ((device->inq_flags & SID_CmdQue) != 0
3349 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3350 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3353 * Clear this in case of a retried CCB that
3354 * failed due to a rejected tag.
3356 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3359 KASSERT(device == work_ccb->ccb_h.path->device,
3360 ("device (%p) / path->device (%p) mismatch",
3361 device, work_ccb->ccb_h.path->device));
3362 proto = xpt_proto_find(device->protocol);
3363 if (proto && proto->ops->debug_out)
3364 proto->ops->debug_out(work_ccb);
3367 * Device queues can be shared among multiple SIM instances
3368 * that reside on different buses. Use the SIM from the
3369 * queued device, rather than the one from the calling bus.
3373 if (mtx && !mtx_owned(mtx))
3377 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3378 (*(sim->sim_action))(sim, work_ccb);
3381 mtx_lock(&devq->send_mtx);
3383 devq->send_queue.qfrozen_cnt--;
3387 * This function merges stuff from the slave ccb into the master ccb, while
3388 * keeping important fields in the master ccb constant.
3391 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3395 * Pull fields that are valid for peripheral drivers to set
3396 * into the master CCB along with the CCB "payload".
3398 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3399 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3400 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3401 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3402 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3403 sizeof(union ccb) - sizeof(struct ccb_hdr));
3407 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3408 u_int32_t priority, u_int32_t flags)
3411 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3412 ccb_h->pinfo.priority = priority;
3414 ccb_h->path_id = path->bus->path_id;
3416 ccb_h->target_id = path->target->target_id;
3418 ccb_h->target_id = CAM_TARGET_WILDCARD;
3420 ccb_h->target_lun = path->device->lun_id;
3421 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3423 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3425 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3426 ccb_h->flags = flags;
3431 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3433 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3436 /* Path manipulation functions */
3438 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3439 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3441 struct cam_path *path;
3444 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3447 status = CAM_RESRC_UNAVAIL;
3450 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3451 if (status != CAM_REQ_CMP) {
3452 free(path, M_CAMPATH);
3455 *new_path_ptr = path;
3460 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3461 struct cam_periph *periph, path_id_t path_id,
3462 target_id_t target_id, lun_id_t lun_id)
3465 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3470 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3471 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3474 struct cam_et *target;
3475 struct cam_ed *device;
3478 status = CAM_REQ_CMP; /* Completed without error */
3479 target = NULL; /* Wildcarded */
3480 device = NULL; /* Wildcarded */
3483 * We will potentially modify the EDT, so block interrupts
3484 * that may attempt to create cam paths.
3486 bus = xpt_find_bus(path_id);
3488 status = CAM_PATH_INVALID;
3491 mtx_lock(&bus->eb_mtx);
3492 target = xpt_find_target(bus, target_id);
3493 if (target == NULL) {
3495 struct cam_et *new_target;
3497 new_target = xpt_alloc_target(bus, target_id);
3498 if (new_target == NULL) {
3499 status = CAM_RESRC_UNAVAIL;
3501 target = new_target;
3505 if (target != NULL) {
3506 device = xpt_find_device(target, lun_id);
3507 if (device == NULL) {
3509 struct cam_ed *new_device;
3512 (*(bus->xport->ops->alloc_device))(bus,
3515 if (new_device == NULL) {
3516 status = CAM_RESRC_UNAVAIL;
3518 device = new_device;
3522 mtx_unlock(&bus->eb_mtx);
3526 * Only touch the user's data if we are successful.
3528 if (status == CAM_REQ_CMP) {
3529 new_path->periph = perph;
3530 new_path->bus = bus;
3531 new_path->target = target;
3532 new_path->device = device;
3533 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3536 xpt_release_device(device);
3538 xpt_release_target(target);
3540 xpt_release_bus(bus);
3546 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3548 struct cam_path *new_path;
3550 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3551 if (new_path == NULL)
3552 return(CAM_RESRC_UNAVAIL);
3553 xpt_copy_path(new_path, path);
3554 *new_path_ptr = new_path;
3555 return (CAM_REQ_CMP);
3559 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3563 if (path->bus != NULL)
3564 xpt_acquire_bus(path->bus);
3565 if (path->target != NULL)
3566 xpt_acquire_target(path->target);
3567 if (path->device != NULL)
3568 xpt_acquire_device(path->device);
3572 xpt_release_path(struct cam_path *path)
3574 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3575 if (path->device != NULL) {
3576 xpt_release_device(path->device);
3577 path->device = NULL;
3579 if (path->target != NULL) {
3580 xpt_release_target(path->target);
3581 path->target = NULL;
3583 if (path->bus != NULL) {
3584 xpt_release_bus(path->bus);
3590 xpt_free_path(struct cam_path *path)
3593 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3594 xpt_release_path(path);
3595 free(path, M_CAMPATH);
3599 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3600 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3606 *bus_ref = path->bus->refcount;
3612 *periph_ref = path->periph->refcount;
3619 *target_ref = path->target->refcount;
3625 *device_ref = path->device->refcount;
3632 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3633 * in path1, 2 for match with wildcards in path2.
3636 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3640 if (path1->bus != path2->bus) {
3641 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3643 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3648 if (path1->target != path2->target) {
3649 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3652 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3657 if (path1->device != path2->device) {
3658 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3661 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3670 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3674 if (path->bus != dev->target->bus) {
3675 if (path->bus->path_id == CAM_BUS_WILDCARD)
3677 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3682 if (path->target != dev->target) {
3683 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3686 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3691 if (path->device != dev) {
3692 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3695 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3704 xpt_print_path(struct cam_path *path)
3707 char buffer[XPT_PRINT_LEN];
3709 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3710 xpt_path_sbuf(path, &sb);
3712 printf("%s", sbuf_data(&sb));
3717 xpt_print_device(struct cam_ed *device)
3721 printf("(nopath): ");
3723 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3724 device->sim->unit_number,
3725 device->sim->bus_id,
3726 device->target->target_id,
3727 (uintmax_t)device->lun_id);
3732 xpt_print(struct cam_path *path, const char *fmt, ...)
3736 char buffer[XPT_PRINT_LEN];
3738 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3740 xpt_path_sbuf(path, &sb);
3742 sbuf_vprintf(&sb, fmt, ap);
3746 printf("%s", sbuf_data(&sb));
3751 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3756 sbuf_new(&sb, str, str_len, 0);
3757 len = xpt_path_sbuf(path, &sb);
3763 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3767 sbuf_printf(sb, "(nopath): ");
3769 if (path->periph != NULL)
3770 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3771 path->periph->unit_number);
3773 sbuf_printf(sb, "(noperiph:");
3775 if (path->bus != NULL)
3776 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3777 path->bus->sim->unit_number,
3778 path->bus->sim->bus_id);
3780 sbuf_printf(sb, "nobus:");
3782 if (path->target != NULL)
3783 sbuf_printf(sb, "%d:", path->target->target_id);
3785 sbuf_printf(sb, "X:");
3787 if (path->device != NULL)
3788 sbuf_printf(sb, "%jx): ",
3789 (uintmax_t)path->device->lun_id);
3791 sbuf_printf(sb, "X): ");
3794 return(sbuf_len(sb));
3798 xpt_path_path_id(struct cam_path *path)
3800 return(path->bus->path_id);
3804 xpt_path_target_id(struct cam_path *path)
3806 if (path->target != NULL)
3807 return (path->target->target_id);
3809 return (CAM_TARGET_WILDCARD);
3813 xpt_path_lun_id(struct cam_path *path)
3815 if (path->device != NULL)
3816 return (path->device->lun_id);
3818 return (CAM_LUN_WILDCARD);
3822 xpt_path_sim(struct cam_path *path)
3825 return (path->bus->sim);
3829 xpt_path_periph(struct cam_path *path)
3832 return (path->periph);
3836 * Release a CAM control block for the caller. Remit the cost of the structure
3837 * to the device referenced by the path. If the this device had no 'credits'
3838 * and peripheral drivers have registered async callbacks for this notification
3842 xpt_release_ccb(union ccb *free_ccb)
3844 struct cam_ed *device;
3845 struct cam_periph *periph;
3847 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3848 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3849 device = free_ccb->ccb_h.path->device;
3850 periph = free_ccb->ccb_h.path->periph;
3852 xpt_free_ccb(free_ccb);
3853 periph->periph_allocated--;
3854 cam_ccbq_release_opening(&device->ccbq);
3855 xpt_run_allocq(periph, 0);
3858 /* Functions accessed by SIM drivers */
3860 static struct xpt_xport_ops xport_default_ops = {
3861 .alloc_device = xpt_alloc_device_default,
3862 .action = xpt_action_default,
3863 .async = xpt_dev_async_default,
3865 static struct xpt_xport xport_default = {
3866 .xport = XPORT_UNKNOWN,
3868 .ops = &xport_default_ops,
3871 CAM_XPT_XPORT(xport_default);
3874 * A sim structure, listing the SIM entry points and instance
3875 * identification info is passed to xpt_bus_register to hook the SIM
3876 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3877 * for this new bus and places it in the array of buses and assigns
3878 * it a path_id. The path_id may be influenced by "hard wiring"
3879 * information specified by the user. Once interrupt services are
3880 * available, the bus will be probed.
3883 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3885 struct cam_eb *new_bus;
3886 struct cam_eb *old_bus;
3887 struct ccb_pathinq cpi;
3888 struct cam_path *path;
3892 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3893 M_CAMXPT, M_NOWAIT|M_ZERO);
3894 if (new_bus == NULL) {
3895 /* Couldn't satisfy request */
3896 return (CAM_RESRC_UNAVAIL);
3899 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3900 TAILQ_INIT(&new_bus->et_entries);
3903 timevalclear(&new_bus->last_reset);
3905 new_bus->refcount = 1; /* Held until a bus_deregister event */
3906 new_bus->generation = 0;
3909 sim->path_id = new_bus->path_id =
3910 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3911 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3912 while (old_bus != NULL
3913 && old_bus->path_id < new_bus->path_id)
3914 old_bus = TAILQ_NEXT(old_bus, links);
3915 if (old_bus != NULL)
3916 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3918 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3919 xsoftc.bus_generation++;
3923 * Set a default transport so that a PATH_INQ can be issued to
3924 * the SIM. This will then allow for probing and attaching of
3925 * a more appropriate transport.
3927 new_bus->xport = &xport_default;
3929 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3930 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3931 if (status != CAM_REQ_CMP) {
3932 xpt_release_bus(new_bus);
3933 free(path, M_CAMXPT);
3934 return (CAM_RESRC_UNAVAIL);
3937 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3938 cpi.ccb_h.func_code = XPT_PATH_INQ;
3939 xpt_action((union ccb *)&cpi);
3941 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3942 struct xpt_xport **xpt;
3944 SET_FOREACH(xpt, cam_xpt_xport_set) {
3945 if ((*xpt)->xport == cpi.transport) {
3946 new_bus->xport = *xpt;
3950 if (new_bus->xport == NULL) {
3952 "No transport found for %d\n", cpi.transport);
3953 xpt_release_bus(new_bus);
3954 free(path, M_CAMXPT);
3955 return (CAM_RESRC_UNAVAIL);
3959 /* Notify interested parties */
3960 if (sim->path_id != CAM_XPT_PATH_ID) {
3962 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3963 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3964 union ccb *scan_ccb;
3966 /* Initiate bus rescan. */
3967 scan_ccb = xpt_alloc_ccb_nowait();
3968 if (scan_ccb != NULL) {
3969 scan_ccb->ccb_h.path = path;
3970 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3971 scan_ccb->crcn.flags = 0;
3972 xpt_rescan(scan_ccb);
3975 "Can't allocate CCB to scan bus\n");
3976 xpt_free_path(path);
3979 xpt_free_path(path);
3981 xpt_free_path(path);
3982 return (CAM_SUCCESS);
3986 xpt_bus_deregister(path_id_t pathid)
3988 struct cam_path bus_path;
3991 status = xpt_compile_path(&bus_path, NULL, pathid,
3992 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3993 if (status != CAM_REQ_CMP)
3996 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3997 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3999 /* Release the reference count held while registered. */
4000 xpt_release_bus(bus_path.bus);
4001 xpt_release_path(&bus_path);
4003 return (CAM_REQ_CMP);
4007 xptnextfreepathid(void)
4013 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4015 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4017 /* Find an unoccupied pathid */
4018 while (bus != NULL && bus->path_id <= pathid) {
4019 if (bus->path_id == pathid)
4021 bus = TAILQ_NEXT(bus, links);
4025 * Ensure that this pathid is not reserved for
4026 * a bus that may be registered in the future.
4028 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4030 /* Start the search over */
4037 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4044 pathid = CAM_XPT_PATH_ID;
4045 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4046 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4049 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4050 if (strcmp(dname, "scbus")) {
4051 /* Avoid a bit of foot shooting. */
4054 if (dunit < 0) /* unwired?! */
4056 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4057 if (sim_bus == val) {
4061 } else if (sim_bus == 0) {
4062 /* Unspecified matches bus 0 */
4066 printf("Ambiguous scbus configuration for %s%d "
4067 "bus %d, cannot wire down. The kernel "
4068 "config entry for scbus%d should "
4069 "specify a controller bus.\n"
4070 "Scbus will be assigned dynamically.\n",
4071 sim_name, sim_unit, sim_bus, dunit);
4076 if (pathid == CAM_XPT_PATH_ID)
4077 pathid = xptnextfreepathid();
4082 xpt_async_string(u_int32_t async_code)
4085 switch (async_code) {
4086 case AC_BUS_RESET: return ("AC_BUS_RESET");
4087 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4088 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4089 case AC_SENT_BDR: return ("AC_SENT_BDR");
4090 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4091 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4092 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4093 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4094 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4095 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4096 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4097 case AC_CONTRACT: return ("AC_CONTRACT");
4098 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4099 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4101 return ("AC_UNKNOWN");
4105 xpt_async_size(u_int32_t async_code)
4108 switch (async_code) {
4109 case AC_BUS_RESET: return (0);
4110 case AC_UNSOL_RESEL: return (0);
4111 case AC_SCSI_AEN: return (0);
4112 case AC_SENT_BDR: return (0);
4113 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4114 case AC_PATH_DEREGISTERED: return (0);
4115 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4116 case AC_LOST_DEVICE: return (0);
4117 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4118 case AC_INQ_CHANGED: return (0);
4119 case AC_GETDEV_CHANGED: return (0);
4120 case AC_CONTRACT: return (sizeof(struct ac_contract));
4121 case AC_ADVINFO_CHANGED: return (-1);
4122 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4128 xpt_async_process_dev(struct cam_ed *device, void *arg)
4130 union ccb *ccb = arg;
4131 struct cam_path *path = ccb->ccb_h.path;
4132 void *async_arg = ccb->casync.async_arg_ptr;
4133 u_int32_t async_code = ccb->casync.async_code;
4136 if (path->device != device
4137 && path->device->lun_id != CAM_LUN_WILDCARD
4138 && device->lun_id != CAM_LUN_WILDCARD)
4142 * The async callback could free the device.
4143 * If it is a broadcast async, it doesn't hold
4144 * device reference, so take our own reference.
4146 xpt_acquire_device(device);
4149 * If async for specific device is to be delivered to
4150 * the wildcard client, take the specific device lock.
4151 * XXX: We may need a way for client to specify it.
4153 if ((device->lun_id == CAM_LUN_WILDCARD &&
4154 path->device->lun_id != CAM_LUN_WILDCARD) ||
4155 (device->target->target_id == CAM_TARGET_WILDCARD &&
4156 path->target->target_id != CAM_TARGET_WILDCARD) ||
4157 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4158 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4159 mtx_unlock(&device->device_mtx);
4160 xpt_path_lock(path);
4165 (*(device->target->bus->xport->ops->async))(async_code,
4166 device->target->bus, device->target, device, async_arg);
4167 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4170 xpt_path_unlock(path);
4171 mtx_lock(&device->device_mtx);
4173 xpt_release_device(device);
4178 xpt_async_process_tgt(struct cam_et *target, void *arg)
4180 union ccb *ccb = arg;
4181 struct cam_path *path = ccb->ccb_h.path;
4183 if (path->target != target
4184 && path->target->target_id != CAM_TARGET_WILDCARD
4185 && target->target_id != CAM_TARGET_WILDCARD)
4188 if (ccb->casync.async_code == AC_SENT_BDR) {
4189 /* Update our notion of when the last reset occurred */
4190 microtime(&target->last_reset);
4193 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4197 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4200 struct cam_path *path;
4202 u_int32_t async_code;
4204 path = ccb->ccb_h.path;
4205 async_code = ccb->casync.async_code;
4206 async_arg = ccb->casync.async_arg_ptr;
4207 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4208 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4211 if (async_code == AC_BUS_RESET) {
4212 /* Update our notion of when the last reset occurred */
4213 microtime(&bus->last_reset);
4216 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4219 * If this wasn't a fully wildcarded async, tell all
4220 * clients that want all async events.
4222 if (bus != xpt_periph->path->bus) {
4223 xpt_path_lock(xpt_periph->path);
4224 xpt_async_process_dev(xpt_periph->path->device, ccb);
4225 xpt_path_unlock(xpt_periph->path);
4228 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4229 xpt_release_devq(path, 1, TRUE);
4231 xpt_release_simq(path->bus->sim, TRUE);
4232 if (ccb->casync.async_arg_size > 0)
4233 free(async_arg, M_CAMXPT);
4234 xpt_free_path(path);
4239 xpt_async_bcast(struct async_list *async_head,
4240 u_int32_t async_code,
4241 struct cam_path *path, void *async_arg)
4243 struct async_node *cur_entry;
4246 cur_entry = SLIST_FIRST(async_head);
4247 while (cur_entry != NULL) {
4248 struct async_node *next_entry;
4250 * Grab the next list entry before we call the current
4251 * entry's callback. This is because the callback function
4252 * can delete its async callback entry.
4254 next_entry = SLIST_NEXT(cur_entry, links);
4255 if ((cur_entry->event_enable & async_code) != 0) {
4256 mtx = cur_entry->event_lock ?
4257 path->device->sim->mtx : NULL;
4260 cur_entry->callback(cur_entry->callback_arg,
4266 cur_entry = next_entry;
4271 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4276 ccb = xpt_alloc_ccb_nowait();
4278 xpt_print(path, "Can't allocate CCB to send %s\n",
4279 xpt_async_string(async_code));
4283 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4284 xpt_print(path, "Can't allocate path to send %s\n",
4285 xpt_async_string(async_code));
4289 ccb->ccb_h.path->periph = NULL;
4290 ccb->ccb_h.func_code = XPT_ASYNC;
4291 ccb->ccb_h.cbfcnp = xpt_async_process;
4292 ccb->ccb_h.flags |= CAM_UNLOCKED;
4293 ccb->casync.async_code = async_code;
4294 ccb->casync.async_arg_size = 0;
4295 size = xpt_async_size(async_code);
4296 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4297 ("xpt_async: func %#x %s aync_code %d %s\n",
4298 ccb->ccb_h.func_code,
4299 xpt_action_name(ccb->ccb_h.func_code),
4301 xpt_async_string(async_code)));
4302 if (size > 0 && async_arg != NULL) {
4303 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4304 if (ccb->casync.async_arg_ptr == NULL) {
4305 xpt_print(path, "Can't allocate argument to send %s\n",
4306 xpt_async_string(async_code));
4307 xpt_free_path(ccb->ccb_h.path);
4311 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4312 ccb->casync.async_arg_size = size;
4313 } else if (size < 0) {
4314 ccb->casync.async_arg_ptr = async_arg;
4315 ccb->casync.async_arg_size = size;
4317 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4318 xpt_freeze_devq(path, 1);
4320 xpt_freeze_simq(path->bus->sim, 1);
4325 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4326 struct cam_et *target, struct cam_ed *device,
4331 * We only need to handle events for real devices.
4333 if (target->target_id == CAM_TARGET_WILDCARD
4334 || device->lun_id == CAM_LUN_WILDCARD)
4337 printf("%s called\n", __func__);
4341 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4343 struct cam_devq *devq;
4346 devq = dev->sim->devq;
4347 mtx_assert(&devq->send_mtx, MA_OWNED);
4348 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4349 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4350 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4351 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4352 /* Remove frozen device from sendq. */
4353 if (device_is_queued(dev))
4354 camq_remove(&devq->send_queue, dev->devq_entry.index);
4359 xpt_freeze_devq(struct cam_path *path, u_int count)
4361 struct cam_ed *dev = path->device;
4362 struct cam_devq *devq;
4365 devq = dev->sim->devq;
4366 mtx_lock(&devq->send_mtx);
4367 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4368 freeze = xpt_freeze_devq_device(dev, count);
4369 mtx_unlock(&devq->send_mtx);
4374 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4376 struct cam_devq *devq;
4380 mtx_lock(&devq->send_mtx);
4381 freeze = (devq->send_queue.qfrozen_cnt += count);
4382 mtx_unlock(&devq->send_mtx);
4387 xpt_release_devq_timeout(void *arg)
4390 struct cam_devq *devq;
4392 dev = (struct cam_ed *)arg;
4393 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4394 devq = dev->sim->devq;
4395 mtx_assert(&devq->send_mtx, MA_OWNED);
4396 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4401 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4404 struct cam_devq *devq;
4406 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4409 devq = dev->sim->devq;
4410 mtx_lock(&devq->send_mtx);
4411 if (xpt_release_devq_device(dev, count, run_queue))
4412 xpt_run_devq(dev->sim->devq);
4413 mtx_unlock(&devq->send_mtx);
4417 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4420 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4421 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4422 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4423 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4424 if (count > dev->ccbq.queue.qfrozen_cnt) {
4426 printf("xpt_release_devq(): requested %u > present %u\n",
4427 count, dev->ccbq.queue.qfrozen_cnt);
4429 count = dev->ccbq.queue.qfrozen_cnt;
4431 dev->ccbq.queue.qfrozen_cnt -= count;
4432 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4434 * No longer need to wait for a successful
4435 * command completion.
4437 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4439 * Remove any timeouts that might be scheduled
4440 * to release this queue.
4442 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4443 callout_stop(&dev->callout);
4444 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4447 * Now that we are unfrozen schedule the
4448 * device so any pending transactions are
4451 xpt_schedule_devq(dev->sim->devq, dev);
4458 xpt_release_simq(struct cam_sim *sim, int run_queue)
4460 struct cam_devq *devq;
4463 mtx_lock(&devq->send_mtx);
4464 if (devq->send_queue.qfrozen_cnt <= 0) {
4466 printf("xpt_release_simq: requested 1 > present %u\n",
4467 devq->send_queue.qfrozen_cnt);
4470 devq->send_queue.qfrozen_cnt--;
4471 if (devq->send_queue.qfrozen_cnt == 0) {
4473 * If there is a timeout scheduled to release this
4474 * sim queue, remove it. The queue frozen count is
4477 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4478 callout_stop(&sim->callout);
4479 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4483 * Now that we are unfrozen run the send queue.
4485 xpt_run_devq(sim->devq);
4488 mtx_unlock(&devq->send_mtx);
4492 * XXX Appears to be unused.
4495 xpt_release_simq_timeout(void *arg)
4497 struct cam_sim *sim;
4499 sim = (struct cam_sim *)arg;
4500 xpt_release_simq(sim, /* run_queue */ TRUE);
4504 xpt_done(union ccb *done_ccb)
4506 struct cam_doneq *queue;
4509 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4510 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4511 done_ccb->csio.bio != NULL)
4512 biotrack(done_ccb->csio.bio, __func__);
4515 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4516 ("xpt_done: func= %#x %s status %#x\n",
4517 done_ccb->ccb_h.func_code,
4518 xpt_action_name(done_ccb->ccb_h.func_code),
4519 done_ccb->ccb_h.status));
4520 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4523 /* Store the time the ccb was in the sim */
4524 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4525 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4526 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4527 queue = &cam_doneqs[hash];
4528 mtx_lock(&queue->cam_doneq_mtx);
4529 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4530 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4531 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4532 mtx_unlock(&queue->cam_doneq_mtx);
4534 wakeup(&queue->cam_doneq);
4538 xpt_done_direct(union ccb *done_ccb)
4541 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4542 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4543 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4546 /* Store the time the ccb was in the sim */
4547 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4548 xpt_done_process(&done_ccb->ccb_h);
4556 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4561 xpt_alloc_ccb_nowait()
4565 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4570 xpt_free_ccb(union ccb *free_ccb)
4572 free(free_ccb, M_CAMCCB);
4577 /* Private XPT functions */
4580 * Get a CAM control block for the caller. Charge the structure to the device
4581 * referenced by the path. If we don't have sufficient resources to allocate
4582 * more ccbs, we return NULL.
4585 xpt_get_ccb_nowait(struct cam_periph *periph)
4589 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4590 if (new_ccb == NULL)
4592 periph->periph_allocated++;
4593 cam_ccbq_take_opening(&periph->path->device->ccbq);
4598 xpt_get_ccb(struct cam_periph *periph)
4602 cam_periph_unlock(periph);
4603 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4604 cam_periph_lock(periph);
4605 periph->periph_allocated++;
4606 cam_ccbq_take_opening(&periph->path->device->ccbq);
4611 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4613 struct ccb_hdr *ccb_h;
4615 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4616 cam_periph_assert(periph, MA_OWNED);
4617 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4618 ccb_h->pinfo.priority != priority) {
4619 if (priority < periph->immediate_priority) {
4620 periph->immediate_priority = priority;
4621 xpt_run_allocq(periph, 0);
4623 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4626 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4627 return ((union ccb *)ccb_h);
4631 xpt_acquire_bus(struct cam_eb *bus)
4640 xpt_release_bus(struct cam_eb *bus)
4644 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4645 if (--bus->refcount > 0) {
4649 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4650 xsoftc.bus_generation++;
4652 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4653 ("destroying bus, but target list is not empty"));
4654 cam_sim_release(bus->sim);
4655 mtx_destroy(&bus->eb_mtx);
4656 free(bus, M_CAMXPT);
4659 static struct cam_et *
4660 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4662 struct cam_et *cur_target, *target;
4664 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4665 mtx_assert(&bus->eb_mtx, MA_OWNED);
4666 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4671 TAILQ_INIT(&target->ed_entries);
4673 target->target_id = target_id;
4674 target->refcount = 1;
4675 target->generation = 0;
4676 target->luns = NULL;
4677 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4678 timevalclear(&target->last_reset);
4680 * Hold a reference to our parent bus so it
4681 * will not go away before we do.
4685 /* Insertion sort into our bus's target list */
4686 cur_target = TAILQ_FIRST(&bus->et_entries);
4687 while (cur_target != NULL && cur_target->target_id < target_id)
4688 cur_target = TAILQ_NEXT(cur_target, links);
4689 if (cur_target != NULL) {
4690 TAILQ_INSERT_BEFORE(cur_target, target, links);
4692 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4699 xpt_acquire_target(struct cam_et *target)
4701 struct cam_eb *bus = target->bus;
4703 mtx_lock(&bus->eb_mtx);
4705 mtx_unlock(&bus->eb_mtx);
4709 xpt_release_target(struct cam_et *target)
4711 struct cam_eb *bus = target->bus;
4713 mtx_lock(&bus->eb_mtx);
4714 if (--target->refcount > 0) {
4715 mtx_unlock(&bus->eb_mtx);
4718 TAILQ_REMOVE(&bus->et_entries, target, links);
4720 mtx_unlock(&bus->eb_mtx);
4721 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4722 ("destroying target, but device list is not empty"));
4723 xpt_release_bus(bus);
4724 mtx_destroy(&target->luns_mtx);
4726 free(target->luns, M_CAMXPT);
4727 free(target, M_CAMXPT);
4730 static struct cam_ed *
4731 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4734 struct cam_ed *device;
4736 device = xpt_alloc_device(bus, target, lun_id);
4740 device->mintags = 1;
4741 device->maxtags = 1;
4746 xpt_destroy_device(void *context, int pending)
4748 struct cam_ed *device = context;
4750 mtx_lock(&device->device_mtx);
4751 mtx_destroy(&device->device_mtx);
4752 free(device, M_CAMDEV);
4756 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4758 struct cam_ed *cur_device, *device;
4759 struct cam_devq *devq;
4762 mtx_assert(&bus->eb_mtx, MA_OWNED);
4763 /* Make space for us in the device queue on our bus */
4764 devq = bus->sim->devq;
4765 mtx_lock(&devq->send_mtx);
4766 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4767 mtx_unlock(&devq->send_mtx);
4768 if (status != CAM_REQ_CMP)
4771 device = (struct cam_ed *)malloc(sizeof(*device),
4772 M_CAMDEV, M_NOWAIT|M_ZERO);
4776 cam_init_pinfo(&device->devq_entry);
4777 device->target = target;
4778 device->lun_id = lun_id;
4779 device->sim = bus->sim;
4780 if (cam_ccbq_init(&device->ccbq,
4781 bus->sim->max_dev_openings) != 0) {
4782 free(device, M_CAMDEV);
4785 SLIST_INIT(&device->asyncs);
4786 SLIST_INIT(&device->periphs);
4787 device->generation = 0;
4788 device->flags = CAM_DEV_UNCONFIGURED;
4789 device->tag_delay_count = 0;
4790 device->tag_saved_openings = 0;
4791 device->refcount = 1;
4792 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4793 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4794 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4796 * Hold a reference to our parent bus so it
4797 * will not go away before we do.
4801 cur_device = TAILQ_FIRST(&target->ed_entries);
4802 while (cur_device != NULL && cur_device->lun_id < lun_id)
4803 cur_device = TAILQ_NEXT(cur_device, links);
4804 if (cur_device != NULL)
4805 TAILQ_INSERT_BEFORE(cur_device, device, links);
4807 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4808 target->generation++;
4813 xpt_acquire_device(struct cam_ed *device)
4815 struct cam_eb *bus = device->target->bus;
4817 mtx_lock(&bus->eb_mtx);
4819 mtx_unlock(&bus->eb_mtx);
4823 xpt_release_device(struct cam_ed *device)
4825 struct cam_eb *bus = device->target->bus;
4826 struct cam_devq *devq;
4828 mtx_lock(&bus->eb_mtx);
4829 if (--device->refcount > 0) {
4830 mtx_unlock(&bus->eb_mtx);
4834 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4835 device->target->generation++;
4836 mtx_unlock(&bus->eb_mtx);
4838 /* Release our slot in the devq */
4839 devq = bus->sim->devq;
4840 mtx_lock(&devq->send_mtx);
4841 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4842 mtx_unlock(&devq->send_mtx);
4844 KASSERT(SLIST_EMPTY(&device->periphs),
4845 ("destroying device, but periphs list is not empty"));
4846 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4847 ("destroying device while still queued for ccbs"));
4849 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4850 callout_stop(&device->callout);
4852 xpt_release_target(device->target);
4854 cam_ccbq_fini(&device->ccbq);
4856 * Free allocated memory. free(9) does nothing if the
4857 * supplied pointer is NULL, so it is safe to call without
4860 free(device->supported_vpds, M_CAMXPT);
4861 free(device->device_id, M_CAMXPT);
4862 free(device->ext_inq, M_CAMXPT);
4863 free(device->physpath, M_CAMXPT);
4864 free(device->rcap_buf, M_CAMXPT);
4865 free(device->serial_num, M_CAMXPT);
4866 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4870 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4876 mtx_lock(&dev->sim->devq->send_mtx);
4877 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4878 mtx_unlock(&dev->sim->devq->send_mtx);
4879 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4880 || (dev->inq_flags & SID_CmdQue) != 0)
4881 dev->tag_saved_openings = newopenings;
4885 static struct cam_eb *
4886 xpt_find_bus(path_id_t path_id)
4891 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4893 bus = TAILQ_NEXT(bus, links)) {
4894 if (bus->path_id == path_id) {
4903 static struct cam_et *
4904 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4906 struct cam_et *target;
4908 mtx_assert(&bus->eb_mtx, MA_OWNED);
4909 for (target = TAILQ_FIRST(&bus->et_entries);
4911 target = TAILQ_NEXT(target, links)) {
4912 if (target->target_id == target_id) {
4920 static struct cam_ed *
4921 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4923 struct cam_ed *device;
4925 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4926 for (device = TAILQ_FIRST(&target->ed_entries);
4928 device = TAILQ_NEXT(device, links)) {
4929 if (device->lun_id == lun_id) {
4938 xpt_start_tags(struct cam_path *path)
4940 struct ccb_relsim crs;
4941 struct cam_ed *device;
4942 struct cam_sim *sim;
4945 device = path->device;
4946 sim = path->bus->sim;
4947 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4948 xpt_freeze_devq(path, /*count*/1);
4949 device->inq_flags |= SID_CmdQue;
4950 if (device->tag_saved_openings != 0)
4951 newopenings = device->tag_saved_openings;
4953 newopenings = min(device->maxtags,
4954 sim->max_tagged_dev_openings);
4955 xpt_dev_ccbq_resize(path, newopenings);
4956 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4957 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4958 crs.ccb_h.func_code = XPT_REL_SIMQ;
4959 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4961 = crs.release_timeout
4964 xpt_action((union ccb *)&crs);
4968 xpt_stop_tags(struct cam_path *path)
4970 struct ccb_relsim crs;
4971 struct cam_ed *device;
4972 struct cam_sim *sim;
4974 device = path->device;
4975 sim = path->bus->sim;
4976 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4977 device->tag_delay_count = 0;
4978 xpt_freeze_devq(path, /*count*/1);
4979 device->inq_flags &= ~SID_CmdQue;
4980 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4981 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4982 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4983 crs.ccb_h.func_code = XPT_REL_SIMQ;
4984 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4986 = crs.release_timeout
4989 xpt_action((union ccb *)&crs);
4993 xpt_boot_delay(void *arg)
5000 xpt_config(void *arg)
5003 * Now that interrupts are enabled, go find our devices
5005 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5006 printf("xpt_config: failed to create taskqueue thread.\n");
5008 /* Setup debugging path */
5009 if (cam_dflags != CAM_DEBUG_NONE) {
5010 if (xpt_create_path(&cam_dpath, NULL,
5011 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5012 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5013 printf("xpt_config: xpt_create_path() failed for debug"
5014 " target %d:%d:%d, debugging disabled\n",
5015 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5016 cam_dflags = CAM_DEBUG_NONE;
5021 periphdriver_init(1);
5023 callout_init(&xsoftc.boot_callout, 1);
5024 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5025 xpt_boot_delay, NULL, 0);
5026 /* Fire up rescan thread. */
5027 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5028 "cam", "scanner")) {
5029 printf("xpt_config: failed to create rescan thread.\n");
5037 xsoftc.buses_to_config++;
5042 xpt_release_boot(void)
5045 xsoftc.buses_to_config--;
5046 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5047 struct xpt_task *task;
5049 xsoftc.buses_config_done = 1;
5051 /* Call manually because we don't have any buses */
5052 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5054 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5055 taskqueue_enqueue(taskqueue_thread, &task->task);
5062 * If the given device only has one peripheral attached to it, and if that
5063 * peripheral is the passthrough driver, announce it. This insures that the
5064 * user sees some sort of announcement for every peripheral in their system.
5067 xptpassannouncefunc(struct cam_ed *device, void *arg)
5069 struct cam_periph *periph;
5072 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5073 periph = SLIST_NEXT(periph, periph_links), i++);
5075 periph = SLIST_FIRST(&device->periphs);
5077 && (strncmp(periph->periph_name, "pass", 4) == 0))
5078 xpt_announce_periph(periph, NULL);
5084 xpt_finishconfig_task(void *context, int pending)
5087 periphdriver_init(2);
5089 * Check for devices with no "standard" peripheral driver
5090 * attached. For any devices like that, announce the
5091 * passthrough driver so the user will see something.
5094 xpt_for_all_devices(xptpassannouncefunc, NULL);
5096 /* Release our hook so that the boot can continue. */
5097 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5098 free(xsoftc.xpt_config_hook, M_CAMXPT);
5099 xsoftc.xpt_config_hook = NULL;
5101 free(context, M_CAMXPT);
5105 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5106 struct cam_path *path)
5108 struct ccb_setasync csa;
5113 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5114 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5115 if (status != CAM_REQ_CMP)
5117 xpt_path_lock(path);
5121 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5122 csa.ccb_h.func_code = XPT_SASYNC_CB;
5123 csa.event_enable = event;
5124 csa.callback = cbfunc;
5125 csa.callback_arg = cbarg;
5126 xpt_action((union ccb *)&csa);
5127 status = csa.ccb_h.status;
5129 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5130 ("xpt_register_async: func %p\n", cbfunc));
5133 xpt_path_unlock(path);
5134 xpt_free_path(path);
5137 if ((status == CAM_REQ_CMP) &&
5138 (csa.event_enable & AC_FOUND_DEVICE)) {
5140 * Get this peripheral up to date with all
5141 * the currently existing devices.
5143 xpt_for_all_devices(xptsetasyncfunc, &csa);
5145 if ((status == CAM_REQ_CMP) &&
5146 (csa.event_enable & AC_PATH_REGISTERED)) {
5148 * Get this peripheral up to date with all
5149 * the currently existing buses.
5151 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5158 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5160 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5162 switch (work_ccb->ccb_h.func_code) {
5163 /* Common cases first */
5164 case XPT_PATH_INQ: /* Path routing inquiry */
5166 struct ccb_pathinq *cpi;
5168 cpi = &work_ccb->cpi;
5169 cpi->version_num = 1; /* XXX??? */
5170 cpi->hba_inquiry = 0;
5171 cpi->target_sprt = 0;
5173 cpi->hba_eng_cnt = 0;
5174 cpi->max_target = 0;
5176 cpi->initiator_id = 0;
5177 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5178 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5179 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5180 cpi->unit_number = sim->unit_number;
5181 cpi->bus_id = sim->bus_id;
5182 cpi->base_transfer_speed = 0;
5183 cpi->protocol = PROTO_UNSPECIFIED;
5184 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5185 cpi->transport = XPORT_UNSPECIFIED;
5186 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5187 cpi->ccb_h.status = CAM_REQ_CMP;
5192 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5199 * The xpt as a "controller" has no interrupt sources, so polling
5203 xptpoll(struct cam_sim *sim)
5208 xpt_lock_buses(void)
5210 mtx_lock(&xsoftc.xpt_topo_lock);
5214 xpt_unlock_buses(void)
5216 mtx_unlock(&xsoftc.xpt_topo_lock);
5220 xpt_path_mtx(struct cam_path *path)
5223 return (&path->device->device_mtx);
5227 xpt_done_process(struct ccb_hdr *ccb_h)
5229 struct cam_sim *sim;
5230 struct cam_devq *devq;
5231 struct mtx *mtx = NULL;
5233 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5234 struct ccb_scsiio *csio;
5236 if (ccb_h->func_code == XPT_SCSI_IO) {
5237 csio = &((union ccb *)ccb_h)->csio;
5238 if (csio->bio != NULL)
5239 biotrack(csio->bio, __func__);
5243 if (ccb_h->flags & CAM_HIGH_POWER) {
5244 struct highpowerlist *hphead;
5245 struct cam_ed *device;
5247 mtx_lock(&xsoftc.xpt_highpower_lock);
5248 hphead = &xsoftc.highpowerq;
5250 device = STAILQ_FIRST(hphead);
5253 * Increment the count since this command is done.
5255 xsoftc.num_highpower++;
5258 * Any high powered commands queued up?
5260 if (device != NULL) {
5262 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5263 mtx_unlock(&xsoftc.xpt_highpower_lock);
5265 mtx_lock(&device->sim->devq->send_mtx);
5266 xpt_release_devq_device(device,
5267 /*count*/1, /*runqueue*/TRUE);
5268 mtx_unlock(&device->sim->devq->send_mtx);
5270 mtx_unlock(&xsoftc.xpt_highpower_lock);
5273 sim = ccb_h->path->bus->sim;
5275 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5276 xpt_release_simq(sim, /*run_queue*/FALSE);
5277 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5280 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5281 && (ccb_h->status & CAM_DEV_QFRZN)) {
5282 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5283 ccb_h->status &= ~CAM_DEV_QFRZN;
5287 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5288 struct cam_ed *dev = ccb_h->path->device;
5290 mtx_lock(&devq->send_mtx);
5291 devq->send_active--;
5292 devq->send_openings++;
5293 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5295 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5296 && (dev->ccbq.dev_active == 0))) {
5297 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5298 xpt_release_devq_device(dev, /*count*/1,
5299 /*run_queue*/FALSE);
5302 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5303 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5304 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5305 xpt_release_devq_device(dev, /*count*/1,
5306 /*run_queue*/FALSE);
5309 if (!device_is_queued(dev))
5310 (void)xpt_schedule_devq(devq, dev);
5312 mtx_unlock(&devq->send_mtx);
5314 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5315 mtx = xpt_path_mtx(ccb_h->path);
5318 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5319 && (--dev->tag_delay_count == 0))
5320 xpt_start_tags(ccb_h->path);
5324 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5326 mtx = xpt_path_mtx(ccb_h->path);
5336 /* Call the peripheral driver's callback */
5337 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5338 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5344 xpt_done_td(void *arg)
5346 struct cam_doneq *queue = arg;
5347 struct ccb_hdr *ccb_h;
5348 STAILQ_HEAD(, ccb_hdr) doneq;
5350 STAILQ_INIT(&doneq);
5351 mtx_lock(&queue->cam_doneq_mtx);
5353 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5354 queue->cam_doneq_sleep = 1;
5355 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5357 queue->cam_doneq_sleep = 0;
5359 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5360 mtx_unlock(&queue->cam_doneq_mtx);
5362 THREAD_NO_SLEEPING();
5363 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5364 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5365 xpt_done_process(ccb_h);
5367 THREAD_SLEEPING_OK();
5369 mtx_lock(&queue->cam_doneq_mtx);
5374 camisr_runqueue(void)
5376 struct ccb_hdr *ccb_h;
5377 struct cam_doneq *queue;
5380 /* Process global queues. */
5381 for (i = 0; i < cam_num_doneqs; i++) {
5382 queue = &cam_doneqs[i];
5383 mtx_lock(&queue->cam_doneq_mtx);
5384 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5385 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5386 mtx_unlock(&queue->cam_doneq_mtx);
5387 xpt_done_process(ccb_h);
5388 mtx_lock(&queue->cam_doneq_mtx);
5390 mtx_unlock(&queue->cam_doneq_mtx);
5400 static struct kv map[] = {
5401 { XPT_NOOP, "XPT_NOOP" },
5402 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5403 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5404 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5405 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5406 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5407 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5408 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5409 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5410 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5411 { XPT_DEBUG, "XPT_DEBUG" },
5412 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5413 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5414 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5415 { XPT_ASYNC, "XPT_ASYNC" },
5416 { XPT_ABORT, "XPT_ABORT" },
5417 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5418 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5419 { XPT_TERM_IO, "XPT_TERM_IO" },
5420 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5421 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5422 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5423 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5424 { XPT_ATA_IO, "XPT_ATA_IO" },
5425 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5426 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5427 { XPT_NVME_IO, "XPT_NVME_IO" },
5428 { XPT_MMCSD_IO, "XPT_MMCSD_IO" },
5429 { XPT_SMP_IO, "XPT_SMP_IO" },
5430 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5431 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5432 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5433 { XPT_EN_LUN, "XPT_EN_LUN" },
5434 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5435 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5436 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5437 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5438 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5439 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5440 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5445 xpt_action_name(uint32_t action)
5447 static char buffer[32]; /* Only for unknown messages -- racy */
5448 struct kv *walker = map;
5450 while (walker->name != NULL) {
5451 if (walker->v == action)
5452 return (walker->name);
5456 snprintf(buffer, sizeof(buffer), "%#x", action);