2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_compat.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
69 #include <machine/md_var.h> /* geometry translation */
70 #include <machine/stdarg.h> /* for xpt_print below */
75 * This is the maximum number of high powered commands (e.g. start unit)
76 * that can be outstanding at a particular time.
78 #ifndef CAM_MAX_HIGHPOWER
79 #define CAM_MAX_HIGHPOWER 4
82 /* Datastructures internal to the xpt layer */
83 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
88 /* Object for defering XPT actions to a taskqueue */
96 uint32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 struct mtx xpt_highpower_lock;
100 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 int buses_config_done;
108 /* Registered busses */
109 TAILQ_HEAD(,cam_eb) xpt_busses;
110 u_int bus_generation;
112 struct intr_config_hook *xpt_config_hook;
115 struct callout boot_callout;
117 struct mtx xpt_topo_lock;
119 struct taskqueue *xpt_taskq;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
158 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
159 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
162 struct mtx_padalign cam_doneq_mtx;
163 STAILQ_HEAD(, ccb_hdr) cam_doneq;
167 static struct cam_doneq cam_doneqs[MAXCPU];
168 static int cam_num_doneqs;
169 static struct proc *cam_proc;
171 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
172 &cam_num_doneqs, 0, "Number of completion queues/threads");
174 struct cam_periph *xpt_periph;
176 static periph_init_t xpt_periph_init;
178 static struct periph_driver xpt_driver =
180 xpt_periph_init, "xpt",
181 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
185 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
187 static d_open_t xptopen;
188 static d_close_t xptclose;
189 static d_ioctl_t xptioctl;
190 static d_ioctl_t xptdoioctl;
192 static struct cdevsw xpt_cdevsw = {
193 .d_version = D_VERSION,
201 /* Storage for debugging datastructures */
202 struct cam_path *cam_dpath;
203 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
204 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
205 &cam_dflags, 0, "Enabled debug flags");
206 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
207 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
208 &cam_debug_delay, 0, "Delay in us after each debug message");
210 /* Our boot-time initialization hook */
211 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
213 static moduledata_t cam_moduledata = {
215 cam_module_event_handler,
219 static int xpt_init(void *);
221 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
222 MODULE_VERSION(cam, 1);
225 static void xpt_async_bcast(struct async_list *async_head,
226 u_int32_t async_code,
227 struct cam_path *path,
229 static path_id_t xptnextfreepathid(void);
230 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
231 static union ccb *xpt_get_ccb(struct cam_periph *periph);
232 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
233 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
234 static void xpt_run_allocq_task(void *context, int pending);
235 static void xpt_run_devq(struct cam_devq *devq);
236 static timeout_t xpt_release_devq_timeout;
237 static void xpt_release_simq_timeout(void *arg) __unused;
238 static void xpt_acquire_bus(struct cam_eb *bus);
239 static void xpt_release_bus(struct cam_eb *bus);
240 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
241 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
243 static struct cam_et*
244 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
245 static void xpt_acquire_target(struct cam_et *target);
246 static void xpt_release_target(struct cam_et *target);
247 static struct cam_eb*
248 xpt_find_bus(path_id_t path_id);
249 static struct cam_et*
250 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
251 static struct cam_ed*
252 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
253 static void xpt_config(void *arg);
254 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
255 u_int32_t new_priority);
256 static xpt_devicefunc_t xptpassannouncefunc;
257 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
258 static void xptpoll(struct cam_sim *sim);
259 static void camisr_runqueue(void);
260 static void xpt_done_process(struct ccb_hdr *ccb_h);
261 static void xpt_done_td(void *);
262 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
263 u_int num_patterns, struct cam_eb *bus);
264 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
266 struct cam_ed *device);
267 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
269 struct cam_periph *periph);
270 static xpt_busfunc_t xptedtbusfunc;
271 static xpt_targetfunc_t xptedttargetfunc;
272 static xpt_devicefunc_t xptedtdevicefunc;
273 static xpt_periphfunc_t xptedtperiphfunc;
274 static xpt_pdrvfunc_t xptplistpdrvfunc;
275 static xpt_periphfunc_t xptplistperiphfunc;
276 static int xptedtmatch(struct ccb_dev_match *cdm);
277 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
278 static int xptbustraverse(struct cam_eb *start_bus,
279 xpt_busfunc_t *tr_func, void *arg);
280 static int xpttargettraverse(struct cam_eb *bus,
281 struct cam_et *start_target,
282 xpt_targetfunc_t *tr_func, void *arg);
283 static int xptdevicetraverse(struct cam_et *target,
284 struct cam_ed *start_device,
285 xpt_devicefunc_t *tr_func, void *arg);
286 static int xptperiphtraverse(struct cam_ed *device,
287 struct cam_periph *start_periph,
288 xpt_periphfunc_t *tr_func, void *arg);
289 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
290 xpt_pdrvfunc_t *tr_func, void *arg);
291 static int xptpdperiphtraverse(struct periph_driver **pdrv,
292 struct cam_periph *start_periph,
293 xpt_periphfunc_t *tr_func,
295 static xpt_busfunc_t xptdefbusfunc;
296 static xpt_targetfunc_t xptdeftargetfunc;
297 static xpt_devicefunc_t xptdefdevicefunc;
298 static xpt_periphfunc_t xptdefperiphfunc;
299 static void xpt_finishconfig_task(void *context, int pending);
300 static void xpt_dev_async_default(u_int32_t async_code,
302 struct cam_et *target,
303 struct cam_ed *device,
305 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
306 struct cam_et *target,
308 static xpt_devicefunc_t xptsetasyncfunc;
309 static xpt_busfunc_t xptsetasyncbusfunc;
310 static cam_status xptregister(struct cam_periph *periph,
312 static const char * xpt_action_name(uint32_t action);
313 static __inline int device_is_queued(struct cam_ed *device);
316 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
320 mtx_assert(&devq->send_mtx, MA_OWNED);
321 if ((dev->ccbq.queue.entries > 0) &&
322 (dev->ccbq.dev_openings > 0) &&
323 (dev->ccbq.queue.qfrozen_cnt == 0)) {
325 * The priority of a device waiting for controller
326 * resources is that of the highest priority CCB
330 xpt_schedule_dev(&devq->send_queue,
332 CAMQ_GET_PRIO(&dev->ccbq.queue));
340 device_is_queued(struct cam_ed *device)
342 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
348 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
352 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
356 * Only allow read-write access.
358 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
362 * We don't allow nonblocking access.
364 if ((flags & O_NONBLOCK) != 0) {
365 printf("%s: can't do nonblocking access\n", devtoname(dev));
373 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
380 * Don't automatically grab the xpt softc lock here even though this is going
381 * through the xpt device. The xpt device is really just a back door for
382 * accessing other devices and SIMs, so the right thing to do is to grab
383 * the appropriate SIM lock once the bus/SIM is located.
386 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
390 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
391 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
397 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
405 * For the transport layer CAMIOCOMMAND ioctl, we really only want
406 * to accept CCB types that don't quite make sense to send through a
407 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
415 inccb = (union ccb *)addr;
417 bus = xpt_find_bus(inccb->ccb_h.path_id);
421 switch (inccb->ccb_h.func_code) {
424 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
425 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
426 xpt_release_bus(bus);
431 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
432 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
433 xpt_release_bus(bus);
441 switch(inccb->ccb_h.func_code) {
449 ccb = xpt_alloc_ccb();
452 * Create a path using the bus, target, and lun the
455 if (xpt_create_path(&ccb->ccb_h.path, NULL,
456 inccb->ccb_h.path_id,
457 inccb->ccb_h.target_id,
458 inccb->ccb_h.target_lun) !=
464 /* Ensure all of our fields are correct */
465 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
466 inccb->ccb_h.pinfo.priority);
467 xpt_merge_ccb(ccb, inccb);
468 xpt_path_lock(ccb->ccb_h.path);
469 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
470 xpt_path_unlock(ccb->ccb_h.path);
471 bcopy(ccb, inccb, sizeof(union ccb));
472 xpt_free_path(ccb->ccb_h.path);
480 * This is an immediate CCB, so it's okay to
481 * allocate it on the stack.
485 * Create a path using the bus, target, and lun the
488 if (xpt_create_path(&ccb.ccb_h.path, NULL,
489 inccb->ccb_h.path_id,
490 inccb->ccb_h.target_id,
491 inccb->ccb_h.target_lun) !=
496 /* Ensure all of our fields are correct */
497 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
498 inccb->ccb_h.pinfo.priority);
499 xpt_merge_ccb(&ccb, inccb);
501 bcopy(&ccb, inccb, sizeof(union ccb));
502 xpt_free_path(ccb.ccb_h.path);
506 case XPT_DEV_MATCH: {
507 struct cam_periph_map_info mapinfo;
508 struct cam_path *old_path;
511 * We can't deal with physical addresses for this
512 * type of transaction.
514 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
521 * Save this in case the caller had it set to
522 * something in particular.
524 old_path = inccb->ccb_h.path;
527 * We really don't need a path for the matching
528 * code. The path is needed because of the
529 * debugging statements in xpt_action(). They
530 * assume that the CCB has a valid path.
532 inccb->ccb_h.path = xpt_periph->path;
534 bzero(&mapinfo, sizeof(mapinfo));
537 * Map the pattern and match buffers into kernel
538 * virtual address space.
540 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
543 inccb->ccb_h.path = old_path;
548 * This is an immediate CCB, we can send it on directly.
553 * Map the buffers back into user space.
555 cam_periph_unmapmem(inccb, &mapinfo);
557 inccb->ccb_h.path = old_path;
566 xpt_release_bus(bus);
570 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
571 * with the periphal driver name and unit name filled in. The other
572 * fields don't really matter as input. The passthrough driver name
573 * ("pass"), and unit number are passed back in the ccb. The current
574 * device generation number, and the index into the device peripheral
575 * driver list, and the status are also passed back. Note that
576 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
577 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
578 * (or rather should be) impossible for the device peripheral driver
579 * list to change since we look at the whole thing in one pass, and
580 * we do it with lock protection.
583 case CAMGETPASSTHRU: {
585 struct cam_periph *periph;
586 struct periph_driver **p_drv;
589 int base_periph_found;
591 ccb = (union ccb *)addr;
592 unit = ccb->cgdl.unit_number;
593 name = ccb->cgdl.periph_name;
594 base_periph_found = 0;
597 * Sanity check -- make sure we don't get a null peripheral
600 if (*ccb->cgdl.periph_name == '\0') {
605 /* Keep the list from changing while we traverse it */
608 /* first find our driver in the list of drivers */
609 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
610 if (strcmp((*p_drv)->driver_name, name) == 0)
613 if (*p_drv == NULL) {
615 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
616 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
617 *ccb->cgdl.periph_name = '\0';
618 ccb->cgdl.unit_number = 0;
624 * Run through every peripheral instance of this driver
625 * and check to see whether it matches the unit passed
626 * in by the user. If it does, get out of the loops and
627 * find the passthrough driver associated with that
630 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
631 periph = TAILQ_NEXT(periph, unit_links)) {
633 if (periph->unit_number == unit)
637 * If we found the peripheral driver that the user passed
638 * in, go through all of the peripheral drivers for that
639 * particular device and look for a passthrough driver.
641 if (periph != NULL) {
642 struct cam_ed *device;
645 base_periph_found = 1;
646 device = periph->path->device;
647 for (i = 0, periph = SLIST_FIRST(&device->periphs);
649 periph = SLIST_NEXT(periph, periph_links), i++) {
651 * Check to see whether we have a
652 * passthrough device or not.
654 if (strcmp(periph->periph_name, "pass") == 0) {
656 * Fill in the getdevlist fields.
658 strlcpy(ccb->cgdl.periph_name,
660 sizeof(ccb->cgdl.periph_name));
661 ccb->cgdl.unit_number =
663 if (SLIST_NEXT(periph, periph_links))
665 CAM_GDEVLIST_MORE_DEVS;
668 CAM_GDEVLIST_LAST_DEVICE;
669 ccb->cgdl.generation =
673 * Fill in some CCB header fields
674 * that the user may want.
677 periph->path->bus->path_id;
678 ccb->ccb_h.target_id =
679 periph->path->target->target_id;
680 ccb->ccb_h.target_lun =
681 periph->path->device->lun_id;
682 ccb->ccb_h.status = CAM_REQ_CMP;
689 * If the periph is null here, one of two things has
690 * happened. The first possibility is that we couldn't
691 * find the unit number of the particular peripheral driver
692 * that the user is asking about. e.g. the user asks for
693 * the passthrough driver for "da11". We find the list of
694 * "da" peripherals all right, but there is no unit 11.
695 * The other possibility is that we went through the list
696 * of peripheral drivers attached to the device structure,
697 * but didn't find one with the name "pass". Either way,
698 * we return ENOENT, since we couldn't find something.
700 if (periph == NULL) {
701 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
702 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
703 *ccb->cgdl.periph_name = '\0';
704 ccb->cgdl.unit_number = 0;
707 * It is unfortunate that this is even necessary,
708 * but there are many, many clueless users out there.
709 * If this is true, the user is looking for the
710 * passthrough driver, but doesn't have one in his
713 if (base_periph_found == 1) {
714 printf("xptioctl: pass driver is not in the "
716 printf("xptioctl: put \"device pass\" in "
717 "your kernel config file\n");
732 cam_module_event_handler(module_t mod, int what, void *arg)
738 if ((error = xpt_init(NULL)) != 0)
751 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
754 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
755 xpt_free_path(done_ccb->ccb_h.path);
756 xpt_free_ccb(done_ccb);
758 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
759 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
764 /* thread to handle bus rescans */
766 xpt_scanner_thread(void *dummy)
769 struct cam_path path;
773 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
774 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
776 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
777 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
781 * Since lock can be dropped inside and path freed
782 * by completion callback even before return here,
783 * take our own path copy for reference.
785 xpt_copy_path(&path, ccb->ccb_h.path);
786 xpt_path_lock(&path);
788 xpt_path_unlock(&path);
789 xpt_release_path(&path);
797 xpt_rescan(union ccb *ccb)
801 /* Prepare request */
802 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
803 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
804 ccb->ccb_h.func_code = XPT_SCAN_BUS;
805 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
806 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
807 ccb->ccb_h.func_code = XPT_SCAN_TGT;
808 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
809 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
810 ccb->ccb_h.func_code = XPT_SCAN_LUN;
812 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
813 xpt_free_path(ccb->ccb_h.path);
817 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
818 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
819 xpt_action_name(ccb->ccb_h.func_code)));
821 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
822 ccb->ccb_h.cbfcnp = xpt_rescan_done;
823 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
824 /* Don't make duplicate entries for the same paths. */
826 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
827 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
828 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
829 wakeup(&xsoftc.ccb_scanq);
831 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
832 xpt_free_path(ccb->ccb_h.path);
838 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
839 xsoftc.buses_to_config++;
840 wakeup(&xsoftc.ccb_scanq);
844 /* Functions accessed by the peripheral drivers */
846 xpt_init(void *dummy)
848 struct cam_sim *xpt_sim;
849 struct cam_path *path;
850 struct cam_devq *devq;
854 TAILQ_INIT(&xsoftc.xpt_busses);
855 TAILQ_INIT(&xsoftc.ccb_scanq);
856 STAILQ_INIT(&xsoftc.highpowerq);
857 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
859 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
860 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
861 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
862 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
864 #ifdef CAM_BOOT_DELAY
866 * Override this value at compile time to assist our users
867 * who don't use loader to boot a kernel.
869 xsoftc.boot_delay = CAM_BOOT_DELAY;
872 * The xpt layer is, itself, the equivalent of a SIM.
873 * Allow 16 ccbs in the ccb pool for it. This should
874 * give decent parallelism when we probe busses and
875 * perform other XPT functions.
877 devq = cam_simq_alloc(16);
878 xpt_sim = cam_sim_alloc(xptaction,
883 /*mtx*/&xsoftc.xpt_lock,
884 /*max_dev_transactions*/0,
885 /*max_tagged_dev_transactions*/0,
890 mtx_lock(&xsoftc.xpt_lock);
891 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
892 mtx_unlock(&xsoftc.xpt_lock);
893 printf("xpt_init: xpt_bus_register failed with status %#x,"
894 " failing attach\n", status);
897 mtx_unlock(&xsoftc.xpt_lock);
900 * Looking at the XPT from the SIM layer, the XPT is
901 * the equivalent of a peripheral driver. Allocate
902 * a peripheral driver entry for us.
904 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
906 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
907 printf("xpt_init: xpt_create_path failed with status %#x,"
908 " failing attach\n", status);
912 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
913 path, NULL, 0, xpt_sim);
914 xpt_path_unlock(path);
917 if (cam_num_doneqs < 1)
918 cam_num_doneqs = 1 + mp_ncpus / 6;
919 else if (cam_num_doneqs > MAXCPU)
920 cam_num_doneqs = MAXCPU;
921 for (i = 0; i < cam_num_doneqs; i++) {
922 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
924 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
925 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
926 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
932 if (cam_num_doneqs < 1) {
933 printf("xpt_init: Cannot init completion queues "
934 "- failing attach\n");
938 * Register a callback for when interrupts are enabled.
940 xsoftc.xpt_config_hook =
941 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
942 M_CAMXPT, M_NOWAIT | M_ZERO);
943 if (xsoftc.xpt_config_hook == NULL) {
944 printf("xpt_init: Cannot malloc config hook "
945 "- failing attach\n");
948 xsoftc.xpt_config_hook->ich_func = xpt_config;
949 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
950 free (xsoftc.xpt_config_hook, M_CAMXPT);
951 printf("xpt_init: config_intrhook_establish failed "
952 "- failing attach\n");
959 xptregister(struct cam_periph *periph, void *arg)
961 struct cam_sim *xpt_sim;
963 if (periph == NULL) {
964 printf("xptregister: periph was NULL!!\n");
965 return(CAM_REQ_CMP_ERR);
968 xpt_sim = (struct cam_sim *)arg;
969 xpt_sim->softc = periph;
971 periph->softc = NULL;
977 xpt_add_periph(struct cam_periph *periph)
979 struct cam_ed *device;
982 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
983 device = periph->path->device;
984 status = CAM_REQ_CMP;
985 if (device != NULL) {
986 mtx_lock(&device->target->bus->eb_mtx);
987 device->generation++;
988 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
989 mtx_unlock(&device->target->bus->eb_mtx);
990 atomic_add_32(&xsoftc.xpt_generation, 1);
997 xpt_remove_periph(struct cam_periph *periph)
999 struct cam_ed *device;
1001 device = periph->path->device;
1002 if (device != NULL) {
1003 mtx_lock(&device->target->bus->eb_mtx);
1004 device->generation++;
1005 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1006 mtx_unlock(&device->target->bus->eb_mtx);
1007 atomic_add_32(&xsoftc.xpt_generation, 1);
1013 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1015 struct cam_path *path = periph->path;
1017 cam_periph_assert(periph, MA_OWNED);
1018 periph->flags |= CAM_PERIPH_ANNOUNCED;
1020 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1021 periph->periph_name, periph->unit_number,
1022 path->bus->sim->sim_name,
1023 path->bus->sim->unit_number,
1024 path->bus->sim->bus_id,
1026 path->target->target_id,
1027 (uintmax_t)path->device->lun_id);
1028 printf("%s%d: ", periph->periph_name, periph->unit_number);
1029 if (path->device->protocol == PROTO_SCSI)
1030 scsi_print_inquiry(&path->device->inq_data);
1031 else if (path->device->protocol == PROTO_ATA ||
1032 path->device->protocol == PROTO_SATAPM)
1033 ata_print_ident(&path->device->ident_data);
1034 else if (path->device->protocol == PROTO_SEMB)
1036 (struct sep_identify_data *)&path->device->ident_data);
1037 else if (path->device->protocol == PROTO_NVME)
1038 nvme_print_ident(path->device->nvme_cdata, path->device->nvme_data);
1040 printf("Unknown protocol device\n");
1041 if (path->device->serial_num_len > 0) {
1042 /* Don't wrap the screen - print only the first 60 chars */
1043 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1044 periph->unit_number, path->device->serial_num);
1046 /* Announce transport details. */
1047 (*(path->bus->xport->announce))(periph);
1048 /* Announce command queueing. */
1049 if (path->device->inq_flags & SID_CmdQue
1050 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1051 printf("%s%d: Command Queueing enabled\n",
1052 periph->periph_name, periph->unit_number);
1054 /* Announce caller's details if they've passed in. */
1055 if (announce_string != NULL)
1056 printf("%s%d: %s\n", periph->periph_name,
1057 periph->unit_number, announce_string);
1061 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1064 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1065 periph->unit_number, quirks, bit_string);
1070 xpt_denounce_periph(struct cam_periph *periph)
1072 struct cam_path *path = periph->path;
1074 cam_periph_assert(periph, MA_OWNED);
1075 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1076 periph->periph_name, periph->unit_number,
1077 path->bus->sim->sim_name,
1078 path->bus->sim->unit_number,
1079 path->bus->sim->bus_id,
1081 path->target->target_id,
1082 (uintmax_t)path->device->lun_id);
1083 printf("%s%d: ", periph->periph_name, periph->unit_number);
1084 if (path->device->protocol == PROTO_SCSI)
1085 scsi_print_inquiry_short(&path->device->inq_data);
1086 else if (path->device->protocol == PROTO_ATA ||
1087 path->device->protocol == PROTO_SATAPM)
1088 ata_print_ident_short(&path->device->ident_data);
1089 else if (path->device->protocol == PROTO_SEMB)
1090 semb_print_ident_short(
1091 (struct sep_identify_data *)&path->device->ident_data);
1092 else if (path->device->protocol == PROTO_NVME)
1093 nvme_print_ident(path->device->nvme_cdata, path->device->nvme_data);
1095 printf("Unknown protocol device");
1096 if (path->device->serial_num_len > 0)
1097 printf(" s/n %.60s", path->device->serial_num);
1098 printf(" detached\n");
1103 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1106 struct ccb_dev_advinfo cdai;
1107 struct scsi_vpd_id_descriptor *idd;
1109 xpt_path_assert(path, MA_OWNED);
1111 memset(&cdai, 0, sizeof(cdai));
1112 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1113 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1114 cdai.flags = CDAI_FLAG_NONE;
1117 if (!strcmp(attr, "GEOM::ident"))
1118 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1119 else if (!strcmp(attr, "GEOM::physpath"))
1120 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1121 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1122 strcmp(attr, "GEOM::lunname") == 0) {
1123 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1124 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1128 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1129 if (cdai.buf == NULL) {
1133 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1134 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1135 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1136 if (cdai.provsiz == 0)
1138 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1139 if (strcmp(attr, "GEOM::lunid") == 0) {
1140 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1141 cdai.provsiz, scsi_devid_is_lun_naa);
1143 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1144 cdai.provsiz, scsi_devid_is_lun_eui64);
1146 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1147 cdai.provsiz, scsi_devid_is_lun_uuid);
1149 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1150 cdai.provsiz, scsi_devid_is_lun_md5);
1154 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1155 cdai.provsiz, scsi_devid_is_lun_t10);
1157 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1158 cdai.provsiz, scsi_devid_is_lun_name);
1162 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1163 if (idd->length < len) {
1164 for (l = 0; l < idd->length; l++)
1165 buf[l] = idd->identifier[l] ?
1166 idd->identifier[l] : ' ';
1170 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1171 l = strnlen(idd->identifier, idd->length);
1173 bcopy(idd->identifier, buf, l);
1177 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1178 && idd->identifier[0] == 0x10) {
1179 if ((idd->length - 2) * 2 + 4 < len) {
1180 for (l = 2, o = 0; l < idd->length; l++) {
1181 if (l == 6 || l == 8 || l == 10 || l == 12)
1182 o += sprintf(buf + o, "-");
1183 o += sprintf(buf + o, "%02x",
1184 idd->identifier[l]);
1189 if (idd->length * 2 < len) {
1190 for (l = 0; l < idd->length; l++)
1191 sprintf(buf + l * 2, "%02x",
1192 idd->identifier[l]);
1198 if (strlcpy(buf, cdai.buf, len) >= len)
1203 if (cdai.buf != NULL)
1204 free(cdai.buf, M_CAMXPT);
1208 static dev_match_ret
1209 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1212 dev_match_ret retval;
1215 retval = DM_RET_NONE;
1218 * If we aren't given something to match against, that's an error.
1221 return(DM_RET_ERROR);
1224 * If there are no match entries, then this bus matches no
1227 if ((patterns == NULL) || (num_patterns == 0))
1228 return(DM_RET_DESCEND | DM_RET_COPY);
1230 for (i = 0; i < num_patterns; i++) {
1231 struct bus_match_pattern *cur_pattern;
1234 * If the pattern in question isn't for a bus node, we
1235 * aren't interested. However, we do indicate to the
1236 * calling routine that we should continue descending the
1237 * tree, since the user wants to match against lower-level
1240 if (patterns[i].type != DEV_MATCH_BUS) {
1241 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1242 retval |= DM_RET_DESCEND;
1246 cur_pattern = &patterns[i].pattern.bus_pattern;
1249 * If they want to match any bus node, we give them any
1252 if (cur_pattern->flags == BUS_MATCH_ANY) {
1253 /* set the copy flag */
1254 retval |= DM_RET_COPY;
1257 * If we've already decided on an action, go ahead
1260 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1265 * Not sure why someone would do this...
1267 if (cur_pattern->flags == BUS_MATCH_NONE)
1270 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1271 && (cur_pattern->path_id != bus->path_id))
1274 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1275 && (cur_pattern->bus_id != bus->sim->bus_id))
1278 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1279 && (cur_pattern->unit_number != bus->sim->unit_number))
1282 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1283 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1288 * If we get to this point, the user definitely wants
1289 * information on this bus. So tell the caller to copy the
1292 retval |= DM_RET_COPY;
1295 * If the return action has been set to descend, then we
1296 * know that we've already seen a non-bus matching
1297 * expression, therefore we need to further descend the tree.
1298 * This won't change by continuing around the loop, so we
1299 * go ahead and return. If we haven't seen a non-bus
1300 * matching expression, we keep going around the loop until
1301 * we exhaust the matching expressions. We'll set the stop
1302 * flag once we fall out of the loop.
1304 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1309 * If the return action hasn't been set to descend yet, that means
1310 * we haven't seen anything other than bus matching patterns. So
1311 * tell the caller to stop descending the tree -- the user doesn't
1312 * want to match against lower level tree elements.
1314 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1315 retval |= DM_RET_STOP;
1320 static dev_match_ret
1321 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1322 struct cam_ed *device)
1324 dev_match_ret retval;
1327 retval = DM_RET_NONE;
1330 * If we aren't given something to match against, that's an error.
1333 return(DM_RET_ERROR);
1336 * If there are no match entries, then this device matches no
1339 if ((patterns == NULL) || (num_patterns == 0))
1340 return(DM_RET_DESCEND | DM_RET_COPY);
1342 for (i = 0; i < num_patterns; i++) {
1343 struct device_match_pattern *cur_pattern;
1344 struct scsi_vpd_device_id *device_id_page;
1347 * If the pattern in question isn't for a device node, we
1348 * aren't interested.
1350 if (patterns[i].type != DEV_MATCH_DEVICE) {
1351 if ((patterns[i].type == DEV_MATCH_PERIPH)
1352 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1353 retval |= DM_RET_DESCEND;
1357 cur_pattern = &patterns[i].pattern.device_pattern;
1359 /* Error out if mutually exclusive options are specified. */
1360 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1361 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1362 return(DM_RET_ERROR);
1365 * If they want to match any device node, we give them any
1368 if (cur_pattern->flags == DEV_MATCH_ANY)
1372 * Not sure why someone would do this...
1374 if (cur_pattern->flags == DEV_MATCH_NONE)
1377 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1378 && (cur_pattern->path_id != device->target->bus->path_id))
1381 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1382 && (cur_pattern->target_id != device->target->target_id))
1385 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1386 && (cur_pattern->target_lun != device->lun_id))
1389 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1390 && (cam_quirkmatch((caddr_t)&device->inq_data,
1391 (caddr_t)&cur_pattern->data.inq_pat,
1392 1, sizeof(cur_pattern->data.inq_pat),
1393 scsi_static_inquiry_match) == NULL))
1396 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1397 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1398 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1399 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1400 device->device_id_len
1401 - SVPD_DEVICE_ID_HDR_LEN,
1402 cur_pattern->data.devid_pat.id,
1403 cur_pattern->data.devid_pat.id_len) != 0))
1408 * If we get to this point, the user definitely wants
1409 * information on this device. So tell the caller to copy
1412 retval |= DM_RET_COPY;
1415 * If the return action has been set to descend, then we
1416 * know that we've already seen a peripheral matching
1417 * expression, therefore we need to further descend the tree.
1418 * This won't change by continuing around the loop, so we
1419 * go ahead and return. If we haven't seen a peripheral
1420 * matching expression, we keep going around the loop until
1421 * we exhaust the matching expressions. We'll set the stop
1422 * flag once we fall out of the loop.
1424 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1429 * If the return action hasn't been set to descend yet, that means
1430 * we haven't seen any peripheral matching patterns. So tell the
1431 * caller to stop descending the tree -- the user doesn't want to
1432 * match against lower level tree elements.
1434 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1435 retval |= DM_RET_STOP;
1441 * Match a single peripheral against any number of match patterns.
1443 static dev_match_ret
1444 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1445 struct cam_periph *periph)
1447 dev_match_ret retval;
1451 * If we aren't given something to match against, that's an error.
1454 return(DM_RET_ERROR);
1457 * If there are no match entries, then this peripheral matches no
1460 if ((patterns == NULL) || (num_patterns == 0))
1461 return(DM_RET_STOP | DM_RET_COPY);
1464 * There aren't any nodes below a peripheral node, so there's no
1465 * reason to descend the tree any further.
1467 retval = DM_RET_STOP;
1469 for (i = 0; i < num_patterns; i++) {
1470 struct periph_match_pattern *cur_pattern;
1473 * If the pattern in question isn't for a peripheral, we
1474 * aren't interested.
1476 if (patterns[i].type != DEV_MATCH_PERIPH)
1479 cur_pattern = &patterns[i].pattern.periph_pattern;
1482 * If they want to match on anything, then we will do so.
1484 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1485 /* set the copy flag */
1486 retval |= DM_RET_COPY;
1489 * We've already set the return action to stop,
1490 * since there are no nodes below peripherals in
1497 * Not sure why someone would do this...
1499 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1502 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1503 && (cur_pattern->path_id != periph->path->bus->path_id))
1507 * For the target and lun id's, we have to make sure the
1508 * target and lun pointers aren't NULL. The xpt peripheral
1509 * has a wildcard target and device.
1511 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1512 && ((periph->path->target == NULL)
1513 ||(cur_pattern->target_id != periph->path->target->target_id)))
1516 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1517 && ((periph->path->device == NULL)
1518 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1521 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1522 && (cur_pattern->unit_number != periph->unit_number))
1525 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1526 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1531 * If we get to this point, the user definitely wants
1532 * information on this peripheral. So tell the caller to
1533 * copy the data out.
1535 retval |= DM_RET_COPY;
1538 * The return action has already been set to stop, since
1539 * peripherals don't have any nodes below them in the EDT.
1545 * If we get to this point, the peripheral that was passed in
1546 * doesn't match any of the patterns.
1552 xptedtbusfunc(struct cam_eb *bus, void *arg)
1554 struct ccb_dev_match *cdm;
1555 struct cam_et *target;
1556 dev_match_ret retval;
1558 cdm = (struct ccb_dev_match *)arg;
1561 * If our position is for something deeper in the tree, that means
1562 * that we've already seen this node. So, we keep going down.
1564 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1565 && (cdm->pos.cookie.bus == bus)
1566 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1567 && (cdm->pos.cookie.target != NULL))
1568 retval = DM_RET_DESCEND;
1570 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1573 * If we got an error, bail out of the search.
1575 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1576 cdm->status = CAM_DEV_MATCH_ERROR;
1581 * If the copy flag is set, copy this bus out.
1583 if (retval & DM_RET_COPY) {
1586 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1587 sizeof(struct dev_match_result));
1590 * If we don't have enough space to put in another
1591 * match result, save our position and tell the
1592 * user there are more devices to check.
1594 if (spaceleft < sizeof(struct dev_match_result)) {
1595 bzero(&cdm->pos, sizeof(cdm->pos));
1596 cdm->pos.position_type =
1597 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1599 cdm->pos.cookie.bus = bus;
1600 cdm->pos.generations[CAM_BUS_GENERATION]=
1601 xsoftc.bus_generation;
1602 cdm->status = CAM_DEV_MATCH_MORE;
1605 j = cdm->num_matches;
1607 cdm->matches[j].type = DEV_MATCH_BUS;
1608 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1609 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1610 cdm->matches[j].result.bus_result.unit_number =
1611 bus->sim->unit_number;
1612 strlcpy(cdm->matches[j].result.bus_result.dev_name,
1614 sizeof(cdm->matches[j].result.bus_result.dev_name));
1618 * If the user is only interested in busses, there's no
1619 * reason to descend to the next level in the tree.
1621 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1625 * If there is a target generation recorded, check it to
1626 * make sure the target list hasn't changed.
1628 mtx_lock(&bus->eb_mtx);
1629 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1630 && (cdm->pos.cookie.bus == bus)
1631 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1632 && (cdm->pos.cookie.target != NULL)) {
1633 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1635 mtx_unlock(&bus->eb_mtx);
1636 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1639 target = (struct cam_et *)cdm->pos.cookie.target;
1643 mtx_unlock(&bus->eb_mtx);
1645 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1649 xptedttargetfunc(struct cam_et *target, void *arg)
1651 struct ccb_dev_match *cdm;
1653 struct cam_ed *device;
1655 cdm = (struct ccb_dev_match *)arg;
1659 * If there is a device list generation recorded, check it to
1660 * make sure the device list hasn't changed.
1662 mtx_lock(&bus->eb_mtx);
1663 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1664 && (cdm->pos.cookie.bus == bus)
1665 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1666 && (cdm->pos.cookie.target == target)
1667 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1668 && (cdm->pos.cookie.device != NULL)) {
1669 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1670 target->generation) {
1671 mtx_unlock(&bus->eb_mtx);
1672 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1675 device = (struct cam_ed *)cdm->pos.cookie.device;
1679 mtx_unlock(&bus->eb_mtx);
1681 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1685 xptedtdevicefunc(struct cam_ed *device, void *arg)
1688 struct cam_periph *periph;
1689 struct ccb_dev_match *cdm;
1690 dev_match_ret retval;
1692 cdm = (struct ccb_dev_match *)arg;
1693 bus = device->target->bus;
1696 * If our position is for something deeper in the tree, that means
1697 * that we've already seen this node. So, we keep going down.
1699 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1700 && (cdm->pos.cookie.device == device)
1701 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1702 && (cdm->pos.cookie.periph != NULL))
1703 retval = DM_RET_DESCEND;
1705 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1708 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1709 cdm->status = CAM_DEV_MATCH_ERROR;
1714 * If the copy flag is set, copy this device out.
1716 if (retval & DM_RET_COPY) {
1719 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1720 sizeof(struct dev_match_result));
1723 * If we don't have enough space to put in another
1724 * match result, save our position and tell the
1725 * user there are more devices to check.
1727 if (spaceleft < sizeof(struct dev_match_result)) {
1728 bzero(&cdm->pos, sizeof(cdm->pos));
1729 cdm->pos.position_type =
1730 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1731 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1733 cdm->pos.cookie.bus = device->target->bus;
1734 cdm->pos.generations[CAM_BUS_GENERATION]=
1735 xsoftc.bus_generation;
1736 cdm->pos.cookie.target = device->target;
1737 cdm->pos.generations[CAM_TARGET_GENERATION] =
1738 device->target->bus->generation;
1739 cdm->pos.cookie.device = device;
1740 cdm->pos.generations[CAM_DEV_GENERATION] =
1741 device->target->generation;
1742 cdm->status = CAM_DEV_MATCH_MORE;
1745 j = cdm->num_matches;
1747 cdm->matches[j].type = DEV_MATCH_DEVICE;
1748 cdm->matches[j].result.device_result.path_id =
1749 device->target->bus->path_id;
1750 cdm->matches[j].result.device_result.target_id =
1751 device->target->target_id;
1752 cdm->matches[j].result.device_result.target_lun =
1754 cdm->matches[j].result.device_result.protocol =
1756 bcopy(&device->inq_data,
1757 &cdm->matches[j].result.device_result.inq_data,
1758 sizeof(struct scsi_inquiry_data));
1759 bcopy(&device->ident_data,
1760 &cdm->matches[j].result.device_result.ident_data,
1761 sizeof(struct ata_params));
1763 /* Let the user know whether this device is unconfigured */
1764 if (device->flags & CAM_DEV_UNCONFIGURED)
1765 cdm->matches[j].result.device_result.flags =
1766 DEV_RESULT_UNCONFIGURED;
1768 cdm->matches[j].result.device_result.flags =
1773 * If the user isn't interested in peripherals, don't descend
1774 * the tree any further.
1776 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1780 * If there is a peripheral list generation recorded, make sure
1781 * it hasn't changed.
1784 mtx_lock(&bus->eb_mtx);
1785 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1786 && (cdm->pos.cookie.bus == bus)
1787 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1788 && (cdm->pos.cookie.target == device->target)
1789 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1790 && (cdm->pos.cookie.device == device)
1791 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1792 && (cdm->pos.cookie.periph != NULL)) {
1793 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1794 device->generation) {
1795 mtx_unlock(&bus->eb_mtx);
1797 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1800 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1804 mtx_unlock(&bus->eb_mtx);
1807 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1811 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1813 struct ccb_dev_match *cdm;
1814 dev_match_ret retval;
1816 cdm = (struct ccb_dev_match *)arg;
1818 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1820 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1821 cdm->status = CAM_DEV_MATCH_ERROR;
1826 * If the copy flag is set, copy this peripheral out.
1828 if (retval & DM_RET_COPY) {
1832 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1833 sizeof(struct dev_match_result));
1836 * If we don't have enough space to put in another
1837 * match result, save our position and tell the
1838 * user there are more devices to check.
1840 if (spaceleft < sizeof(struct dev_match_result)) {
1841 bzero(&cdm->pos, sizeof(cdm->pos));
1842 cdm->pos.position_type =
1843 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1844 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1847 cdm->pos.cookie.bus = periph->path->bus;
1848 cdm->pos.generations[CAM_BUS_GENERATION]=
1849 xsoftc.bus_generation;
1850 cdm->pos.cookie.target = periph->path->target;
1851 cdm->pos.generations[CAM_TARGET_GENERATION] =
1852 periph->path->bus->generation;
1853 cdm->pos.cookie.device = periph->path->device;
1854 cdm->pos.generations[CAM_DEV_GENERATION] =
1855 periph->path->target->generation;
1856 cdm->pos.cookie.periph = periph;
1857 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1858 periph->path->device->generation;
1859 cdm->status = CAM_DEV_MATCH_MORE;
1863 j = cdm->num_matches;
1865 cdm->matches[j].type = DEV_MATCH_PERIPH;
1866 cdm->matches[j].result.periph_result.path_id =
1867 periph->path->bus->path_id;
1868 cdm->matches[j].result.periph_result.target_id =
1869 periph->path->target->target_id;
1870 cdm->matches[j].result.periph_result.target_lun =
1871 periph->path->device->lun_id;
1872 cdm->matches[j].result.periph_result.unit_number =
1873 periph->unit_number;
1874 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
1875 strlcpy(cdm->matches[j].result.periph_result.periph_name,
1876 periph->periph_name, l);
1883 xptedtmatch(struct ccb_dev_match *cdm)
1888 cdm->num_matches = 0;
1891 * Check the bus list generation. If it has changed, the user
1892 * needs to reset everything and start over.
1895 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1896 && (cdm->pos.cookie.bus != NULL)) {
1897 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1898 xsoftc.bus_generation) {
1900 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1903 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1909 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1912 * If we get back 0, that means that we had to stop before fully
1913 * traversing the EDT. It also means that one of the subroutines
1914 * has set the status field to the proper value. If we get back 1,
1915 * we've fully traversed the EDT and copied out any matching entries.
1918 cdm->status = CAM_DEV_MATCH_LAST;
1924 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1926 struct cam_periph *periph;
1927 struct ccb_dev_match *cdm;
1929 cdm = (struct ccb_dev_match *)arg;
1932 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1933 && (cdm->pos.cookie.pdrv == pdrv)
1934 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1935 && (cdm->pos.cookie.periph != NULL)) {
1936 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1937 (*pdrv)->generation) {
1939 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1942 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1948 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1952 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1954 struct ccb_dev_match *cdm;
1955 dev_match_ret retval;
1957 cdm = (struct ccb_dev_match *)arg;
1959 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1961 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1962 cdm->status = CAM_DEV_MATCH_ERROR;
1967 * If the copy flag is set, copy this peripheral out.
1969 if (retval & DM_RET_COPY) {
1973 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1974 sizeof(struct dev_match_result));
1977 * If we don't have enough space to put in another
1978 * match result, save our position and tell the
1979 * user there are more devices to check.
1981 if (spaceleft < sizeof(struct dev_match_result)) {
1982 struct periph_driver **pdrv;
1985 bzero(&cdm->pos, sizeof(cdm->pos));
1986 cdm->pos.position_type =
1987 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1991 * This may look a bit non-sensical, but it is
1992 * actually quite logical. There are very few
1993 * peripheral drivers, and bloating every peripheral
1994 * structure with a pointer back to its parent
1995 * peripheral driver linker set entry would cost
1996 * more in the long run than doing this quick lookup.
1998 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1999 if (strcmp((*pdrv)->driver_name,
2000 periph->periph_name) == 0)
2004 if (*pdrv == NULL) {
2005 cdm->status = CAM_DEV_MATCH_ERROR;
2009 cdm->pos.cookie.pdrv = pdrv;
2011 * The periph generation slot does double duty, as
2012 * does the periph pointer slot. They are used for
2013 * both edt and pdrv lookups and positioning.
2015 cdm->pos.cookie.periph = periph;
2016 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2017 (*pdrv)->generation;
2018 cdm->status = CAM_DEV_MATCH_MORE;
2022 j = cdm->num_matches;
2024 cdm->matches[j].type = DEV_MATCH_PERIPH;
2025 cdm->matches[j].result.periph_result.path_id =
2026 periph->path->bus->path_id;
2029 * The transport layer peripheral doesn't have a target or
2032 if (periph->path->target)
2033 cdm->matches[j].result.periph_result.target_id =
2034 periph->path->target->target_id;
2036 cdm->matches[j].result.periph_result.target_id =
2037 CAM_TARGET_WILDCARD;
2039 if (periph->path->device)
2040 cdm->matches[j].result.periph_result.target_lun =
2041 periph->path->device->lun_id;
2043 cdm->matches[j].result.periph_result.target_lun =
2046 cdm->matches[j].result.periph_result.unit_number =
2047 periph->unit_number;
2048 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2049 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2050 periph->periph_name, l);
2057 xptperiphlistmatch(struct ccb_dev_match *cdm)
2061 cdm->num_matches = 0;
2064 * At this point in the edt traversal function, we check the bus
2065 * list generation to make sure that no busses have been added or
2066 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2067 * For the peripheral driver list traversal function, however, we
2068 * don't have to worry about new peripheral driver types coming or
2069 * going; they're in a linker set, and therefore can't change
2070 * without a recompile.
2073 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2074 && (cdm->pos.cookie.pdrv != NULL))
2075 ret = xptpdrvtraverse(
2076 (struct periph_driver **)cdm->pos.cookie.pdrv,
2077 xptplistpdrvfunc, cdm);
2079 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2082 * If we get back 0, that means that we had to stop before fully
2083 * traversing the peripheral driver tree. It also means that one of
2084 * the subroutines has set the status field to the proper value. If
2085 * we get back 1, we've fully traversed the EDT and copied out any
2089 cdm->status = CAM_DEV_MATCH_LAST;
2095 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2097 struct cam_eb *bus, *next_bus;
2105 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2113 for (; bus != NULL; bus = next_bus) {
2114 retval = tr_func(bus, arg);
2116 xpt_release_bus(bus);
2120 next_bus = TAILQ_NEXT(bus, links);
2122 next_bus->refcount++;
2124 xpt_release_bus(bus);
2130 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2131 xpt_targetfunc_t *tr_func, void *arg)
2133 struct cam_et *target, *next_target;
2138 target = start_target;
2140 mtx_lock(&bus->eb_mtx);
2141 target = TAILQ_FIRST(&bus->et_entries);
2142 if (target == NULL) {
2143 mtx_unlock(&bus->eb_mtx);
2147 mtx_unlock(&bus->eb_mtx);
2149 for (; target != NULL; target = next_target) {
2150 retval = tr_func(target, arg);
2152 xpt_release_target(target);
2155 mtx_lock(&bus->eb_mtx);
2156 next_target = TAILQ_NEXT(target, links);
2158 next_target->refcount++;
2159 mtx_unlock(&bus->eb_mtx);
2160 xpt_release_target(target);
2166 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2167 xpt_devicefunc_t *tr_func, void *arg)
2170 struct cam_ed *device, *next_device;
2176 device = start_device;
2178 mtx_lock(&bus->eb_mtx);
2179 device = TAILQ_FIRST(&target->ed_entries);
2180 if (device == NULL) {
2181 mtx_unlock(&bus->eb_mtx);
2185 mtx_unlock(&bus->eb_mtx);
2187 for (; device != NULL; device = next_device) {
2188 mtx_lock(&device->device_mtx);
2189 retval = tr_func(device, arg);
2190 mtx_unlock(&device->device_mtx);
2192 xpt_release_device(device);
2195 mtx_lock(&bus->eb_mtx);
2196 next_device = TAILQ_NEXT(device, links);
2198 next_device->refcount++;
2199 mtx_unlock(&bus->eb_mtx);
2200 xpt_release_device(device);
2206 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2207 xpt_periphfunc_t *tr_func, void *arg)
2210 struct cam_periph *periph, *next_periph;
2215 bus = device->target->bus;
2217 periph = start_periph;
2220 mtx_lock(&bus->eb_mtx);
2221 periph = SLIST_FIRST(&device->periphs);
2222 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2223 periph = SLIST_NEXT(periph, periph_links);
2224 if (periph == NULL) {
2225 mtx_unlock(&bus->eb_mtx);
2230 mtx_unlock(&bus->eb_mtx);
2233 for (; periph != NULL; periph = next_periph) {
2234 retval = tr_func(periph, arg);
2236 cam_periph_release_locked(periph);
2240 mtx_lock(&bus->eb_mtx);
2241 next_periph = SLIST_NEXT(periph, periph_links);
2242 while (next_periph != NULL &&
2243 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2244 next_periph = SLIST_NEXT(next_periph, periph_links);
2246 next_periph->refcount++;
2247 mtx_unlock(&bus->eb_mtx);
2249 cam_periph_release_locked(periph);
2255 xptpdrvtraverse(struct periph_driver **start_pdrv,
2256 xpt_pdrvfunc_t *tr_func, void *arg)
2258 struct periph_driver **pdrv;
2264 * We don't traverse the peripheral driver list like we do the
2265 * other lists, because it is a linker set, and therefore cannot be
2266 * changed during runtime. If the peripheral driver list is ever
2267 * re-done to be something other than a linker set (i.e. it can
2268 * change while the system is running), the list traversal should
2269 * be modified to work like the other traversal functions.
2271 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2272 *pdrv != NULL; pdrv++) {
2273 retval = tr_func(pdrv, arg);
2283 xptpdperiphtraverse(struct periph_driver **pdrv,
2284 struct cam_periph *start_periph,
2285 xpt_periphfunc_t *tr_func, void *arg)
2287 struct cam_periph *periph, *next_periph;
2293 periph = start_periph;
2296 periph = TAILQ_FIRST(&(*pdrv)->units);
2297 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2298 periph = TAILQ_NEXT(periph, unit_links);
2299 if (periph == NULL) {
2306 for (; periph != NULL; periph = next_periph) {
2307 cam_periph_lock(periph);
2308 retval = tr_func(periph, arg);
2309 cam_periph_unlock(periph);
2311 cam_periph_release(periph);
2315 next_periph = TAILQ_NEXT(periph, unit_links);
2316 while (next_periph != NULL &&
2317 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2318 next_periph = TAILQ_NEXT(next_periph, unit_links);
2320 next_periph->refcount++;
2322 cam_periph_release(periph);
2328 xptdefbusfunc(struct cam_eb *bus, void *arg)
2330 struct xpt_traverse_config *tr_config;
2332 tr_config = (struct xpt_traverse_config *)arg;
2334 if (tr_config->depth == XPT_DEPTH_BUS) {
2335 xpt_busfunc_t *tr_func;
2337 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2339 return(tr_func(bus, tr_config->tr_arg));
2341 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2345 xptdeftargetfunc(struct cam_et *target, void *arg)
2347 struct xpt_traverse_config *tr_config;
2349 tr_config = (struct xpt_traverse_config *)arg;
2351 if (tr_config->depth == XPT_DEPTH_TARGET) {
2352 xpt_targetfunc_t *tr_func;
2354 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2356 return(tr_func(target, tr_config->tr_arg));
2358 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2362 xptdefdevicefunc(struct cam_ed *device, void *arg)
2364 struct xpt_traverse_config *tr_config;
2366 tr_config = (struct xpt_traverse_config *)arg;
2368 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2369 xpt_devicefunc_t *tr_func;
2371 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2373 return(tr_func(device, tr_config->tr_arg));
2375 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2379 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2381 struct xpt_traverse_config *tr_config;
2382 xpt_periphfunc_t *tr_func;
2384 tr_config = (struct xpt_traverse_config *)arg;
2386 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2389 * Unlike the other default functions, we don't check for depth
2390 * here. The peripheral driver level is the last level in the EDT,
2391 * so if we're here, we should execute the function in question.
2393 return(tr_func(periph, tr_config->tr_arg));
2397 * Execute the given function for every bus in the EDT.
2400 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2402 struct xpt_traverse_config tr_config;
2404 tr_config.depth = XPT_DEPTH_BUS;
2405 tr_config.tr_func = tr_func;
2406 tr_config.tr_arg = arg;
2408 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2412 * Execute the given function for every device in the EDT.
2415 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2417 struct xpt_traverse_config tr_config;
2419 tr_config.depth = XPT_DEPTH_DEVICE;
2420 tr_config.tr_func = tr_func;
2421 tr_config.tr_arg = arg;
2423 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2427 xptsetasyncfunc(struct cam_ed *device, void *arg)
2429 struct cam_path path;
2430 struct ccb_getdev cgd;
2431 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2434 * Don't report unconfigured devices (Wildcard devs,
2435 * devices only for target mode, device instances
2436 * that have been invalidated but are waiting for
2437 * their last reference count to be released).
2439 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2442 xpt_compile_path(&path,
2444 device->target->bus->path_id,
2445 device->target->target_id,
2447 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2448 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2449 xpt_action((union ccb *)&cgd);
2450 csa->callback(csa->callback_arg,
2453 xpt_release_path(&path);
2459 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2461 struct cam_path path;
2462 struct ccb_pathinq cpi;
2463 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2465 xpt_compile_path(&path, /*periph*/NULL,
2467 CAM_TARGET_WILDCARD,
2469 xpt_path_lock(&path);
2470 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2471 cpi.ccb_h.func_code = XPT_PATH_INQ;
2472 xpt_action((union ccb *)&cpi);
2473 csa->callback(csa->callback_arg,
2476 xpt_path_unlock(&path);
2477 xpt_release_path(&path);
2483 xpt_action(union ccb *start_ccb)
2486 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2487 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2488 xpt_action_name(start_ccb->ccb_h.func_code)));
2490 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2491 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2495 xpt_action_default(union ccb *start_ccb)
2497 struct cam_path *path;
2498 struct cam_sim *sim;
2501 path = start_ccb->ccb_h.path;
2502 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2503 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2504 xpt_action_name(start_ccb->ccb_h.func_code)));
2506 switch (start_ccb->ccb_h.func_code) {
2509 struct cam_ed *device;
2512 * For the sake of compatibility with SCSI-1
2513 * devices that may not understand the identify
2514 * message, we include lun information in the
2515 * second byte of all commands. SCSI-1 specifies
2516 * that luns are a 3 bit value and reserves only 3
2517 * bits for lun information in the CDB. Later
2518 * revisions of the SCSI spec allow for more than 8
2519 * luns, but have deprecated lun information in the
2520 * CDB. So, if the lun won't fit, we must omit.
2522 * Also be aware that during initial probing for devices,
2523 * the inquiry information is unknown but initialized to 0.
2524 * This means that this code will be exercised while probing
2525 * devices with an ANSI revision greater than 2.
2527 device = path->device;
2528 if (device->protocol_version <= SCSI_REV_2
2529 && start_ccb->ccb_h.target_lun < 8
2530 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2532 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2533 start_ccb->ccb_h.target_lun << 5;
2535 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2539 case XPT_CONT_TARGET_IO:
2540 start_ccb->csio.sense_resid = 0;
2541 start_ccb->csio.resid = 0;
2544 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2545 start_ccb->ataio.resid = 0;
2548 if (start_ccb->ccb_h.func_code == XPT_NVME_IO)
2549 start_ccb->nvmeio.resid = 0;
2555 struct cam_devq *devq;
2557 devq = path->bus->sim->devq;
2558 mtx_lock(&devq->send_mtx);
2559 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2560 if (xpt_schedule_devq(devq, path->device) != 0)
2562 mtx_unlock(&devq->send_mtx);
2565 case XPT_CALC_GEOMETRY:
2566 /* Filter out garbage */
2567 if (start_ccb->ccg.block_size == 0
2568 || start_ccb->ccg.volume_size == 0) {
2569 start_ccb->ccg.cylinders = 0;
2570 start_ccb->ccg.heads = 0;
2571 start_ccb->ccg.secs_per_track = 0;
2572 start_ccb->ccb_h.status = CAM_REQ_CMP;
2575 #if defined(PC98) || defined(__sparc64__)
2577 * In a PC-98 system, geometry translation depens on
2578 * the "real" device geometry obtained from mode page 4.
2579 * SCSI geometry translation is performed in the
2580 * initialization routine of the SCSI BIOS and the result
2581 * stored in host memory. If the translation is available
2582 * in host memory, use it. If not, rely on the default
2583 * translation the device driver performs.
2584 * For sparc64, we may need adjust the geometry of large
2585 * disks in order to fit the limitations of the 16-bit
2586 * fields of the VTOC8 disk label.
2588 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2589 start_ccb->ccb_h.status = CAM_REQ_CMP;
2596 union ccb* abort_ccb;
2598 abort_ccb = start_ccb->cab.abort_ccb;
2599 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2600 struct cam_ed *device;
2601 struct cam_devq *devq;
2603 device = abort_ccb->ccb_h.path->device;
2604 devq = device->sim->devq;
2606 mtx_lock(&devq->send_mtx);
2607 if (abort_ccb->ccb_h.pinfo.index > 0) {
2608 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2609 abort_ccb->ccb_h.status =
2610 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2611 xpt_freeze_devq_device(device, 1);
2612 mtx_unlock(&devq->send_mtx);
2613 xpt_done(abort_ccb);
2614 start_ccb->ccb_h.status = CAM_REQ_CMP;
2617 mtx_unlock(&devq->send_mtx);
2619 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2620 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2622 * We've caught this ccb en route to
2623 * the SIM. Flag it for abort and the
2624 * SIM will do so just before starting
2625 * real work on the CCB.
2627 abort_ccb->ccb_h.status =
2628 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2629 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2630 start_ccb->ccb_h.status = CAM_REQ_CMP;
2634 if (XPT_FC_IS_QUEUED(abort_ccb)
2635 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2637 * It's already completed but waiting
2638 * for our SWI to get to it.
2640 start_ccb->ccb_h.status = CAM_UA_ABORT;
2644 * If we weren't able to take care of the abort request
2645 * in the XPT, pass the request down to the SIM for processing.
2649 case XPT_ACCEPT_TARGET_IO:
2651 case XPT_IMMED_NOTIFY:
2652 case XPT_NOTIFY_ACK:
2654 case XPT_IMMEDIATE_NOTIFY:
2655 case XPT_NOTIFY_ACKNOWLEDGE:
2656 case XPT_GET_SIM_KNOB_OLD:
2657 case XPT_GET_SIM_KNOB:
2658 case XPT_SET_SIM_KNOB:
2659 case XPT_GET_TRAN_SETTINGS:
2660 case XPT_SET_TRAN_SETTINGS:
2663 sim = path->bus->sim;
2665 if (mtx && !mtx_owned(mtx))
2669 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2670 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2671 (*(sim->sim_action))(sim, start_ccb);
2672 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2673 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2677 case XPT_PATH_STATS:
2678 start_ccb->cpis.last_reset = path->bus->last_reset;
2679 start_ccb->ccb_h.status = CAM_REQ_CMP;
2686 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2687 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2689 struct ccb_getdev *cgd;
2691 cgd = &start_ccb->cgd;
2692 cgd->protocol = dev->protocol;
2693 cgd->inq_data = dev->inq_data;
2694 cgd->ident_data = dev->ident_data;
2695 cgd->inq_flags = dev->inq_flags;
2696 cgd->nvme_data = dev->nvme_data;
2697 cgd->nvme_cdata = dev->nvme_cdata;
2698 cgd->ccb_h.status = CAM_REQ_CMP;
2699 cgd->serial_num_len = dev->serial_num_len;
2700 if ((dev->serial_num_len > 0)
2701 && (dev->serial_num != NULL))
2702 bcopy(dev->serial_num, cgd->serial_num,
2703 dev->serial_num_len);
2707 case XPT_GDEV_STATS:
2709 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2710 struct cam_ed *dev = path->device;
2711 struct cam_eb *bus = path->bus;
2712 struct cam_et *tar = path->target;
2713 struct cam_devq *devq = bus->sim->devq;
2715 mtx_lock(&devq->send_mtx);
2716 cgds->dev_openings = dev->ccbq.dev_openings;
2717 cgds->dev_active = dev->ccbq.dev_active;
2718 cgds->allocated = dev->ccbq.allocated;
2719 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2720 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2721 cgds->last_reset = tar->last_reset;
2722 cgds->maxtags = dev->maxtags;
2723 cgds->mintags = dev->mintags;
2724 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2725 cgds->last_reset = bus->last_reset;
2726 mtx_unlock(&devq->send_mtx);
2727 cgds->ccb_h.status = CAM_REQ_CMP;
2732 struct cam_periph *nperiph;
2733 struct periph_list *periph_head;
2734 struct ccb_getdevlist *cgdl;
2736 struct cam_ed *device;
2743 * Don't want anyone mucking with our data.
2745 device = path->device;
2746 periph_head = &device->periphs;
2747 cgdl = &start_ccb->cgdl;
2750 * Check and see if the list has changed since the user
2751 * last requested a list member. If so, tell them that the
2752 * list has changed, and therefore they need to start over
2753 * from the beginning.
2755 if ((cgdl->index != 0) &&
2756 (cgdl->generation != device->generation)) {
2757 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2762 * Traverse the list of peripherals and attempt to find
2763 * the requested peripheral.
2765 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2766 (nperiph != NULL) && (i <= cgdl->index);
2767 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2768 if (i == cgdl->index) {
2769 strlcpy(cgdl->periph_name,
2770 nperiph->periph_name,
2771 sizeof(cgdl->periph_name));
2772 cgdl->unit_number = nperiph->unit_number;
2777 cgdl->status = CAM_GDEVLIST_ERROR;
2781 if (nperiph == NULL)
2782 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2784 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2787 cgdl->generation = device->generation;
2789 cgdl->ccb_h.status = CAM_REQ_CMP;
2794 dev_pos_type position_type;
2795 struct ccb_dev_match *cdm;
2797 cdm = &start_ccb->cdm;
2800 * There are two ways of getting at information in the EDT.
2801 * The first way is via the primary EDT tree. It starts
2802 * with a list of busses, then a list of targets on a bus,
2803 * then devices/luns on a target, and then peripherals on a
2804 * device/lun. The "other" way is by the peripheral driver
2805 * lists. The peripheral driver lists are organized by
2806 * peripheral driver. (obviously) So it makes sense to
2807 * use the peripheral driver list if the user is looking
2808 * for something like "da1", or all "da" devices. If the
2809 * user is looking for something on a particular bus/target
2810 * or lun, it's generally better to go through the EDT tree.
2813 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2814 position_type = cdm->pos.position_type;
2818 position_type = CAM_DEV_POS_NONE;
2820 for (i = 0; i < cdm->num_patterns; i++) {
2821 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2822 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2823 position_type = CAM_DEV_POS_EDT;
2828 if (cdm->num_patterns == 0)
2829 position_type = CAM_DEV_POS_EDT;
2830 else if (position_type == CAM_DEV_POS_NONE)
2831 position_type = CAM_DEV_POS_PDRV;
2834 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2835 case CAM_DEV_POS_EDT:
2838 case CAM_DEV_POS_PDRV:
2839 xptperiphlistmatch(cdm);
2842 cdm->status = CAM_DEV_MATCH_ERROR;
2846 if (cdm->status == CAM_DEV_MATCH_ERROR)
2847 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2849 start_ccb->ccb_h.status = CAM_REQ_CMP;
2855 struct ccb_setasync *csa;
2856 struct async_node *cur_entry;
2857 struct async_list *async_head;
2860 csa = &start_ccb->csa;
2861 added = csa->event_enable;
2862 async_head = &path->device->asyncs;
2865 * If there is already an entry for us, simply
2868 cur_entry = SLIST_FIRST(async_head);
2869 while (cur_entry != NULL) {
2870 if ((cur_entry->callback_arg == csa->callback_arg)
2871 && (cur_entry->callback == csa->callback))
2873 cur_entry = SLIST_NEXT(cur_entry, links);
2876 if (cur_entry != NULL) {
2878 * If the request has no flags set,
2881 added &= ~cur_entry->event_enable;
2882 if (csa->event_enable == 0) {
2883 SLIST_REMOVE(async_head, cur_entry,
2885 xpt_release_device(path->device);
2886 free(cur_entry, M_CAMXPT);
2888 cur_entry->event_enable = csa->event_enable;
2890 csa->event_enable = added;
2892 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2894 if (cur_entry == NULL) {
2895 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2898 cur_entry->event_enable = csa->event_enable;
2899 cur_entry->event_lock = (path->bus->sim->mtx &&
2900 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
2901 cur_entry->callback_arg = csa->callback_arg;
2902 cur_entry->callback = csa->callback;
2903 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2904 xpt_acquire_device(path->device);
2906 start_ccb->ccb_h.status = CAM_REQ_CMP;
2911 struct ccb_relsim *crs;
2914 crs = &start_ccb->crs;
2918 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2922 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2924 /* Don't ever go below one opening */
2925 if (crs->openings > 0) {
2926 xpt_dev_ccbq_resize(path, crs->openings);
2929 "number of openings is now %d\n",
2935 mtx_lock(&dev->sim->devq->send_mtx);
2936 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2938 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2941 * Just extend the old timeout and decrement
2942 * the freeze count so that a single timeout
2943 * is sufficient for releasing the queue.
2945 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2946 callout_stop(&dev->callout);
2949 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2952 callout_reset_sbt(&dev->callout,
2953 SBT_1MS * crs->release_timeout, 0,
2954 xpt_release_devq_timeout, dev, 0);
2956 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2960 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2962 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2964 * Decrement the freeze count so that a single
2965 * completion is still sufficient to unfreeze
2968 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2971 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2972 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2976 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2978 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2979 || (dev->ccbq.dev_active == 0)) {
2981 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2984 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2985 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2988 mtx_unlock(&dev->sim->devq->send_mtx);
2990 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2991 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2992 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2993 start_ccb->ccb_h.status = CAM_REQ_CMP;
2997 struct cam_path *oldpath;
2999 /* Check that all request bits are supported. */
3000 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3001 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3005 cam_dflags = CAM_DEBUG_NONE;
3006 if (cam_dpath != NULL) {
3007 oldpath = cam_dpath;
3009 xpt_free_path(oldpath);
3011 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3012 if (xpt_create_path(&cam_dpath, NULL,
3013 start_ccb->ccb_h.path_id,
3014 start_ccb->ccb_h.target_id,
3015 start_ccb->ccb_h.target_lun) !=
3017 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3019 cam_dflags = start_ccb->cdbg.flags;
3020 start_ccb->ccb_h.status = CAM_REQ_CMP;
3021 xpt_print(cam_dpath, "debugging flags now %x\n",
3025 start_ccb->ccb_h.status = CAM_REQ_CMP;
3029 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3030 xpt_freeze_devq(path, 1);
3031 start_ccb->ccb_h.status = CAM_REQ_CMP;
3033 case XPT_REPROBE_LUN:
3034 xpt_async(AC_INQ_CHANGED, path, NULL);
3035 start_ccb->ccb_h.status = CAM_REQ_CMP;
3036 xpt_done(start_ccb);
3043 xpt_print_path(start_ccb->ccb_h.path);
3044 printf("%s: CCB type %#x %s not supported\n", __func__,
3045 start_ccb->ccb_h.func_code,
3046 xpt_action_name(start_ccb->ccb_h.func_code));
3047 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3048 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3049 xpt_done(start_ccb);
3053 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3054 ("xpt_action_default: func= %#x %s status %#x\n",
3055 start_ccb->ccb_h.func_code,
3056 xpt_action_name(start_ccb->ccb_h.func_code),
3057 start_ccb->ccb_h.status));
3061 xpt_polled_action(union ccb *start_ccb)
3064 struct cam_sim *sim;
3065 struct cam_devq *devq;
3069 timeout = start_ccb->ccb_h.timeout * 10;
3070 sim = start_ccb->ccb_h.path->bus->sim;
3073 dev = start_ccb->ccb_h.path->device;
3075 mtx_unlock(&dev->device_mtx);
3078 * Steal an opening so that no other queued requests
3079 * can get it before us while we simulate interrupts.
3081 mtx_lock(&devq->send_mtx);
3082 dev->ccbq.dev_openings--;
3083 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3085 mtx_unlock(&devq->send_mtx);
3089 (*(sim->sim_poll))(sim);
3093 mtx_lock(&devq->send_mtx);
3095 dev->ccbq.dev_openings++;
3096 mtx_unlock(&devq->send_mtx);
3099 xpt_action(start_ccb);
3100 while(--timeout > 0) {
3103 (*(sim->sim_poll))(sim);
3107 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3114 * XXX Is it worth adding a sim_timeout entry
3115 * point so we can attempt recovery? If
3116 * this is only used for dumps, I don't think
3119 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3122 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3125 mtx_lock(&dev->device_mtx);
3129 * Schedule a peripheral driver to receive a ccb when its
3130 * target device has space for more transactions.
3133 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3136 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3137 cam_periph_assert(periph, MA_OWNED);
3138 if (new_priority < periph->scheduled_priority) {
3139 periph->scheduled_priority = new_priority;
3140 xpt_run_allocq(periph, 0);
3146 * Schedule a device to run on a given queue.
3147 * If the device was inserted as a new entry on the queue,
3148 * return 1 meaning the device queue should be run. If we
3149 * were already queued, implying someone else has already
3150 * started the queue, return 0 so the caller doesn't attempt
3154 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3155 u_int32_t new_priority)
3158 u_int32_t old_priority;
3160 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3162 old_priority = pinfo->priority;
3165 * Are we already queued?
3167 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3168 /* Simply reorder based on new priority */
3169 if (new_priority < old_priority) {
3170 camq_change_priority(queue, pinfo->index,
3172 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3173 ("changed priority to %d\n",
3179 /* New entry on the queue */
3180 if (new_priority < old_priority)
3181 pinfo->priority = new_priority;
3183 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3184 ("Inserting onto queue\n"));
3185 pinfo->generation = ++queue->generation;
3186 camq_insert(queue, pinfo);
3193 xpt_run_allocq_task(void *context, int pending)
3195 struct cam_periph *periph = context;
3197 cam_periph_lock(periph);
3198 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3199 xpt_run_allocq(periph, 1);
3200 cam_periph_unlock(periph);
3201 cam_periph_release(periph);
3205 xpt_run_allocq(struct cam_periph *periph, int sleep)
3207 struct cam_ed *device;
3211 cam_periph_assert(periph, MA_OWNED);
3212 if (periph->periph_allocating)
3214 periph->periph_allocating = 1;
3215 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3216 device = periph->path->device;
3219 while ((prio = min(periph->scheduled_priority,
3220 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3221 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3222 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3225 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3227 ccb = xpt_get_ccb(periph);
3230 if (periph->flags & CAM_PERIPH_RUN_TASK)
3232 cam_periph_doacquire(periph);
3233 periph->flags |= CAM_PERIPH_RUN_TASK;
3234 taskqueue_enqueue(xsoftc.xpt_taskq,
3235 &periph->periph_run_task);
3238 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3239 if (prio == periph->immediate_priority) {
3240 periph->immediate_priority = CAM_PRIORITY_NONE;
3241 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3242 ("waking cam_periph_getccb()\n"));
3243 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3245 wakeup(&periph->ccb_list);
3247 periph->scheduled_priority = CAM_PRIORITY_NONE;
3248 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3249 ("calling periph_start()\n"));
3250 periph->periph_start(periph, ccb);
3255 xpt_release_ccb(ccb);
3256 periph->periph_allocating = 0;
3260 xpt_run_devq(struct cam_devq *devq)
3262 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3265 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3267 devq->send_queue.qfrozen_cnt++;
3268 while ((devq->send_queue.entries > 0)
3269 && (devq->send_openings > 0)
3270 && (devq->send_queue.qfrozen_cnt <= 1)) {
3271 struct cam_ed *device;
3272 union ccb *work_ccb;
3273 struct cam_sim *sim;
3275 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3277 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3278 ("running device %p\n", device));
3280 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3281 if (work_ccb == NULL) {
3282 printf("device on run queue with no ccbs???\n");
3286 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3288 mtx_lock(&xsoftc.xpt_highpower_lock);
3289 if (xsoftc.num_highpower <= 0) {
3291 * We got a high power command, but we
3292 * don't have any available slots. Freeze
3293 * the device queue until we have a slot
3296 xpt_freeze_devq_device(device, 1);
3297 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3300 mtx_unlock(&xsoftc.xpt_highpower_lock);
3304 * Consume a high power slot while
3307 xsoftc.num_highpower--;
3309 mtx_unlock(&xsoftc.xpt_highpower_lock);
3311 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3312 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3313 devq->send_openings--;
3314 devq->send_active++;
3315 xpt_schedule_devq(devq, device);
3316 mtx_unlock(&devq->send_mtx);
3318 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3320 * The client wants to freeze the queue
3321 * after this CCB is sent.
3323 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3326 /* In Target mode, the peripheral driver knows best... */
3327 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3328 if ((device->inq_flags & SID_CmdQue) != 0
3329 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3330 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3333 * Clear this in case of a retried CCB that
3334 * failed due to a rejected tag.
3336 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3339 switch (work_ccb->ccb_h.func_code) {
3341 CAM_DEBUG(work_ccb->ccb_h.path,
3342 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3343 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3345 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3346 cdb_str, sizeof(cdb_str))));
3349 CAM_DEBUG(work_ccb->ccb_h.path,
3350 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3351 ata_op_string(&work_ccb->ataio.cmd),
3352 ata_cmd_string(&work_ccb->ataio.cmd,
3353 cdb_str, sizeof(cdb_str))));
3356 CAM_DEBUG(work_ccb->ccb_h.path,
3357 CAM_DEBUG_CDB,("%s. NCB: %s\n",
3358 nvme_op_string(&work_ccb->nvmeio.cmd),
3359 nvme_cmd_string(&work_ccb->nvmeio.cmd,
3360 cdb_str, sizeof(cdb_str))));
3367 * Device queues can be shared among multiple SIM instances
3368 * that reside on different busses. Use the SIM from the
3369 * queued device, rather than the one from the calling bus.
3373 if (mtx && !mtx_owned(mtx))
3377 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3378 (*(sim->sim_action))(sim, work_ccb);
3381 mtx_lock(&devq->send_mtx);
3383 devq->send_queue.qfrozen_cnt--;
3387 * This function merges stuff from the slave ccb into the master ccb, while
3388 * keeping important fields in the master ccb constant.
3391 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3395 * Pull fields that are valid for peripheral drivers to set
3396 * into the master CCB along with the CCB "payload".
3398 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3399 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3400 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3401 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3402 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3403 sizeof(union ccb) - sizeof(struct ccb_hdr));
3407 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3408 u_int32_t priority, u_int32_t flags)
3411 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3412 ccb_h->pinfo.priority = priority;
3414 ccb_h->path_id = path->bus->path_id;
3416 ccb_h->target_id = path->target->target_id;
3418 ccb_h->target_id = CAM_TARGET_WILDCARD;
3420 ccb_h->target_lun = path->device->lun_id;
3421 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3423 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3425 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3426 ccb_h->flags = flags;
3431 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3433 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3436 /* Path manipulation functions */
3438 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3439 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3441 struct cam_path *path;
3444 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3447 status = CAM_RESRC_UNAVAIL;
3450 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3451 if (status != CAM_REQ_CMP) {
3452 free(path, M_CAMPATH);
3455 *new_path_ptr = path;
3460 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3461 struct cam_periph *periph, path_id_t path_id,
3462 target_id_t target_id, lun_id_t lun_id)
3465 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3470 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3471 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3474 struct cam_et *target;
3475 struct cam_ed *device;
3478 status = CAM_REQ_CMP; /* Completed without error */
3479 target = NULL; /* Wildcarded */
3480 device = NULL; /* Wildcarded */
3483 * We will potentially modify the EDT, so block interrupts
3484 * that may attempt to create cam paths.
3486 bus = xpt_find_bus(path_id);
3488 status = CAM_PATH_INVALID;
3491 mtx_lock(&bus->eb_mtx);
3492 target = xpt_find_target(bus, target_id);
3493 if (target == NULL) {
3495 struct cam_et *new_target;
3497 new_target = xpt_alloc_target(bus, target_id);
3498 if (new_target == NULL) {
3499 status = CAM_RESRC_UNAVAIL;
3501 target = new_target;
3505 if (target != NULL) {
3506 device = xpt_find_device(target, lun_id);
3507 if (device == NULL) {
3509 struct cam_ed *new_device;
3512 (*(bus->xport->alloc_device))(bus,
3515 if (new_device == NULL) {
3516 status = CAM_RESRC_UNAVAIL;
3518 device = new_device;
3522 mtx_unlock(&bus->eb_mtx);
3526 * Only touch the user's data if we are successful.
3528 if (status == CAM_REQ_CMP) {
3529 new_path->periph = perph;
3530 new_path->bus = bus;
3531 new_path->target = target;
3532 new_path->device = device;
3533 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3536 xpt_release_device(device);
3538 xpt_release_target(target);
3540 xpt_release_bus(bus);
3546 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3548 struct cam_path *new_path;
3550 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3551 if (new_path == NULL)
3552 return(CAM_RESRC_UNAVAIL);
3553 xpt_copy_path(new_path, path);
3554 *new_path_ptr = new_path;
3555 return (CAM_REQ_CMP);
3559 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3563 if (path->bus != NULL)
3564 xpt_acquire_bus(path->bus);
3565 if (path->target != NULL)
3566 xpt_acquire_target(path->target);
3567 if (path->device != NULL)
3568 xpt_acquire_device(path->device);
3572 xpt_release_path(struct cam_path *path)
3574 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3575 if (path->device != NULL) {
3576 xpt_release_device(path->device);
3577 path->device = NULL;
3579 if (path->target != NULL) {
3580 xpt_release_target(path->target);
3581 path->target = NULL;
3583 if (path->bus != NULL) {
3584 xpt_release_bus(path->bus);
3590 xpt_free_path(struct cam_path *path)
3593 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3594 xpt_release_path(path);
3595 free(path, M_CAMPATH);
3599 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3600 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3606 *bus_ref = path->bus->refcount;
3612 *periph_ref = path->periph->refcount;
3619 *target_ref = path->target->refcount;
3625 *device_ref = path->device->refcount;
3632 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3633 * in path1, 2 for match with wildcards in path2.
3636 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3640 if (path1->bus != path2->bus) {
3641 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3643 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3648 if (path1->target != path2->target) {
3649 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3652 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3657 if (path1->device != path2->device) {
3658 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3661 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3670 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3674 if (path->bus != dev->target->bus) {
3675 if (path->bus->path_id == CAM_BUS_WILDCARD)
3677 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3682 if (path->target != dev->target) {
3683 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3686 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3691 if (path->device != dev) {
3692 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3695 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3704 xpt_print_path(struct cam_path *path)
3708 printf("(nopath): ");
3710 if (path->periph != NULL)
3711 printf("(%s%d:", path->periph->periph_name,
3712 path->periph->unit_number);
3714 printf("(noperiph:");
3716 if (path->bus != NULL)
3717 printf("%s%d:%d:", path->bus->sim->sim_name,
3718 path->bus->sim->unit_number,
3719 path->bus->sim->bus_id);
3723 if (path->target != NULL)
3724 printf("%d:", path->target->target_id);
3728 if (path->device != NULL)
3729 printf("%jx): ", (uintmax_t)path->device->lun_id);
3736 xpt_print_device(struct cam_ed *device)
3740 printf("(nopath): ");
3742 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3743 device->sim->unit_number,
3744 device->sim->bus_id,
3745 device->target->target_id,
3746 (uintmax_t)device->lun_id);
3751 xpt_print(struct cam_path *path, const char *fmt, ...)
3754 xpt_print_path(path);
3761 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3765 sbuf_new(&sb, str, str_len, 0);
3768 sbuf_printf(&sb, "(nopath): ");
3770 if (path->periph != NULL)
3771 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3772 path->periph->unit_number);
3774 sbuf_printf(&sb, "(noperiph:");
3776 if (path->bus != NULL)
3777 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3778 path->bus->sim->unit_number,
3779 path->bus->sim->bus_id);
3781 sbuf_printf(&sb, "nobus:");
3783 if (path->target != NULL)
3784 sbuf_printf(&sb, "%d:", path->target->target_id);
3786 sbuf_printf(&sb, "X:");
3788 if (path->device != NULL)
3789 sbuf_printf(&sb, "%jx): ",
3790 (uintmax_t)path->device->lun_id);
3792 sbuf_printf(&sb, "X): ");
3796 return(sbuf_len(&sb));
3800 xpt_path_path_id(struct cam_path *path)
3802 return(path->bus->path_id);
3806 xpt_path_target_id(struct cam_path *path)
3808 if (path->target != NULL)
3809 return (path->target->target_id);
3811 return (CAM_TARGET_WILDCARD);
3815 xpt_path_lun_id(struct cam_path *path)
3817 if (path->device != NULL)
3818 return (path->device->lun_id);
3820 return (CAM_LUN_WILDCARD);
3824 xpt_path_sim(struct cam_path *path)
3827 return (path->bus->sim);
3831 xpt_path_periph(struct cam_path *path)
3834 return (path->periph);
3838 * Release a CAM control block for the caller. Remit the cost of the structure
3839 * to the device referenced by the path. If the this device had no 'credits'
3840 * and peripheral drivers have registered async callbacks for this notification
3844 xpt_release_ccb(union ccb *free_ccb)
3846 struct cam_ed *device;
3847 struct cam_periph *periph;
3849 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3850 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3851 device = free_ccb->ccb_h.path->device;
3852 periph = free_ccb->ccb_h.path->periph;
3854 xpt_free_ccb(free_ccb);
3855 periph->periph_allocated--;
3856 cam_ccbq_release_opening(&device->ccbq);
3857 xpt_run_allocq(periph, 0);
3860 /* Functions accessed by SIM drivers */
3862 static struct xpt_xport xport_default = {
3863 .alloc_device = xpt_alloc_device_default,
3864 .action = xpt_action_default,
3865 .async = xpt_dev_async_default,
3869 * A sim structure, listing the SIM entry points and instance
3870 * identification info is passed to xpt_bus_register to hook the SIM
3871 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3872 * for this new bus and places it in the array of busses and assigns
3873 * it a path_id. The path_id may be influenced by "hard wiring"
3874 * information specified by the user. Once interrupt services are
3875 * available, the bus will be probed.
3878 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3880 struct cam_eb *new_bus;
3881 struct cam_eb *old_bus;
3882 struct ccb_pathinq cpi;
3883 struct cam_path *path;
3887 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3888 M_CAMXPT, M_NOWAIT|M_ZERO);
3889 if (new_bus == NULL) {
3890 /* Couldn't satisfy request */
3891 return (CAM_RESRC_UNAVAIL);
3894 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3895 TAILQ_INIT(&new_bus->et_entries);
3898 timevalclear(&new_bus->last_reset);
3900 new_bus->refcount = 1; /* Held until a bus_deregister event */
3901 new_bus->generation = 0;
3904 sim->path_id = new_bus->path_id =
3905 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3906 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3907 while (old_bus != NULL
3908 && old_bus->path_id < new_bus->path_id)
3909 old_bus = TAILQ_NEXT(old_bus, links);
3910 if (old_bus != NULL)
3911 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3913 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3914 xsoftc.bus_generation++;
3918 * Set a default transport so that a PATH_INQ can be issued to
3919 * the SIM. This will then allow for probing and attaching of
3920 * a more appropriate transport.
3922 new_bus->xport = &xport_default;
3924 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3925 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3926 if (status != CAM_REQ_CMP) {
3927 xpt_release_bus(new_bus);
3928 return (CAM_RESRC_UNAVAIL);
3931 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3932 cpi.ccb_h.func_code = XPT_PATH_INQ;
3933 xpt_action((union ccb *)&cpi);
3935 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3936 switch (cpi.transport) {
3944 new_bus->xport = scsi_get_xport();
3948 new_bus->xport = ata_get_xport();
3951 new_bus->xport = nvme_get_xport();
3954 new_bus->xport = &xport_default;
3959 /* Notify interested parties */
3960 if (sim->path_id != CAM_XPT_PATH_ID) {
3962 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3963 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3964 union ccb *scan_ccb;
3966 /* Initiate bus rescan. */
3967 scan_ccb = xpt_alloc_ccb_nowait();
3968 if (scan_ccb != NULL) {
3969 scan_ccb->ccb_h.path = path;
3970 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3971 scan_ccb->crcn.flags = 0;
3972 xpt_rescan(scan_ccb);
3975 "Can't allocate CCB to scan bus\n");
3976 xpt_free_path(path);
3979 xpt_free_path(path);
3981 xpt_free_path(path);
3982 return (CAM_SUCCESS);
3986 xpt_bus_deregister(path_id_t pathid)
3988 struct cam_path bus_path;
3991 status = xpt_compile_path(&bus_path, NULL, pathid,
3992 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3993 if (status != CAM_REQ_CMP)
3996 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3997 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3999 /* Release the reference count held while registered. */
4000 xpt_release_bus(bus_path.bus);
4001 xpt_release_path(&bus_path);
4003 return (CAM_REQ_CMP);
4007 xptnextfreepathid(void)
4013 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4015 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4017 /* Find an unoccupied pathid */
4018 while (bus != NULL && bus->path_id <= pathid) {
4019 if (bus->path_id == pathid)
4021 bus = TAILQ_NEXT(bus, links);
4025 * Ensure that this pathid is not reserved for
4026 * a bus that may be registered in the future.
4028 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4030 /* Start the search over */
4037 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4044 pathid = CAM_XPT_PATH_ID;
4045 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4046 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4049 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4050 if (strcmp(dname, "scbus")) {
4051 /* Avoid a bit of foot shooting. */
4054 if (dunit < 0) /* unwired?! */
4056 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4057 if (sim_bus == val) {
4061 } else if (sim_bus == 0) {
4062 /* Unspecified matches bus 0 */
4066 printf("Ambiguous scbus configuration for %s%d "
4067 "bus %d, cannot wire down. The kernel "
4068 "config entry for scbus%d should "
4069 "specify a controller bus.\n"
4070 "Scbus will be assigned dynamically.\n",
4071 sim_name, sim_unit, sim_bus, dunit);
4076 if (pathid == CAM_XPT_PATH_ID)
4077 pathid = xptnextfreepathid();
4082 xpt_async_string(u_int32_t async_code)
4085 switch (async_code) {
4086 case AC_BUS_RESET: return ("AC_BUS_RESET");
4087 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4088 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4089 case AC_SENT_BDR: return ("AC_SENT_BDR");
4090 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4091 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4092 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4093 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4094 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4095 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4096 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4097 case AC_CONTRACT: return ("AC_CONTRACT");
4098 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4099 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4101 return ("AC_UNKNOWN");
4105 xpt_async_size(u_int32_t async_code)
4108 switch (async_code) {
4109 case AC_BUS_RESET: return (0);
4110 case AC_UNSOL_RESEL: return (0);
4111 case AC_SCSI_AEN: return (0);
4112 case AC_SENT_BDR: return (0);
4113 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4114 case AC_PATH_DEREGISTERED: return (0);
4115 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4116 case AC_LOST_DEVICE: return (0);
4117 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4118 case AC_INQ_CHANGED: return (0);
4119 case AC_GETDEV_CHANGED: return (0);
4120 case AC_CONTRACT: return (sizeof(struct ac_contract));
4121 case AC_ADVINFO_CHANGED: return (-1);
4122 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4128 xpt_async_process_dev(struct cam_ed *device, void *arg)
4130 union ccb *ccb = arg;
4131 struct cam_path *path = ccb->ccb_h.path;
4132 void *async_arg = ccb->casync.async_arg_ptr;
4133 u_int32_t async_code = ccb->casync.async_code;
4136 if (path->device != device
4137 && path->device->lun_id != CAM_LUN_WILDCARD
4138 && device->lun_id != CAM_LUN_WILDCARD)
4142 * The async callback could free the device.
4143 * If it is a broadcast async, it doesn't hold
4144 * device reference, so take our own reference.
4146 xpt_acquire_device(device);
4149 * If async for specific device is to be delivered to
4150 * the wildcard client, take the specific device lock.
4151 * XXX: We may need a way for client to specify it.
4153 if ((device->lun_id == CAM_LUN_WILDCARD &&
4154 path->device->lun_id != CAM_LUN_WILDCARD) ||
4155 (device->target->target_id == CAM_TARGET_WILDCARD &&
4156 path->target->target_id != CAM_TARGET_WILDCARD) ||
4157 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4158 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4159 mtx_unlock(&device->device_mtx);
4160 xpt_path_lock(path);
4165 (*(device->target->bus->xport->async))(async_code,
4166 device->target->bus, device->target, device, async_arg);
4167 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4170 xpt_path_unlock(path);
4171 mtx_lock(&device->device_mtx);
4173 xpt_release_device(device);
4178 xpt_async_process_tgt(struct cam_et *target, void *arg)
4180 union ccb *ccb = arg;
4181 struct cam_path *path = ccb->ccb_h.path;
4183 if (path->target != target
4184 && path->target->target_id != CAM_TARGET_WILDCARD
4185 && target->target_id != CAM_TARGET_WILDCARD)
4188 if (ccb->casync.async_code == AC_SENT_BDR) {
4189 /* Update our notion of when the last reset occurred */
4190 microtime(&target->last_reset);
4193 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4197 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4200 struct cam_path *path;
4202 u_int32_t async_code;
4204 path = ccb->ccb_h.path;
4205 async_code = ccb->casync.async_code;
4206 async_arg = ccb->casync.async_arg_ptr;
4207 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4208 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4211 if (async_code == AC_BUS_RESET) {
4212 /* Update our notion of when the last reset occurred */
4213 microtime(&bus->last_reset);
4216 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4219 * If this wasn't a fully wildcarded async, tell all
4220 * clients that want all async events.
4222 if (bus != xpt_periph->path->bus) {
4223 xpt_path_lock(xpt_periph->path);
4224 xpt_async_process_dev(xpt_periph->path->device, ccb);
4225 xpt_path_unlock(xpt_periph->path);
4228 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4229 xpt_release_devq(path, 1, TRUE);
4231 xpt_release_simq(path->bus->sim, TRUE);
4232 if (ccb->casync.async_arg_size > 0)
4233 free(async_arg, M_CAMXPT);
4234 xpt_free_path(path);
4239 xpt_async_bcast(struct async_list *async_head,
4240 u_int32_t async_code,
4241 struct cam_path *path, void *async_arg)
4243 struct async_node *cur_entry;
4246 cur_entry = SLIST_FIRST(async_head);
4247 while (cur_entry != NULL) {
4248 struct async_node *next_entry;
4250 * Grab the next list entry before we call the current
4251 * entry's callback. This is because the callback function
4252 * can delete its async callback entry.
4254 next_entry = SLIST_NEXT(cur_entry, links);
4255 if ((cur_entry->event_enable & async_code) != 0) {
4256 mtx = cur_entry->event_lock ?
4257 path->device->sim->mtx : NULL;
4260 cur_entry->callback(cur_entry->callback_arg,
4266 cur_entry = next_entry;
4271 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4276 ccb = xpt_alloc_ccb_nowait();
4278 xpt_print(path, "Can't allocate CCB to send %s\n",
4279 xpt_async_string(async_code));
4283 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4284 xpt_print(path, "Can't allocate path to send %s\n",
4285 xpt_async_string(async_code));
4289 ccb->ccb_h.path->periph = NULL;
4290 ccb->ccb_h.func_code = XPT_ASYNC;
4291 ccb->ccb_h.cbfcnp = xpt_async_process;
4292 ccb->ccb_h.flags |= CAM_UNLOCKED;
4293 ccb->casync.async_code = async_code;
4294 ccb->casync.async_arg_size = 0;
4295 size = xpt_async_size(async_code);
4296 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4297 ("xpt_async: func %#x %s aync_code %d %s\n",
4298 ccb->ccb_h.func_code,
4299 xpt_action_name(ccb->ccb_h.func_code),
4301 xpt_async_string(async_code)));
4302 if (size > 0 && async_arg != NULL) {
4303 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4304 if (ccb->casync.async_arg_ptr == NULL) {
4305 xpt_print(path, "Can't allocate argument to send %s\n",
4306 xpt_async_string(async_code));
4307 xpt_free_path(ccb->ccb_h.path);
4311 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4312 ccb->casync.async_arg_size = size;
4313 } else if (size < 0) {
4314 ccb->casync.async_arg_ptr = async_arg;
4315 ccb->casync.async_arg_size = size;
4317 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4318 xpt_freeze_devq(path, 1);
4320 xpt_freeze_simq(path->bus->sim, 1);
4325 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4326 struct cam_et *target, struct cam_ed *device,
4331 * We only need to handle events for real devices.
4333 if (target->target_id == CAM_TARGET_WILDCARD
4334 || device->lun_id == CAM_LUN_WILDCARD)
4337 printf("%s called\n", __func__);
4341 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4343 struct cam_devq *devq;
4346 devq = dev->sim->devq;
4347 mtx_assert(&devq->send_mtx, MA_OWNED);
4348 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4349 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4350 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4351 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4352 /* Remove frozen device from sendq. */
4353 if (device_is_queued(dev))
4354 camq_remove(&devq->send_queue, dev->devq_entry.index);
4359 xpt_freeze_devq(struct cam_path *path, u_int count)
4361 struct cam_ed *dev = path->device;
4362 struct cam_devq *devq;
4365 devq = dev->sim->devq;
4366 mtx_lock(&devq->send_mtx);
4367 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4368 freeze = xpt_freeze_devq_device(dev, count);
4369 mtx_unlock(&devq->send_mtx);
4374 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4376 struct cam_devq *devq;
4380 mtx_lock(&devq->send_mtx);
4381 freeze = (devq->send_queue.qfrozen_cnt += count);
4382 mtx_unlock(&devq->send_mtx);
4387 xpt_release_devq_timeout(void *arg)
4390 struct cam_devq *devq;
4392 dev = (struct cam_ed *)arg;
4393 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4394 devq = dev->sim->devq;
4395 mtx_assert(&devq->send_mtx, MA_OWNED);
4396 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4401 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4404 struct cam_devq *devq;
4406 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4409 devq = dev->sim->devq;
4410 mtx_lock(&devq->send_mtx);
4411 if (xpt_release_devq_device(dev, count, run_queue))
4412 xpt_run_devq(dev->sim->devq);
4413 mtx_unlock(&devq->send_mtx);
4417 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4420 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4421 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4422 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4423 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4424 if (count > dev->ccbq.queue.qfrozen_cnt) {
4426 printf("xpt_release_devq(): requested %u > present %u\n",
4427 count, dev->ccbq.queue.qfrozen_cnt);
4429 count = dev->ccbq.queue.qfrozen_cnt;
4431 dev->ccbq.queue.qfrozen_cnt -= count;
4432 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4434 * No longer need to wait for a successful
4435 * command completion.
4437 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4439 * Remove any timeouts that might be scheduled
4440 * to release this queue.
4442 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4443 callout_stop(&dev->callout);
4444 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4447 * Now that we are unfrozen schedule the
4448 * device so any pending transactions are
4451 xpt_schedule_devq(dev->sim->devq, dev);
4458 xpt_release_simq(struct cam_sim *sim, int run_queue)
4460 struct cam_devq *devq;
4463 mtx_lock(&devq->send_mtx);
4464 if (devq->send_queue.qfrozen_cnt <= 0) {
4466 printf("xpt_release_simq: requested 1 > present %u\n",
4467 devq->send_queue.qfrozen_cnt);
4470 devq->send_queue.qfrozen_cnt--;
4471 if (devq->send_queue.qfrozen_cnt == 0) {
4473 * If there is a timeout scheduled to release this
4474 * sim queue, remove it. The queue frozen count is
4477 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4478 callout_stop(&sim->callout);
4479 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4483 * Now that we are unfrozen run the send queue.
4485 xpt_run_devq(sim->devq);
4488 mtx_unlock(&devq->send_mtx);
4492 * XXX Appears to be unused.
4495 xpt_release_simq_timeout(void *arg)
4497 struct cam_sim *sim;
4499 sim = (struct cam_sim *)arg;
4500 xpt_release_simq(sim, /* run_queue */ TRUE);
4504 xpt_done(union ccb *done_ccb)
4506 struct cam_doneq *queue;
4509 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4510 ("xpt_done: func= %#x %s status %#x\n",
4511 done_ccb->ccb_h.func_code,
4512 xpt_action_name(done_ccb->ccb_h.func_code),
4513 done_ccb->ccb_h.status));
4514 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4517 /* Store the time the ccb was in the sim */
4518 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4519 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4520 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4521 queue = &cam_doneqs[hash];
4522 mtx_lock(&queue->cam_doneq_mtx);
4523 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4524 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4525 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4526 mtx_unlock(&queue->cam_doneq_mtx);
4528 wakeup(&queue->cam_doneq);
4532 xpt_done_direct(union ccb *done_ccb)
4535 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4536 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4537 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4540 /* Store the time the ccb was in the sim */
4541 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4542 xpt_done_process(&done_ccb->ccb_h);
4550 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4555 xpt_alloc_ccb_nowait()
4559 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4564 xpt_free_ccb(union ccb *free_ccb)
4566 free(free_ccb, M_CAMCCB);
4571 /* Private XPT functions */
4574 * Get a CAM control block for the caller. Charge the structure to the device
4575 * referenced by the path. If we don't have sufficient resources to allocate
4576 * more ccbs, we return NULL.
4579 xpt_get_ccb_nowait(struct cam_periph *periph)
4583 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4584 if (new_ccb == NULL)
4586 periph->periph_allocated++;
4587 cam_ccbq_take_opening(&periph->path->device->ccbq);
4592 xpt_get_ccb(struct cam_periph *periph)
4596 cam_periph_unlock(periph);
4597 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4598 cam_periph_lock(periph);
4599 periph->periph_allocated++;
4600 cam_ccbq_take_opening(&periph->path->device->ccbq);
4605 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4607 struct ccb_hdr *ccb_h;
4609 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4610 cam_periph_assert(periph, MA_OWNED);
4611 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4612 ccb_h->pinfo.priority != priority) {
4613 if (priority < periph->immediate_priority) {
4614 periph->immediate_priority = priority;
4615 xpt_run_allocq(periph, 0);
4617 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4620 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4621 return ((union ccb *)ccb_h);
4625 xpt_acquire_bus(struct cam_eb *bus)
4634 xpt_release_bus(struct cam_eb *bus)
4638 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4639 if (--bus->refcount > 0) {
4643 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4644 xsoftc.bus_generation++;
4646 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4647 ("destroying bus, but target list is not empty"));
4648 cam_sim_release(bus->sim);
4649 mtx_destroy(&bus->eb_mtx);
4650 free(bus, M_CAMXPT);
4653 static struct cam_et *
4654 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4656 struct cam_et *cur_target, *target;
4658 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4659 mtx_assert(&bus->eb_mtx, MA_OWNED);
4660 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4665 TAILQ_INIT(&target->ed_entries);
4667 target->target_id = target_id;
4668 target->refcount = 1;
4669 target->generation = 0;
4670 target->luns = NULL;
4671 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4672 timevalclear(&target->last_reset);
4674 * Hold a reference to our parent bus so it
4675 * will not go away before we do.
4679 /* Insertion sort into our bus's target list */
4680 cur_target = TAILQ_FIRST(&bus->et_entries);
4681 while (cur_target != NULL && cur_target->target_id < target_id)
4682 cur_target = TAILQ_NEXT(cur_target, links);
4683 if (cur_target != NULL) {
4684 TAILQ_INSERT_BEFORE(cur_target, target, links);
4686 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4693 xpt_acquire_target(struct cam_et *target)
4695 struct cam_eb *bus = target->bus;
4697 mtx_lock(&bus->eb_mtx);
4699 mtx_unlock(&bus->eb_mtx);
4703 xpt_release_target(struct cam_et *target)
4705 struct cam_eb *bus = target->bus;
4707 mtx_lock(&bus->eb_mtx);
4708 if (--target->refcount > 0) {
4709 mtx_unlock(&bus->eb_mtx);
4712 TAILQ_REMOVE(&bus->et_entries, target, links);
4714 mtx_unlock(&bus->eb_mtx);
4715 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4716 ("destroying target, but device list is not empty"));
4717 xpt_release_bus(bus);
4718 mtx_destroy(&target->luns_mtx);
4720 free(target->luns, M_CAMXPT);
4721 free(target, M_CAMXPT);
4724 static struct cam_ed *
4725 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4728 struct cam_ed *device;
4730 device = xpt_alloc_device(bus, target, lun_id);
4734 device->mintags = 1;
4735 device->maxtags = 1;
4740 xpt_destroy_device(void *context, int pending)
4742 struct cam_ed *device = context;
4744 mtx_lock(&device->device_mtx);
4745 mtx_destroy(&device->device_mtx);
4746 free(device, M_CAMDEV);
4750 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4752 struct cam_ed *cur_device, *device;
4753 struct cam_devq *devq;
4756 mtx_assert(&bus->eb_mtx, MA_OWNED);
4757 /* Make space for us in the device queue on our bus */
4758 devq = bus->sim->devq;
4759 mtx_lock(&devq->send_mtx);
4760 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4761 mtx_unlock(&devq->send_mtx);
4762 if (status != CAM_REQ_CMP)
4765 device = (struct cam_ed *)malloc(sizeof(*device),
4766 M_CAMDEV, M_NOWAIT|M_ZERO);
4770 cam_init_pinfo(&device->devq_entry);
4771 device->target = target;
4772 device->lun_id = lun_id;
4773 device->sim = bus->sim;
4774 if (cam_ccbq_init(&device->ccbq,
4775 bus->sim->max_dev_openings) != 0) {
4776 free(device, M_CAMDEV);
4779 SLIST_INIT(&device->asyncs);
4780 SLIST_INIT(&device->periphs);
4781 device->generation = 0;
4782 device->flags = CAM_DEV_UNCONFIGURED;
4783 device->tag_delay_count = 0;
4784 device->tag_saved_openings = 0;
4785 device->refcount = 1;
4786 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4787 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4788 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4790 * Hold a reference to our parent bus so it
4791 * will not go away before we do.
4795 cur_device = TAILQ_FIRST(&target->ed_entries);
4796 while (cur_device != NULL && cur_device->lun_id < lun_id)
4797 cur_device = TAILQ_NEXT(cur_device, links);
4798 if (cur_device != NULL)
4799 TAILQ_INSERT_BEFORE(cur_device, device, links);
4801 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4802 target->generation++;
4807 xpt_acquire_device(struct cam_ed *device)
4809 struct cam_eb *bus = device->target->bus;
4811 mtx_lock(&bus->eb_mtx);
4813 mtx_unlock(&bus->eb_mtx);
4817 xpt_release_device(struct cam_ed *device)
4819 struct cam_eb *bus = device->target->bus;
4820 struct cam_devq *devq;
4822 mtx_lock(&bus->eb_mtx);
4823 if (--device->refcount > 0) {
4824 mtx_unlock(&bus->eb_mtx);
4828 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4829 device->target->generation++;
4830 mtx_unlock(&bus->eb_mtx);
4832 /* Release our slot in the devq */
4833 devq = bus->sim->devq;
4834 mtx_lock(&devq->send_mtx);
4835 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4836 mtx_unlock(&devq->send_mtx);
4838 KASSERT(SLIST_EMPTY(&device->periphs),
4839 ("destroying device, but periphs list is not empty"));
4840 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4841 ("destroying device while still queued for ccbs"));
4843 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4844 callout_stop(&device->callout);
4846 xpt_release_target(device->target);
4848 cam_ccbq_fini(&device->ccbq);
4850 * Free allocated memory. free(9) does nothing if the
4851 * supplied pointer is NULL, so it is safe to call without
4854 free(device->supported_vpds, M_CAMXPT);
4855 free(device->device_id, M_CAMXPT);
4856 free(device->ext_inq, M_CAMXPT);
4857 free(device->physpath, M_CAMXPT);
4858 free(device->rcap_buf, M_CAMXPT);
4859 free(device->serial_num, M_CAMXPT);
4860 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4864 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4870 mtx_lock(&dev->sim->devq->send_mtx);
4871 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4872 mtx_unlock(&dev->sim->devq->send_mtx);
4873 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4874 || (dev->inq_flags & SID_CmdQue) != 0)
4875 dev->tag_saved_openings = newopenings;
4879 static struct cam_eb *
4880 xpt_find_bus(path_id_t path_id)
4885 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4887 bus = TAILQ_NEXT(bus, links)) {
4888 if (bus->path_id == path_id) {
4897 static struct cam_et *
4898 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4900 struct cam_et *target;
4902 mtx_assert(&bus->eb_mtx, MA_OWNED);
4903 for (target = TAILQ_FIRST(&bus->et_entries);
4905 target = TAILQ_NEXT(target, links)) {
4906 if (target->target_id == target_id) {
4914 static struct cam_ed *
4915 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4917 struct cam_ed *device;
4919 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4920 for (device = TAILQ_FIRST(&target->ed_entries);
4922 device = TAILQ_NEXT(device, links)) {
4923 if (device->lun_id == lun_id) {
4932 xpt_start_tags(struct cam_path *path)
4934 struct ccb_relsim crs;
4935 struct cam_ed *device;
4936 struct cam_sim *sim;
4939 device = path->device;
4940 sim = path->bus->sim;
4941 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4942 xpt_freeze_devq(path, /*count*/1);
4943 device->inq_flags |= SID_CmdQue;
4944 if (device->tag_saved_openings != 0)
4945 newopenings = device->tag_saved_openings;
4947 newopenings = min(device->maxtags,
4948 sim->max_tagged_dev_openings);
4949 xpt_dev_ccbq_resize(path, newopenings);
4950 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4951 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4952 crs.ccb_h.func_code = XPT_REL_SIMQ;
4953 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4955 = crs.release_timeout
4958 xpt_action((union ccb *)&crs);
4962 xpt_stop_tags(struct cam_path *path)
4964 struct ccb_relsim crs;
4965 struct cam_ed *device;
4966 struct cam_sim *sim;
4968 device = path->device;
4969 sim = path->bus->sim;
4970 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4971 device->tag_delay_count = 0;
4972 xpt_freeze_devq(path, /*count*/1);
4973 device->inq_flags &= ~SID_CmdQue;
4974 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4975 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4976 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4977 crs.ccb_h.func_code = XPT_REL_SIMQ;
4978 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4980 = crs.release_timeout
4983 xpt_action((union ccb *)&crs);
4987 xpt_boot_delay(void *arg)
4994 xpt_config(void *arg)
4997 * Now that interrupts are enabled, go find our devices
4999 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5000 printf("xpt_config: failed to create taskqueue thread.\n");
5002 /* Setup debugging path */
5003 if (cam_dflags != CAM_DEBUG_NONE) {
5004 if (xpt_create_path(&cam_dpath, NULL,
5005 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5006 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5007 printf("xpt_config: xpt_create_path() failed for debug"
5008 " target %d:%d:%d, debugging disabled\n",
5009 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5010 cam_dflags = CAM_DEBUG_NONE;
5015 periphdriver_init(1);
5017 callout_init(&xsoftc.boot_callout, 1);
5018 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5019 xpt_boot_delay, NULL, 0);
5020 /* Fire up rescan thread. */
5021 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5022 "cam", "scanner")) {
5023 printf("xpt_config: failed to create rescan thread.\n");
5031 xsoftc.buses_to_config++;
5036 xpt_release_boot(void)
5039 xsoftc.buses_to_config--;
5040 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5041 struct xpt_task *task;
5043 xsoftc.buses_config_done = 1;
5045 /* Call manually because we don't have any busses */
5046 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5048 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5049 taskqueue_enqueue(taskqueue_thread, &task->task);
5056 * If the given device only has one peripheral attached to it, and if that
5057 * peripheral is the passthrough driver, announce it. This insures that the
5058 * user sees some sort of announcement for every peripheral in their system.
5061 xptpassannouncefunc(struct cam_ed *device, void *arg)
5063 struct cam_periph *periph;
5066 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5067 periph = SLIST_NEXT(periph, periph_links), i++);
5069 periph = SLIST_FIRST(&device->periphs);
5071 && (strncmp(periph->periph_name, "pass", 4) == 0))
5072 xpt_announce_periph(periph, NULL);
5078 xpt_finishconfig_task(void *context, int pending)
5081 periphdriver_init(2);
5083 * Check for devices with no "standard" peripheral driver
5084 * attached. For any devices like that, announce the
5085 * passthrough driver so the user will see something.
5088 xpt_for_all_devices(xptpassannouncefunc, NULL);
5090 /* Release our hook so that the boot can continue. */
5091 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5092 free(xsoftc.xpt_config_hook, M_CAMXPT);
5093 xsoftc.xpt_config_hook = NULL;
5095 free(context, M_CAMXPT);
5099 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5100 struct cam_path *path)
5102 struct ccb_setasync csa;
5107 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5108 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5109 if (status != CAM_REQ_CMP)
5111 xpt_path_lock(path);
5115 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5116 csa.ccb_h.func_code = XPT_SASYNC_CB;
5117 csa.event_enable = event;
5118 csa.callback = cbfunc;
5119 csa.callback_arg = cbarg;
5120 xpt_action((union ccb *)&csa);
5121 status = csa.ccb_h.status;
5123 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5124 ("xpt_register_async: func %p\n", cbfunc));
5127 xpt_path_unlock(path);
5128 xpt_free_path(path);
5131 if ((status == CAM_REQ_CMP) &&
5132 (csa.event_enable & AC_FOUND_DEVICE)) {
5134 * Get this peripheral up to date with all
5135 * the currently existing devices.
5137 xpt_for_all_devices(xptsetasyncfunc, &csa);
5139 if ((status == CAM_REQ_CMP) &&
5140 (csa.event_enable & AC_PATH_REGISTERED)) {
5142 * Get this peripheral up to date with all
5143 * the currently existing busses.
5145 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5152 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5154 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5156 switch (work_ccb->ccb_h.func_code) {
5157 /* Common cases first */
5158 case XPT_PATH_INQ: /* Path routing inquiry */
5160 struct ccb_pathinq *cpi;
5162 cpi = &work_ccb->cpi;
5163 cpi->version_num = 1; /* XXX??? */
5164 cpi->hba_inquiry = 0;
5165 cpi->target_sprt = 0;
5167 cpi->hba_eng_cnt = 0;
5168 cpi->max_target = 0;
5170 cpi->initiator_id = 0;
5171 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5172 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5173 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5174 cpi->unit_number = sim->unit_number;
5175 cpi->bus_id = sim->bus_id;
5176 cpi->base_transfer_speed = 0;
5177 cpi->protocol = PROTO_UNSPECIFIED;
5178 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5179 cpi->transport = XPORT_UNSPECIFIED;
5180 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5181 cpi->ccb_h.status = CAM_REQ_CMP;
5186 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5193 * The xpt as a "controller" has no interrupt sources, so polling
5197 xptpoll(struct cam_sim *sim)
5202 xpt_lock_buses(void)
5204 mtx_lock(&xsoftc.xpt_topo_lock);
5208 xpt_unlock_buses(void)
5210 mtx_unlock(&xsoftc.xpt_topo_lock);
5214 xpt_path_mtx(struct cam_path *path)
5217 return (&path->device->device_mtx);
5221 xpt_done_process(struct ccb_hdr *ccb_h)
5223 struct cam_sim *sim;
5224 struct cam_devq *devq;
5225 struct mtx *mtx = NULL;
5227 if (ccb_h->flags & CAM_HIGH_POWER) {
5228 struct highpowerlist *hphead;
5229 struct cam_ed *device;
5231 mtx_lock(&xsoftc.xpt_highpower_lock);
5232 hphead = &xsoftc.highpowerq;
5234 device = STAILQ_FIRST(hphead);
5237 * Increment the count since this command is done.
5239 xsoftc.num_highpower++;
5242 * Any high powered commands queued up?
5244 if (device != NULL) {
5246 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5247 mtx_unlock(&xsoftc.xpt_highpower_lock);
5249 mtx_lock(&device->sim->devq->send_mtx);
5250 xpt_release_devq_device(device,
5251 /*count*/1, /*runqueue*/TRUE);
5252 mtx_unlock(&device->sim->devq->send_mtx);
5254 mtx_unlock(&xsoftc.xpt_highpower_lock);
5257 sim = ccb_h->path->bus->sim;
5259 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5260 xpt_release_simq(sim, /*run_queue*/FALSE);
5261 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5264 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5265 && (ccb_h->status & CAM_DEV_QFRZN)) {
5266 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5267 ccb_h->status &= ~CAM_DEV_QFRZN;
5271 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5272 struct cam_ed *dev = ccb_h->path->device;
5274 mtx_lock(&devq->send_mtx);
5275 devq->send_active--;
5276 devq->send_openings++;
5277 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5279 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5280 && (dev->ccbq.dev_active == 0))) {
5281 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5282 xpt_release_devq_device(dev, /*count*/1,
5283 /*run_queue*/FALSE);
5286 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5287 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5288 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5289 xpt_release_devq_device(dev, /*count*/1,
5290 /*run_queue*/FALSE);
5293 if (!device_is_queued(dev))
5294 (void)xpt_schedule_devq(devq, dev);
5296 mtx_unlock(&devq->send_mtx);
5298 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5299 mtx = xpt_path_mtx(ccb_h->path);
5302 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5303 && (--dev->tag_delay_count == 0))
5304 xpt_start_tags(ccb_h->path);
5308 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5310 mtx = xpt_path_mtx(ccb_h->path);
5320 /* Call the peripheral driver's callback */
5321 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5322 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5328 xpt_done_td(void *arg)
5330 struct cam_doneq *queue = arg;
5331 struct ccb_hdr *ccb_h;
5332 STAILQ_HEAD(, ccb_hdr) doneq;
5334 STAILQ_INIT(&doneq);
5335 mtx_lock(&queue->cam_doneq_mtx);
5337 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5338 queue->cam_doneq_sleep = 1;
5339 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5341 queue->cam_doneq_sleep = 0;
5343 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5344 mtx_unlock(&queue->cam_doneq_mtx);
5346 THREAD_NO_SLEEPING();
5347 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5348 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5349 xpt_done_process(ccb_h);
5351 THREAD_SLEEPING_OK();
5353 mtx_lock(&queue->cam_doneq_mtx);
5358 camisr_runqueue(void)
5360 struct ccb_hdr *ccb_h;
5361 struct cam_doneq *queue;
5364 /* Process global queues. */
5365 for (i = 0; i < cam_num_doneqs; i++) {
5366 queue = &cam_doneqs[i];
5367 mtx_lock(&queue->cam_doneq_mtx);
5368 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5369 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5370 mtx_unlock(&queue->cam_doneq_mtx);
5371 xpt_done_process(ccb_h);
5372 mtx_lock(&queue->cam_doneq_mtx);
5374 mtx_unlock(&queue->cam_doneq_mtx);
5384 static struct kv map[] = {
5385 { XPT_NOOP, "XPT_NOOP" },
5386 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5387 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5388 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5389 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5390 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5391 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5392 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5393 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5394 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5395 { XPT_DEBUG, "XPT_DEBUG" },
5396 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5397 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5398 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5399 { XPT_ASYNC, "XPT_ASYNC" },
5400 { XPT_ABORT, "XPT_ABORT" },
5401 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5402 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5403 { XPT_TERM_IO, "XPT_TERM_IO" },
5404 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5405 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5406 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5407 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5408 { XPT_ATA_IO, "XPT_ATA_IO" },
5409 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5410 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5411 { XPT_NVME_IO, "XPT_NVME_IO" },
5412 { XPT_MMCSD_IO, "XPT_MMCSD_IO" },
5413 { XPT_SMP_IO, "XPT_SMP_IO" },
5414 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5415 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5416 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5417 { XPT_EN_LUN, "XPT_EN_LUN" },
5418 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5419 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5420 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5421 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5422 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5423 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5424 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5429 xpt_action_name(uint32_t action)
5431 static char buffer[32]; /* Only for unknown messages -- racy */
5432 struct kv *walker = map;
5434 while (walker->name != NULL) {
5435 if (walker->v == action)
5436 return (walker->name);
5440 snprintf(buffer, sizeof(buffer), "%#x", action);