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");
103 uint32_t xpt_generation;
105 /* number of high powered commands that can go through right now */
106 struct mtx xpt_highpower_lock;
107 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
110 /* queue for handling async rescan requests. */
111 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
113 int buses_config_done;
119 * N.B., "busses" is an archaic spelling of "buses". In new code
120 * "buses" is preferred.
122 TAILQ_HEAD(,cam_eb) xpt_busses;
123 u_int bus_generation;
126 struct callout boot_callout;
127 struct task boot_task;
128 struct root_hold_token xpt_rootmount;
130 struct mtx xpt_topo_lock;
131 struct taskqueue *xpt_taskq;
136 DM_RET_FLAG_MASK = 0x0f,
139 DM_RET_DESCEND = 0x20,
141 DM_RET_ACTION_MASK = 0xf0
149 } xpt_traverse_depth;
151 struct xpt_traverse_config {
152 xpt_traverse_depth depth;
157 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
158 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
159 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
160 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
161 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
163 /* Transport layer configuration information */
164 static struct xpt_softc xsoftc;
166 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
168 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
169 &xsoftc.boot_delay, 0, "Bus registration wait time");
170 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
171 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
172 SYSCTL_INT(_kern_cam, OID_AUTO, announce_nosbuf, CTLFLAG_RWTUN,
173 &xsoftc.announce_nosbuf, 0, "Don't use sbuf for announcements");
176 struct mtx_padalign cam_doneq_mtx;
177 STAILQ_HEAD(, ccb_hdr) cam_doneq;
181 static struct cam_doneq cam_doneqs[MAXCPU];
182 static int cam_num_doneqs;
183 static struct proc *cam_proc;
185 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
186 &cam_num_doneqs, 0, "Number of completion queues/threads");
188 struct cam_periph *xpt_periph;
190 static periph_init_t xpt_periph_init;
192 static struct periph_driver xpt_driver =
194 xpt_periph_init, "xpt",
195 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
199 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
201 static d_open_t xptopen;
202 static d_close_t xptclose;
203 static d_ioctl_t xptioctl;
204 static d_ioctl_t xptdoioctl;
206 static struct cdevsw xpt_cdevsw = {
207 .d_version = D_VERSION,
215 /* Storage for debugging datastructures */
216 struct cam_path *cam_dpath;
217 u_int32_t __read_mostly cam_dflags = CAM_DEBUG_FLAGS;
218 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
219 &cam_dflags, 0, "Enabled debug flags");
220 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
221 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
222 &cam_debug_delay, 0, "Delay in us after each debug message");
224 /* Our boot-time initialization hook */
225 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
227 static moduledata_t cam_moduledata = {
229 cam_module_event_handler,
233 static int xpt_init(void *);
235 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
236 MODULE_VERSION(cam, 1);
239 static void xpt_async_bcast(struct async_list *async_head,
240 u_int32_t async_code,
241 struct cam_path *path,
243 static path_id_t xptnextfreepathid(void);
244 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
245 static union ccb *xpt_get_ccb(struct cam_periph *periph);
246 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
247 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
248 static void xpt_run_allocq_task(void *context, int pending);
249 static void xpt_run_devq(struct cam_devq *devq);
250 static callout_func_t xpt_release_devq_timeout;
251 static void xpt_release_simq_timeout(void *arg) __unused;
252 static void xpt_acquire_bus(struct cam_eb *bus);
253 static void xpt_release_bus(struct cam_eb *bus);
254 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
255 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
257 static struct cam_et*
258 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
259 static void xpt_acquire_target(struct cam_et *target);
260 static void xpt_release_target(struct cam_et *target);
261 static struct cam_eb*
262 xpt_find_bus(path_id_t path_id);
263 static struct cam_et*
264 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
265 static struct cam_ed*
266 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
267 static void xpt_config(void *arg);
268 static void xpt_hold_boot_locked(void);
269 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
270 u_int32_t new_priority);
271 static xpt_devicefunc_t xptpassannouncefunc;
272 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
273 static void xptpoll(struct cam_sim *sim);
274 static void camisr_runqueue(void);
275 static void xpt_done_process(struct ccb_hdr *ccb_h);
276 static void xpt_done_td(void *);
277 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
278 u_int num_patterns, struct cam_eb *bus);
279 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
281 struct cam_ed *device);
282 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
284 struct cam_periph *periph);
285 static xpt_busfunc_t xptedtbusfunc;
286 static xpt_targetfunc_t xptedttargetfunc;
287 static xpt_devicefunc_t xptedtdevicefunc;
288 static xpt_periphfunc_t xptedtperiphfunc;
289 static xpt_pdrvfunc_t xptplistpdrvfunc;
290 static xpt_periphfunc_t xptplistperiphfunc;
291 static int xptedtmatch(struct ccb_dev_match *cdm);
292 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
293 static int xptbustraverse(struct cam_eb *start_bus,
294 xpt_busfunc_t *tr_func, void *arg);
295 static int xpttargettraverse(struct cam_eb *bus,
296 struct cam_et *start_target,
297 xpt_targetfunc_t *tr_func, void *arg);
298 static int xptdevicetraverse(struct cam_et *target,
299 struct cam_ed *start_device,
300 xpt_devicefunc_t *tr_func, void *arg);
301 static int xptperiphtraverse(struct cam_ed *device,
302 struct cam_periph *start_periph,
303 xpt_periphfunc_t *tr_func, void *arg);
304 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
305 xpt_pdrvfunc_t *tr_func, void *arg);
306 static int xptpdperiphtraverse(struct periph_driver **pdrv,
307 struct cam_periph *start_periph,
308 xpt_periphfunc_t *tr_func,
310 static xpt_busfunc_t xptdefbusfunc;
311 static xpt_targetfunc_t xptdeftargetfunc;
312 static xpt_devicefunc_t xptdefdevicefunc;
313 static xpt_periphfunc_t xptdefperiphfunc;
314 static void xpt_finishconfig_task(void *context, int pending);
315 static void xpt_dev_async_default(u_int32_t async_code,
317 struct cam_et *target,
318 struct cam_ed *device,
320 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
321 struct cam_et *target,
323 static xpt_devicefunc_t xptsetasyncfunc;
324 static xpt_busfunc_t xptsetasyncbusfunc;
325 static cam_status xptregister(struct cam_periph *periph,
327 static __inline int device_is_queued(struct cam_ed *device);
330 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
334 mtx_assert(&devq->send_mtx, MA_OWNED);
335 if ((dev->ccbq.queue.entries > 0) &&
336 (dev->ccbq.dev_openings > 0) &&
337 (dev->ccbq.queue.qfrozen_cnt == 0)) {
339 * The priority of a device waiting for controller
340 * resources is that of the highest priority CCB
344 xpt_schedule_dev(&devq->send_queue,
346 CAMQ_GET_PRIO(&dev->ccbq.queue));
354 device_is_queued(struct cam_ed *device)
356 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
362 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
366 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
370 * Only allow read-write access.
372 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
376 * We don't allow nonblocking access.
378 if ((flags & O_NONBLOCK) != 0) {
379 printf("%s: can't do nonblocking access\n", devtoname(dev));
387 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
394 * Don't automatically grab the xpt softc lock here even though this is going
395 * through the xpt device. The xpt device is really just a back door for
396 * accessing other devices and SIMs, so the right thing to do is to grab
397 * the appropriate SIM lock once the bus/SIM is located.
400 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
404 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
405 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
411 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
419 * For the transport layer CAMIOCOMMAND ioctl, we really only want
420 * to accept CCB types that don't quite make sense to send through a
421 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
429 inccb = (union ccb *)addr;
430 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
431 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
432 inccb->csio.bio = NULL;
435 if (inccb->ccb_h.flags & CAM_UNLOCKED)
438 bus = xpt_find_bus(inccb->ccb_h.path_id);
442 switch (inccb->ccb_h.func_code) {
445 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
446 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
447 xpt_release_bus(bus);
452 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
453 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
454 xpt_release_bus(bus);
462 switch(inccb->ccb_h.func_code) {
470 ccb = xpt_alloc_ccb();
473 * Create a path using the bus, target, and lun the
476 if (xpt_create_path(&ccb->ccb_h.path, NULL,
477 inccb->ccb_h.path_id,
478 inccb->ccb_h.target_id,
479 inccb->ccb_h.target_lun) !=
485 /* Ensure all of our fields are correct */
486 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
487 inccb->ccb_h.pinfo.priority);
488 xpt_merge_ccb(ccb, inccb);
489 xpt_path_lock(ccb->ccb_h.path);
490 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
491 xpt_path_unlock(ccb->ccb_h.path);
492 bcopy(ccb, inccb, sizeof(union ccb));
493 xpt_free_path(ccb->ccb_h.path);
501 * This is an immediate CCB, so it's okay to
502 * allocate it on the stack.
506 * Create a path using the bus, target, and lun the
509 if (xpt_create_path(&ccb.ccb_h.path, NULL,
510 inccb->ccb_h.path_id,
511 inccb->ccb_h.target_id,
512 inccb->ccb_h.target_lun) !=
517 /* Ensure all of our fields are correct */
518 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
519 inccb->ccb_h.pinfo.priority);
520 xpt_merge_ccb(&ccb, inccb);
522 bcopy(&ccb, inccb, sizeof(union ccb));
523 xpt_free_path(ccb.ccb_h.path);
527 case XPT_DEV_MATCH: {
528 struct cam_periph_map_info mapinfo;
529 struct cam_path *old_path;
532 * We can't deal with physical addresses for this
533 * type of transaction.
535 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
542 * Save this in case the caller had it set to
543 * something in particular.
545 old_path = inccb->ccb_h.path;
548 * We really don't need a path for the matching
549 * code. The path is needed because of the
550 * debugging statements in xpt_action(). They
551 * assume that the CCB has a valid path.
553 inccb->ccb_h.path = xpt_periph->path;
555 bzero(&mapinfo, sizeof(mapinfo));
558 * Map the pattern and match buffers into kernel
559 * virtual address space.
561 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
564 inccb->ccb_h.path = old_path;
569 * This is an immediate CCB, we can send it on directly.
574 * Map the buffers back into user space.
576 cam_periph_unmapmem(inccb, &mapinfo);
578 inccb->ccb_h.path = old_path;
587 xpt_release_bus(bus);
591 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
592 * with the periphal driver name and unit name filled in. The other
593 * fields don't really matter as input. The passthrough driver name
594 * ("pass"), and unit number are passed back in the ccb. The current
595 * device generation number, and the index into the device peripheral
596 * driver list, and the status are also passed back. Note that
597 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
598 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
599 * (or rather should be) impossible for the device peripheral driver
600 * list to change since we look at the whole thing in one pass, and
601 * we do it with lock protection.
604 case CAMGETPASSTHRU: {
606 struct cam_periph *periph;
607 struct periph_driver **p_drv;
610 int base_periph_found;
612 ccb = (union ccb *)addr;
613 unit = ccb->cgdl.unit_number;
614 name = ccb->cgdl.periph_name;
615 base_periph_found = 0;
616 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
617 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
618 ccb->csio.bio = NULL;
622 * Sanity check -- make sure we don't get a null peripheral
625 if (*ccb->cgdl.periph_name == '\0') {
630 /* Keep the list from changing while we traverse it */
633 /* first find our driver in the list of drivers */
634 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
635 if (strcmp((*p_drv)->driver_name, name) == 0)
638 if (*p_drv == NULL) {
640 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
641 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
642 *ccb->cgdl.periph_name = '\0';
643 ccb->cgdl.unit_number = 0;
649 * Run through every peripheral instance of this driver
650 * and check to see whether it matches the unit passed
651 * in by the user. If it does, get out of the loops and
652 * find the passthrough driver associated with that
655 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
656 periph = TAILQ_NEXT(periph, unit_links)) {
658 if (periph->unit_number == unit)
662 * If we found the peripheral driver that the user passed
663 * in, go through all of the peripheral drivers for that
664 * particular device and look for a passthrough driver.
666 if (periph != NULL) {
667 struct cam_ed *device;
670 base_periph_found = 1;
671 device = periph->path->device;
672 for (i = 0, periph = SLIST_FIRST(&device->periphs);
674 periph = SLIST_NEXT(periph, periph_links), i++) {
676 * Check to see whether we have a
677 * passthrough device or not.
679 if (strcmp(periph->periph_name, "pass") == 0) {
681 * Fill in the getdevlist fields.
683 strlcpy(ccb->cgdl.periph_name,
685 sizeof(ccb->cgdl.periph_name));
686 ccb->cgdl.unit_number =
688 if (SLIST_NEXT(periph, periph_links))
690 CAM_GDEVLIST_MORE_DEVS;
693 CAM_GDEVLIST_LAST_DEVICE;
694 ccb->cgdl.generation =
698 * Fill in some CCB header fields
699 * that the user may want.
702 periph->path->bus->path_id;
703 ccb->ccb_h.target_id =
704 periph->path->target->target_id;
705 ccb->ccb_h.target_lun =
706 periph->path->device->lun_id;
707 ccb->ccb_h.status = CAM_REQ_CMP;
714 * If the periph is null here, one of two things has
715 * happened. The first possibility is that we couldn't
716 * find the unit number of the particular peripheral driver
717 * that the user is asking about. e.g. the user asks for
718 * the passthrough driver for "da11". We find the list of
719 * "da" peripherals all right, but there is no unit 11.
720 * The other possibility is that we went through the list
721 * of peripheral drivers attached to the device structure,
722 * but didn't find one with the name "pass". Either way,
723 * we return ENOENT, since we couldn't find something.
725 if (periph == NULL) {
726 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
727 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
728 *ccb->cgdl.periph_name = '\0';
729 ccb->cgdl.unit_number = 0;
732 * It is unfortunate that this is even necessary,
733 * but there are many, many clueless users out there.
734 * If this is true, the user is looking for the
735 * passthrough driver, but doesn't have one in his
738 if (base_periph_found == 1) {
739 printf("xptioctl: pass driver is not in the "
741 printf("xptioctl: put \"device pass\" in "
742 "your kernel config file\n");
757 cam_module_event_handler(module_t mod, int what, void *arg)
763 if ((error = xpt_init(NULL)) != 0)
775 static struct xpt_proto *
776 xpt_proto_find(cam_proto proto)
778 struct xpt_proto **pp;
780 SET_FOREACH(pp, cam_xpt_proto_set) {
781 if ((*pp)->proto == proto)
789 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
792 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
793 xpt_free_path(done_ccb->ccb_h.path);
794 xpt_free_ccb(done_ccb);
796 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
797 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
802 /* thread to handle bus rescans */
804 xpt_scanner_thread(void *dummy)
807 struct cam_path path;
811 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
812 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
814 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
815 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
819 * Since lock can be dropped inside and path freed
820 * by completion callback even before return here,
821 * take our own path copy for reference.
823 xpt_copy_path(&path, ccb->ccb_h.path);
824 xpt_path_lock(&path);
826 xpt_path_unlock(&path);
827 xpt_release_path(&path);
835 xpt_rescan(union ccb *ccb)
839 /* Prepare request */
840 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
841 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
842 ccb->ccb_h.func_code = XPT_SCAN_BUS;
843 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
844 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
845 ccb->ccb_h.func_code = XPT_SCAN_TGT;
846 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
847 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
848 ccb->ccb_h.func_code = XPT_SCAN_LUN;
850 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
851 xpt_free_path(ccb->ccb_h.path);
855 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
856 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
857 xpt_action_name(ccb->ccb_h.func_code)));
859 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
860 ccb->ccb_h.cbfcnp = xpt_rescan_done;
861 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
862 /* Don't make duplicate entries for the same paths. */
864 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
865 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
866 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
867 wakeup(&xsoftc.ccb_scanq);
869 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
870 xpt_free_path(ccb->ccb_h.path);
876 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
877 xpt_hold_boot_locked();
878 wakeup(&xsoftc.ccb_scanq);
882 /* Functions accessed by the peripheral drivers */
884 xpt_init(void *dummy)
886 struct cam_sim *xpt_sim;
887 struct cam_path *path;
888 struct cam_devq *devq;
892 TAILQ_INIT(&xsoftc.xpt_busses);
893 TAILQ_INIT(&xsoftc.ccb_scanq);
894 STAILQ_INIT(&xsoftc.highpowerq);
895 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
897 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
898 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
899 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
901 #ifdef CAM_BOOT_DELAY
903 * Override this value at compile time to assist our users
904 * who don't use loader to boot a kernel.
906 xsoftc.boot_delay = CAM_BOOT_DELAY;
910 * The xpt layer is, itself, the equivalent of a SIM.
911 * Allow 16 ccbs in the ccb pool for it. This should
912 * give decent parallelism when we probe buses and
913 * perform other XPT functions.
915 devq = cam_simq_alloc(16);
916 xpt_sim = cam_sim_alloc(xptaction,
922 /*max_dev_transactions*/0,
923 /*max_tagged_dev_transactions*/0,
928 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
929 printf("xpt_init: xpt_bus_register failed with status %#x,"
930 " failing attach\n", status);
935 * Looking at the XPT from the SIM layer, the XPT is
936 * the equivalent of a peripheral driver. Allocate
937 * a peripheral driver entry for us.
939 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
941 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
942 printf("xpt_init: xpt_create_path failed with status %#x,"
943 " failing attach\n", status);
947 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
948 path, NULL, 0, xpt_sim);
949 xpt_path_unlock(path);
952 if (cam_num_doneqs < 1)
953 cam_num_doneqs = 1 + mp_ncpus / 6;
954 else if (cam_num_doneqs > MAXCPU)
955 cam_num_doneqs = MAXCPU;
956 for (i = 0; i < cam_num_doneqs; i++) {
957 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
959 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
960 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
961 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
967 if (cam_num_doneqs < 1) {
968 printf("xpt_init: Cannot init completion queues "
969 "- failing attach\n");
974 * Register a callback for when interrupts are enabled.
976 config_intrhook_oneshot(xpt_config, NULL);
982 xptregister(struct cam_periph *periph, void *arg)
984 struct cam_sim *xpt_sim;
986 if (periph == NULL) {
987 printf("xptregister: periph was NULL!!\n");
988 return(CAM_REQ_CMP_ERR);
991 xpt_sim = (struct cam_sim *)arg;
992 xpt_sim->softc = periph;
994 periph->softc = NULL;
1000 xpt_add_periph(struct cam_periph *periph)
1002 struct cam_ed *device;
1005 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1006 device = periph->path->device;
1007 status = CAM_REQ_CMP;
1008 if (device != NULL) {
1009 mtx_lock(&device->target->bus->eb_mtx);
1010 device->generation++;
1011 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1012 mtx_unlock(&device->target->bus->eb_mtx);
1013 atomic_add_32(&xsoftc.xpt_generation, 1);
1020 xpt_remove_periph(struct cam_periph *periph)
1022 struct cam_ed *device;
1024 device = periph->path->device;
1025 if (device != NULL) {
1026 mtx_lock(&device->target->bus->eb_mtx);
1027 device->generation++;
1028 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1029 mtx_unlock(&device->target->bus->eb_mtx);
1030 atomic_add_32(&xsoftc.xpt_generation, 1);
1036 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1038 struct cam_path *path = periph->path;
1039 struct xpt_proto *proto;
1041 cam_periph_assert(periph, MA_OWNED);
1042 periph->flags |= CAM_PERIPH_ANNOUNCED;
1044 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1045 periph->periph_name, periph->unit_number,
1046 path->bus->sim->sim_name,
1047 path->bus->sim->unit_number,
1048 path->bus->sim->bus_id,
1050 path->target->target_id,
1051 (uintmax_t)path->device->lun_id);
1052 printf("%s%d: ", periph->periph_name, periph->unit_number);
1053 proto = xpt_proto_find(path->device->protocol);
1055 proto->ops->announce(path->device);
1057 printf("%s%d: Unknown protocol device %d\n",
1058 periph->periph_name, periph->unit_number,
1059 path->device->protocol);
1060 if (path->device->serial_num_len > 0) {
1061 /* Don't wrap the screen - print only the first 60 chars */
1062 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1063 periph->unit_number, path->device->serial_num);
1065 /* Announce transport details. */
1066 path->bus->xport->ops->announce(periph);
1067 /* Announce command queueing. */
1068 if (path->device->inq_flags & SID_CmdQue
1069 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1070 printf("%s%d: Command Queueing enabled\n",
1071 periph->periph_name, periph->unit_number);
1073 /* Announce caller's details if they've passed in. */
1074 if (announce_string != NULL)
1075 printf("%s%d: %s\n", periph->periph_name,
1076 periph->unit_number, announce_string);
1080 xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
1081 char *announce_string)
1083 struct cam_path *path = periph->path;
1084 struct xpt_proto *proto;
1086 cam_periph_assert(periph, MA_OWNED);
1087 periph->flags |= CAM_PERIPH_ANNOUNCED;
1089 /* Fall back to the non-sbuf method if necessary */
1090 if (xsoftc.announce_nosbuf != 0) {
1091 xpt_announce_periph(periph, announce_string);
1094 proto = xpt_proto_find(path->device->protocol);
1095 if (((proto != NULL) && (proto->ops->announce_sbuf == NULL)) ||
1096 (path->bus->xport->ops->announce_sbuf == NULL)) {
1097 xpt_announce_periph(periph, announce_string);
1101 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1102 periph->periph_name, periph->unit_number,
1103 path->bus->sim->sim_name,
1104 path->bus->sim->unit_number,
1105 path->bus->sim->bus_id,
1107 path->target->target_id,
1108 (uintmax_t)path->device->lun_id);
1109 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1112 proto->ops->announce_sbuf(path->device, sb);
1114 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1115 periph->periph_name, periph->unit_number,
1116 path->device->protocol);
1117 if (path->device->serial_num_len > 0) {
1118 /* Don't wrap the screen - print only the first 60 chars */
1119 sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
1120 periph->periph_name, periph->unit_number,
1121 path->device->serial_num);
1123 /* Announce transport details. */
1124 path->bus->xport->ops->announce_sbuf(periph, sb);
1125 /* Announce command queueing. */
1126 if (path->device->inq_flags & SID_CmdQue
1127 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1128 sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
1129 periph->periph_name, periph->unit_number);
1131 /* Announce caller's details if they've passed in. */
1132 if (announce_string != NULL)
1133 sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
1134 periph->unit_number, announce_string);
1138 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1141 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1142 periph->unit_number, quirks, bit_string);
1147 xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
1148 int quirks, char *bit_string)
1150 if (xsoftc.announce_nosbuf != 0) {
1151 xpt_announce_quirks(periph, quirks, bit_string);
1156 sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
1157 periph->unit_number, quirks, bit_string);
1162 xpt_denounce_periph(struct cam_periph *periph)
1164 struct cam_path *path = periph->path;
1165 struct xpt_proto *proto;
1167 cam_periph_assert(periph, MA_OWNED);
1168 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1169 periph->periph_name, periph->unit_number,
1170 path->bus->sim->sim_name,
1171 path->bus->sim->unit_number,
1172 path->bus->sim->bus_id,
1174 path->target->target_id,
1175 (uintmax_t)path->device->lun_id);
1176 printf("%s%d: ", periph->periph_name, periph->unit_number);
1177 proto = xpt_proto_find(path->device->protocol);
1179 proto->ops->denounce(path->device);
1181 printf("%s%d: Unknown protocol device %d\n",
1182 periph->periph_name, periph->unit_number,
1183 path->device->protocol);
1184 if (path->device->serial_num_len > 0)
1185 printf(" s/n %.60s", path->device->serial_num);
1186 printf(" detached\n");
1190 xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
1192 struct cam_path *path = periph->path;
1193 struct xpt_proto *proto;
1195 cam_periph_assert(periph, MA_OWNED);
1197 /* Fall back to the non-sbuf method if necessary */
1198 if (xsoftc.announce_nosbuf != 0) {
1199 xpt_denounce_periph(periph);
1202 proto = xpt_proto_find(path->device->protocol);
1203 if ((proto != NULL) && (proto->ops->denounce_sbuf == NULL)) {
1204 xpt_denounce_periph(periph);
1208 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1209 periph->periph_name, periph->unit_number,
1210 path->bus->sim->sim_name,
1211 path->bus->sim->unit_number,
1212 path->bus->sim->bus_id,
1214 path->target->target_id,
1215 (uintmax_t)path->device->lun_id);
1216 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1219 proto->ops->denounce_sbuf(path->device, sb);
1221 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1222 periph->periph_name, periph->unit_number,
1223 path->device->protocol);
1224 if (path->device->serial_num_len > 0)
1225 sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1226 sbuf_printf(sb, " detached\n");
1230 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1233 struct ccb_dev_advinfo cdai;
1234 struct scsi_vpd_device_id *did;
1235 struct scsi_vpd_id_descriptor *idd;
1237 xpt_path_assert(path, MA_OWNED);
1239 memset(&cdai, 0, sizeof(cdai));
1240 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1241 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1242 cdai.flags = CDAI_FLAG_NONE;
1246 if (!strcmp(attr, "GEOM::ident"))
1247 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1248 else if (!strcmp(attr, "GEOM::physpath"))
1249 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1250 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1251 strcmp(attr, "GEOM::lunname") == 0) {
1252 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1253 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1254 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT);
1255 if (cdai.buf == NULL) {
1262 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1263 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1264 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1265 if (cdai.provsiz == 0)
1267 switch(cdai.buftype) {
1268 case CDAI_TYPE_SCSI_DEVID:
1269 did = (struct scsi_vpd_device_id *)cdai.buf;
1270 if (strcmp(attr, "GEOM::lunid") == 0) {
1271 idd = scsi_get_devid(did, cdai.provsiz,
1272 scsi_devid_is_lun_naa);
1274 idd = scsi_get_devid(did, cdai.provsiz,
1275 scsi_devid_is_lun_eui64);
1277 idd = scsi_get_devid(did, cdai.provsiz,
1278 scsi_devid_is_lun_uuid);
1280 idd = scsi_get_devid(did, cdai.provsiz,
1281 scsi_devid_is_lun_md5);
1286 idd = scsi_get_devid(did, cdai.provsiz,
1287 scsi_devid_is_lun_t10);
1289 idd = scsi_get_devid(did, cdai.provsiz,
1290 scsi_devid_is_lun_name);
1295 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1296 SVPD_ID_CODESET_ASCII) {
1297 if (idd->length < len) {
1298 for (l = 0; l < idd->length; l++)
1299 buf[l] = idd->identifier[l] ?
1300 idd->identifier[l] : ' ';
1306 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1307 SVPD_ID_CODESET_UTF8) {
1308 l = strnlen(idd->identifier, idd->length);
1310 bcopy(idd->identifier, buf, l);
1316 if ((idd->id_type & SVPD_ID_TYPE_MASK) ==
1317 SVPD_ID_TYPE_UUID && idd->identifier[0] == 0x10) {
1318 if ((idd->length - 2) * 2 + 4 >= len) {
1322 for (l = 2, o = 0; l < idd->length; l++) {
1323 if (l == 6 || l == 8 || l == 10 || l == 12)
1324 o += sprintf(buf + o, "-");
1325 o += sprintf(buf + o, "%02x",
1326 idd->identifier[l]);
1330 if (idd->length * 2 < len) {
1331 for (l = 0; l < idd->length; l++)
1332 sprintf(buf + l * 2, "%02x",
1333 idd->identifier[l]);
1338 if (cdai.provsiz < len) {
1339 cdai.buf[cdai.provsiz] = 0;
1347 if ((char *)cdai.buf != buf)
1348 free(cdai.buf, M_CAMXPT);
1352 static dev_match_ret
1353 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1356 dev_match_ret retval;
1359 retval = DM_RET_NONE;
1362 * If we aren't given something to match against, that's an error.
1365 return(DM_RET_ERROR);
1368 * If there are no match entries, then this bus matches no
1371 if ((patterns == NULL) || (num_patterns == 0))
1372 return(DM_RET_DESCEND | DM_RET_COPY);
1374 for (i = 0; i < num_patterns; i++) {
1375 struct bus_match_pattern *cur_pattern;
1378 * If the pattern in question isn't for a bus node, we
1379 * aren't interested. However, we do indicate to the
1380 * calling routine that we should continue descending the
1381 * tree, since the user wants to match against lower-level
1384 if (patterns[i].type != DEV_MATCH_BUS) {
1385 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1386 retval |= DM_RET_DESCEND;
1390 cur_pattern = &patterns[i].pattern.bus_pattern;
1393 * If they want to match any bus node, we give them any
1396 if (cur_pattern->flags == BUS_MATCH_ANY) {
1397 /* set the copy flag */
1398 retval |= DM_RET_COPY;
1401 * If we've already decided on an action, go ahead
1404 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1409 * Not sure why someone would do this...
1411 if (cur_pattern->flags == BUS_MATCH_NONE)
1414 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1415 && (cur_pattern->path_id != bus->path_id))
1418 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1419 && (cur_pattern->bus_id != bus->sim->bus_id))
1422 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1423 && (cur_pattern->unit_number != bus->sim->unit_number))
1426 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1427 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1432 * If we get to this point, the user definitely wants
1433 * information on this bus. So tell the caller to copy the
1436 retval |= DM_RET_COPY;
1439 * If the return action has been set to descend, then we
1440 * know that we've already seen a non-bus matching
1441 * expression, therefore we need to further descend the tree.
1442 * This won't change by continuing around the loop, so we
1443 * go ahead and return. If we haven't seen a non-bus
1444 * matching expression, we keep going around the loop until
1445 * we exhaust the matching expressions. We'll set the stop
1446 * flag once we fall out of the loop.
1448 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1453 * If the return action hasn't been set to descend yet, that means
1454 * we haven't seen anything other than bus matching patterns. So
1455 * tell the caller to stop descending the tree -- the user doesn't
1456 * want to match against lower level tree elements.
1458 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1459 retval |= DM_RET_STOP;
1464 static dev_match_ret
1465 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1466 struct cam_ed *device)
1468 dev_match_ret retval;
1471 retval = DM_RET_NONE;
1474 * If we aren't given something to match against, that's an error.
1477 return(DM_RET_ERROR);
1480 * If there are no match entries, then this device matches no
1483 if ((patterns == NULL) || (num_patterns == 0))
1484 return(DM_RET_DESCEND | DM_RET_COPY);
1486 for (i = 0; i < num_patterns; i++) {
1487 struct device_match_pattern *cur_pattern;
1488 struct scsi_vpd_device_id *device_id_page;
1491 * If the pattern in question isn't for a device node, we
1492 * aren't interested.
1494 if (patterns[i].type != DEV_MATCH_DEVICE) {
1495 if ((patterns[i].type == DEV_MATCH_PERIPH)
1496 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1497 retval |= DM_RET_DESCEND;
1501 cur_pattern = &patterns[i].pattern.device_pattern;
1503 /* Error out if mutually exclusive options are specified. */
1504 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1505 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1506 return(DM_RET_ERROR);
1509 * If they want to match any device node, we give them any
1512 if (cur_pattern->flags == DEV_MATCH_ANY)
1516 * Not sure why someone would do this...
1518 if (cur_pattern->flags == DEV_MATCH_NONE)
1521 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1522 && (cur_pattern->path_id != device->target->bus->path_id))
1525 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1526 && (cur_pattern->target_id != device->target->target_id))
1529 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1530 && (cur_pattern->target_lun != device->lun_id))
1533 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1534 && (cam_quirkmatch((caddr_t)&device->inq_data,
1535 (caddr_t)&cur_pattern->data.inq_pat,
1536 1, sizeof(cur_pattern->data.inq_pat),
1537 scsi_static_inquiry_match) == NULL))
1540 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1541 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1542 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1543 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1544 device->device_id_len
1545 - SVPD_DEVICE_ID_HDR_LEN,
1546 cur_pattern->data.devid_pat.id,
1547 cur_pattern->data.devid_pat.id_len) != 0))
1552 * If we get to this point, the user definitely wants
1553 * information on this device. So tell the caller to copy
1556 retval |= DM_RET_COPY;
1559 * If the return action has been set to descend, then we
1560 * know that we've already seen a peripheral matching
1561 * expression, therefore we need to further descend the tree.
1562 * This won't change by continuing around the loop, so we
1563 * go ahead and return. If we haven't seen a peripheral
1564 * matching expression, we keep going around the loop until
1565 * we exhaust the matching expressions. We'll set the stop
1566 * flag once we fall out of the loop.
1568 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1573 * If the return action hasn't been set to descend yet, that means
1574 * we haven't seen any peripheral matching patterns. So tell the
1575 * caller to stop descending the tree -- the user doesn't want to
1576 * match against lower level tree elements.
1578 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1579 retval |= DM_RET_STOP;
1585 * Match a single peripheral against any number of match patterns.
1587 static dev_match_ret
1588 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1589 struct cam_periph *periph)
1591 dev_match_ret retval;
1595 * If we aren't given something to match against, that's an error.
1598 return(DM_RET_ERROR);
1601 * If there are no match entries, then this peripheral matches no
1604 if ((patterns == NULL) || (num_patterns == 0))
1605 return(DM_RET_STOP | DM_RET_COPY);
1608 * There aren't any nodes below a peripheral node, so there's no
1609 * reason to descend the tree any further.
1611 retval = DM_RET_STOP;
1613 for (i = 0; i < num_patterns; i++) {
1614 struct periph_match_pattern *cur_pattern;
1617 * If the pattern in question isn't for a peripheral, we
1618 * aren't interested.
1620 if (patterns[i].type != DEV_MATCH_PERIPH)
1623 cur_pattern = &patterns[i].pattern.periph_pattern;
1626 * If they want to match on anything, then we will do so.
1628 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1629 /* set the copy flag */
1630 retval |= DM_RET_COPY;
1633 * We've already set the return action to stop,
1634 * since there are no nodes below peripherals in
1641 * Not sure why someone would do this...
1643 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1646 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1647 && (cur_pattern->path_id != periph->path->bus->path_id))
1651 * For the target and lun id's, we have to make sure the
1652 * target and lun pointers aren't NULL. The xpt peripheral
1653 * has a wildcard target and device.
1655 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1656 && ((periph->path->target == NULL)
1657 ||(cur_pattern->target_id != periph->path->target->target_id)))
1660 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1661 && ((periph->path->device == NULL)
1662 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1665 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1666 && (cur_pattern->unit_number != periph->unit_number))
1669 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1670 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1675 * If we get to this point, the user definitely wants
1676 * information on this peripheral. So tell the caller to
1677 * copy the data out.
1679 retval |= DM_RET_COPY;
1682 * The return action has already been set to stop, since
1683 * peripherals don't have any nodes below them in the EDT.
1689 * If we get to this point, the peripheral that was passed in
1690 * doesn't match any of the patterns.
1696 xptedtbusfunc(struct cam_eb *bus, void *arg)
1698 struct ccb_dev_match *cdm;
1699 struct cam_et *target;
1700 dev_match_ret retval;
1702 cdm = (struct ccb_dev_match *)arg;
1705 * If our position is for something deeper in the tree, that means
1706 * that we've already seen this node. So, we keep going down.
1708 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1709 && (cdm->pos.cookie.bus == bus)
1710 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1711 && (cdm->pos.cookie.target != NULL))
1712 retval = DM_RET_DESCEND;
1714 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1717 * If we got an error, bail out of the search.
1719 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1720 cdm->status = CAM_DEV_MATCH_ERROR;
1725 * If the copy flag is set, copy this bus out.
1727 if (retval & DM_RET_COPY) {
1730 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1731 sizeof(struct dev_match_result));
1734 * If we don't have enough space to put in another
1735 * match result, save our position and tell the
1736 * user there are more devices to check.
1738 if (spaceleft < sizeof(struct dev_match_result)) {
1739 bzero(&cdm->pos, sizeof(cdm->pos));
1740 cdm->pos.position_type =
1741 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1743 cdm->pos.cookie.bus = bus;
1744 cdm->pos.generations[CAM_BUS_GENERATION]=
1745 xsoftc.bus_generation;
1746 cdm->status = CAM_DEV_MATCH_MORE;
1749 j = cdm->num_matches;
1751 cdm->matches[j].type = DEV_MATCH_BUS;
1752 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1753 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1754 cdm->matches[j].result.bus_result.unit_number =
1755 bus->sim->unit_number;
1756 strlcpy(cdm->matches[j].result.bus_result.dev_name,
1758 sizeof(cdm->matches[j].result.bus_result.dev_name));
1762 * If the user is only interested in buses, there's no
1763 * reason to descend to the next level in the tree.
1765 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1769 * If there is a target generation recorded, check it to
1770 * make sure the target list hasn't changed.
1772 mtx_lock(&bus->eb_mtx);
1773 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1774 && (cdm->pos.cookie.bus == bus)
1775 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1776 && (cdm->pos.cookie.target != NULL)) {
1777 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1779 mtx_unlock(&bus->eb_mtx);
1780 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1783 target = (struct cam_et *)cdm->pos.cookie.target;
1787 mtx_unlock(&bus->eb_mtx);
1789 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1793 xptedttargetfunc(struct cam_et *target, void *arg)
1795 struct ccb_dev_match *cdm;
1797 struct cam_ed *device;
1799 cdm = (struct ccb_dev_match *)arg;
1803 * If there is a device list generation recorded, check it to
1804 * make sure the device list hasn't changed.
1806 mtx_lock(&bus->eb_mtx);
1807 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1808 && (cdm->pos.cookie.bus == bus)
1809 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1810 && (cdm->pos.cookie.target == target)
1811 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1812 && (cdm->pos.cookie.device != NULL)) {
1813 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1814 target->generation) {
1815 mtx_unlock(&bus->eb_mtx);
1816 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1819 device = (struct cam_ed *)cdm->pos.cookie.device;
1823 mtx_unlock(&bus->eb_mtx);
1825 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1829 xptedtdevicefunc(struct cam_ed *device, void *arg)
1832 struct cam_periph *periph;
1833 struct ccb_dev_match *cdm;
1834 dev_match_ret retval;
1836 cdm = (struct ccb_dev_match *)arg;
1837 bus = device->target->bus;
1840 * If our position is for something deeper in the tree, that means
1841 * that we've already seen this node. So, we keep going down.
1843 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1844 && (cdm->pos.cookie.device == device)
1845 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1846 && (cdm->pos.cookie.periph != NULL))
1847 retval = DM_RET_DESCEND;
1849 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1852 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1853 cdm->status = CAM_DEV_MATCH_ERROR;
1858 * If the copy flag is set, copy this device out.
1860 if (retval & DM_RET_COPY) {
1863 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1864 sizeof(struct dev_match_result));
1867 * If we don't have enough space to put in another
1868 * match result, save our position and tell the
1869 * user there are more devices to check.
1871 if (spaceleft < sizeof(struct dev_match_result)) {
1872 bzero(&cdm->pos, sizeof(cdm->pos));
1873 cdm->pos.position_type =
1874 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1875 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1877 cdm->pos.cookie.bus = device->target->bus;
1878 cdm->pos.generations[CAM_BUS_GENERATION]=
1879 xsoftc.bus_generation;
1880 cdm->pos.cookie.target = device->target;
1881 cdm->pos.generations[CAM_TARGET_GENERATION] =
1882 device->target->bus->generation;
1883 cdm->pos.cookie.device = device;
1884 cdm->pos.generations[CAM_DEV_GENERATION] =
1885 device->target->generation;
1886 cdm->status = CAM_DEV_MATCH_MORE;
1889 j = cdm->num_matches;
1891 cdm->matches[j].type = DEV_MATCH_DEVICE;
1892 cdm->matches[j].result.device_result.path_id =
1893 device->target->bus->path_id;
1894 cdm->matches[j].result.device_result.target_id =
1895 device->target->target_id;
1896 cdm->matches[j].result.device_result.target_lun =
1898 cdm->matches[j].result.device_result.protocol =
1900 bcopy(&device->inq_data,
1901 &cdm->matches[j].result.device_result.inq_data,
1902 sizeof(struct scsi_inquiry_data));
1903 bcopy(&device->ident_data,
1904 &cdm->matches[j].result.device_result.ident_data,
1905 sizeof(struct ata_params));
1907 /* Let the user know whether this device is unconfigured */
1908 if (device->flags & CAM_DEV_UNCONFIGURED)
1909 cdm->matches[j].result.device_result.flags =
1910 DEV_RESULT_UNCONFIGURED;
1912 cdm->matches[j].result.device_result.flags =
1917 * If the user isn't interested in peripherals, don't descend
1918 * the tree any further.
1920 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1924 * If there is a peripheral list generation recorded, make sure
1925 * it hasn't changed.
1928 mtx_lock(&bus->eb_mtx);
1929 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1930 && (cdm->pos.cookie.bus == bus)
1931 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1932 && (cdm->pos.cookie.target == device->target)
1933 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1934 && (cdm->pos.cookie.device == device)
1935 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1936 && (cdm->pos.cookie.periph != NULL)) {
1937 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1938 device->generation) {
1939 mtx_unlock(&bus->eb_mtx);
1941 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1944 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1948 mtx_unlock(&bus->eb_mtx);
1951 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1955 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1957 struct ccb_dev_match *cdm;
1958 dev_match_ret retval;
1960 cdm = (struct ccb_dev_match *)arg;
1962 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1964 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1965 cdm->status = CAM_DEV_MATCH_ERROR;
1970 * If the copy flag is set, copy this peripheral out.
1972 if (retval & DM_RET_COPY) {
1976 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1977 sizeof(struct dev_match_result));
1980 * If we don't have enough space to put in another
1981 * match result, save our position and tell the
1982 * user there are more devices to check.
1984 if (spaceleft < sizeof(struct dev_match_result)) {
1985 bzero(&cdm->pos, sizeof(cdm->pos));
1986 cdm->pos.position_type =
1987 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1988 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1991 cdm->pos.cookie.bus = periph->path->bus;
1992 cdm->pos.generations[CAM_BUS_GENERATION]=
1993 xsoftc.bus_generation;
1994 cdm->pos.cookie.target = periph->path->target;
1995 cdm->pos.generations[CAM_TARGET_GENERATION] =
1996 periph->path->bus->generation;
1997 cdm->pos.cookie.device = periph->path->device;
1998 cdm->pos.generations[CAM_DEV_GENERATION] =
1999 periph->path->target->generation;
2000 cdm->pos.cookie.periph = periph;
2001 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2002 periph->path->device->generation;
2003 cdm->status = CAM_DEV_MATCH_MORE;
2007 j = cdm->num_matches;
2009 cdm->matches[j].type = DEV_MATCH_PERIPH;
2010 cdm->matches[j].result.periph_result.path_id =
2011 periph->path->bus->path_id;
2012 cdm->matches[j].result.periph_result.target_id =
2013 periph->path->target->target_id;
2014 cdm->matches[j].result.periph_result.target_lun =
2015 periph->path->device->lun_id;
2016 cdm->matches[j].result.periph_result.unit_number =
2017 periph->unit_number;
2018 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2019 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2020 periph->periph_name, l);
2027 xptedtmatch(struct ccb_dev_match *cdm)
2032 cdm->num_matches = 0;
2035 * Check the bus list generation. If it has changed, the user
2036 * needs to reset everything and start over.
2039 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2040 && (cdm->pos.cookie.bus != NULL)) {
2041 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
2042 xsoftc.bus_generation) {
2044 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2047 bus = (struct cam_eb *)cdm->pos.cookie.bus;
2053 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
2056 * If we get back 0, that means that we had to stop before fully
2057 * traversing the EDT. It also means that one of the subroutines
2058 * has set the status field to the proper value. If we get back 1,
2059 * we've fully traversed the EDT and copied out any matching entries.
2062 cdm->status = CAM_DEV_MATCH_LAST;
2068 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2070 struct cam_periph *periph;
2071 struct ccb_dev_match *cdm;
2073 cdm = (struct ccb_dev_match *)arg;
2076 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2077 && (cdm->pos.cookie.pdrv == pdrv)
2078 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2079 && (cdm->pos.cookie.periph != NULL)) {
2080 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2081 (*pdrv)->generation) {
2083 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2086 periph = (struct cam_periph *)cdm->pos.cookie.periph;
2092 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
2096 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2098 struct ccb_dev_match *cdm;
2099 dev_match_ret retval;
2101 cdm = (struct ccb_dev_match *)arg;
2103 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2105 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2106 cdm->status = CAM_DEV_MATCH_ERROR;
2111 * If the copy flag is set, copy this peripheral out.
2113 if (retval & DM_RET_COPY) {
2117 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2118 sizeof(struct dev_match_result));
2121 * If we don't have enough space to put in another
2122 * match result, save our position and tell the
2123 * user there are more devices to check.
2125 if (spaceleft < sizeof(struct dev_match_result)) {
2126 struct periph_driver **pdrv;
2129 bzero(&cdm->pos, sizeof(cdm->pos));
2130 cdm->pos.position_type =
2131 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2135 * This may look a bit non-sensical, but it is
2136 * actually quite logical. There are very few
2137 * peripheral drivers, and bloating every peripheral
2138 * structure with a pointer back to its parent
2139 * peripheral driver linker set entry would cost
2140 * more in the long run than doing this quick lookup.
2142 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2143 if (strcmp((*pdrv)->driver_name,
2144 periph->periph_name) == 0)
2148 if (*pdrv == NULL) {
2149 cdm->status = CAM_DEV_MATCH_ERROR;
2153 cdm->pos.cookie.pdrv = pdrv;
2155 * The periph generation slot does double duty, as
2156 * does the periph pointer slot. They are used for
2157 * both edt and pdrv lookups and positioning.
2159 cdm->pos.cookie.periph = periph;
2160 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2161 (*pdrv)->generation;
2162 cdm->status = CAM_DEV_MATCH_MORE;
2166 j = cdm->num_matches;
2168 cdm->matches[j].type = DEV_MATCH_PERIPH;
2169 cdm->matches[j].result.periph_result.path_id =
2170 periph->path->bus->path_id;
2173 * The transport layer peripheral doesn't have a target or
2176 if (periph->path->target)
2177 cdm->matches[j].result.periph_result.target_id =
2178 periph->path->target->target_id;
2180 cdm->matches[j].result.periph_result.target_id =
2181 CAM_TARGET_WILDCARD;
2183 if (periph->path->device)
2184 cdm->matches[j].result.periph_result.target_lun =
2185 periph->path->device->lun_id;
2187 cdm->matches[j].result.periph_result.target_lun =
2190 cdm->matches[j].result.periph_result.unit_number =
2191 periph->unit_number;
2192 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2193 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2194 periph->periph_name, l);
2201 xptperiphlistmatch(struct ccb_dev_match *cdm)
2205 cdm->num_matches = 0;
2208 * At this point in the edt traversal function, we check the bus
2209 * list generation to make sure that no buses have been added or
2210 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2211 * For the peripheral driver list traversal function, however, we
2212 * don't have to worry about new peripheral driver types coming or
2213 * going; they're in a linker set, and therefore can't change
2214 * without a recompile.
2217 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2218 && (cdm->pos.cookie.pdrv != NULL))
2219 ret = xptpdrvtraverse(
2220 (struct periph_driver **)cdm->pos.cookie.pdrv,
2221 xptplistpdrvfunc, cdm);
2223 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2226 * If we get back 0, that means that we had to stop before fully
2227 * traversing the peripheral driver tree. It also means that one of
2228 * the subroutines has set the status field to the proper value. If
2229 * we get back 1, we've fully traversed the EDT and copied out any
2233 cdm->status = CAM_DEV_MATCH_LAST;
2239 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2241 struct cam_eb *bus, *next_bus;
2249 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2257 for (; bus != NULL; bus = next_bus) {
2258 retval = tr_func(bus, arg);
2260 xpt_release_bus(bus);
2264 next_bus = TAILQ_NEXT(bus, links);
2266 next_bus->refcount++;
2268 xpt_release_bus(bus);
2274 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2275 xpt_targetfunc_t *tr_func, void *arg)
2277 struct cam_et *target, *next_target;
2282 target = start_target;
2284 mtx_lock(&bus->eb_mtx);
2285 target = TAILQ_FIRST(&bus->et_entries);
2286 if (target == NULL) {
2287 mtx_unlock(&bus->eb_mtx);
2291 mtx_unlock(&bus->eb_mtx);
2293 for (; target != NULL; target = next_target) {
2294 retval = tr_func(target, arg);
2296 xpt_release_target(target);
2299 mtx_lock(&bus->eb_mtx);
2300 next_target = TAILQ_NEXT(target, links);
2302 next_target->refcount++;
2303 mtx_unlock(&bus->eb_mtx);
2304 xpt_release_target(target);
2310 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2311 xpt_devicefunc_t *tr_func, void *arg)
2314 struct cam_ed *device, *next_device;
2320 device = start_device;
2322 mtx_lock(&bus->eb_mtx);
2323 device = TAILQ_FIRST(&target->ed_entries);
2324 if (device == NULL) {
2325 mtx_unlock(&bus->eb_mtx);
2329 mtx_unlock(&bus->eb_mtx);
2331 for (; device != NULL; device = next_device) {
2332 mtx_lock(&device->device_mtx);
2333 retval = tr_func(device, arg);
2334 mtx_unlock(&device->device_mtx);
2336 xpt_release_device(device);
2339 mtx_lock(&bus->eb_mtx);
2340 next_device = TAILQ_NEXT(device, links);
2342 next_device->refcount++;
2343 mtx_unlock(&bus->eb_mtx);
2344 xpt_release_device(device);
2350 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2351 xpt_periphfunc_t *tr_func, void *arg)
2354 struct cam_periph *periph, *next_periph;
2359 bus = device->target->bus;
2361 periph = start_periph;
2364 mtx_lock(&bus->eb_mtx);
2365 periph = SLIST_FIRST(&device->periphs);
2366 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2367 periph = SLIST_NEXT(periph, periph_links);
2368 if (periph == NULL) {
2369 mtx_unlock(&bus->eb_mtx);
2374 mtx_unlock(&bus->eb_mtx);
2377 for (; periph != NULL; periph = next_periph) {
2378 retval = tr_func(periph, arg);
2380 cam_periph_release_locked(periph);
2384 mtx_lock(&bus->eb_mtx);
2385 next_periph = SLIST_NEXT(periph, periph_links);
2386 while (next_periph != NULL &&
2387 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2388 next_periph = SLIST_NEXT(next_periph, periph_links);
2390 next_periph->refcount++;
2391 mtx_unlock(&bus->eb_mtx);
2393 cam_periph_release_locked(periph);
2399 xptpdrvtraverse(struct periph_driver **start_pdrv,
2400 xpt_pdrvfunc_t *tr_func, void *arg)
2402 struct periph_driver **pdrv;
2408 * We don't traverse the peripheral driver list like we do the
2409 * other lists, because it is a linker set, and therefore cannot be
2410 * changed during runtime. If the peripheral driver list is ever
2411 * re-done to be something other than a linker set (i.e. it can
2412 * change while the system is running), the list traversal should
2413 * be modified to work like the other traversal functions.
2415 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2416 *pdrv != NULL; pdrv++) {
2417 retval = tr_func(pdrv, arg);
2427 xptpdperiphtraverse(struct periph_driver **pdrv,
2428 struct cam_periph *start_periph,
2429 xpt_periphfunc_t *tr_func, void *arg)
2431 struct cam_periph *periph, *next_periph;
2437 periph = start_periph;
2440 periph = TAILQ_FIRST(&(*pdrv)->units);
2441 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2442 periph = TAILQ_NEXT(periph, unit_links);
2443 if (periph == NULL) {
2450 for (; periph != NULL; periph = next_periph) {
2451 cam_periph_lock(periph);
2452 retval = tr_func(periph, arg);
2453 cam_periph_unlock(periph);
2455 cam_periph_release(periph);
2459 next_periph = TAILQ_NEXT(periph, unit_links);
2460 while (next_periph != NULL &&
2461 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2462 next_periph = TAILQ_NEXT(next_periph, unit_links);
2464 next_periph->refcount++;
2466 cam_periph_release(periph);
2472 xptdefbusfunc(struct cam_eb *bus, void *arg)
2474 struct xpt_traverse_config *tr_config;
2476 tr_config = (struct xpt_traverse_config *)arg;
2478 if (tr_config->depth == XPT_DEPTH_BUS) {
2479 xpt_busfunc_t *tr_func;
2481 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2483 return(tr_func(bus, tr_config->tr_arg));
2485 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2489 xptdeftargetfunc(struct cam_et *target, void *arg)
2491 struct xpt_traverse_config *tr_config;
2493 tr_config = (struct xpt_traverse_config *)arg;
2495 if (tr_config->depth == XPT_DEPTH_TARGET) {
2496 xpt_targetfunc_t *tr_func;
2498 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2500 return(tr_func(target, tr_config->tr_arg));
2502 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2506 xptdefdevicefunc(struct cam_ed *device, void *arg)
2508 struct xpt_traverse_config *tr_config;
2510 tr_config = (struct xpt_traverse_config *)arg;
2512 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2513 xpt_devicefunc_t *tr_func;
2515 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2517 return(tr_func(device, tr_config->tr_arg));
2519 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2523 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2525 struct xpt_traverse_config *tr_config;
2526 xpt_periphfunc_t *tr_func;
2528 tr_config = (struct xpt_traverse_config *)arg;
2530 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2533 * Unlike the other default functions, we don't check for depth
2534 * here. The peripheral driver level is the last level in the EDT,
2535 * so if we're here, we should execute the function in question.
2537 return(tr_func(periph, tr_config->tr_arg));
2541 * Execute the given function for every bus in the EDT.
2544 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2546 struct xpt_traverse_config tr_config;
2548 tr_config.depth = XPT_DEPTH_BUS;
2549 tr_config.tr_func = tr_func;
2550 tr_config.tr_arg = arg;
2552 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2556 * Execute the given function for every device in the EDT.
2559 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2561 struct xpt_traverse_config tr_config;
2563 tr_config.depth = XPT_DEPTH_DEVICE;
2564 tr_config.tr_func = tr_func;
2565 tr_config.tr_arg = arg;
2567 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2571 xptsetasyncfunc(struct cam_ed *device, void *arg)
2573 struct cam_path path;
2574 struct ccb_getdev cgd;
2575 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2578 * Don't report unconfigured devices (Wildcard devs,
2579 * devices only for target mode, device instances
2580 * that have been invalidated but are waiting for
2581 * their last reference count to be released).
2583 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2586 xpt_compile_path(&path,
2588 device->target->bus->path_id,
2589 device->target->target_id,
2591 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2592 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2593 xpt_action((union ccb *)&cgd);
2594 csa->callback(csa->callback_arg,
2597 xpt_release_path(&path);
2603 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2605 struct cam_path path;
2606 struct ccb_pathinq cpi;
2607 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2609 xpt_compile_path(&path, /*periph*/NULL,
2611 CAM_TARGET_WILDCARD,
2613 xpt_path_lock(&path);
2614 xpt_path_inq(&cpi, &path);
2615 csa->callback(csa->callback_arg,
2618 xpt_path_unlock(&path);
2619 xpt_release_path(&path);
2625 xpt_action(union ccb *start_ccb)
2628 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2629 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2630 xpt_action_name(start_ccb->ccb_h.func_code)));
2632 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2633 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2637 xpt_action_default(union ccb *start_ccb)
2639 struct cam_path *path;
2640 struct cam_sim *sim;
2643 path = start_ccb->ccb_h.path;
2644 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2645 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2646 xpt_action_name(start_ccb->ccb_h.func_code)));
2648 switch (start_ccb->ccb_h.func_code) {
2651 struct cam_ed *device;
2654 * For the sake of compatibility with SCSI-1
2655 * devices that may not understand the identify
2656 * message, we include lun information in the
2657 * second byte of all commands. SCSI-1 specifies
2658 * that luns are a 3 bit value and reserves only 3
2659 * bits for lun information in the CDB. Later
2660 * revisions of the SCSI spec allow for more than 8
2661 * luns, but have deprecated lun information in the
2662 * CDB. So, if the lun won't fit, we must omit.
2664 * Also be aware that during initial probing for devices,
2665 * the inquiry information is unknown but initialized to 0.
2666 * This means that this code will be exercised while probing
2667 * devices with an ANSI revision greater than 2.
2669 device = path->device;
2670 if (device->protocol_version <= SCSI_REV_2
2671 && start_ccb->ccb_h.target_lun < 8
2672 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2674 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2675 start_ccb->ccb_h.target_lun << 5;
2677 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2681 case XPT_CONT_TARGET_IO:
2682 start_ccb->csio.sense_resid = 0;
2683 start_ccb->csio.resid = 0;
2686 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2687 start_ccb->ataio.resid = 0;
2691 case XPT_NVME_ADMIN:
2694 /* XXX just like nmve_io? */
2699 struct cam_devq *devq;
2701 devq = path->bus->sim->devq;
2702 mtx_lock(&devq->send_mtx);
2703 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2704 if (xpt_schedule_devq(devq, path->device) != 0)
2706 mtx_unlock(&devq->send_mtx);
2709 case XPT_CALC_GEOMETRY:
2710 /* Filter out garbage */
2711 if (start_ccb->ccg.block_size == 0
2712 || start_ccb->ccg.volume_size == 0) {
2713 start_ccb->ccg.cylinders = 0;
2714 start_ccb->ccg.heads = 0;
2715 start_ccb->ccg.secs_per_track = 0;
2716 start_ccb->ccb_h.status = CAM_REQ_CMP;
2719 #if defined(__sparc64__)
2721 * For sparc64, we may need adjust the geometry of large
2722 * disks in order to fit the limitations of the 16-bit
2723 * fields of the VTOC8 disk label.
2725 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2726 start_ccb->ccb_h.status = CAM_REQ_CMP;
2733 union ccb* abort_ccb;
2735 abort_ccb = start_ccb->cab.abort_ccb;
2736 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2737 struct cam_ed *device;
2738 struct cam_devq *devq;
2740 device = abort_ccb->ccb_h.path->device;
2741 devq = device->sim->devq;
2743 mtx_lock(&devq->send_mtx);
2744 if (abort_ccb->ccb_h.pinfo.index > 0) {
2745 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2746 abort_ccb->ccb_h.status =
2747 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2748 xpt_freeze_devq_device(device, 1);
2749 mtx_unlock(&devq->send_mtx);
2750 xpt_done(abort_ccb);
2751 start_ccb->ccb_h.status = CAM_REQ_CMP;
2754 mtx_unlock(&devq->send_mtx);
2756 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2757 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2759 * We've caught this ccb en route to
2760 * the SIM. Flag it for abort and the
2761 * SIM will do so just before starting
2762 * real work on the CCB.
2764 abort_ccb->ccb_h.status =
2765 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2766 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2767 start_ccb->ccb_h.status = CAM_REQ_CMP;
2771 if (XPT_FC_IS_QUEUED(abort_ccb)
2772 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2774 * It's already completed but waiting
2775 * for our SWI to get to it.
2777 start_ccb->ccb_h.status = CAM_UA_ABORT;
2781 * If we weren't able to take care of the abort request
2782 * in the XPT, pass the request down to the SIM for processing.
2786 case XPT_ACCEPT_TARGET_IO:
2788 case XPT_IMMED_NOTIFY:
2789 case XPT_NOTIFY_ACK:
2791 case XPT_IMMEDIATE_NOTIFY:
2792 case XPT_NOTIFY_ACKNOWLEDGE:
2793 case XPT_GET_SIM_KNOB_OLD:
2794 case XPT_GET_SIM_KNOB:
2795 case XPT_SET_SIM_KNOB:
2796 case XPT_GET_TRAN_SETTINGS:
2797 case XPT_SET_TRAN_SETTINGS:
2800 sim = path->bus->sim;
2802 if (mtx && !mtx_owned(mtx))
2807 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2808 ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2809 (*(sim->sim_action))(sim, start_ccb);
2810 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2811 ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2815 case XPT_PATH_STATS:
2816 start_ccb->cpis.last_reset = path->bus->last_reset;
2817 start_ccb->ccb_h.status = CAM_REQ_CMP;
2824 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2825 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2827 struct ccb_getdev *cgd;
2829 cgd = &start_ccb->cgd;
2830 cgd->protocol = dev->protocol;
2831 cgd->inq_data = dev->inq_data;
2832 cgd->ident_data = dev->ident_data;
2833 cgd->inq_flags = dev->inq_flags;
2834 cgd->ccb_h.status = CAM_REQ_CMP;
2835 cgd->serial_num_len = dev->serial_num_len;
2836 if ((dev->serial_num_len > 0)
2837 && (dev->serial_num != NULL))
2838 bcopy(dev->serial_num, cgd->serial_num,
2839 dev->serial_num_len);
2843 case XPT_GDEV_STATS:
2845 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2846 struct cam_ed *dev = path->device;
2847 struct cam_eb *bus = path->bus;
2848 struct cam_et *tar = path->target;
2849 struct cam_devq *devq = bus->sim->devq;
2851 mtx_lock(&devq->send_mtx);
2852 cgds->dev_openings = dev->ccbq.dev_openings;
2853 cgds->dev_active = dev->ccbq.dev_active;
2854 cgds->allocated = dev->ccbq.allocated;
2855 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2856 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2857 cgds->last_reset = tar->last_reset;
2858 cgds->maxtags = dev->maxtags;
2859 cgds->mintags = dev->mintags;
2860 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2861 cgds->last_reset = bus->last_reset;
2862 mtx_unlock(&devq->send_mtx);
2863 cgds->ccb_h.status = CAM_REQ_CMP;
2868 struct cam_periph *nperiph;
2869 struct periph_list *periph_head;
2870 struct ccb_getdevlist *cgdl;
2872 struct cam_ed *device;
2879 * Don't want anyone mucking with our data.
2881 device = path->device;
2882 periph_head = &device->periphs;
2883 cgdl = &start_ccb->cgdl;
2886 * Check and see if the list has changed since the user
2887 * last requested a list member. If so, tell them that the
2888 * list has changed, and therefore they need to start over
2889 * from the beginning.
2891 if ((cgdl->index != 0) &&
2892 (cgdl->generation != device->generation)) {
2893 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2898 * Traverse the list of peripherals and attempt to find
2899 * the requested peripheral.
2901 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2902 (nperiph != NULL) && (i <= cgdl->index);
2903 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2904 if (i == cgdl->index) {
2905 strlcpy(cgdl->periph_name,
2906 nperiph->periph_name,
2907 sizeof(cgdl->periph_name));
2908 cgdl->unit_number = nperiph->unit_number;
2913 cgdl->status = CAM_GDEVLIST_ERROR;
2917 if (nperiph == NULL)
2918 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2920 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2923 cgdl->generation = device->generation;
2925 cgdl->ccb_h.status = CAM_REQ_CMP;
2930 dev_pos_type position_type;
2931 struct ccb_dev_match *cdm;
2933 cdm = &start_ccb->cdm;
2936 * There are two ways of getting at information in the EDT.
2937 * The first way is via the primary EDT tree. It starts
2938 * with a list of buses, then a list of targets on a bus,
2939 * then devices/luns on a target, and then peripherals on a
2940 * device/lun. The "other" way is by the peripheral driver
2941 * lists. The peripheral driver lists are organized by
2942 * peripheral driver. (obviously) So it makes sense to
2943 * use the peripheral driver list if the user is looking
2944 * for something like "da1", or all "da" devices. If the
2945 * user is looking for something on a particular bus/target
2946 * or lun, it's generally better to go through the EDT tree.
2949 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2950 position_type = cdm->pos.position_type;
2954 position_type = CAM_DEV_POS_NONE;
2956 for (i = 0; i < cdm->num_patterns; i++) {
2957 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2958 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2959 position_type = CAM_DEV_POS_EDT;
2964 if (cdm->num_patterns == 0)
2965 position_type = CAM_DEV_POS_EDT;
2966 else if (position_type == CAM_DEV_POS_NONE)
2967 position_type = CAM_DEV_POS_PDRV;
2970 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2971 case CAM_DEV_POS_EDT:
2974 case CAM_DEV_POS_PDRV:
2975 xptperiphlistmatch(cdm);
2978 cdm->status = CAM_DEV_MATCH_ERROR;
2982 if (cdm->status == CAM_DEV_MATCH_ERROR)
2983 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2985 start_ccb->ccb_h.status = CAM_REQ_CMP;
2991 struct ccb_setasync *csa;
2992 struct async_node *cur_entry;
2993 struct async_list *async_head;
2996 csa = &start_ccb->csa;
2997 added = csa->event_enable;
2998 async_head = &path->device->asyncs;
3001 * If there is already an entry for us, simply
3004 cur_entry = SLIST_FIRST(async_head);
3005 while (cur_entry != NULL) {
3006 if ((cur_entry->callback_arg == csa->callback_arg)
3007 && (cur_entry->callback == csa->callback))
3009 cur_entry = SLIST_NEXT(cur_entry, links);
3012 if (cur_entry != NULL) {
3014 * If the request has no flags set,
3017 added &= ~cur_entry->event_enable;
3018 if (csa->event_enable == 0) {
3019 SLIST_REMOVE(async_head, cur_entry,
3021 xpt_release_device(path->device);
3022 free(cur_entry, M_CAMXPT);
3024 cur_entry->event_enable = csa->event_enable;
3026 csa->event_enable = added;
3028 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3030 if (cur_entry == NULL) {
3031 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3034 cur_entry->event_enable = csa->event_enable;
3035 cur_entry->event_lock = (path->bus->sim->mtx &&
3036 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
3037 cur_entry->callback_arg = csa->callback_arg;
3038 cur_entry->callback = csa->callback;
3039 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3040 xpt_acquire_device(path->device);
3042 start_ccb->ccb_h.status = CAM_REQ_CMP;
3047 struct ccb_relsim *crs;
3050 crs = &start_ccb->crs;
3054 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3058 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3060 /* Don't ever go below one opening */
3061 if (crs->openings > 0) {
3062 xpt_dev_ccbq_resize(path, crs->openings);
3065 "number of openings is now %d\n",
3071 mtx_lock(&dev->sim->devq->send_mtx);
3072 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3074 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3077 * Just extend the old timeout and decrement
3078 * the freeze count so that a single timeout
3079 * is sufficient for releasing the queue.
3081 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3082 callout_stop(&dev->callout);
3085 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3088 callout_reset_sbt(&dev->callout,
3089 SBT_1MS * crs->release_timeout, 0,
3090 xpt_release_devq_timeout, dev, 0);
3092 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3096 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3098 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3100 * Decrement the freeze count so that a single
3101 * completion is still sufficient to unfreeze
3104 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3107 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3108 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3112 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3114 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3115 || (dev->ccbq.dev_active == 0)) {
3117 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3120 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3121 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3124 mtx_unlock(&dev->sim->devq->send_mtx);
3126 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3127 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3128 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3129 start_ccb->ccb_h.status = CAM_REQ_CMP;
3133 struct cam_path *oldpath;
3135 /* Check that all request bits are supported. */
3136 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3137 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3141 cam_dflags = CAM_DEBUG_NONE;
3142 if (cam_dpath != NULL) {
3143 oldpath = cam_dpath;
3145 xpt_free_path(oldpath);
3147 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3148 if (xpt_create_path(&cam_dpath, NULL,
3149 start_ccb->ccb_h.path_id,
3150 start_ccb->ccb_h.target_id,
3151 start_ccb->ccb_h.target_lun) !=
3153 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3155 cam_dflags = start_ccb->cdbg.flags;
3156 start_ccb->ccb_h.status = CAM_REQ_CMP;
3157 xpt_print(cam_dpath, "debugging flags now %x\n",
3161 start_ccb->ccb_h.status = CAM_REQ_CMP;
3165 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3166 xpt_freeze_devq(path, 1);
3167 start_ccb->ccb_h.status = CAM_REQ_CMP;
3169 case XPT_REPROBE_LUN:
3170 xpt_async(AC_INQ_CHANGED, path, NULL);
3171 start_ccb->ccb_h.status = CAM_REQ_CMP;
3172 xpt_done(start_ccb);
3179 xpt_print(start_ccb->ccb_h.path,
3180 "%s: CCB type %#x %s not supported\n", __func__,
3181 start_ccb->ccb_h.func_code,
3182 xpt_action_name(start_ccb->ccb_h.func_code));
3183 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3184 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3185 xpt_done(start_ccb);
3189 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3190 ("xpt_action_default: func= %#x %s status %#x\n",
3191 start_ccb->ccb_h.func_code,
3192 xpt_action_name(start_ccb->ccb_h.func_code),
3193 start_ccb->ccb_h.status));
3197 * Call the sim poll routine to allow the sim to complete
3198 * any inflight requests, then call camisr_runqueue to
3199 * complete any CCB that the polling completed.
3202 xpt_sim_poll(struct cam_sim *sim)
3209 (*(sim->sim_poll))(sim);
3216 xpt_poll_setup(union ccb *start_ccb)
3219 struct cam_sim *sim;
3220 struct cam_devq *devq;
3223 timeout = start_ccb->ccb_h.timeout * 10;
3224 sim = start_ccb->ccb_h.path->bus->sim;
3226 dev = start_ccb->ccb_h.path->device;
3229 * Steal an opening so that no other queued requests
3230 * can get it before us while we simulate interrupts.
3232 mtx_lock(&devq->send_mtx);
3233 dev->ccbq.dev_openings--;
3234 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3236 mtx_unlock(&devq->send_mtx);
3239 mtx_lock(&devq->send_mtx);
3241 dev->ccbq.dev_openings++;
3242 mtx_unlock(&devq->send_mtx);
3248 xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3251 while (--timeout > 0) {
3252 xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
3253 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3261 * XXX Is it worth adding a sim_timeout entry
3262 * point so we can attempt recovery? If
3263 * this is only used for dumps, I don't think
3266 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3271 xpt_polled_action(union ccb *start_ccb)
3276 timeout = start_ccb->ccb_h.timeout * 10;
3277 dev = start_ccb->ccb_h.path->device;
3279 mtx_unlock(&dev->device_mtx);
3281 timeout = xpt_poll_setup(start_ccb);
3283 xpt_action(start_ccb);
3284 xpt_pollwait(start_ccb, timeout);
3286 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3289 mtx_lock(&dev->device_mtx);
3293 * Schedule a peripheral driver to receive a ccb when its
3294 * target device has space for more transactions.
3297 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3300 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3301 cam_periph_assert(periph, MA_OWNED);
3302 if (new_priority < periph->scheduled_priority) {
3303 periph->scheduled_priority = new_priority;
3304 xpt_run_allocq(periph, 0);
3310 * Schedule a device to run on a given queue.
3311 * If the device was inserted as a new entry on the queue,
3312 * return 1 meaning the device queue should be run. If we
3313 * were already queued, implying someone else has already
3314 * started the queue, return 0 so the caller doesn't attempt
3318 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3319 u_int32_t new_priority)
3322 u_int32_t old_priority;
3324 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3327 old_priority = pinfo->priority;
3330 * Are we already queued?
3332 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3333 /* Simply reorder based on new priority */
3334 if (new_priority < old_priority) {
3335 camq_change_priority(queue, pinfo->index,
3337 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3338 ("changed priority to %d\n",
3344 /* New entry on the queue */
3345 if (new_priority < old_priority)
3346 pinfo->priority = new_priority;
3348 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3349 ("Inserting onto queue\n"));
3350 pinfo->generation = ++queue->generation;
3351 camq_insert(queue, pinfo);
3358 xpt_run_allocq_task(void *context, int pending)
3360 struct cam_periph *periph = context;
3362 cam_periph_lock(periph);
3363 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3364 xpt_run_allocq(periph, 1);
3365 cam_periph_unlock(periph);
3366 cam_periph_release(periph);
3370 xpt_run_allocq(struct cam_periph *periph, int sleep)
3372 struct cam_ed *device;
3376 cam_periph_assert(periph, MA_OWNED);
3377 if (periph->periph_allocating)
3379 cam_periph_doacquire(periph);
3380 periph->periph_allocating = 1;
3381 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3382 device = periph->path->device;
3385 while ((prio = min(periph->scheduled_priority,
3386 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3387 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3388 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3391 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3393 ccb = xpt_get_ccb(periph);
3396 if (periph->flags & CAM_PERIPH_RUN_TASK)
3398 cam_periph_doacquire(periph);
3399 periph->flags |= CAM_PERIPH_RUN_TASK;
3400 taskqueue_enqueue(xsoftc.xpt_taskq,
3401 &periph->periph_run_task);
3404 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3405 if (prio == periph->immediate_priority) {
3406 periph->immediate_priority = CAM_PRIORITY_NONE;
3407 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3408 ("waking cam_periph_getccb()\n"));
3409 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3411 wakeup(&periph->ccb_list);
3413 periph->scheduled_priority = CAM_PRIORITY_NONE;
3414 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3415 ("calling periph_start()\n"));
3416 periph->periph_start(periph, ccb);
3421 xpt_release_ccb(ccb);
3422 periph->periph_allocating = 0;
3423 cam_periph_release_locked(periph);
3427 xpt_run_devq(struct cam_devq *devq)
3431 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3433 devq->send_queue.qfrozen_cnt++;
3434 while ((devq->send_queue.entries > 0)
3435 && (devq->send_openings > 0)
3436 && (devq->send_queue.qfrozen_cnt <= 1)) {
3437 struct cam_ed *device;
3438 union ccb *work_ccb;
3439 struct cam_sim *sim;
3440 struct xpt_proto *proto;
3442 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3444 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3445 ("running device %p\n", device));
3447 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3448 if (work_ccb == NULL) {
3449 printf("device on run queue with no ccbs???\n");
3453 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3455 mtx_lock(&xsoftc.xpt_highpower_lock);
3456 if (xsoftc.num_highpower <= 0) {
3458 * We got a high power command, but we
3459 * don't have any available slots. Freeze
3460 * the device queue until we have a slot
3463 xpt_freeze_devq_device(device, 1);
3464 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3467 mtx_unlock(&xsoftc.xpt_highpower_lock);
3471 * Consume a high power slot while
3474 xsoftc.num_highpower--;
3476 mtx_unlock(&xsoftc.xpt_highpower_lock);
3478 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3479 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3480 devq->send_openings--;
3481 devq->send_active++;
3482 xpt_schedule_devq(devq, device);
3483 mtx_unlock(&devq->send_mtx);
3485 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3487 * The client wants to freeze the queue
3488 * after this CCB is sent.
3490 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3493 /* In Target mode, the peripheral driver knows best... */
3494 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3495 if ((device->inq_flags & SID_CmdQue) != 0
3496 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3497 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3500 * Clear this in case of a retried CCB that
3501 * failed due to a rejected tag.
3503 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3506 KASSERT(device == work_ccb->ccb_h.path->device,
3507 ("device (%p) / path->device (%p) mismatch",
3508 device, work_ccb->ccb_h.path->device));
3509 proto = xpt_proto_find(device->protocol);
3510 if (proto && proto->ops->debug_out)
3511 proto->ops->debug_out(work_ccb);
3514 * Device queues can be shared among multiple SIM instances
3515 * that reside on different buses. Use the SIM from the
3516 * queued device, rather than the one from the calling bus.
3520 if (mtx && !mtx_owned(mtx))
3524 work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3525 (*(sim->sim_action))(sim, work_ccb);
3528 mtx_lock(&devq->send_mtx);
3530 devq->send_queue.qfrozen_cnt--;
3534 * This function merges stuff from the slave ccb into the master ccb, while
3535 * keeping important fields in the master ccb constant.
3538 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3542 * Pull fields that are valid for peripheral drivers to set
3543 * into the master CCB along with the CCB "payload".
3545 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3546 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3547 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3548 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3549 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3550 sizeof(union ccb) - sizeof(struct ccb_hdr));
3554 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3555 u_int32_t priority, u_int32_t flags)
3558 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3559 ccb_h->pinfo.priority = priority;
3561 ccb_h->path_id = path->bus->path_id;
3563 ccb_h->target_id = path->target->target_id;
3565 ccb_h->target_id = CAM_TARGET_WILDCARD;
3567 ccb_h->target_lun = path->device->lun_id;
3568 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3570 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3572 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3573 ccb_h->flags = flags;
3578 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3580 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3583 /* Path manipulation functions */
3585 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3586 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3588 struct cam_path *path;
3591 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3594 status = CAM_RESRC_UNAVAIL;
3597 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3598 if (status != CAM_REQ_CMP) {
3599 free(path, M_CAMPATH);
3602 *new_path_ptr = path;
3607 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3608 struct cam_periph *periph, path_id_t path_id,
3609 target_id_t target_id, lun_id_t lun_id)
3612 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3617 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3618 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3621 struct cam_et *target;
3622 struct cam_ed *device;
3625 status = CAM_REQ_CMP; /* Completed without error */
3626 target = NULL; /* Wildcarded */
3627 device = NULL; /* Wildcarded */
3630 * We will potentially modify the EDT, so block interrupts
3631 * that may attempt to create cam paths.
3633 bus = xpt_find_bus(path_id);
3635 status = CAM_PATH_INVALID;
3638 mtx_lock(&bus->eb_mtx);
3639 target = xpt_find_target(bus, target_id);
3640 if (target == NULL) {
3642 struct cam_et *new_target;
3644 new_target = xpt_alloc_target(bus, target_id);
3645 if (new_target == NULL) {
3646 status = CAM_RESRC_UNAVAIL;
3648 target = new_target;
3652 if (target != NULL) {
3653 device = xpt_find_device(target, lun_id);
3654 if (device == NULL) {
3656 struct cam_ed *new_device;
3659 (*(bus->xport->ops->alloc_device))(bus,
3662 if (new_device == NULL) {
3663 status = CAM_RESRC_UNAVAIL;
3665 device = new_device;
3669 mtx_unlock(&bus->eb_mtx);
3673 * Only touch the user's data if we are successful.
3675 if (status == CAM_REQ_CMP) {
3676 new_path->periph = perph;
3677 new_path->bus = bus;
3678 new_path->target = target;
3679 new_path->device = device;
3680 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3683 xpt_release_device(device);
3685 xpt_release_target(target);
3687 xpt_release_bus(bus);
3693 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3695 struct cam_path *new_path;
3697 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3698 if (new_path == NULL)
3699 return(CAM_RESRC_UNAVAIL);
3700 xpt_copy_path(new_path, path);
3701 *new_path_ptr = new_path;
3702 return (CAM_REQ_CMP);
3706 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3710 if (path->bus != NULL)
3711 xpt_acquire_bus(path->bus);
3712 if (path->target != NULL)
3713 xpt_acquire_target(path->target);
3714 if (path->device != NULL)
3715 xpt_acquire_device(path->device);
3719 xpt_release_path(struct cam_path *path)
3721 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3722 if (path->device != NULL) {
3723 xpt_release_device(path->device);
3724 path->device = NULL;
3726 if (path->target != NULL) {
3727 xpt_release_target(path->target);
3728 path->target = NULL;
3730 if (path->bus != NULL) {
3731 xpt_release_bus(path->bus);
3737 xpt_free_path(struct cam_path *path)
3740 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3741 xpt_release_path(path);
3742 free(path, M_CAMPATH);
3746 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3747 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3753 *bus_ref = path->bus->refcount;
3759 *periph_ref = path->periph->refcount;
3766 *target_ref = path->target->refcount;
3772 *device_ref = path->device->refcount;
3779 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3780 * in path1, 2 for match with wildcards in path2.
3783 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3787 if (path1->bus != path2->bus) {
3788 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3790 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3795 if (path1->target != path2->target) {
3796 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3799 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3804 if (path1->device != path2->device) {
3805 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3808 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3817 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3821 if (path->bus != dev->target->bus) {
3822 if (path->bus->path_id == CAM_BUS_WILDCARD)
3824 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3829 if (path->target != dev->target) {
3830 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3833 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3838 if (path->device != dev) {
3839 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3842 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3851 xpt_print_path(struct cam_path *path)
3854 char buffer[XPT_PRINT_LEN];
3856 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3857 xpt_path_sbuf(path, &sb);
3859 printf("%s", sbuf_data(&sb));
3864 xpt_print_device(struct cam_ed *device)
3868 printf("(nopath): ");
3870 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3871 device->sim->unit_number,
3872 device->sim->bus_id,
3873 device->target->target_id,
3874 (uintmax_t)device->lun_id);
3879 xpt_print(struct cam_path *path, const char *fmt, ...)
3883 char buffer[XPT_PRINT_LEN];
3885 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3887 xpt_path_sbuf(path, &sb);
3889 sbuf_vprintf(&sb, fmt, ap);
3893 printf("%s", sbuf_data(&sb));
3898 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3903 sbuf_new(&sb, str, str_len, 0);
3904 len = xpt_path_sbuf(path, &sb);
3910 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3914 sbuf_printf(sb, "(nopath): ");
3916 if (path->periph != NULL)
3917 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3918 path->periph->unit_number);
3920 sbuf_printf(sb, "(noperiph:");
3922 if (path->bus != NULL)
3923 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3924 path->bus->sim->unit_number,
3925 path->bus->sim->bus_id);
3927 sbuf_printf(sb, "nobus:");
3929 if (path->target != NULL)
3930 sbuf_printf(sb, "%d:", path->target->target_id);
3932 sbuf_printf(sb, "X:");
3934 if (path->device != NULL)
3935 sbuf_printf(sb, "%jx): ",
3936 (uintmax_t)path->device->lun_id);
3938 sbuf_printf(sb, "X): ");
3941 return(sbuf_len(sb));
3945 xpt_path_path_id(struct cam_path *path)
3947 return(path->bus->path_id);
3951 xpt_path_target_id(struct cam_path *path)
3953 if (path->target != NULL)
3954 return (path->target->target_id);
3956 return (CAM_TARGET_WILDCARD);
3960 xpt_path_lun_id(struct cam_path *path)
3962 if (path->device != NULL)
3963 return (path->device->lun_id);
3965 return (CAM_LUN_WILDCARD);
3969 xpt_path_sim(struct cam_path *path)
3972 return (path->bus->sim);
3976 xpt_path_periph(struct cam_path *path)
3979 return (path->periph);
3983 * Release a CAM control block for the caller. Remit the cost of the structure
3984 * to the device referenced by the path. If the this device had no 'credits'
3985 * and peripheral drivers have registered async callbacks for this notification
3989 xpt_release_ccb(union ccb *free_ccb)
3991 struct cam_ed *device;
3992 struct cam_periph *periph;
3994 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3995 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3996 device = free_ccb->ccb_h.path->device;
3997 periph = free_ccb->ccb_h.path->periph;
3999 xpt_free_ccb(free_ccb);
4000 periph->periph_allocated--;
4001 cam_ccbq_release_opening(&device->ccbq);
4002 xpt_run_allocq(periph, 0);
4005 /* Functions accessed by SIM drivers */
4007 static struct xpt_xport_ops xport_default_ops = {
4008 .alloc_device = xpt_alloc_device_default,
4009 .action = xpt_action_default,
4010 .async = xpt_dev_async_default,
4012 static struct xpt_xport xport_default = {
4013 .xport = XPORT_UNKNOWN,
4015 .ops = &xport_default_ops,
4018 CAM_XPT_XPORT(xport_default);
4021 * A sim structure, listing the SIM entry points and instance
4022 * identification info is passed to xpt_bus_register to hook the SIM
4023 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4024 * for this new bus and places it in the array of buses and assigns
4025 * it a path_id. The path_id may be influenced by "hard wiring"
4026 * information specified by the user. Once interrupt services are
4027 * available, the bus will be probed.
4030 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
4032 struct cam_eb *new_bus;
4033 struct cam_eb *old_bus;
4034 struct ccb_pathinq cpi;
4035 struct cam_path *path;
4039 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4040 M_CAMXPT, M_NOWAIT|M_ZERO);
4041 if (new_bus == NULL) {
4042 /* Couldn't satisfy request */
4043 return (CAM_RESRC_UNAVAIL);
4046 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4047 TAILQ_INIT(&new_bus->et_entries);
4050 timevalclear(&new_bus->last_reset);
4052 new_bus->refcount = 1; /* Held until a bus_deregister event */
4053 new_bus->generation = 0;
4056 sim->path_id = new_bus->path_id =
4057 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4058 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4059 while (old_bus != NULL
4060 && old_bus->path_id < new_bus->path_id)
4061 old_bus = TAILQ_NEXT(old_bus, links);
4062 if (old_bus != NULL)
4063 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4065 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4066 xsoftc.bus_generation++;
4070 * Set a default transport so that a PATH_INQ can be issued to
4071 * the SIM. This will then allow for probing and attaching of
4072 * a more appropriate transport.
4074 new_bus->xport = &xport_default;
4076 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
4077 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4078 if (status != CAM_REQ_CMP) {
4079 xpt_release_bus(new_bus);
4080 return (CAM_RESRC_UNAVAIL);
4083 xpt_path_inq(&cpi, path);
4085 if (cpi.ccb_h.status == CAM_REQ_CMP) {
4086 struct xpt_xport **xpt;
4088 SET_FOREACH(xpt, cam_xpt_xport_set) {
4089 if ((*xpt)->xport == cpi.transport) {
4090 new_bus->xport = *xpt;
4094 if (new_bus->xport == NULL) {
4096 "No transport found for %d\n", cpi.transport);
4097 xpt_release_bus(new_bus);
4098 free(path, M_CAMXPT);
4099 return (CAM_RESRC_UNAVAIL);
4103 /* Notify interested parties */
4104 if (sim->path_id != CAM_XPT_PATH_ID) {
4106 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4107 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4108 union ccb *scan_ccb;
4110 /* Initiate bus rescan. */
4111 scan_ccb = xpt_alloc_ccb_nowait();
4112 if (scan_ccb != NULL) {
4113 scan_ccb->ccb_h.path = path;
4114 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4115 scan_ccb->crcn.flags = 0;
4116 xpt_rescan(scan_ccb);
4119 "Can't allocate CCB to scan bus\n");
4120 xpt_free_path(path);
4123 xpt_free_path(path);
4125 xpt_free_path(path);
4126 return (CAM_SUCCESS);
4130 xpt_bus_deregister(path_id_t pathid)
4132 struct cam_path bus_path;
4135 status = xpt_compile_path(&bus_path, NULL, pathid,
4136 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4137 if (status != CAM_REQ_CMP)
4140 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4141 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4143 /* Release the reference count held while registered. */
4144 xpt_release_bus(bus_path.bus);
4145 xpt_release_path(&bus_path);
4147 return (CAM_REQ_CMP);
4151 xptnextfreepathid(void)
4157 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4159 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4161 /* Find an unoccupied pathid */
4162 while (bus != NULL && bus->path_id <= pathid) {
4163 if (bus->path_id == pathid)
4165 bus = TAILQ_NEXT(bus, links);
4169 * Ensure that this pathid is not reserved for
4170 * a bus that may be registered in the future.
4172 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4174 /* Start the search over */
4181 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4188 pathid = CAM_XPT_PATH_ID;
4189 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4190 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4193 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4194 if (strcmp(dname, "scbus")) {
4195 /* Avoid a bit of foot shooting. */
4198 if (dunit < 0) /* unwired?! */
4200 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4201 if (sim_bus == val) {
4205 } else if (sim_bus == 0) {
4206 /* Unspecified matches bus 0 */
4210 printf("Ambiguous scbus configuration for %s%d "
4211 "bus %d, cannot wire down. The kernel "
4212 "config entry for scbus%d should "
4213 "specify a controller bus.\n"
4214 "Scbus will be assigned dynamically.\n",
4215 sim_name, sim_unit, sim_bus, dunit);
4220 if (pathid == CAM_XPT_PATH_ID)
4221 pathid = xptnextfreepathid();
4226 xpt_async_string(u_int32_t async_code)
4229 switch (async_code) {
4230 case AC_BUS_RESET: return ("AC_BUS_RESET");
4231 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4232 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4233 case AC_SENT_BDR: return ("AC_SENT_BDR");
4234 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4235 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4236 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4237 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4238 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4239 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4240 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4241 case AC_CONTRACT: return ("AC_CONTRACT");
4242 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4243 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4245 return ("AC_UNKNOWN");
4249 xpt_async_size(u_int32_t async_code)
4252 switch (async_code) {
4253 case AC_BUS_RESET: return (0);
4254 case AC_UNSOL_RESEL: return (0);
4255 case AC_SCSI_AEN: return (0);
4256 case AC_SENT_BDR: return (0);
4257 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4258 case AC_PATH_DEREGISTERED: return (0);
4259 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4260 case AC_LOST_DEVICE: return (0);
4261 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4262 case AC_INQ_CHANGED: return (0);
4263 case AC_GETDEV_CHANGED: return (0);
4264 case AC_CONTRACT: return (sizeof(struct ac_contract));
4265 case AC_ADVINFO_CHANGED: return (-1);
4266 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4272 xpt_async_process_dev(struct cam_ed *device, void *arg)
4274 union ccb *ccb = arg;
4275 struct cam_path *path = ccb->ccb_h.path;
4276 void *async_arg = ccb->casync.async_arg_ptr;
4277 u_int32_t async_code = ccb->casync.async_code;
4280 if (path->device != device
4281 && path->device->lun_id != CAM_LUN_WILDCARD
4282 && device->lun_id != CAM_LUN_WILDCARD)
4286 * The async callback could free the device.
4287 * If it is a broadcast async, it doesn't hold
4288 * device reference, so take our own reference.
4290 xpt_acquire_device(device);
4293 * If async for specific device is to be delivered to
4294 * the wildcard client, take the specific device lock.
4295 * XXX: We may need a way for client to specify it.
4297 if ((device->lun_id == CAM_LUN_WILDCARD &&
4298 path->device->lun_id != CAM_LUN_WILDCARD) ||
4299 (device->target->target_id == CAM_TARGET_WILDCARD &&
4300 path->target->target_id != CAM_TARGET_WILDCARD) ||
4301 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4302 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4303 mtx_unlock(&device->device_mtx);
4304 xpt_path_lock(path);
4309 (*(device->target->bus->xport->ops->async))(async_code,
4310 device->target->bus, device->target, device, async_arg);
4311 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4314 xpt_path_unlock(path);
4315 mtx_lock(&device->device_mtx);
4317 xpt_release_device(device);
4322 xpt_async_process_tgt(struct cam_et *target, void *arg)
4324 union ccb *ccb = arg;
4325 struct cam_path *path = ccb->ccb_h.path;
4327 if (path->target != target
4328 && path->target->target_id != CAM_TARGET_WILDCARD
4329 && target->target_id != CAM_TARGET_WILDCARD)
4332 if (ccb->casync.async_code == AC_SENT_BDR) {
4333 /* Update our notion of when the last reset occurred */
4334 microtime(&target->last_reset);
4337 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4341 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4344 struct cam_path *path;
4346 u_int32_t async_code;
4348 path = ccb->ccb_h.path;
4349 async_code = ccb->casync.async_code;
4350 async_arg = ccb->casync.async_arg_ptr;
4351 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4352 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4355 if (async_code == AC_BUS_RESET) {
4356 /* Update our notion of when the last reset occurred */
4357 microtime(&bus->last_reset);
4360 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4363 * If this wasn't a fully wildcarded async, tell all
4364 * clients that want all async events.
4366 if (bus != xpt_periph->path->bus) {
4367 xpt_path_lock(xpt_periph->path);
4368 xpt_async_process_dev(xpt_periph->path->device, ccb);
4369 xpt_path_unlock(xpt_periph->path);
4372 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4373 xpt_release_devq(path, 1, TRUE);
4375 xpt_release_simq(path->bus->sim, TRUE);
4376 if (ccb->casync.async_arg_size > 0)
4377 free(async_arg, M_CAMXPT);
4378 xpt_free_path(path);
4383 xpt_async_bcast(struct async_list *async_head,
4384 u_int32_t async_code,
4385 struct cam_path *path, void *async_arg)
4387 struct async_node *cur_entry;
4390 cur_entry = SLIST_FIRST(async_head);
4391 while (cur_entry != NULL) {
4392 struct async_node *next_entry;
4394 * Grab the next list entry before we call the current
4395 * entry's callback. This is because the callback function
4396 * can delete its async callback entry.
4398 next_entry = SLIST_NEXT(cur_entry, links);
4399 if ((cur_entry->event_enable & async_code) != 0) {
4400 mtx = cur_entry->event_lock ?
4401 path->device->sim->mtx : NULL;
4404 cur_entry->callback(cur_entry->callback_arg,
4410 cur_entry = next_entry;
4415 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4420 ccb = xpt_alloc_ccb_nowait();
4422 xpt_print(path, "Can't allocate CCB to send %s\n",
4423 xpt_async_string(async_code));
4427 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4428 xpt_print(path, "Can't allocate path to send %s\n",
4429 xpt_async_string(async_code));
4433 ccb->ccb_h.path->periph = NULL;
4434 ccb->ccb_h.func_code = XPT_ASYNC;
4435 ccb->ccb_h.cbfcnp = xpt_async_process;
4436 ccb->ccb_h.flags |= CAM_UNLOCKED;
4437 ccb->casync.async_code = async_code;
4438 ccb->casync.async_arg_size = 0;
4439 size = xpt_async_size(async_code);
4440 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4441 ("xpt_async: func %#x %s aync_code %d %s\n",
4442 ccb->ccb_h.func_code,
4443 xpt_action_name(ccb->ccb_h.func_code),
4445 xpt_async_string(async_code)));
4446 if (size > 0 && async_arg != NULL) {
4447 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4448 if (ccb->casync.async_arg_ptr == NULL) {
4449 xpt_print(path, "Can't allocate argument to send %s\n",
4450 xpt_async_string(async_code));
4451 xpt_free_path(ccb->ccb_h.path);
4455 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4456 ccb->casync.async_arg_size = size;
4457 } else if (size < 0) {
4458 ccb->casync.async_arg_ptr = async_arg;
4459 ccb->casync.async_arg_size = size;
4461 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4462 xpt_freeze_devq(path, 1);
4464 xpt_freeze_simq(path->bus->sim, 1);
4469 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4470 struct cam_et *target, struct cam_ed *device,
4475 * We only need to handle events for real devices.
4477 if (target->target_id == CAM_TARGET_WILDCARD
4478 || device->lun_id == CAM_LUN_WILDCARD)
4481 printf("%s called\n", __func__);
4485 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4487 struct cam_devq *devq;
4490 devq = dev->sim->devq;
4491 mtx_assert(&devq->send_mtx, MA_OWNED);
4492 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4493 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4494 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4495 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4496 /* Remove frozen device from sendq. */
4497 if (device_is_queued(dev))
4498 camq_remove(&devq->send_queue, dev->devq_entry.index);
4503 xpt_freeze_devq(struct cam_path *path, u_int count)
4505 struct cam_ed *dev = path->device;
4506 struct cam_devq *devq;
4509 devq = dev->sim->devq;
4510 mtx_lock(&devq->send_mtx);
4511 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4512 freeze = xpt_freeze_devq_device(dev, count);
4513 mtx_unlock(&devq->send_mtx);
4518 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4520 struct cam_devq *devq;
4524 mtx_lock(&devq->send_mtx);
4525 freeze = (devq->send_queue.qfrozen_cnt += count);
4526 mtx_unlock(&devq->send_mtx);
4531 xpt_release_devq_timeout(void *arg)
4534 struct cam_devq *devq;
4536 dev = (struct cam_ed *)arg;
4537 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4538 devq = dev->sim->devq;
4539 mtx_assert(&devq->send_mtx, MA_OWNED);
4540 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4545 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4548 struct cam_devq *devq;
4550 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4553 devq = dev->sim->devq;
4554 mtx_lock(&devq->send_mtx);
4555 if (xpt_release_devq_device(dev, count, run_queue))
4556 xpt_run_devq(dev->sim->devq);
4557 mtx_unlock(&devq->send_mtx);
4561 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4564 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4565 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4566 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4567 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4568 if (count > dev->ccbq.queue.qfrozen_cnt) {
4570 printf("xpt_release_devq(): requested %u > present %u\n",
4571 count, dev->ccbq.queue.qfrozen_cnt);
4573 count = dev->ccbq.queue.qfrozen_cnt;
4575 dev->ccbq.queue.qfrozen_cnt -= count;
4576 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4578 * No longer need to wait for a successful
4579 * command completion.
4581 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4583 * Remove any timeouts that might be scheduled
4584 * to release this queue.
4586 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4587 callout_stop(&dev->callout);
4588 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4591 * Now that we are unfrozen schedule the
4592 * device so any pending transactions are
4595 xpt_schedule_devq(dev->sim->devq, dev);
4602 xpt_release_simq(struct cam_sim *sim, int run_queue)
4604 struct cam_devq *devq;
4607 mtx_lock(&devq->send_mtx);
4608 if (devq->send_queue.qfrozen_cnt <= 0) {
4610 printf("xpt_release_simq: requested 1 > present %u\n",
4611 devq->send_queue.qfrozen_cnt);
4614 devq->send_queue.qfrozen_cnt--;
4615 if (devq->send_queue.qfrozen_cnt == 0) {
4617 * If there is a timeout scheduled to release this
4618 * sim queue, remove it. The queue frozen count is
4621 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4622 callout_stop(&sim->callout);
4623 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4627 * Now that we are unfrozen run the send queue.
4629 xpt_run_devq(sim->devq);
4632 mtx_unlock(&devq->send_mtx);
4636 * XXX Appears to be unused.
4639 xpt_release_simq_timeout(void *arg)
4641 struct cam_sim *sim;
4643 sim = (struct cam_sim *)arg;
4644 xpt_release_simq(sim, /* run_queue */ TRUE);
4648 xpt_done(union ccb *done_ccb)
4650 struct cam_doneq *queue;
4653 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4654 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4655 done_ccb->csio.bio != NULL)
4656 biotrack(done_ccb->csio.bio, __func__);
4659 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4660 ("xpt_done: func= %#x %s status %#x\n",
4661 done_ccb->ccb_h.func_code,
4662 xpt_action_name(done_ccb->ccb_h.func_code),
4663 done_ccb->ccb_h.status));
4664 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4667 /* Store the time the ccb was in the sim */
4668 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4669 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4670 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4671 queue = &cam_doneqs[hash];
4672 mtx_lock(&queue->cam_doneq_mtx);
4673 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4674 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4675 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4676 mtx_unlock(&queue->cam_doneq_mtx);
4678 wakeup(&queue->cam_doneq);
4682 xpt_done_direct(union ccb *done_ccb)
4685 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4686 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4687 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4690 /* Store the time the ccb was in the sim */
4691 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4692 xpt_done_process(&done_ccb->ccb_h);
4700 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4705 xpt_alloc_ccb_nowait()
4709 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4714 xpt_free_ccb(union ccb *free_ccb)
4716 free(free_ccb, M_CAMCCB);
4721 /* Private XPT functions */
4724 * Get a CAM control block for the caller. Charge the structure to the device
4725 * referenced by the path. If we don't have sufficient resources to allocate
4726 * more ccbs, we return NULL.
4729 xpt_get_ccb_nowait(struct cam_periph *periph)
4733 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4734 if (new_ccb == NULL)
4736 periph->periph_allocated++;
4737 cam_ccbq_take_opening(&periph->path->device->ccbq);
4742 xpt_get_ccb(struct cam_periph *periph)
4746 cam_periph_unlock(periph);
4747 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4748 cam_periph_lock(periph);
4749 periph->periph_allocated++;
4750 cam_ccbq_take_opening(&periph->path->device->ccbq);
4755 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4757 struct ccb_hdr *ccb_h;
4759 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4760 cam_periph_assert(periph, MA_OWNED);
4761 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4762 ccb_h->pinfo.priority != priority) {
4763 if (priority < periph->immediate_priority) {
4764 periph->immediate_priority = priority;
4765 xpt_run_allocq(periph, 0);
4767 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4770 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4771 return ((union ccb *)ccb_h);
4775 xpt_acquire_bus(struct cam_eb *bus)
4784 xpt_release_bus(struct cam_eb *bus)
4788 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4789 if (--bus->refcount > 0) {
4793 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4794 xsoftc.bus_generation++;
4796 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4797 ("destroying bus, but target list is not empty"));
4798 cam_sim_release(bus->sim);
4799 mtx_destroy(&bus->eb_mtx);
4800 free(bus, M_CAMXPT);
4803 static struct cam_et *
4804 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4806 struct cam_et *cur_target, *target;
4808 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4809 mtx_assert(&bus->eb_mtx, MA_OWNED);
4810 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4815 TAILQ_INIT(&target->ed_entries);
4817 target->target_id = target_id;
4818 target->refcount = 1;
4819 target->generation = 0;
4820 target->luns = NULL;
4821 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4822 timevalclear(&target->last_reset);
4824 * Hold a reference to our parent bus so it
4825 * will not go away before we do.
4829 /* Insertion sort into our bus's target list */
4830 cur_target = TAILQ_FIRST(&bus->et_entries);
4831 while (cur_target != NULL && cur_target->target_id < target_id)
4832 cur_target = TAILQ_NEXT(cur_target, links);
4833 if (cur_target != NULL) {
4834 TAILQ_INSERT_BEFORE(cur_target, target, links);
4836 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4843 xpt_acquire_target(struct cam_et *target)
4845 struct cam_eb *bus = target->bus;
4847 mtx_lock(&bus->eb_mtx);
4849 mtx_unlock(&bus->eb_mtx);
4853 xpt_release_target(struct cam_et *target)
4855 struct cam_eb *bus = target->bus;
4857 mtx_lock(&bus->eb_mtx);
4858 if (--target->refcount > 0) {
4859 mtx_unlock(&bus->eb_mtx);
4862 TAILQ_REMOVE(&bus->et_entries, target, links);
4864 mtx_unlock(&bus->eb_mtx);
4865 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4866 ("destroying target, but device list is not empty"));
4867 xpt_release_bus(bus);
4868 mtx_destroy(&target->luns_mtx);
4870 free(target->luns, M_CAMXPT);
4871 free(target, M_CAMXPT);
4874 static struct cam_ed *
4875 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4878 struct cam_ed *device;
4880 device = xpt_alloc_device(bus, target, lun_id);
4884 device->mintags = 1;
4885 device->maxtags = 1;
4890 xpt_destroy_device(void *context, int pending)
4892 struct cam_ed *device = context;
4894 mtx_lock(&device->device_mtx);
4895 mtx_destroy(&device->device_mtx);
4896 free(device, M_CAMDEV);
4900 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4902 struct cam_ed *cur_device, *device;
4903 struct cam_devq *devq;
4906 mtx_assert(&bus->eb_mtx, MA_OWNED);
4907 /* Make space for us in the device queue on our bus */
4908 devq = bus->sim->devq;
4909 mtx_lock(&devq->send_mtx);
4910 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4911 mtx_unlock(&devq->send_mtx);
4912 if (status != CAM_REQ_CMP)
4915 device = (struct cam_ed *)malloc(sizeof(*device),
4916 M_CAMDEV, M_NOWAIT|M_ZERO);
4920 cam_init_pinfo(&device->devq_entry);
4921 device->target = target;
4922 device->lun_id = lun_id;
4923 device->sim = bus->sim;
4924 if (cam_ccbq_init(&device->ccbq,
4925 bus->sim->max_dev_openings) != 0) {
4926 free(device, M_CAMDEV);
4929 SLIST_INIT(&device->asyncs);
4930 SLIST_INIT(&device->periphs);
4931 device->generation = 0;
4932 device->flags = CAM_DEV_UNCONFIGURED;
4933 device->tag_delay_count = 0;
4934 device->tag_saved_openings = 0;
4935 device->refcount = 1;
4936 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4937 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4938 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4940 * Hold a reference to our parent bus so it
4941 * will not go away before we do.
4945 cur_device = TAILQ_FIRST(&target->ed_entries);
4946 while (cur_device != NULL && cur_device->lun_id < lun_id)
4947 cur_device = TAILQ_NEXT(cur_device, links);
4948 if (cur_device != NULL)
4949 TAILQ_INSERT_BEFORE(cur_device, device, links);
4951 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4952 target->generation++;
4957 xpt_acquire_device(struct cam_ed *device)
4959 struct cam_eb *bus = device->target->bus;
4961 mtx_lock(&bus->eb_mtx);
4963 mtx_unlock(&bus->eb_mtx);
4967 xpt_release_device(struct cam_ed *device)
4969 struct cam_eb *bus = device->target->bus;
4970 struct cam_devq *devq;
4972 mtx_lock(&bus->eb_mtx);
4973 if (--device->refcount > 0) {
4974 mtx_unlock(&bus->eb_mtx);
4978 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4979 device->target->generation++;
4980 mtx_unlock(&bus->eb_mtx);
4982 /* Release our slot in the devq */
4983 devq = bus->sim->devq;
4984 mtx_lock(&devq->send_mtx);
4985 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4986 mtx_unlock(&devq->send_mtx);
4988 KASSERT(SLIST_EMPTY(&device->periphs),
4989 ("destroying device, but periphs list is not empty"));
4990 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4991 ("destroying device while still queued for ccbs"));
4993 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4994 callout_stop(&device->callout);
4996 xpt_release_target(device->target);
4998 cam_ccbq_fini(&device->ccbq);
5000 * Free allocated memory. free(9) does nothing if the
5001 * supplied pointer is NULL, so it is safe to call without
5004 free(device->supported_vpds, M_CAMXPT);
5005 free(device->device_id, M_CAMXPT);
5006 free(device->ext_inq, M_CAMXPT);
5007 free(device->physpath, M_CAMXPT);
5008 free(device->rcap_buf, M_CAMXPT);
5009 free(device->serial_num, M_CAMXPT);
5010 free(device->nvme_data, M_CAMXPT);
5011 free(device->nvme_cdata, M_CAMXPT);
5012 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
5016 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5022 mtx_lock(&dev->sim->devq->send_mtx);
5023 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5024 mtx_unlock(&dev->sim->devq->send_mtx);
5025 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5026 || (dev->inq_flags & SID_CmdQue) != 0)
5027 dev->tag_saved_openings = newopenings;
5031 static struct cam_eb *
5032 xpt_find_bus(path_id_t path_id)
5037 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5039 bus = TAILQ_NEXT(bus, links)) {
5040 if (bus->path_id == path_id) {
5049 static struct cam_et *
5050 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5052 struct cam_et *target;
5054 mtx_assert(&bus->eb_mtx, MA_OWNED);
5055 for (target = TAILQ_FIRST(&bus->et_entries);
5057 target = TAILQ_NEXT(target, links)) {
5058 if (target->target_id == target_id) {
5066 static struct cam_ed *
5067 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5069 struct cam_ed *device;
5071 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
5072 for (device = TAILQ_FIRST(&target->ed_entries);
5074 device = TAILQ_NEXT(device, links)) {
5075 if (device->lun_id == lun_id) {
5084 xpt_start_tags(struct cam_path *path)
5086 struct ccb_relsim crs;
5087 struct cam_ed *device;
5088 struct cam_sim *sim;
5091 device = path->device;
5092 sim = path->bus->sim;
5093 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5094 xpt_freeze_devq(path, /*count*/1);
5095 device->inq_flags |= SID_CmdQue;
5096 if (device->tag_saved_openings != 0)
5097 newopenings = device->tag_saved_openings;
5099 newopenings = min(device->maxtags,
5100 sim->max_tagged_dev_openings);
5101 xpt_dev_ccbq_resize(path, newopenings);
5102 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5103 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5104 crs.ccb_h.func_code = XPT_REL_SIMQ;
5105 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5107 = crs.release_timeout
5110 xpt_action((union ccb *)&crs);
5114 xpt_stop_tags(struct cam_path *path)
5116 struct ccb_relsim crs;
5117 struct cam_ed *device;
5118 struct cam_sim *sim;
5120 device = path->device;
5121 sim = path->bus->sim;
5122 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5123 device->tag_delay_count = 0;
5124 xpt_freeze_devq(path, /*count*/1);
5125 device->inq_flags &= ~SID_CmdQue;
5126 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5127 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5128 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5129 crs.ccb_h.func_code = XPT_REL_SIMQ;
5130 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5132 = crs.release_timeout
5135 xpt_action((union ccb *)&crs);
5139 * Assume all possible buses are detected by this time, so allow boot
5140 * as soon as they all are scanned.
5143 xpt_boot_delay(void *arg)
5150 * Now that all config hooks have completed, start boot_delay timer,
5151 * waiting for possibly still undetected buses (USB) to appear.
5154 xpt_ch_done(void *arg)
5157 callout_init(&xsoftc.boot_callout, 1);
5158 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5159 xpt_boot_delay, NULL, 0);
5161 SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5164 * Now that interrupts are enabled, go find our devices
5167 xpt_config(void *arg)
5169 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5170 printf("xpt_config: failed to create taskqueue thread.\n");
5172 /* Setup debugging path */
5173 if (cam_dflags != CAM_DEBUG_NONE) {
5174 if (xpt_create_path(&cam_dpath, NULL,
5175 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5176 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5177 printf("xpt_config: xpt_create_path() failed for debug"
5178 " target %d:%d:%d, debugging disabled\n",
5179 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5180 cam_dflags = CAM_DEBUG_NONE;
5185 periphdriver_init(1);
5188 /* Fire up rescan thread. */
5189 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5190 "cam", "scanner")) {
5191 printf("xpt_config: failed to create rescan thread.\n");
5196 xpt_hold_boot_locked(void)
5199 if (xsoftc.buses_to_config++ == 0)
5200 root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5208 xpt_hold_boot_locked();
5213 xpt_release_boot(void)
5217 if (--xsoftc.buses_to_config == 0) {
5218 if (xsoftc.buses_config_done == 0) {
5219 xsoftc.buses_config_done = 1;
5220 xsoftc.buses_to_config++;
5221 TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5223 taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
5225 root_mount_rel(&xsoftc.xpt_rootmount);
5231 * If the given device only has one peripheral attached to it, and if that
5232 * peripheral is the passthrough driver, announce it. This insures that the
5233 * user sees some sort of announcement for every peripheral in their system.
5236 xptpassannouncefunc(struct cam_ed *device, void *arg)
5238 struct cam_periph *periph;
5241 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5242 periph = SLIST_NEXT(periph, periph_links), i++);
5244 periph = SLIST_FIRST(&device->periphs);
5246 && (strncmp(periph->periph_name, "pass", 4) == 0))
5247 xpt_announce_periph(periph, NULL);
5253 xpt_finishconfig_task(void *context, int pending)
5256 periphdriver_init(2);
5258 * Check for devices with no "standard" peripheral driver
5259 * attached. For any devices like that, announce the
5260 * passthrough driver so the user will see something.
5263 xpt_for_all_devices(xptpassannouncefunc, NULL);
5269 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5270 struct cam_path *path)
5272 struct ccb_setasync csa;
5277 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5278 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5279 if (status != CAM_REQ_CMP)
5281 xpt_path_lock(path);
5285 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5286 csa.ccb_h.func_code = XPT_SASYNC_CB;
5287 csa.event_enable = event;
5288 csa.callback = cbfunc;
5289 csa.callback_arg = cbarg;
5290 xpt_action((union ccb *)&csa);
5291 status = csa.ccb_h.status;
5293 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5294 ("xpt_register_async: func %p\n", cbfunc));
5297 xpt_path_unlock(path);
5298 xpt_free_path(path);
5301 if ((status == CAM_REQ_CMP) &&
5302 (csa.event_enable & AC_FOUND_DEVICE)) {
5304 * Get this peripheral up to date with all
5305 * the currently existing devices.
5307 xpt_for_all_devices(xptsetasyncfunc, &csa);
5309 if ((status == CAM_REQ_CMP) &&
5310 (csa.event_enable & AC_PATH_REGISTERED)) {
5312 * Get this peripheral up to date with all
5313 * the currently existing buses.
5315 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5322 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5324 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5326 switch (work_ccb->ccb_h.func_code) {
5327 /* Common cases first */
5328 case XPT_PATH_INQ: /* Path routing inquiry */
5330 struct ccb_pathinq *cpi;
5332 cpi = &work_ccb->cpi;
5333 cpi->version_num = 1; /* XXX??? */
5334 cpi->hba_inquiry = 0;
5335 cpi->target_sprt = 0;
5337 cpi->hba_eng_cnt = 0;
5338 cpi->max_target = 0;
5340 cpi->initiator_id = 0;
5341 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5342 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5343 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5344 cpi->unit_number = sim->unit_number;
5345 cpi->bus_id = sim->bus_id;
5346 cpi->base_transfer_speed = 0;
5347 cpi->protocol = PROTO_UNSPECIFIED;
5348 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5349 cpi->transport = XPORT_UNSPECIFIED;
5350 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5351 cpi->ccb_h.status = CAM_REQ_CMP;
5356 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5363 * The xpt as a "controller" has no interrupt sources, so polling
5367 xptpoll(struct cam_sim *sim)
5372 xpt_lock_buses(void)
5374 mtx_lock(&xsoftc.xpt_topo_lock);
5378 xpt_unlock_buses(void)
5380 mtx_unlock(&xsoftc.xpt_topo_lock);
5384 xpt_path_mtx(struct cam_path *path)
5387 return (&path->device->device_mtx);
5391 xpt_done_process(struct ccb_hdr *ccb_h)
5393 struct cam_sim *sim = NULL;
5394 struct cam_devq *devq = NULL;
5395 struct mtx *mtx = NULL;
5397 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5398 struct ccb_scsiio *csio;
5400 if (ccb_h->func_code == XPT_SCSI_IO) {
5401 csio = &((union ccb *)ccb_h)->csio;
5402 if (csio->bio != NULL)
5403 biotrack(csio->bio, __func__);
5407 if (ccb_h->flags & CAM_HIGH_POWER) {
5408 struct highpowerlist *hphead;
5409 struct cam_ed *device;
5411 mtx_lock(&xsoftc.xpt_highpower_lock);
5412 hphead = &xsoftc.highpowerq;
5414 device = STAILQ_FIRST(hphead);
5417 * Increment the count since this command is done.
5419 xsoftc.num_highpower++;
5422 * Any high powered commands queued up?
5424 if (device != NULL) {
5426 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5427 mtx_unlock(&xsoftc.xpt_highpower_lock);
5429 mtx_lock(&device->sim->devq->send_mtx);
5430 xpt_release_devq_device(device,
5431 /*count*/1, /*runqueue*/TRUE);
5432 mtx_unlock(&device->sim->devq->send_mtx);
5434 mtx_unlock(&xsoftc.xpt_highpower_lock);
5438 * Insulate against a race where the periph is destroyed but CCBs are
5439 * still not all processed. This shouldn't happen, but allows us better
5440 * bug diagnostic when it does.
5442 if (ccb_h->path->bus)
5443 sim = ccb_h->path->bus->sim;
5445 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5446 KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5447 xpt_release_simq(sim, /*run_queue*/FALSE);
5448 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5451 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5452 && (ccb_h->status & CAM_DEV_QFRZN)) {
5453 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5454 ccb_h->status &= ~CAM_DEV_QFRZN;
5457 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5458 struct cam_ed *dev = ccb_h->path->device;
5462 KASSERT(devq, ("Periph disappeared with request pending."));
5464 mtx_lock(&devq->send_mtx);
5465 devq->send_active--;
5466 devq->send_openings++;
5467 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5469 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5470 && (dev->ccbq.dev_active == 0))) {
5471 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5472 xpt_release_devq_device(dev, /*count*/1,
5473 /*run_queue*/FALSE);
5476 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5477 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5478 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5479 xpt_release_devq_device(dev, /*count*/1,
5480 /*run_queue*/FALSE);
5483 if (!device_is_queued(dev))
5484 (void)xpt_schedule_devq(devq, dev);
5486 mtx_unlock(&devq->send_mtx);
5488 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5489 mtx = xpt_path_mtx(ccb_h->path);
5492 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5493 && (--dev->tag_delay_count == 0))
5494 xpt_start_tags(ccb_h->path);
5498 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5500 mtx = xpt_path_mtx(ccb_h->path);
5510 /* Call the peripheral driver's callback */
5511 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5512 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5518 xpt_done_td(void *arg)
5520 struct cam_doneq *queue = arg;
5521 struct ccb_hdr *ccb_h;
5522 STAILQ_HEAD(, ccb_hdr) doneq;
5524 STAILQ_INIT(&doneq);
5525 mtx_lock(&queue->cam_doneq_mtx);
5527 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5528 queue->cam_doneq_sleep = 1;
5529 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5531 queue->cam_doneq_sleep = 0;
5533 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5534 mtx_unlock(&queue->cam_doneq_mtx);
5536 THREAD_NO_SLEEPING();
5537 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5538 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5539 xpt_done_process(ccb_h);
5541 THREAD_SLEEPING_OK();
5543 mtx_lock(&queue->cam_doneq_mtx);
5548 camisr_runqueue(void)
5550 struct ccb_hdr *ccb_h;
5551 struct cam_doneq *queue;
5554 /* Process global queues. */
5555 for (i = 0; i < cam_num_doneqs; i++) {
5556 queue = &cam_doneqs[i];
5557 mtx_lock(&queue->cam_doneq_mtx);
5558 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5559 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5560 mtx_unlock(&queue->cam_doneq_mtx);
5561 xpt_done_process(ccb_h);
5562 mtx_lock(&queue->cam_doneq_mtx);
5564 mtx_unlock(&queue->cam_doneq_mtx);
5574 static struct kv map[] = {
5575 { XPT_NOOP, "XPT_NOOP" },
5576 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5577 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5578 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5579 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5580 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5581 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5582 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5583 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5584 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5585 { XPT_DEBUG, "XPT_DEBUG" },
5586 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5587 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5588 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5589 { XPT_ASYNC, "XPT_ASYNC" },
5590 { XPT_ABORT, "XPT_ABORT" },
5591 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5592 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5593 { XPT_TERM_IO, "XPT_TERM_IO" },
5594 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5595 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5596 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5597 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5598 { XPT_ATA_IO, "XPT_ATA_IO" },
5599 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5600 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5601 { XPT_NVME_IO, "XPT_NVME_IO" },
5602 { XPT_MMC_IO, "XPT_MMC_IO" },
5603 { XPT_SMP_IO, "XPT_SMP_IO" },
5604 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5605 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5606 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5607 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5608 { XPT_EN_LUN, "XPT_EN_LUN" },
5609 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5610 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5611 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5612 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5613 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5614 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5615 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5620 xpt_action_name(uint32_t action)
5622 static char buffer[32]; /* Only for unknown messages -- racy */
5623 struct kv *walker = map;
5625 while (walker->name != NULL) {
5626 if (walker->v == action)
5627 return (walker->name);
5631 snprintf(buffer, sizeof(buffer), "%#x", action);