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>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
62 #include <cam/cam_compat.h>
64 #include <cam/scsi/scsi_all.h>
65 #include <cam/scsi/scsi_message.h>
66 #include <cam/scsi/scsi_pass.h>
68 #include <machine/md_var.h> /* geometry translation */
69 #include <machine/stdarg.h> /* for xpt_print below */
74 * This is the maximum number of high powered commands (e.g. start unit)
75 * that can be outstanding at a particular time.
77 #ifndef CAM_MAX_HIGHPOWER
78 #define CAM_MAX_HIGHPOWER 4
81 /* Datastructures internal to the xpt layer */
82 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
83 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
84 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
85 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
87 /* Object for defering XPT actions to a taskqueue */
95 uint32_t xpt_generation;
97 /* number of high powered commands that can go through right now */
98 struct mtx xpt_highpower_lock;
99 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
102 /* queue for handling async rescan requests. */
103 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
105 int buses_config_done;
107 /* Registered busses */
108 TAILQ_HEAD(,cam_eb) xpt_busses;
109 u_int bus_generation;
111 struct intr_config_hook *xpt_config_hook;
114 struct callout boot_callout;
116 struct mtx xpt_topo_lock;
118 struct taskqueue *xpt_taskq;
123 DM_RET_FLAG_MASK = 0x0f,
126 DM_RET_DESCEND = 0x20,
128 DM_RET_ACTION_MASK = 0xf0
136 } xpt_traverse_depth;
138 struct xpt_traverse_config {
139 xpt_traverse_depth depth;
144 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
145 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
146 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
147 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
148 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
150 /* Transport layer configuration information */
151 static struct xpt_softc xsoftc;
153 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
155 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
156 &xsoftc.boot_delay, 0, "Bus registration wait time");
157 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
158 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
161 struct mtx_padalign cam_doneq_mtx;
162 STAILQ_HEAD(, ccb_hdr) cam_doneq;
166 static struct cam_doneq cam_doneqs[MAXCPU];
167 static int cam_num_doneqs;
168 static struct proc *cam_proc;
170 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
171 &cam_num_doneqs, 0, "Number of completion queues/threads");
173 struct cam_periph *xpt_periph;
175 static periph_init_t xpt_periph_init;
177 static struct periph_driver xpt_driver =
179 xpt_periph_init, "xpt",
180 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
184 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
186 static d_open_t xptopen;
187 static d_close_t xptclose;
188 static d_ioctl_t xptioctl;
189 static d_ioctl_t xptdoioctl;
191 static struct cdevsw xpt_cdevsw = {
192 .d_version = D_VERSION,
200 /* Storage for debugging datastructures */
201 struct cam_path *cam_dpath;
202 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
203 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
204 &cam_dflags, 0, "Enabled debug flags");
205 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
206 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
207 &cam_debug_delay, 0, "Delay in us after each debug message");
209 /* Our boot-time initialization hook */
210 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
212 static moduledata_t cam_moduledata = {
214 cam_module_event_handler,
218 static int xpt_init(void *);
220 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
221 MODULE_VERSION(cam, 1);
224 static void xpt_async_bcast(struct async_list *async_head,
225 u_int32_t async_code,
226 struct cam_path *path,
228 static path_id_t xptnextfreepathid(void);
229 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
230 static union ccb *xpt_get_ccb(struct cam_periph *periph);
231 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
232 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
233 static void xpt_run_allocq_task(void *context, int pending);
234 static void xpt_run_devq(struct cam_devq *devq);
235 static timeout_t xpt_release_devq_timeout;
236 static void xpt_release_simq_timeout(void *arg) __unused;
237 static void xpt_acquire_bus(struct cam_eb *bus);
238 static void xpt_release_bus(struct cam_eb *bus);
239 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
240 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
242 static struct cam_et*
243 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
244 static void xpt_acquire_target(struct cam_et *target);
245 static void xpt_release_target(struct cam_et *target);
246 static struct cam_eb*
247 xpt_find_bus(path_id_t path_id);
248 static struct cam_et*
249 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
250 static struct cam_ed*
251 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
252 static void xpt_config(void *arg);
253 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
254 u_int32_t new_priority);
255 static xpt_devicefunc_t xptpassannouncefunc;
256 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
257 static void xptpoll(struct cam_sim *sim);
258 static void camisr_runqueue(void);
259 static void xpt_done_process(struct ccb_hdr *ccb_h);
260 static void xpt_done_td(void *);
261 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
262 u_int num_patterns, struct cam_eb *bus);
263 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
265 struct cam_ed *device);
266 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
268 struct cam_periph *periph);
269 static xpt_busfunc_t xptedtbusfunc;
270 static xpt_targetfunc_t xptedttargetfunc;
271 static xpt_devicefunc_t xptedtdevicefunc;
272 static xpt_periphfunc_t xptedtperiphfunc;
273 static xpt_pdrvfunc_t xptplistpdrvfunc;
274 static xpt_periphfunc_t xptplistperiphfunc;
275 static int xptedtmatch(struct ccb_dev_match *cdm);
276 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
277 static int xptbustraverse(struct cam_eb *start_bus,
278 xpt_busfunc_t *tr_func, void *arg);
279 static int xpttargettraverse(struct cam_eb *bus,
280 struct cam_et *start_target,
281 xpt_targetfunc_t *tr_func, void *arg);
282 static int xptdevicetraverse(struct cam_et *target,
283 struct cam_ed *start_device,
284 xpt_devicefunc_t *tr_func, void *arg);
285 static int xptperiphtraverse(struct cam_ed *device,
286 struct cam_periph *start_periph,
287 xpt_periphfunc_t *tr_func, void *arg);
288 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
289 xpt_pdrvfunc_t *tr_func, void *arg);
290 static int xptpdperiphtraverse(struct periph_driver **pdrv,
291 struct cam_periph *start_periph,
292 xpt_periphfunc_t *tr_func,
294 static xpt_busfunc_t xptdefbusfunc;
295 static xpt_targetfunc_t xptdeftargetfunc;
296 static xpt_devicefunc_t xptdefdevicefunc;
297 static xpt_periphfunc_t xptdefperiphfunc;
298 static void xpt_finishconfig_task(void *context, int pending);
299 static void xpt_dev_async_default(u_int32_t async_code,
301 struct cam_et *target,
302 struct cam_ed *device,
304 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
305 struct cam_et *target,
307 static xpt_devicefunc_t xptsetasyncfunc;
308 static xpt_busfunc_t xptsetasyncbusfunc;
309 static cam_status xptregister(struct cam_periph *periph,
311 static const char * xpt_action_name(uint32_t action);
312 static __inline int device_is_queued(struct cam_ed *device);
315 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
319 mtx_assert(&devq->send_mtx, MA_OWNED);
320 if ((dev->ccbq.queue.entries > 0) &&
321 (dev->ccbq.dev_openings > 0) &&
322 (dev->ccbq.queue.qfrozen_cnt == 0)) {
324 * The priority of a device waiting for controller
325 * resources is that of the highest priority CCB
329 xpt_schedule_dev(&devq->send_queue,
331 CAMQ_GET_PRIO(&dev->ccbq.queue));
339 device_is_queued(struct cam_ed *device)
341 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
347 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
351 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
355 * Only allow read-write access.
357 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
361 * We don't allow nonblocking access.
363 if ((flags & O_NONBLOCK) != 0) {
364 printf("%s: can't do nonblocking access\n", devtoname(dev));
372 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
379 * Don't automatically grab the xpt softc lock here even though this is going
380 * through the xpt device. The xpt device is really just a back door for
381 * accessing other devices and SIMs, so the right thing to do is to grab
382 * the appropriate SIM lock once the bus/SIM is located.
385 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
389 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
390 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
396 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
404 * For the transport layer CAMIOCOMMAND ioctl, we really only want
405 * to accept CCB types that don't quite make sense to send through a
406 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
414 inccb = (union ccb *)addr;
416 if (inccb->ccb_h.flags & CAM_UNLOCKED)
419 bus = xpt_find_bus(inccb->ccb_h.path_id);
423 switch (inccb->ccb_h.func_code) {
426 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
427 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
428 xpt_release_bus(bus);
433 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
434 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
435 xpt_release_bus(bus);
443 switch(inccb->ccb_h.func_code) {
451 ccb = xpt_alloc_ccb();
454 * Create a path using the bus, target, and lun the
457 if (xpt_create_path(&ccb->ccb_h.path, NULL,
458 inccb->ccb_h.path_id,
459 inccb->ccb_h.target_id,
460 inccb->ccb_h.target_lun) !=
466 /* Ensure all of our fields are correct */
467 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
468 inccb->ccb_h.pinfo.priority);
469 xpt_merge_ccb(ccb, inccb);
470 xpt_path_lock(ccb->ccb_h.path);
471 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
472 xpt_path_unlock(ccb->ccb_h.path);
473 bcopy(ccb, inccb, sizeof(union ccb));
474 xpt_free_path(ccb->ccb_h.path);
482 * This is an immediate CCB, so it's okay to
483 * allocate it on the stack.
487 * Create a path using the bus, target, and lun the
490 if (xpt_create_path(&ccb.ccb_h.path, NULL,
491 inccb->ccb_h.path_id,
492 inccb->ccb_h.target_id,
493 inccb->ccb_h.target_lun) !=
498 /* Ensure all of our fields are correct */
499 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
500 inccb->ccb_h.pinfo.priority);
501 xpt_merge_ccb(&ccb, inccb);
503 bcopy(&ccb, inccb, sizeof(union ccb));
504 xpt_free_path(ccb.ccb_h.path);
508 case XPT_DEV_MATCH: {
509 struct cam_periph_map_info mapinfo;
510 struct cam_path *old_path;
513 * We can't deal with physical addresses for this
514 * type of transaction.
516 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
523 * Save this in case the caller had it set to
524 * something in particular.
526 old_path = inccb->ccb_h.path;
529 * We really don't need a path for the matching
530 * code. The path is needed because of the
531 * debugging statements in xpt_action(). They
532 * assume that the CCB has a valid path.
534 inccb->ccb_h.path = xpt_periph->path;
536 bzero(&mapinfo, sizeof(mapinfo));
539 * Map the pattern and match buffers into kernel
540 * virtual address space.
542 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
545 inccb->ccb_h.path = old_path;
550 * This is an immediate CCB, we can send it on directly.
555 * Map the buffers back into user space.
557 cam_periph_unmapmem(inccb, &mapinfo);
559 inccb->ccb_h.path = old_path;
568 xpt_release_bus(bus);
572 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
573 * with the periphal driver name and unit name filled in. The other
574 * fields don't really matter as input. The passthrough driver name
575 * ("pass"), and unit number are passed back in the ccb. The current
576 * device generation number, and the index into the device peripheral
577 * driver list, and the status are also passed back. Note that
578 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
579 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
580 * (or rather should be) impossible for the device peripheral driver
581 * list to change since we look at the whole thing in one pass, and
582 * we do it with lock protection.
585 case CAMGETPASSTHRU: {
587 struct cam_periph *periph;
588 struct periph_driver **p_drv;
591 int base_periph_found;
593 ccb = (union ccb *)addr;
594 unit = ccb->cgdl.unit_number;
595 name = ccb->cgdl.periph_name;
596 base_periph_found = 0;
599 * Sanity check -- make sure we don't get a null peripheral
602 if (*ccb->cgdl.periph_name == '\0') {
607 /* Keep the list from changing while we traverse it */
610 /* first find our driver in the list of drivers */
611 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
612 if (strcmp((*p_drv)->driver_name, name) == 0)
615 if (*p_drv == NULL) {
617 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
618 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
619 *ccb->cgdl.periph_name = '\0';
620 ccb->cgdl.unit_number = 0;
626 * Run through every peripheral instance of this driver
627 * and check to see whether it matches the unit passed
628 * in by the user. If it does, get out of the loops and
629 * find the passthrough driver associated with that
632 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
633 periph = TAILQ_NEXT(periph, unit_links)) {
635 if (periph->unit_number == unit)
639 * If we found the peripheral driver that the user passed
640 * in, go through all of the peripheral drivers for that
641 * particular device and look for a passthrough driver.
643 if (periph != NULL) {
644 struct cam_ed *device;
647 base_periph_found = 1;
648 device = periph->path->device;
649 for (i = 0, periph = SLIST_FIRST(&device->periphs);
651 periph = SLIST_NEXT(periph, periph_links), i++) {
653 * Check to see whether we have a
654 * passthrough device or not.
656 if (strcmp(periph->periph_name, "pass") == 0) {
658 * Fill in the getdevlist fields.
660 strlcpy(ccb->cgdl.periph_name,
662 sizeof(ccb->cgdl.periph_name));
663 ccb->cgdl.unit_number =
665 if (SLIST_NEXT(periph, periph_links))
667 CAM_GDEVLIST_MORE_DEVS;
670 CAM_GDEVLIST_LAST_DEVICE;
671 ccb->cgdl.generation =
675 * Fill in some CCB header fields
676 * that the user may want.
679 periph->path->bus->path_id;
680 ccb->ccb_h.target_id =
681 periph->path->target->target_id;
682 ccb->ccb_h.target_lun =
683 periph->path->device->lun_id;
684 ccb->ccb_h.status = CAM_REQ_CMP;
691 * If the periph is null here, one of two things has
692 * happened. The first possibility is that we couldn't
693 * find the unit number of the particular peripheral driver
694 * that the user is asking about. e.g. the user asks for
695 * the passthrough driver for "da11". We find the list of
696 * "da" peripherals all right, but there is no unit 11.
697 * The other possibility is that we went through the list
698 * of peripheral drivers attached to the device structure,
699 * but didn't find one with the name "pass". Either way,
700 * we return ENOENT, since we couldn't find something.
702 if (periph == NULL) {
703 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
704 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
705 *ccb->cgdl.periph_name = '\0';
706 ccb->cgdl.unit_number = 0;
709 * It is unfortunate that this is even necessary,
710 * but there are many, many clueless users out there.
711 * If this is true, the user is looking for the
712 * passthrough driver, but doesn't have one in his
715 if (base_periph_found == 1) {
716 printf("xptioctl: pass driver is not in the "
718 printf("xptioctl: put \"device pass\" in "
719 "your kernel config file\n");
734 cam_module_event_handler(module_t mod, int what, void *arg)
740 if ((error = xpt_init(NULL)) != 0)
752 static struct xpt_proto *
753 xpt_proto_find(cam_proto proto)
755 struct xpt_proto **pp;
757 SET_FOREACH(pp, cam_xpt_proto_set) {
758 if ((*pp)->proto == proto)
766 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
769 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
770 xpt_free_path(done_ccb->ccb_h.path);
771 xpt_free_ccb(done_ccb);
773 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
774 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
779 /* thread to handle bus rescans */
781 xpt_scanner_thread(void *dummy)
784 struct cam_path path;
788 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
789 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
791 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
792 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
796 * Since lock can be dropped inside and path freed
797 * by completion callback even before return here,
798 * take our own path copy for reference.
800 xpt_copy_path(&path, ccb->ccb_h.path);
801 xpt_path_lock(&path);
803 xpt_path_unlock(&path);
804 xpt_release_path(&path);
812 xpt_rescan(union ccb *ccb)
816 /* Prepare request */
817 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
818 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
819 ccb->ccb_h.func_code = XPT_SCAN_BUS;
820 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
821 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
822 ccb->ccb_h.func_code = XPT_SCAN_TGT;
823 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
824 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
825 ccb->ccb_h.func_code = XPT_SCAN_LUN;
827 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
828 xpt_free_path(ccb->ccb_h.path);
832 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
833 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
834 xpt_action_name(ccb->ccb_h.func_code)));
836 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
837 ccb->ccb_h.cbfcnp = xpt_rescan_done;
838 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
839 /* Don't make duplicate entries for the same paths. */
841 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
842 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
843 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
844 wakeup(&xsoftc.ccb_scanq);
846 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
847 xpt_free_path(ccb->ccb_h.path);
853 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
854 xsoftc.buses_to_config++;
855 wakeup(&xsoftc.ccb_scanq);
859 /* Functions accessed by the peripheral drivers */
861 xpt_init(void *dummy)
863 struct cam_sim *xpt_sim;
864 struct cam_path *path;
865 struct cam_devq *devq;
869 TAILQ_INIT(&xsoftc.xpt_busses);
870 TAILQ_INIT(&xsoftc.ccb_scanq);
871 STAILQ_INIT(&xsoftc.highpowerq);
872 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
874 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
875 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
876 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
877 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
879 #ifdef CAM_BOOT_DELAY
881 * Override this value at compile time to assist our users
882 * who don't use loader to boot a kernel.
884 xsoftc.boot_delay = CAM_BOOT_DELAY;
887 * The xpt layer is, itself, the equivalent of a SIM.
888 * Allow 16 ccbs in the ccb pool for it. This should
889 * give decent parallelism when we probe busses and
890 * perform other XPT functions.
892 devq = cam_simq_alloc(16);
893 xpt_sim = cam_sim_alloc(xptaction,
898 /*mtx*/&xsoftc.xpt_lock,
899 /*max_dev_transactions*/0,
900 /*max_tagged_dev_transactions*/0,
905 mtx_lock(&xsoftc.xpt_lock);
906 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
907 mtx_unlock(&xsoftc.xpt_lock);
908 printf("xpt_init: xpt_bus_register failed with status %#x,"
909 " failing attach\n", status);
912 mtx_unlock(&xsoftc.xpt_lock);
915 * Looking at the XPT from the SIM layer, the XPT is
916 * the equivalent of a peripheral driver. Allocate
917 * a peripheral driver entry for us.
919 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
921 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
922 printf("xpt_init: xpt_create_path failed with status %#x,"
923 " failing attach\n", status);
927 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
928 path, NULL, 0, xpt_sim);
929 xpt_path_unlock(path);
932 if (cam_num_doneqs < 1)
933 cam_num_doneqs = 1 + mp_ncpus / 6;
934 else if (cam_num_doneqs > MAXCPU)
935 cam_num_doneqs = MAXCPU;
936 for (i = 0; i < cam_num_doneqs; i++) {
937 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
939 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
940 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
941 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
947 if (cam_num_doneqs < 1) {
948 printf("xpt_init: Cannot init completion queues "
949 "- failing attach\n");
953 * Register a callback for when interrupts are enabled.
955 xsoftc.xpt_config_hook =
956 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
957 M_CAMXPT, M_NOWAIT | M_ZERO);
958 if (xsoftc.xpt_config_hook == NULL) {
959 printf("xpt_init: Cannot malloc config hook "
960 "- failing attach\n");
963 xsoftc.xpt_config_hook->ich_func = xpt_config;
964 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
965 free (xsoftc.xpt_config_hook, M_CAMXPT);
966 printf("xpt_init: config_intrhook_establish failed "
967 "- failing attach\n");
974 xptregister(struct cam_periph *periph, void *arg)
976 struct cam_sim *xpt_sim;
978 if (periph == NULL) {
979 printf("xptregister: periph was NULL!!\n");
980 return(CAM_REQ_CMP_ERR);
983 xpt_sim = (struct cam_sim *)arg;
984 xpt_sim->softc = periph;
986 periph->softc = NULL;
992 xpt_add_periph(struct cam_periph *periph)
994 struct cam_ed *device;
997 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
998 device = periph->path->device;
999 status = CAM_REQ_CMP;
1000 if (device != NULL) {
1001 mtx_lock(&device->target->bus->eb_mtx);
1002 device->generation++;
1003 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1004 mtx_unlock(&device->target->bus->eb_mtx);
1005 atomic_add_32(&xsoftc.xpt_generation, 1);
1012 xpt_remove_periph(struct cam_periph *periph)
1014 struct cam_ed *device;
1016 device = periph->path->device;
1017 if (device != NULL) {
1018 mtx_lock(&device->target->bus->eb_mtx);
1019 device->generation++;
1020 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1021 mtx_unlock(&device->target->bus->eb_mtx);
1022 atomic_add_32(&xsoftc.xpt_generation, 1);
1028 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1030 struct cam_path *path = periph->path;
1031 struct xpt_proto *proto;
1033 cam_periph_assert(periph, MA_OWNED);
1034 periph->flags |= CAM_PERIPH_ANNOUNCED;
1036 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1037 periph->periph_name, periph->unit_number,
1038 path->bus->sim->sim_name,
1039 path->bus->sim->unit_number,
1040 path->bus->sim->bus_id,
1042 path->target->target_id,
1043 (uintmax_t)path->device->lun_id);
1044 printf("%s%d: ", periph->periph_name, periph->unit_number);
1045 proto = xpt_proto_find(path->device->protocol);
1047 proto->ops->announce(path->device);
1049 printf("%s%d: Unknown protocol device %d\n",
1050 periph->periph_name, periph->unit_number,
1051 path->device->protocol);
1052 if (path->device->serial_num_len > 0) {
1053 /* Don't wrap the screen - print only the first 60 chars */
1054 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1055 periph->unit_number, path->device->serial_num);
1057 /* Announce transport details. */
1058 path->bus->xport->ops->announce(periph);
1059 /* Announce command queueing. */
1060 if (path->device->inq_flags & SID_CmdQue
1061 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1062 printf("%s%d: Command Queueing enabled\n",
1063 periph->periph_name, periph->unit_number);
1065 /* Announce caller's details if they've passed in. */
1066 if (announce_string != NULL)
1067 printf("%s%d: %s\n", periph->periph_name,
1068 periph->unit_number, announce_string);
1072 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1075 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1076 periph->unit_number, quirks, bit_string);
1081 xpt_denounce_periph(struct cam_periph *periph)
1083 struct cam_path *path = periph->path;
1084 struct xpt_proto *proto;
1086 cam_periph_assert(periph, MA_OWNED);
1087 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1088 periph->periph_name, periph->unit_number,
1089 path->bus->sim->sim_name,
1090 path->bus->sim->unit_number,
1091 path->bus->sim->bus_id,
1093 path->target->target_id,
1094 (uintmax_t)path->device->lun_id);
1095 printf("%s%d: ", periph->periph_name, periph->unit_number);
1096 proto = xpt_proto_find(path->device->protocol);
1098 proto->ops->denounce(path->device);
1100 printf("%s%d: Unknown protocol device %d\n",
1101 periph->periph_name, periph->unit_number,
1102 path->device->protocol);
1103 if (path->device->serial_num_len > 0)
1104 printf(" s/n %.60s", path->device->serial_num);
1105 printf(" detached\n");
1110 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1113 struct ccb_dev_advinfo cdai;
1114 struct scsi_vpd_id_descriptor *idd;
1116 xpt_path_assert(path, MA_OWNED);
1118 memset(&cdai, 0, sizeof(cdai));
1119 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1120 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1121 cdai.flags = CDAI_FLAG_NONE;
1124 if (!strcmp(attr, "GEOM::ident"))
1125 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1126 else if (!strcmp(attr, "GEOM::physpath"))
1127 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1128 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1129 strcmp(attr, "GEOM::lunname") == 0) {
1130 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1131 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1135 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1136 if (cdai.buf == NULL) {
1140 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1141 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1142 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1143 if (cdai.provsiz == 0)
1145 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1146 if (strcmp(attr, "GEOM::lunid") == 0) {
1147 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1148 cdai.provsiz, scsi_devid_is_lun_naa);
1150 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1151 cdai.provsiz, scsi_devid_is_lun_eui64);
1153 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1154 cdai.provsiz, scsi_devid_is_lun_uuid);
1156 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1157 cdai.provsiz, scsi_devid_is_lun_md5);
1161 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1162 cdai.provsiz, scsi_devid_is_lun_t10);
1164 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1165 cdai.provsiz, scsi_devid_is_lun_name);
1169 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1170 if (idd->length < len) {
1171 for (l = 0; l < idd->length; l++)
1172 buf[l] = idd->identifier[l] ?
1173 idd->identifier[l] : ' ';
1177 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1178 l = strnlen(idd->identifier, idd->length);
1180 bcopy(idd->identifier, buf, l);
1184 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1185 && idd->identifier[0] == 0x10) {
1186 if ((idd->length - 2) * 2 + 4 < len) {
1187 for (l = 2, o = 0; l < idd->length; l++) {
1188 if (l == 6 || l == 8 || l == 10 || l == 12)
1189 o += sprintf(buf + o, "-");
1190 o += sprintf(buf + o, "%02x",
1191 idd->identifier[l]);
1196 if (idd->length * 2 < len) {
1197 for (l = 0; l < idd->length; l++)
1198 sprintf(buf + l * 2, "%02x",
1199 idd->identifier[l]);
1205 if (strlcpy(buf, cdai.buf, len) >= len)
1210 if (cdai.buf != NULL)
1211 free(cdai.buf, M_CAMXPT);
1215 static dev_match_ret
1216 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1219 dev_match_ret retval;
1222 retval = DM_RET_NONE;
1225 * If we aren't given something to match against, that's an error.
1228 return(DM_RET_ERROR);
1231 * If there are no match entries, then this bus matches no
1234 if ((patterns == NULL) || (num_patterns == 0))
1235 return(DM_RET_DESCEND | DM_RET_COPY);
1237 for (i = 0; i < num_patterns; i++) {
1238 struct bus_match_pattern *cur_pattern;
1241 * If the pattern in question isn't for a bus node, we
1242 * aren't interested. However, we do indicate to the
1243 * calling routine that we should continue descending the
1244 * tree, since the user wants to match against lower-level
1247 if (patterns[i].type != DEV_MATCH_BUS) {
1248 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1249 retval |= DM_RET_DESCEND;
1253 cur_pattern = &patterns[i].pattern.bus_pattern;
1256 * If they want to match any bus node, we give them any
1259 if (cur_pattern->flags == BUS_MATCH_ANY) {
1260 /* set the copy flag */
1261 retval |= DM_RET_COPY;
1264 * If we've already decided on an action, go ahead
1267 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1272 * Not sure why someone would do this...
1274 if (cur_pattern->flags == BUS_MATCH_NONE)
1277 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1278 && (cur_pattern->path_id != bus->path_id))
1281 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1282 && (cur_pattern->bus_id != bus->sim->bus_id))
1285 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1286 && (cur_pattern->unit_number != bus->sim->unit_number))
1289 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1290 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1295 * If we get to this point, the user definitely wants
1296 * information on this bus. So tell the caller to copy the
1299 retval |= DM_RET_COPY;
1302 * If the return action has been set to descend, then we
1303 * know that we've already seen a non-bus matching
1304 * expression, therefore we need to further descend the tree.
1305 * This won't change by continuing around the loop, so we
1306 * go ahead and return. If we haven't seen a non-bus
1307 * matching expression, we keep going around the loop until
1308 * we exhaust the matching expressions. We'll set the stop
1309 * flag once we fall out of the loop.
1311 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1316 * If the return action hasn't been set to descend yet, that means
1317 * we haven't seen anything other than bus matching patterns. So
1318 * tell the caller to stop descending the tree -- the user doesn't
1319 * want to match against lower level tree elements.
1321 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1322 retval |= DM_RET_STOP;
1327 static dev_match_ret
1328 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1329 struct cam_ed *device)
1331 dev_match_ret retval;
1334 retval = DM_RET_NONE;
1337 * If we aren't given something to match against, that's an error.
1340 return(DM_RET_ERROR);
1343 * If there are no match entries, then this device matches no
1346 if ((patterns == NULL) || (num_patterns == 0))
1347 return(DM_RET_DESCEND | DM_RET_COPY);
1349 for (i = 0; i < num_patterns; i++) {
1350 struct device_match_pattern *cur_pattern;
1351 struct scsi_vpd_device_id *device_id_page;
1354 * If the pattern in question isn't for a device node, we
1355 * aren't interested.
1357 if (patterns[i].type != DEV_MATCH_DEVICE) {
1358 if ((patterns[i].type == DEV_MATCH_PERIPH)
1359 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1360 retval |= DM_RET_DESCEND;
1364 cur_pattern = &patterns[i].pattern.device_pattern;
1366 /* Error out if mutually exclusive options are specified. */
1367 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1368 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1369 return(DM_RET_ERROR);
1372 * If they want to match any device node, we give them any
1375 if (cur_pattern->flags == DEV_MATCH_ANY)
1379 * Not sure why someone would do this...
1381 if (cur_pattern->flags == DEV_MATCH_NONE)
1384 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1385 && (cur_pattern->path_id != device->target->bus->path_id))
1388 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1389 && (cur_pattern->target_id != device->target->target_id))
1392 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1393 && (cur_pattern->target_lun != device->lun_id))
1396 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1397 && (cam_quirkmatch((caddr_t)&device->inq_data,
1398 (caddr_t)&cur_pattern->data.inq_pat,
1399 1, sizeof(cur_pattern->data.inq_pat),
1400 scsi_static_inquiry_match) == NULL))
1403 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1404 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1405 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1406 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1407 device->device_id_len
1408 - SVPD_DEVICE_ID_HDR_LEN,
1409 cur_pattern->data.devid_pat.id,
1410 cur_pattern->data.devid_pat.id_len) != 0))
1415 * If we get to this point, the user definitely wants
1416 * information on this device. So tell the caller to copy
1419 retval |= DM_RET_COPY;
1422 * If the return action has been set to descend, then we
1423 * know that we've already seen a peripheral matching
1424 * expression, therefore we need to further descend the tree.
1425 * This won't change by continuing around the loop, so we
1426 * go ahead and return. If we haven't seen a peripheral
1427 * matching expression, we keep going around the loop until
1428 * we exhaust the matching expressions. We'll set the stop
1429 * flag once we fall out of the loop.
1431 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1436 * If the return action hasn't been set to descend yet, that means
1437 * we haven't seen any peripheral matching patterns. So tell the
1438 * caller to stop descending the tree -- the user doesn't want to
1439 * match against lower level tree elements.
1441 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1442 retval |= DM_RET_STOP;
1448 * Match a single peripheral against any number of match patterns.
1450 static dev_match_ret
1451 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1452 struct cam_periph *periph)
1454 dev_match_ret retval;
1458 * If we aren't given something to match against, that's an error.
1461 return(DM_RET_ERROR);
1464 * If there are no match entries, then this peripheral matches no
1467 if ((patterns == NULL) || (num_patterns == 0))
1468 return(DM_RET_STOP | DM_RET_COPY);
1471 * There aren't any nodes below a peripheral node, so there's no
1472 * reason to descend the tree any further.
1474 retval = DM_RET_STOP;
1476 for (i = 0; i < num_patterns; i++) {
1477 struct periph_match_pattern *cur_pattern;
1480 * If the pattern in question isn't for a peripheral, we
1481 * aren't interested.
1483 if (patterns[i].type != DEV_MATCH_PERIPH)
1486 cur_pattern = &patterns[i].pattern.periph_pattern;
1489 * If they want to match on anything, then we will do so.
1491 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1492 /* set the copy flag */
1493 retval |= DM_RET_COPY;
1496 * We've already set the return action to stop,
1497 * since there are no nodes below peripherals in
1504 * Not sure why someone would do this...
1506 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1509 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1510 && (cur_pattern->path_id != periph->path->bus->path_id))
1514 * For the target and lun id's, we have to make sure the
1515 * target and lun pointers aren't NULL. The xpt peripheral
1516 * has a wildcard target and device.
1518 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1519 && ((periph->path->target == NULL)
1520 ||(cur_pattern->target_id != periph->path->target->target_id)))
1523 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1524 && ((periph->path->device == NULL)
1525 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1528 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1529 && (cur_pattern->unit_number != periph->unit_number))
1532 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1533 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1538 * If we get to this point, the user definitely wants
1539 * information on this peripheral. So tell the caller to
1540 * copy the data out.
1542 retval |= DM_RET_COPY;
1545 * The return action has already been set to stop, since
1546 * peripherals don't have any nodes below them in the EDT.
1552 * If we get to this point, the peripheral that was passed in
1553 * doesn't match any of the patterns.
1559 xptedtbusfunc(struct cam_eb *bus, void *arg)
1561 struct ccb_dev_match *cdm;
1562 struct cam_et *target;
1563 dev_match_ret retval;
1565 cdm = (struct ccb_dev_match *)arg;
1568 * If our position is for something deeper in the tree, that means
1569 * that we've already seen this node. So, we keep going down.
1571 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1572 && (cdm->pos.cookie.bus == bus)
1573 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1574 && (cdm->pos.cookie.target != NULL))
1575 retval = DM_RET_DESCEND;
1577 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1580 * If we got an error, bail out of the search.
1582 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1583 cdm->status = CAM_DEV_MATCH_ERROR;
1588 * If the copy flag is set, copy this bus out.
1590 if (retval & DM_RET_COPY) {
1593 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1594 sizeof(struct dev_match_result));
1597 * If we don't have enough space to put in another
1598 * match result, save our position and tell the
1599 * user there are more devices to check.
1601 if (spaceleft < sizeof(struct dev_match_result)) {
1602 bzero(&cdm->pos, sizeof(cdm->pos));
1603 cdm->pos.position_type =
1604 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1606 cdm->pos.cookie.bus = bus;
1607 cdm->pos.generations[CAM_BUS_GENERATION]=
1608 xsoftc.bus_generation;
1609 cdm->status = CAM_DEV_MATCH_MORE;
1612 j = cdm->num_matches;
1614 cdm->matches[j].type = DEV_MATCH_BUS;
1615 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1616 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1617 cdm->matches[j].result.bus_result.unit_number =
1618 bus->sim->unit_number;
1619 strlcpy(cdm->matches[j].result.bus_result.dev_name,
1621 sizeof(cdm->matches[j].result.bus_result.dev_name));
1625 * If the user is only interested in busses, there's no
1626 * reason to descend to the next level in the tree.
1628 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1632 * If there is a target generation recorded, check it to
1633 * make sure the target list hasn't changed.
1635 mtx_lock(&bus->eb_mtx);
1636 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1637 && (cdm->pos.cookie.bus == bus)
1638 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1639 && (cdm->pos.cookie.target != NULL)) {
1640 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1642 mtx_unlock(&bus->eb_mtx);
1643 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1646 target = (struct cam_et *)cdm->pos.cookie.target;
1650 mtx_unlock(&bus->eb_mtx);
1652 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1656 xptedttargetfunc(struct cam_et *target, void *arg)
1658 struct ccb_dev_match *cdm;
1660 struct cam_ed *device;
1662 cdm = (struct ccb_dev_match *)arg;
1666 * If there is a device list generation recorded, check it to
1667 * make sure the device list hasn't changed.
1669 mtx_lock(&bus->eb_mtx);
1670 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1671 && (cdm->pos.cookie.bus == bus)
1672 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1673 && (cdm->pos.cookie.target == target)
1674 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1675 && (cdm->pos.cookie.device != NULL)) {
1676 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1677 target->generation) {
1678 mtx_unlock(&bus->eb_mtx);
1679 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1682 device = (struct cam_ed *)cdm->pos.cookie.device;
1686 mtx_unlock(&bus->eb_mtx);
1688 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1692 xptedtdevicefunc(struct cam_ed *device, void *arg)
1695 struct cam_periph *periph;
1696 struct ccb_dev_match *cdm;
1697 dev_match_ret retval;
1699 cdm = (struct ccb_dev_match *)arg;
1700 bus = device->target->bus;
1703 * If our position is for something deeper in the tree, that means
1704 * that we've already seen this node. So, we keep going down.
1706 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1707 && (cdm->pos.cookie.device == device)
1708 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1709 && (cdm->pos.cookie.periph != NULL))
1710 retval = DM_RET_DESCEND;
1712 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1715 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1716 cdm->status = CAM_DEV_MATCH_ERROR;
1721 * If the copy flag is set, copy this device out.
1723 if (retval & DM_RET_COPY) {
1726 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1727 sizeof(struct dev_match_result));
1730 * If we don't have enough space to put in another
1731 * match result, save our position and tell the
1732 * user there are more devices to check.
1734 if (spaceleft < sizeof(struct dev_match_result)) {
1735 bzero(&cdm->pos, sizeof(cdm->pos));
1736 cdm->pos.position_type =
1737 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1738 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1740 cdm->pos.cookie.bus = device->target->bus;
1741 cdm->pos.generations[CAM_BUS_GENERATION]=
1742 xsoftc.bus_generation;
1743 cdm->pos.cookie.target = device->target;
1744 cdm->pos.generations[CAM_TARGET_GENERATION] =
1745 device->target->bus->generation;
1746 cdm->pos.cookie.device = device;
1747 cdm->pos.generations[CAM_DEV_GENERATION] =
1748 device->target->generation;
1749 cdm->status = CAM_DEV_MATCH_MORE;
1752 j = cdm->num_matches;
1754 cdm->matches[j].type = DEV_MATCH_DEVICE;
1755 cdm->matches[j].result.device_result.path_id =
1756 device->target->bus->path_id;
1757 cdm->matches[j].result.device_result.target_id =
1758 device->target->target_id;
1759 cdm->matches[j].result.device_result.target_lun =
1761 cdm->matches[j].result.device_result.protocol =
1763 bcopy(&device->inq_data,
1764 &cdm->matches[j].result.device_result.inq_data,
1765 sizeof(struct scsi_inquiry_data));
1766 bcopy(&device->ident_data,
1767 &cdm->matches[j].result.device_result.ident_data,
1768 sizeof(struct ata_params));
1770 /* Let the user know whether this device is unconfigured */
1771 if (device->flags & CAM_DEV_UNCONFIGURED)
1772 cdm->matches[j].result.device_result.flags =
1773 DEV_RESULT_UNCONFIGURED;
1775 cdm->matches[j].result.device_result.flags =
1780 * If the user isn't interested in peripherals, don't descend
1781 * the tree any further.
1783 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1787 * If there is a peripheral list generation recorded, make sure
1788 * it hasn't changed.
1791 mtx_lock(&bus->eb_mtx);
1792 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1793 && (cdm->pos.cookie.bus == bus)
1794 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1795 && (cdm->pos.cookie.target == device->target)
1796 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1797 && (cdm->pos.cookie.device == device)
1798 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1799 && (cdm->pos.cookie.periph != NULL)) {
1800 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1801 device->generation) {
1802 mtx_unlock(&bus->eb_mtx);
1804 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1807 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1811 mtx_unlock(&bus->eb_mtx);
1814 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1818 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1820 struct ccb_dev_match *cdm;
1821 dev_match_ret retval;
1823 cdm = (struct ccb_dev_match *)arg;
1825 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1827 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1828 cdm->status = CAM_DEV_MATCH_ERROR;
1833 * If the copy flag is set, copy this peripheral out.
1835 if (retval & DM_RET_COPY) {
1839 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1840 sizeof(struct dev_match_result));
1843 * If we don't have enough space to put in another
1844 * match result, save our position and tell the
1845 * user there are more devices to check.
1847 if (spaceleft < sizeof(struct dev_match_result)) {
1848 bzero(&cdm->pos, sizeof(cdm->pos));
1849 cdm->pos.position_type =
1850 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1851 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1854 cdm->pos.cookie.bus = periph->path->bus;
1855 cdm->pos.generations[CAM_BUS_GENERATION]=
1856 xsoftc.bus_generation;
1857 cdm->pos.cookie.target = periph->path->target;
1858 cdm->pos.generations[CAM_TARGET_GENERATION] =
1859 periph->path->bus->generation;
1860 cdm->pos.cookie.device = periph->path->device;
1861 cdm->pos.generations[CAM_DEV_GENERATION] =
1862 periph->path->target->generation;
1863 cdm->pos.cookie.periph = periph;
1864 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1865 periph->path->device->generation;
1866 cdm->status = CAM_DEV_MATCH_MORE;
1870 j = cdm->num_matches;
1872 cdm->matches[j].type = DEV_MATCH_PERIPH;
1873 cdm->matches[j].result.periph_result.path_id =
1874 periph->path->bus->path_id;
1875 cdm->matches[j].result.periph_result.target_id =
1876 periph->path->target->target_id;
1877 cdm->matches[j].result.periph_result.target_lun =
1878 periph->path->device->lun_id;
1879 cdm->matches[j].result.periph_result.unit_number =
1880 periph->unit_number;
1881 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
1882 strlcpy(cdm->matches[j].result.periph_result.periph_name,
1883 periph->periph_name, l);
1890 xptedtmatch(struct ccb_dev_match *cdm)
1895 cdm->num_matches = 0;
1898 * Check the bus list generation. If it has changed, the user
1899 * needs to reset everything and start over.
1902 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1903 && (cdm->pos.cookie.bus != NULL)) {
1904 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1905 xsoftc.bus_generation) {
1907 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1910 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1916 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1919 * If we get back 0, that means that we had to stop before fully
1920 * traversing the EDT. It also means that one of the subroutines
1921 * has set the status field to the proper value. If we get back 1,
1922 * we've fully traversed the EDT and copied out any matching entries.
1925 cdm->status = CAM_DEV_MATCH_LAST;
1931 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1933 struct cam_periph *periph;
1934 struct ccb_dev_match *cdm;
1936 cdm = (struct ccb_dev_match *)arg;
1939 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1940 && (cdm->pos.cookie.pdrv == pdrv)
1941 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1942 && (cdm->pos.cookie.periph != NULL)) {
1943 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1944 (*pdrv)->generation) {
1946 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1949 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1955 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1959 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1961 struct ccb_dev_match *cdm;
1962 dev_match_ret retval;
1964 cdm = (struct ccb_dev_match *)arg;
1966 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1968 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1969 cdm->status = CAM_DEV_MATCH_ERROR;
1974 * If the copy flag is set, copy this peripheral out.
1976 if (retval & DM_RET_COPY) {
1980 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1981 sizeof(struct dev_match_result));
1984 * If we don't have enough space to put in another
1985 * match result, save our position and tell the
1986 * user there are more devices to check.
1988 if (spaceleft < sizeof(struct dev_match_result)) {
1989 struct periph_driver **pdrv;
1992 bzero(&cdm->pos, sizeof(cdm->pos));
1993 cdm->pos.position_type =
1994 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1998 * This may look a bit non-sensical, but it is
1999 * actually quite logical. There are very few
2000 * peripheral drivers, and bloating every peripheral
2001 * structure with a pointer back to its parent
2002 * peripheral driver linker set entry would cost
2003 * more in the long run than doing this quick lookup.
2005 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2006 if (strcmp((*pdrv)->driver_name,
2007 periph->periph_name) == 0)
2011 if (*pdrv == NULL) {
2012 cdm->status = CAM_DEV_MATCH_ERROR;
2016 cdm->pos.cookie.pdrv = pdrv;
2018 * The periph generation slot does double duty, as
2019 * does the periph pointer slot. They are used for
2020 * both edt and pdrv lookups and positioning.
2022 cdm->pos.cookie.periph = periph;
2023 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2024 (*pdrv)->generation;
2025 cdm->status = CAM_DEV_MATCH_MORE;
2029 j = cdm->num_matches;
2031 cdm->matches[j].type = DEV_MATCH_PERIPH;
2032 cdm->matches[j].result.periph_result.path_id =
2033 periph->path->bus->path_id;
2036 * The transport layer peripheral doesn't have a target or
2039 if (periph->path->target)
2040 cdm->matches[j].result.periph_result.target_id =
2041 periph->path->target->target_id;
2043 cdm->matches[j].result.periph_result.target_id =
2044 CAM_TARGET_WILDCARD;
2046 if (periph->path->device)
2047 cdm->matches[j].result.periph_result.target_lun =
2048 periph->path->device->lun_id;
2050 cdm->matches[j].result.periph_result.target_lun =
2053 cdm->matches[j].result.periph_result.unit_number =
2054 periph->unit_number;
2055 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2056 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2057 periph->periph_name, l);
2064 xptperiphlistmatch(struct ccb_dev_match *cdm)
2068 cdm->num_matches = 0;
2071 * At this point in the edt traversal function, we check the bus
2072 * list generation to make sure that no busses have been added or
2073 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2074 * For the peripheral driver list traversal function, however, we
2075 * don't have to worry about new peripheral driver types coming or
2076 * going; they're in a linker set, and therefore can't change
2077 * without a recompile.
2080 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2081 && (cdm->pos.cookie.pdrv != NULL))
2082 ret = xptpdrvtraverse(
2083 (struct periph_driver **)cdm->pos.cookie.pdrv,
2084 xptplistpdrvfunc, cdm);
2086 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2089 * If we get back 0, that means that we had to stop before fully
2090 * traversing the peripheral driver tree. It also means that one of
2091 * the subroutines has set the status field to the proper value. If
2092 * we get back 1, we've fully traversed the EDT and copied out any
2096 cdm->status = CAM_DEV_MATCH_LAST;
2102 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2104 struct cam_eb *bus, *next_bus;
2112 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2120 for (; bus != NULL; bus = next_bus) {
2121 retval = tr_func(bus, arg);
2123 xpt_release_bus(bus);
2127 next_bus = TAILQ_NEXT(bus, links);
2129 next_bus->refcount++;
2131 xpt_release_bus(bus);
2137 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2138 xpt_targetfunc_t *tr_func, void *arg)
2140 struct cam_et *target, *next_target;
2145 target = start_target;
2147 mtx_lock(&bus->eb_mtx);
2148 target = TAILQ_FIRST(&bus->et_entries);
2149 if (target == NULL) {
2150 mtx_unlock(&bus->eb_mtx);
2154 mtx_unlock(&bus->eb_mtx);
2156 for (; target != NULL; target = next_target) {
2157 retval = tr_func(target, arg);
2159 xpt_release_target(target);
2162 mtx_lock(&bus->eb_mtx);
2163 next_target = TAILQ_NEXT(target, links);
2165 next_target->refcount++;
2166 mtx_unlock(&bus->eb_mtx);
2167 xpt_release_target(target);
2173 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2174 xpt_devicefunc_t *tr_func, void *arg)
2177 struct cam_ed *device, *next_device;
2183 device = start_device;
2185 mtx_lock(&bus->eb_mtx);
2186 device = TAILQ_FIRST(&target->ed_entries);
2187 if (device == NULL) {
2188 mtx_unlock(&bus->eb_mtx);
2192 mtx_unlock(&bus->eb_mtx);
2194 for (; device != NULL; device = next_device) {
2195 mtx_lock(&device->device_mtx);
2196 retval = tr_func(device, arg);
2197 mtx_unlock(&device->device_mtx);
2199 xpt_release_device(device);
2202 mtx_lock(&bus->eb_mtx);
2203 next_device = TAILQ_NEXT(device, links);
2205 next_device->refcount++;
2206 mtx_unlock(&bus->eb_mtx);
2207 xpt_release_device(device);
2213 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2214 xpt_periphfunc_t *tr_func, void *arg)
2217 struct cam_periph *periph, *next_periph;
2222 bus = device->target->bus;
2224 periph = start_periph;
2227 mtx_lock(&bus->eb_mtx);
2228 periph = SLIST_FIRST(&device->periphs);
2229 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2230 periph = SLIST_NEXT(periph, periph_links);
2231 if (periph == NULL) {
2232 mtx_unlock(&bus->eb_mtx);
2237 mtx_unlock(&bus->eb_mtx);
2240 for (; periph != NULL; periph = next_periph) {
2241 retval = tr_func(periph, arg);
2243 cam_periph_release_locked(periph);
2247 mtx_lock(&bus->eb_mtx);
2248 next_periph = SLIST_NEXT(periph, periph_links);
2249 while (next_periph != NULL &&
2250 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2251 next_periph = SLIST_NEXT(next_periph, periph_links);
2253 next_periph->refcount++;
2254 mtx_unlock(&bus->eb_mtx);
2256 cam_periph_release_locked(periph);
2262 xptpdrvtraverse(struct periph_driver **start_pdrv,
2263 xpt_pdrvfunc_t *tr_func, void *arg)
2265 struct periph_driver **pdrv;
2271 * We don't traverse the peripheral driver list like we do the
2272 * other lists, because it is a linker set, and therefore cannot be
2273 * changed during runtime. If the peripheral driver list is ever
2274 * re-done to be something other than a linker set (i.e. it can
2275 * change while the system is running), the list traversal should
2276 * be modified to work like the other traversal functions.
2278 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2279 *pdrv != NULL; pdrv++) {
2280 retval = tr_func(pdrv, arg);
2290 xptpdperiphtraverse(struct periph_driver **pdrv,
2291 struct cam_periph *start_periph,
2292 xpt_periphfunc_t *tr_func, void *arg)
2294 struct cam_periph *periph, *next_periph;
2300 periph = start_periph;
2303 periph = TAILQ_FIRST(&(*pdrv)->units);
2304 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2305 periph = TAILQ_NEXT(periph, unit_links);
2306 if (periph == NULL) {
2313 for (; periph != NULL; periph = next_periph) {
2314 cam_periph_lock(periph);
2315 retval = tr_func(periph, arg);
2316 cam_periph_unlock(periph);
2318 cam_periph_release(periph);
2322 next_periph = TAILQ_NEXT(periph, unit_links);
2323 while (next_periph != NULL &&
2324 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2325 next_periph = TAILQ_NEXT(next_periph, unit_links);
2327 next_periph->refcount++;
2329 cam_periph_release(periph);
2335 xptdefbusfunc(struct cam_eb *bus, void *arg)
2337 struct xpt_traverse_config *tr_config;
2339 tr_config = (struct xpt_traverse_config *)arg;
2341 if (tr_config->depth == XPT_DEPTH_BUS) {
2342 xpt_busfunc_t *tr_func;
2344 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2346 return(tr_func(bus, tr_config->tr_arg));
2348 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2352 xptdeftargetfunc(struct cam_et *target, void *arg)
2354 struct xpt_traverse_config *tr_config;
2356 tr_config = (struct xpt_traverse_config *)arg;
2358 if (tr_config->depth == XPT_DEPTH_TARGET) {
2359 xpt_targetfunc_t *tr_func;
2361 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2363 return(tr_func(target, tr_config->tr_arg));
2365 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2369 xptdefdevicefunc(struct cam_ed *device, void *arg)
2371 struct xpt_traverse_config *tr_config;
2373 tr_config = (struct xpt_traverse_config *)arg;
2375 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2376 xpt_devicefunc_t *tr_func;
2378 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2380 return(tr_func(device, tr_config->tr_arg));
2382 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2386 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2388 struct xpt_traverse_config *tr_config;
2389 xpt_periphfunc_t *tr_func;
2391 tr_config = (struct xpt_traverse_config *)arg;
2393 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2396 * Unlike the other default functions, we don't check for depth
2397 * here. The peripheral driver level is the last level in the EDT,
2398 * so if we're here, we should execute the function in question.
2400 return(tr_func(periph, tr_config->tr_arg));
2404 * Execute the given function for every bus in the EDT.
2407 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2409 struct xpt_traverse_config tr_config;
2411 tr_config.depth = XPT_DEPTH_BUS;
2412 tr_config.tr_func = tr_func;
2413 tr_config.tr_arg = arg;
2415 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2419 * Execute the given function for every device in the EDT.
2422 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2424 struct xpt_traverse_config tr_config;
2426 tr_config.depth = XPT_DEPTH_DEVICE;
2427 tr_config.tr_func = tr_func;
2428 tr_config.tr_arg = arg;
2430 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2434 xptsetasyncfunc(struct cam_ed *device, void *arg)
2436 struct cam_path path;
2437 struct ccb_getdev cgd;
2438 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2441 * Don't report unconfigured devices (Wildcard devs,
2442 * devices only for target mode, device instances
2443 * that have been invalidated but are waiting for
2444 * their last reference count to be released).
2446 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2449 xpt_compile_path(&path,
2451 device->target->bus->path_id,
2452 device->target->target_id,
2454 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2455 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2456 xpt_action((union ccb *)&cgd);
2457 csa->callback(csa->callback_arg,
2460 xpt_release_path(&path);
2466 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2468 struct cam_path path;
2469 struct ccb_pathinq cpi;
2470 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2472 xpt_compile_path(&path, /*periph*/NULL,
2474 CAM_TARGET_WILDCARD,
2476 xpt_path_lock(&path);
2477 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2478 cpi.ccb_h.func_code = XPT_PATH_INQ;
2479 xpt_action((union ccb *)&cpi);
2480 csa->callback(csa->callback_arg,
2483 xpt_path_unlock(&path);
2484 xpt_release_path(&path);
2490 xpt_action(union ccb *start_ccb)
2493 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2494 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2495 xpt_action_name(start_ccb->ccb_h.func_code)));
2497 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2498 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2502 xpt_action_default(union ccb *start_ccb)
2504 struct cam_path *path;
2505 struct cam_sim *sim;
2508 path = start_ccb->ccb_h.path;
2509 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2510 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2511 xpt_action_name(start_ccb->ccb_h.func_code)));
2513 switch (start_ccb->ccb_h.func_code) {
2516 struct cam_ed *device;
2519 * For the sake of compatibility with SCSI-1
2520 * devices that may not understand the identify
2521 * message, we include lun information in the
2522 * second byte of all commands. SCSI-1 specifies
2523 * that luns are a 3 bit value and reserves only 3
2524 * bits for lun information in the CDB. Later
2525 * revisions of the SCSI spec allow for more than 8
2526 * luns, but have deprecated lun information in the
2527 * CDB. So, if the lun won't fit, we must omit.
2529 * Also be aware that during initial probing for devices,
2530 * the inquiry information is unknown but initialized to 0.
2531 * This means that this code will be exercised while probing
2532 * devices with an ANSI revision greater than 2.
2534 device = path->device;
2535 if (device->protocol_version <= SCSI_REV_2
2536 && start_ccb->ccb_h.target_lun < 8
2537 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2539 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2540 start_ccb->ccb_h.target_lun << 5;
2542 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2546 case XPT_CONT_TARGET_IO:
2547 start_ccb->csio.sense_resid = 0;
2548 start_ccb->csio.resid = 0;
2551 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2552 start_ccb->ataio.resid = 0;
2556 case XPT_NVME_ADMIN:
2562 struct cam_devq *devq;
2564 devq = path->bus->sim->devq;
2565 mtx_lock(&devq->send_mtx);
2566 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2567 if (xpt_schedule_devq(devq, path->device) != 0)
2569 mtx_unlock(&devq->send_mtx);
2572 case XPT_CALC_GEOMETRY:
2573 /* Filter out garbage */
2574 if (start_ccb->ccg.block_size == 0
2575 || start_ccb->ccg.volume_size == 0) {
2576 start_ccb->ccg.cylinders = 0;
2577 start_ccb->ccg.heads = 0;
2578 start_ccb->ccg.secs_per_track = 0;
2579 start_ccb->ccb_h.status = CAM_REQ_CMP;
2582 #if defined(PC98) || defined(__sparc64__)
2584 * In a PC-98 system, geometry translation depens on
2585 * the "real" device geometry obtained from mode page 4.
2586 * SCSI geometry translation is performed in the
2587 * initialization routine of the SCSI BIOS and the result
2588 * stored in host memory. If the translation is available
2589 * in host memory, use it. If not, rely on the default
2590 * translation the device driver performs.
2591 * For sparc64, we may need adjust the geometry of large
2592 * disks in order to fit the limitations of the 16-bit
2593 * fields of the VTOC8 disk label.
2595 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2596 start_ccb->ccb_h.status = CAM_REQ_CMP;
2603 union ccb* abort_ccb;
2605 abort_ccb = start_ccb->cab.abort_ccb;
2606 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2607 struct cam_ed *device;
2608 struct cam_devq *devq;
2610 device = abort_ccb->ccb_h.path->device;
2611 devq = device->sim->devq;
2613 mtx_lock(&devq->send_mtx);
2614 if (abort_ccb->ccb_h.pinfo.index > 0) {
2615 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2616 abort_ccb->ccb_h.status =
2617 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2618 xpt_freeze_devq_device(device, 1);
2619 mtx_unlock(&devq->send_mtx);
2620 xpt_done(abort_ccb);
2621 start_ccb->ccb_h.status = CAM_REQ_CMP;
2624 mtx_unlock(&devq->send_mtx);
2626 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2627 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2629 * We've caught this ccb en route to
2630 * the SIM. Flag it for abort and the
2631 * SIM will do so just before starting
2632 * real work on the CCB.
2634 abort_ccb->ccb_h.status =
2635 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2636 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2637 start_ccb->ccb_h.status = CAM_REQ_CMP;
2641 if (XPT_FC_IS_QUEUED(abort_ccb)
2642 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2644 * It's already completed but waiting
2645 * for our SWI to get to it.
2647 start_ccb->ccb_h.status = CAM_UA_ABORT;
2651 * If we weren't able to take care of the abort request
2652 * in the XPT, pass the request down to the SIM for processing.
2656 case XPT_ACCEPT_TARGET_IO:
2658 case XPT_IMMED_NOTIFY:
2659 case XPT_NOTIFY_ACK:
2661 case XPT_IMMEDIATE_NOTIFY:
2662 case XPT_NOTIFY_ACKNOWLEDGE:
2663 case XPT_GET_SIM_KNOB_OLD:
2664 case XPT_GET_SIM_KNOB:
2665 case XPT_SET_SIM_KNOB:
2666 case XPT_GET_TRAN_SETTINGS:
2667 case XPT_SET_TRAN_SETTINGS:
2670 sim = path->bus->sim;
2672 if (mtx && !mtx_owned(mtx))
2676 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2677 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2678 (*(sim->sim_action))(sim, start_ccb);
2679 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2680 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2684 case XPT_PATH_STATS:
2685 start_ccb->cpis.last_reset = path->bus->last_reset;
2686 start_ccb->ccb_h.status = CAM_REQ_CMP;
2693 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2694 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2696 struct ccb_getdev *cgd;
2698 cgd = &start_ccb->cgd;
2699 cgd->protocol = dev->protocol;
2700 cgd->inq_data = dev->inq_data;
2701 cgd->ident_data = dev->ident_data;
2702 cgd->inq_flags = dev->inq_flags;
2703 cgd->ccb_h.status = CAM_REQ_CMP;
2704 cgd->serial_num_len = dev->serial_num_len;
2705 if ((dev->serial_num_len > 0)
2706 && (dev->serial_num != NULL))
2707 bcopy(dev->serial_num, cgd->serial_num,
2708 dev->serial_num_len);
2712 case XPT_GDEV_STATS:
2714 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2715 struct cam_ed *dev = path->device;
2716 struct cam_eb *bus = path->bus;
2717 struct cam_et *tar = path->target;
2718 struct cam_devq *devq = bus->sim->devq;
2720 mtx_lock(&devq->send_mtx);
2721 cgds->dev_openings = dev->ccbq.dev_openings;
2722 cgds->dev_active = dev->ccbq.dev_active;
2723 cgds->allocated = dev->ccbq.allocated;
2724 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2725 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2726 cgds->last_reset = tar->last_reset;
2727 cgds->maxtags = dev->maxtags;
2728 cgds->mintags = dev->mintags;
2729 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2730 cgds->last_reset = bus->last_reset;
2731 mtx_unlock(&devq->send_mtx);
2732 cgds->ccb_h.status = CAM_REQ_CMP;
2737 struct cam_periph *nperiph;
2738 struct periph_list *periph_head;
2739 struct ccb_getdevlist *cgdl;
2741 struct cam_ed *device;
2748 * Don't want anyone mucking with our data.
2750 device = path->device;
2751 periph_head = &device->periphs;
2752 cgdl = &start_ccb->cgdl;
2755 * Check and see if the list has changed since the user
2756 * last requested a list member. If so, tell them that the
2757 * list has changed, and therefore they need to start over
2758 * from the beginning.
2760 if ((cgdl->index != 0) &&
2761 (cgdl->generation != device->generation)) {
2762 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2767 * Traverse the list of peripherals and attempt to find
2768 * the requested peripheral.
2770 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2771 (nperiph != NULL) && (i <= cgdl->index);
2772 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2773 if (i == cgdl->index) {
2774 strlcpy(cgdl->periph_name,
2775 nperiph->periph_name,
2776 sizeof(cgdl->periph_name));
2777 cgdl->unit_number = nperiph->unit_number;
2782 cgdl->status = CAM_GDEVLIST_ERROR;
2786 if (nperiph == NULL)
2787 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2789 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2792 cgdl->generation = device->generation;
2794 cgdl->ccb_h.status = CAM_REQ_CMP;
2799 dev_pos_type position_type;
2800 struct ccb_dev_match *cdm;
2802 cdm = &start_ccb->cdm;
2805 * There are two ways of getting at information in the EDT.
2806 * The first way is via the primary EDT tree. It starts
2807 * with a list of busses, then a list of targets on a bus,
2808 * then devices/luns on a target, and then peripherals on a
2809 * device/lun. The "other" way is by the peripheral driver
2810 * lists. The peripheral driver lists are organized by
2811 * peripheral driver. (obviously) So it makes sense to
2812 * use the peripheral driver list if the user is looking
2813 * for something like "da1", or all "da" devices. If the
2814 * user is looking for something on a particular bus/target
2815 * or lun, it's generally better to go through the EDT tree.
2818 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2819 position_type = cdm->pos.position_type;
2823 position_type = CAM_DEV_POS_NONE;
2825 for (i = 0; i < cdm->num_patterns; i++) {
2826 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2827 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2828 position_type = CAM_DEV_POS_EDT;
2833 if (cdm->num_patterns == 0)
2834 position_type = CAM_DEV_POS_EDT;
2835 else if (position_type == CAM_DEV_POS_NONE)
2836 position_type = CAM_DEV_POS_PDRV;
2839 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2840 case CAM_DEV_POS_EDT:
2843 case CAM_DEV_POS_PDRV:
2844 xptperiphlistmatch(cdm);
2847 cdm->status = CAM_DEV_MATCH_ERROR;
2851 if (cdm->status == CAM_DEV_MATCH_ERROR)
2852 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2854 start_ccb->ccb_h.status = CAM_REQ_CMP;
2860 struct ccb_setasync *csa;
2861 struct async_node *cur_entry;
2862 struct async_list *async_head;
2865 csa = &start_ccb->csa;
2866 added = csa->event_enable;
2867 async_head = &path->device->asyncs;
2870 * If there is already an entry for us, simply
2873 cur_entry = SLIST_FIRST(async_head);
2874 while (cur_entry != NULL) {
2875 if ((cur_entry->callback_arg == csa->callback_arg)
2876 && (cur_entry->callback == csa->callback))
2878 cur_entry = SLIST_NEXT(cur_entry, links);
2881 if (cur_entry != NULL) {
2883 * If the request has no flags set,
2886 added &= ~cur_entry->event_enable;
2887 if (csa->event_enable == 0) {
2888 SLIST_REMOVE(async_head, cur_entry,
2890 xpt_release_device(path->device);
2891 free(cur_entry, M_CAMXPT);
2893 cur_entry->event_enable = csa->event_enable;
2895 csa->event_enable = added;
2897 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2899 if (cur_entry == NULL) {
2900 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2903 cur_entry->event_enable = csa->event_enable;
2904 cur_entry->event_lock = (path->bus->sim->mtx &&
2905 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
2906 cur_entry->callback_arg = csa->callback_arg;
2907 cur_entry->callback = csa->callback;
2908 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2909 xpt_acquire_device(path->device);
2911 start_ccb->ccb_h.status = CAM_REQ_CMP;
2916 struct ccb_relsim *crs;
2919 crs = &start_ccb->crs;
2923 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2927 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2929 /* Don't ever go below one opening */
2930 if (crs->openings > 0) {
2931 xpt_dev_ccbq_resize(path, crs->openings);
2934 "number of openings is now %d\n",
2940 mtx_lock(&dev->sim->devq->send_mtx);
2941 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2943 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2946 * Just extend the old timeout and decrement
2947 * the freeze count so that a single timeout
2948 * is sufficient for releasing the queue.
2950 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2951 callout_stop(&dev->callout);
2954 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2957 callout_reset_sbt(&dev->callout,
2958 SBT_1MS * crs->release_timeout, 0,
2959 xpt_release_devq_timeout, dev, 0);
2961 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2965 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2967 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2969 * Decrement the freeze count so that a single
2970 * completion is still sufficient to unfreeze
2973 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2976 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2977 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2981 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2983 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2984 || (dev->ccbq.dev_active == 0)) {
2986 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2989 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2990 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2993 mtx_unlock(&dev->sim->devq->send_mtx);
2995 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2996 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2997 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2998 start_ccb->ccb_h.status = CAM_REQ_CMP;
3002 struct cam_path *oldpath;
3004 /* Check that all request bits are supported. */
3005 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3006 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3010 cam_dflags = CAM_DEBUG_NONE;
3011 if (cam_dpath != NULL) {
3012 oldpath = cam_dpath;
3014 xpt_free_path(oldpath);
3016 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3017 if (xpt_create_path(&cam_dpath, NULL,
3018 start_ccb->ccb_h.path_id,
3019 start_ccb->ccb_h.target_id,
3020 start_ccb->ccb_h.target_lun) !=
3022 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3024 cam_dflags = start_ccb->cdbg.flags;
3025 start_ccb->ccb_h.status = CAM_REQ_CMP;
3026 xpt_print(cam_dpath, "debugging flags now %x\n",
3030 start_ccb->ccb_h.status = CAM_REQ_CMP;
3034 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3035 xpt_freeze_devq(path, 1);
3036 start_ccb->ccb_h.status = CAM_REQ_CMP;
3038 case XPT_REPROBE_LUN:
3039 xpt_async(AC_INQ_CHANGED, path, NULL);
3040 start_ccb->ccb_h.status = CAM_REQ_CMP;
3041 xpt_done(start_ccb);
3048 xpt_print_path(start_ccb->ccb_h.path);
3049 printf("%s: CCB type %#x %s not supported\n", __func__,
3050 start_ccb->ccb_h.func_code,
3051 xpt_action_name(start_ccb->ccb_h.func_code));
3052 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3053 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3054 xpt_done(start_ccb);
3058 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3059 ("xpt_action_default: func= %#x %s status %#x\n",
3060 start_ccb->ccb_h.func_code,
3061 xpt_action_name(start_ccb->ccb_h.func_code),
3062 start_ccb->ccb_h.status));
3066 xpt_polled_action(union ccb *start_ccb)
3069 struct cam_sim *sim;
3070 struct cam_devq *devq;
3074 timeout = start_ccb->ccb_h.timeout * 10;
3075 sim = start_ccb->ccb_h.path->bus->sim;
3078 dev = start_ccb->ccb_h.path->device;
3080 mtx_unlock(&dev->device_mtx);
3083 * Steal an opening so that no other queued requests
3084 * can get it before us while we simulate interrupts.
3086 mtx_lock(&devq->send_mtx);
3087 dev->ccbq.dev_openings--;
3088 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3090 mtx_unlock(&devq->send_mtx);
3094 (*(sim->sim_poll))(sim);
3098 mtx_lock(&devq->send_mtx);
3100 dev->ccbq.dev_openings++;
3101 mtx_unlock(&devq->send_mtx);
3104 xpt_action(start_ccb);
3105 while(--timeout > 0) {
3108 (*(sim->sim_poll))(sim);
3112 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3119 * XXX Is it worth adding a sim_timeout entry
3120 * point so we can attempt recovery? If
3121 * this is only used for dumps, I don't think
3124 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3127 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3130 mtx_lock(&dev->device_mtx);
3134 * Schedule a peripheral driver to receive a ccb when its
3135 * target device has space for more transactions.
3138 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3141 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3142 cam_periph_assert(periph, MA_OWNED);
3143 if (new_priority < periph->scheduled_priority) {
3144 periph->scheduled_priority = new_priority;
3145 xpt_run_allocq(periph, 0);
3151 * Schedule a device to run on a given queue.
3152 * If the device was inserted as a new entry on the queue,
3153 * return 1 meaning the device queue should be run. If we
3154 * were already queued, implying someone else has already
3155 * started the queue, return 0 so the caller doesn't attempt
3159 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3160 u_int32_t new_priority)
3163 u_int32_t old_priority;
3165 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3167 old_priority = pinfo->priority;
3170 * Are we already queued?
3172 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3173 /* Simply reorder based on new priority */
3174 if (new_priority < old_priority) {
3175 camq_change_priority(queue, pinfo->index,
3177 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3178 ("changed priority to %d\n",
3184 /* New entry on the queue */
3185 if (new_priority < old_priority)
3186 pinfo->priority = new_priority;
3188 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3189 ("Inserting onto queue\n"));
3190 pinfo->generation = ++queue->generation;
3191 camq_insert(queue, pinfo);
3198 xpt_run_allocq_task(void *context, int pending)
3200 struct cam_periph *periph = context;
3202 cam_periph_lock(periph);
3203 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3204 xpt_run_allocq(periph, 1);
3205 cam_periph_unlock(periph);
3206 cam_periph_release(periph);
3210 xpt_run_allocq(struct cam_periph *periph, int sleep)
3212 struct cam_ed *device;
3216 cam_periph_assert(periph, MA_OWNED);
3217 if (periph->periph_allocating)
3219 periph->periph_allocating = 1;
3220 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3221 device = periph->path->device;
3224 while ((prio = min(periph->scheduled_priority,
3225 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3226 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3227 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3230 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3232 ccb = xpt_get_ccb(periph);
3235 if (periph->flags & CAM_PERIPH_RUN_TASK)
3237 cam_periph_doacquire(periph);
3238 periph->flags |= CAM_PERIPH_RUN_TASK;
3239 taskqueue_enqueue(xsoftc.xpt_taskq,
3240 &periph->periph_run_task);
3243 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3244 if (prio == periph->immediate_priority) {
3245 periph->immediate_priority = CAM_PRIORITY_NONE;
3246 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3247 ("waking cam_periph_getccb()\n"));
3248 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3250 wakeup(&periph->ccb_list);
3252 periph->scheduled_priority = CAM_PRIORITY_NONE;
3253 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3254 ("calling periph_start()\n"));
3255 periph->periph_start(periph, ccb);
3260 xpt_release_ccb(ccb);
3261 periph->periph_allocating = 0;
3265 xpt_run_devq(struct cam_devq *devq)
3269 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3271 devq->send_queue.qfrozen_cnt++;
3272 while ((devq->send_queue.entries > 0)
3273 && (devq->send_openings > 0)
3274 && (devq->send_queue.qfrozen_cnt <= 1)) {
3275 struct cam_ed *device;
3276 union ccb *work_ccb;
3277 struct cam_sim *sim;
3278 struct xpt_proto *proto;
3280 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3282 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3283 ("running device %p\n", device));
3285 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3286 if (work_ccb == NULL) {
3287 printf("device on run queue with no ccbs???\n");
3291 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3293 mtx_lock(&xsoftc.xpt_highpower_lock);
3294 if (xsoftc.num_highpower <= 0) {
3296 * We got a high power command, but we
3297 * don't have any available slots. Freeze
3298 * the device queue until we have a slot
3301 xpt_freeze_devq_device(device, 1);
3302 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3305 mtx_unlock(&xsoftc.xpt_highpower_lock);
3309 * Consume a high power slot while
3312 xsoftc.num_highpower--;
3314 mtx_unlock(&xsoftc.xpt_highpower_lock);
3316 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3317 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3318 devq->send_openings--;
3319 devq->send_active++;
3320 xpt_schedule_devq(devq, device);
3321 mtx_unlock(&devq->send_mtx);
3323 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3325 * The client wants to freeze the queue
3326 * after this CCB is sent.
3328 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3331 /* In Target mode, the peripheral driver knows best... */
3332 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3333 if ((device->inq_flags & SID_CmdQue) != 0
3334 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3335 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3338 * Clear this in case of a retried CCB that
3339 * failed due to a rejected tag.
3341 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3344 KASSERT(device == work_ccb->ccb_h.path->device,
3345 ("device (%p) / path->device (%p) mismatch",
3346 device, work_ccb->ccb_h.path->device));
3347 proto = xpt_proto_find(device->protocol);
3348 if (proto && proto->ops->debug_out)
3349 proto->ops->debug_out(work_ccb);
3352 * Device queues can be shared among multiple SIM instances
3353 * that reside on different busses. Use the SIM from the
3354 * queued device, rather than the one from the calling bus.
3358 if (mtx && !mtx_owned(mtx))
3362 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3363 (*(sim->sim_action))(sim, work_ccb);
3366 mtx_lock(&devq->send_mtx);
3368 devq->send_queue.qfrozen_cnt--;
3372 * This function merges stuff from the slave ccb into the master ccb, while
3373 * keeping important fields in the master ccb constant.
3376 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3380 * Pull fields that are valid for peripheral drivers to set
3381 * into the master CCB along with the CCB "payload".
3383 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3384 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3385 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3386 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3387 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3388 sizeof(union ccb) - sizeof(struct ccb_hdr));
3392 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3393 u_int32_t priority, u_int32_t flags)
3396 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3397 ccb_h->pinfo.priority = priority;
3399 ccb_h->path_id = path->bus->path_id;
3401 ccb_h->target_id = path->target->target_id;
3403 ccb_h->target_id = CAM_TARGET_WILDCARD;
3405 ccb_h->target_lun = path->device->lun_id;
3406 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3408 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3410 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3411 ccb_h->flags = flags;
3416 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3418 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3421 /* Path manipulation functions */
3423 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3424 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3426 struct cam_path *path;
3429 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3432 status = CAM_RESRC_UNAVAIL;
3435 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3436 if (status != CAM_REQ_CMP) {
3437 free(path, M_CAMPATH);
3440 *new_path_ptr = path;
3445 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3446 struct cam_periph *periph, path_id_t path_id,
3447 target_id_t target_id, lun_id_t lun_id)
3450 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3455 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3456 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3459 struct cam_et *target;
3460 struct cam_ed *device;
3463 status = CAM_REQ_CMP; /* Completed without error */
3464 target = NULL; /* Wildcarded */
3465 device = NULL; /* Wildcarded */
3468 * We will potentially modify the EDT, so block interrupts
3469 * that may attempt to create cam paths.
3471 bus = xpt_find_bus(path_id);
3473 status = CAM_PATH_INVALID;
3476 mtx_lock(&bus->eb_mtx);
3477 target = xpt_find_target(bus, target_id);
3478 if (target == NULL) {
3480 struct cam_et *new_target;
3482 new_target = xpt_alloc_target(bus, target_id);
3483 if (new_target == NULL) {
3484 status = CAM_RESRC_UNAVAIL;
3486 target = new_target;
3490 if (target != NULL) {
3491 device = xpt_find_device(target, lun_id);
3492 if (device == NULL) {
3494 struct cam_ed *new_device;
3497 (*(bus->xport->ops->alloc_device))(bus,
3500 if (new_device == NULL) {
3501 status = CAM_RESRC_UNAVAIL;
3503 device = new_device;
3507 mtx_unlock(&bus->eb_mtx);
3511 * Only touch the user's data if we are successful.
3513 if (status == CAM_REQ_CMP) {
3514 new_path->periph = perph;
3515 new_path->bus = bus;
3516 new_path->target = target;
3517 new_path->device = device;
3518 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3521 xpt_release_device(device);
3523 xpt_release_target(target);
3525 xpt_release_bus(bus);
3531 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3533 struct cam_path *new_path;
3535 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3536 if (new_path == NULL)
3537 return(CAM_RESRC_UNAVAIL);
3538 xpt_copy_path(new_path, path);
3539 *new_path_ptr = new_path;
3540 return (CAM_REQ_CMP);
3544 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3548 if (path->bus != NULL)
3549 xpt_acquire_bus(path->bus);
3550 if (path->target != NULL)
3551 xpt_acquire_target(path->target);
3552 if (path->device != NULL)
3553 xpt_acquire_device(path->device);
3557 xpt_release_path(struct cam_path *path)
3559 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3560 if (path->device != NULL) {
3561 xpt_release_device(path->device);
3562 path->device = NULL;
3564 if (path->target != NULL) {
3565 xpt_release_target(path->target);
3566 path->target = NULL;
3568 if (path->bus != NULL) {
3569 xpt_release_bus(path->bus);
3575 xpt_free_path(struct cam_path *path)
3578 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3579 xpt_release_path(path);
3580 free(path, M_CAMPATH);
3584 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3585 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3591 *bus_ref = path->bus->refcount;
3597 *periph_ref = path->periph->refcount;
3604 *target_ref = path->target->refcount;
3610 *device_ref = path->device->refcount;
3617 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3618 * in path1, 2 for match with wildcards in path2.
3621 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3625 if (path1->bus != path2->bus) {
3626 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3628 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3633 if (path1->target != path2->target) {
3634 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3637 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3642 if (path1->device != path2->device) {
3643 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3646 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3655 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3659 if (path->bus != dev->target->bus) {
3660 if (path->bus->path_id == CAM_BUS_WILDCARD)
3662 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3667 if (path->target != dev->target) {
3668 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3671 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3676 if (path->device != dev) {
3677 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3680 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3689 xpt_print_path(struct cam_path *path)
3693 printf("(nopath): ");
3695 if (path->periph != NULL)
3696 printf("(%s%d:", path->periph->periph_name,
3697 path->periph->unit_number);
3699 printf("(noperiph:");
3701 if (path->bus != NULL)
3702 printf("%s%d:%d:", path->bus->sim->sim_name,
3703 path->bus->sim->unit_number,
3704 path->bus->sim->bus_id);
3708 if (path->target != NULL)
3709 printf("%d:", path->target->target_id);
3713 if (path->device != NULL)
3714 printf("%jx): ", (uintmax_t)path->device->lun_id);
3721 xpt_print_device(struct cam_ed *device)
3725 printf("(nopath): ");
3727 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3728 device->sim->unit_number,
3729 device->sim->bus_id,
3730 device->target->target_id,
3731 (uintmax_t)device->lun_id);
3736 xpt_print(struct cam_path *path, const char *fmt, ...)
3739 xpt_print_path(path);
3746 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3750 sbuf_new(&sb, str, str_len, 0);
3753 sbuf_printf(&sb, "(nopath): ");
3755 if (path->periph != NULL)
3756 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3757 path->periph->unit_number);
3759 sbuf_printf(&sb, "(noperiph:");
3761 if (path->bus != NULL)
3762 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3763 path->bus->sim->unit_number,
3764 path->bus->sim->bus_id);
3766 sbuf_printf(&sb, "nobus:");
3768 if (path->target != NULL)
3769 sbuf_printf(&sb, "%d:", path->target->target_id);
3771 sbuf_printf(&sb, "X:");
3773 if (path->device != NULL)
3774 sbuf_printf(&sb, "%jx): ",
3775 (uintmax_t)path->device->lun_id);
3777 sbuf_printf(&sb, "X): ");
3781 return(sbuf_len(&sb));
3785 xpt_path_path_id(struct cam_path *path)
3787 return(path->bus->path_id);
3791 xpt_path_target_id(struct cam_path *path)
3793 if (path->target != NULL)
3794 return (path->target->target_id);
3796 return (CAM_TARGET_WILDCARD);
3800 xpt_path_lun_id(struct cam_path *path)
3802 if (path->device != NULL)
3803 return (path->device->lun_id);
3805 return (CAM_LUN_WILDCARD);
3809 xpt_path_sim(struct cam_path *path)
3812 return (path->bus->sim);
3816 xpt_path_periph(struct cam_path *path)
3819 return (path->periph);
3823 * Release a CAM control block for the caller. Remit the cost of the structure
3824 * to the device referenced by the path. If the this device had no 'credits'
3825 * and peripheral drivers have registered async callbacks for this notification
3829 xpt_release_ccb(union ccb *free_ccb)
3831 struct cam_ed *device;
3832 struct cam_periph *periph;
3834 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3835 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3836 device = free_ccb->ccb_h.path->device;
3837 periph = free_ccb->ccb_h.path->periph;
3839 xpt_free_ccb(free_ccb);
3840 periph->periph_allocated--;
3841 cam_ccbq_release_opening(&device->ccbq);
3842 xpt_run_allocq(periph, 0);
3845 /* Functions accessed by SIM drivers */
3847 static struct xpt_xport_ops xport_default_ops = {
3848 .alloc_device = xpt_alloc_device_default,
3849 .action = xpt_action_default,
3850 .async = xpt_dev_async_default,
3852 static struct xpt_xport xport_default = {
3853 .xport = XPORT_UNKNOWN,
3855 .ops = &xport_default_ops,
3858 CAM_XPT_XPORT(xport_default);
3861 * A sim structure, listing the SIM entry points and instance
3862 * identification info is passed to xpt_bus_register to hook the SIM
3863 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3864 * for this new bus and places it in the array of busses and assigns
3865 * it a path_id. The path_id may be influenced by "hard wiring"
3866 * information specified by the user. Once interrupt services are
3867 * available, the bus will be probed.
3870 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3872 struct cam_eb *new_bus;
3873 struct cam_eb *old_bus;
3874 struct ccb_pathinq cpi;
3875 struct cam_path *path;
3879 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3880 M_CAMXPT, M_NOWAIT|M_ZERO);
3881 if (new_bus == NULL) {
3882 /* Couldn't satisfy request */
3883 return (CAM_RESRC_UNAVAIL);
3886 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3887 TAILQ_INIT(&new_bus->et_entries);
3890 timevalclear(&new_bus->last_reset);
3892 new_bus->refcount = 1; /* Held until a bus_deregister event */
3893 new_bus->generation = 0;
3896 sim->path_id = new_bus->path_id =
3897 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3898 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3899 while (old_bus != NULL
3900 && old_bus->path_id < new_bus->path_id)
3901 old_bus = TAILQ_NEXT(old_bus, links);
3902 if (old_bus != NULL)
3903 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3905 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3906 xsoftc.bus_generation++;
3910 * Set a default transport so that a PATH_INQ can be issued to
3911 * the SIM. This will then allow for probing and attaching of
3912 * a more appropriate transport.
3914 new_bus->xport = &xport_default;
3916 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3917 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3918 if (status != CAM_REQ_CMP) {
3919 xpt_release_bus(new_bus);
3920 return (CAM_RESRC_UNAVAIL);
3923 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3924 cpi.ccb_h.func_code = XPT_PATH_INQ;
3925 xpt_action((union ccb *)&cpi);
3927 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3928 struct xpt_xport **xpt;
3930 SET_FOREACH(xpt, cam_xpt_xport_set) {
3931 if ((*xpt)->xport == cpi.transport) {
3932 new_bus->xport = *xpt;
3936 if (new_bus->xport == NULL) {
3937 xpt_print_path(path);
3938 printf("No transport found for %d\n", cpi.transport);
3939 xpt_release_bus(new_bus);
3940 free(path, M_CAMXPT);
3941 return (CAM_RESRC_UNAVAIL);
3945 /* Notify interested parties */
3946 if (sim->path_id != CAM_XPT_PATH_ID) {
3948 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3949 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3950 union ccb *scan_ccb;
3952 /* Initiate bus rescan. */
3953 scan_ccb = xpt_alloc_ccb_nowait();
3954 if (scan_ccb != NULL) {
3955 scan_ccb->ccb_h.path = path;
3956 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3957 scan_ccb->crcn.flags = 0;
3958 xpt_rescan(scan_ccb);
3961 "Can't allocate CCB to scan bus\n");
3962 xpt_free_path(path);
3965 xpt_free_path(path);
3967 xpt_free_path(path);
3968 return (CAM_SUCCESS);
3972 xpt_bus_deregister(path_id_t pathid)
3974 struct cam_path bus_path;
3977 status = xpt_compile_path(&bus_path, NULL, pathid,
3978 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3979 if (status != CAM_REQ_CMP)
3982 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3983 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3985 /* Release the reference count held while registered. */
3986 xpt_release_bus(bus_path.bus);
3987 xpt_release_path(&bus_path);
3989 return (CAM_REQ_CMP);
3993 xptnextfreepathid(void)
3999 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4001 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4003 /* Find an unoccupied pathid */
4004 while (bus != NULL && bus->path_id <= pathid) {
4005 if (bus->path_id == pathid)
4007 bus = TAILQ_NEXT(bus, links);
4011 * Ensure that this pathid is not reserved for
4012 * a bus that may be registered in the future.
4014 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4016 /* Start the search over */
4023 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4030 pathid = CAM_XPT_PATH_ID;
4031 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4032 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4035 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4036 if (strcmp(dname, "scbus")) {
4037 /* Avoid a bit of foot shooting. */
4040 if (dunit < 0) /* unwired?! */
4042 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4043 if (sim_bus == val) {
4047 } else if (sim_bus == 0) {
4048 /* Unspecified matches bus 0 */
4052 printf("Ambiguous scbus configuration for %s%d "
4053 "bus %d, cannot wire down. The kernel "
4054 "config entry for scbus%d should "
4055 "specify a controller bus.\n"
4056 "Scbus will be assigned dynamically.\n",
4057 sim_name, sim_unit, sim_bus, dunit);
4062 if (pathid == CAM_XPT_PATH_ID)
4063 pathid = xptnextfreepathid();
4068 xpt_async_string(u_int32_t async_code)
4071 switch (async_code) {
4072 case AC_BUS_RESET: return ("AC_BUS_RESET");
4073 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4074 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4075 case AC_SENT_BDR: return ("AC_SENT_BDR");
4076 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4077 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4078 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4079 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4080 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4081 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4082 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4083 case AC_CONTRACT: return ("AC_CONTRACT");
4084 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4085 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4087 return ("AC_UNKNOWN");
4091 xpt_async_size(u_int32_t async_code)
4094 switch (async_code) {
4095 case AC_BUS_RESET: return (0);
4096 case AC_UNSOL_RESEL: return (0);
4097 case AC_SCSI_AEN: return (0);
4098 case AC_SENT_BDR: return (0);
4099 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4100 case AC_PATH_DEREGISTERED: return (0);
4101 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4102 case AC_LOST_DEVICE: return (0);
4103 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4104 case AC_INQ_CHANGED: return (0);
4105 case AC_GETDEV_CHANGED: return (0);
4106 case AC_CONTRACT: return (sizeof(struct ac_contract));
4107 case AC_ADVINFO_CHANGED: return (-1);
4108 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4114 xpt_async_process_dev(struct cam_ed *device, void *arg)
4116 union ccb *ccb = arg;
4117 struct cam_path *path = ccb->ccb_h.path;
4118 void *async_arg = ccb->casync.async_arg_ptr;
4119 u_int32_t async_code = ccb->casync.async_code;
4122 if (path->device != device
4123 && path->device->lun_id != CAM_LUN_WILDCARD
4124 && device->lun_id != CAM_LUN_WILDCARD)
4128 * The async callback could free the device.
4129 * If it is a broadcast async, it doesn't hold
4130 * device reference, so take our own reference.
4132 xpt_acquire_device(device);
4135 * If async for specific device is to be delivered to
4136 * the wildcard client, take the specific device lock.
4137 * XXX: We may need a way for client to specify it.
4139 if ((device->lun_id == CAM_LUN_WILDCARD &&
4140 path->device->lun_id != CAM_LUN_WILDCARD) ||
4141 (device->target->target_id == CAM_TARGET_WILDCARD &&
4142 path->target->target_id != CAM_TARGET_WILDCARD) ||
4143 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4144 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4145 mtx_unlock(&device->device_mtx);
4146 xpt_path_lock(path);
4151 (*(device->target->bus->xport->ops->async))(async_code,
4152 device->target->bus, device->target, device, async_arg);
4153 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4156 xpt_path_unlock(path);
4157 mtx_lock(&device->device_mtx);
4159 xpt_release_device(device);
4164 xpt_async_process_tgt(struct cam_et *target, void *arg)
4166 union ccb *ccb = arg;
4167 struct cam_path *path = ccb->ccb_h.path;
4169 if (path->target != target
4170 && path->target->target_id != CAM_TARGET_WILDCARD
4171 && target->target_id != CAM_TARGET_WILDCARD)
4174 if (ccb->casync.async_code == AC_SENT_BDR) {
4175 /* Update our notion of when the last reset occurred */
4176 microtime(&target->last_reset);
4179 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4183 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4186 struct cam_path *path;
4188 u_int32_t async_code;
4190 path = ccb->ccb_h.path;
4191 async_code = ccb->casync.async_code;
4192 async_arg = ccb->casync.async_arg_ptr;
4193 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4194 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4197 if (async_code == AC_BUS_RESET) {
4198 /* Update our notion of when the last reset occurred */
4199 microtime(&bus->last_reset);
4202 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4205 * If this wasn't a fully wildcarded async, tell all
4206 * clients that want all async events.
4208 if (bus != xpt_periph->path->bus) {
4209 xpt_path_lock(xpt_periph->path);
4210 xpt_async_process_dev(xpt_periph->path->device, ccb);
4211 xpt_path_unlock(xpt_periph->path);
4214 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4215 xpt_release_devq(path, 1, TRUE);
4217 xpt_release_simq(path->bus->sim, TRUE);
4218 if (ccb->casync.async_arg_size > 0)
4219 free(async_arg, M_CAMXPT);
4220 xpt_free_path(path);
4225 xpt_async_bcast(struct async_list *async_head,
4226 u_int32_t async_code,
4227 struct cam_path *path, void *async_arg)
4229 struct async_node *cur_entry;
4232 cur_entry = SLIST_FIRST(async_head);
4233 while (cur_entry != NULL) {
4234 struct async_node *next_entry;
4236 * Grab the next list entry before we call the current
4237 * entry's callback. This is because the callback function
4238 * can delete its async callback entry.
4240 next_entry = SLIST_NEXT(cur_entry, links);
4241 if ((cur_entry->event_enable & async_code) != 0) {
4242 mtx = cur_entry->event_lock ?
4243 path->device->sim->mtx : NULL;
4246 cur_entry->callback(cur_entry->callback_arg,
4252 cur_entry = next_entry;
4257 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4262 ccb = xpt_alloc_ccb_nowait();
4264 xpt_print(path, "Can't allocate CCB to send %s\n",
4265 xpt_async_string(async_code));
4269 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4270 xpt_print(path, "Can't allocate path to send %s\n",
4271 xpt_async_string(async_code));
4275 ccb->ccb_h.path->periph = NULL;
4276 ccb->ccb_h.func_code = XPT_ASYNC;
4277 ccb->ccb_h.cbfcnp = xpt_async_process;
4278 ccb->ccb_h.flags |= CAM_UNLOCKED;
4279 ccb->casync.async_code = async_code;
4280 ccb->casync.async_arg_size = 0;
4281 size = xpt_async_size(async_code);
4282 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4283 ("xpt_async: func %#x %s aync_code %d %s\n",
4284 ccb->ccb_h.func_code,
4285 xpt_action_name(ccb->ccb_h.func_code),
4287 xpt_async_string(async_code)));
4288 if (size > 0 && async_arg != NULL) {
4289 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4290 if (ccb->casync.async_arg_ptr == NULL) {
4291 xpt_print(path, "Can't allocate argument to send %s\n",
4292 xpt_async_string(async_code));
4293 xpt_free_path(ccb->ccb_h.path);
4297 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4298 ccb->casync.async_arg_size = size;
4299 } else if (size < 0) {
4300 ccb->casync.async_arg_ptr = async_arg;
4301 ccb->casync.async_arg_size = size;
4303 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4304 xpt_freeze_devq(path, 1);
4306 xpt_freeze_simq(path->bus->sim, 1);
4311 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4312 struct cam_et *target, struct cam_ed *device,
4317 * We only need to handle events for real devices.
4319 if (target->target_id == CAM_TARGET_WILDCARD
4320 || device->lun_id == CAM_LUN_WILDCARD)
4323 printf("%s called\n", __func__);
4327 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4329 struct cam_devq *devq;
4332 devq = dev->sim->devq;
4333 mtx_assert(&devq->send_mtx, MA_OWNED);
4334 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4335 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4336 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4337 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4338 /* Remove frozen device from sendq. */
4339 if (device_is_queued(dev))
4340 camq_remove(&devq->send_queue, dev->devq_entry.index);
4345 xpt_freeze_devq(struct cam_path *path, u_int count)
4347 struct cam_ed *dev = path->device;
4348 struct cam_devq *devq;
4351 devq = dev->sim->devq;
4352 mtx_lock(&devq->send_mtx);
4353 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4354 freeze = xpt_freeze_devq_device(dev, count);
4355 mtx_unlock(&devq->send_mtx);
4360 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4362 struct cam_devq *devq;
4366 mtx_lock(&devq->send_mtx);
4367 freeze = (devq->send_queue.qfrozen_cnt += count);
4368 mtx_unlock(&devq->send_mtx);
4373 xpt_release_devq_timeout(void *arg)
4376 struct cam_devq *devq;
4378 dev = (struct cam_ed *)arg;
4379 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4380 devq = dev->sim->devq;
4381 mtx_assert(&devq->send_mtx, MA_OWNED);
4382 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4387 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4390 struct cam_devq *devq;
4392 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4395 devq = dev->sim->devq;
4396 mtx_lock(&devq->send_mtx);
4397 if (xpt_release_devq_device(dev, count, run_queue))
4398 xpt_run_devq(dev->sim->devq);
4399 mtx_unlock(&devq->send_mtx);
4403 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4406 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4407 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4408 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4409 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4410 if (count > dev->ccbq.queue.qfrozen_cnt) {
4412 printf("xpt_release_devq(): requested %u > present %u\n",
4413 count, dev->ccbq.queue.qfrozen_cnt);
4415 count = dev->ccbq.queue.qfrozen_cnt;
4417 dev->ccbq.queue.qfrozen_cnt -= count;
4418 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4420 * No longer need to wait for a successful
4421 * command completion.
4423 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4425 * Remove any timeouts that might be scheduled
4426 * to release this queue.
4428 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4429 callout_stop(&dev->callout);
4430 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4433 * Now that we are unfrozen schedule the
4434 * device so any pending transactions are
4437 xpt_schedule_devq(dev->sim->devq, dev);
4444 xpt_release_simq(struct cam_sim *sim, int run_queue)
4446 struct cam_devq *devq;
4449 mtx_lock(&devq->send_mtx);
4450 if (devq->send_queue.qfrozen_cnt <= 0) {
4452 printf("xpt_release_simq: requested 1 > present %u\n",
4453 devq->send_queue.qfrozen_cnt);
4456 devq->send_queue.qfrozen_cnt--;
4457 if (devq->send_queue.qfrozen_cnt == 0) {
4459 * If there is a timeout scheduled to release this
4460 * sim queue, remove it. The queue frozen count is
4463 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4464 callout_stop(&sim->callout);
4465 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4469 * Now that we are unfrozen run the send queue.
4471 xpt_run_devq(sim->devq);
4474 mtx_unlock(&devq->send_mtx);
4478 * XXX Appears to be unused.
4481 xpt_release_simq_timeout(void *arg)
4483 struct cam_sim *sim;
4485 sim = (struct cam_sim *)arg;
4486 xpt_release_simq(sim, /* run_queue */ TRUE);
4490 xpt_done(union ccb *done_ccb)
4492 struct cam_doneq *queue;
4495 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4496 ("xpt_done: func= %#x %s status %#x\n",
4497 done_ccb->ccb_h.func_code,
4498 xpt_action_name(done_ccb->ccb_h.func_code),
4499 done_ccb->ccb_h.status));
4500 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4503 /* Store the time the ccb was in the sim */
4504 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4505 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4506 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4507 queue = &cam_doneqs[hash];
4508 mtx_lock(&queue->cam_doneq_mtx);
4509 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4510 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4511 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4512 mtx_unlock(&queue->cam_doneq_mtx);
4514 wakeup(&queue->cam_doneq);
4518 xpt_done_direct(union ccb *done_ccb)
4521 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4522 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4523 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4526 /* Store the time the ccb was in the sim */
4527 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4528 xpt_done_process(&done_ccb->ccb_h);
4536 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4541 xpt_alloc_ccb_nowait()
4545 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4550 xpt_free_ccb(union ccb *free_ccb)
4552 free(free_ccb, M_CAMCCB);
4557 /* Private XPT functions */
4560 * Get a CAM control block for the caller. Charge the structure to the device
4561 * referenced by the path. If we don't have sufficient resources to allocate
4562 * more ccbs, we return NULL.
4565 xpt_get_ccb_nowait(struct cam_periph *periph)
4569 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4570 if (new_ccb == NULL)
4572 periph->periph_allocated++;
4573 cam_ccbq_take_opening(&periph->path->device->ccbq);
4578 xpt_get_ccb(struct cam_periph *periph)
4582 cam_periph_unlock(periph);
4583 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4584 cam_periph_lock(periph);
4585 periph->periph_allocated++;
4586 cam_ccbq_take_opening(&periph->path->device->ccbq);
4591 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4593 struct ccb_hdr *ccb_h;
4595 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4596 cam_periph_assert(periph, MA_OWNED);
4597 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4598 ccb_h->pinfo.priority != priority) {
4599 if (priority < periph->immediate_priority) {
4600 periph->immediate_priority = priority;
4601 xpt_run_allocq(periph, 0);
4603 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4606 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4607 return ((union ccb *)ccb_h);
4611 xpt_acquire_bus(struct cam_eb *bus)
4620 xpt_release_bus(struct cam_eb *bus)
4624 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4625 if (--bus->refcount > 0) {
4629 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4630 xsoftc.bus_generation++;
4632 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4633 ("destroying bus, but target list is not empty"));
4634 cam_sim_release(bus->sim);
4635 mtx_destroy(&bus->eb_mtx);
4636 free(bus, M_CAMXPT);
4639 static struct cam_et *
4640 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4642 struct cam_et *cur_target, *target;
4644 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4645 mtx_assert(&bus->eb_mtx, MA_OWNED);
4646 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4651 TAILQ_INIT(&target->ed_entries);
4653 target->target_id = target_id;
4654 target->refcount = 1;
4655 target->generation = 0;
4656 target->luns = NULL;
4657 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4658 timevalclear(&target->last_reset);
4660 * Hold a reference to our parent bus so it
4661 * will not go away before we do.
4665 /* Insertion sort into our bus's target list */
4666 cur_target = TAILQ_FIRST(&bus->et_entries);
4667 while (cur_target != NULL && cur_target->target_id < target_id)
4668 cur_target = TAILQ_NEXT(cur_target, links);
4669 if (cur_target != NULL) {
4670 TAILQ_INSERT_BEFORE(cur_target, target, links);
4672 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4679 xpt_acquire_target(struct cam_et *target)
4681 struct cam_eb *bus = target->bus;
4683 mtx_lock(&bus->eb_mtx);
4685 mtx_unlock(&bus->eb_mtx);
4689 xpt_release_target(struct cam_et *target)
4691 struct cam_eb *bus = target->bus;
4693 mtx_lock(&bus->eb_mtx);
4694 if (--target->refcount > 0) {
4695 mtx_unlock(&bus->eb_mtx);
4698 TAILQ_REMOVE(&bus->et_entries, target, links);
4700 mtx_unlock(&bus->eb_mtx);
4701 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4702 ("destroying target, but device list is not empty"));
4703 xpt_release_bus(bus);
4704 mtx_destroy(&target->luns_mtx);
4706 free(target->luns, M_CAMXPT);
4707 free(target, M_CAMXPT);
4710 static struct cam_ed *
4711 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4714 struct cam_ed *device;
4716 device = xpt_alloc_device(bus, target, lun_id);
4720 device->mintags = 1;
4721 device->maxtags = 1;
4726 xpt_destroy_device(void *context, int pending)
4728 struct cam_ed *device = context;
4730 mtx_lock(&device->device_mtx);
4731 mtx_destroy(&device->device_mtx);
4732 free(device, M_CAMDEV);
4736 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4738 struct cam_ed *cur_device, *device;
4739 struct cam_devq *devq;
4742 mtx_assert(&bus->eb_mtx, MA_OWNED);
4743 /* Make space for us in the device queue on our bus */
4744 devq = bus->sim->devq;
4745 mtx_lock(&devq->send_mtx);
4746 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4747 mtx_unlock(&devq->send_mtx);
4748 if (status != CAM_REQ_CMP)
4751 device = (struct cam_ed *)malloc(sizeof(*device),
4752 M_CAMDEV, M_NOWAIT|M_ZERO);
4756 cam_init_pinfo(&device->devq_entry);
4757 device->target = target;
4758 device->lun_id = lun_id;
4759 device->sim = bus->sim;
4760 if (cam_ccbq_init(&device->ccbq,
4761 bus->sim->max_dev_openings) != 0) {
4762 free(device, M_CAMDEV);
4765 SLIST_INIT(&device->asyncs);
4766 SLIST_INIT(&device->periphs);
4767 device->generation = 0;
4768 device->flags = CAM_DEV_UNCONFIGURED;
4769 device->tag_delay_count = 0;
4770 device->tag_saved_openings = 0;
4771 device->refcount = 1;
4772 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4773 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4774 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4776 * Hold a reference to our parent bus so it
4777 * will not go away before we do.
4781 cur_device = TAILQ_FIRST(&target->ed_entries);
4782 while (cur_device != NULL && cur_device->lun_id < lun_id)
4783 cur_device = TAILQ_NEXT(cur_device, links);
4784 if (cur_device != NULL)
4785 TAILQ_INSERT_BEFORE(cur_device, device, links);
4787 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4788 target->generation++;
4793 xpt_acquire_device(struct cam_ed *device)
4795 struct cam_eb *bus = device->target->bus;
4797 mtx_lock(&bus->eb_mtx);
4799 mtx_unlock(&bus->eb_mtx);
4803 xpt_release_device(struct cam_ed *device)
4805 struct cam_eb *bus = device->target->bus;
4806 struct cam_devq *devq;
4808 mtx_lock(&bus->eb_mtx);
4809 if (--device->refcount > 0) {
4810 mtx_unlock(&bus->eb_mtx);
4814 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4815 device->target->generation++;
4816 mtx_unlock(&bus->eb_mtx);
4818 /* Release our slot in the devq */
4819 devq = bus->sim->devq;
4820 mtx_lock(&devq->send_mtx);
4821 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4822 mtx_unlock(&devq->send_mtx);
4824 KASSERT(SLIST_EMPTY(&device->periphs),
4825 ("destroying device, but periphs list is not empty"));
4826 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4827 ("destroying device while still queued for ccbs"));
4829 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4830 callout_stop(&device->callout);
4832 xpt_release_target(device->target);
4834 cam_ccbq_fini(&device->ccbq);
4836 * Free allocated memory. free(9) does nothing if the
4837 * supplied pointer is NULL, so it is safe to call without
4840 free(device->supported_vpds, M_CAMXPT);
4841 free(device->device_id, M_CAMXPT);
4842 free(device->ext_inq, M_CAMXPT);
4843 free(device->physpath, M_CAMXPT);
4844 free(device->rcap_buf, M_CAMXPT);
4845 free(device->serial_num, M_CAMXPT);
4846 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4850 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4856 mtx_lock(&dev->sim->devq->send_mtx);
4857 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4858 mtx_unlock(&dev->sim->devq->send_mtx);
4859 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4860 || (dev->inq_flags & SID_CmdQue) != 0)
4861 dev->tag_saved_openings = newopenings;
4865 static struct cam_eb *
4866 xpt_find_bus(path_id_t path_id)
4871 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4873 bus = TAILQ_NEXT(bus, links)) {
4874 if (bus->path_id == path_id) {
4883 static struct cam_et *
4884 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4886 struct cam_et *target;
4888 mtx_assert(&bus->eb_mtx, MA_OWNED);
4889 for (target = TAILQ_FIRST(&bus->et_entries);
4891 target = TAILQ_NEXT(target, links)) {
4892 if (target->target_id == target_id) {
4900 static struct cam_ed *
4901 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4903 struct cam_ed *device;
4905 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4906 for (device = TAILQ_FIRST(&target->ed_entries);
4908 device = TAILQ_NEXT(device, links)) {
4909 if (device->lun_id == lun_id) {
4918 xpt_start_tags(struct cam_path *path)
4920 struct ccb_relsim crs;
4921 struct cam_ed *device;
4922 struct cam_sim *sim;
4925 device = path->device;
4926 sim = path->bus->sim;
4927 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4928 xpt_freeze_devq(path, /*count*/1);
4929 device->inq_flags |= SID_CmdQue;
4930 if (device->tag_saved_openings != 0)
4931 newopenings = device->tag_saved_openings;
4933 newopenings = min(device->maxtags,
4934 sim->max_tagged_dev_openings);
4935 xpt_dev_ccbq_resize(path, newopenings);
4936 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4937 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4938 crs.ccb_h.func_code = XPT_REL_SIMQ;
4939 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4941 = crs.release_timeout
4944 xpt_action((union ccb *)&crs);
4948 xpt_stop_tags(struct cam_path *path)
4950 struct ccb_relsim crs;
4951 struct cam_ed *device;
4952 struct cam_sim *sim;
4954 device = path->device;
4955 sim = path->bus->sim;
4956 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4957 device->tag_delay_count = 0;
4958 xpt_freeze_devq(path, /*count*/1);
4959 device->inq_flags &= ~SID_CmdQue;
4960 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4961 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4962 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4963 crs.ccb_h.func_code = XPT_REL_SIMQ;
4964 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4966 = crs.release_timeout
4969 xpt_action((union ccb *)&crs);
4973 xpt_boot_delay(void *arg)
4980 xpt_config(void *arg)
4983 * Now that interrupts are enabled, go find our devices
4985 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4986 printf("xpt_config: failed to create taskqueue thread.\n");
4988 /* Setup debugging path */
4989 if (cam_dflags != CAM_DEBUG_NONE) {
4990 if (xpt_create_path(&cam_dpath, NULL,
4991 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4992 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4993 printf("xpt_config: xpt_create_path() failed for debug"
4994 " target %d:%d:%d, debugging disabled\n",
4995 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4996 cam_dflags = CAM_DEBUG_NONE;
5001 periphdriver_init(1);
5003 callout_init(&xsoftc.boot_callout, 1);
5004 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5005 xpt_boot_delay, NULL, 0);
5006 /* Fire up rescan thread. */
5007 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5008 "cam", "scanner")) {
5009 printf("xpt_config: failed to create rescan thread.\n");
5017 xsoftc.buses_to_config++;
5022 xpt_release_boot(void)
5025 xsoftc.buses_to_config--;
5026 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5027 struct xpt_task *task;
5029 xsoftc.buses_config_done = 1;
5031 /* Call manually because we don't have any busses */
5032 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5034 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5035 taskqueue_enqueue(taskqueue_thread, &task->task);
5042 * If the given device only has one peripheral attached to it, and if that
5043 * peripheral is the passthrough driver, announce it. This insures that the
5044 * user sees some sort of announcement for every peripheral in their system.
5047 xptpassannouncefunc(struct cam_ed *device, void *arg)
5049 struct cam_periph *periph;
5052 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5053 periph = SLIST_NEXT(periph, periph_links), i++);
5055 periph = SLIST_FIRST(&device->periphs);
5057 && (strncmp(periph->periph_name, "pass", 4) == 0))
5058 xpt_announce_periph(periph, NULL);
5064 xpt_finishconfig_task(void *context, int pending)
5067 periphdriver_init(2);
5069 * Check for devices with no "standard" peripheral driver
5070 * attached. For any devices like that, announce the
5071 * passthrough driver so the user will see something.
5074 xpt_for_all_devices(xptpassannouncefunc, NULL);
5076 /* Release our hook so that the boot can continue. */
5077 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5078 free(xsoftc.xpt_config_hook, M_CAMXPT);
5079 xsoftc.xpt_config_hook = NULL;
5081 free(context, M_CAMXPT);
5085 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5086 struct cam_path *path)
5088 struct ccb_setasync csa;
5093 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5094 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5095 if (status != CAM_REQ_CMP)
5097 xpt_path_lock(path);
5101 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5102 csa.ccb_h.func_code = XPT_SASYNC_CB;
5103 csa.event_enable = event;
5104 csa.callback = cbfunc;
5105 csa.callback_arg = cbarg;
5106 xpt_action((union ccb *)&csa);
5107 status = csa.ccb_h.status;
5109 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5110 ("xpt_register_async: func %p\n", cbfunc));
5113 xpt_path_unlock(path);
5114 xpt_free_path(path);
5117 if ((status == CAM_REQ_CMP) &&
5118 (csa.event_enable & AC_FOUND_DEVICE)) {
5120 * Get this peripheral up to date with all
5121 * the currently existing devices.
5123 xpt_for_all_devices(xptsetasyncfunc, &csa);
5125 if ((status == CAM_REQ_CMP) &&
5126 (csa.event_enable & AC_PATH_REGISTERED)) {
5128 * Get this peripheral up to date with all
5129 * the currently existing busses.
5131 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5138 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5140 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5142 switch (work_ccb->ccb_h.func_code) {
5143 /* Common cases first */
5144 case XPT_PATH_INQ: /* Path routing inquiry */
5146 struct ccb_pathinq *cpi;
5148 cpi = &work_ccb->cpi;
5149 cpi->version_num = 1; /* XXX??? */
5150 cpi->hba_inquiry = 0;
5151 cpi->target_sprt = 0;
5153 cpi->hba_eng_cnt = 0;
5154 cpi->max_target = 0;
5156 cpi->initiator_id = 0;
5157 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5158 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5159 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5160 cpi->unit_number = sim->unit_number;
5161 cpi->bus_id = sim->bus_id;
5162 cpi->base_transfer_speed = 0;
5163 cpi->protocol = PROTO_UNSPECIFIED;
5164 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5165 cpi->transport = XPORT_UNSPECIFIED;
5166 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5167 cpi->ccb_h.status = CAM_REQ_CMP;
5172 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5179 * The xpt as a "controller" has no interrupt sources, so polling
5183 xptpoll(struct cam_sim *sim)
5188 xpt_lock_buses(void)
5190 mtx_lock(&xsoftc.xpt_topo_lock);
5194 xpt_unlock_buses(void)
5196 mtx_unlock(&xsoftc.xpt_topo_lock);
5200 xpt_path_mtx(struct cam_path *path)
5203 return (&path->device->device_mtx);
5207 xpt_done_process(struct ccb_hdr *ccb_h)
5209 struct cam_sim *sim;
5210 struct cam_devq *devq;
5211 struct mtx *mtx = NULL;
5213 if (ccb_h->flags & CAM_HIGH_POWER) {
5214 struct highpowerlist *hphead;
5215 struct cam_ed *device;
5217 mtx_lock(&xsoftc.xpt_highpower_lock);
5218 hphead = &xsoftc.highpowerq;
5220 device = STAILQ_FIRST(hphead);
5223 * Increment the count since this command is done.
5225 xsoftc.num_highpower++;
5228 * Any high powered commands queued up?
5230 if (device != NULL) {
5232 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5233 mtx_unlock(&xsoftc.xpt_highpower_lock);
5235 mtx_lock(&device->sim->devq->send_mtx);
5236 xpt_release_devq_device(device,
5237 /*count*/1, /*runqueue*/TRUE);
5238 mtx_unlock(&device->sim->devq->send_mtx);
5240 mtx_unlock(&xsoftc.xpt_highpower_lock);
5243 sim = ccb_h->path->bus->sim;
5245 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5246 xpt_release_simq(sim, /*run_queue*/FALSE);
5247 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5250 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5251 && (ccb_h->status & CAM_DEV_QFRZN)) {
5252 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5253 ccb_h->status &= ~CAM_DEV_QFRZN;
5257 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5258 struct cam_ed *dev = ccb_h->path->device;
5260 mtx_lock(&devq->send_mtx);
5261 devq->send_active--;
5262 devq->send_openings++;
5263 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5265 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5266 && (dev->ccbq.dev_active == 0))) {
5267 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5268 xpt_release_devq_device(dev, /*count*/1,
5269 /*run_queue*/FALSE);
5272 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5273 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5274 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5275 xpt_release_devq_device(dev, /*count*/1,
5276 /*run_queue*/FALSE);
5279 if (!device_is_queued(dev))
5280 (void)xpt_schedule_devq(devq, dev);
5282 mtx_unlock(&devq->send_mtx);
5284 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5285 mtx = xpt_path_mtx(ccb_h->path);
5288 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5289 && (--dev->tag_delay_count == 0))
5290 xpt_start_tags(ccb_h->path);
5294 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5296 mtx = xpt_path_mtx(ccb_h->path);
5306 /* Call the peripheral driver's callback */
5307 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5308 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5314 xpt_done_td(void *arg)
5316 struct cam_doneq *queue = arg;
5317 struct ccb_hdr *ccb_h;
5318 STAILQ_HEAD(, ccb_hdr) doneq;
5320 STAILQ_INIT(&doneq);
5321 mtx_lock(&queue->cam_doneq_mtx);
5323 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5324 queue->cam_doneq_sleep = 1;
5325 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5327 queue->cam_doneq_sleep = 0;
5329 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5330 mtx_unlock(&queue->cam_doneq_mtx);
5332 THREAD_NO_SLEEPING();
5333 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5334 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5335 xpt_done_process(ccb_h);
5337 THREAD_SLEEPING_OK();
5339 mtx_lock(&queue->cam_doneq_mtx);
5344 camisr_runqueue(void)
5346 struct ccb_hdr *ccb_h;
5347 struct cam_doneq *queue;
5350 /* Process global queues. */
5351 for (i = 0; i < cam_num_doneqs; i++) {
5352 queue = &cam_doneqs[i];
5353 mtx_lock(&queue->cam_doneq_mtx);
5354 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5355 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5356 mtx_unlock(&queue->cam_doneq_mtx);
5357 xpt_done_process(ccb_h);
5358 mtx_lock(&queue->cam_doneq_mtx);
5360 mtx_unlock(&queue->cam_doneq_mtx);
5370 static struct kv map[] = {
5371 { XPT_NOOP, "XPT_NOOP" },
5372 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5373 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5374 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5375 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5376 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5377 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5378 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5379 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5380 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5381 { XPT_DEBUG, "XPT_DEBUG" },
5382 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5383 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5384 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5385 { XPT_ASYNC, "XPT_ASYNC" },
5386 { XPT_ABORT, "XPT_ABORT" },
5387 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5388 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5389 { XPT_TERM_IO, "XPT_TERM_IO" },
5390 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5391 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5392 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5393 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5394 { XPT_ATA_IO, "XPT_ATA_IO" },
5395 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5396 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5397 { XPT_NVME_IO, "XPT_NVME_IO" },
5398 { XPT_MMCSD_IO, "XPT_MMCSD_IO" },
5399 { XPT_SMP_IO, "XPT_SMP_IO" },
5400 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5401 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5402 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5403 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5404 { XPT_EN_LUN, "XPT_EN_LUN" },
5405 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5406 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5407 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5408 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5409 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5410 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5411 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5416 xpt_action_name(uint32_t action)
5418 static char buffer[32]; /* Only for unknown messages -- racy */
5419 struct kv *walker = map;
5421 while (walker->name != NULL) {
5422 if (walker->v == action)
5423 return (walker->name);
5427 snprintf(buffer, sizeof(buffer), "%#x", action);