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_iosched.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_queue.h>
61 #include <cam/cam_sim.h>
62 #include <cam/cam_xpt.h>
63 #include <cam/cam_xpt_sim.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_xpt_internal.h>
66 #include <cam/cam_debug.h>
67 #include <cam/cam_compat.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_message.h>
71 #include <cam/scsi/scsi_pass.h>
73 #include <machine/md_var.h> /* geometry translation */
74 #include <machine/stdarg.h> /* for xpt_print below */
78 /* Wild guess based on not wanting to grow the stack too much */
79 #define XPT_PRINT_MAXLEN 512
80 #ifdef PRINTF_BUFR_SIZE
81 #define XPT_PRINT_LEN PRINTF_BUFR_SIZE
83 #define XPT_PRINT_LEN 128
85 _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
88 * This is the maximum number of high powered commands (e.g. start unit)
89 * that can be outstanding at a particular time.
91 #ifndef CAM_MAX_HIGHPOWER
92 #define CAM_MAX_HIGHPOWER 4
95 /* Datastructures internal to the xpt layer */
96 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
97 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
98 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
99 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
101 /* Object for defering XPT actions to a taskqueue */
109 uint32_t xpt_generation;
111 /* number of high powered commands that can go through right now */
112 struct mtx xpt_highpower_lock;
113 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
116 /* queue for handling async rescan requests. */
117 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
119 int buses_config_done;
125 * N.B., "busses" is an archaic spelling of "buses". In new code
126 * "buses" is preferred.
128 TAILQ_HEAD(,cam_eb) xpt_busses;
129 u_int bus_generation;
131 struct intr_config_hook *xpt_config_hook;
134 struct callout boot_callout;
136 struct mtx xpt_topo_lock;
138 struct taskqueue *xpt_taskq;
143 DM_RET_FLAG_MASK = 0x0f,
146 DM_RET_DESCEND = 0x20,
148 DM_RET_ACTION_MASK = 0xf0
156 } xpt_traverse_depth;
158 struct xpt_traverse_config {
159 xpt_traverse_depth depth;
164 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
165 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
166 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
167 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
168 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
170 /* Transport layer configuration information */
171 static struct xpt_softc xsoftc;
173 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
175 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
176 &xsoftc.boot_delay, 0, "Bus registration wait time");
177 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
178 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
179 SYSCTL_INT(_kern_cam, OID_AUTO, announce_nosbuf, CTLFLAG_RWTUN,
180 &xsoftc.announce_nosbuf, 0, "Don't use sbuf for announcements");
183 struct mtx_padalign cam_doneq_mtx;
184 STAILQ_HEAD(, ccb_hdr) cam_doneq;
188 static struct cam_doneq cam_doneqs[MAXCPU];
189 static int cam_num_doneqs;
190 static struct proc *cam_proc;
192 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
193 &cam_num_doneqs, 0, "Number of completion queues/threads");
195 struct cam_periph *xpt_periph;
197 static periph_init_t xpt_periph_init;
199 static struct periph_driver xpt_driver =
201 xpt_periph_init, "xpt",
202 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
206 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
208 static d_open_t xptopen;
209 static d_close_t xptclose;
210 static d_ioctl_t xptioctl;
211 static d_ioctl_t xptdoioctl;
213 static struct cdevsw xpt_cdevsw = {
214 .d_version = D_VERSION,
222 /* Storage for debugging datastructures */
223 struct cam_path *cam_dpath;
224 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
225 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
226 &cam_dflags, 0, "Enabled debug flags");
227 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
228 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
229 &cam_debug_delay, 0, "Delay in us after each debug message");
231 /* Our boot-time initialization hook */
232 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
234 static moduledata_t cam_moduledata = {
236 cam_module_event_handler,
240 static int xpt_init(void *);
242 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
243 MODULE_VERSION(cam, 1);
246 static void xpt_async_bcast(struct async_list *async_head,
247 u_int32_t async_code,
248 struct cam_path *path,
250 static path_id_t xptnextfreepathid(void);
251 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
252 static union ccb *xpt_get_ccb(struct cam_periph *periph);
253 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
254 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
255 static void xpt_run_allocq_task(void *context, int pending);
256 static void xpt_run_devq(struct cam_devq *devq);
257 static timeout_t xpt_release_devq_timeout;
258 static void xpt_release_simq_timeout(void *arg) __unused;
259 static void xpt_acquire_bus(struct cam_eb *bus);
260 static void xpt_release_bus(struct cam_eb *bus);
261 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
262 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
264 static struct cam_et*
265 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
266 static void xpt_acquire_target(struct cam_et *target);
267 static void xpt_release_target(struct cam_et *target);
268 static struct cam_eb*
269 xpt_find_bus(path_id_t path_id);
270 static struct cam_et*
271 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
272 static struct cam_ed*
273 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
274 static void xpt_config(void *arg);
275 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
276 u_int32_t new_priority);
277 static xpt_devicefunc_t xptpassannouncefunc;
278 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
279 static void xptpoll(struct cam_sim *sim);
280 static void camisr_runqueue(void);
281 static void xpt_done_process(struct ccb_hdr *ccb_h);
282 static void xpt_done_td(void *);
283 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
284 u_int num_patterns, struct cam_eb *bus);
285 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
287 struct cam_ed *device);
288 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
290 struct cam_periph *periph);
291 static xpt_busfunc_t xptedtbusfunc;
292 static xpt_targetfunc_t xptedttargetfunc;
293 static xpt_devicefunc_t xptedtdevicefunc;
294 static xpt_periphfunc_t xptedtperiphfunc;
295 static xpt_pdrvfunc_t xptplistpdrvfunc;
296 static xpt_periphfunc_t xptplistperiphfunc;
297 static int xptedtmatch(struct ccb_dev_match *cdm);
298 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
299 static int xptbustraverse(struct cam_eb *start_bus,
300 xpt_busfunc_t *tr_func, void *arg);
301 static int xpttargettraverse(struct cam_eb *bus,
302 struct cam_et *start_target,
303 xpt_targetfunc_t *tr_func, void *arg);
304 static int xptdevicetraverse(struct cam_et *target,
305 struct cam_ed *start_device,
306 xpt_devicefunc_t *tr_func, void *arg);
307 static int xptperiphtraverse(struct cam_ed *device,
308 struct cam_periph *start_periph,
309 xpt_periphfunc_t *tr_func, void *arg);
310 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
311 xpt_pdrvfunc_t *tr_func, void *arg);
312 static int xptpdperiphtraverse(struct periph_driver **pdrv,
313 struct cam_periph *start_periph,
314 xpt_periphfunc_t *tr_func,
316 static xpt_busfunc_t xptdefbusfunc;
317 static xpt_targetfunc_t xptdeftargetfunc;
318 static xpt_devicefunc_t xptdefdevicefunc;
319 static xpt_periphfunc_t xptdefperiphfunc;
320 static void xpt_finishconfig_task(void *context, int pending);
321 static void xpt_dev_async_default(u_int32_t async_code,
323 struct cam_et *target,
324 struct cam_ed *device,
326 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
327 struct cam_et *target,
329 static xpt_devicefunc_t xptsetasyncfunc;
330 static xpt_busfunc_t xptsetasyncbusfunc;
331 static cam_status xptregister(struct cam_periph *periph,
333 static __inline int device_is_queued(struct cam_ed *device);
336 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
340 mtx_assert(&devq->send_mtx, MA_OWNED);
341 if ((dev->ccbq.queue.entries > 0) &&
342 (dev->ccbq.dev_openings > 0) &&
343 (dev->ccbq.queue.qfrozen_cnt == 0)) {
345 * The priority of a device waiting for controller
346 * resources is that of the highest priority CCB
350 xpt_schedule_dev(&devq->send_queue,
352 CAMQ_GET_PRIO(&dev->ccbq.queue));
360 device_is_queued(struct cam_ed *device)
362 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
368 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
372 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
376 * Only allow read-write access.
378 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
382 * We don't allow nonblocking access.
384 if ((flags & O_NONBLOCK) != 0) {
385 printf("%s: can't do nonblocking access\n", devtoname(dev));
393 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
400 * Don't automatically grab the xpt softc lock here even though this is going
401 * through the xpt device. The xpt device is really just a back door for
402 * accessing other devices and SIMs, so the right thing to do is to grab
403 * the appropriate SIM lock once the bus/SIM is located.
406 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
410 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
411 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
417 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
425 * For the transport layer CAMIOCOMMAND ioctl, we really only want
426 * to accept CCB types that don't quite make sense to send through a
427 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
435 inccb = (union ccb *)addr;
436 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
437 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
438 inccb->csio.bio = NULL;
441 if (inccb->ccb_h.flags & CAM_UNLOCKED)
444 bus = xpt_find_bus(inccb->ccb_h.path_id);
448 switch (inccb->ccb_h.func_code) {
451 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
452 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
453 xpt_release_bus(bus);
458 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
459 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
460 xpt_release_bus(bus);
468 switch(inccb->ccb_h.func_code) {
476 ccb = xpt_alloc_ccb();
479 * Create a path using the bus, target, and lun the
482 if (xpt_create_path(&ccb->ccb_h.path, NULL,
483 inccb->ccb_h.path_id,
484 inccb->ccb_h.target_id,
485 inccb->ccb_h.target_lun) !=
491 /* Ensure all of our fields are correct */
492 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
493 inccb->ccb_h.pinfo.priority);
494 xpt_merge_ccb(ccb, inccb);
495 xpt_path_lock(ccb->ccb_h.path);
496 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
497 xpt_path_unlock(ccb->ccb_h.path);
498 bcopy(ccb, inccb, sizeof(union ccb));
499 xpt_free_path(ccb->ccb_h.path);
507 * This is an immediate CCB, so it's okay to
508 * allocate it on the stack.
512 * Create a path using the bus, target, and lun the
515 if (xpt_create_path(&ccb.ccb_h.path, NULL,
516 inccb->ccb_h.path_id,
517 inccb->ccb_h.target_id,
518 inccb->ccb_h.target_lun) !=
523 /* Ensure all of our fields are correct */
524 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
525 inccb->ccb_h.pinfo.priority);
526 xpt_merge_ccb(&ccb, inccb);
528 bcopy(&ccb, inccb, sizeof(union ccb));
529 xpt_free_path(ccb.ccb_h.path);
533 case XPT_DEV_MATCH: {
534 struct cam_periph_map_info mapinfo;
535 struct cam_path *old_path;
538 * We can't deal with physical addresses for this
539 * type of transaction.
541 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
548 * Save this in case the caller had it set to
549 * something in particular.
551 old_path = inccb->ccb_h.path;
554 * We really don't need a path for the matching
555 * code. The path is needed because of the
556 * debugging statements in xpt_action(). They
557 * assume that the CCB has a valid path.
559 inccb->ccb_h.path = xpt_periph->path;
561 bzero(&mapinfo, sizeof(mapinfo));
564 * Map the pattern and match buffers into kernel
565 * virtual address space.
567 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
570 inccb->ccb_h.path = old_path;
575 * This is an immediate CCB, we can send it on directly.
580 * Map the buffers back into user space.
582 cam_periph_unmapmem(inccb, &mapinfo);
584 inccb->ccb_h.path = old_path;
593 xpt_release_bus(bus);
597 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
598 * with the periphal driver name and unit name filled in. The other
599 * fields don't really matter as input. The passthrough driver name
600 * ("pass"), and unit number are passed back in the ccb. The current
601 * device generation number, and the index into the device peripheral
602 * driver list, and the status are also passed back. Note that
603 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
604 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
605 * (or rather should be) impossible for the device peripheral driver
606 * list to change since we look at the whole thing in one pass, and
607 * we do it with lock protection.
610 case CAMGETPASSTHRU: {
612 struct cam_periph *periph;
613 struct periph_driver **p_drv;
616 int base_periph_found;
618 ccb = (union ccb *)addr;
619 unit = ccb->cgdl.unit_number;
620 name = ccb->cgdl.periph_name;
621 base_periph_found = 0;
622 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
623 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
624 ccb->csio.bio = NULL;
628 * Sanity check -- make sure we don't get a null peripheral
631 if (*ccb->cgdl.periph_name == '\0') {
636 /* Keep the list from changing while we traverse it */
639 /* first find our driver in the list of drivers */
640 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
641 if (strcmp((*p_drv)->driver_name, name) == 0)
644 if (*p_drv == NULL) {
646 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
647 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
648 *ccb->cgdl.periph_name = '\0';
649 ccb->cgdl.unit_number = 0;
655 * Run through every peripheral instance of this driver
656 * and check to see whether it matches the unit passed
657 * in by the user. If it does, get out of the loops and
658 * find the passthrough driver associated with that
661 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
662 periph = TAILQ_NEXT(periph, unit_links)) {
664 if (periph->unit_number == unit)
668 * If we found the peripheral driver that the user passed
669 * in, go through all of the peripheral drivers for that
670 * particular device and look for a passthrough driver.
672 if (periph != NULL) {
673 struct cam_ed *device;
676 base_periph_found = 1;
677 device = periph->path->device;
678 for (i = 0, periph = SLIST_FIRST(&device->periphs);
680 periph = SLIST_NEXT(periph, periph_links), i++) {
682 * Check to see whether we have a
683 * passthrough device or not.
685 if (strcmp(periph->periph_name, "pass") == 0) {
687 * Fill in the getdevlist fields.
689 strcpy(ccb->cgdl.periph_name,
690 periph->periph_name);
691 ccb->cgdl.unit_number =
693 if (SLIST_NEXT(periph, periph_links))
695 CAM_GDEVLIST_MORE_DEVS;
698 CAM_GDEVLIST_LAST_DEVICE;
699 ccb->cgdl.generation =
703 * Fill in some CCB header fields
704 * that the user may want.
707 periph->path->bus->path_id;
708 ccb->ccb_h.target_id =
709 periph->path->target->target_id;
710 ccb->ccb_h.target_lun =
711 periph->path->device->lun_id;
712 ccb->ccb_h.status = CAM_REQ_CMP;
719 * If the periph is null here, one of two things has
720 * happened. The first possibility is that we couldn't
721 * find the unit number of the particular peripheral driver
722 * that the user is asking about. e.g. the user asks for
723 * the passthrough driver for "da11". We find the list of
724 * "da" peripherals all right, but there is no unit 11.
725 * The other possibility is that we went through the list
726 * of peripheral drivers attached to the device structure,
727 * but didn't find one with the name "pass". Either way,
728 * we return ENOENT, since we couldn't find something.
730 if (periph == NULL) {
731 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
732 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
733 *ccb->cgdl.periph_name = '\0';
734 ccb->cgdl.unit_number = 0;
737 * It is unfortunate that this is even necessary,
738 * but there are many, many clueless users out there.
739 * If this is true, the user is looking for the
740 * passthrough driver, but doesn't have one in his
743 if (base_periph_found == 1) {
744 printf("xptioctl: pass driver is not in the "
746 printf("xptioctl: put \"device pass\" in "
747 "your kernel config file\n");
762 cam_module_event_handler(module_t mod, int what, void *arg)
768 if ((error = xpt_init(NULL)) != 0)
780 static struct xpt_proto *
781 xpt_proto_find(cam_proto proto)
783 struct xpt_proto **pp;
785 SET_FOREACH(pp, cam_xpt_proto_set) {
786 if ((*pp)->proto == proto)
794 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
797 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
798 xpt_free_path(done_ccb->ccb_h.path);
799 xpt_free_ccb(done_ccb);
801 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
802 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
807 /* thread to handle bus rescans */
809 xpt_scanner_thread(void *dummy)
812 struct cam_path path;
816 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
817 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
819 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
820 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
824 * Since lock can be dropped inside and path freed
825 * by completion callback even before return here,
826 * take our own path copy for reference.
828 xpt_copy_path(&path, ccb->ccb_h.path);
829 xpt_path_lock(&path);
831 xpt_path_unlock(&path);
832 xpt_release_path(&path);
840 xpt_rescan(union ccb *ccb)
844 /* Prepare request */
845 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_BUS;
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_TGT;
851 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
852 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
853 ccb->ccb_h.func_code = XPT_SCAN_LUN;
855 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
856 xpt_free_path(ccb->ccb_h.path);
860 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
861 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
862 xpt_action_name(ccb->ccb_h.func_code)));
864 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
865 ccb->ccb_h.cbfcnp = xpt_rescan_done;
866 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
867 /* Don't make duplicate entries for the same paths. */
869 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
870 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
871 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
872 wakeup(&xsoftc.ccb_scanq);
874 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
875 xpt_free_path(ccb->ccb_h.path);
881 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
882 xsoftc.buses_to_config++;
883 wakeup(&xsoftc.ccb_scanq);
887 /* Functions accessed by the peripheral drivers */
889 xpt_init(void *dummy)
891 struct cam_sim *xpt_sim;
892 struct cam_path *path;
893 struct cam_devq *devq;
897 TAILQ_INIT(&xsoftc.xpt_busses);
898 TAILQ_INIT(&xsoftc.ccb_scanq);
899 STAILQ_INIT(&xsoftc.highpowerq);
900 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
902 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
903 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
904 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
905 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
907 #ifdef CAM_BOOT_DELAY
909 * Override this value at compile time to assist our users
910 * who don't use loader to boot a kernel.
912 xsoftc.boot_delay = CAM_BOOT_DELAY;
915 * The xpt layer is, itself, the equivalent of a SIM.
916 * Allow 16 ccbs in the ccb pool for it. This should
917 * give decent parallelism when we probe buses and
918 * perform other XPT functions.
920 devq = cam_simq_alloc(16);
921 xpt_sim = cam_sim_alloc(xptaction,
926 /*mtx*/&xsoftc.xpt_lock,
927 /*max_dev_transactions*/0,
928 /*max_tagged_dev_transactions*/0,
933 mtx_lock(&xsoftc.xpt_lock);
934 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
935 mtx_unlock(&xsoftc.xpt_lock);
936 printf("xpt_init: xpt_bus_register failed with status %#x,"
937 " failing attach\n", status);
940 mtx_unlock(&xsoftc.xpt_lock);
943 * Looking at the XPT from the SIM layer, the XPT is
944 * the equivalent of a peripheral driver. Allocate
945 * a peripheral driver entry for us.
947 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
949 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
950 printf("xpt_init: xpt_create_path failed with status %#x,"
951 " failing attach\n", status);
955 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
956 path, NULL, 0, xpt_sim);
957 xpt_path_unlock(path);
960 if (cam_num_doneqs < 1)
961 cam_num_doneqs = 1 + mp_ncpus / 6;
962 else if (cam_num_doneqs > MAXCPU)
963 cam_num_doneqs = MAXCPU;
964 for (i = 0; i < cam_num_doneqs; i++) {
965 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
967 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
968 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
969 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
975 if (cam_num_doneqs < 1) {
976 printf("xpt_init: Cannot init completion queues "
977 "- failing attach\n");
981 * Register a callback for when interrupts are enabled.
983 xsoftc.xpt_config_hook =
984 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
985 M_CAMXPT, M_NOWAIT | M_ZERO);
986 if (xsoftc.xpt_config_hook == NULL) {
987 printf("xpt_init: Cannot malloc config hook "
988 "- failing attach\n");
991 xsoftc.xpt_config_hook->ich_func = xpt_config;
992 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
993 free (xsoftc.xpt_config_hook, M_CAMXPT);
994 printf("xpt_init: config_intrhook_establish failed "
995 "- failing attach\n");
1002 xptregister(struct cam_periph *periph, void *arg)
1004 struct cam_sim *xpt_sim;
1006 if (periph == NULL) {
1007 printf("xptregister: periph was NULL!!\n");
1008 return(CAM_REQ_CMP_ERR);
1011 xpt_sim = (struct cam_sim *)arg;
1012 xpt_sim->softc = periph;
1013 xpt_periph = periph;
1014 periph->softc = NULL;
1016 return(CAM_REQ_CMP);
1020 xpt_add_periph(struct cam_periph *periph)
1022 struct cam_ed *device;
1025 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1026 device = periph->path->device;
1027 status = CAM_REQ_CMP;
1028 if (device != NULL) {
1029 mtx_lock(&device->target->bus->eb_mtx);
1030 device->generation++;
1031 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1032 mtx_unlock(&device->target->bus->eb_mtx);
1033 atomic_add_32(&xsoftc.xpt_generation, 1);
1040 xpt_remove_periph(struct cam_periph *periph)
1042 struct cam_ed *device;
1044 device = periph->path->device;
1045 if (device != NULL) {
1046 mtx_lock(&device->target->bus->eb_mtx);
1047 device->generation++;
1048 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1049 mtx_unlock(&device->target->bus->eb_mtx);
1050 atomic_add_32(&xsoftc.xpt_generation, 1);
1056 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1058 struct cam_path *path = periph->path;
1059 struct xpt_proto *proto;
1061 cam_periph_assert(periph, MA_OWNED);
1062 periph->flags |= CAM_PERIPH_ANNOUNCED;
1064 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1065 periph->periph_name, periph->unit_number,
1066 path->bus->sim->sim_name,
1067 path->bus->sim->unit_number,
1068 path->bus->sim->bus_id,
1070 path->target->target_id,
1071 (uintmax_t)path->device->lun_id);
1072 printf("%s%d: ", periph->periph_name, periph->unit_number);
1073 proto = xpt_proto_find(path->device->protocol);
1075 proto->ops->announce(path->device);
1077 printf("%s%d: Unknown protocol device %d\n",
1078 periph->periph_name, periph->unit_number,
1079 path->device->protocol);
1080 if (path->device->serial_num_len > 0) {
1081 /* Don't wrap the screen - print only the first 60 chars */
1082 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1083 periph->unit_number, path->device->serial_num);
1085 /* Announce transport details. */
1086 path->bus->xport->ops->announce(periph);
1087 /* Announce command queueing. */
1088 if (path->device->inq_flags & SID_CmdQue
1089 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1090 printf("%s%d: Command Queueing enabled\n",
1091 periph->periph_name, periph->unit_number);
1093 /* Announce caller's details if they've passed in. */
1094 if (announce_string != NULL)
1095 printf("%s%d: %s\n", periph->periph_name,
1096 periph->unit_number, announce_string);
1100 xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
1101 char *announce_string)
1103 struct cam_path *path = periph->path;
1104 struct xpt_proto *proto;
1106 cam_periph_assert(periph, MA_OWNED);
1107 periph->flags |= CAM_PERIPH_ANNOUNCED;
1109 /* Fall back to the non-sbuf method if necessary */
1110 if (xsoftc.announce_nosbuf != 0) {
1111 xpt_announce_periph(periph, announce_string);
1114 proto = xpt_proto_find(path->device->protocol);
1115 if (((proto != NULL) && (proto->ops->announce_sbuf == NULL)) ||
1116 (path->bus->xport->ops->announce_sbuf == NULL)) {
1117 xpt_announce_periph(periph, announce_string);
1121 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1122 periph->periph_name, periph->unit_number,
1123 path->bus->sim->sim_name,
1124 path->bus->sim->unit_number,
1125 path->bus->sim->bus_id,
1127 path->target->target_id,
1128 (uintmax_t)path->device->lun_id);
1129 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1132 proto->ops->announce_sbuf(path->device, sb);
1134 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1135 periph->periph_name, periph->unit_number,
1136 path->device->protocol);
1137 if (path->device->serial_num_len > 0) {
1138 /* Don't wrap the screen - print only the first 60 chars */
1139 sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
1140 periph->periph_name, periph->unit_number,
1141 path->device->serial_num);
1143 /* Announce transport details. */
1144 path->bus->xport->ops->announce_sbuf(periph, sb);
1145 /* Announce command queueing. */
1146 if (path->device->inq_flags & SID_CmdQue
1147 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1148 sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
1149 periph->periph_name, periph->unit_number);
1151 /* Announce caller's details if they've passed in. */
1152 if (announce_string != NULL)
1153 sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
1154 periph->unit_number, announce_string);
1158 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1161 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1162 periph->unit_number, quirks, bit_string);
1167 xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
1168 int quirks, char *bit_string)
1170 if (xsoftc.announce_nosbuf != 0) {
1171 xpt_announce_quirks(periph, quirks, bit_string);
1176 sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
1177 periph->unit_number, quirks, bit_string);
1182 xpt_denounce_periph(struct cam_periph *periph)
1184 struct cam_path *path = periph->path;
1185 struct xpt_proto *proto;
1187 cam_periph_assert(periph, MA_OWNED);
1188 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1189 periph->periph_name, periph->unit_number,
1190 path->bus->sim->sim_name,
1191 path->bus->sim->unit_number,
1192 path->bus->sim->bus_id,
1194 path->target->target_id,
1195 (uintmax_t)path->device->lun_id);
1196 printf("%s%d: ", periph->periph_name, periph->unit_number);
1197 proto = xpt_proto_find(path->device->protocol);
1199 proto->ops->denounce(path->device);
1201 printf("%s%d: Unknown protocol device %d\n",
1202 periph->periph_name, periph->unit_number,
1203 path->device->protocol);
1204 if (path->device->serial_num_len > 0)
1205 printf(" s/n %.60s", path->device->serial_num);
1206 printf(" detached\n");
1210 xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
1212 struct cam_path *path = periph->path;
1213 struct xpt_proto *proto;
1215 cam_periph_assert(periph, MA_OWNED);
1217 /* Fall back to the non-sbuf method if necessary */
1218 if (xsoftc.announce_nosbuf != 0) {
1219 xpt_denounce_periph(periph);
1222 proto = xpt_proto_find(path->device->protocol);
1223 if ((proto != NULL) && (proto->ops->denounce_sbuf == NULL)) {
1224 xpt_denounce_periph(periph);
1228 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1229 periph->periph_name, periph->unit_number,
1230 path->bus->sim->sim_name,
1231 path->bus->sim->unit_number,
1232 path->bus->sim->bus_id,
1234 path->target->target_id,
1235 (uintmax_t)path->device->lun_id);
1236 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1239 proto->ops->denounce_sbuf(path->device, sb);
1241 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1242 periph->periph_name, periph->unit_number,
1243 path->device->protocol);
1244 if (path->device->serial_num_len > 0)
1245 sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1246 sbuf_printf(sb, " detached\n");
1250 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1253 struct ccb_dev_advinfo cdai;
1254 struct scsi_vpd_id_descriptor *idd;
1256 xpt_path_assert(path, MA_OWNED);
1258 memset(&cdai, 0, sizeof(cdai));
1259 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1260 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1261 cdai.flags = CDAI_FLAG_NONE;
1264 if (!strcmp(attr, "GEOM::ident"))
1265 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1266 else if (!strcmp(attr, "GEOM::physpath"))
1267 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1268 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1269 strcmp(attr, "GEOM::lunname") == 0) {
1270 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1271 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1275 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1276 if (cdai.buf == NULL) {
1280 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1281 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1282 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1283 if (cdai.provsiz == 0)
1285 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1286 if (strcmp(attr, "GEOM::lunid") == 0) {
1287 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1288 cdai.provsiz, scsi_devid_is_lun_naa);
1290 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1291 cdai.provsiz, scsi_devid_is_lun_eui64);
1293 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1294 cdai.provsiz, scsi_devid_is_lun_uuid);
1296 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1297 cdai.provsiz, scsi_devid_is_lun_md5);
1301 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1302 cdai.provsiz, scsi_devid_is_lun_t10);
1304 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1305 cdai.provsiz, scsi_devid_is_lun_name);
1309 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1310 if (idd->length < len) {
1311 for (l = 0; l < idd->length; l++)
1312 buf[l] = idd->identifier[l] ?
1313 idd->identifier[l] : ' ';
1317 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1318 l = strnlen(idd->identifier, idd->length);
1320 bcopy(idd->identifier, buf, l);
1324 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1325 && idd->identifier[0] == 0x10) {
1326 if ((idd->length - 2) * 2 + 4 < len) {
1327 for (l = 2, o = 0; l < idd->length; l++) {
1328 if (l == 6 || l == 8 || l == 10 || l == 12)
1329 o += sprintf(buf + o, "-");
1330 o += sprintf(buf + o, "%02x",
1331 idd->identifier[l]);
1336 if (idd->length * 2 < len) {
1337 for (l = 0; l < idd->length; l++)
1338 sprintf(buf + l * 2, "%02x",
1339 idd->identifier[l]);
1345 if (strlcpy(buf, cdai.buf, len) >= len)
1350 if (cdai.buf != NULL)
1351 free(cdai.buf, M_CAMXPT);
1355 static dev_match_ret
1356 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1359 dev_match_ret retval;
1362 retval = DM_RET_NONE;
1365 * If we aren't given something to match against, that's an error.
1368 return(DM_RET_ERROR);
1371 * If there are no match entries, then this bus matches no
1374 if ((patterns == NULL) || (num_patterns == 0))
1375 return(DM_RET_DESCEND | DM_RET_COPY);
1377 for (i = 0; i < num_patterns; i++) {
1378 struct bus_match_pattern *cur_pattern;
1381 * If the pattern in question isn't for a bus node, we
1382 * aren't interested. However, we do indicate to the
1383 * calling routine that we should continue descending the
1384 * tree, since the user wants to match against lower-level
1387 if (patterns[i].type != DEV_MATCH_BUS) {
1388 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1389 retval |= DM_RET_DESCEND;
1393 cur_pattern = &patterns[i].pattern.bus_pattern;
1396 * If they want to match any bus node, we give them any
1399 if (cur_pattern->flags == BUS_MATCH_ANY) {
1400 /* set the copy flag */
1401 retval |= DM_RET_COPY;
1404 * If we've already decided on an action, go ahead
1407 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1412 * Not sure why someone would do this...
1414 if (cur_pattern->flags == BUS_MATCH_NONE)
1417 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1418 && (cur_pattern->path_id != bus->path_id))
1421 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1422 && (cur_pattern->bus_id != bus->sim->bus_id))
1425 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1426 && (cur_pattern->unit_number != bus->sim->unit_number))
1429 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1430 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1435 * If we get to this point, the user definitely wants
1436 * information on this bus. So tell the caller to copy the
1439 retval |= DM_RET_COPY;
1442 * If the return action has been set to descend, then we
1443 * know that we've already seen a non-bus matching
1444 * expression, therefore we need to further descend the tree.
1445 * This won't change by continuing around the loop, so we
1446 * go ahead and return. If we haven't seen a non-bus
1447 * matching expression, we keep going around the loop until
1448 * we exhaust the matching expressions. We'll set the stop
1449 * flag once we fall out of the loop.
1451 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1456 * If the return action hasn't been set to descend yet, that means
1457 * we haven't seen anything other than bus matching patterns. So
1458 * tell the caller to stop descending the tree -- the user doesn't
1459 * want to match against lower level tree elements.
1461 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1462 retval |= DM_RET_STOP;
1467 static dev_match_ret
1468 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1469 struct cam_ed *device)
1471 dev_match_ret retval;
1474 retval = DM_RET_NONE;
1477 * If we aren't given something to match against, that's an error.
1480 return(DM_RET_ERROR);
1483 * If there are no match entries, then this device matches no
1486 if ((patterns == NULL) || (num_patterns == 0))
1487 return(DM_RET_DESCEND | DM_RET_COPY);
1489 for (i = 0; i < num_patterns; i++) {
1490 struct device_match_pattern *cur_pattern;
1491 struct scsi_vpd_device_id *device_id_page;
1494 * If the pattern in question isn't for a device node, we
1495 * aren't interested.
1497 if (patterns[i].type != DEV_MATCH_DEVICE) {
1498 if ((patterns[i].type == DEV_MATCH_PERIPH)
1499 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1500 retval |= DM_RET_DESCEND;
1504 cur_pattern = &patterns[i].pattern.device_pattern;
1506 /* Error out if mutually exclusive options are specified. */
1507 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1508 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1509 return(DM_RET_ERROR);
1512 * If they want to match any device node, we give them any
1515 if (cur_pattern->flags == DEV_MATCH_ANY)
1519 * Not sure why someone would do this...
1521 if (cur_pattern->flags == DEV_MATCH_NONE)
1524 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1525 && (cur_pattern->path_id != device->target->bus->path_id))
1528 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1529 && (cur_pattern->target_id != device->target->target_id))
1532 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1533 && (cur_pattern->target_lun != device->lun_id))
1536 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1537 && (cam_quirkmatch((caddr_t)&device->inq_data,
1538 (caddr_t)&cur_pattern->data.inq_pat,
1539 1, sizeof(cur_pattern->data.inq_pat),
1540 scsi_static_inquiry_match) == NULL))
1543 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1544 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1545 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1546 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1547 device->device_id_len
1548 - SVPD_DEVICE_ID_HDR_LEN,
1549 cur_pattern->data.devid_pat.id,
1550 cur_pattern->data.devid_pat.id_len) != 0))
1555 * If we get to this point, the user definitely wants
1556 * information on this device. So tell the caller to copy
1559 retval |= DM_RET_COPY;
1562 * If the return action has been set to descend, then we
1563 * know that we've already seen a peripheral matching
1564 * expression, therefore we need to further descend the tree.
1565 * This won't change by continuing around the loop, so we
1566 * go ahead and return. If we haven't seen a peripheral
1567 * matching expression, we keep going around the loop until
1568 * we exhaust the matching expressions. We'll set the stop
1569 * flag once we fall out of the loop.
1571 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1576 * If the return action hasn't been set to descend yet, that means
1577 * we haven't seen any peripheral matching patterns. So tell the
1578 * caller to stop descending the tree -- the user doesn't want to
1579 * match against lower level tree elements.
1581 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1582 retval |= DM_RET_STOP;
1588 * Match a single peripheral against any number of match patterns.
1590 static dev_match_ret
1591 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1592 struct cam_periph *periph)
1594 dev_match_ret retval;
1598 * If we aren't given something to match against, that's an error.
1601 return(DM_RET_ERROR);
1604 * If there are no match entries, then this peripheral matches no
1607 if ((patterns == NULL) || (num_patterns == 0))
1608 return(DM_RET_STOP | DM_RET_COPY);
1611 * There aren't any nodes below a peripheral node, so there's no
1612 * reason to descend the tree any further.
1614 retval = DM_RET_STOP;
1616 for (i = 0; i < num_patterns; i++) {
1617 struct periph_match_pattern *cur_pattern;
1620 * If the pattern in question isn't for a peripheral, we
1621 * aren't interested.
1623 if (patterns[i].type != DEV_MATCH_PERIPH)
1626 cur_pattern = &patterns[i].pattern.periph_pattern;
1629 * If they want to match on anything, then we will do so.
1631 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1632 /* set the copy flag */
1633 retval |= DM_RET_COPY;
1636 * We've already set the return action to stop,
1637 * since there are no nodes below peripherals in
1644 * Not sure why someone would do this...
1646 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1649 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1650 && (cur_pattern->path_id != periph->path->bus->path_id))
1654 * For the target and lun id's, we have to make sure the
1655 * target and lun pointers aren't NULL. The xpt peripheral
1656 * has a wildcard target and device.
1658 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1659 && ((periph->path->target == NULL)
1660 ||(cur_pattern->target_id != periph->path->target->target_id)))
1663 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1664 && ((periph->path->device == NULL)
1665 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1668 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1669 && (cur_pattern->unit_number != periph->unit_number))
1672 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1673 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1678 * If we get to this point, the user definitely wants
1679 * information on this peripheral. So tell the caller to
1680 * copy the data out.
1682 retval |= DM_RET_COPY;
1685 * The return action has already been set to stop, since
1686 * peripherals don't have any nodes below them in the EDT.
1692 * If we get to this point, the peripheral that was passed in
1693 * doesn't match any of the patterns.
1699 xptedtbusfunc(struct cam_eb *bus, void *arg)
1701 struct ccb_dev_match *cdm;
1702 struct cam_et *target;
1703 dev_match_ret retval;
1705 cdm = (struct ccb_dev_match *)arg;
1708 * If our position is for something deeper in the tree, that means
1709 * that we've already seen this node. So, we keep going down.
1711 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1712 && (cdm->pos.cookie.bus == bus)
1713 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1714 && (cdm->pos.cookie.target != NULL))
1715 retval = DM_RET_DESCEND;
1717 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1720 * If we got an error, bail out of the search.
1722 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1723 cdm->status = CAM_DEV_MATCH_ERROR;
1728 * If the copy flag is set, copy this bus out.
1730 if (retval & DM_RET_COPY) {
1733 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1734 sizeof(struct dev_match_result));
1737 * If we don't have enough space to put in another
1738 * match result, save our position and tell the
1739 * user there are more devices to check.
1741 if (spaceleft < sizeof(struct dev_match_result)) {
1742 bzero(&cdm->pos, sizeof(cdm->pos));
1743 cdm->pos.position_type =
1744 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1746 cdm->pos.cookie.bus = bus;
1747 cdm->pos.generations[CAM_BUS_GENERATION]=
1748 xsoftc.bus_generation;
1749 cdm->status = CAM_DEV_MATCH_MORE;
1752 j = cdm->num_matches;
1754 cdm->matches[j].type = DEV_MATCH_BUS;
1755 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1756 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1757 cdm->matches[j].result.bus_result.unit_number =
1758 bus->sim->unit_number;
1759 strncpy(cdm->matches[j].result.bus_result.dev_name,
1760 bus->sim->sim_name, DEV_IDLEN);
1764 * If the user is only interested in buses, there's no
1765 * reason to descend to the next level in the tree.
1767 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1771 * If there is a target generation recorded, check it to
1772 * make sure the target list hasn't changed.
1774 mtx_lock(&bus->eb_mtx);
1775 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1776 && (cdm->pos.cookie.bus == bus)
1777 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1778 && (cdm->pos.cookie.target != NULL)) {
1779 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1781 mtx_unlock(&bus->eb_mtx);
1782 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1785 target = (struct cam_et *)cdm->pos.cookie.target;
1789 mtx_unlock(&bus->eb_mtx);
1791 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1795 xptedttargetfunc(struct cam_et *target, void *arg)
1797 struct ccb_dev_match *cdm;
1799 struct cam_ed *device;
1801 cdm = (struct ccb_dev_match *)arg;
1805 * If there is a device list generation recorded, check it to
1806 * make sure the device list hasn't changed.
1808 mtx_lock(&bus->eb_mtx);
1809 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1810 && (cdm->pos.cookie.bus == bus)
1811 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1812 && (cdm->pos.cookie.target == target)
1813 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1814 && (cdm->pos.cookie.device != NULL)) {
1815 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1816 target->generation) {
1817 mtx_unlock(&bus->eb_mtx);
1818 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1821 device = (struct cam_ed *)cdm->pos.cookie.device;
1825 mtx_unlock(&bus->eb_mtx);
1827 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1831 xptedtdevicefunc(struct cam_ed *device, void *arg)
1834 struct cam_periph *periph;
1835 struct ccb_dev_match *cdm;
1836 dev_match_ret retval;
1838 cdm = (struct ccb_dev_match *)arg;
1839 bus = device->target->bus;
1842 * If our position is for something deeper in the tree, that means
1843 * that we've already seen this node. So, we keep going down.
1845 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1846 && (cdm->pos.cookie.device == device)
1847 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1848 && (cdm->pos.cookie.periph != NULL))
1849 retval = DM_RET_DESCEND;
1851 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1854 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1855 cdm->status = CAM_DEV_MATCH_ERROR;
1860 * If the copy flag is set, copy this device out.
1862 if (retval & DM_RET_COPY) {
1865 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1866 sizeof(struct dev_match_result));
1869 * If we don't have enough space to put in another
1870 * match result, save our position and tell the
1871 * user there are more devices to check.
1873 if (spaceleft < sizeof(struct dev_match_result)) {
1874 bzero(&cdm->pos, sizeof(cdm->pos));
1875 cdm->pos.position_type =
1876 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1877 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1879 cdm->pos.cookie.bus = device->target->bus;
1880 cdm->pos.generations[CAM_BUS_GENERATION]=
1881 xsoftc.bus_generation;
1882 cdm->pos.cookie.target = device->target;
1883 cdm->pos.generations[CAM_TARGET_GENERATION] =
1884 device->target->bus->generation;
1885 cdm->pos.cookie.device = device;
1886 cdm->pos.generations[CAM_DEV_GENERATION] =
1887 device->target->generation;
1888 cdm->status = CAM_DEV_MATCH_MORE;
1891 j = cdm->num_matches;
1893 cdm->matches[j].type = DEV_MATCH_DEVICE;
1894 cdm->matches[j].result.device_result.path_id =
1895 device->target->bus->path_id;
1896 cdm->matches[j].result.device_result.target_id =
1897 device->target->target_id;
1898 cdm->matches[j].result.device_result.target_lun =
1900 cdm->matches[j].result.device_result.protocol =
1902 bcopy(&device->inq_data,
1903 &cdm->matches[j].result.device_result.inq_data,
1904 sizeof(struct scsi_inquiry_data));
1905 bcopy(&device->ident_data,
1906 &cdm->matches[j].result.device_result.ident_data,
1907 sizeof(struct ata_params));
1908 bcopy(&device->mmc_ident_data,
1909 &cdm->matches[j].result.device_result.mmc_ident_data,
1910 sizeof(struct mmc_params));
1912 /* Let the user know whether this device is unconfigured */
1913 if (device->flags & CAM_DEV_UNCONFIGURED)
1914 cdm->matches[j].result.device_result.flags =
1915 DEV_RESULT_UNCONFIGURED;
1917 cdm->matches[j].result.device_result.flags =
1922 * If the user isn't interested in peripherals, don't descend
1923 * the tree any further.
1925 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1929 * If there is a peripheral list generation recorded, make sure
1930 * it hasn't changed.
1933 mtx_lock(&bus->eb_mtx);
1934 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1935 && (cdm->pos.cookie.bus == bus)
1936 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1937 && (cdm->pos.cookie.target == device->target)
1938 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1939 && (cdm->pos.cookie.device == device)
1940 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1941 && (cdm->pos.cookie.periph != NULL)) {
1942 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1943 device->generation) {
1944 mtx_unlock(&bus->eb_mtx);
1946 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1949 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1953 mtx_unlock(&bus->eb_mtx);
1956 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1960 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1962 struct ccb_dev_match *cdm;
1963 dev_match_ret retval;
1965 cdm = (struct ccb_dev_match *)arg;
1967 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1969 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1970 cdm->status = CAM_DEV_MATCH_ERROR;
1975 * If the copy flag is set, copy this peripheral out.
1977 if (retval & DM_RET_COPY) {
1980 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1981 sizeof(struct dev_match_result));
1984 * If we don't have enough space to put in another
1985 * match result, save our position and tell the
1986 * user there are more devices to check.
1988 if (spaceleft < sizeof(struct dev_match_result)) {
1989 bzero(&cdm->pos, sizeof(cdm->pos));
1990 cdm->pos.position_type =
1991 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1992 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1995 cdm->pos.cookie.bus = periph->path->bus;
1996 cdm->pos.generations[CAM_BUS_GENERATION]=
1997 xsoftc.bus_generation;
1998 cdm->pos.cookie.target = periph->path->target;
1999 cdm->pos.generations[CAM_TARGET_GENERATION] =
2000 periph->path->bus->generation;
2001 cdm->pos.cookie.device = periph->path->device;
2002 cdm->pos.generations[CAM_DEV_GENERATION] =
2003 periph->path->target->generation;
2004 cdm->pos.cookie.periph = periph;
2005 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2006 periph->path->device->generation;
2007 cdm->status = CAM_DEV_MATCH_MORE;
2011 j = cdm->num_matches;
2013 cdm->matches[j].type = DEV_MATCH_PERIPH;
2014 cdm->matches[j].result.periph_result.path_id =
2015 periph->path->bus->path_id;
2016 cdm->matches[j].result.periph_result.target_id =
2017 periph->path->target->target_id;
2018 cdm->matches[j].result.periph_result.target_lun =
2019 periph->path->device->lun_id;
2020 cdm->matches[j].result.periph_result.unit_number =
2021 periph->unit_number;
2022 strncpy(cdm->matches[j].result.periph_result.periph_name,
2023 periph->periph_name, DEV_IDLEN);
2030 xptedtmatch(struct ccb_dev_match *cdm)
2035 cdm->num_matches = 0;
2038 * Check the bus list generation. If it has changed, the user
2039 * needs to reset everything and start over.
2042 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2043 && (cdm->pos.cookie.bus != NULL)) {
2044 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
2045 xsoftc.bus_generation) {
2047 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2050 bus = (struct cam_eb *)cdm->pos.cookie.bus;
2056 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
2059 * If we get back 0, that means that we had to stop before fully
2060 * traversing the EDT. It also means that one of the subroutines
2061 * has set the status field to the proper value. If we get back 1,
2062 * we've fully traversed the EDT and copied out any matching entries.
2065 cdm->status = CAM_DEV_MATCH_LAST;
2071 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2073 struct cam_periph *periph;
2074 struct ccb_dev_match *cdm;
2076 cdm = (struct ccb_dev_match *)arg;
2079 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2080 && (cdm->pos.cookie.pdrv == pdrv)
2081 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2082 && (cdm->pos.cookie.periph != NULL)) {
2083 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2084 (*pdrv)->generation) {
2086 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2089 periph = (struct cam_periph *)cdm->pos.cookie.periph;
2095 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
2099 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2101 struct ccb_dev_match *cdm;
2102 dev_match_ret retval;
2104 cdm = (struct ccb_dev_match *)arg;
2106 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2108 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2109 cdm->status = CAM_DEV_MATCH_ERROR;
2114 * If the copy flag is set, copy this peripheral out.
2116 if (retval & DM_RET_COPY) {
2119 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2120 sizeof(struct dev_match_result));
2123 * If we don't have enough space to put in another
2124 * match result, save our position and tell the
2125 * user there are more devices to check.
2127 if (spaceleft < sizeof(struct dev_match_result)) {
2128 struct periph_driver **pdrv;
2131 bzero(&cdm->pos, sizeof(cdm->pos));
2132 cdm->pos.position_type =
2133 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2137 * This may look a bit non-sensical, but it is
2138 * actually quite logical. There are very few
2139 * peripheral drivers, and bloating every peripheral
2140 * structure with a pointer back to its parent
2141 * peripheral driver linker set entry would cost
2142 * more in the long run than doing this quick lookup.
2144 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2145 if (strcmp((*pdrv)->driver_name,
2146 periph->periph_name) == 0)
2150 if (*pdrv == NULL) {
2151 cdm->status = CAM_DEV_MATCH_ERROR;
2155 cdm->pos.cookie.pdrv = pdrv;
2157 * The periph generation slot does double duty, as
2158 * does the periph pointer slot. They are used for
2159 * both edt and pdrv lookups and positioning.
2161 cdm->pos.cookie.periph = periph;
2162 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2163 (*pdrv)->generation;
2164 cdm->status = CAM_DEV_MATCH_MORE;
2168 j = cdm->num_matches;
2170 cdm->matches[j].type = DEV_MATCH_PERIPH;
2171 cdm->matches[j].result.periph_result.path_id =
2172 periph->path->bus->path_id;
2175 * The transport layer peripheral doesn't have a target or
2178 if (periph->path->target)
2179 cdm->matches[j].result.periph_result.target_id =
2180 periph->path->target->target_id;
2182 cdm->matches[j].result.periph_result.target_id =
2183 CAM_TARGET_WILDCARD;
2185 if (periph->path->device)
2186 cdm->matches[j].result.periph_result.target_lun =
2187 periph->path->device->lun_id;
2189 cdm->matches[j].result.periph_result.target_lun =
2192 cdm->matches[j].result.periph_result.unit_number =
2193 periph->unit_number;
2194 strncpy(cdm->matches[j].result.periph_result.periph_name,
2195 periph->periph_name, DEV_IDLEN);
2202 xptperiphlistmatch(struct ccb_dev_match *cdm)
2206 cdm->num_matches = 0;
2209 * At this point in the edt traversal function, we check the bus
2210 * list generation to make sure that no buses have been added or
2211 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2212 * For the peripheral driver list traversal function, however, we
2213 * don't have to worry about new peripheral driver types coming or
2214 * going; they're in a linker set, and therefore can't change
2215 * without a recompile.
2218 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2219 && (cdm->pos.cookie.pdrv != NULL))
2220 ret = xptpdrvtraverse(
2221 (struct periph_driver **)cdm->pos.cookie.pdrv,
2222 xptplistpdrvfunc, cdm);
2224 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2227 * If we get back 0, that means that we had to stop before fully
2228 * traversing the peripheral driver tree. It also means that one of
2229 * the subroutines has set the status field to the proper value. If
2230 * we get back 1, we've fully traversed the EDT and copied out any
2234 cdm->status = CAM_DEV_MATCH_LAST;
2240 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2242 struct cam_eb *bus, *next_bus;
2250 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2258 for (; bus != NULL; bus = next_bus) {
2259 retval = tr_func(bus, arg);
2261 xpt_release_bus(bus);
2265 next_bus = TAILQ_NEXT(bus, links);
2267 next_bus->refcount++;
2269 xpt_release_bus(bus);
2275 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2276 xpt_targetfunc_t *tr_func, void *arg)
2278 struct cam_et *target, *next_target;
2283 target = start_target;
2285 mtx_lock(&bus->eb_mtx);
2286 target = TAILQ_FIRST(&bus->et_entries);
2287 if (target == NULL) {
2288 mtx_unlock(&bus->eb_mtx);
2292 mtx_unlock(&bus->eb_mtx);
2294 for (; target != NULL; target = next_target) {
2295 retval = tr_func(target, arg);
2297 xpt_release_target(target);
2300 mtx_lock(&bus->eb_mtx);
2301 next_target = TAILQ_NEXT(target, links);
2303 next_target->refcount++;
2304 mtx_unlock(&bus->eb_mtx);
2305 xpt_release_target(target);
2311 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2312 xpt_devicefunc_t *tr_func, void *arg)
2315 struct cam_ed *device, *next_device;
2321 device = start_device;
2323 mtx_lock(&bus->eb_mtx);
2324 device = TAILQ_FIRST(&target->ed_entries);
2325 if (device == NULL) {
2326 mtx_unlock(&bus->eb_mtx);
2330 mtx_unlock(&bus->eb_mtx);
2332 for (; device != NULL; device = next_device) {
2333 mtx_lock(&device->device_mtx);
2334 retval = tr_func(device, arg);
2335 mtx_unlock(&device->device_mtx);
2337 xpt_release_device(device);
2340 mtx_lock(&bus->eb_mtx);
2341 next_device = TAILQ_NEXT(device, links);
2343 next_device->refcount++;
2344 mtx_unlock(&bus->eb_mtx);
2345 xpt_release_device(device);
2351 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2352 xpt_periphfunc_t *tr_func, void *arg)
2355 struct cam_periph *periph, *next_periph;
2360 bus = device->target->bus;
2362 periph = start_periph;
2365 mtx_lock(&bus->eb_mtx);
2366 periph = SLIST_FIRST(&device->periphs);
2367 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2368 periph = SLIST_NEXT(periph, periph_links);
2369 if (periph == NULL) {
2370 mtx_unlock(&bus->eb_mtx);
2375 mtx_unlock(&bus->eb_mtx);
2378 for (; periph != NULL; periph = next_periph) {
2379 retval = tr_func(periph, arg);
2381 cam_periph_release_locked(periph);
2385 mtx_lock(&bus->eb_mtx);
2386 next_periph = SLIST_NEXT(periph, periph_links);
2387 while (next_periph != NULL &&
2388 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2389 next_periph = SLIST_NEXT(next_periph, periph_links);
2391 next_periph->refcount++;
2392 mtx_unlock(&bus->eb_mtx);
2394 cam_periph_release_locked(periph);
2400 xptpdrvtraverse(struct periph_driver **start_pdrv,
2401 xpt_pdrvfunc_t *tr_func, void *arg)
2403 struct periph_driver **pdrv;
2409 * We don't traverse the peripheral driver list like we do the
2410 * other lists, because it is a linker set, and therefore cannot be
2411 * changed during runtime. If the peripheral driver list is ever
2412 * re-done to be something other than a linker set (i.e. it can
2413 * change while the system is running), the list traversal should
2414 * be modified to work like the other traversal functions.
2416 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2417 *pdrv != NULL; pdrv++) {
2418 retval = tr_func(pdrv, arg);
2428 xptpdperiphtraverse(struct periph_driver **pdrv,
2429 struct cam_periph *start_periph,
2430 xpt_periphfunc_t *tr_func, void *arg)
2432 struct cam_periph *periph, *next_periph;
2438 periph = start_periph;
2441 periph = TAILQ_FIRST(&(*pdrv)->units);
2442 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2443 periph = TAILQ_NEXT(periph, unit_links);
2444 if (periph == NULL) {
2451 for (; periph != NULL; periph = next_periph) {
2452 cam_periph_lock(periph);
2453 retval = tr_func(periph, arg);
2454 cam_periph_unlock(periph);
2456 cam_periph_release(periph);
2460 next_periph = TAILQ_NEXT(periph, unit_links);
2461 while (next_periph != NULL &&
2462 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2463 next_periph = TAILQ_NEXT(next_periph, unit_links);
2465 next_periph->refcount++;
2467 cam_periph_release(periph);
2473 xptdefbusfunc(struct cam_eb *bus, void *arg)
2475 struct xpt_traverse_config *tr_config;
2477 tr_config = (struct xpt_traverse_config *)arg;
2479 if (tr_config->depth == XPT_DEPTH_BUS) {
2480 xpt_busfunc_t *tr_func;
2482 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2484 return(tr_func(bus, tr_config->tr_arg));
2486 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2490 xptdeftargetfunc(struct cam_et *target, void *arg)
2492 struct xpt_traverse_config *tr_config;
2494 tr_config = (struct xpt_traverse_config *)arg;
2496 if (tr_config->depth == XPT_DEPTH_TARGET) {
2497 xpt_targetfunc_t *tr_func;
2499 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2501 return(tr_func(target, tr_config->tr_arg));
2503 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2507 xptdefdevicefunc(struct cam_ed *device, void *arg)
2509 struct xpt_traverse_config *tr_config;
2511 tr_config = (struct xpt_traverse_config *)arg;
2513 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2514 xpt_devicefunc_t *tr_func;
2516 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2518 return(tr_func(device, tr_config->tr_arg));
2520 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2524 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2526 struct xpt_traverse_config *tr_config;
2527 xpt_periphfunc_t *tr_func;
2529 tr_config = (struct xpt_traverse_config *)arg;
2531 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2534 * Unlike the other default functions, we don't check for depth
2535 * here. The peripheral driver level is the last level in the EDT,
2536 * so if we're here, we should execute the function in question.
2538 return(tr_func(periph, tr_config->tr_arg));
2542 * Execute the given function for every bus in the EDT.
2545 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2547 struct xpt_traverse_config tr_config;
2549 tr_config.depth = XPT_DEPTH_BUS;
2550 tr_config.tr_func = tr_func;
2551 tr_config.tr_arg = arg;
2553 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2557 * Execute the given function for every device in the EDT.
2560 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2562 struct xpt_traverse_config tr_config;
2564 tr_config.depth = XPT_DEPTH_DEVICE;
2565 tr_config.tr_func = tr_func;
2566 tr_config.tr_arg = arg;
2568 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2572 xptsetasyncfunc(struct cam_ed *device, void *arg)
2574 struct cam_path path;
2575 struct ccb_getdev cgd;
2576 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2579 * Don't report unconfigured devices (Wildcard devs,
2580 * devices only for target mode, device instances
2581 * that have been invalidated but are waiting for
2582 * their last reference count to be released).
2584 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2587 xpt_compile_path(&path,
2589 device->target->bus->path_id,
2590 device->target->target_id,
2592 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2593 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2594 xpt_action((union ccb *)&cgd);
2595 csa->callback(csa->callback_arg,
2598 xpt_release_path(&path);
2604 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2606 struct cam_path path;
2607 struct ccb_pathinq cpi;
2608 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2610 xpt_compile_path(&path, /*periph*/NULL,
2612 CAM_TARGET_WILDCARD,
2614 xpt_path_lock(&path);
2615 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2616 cpi.ccb_h.func_code = XPT_PATH_INQ;
2617 xpt_action((union ccb *)&cpi);
2618 csa->callback(csa->callback_arg,
2621 xpt_path_unlock(&path);
2622 xpt_release_path(&path);
2628 xpt_action(union ccb *start_ccb)
2631 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2632 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2633 xpt_action_name(start_ccb->ccb_h.func_code)));
2635 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2636 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2640 xpt_action_default(union ccb *start_ccb)
2642 struct cam_path *path;
2643 struct cam_sim *sim;
2646 path = start_ccb->ccb_h.path;
2647 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2648 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2649 xpt_action_name(start_ccb->ccb_h.func_code)));
2651 switch (start_ccb->ccb_h.func_code) {
2654 struct cam_ed *device;
2657 * For the sake of compatibility with SCSI-1
2658 * devices that may not understand the identify
2659 * message, we include lun information in the
2660 * second byte of all commands. SCSI-1 specifies
2661 * that luns are a 3 bit value and reserves only 3
2662 * bits for lun information in the CDB. Later
2663 * revisions of the SCSI spec allow for more than 8
2664 * luns, but have deprecated lun information in the
2665 * CDB. So, if the lun won't fit, we must omit.
2667 * Also be aware that during initial probing for devices,
2668 * the inquiry information is unknown but initialized to 0.
2669 * This means that this code will be exercised while probing
2670 * devices with an ANSI revision greater than 2.
2672 device = path->device;
2673 if (device->protocol_version <= SCSI_REV_2
2674 && start_ccb->ccb_h.target_lun < 8
2675 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2677 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2678 start_ccb->ccb_h.target_lun << 5;
2680 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2684 case XPT_CONT_TARGET_IO:
2685 start_ccb->csio.sense_resid = 0;
2686 start_ccb->csio.resid = 0;
2689 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2690 start_ccb->ataio.resid = 0;
2694 case XPT_NVME_ADMIN:
2697 /* XXX just like nmve_io? */
2702 struct cam_devq *devq;
2704 devq = path->bus->sim->devq;
2705 mtx_lock(&devq->send_mtx);
2706 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2707 if (xpt_schedule_devq(devq, path->device) != 0)
2709 mtx_unlock(&devq->send_mtx);
2712 case XPT_CALC_GEOMETRY:
2713 /* Filter out garbage */
2714 if (start_ccb->ccg.block_size == 0
2715 || start_ccb->ccg.volume_size == 0) {
2716 start_ccb->ccg.cylinders = 0;
2717 start_ccb->ccg.heads = 0;
2718 start_ccb->ccg.secs_per_track = 0;
2719 start_ccb->ccb_h.status = CAM_REQ_CMP;
2722 #if defined(__sparc64__)
2724 * For sparc64, we may need adjust the geometry of large
2725 * disks in order to fit the limitations of the 16-bit
2726 * fields of the VTOC8 disk label.
2728 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2729 start_ccb->ccb_h.status = CAM_REQ_CMP;
2736 union ccb* abort_ccb;
2738 abort_ccb = start_ccb->cab.abort_ccb;
2739 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2740 struct cam_ed *device;
2741 struct cam_devq *devq;
2743 device = abort_ccb->ccb_h.path->device;
2744 devq = device->sim->devq;
2746 mtx_lock(&devq->send_mtx);
2747 if (abort_ccb->ccb_h.pinfo.index > 0) {
2748 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2749 abort_ccb->ccb_h.status =
2750 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2751 xpt_freeze_devq_device(device, 1);
2752 mtx_unlock(&devq->send_mtx);
2753 xpt_done(abort_ccb);
2754 start_ccb->ccb_h.status = CAM_REQ_CMP;
2757 mtx_unlock(&devq->send_mtx);
2759 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2760 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2762 * We've caught this ccb en route to
2763 * the SIM. Flag it for abort and the
2764 * SIM will do so just before starting
2765 * real work on the CCB.
2767 abort_ccb->ccb_h.status =
2768 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2769 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2770 start_ccb->ccb_h.status = CAM_REQ_CMP;
2774 if (XPT_FC_IS_QUEUED(abort_ccb)
2775 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2777 * It's already completed but waiting
2778 * for our SWI to get to it.
2780 start_ccb->ccb_h.status = CAM_UA_ABORT;
2784 * If we weren't able to take care of the abort request
2785 * in the XPT, pass the request down to the SIM for processing.
2789 case XPT_ACCEPT_TARGET_IO:
2791 case XPT_IMMED_NOTIFY:
2792 case XPT_NOTIFY_ACK:
2794 case XPT_IMMEDIATE_NOTIFY:
2795 case XPT_NOTIFY_ACKNOWLEDGE:
2796 case XPT_GET_SIM_KNOB_OLD:
2797 case XPT_GET_SIM_KNOB:
2798 case XPT_SET_SIM_KNOB:
2799 case XPT_GET_TRAN_SETTINGS:
2800 case XPT_SET_TRAN_SETTINGS:
2803 sim = path->bus->sim;
2805 if (mtx && !mtx_owned(mtx))
2810 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2811 ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2812 (*(sim->sim_action))(sim, start_ccb);
2813 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2814 ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2818 case XPT_PATH_STATS:
2819 start_ccb->cpis.last_reset = path->bus->last_reset;
2820 start_ccb->ccb_h.status = CAM_REQ_CMP;
2827 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2828 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2830 struct ccb_getdev *cgd;
2832 cgd = &start_ccb->cgd;
2833 cgd->protocol = dev->protocol;
2834 cgd->inq_data = dev->inq_data;
2835 cgd->ident_data = dev->ident_data;
2836 cgd->inq_flags = dev->inq_flags;
2837 cgd->nvme_data = dev->nvme_data;
2838 cgd->nvme_cdata = dev->nvme_cdata;
2839 cgd->ccb_h.status = CAM_REQ_CMP;
2840 cgd->serial_num_len = dev->serial_num_len;
2841 if ((dev->serial_num_len > 0)
2842 && (dev->serial_num != NULL))
2843 bcopy(dev->serial_num, cgd->serial_num,
2844 dev->serial_num_len);
2848 case XPT_GDEV_STATS:
2850 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2851 struct cam_ed *dev = path->device;
2852 struct cam_eb *bus = path->bus;
2853 struct cam_et *tar = path->target;
2854 struct cam_devq *devq = bus->sim->devq;
2856 mtx_lock(&devq->send_mtx);
2857 cgds->dev_openings = dev->ccbq.dev_openings;
2858 cgds->dev_active = dev->ccbq.dev_active;
2859 cgds->allocated = dev->ccbq.allocated;
2860 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2861 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2862 cgds->last_reset = tar->last_reset;
2863 cgds->maxtags = dev->maxtags;
2864 cgds->mintags = dev->mintags;
2865 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2866 cgds->last_reset = bus->last_reset;
2867 mtx_unlock(&devq->send_mtx);
2868 cgds->ccb_h.status = CAM_REQ_CMP;
2873 struct cam_periph *nperiph;
2874 struct periph_list *periph_head;
2875 struct ccb_getdevlist *cgdl;
2877 struct cam_ed *device;
2884 * Don't want anyone mucking with our data.
2886 device = path->device;
2887 periph_head = &device->periphs;
2888 cgdl = &start_ccb->cgdl;
2891 * Check and see if the list has changed since the user
2892 * last requested a list member. If so, tell them that the
2893 * list has changed, and therefore they need to start over
2894 * from the beginning.
2896 if ((cgdl->index != 0) &&
2897 (cgdl->generation != device->generation)) {
2898 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2903 * Traverse the list of peripherals and attempt to find
2904 * the requested peripheral.
2906 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2907 (nperiph != NULL) && (i <= cgdl->index);
2908 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2909 if (i == cgdl->index) {
2910 strncpy(cgdl->periph_name,
2911 nperiph->periph_name,
2913 cgdl->unit_number = nperiph->unit_number;
2918 cgdl->status = CAM_GDEVLIST_ERROR;
2922 if (nperiph == NULL)
2923 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2925 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2928 cgdl->generation = device->generation;
2930 cgdl->ccb_h.status = CAM_REQ_CMP;
2935 dev_pos_type position_type;
2936 struct ccb_dev_match *cdm;
2938 cdm = &start_ccb->cdm;
2941 * There are two ways of getting at information in the EDT.
2942 * The first way is via the primary EDT tree. It starts
2943 * with a list of buses, then a list of targets on a bus,
2944 * then devices/luns on a target, and then peripherals on a
2945 * device/lun. The "other" way is by the peripheral driver
2946 * lists. The peripheral driver lists are organized by
2947 * peripheral driver. (obviously) So it makes sense to
2948 * use the peripheral driver list if the user is looking
2949 * for something like "da1", or all "da" devices. If the
2950 * user is looking for something on a particular bus/target
2951 * or lun, it's generally better to go through the EDT tree.
2954 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2955 position_type = cdm->pos.position_type;
2959 position_type = CAM_DEV_POS_NONE;
2961 for (i = 0; i < cdm->num_patterns; i++) {
2962 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2963 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2964 position_type = CAM_DEV_POS_EDT;
2969 if (cdm->num_patterns == 0)
2970 position_type = CAM_DEV_POS_EDT;
2971 else if (position_type == CAM_DEV_POS_NONE)
2972 position_type = CAM_DEV_POS_PDRV;
2975 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2976 case CAM_DEV_POS_EDT:
2979 case CAM_DEV_POS_PDRV:
2980 xptperiphlistmatch(cdm);
2983 cdm->status = CAM_DEV_MATCH_ERROR;
2987 if (cdm->status == CAM_DEV_MATCH_ERROR)
2988 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2990 start_ccb->ccb_h.status = CAM_REQ_CMP;
2996 struct ccb_setasync *csa;
2997 struct async_node *cur_entry;
2998 struct async_list *async_head;
3001 csa = &start_ccb->csa;
3002 added = csa->event_enable;
3003 async_head = &path->device->asyncs;
3006 * If there is already an entry for us, simply
3009 cur_entry = SLIST_FIRST(async_head);
3010 while (cur_entry != NULL) {
3011 if ((cur_entry->callback_arg == csa->callback_arg)
3012 && (cur_entry->callback == csa->callback))
3014 cur_entry = SLIST_NEXT(cur_entry, links);
3017 if (cur_entry != NULL) {
3019 * If the request has no flags set,
3022 added &= ~cur_entry->event_enable;
3023 if (csa->event_enable == 0) {
3024 SLIST_REMOVE(async_head, cur_entry,
3026 xpt_release_device(path->device);
3027 free(cur_entry, M_CAMXPT);
3029 cur_entry->event_enable = csa->event_enable;
3031 csa->event_enable = added;
3033 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3035 if (cur_entry == NULL) {
3036 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3039 cur_entry->event_enable = csa->event_enable;
3040 cur_entry->event_lock = (path->bus->sim->mtx &&
3041 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
3042 cur_entry->callback_arg = csa->callback_arg;
3043 cur_entry->callback = csa->callback;
3044 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3045 xpt_acquire_device(path->device);
3047 start_ccb->ccb_h.status = CAM_REQ_CMP;
3052 struct ccb_relsim *crs;
3055 crs = &start_ccb->crs;
3059 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3063 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3065 /* Don't ever go below one opening */
3066 if (crs->openings > 0) {
3067 xpt_dev_ccbq_resize(path, crs->openings);
3070 "number of openings is now %d\n",
3076 mtx_lock(&dev->sim->devq->send_mtx);
3077 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3079 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3082 * Just extend the old timeout and decrement
3083 * the freeze count so that a single timeout
3084 * is sufficient for releasing the queue.
3086 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3087 callout_stop(&dev->callout);
3090 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3093 callout_reset_sbt(&dev->callout,
3094 SBT_1MS * crs->release_timeout, 0,
3095 xpt_release_devq_timeout, dev, 0);
3097 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3101 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3103 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3105 * Decrement the freeze count so that a single
3106 * completion is still sufficient to unfreeze
3109 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3112 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3113 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3117 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3119 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3120 || (dev->ccbq.dev_active == 0)) {
3122 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3125 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3126 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3129 mtx_unlock(&dev->sim->devq->send_mtx);
3131 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3132 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3133 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3134 start_ccb->ccb_h.status = CAM_REQ_CMP;
3138 struct cam_path *oldpath;
3140 /* Check that all request bits are supported. */
3141 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3142 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3146 cam_dflags = CAM_DEBUG_NONE;
3147 if (cam_dpath != NULL) {
3148 oldpath = cam_dpath;
3150 xpt_free_path(oldpath);
3152 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3153 if (xpt_create_path(&cam_dpath, NULL,
3154 start_ccb->ccb_h.path_id,
3155 start_ccb->ccb_h.target_id,
3156 start_ccb->ccb_h.target_lun) !=
3158 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3160 cam_dflags = start_ccb->cdbg.flags;
3161 start_ccb->ccb_h.status = CAM_REQ_CMP;
3162 xpt_print(cam_dpath, "debugging flags now %x\n",
3166 start_ccb->ccb_h.status = CAM_REQ_CMP;
3170 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3171 xpt_freeze_devq(path, 1);
3172 start_ccb->ccb_h.status = CAM_REQ_CMP;
3174 case XPT_REPROBE_LUN:
3175 xpt_async(AC_INQ_CHANGED, path, NULL);
3176 start_ccb->ccb_h.status = CAM_REQ_CMP;
3177 xpt_done(start_ccb);
3184 xpt_print(start_ccb->ccb_h.path,
3185 "%s: CCB type %#x %s not supported\n", __func__,
3186 start_ccb->ccb_h.func_code,
3187 xpt_action_name(start_ccb->ccb_h.func_code));
3188 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3189 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3190 xpt_done(start_ccb);
3194 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3195 ("xpt_action_default: func= %#x %s status %#x\n",
3196 start_ccb->ccb_h.func_code,
3197 xpt_action_name(start_ccb->ccb_h.func_code),
3198 start_ccb->ccb_h.status));
3202 xpt_polled_action(union ccb *start_ccb)
3205 struct cam_sim *sim;
3206 struct cam_devq *devq;
3210 timeout = start_ccb->ccb_h.timeout * 10;
3211 sim = start_ccb->ccb_h.path->bus->sim;
3214 dev = start_ccb->ccb_h.path->device;
3216 mtx_unlock(&dev->device_mtx);
3219 * Steal an opening so that no other queued requests
3220 * can get it before us while we simulate interrupts.
3222 mtx_lock(&devq->send_mtx);
3223 dev->ccbq.dev_openings--;
3224 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3226 mtx_unlock(&devq->send_mtx);
3230 (*(sim->sim_poll))(sim);
3234 mtx_lock(&devq->send_mtx);
3236 dev->ccbq.dev_openings++;
3237 mtx_unlock(&devq->send_mtx);
3240 xpt_action(start_ccb);
3241 while(--timeout > 0) {
3244 (*(sim->sim_poll))(sim);
3248 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3255 * XXX Is it worth adding a sim_timeout entry
3256 * point so we can attempt recovery? If
3257 * this is only used for dumps, I don't think
3260 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3263 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3266 mtx_lock(&dev->device_mtx);
3270 * Schedule a peripheral driver to receive a ccb when its
3271 * target device has space for more transactions.
3274 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3277 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3278 cam_periph_assert(periph, MA_OWNED);
3279 if (new_priority < periph->scheduled_priority) {
3280 periph->scheduled_priority = new_priority;
3281 xpt_run_allocq(periph, 0);
3287 * Schedule a device to run on a given queue.
3288 * If the device was inserted as a new entry on the queue,
3289 * return 1 meaning the device queue should be run. If we
3290 * were already queued, implying someone else has already
3291 * started the queue, return 0 so the caller doesn't attempt
3295 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3296 u_int32_t new_priority)
3299 u_int32_t old_priority;
3301 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3303 old_priority = pinfo->priority;
3306 * Are we already queued?
3308 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3309 /* Simply reorder based on new priority */
3310 if (new_priority < old_priority) {
3311 camq_change_priority(queue, pinfo->index,
3313 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3314 ("changed priority to %d\n",
3320 /* New entry on the queue */
3321 if (new_priority < old_priority)
3322 pinfo->priority = new_priority;
3324 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3325 ("Inserting onto queue\n"));
3326 pinfo->generation = ++queue->generation;
3327 camq_insert(queue, pinfo);
3334 xpt_run_allocq_task(void *context, int pending)
3336 struct cam_periph *periph = context;
3338 cam_periph_lock(periph);
3339 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3340 xpt_run_allocq(periph, 1);
3341 cam_periph_unlock(periph);
3342 cam_periph_release(periph);
3346 xpt_run_allocq(struct cam_periph *periph, int sleep)
3348 struct cam_ed *device;
3352 cam_periph_assert(periph, MA_OWNED);
3353 if (periph->periph_allocating)
3355 periph->periph_allocating = 1;
3356 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3357 device = periph->path->device;
3360 while ((prio = min(periph->scheduled_priority,
3361 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3362 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3363 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3366 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3368 ccb = xpt_get_ccb(periph);
3371 if (periph->flags & CAM_PERIPH_RUN_TASK)
3373 cam_periph_doacquire(periph);
3374 periph->flags |= CAM_PERIPH_RUN_TASK;
3375 taskqueue_enqueue(xsoftc.xpt_taskq,
3376 &periph->periph_run_task);
3379 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3380 if (prio == periph->immediate_priority) {
3381 periph->immediate_priority = CAM_PRIORITY_NONE;
3382 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3383 ("waking cam_periph_getccb()\n"));
3384 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3386 wakeup(&periph->ccb_list);
3388 periph->scheduled_priority = CAM_PRIORITY_NONE;
3389 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3390 ("calling periph_start()\n"));
3391 periph->periph_start(periph, ccb);
3396 xpt_release_ccb(ccb);
3397 periph->periph_allocating = 0;
3401 xpt_run_devq(struct cam_devq *devq)
3405 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3407 devq->send_queue.qfrozen_cnt++;
3408 while ((devq->send_queue.entries > 0)
3409 && (devq->send_openings > 0)
3410 && (devq->send_queue.qfrozen_cnt <= 1)) {
3411 struct cam_ed *device;
3412 union ccb *work_ccb;
3413 struct cam_sim *sim;
3414 struct xpt_proto *proto;
3416 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3418 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3419 ("running device %p\n", device));
3421 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3422 if (work_ccb == NULL) {
3423 printf("device on run queue with no ccbs???\n");
3427 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3429 mtx_lock(&xsoftc.xpt_highpower_lock);
3430 if (xsoftc.num_highpower <= 0) {
3432 * We got a high power command, but we
3433 * don't have any available slots. Freeze
3434 * the device queue until we have a slot
3437 xpt_freeze_devq_device(device, 1);
3438 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3441 mtx_unlock(&xsoftc.xpt_highpower_lock);
3445 * Consume a high power slot while
3448 xsoftc.num_highpower--;
3450 mtx_unlock(&xsoftc.xpt_highpower_lock);
3452 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3453 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3454 devq->send_openings--;
3455 devq->send_active++;
3456 xpt_schedule_devq(devq, device);
3457 mtx_unlock(&devq->send_mtx);
3459 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3461 * The client wants to freeze the queue
3462 * after this CCB is sent.
3464 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3467 /* In Target mode, the peripheral driver knows best... */
3468 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3469 if ((device->inq_flags & SID_CmdQue) != 0
3470 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3471 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3474 * Clear this in case of a retried CCB that
3475 * failed due to a rejected tag.
3477 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3480 KASSERT(device == work_ccb->ccb_h.path->device,
3481 ("device (%p) / path->device (%p) mismatch",
3482 device, work_ccb->ccb_h.path->device));
3483 proto = xpt_proto_find(device->protocol);
3484 if (proto && proto->ops->debug_out)
3485 proto->ops->debug_out(work_ccb);
3488 * Device queues can be shared among multiple SIM instances
3489 * that reside on different buses. Use the SIM from the
3490 * queued device, rather than the one from the calling bus.
3494 if (mtx && !mtx_owned(mtx))
3498 work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3499 (*(sim->sim_action))(sim, work_ccb);
3502 mtx_lock(&devq->send_mtx);
3504 devq->send_queue.qfrozen_cnt--;
3508 * This function merges stuff from the slave ccb into the master ccb, while
3509 * keeping important fields in the master ccb constant.
3512 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3516 * Pull fields that are valid for peripheral drivers to set
3517 * into the master CCB along with the CCB "payload".
3519 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3520 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3521 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3522 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3523 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3524 sizeof(union ccb) - sizeof(struct ccb_hdr));
3528 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3529 u_int32_t priority, u_int32_t flags)
3532 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3533 ccb_h->pinfo.priority = priority;
3535 ccb_h->path_id = path->bus->path_id;
3537 ccb_h->target_id = path->target->target_id;
3539 ccb_h->target_id = CAM_TARGET_WILDCARD;
3541 ccb_h->target_lun = path->device->lun_id;
3542 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3544 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3546 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3547 ccb_h->flags = flags;
3552 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3554 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3557 /* Path manipulation functions */
3559 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3560 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3562 struct cam_path *path;
3565 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3568 status = CAM_RESRC_UNAVAIL;
3571 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3572 if (status != CAM_REQ_CMP) {
3573 free(path, M_CAMPATH);
3576 *new_path_ptr = path;
3581 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3582 struct cam_periph *periph, path_id_t path_id,
3583 target_id_t target_id, lun_id_t lun_id)
3586 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3591 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3592 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3595 struct cam_et *target;
3596 struct cam_ed *device;
3599 status = CAM_REQ_CMP; /* Completed without error */
3600 target = NULL; /* Wildcarded */
3601 device = NULL; /* Wildcarded */
3604 * We will potentially modify the EDT, so block interrupts
3605 * that may attempt to create cam paths.
3607 bus = xpt_find_bus(path_id);
3609 status = CAM_PATH_INVALID;
3612 mtx_lock(&bus->eb_mtx);
3613 target = xpt_find_target(bus, target_id);
3614 if (target == NULL) {
3616 struct cam_et *new_target;
3618 new_target = xpt_alloc_target(bus, target_id);
3619 if (new_target == NULL) {
3620 status = CAM_RESRC_UNAVAIL;
3622 target = new_target;
3626 if (target != NULL) {
3627 device = xpt_find_device(target, lun_id);
3628 if (device == NULL) {
3630 struct cam_ed *new_device;
3633 (*(bus->xport->ops->alloc_device))(bus,
3636 if (new_device == NULL) {
3637 status = CAM_RESRC_UNAVAIL;
3639 device = new_device;
3643 mtx_unlock(&bus->eb_mtx);
3647 * Only touch the user's data if we are successful.
3649 if (status == CAM_REQ_CMP) {
3650 new_path->periph = perph;
3651 new_path->bus = bus;
3652 new_path->target = target;
3653 new_path->device = device;
3654 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3657 xpt_release_device(device);
3659 xpt_release_target(target);
3661 xpt_release_bus(bus);
3667 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3669 struct cam_path *new_path;
3671 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3672 if (new_path == NULL)
3673 return(CAM_RESRC_UNAVAIL);
3674 xpt_copy_path(new_path, path);
3675 *new_path_ptr = new_path;
3676 return (CAM_REQ_CMP);
3680 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3684 if (path->bus != NULL)
3685 xpt_acquire_bus(path->bus);
3686 if (path->target != NULL)
3687 xpt_acquire_target(path->target);
3688 if (path->device != NULL)
3689 xpt_acquire_device(path->device);
3693 xpt_release_path(struct cam_path *path)
3695 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3696 if (path->device != NULL) {
3697 xpt_release_device(path->device);
3698 path->device = NULL;
3700 if (path->target != NULL) {
3701 xpt_release_target(path->target);
3702 path->target = NULL;
3704 if (path->bus != NULL) {
3705 xpt_release_bus(path->bus);
3711 xpt_free_path(struct cam_path *path)
3714 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3715 xpt_release_path(path);
3716 free(path, M_CAMPATH);
3720 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3721 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3727 *bus_ref = path->bus->refcount;
3733 *periph_ref = path->periph->refcount;
3740 *target_ref = path->target->refcount;
3746 *device_ref = path->device->refcount;
3753 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3754 * in path1, 2 for match with wildcards in path2.
3757 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3761 if (path1->bus != path2->bus) {
3762 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3764 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3769 if (path1->target != path2->target) {
3770 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3773 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3778 if (path1->device != path2->device) {
3779 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3782 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3791 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3795 if (path->bus != dev->target->bus) {
3796 if (path->bus->path_id == CAM_BUS_WILDCARD)
3798 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3803 if (path->target != dev->target) {
3804 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3807 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3812 if (path->device != dev) {
3813 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3816 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3825 xpt_print_path(struct cam_path *path)
3828 char buffer[XPT_PRINT_LEN];
3830 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3831 xpt_path_sbuf(path, &sb);
3833 printf("%s", sbuf_data(&sb));
3838 xpt_print_device(struct cam_ed *device)
3842 printf("(nopath): ");
3844 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3845 device->sim->unit_number,
3846 device->sim->bus_id,
3847 device->target->target_id,
3848 (uintmax_t)device->lun_id);
3853 xpt_print(struct cam_path *path, const char *fmt, ...)
3857 char buffer[XPT_PRINT_LEN];
3859 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3861 xpt_path_sbuf(path, &sb);
3863 sbuf_vprintf(&sb, fmt, ap);
3867 printf("%s", sbuf_data(&sb));
3872 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3877 sbuf_new(&sb, str, str_len, 0);
3878 len = xpt_path_sbuf(path, &sb);
3884 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3888 sbuf_printf(sb, "(nopath): ");
3890 if (path->periph != NULL)
3891 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3892 path->periph->unit_number);
3894 sbuf_printf(sb, "(noperiph:");
3896 if (path->bus != NULL)
3897 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3898 path->bus->sim->unit_number,
3899 path->bus->sim->bus_id);
3901 sbuf_printf(sb, "nobus:");
3903 if (path->target != NULL)
3904 sbuf_printf(sb, "%d:", path->target->target_id);
3906 sbuf_printf(sb, "X:");
3908 if (path->device != NULL)
3909 sbuf_printf(sb, "%jx): ",
3910 (uintmax_t)path->device->lun_id);
3912 sbuf_printf(sb, "X): ");
3915 return(sbuf_len(sb));
3919 xpt_path_path_id(struct cam_path *path)
3921 return(path->bus->path_id);
3925 xpt_path_target_id(struct cam_path *path)
3927 if (path->target != NULL)
3928 return (path->target->target_id);
3930 return (CAM_TARGET_WILDCARD);
3934 xpt_path_lun_id(struct cam_path *path)
3936 if (path->device != NULL)
3937 return (path->device->lun_id);
3939 return (CAM_LUN_WILDCARD);
3943 xpt_path_sim(struct cam_path *path)
3946 return (path->bus->sim);
3950 xpt_path_periph(struct cam_path *path)
3953 return (path->periph);
3957 * Release a CAM control block for the caller. Remit the cost of the structure
3958 * to the device referenced by the path. If the this device had no 'credits'
3959 * and peripheral drivers have registered async callbacks for this notification
3963 xpt_release_ccb(union ccb *free_ccb)
3965 struct cam_ed *device;
3966 struct cam_periph *periph;
3968 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3969 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3970 device = free_ccb->ccb_h.path->device;
3971 periph = free_ccb->ccb_h.path->periph;
3973 xpt_free_ccb(free_ccb);
3974 periph->periph_allocated--;
3975 cam_ccbq_release_opening(&device->ccbq);
3976 xpt_run_allocq(periph, 0);
3979 /* Functions accessed by SIM drivers */
3981 static struct xpt_xport_ops xport_default_ops = {
3982 .alloc_device = xpt_alloc_device_default,
3983 .action = xpt_action_default,
3984 .async = xpt_dev_async_default,
3986 static struct xpt_xport xport_default = {
3987 .xport = XPORT_UNKNOWN,
3989 .ops = &xport_default_ops,
3992 CAM_XPT_XPORT(xport_default);
3995 * A sim structure, listing the SIM entry points and instance
3996 * identification info is passed to xpt_bus_register to hook the SIM
3997 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3998 * for this new bus and places it in the array of buses and assigns
3999 * it a path_id. The path_id may be influenced by "hard wiring"
4000 * information specified by the user. Once interrupt services are
4001 * available, the bus will be probed.
4004 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
4006 struct cam_eb *new_bus;
4007 struct cam_eb *old_bus;
4008 struct ccb_pathinq cpi;
4009 struct cam_path *path;
4013 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4014 M_CAMXPT, M_NOWAIT|M_ZERO);
4015 if (new_bus == NULL) {
4016 /* Couldn't satisfy request */
4017 return (CAM_RESRC_UNAVAIL);
4020 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4021 TAILQ_INIT(&new_bus->et_entries);
4024 timevalclear(&new_bus->last_reset);
4026 new_bus->refcount = 1; /* Held until a bus_deregister event */
4027 new_bus->generation = 0;
4030 sim->path_id = new_bus->path_id =
4031 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4032 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4033 while (old_bus != NULL
4034 && old_bus->path_id < new_bus->path_id)
4035 old_bus = TAILQ_NEXT(old_bus, links);
4036 if (old_bus != NULL)
4037 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4039 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4040 xsoftc.bus_generation++;
4044 * Set a default transport so that a PATH_INQ can be issued to
4045 * the SIM. This will then allow for probing and attaching of
4046 * a more appropriate transport.
4048 new_bus->xport = &xport_default;
4050 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
4051 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4052 if (status != CAM_REQ_CMP) {
4053 xpt_release_bus(new_bus);
4054 free(path, M_CAMXPT);
4055 return (CAM_RESRC_UNAVAIL);
4058 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
4059 cpi.ccb_h.func_code = XPT_PATH_INQ;
4060 xpt_action((union ccb *)&cpi);
4062 if (cpi.ccb_h.status == CAM_REQ_CMP) {
4063 struct xpt_xport **xpt;
4065 SET_FOREACH(xpt, cam_xpt_xport_set) {
4066 if ((*xpt)->xport == cpi.transport) {
4067 new_bus->xport = *xpt;
4071 if (new_bus->xport == NULL) {
4073 "No transport found for %d\n", cpi.transport);
4074 xpt_release_bus(new_bus);
4075 free(path, M_CAMXPT);
4076 return (CAM_RESRC_UNAVAIL);
4080 /* Notify interested parties */
4081 if (sim->path_id != CAM_XPT_PATH_ID) {
4083 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4084 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4085 union ccb *scan_ccb;
4087 /* Initiate bus rescan. */
4088 scan_ccb = xpt_alloc_ccb_nowait();
4089 if (scan_ccb != NULL) {
4090 scan_ccb->ccb_h.path = path;
4091 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4092 scan_ccb->crcn.flags = 0;
4093 xpt_rescan(scan_ccb);
4096 "Can't allocate CCB to scan bus\n");
4097 xpt_free_path(path);
4100 xpt_free_path(path);
4102 xpt_free_path(path);
4103 return (CAM_SUCCESS);
4107 xpt_bus_deregister(path_id_t pathid)
4109 struct cam_path bus_path;
4112 status = xpt_compile_path(&bus_path, NULL, pathid,
4113 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4114 if (status != CAM_REQ_CMP)
4117 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4118 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4120 /* Release the reference count held while registered. */
4121 xpt_release_bus(bus_path.bus);
4122 xpt_release_path(&bus_path);
4124 return (CAM_REQ_CMP);
4128 xptnextfreepathid(void)
4134 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4136 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4138 /* Find an unoccupied pathid */
4139 while (bus != NULL && bus->path_id <= pathid) {
4140 if (bus->path_id == pathid)
4142 bus = TAILQ_NEXT(bus, links);
4146 * Ensure that this pathid is not reserved for
4147 * a bus that may be registered in the future.
4149 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4151 /* Start the search over */
4158 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4165 pathid = CAM_XPT_PATH_ID;
4166 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4167 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4170 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4171 if (strcmp(dname, "scbus")) {
4172 /* Avoid a bit of foot shooting. */
4175 if (dunit < 0) /* unwired?! */
4177 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4178 if (sim_bus == val) {
4182 } else if (sim_bus == 0) {
4183 /* Unspecified matches bus 0 */
4187 printf("Ambiguous scbus configuration for %s%d "
4188 "bus %d, cannot wire down. The kernel "
4189 "config entry for scbus%d should "
4190 "specify a controller bus.\n"
4191 "Scbus will be assigned dynamically.\n",
4192 sim_name, sim_unit, sim_bus, dunit);
4197 if (pathid == CAM_XPT_PATH_ID)
4198 pathid = xptnextfreepathid();
4203 xpt_async_string(u_int32_t async_code)
4206 switch (async_code) {
4207 case AC_BUS_RESET: return ("AC_BUS_RESET");
4208 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4209 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4210 case AC_SENT_BDR: return ("AC_SENT_BDR");
4211 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4212 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4213 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4214 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4215 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4216 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4217 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4218 case AC_CONTRACT: return ("AC_CONTRACT");
4219 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4220 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4222 return ("AC_UNKNOWN");
4226 xpt_async_size(u_int32_t async_code)
4229 switch (async_code) {
4230 case AC_BUS_RESET: return (0);
4231 case AC_UNSOL_RESEL: return (0);
4232 case AC_SCSI_AEN: return (0);
4233 case AC_SENT_BDR: return (0);
4234 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4235 case AC_PATH_DEREGISTERED: return (0);
4236 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4237 case AC_LOST_DEVICE: return (0);
4238 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4239 case AC_INQ_CHANGED: return (0);
4240 case AC_GETDEV_CHANGED: return (0);
4241 case AC_CONTRACT: return (sizeof(struct ac_contract));
4242 case AC_ADVINFO_CHANGED: return (-1);
4243 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4249 xpt_async_process_dev(struct cam_ed *device, void *arg)
4251 union ccb *ccb = arg;
4252 struct cam_path *path = ccb->ccb_h.path;
4253 void *async_arg = ccb->casync.async_arg_ptr;
4254 u_int32_t async_code = ccb->casync.async_code;
4257 if (path->device != device
4258 && path->device->lun_id != CAM_LUN_WILDCARD
4259 && device->lun_id != CAM_LUN_WILDCARD)
4263 * The async callback could free the device.
4264 * If it is a broadcast async, it doesn't hold
4265 * device reference, so take our own reference.
4267 xpt_acquire_device(device);
4270 * If async for specific device is to be delivered to
4271 * the wildcard client, take the specific device lock.
4272 * XXX: We may need a way for client to specify it.
4274 if ((device->lun_id == CAM_LUN_WILDCARD &&
4275 path->device->lun_id != CAM_LUN_WILDCARD) ||
4276 (device->target->target_id == CAM_TARGET_WILDCARD &&
4277 path->target->target_id != CAM_TARGET_WILDCARD) ||
4278 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4279 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4280 mtx_unlock(&device->device_mtx);
4281 xpt_path_lock(path);
4286 (*(device->target->bus->xport->ops->async))(async_code,
4287 device->target->bus, device->target, device, async_arg);
4288 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4291 xpt_path_unlock(path);
4292 mtx_lock(&device->device_mtx);
4294 xpt_release_device(device);
4299 xpt_async_process_tgt(struct cam_et *target, void *arg)
4301 union ccb *ccb = arg;
4302 struct cam_path *path = ccb->ccb_h.path;
4304 if (path->target != target
4305 && path->target->target_id != CAM_TARGET_WILDCARD
4306 && target->target_id != CAM_TARGET_WILDCARD)
4309 if (ccb->casync.async_code == AC_SENT_BDR) {
4310 /* Update our notion of when the last reset occurred */
4311 microtime(&target->last_reset);
4314 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4318 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4321 struct cam_path *path;
4323 u_int32_t async_code;
4325 path = ccb->ccb_h.path;
4326 async_code = ccb->casync.async_code;
4327 async_arg = ccb->casync.async_arg_ptr;
4328 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4329 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4332 if (async_code == AC_BUS_RESET) {
4333 /* Update our notion of when the last reset occurred */
4334 microtime(&bus->last_reset);
4337 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4340 * If this wasn't a fully wildcarded async, tell all
4341 * clients that want all async events.
4343 if (bus != xpt_periph->path->bus) {
4344 xpt_path_lock(xpt_periph->path);
4345 xpt_async_process_dev(xpt_periph->path->device, ccb);
4346 xpt_path_unlock(xpt_periph->path);
4349 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4350 xpt_release_devq(path, 1, TRUE);
4352 xpt_release_simq(path->bus->sim, TRUE);
4353 if (ccb->casync.async_arg_size > 0)
4354 free(async_arg, M_CAMXPT);
4355 xpt_free_path(path);
4360 xpt_async_bcast(struct async_list *async_head,
4361 u_int32_t async_code,
4362 struct cam_path *path, void *async_arg)
4364 struct async_node *cur_entry;
4367 cur_entry = SLIST_FIRST(async_head);
4368 while (cur_entry != NULL) {
4369 struct async_node *next_entry;
4371 * Grab the next list entry before we call the current
4372 * entry's callback. This is because the callback function
4373 * can delete its async callback entry.
4375 next_entry = SLIST_NEXT(cur_entry, links);
4376 if ((cur_entry->event_enable & async_code) != 0) {
4377 mtx = cur_entry->event_lock ?
4378 path->device->sim->mtx : NULL;
4381 cur_entry->callback(cur_entry->callback_arg,
4387 cur_entry = next_entry;
4392 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4397 ccb = xpt_alloc_ccb_nowait();
4399 xpt_print(path, "Can't allocate CCB to send %s\n",
4400 xpt_async_string(async_code));
4404 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4405 xpt_print(path, "Can't allocate path to send %s\n",
4406 xpt_async_string(async_code));
4410 ccb->ccb_h.path->periph = NULL;
4411 ccb->ccb_h.func_code = XPT_ASYNC;
4412 ccb->ccb_h.cbfcnp = xpt_async_process;
4413 ccb->ccb_h.flags |= CAM_UNLOCKED;
4414 ccb->casync.async_code = async_code;
4415 ccb->casync.async_arg_size = 0;
4416 size = xpt_async_size(async_code);
4417 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4418 ("xpt_async: func %#x %s aync_code %d %s\n",
4419 ccb->ccb_h.func_code,
4420 xpt_action_name(ccb->ccb_h.func_code),
4422 xpt_async_string(async_code)));
4423 if (size > 0 && async_arg != NULL) {
4424 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4425 if (ccb->casync.async_arg_ptr == NULL) {
4426 xpt_print(path, "Can't allocate argument to send %s\n",
4427 xpt_async_string(async_code));
4428 xpt_free_path(ccb->ccb_h.path);
4432 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4433 ccb->casync.async_arg_size = size;
4434 } else if (size < 0) {
4435 ccb->casync.async_arg_ptr = async_arg;
4436 ccb->casync.async_arg_size = size;
4438 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4439 xpt_freeze_devq(path, 1);
4441 xpt_freeze_simq(path->bus->sim, 1);
4446 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4447 struct cam_et *target, struct cam_ed *device,
4452 * We only need to handle events for real devices.
4454 if (target->target_id == CAM_TARGET_WILDCARD
4455 || device->lun_id == CAM_LUN_WILDCARD)
4458 printf("%s called\n", __func__);
4462 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4464 struct cam_devq *devq;
4467 devq = dev->sim->devq;
4468 mtx_assert(&devq->send_mtx, MA_OWNED);
4469 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4470 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4471 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4472 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4473 /* Remove frozen device from sendq. */
4474 if (device_is_queued(dev))
4475 camq_remove(&devq->send_queue, dev->devq_entry.index);
4480 xpt_freeze_devq(struct cam_path *path, u_int count)
4482 struct cam_ed *dev = path->device;
4483 struct cam_devq *devq;
4486 devq = dev->sim->devq;
4487 mtx_lock(&devq->send_mtx);
4488 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4489 freeze = xpt_freeze_devq_device(dev, count);
4490 mtx_unlock(&devq->send_mtx);
4495 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4497 struct cam_devq *devq;
4501 mtx_lock(&devq->send_mtx);
4502 freeze = (devq->send_queue.qfrozen_cnt += count);
4503 mtx_unlock(&devq->send_mtx);
4508 xpt_release_devq_timeout(void *arg)
4511 struct cam_devq *devq;
4513 dev = (struct cam_ed *)arg;
4514 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4515 devq = dev->sim->devq;
4516 mtx_assert(&devq->send_mtx, MA_OWNED);
4517 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4522 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4525 struct cam_devq *devq;
4527 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4530 devq = dev->sim->devq;
4531 mtx_lock(&devq->send_mtx);
4532 if (xpt_release_devq_device(dev, count, run_queue))
4533 xpt_run_devq(dev->sim->devq);
4534 mtx_unlock(&devq->send_mtx);
4538 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4541 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4542 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4543 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4544 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4545 if (count > dev->ccbq.queue.qfrozen_cnt) {
4547 printf("xpt_release_devq(): requested %u > present %u\n",
4548 count, dev->ccbq.queue.qfrozen_cnt);
4550 count = dev->ccbq.queue.qfrozen_cnt;
4552 dev->ccbq.queue.qfrozen_cnt -= count;
4553 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4555 * No longer need to wait for a successful
4556 * command completion.
4558 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4560 * Remove any timeouts that might be scheduled
4561 * to release this queue.
4563 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4564 callout_stop(&dev->callout);
4565 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4568 * Now that we are unfrozen schedule the
4569 * device so any pending transactions are
4572 xpt_schedule_devq(dev->sim->devq, dev);
4579 xpt_release_simq(struct cam_sim *sim, int run_queue)
4581 struct cam_devq *devq;
4584 mtx_lock(&devq->send_mtx);
4585 if (devq->send_queue.qfrozen_cnt <= 0) {
4587 printf("xpt_release_simq: requested 1 > present %u\n",
4588 devq->send_queue.qfrozen_cnt);
4591 devq->send_queue.qfrozen_cnt--;
4592 if (devq->send_queue.qfrozen_cnt == 0) {
4594 * If there is a timeout scheduled to release this
4595 * sim queue, remove it. The queue frozen count is
4598 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4599 callout_stop(&sim->callout);
4600 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4604 * Now that we are unfrozen run the send queue.
4606 xpt_run_devq(sim->devq);
4609 mtx_unlock(&devq->send_mtx);
4613 * XXX Appears to be unused.
4616 xpt_release_simq_timeout(void *arg)
4618 struct cam_sim *sim;
4620 sim = (struct cam_sim *)arg;
4621 xpt_release_simq(sim, /* run_queue */ TRUE);
4625 xpt_done(union ccb *done_ccb)
4627 struct cam_doneq *queue;
4630 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4631 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4632 done_ccb->csio.bio != NULL)
4633 biotrack(done_ccb->csio.bio, __func__);
4636 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4637 ("xpt_done: func= %#x %s status %#x\n",
4638 done_ccb->ccb_h.func_code,
4639 xpt_action_name(done_ccb->ccb_h.func_code),
4640 done_ccb->ccb_h.status));
4641 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4644 /* Store the time the ccb was in the sim */
4645 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4646 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4647 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4648 queue = &cam_doneqs[hash];
4649 mtx_lock(&queue->cam_doneq_mtx);
4650 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4651 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4652 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4653 mtx_unlock(&queue->cam_doneq_mtx);
4655 wakeup(&queue->cam_doneq);
4659 xpt_done_direct(union ccb *done_ccb)
4662 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4663 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4664 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4667 /* Store the time the ccb was in the sim */
4668 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4669 xpt_done_process(&done_ccb->ccb_h);
4677 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4682 xpt_alloc_ccb_nowait()
4686 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4691 xpt_free_ccb(union ccb *free_ccb)
4693 free(free_ccb, M_CAMCCB);
4698 /* Private XPT functions */
4701 * Get a CAM control block for the caller. Charge the structure to the device
4702 * referenced by the path. If we don't have sufficient resources to allocate
4703 * more ccbs, we return NULL.
4706 xpt_get_ccb_nowait(struct cam_periph *periph)
4710 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4711 if (new_ccb == NULL)
4713 periph->periph_allocated++;
4714 cam_ccbq_take_opening(&periph->path->device->ccbq);
4719 xpt_get_ccb(struct cam_periph *periph)
4723 cam_periph_unlock(periph);
4724 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4725 cam_periph_lock(periph);
4726 periph->periph_allocated++;
4727 cam_ccbq_take_opening(&periph->path->device->ccbq);
4732 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4734 struct ccb_hdr *ccb_h;
4736 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4737 cam_periph_assert(periph, MA_OWNED);
4738 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4739 ccb_h->pinfo.priority != priority) {
4740 if (priority < periph->immediate_priority) {
4741 periph->immediate_priority = priority;
4742 xpt_run_allocq(periph, 0);
4744 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4747 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4748 return ((union ccb *)ccb_h);
4752 xpt_acquire_bus(struct cam_eb *bus)
4761 xpt_release_bus(struct cam_eb *bus)
4765 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4766 if (--bus->refcount > 0) {
4770 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4771 xsoftc.bus_generation++;
4773 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4774 ("destroying bus, but target list is not empty"));
4775 cam_sim_release(bus->sim);
4776 mtx_destroy(&bus->eb_mtx);
4777 free(bus, M_CAMXPT);
4780 static struct cam_et *
4781 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4783 struct cam_et *cur_target, *target;
4785 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4786 mtx_assert(&bus->eb_mtx, MA_OWNED);
4787 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4792 TAILQ_INIT(&target->ed_entries);
4794 target->target_id = target_id;
4795 target->refcount = 1;
4796 target->generation = 0;
4797 target->luns = NULL;
4798 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4799 timevalclear(&target->last_reset);
4801 * Hold a reference to our parent bus so it
4802 * will not go away before we do.
4806 /* Insertion sort into our bus's target list */
4807 cur_target = TAILQ_FIRST(&bus->et_entries);
4808 while (cur_target != NULL && cur_target->target_id < target_id)
4809 cur_target = TAILQ_NEXT(cur_target, links);
4810 if (cur_target != NULL) {
4811 TAILQ_INSERT_BEFORE(cur_target, target, links);
4813 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4820 xpt_acquire_target(struct cam_et *target)
4822 struct cam_eb *bus = target->bus;
4824 mtx_lock(&bus->eb_mtx);
4826 mtx_unlock(&bus->eb_mtx);
4830 xpt_release_target(struct cam_et *target)
4832 struct cam_eb *bus = target->bus;
4834 mtx_lock(&bus->eb_mtx);
4835 if (--target->refcount > 0) {
4836 mtx_unlock(&bus->eb_mtx);
4839 TAILQ_REMOVE(&bus->et_entries, target, links);
4841 mtx_unlock(&bus->eb_mtx);
4842 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4843 ("destroying target, but device list is not empty"));
4844 xpt_release_bus(bus);
4845 mtx_destroy(&target->luns_mtx);
4847 free(target->luns, M_CAMXPT);
4848 free(target, M_CAMXPT);
4851 static struct cam_ed *
4852 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4855 struct cam_ed *device;
4857 device = xpt_alloc_device(bus, target, lun_id);
4861 device->mintags = 1;
4862 device->maxtags = 1;
4867 xpt_destroy_device(void *context, int pending)
4869 struct cam_ed *device = context;
4871 mtx_lock(&device->device_mtx);
4872 mtx_destroy(&device->device_mtx);
4873 free(device, M_CAMDEV);
4877 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4879 struct cam_ed *cur_device, *device;
4880 struct cam_devq *devq;
4883 mtx_assert(&bus->eb_mtx, MA_OWNED);
4884 /* Make space for us in the device queue on our bus */
4885 devq = bus->sim->devq;
4886 mtx_lock(&devq->send_mtx);
4887 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4888 mtx_unlock(&devq->send_mtx);
4889 if (status != CAM_REQ_CMP)
4892 device = (struct cam_ed *)malloc(sizeof(*device),
4893 M_CAMDEV, M_NOWAIT|M_ZERO);
4897 cam_init_pinfo(&device->devq_entry);
4898 device->target = target;
4899 device->lun_id = lun_id;
4900 device->sim = bus->sim;
4901 if (cam_ccbq_init(&device->ccbq,
4902 bus->sim->max_dev_openings) != 0) {
4903 free(device, M_CAMDEV);
4906 SLIST_INIT(&device->asyncs);
4907 SLIST_INIT(&device->periphs);
4908 device->generation = 0;
4909 device->flags = CAM_DEV_UNCONFIGURED;
4910 device->tag_delay_count = 0;
4911 device->tag_saved_openings = 0;
4912 device->refcount = 1;
4913 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4914 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4915 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4917 * Hold a reference to our parent bus so it
4918 * will not go away before we do.
4922 cur_device = TAILQ_FIRST(&target->ed_entries);
4923 while (cur_device != NULL && cur_device->lun_id < lun_id)
4924 cur_device = TAILQ_NEXT(cur_device, links);
4925 if (cur_device != NULL)
4926 TAILQ_INSERT_BEFORE(cur_device, device, links);
4928 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4929 target->generation++;
4934 xpt_acquire_device(struct cam_ed *device)
4936 struct cam_eb *bus = device->target->bus;
4938 mtx_lock(&bus->eb_mtx);
4940 mtx_unlock(&bus->eb_mtx);
4944 xpt_release_device(struct cam_ed *device)
4946 struct cam_eb *bus = device->target->bus;
4947 struct cam_devq *devq;
4949 mtx_lock(&bus->eb_mtx);
4950 if (--device->refcount > 0) {
4951 mtx_unlock(&bus->eb_mtx);
4955 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4956 device->target->generation++;
4957 mtx_unlock(&bus->eb_mtx);
4959 /* Release our slot in the devq */
4960 devq = bus->sim->devq;
4961 mtx_lock(&devq->send_mtx);
4962 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4963 mtx_unlock(&devq->send_mtx);
4965 KASSERT(SLIST_EMPTY(&device->periphs),
4966 ("destroying device, but periphs list is not empty"));
4967 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4968 ("destroying device while still queued for ccbs"));
4970 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4971 callout_stop(&device->callout);
4973 xpt_release_target(device->target);
4975 cam_ccbq_fini(&device->ccbq);
4977 * Free allocated memory. free(9) does nothing if the
4978 * supplied pointer is NULL, so it is safe to call without
4981 free(device->supported_vpds, M_CAMXPT);
4982 free(device->device_id, M_CAMXPT);
4983 free(device->ext_inq, M_CAMXPT);
4984 free(device->physpath, M_CAMXPT);
4985 free(device->rcap_buf, M_CAMXPT);
4986 free(device->serial_num, M_CAMXPT);
4987 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4991 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4997 mtx_lock(&dev->sim->devq->send_mtx);
4998 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4999 mtx_unlock(&dev->sim->devq->send_mtx);
5000 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5001 || (dev->inq_flags & SID_CmdQue) != 0)
5002 dev->tag_saved_openings = newopenings;
5006 static struct cam_eb *
5007 xpt_find_bus(path_id_t path_id)
5012 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5014 bus = TAILQ_NEXT(bus, links)) {
5015 if (bus->path_id == path_id) {
5024 static struct cam_et *
5025 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5027 struct cam_et *target;
5029 mtx_assert(&bus->eb_mtx, MA_OWNED);
5030 for (target = TAILQ_FIRST(&bus->et_entries);
5032 target = TAILQ_NEXT(target, links)) {
5033 if (target->target_id == target_id) {
5041 static struct cam_ed *
5042 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5044 struct cam_ed *device;
5046 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
5047 for (device = TAILQ_FIRST(&target->ed_entries);
5049 device = TAILQ_NEXT(device, links)) {
5050 if (device->lun_id == lun_id) {
5059 xpt_start_tags(struct cam_path *path)
5061 struct ccb_relsim crs;
5062 struct cam_ed *device;
5063 struct cam_sim *sim;
5066 device = path->device;
5067 sim = path->bus->sim;
5068 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5069 xpt_freeze_devq(path, /*count*/1);
5070 device->inq_flags |= SID_CmdQue;
5071 if (device->tag_saved_openings != 0)
5072 newopenings = device->tag_saved_openings;
5074 newopenings = min(device->maxtags,
5075 sim->max_tagged_dev_openings);
5076 xpt_dev_ccbq_resize(path, newopenings);
5077 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5078 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5079 crs.ccb_h.func_code = XPT_REL_SIMQ;
5080 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5082 = crs.release_timeout
5085 xpt_action((union ccb *)&crs);
5089 xpt_stop_tags(struct cam_path *path)
5091 struct ccb_relsim crs;
5092 struct cam_ed *device;
5093 struct cam_sim *sim;
5095 device = path->device;
5096 sim = path->bus->sim;
5097 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5098 device->tag_delay_count = 0;
5099 xpt_freeze_devq(path, /*count*/1);
5100 device->inq_flags &= ~SID_CmdQue;
5101 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5102 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5103 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5104 crs.ccb_h.func_code = XPT_REL_SIMQ;
5105 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5107 = crs.release_timeout
5110 xpt_action((union ccb *)&crs);
5114 xpt_boot_delay(void *arg)
5121 xpt_config(void *arg)
5124 * Now that interrupts are enabled, go find our devices
5126 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5127 printf("xpt_config: failed to create taskqueue thread.\n");
5129 /* Setup debugging path */
5130 if (cam_dflags != CAM_DEBUG_NONE) {
5131 if (xpt_create_path(&cam_dpath, NULL,
5132 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5133 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5134 printf("xpt_config: xpt_create_path() failed for debug"
5135 " target %d:%d:%d, debugging disabled\n",
5136 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5137 cam_dflags = CAM_DEBUG_NONE;
5142 periphdriver_init(1);
5144 callout_init(&xsoftc.boot_callout, 1);
5145 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5146 xpt_boot_delay, NULL, 0);
5147 /* Fire up rescan thread. */
5148 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5149 "cam", "scanner")) {
5150 printf("xpt_config: failed to create rescan thread.\n");
5158 xsoftc.buses_to_config++;
5163 xpt_release_boot(void)
5166 xsoftc.buses_to_config--;
5167 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5168 struct xpt_task *task;
5170 xsoftc.buses_config_done = 1;
5172 /* Call manually because we don't have any buses */
5173 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5175 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5176 taskqueue_enqueue(taskqueue_thread, &task->task);
5183 * If the given device only has one peripheral attached to it, and if that
5184 * peripheral is the passthrough driver, announce it. This insures that the
5185 * user sees some sort of announcement for every peripheral in their system.
5188 xptpassannouncefunc(struct cam_ed *device, void *arg)
5190 struct cam_periph *periph;
5193 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5194 periph = SLIST_NEXT(periph, periph_links), i++);
5196 periph = SLIST_FIRST(&device->periphs);
5198 && (strncmp(periph->periph_name, "pass", 4) == 0))
5199 xpt_announce_periph(periph, NULL);
5205 xpt_finishconfig_task(void *context, int pending)
5208 periphdriver_init(2);
5210 * Check for devices with no "standard" peripheral driver
5211 * attached. For any devices like that, announce the
5212 * passthrough driver so the user will see something.
5215 xpt_for_all_devices(xptpassannouncefunc, NULL);
5217 /* Release our hook so that the boot can continue. */
5218 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5219 free(xsoftc.xpt_config_hook, M_CAMXPT);
5220 xsoftc.xpt_config_hook = NULL;
5222 free(context, M_CAMXPT);
5226 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5227 struct cam_path *path)
5229 struct ccb_setasync csa;
5234 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5235 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5236 if (status != CAM_REQ_CMP)
5238 xpt_path_lock(path);
5242 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5243 csa.ccb_h.func_code = XPT_SASYNC_CB;
5244 csa.event_enable = event;
5245 csa.callback = cbfunc;
5246 csa.callback_arg = cbarg;
5247 xpt_action((union ccb *)&csa);
5248 status = csa.ccb_h.status;
5250 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5251 ("xpt_register_async: func %p\n", cbfunc));
5254 xpt_path_unlock(path);
5255 xpt_free_path(path);
5258 if ((status == CAM_REQ_CMP) &&
5259 (csa.event_enable & AC_FOUND_DEVICE)) {
5261 * Get this peripheral up to date with all
5262 * the currently existing devices.
5264 xpt_for_all_devices(xptsetasyncfunc, &csa);
5266 if ((status == CAM_REQ_CMP) &&
5267 (csa.event_enable & AC_PATH_REGISTERED)) {
5269 * Get this peripheral up to date with all
5270 * the currently existing buses.
5272 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5279 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5281 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5283 switch (work_ccb->ccb_h.func_code) {
5284 /* Common cases first */
5285 case XPT_PATH_INQ: /* Path routing inquiry */
5287 struct ccb_pathinq *cpi;
5289 cpi = &work_ccb->cpi;
5290 cpi->version_num = 1; /* XXX??? */
5291 cpi->hba_inquiry = 0;
5292 cpi->target_sprt = 0;
5294 cpi->hba_eng_cnt = 0;
5295 cpi->max_target = 0;
5297 cpi->initiator_id = 0;
5298 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5299 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5300 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5301 cpi->unit_number = sim->unit_number;
5302 cpi->bus_id = sim->bus_id;
5303 cpi->base_transfer_speed = 0;
5304 cpi->protocol = PROTO_UNSPECIFIED;
5305 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5306 cpi->transport = XPORT_UNSPECIFIED;
5307 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5308 cpi->ccb_h.status = CAM_REQ_CMP;
5313 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5320 * The xpt as a "controller" has no interrupt sources, so polling
5324 xptpoll(struct cam_sim *sim)
5329 xpt_lock_buses(void)
5331 mtx_lock(&xsoftc.xpt_topo_lock);
5335 xpt_unlock_buses(void)
5337 mtx_unlock(&xsoftc.xpt_topo_lock);
5341 xpt_path_mtx(struct cam_path *path)
5344 return (&path->device->device_mtx);
5348 xpt_done_process(struct ccb_hdr *ccb_h)
5350 struct cam_sim *sim;
5351 struct cam_devq *devq;
5352 struct mtx *mtx = NULL;
5354 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5355 struct ccb_scsiio *csio;
5357 if (ccb_h->func_code == XPT_SCSI_IO) {
5358 csio = &((union ccb *)ccb_h)->csio;
5359 if (csio->bio != NULL)
5360 biotrack(csio->bio, __func__);
5364 if (ccb_h->flags & CAM_HIGH_POWER) {
5365 struct highpowerlist *hphead;
5366 struct cam_ed *device;
5368 mtx_lock(&xsoftc.xpt_highpower_lock);
5369 hphead = &xsoftc.highpowerq;
5371 device = STAILQ_FIRST(hphead);
5374 * Increment the count since this command is done.
5376 xsoftc.num_highpower++;
5379 * Any high powered commands queued up?
5381 if (device != NULL) {
5383 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5384 mtx_unlock(&xsoftc.xpt_highpower_lock);
5386 mtx_lock(&device->sim->devq->send_mtx);
5387 xpt_release_devq_device(device,
5388 /*count*/1, /*runqueue*/TRUE);
5389 mtx_unlock(&device->sim->devq->send_mtx);
5391 mtx_unlock(&xsoftc.xpt_highpower_lock);
5394 sim = ccb_h->path->bus->sim;
5396 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5397 xpt_release_simq(sim, /*run_queue*/FALSE);
5398 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5401 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5402 && (ccb_h->status & CAM_DEV_QFRZN)) {
5403 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5404 ccb_h->status &= ~CAM_DEV_QFRZN;
5408 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5409 struct cam_ed *dev = ccb_h->path->device;
5411 mtx_lock(&devq->send_mtx);
5412 devq->send_active--;
5413 devq->send_openings++;
5414 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5416 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5417 && (dev->ccbq.dev_active == 0))) {
5418 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5419 xpt_release_devq_device(dev, /*count*/1,
5420 /*run_queue*/FALSE);
5423 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5424 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5425 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5426 xpt_release_devq_device(dev, /*count*/1,
5427 /*run_queue*/FALSE);
5430 if (!device_is_queued(dev))
5431 (void)xpt_schedule_devq(devq, dev);
5433 mtx_unlock(&devq->send_mtx);
5435 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5436 mtx = xpt_path_mtx(ccb_h->path);
5439 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5440 && (--dev->tag_delay_count == 0))
5441 xpt_start_tags(ccb_h->path);
5445 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5447 mtx = xpt_path_mtx(ccb_h->path);
5457 /* Call the peripheral driver's callback */
5458 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5459 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5465 xpt_done_td(void *arg)
5467 struct cam_doneq *queue = arg;
5468 struct ccb_hdr *ccb_h;
5469 STAILQ_HEAD(, ccb_hdr) doneq;
5471 STAILQ_INIT(&doneq);
5472 mtx_lock(&queue->cam_doneq_mtx);
5474 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5475 queue->cam_doneq_sleep = 1;
5476 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5478 queue->cam_doneq_sleep = 0;
5480 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5481 mtx_unlock(&queue->cam_doneq_mtx);
5483 THREAD_NO_SLEEPING();
5484 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5485 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5486 xpt_done_process(ccb_h);
5488 THREAD_SLEEPING_OK();
5490 mtx_lock(&queue->cam_doneq_mtx);
5495 camisr_runqueue(void)
5497 struct ccb_hdr *ccb_h;
5498 struct cam_doneq *queue;
5501 /* Process global queues. */
5502 for (i = 0; i < cam_num_doneqs; i++) {
5503 queue = &cam_doneqs[i];
5504 mtx_lock(&queue->cam_doneq_mtx);
5505 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5506 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5507 mtx_unlock(&queue->cam_doneq_mtx);
5508 xpt_done_process(ccb_h);
5509 mtx_lock(&queue->cam_doneq_mtx);
5511 mtx_unlock(&queue->cam_doneq_mtx);
5521 static struct kv map[] = {
5522 { XPT_NOOP, "XPT_NOOP" },
5523 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5524 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5525 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5526 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5527 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5528 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5529 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5530 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5531 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5532 { XPT_DEBUG, "XPT_DEBUG" },
5533 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5534 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5535 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5536 { XPT_ASYNC, "XPT_ASYNC" },
5537 { XPT_ABORT, "XPT_ABORT" },
5538 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5539 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5540 { XPT_TERM_IO, "XPT_TERM_IO" },
5541 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5542 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5543 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5544 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5545 { XPT_ATA_IO, "XPT_ATA_IO" },
5546 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5547 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5548 { XPT_NVME_IO, "XPT_NVME_IO" },
5549 { XPT_MMC_IO, "XPT_MMC_IO" },
5550 { XPT_SMP_IO, "XPT_SMP_IO" },
5551 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5552 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5553 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5554 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5555 { XPT_EN_LUN, "XPT_EN_LUN" },
5556 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5557 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5558 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5559 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5560 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5561 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5562 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5567 xpt_action_name(uint32_t action)
5569 static char buffer[32]; /* Only for unknown messages -- racy */
5570 struct kv *walker = map;
5572 while (walker->name != NULL) {
5573 if (walker->v == action)
5574 return (walker->name);
5578 snprintf(buffer, sizeof(buffer), "%#x", action);