2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "opt_printf.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/types.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
50 #include <sys/taskqueue.h>
53 #include <sys/mutex.h>
54 #include <sys/sysctl.h>
55 #include <sys/kthread.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_iosched.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_queue.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt.h>
64 #include <cam/cam_xpt_sim.h>
65 #include <cam/cam_xpt_periph.h>
66 #include <cam/cam_xpt_internal.h>
67 #include <cam/cam_debug.h>
68 #include <cam/cam_compat.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
72 #include <cam/scsi/scsi_pass.h>
74 #include <machine/md_var.h> /* geometry translation */
75 #include <machine/stdarg.h> /* for xpt_print below */
79 /* Wild guess based on not wanting to grow the stack too much */
80 #define XPT_PRINT_MAXLEN 512
81 #ifdef PRINTF_BUFR_SIZE
82 #define XPT_PRINT_LEN PRINTF_BUFR_SIZE
84 #define XPT_PRINT_LEN 128
86 _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
89 * This is the maximum number of high powered commands (e.g. start unit)
90 * that can be outstanding at a particular time.
92 #ifndef CAM_MAX_HIGHPOWER
93 #define CAM_MAX_HIGHPOWER 4
96 /* Datastructures internal to the xpt layer */
97 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
98 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
99 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
100 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
102 /* Object for defering XPT actions to a taskqueue */
110 uint32_t xpt_generation;
112 /* number of high powered commands that can go through right now */
113 struct mtx xpt_highpower_lock;
114 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
117 /* queue for handling async rescan requests. */
118 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
120 int buses_config_done;
126 * N.B., "busses" is an archaic spelling of "buses". In new code
127 * "buses" is preferred.
129 TAILQ_HEAD(,cam_eb) xpt_busses;
130 u_int bus_generation;
132 struct intr_config_hook xpt_config_hook;
135 struct callout boot_callout;
137 struct mtx xpt_topo_lock;
139 struct taskqueue *xpt_taskq;
144 DM_RET_FLAG_MASK = 0x0f,
147 DM_RET_DESCEND = 0x20,
149 DM_RET_ACTION_MASK = 0xf0
157 } xpt_traverse_depth;
159 struct xpt_traverse_config {
160 xpt_traverse_depth depth;
165 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
166 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
167 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
168 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
169 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
171 /* Transport layer configuration information */
172 static struct xpt_softc xsoftc;
174 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
176 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
177 &xsoftc.boot_delay, 0, "Bus registration wait time");
178 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
179 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
180 SYSCTL_INT(_kern_cam, OID_AUTO, announce_nosbuf, CTLFLAG_RWTUN,
181 &xsoftc.announce_nosbuf, 0, "Don't use sbuf for announcements");
184 struct mtx_padalign cam_doneq_mtx;
185 STAILQ_HEAD(, ccb_hdr) cam_doneq;
189 static struct cam_doneq cam_doneqs[MAXCPU];
190 static int cam_num_doneqs;
191 static struct proc *cam_proc;
193 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
194 &cam_num_doneqs, 0, "Number of completion queues/threads");
196 struct cam_periph *xpt_periph;
198 static periph_init_t xpt_periph_init;
200 static struct periph_driver xpt_driver =
202 xpt_periph_init, "xpt",
203 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
207 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
209 static d_open_t xptopen;
210 static d_close_t xptclose;
211 static d_ioctl_t xptioctl;
212 static d_ioctl_t xptdoioctl;
214 static struct cdevsw xpt_cdevsw = {
215 .d_version = D_VERSION,
223 /* Storage for debugging datastructures */
224 struct cam_path *cam_dpath;
225 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
226 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
227 &cam_dflags, 0, "Enabled debug flags");
228 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
229 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
230 &cam_debug_delay, 0, "Delay in us after each debug message");
232 /* Our boot-time initialization hook */
233 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
235 static moduledata_t cam_moduledata = {
237 cam_module_event_handler,
241 static int xpt_init(void *);
243 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
244 MODULE_VERSION(cam, 1);
247 static void xpt_async_bcast(struct async_list *async_head,
248 u_int32_t async_code,
249 struct cam_path *path,
251 static path_id_t xptnextfreepathid(void);
252 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
253 static union ccb *xpt_get_ccb(struct cam_periph *periph);
254 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
255 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
256 static void xpt_run_allocq_task(void *context, int pending);
257 static void xpt_run_devq(struct cam_devq *devq);
258 static timeout_t xpt_release_devq_timeout;
259 static void xpt_release_simq_timeout(void *arg) __unused;
260 static void xpt_acquire_bus(struct cam_eb *bus);
261 static void xpt_release_bus(struct cam_eb *bus);
262 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
263 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
265 static struct cam_et*
266 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
267 static void xpt_acquire_target(struct cam_et *target);
268 static void xpt_release_target(struct cam_et *target);
269 static struct cam_eb*
270 xpt_find_bus(path_id_t path_id);
271 static struct cam_et*
272 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
273 static struct cam_ed*
274 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
275 static void xpt_config(void *arg);
276 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
277 u_int32_t new_priority);
278 static xpt_devicefunc_t xptpassannouncefunc;
279 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
280 static void xptpoll(struct cam_sim *sim);
281 static void camisr_runqueue(void);
282 static void xpt_done_process(struct ccb_hdr *ccb_h);
283 static void xpt_done_td(void *);
284 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
285 u_int num_patterns, struct cam_eb *bus);
286 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
288 struct cam_ed *device);
289 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
291 struct cam_periph *periph);
292 static xpt_busfunc_t xptedtbusfunc;
293 static xpt_targetfunc_t xptedttargetfunc;
294 static xpt_devicefunc_t xptedtdevicefunc;
295 static xpt_periphfunc_t xptedtperiphfunc;
296 static xpt_pdrvfunc_t xptplistpdrvfunc;
297 static xpt_periphfunc_t xptplistperiphfunc;
298 static int xptedtmatch(struct ccb_dev_match *cdm);
299 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
300 static int xptbustraverse(struct cam_eb *start_bus,
301 xpt_busfunc_t *tr_func, void *arg);
302 static int xpttargettraverse(struct cam_eb *bus,
303 struct cam_et *start_target,
304 xpt_targetfunc_t *tr_func, void *arg);
305 static int xptdevicetraverse(struct cam_et *target,
306 struct cam_ed *start_device,
307 xpt_devicefunc_t *tr_func, void *arg);
308 static int xptperiphtraverse(struct cam_ed *device,
309 struct cam_periph *start_periph,
310 xpt_periphfunc_t *tr_func, void *arg);
311 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
312 xpt_pdrvfunc_t *tr_func, void *arg);
313 static int xptpdperiphtraverse(struct periph_driver **pdrv,
314 struct cam_periph *start_periph,
315 xpt_periphfunc_t *tr_func,
317 static xpt_busfunc_t xptdefbusfunc;
318 static xpt_targetfunc_t xptdeftargetfunc;
319 static xpt_devicefunc_t xptdefdevicefunc;
320 static xpt_periphfunc_t xptdefperiphfunc;
321 static void xpt_finishconfig_task(void *context, int pending);
322 static void xpt_dev_async_default(u_int32_t async_code,
324 struct cam_et *target,
325 struct cam_ed *device,
327 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
328 struct cam_et *target,
330 static xpt_devicefunc_t xptsetasyncfunc;
331 static xpt_busfunc_t xptsetasyncbusfunc;
332 static cam_status xptregister(struct cam_periph *periph,
334 static __inline int device_is_queued(struct cam_ed *device);
337 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
341 mtx_assert(&devq->send_mtx, MA_OWNED);
342 if ((dev->ccbq.queue.entries > 0) &&
343 (dev->ccbq.dev_openings > 0) &&
344 (dev->ccbq.queue.qfrozen_cnt == 0)) {
346 * The priority of a device waiting for controller
347 * resources is that of the highest priority CCB
351 xpt_schedule_dev(&devq->send_queue,
353 CAMQ_GET_PRIO(&dev->ccbq.queue));
361 device_is_queued(struct cam_ed *device)
363 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
369 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
373 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
377 * Only allow read-write access.
379 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
383 * We don't allow nonblocking access.
385 if ((flags & O_NONBLOCK) != 0) {
386 printf("%s: can't do nonblocking access\n", devtoname(dev));
394 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
401 * Don't automatically grab the xpt softc lock here even though this is going
402 * through the xpt device. The xpt device is really just a back door for
403 * accessing other devices and SIMs, so the right thing to do is to grab
404 * the appropriate SIM lock once the bus/SIM is located.
407 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
411 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
412 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
418 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
426 * For the transport layer CAMIOCOMMAND ioctl, we really only want
427 * to accept CCB types that don't quite make sense to send through a
428 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
436 inccb = (union ccb *)addr;
437 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
438 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
439 inccb->csio.bio = NULL;
442 if (inccb->ccb_h.flags & CAM_UNLOCKED)
445 bus = xpt_find_bus(inccb->ccb_h.path_id);
449 switch (inccb->ccb_h.func_code) {
452 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
453 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
454 xpt_release_bus(bus);
459 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
460 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
461 xpt_release_bus(bus);
469 switch(inccb->ccb_h.func_code) {
477 ccb = xpt_alloc_ccb();
480 * Create a path using the bus, target, and lun the
483 if (xpt_create_path(&ccb->ccb_h.path, NULL,
484 inccb->ccb_h.path_id,
485 inccb->ccb_h.target_id,
486 inccb->ccb_h.target_lun) !=
492 /* Ensure all of our fields are correct */
493 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
494 inccb->ccb_h.pinfo.priority);
495 xpt_merge_ccb(ccb, inccb);
496 xpt_path_lock(ccb->ccb_h.path);
497 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
498 xpt_path_unlock(ccb->ccb_h.path);
499 bcopy(ccb, inccb, sizeof(union ccb));
500 xpt_free_path(ccb->ccb_h.path);
508 * This is an immediate CCB, so it's okay to
509 * allocate it on the stack.
513 * Create a path using the bus, target, and lun the
516 if (xpt_create_path(&ccb.ccb_h.path, NULL,
517 inccb->ccb_h.path_id,
518 inccb->ccb_h.target_id,
519 inccb->ccb_h.target_lun) !=
524 /* Ensure all of our fields are correct */
525 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
526 inccb->ccb_h.pinfo.priority);
527 xpt_merge_ccb(&ccb, inccb);
529 bcopy(&ccb, inccb, sizeof(union ccb));
530 xpt_free_path(ccb.ccb_h.path);
534 case XPT_DEV_MATCH: {
535 struct cam_periph_map_info mapinfo;
536 struct cam_path *old_path;
539 * We can't deal with physical addresses for this
540 * type of transaction.
542 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
549 * Save this in case the caller had it set to
550 * something in particular.
552 old_path = inccb->ccb_h.path;
555 * We really don't need a path for the matching
556 * code. The path is needed because of the
557 * debugging statements in xpt_action(). They
558 * assume that the CCB has a valid path.
560 inccb->ccb_h.path = xpt_periph->path;
562 bzero(&mapinfo, sizeof(mapinfo));
565 * Map the pattern and match buffers into kernel
566 * virtual address space.
568 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
571 inccb->ccb_h.path = old_path;
576 * This is an immediate CCB, we can send it on directly.
581 * Map the buffers back into user space.
583 cam_periph_unmapmem(inccb, &mapinfo);
585 inccb->ccb_h.path = old_path;
594 xpt_release_bus(bus);
598 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
599 * with the periphal driver name and unit name filled in. The other
600 * fields don't really matter as input. The passthrough driver name
601 * ("pass"), and unit number are passed back in the ccb. The current
602 * device generation number, and the index into the device peripheral
603 * driver list, and the status are also passed back. Note that
604 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
605 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
606 * (or rather should be) impossible for the device peripheral driver
607 * list to change since we look at the whole thing in one pass, and
608 * we do it with lock protection.
611 case CAMGETPASSTHRU: {
613 struct cam_periph *periph;
614 struct periph_driver **p_drv;
617 int base_periph_found;
619 ccb = (union ccb *)addr;
620 unit = ccb->cgdl.unit_number;
621 name = ccb->cgdl.periph_name;
622 base_periph_found = 0;
623 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
624 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
625 ccb->csio.bio = NULL;
629 * Sanity check -- make sure we don't get a null peripheral
632 if (*ccb->cgdl.periph_name == '\0') {
637 /* Keep the list from changing while we traverse it */
640 /* first find our driver in the list of drivers */
641 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
642 if (strcmp((*p_drv)->driver_name, name) == 0)
645 if (*p_drv == NULL) {
647 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
648 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
649 *ccb->cgdl.periph_name = '\0';
650 ccb->cgdl.unit_number = 0;
656 * Run through every peripheral instance of this driver
657 * and check to see whether it matches the unit passed
658 * in by the user. If it does, get out of the loops and
659 * find the passthrough driver associated with that
662 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
663 periph = TAILQ_NEXT(periph, unit_links)) {
665 if (periph->unit_number == unit)
669 * If we found the peripheral driver that the user passed
670 * in, go through all of the peripheral drivers for that
671 * particular device and look for a passthrough driver.
673 if (periph != NULL) {
674 struct cam_ed *device;
677 base_periph_found = 1;
678 device = periph->path->device;
679 for (i = 0, periph = SLIST_FIRST(&device->periphs);
681 periph = SLIST_NEXT(periph, periph_links), i++) {
683 * Check to see whether we have a
684 * passthrough device or not.
686 if (strcmp(periph->periph_name, "pass") == 0) {
688 * Fill in the getdevlist fields.
690 strlcpy(ccb->cgdl.periph_name,
692 sizeof(ccb->cgdl.periph_name));
693 ccb->cgdl.unit_number =
695 if (SLIST_NEXT(periph, periph_links))
697 CAM_GDEVLIST_MORE_DEVS;
700 CAM_GDEVLIST_LAST_DEVICE;
701 ccb->cgdl.generation =
705 * Fill in some CCB header fields
706 * that the user may want.
709 periph->path->bus->path_id;
710 ccb->ccb_h.target_id =
711 periph->path->target->target_id;
712 ccb->ccb_h.target_lun =
713 periph->path->device->lun_id;
714 ccb->ccb_h.status = CAM_REQ_CMP;
721 * If the periph is null here, one of two things has
722 * happened. The first possibility is that we couldn't
723 * find the unit number of the particular peripheral driver
724 * that the user is asking about. e.g. the user asks for
725 * the passthrough driver for "da11". We find the list of
726 * "da" peripherals all right, but there is no unit 11.
727 * The other possibility is that we went through the list
728 * of peripheral drivers attached to the device structure,
729 * but didn't find one with the name "pass". Either way,
730 * we return ENOENT, since we couldn't find something.
732 if (periph == NULL) {
733 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
734 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
735 *ccb->cgdl.periph_name = '\0';
736 ccb->cgdl.unit_number = 0;
739 * It is unfortunate that this is even necessary,
740 * but there are many, many clueless users out there.
741 * If this is true, the user is looking for the
742 * passthrough driver, but doesn't have one in his
745 if (base_periph_found == 1) {
746 printf("xptioctl: pass driver is not in the "
748 printf("xptioctl: put \"device pass\" in "
749 "your kernel config file\n");
764 cam_module_event_handler(module_t mod, int what, void *arg)
770 if ((error = xpt_init(NULL)) != 0)
782 static struct xpt_proto *
783 xpt_proto_find(cam_proto proto)
785 struct xpt_proto **pp;
787 SET_FOREACH(pp, cam_xpt_proto_set) {
788 if ((*pp)->proto == proto)
796 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
799 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
800 xpt_free_path(done_ccb->ccb_h.path);
801 xpt_free_ccb(done_ccb);
803 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
804 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
809 /* thread to handle bus rescans */
811 xpt_scanner_thread(void *dummy)
814 struct cam_path path;
818 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
819 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
821 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
822 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
826 * Since lock can be dropped inside and path freed
827 * by completion callback even before return here,
828 * take our own path copy for reference.
830 xpt_copy_path(&path, ccb->ccb_h.path);
831 xpt_path_lock(&path);
833 xpt_path_unlock(&path);
834 xpt_release_path(&path);
842 xpt_rescan(union ccb *ccb)
846 /* Prepare request */
847 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
848 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
849 ccb->ccb_h.func_code = XPT_SCAN_BUS;
850 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
851 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
852 ccb->ccb_h.func_code = XPT_SCAN_TGT;
853 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
854 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
855 ccb->ccb_h.func_code = XPT_SCAN_LUN;
857 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
858 xpt_free_path(ccb->ccb_h.path);
862 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
863 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
864 xpt_action_name(ccb->ccb_h.func_code)));
866 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
867 ccb->ccb_h.cbfcnp = xpt_rescan_done;
868 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
869 /* Don't make duplicate entries for the same paths. */
871 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
872 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
873 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
874 wakeup(&xsoftc.ccb_scanq);
876 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
877 xpt_free_path(ccb->ccb_h.path);
883 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
884 xsoftc.buses_to_config++;
885 wakeup(&xsoftc.ccb_scanq);
889 /* Functions accessed by the peripheral drivers */
891 xpt_init(void *dummy)
893 struct cam_sim *xpt_sim;
894 struct cam_path *path;
895 struct cam_devq *devq;
899 TAILQ_INIT(&xsoftc.xpt_busses);
900 TAILQ_INIT(&xsoftc.ccb_scanq);
901 STAILQ_INIT(&xsoftc.highpowerq);
902 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
904 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
905 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
906 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
907 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
909 #ifdef CAM_BOOT_DELAY
911 * Override this value at compile time to assist our users
912 * who don't use loader to boot a kernel.
914 xsoftc.boot_delay = CAM_BOOT_DELAY;
917 * The xpt layer is, itself, the equivalent of a SIM.
918 * Allow 16 ccbs in the ccb pool for it. This should
919 * give decent parallelism when we probe buses and
920 * perform other XPT functions.
922 devq = cam_simq_alloc(16);
923 xpt_sim = cam_sim_alloc(xptaction,
928 /*mtx*/&xsoftc.xpt_lock,
929 /*max_dev_transactions*/0,
930 /*max_tagged_dev_transactions*/0,
935 mtx_lock(&xsoftc.xpt_lock);
936 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
937 mtx_unlock(&xsoftc.xpt_lock);
938 printf("xpt_init: xpt_bus_register failed with status %#x,"
939 " failing attach\n", status);
942 mtx_unlock(&xsoftc.xpt_lock);
945 * Looking at the XPT from the SIM layer, the XPT is
946 * the equivalent of a peripheral driver. Allocate
947 * a peripheral driver entry for us.
949 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
951 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
952 printf("xpt_init: xpt_create_path failed with status %#x,"
953 " failing attach\n", status);
957 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
958 path, NULL, 0, xpt_sim);
959 xpt_path_unlock(path);
962 if (cam_num_doneqs < 1)
963 cam_num_doneqs = 1 + mp_ncpus / 6;
964 else if (cam_num_doneqs > MAXCPU)
965 cam_num_doneqs = MAXCPU;
966 for (i = 0; i < cam_num_doneqs; i++) {
967 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
969 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
970 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
971 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
977 if (cam_num_doneqs < 1) {
978 printf("xpt_init: Cannot init completion queues "
979 "- failing attach\n");
983 * Register a callback for when interrupts are enabled.
985 xsoftc.xpt_config_hook.ich_func = xpt_config;
986 if (config_intrhook_establish(&xsoftc.xpt_config_hook) != 0) {
987 printf("xpt_init: config_intrhook_establish failed "
988 "- failing attach\n");
995 xptregister(struct cam_periph *periph, void *arg)
997 struct cam_sim *xpt_sim;
999 if (periph == NULL) {
1000 printf("xptregister: periph was NULL!!\n");
1001 return(CAM_REQ_CMP_ERR);
1004 xpt_sim = (struct cam_sim *)arg;
1005 xpt_sim->softc = periph;
1006 xpt_periph = periph;
1007 periph->softc = NULL;
1009 return(CAM_REQ_CMP);
1013 xpt_add_periph(struct cam_periph *periph)
1015 struct cam_ed *device;
1018 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1019 device = periph->path->device;
1020 status = CAM_REQ_CMP;
1021 if (device != NULL) {
1022 mtx_lock(&device->target->bus->eb_mtx);
1023 device->generation++;
1024 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1025 mtx_unlock(&device->target->bus->eb_mtx);
1026 atomic_add_32(&xsoftc.xpt_generation, 1);
1033 xpt_remove_periph(struct cam_periph *periph)
1035 struct cam_ed *device;
1037 device = periph->path->device;
1038 if (device != NULL) {
1039 mtx_lock(&device->target->bus->eb_mtx);
1040 device->generation++;
1041 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1042 mtx_unlock(&device->target->bus->eb_mtx);
1043 atomic_add_32(&xsoftc.xpt_generation, 1);
1049 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1051 struct cam_path *path = periph->path;
1052 struct xpt_proto *proto;
1054 cam_periph_assert(periph, MA_OWNED);
1055 periph->flags |= CAM_PERIPH_ANNOUNCED;
1057 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1058 periph->periph_name, periph->unit_number,
1059 path->bus->sim->sim_name,
1060 path->bus->sim->unit_number,
1061 path->bus->sim->bus_id,
1063 path->target->target_id,
1064 (uintmax_t)path->device->lun_id);
1065 printf("%s%d: ", periph->periph_name, periph->unit_number);
1066 proto = xpt_proto_find(path->device->protocol);
1068 proto->ops->announce(path->device);
1070 printf("%s%d: Unknown protocol device %d\n",
1071 periph->periph_name, periph->unit_number,
1072 path->device->protocol);
1073 if (path->device->serial_num_len > 0) {
1074 /* Don't wrap the screen - print only the first 60 chars */
1075 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1076 periph->unit_number, path->device->serial_num);
1078 /* Announce transport details. */
1079 path->bus->xport->ops->announce(periph);
1080 /* Announce command queueing. */
1081 if (path->device->inq_flags & SID_CmdQue
1082 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1083 printf("%s%d: Command Queueing enabled\n",
1084 periph->periph_name, periph->unit_number);
1086 /* Announce caller's details if they've passed in. */
1087 if (announce_string != NULL)
1088 printf("%s%d: %s\n", periph->periph_name,
1089 periph->unit_number, announce_string);
1093 xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
1094 char *announce_string)
1096 struct cam_path *path = periph->path;
1097 struct xpt_proto *proto;
1099 cam_periph_assert(periph, MA_OWNED);
1100 periph->flags |= CAM_PERIPH_ANNOUNCED;
1102 /* Fall back to the non-sbuf method if necessary */
1103 if (xsoftc.announce_nosbuf != 0) {
1104 xpt_announce_periph(periph, announce_string);
1107 proto = xpt_proto_find(path->device->protocol);
1108 if (((proto != NULL) && (proto->ops->announce_sbuf == NULL)) ||
1109 (path->bus->xport->ops->announce_sbuf == NULL)) {
1110 xpt_announce_periph(periph, announce_string);
1114 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1115 periph->periph_name, periph->unit_number,
1116 path->bus->sim->sim_name,
1117 path->bus->sim->unit_number,
1118 path->bus->sim->bus_id,
1120 path->target->target_id,
1121 (uintmax_t)path->device->lun_id);
1122 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1125 proto->ops->announce_sbuf(path->device, sb);
1127 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1128 periph->periph_name, periph->unit_number,
1129 path->device->protocol);
1130 if (path->device->serial_num_len > 0) {
1131 /* Don't wrap the screen - print only the first 60 chars */
1132 sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
1133 periph->periph_name, periph->unit_number,
1134 path->device->serial_num);
1136 /* Announce transport details. */
1137 path->bus->xport->ops->announce_sbuf(periph, sb);
1138 /* Announce command queueing. */
1139 if (path->device->inq_flags & SID_CmdQue
1140 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1141 sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
1142 periph->periph_name, periph->unit_number);
1144 /* Announce caller's details if they've passed in. */
1145 if (announce_string != NULL)
1146 sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
1147 periph->unit_number, announce_string);
1151 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1154 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1155 periph->unit_number, quirks, bit_string);
1160 xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
1161 int quirks, char *bit_string)
1163 if (xsoftc.announce_nosbuf != 0) {
1164 xpt_announce_quirks(periph, quirks, bit_string);
1169 sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
1170 periph->unit_number, quirks, bit_string);
1175 xpt_denounce_periph(struct cam_periph *periph)
1177 struct cam_path *path = periph->path;
1178 struct xpt_proto *proto;
1180 cam_periph_assert(periph, MA_OWNED);
1181 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1182 periph->periph_name, periph->unit_number,
1183 path->bus->sim->sim_name,
1184 path->bus->sim->unit_number,
1185 path->bus->sim->bus_id,
1187 path->target->target_id,
1188 (uintmax_t)path->device->lun_id);
1189 printf("%s%d: ", periph->periph_name, periph->unit_number);
1190 proto = xpt_proto_find(path->device->protocol);
1192 proto->ops->denounce(path->device);
1194 printf("%s%d: Unknown protocol device %d\n",
1195 periph->periph_name, periph->unit_number,
1196 path->device->protocol);
1197 if (path->device->serial_num_len > 0)
1198 printf(" s/n %.60s", path->device->serial_num);
1199 printf(" detached\n");
1203 xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
1205 struct cam_path *path = periph->path;
1206 struct xpt_proto *proto;
1208 cam_periph_assert(periph, MA_OWNED);
1210 /* Fall back to the non-sbuf method if necessary */
1211 if (xsoftc.announce_nosbuf != 0) {
1212 xpt_denounce_periph(periph);
1215 proto = xpt_proto_find(path->device->protocol);
1216 if ((proto != NULL) && (proto->ops->denounce_sbuf == NULL)) {
1217 xpt_denounce_periph(periph);
1221 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1222 periph->periph_name, periph->unit_number,
1223 path->bus->sim->sim_name,
1224 path->bus->sim->unit_number,
1225 path->bus->sim->bus_id,
1227 path->target->target_id,
1228 (uintmax_t)path->device->lun_id);
1229 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1232 proto->ops->denounce_sbuf(path->device, sb);
1234 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1235 periph->periph_name, periph->unit_number,
1236 path->device->protocol);
1237 if (path->device->serial_num_len > 0)
1238 sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1239 sbuf_printf(sb, " detached\n");
1243 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1246 struct ccb_dev_advinfo cdai;
1247 struct scsi_vpd_id_descriptor *idd;
1249 xpt_path_assert(path, MA_OWNED);
1251 memset(&cdai, 0, sizeof(cdai));
1252 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1253 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1254 cdai.flags = CDAI_FLAG_NONE;
1257 if (!strcmp(attr, "GEOM::ident"))
1258 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1259 else if (!strcmp(attr, "GEOM::physpath"))
1260 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1261 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1262 strcmp(attr, "GEOM::lunname") == 0) {
1263 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1264 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1268 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1269 if (cdai.buf == NULL) {
1273 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1274 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1275 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1276 if (cdai.provsiz == 0)
1278 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1279 if (strcmp(attr, "GEOM::lunid") == 0) {
1280 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1281 cdai.provsiz, scsi_devid_is_lun_naa);
1283 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1284 cdai.provsiz, scsi_devid_is_lun_eui64);
1286 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1287 cdai.provsiz, scsi_devid_is_lun_uuid);
1289 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1290 cdai.provsiz, scsi_devid_is_lun_md5);
1294 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1295 cdai.provsiz, scsi_devid_is_lun_t10);
1297 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1298 cdai.provsiz, scsi_devid_is_lun_name);
1302 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1303 if (idd->length < len) {
1304 for (l = 0; l < idd->length; l++)
1305 buf[l] = idd->identifier[l] ?
1306 idd->identifier[l] : ' ';
1310 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1311 l = strnlen(idd->identifier, idd->length);
1313 bcopy(idd->identifier, buf, l);
1317 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1318 && idd->identifier[0] == 0x10) {
1319 if ((idd->length - 2) * 2 + 4 < len) {
1320 for (l = 2, o = 0; l < idd->length; l++) {
1321 if (l == 6 || l == 8 || l == 10 || l == 12)
1322 o += sprintf(buf + o, "-");
1323 o += sprintf(buf + o, "%02x",
1324 idd->identifier[l]);
1329 if (idd->length * 2 < len) {
1330 for (l = 0; l < idd->length; l++)
1331 sprintf(buf + l * 2, "%02x",
1332 idd->identifier[l]);
1338 if (strlcpy(buf, cdai.buf, len) >= len)
1343 if (cdai.buf != NULL)
1344 free(cdai.buf, M_CAMXPT);
1348 static dev_match_ret
1349 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1352 dev_match_ret retval;
1355 retval = DM_RET_NONE;
1358 * If we aren't given something to match against, that's an error.
1361 return(DM_RET_ERROR);
1364 * If there are no match entries, then this bus matches no
1367 if ((patterns == NULL) || (num_patterns == 0))
1368 return(DM_RET_DESCEND | DM_RET_COPY);
1370 for (i = 0; i < num_patterns; i++) {
1371 struct bus_match_pattern *cur_pattern;
1374 * If the pattern in question isn't for a bus node, we
1375 * aren't interested. However, we do indicate to the
1376 * calling routine that we should continue descending the
1377 * tree, since the user wants to match against lower-level
1380 if (patterns[i].type != DEV_MATCH_BUS) {
1381 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1382 retval |= DM_RET_DESCEND;
1386 cur_pattern = &patterns[i].pattern.bus_pattern;
1389 * If they want to match any bus node, we give them any
1392 if (cur_pattern->flags == BUS_MATCH_ANY) {
1393 /* set the copy flag */
1394 retval |= DM_RET_COPY;
1397 * If we've already decided on an action, go ahead
1400 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1405 * Not sure why someone would do this...
1407 if (cur_pattern->flags == BUS_MATCH_NONE)
1410 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1411 && (cur_pattern->path_id != bus->path_id))
1414 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1415 && (cur_pattern->bus_id != bus->sim->bus_id))
1418 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1419 && (cur_pattern->unit_number != bus->sim->unit_number))
1422 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1423 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1428 * If we get to this point, the user definitely wants
1429 * information on this bus. So tell the caller to copy the
1432 retval |= DM_RET_COPY;
1435 * If the return action has been set to descend, then we
1436 * know that we've already seen a non-bus matching
1437 * expression, therefore we need to further descend the tree.
1438 * This won't change by continuing around the loop, so we
1439 * go ahead and return. If we haven't seen a non-bus
1440 * matching expression, we keep going around the loop until
1441 * we exhaust the matching expressions. We'll set the stop
1442 * flag once we fall out of the loop.
1444 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1449 * If the return action hasn't been set to descend yet, that means
1450 * we haven't seen anything other than bus matching patterns. So
1451 * tell the caller to stop descending the tree -- the user doesn't
1452 * want to match against lower level tree elements.
1454 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1455 retval |= DM_RET_STOP;
1460 static dev_match_ret
1461 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1462 struct cam_ed *device)
1464 dev_match_ret retval;
1467 retval = DM_RET_NONE;
1470 * If we aren't given something to match against, that's an error.
1473 return(DM_RET_ERROR);
1476 * If there are no match entries, then this device matches no
1479 if ((patterns == NULL) || (num_patterns == 0))
1480 return(DM_RET_DESCEND | DM_RET_COPY);
1482 for (i = 0; i < num_patterns; i++) {
1483 struct device_match_pattern *cur_pattern;
1484 struct scsi_vpd_device_id *device_id_page;
1487 * If the pattern in question isn't for a device node, we
1488 * aren't interested.
1490 if (patterns[i].type != DEV_MATCH_DEVICE) {
1491 if ((patterns[i].type == DEV_MATCH_PERIPH)
1492 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1493 retval |= DM_RET_DESCEND;
1497 cur_pattern = &patterns[i].pattern.device_pattern;
1499 /* Error out if mutually exclusive options are specified. */
1500 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1501 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1502 return(DM_RET_ERROR);
1505 * If they want to match any device node, we give them any
1508 if (cur_pattern->flags == DEV_MATCH_ANY)
1512 * Not sure why someone would do this...
1514 if (cur_pattern->flags == DEV_MATCH_NONE)
1517 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1518 && (cur_pattern->path_id != device->target->bus->path_id))
1521 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1522 && (cur_pattern->target_id != device->target->target_id))
1525 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1526 && (cur_pattern->target_lun != device->lun_id))
1529 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1530 && (cam_quirkmatch((caddr_t)&device->inq_data,
1531 (caddr_t)&cur_pattern->data.inq_pat,
1532 1, sizeof(cur_pattern->data.inq_pat),
1533 scsi_static_inquiry_match) == NULL))
1536 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1537 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1538 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1539 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1540 device->device_id_len
1541 - SVPD_DEVICE_ID_HDR_LEN,
1542 cur_pattern->data.devid_pat.id,
1543 cur_pattern->data.devid_pat.id_len) != 0))
1548 * If we get to this point, the user definitely wants
1549 * information on this device. So tell the caller to copy
1552 retval |= DM_RET_COPY;
1555 * If the return action has been set to descend, then we
1556 * know that we've already seen a peripheral matching
1557 * expression, therefore we need to further descend the tree.
1558 * This won't change by continuing around the loop, so we
1559 * go ahead and return. If we haven't seen a peripheral
1560 * matching expression, we keep going around the loop until
1561 * we exhaust the matching expressions. We'll set the stop
1562 * flag once we fall out of the loop.
1564 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1569 * If the return action hasn't been set to descend yet, that means
1570 * we haven't seen any peripheral matching patterns. So tell the
1571 * caller to stop descending the tree -- the user doesn't want to
1572 * match against lower level tree elements.
1574 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1575 retval |= DM_RET_STOP;
1581 * Match a single peripheral against any number of match patterns.
1583 static dev_match_ret
1584 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1585 struct cam_periph *periph)
1587 dev_match_ret retval;
1591 * If we aren't given something to match against, that's an error.
1594 return(DM_RET_ERROR);
1597 * If there are no match entries, then this peripheral matches no
1600 if ((patterns == NULL) || (num_patterns == 0))
1601 return(DM_RET_STOP | DM_RET_COPY);
1604 * There aren't any nodes below a peripheral node, so there's no
1605 * reason to descend the tree any further.
1607 retval = DM_RET_STOP;
1609 for (i = 0; i < num_patterns; i++) {
1610 struct periph_match_pattern *cur_pattern;
1613 * If the pattern in question isn't for a peripheral, we
1614 * aren't interested.
1616 if (patterns[i].type != DEV_MATCH_PERIPH)
1619 cur_pattern = &patterns[i].pattern.periph_pattern;
1622 * If they want to match on anything, then we will do so.
1624 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1625 /* set the copy flag */
1626 retval |= DM_RET_COPY;
1629 * We've already set the return action to stop,
1630 * since there are no nodes below peripherals in
1637 * Not sure why someone would do this...
1639 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1642 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1643 && (cur_pattern->path_id != periph->path->bus->path_id))
1647 * For the target and lun id's, we have to make sure the
1648 * target and lun pointers aren't NULL. The xpt peripheral
1649 * has a wildcard target and device.
1651 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1652 && ((periph->path->target == NULL)
1653 ||(cur_pattern->target_id != periph->path->target->target_id)))
1656 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1657 && ((periph->path->device == NULL)
1658 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1661 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1662 && (cur_pattern->unit_number != periph->unit_number))
1665 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1666 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1671 * If we get to this point, the user definitely wants
1672 * information on this peripheral. So tell the caller to
1673 * copy the data out.
1675 retval |= DM_RET_COPY;
1678 * The return action has already been set to stop, since
1679 * peripherals don't have any nodes below them in the EDT.
1685 * If we get to this point, the peripheral that was passed in
1686 * doesn't match any of the patterns.
1692 xptedtbusfunc(struct cam_eb *bus, void *arg)
1694 struct ccb_dev_match *cdm;
1695 struct cam_et *target;
1696 dev_match_ret retval;
1698 cdm = (struct ccb_dev_match *)arg;
1701 * If our position is for something deeper in the tree, that means
1702 * that we've already seen this node. So, we keep going down.
1704 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1705 && (cdm->pos.cookie.bus == bus)
1706 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1707 && (cdm->pos.cookie.target != NULL))
1708 retval = DM_RET_DESCEND;
1710 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1713 * If we got an error, bail out of the search.
1715 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1716 cdm->status = CAM_DEV_MATCH_ERROR;
1721 * If the copy flag is set, copy this bus out.
1723 if (retval & DM_RET_COPY) {
1726 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1727 sizeof(struct dev_match_result));
1730 * If we don't have enough space to put in another
1731 * match result, save our position and tell the
1732 * user there are more devices to check.
1734 if (spaceleft < sizeof(struct dev_match_result)) {
1735 bzero(&cdm->pos, sizeof(cdm->pos));
1736 cdm->pos.position_type =
1737 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1739 cdm->pos.cookie.bus = bus;
1740 cdm->pos.generations[CAM_BUS_GENERATION]=
1741 xsoftc.bus_generation;
1742 cdm->status = CAM_DEV_MATCH_MORE;
1745 j = cdm->num_matches;
1747 cdm->matches[j].type = DEV_MATCH_BUS;
1748 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1749 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1750 cdm->matches[j].result.bus_result.unit_number =
1751 bus->sim->unit_number;
1752 strlcpy(cdm->matches[j].result.bus_result.dev_name,
1754 sizeof(cdm->matches[j].result.bus_result.dev_name));
1758 * If the user is only interested in buses, there's no
1759 * reason to descend to the next level in the tree.
1761 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1765 * If there is a target generation recorded, check it to
1766 * make sure the target list hasn't changed.
1768 mtx_lock(&bus->eb_mtx);
1769 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1770 && (cdm->pos.cookie.bus == bus)
1771 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1772 && (cdm->pos.cookie.target != NULL)) {
1773 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1775 mtx_unlock(&bus->eb_mtx);
1776 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1779 target = (struct cam_et *)cdm->pos.cookie.target;
1783 mtx_unlock(&bus->eb_mtx);
1785 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1789 xptedttargetfunc(struct cam_et *target, void *arg)
1791 struct ccb_dev_match *cdm;
1793 struct cam_ed *device;
1795 cdm = (struct ccb_dev_match *)arg;
1799 * If there is a device list generation recorded, check it to
1800 * make sure the device list hasn't changed.
1802 mtx_lock(&bus->eb_mtx);
1803 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1804 && (cdm->pos.cookie.bus == bus)
1805 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1806 && (cdm->pos.cookie.target == target)
1807 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1808 && (cdm->pos.cookie.device != NULL)) {
1809 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1810 target->generation) {
1811 mtx_unlock(&bus->eb_mtx);
1812 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1815 device = (struct cam_ed *)cdm->pos.cookie.device;
1819 mtx_unlock(&bus->eb_mtx);
1821 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1825 xptedtdevicefunc(struct cam_ed *device, void *arg)
1828 struct cam_periph *periph;
1829 struct ccb_dev_match *cdm;
1830 dev_match_ret retval;
1832 cdm = (struct ccb_dev_match *)arg;
1833 bus = device->target->bus;
1836 * If our position is for something deeper in the tree, that means
1837 * that we've already seen this node. So, we keep going down.
1839 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1840 && (cdm->pos.cookie.device == device)
1841 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1842 && (cdm->pos.cookie.periph != NULL))
1843 retval = DM_RET_DESCEND;
1845 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1848 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1849 cdm->status = CAM_DEV_MATCH_ERROR;
1854 * If the copy flag is set, copy this device out.
1856 if (retval & DM_RET_COPY) {
1859 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1860 sizeof(struct dev_match_result));
1863 * If we don't have enough space to put in another
1864 * match result, save our position and tell the
1865 * user there are more devices to check.
1867 if (spaceleft < sizeof(struct dev_match_result)) {
1868 bzero(&cdm->pos, sizeof(cdm->pos));
1869 cdm->pos.position_type =
1870 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1871 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1873 cdm->pos.cookie.bus = device->target->bus;
1874 cdm->pos.generations[CAM_BUS_GENERATION]=
1875 xsoftc.bus_generation;
1876 cdm->pos.cookie.target = device->target;
1877 cdm->pos.generations[CAM_TARGET_GENERATION] =
1878 device->target->bus->generation;
1879 cdm->pos.cookie.device = device;
1880 cdm->pos.generations[CAM_DEV_GENERATION] =
1881 device->target->generation;
1882 cdm->status = CAM_DEV_MATCH_MORE;
1885 j = cdm->num_matches;
1887 cdm->matches[j].type = DEV_MATCH_DEVICE;
1888 cdm->matches[j].result.device_result.path_id =
1889 device->target->bus->path_id;
1890 cdm->matches[j].result.device_result.target_id =
1891 device->target->target_id;
1892 cdm->matches[j].result.device_result.target_lun =
1894 cdm->matches[j].result.device_result.protocol =
1896 bcopy(&device->inq_data,
1897 &cdm->matches[j].result.device_result.inq_data,
1898 sizeof(struct scsi_inquiry_data));
1899 bcopy(&device->ident_data,
1900 &cdm->matches[j].result.device_result.ident_data,
1901 sizeof(struct ata_params));
1903 /* Let the user know whether this device is unconfigured */
1904 if (device->flags & CAM_DEV_UNCONFIGURED)
1905 cdm->matches[j].result.device_result.flags =
1906 DEV_RESULT_UNCONFIGURED;
1908 cdm->matches[j].result.device_result.flags =
1913 * If the user isn't interested in peripherals, don't descend
1914 * the tree any further.
1916 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1920 * If there is a peripheral list generation recorded, make sure
1921 * it hasn't changed.
1924 mtx_lock(&bus->eb_mtx);
1925 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1926 && (cdm->pos.cookie.bus == bus)
1927 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1928 && (cdm->pos.cookie.target == device->target)
1929 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1930 && (cdm->pos.cookie.device == device)
1931 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1932 && (cdm->pos.cookie.periph != NULL)) {
1933 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1934 device->generation) {
1935 mtx_unlock(&bus->eb_mtx);
1937 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1940 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1944 mtx_unlock(&bus->eb_mtx);
1947 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1951 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1953 struct ccb_dev_match *cdm;
1954 dev_match_ret retval;
1956 cdm = (struct ccb_dev_match *)arg;
1958 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1960 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1961 cdm->status = CAM_DEV_MATCH_ERROR;
1966 * If the copy flag is set, copy this peripheral out.
1968 if (retval & DM_RET_COPY) {
1972 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1973 sizeof(struct dev_match_result));
1976 * If we don't have enough space to put in another
1977 * match result, save our position and tell the
1978 * user there are more devices to check.
1980 if (spaceleft < sizeof(struct dev_match_result)) {
1981 bzero(&cdm->pos, sizeof(cdm->pos));
1982 cdm->pos.position_type =
1983 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1984 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1987 cdm->pos.cookie.bus = periph->path->bus;
1988 cdm->pos.generations[CAM_BUS_GENERATION]=
1989 xsoftc.bus_generation;
1990 cdm->pos.cookie.target = periph->path->target;
1991 cdm->pos.generations[CAM_TARGET_GENERATION] =
1992 periph->path->bus->generation;
1993 cdm->pos.cookie.device = periph->path->device;
1994 cdm->pos.generations[CAM_DEV_GENERATION] =
1995 periph->path->target->generation;
1996 cdm->pos.cookie.periph = periph;
1997 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1998 periph->path->device->generation;
1999 cdm->status = CAM_DEV_MATCH_MORE;
2003 j = cdm->num_matches;
2005 cdm->matches[j].type = DEV_MATCH_PERIPH;
2006 cdm->matches[j].result.periph_result.path_id =
2007 periph->path->bus->path_id;
2008 cdm->matches[j].result.periph_result.target_id =
2009 periph->path->target->target_id;
2010 cdm->matches[j].result.periph_result.target_lun =
2011 periph->path->device->lun_id;
2012 cdm->matches[j].result.periph_result.unit_number =
2013 periph->unit_number;
2014 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2015 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2016 periph->periph_name, l);
2023 xptedtmatch(struct ccb_dev_match *cdm)
2028 cdm->num_matches = 0;
2031 * Check the bus list generation. If it has changed, the user
2032 * needs to reset everything and start over.
2035 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2036 && (cdm->pos.cookie.bus != NULL)) {
2037 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
2038 xsoftc.bus_generation) {
2040 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2043 bus = (struct cam_eb *)cdm->pos.cookie.bus;
2049 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
2052 * If we get back 0, that means that we had to stop before fully
2053 * traversing the EDT. It also means that one of the subroutines
2054 * has set the status field to the proper value. If we get back 1,
2055 * we've fully traversed the EDT and copied out any matching entries.
2058 cdm->status = CAM_DEV_MATCH_LAST;
2064 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2066 struct cam_periph *periph;
2067 struct ccb_dev_match *cdm;
2069 cdm = (struct ccb_dev_match *)arg;
2072 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2073 && (cdm->pos.cookie.pdrv == pdrv)
2074 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2075 && (cdm->pos.cookie.periph != NULL)) {
2076 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2077 (*pdrv)->generation) {
2079 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2082 periph = (struct cam_periph *)cdm->pos.cookie.periph;
2088 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
2092 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2094 struct ccb_dev_match *cdm;
2095 dev_match_ret retval;
2097 cdm = (struct ccb_dev_match *)arg;
2099 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2101 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2102 cdm->status = CAM_DEV_MATCH_ERROR;
2107 * If the copy flag is set, copy this peripheral out.
2109 if (retval & DM_RET_COPY) {
2113 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2114 sizeof(struct dev_match_result));
2117 * If we don't have enough space to put in another
2118 * match result, save our position and tell the
2119 * user there are more devices to check.
2121 if (spaceleft < sizeof(struct dev_match_result)) {
2122 struct periph_driver **pdrv;
2125 bzero(&cdm->pos, sizeof(cdm->pos));
2126 cdm->pos.position_type =
2127 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2131 * This may look a bit non-sensical, but it is
2132 * actually quite logical. There are very few
2133 * peripheral drivers, and bloating every peripheral
2134 * structure with a pointer back to its parent
2135 * peripheral driver linker set entry would cost
2136 * more in the long run than doing this quick lookup.
2138 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2139 if (strcmp((*pdrv)->driver_name,
2140 periph->periph_name) == 0)
2144 if (*pdrv == NULL) {
2145 cdm->status = CAM_DEV_MATCH_ERROR;
2149 cdm->pos.cookie.pdrv = pdrv;
2151 * The periph generation slot does double duty, as
2152 * does the periph pointer slot. They are used for
2153 * both edt and pdrv lookups and positioning.
2155 cdm->pos.cookie.periph = periph;
2156 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2157 (*pdrv)->generation;
2158 cdm->status = CAM_DEV_MATCH_MORE;
2162 j = cdm->num_matches;
2164 cdm->matches[j].type = DEV_MATCH_PERIPH;
2165 cdm->matches[j].result.periph_result.path_id =
2166 periph->path->bus->path_id;
2169 * The transport layer peripheral doesn't have a target or
2172 if (periph->path->target)
2173 cdm->matches[j].result.periph_result.target_id =
2174 periph->path->target->target_id;
2176 cdm->matches[j].result.periph_result.target_id =
2177 CAM_TARGET_WILDCARD;
2179 if (periph->path->device)
2180 cdm->matches[j].result.periph_result.target_lun =
2181 periph->path->device->lun_id;
2183 cdm->matches[j].result.periph_result.target_lun =
2186 cdm->matches[j].result.periph_result.unit_number =
2187 periph->unit_number;
2188 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2189 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2190 periph->periph_name, l);
2197 xptperiphlistmatch(struct ccb_dev_match *cdm)
2201 cdm->num_matches = 0;
2204 * At this point in the edt traversal function, we check the bus
2205 * list generation to make sure that no buses have been added or
2206 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2207 * For the peripheral driver list traversal function, however, we
2208 * don't have to worry about new peripheral driver types coming or
2209 * going; they're in a linker set, and therefore can't change
2210 * without a recompile.
2213 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2214 && (cdm->pos.cookie.pdrv != NULL))
2215 ret = xptpdrvtraverse(
2216 (struct periph_driver **)cdm->pos.cookie.pdrv,
2217 xptplistpdrvfunc, cdm);
2219 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2222 * If we get back 0, that means that we had to stop before fully
2223 * traversing the peripheral driver tree. It also means that one of
2224 * the subroutines has set the status field to the proper value. If
2225 * we get back 1, we've fully traversed the EDT and copied out any
2229 cdm->status = CAM_DEV_MATCH_LAST;
2235 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2237 struct cam_eb *bus, *next_bus;
2245 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2253 for (; bus != NULL; bus = next_bus) {
2254 retval = tr_func(bus, arg);
2256 xpt_release_bus(bus);
2260 next_bus = TAILQ_NEXT(bus, links);
2262 next_bus->refcount++;
2264 xpt_release_bus(bus);
2270 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2271 xpt_targetfunc_t *tr_func, void *arg)
2273 struct cam_et *target, *next_target;
2278 target = start_target;
2280 mtx_lock(&bus->eb_mtx);
2281 target = TAILQ_FIRST(&bus->et_entries);
2282 if (target == NULL) {
2283 mtx_unlock(&bus->eb_mtx);
2287 mtx_unlock(&bus->eb_mtx);
2289 for (; target != NULL; target = next_target) {
2290 retval = tr_func(target, arg);
2292 xpt_release_target(target);
2295 mtx_lock(&bus->eb_mtx);
2296 next_target = TAILQ_NEXT(target, links);
2298 next_target->refcount++;
2299 mtx_unlock(&bus->eb_mtx);
2300 xpt_release_target(target);
2306 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2307 xpt_devicefunc_t *tr_func, void *arg)
2310 struct cam_ed *device, *next_device;
2316 device = start_device;
2318 mtx_lock(&bus->eb_mtx);
2319 device = TAILQ_FIRST(&target->ed_entries);
2320 if (device == NULL) {
2321 mtx_unlock(&bus->eb_mtx);
2325 mtx_unlock(&bus->eb_mtx);
2327 for (; device != NULL; device = next_device) {
2328 mtx_lock(&device->device_mtx);
2329 retval = tr_func(device, arg);
2330 mtx_unlock(&device->device_mtx);
2332 xpt_release_device(device);
2335 mtx_lock(&bus->eb_mtx);
2336 next_device = TAILQ_NEXT(device, links);
2338 next_device->refcount++;
2339 mtx_unlock(&bus->eb_mtx);
2340 xpt_release_device(device);
2346 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2347 xpt_periphfunc_t *tr_func, void *arg)
2350 struct cam_periph *periph, *next_periph;
2355 bus = device->target->bus;
2357 periph = start_periph;
2360 mtx_lock(&bus->eb_mtx);
2361 periph = SLIST_FIRST(&device->periphs);
2362 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2363 periph = SLIST_NEXT(periph, periph_links);
2364 if (periph == NULL) {
2365 mtx_unlock(&bus->eb_mtx);
2370 mtx_unlock(&bus->eb_mtx);
2373 for (; periph != NULL; periph = next_periph) {
2374 retval = tr_func(periph, arg);
2376 cam_periph_release_locked(periph);
2380 mtx_lock(&bus->eb_mtx);
2381 next_periph = SLIST_NEXT(periph, periph_links);
2382 while (next_periph != NULL &&
2383 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2384 next_periph = SLIST_NEXT(next_periph, periph_links);
2386 next_periph->refcount++;
2387 mtx_unlock(&bus->eb_mtx);
2389 cam_periph_release_locked(periph);
2395 xptpdrvtraverse(struct periph_driver **start_pdrv,
2396 xpt_pdrvfunc_t *tr_func, void *arg)
2398 struct periph_driver **pdrv;
2404 * We don't traverse the peripheral driver list like we do the
2405 * other lists, because it is a linker set, and therefore cannot be
2406 * changed during runtime. If the peripheral driver list is ever
2407 * re-done to be something other than a linker set (i.e. it can
2408 * change while the system is running), the list traversal should
2409 * be modified to work like the other traversal functions.
2411 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2412 *pdrv != NULL; pdrv++) {
2413 retval = tr_func(pdrv, arg);
2423 xptpdperiphtraverse(struct periph_driver **pdrv,
2424 struct cam_periph *start_periph,
2425 xpt_periphfunc_t *tr_func, void *arg)
2427 struct cam_periph *periph, *next_periph;
2433 periph = start_periph;
2436 periph = TAILQ_FIRST(&(*pdrv)->units);
2437 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2438 periph = TAILQ_NEXT(periph, unit_links);
2439 if (periph == NULL) {
2446 for (; periph != NULL; periph = next_periph) {
2447 cam_periph_lock(periph);
2448 retval = tr_func(periph, arg);
2449 cam_periph_unlock(periph);
2451 cam_periph_release(periph);
2455 next_periph = TAILQ_NEXT(periph, unit_links);
2456 while (next_periph != NULL &&
2457 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2458 next_periph = TAILQ_NEXT(next_periph, unit_links);
2460 next_periph->refcount++;
2462 cam_periph_release(periph);
2468 xptdefbusfunc(struct cam_eb *bus, void *arg)
2470 struct xpt_traverse_config *tr_config;
2472 tr_config = (struct xpt_traverse_config *)arg;
2474 if (tr_config->depth == XPT_DEPTH_BUS) {
2475 xpt_busfunc_t *tr_func;
2477 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2479 return(tr_func(bus, tr_config->tr_arg));
2481 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2485 xptdeftargetfunc(struct cam_et *target, void *arg)
2487 struct xpt_traverse_config *tr_config;
2489 tr_config = (struct xpt_traverse_config *)arg;
2491 if (tr_config->depth == XPT_DEPTH_TARGET) {
2492 xpt_targetfunc_t *tr_func;
2494 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2496 return(tr_func(target, tr_config->tr_arg));
2498 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2502 xptdefdevicefunc(struct cam_ed *device, void *arg)
2504 struct xpt_traverse_config *tr_config;
2506 tr_config = (struct xpt_traverse_config *)arg;
2508 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2509 xpt_devicefunc_t *tr_func;
2511 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2513 return(tr_func(device, tr_config->tr_arg));
2515 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2519 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2521 struct xpt_traverse_config *tr_config;
2522 xpt_periphfunc_t *tr_func;
2524 tr_config = (struct xpt_traverse_config *)arg;
2526 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2529 * Unlike the other default functions, we don't check for depth
2530 * here. The peripheral driver level is the last level in the EDT,
2531 * so if we're here, we should execute the function in question.
2533 return(tr_func(periph, tr_config->tr_arg));
2537 * Execute the given function for every bus in the EDT.
2540 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2542 struct xpt_traverse_config tr_config;
2544 tr_config.depth = XPT_DEPTH_BUS;
2545 tr_config.tr_func = tr_func;
2546 tr_config.tr_arg = arg;
2548 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2552 * Execute the given function for every device in the EDT.
2555 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2557 struct xpt_traverse_config tr_config;
2559 tr_config.depth = XPT_DEPTH_DEVICE;
2560 tr_config.tr_func = tr_func;
2561 tr_config.tr_arg = arg;
2563 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2567 xptsetasyncfunc(struct cam_ed *device, void *arg)
2569 struct cam_path path;
2570 struct ccb_getdev cgd;
2571 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2574 * Don't report unconfigured devices (Wildcard devs,
2575 * devices only for target mode, device instances
2576 * that have been invalidated but are waiting for
2577 * their last reference count to be released).
2579 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2582 xpt_compile_path(&path,
2584 device->target->bus->path_id,
2585 device->target->target_id,
2587 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2588 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2589 xpt_action((union ccb *)&cgd);
2590 csa->callback(csa->callback_arg,
2593 xpt_release_path(&path);
2599 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2601 struct cam_path path;
2602 struct ccb_pathinq cpi;
2603 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2605 xpt_compile_path(&path, /*periph*/NULL,
2607 CAM_TARGET_WILDCARD,
2609 xpt_path_lock(&path);
2610 xpt_path_inq(&cpi, &path);
2611 csa->callback(csa->callback_arg,
2614 xpt_path_unlock(&path);
2615 xpt_release_path(&path);
2621 xpt_action(union ccb *start_ccb)
2624 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2625 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2626 xpt_action_name(start_ccb->ccb_h.func_code)));
2628 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2629 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2633 xpt_action_default(union ccb *start_ccb)
2635 struct cam_path *path;
2636 struct cam_sim *sim;
2639 path = start_ccb->ccb_h.path;
2640 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2641 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2642 xpt_action_name(start_ccb->ccb_h.func_code)));
2644 switch (start_ccb->ccb_h.func_code) {
2647 struct cam_ed *device;
2650 * For the sake of compatibility with SCSI-1
2651 * devices that may not understand the identify
2652 * message, we include lun information in the
2653 * second byte of all commands. SCSI-1 specifies
2654 * that luns are a 3 bit value and reserves only 3
2655 * bits for lun information in the CDB. Later
2656 * revisions of the SCSI spec allow for more than 8
2657 * luns, but have deprecated lun information in the
2658 * CDB. So, if the lun won't fit, we must omit.
2660 * Also be aware that during initial probing for devices,
2661 * the inquiry information is unknown but initialized to 0.
2662 * This means that this code will be exercised while probing
2663 * devices with an ANSI revision greater than 2.
2665 device = path->device;
2666 if (device->protocol_version <= SCSI_REV_2
2667 && start_ccb->ccb_h.target_lun < 8
2668 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2670 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2671 start_ccb->ccb_h.target_lun << 5;
2673 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2677 case XPT_CONT_TARGET_IO:
2678 start_ccb->csio.sense_resid = 0;
2679 start_ccb->csio.resid = 0;
2682 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2683 start_ccb->ataio.resid = 0;
2687 case XPT_NVME_ADMIN:
2690 /* XXX just like nmve_io? */
2695 struct cam_devq *devq;
2697 devq = path->bus->sim->devq;
2698 mtx_lock(&devq->send_mtx);
2699 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2700 if (xpt_schedule_devq(devq, path->device) != 0)
2702 mtx_unlock(&devq->send_mtx);
2705 case XPT_CALC_GEOMETRY:
2706 /* Filter out garbage */
2707 if (start_ccb->ccg.block_size == 0
2708 || start_ccb->ccg.volume_size == 0) {
2709 start_ccb->ccg.cylinders = 0;
2710 start_ccb->ccg.heads = 0;
2711 start_ccb->ccg.secs_per_track = 0;
2712 start_ccb->ccb_h.status = CAM_REQ_CMP;
2715 #if defined(__sparc64__)
2717 * For sparc64, we may need adjust the geometry of large
2718 * disks in order to fit the limitations of the 16-bit
2719 * fields of the VTOC8 disk label.
2721 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2722 start_ccb->ccb_h.status = CAM_REQ_CMP;
2729 union ccb* abort_ccb;
2731 abort_ccb = start_ccb->cab.abort_ccb;
2732 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2733 struct cam_ed *device;
2734 struct cam_devq *devq;
2736 device = abort_ccb->ccb_h.path->device;
2737 devq = device->sim->devq;
2739 mtx_lock(&devq->send_mtx);
2740 if (abort_ccb->ccb_h.pinfo.index > 0) {
2741 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2742 abort_ccb->ccb_h.status =
2743 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2744 xpt_freeze_devq_device(device, 1);
2745 mtx_unlock(&devq->send_mtx);
2746 xpt_done(abort_ccb);
2747 start_ccb->ccb_h.status = CAM_REQ_CMP;
2750 mtx_unlock(&devq->send_mtx);
2752 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2753 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2755 * We've caught this ccb en route to
2756 * the SIM. Flag it for abort and the
2757 * SIM will do so just before starting
2758 * real work on the CCB.
2760 abort_ccb->ccb_h.status =
2761 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2762 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2763 start_ccb->ccb_h.status = CAM_REQ_CMP;
2767 if (XPT_FC_IS_QUEUED(abort_ccb)
2768 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2770 * It's already completed but waiting
2771 * for our SWI to get to it.
2773 start_ccb->ccb_h.status = CAM_UA_ABORT;
2777 * If we weren't able to take care of the abort request
2778 * in the XPT, pass the request down to the SIM for processing.
2782 case XPT_ACCEPT_TARGET_IO:
2784 case XPT_IMMED_NOTIFY:
2785 case XPT_NOTIFY_ACK:
2787 case XPT_IMMEDIATE_NOTIFY:
2788 case XPT_NOTIFY_ACKNOWLEDGE:
2789 case XPT_GET_SIM_KNOB_OLD:
2790 case XPT_GET_SIM_KNOB:
2791 case XPT_SET_SIM_KNOB:
2792 case XPT_GET_TRAN_SETTINGS:
2793 case XPT_SET_TRAN_SETTINGS:
2796 sim = path->bus->sim;
2798 if (mtx && !mtx_owned(mtx))
2803 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2804 ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2805 (*(sim->sim_action))(sim, start_ccb);
2806 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2807 ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2811 case XPT_PATH_STATS:
2812 start_ccb->cpis.last_reset = path->bus->last_reset;
2813 start_ccb->ccb_h.status = CAM_REQ_CMP;
2820 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2821 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2823 struct ccb_getdev *cgd;
2825 cgd = &start_ccb->cgd;
2826 cgd->protocol = dev->protocol;
2827 cgd->inq_data = dev->inq_data;
2828 cgd->ident_data = dev->ident_data;
2829 cgd->inq_flags = dev->inq_flags;
2830 cgd->ccb_h.status = CAM_REQ_CMP;
2831 cgd->serial_num_len = dev->serial_num_len;
2832 if ((dev->serial_num_len > 0)
2833 && (dev->serial_num != NULL))
2834 bcopy(dev->serial_num, cgd->serial_num,
2835 dev->serial_num_len);
2839 case XPT_GDEV_STATS:
2841 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2842 struct cam_ed *dev = path->device;
2843 struct cam_eb *bus = path->bus;
2844 struct cam_et *tar = path->target;
2845 struct cam_devq *devq = bus->sim->devq;
2847 mtx_lock(&devq->send_mtx);
2848 cgds->dev_openings = dev->ccbq.dev_openings;
2849 cgds->dev_active = dev->ccbq.dev_active;
2850 cgds->allocated = dev->ccbq.allocated;
2851 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2852 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2853 cgds->last_reset = tar->last_reset;
2854 cgds->maxtags = dev->maxtags;
2855 cgds->mintags = dev->mintags;
2856 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2857 cgds->last_reset = bus->last_reset;
2858 mtx_unlock(&devq->send_mtx);
2859 cgds->ccb_h.status = CAM_REQ_CMP;
2864 struct cam_periph *nperiph;
2865 struct periph_list *periph_head;
2866 struct ccb_getdevlist *cgdl;
2868 struct cam_ed *device;
2875 * Don't want anyone mucking with our data.
2877 device = path->device;
2878 periph_head = &device->periphs;
2879 cgdl = &start_ccb->cgdl;
2882 * Check and see if the list has changed since the user
2883 * last requested a list member. If so, tell them that the
2884 * list has changed, and therefore they need to start over
2885 * from the beginning.
2887 if ((cgdl->index != 0) &&
2888 (cgdl->generation != device->generation)) {
2889 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2894 * Traverse the list of peripherals and attempt to find
2895 * the requested peripheral.
2897 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2898 (nperiph != NULL) && (i <= cgdl->index);
2899 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2900 if (i == cgdl->index) {
2901 strlcpy(cgdl->periph_name,
2902 nperiph->periph_name,
2903 sizeof(cgdl->periph_name));
2904 cgdl->unit_number = nperiph->unit_number;
2909 cgdl->status = CAM_GDEVLIST_ERROR;
2913 if (nperiph == NULL)
2914 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2916 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2919 cgdl->generation = device->generation;
2921 cgdl->ccb_h.status = CAM_REQ_CMP;
2926 dev_pos_type position_type;
2927 struct ccb_dev_match *cdm;
2929 cdm = &start_ccb->cdm;
2932 * There are two ways of getting at information in the EDT.
2933 * The first way is via the primary EDT tree. It starts
2934 * with a list of buses, then a list of targets on a bus,
2935 * then devices/luns on a target, and then peripherals on a
2936 * device/lun. The "other" way is by the peripheral driver
2937 * lists. The peripheral driver lists are organized by
2938 * peripheral driver. (obviously) So it makes sense to
2939 * use the peripheral driver list if the user is looking
2940 * for something like "da1", or all "da" devices. If the
2941 * user is looking for something on a particular bus/target
2942 * or lun, it's generally better to go through the EDT tree.
2945 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2946 position_type = cdm->pos.position_type;
2950 position_type = CAM_DEV_POS_NONE;
2952 for (i = 0; i < cdm->num_patterns; i++) {
2953 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2954 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2955 position_type = CAM_DEV_POS_EDT;
2960 if (cdm->num_patterns == 0)
2961 position_type = CAM_DEV_POS_EDT;
2962 else if (position_type == CAM_DEV_POS_NONE)
2963 position_type = CAM_DEV_POS_PDRV;
2966 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2967 case CAM_DEV_POS_EDT:
2970 case CAM_DEV_POS_PDRV:
2971 xptperiphlistmatch(cdm);
2974 cdm->status = CAM_DEV_MATCH_ERROR;
2978 if (cdm->status == CAM_DEV_MATCH_ERROR)
2979 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2981 start_ccb->ccb_h.status = CAM_REQ_CMP;
2987 struct ccb_setasync *csa;
2988 struct async_node *cur_entry;
2989 struct async_list *async_head;
2992 csa = &start_ccb->csa;
2993 added = csa->event_enable;
2994 async_head = &path->device->asyncs;
2997 * If there is already an entry for us, simply
3000 cur_entry = SLIST_FIRST(async_head);
3001 while (cur_entry != NULL) {
3002 if ((cur_entry->callback_arg == csa->callback_arg)
3003 && (cur_entry->callback == csa->callback))
3005 cur_entry = SLIST_NEXT(cur_entry, links);
3008 if (cur_entry != NULL) {
3010 * If the request has no flags set,
3013 added &= ~cur_entry->event_enable;
3014 if (csa->event_enable == 0) {
3015 SLIST_REMOVE(async_head, cur_entry,
3017 xpt_release_device(path->device);
3018 free(cur_entry, M_CAMXPT);
3020 cur_entry->event_enable = csa->event_enable;
3022 csa->event_enable = added;
3024 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3026 if (cur_entry == NULL) {
3027 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3030 cur_entry->event_enable = csa->event_enable;
3031 cur_entry->event_lock = (path->bus->sim->mtx &&
3032 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
3033 cur_entry->callback_arg = csa->callback_arg;
3034 cur_entry->callback = csa->callback;
3035 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3036 xpt_acquire_device(path->device);
3038 start_ccb->ccb_h.status = CAM_REQ_CMP;
3043 struct ccb_relsim *crs;
3046 crs = &start_ccb->crs;
3050 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3054 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3056 /* Don't ever go below one opening */
3057 if (crs->openings > 0) {
3058 xpt_dev_ccbq_resize(path, crs->openings);
3061 "number of openings is now %d\n",
3067 mtx_lock(&dev->sim->devq->send_mtx);
3068 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3070 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3073 * Just extend the old timeout and decrement
3074 * the freeze count so that a single timeout
3075 * is sufficient for releasing the queue.
3077 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3078 callout_stop(&dev->callout);
3081 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3084 callout_reset_sbt(&dev->callout,
3085 SBT_1MS * crs->release_timeout, 0,
3086 xpt_release_devq_timeout, dev, 0);
3088 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3092 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3094 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3096 * Decrement the freeze count so that a single
3097 * completion is still sufficient to unfreeze
3100 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3103 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3104 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3108 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3110 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3111 || (dev->ccbq.dev_active == 0)) {
3113 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3116 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3117 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3120 mtx_unlock(&dev->sim->devq->send_mtx);
3122 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3123 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3124 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3125 start_ccb->ccb_h.status = CAM_REQ_CMP;
3129 struct cam_path *oldpath;
3131 /* Check that all request bits are supported. */
3132 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3133 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3137 cam_dflags = CAM_DEBUG_NONE;
3138 if (cam_dpath != NULL) {
3139 oldpath = cam_dpath;
3141 xpt_free_path(oldpath);
3143 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3144 if (xpt_create_path(&cam_dpath, NULL,
3145 start_ccb->ccb_h.path_id,
3146 start_ccb->ccb_h.target_id,
3147 start_ccb->ccb_h.target_lun) !=
3149 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3151 cam_dflags = start_ccb->cdbg.flags;
3152 start_ccb->ccb_h.status = CAM_REQ_CMP;
3153 xpt_print(cam_dpath, "debugging flags now %x\n",
3157 start_ccb->ccb_h.status = CAM_REQ_CMP;
3161 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3162 xpt_freeze_devq(path, 1);
3163 start_ccb->ccb_h.status = CAM_REQ_CMP;
3165 case XPT_REPROBE_LUN:
3166 xpt_async(AC_INQ_CHANGED, path, NULL);
3167 start_ccb->ccb_h.status = CAM_REQ_CMP;
3168 xpt_done(start_ccb);
3175 xpt_print(start_ccb->ccb_h.path,
3176 "%s: CCB type %#x %s not supported\n", __func__,
3177 start_ccb->ccb_h.func_code,
3178 xpt_action_name(start_ccb->ccb_h.func_code));
3179 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3180 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3181 xpt_done(start_ccb);
3185 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3186 ("xpt_action_default: func= %#x %s status %#x\n",
3187 start_ccb->ccb_h.func_code,
3188 xpt_action_name(start_ccb->ccb_h.func_code),
3189 start_ccb->ccb_h.status));
3193 * Call the sim poll routine to allow the sim to complete
3194 * any inflight requests, then call camisr_runqueue to
3195 * complete any CCB that the polling completed.
3198 xpt_sim_poll(struct cam_sim *sim)
3205 (*(sim->sim_poll))(sim);
3212 xpt_poll_setup(union ccb *start_ccb)
3215 struct cam_sim *sim;
3216 struct cam_devq *devq;
3219 timeout = start_ccb->ccb_h.timeout * 10;
3220 sim = start_ccb->ccb_h.path->bus->sim;
3222 dev = start_ccb->ccb_h.path->device;
3225 * Steal an opening so that no other queued requests
3226 * can get it before us while we simulate interrupts.
3228 mtx_lock(&devq->send_mtx);
3229 dev->ccbq.dev_openings--;
3230 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3232 mtx_unlock(&devq->send_mtx);
3235 mtx_lock(&devq->send_mtx);
3237 dev->ccbq.dev_openings++;
3238 mtx_unlock(&devq->send_mtx);
3244 xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3247 while (--timeout > 0) {
3248 xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
3249 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3257 * XXX Is it worth adding a sim_timeout entry
3258 * point so we can attempt recovery? If
3259 * this is only used for dumps, I don't think
3262 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3267 xpt_polled_action(union ccb *start_ccb)
3272 timeout = start_ccb->ccb_h.timeout * 10;
3273 dev = start_ccb->ccb_h.path->device;
3275 mtx_unlock(&dev->device_mtx);
3277 timeout = xpt_poll_setup(start_ccb);
3279 xpt_action(start_ccb);
3280 xpt_pollwait(start_ccb, timeout);
3282 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3285 mtx_lock(&dev->device_mtx);
3289 * Schedule a peripheral driver to receive a ccb when its
3290 * target device has space for more transactions.
3293 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3296 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3297 cam_periph_assert(periph, MA_OWNED);
3298 if (new_priority < periph->scheduled_priority) {
3299 periph->scheduled_priority = new_priority;
3300 xpt_run_allocq(periph, 0);
3306 * Schedule a device to run on a given queue.
3307 * If the device was inserted as a new entry on the queue,
3308 * return 1 meaning the device queue should be run. If we
3309 * were already queued, implying someone else has already
3310 * started the queue, return 0 so the caller doesn't attempt
3314 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3315 u_int32_t new_priority)
3318 u_int32_t old_priority;
3320 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3323 old_priority = pinfo->priority;
3326 * Are we already queued?
3328 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3329 /* Simply reorder based on new priority */
3330 if (new_priority < old_priority) {
3331 camq_change_priority(queue, pinfo->index,
3333 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3334 ("changed priority to %d\n",
3340 /* New entry on the queue */
3341 if (new_priority < old_priority)
3342 pinfo->priority = new_priority;
3344 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3345 ("Inserting onto queue\n"));
3346 pinfo->generation = ++queue->generation;
3347 camq_insert(queue, pinfo);
3354 xpt_run_allocq_task(void *context, int pending)
3356 struct cam_periph *periph = context;
3358 cam_periph_lock(periph);
3359 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3360 xpt_run_allocq(periph, 1);
3361 cam_periph_unlock(periph);
3362 cam_periph_release(periph);
3366 xpt_run_allocq(struct cam_periph *periph, int sleep)
3368 struct cam_ed *device;
3372 cam_periph_assert(periph, MA_OWNED);
3373 if (periph->periph_allocating)
3375 cam_periph_doacquire(periph);
3376 periph->periph_allocating = 1;
3377 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3378 device = periph->path->device;
3381 while ((prio = min(periph->scheduled_priority,
3382 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3383 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3384 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3387 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3389 ccb = xpt_get_ccb(periph);
3392 if (periph->flags & CAM_PERIPH_RUN_TASK)
3394 cam_periph_doacquire(periph);
3395 periph->flags |= CAM_PERIPH_RUN_TASK;
3396 taskqueue_enqueue(xsoftc.xpt_taskq,
3397 &periph->periph_run_task);
3400 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3401 if (prio == periph->immediate_priority) {
3402 periph->immediate_priority = CAM_PRIORITY_NONE;
3403 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3404 ("waking cam_periph_getccb()\n"));
3405 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3407 wakeup(&periph->ccb_list);
3409 periph->scheduled_priority = CAM_PRIORITY_NONE;
3410 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3411 ("calling periph_start()\n"));
3412 periph->periph_start(periph, ccb);
3417 xpt_release_ccb(ccb);
3418 periph->periph_allocating = 0;
3419 cam_periph_release_locked(periph);
3423 xpt_run_devq(struct cam_devq *devq)
3427 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3429 devq->send_queue.qfrozen_cnt++;
3430 while ((devq->send_queue.entries > 0)
3431 && (devq->send_openings > 0)
3432 && (devq->send_queue.qfrozen_cnt <= 1)) {
3433 struct cam_ed *device;
3434 union ccb *work_ccb;
3435 struct cam_sim *sim;
3436 struct xpt_proto *proto;
3438 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3440 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3441 ("running device %p\n", device));
3443 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3444 if (work_ccb == NULL) {
3445 printf("device on run queue with no ccbs???\n");
3449 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3451 mtx_lock(&xsoftc.xpt_highpower_lock);
3452 if (xsoftc.num_highpower <= 0) {
3454 * We got a high power command, but we
3455 * don't have any available slots. Freeze
3456 * the device queue until we have a slot
3459 xpt_freeze_devq_device(device, 1);
3460 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3463 mtx_unlock(&xsoftc.xpt_highpower_lock);
3467 * Consume a high power slot while
3470 xsoftc.num_highpower--;
3472 mtx_unlock(&xsoftc.xpt_highpower_lock);
3474 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3475 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3476 devq->send_openings--;
3477 devq->send_active++;
3478 xpt_schedule_devq(devq, device);
3479 mtx_unlock(&devq->send_mtx);
3481 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3483 * The client wants to freeze the queue
3484 * after this CCB is sent.
3486 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3489 /* In Target mode, the peripheral driver knows best... */
3490 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3491 if ((device->inq_flags & SID_CmdQue) != 0
3492 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3493 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3496 * Clear this in case of a retried CCB that
3497 * failed due to a rejected tag.
3499 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3502 KASSERT(device == work_ccb->ccb_h.path->device,
3503 ("device (%p) / path->device (%p) mismatch",
3504 device, work_ccb->ccb_h.path->device));
3505 proto = xpt_proto_find(device->protocol);
3506 if (proto && proto->ops->debug_out)
3507 proto->ops->debug_out(work_ccb);
3510 * Device queues can be shared among multiple SIM instances
3511 * that reside on different buses. Use the SIM from the
3512 * queued device, rather than the one from the calling bus.
3516 if (mtx && !mtx_owned(mtx))
3520 work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3521 (*(sim->sim_action))(sim, work_ccb);
3524 mtx_lock(&devq->send_mtx);
3526 devq->send_queue.qfrozen_cnt--;
3530 * This function merges stuff from the slave ccb into the master ccb, while
3531 * keeping important fields in the master ccb constant.
3534 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3538 * Pull fields that are valid for peripheral drivers to set
3539 * into the master CCB along with the CCB "payload".
3541 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3542 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3543 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3544 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3545 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3546 sizeof(union ccb) - sizeof(struct ccb_hdr));
3550 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3551 u_int32_t priority, u_int32_t flags)
3554 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3555 ccb_h->pinfo.priority = priority;
3557 ccb_h->path_id = path->bus->path_id;
3559 ccb_h->target_id = path->target->target_id;
3561 ccb_h->target_id = CAM_TARGET_WILDCARD;
3563 ccb_h->target_lun = path->device->lun_id;
3564 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3566 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3568 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3569 ccb_h->flags = flags;
3574 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3576 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3579 /* Path manipulation functions */
3581 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3582 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3584 struct cam_path *path;
3587 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3590 status = CAM_RESRC_UNAVAIL;
3593 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3594 if (status != CAM_REQ_CMP) {
3595 free(path, M_CAMPATH);
3598 *new_path_ptr = path;
3603 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3604 struct cam_periph *periph, path_id_t path_id,
3605 target_id_t target_id, lun_id_t lun_id)
3608 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3613 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3614 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3617 struct cam_et *target;
3618 struct cam_ed *device;
3621 status = CAM_REQ_CMP; /* Completed without error */
3622 target = NULL; /* Wildcarded */
3623 device = NULL; /* Wildcarded */
3626 * We will potentially modify the EDT, so block interrupts
3627 * that may attempt to create cam paths.
3629 bus = xpt_find_bus(path_id);
3631 status = CAM_PATH_INVALID;
3634 mtx_lock(&bus->eb_mtx);
3635 target = xpt_find_target(bus, target_id);
3636 if (target == NULL) {
3638 struct cam_et *new_target;
3640 new_target = xpt_alloc_target(bus, target_id);
3641 if (new_target == NULL) {
3642 status = CAM_RESRC_UNAVAIL;
3644 target = new_target;
3648 if (target != NULL) {
3649 device = xpt_find_device(target, lun_id);
3650 if (device == NULL) {
3652 struct cam_ed *new_device;
3655 (*(bus->xport->ops->alloc_device))(bus,
3658 if (new_device == NULL) {
3659 status = CAM_RESRC_UNAVAIL;
3661 device = new_device;
3665 mtx_unlock(&bus->eb_mtx);
3669 * Only touch the user's data if we are successful.
3671 if (status == CAM_REQ_CMP) {
3672 new_path->periph = perph;
3673 new_path->bus = bus;
3674 new_path->target = target;
3675 new_path->device = device;
3676 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3679 xpt_release_device(device);
3681 xpt_release_target(target);
3683 xpt_release_bus(bus);
3689 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3691 struct cam_path *new_path;
3693 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3694 if (new_path == NULL)
3695 return(CAM_RESRC_UNAVAIL);
3696 xpt_copy_path(new_path, path);
3697 *new_path_ptr = new_path;
3698 return (CAM_REQ_CMP);
3702 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3706 if (path->bus != NULL)
3707 xpt_acquire_bus(path->bus);
3708 if (path->target != NULL)
3709 xpt_acquire_target(path->target);
3710 if (path->device != NULL)
3711 xpt_acquire_device(path->device);
3715 xpt_release_path(struct cam_path *path)
3717 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3718 if (path->device != NULL) {
3719 xpt_release_device(path->device);
3720 path->device = NULL;
3722 if (path->target != NULL) {
3723 xpt_release_target(path->target);
3724 path->target = NULL;
3726 if (path->bus != NULL) {
3727 xpt_release_bus(path->bus);
3733 xpt_free_path(struct cam_path *path)
3736 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3737 xpt_release_path(path);
3738 free(path, M_CAMPATH);
3742 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3743 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3749 *bus_ref = path->bus->refcount;
3755 *periph_ref = path->periph->refcount;
3762 *target_ref = path->target->refcount;
3768 *device_ref = path->device->refcount;
3775 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3776 * in path1, 2 for match with wildcards in path2.
3779 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3783 if (path1->bus != path2->bus) {
3784 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3786 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3791 if (path1->target != path2->target) {
3792 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3795 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3800 if (path1->device != path2->device) {
3801 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3804 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3813 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3817 if (path->bus != dev->target->bus) {
3818 if (path->bus->path_id == CAM_BUS_WILDCARD)
3820 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3825 if (path->target != dev->target) {
3826 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3829 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3834 if (path->device != dev) {
3835 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3838 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3847 xpt_print_path(struct cam_path *path)
3850 char buffer[XPT_PRINT_LEN];
3852 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3853 xpt_path_sbuf(path, &sb);
3855 printf("%s", sbuf_data(&sb));
3860 xpt_print_device(struct cam_ed *device)
3864 printf("(nopath): ");
3866 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3867 device->sim->unit_number,
3868 device->sim->bus_id,
3869 device->target->target_id,
3870 (uintmax_t)device->lun_id);
3875 xpt_print(struct cam_path *path, const char *fmt, ...)
3879 char buffer[XPT_PRINT_LEN];
3881 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3883 xpt_path_sbuf(path, &sb);
3885 sbuf_vprintf(&sb, fmt, ap);
3889 printf("%s", sbuf_data(&sb));
3894 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3899 sbuf_new(&sb, str, str_len, 0);
3900 len = xpt_path_sbuf(path, &sb);
3906 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3910 sbuf_printf(sb, "(nopath): ");
3912 if (path->periph != NULL)
3913 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3914 path->periph->unit_number);
3916 sbuf_printf(sb, "(noperiph:");
3918 if (path->bus != NULL)
3919 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3920 path->bus->sim->unit_number,
3921 path->bus->sim->bus_id);
3923 sbuf_printf(sb, "nobus:");
3925 if (path->target != NULL)
3926 sbuf_printf(sb, "%d:", path->target->target_id);
3928 sbuf_printf(sb, "X:");
3930 if (path->device != NULL)
3931 sbuf_printf(sb, "%jx): ",
3932 (uintmax_t)path->device->lun_id);
3934 sbuf_printf(sb, "X): ");
3937 return(sbuf_len(sb));
3941 xpt_path_path_id(struct cam_path *path)
3943 return(path->bus->path_id);
3947 xpt_path_target_id(struct cam_path *path)
3949 if (path->target != NULL)
3950 return (path->target->target_id);
3952 return (CAM_TARGET_WILDCARD);
3956 xpt_path_lun_id(struct cam_path *path)
3958 if (path->device != NULL)
3959 return (path->device->lun_id);
3961 return (CAM_LUN_WILDCARD);
3965 xpt_path_sim(struct cam_path *path)
3968 return (path->bus->sim);
3972 xpt_path_periph(struct cam_path *path)
3975 return (path->periph);
3979 * Release a CAM control block for the caller. Remit the cost of the structure
3980 * to the device referenced by the path. If the this device had no 'credits'
3981 * and peripheral drivers have registered async callbacks for this notification
3985 xpt_release_ccb(union ccb *free_ccb)
3987 struct cam_ed *device;
3988 struct cam_periph *periph;
3990 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3991 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3992 device = free_ccb->ccb_h.path->device;
3993 periph = free_ccb->ccb_h.path->periph;
3995 xpt_free_ccb(free_ccb);
3996 periph->periph_allocated--;
3997 cam_ccbq_release_opening(&device->ccbq);
3998 xpt_run_allocq(periph, 0);
4001 /* Functions accessed by SIM drivers */
4003 static struct xpt_xport_ops xport_default_ops = {
4004 .alloc_device = xpt_alloc_device_default,
4005 .action = xpt_action_default,
4006 .async = xpt_dev_async_default,
4008 static struct xpt_xport xport_default = {
4009 .xport = XPORT_UNKNOWN,
4011 .ops = &xport_default_ops,
4014 CAM_XPT_XPORT(xport_default);
4017 * A sim structure, listing the SIM entry points and instance
4018 * identification info is passed to xpt_bus_register to hook the SIM
4019 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4020 * for this new bus and places it in the array of buses and assigns
4021 * it a path_id. The path_id may be influenced by "hard wiring"
4022 * information specified by the user. Once interrupt services are
4023 * available, the bus will be probed.
4026 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
4028 struct cam_eb *new_bus;
4029 struct cam_eb *old_bus;
4030 struct ccb_pathinq cpi;
4031 struct cam_path *path;
4035 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4036 M_CAMXPT, M_NOWAIT|M_ZERO);
4037 if (new_bus == NULL) {
4038 /* Couldn't satisfy request */
4039 return (CAM_RESRC_UNAVAIL);
4042 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4043 TAILQ_INIT(&new_bus->et_entries);
4046 timevalclear(&new_bus->last_reset);
4048 new_bus->refcount = 1; /* Held until a bus_deregister event */
4049 new_bus->generation = 0;
4052 sim->path_id = new_bus->path_id =
4053 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4054 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4055 while (old_bus != NULL
4056 && old_bus->path_id < new_bus->path_id)
4057 old_bus = TAILQ_NEXT(old_bus, links);
4058 if (old_bus != NULL)
4059 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4061 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4062 xsoftc.bus_generation++;
4066 * Set a default transport so that a PATH_INQ can be issued to
4067 * the SIM. This will then allow for probing and attaching of
4068 * a more appropriate transport.
4070 new_bus->xport = &xport_default;
4072 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
4073 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4074 if (status != CAM_REQ_CMP) {
4075 xpt_release_bus(new_bus);
4076 return (CAM_RESRC_UNAVAIL);
4079 xpt_path_inq(&cpi, path);
4081 if (cpi.ccb_h.status == CAM_REQ_CMP) {
4082 struct xpt_xport **xpt;
4084 SET_FOREACH(xpt, cam_xpt_xport_set) {
4085 if ((*xpt)->xport == cpi.transport) {
4086 new_bus->xport = *xpt;
4090 if (new_bus->xport == NULL) {
4092 "No transport found for %d\n", cpi.transport);
4093 xpt_release_bus(new_bus);
4094 free(path, M_CAMXPT);
4095 return (CAM_RESRC_UNAVAIL);
4099 /* Notify interested parties */
4100 if (sim->path_id != CAM_XPT_PATH_ID) {
4102 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4103 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4104 union ccb *scan_ccb;
4106 /* Initiate bus rescan. */
4107 scan_ccb = xpt_alloc_ccb_nowait();
4108 if (scan_ccb != NULL) {
4109 scan_ccb->ccb_h.path = path;
4110 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4111 scan_ccb->crcn.flags = 0;
4112 xpt_rescan(scan_ccb);
4115 "Can't allocate CCB to scan bus\n");
4116 xpt_free_path(path);
4119 xpt_free_path(path);
4121 xpt_free_path(path);
4122 return (CAM_SUCCESS);
4126 xpt_bus_deregister(path_id_t pathid)
4128 struct cam_path bus_path;
4131 status = xpt_compile_path(&bus_path, NULL, pathid,
4132 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4133 if (status != CAM_REQ_CMP)
4136 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4137 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4139 /* Release the reference count held while registered. */
4140 xpt_release_bus(bus_path.bus);
4141 xpt_release_path(&bus_path);
4143 return (CAM_REQ_CMP);
4147 xptnextfreepathid(void)
4153 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4155 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4157 /* Find an unoccupied pathid */
4158 while (bus != NULL && bus->path_id <= pathid) {
4159 if (bus->path_id == pathid)
4161 bus = TAILQ_NEXT(bus, links);
4165 * Ensure that this pathid is not reserved for
4166 * a bus that may be registered in the future.
4168 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4170 /* Start the search over */
4177 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4184 pathid = CAM_XPT_PATH_ID;
4185 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4186 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4189 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4190 if (strcmp(dname, "scbus")) {
4191 /* Avoid a bit of foot shooting. */
4194 if (dunit < 0) /* unwired?! */
4196 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4197 if (sim_bus == val) {
4201 } else if (sim_bus == 0) {
4202 /* Unspecified matches bus 0 */
4206 printf("Ambiguous scbus configuration for %s%d "
4207 "bus %d, cannot wire down. The kernel "
4208 "config entry for scbus%d should "
4209 "specify a controller bus.\n"
4210 "Scbus will be assigned dynamically.\n",
4211 sim_name, sim_unit, sim_bus, dunit);
4216 if (pathid == CAM_XPT_PATH_ID)
4217 pathid = xptnextfreepathid();
4222 xpt_async_string(u_int32_t async_code)
4225 switch (async_code) {
4226 case AC_BUS_RESET: return ("AC_BUS_RESET");
4227 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4228 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4229 case AC_SENT_BDR: return ("AC_SENT_BDR");
4230 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4231 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4232 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4233 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4234 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4235 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4236 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4237 case AC_CONTRACT: return ("AC_CONTRACT");
4238 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4239 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4241 return ("AC_UNKNOWN");
4245 xpt_async_size(u_int32_t async_code)
4248 switch (async_code) {
4249 case AC_BUS_RESET: return (0);
4250 case AC_UNSOL_RESEL: return (0);
4251 case AC_SCSI_AEN: return (0);
4252 case AC_SENT_BDR: return (0);
4253 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4254 case AC_PATH_DEREGISTERED: return (0);
4255 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4256 case AC_LOST_DEVICE: return (0);
4257 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4258 case AC_INQ_CHANGED: return (0);
4259 case AC_GETDEV_CHANGED: return (0);
4260 case AC_CONTRACT: return (sizeof(struct ac_contract));
4261 case AC_ADVINFO_CHANGED: return (-1);
4262 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4268 xpt_async_process_dev(struct cam_ed *device, void *arg)
4270 union ccb *ccb = arg;
4271 struct cam_path *path = ccb->ccb_h.path;
4272 void *async_arg = ccb->casync.async_arg_ptr;
4273 u_int32_t async_code = ccb->casync.async_code;
4276 if (path->device != device
4277 && path->device->lun_id != CAM_LUN_WILDCARD
4278 && device->lun_id != CAM_LUN_WILDCARD)
4282 * The async callback could free the device.
4283 * If it is a broadcast async, it doesn't hold
4284 * device reference, so take our own reference.
4286 xpt_acquire_device(device);
4289 * If async for specific device is to be delivered to
4290 * the wildcard client, take the specific device lock.
4291 * XXX: We may need a way for client to specify it.
4293 if ((device->lun_id == CAM_LUN_WILDCARD &&
4294 path->device->lun_id != CAM_LUN_WILDCARD) ||
4295 (device->target->target_id == CAM_TARGET_WILDCARD &&
4296 path->target->target_id != CAM_TARGET_WILDCARD) ||
4297 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4298 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4299 mtx_unlock(&device->device_mtx);
4300 xpt_path_lock(path);
4305 (*(device->target->bus->xport->ops->async))(async_code,
4306 device->target->bus, device->target, device, async_arg);
4307 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4310 xpt_path_unlock(path);
4311 mtx_lock(&device->device_mtx);
4313 xpt_release_device(device);
4318 xpt_async_process_tgt(struct cam_et *target, void *arg)
4320 union ccb *ccb = arg;
4321 struct cam_path *path = ccb->ccb_h.path;
4323 if (path->target != target
4324 && path->target->target_id != CAM_TARGET_WILDCARD
4325 && target->target_id != CAM_TARGET_WILDCARD)
4328 if (ccb->casync.async_code == AC_SENT_BDR) {
4329 /* Update our notion of when the last reset occurred */
4330 microtime(&target->last_reset);
4333 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4337 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4340 struct cam_path *path;
4342 u_int32_t async_code;
4344 path = ccb->ccb_h.path;
4345 async_code = ccb->casync.async_code;
4346 async_arg = ccb->casync.async_arg_ptr;
4347 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4348 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4351 if (async_code == AC_BUS_RESET) {
4352 /* Update our notion of when the last reset occurred */
4353 microtime(&bus->last_reset);
4356 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4359 * If this wasn't a fully wildcarded async, tell all
4360 * clients that want all async events.
4362 if (bus != xpt_periph->path->bus) {
4363 xpt_path_lock(xpt_periph->path);
4364 xpt_async_process_dev(xpt_periph->path->device, ccb);
4365 xpt_path_unlock(xpt_periph->path);
4368 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4369 xpt_release_devq(path, 1, TRUE);
4371 xpt_release_simq(path->bus->sim, TRUE);
4372 if (ccb->casync.async_arg_size > 0)
4373 free(async_arg, M_CAMXPT);
4374 xpt_free_path(path);
4379 xpt_async_bcast(struct async_list *async_head,
4380 u_int32_t async_code,
4381 struct cam_path *path, void *async_arg)
4383 struct async_node *cur_entry;
4386 cur_entry = SLIST_FIRST(async_head);
4387 while (cur_entry != NULL) {
4388 struct async_node *next_entry;
4390 * Grab the next list entry before we call the current
4391 * entry's callback. This is because the callback function
4392 * can delete its async callback entry.
4394 next_entry = SLIST_NEXT(cur_entry, links);
4395 if ((cur_entry->event_enable & async_code) != 0) {
4396 mtx = cur_entry->event_lock ?
4397 path->device->sim->mtx : NULL;
4400 cur_entry->callback(cur_entry->callback_arg,
4406 cur_entry = next_entry;
4411 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4416 ccb = xpt_alloc_ccb_nowait();
4418 xpt_print(path, "Can't allocate CCB to send %s\n",
4419 xpt_async_string(async_code));
4423 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4424 xpt_print(path, "Can't allocate path to send %s\n",
4425 xpt_async_string(async_code));
4429 ccb->ccb_h.path->periph = NULL;
4430 ccb->ccb_h.func_code = XPT_ASYNC;
4431 ccb->ccb_h.cbfcnp = xpt_async_process;
4432 ccb->ccb_h.flags |= CAM_UNLOCKED;
4433 ccb->casync.async_code = async_code;
4434 ccb->casync.async_arg_size = 0;
4435 size = xpt_async_size(async_code);
4436 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4437 ("xpt_async: func %#x %s aync_code %d %s\n",
4438 ccb->ccb_h.func_code,
4439 xpt_action_name(ccb->ccb_h.func_code),
4441 xpt_async_string(async_code)));
4442 if (size > 0 && async_arg != NULL) {
4443 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4444 if (ccb->casync.async_arg_ptr == NULL) {
4445 xpt_print(path, "Can't allocate argument to send %s\n",
4446 xpt_async_string(async_code));
4447 xpt_free_path(ccb->ccb_h.path);
4451 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4452 ccb->casync.async_arg_size = size;
4453 } else if (size < 0) {
4454 ccb->casync.async_arg_ptr = async_arg;
4455 ccb->casync.async_arg_size = size;
4457 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4458 xpt_freeze_devq(path, 1);
4460 xpt_freeze_simq(path->bus->sim, 1);
4465 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4466 struct cam_et *target, struct cam_ed *device,
4471 * We only need to handle events for real devices.
4473 if (target->target_id == CAM_TARGET_WILDCARD
4474 || device->lun_id == CAM_LUN_WILDCARD)
4477 printf("%s called\n", __func__);
4481 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4483 struct cam_devq *devq;
4486 devq = dev->sim->devq;
4487 mtx_assert(&devq->send_mtx, MA_OWNED);
4488 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4489 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4490 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4491 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4492 /* Remove frozen device from sendq. */
4493 if (device_is_queued(dev))
4494 camq_remove(&devq->send_queue, dev->devq_entry.index);
4499 xpt_freeze_devq(struct cam_path *path, u_int count)
4501 struct cam_ed *dev = path->device;
4502 struct cam_devq *devq;
4505 devq = dev->sim->devq;
4506 mtx_lock(&devq->send_mtx);
4507 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4508 freeze = xpt_freeze_devq_device(dev, count);
4509 mtx_unlock(&devq->send_mtx);
4514 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4516 struct cam_devq *devq;
4520 mtx_lock(&devq->send_mtx);
4521 freeze = (devq->send_queue.qfrozen_cnt += count);
4522 mtx_unlock(&devq->send_mtx);
4527 xpt_release_devq_timeout(void *arg)
4530 struct cam_devq *devq;
4532 dev = (struct cam_ed *)arg;
4533 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4534 devq = dev->sim->devq;
4535 mtx_assert(&devq->send_mtx, MA_OWNED);
4536 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4541 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4544 struct cam_devq *devq;
4546 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4549 devq = dev->sim->devq;
4550 mtx_lock(&devq->send_mtx);
4551 if (xpt_release_devq_device(dev, count, run_queue))
4552 xpt_run_devq(dev->sim->devq);
4553 mtx_unlock(&devq->send_mtx);
4557 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4560 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4561 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4562 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4563 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4564 if (count > dev->ccbq.queue.qfrozen_cnt) {
4566 printf("xpt_release_devq(): requested %u > present %u\n",
4567 count, dev->ccbq.queue.qfrozen_cnt);
4569 count = dev->ccbq.queue.qfrozen_cnt;
4571 dev->ccbq.queue.qfrozen_cnt -= count;
4572 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4574 * No longer need to wait for a successful
4575 * command completion.
4577 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4579 * Remove any timeouts that might be scheduled
4580 * to release this queue.
4582 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4583 callout_stop(&dev->callout);
4584 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4587 * Now that we are unfrozen schedule the
4588 * device so any pending transactions are
4591 xpt_schedule_devq(dev->sim->devq, dev);
4598 xpt_release_simq(struct cam_sim *sim, int run_queue)
4600 struct cam_devq *devq;
4603 mtx_lock(&devq->send_mtx);
4604 if (devq->send_queue.qfrozen_cnt <= 0) {
4606 printf("xpt_release_simq: requested 1 > present %u\n",
4607 devq->send_queue.qfrozen_cnt);
4610 devq->send_queue.qfrozen_cnt--;
4611 if (devq->send_queue.qfrozen_cnt == 0) {
4613 * If there is a timeout scheduled to release this
4614 * sim queue, remove it. The queue frozen count is
4617 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4618 callout_stop(&sim->callout);
4619 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4623 * Now that we are unfrozen run the send queue.
4625 xpt_run_devq(sim->devq);
4628 mtx_unlock(&devq->send_mtx);
4632 * XXX Appears to be unused.
4635 xpt_release_simq_timeout(void *arg)
4637 struct cam_sim *sim;
4639 sim = (struct cam_sim *)arg;
4640 xpt_release_simq(sim, /* run_queue */ TRUE);
4644 xpt_done(union ccb *done_ccb)
4646 struct cam_doneq *queue;
4649 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4650 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4651 done_ccb->csio.bio != NULL)
4652 biotrack(done_ccb->csio.bio, __func__);
4655 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4656 ("xpt_done: func= %#x %s status %#x\n",
4657 done_ccb->ccb_h.func_code,
4658 xpt_action_name(done_ccb->ccb_h.func_code),
4659 done_ccb->ccb_h.status));
4660 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4663 /* Store the time the ccb was in the sim */
4664 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4665 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4666 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4667 queue = &cam_doneqs[hash];
4668 mtx_lock(&queue->cam_doneq_mtx);
4669 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4670 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4671 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4672 mtx_unlock(&queue->cam_doneq_mtx);
4674 wakeup(&queue->cam_doneq);
4678 xpt_done_direct(union ccb *done_ccb)
4681 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4682 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4683 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4686 /* Store the time the ccb was in the sim */
4687 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4688 xpt_done_process(&done_ccb->ccb_h);
4696 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4701 xpt_alloc_ccb_nowait()
4705 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4710 xpt_free_ccb(union ccb *free_ccb)
4712 free(free_ccb, M_CAMCCB);
4717 /* Private XPT functions */
4720 * Get a CAM control block for the caller. Charge the structure to the device
4721 * referenced by the path. If we don't have sufficient resources to allocate
4722 * more ccbs, we return NULL.
4725 xpt_get_ccb_nowait(struct cam_periph *periph)
4729 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4730 if (new_ccb == NULL)
4732 periph->periph_allocated++;
4733 cam_ccbq_take_opening(&periph->path->device->ccbq);
4738 xpt_get_ccb(struct cam_periph *periph)
4742 cam_periph_unlock(periph);
4743 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4744 cam_periph_lock(periph);
4745 periph->periph_allocated++;
4746 cam_ccbq_take_opening(&periph->path->device->ccbq);
4751 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4753 struct ccb_hdr *ccb_h;
4755 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4756 cam_periph_assert(periph, MA_OWNED);
4757 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4758 ccb_h->pinfo.priority != priority) {
4759 if (priority < periph->immediate_priority) {
4760 periph->immediate_priority = priority;
4761 xpt_run_allocq(periph, 0);
4763 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4766 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4767 return ((union ccb *)ccb_h);
4771 xpt_acquire_bus(struct cam_eb *bus)
4780 xpt_release_bus(struct cam_eb *bus)
4784 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4785 if (--bus->refcount > 0) {
4789 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4790 xsoftc.bus_generation++;
4792 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4793 ("destroying bus, but target list is not empty"));
4794 cam_sim_release(bus->sim);
4795 mtx_destroy(&bus->eb_mtx);
4796 free(bus, M_CAMXPT);
4799 static struct cam_et *
4800 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4802 struct cam_et *cur_target, *target;
4804 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4805 mtx_assert(&bus->eb_mtx, MA_OWNED);
4806 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4811 TAILQ_INIT(&target->ed_entries);
4813 target->target_id = target_id;
4814 target->refcount = 1;
4815 target->generation = 0;
4816 target->luns = NULL;
4817 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4818 timevalclear(&target->last_reset);
4820 * Hold a reference to our parent bus so it
4821 * will not go away before we do.
4825 /* Insertion sort into our bus's target list */
4826 cur_target = TAILQ_FIRST(&bus->et_entries);
4827 while (cur_target != NULL && cur_target->target_id < target_id)
4828 cur_target = TAILQ_NEXT(cur_target, links);
4829 if (cur_target != NULL) {
4830 TAILQ_INSERT_BEFORE(cur_target, target, links);
4832 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4839 xpt_acquire_target(struct cam_et *target)
4841 struct cam_eb *bus = target->bus;
4843 mtx_lock(&bus->eb_mtx);
4845 mtx_unlock(&bus->eb_mtx);
4849 xpt_release_target(struct cam_et *target)
4851 struct cam_eb *bus = target->bus;
4853 mtx_lock(&bus->eb_mtx);
4854 if (--target->refcount > 0) {
4855 mtx_unlock(&bus->eb_mtx);
4858 TAILQ_REMOVE(&bus->et_entries, target, links);
4860 mtx_unlock(&bus->eb_mtx);
4861 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4862 ("destroying target, but device list is not empty"));
4863 xpt_release_bus(bus);
4864 mtx_destroy(&target->luns_mtx);
4866 free(target->luns, M_CAMXPT);
4867 free(target, M_CAMXPT);
4870 static struct cam_ed *
4871 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4874 struct cam_ed *device;
4876 device = xpt_alloc_device(bus, target, lun_id);
4880 device->mintags = 1;
4881 device->maxtags = 1;
4886 xpt_destroy_device(void *context, int pending)
4888 struct cam_ed *device = context;
4890 mtx_lock(&device->device_mtx);
4891 mtx_destroy(&device->device_mtx);
4892 free(device, M_CAMDEV);
4896 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4898 struct cam_ed *cur_device, *device;
4899 struct cam_devq *devq;
4902 mtx_assert(&bus->eb_mtx, MA_OWNED);
4903 /* Make space for us in the device queue on our bus */
4904 devq = bus->sim->devq;
4905 mtx_lock(&devq->send_mtx);
4906 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4907 mtx_unlock(&devq->send_mtx);
4908 if (status != CAM_REQ_CMP)
4911 device = (struct cam_ed *)malloc(sizeof(*device),
4912 M_CAMDEV, M_NOWAIT|M_ZERO);
4916 cam_init_pinfo(&device->devq_entry);
4917 device->target = target;
4918 device->lun_id = lun_id;
4919 device->sim = bus->sim;
4920 if (cam_ccbq_init(&device->ccbq,
4921 bus->sim->max_dev_openings) != 0) {
4922 free(device, M_CAMDEV);
4925 SLIST_INIT(&device->asyncs);
4926 SLIST_INIT(&device->periphs);
4927 device->generation = 0;
4928 device->flags = CAM_DEV_UNCONFIGURED;
4929 device->tag_delay_count = 0;
4930 device->tag_saved_openings = 0;
4931 device->refcount = 1;
4932 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4933 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4934 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4936 * Hold a reference to our parent bus so it
4937 * will not go away before we do.
4941 cur_device = TAILQ_FIRST(&target->ed_entries);
4942 while (cur_device != NULL && cur_device->lun_id < lun_id)
4943 cur_device = TAILQ_NEXT(cur_device, links);
4944 if (cur_device != NULL)
4945 TAILQ_INSERT_BEFORE(cur_device, device, links);
4947 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4948 target->generation++;
4953 xpt_acquire_device(struct cam_ed *device)
4955 struct cam_eb *bus = device->target->bus;
4957 mtx_lock(&bus->eb_mtx);
4959 mtx_unlock(&bus->eb_mtx);
4963 xpt_release_device(struct cam_ed *device)
4965 struct cam_eb *bus = device->target->bus;
4966 struct cam_devq *devq;
4968 mtx_lock(&bus->eb_mtx);
4969 if (--device->refcount > 0) {
4970 mtx_unlock(&bus->eb_mtx);
4974 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4975 device->target->generation++;
4976 mtx_unlock(&bus->eb_mtx);
4978 /* Release our slot in the devq */
4979 devq = bus->sim->devq;
4980 mtx_lock(&devq->send_mtx);
4981 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4982 mtx_unlock(&devq->send_mtx);
4984 KASSERT(SLIST_EMPTY(&device->periphs),
4985 ("destroying device, but periphs list is not empty"));
4986 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4987 ("destroying device while still queued for ccbs"));
4989 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4990 callout_stop(&device->callout);
4992 xpt_release_target(device->target);
4994 cam_ccbq_fini(&device->ccbq);
4996 * Free allocated memory. free(9) does nothing if the
4997 * supplied pointer is NULL, so it is safe to call without
5000 free(device->supported_vpds, M_CAMXPT);
5001 free(device->device_id, M_CAMXPT);
5002 free(device->ext_inq, M_CAMXPT);
5003 free(device->physpath, M_CAMXPT);
5004 free(device->rcap_buf, M_CAMXPT);
5005 free(device->serial_num, M_CAMXPT);
5006 free(device->nvme_data, M_CAMXPT);
5007 free(device->nvme_cdata, M_CAMXPT);
5008 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
5012 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5018 mtx_lock(&dev->sim->devq->send_mtx);
5019 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5020 mtx_unlock(&dev->sim->devq->send_mtx);
5021 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5022 || (dev->inq_flags & SID_CmdQue) != 0)
5023 dev->tag_saved_openings = newopenings;
5027 static struct cam_eb *
5028 xpt_find_bus(path_id_t path_id)
5033 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5035 bus = TAILQ_NEXT(bus, links)) {
5036 if (bus->path_id == path_id) {
5045 static struct cam_et *
5046 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5048 struct cam_et *target;
5050 mtx_assert(&bus->eb_mtx, MA_OWNED);
5051 for (target = TAILQ_FIRST(&bus->et_entries);
5053 target = TAILQ_NEXT(target, links)) {
5054 if (target->target_id == target_id) {
5062 static struct cam_ed *
5063 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5065 struct cam_ed *device;
5067 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
5068 for (device = TAILQ_FIRST(&target->ed_entries);
5070 device = TAILQ_NEXT(device, links)) {
5071 if (device->lun_id == lun_id) {
5080 xpt_start_tags(struct cam_path *path)
5082 struct ccb_relsim crs;
5083 struct cam_ed *device;
5084 struct cam_sim *sim;
5087 device = path->device;
5088 sim = path->bus->sim;
5089 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5090 xpt_freeze_devq(path, /*count*/1);
5091 device->inq_flags |= SID_CmdQue;
5092 if (device->tag_saved_openings != 0)
5093 newopenings = device->tag_saved_openings;
5095 newopenings = min(device->maxtags,
5096 sim->max_tagged_dev_openings);
5097 xpt_dev_ccbq_resize(path, newopenings);
5098 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5099 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5100 crs.ccb_h.func_code = XPT_REL_SIMQ;
5101 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5103 = crs.release_timeout
5106 xpt_action((union ccb *)&crs);
5110 xpt_stop_tags(struct cam_path *path)
5112 struct ccb_relsim crs;
5113 struct cam_ed *device;
5114 struct cam_sim *sim;
5116 device = path->device;
5117 sim = path->bus->sim;
5118 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5119 device->tag_delay_count = 0;
5120 xpt_freeze_devq(path, /*count*/1);
5121 device->inq_flags &= ~SID_CmdQue;
5122 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5123 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5124 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5125 crs.ccb_h.func_code = XPT_REL_SIMQ;
5126 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5128 = crs.release_timeout
5131 xpt_action((union ccb *)&crs);
5135 xpt_boot_delay(void *arg)
5142 xpt_config(void *arg)
5145 * Now that interrupts are enabled, go find our devices
5147 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5148 printf("xpt_config: failed to create taskqueue thread.\n");
5150 /* Setup debugging path */
5151 if (cam_dflags != CAM_DEBUG_NONE) {
5152 if (xpt_create_path(&cam_dpath, NULL,
5153 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5154 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5155 printf("xpt_config: xpt_create_path() failed for debug"
5156 " target %d:%d:%d, debugging disabled\n",
5157 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5158 cam_dflags = CAM_DEBUG_NONE;
5163 periphdriver_init(1);
5165 callout_init(&xsoftc.boot_callout, 1);
5166 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5167 xpt_boot_delay, NULL, 0);
5168 /* Fire up rescan thread. */
5169 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5170 "cam", "scanner")) {
5171 printf("xpt_config: failed to create rescan thread.\n");
5179 xsoftc.buses_to_config++;
5184 xpt_release_boot(void)
5187 xsoftc.buses_to_config--;
5188 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5189 struct xpt_task *task;
5191 xsoftc.buses_config_done = 1;
5193 /* Call manually because we don't have any buses */
5194 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5196 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5197 taskqueue_enqueue(taskqueue_thread, &task->task);
5204 * If the given device only has one peripheral attached to it, and if that
5205 * peripheral is the passthrough driver, announce it. This insures that the
5206 * user sees some sort of announcement for every peripheral in their system.
5209 xptpassannouncefunc(struct cam_ed *device, void *arg)
5211 struct cam_periph *periph;
5214 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5215 periph = SLIST_NEXT(periph, periph_links), i++);
5217 periph = SLIST_FIRST(&device->periphs);
5219 && (strncmp(periph->periph_name, "pass", 4) == 0))
5220 xpt_announce_periph(periph, NULL);
5226 xpt_finishconfig_task(void *context, int pending)
5229 periphdriver_init(2);
5231 * Check for devices with no "standard" peripheral driver
5232 * attached. For any devices like that, announce the
5233 * passthrough driver so the user will see something.
5236 xpt_for_all_devices(xptpassannouncefunc, NULL);
5238 /* Release our hook so that the boot can continue. */
5239 config_intrhook_disestablish(&xsoftc.xpt_config_hook);
5241 free(context, M_CAMXPT);
5245 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5246 struct cam_path *path)
5248 struct ccb_setasync csa;
5253 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5254 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5255 if (status != CAM_REQ_CMP)
5257 xpt_path_lock(path);
5261 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5262 csa.ccb_h.func_code = XPT_SASYNC_CB;
5263 csa.event_enable = event;
5264 csa.callback = cbfunc;
5265 csa.callback_arg = cbarg;
5266 xpt_action((union ccb *)&csa);
5267 status = csa.ccb_h.status;
5269 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5270 ("xpt_register_async: func %p\n", cbfunc));
5273 xpt_path_unlock(path);
5274 xpt_free_path(path);
5277 if ((status == CAM_REQ_CMP) &&
5278 (csa.event_enable & AC_FOUND_DEVICE)) {
5280 * Get this peripheral up to date with all
5281 * the currently existing devices.
5283 xpt_for_all_devices(xptsetasyncfunc, &csa);
5285 if ((status == CAM_REQ_CMP) &&
5286 (csa.event_enable & AC_PATH_REGISTERED)) {
5288 * Get this peripheral up to date with all
5289 * the currently existing buses.
5291 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5298 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5300 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5302 switch (work_ccb->ccb_h.func_code) {
5303 /* Common cases first */
5304 case XPT_PATH_INQ: /* Path routing inquiry */
5306 struct ccb_pathinq *cpi;
5308 cpi = &work_ccb->cpi;
5309 cpi->version_num = 1; /* XXX??? */
5310 cpi->hba_inquiry = 0;
5311 cpi->target_sprt = 0;
5313 cpi->hba_eng_cnt = 0;
5314 cpi->max_target = 0;
5316 cpi->initiator_id = 0;
5317 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5318 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5319 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5320 cpi->unit_number = sim->unit_number;
5321 cpi->bus_id = sim->bus_id;
5322 cpi->base_transfer_speed = 0;
5323 cpi->protocol = PROTO_UNSPECIFIED;
5324 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5325 cpi->transport = XPORT_UNSPECIFIED;
5326 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5327 cpi->ccb_h.status = CAM_REQ_CMP;
5332 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5339 * The xpt as a "controller" has no interrupt sources, so polling
5343 xptpoll(struct cam_sim *sim)
5348 xpt_lock_buses(void)
5350 mtx_lock(&xsoftc.xpt_topo_lock);
5354 xpt_unlock_buses(void)
5356 mtx_unlock(&xsoftc.xpt_topo_lock);
5360 xpt_path_mtx(struct cam_path *path)
5363 return (&path->device->device_mtx);
5367 xpt_done_process(struct ccb_hdr *ccb_h)
5369 struct cam_sim *sim = NULL;
5370 struct cam_devq *devq = NULL;
5371 struct mtx *mtx = NULL;
5373 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5374 struct ccb_scsiio *csio;
5376 if (ccb_h->func_code == XPT_SCSI_IO) {
5377 csio = &((union ccb *)ccb_h)->csio;
5378 if (csio->bio != NULL)
5379 biotrack(csio->bio, __func__);
5383 if (ccb_h->flags & CAM_HIGH_POWER) {
5384 struct highpowerlist *hphead;
5385 struct cam_ed *device;
5387 mtx_lock(&xsoftc.xpt_highpower_lock);
5388 hphead = &xsoftc.highpowerq;
5390 device = STAILQ_FIRST(hphead);
5393 * Increment the count since this command is done.
5395 xsoftc.num_highpower++;
5398 * Any high powered commands queued up?
5400 if (device != NULL) {
5402 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5403 mtx_unlock(&xsoftc.xpt_highpower_lock);
5405 mtx_lock(&device->sim->devq->send_mtx);
5406 xpt_release_devq_device(device,
5407 /*count*/1, /*runqueue*/TRUE);
5408 mtx_unlock(&device->sim->devq->send_mtx);
5410 mtx_unlock(&xsoftc.xpt_highpower_lock);
5414 * Insulate against a race where the periph is destroyed but CCBs are
5415 * still not all processed. This shouldn't happen, but allows us better
5416 * bug diagnostic when it does.
5418 if (ccb_h->path->bus)
5419 sim = ccb_h->path->bus->sim;
5421 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5422 KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5423 xpt_release_simq(sim, /*run_queue*/FALSE);
5424 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5427 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5428 && (ccb_h->status & CAM_DEV_QFRZN)) {
5429 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5430 ccb_h->status &= ~CAM_DEV_QFRZN;
5433 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5434 struct cam_ed *dev = ccb_h->path->device;
5438 KASSERT(devq, ("Periph disappeared with request pending."));
5440 mtx_lock(&devq->send_mtx);
5441 devq->send_active--;
5442 devq->send_openings++;
5443 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5445 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5446 && (dev->ccbq.dev_active == 0))) {
5447 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5448 xpt_release_devq_device(dev, /*count*/1,
5449 /*run_queue*/FALSE);
5452 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5453 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5454 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5455 xpt_release_devq_device(dev, /*count*/1,
5456 /*run_queue*/FALSE);
5459 if (!device_is_queued(dev))
5460 (void)xpt_schedule_devq(devq, dev);
5462 mtx_unlock(&devq->send_mtx);
5464 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5465 mtx = xpt_path_mtx(ccb_h->path);
5468 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5469 && (--dev->tag_delay_count == 0))
5470 xpt_start_tags(ccb_h->path);
5474 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5476 mtx = xpt_path_mtx(ccb_h->path);
5486 /* Call the peripheral driver's callback */
5487 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5488 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5494 xpt_done_td(void *arg)
5496 struct cam_doneq *queue = arg;
5497 struct ccb_hdr *ccb_h;
5498 STAILQ_HEAD(, ccb_hdr) doneq;
5500 STAILQ_INIT(&doneq);
5501 mtx_lock(&queue->cam_doneq_mtx);
5503 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5504 queue->cam_doneq_sleep = 1;
5505 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5507 queue->cam_doneq_sleep = 0;
5509 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5510 mtx_unlock(&queue->cam_doneq_mtx);
5512 THREAD_NO_SLEEPING();
5513 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5514 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5515 xpt_done_process(ccb_h);
5517 THREAD_SLEEPING_OK();
5519 mtx_lock(&queue->cam_doneq_mtx);
5524 camisr_runqueue(void)
5526 struct ccb_hdr *ccb_h;
5527 struct cam_doneq *queue;
5530 /* Process global queues. */
5531 for (i = 0; i < cam_num_doneqs; i++) {
5532 queue = &cam_doneqs[i];
5533 mtx_lock(&queue->cam_doneq_mtx);
5534 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5535 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5536 mtx_unlock(&queue->cam_doneq_mtx);
5537 xpt_done_process(ccb_h);
5538 mtx_lock(&queue->cam_doneq_mtx);
5540 mtx_unlock(&queue->cam_doneq_mtx);
5550 static struct kv map[] = {
5551 { XPT_NOOP, "XPT_NOOP" },
5552 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5553 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5554 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5555 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5556 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5557 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5558 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5559 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5560 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5561 { XPT_DEBUG, "XPT_DEBUG" },
5562 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5563 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5564 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5565 { XPT_ASYNC, "XPT_ASYNC" },
5566 { XPT_ABORT, "XPT_ABORT" },
5567 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5568 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5569 { XPT_TERM_IO, "XPT_TERM_IO" },
5570 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5571 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5572 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5573 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5574 { XPT_ATA_IO, "XPT_ATA_IO" },
5575 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5576 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5577 { XPT_NVME_IO, "XPT_NVME_IO" },
5578 { XPT_MMC_IO, "XPT_MMC_IO" },
5579 { XPT_SMP_IO, "XPT_SMP_IO" },
5580 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5581 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5582 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5583 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5584 { XPT_EN_LUN, "XPT_EN_LUN" },
5585 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5586 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5587 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5588 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5589 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5590 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5591 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5596 xpt_action_name(uint32_t action)
5598 static char buffer[32]; /* Only for unknown messages -- racy */
5599 struct kv *walker = map;
5601 while (walker->name != NULL) {
5602 if (walker->v == action)
5603 return (walker->name);
5607 snprintf(buffer, sizeof(buffer), "%#x", action);