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;
2722 union ccb* abort_ccb;
2724 abort_ccb = start_ccb->cab.abort_ccb;
2725 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2726 struct cam_ed *device;
2727 struct cam_devq *devq;
2729 device = abort_ccb->ccb_h.path->device;
2730 devq = device->sim->devq;
2732 mtx_lock(&devq->send_mtx);
2733 if (abort_ccb->ccb_h.pinfo.index > 0) {
2734 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2735 abort_ccb->ccb_h.status =
2736 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2737 xpt_freeze_devq_device(device, 1);
2738 mtx_unlock(&devq->send_mtx);
2739 xpt_done(abort_ccb);
2740 start_ccb->ccb_h.status = CAM_REQ_CMP;
2743 mtx_unlock(&devq->send_mtx);
2745 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2746 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2748 * We've caught this ccb en route to
2749 * the SIM. Flag it for abort and the
2750 * SIM will do so just before starting
2751 * real work on the CCB.
2753 abort_ccb->ccb_h.status =
2754 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2755 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2756 start_ccb->ccb_h.status = CAM_REQ_CMP;
2760 if (XPT_FC_IS_QUEUED(abort_ccb)
2761 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2763 * It's already completed but waiting
2764 * for our SWI to get to it.
2766 start_ccb->ccb_h.status = CAM_UA_ABORT;
2770 * If we weren't able to take care of the abort request
2771 * in the XPT, pass the request down to the SIM for processing.
2775 case XPT_ACCEPT_TARGET_IO:
2777 case XPT_IMMED_NOTIFY:
2778 case XPT_NOTIFY_ACK:
2780 case XPT_IMMEDIATE_NOTIFY:
2781 case XPT_NOTIFY_ACKNOWLEDGE:
2782 case XPT_GET_SIM_KNOB_OLD:
2783 case XPT_GET_SIM_KNOB:
2784 case XPT_SET_SIM_KNOB:
2785 case XPT_GET_TRAN_SETTINGS:
2786 case XPT_SET_TRAN_SETTINGS:
2789 sim = path->bus->sim;
2791 if (mtx && !mtx_owned(mtx))
2796 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2797 ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2798 (*(sim->sim_action))(sim, start_ccb);
2799 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2800 ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2804 case XPT_PATH_STATS:
2805 start_ccb->cpis.last_reset = path->bus->last_reset;
2806 start_ccb->ccb_h.status = CAM_REQ_CMP;
2813 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2814 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2816 struct ccb_getdev *cgd;
2818 cgd = &start_ccb->cgd;
2819 cgd->protocol = dev->protocol;
2820 cgd->inq_data = dev->inq_data;
2821 cgd->ident_data = dev->ident_data;
2822 cgd->inq_flags = dev->inq_flags;
2823 cgd->ccb_h.status = CAM_REQ_CMP;
2824 cgd->serial_num_len = dev->serial_num_len;
2825 if ((dev->serial_num_len > 0)
2826 && (dev->serial_num != NULL))
2827 bcopy(dev->serial_num, cgd->serial_num,
2828 dev->serial_num_len);
2832 case XPT_GDEV_STATS:
2834 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2835 struct cam_ed *dev = path->device;
2836 struct cam_eb *bus = path->bus;
2837 struct cam_et *tar = path->target;
2838 struct cam_devq *devq = bus->sim->devq;
2840 mtx_lock(&devq->send_mtx);
2841 cgds->dev_openings = dev->ccbq.dev_openings;
2842 cgds->dev_active = dev->ccbq.dev_active;
2843 cgds->allocated = dev->ccbq.allocated;
2844 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2845 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2846 cgds->last_reset = tar->last_reset;
2847 cgds->maxtags = dev->maxtags;
2848 cgds->mintags = dev->mintags;
2849 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2850 cgds->last_reset = bus->last_reset;
2851 mtx_unlock(&devq->send_mtx);
2852 cgds->ccb_h.status = CAM_REQ_CMP;
2857 struct cam_periph *nperiph;
2858 struct periph_list *periph_head;
2859 struct ccb_getdevlist *cgdl;
2861 struct cam_ed *device;
2868 * Don't want anyone mucking with our data.
2870 device = path->device;
2871 periph_head = &device->periphs;
2872 cgdl = &start_ccb->cgdl;
2875 * Check and see if the list has changed since the user
2876 * last requested a list member. If so, tell them that the
2877 * list has changed, and therefore they need to start over
2878 * from the beginning.
2880 if ((cgdl->index != 0) &&
2881 (cgdl->generation != device->generation)) {
2882 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2887 * Traverse the list of peripherals and attempt to find
2888 * the requested peripheral.
2890 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2891 (nperiph != NULL) && (i <= cgdl->index);
2892 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2893 if (i == cgdl->index) {
2894 strlcpy(cgdl->periph_name,
2895 nperiph->periph_name,
2896 sizeof(cgdl->periph_name));
2897 cgdl->unit_number = nperiph->unit_number;
2902 cgdl->status = CAM_GDEVLIST_ERROR;
2906 if (nperiph == NULL)
2907 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2909 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2912 cgdl->generation = device->generation;
2914 cgdl->ccb_h.status = CAM_REQ_CMP;
2919 dev_pos_type position_type;
2920 struct ccb_dev_match *cdm;
2922 cdm = &start_ccb->cdm;
2925 * There are two ways of getting at information in the EDT.
2926 * The first way is via the primary EDT tree. It starts
2927 * with a list of buses, then a list of targets on a bus,
2928 * then devices/luns on a target, and then peripherals on a
2929 * device/lun. The "other" way is by the peripheral driver
2930 * lists. The peripheral driver lists are organized by
2931 * peripheral driver. (obviously) So it makes sense to
2932 * use the peripheral driver list if the user is looking
2933 * for something like "da1", or all "da" devices. If the
2934 * user is looking for something on a particular bus/target
2935 * or lun, it's generally better to go through the EDT tree.
2938 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2939 position_type = cdm->pos.position_type;
2943 position_type = CAM_DEV_POS_NONE;
2945 for (i = 0; i < cdm->num_patterns; i++) {
2946 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2947 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2948 position_type = CAM_DEV_POS_EDT;
2953 if (cdm->num_patterns == 0)
2954 position_type = CAM_DEV_POS_EDT;
2955 else if (position_type == CAM_DEV_POS_NONE)
2956 position_type = CAM_DEV_POS_PDRV;
2959 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2960 case CAM_DEV_POS_EDT:
2963 case CAM_DEV_POS_PDRV:
2964 xptperiphlistmatch(cdm);
2967 cdm->status = CAM_DEV_MATCH_ERROR;
2971 if (cdm->status == CAM_DEV_MATCH_ERROR)
2972 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2974 start_ccb->ccb_h.status = CAM_REQ_CMP;
2980 struct ccb_setasync *csa;
2981 struct async_node *cur_entry;
2982 struct async_list *async_head;
2985 csa = &start_ccb->csa;
2986 added = csa->event_enable;
2987 async_head = &path->device->asyncs;
2990 * If there is already an entry for us, simply
2993 cur_entry = SLIST_FIRST(async_head);
2994 while (cur_entry != NULL) {
2995 if ((cur_entry->callback_arg == csa->callback_arg)
2996 && (cur_entry->callback == csa->callback))
2998 cur_entry = SLIST_NEXT(cur_entry, links);
3001 if (cur_entry != NULL) {
3003 * If the request has no flags set,
3006 added &= ~cur_entry->event_enable;
3007 if (csa->event_enable == 0) {
3008 SLIST_REMOVE(async_head, cur_entry,
3010 xpt_release_device(path->device);
3011 free(cur_entry, M_CAMXPT);
3013 cur_entry->event_enable = csa->event_enable;
3015 csa->event_enable = added;
3017 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3019 if (cur_entry == NULL) {
3020 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3023 cur_entry->event_enable = csa->event_enable;
3024 cur_entry->event_lock = (path->bus->sim->mtx &&
3025 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
3026 cur_entry->callback_arg = csa->callback_arg;
3027 cur_entry->callback = csa->callback;
3028 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3029 xpt_acquire_device(path->device);
3031 start_ccb->ccb_h.status = CAM_REQ_CMP;
3036 struct ccb_relsim *crs;
3039 crs = &start_ccb->crs;
3043 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3047 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3049 /* Don't ever go below one opening */
3050 if (crs->openings > 0) {
3051 xpt_dev_ccbq_resize(path, crs->openings);
3054 "number of openings is now %d\n",
3060 mtx_lock(&dev->sim->devq->send_mtx);
3061 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3063 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3066 * Just extend the old timeout and decrement
3067 * the freeze count so that a single timeout
3068 * is sufficient for releasing the queue.
3070 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3071 callout_stop(&dev->callout);
3074 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3077 callout_reset_sbt(&dev->callout,
3078 SBT_1MS * crs->release_timeout, 0,
3079 xpt_release_devq_timeout, dev, 0);
3081 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3085 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3087 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3089 * Decrement the freeze count so that a single
3090 * completion is still sufficient to unfreeze
3093 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3096 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3097 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3101 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3103 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3104 || (dev->ccbq.dev_active == 0)) {
3106 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3109 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3110 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3113 mtx_unlock(&dev->sim->devq->send_mtx);
3115 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3116 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3117 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3118 start_ccb->ccb_h.status = CAM_REQ_CMP;
3122 struct cam_path *oldpath;
3124 /* Check that all request bits are supported. */
3125 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3126 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3130 cam_dflags = CAM_DEBUG_NONE;
3131 if (cam_dpath != NULL) {
3132 oldpath = cam_dpath;
3134 xpt_free_path(oldpath);
3136 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3137 if (xpt_create_path(&cam_dpath, NULL,
3138 start_ccb->ccb_h.path_id,
3139 start_ccb->ccb_h.target_id,
3140 start_ccb->ccb_h.target_lun) !=
3142 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3144 cam_dflags = start_ccb->cdbg.flags;
3145 start_ccb->ccb_h.status = CAM_REQ_CMP;
3146 xpt_print(cam_dpath, "debugging flags now %x\n",
3150 start_ccb->ccb_h.status = CAM_REQ_CMP;
3154 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3155 xpt_freeze_devq(path, 1);
3156 start_ccb->ccb_h.status = CAM_REQ_CMP;
3158 case XPT_REPROBE_LUN:
3159 xpt_async(AC_INQ_CHANGED, path, NULL);
3160 start_ccb->ccb_h.status = CAM_REQ_CMP;
3161 xpt_done(start_ccb);
3168 xpt_print(start_ccb->ccb_h.path,
3169 "%s: CCB type %#x %s not supported\n", __func__,
3170 start_ccb->ccb_h.func_code,
3171 xpt_action_name(start_ccb->ccb_h.func_code));
3172 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3173 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3174 xpt_done(start_ccb);
3178 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3179 ("xpt_action_default: func= %#x %s status %#x\n",
3180 start_ccb->ccb_h.func_code,
3181 xpt_action_name(start_ccb->ccb_h.func_code),
3182 start_ccb->ccb_h.status));
3186 * Call the sim poll routine to allow the sim to complete
3187 * any inflight requests, then call camisr_runqueue to
3188 * complete any CCB that the polling completed.
3191 xpt_sim_poll(struct cam_sim *sim)
3198 (*(sim->sim_poll))(sim);
3205 xpt_poll_setup(union ccb *start_ccb)
3208 struct cam_sim *sim;
3209 struct cam_devq *devq;
3212 timeout = start_ccb->ccb_h.timeout * 10;
3213 sim = start_ccb->ccb_h.path->bus->sim;
3215 dev = start_ccb->ccb_h.path->device;
3218 * Steal an opening so that no other queued requests
3219 * can get it before us while we simulate interrupts.
3221 mtx_lock(&devq->send_mtx);
3222 dev->ccbq.dev_openings--;
3223 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3225 mtx_unlock(&devq->send_mtx);
3228 mtx_lock(&devq->send_mtx);
3230 dev->ccbq.dev_openings++;
3231 mtx_unlock(&devq->send_mtx);
3237 xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3240 while (--timeout > 0) {
3241 xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
3242 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3250 * XXX Is it worth adding a sim_timeout entry
3251 * point so we can attempt recovery? If
3252 * this is only used for dumps, I don't think
3255 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3260 xpt_polled_action(union ccb *start_ccb)
3265 timeout = start_ccb->ccb_h.timeout * 10;
3266 dev = start_ccb->ccb_h.path->device;
3268 mtx_unlock(&dev->device_mtx);
3270 timeout = xpt_poll_setup(start_ccb);
3272 xpt_action(start_ccb);
3273 xpt_pollwait(start_ccb, timeout);
3275 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3278 mtx_lock(&dev->device_mtx);
3282 * Schedule a peripheral driver to receive a ccb when its
3283 * target device has space for more transactions.
3286 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3289 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3290 cam_periph_assert(periph, MA_OWNED);
3291 if (new_priority < periph->scheduled_priority) {
3292 periph->scheduled_priority = new_priority;
3293 xpt_run_allocq(periph, 0);
3299 * Schedule a device to run on a given queue.
3300 * If the device was inserted as a new entry on the queue,
3301 * return 1 meaning the device queue should be run. If we
3302 * were already queued, implying someone else has already
3303 * started the queue, return 0 so the caller doesn't attempt
3307 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3308 u_int32_t new_priority)
3311 u_int32_t old_priority;
3313 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3316 old_priority = pinfo->priority;
3319 * Are we already queued?
3321 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3322 /* Simply reorder based on new priority */
3323 if (new_priority < old_priority) {
3324 camq_change_priority(queue, pinfo->index,
3326 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3327 ("changed priority to %d\n",
3333 /* New entry on the queue */
3334 if (new_priority < old_priority)
3335 pinfo->priority = new_priority;
3337 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3338 ("Inserting onto queue\n"));
3339 pinfo->generation = ++queue->generation;
3340 camq_insert(queue, pinfo);
3347 xpt_run_allocq_task(void *context, int pending)
3349 struct cam_periph *periph = context;
3351 cam_periph_lock(periph);
3352 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3353 xpt_run_allocq(periph, 1);
3354 cam_periph_unlock(periph);
3355 cam_periph_release(periph);
3359 xpt_run_allocq(struct cam_periph *periph, int sleep)
3361 struct cam_ed *device;
3365 cam_periph_assert(periph, MA_OWNED);
3366 if (periph->periph_allocating)
3368 cam_periph_doacquire(periph);
3369 periph->periph_allocating = 1;
3370 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3371 device = periph->path->device;
3374 while ((prio = min(periph->scheduled_priority,
3375 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3376 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3377 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3380 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3382 ccb = xpt_get_ccb(periph);
3385 if (periph->flags & CAM_PERIPH_RUN_TASK)
3387 cam_periph_doacquire(periph);
3388 periph->flags |= CAM_PERIPH_RUN_TASK;
3389 taskqueue_enqueue(xsoftc.xpt_taskq,
3390 &periph->periph_run_task);
3393 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3394 if (prio == periph->immediate_priority) {
3395 periph->immediate_priority = CAM_PRIORITY_NONE;
3396 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3397 ("waking cam_periph_getccb()\n"));
3398 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3400 wakeup(&periph->ccb_list);
3402 periph->scheduled_priority = CAM_PRIORITY_NONE;
3403 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3404 ("calling periph_start()\n"));
3405 periph->periph_start(periph, ccb);
3410 xpt_release_ccb(ccb);
3411 periph->periph_allocating = 0;
3412 cam_periph_release_locked(periph);
3416 xpt_run_devq(struct cam_devq *devq)
3420 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3422 devq->send_queue.qfrozen_cnt++;
3423 while ((devq->send_queue.entries > 0)
3424 && (devq->send_openings > 0)
3425 && (devq->send_queue.qfrozen_cnt <= 1)) {
3426 struct cam_ed *device;
3427 union ccb *work_ccb;
3428 struct cam_sim *sim;
3429 struct xpt_proto *proto;
3431 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3433 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3434 ("running device %p\n", device));
3436 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3437 if (work_ccb == NULL) {
3438 printf("device on run queue with no ccbs???\n");
3442 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3444 mtx_lock(&xsoftc.xpt_highpower_lock);
3445 if (xsoftc.num_highpower <= 0) {
3447 * We got a high power command, but we
3448 * don't have any available slots. Freeze
3449 * the device queue until we have a slot
3452 xpt_freeze_devq_device(device, 1);
3453 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3456 mtx_unlock(&xsoftc.xpt_highpower_lock);
3460 * Consume a high power slot while
3463 xsoftc.num_highpower--;
3465 mtx_unlock(&xsoftc.xpt_highpower_lock);
3467 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3468 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3469 devq->send_openings--;
3470 devq->send_active++;
3471 xpt_schedule_devq(devq, device);
3472 mtx_unlock(&devq->send_mtx);
3474 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3476 * The client wants to freeze the queue
3477 * after this CCB is sent.
3479 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3482 /* In Target mode, the peripheral driver knows best... */
3483 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3484 if ((device->inq_flags & SID_CmdQue) != 0
3485 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3486 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3489 * Clear this in case of a retried CCB that
3490 * failed due to a rejected tag.
3492 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3495 KASSERT(device == work_ccb->ccb_h.path->device,
3496 ("device (%p) / path->device (%p) mismatch",
3497 device, work_ccb->ccb_h.path->device));
3498 proto = xpt_proto_find(device->protocol);
3499 if (proto && proto->ops->debug_out)
3500 proto->ops->debug_out(work_ccb);
3503 * Device queues can be shared among multiple SIM instances
3504 * that reside on different buses. Use the SIM from the
3505 * queued device, rather than the one from the calling bus.
3509 if (mtx && !mtx_owned(mtx))
3513 work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3514 (*(sim->sim_action))(sim, work_ccb);
3517 mtx_lock(&devq->send_mtx);
3519 devq->send_queue.qfrozen_cnt--;
3523 * This function merges stuff from the slave ccb into the master ccb, while
3524 * keeping important fields in the master ccb constant.
3527 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3531 * Pull fields that are valid for peripheral drivers to set
3532 * into the master CCB along with the CCB "payload".
3534 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3535 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3536 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3537 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3538 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3539 sizeof(union ccb) - sizeof(struct ccb_hdr));
3543 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3544 u_int32_t priority, u_int32_t flags)
3547 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3548 ccb_h->pinfo.priority = priority;
3550 ccb_h->path_id = path->bus->path_id;
3552 ccb_h->target_id = path->target->target_id;
3554 ccb_h->target_id = CAM_TARGET_WILDCARD;
3556 ccb_h->target_lun = path->device->lun_id;
3557 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3559 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3561 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3562 ccb_h->flags = flags;
3567 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3569 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3572 /* Path manipulation functions */
3574 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3575 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3577 struct cam_path *path;
3580 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3583 status = CAM_RESRC_UNAVAIL;
3586 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3587 if (status != CAM_REQ_CMP) {
3588 free(path, M_CAMPATH);
3591 *new_path_ptr = path;
3596 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3597 struct cam_periph *periph, path_id_t path_id,
3598 target_id_t target_id, lun_id_t lun_id)
3601 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3606 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3607 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3610 struct cam_et *target;
3611 struct cam_ed *device;
3614 status = CAM_REQ_CMP; /* Completed without error */
3615 target = NULL; /* Wildcarded */
3616 device = NULL; /* Wildcarded */
3619 * We will potentially modify the EDT, so block interrupts
3620 * that may attempt to create cam paths.
3622 bus = xpt_find_bus(path_id);
3624 status = CAM_PATH_INVALID;
3627 mtx_lock(&bus->eb_mtx);
3628 target = xpt_find_target(bus, target_id);
3629 if (target == NULL) {
3631 struct cam_et *new_target;
3633 new_target = xpt_alloc_target(bus, target_id);
3634 if (new_target == NULL) {
3635 status = CAM_RESRC_UNAVAIL;
3637 target = new_target;
3641 if (target != NULL) {
3642 device = xpt_find_device(target, lun_id);
3643 if (device == NULL) {
3645 struct cam_ed *new_device;
3648 (*(bus->xport->ops->alloc_device))(bus,
3651 if (new_device == NULL) {
3652 status = CAM_RESRC_UNAVAIL;
3654 device = new_device;
3658 mtx_unlock(&bus->eb_mtx);
3662 * Only touch the user's data if we are successful.
3664 if (status == CAM_REQ_CMP) {
3665 new_path->periph = perph;
3666 new_path->bus = bus;
3667 new_path->target = target;
3668 new_path->device = device;
3669 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3672 xpt_release_device(device);
3674 xpt_release_target(target);
3676 xpt_release_bus(bus);
3682 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3684 struct cam_path *new_path;
3686 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3687 if (new_path == NULL)
3688 return(CAM_RESRC_UNAVAIL);
3689 xpt_copy_path(new_path, path);
3690 *new_path_ptr = new_path;
3691 return (CAM_REQ_CMP);
3695 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3699 if (path->bus != NULL)
3700 xpt_acquire_bus(path->bus);
3701 if (path->target != NULL)
3702 xpt_acquire_target(path->target);
3703 if (path->device != NULL)
3704 xpt_acquire_device(path->device);
3708 xpt_release_path(struct cam_path *path)
3710 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3711 if (path->device != NULL) {
3712 xpt_release_device(path->device);
3713 path->device = NULL;
3715 if (path->target != NULL) {
3716 xpt_release_target(path->target);
3717 path->target = NULL;
3719 if (path->bus != NULL) {
3720 xpt_release_bus(path->bus);
3726 xpt_free_path(struct cam_path *path)
3729 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3730 xpt_release_path(path);
3731 free(path, M_CAMPATH);
3735 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3736 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3742 *bus_ref = path->bus->refcount;
3748 *periph_ref = path->periph->refcount;
3755 *target_ref = path->target->refcount;
3761 *device_ref = path->device->refcount;
3768 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3769 * in path1, 2 for match with wildcards in path2.
3772 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3776 if (path1->bus != path2->bus) {
3777 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3779 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3784 if (path1->target != path2->target) {
3785 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3788 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3793 if (path1->device != path2->device) {
3794 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3797 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3806 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3810 if (path->bus != dev->target->bus) {
3811 if (path->bus->path_id == CAM_BUS_WILDCARD)
3813 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3818 if (path->target != dev->target) {
3819 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3822 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3827 if (path->device != dev) {
3828 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3831 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3840 xpt_print_path(struct cam_path *path)
3843 char buffer[XPT_PRINT_LEN];
3845 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3846 xpt_path_sbuf(path, &sb);
3848 printf("%s", sbuf_data(&sb));
3853 xpt_print_device(struct cam_ed *device)
3857 printf("(nopath): ");
3859 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3860 device->sim->unit_number,
3861 device->sim->bus_id,
3862 device->target->target_id,
3863 (uintmax_t)device->lun_id);
3868 xpt_print(struct cam_path *path, const char *fmt, ...)
3872 char buffer[XPT_PRINT_LEN];
3874 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3876 xpt_path_sbuf(path, &sb);
3878 sbuf_vprintf(&sb, fmt, ap);
3882 printf("%s", sbuf_data(&sb));
3887 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3892 sbuf_new(&sb, str, str_len, 0);
3893 len = xpt_path_sbuf(path, &sb);
3899 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3903 sbuf_printf(sb, "(nopath): ");
3905 if (path->periph != NULL)
3906 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3907 path->periph->unit_number);
3909 sbuf_printf(sb, "(noperiph:");
3911 if (path->bus != NULL)
3912 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3913 path->bus->sim->unit_number,
3914 path->bus->sim->bus_id);
3916 sbuf_printf(sb, "nobus:");
3918 if (path->target != NULL)
3919 sbuf_printf(sb, "%d:", path->target->target_id);
3921 sbuf_printf(sb, "X:");
3923 if (path->device != NULL)
3924 sbuf_printf(sb, "%jx): ",
3925 (uintmax_t)path->device->lun_id);
3927 sbuf_printf(sb, "X): ");
3930 return(sbuf_len(sb));
3934 xpt_path_path_id(struct cam_path *path)
3936 return(path->bus->path_id);
3940 xpt_path_target_id(struct cam_path *path)
3942 if (path->target != NULL)
3943 return (path->target->target_id);
3945 return (CAM_TARGET_WILDCARD);
3949 xpt_path_lun_id(struct cam_path *path)
3951 if (path->device != NULL)
3952 return (path->device->lun_id);
3954 return (CAM_LUN_WILDCARD);
3958 xpt_path_sim(struct cam_path *path)
3961 return (path->bus->sim);
3965 xpt_path_periph(struct cam_path *path)
3968 return (path->periph);
3972 * Release a CAM control block for the caller. Remit the cost of the structure
3973 * to the device referenced by the path. If the this device had no 'credits'
3974 * and peripheral drivers have registered async callbacks for this notification
3978 xpt_release_ccb(union ccb *free_ccb)
3980 struct cam_ed *device;
3981 struct cam_periph *periph;
3983 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3984 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3985 device = free_ccb->ccb_h.path->device;
3986 periph = free_ccb->ccb_h.path->periph;
3988 xpt_free_ccb(free_ccb);
3989 periph->periph_allocated--;
3990 cam_ccbq_release_opening(&device->ccbq);
3991 xpt_run_allocq(periph, 0);
3994 /* Functions accessed by SIM drivers */
3996 static struct xpt_xport_ops xport_default_ops = {
3997 .alloc_device = xpt_alloc_device_default,
3998 .action = xpt_action_default,
3999 .async = xpt_dev_async_default,
4001 static struct xpt_xport xport_default = {
4002 .xport = XPORT_UNKNOWN,
4004 .ops = &xport_default_ops,
4007 CAM_XPT_XPORT(xport_default);
4010 * A sim structure, listing the SIM entry points and instance
4011 * identification info is passed to xpt_bus_register to hook the SIM
4012 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4013 * for this new bus and places it in the array of buses and assigns
4014 * it a path_id. The path_id may be influenced by "hard wiring"
4015 * information specified by the user. Once interrupt services are
4016 * available, the bus will be probed.
4019 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
4021 struct cam_eb *new_bus;
4022 struct cam_eb *old_bus;
4023 struct ccb_pathinq cpi;
4024 struct cam_path *path;
4028 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4029 M_CAMXPT, M_NOWAIT|M_ZERO);
4030 if (new_bus == NULL) {
4031 /* Couldn't satisfy request */
4032 return (CAM_RESRC_UNAVAIL);
4035 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4036 TAILQ_INIT(&new_bus->et_entries);
4039 timevalclear(&new_bus->last_reset);
4041 new_bus->refcount = 1; /* Held until a bus_deregister event */
4042 new_bus->generation = 0;
4045 sim->path_id = new_bus->path_id =
4046 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4047 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4048 while (old_bus != NULL
4049 && old_bus->path_id < new_bus->path_id)
4050 old_bus = TAILQ_NEXT(old_bus, links);
4051 if (old_bus != NULL)
4052 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4054 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4055 xsoftc.bus_generation++;
4059 * Set a default transport so that a PATH_INQ can be issued to
4060 * the SIM. This will then allow for probing and attaching of
4061 * a more appropriate transport.
4063 new_bus->xport = &xport_default;
4065 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
4066 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4067 if (status != CAM_REQ_CMP) {
4068 xpt_release_bus(new_bus);
4069 return (CAM_RESRC_UNAVAIL);
4072 xpt_path_inq(&cpi, path);
4074 if (cpi.ccb_h.status == CAM_REQ_CMP) {
4075 struct xpt_xport **xpt;
4077 SET_FOREACH(xpt, cam_xpt_xport_set) {
4078 if ((*xpt)->xport == cpi.transport) {
4079 new_bus->xport = *xpt;
4083 if (new_bus->xport == NULL) {
4085 "No transport found for %d\n", cpi.transport);
4086 xpt_release_bus(new_bus);
4087 free(path, M_CAMXPT);
4088 return (CAM_RESRC_UNAVAIL);
4092 /* Notify interested parties */
4093 if (sim->path_id != CAM_XPT_PATH_ID) {
4095 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4096 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4097 union ccb *scan_ccb;
4099 /* Initiate bus rescan. */
4100 scan_ccb = xpt_alloc_ccb_nowait();
4101 if (scan_ccb != NULL) {
4102 scan_ccb->ccb_h.path = path;
4103 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4104 scan_ccb->crcn.flags = 0;
4105 xpt_rescan(scan_ccb);
4108 "Can't allocate CCB to scan bus\n");
4109 xpt_free_path(path);
4112 xpt_free_path(path);
4114 xpt_free_path(path);
4115 return (CAM_SUCCESS);
4119 xpt_bus_deregister(path_id_t pathid)
4121 struct cam_path bus_path;
4124 status = xpt_compile_path(&bus_path, NULL, pathid,
4125 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4126 if (status != CAM_REQ_CMP)
4129 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4130 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4132 /* Release the reference count held while registered. */
4133 xpt_release_bus(bus_path.bus);
4134 xpt_release_path(&bus_path);
4136 return (CAM_REQ_CMP);
4140 xptnextfreepathid(void)
4146 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4148 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4150 /* Find an unoccupied pathid */
4151 while (bus != NULL && bus->path_id <= pathid) {
4152 if (bus->path_id == pathid)
4154 bus = TAILQ_NEXT(bus, links);
4158 * Ensure that this pathid is not reserved for
4159 * a bus that may be registered in the future.
4161 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4163 /* Start the search over */
4170 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4177 pathid = CAM_XPT_PATH_ID;
4178 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4179 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4182 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4183 if (strcmp(dname, "scbus")) {
4184 /* Avoid a bit of foot shooting. */
4187 if (dunit < 0) /* unwired?! */
4189 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4190 if (sim_bus == val) {
4194 } else if (sim_bus == 0) {
4195 /* Unspecified matches bus 0 */
4199 printf("Ambiguous scbus configuration for %s%d "
4200 "bus %d, cannot wire down. The kernel "
4201 "config entry for scbus%d should "
4202 "specify a controller bus.\n"
4203 "Scbus will be assigned dynamically.\n",
4204 sim_name, sim_unit, sim_bus, dunit);
4209 if (pathid == CAM_XPT_PATH_ID)
4210 pathid = xptnextfreepathid();
4215 xpt_async_string(u_int32_t async_code)
4218 switch (async_code) {
4219 case AC_BUS_RESET: return ("AC_BUS_RESET");
4220 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4221 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4222 case AC_SENT_BDR: return ("AC_SENT_BDR");
4223 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4224 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4225 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4226 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4227 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4228 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4229 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4230 case AC_CONTRACT: return ("AC_CONTRACT");
4231 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4232 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4234 return ("AC_UNKNOWN");
4238 xpt_async_size(u_int32_t async_code)
4241 switch (async_code) {
4242 case AC_BUS_RESET: return (0);
4243 case AC_UNSOL_RESEL: return (0);
4244 case AC_SCSI_AEN: return (0);
4245 case AC_SENT_BDR: return (0);
4246 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4247 case AC_PATH_DEREGISTERED: return (0);
4248 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4249 case AC_LOST_DEVICE: return (0);
4250 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4251 case AC_INQ_CHANGED: return (0);
4252 case AC_GETDEV_CHANGED: return (0);
4253 case AC_CONTRACT: return (sizeof(struct ac_contract));
4254 case AC_ADVINFO_CHANGED: return (-1);
4255 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4261 xpt_async_process_dev(struct cam_ed *device, void *arg)
4263 union ccb *ccb = arg;
4264 struct cam_path *path = ccb->ccb_h.path;
4265 void *async_arg = ccb->casync.async_arg_ptr;
4266 u_int32_t async_code = ccb->casync.async_code;
4269 if (path->device != device
4270 && path->device->lun_id != CAM_LUN_WILDCARD
4271 && device->lun_id != CAM_LUN_WILDCARD)
4275 * The async callback could free the device.
4276 * If it is a broadcast async, it doesn't hold
4277 * device reference, so take our own reference.
4279 xpt_acquire_device(device);
4282 * If async for specific device is to be delivered to
4283 * the wildcard client, take the specific device lock.
4284 * XXX: We may need a way for client to specify it.
4286 if ((device->lun_id == CAM_LUN_WILDCARD &&
4287 path->device->lun_id != CAM_LUN_WILDCARD) ||
4288 (device->target->target_id == CAM_TARGET_WILDCARD &&
4289 path->target->target_id != CAM_TARGET_WILDCARD) ||
4290 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4291 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4292 mtx_unlock(&device->device_mtx);
4293 xpt_path_lock(path);
4298 (*(device->target->bus->xport->ops->async))(async_code,
4299 device->target->bus, device->target, device, async_arg);
4300 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4303 xpt_path_unlock(path);
4304 mtx_lock(&device->device_mtx);
4306 xpt_release_device(device);
4311 xpt_async_process_tgt(struct cam_et *target, void *arg)
4313 union ccb *ccb = arg;
4314 struct cam_path *path = ccb->ccb_h.path;
4316 if (path->target != target
4317 && path->target->target_id != CAM_TARGET_WILDCARD
4318 && target->target_id != CAM_TARGET_WILDCARD)
4321 if (ccb->casync.async_code == AC_SENT_BDR) {
4322 /* Update our notion of when the last reset occurred */
4323 microtime(&target->last_reset);
4326 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4330 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4333 struct cam_path *path;
4335 u_int32_t async_code;
4337 path = ccb->ccb_h.path;
4338 async_code = ccb->casync.async_code;
4339 async_arg = ccb->casync.async_arg_ptr;
4340 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4341 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4344 if (async_code == AC_BUS_RESET) {
4345 /* Update our notion of when the last reset occurred */
4346 microtime(&bus->last_reset);
4349 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4352 * If this wasn't a fully wildcarded async, tell all
4353 * clients that want all async events.
4355 if (bus != xpt_periph->path->bus) {
4356 xpt_path_lock(xpt_periph->path);
4357 xpt_async_process_dev(xpt_periph->path->device, ccb);
4358 xpt_path_unlock(xpt_periph->path);
4361 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4362 xpt_release_devq(path, 1, TRUE);
4364 xpt_release_simq(path->bus->sim, TRUE);
4365 if (ccb->casync.async_arg_size > 0)
4366 free(async_arg, M_CAMXPT);
4367 xpt_free_path(path);
4372 xpt_async_bcast(struct async_list *async_head,
4373 u_int32_t async_code,
4374 struct cam_path *path, void *async_arg)
4376 struct async_node *cur_entry;
4379 cur_entry = SLIST_FIRST(async_head);
4380 while (cur_entry != NULL) {
4381 struct async_node *next_entry;
4383 * Grab the next list entry before we call the current
4384 * entry's callback. This is because the callback function
4385 * can delete its async callback entry.
4387 next_entry = SLIST_NEXT(cur_entry, links);
4388 if ((cur_entry->event_enable & async_code) != 0) {
4389 mtx = cur_entry->event_lock ?
4390 path->device->sim->mtx : NULL;
4393 cur_entry->callback(cur_entry->callback_arg,
4399 cur_entry = next_entry;
4404 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4409 ccb = xpt_alloc_ccb_nowait();
4411 xpt_print(path, "Can't allocate CCB to send %s\n",
4412 xpt_async_string(async_code));
4416 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4417 xpt_print(path, "Can't allocate path to send %s\n",
4418 xpt_async_string(async_code));
4422 ccb->ccb_h.path->periph = NULL;
4423 ccb->ccb_h.func_code = XPT_ASYNC;
4424 ccb->ccb_h.cbfcnp = xpt_async_process;
4425 ccb->ccb_h.flags |= CAM_UNLOCKED;
4426 ccb->casync.async_code = async_code;
4427 ccb->casync.async_arg_size = 0;
4428 size = xpt_async_size(async_code);
4429 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4430 ("xpt_async: func %#x %s aync_code %d %s\n",
4431 ccb->ccb_h.func_code,
4432 xpt_action_name(ccb->ccb_h.func_code),
4434 xpt_async_string(async_code)));
4435 if (size > 0 && async_arg != NULL) {
4436 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4437 if (ccb->casync.async_arg_ptr == NULL) {
4438 xpt_print(path, "Can't allocate argument to send %s\n",
4439 xpt_async_string(async_code));
4440 xpt_free_path(ccb->ccb_h.path);
4444 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4445 ccb->casync.async_arg_size = size;
4446 } else if (size < 0) {
4447 ccb->casync.async_arg_ptr = async_arg;
4448 ccb->casync.async_arg_size = size;
4450 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4451 xpt_freeze_devq(path, 1);
4453 xpt_freeze_simq(path->bus->sim, 1);
4458 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4459 struct cam_et *target, struct cam_ed *device,
4464 * We only need to handle events for real devices.
4466 if (target->target_id == CAM_TARGET_WILDCARD
4467 || device->lun_id == CAM_LUN_WILDCARD)
4470 printf("%s called\n", __func__);
4474 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4476 struct cam_devq *devq;
4479 devq = dev->sim->devq;
4480 mtx_assert(&devq->send_mtx, MA_OWNED);
4481 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4482 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4483 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4484 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4485 /* Remove frozen device from sendq. */
4486 if (device_is_queued(dev))
4487 camq_remove(&devq->send_queue, dev->devq_entry.index);
4492 xpt_freeze_devq(struct cam_path *path, u_int count)
4494 struct cam_ed *dev = path->device;
4495 struct cam_devq *devq;
4498 devq = dev->sim->devq;
4499 mtx_lock(&devq->send_mtx);
4500 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4501 freeze = xpt_freeze_devq_device(dev, count);
4502 mtx_unlock(&devq->send_mtx);
4507 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4509 struct cam_devq *devq;
4513 mtx_lock(&devq->send_mtx);
4514 freeze = (devq->send_queue.qfrozen_cnt += count);
4515 mtx_unlock(&devq->send_mtx);
4520 xpt_release_devq_timeout(void *arg)
4523 struct cam_devq *devq;
4525 dev = (struct cam_ed *)arg;
4526 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4527 devq = dev->sim->devq;
4528 mtx_assert(&devq->send_mtx, MA_OWNED);
4529 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4534 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4537 struct cam_devq *devq;
4539 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4542 devq = dev->sim->devq;
4543 mtx_lock(&devq->send_mtx);
4544 if (xpt_release_devq_device(dev, count, run_queue))
4545 xpt_run_devq(dev->sim->devq);
4546 mtx_unlock(&devq->send_mtx);
4550 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4553 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4554 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4555 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4556 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4557 if (count > dev->ccbq.queue.qfrozen_cnt) {
4559 printf("xpt_release_devq(): requested %u > present %u\n",
4560 count, dev->ccbq.queue.qfrozen_cnt);
4562 count = dev->ccbq.queue.qfrozen_cnt;
4564 dev->ccbq.queue.qfrozen_cnt -= count;
4565 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4567 * No longer need to wait for a successful
4568 * command completion.
4570 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4572 * Remove any timeouts that might be scheduled
4573 * to release this queue.
4575 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4576 callout_stop(&dev->callout);
4577 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4580 * Now that we are unfrozen schedule the
4581 * device so any pending transactions are
4584 xpt_schedule_devq(dev->sim->devq, dev);
4591 xpt_release_simq(struct cam_sim *sim, int run_queue)
4593 struct cam_devq *devq;
4596 mtx_lock(&devq->send_mtx);
4597 if (devq->send_queue.qfrozen_cnt <= 0) {
4599 printf("xpt_release_simq: requested 1 > present %u\n",
4600 devq->send_queue.qfrozen_cnt);
4603 devq->send_queue.qfrozen_cnt--;
4604 if (devq->send_queue.qfrozen_cnt == 0) {
4606 * If there is a timeout scheduled to release this
4607 * sim queue, remove it. The queue frozen count is
4610 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4611 callout_stop(&sim->callout);
4612 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4616 * Now that we are unfrozen run the send queue.
4618 xpt_run_devq(sim->devq);
4621 mtx_unlock(&devq->send_mtx);
4625 * XXX Appears to be unused.
4628 xpt_release_simq_timeout(void *arg)
4630 struct cam_sim *sim;
4632 sim = (struct cam_sim *)arg;
4633 xpt_release_simq(sim, /* run_queue */ TRUE);
4637 xpt_done(union ccb *done_ccb)
4639 struct cam_doneq *queue;
4642 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4643 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4644 done_ccb->csio.bio != NULL)
4645 biotrack(done_ccb->csio.bio, __func__);
4648 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4649 ("xpt_done: func= %#x %s status %#x\n",
4650 done_ccb->ccb_h.func_code,
4651 xpt_action_name(done_ccb->ccb_h.func_code),
4652 done_ccb->ccb_h.status));
4653 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4656 /* Store the time the ccb was in the sim */
4657 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4658 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4659 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4660 queue = &cam_doneqs[hash];
4661 mtx_lock(&queue->cam_doneq_mtx);
4662 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4663 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4664 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4665 mtx_unlock(&queue->cam_doneq_mtx);
4667 wakeup(&queue->cam_doneq);
4671 xpt_done_direct(union ccb *done_ccb)
4674 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4675 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4676 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4679 /* Store the time the ccb was in the sim */
4680 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4681 xpt_done_process(&done_ccb->ccb_h);
4689 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4694 xpt_alloc_ccb_nowait()
4698 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4703 xpt_free_ccb(union ccb *free_ccb)
4705 free(free_ccb, M_CAMCCB);
4710 /* Private XPT functions */
4713 * Get a CAM control block for the caller. Charge the structure to the device
4714 * referenced by the path. If we don't have sufficient resources to allocate
4715 * more ccbs, we return NULL.
4718 xpt_get_ccb_nowait(struct cam_periph *periph)
4722 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4723 if (new_ccb == NULL)
4725 periph->periph_allocated++;
4726 cam_ccbq_take_opening(&periph->path->device->ccbq);
4731 xpt_get_ccb(struct cam_periph *periph)
4735 cam_periph_unlock(periph);
4736 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4737 cam_periph_lock(periph);
4738 periph->periph_allocated++;
4739 cam_ccbq_take_opening(&periph->path->device->ccbq);
4744 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4746 struct ccb_hdr *ccb_h;
4748 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4749 cam_periph_assert(periph, MA_OWNED);
4750 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4751 ccb_h->pinfo.priority != priority) {
4752 if (priority < periph->immediate_priority) {
4753 periph->immediate_priority = priority;
4754 xpt_run_allocq(periph, 0);
4756 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4759 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4760 return ((union ccb *)ccb_h);
4764 xpt_acquire_bus(struct cam_eb *bus)
4773 xpt_release_bus(struct cam_eb *bus)
4777 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4778 if (--bus->refcount > 0) {
4782 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4783 xsoftc.bus_generation++;
4785 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4786 ("destroying bus, but target list is not empty"));
4787 cam_sim_release(bus->sim);
4788 mtx_destroy(&bus->eb_mtx);
4789 free(bus, M_CAMXPT);
4792 static struct cam_et *
4793 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4795 struct cam_et *cur_target, *target;
4797 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4798 mtx_assert(&bus->eb_mtx, MA_OWNED);
4799 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4804 TAILQ_INIT(&target->ed_entries);
4806 target->target_id = target_id;
4807 target->refcount = 1;
4808 target->generation = 0;
4809 target->luns = NULL;
4810 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4811 timevalclear(&target->last_reset);
4813 * Hold a reference to our parent bus so it
4814 * will not go away before we do.
4818 /* Insertion sort into our bus's target list */
4819 cur_target = TAILQ_FIRST(&bus->et_entries);
4820 while (cur_target != NULL && cur_target->target_id < target_id)
4821 cur_target = TAILQ_NEXT(cur_target, links);
4822 if (cur_target != NULL) {
4823 TAILQ_INSERT_BEFORE(cur_target, target, links);
4825 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4832 xpt_acquire_target(struct cam_et *target)
4834 struct cam_eb *bus = target->bus;
4836 mtx_lock(&bus->eb_mtx);
4838 mtx_unlock(&bus->eb_mtx);
4842 xpt_release_target(struct cam_et *target)
4844 struct cam_eb *bus = target->bus;
4846 mtx_lock(&bus->eb_mtx);
4847 if (--target->refcount > 0) {
4848 mtx_unlock(&bus->eb_mtx);
4851 TAILQ_REMOVE(&bus->et_entries, target, links);
4853 mtx_unlock(&bus->eb_mtx);
4854 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4855 ("destroying target, but device list is not empty"));
4856 xpt_release_bus(bus);
4857 mtx_destroy(&target->luns_mtx);
4859 free(target->luns, M_CAMXPT);
4860 free(target, M_CAMXPT);
4863 static struct cam_ed *
4864 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4867 struct cam_ed *device;
4869 device = xpt_alloc_device(bus, target, lun_id);
4873 device->mintags = 1;
4874 device->maxtags = 1;
4879 xpt_destroy_device(void *context, int pending)
4881 struct cam_ed *device = context;
4883 mtx_lock(&device->device_mtx);
4884 mtx_destroy(&device->device_mtx);
4885 free(device, M_CAMDEV);
4889 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4891 struct cam_ed *cur_device, *device;
4892 struct cam_devq *devq;
4895 mtx_assert(&bus->eb_mtx, MA_OWNED);
4896 /* Make space for us in the device queue on our bus */
4897 devq = bus->sim->devq;
4898 mtx_lock(&devq->send_mtx);
4899 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4900 mtx_unlock(&devq->send_mtx);
4901 if (status != CAM_REQ_CMP)
4904 device = (struct cam_ed *)malloc(sizeof(*device),
4905 M_CAMDEV, M_NOWAIT|M_ZERO);
4909 cam_init_pinfo(&device->devq_entry);
4910 device->target = target;
4911 device->lun_id = lun_id;
4912 device->sim = bus->sim;
4913 if (cam_ccbq_init(&device->ccbq,
4914 bus->sim->max_dev_openings) != 0) {
4915 free(device, M_CAMDEV);
4918 SLIST_INIT(&device->asyncs);
4919 SLIST_INIT(&device->periphs);
4920 device->generation = 0;
4921 device->flags = CAM_DEV_UNCONFIGURED;
4922 device->tag_delay_count = 0;
4923 device->tag_saved_openings = 0;
4924 device->refcount = 1;
4925 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4926 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4927 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4929 * Hold a reference to our parent bus so it
4930 * will not go away before we do.
4934 cur_device = TAILQ_FIRST(&target->ed_entries);
4935 while (cur_device != NULL && cur_device->lun_id < lun_id)
4936 cur_device = TAILQ_NEXT(cur_device, links);
4937 if (cur_device != NULL)
4938 TAILQ_INSERT_BEFORE(cur_device, device, links);
4940 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4941 target->generation++;
4946 xpt_acquire_device(struct cam_ed *device)
4948 struct cam_eb *bus = device->target->bus;
4950 mtx_lock(&bus->eb_mtx);
4952 mtx_unlock(&bus->eb_mtx);
4956 xpt_release_device(struct cam_ed *device)
4958 struct cam_eb *bus = device->target->bus;
4959 struct cam_devq *devq;
4961 mtx_lock(&bus->eb_mtx);
4962 if (--device->refcount > 0) {
4963 mtx_unlock(&bus->eb_mtx);
4967 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4968 device->target->generation++;
4969 mtx_unlock(&bus->eb_mtx);
4971 /* Release our slot in the devq */
4972 devq = bus->sim->devq;
4973 mtx_lock(&devq->send_mtx);
4974 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4975 mtx_unlock(&devq->send_mtx);
4977 KASSERT(SLIST_EMPTY(&device->periphs),
4978 ("destroying device, but periphs list is not empty"));
4979 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4980 ("destroying device while still queued for ccbs"));
4982 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4983 callout_stop(&device->callout);
4985 xpt_release_target(device->target);
4987 cam_ccbq_fini(&device->ccbq);
4989 * Free allocated memory. free(9) does nothing if the
4990 * supplied pointer is NULL, so it is safe to call without
4993 free(device->supported_vpds, M_CAMXPT);
4994 free(device->device_id, M_CAMXPT);
4995 free(device->ext_inq, M_CAMXPT);
4996 free(device->physpath, M_CAMXPT);
4997 free(device->rcap_buf, M_CAMXPT);
4998 free(device->serial_num, M_CAMXPT);
4999 free(device->nvme_data, M_CAMXPT);
5000 free(device->nvme_cdata, M_CAMXPT);
5001 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
5005 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5011 mtx_lock(&dev->sim->devq->send_mtx);
5012 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5013 mtx_unlock(&dev->sim->devq->send_mtx);
5014 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5015 || (dev->inq_flags & SID_CmdQue) != 0)
5016 dev->tag_saved_openings = newopenings;
5020 static struct cam_eb *
5021 xpt_find_bus(path_id_t path_id)
5026 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5028 bus = TAILQ_NEXT(bus, links)) {
5029 if (bus->path_id == path_id) {
5038 static struct cam_et *
5039 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5041 struct cam_et *target;
5043 mtx_assert(&bus->eb_mtx, MA_OWNED);
5044 for (target = TAILQ_FIRST(&bus->et_entries);
5046 target = TAILQ_NEXT(target, links)) {
5047 if (target->target_id == target_id) {
5055 static struct cam_ed *
5056 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5058 struct cam_ed *device;
5060 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
5061 for (device = TAILQ_FIRST(&target->ed_entries);
5063 device = TAILQ_NEXT(device, links)) {
5064 if (device->lun_id == lun_id) {
5073 xpt_start_tags(struct cam_path *path)
5075 struct ccb_relsim crs;
5076 struct cam_ed *device;
5077 struct cam_sim *sim;
5080 device = path->device;
5081 sim = path->bus->sim;
5082 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5083 xpt_freeze_devq(path, /*count*/1);
5084 device->inq_flags |= SID_CmdQue;
5085 if (device->tag_saved_openings != 0)
5086 newopenings = device->tag_saved_openings;
5088 newopenings = min(device->maxtags,
5089 sim->max_tagged_dev_openings);
5090 xpt_dev_ccbq_resize(path, newopenings);
5091 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5092 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5093 crs.ccb_h.func_code = XPT_REL_SIMQ;
5094 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5096 = crs.release_timeout
5099 xpt_action((union ccb *)&crs);
5103 xpt_stop_tags(struct cam_path *path)
5105 struct ccb_relsim crs;
5106 struct cam_ed *device;
5107 struct cam_sim *sim;
5109 device = path->device;
5110 sim = path->bus->sim;
5111 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5112 device->tag_delay_count = 0;
5113 xpt_freeze_devq(path, /*count*/1);
5114 device->inq_flags &= ~SID_CmdQue;
5115 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5116 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5117 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5118 crs.ccb_h.func_code = XPT_REL_SIMQ;
5119 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5121 = crs.release_timeout
5124 xpt_action((union ccb *)&crs);
5128 * Assume all possible buses are detected by this time, so allow boot
5129 * as soon as they all are scanned.
5132 xpt_boot_delay(void *arg)
5139 * Now that all config hooks have completed, start boot_delay timer,
5140 * waiting for possibly still undetected buses (USB) to appear.
5143 xpt_ch_done(void *arg)
5146 callout_init(&xsoftc.boot_callout, 1);
5147 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5148 xpt_boot_delay, NULL, 0);
5150 SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5153 * Now that interrupts are enabled, go find our devices
5156 xpt_config(void *arg)
5158 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5159 printf("xpt_config: failed to create taskqueue thread.\n");
5161 /* Setup debugging path */
5162 if (cam_dflags != CAM_DEBUG_NONE) {
5163 if (xpt_create_path(&cam_dpath, NULL,
5164 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5165 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5166 printf("xpt_config: xpt_create_path() failed for debug"
5167 " target %d:%d:%d, debugging disabled\n",
5168 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5169 cam_dflags = CAM_DEBUG_NONE;
5174 periphdriver_init(1);
5177 /* Fire up rescan thread. */
5178 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5179 "cam", "scanner")) {
5180 printf("xpt_config: failed to create rescan thread.\n");
5185 xpt_hold_boot_locked(void)
5188 if (xsoftc.buses_to_config++ == 0)
5189 root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5197 xpt_hold_boot_locked();
5202 xpt_release_boot(void)
5206 if (--xsoftc.buses_to_config == 0) {
5207 if (xsoftc.buses_config_done == 0) {
5208 xsoftc.buses_config_done = 1;
5209 xsoftc.buses_to_config++;
5210 TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5212 taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
5214 root_mount_rel(&xsoftc.xpt_rootmount);
5220 * If the given device only has one peripheral attached to it, and if that
5221 * peripheral is the passthrough driver, announce it. This insures that the
5222 * user sees some sort of announcement for every peripheral in their system.
5225 xptpassannouncefunc(struct cam_ed *device, void *arg)
5227 struct cam_periph *periph;
5230 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5231 periph = SLIST_NEXT(periph, periph_links), i++);
5233 periph = SLIST_FIRST(&device->periphs);
5235 && (strncmp(periph->periph_name, "pass", 4) == 0))
5236 xpt_announce_periph(periph, NULL);
5242 xpt_finishconfig_task(void *context, int pending)
5245 periphdriver_init(2);
5247 * Check for devices with no "standard" peripheral driver
5248 * attached. For any devices like that, announce the
5249 * passthrough driver so the user will see something.
5252 xpt_for_all_devices(xptpassannouncefunc, NULL);
5258 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5259 struct cam_path *path)
5261 struct ccb_setasync csa;
5266 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5267 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5268 if (status != CAM_REQ_CMP)
5270 xpt_path_lock(path);
5274 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5275 csa.ccb_h.func_code = XPT_SASYNC_CB;
5276 csa.event_enable = event;
5277 csa.callback = cbfunc;
5278 csa.callback_arg = cbarg;
5279 xpt_action((union ccb *)&csa);
5280 status = csa.ccb_h.status;
5282 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5283 ("xpt_register_async: func %p\n", cbfunc));
5286 xpt_path_unlock(path);
5287 xpt_free_path(path);
5290 if ((status == CAM_REQ_CMP) &&
5291 (csa.event_enable & AC_FOUND_DEVICE)) {
5293 * Get this peripheral up to date with all
5294 * the currently existing devices.
5296 xpt_for_all_devices(xptsetasyncfunc, &csa);
5298 if ((status == CAM_REQ_CMP) &&
5299 (csa.event_enable & AC_PATH_REGISTERED)) {
5301 * Get this peripheral up to date with all
5302 * the currently existing buses.
5304 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5311 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5313 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5315 switch (work_ccb->ccb_h.func_code) {
5316 /* Common cases first */
5317 case XPT_PATH_INQ: /* Path routing inquiry */
5319 struct ccb_pathinq *cpi;
5321 cpi = &work_ccb->cpi;
5322 cpi->version_num = 1; /* XXX??? */
5323 cpi->hba_inquiry = 0;
5324 cpi->target_sprt = 0;
5326 cpi->hba_eng_cnt = 0;
5327 cpi->max_target = 0;
5329 cpi->initiator_id = 0;
5330 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5331 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5332 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5333 cpi->unit_number = sim->unit_number;
5334 cpi->bus_id = sim->bus_id;
5335 cpi->base_transfer_speed = 0;
5336 cpi->protocol = PROTO_UNSPECIFIED;
5337 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5338 cpi->transport = XPORT_UNSPECIFIED;
5339 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5340 cpi->ccb_h.status = CAM_REQ_CMP;
5345 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5352 * The xpt as a "controller" has no interrupt sources, so polling
5356 xptpoll(struct cam_sim *sim)
5361 xpt_lock_buses(void)
5363 mtx_lock(&xsoftc.xpt_topo_lock);
5367 xpt_unlock_buses(void)
5369 mtx_unlock(&xsoftc.xpt_topo_lock);
5373 xpt_path_mtx(struct cam_path *path)
5376 return (&path->device->device_mtx);
5380 xpt_done_process(struct ccb_hdr *ccb_h)
5382 struct cam_sim *sim = NULL;
5383 struct cam_devq *devq = NULL;
5384 struct mtx *mtx = NULL;
5386 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5387 struct ccb_scsiio *csio;
5389 if (ccb_h->func_code == XPT_SCSI_IO) {
5390 csio = &((union ccb *)ccb_h)->csio;
5391 if (csio->bio != NULL)
5392 biotrack(csio->bio, __func__);
5396 if (ccb_h->flags & CAM_HIGH_POWER) {
5397 struct highpowerlist *hphead;
5398 struct cam_ed *device;
5400 mtx_lock(&xsoftc.xpt_highpower_lock);
5401 hphead = &xsoftc.highpowerq;
5403 device = STAILQ_FIRST(hphead);
5406 * Increment the count since this command is done.
5408 xsoftc.num_highpower++;
5411 * Any high powered commands queued up?
5413 if (device != NULL) {
5415 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5416 mtx_unlock(&xsoftc.xpt_highpower_lock);
5418 mtx_lock(&device->sim->devq->send_mtx);
5419 xpt_release_devq_device(device,
5420 /*count*/1, /*runqueue*/TRUE);
5421 mtx_unlock(&device->sim->devq->send_mtx);
5423 mtx_unlock(&xsoftc.xpt_highpower_lock);
5427 * Insulate against a race where the periph is destroyed but CCBs are
5428 * still not all processed. This shouldn't happen, but allows us better
5429 * bug diagnostic when it does.
5431 if (ccb_h->path->bus)
5432 sim = ccb_h->path->bus->sim;
5434 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5435 KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5436 xpt_release_simq(sim, /*run_queue*/FALSE);
5437 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5440 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5441 && (ccb_h->status & CAM_DEV_QFRZN)) {
5442 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5443 ccb_h->status &= ~CAM_DEV_QFRZN;
5446 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5447 struct cam_ed *dev = ccb_h->path->device;
5451 KASSERT(devq, ("Periph disappeared with request pending."));
5453 mtx_lock(&devq->send_mtx);
5454 devq->send_active--;
5455 devq->send_openings++;
5456 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5458 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5459 && (dev->ccbq.dev_active == 0))) {
5460 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5461 xpt_release_devq_device(dev, /*count*/1,
5462 /*run_queue*/FALSE);
5465 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5466 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5467 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5468 xpt_release_devq_device(dev, /*count*/1,
5469 /*run_queue*/FALSE);
5472 if (!device_is_queued(dev))
5473 (void)xpt_schedule_devq(devq, dev);
5475 mtx_unlock(&devq->send_mtx);
5477 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5478 mtx = xpt_path_mtx(ccb_h->path);
5481 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5482 && (--dev->tag_delay_count == 0))
5483 xpt_start_tags(ccb_h->path);
5487 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5489 mtx = xpt_path_mtx(ccb_h->path);
5499 /* Call the peripheral driver's callback */
5500 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5501 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5507 xpt_done_td(void *arg)
5509 struct cam_doneq *queue = arg;
5510 struct ccb_hdr *ccb_h;
5511 STAILQ_HEAD(, ccb_hdr) doneq;
5513 STAILQ_INIT(&doneq);
5514 mtx_lock(&queue->cam_doneq_mtx);
5516 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5517 queue->cam_doneq_sleep = 1;
5518 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5520 queue->cam_doneq_sleep = 0;
5522 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5523 mtx_unlock(&queue->cam_doneq_mtx);
5525 THREAD_NO_SLEEPING();
5526 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5527 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5528 xpt_done_process(ccb_h);
5530 THREAD_SLEEPING_OK();
5532 mtx_lock(&queue->cam_doneq_mtx);
5537 camisr_runqueue(void)
5539 struct ccb_hdr *ccb_h;
5540 struct cam_doneq *queue;
5543 /* Process global queues. */
5544 for (i = 0; i < cam_num_doneqs; i++) {
5545 queue = &cam_doneqs[i];
5546 mtx_lock(&queue->cam_doneq_mtx);
5547 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5548 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5549 mtx_unlock(&queue->cam_doneq_mtx);
5550 xpt_done_process(ccb_h);
5551 mtx_lock(&queue->cam_doneq_mtx);
5553 mtx_unlock(&queue->cam_doneq_mtx);
5563 static struct kv map[] = {
5564 { XPT_NOOP, "XPT_NOOP" },
5565 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5566 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5567 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5568 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5569 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5570 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5571 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5572 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5573 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5574 { XPT_DEBUG, "XPT_DEBUG" },
5575 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5576 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5577 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5578 { XPT_ASYNC, "XPT_ASYNC" },
5579 { XPT_ABORT, "XPT_ABORT" },
5580 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5581 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5582 { XPT_TERM_IO, "XPT_TERM_IO" },
5583 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5584 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5585 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5586 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5587 { XPT_ATA_IO, "XPT_ATA_IO" },
5588 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5589 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5590 { XPT_NVME_IO, "XPT_NVME_IO" },
5591 { XPT_MMC_IO, "XPT_MMC_IO" },
5592 { XPT_SMP_IO, "XPT_SMP_IO" },
5593 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5594 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5595 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5596 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5597 { XPT_EN_LUN, "XPT_EN_LUN" },
5598 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5599 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5600 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5601 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5602 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5603 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5604 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5609 xpt_action_name(uint32_t action)
5611 static char buffer[32]; /* Only for unknown messages -- racy */
5612 struct kv *walker = map;
5614 while (walker->name != NULL) {
5615 if (walker->v == action)
5616 return (walker->name);
5620 snprintf(buffer, sizeof(buffer), "%#x", action);