2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_compat.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
69 #include <machine/md_var.h> /* geometry translation */
70 #include <machine/stdarg.h> /* for xpt_print below */
75 * This is the maximum number of high powered commands (e.g. start unit)
76 * that can be outstanding at a particular time.
78 #ifndef CAM_MAX_HIGHPOWER
79 #define CAM_MAX_HIGHPOWER 4
82 /* Datastructures internal to the xpt layer */
83 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
88 /* Object for defering XPT actions to a taskqueue */
96 uint32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 struct mtx xpt_highpower_lock;
100 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 int buses_config_done;
108 /* Registered busses */
109 TAILQ_HEAD(,cam_eb) xpt_busses;
110 u_int bus_generation;
112 struct intr_config_hook *xpt_config_hook;
115 struct callout boot_callout;
117 struct mtx xpt_topo_lock;
119 struct taskqueue *xpt_taskq;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
158 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
159 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
162 struct mtx_padalign cam_doneq_mtx;
163 STAILQ_HEAD(, ccb_hdr) cam_doneq;
167 static struct cam_doneq cam_doneqs[MAXCPU];
168 static int cam_num_doneqs;
169 static struct proc *cam_proc;
171 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
172 &cam_num_doneqs, 0, "Number of completion queues/threads");
174 struct cam_periph *xpt_periph;
176 static periph_init_t xpt_periph_init;
178 static struct periph_driver xpt_driver =
180 xpt_periph_init, "xpt",
181 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
185 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
187 static d_open_t xptopen;
188 static d_close_t xptclose;
189 static d_ioctl_t xptioctl;
190 static d_ioctl_t xptdoioctl;
192 static struct cdevsw xpt_cdevsw = {
193 .d_version = D_VERSION,
201 /* Storage for debugging datastructures */
202 struct cam_path *cam_dpath;
203 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
204 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
205 &cam_dflags, 0, "Enabled debug flags");
206 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
207 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
208 &cam_debug_delay, 0, "Delay in us after each debug message");
210 /* Our boot-time initialization hook */
211 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
213 static moduledata_t cam_moduledata = {
215 cam_module_event_handler,
219 static int xpt_init(void *);
221 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
222 MODULE_VERSION(cam, 1);
225 static void xpt_async_bcast(struct async_list *async_head,
226 u_int32_t async_code,
227 struct cam_path *path,
229 static path_id_t xptnextfreepathid(void);
230 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
231 static union ccb *xpt_get_ccb(struct cam_periph *periph);
232 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
233 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
234 static void xpt_run_allocq_task(void *context, int pending);
235 static void xpt_run_devq(struct cam_devq *devq);
236 static timeout_t xpt_release_devq_timeout;
237 static void xpt_release_simq_timeout(void *arg) __unused;
238 static void xpt_acquire_bus(struct cam_eb *bus);
239 static void xpt_release_bus(struct cam_eb *bus);
240 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
241 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
243 static struct cam_et*
244 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
245 static void xpt_acquire_target(struct cam_et *target);
246 static void xpt_release_target(struct cam_et *target);
247 static struct cam_eb*
248 xpt_find_bus(path_id_t path_id);
249 static struct cam_et*
250 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
251 static struct cam_ed*
252 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
253 static void xpt_config(void *arg);
254 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
255 u_int32_t new_priority);
256 static xpt_devicefunc_t xptpassannouncefunc;
257 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
258 static void xptpoll(struct cam_sim *sim);
259 static void camisr_runqueue(void);
260 static void xpt_done_process(struct ccb_hdr *ccb_h);
261 static void xpt_done_td(void *);
262 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
263 u_int num_patterns, struct cam_eb *bus);
264 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
266 struct cam_ed *device);
267 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
269 struct cam_periph *periph);
270 static xpt_busfunc_t xptedtbusfunc;
271 static xpt_targetfunc_t xptedttargetfunc;
272 static xpt_devicefunc_t xptedtdevicefunc;
273 static xpt_periphfunc_t xptedtperiphfunc;
274 static xpt_pdrvfunc_t xptplistpdrvfunc;
275 static xpt_periphfunc_t xptplistperiphfunc;
276 static int xptedtmatch(struct ccb_dev_match *cdm);
277 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
278 static int xptbustraverse(struct cam_eb *start_bus,
279 xpt_busfunc_t *tr_func, void *arg);
280 static int xpttargettraverse(struct cam_eb *bus,
281 struct cam_et *start_target,
282 xpt_targetfunc_t *tr_func, void *arg);
283 static int xptdevicetraverse(struct cam_et *target,
284 struct cam_ed *start_device,
285 xpt_devicefunc_t *tr_func, void *arg);
286 static int xptperiphtraverse(struct cam_ed *device,
287 struct cam_periph *start_periph,
288 xpt_periphfunc_t *tr_func, void *arg);
289 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
290 xpt_pdrvfunc_t *tr_func, void *arg);
291 static int xptpdperiphtraverse(struct periph_driver **pdrv,
292 struct cam_periph *start_periph,
293 xpt_periphfunc_t *tr_func,
295 static xpt_busfunc_t xptdefbusfunc;
296 static xpt_targetfunc_t xptdeftargetfunc;
297 static xpt_devicefunc_t xptdefdevicefunc;
298 static xpt_periphfunc_t xptdefperiphfunc;
299 static void xpt_finishconfig_task(void *context, int pending);
300 static void xpt_dev_async_default(u_int32_t async_code,
302 struct cam_et *target,
303 struct cam_ed *device,
305 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
306 struct cam_et *target,
308 static xpt_devicefunc_t xptsetasyncfunc;
309 static xpt_busfunc_t xptsetasyncbusfunc;
310 static cam_status xptregister(struct cam_periph *periph,
312 static const char * xpt_action_name(uint32_t action);
313 static __inline int device_is_queued(struct cam_ed *device);
316 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
320 mtx_assert(&devq->send_mtx, MA_OWNED);
321 if ((dev->ccbq.queue.entries > 0) &&
322 (dev->ccbq.dev_openings > 0) &&
323 (dev->ccbq.queue.qfrozen_cnt == 0)) {
325 * The priority of a device waiting for controller
326 * resources is that of the highest priority CCB
330 xpt_schedule_dev(&devq->send_queue,
332 CAMQ_GET_PRIO(&dev->ccbq.queue));
340 device_is_queued(struct cam_ed *device)
342 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
348 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
352 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
356 * Only allow read-write access.
358 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
362 * We don't allow nonblocking access.
364 if ((flags & O_NONBLOCK) != 0) {
365 printf("%s: can't do nonblocking access\n", devtoname(dev));
373 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
380 * Don't automatically grab the xpt softc lock here even though this is going
381 * through the xpt device. The xpt device is really just a back door for
382 * accessing other devices and SIMs, so the right thing to do is to grab
383 * the appropriate SIM lock once the bus/SIM is located.
386 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
390 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
391 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
397 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
405 * For the transport layer CAMIOCOMMAND ioctl, we really only want
406 * to accept CCB types that don't quite make sense to send through a
407 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
415 inccb = (union ccb *)addr;
417 bus = xpt_find_bus(inccb->ccb_h.path_id);
421 switch (inccb->ccb_h.func_code) {
424 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
425 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
426 xpt_release_bus(bus);
431 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
432 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
433 xpt_release_bus(bus);
441 switch(inccb->ccb_h.func_code) {
449 ccb = xpt_alloc_ccb();
452 * Create a path using the bus, target, and lun the
455 if (xpt_create_path(&ccb->ccb_h.path, NULL,
456 inccb->ccb_h.path_id,
457 inccb->ccb_h.target_id,
458 inccb->ccb_h.target_lun) !=
464 /* Ensure all of our fields are correct */
465 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
466 inccb->ccb_h.pinfo.priority);
467 xpt_merge_ccb(ccb, inccb);
468 xpt_path_lock(ccb->ccb_h.path);
469 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
470 xpt_path_unlock(ccb->ccb_h.path);
471 bcopy(ccb, inccb, sizeof(union ccb));
472 xpt_free_path(ccb->ccb_h.path);
480 * This is an immediate CCB, so it's okay to
481 * allocate it on the stack.
485 * Create a path using the bus, target, and lun the
488 if (xpt_create_path(&ccb.ccb_h.path, NULL,
489 inccb->ccb_h.path_id,
490 inccb->ccb_h.target_id,
491 inccb->ccb_h.target_lun) !=
496 /* Ensure all of our fields are correct */
497 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
498 inccb->ccb_h.pinfo.priority);
499 xpt_merge_ccb(&ccb, inccb);
501 bcopy(&ccb, inccb, sizeof(union ccb));
502 xpt_free_path(ccb.ccb_h.path);
506 case XPT_DEV_MATCH: {
507 struct cam_periph_map_info mapinfo;
508 struct cam_path *old_path;
511 * We can't deal with physical addresses for this
512 * type of transaction.
514 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
521 * Save this in case the caller had it set to
522 * something in particular.
524 old_path = inccb->ccb_h.path;
527 * We really don't need a path for the matching
528 * code. The path is needed because of the
529 * debugging statements in xpt_action(). They
530 * assume that the CCB has a valid path.
532 inccb->ccb_h.path = xpt_periph->path;
534 bzero(&mapinfo, sizeof(mapinfo));
537 * Map the pattern and match buffers into kernel
538 * virtual address space.
540 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
543 inccb->ccb_h.path = old_path;
548 * This is an immediate CCB, we can send it on directly.
553 * Map the buffers back into user space.
555 cam_periph_unmapmem(inccb, &mapinfo);
557 inccb->ccb_h.path = old_path;
566 xpt_release_bus(bus);
570 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
571 * with the periphal driver name and unit name filled in. The other
572 * fields don't really matter as input. The passthrough driver name
573 * ("pass"), and unit number are passed back in the ccb. The current
574 * device generation number, and the index into the device peripheral
575 * driver list, and the status are also passed back. Note that
576 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
577 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
578 * (or rather should be) impossible for the device peripheral driver
579 * list to change since we look at the whole thing in one pass, and
580 * we do it with lock protection.
583 case CAMGETPASSTHRU: {
585 struct cam_periph *periph;
586 struct periph_driver **p_drv;
589 int base_periph_found;
591 ccb = (union ccb *)addr;
592 unit = ccb->cgdl.unit_number;
593 name = ccb->cgdl.periph_name;
594 base_periph_found = 0;
597 * Sanity check -- make sure we don't get a null peripheral
600 if (*ccb->cgdl.periph_name == '\0') {
605 /* Keep the list from changing while we traverse it */
608 /* first find our driver in the list of drivers */
609 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
610 if (strcmp((*p_drv)->driver_name, name) == 0)
613 if (*p_drv == NULL) {
615 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
616 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
617 *ccb->cgdl.periph_name = '\0';
618 ccb->cgdl.unit_number = 0;
624 * Run through every peripheral instance of this driver
625 * and check to see whether it matches the unit passed
626 * in by the user. If it does, get out of the loops and
627 * find the passthrough driver associated with that
630 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
631 periph = TAILQ_NEXT(periph, unit_links)) {
633 if (periph->unit_number == unit)
637 * If we found the peripheral driver that the user passed
638 * in, go through all of the peripheral drivers for that
639 * particular device and look for a passthrough driver.
641 if (periph != NULL) {
642 struct cam_ed *device;
645 base_periph_found = 1;
646 device = periph->path->device;
647 for (i = 0, periph = SLIST_FIRST(&device->periphs);
649 periph = SLIST_NEXT(periph, periph_links), i++) {
651 * Check to see whether we have a
652 * passthrough device or not.
654 if (strcmp(periph->periph_name, "pass") == 0) {
656 * Fill in the getdevlist fields.
658 strcpy(ccb->cgdl.periph_name,
659 periph->periph_name);
660 ccb->cgdl.unit_number =
662 if (SLIST_NEXT(periph, periph_links))
664 CAM_GDEVLIST_MORE_DEVS;
667 CAM_GDEVLIST_LAST_DEVICE;
668 ccb->cgdl.generation =
672 * Fill in some CCB header fields
673 * that the user may want.
676 periph->path->bus->path_id;
677 ccb->ccb_h.target_id =
678 periph->path->target->target_id;
679 ccb->ccb_h.target_lun =
680 periph->path->device->lun_id;
681 ccb->ccb_h.status = CAM_REQ_CMP;
688 * If the periph is null here, one of two things has
689 * happened. The first possibility is that we couldn't
690 * find the unit number of the particular peripheral driver
691 * that the user is asking about. e.g. the user asks for
692 * the passthrough driver for "da11". We find the list of
693 * "da" peripherals all right, but there is no unit 11.
694 * The other possibility is that we went through the list
695 * of peripheral drivers attached to the device structure,
696 * but didn't find one with the name "pass". Either way,
697 * we return ENOENT, since we couldn't find something.
699 if (periph == NULL) {
700 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
701 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
702 *ccb->cgdl.periph_name = '\0';
703 ccb->cgdl.unit_number = 0;
706 * It is unfortunate that this is even necessary,
707 * but there are many, many clueless users out there.
708 * If this is true, the user is looking for the
709 * passthrough driver, but doesn't have one in his
712 if (base_periph_found == 1) {
713 printf("xptioctl: pass driver is not in the "
715 printf("xptioctl: put \"device pass\" in "
716 "your kernel config file\n");
731 cam_module_event_handler(module_t mod, int what, void *arg)
737 if ((error = xpt_init(NULL)) != 0)
749 static struct xpt_proto *
750 xpt_proto_find(cam_proto proto)
752 struct xpt_proto **pp;
754 SET_FOREACH(pp, cam_xpt_proto_set) {
755 if ((*pp)->proto == proto)
763 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
766 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
767 xpt_free_path(done_ccb->ccb_h.path);
768 xpt_free_ccb(done_ccb);
770 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
771 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
776 /* thread to handle bus rescans */
778 xpt_scanner_thread(void *dummy)
781 struct cam_path path;
785 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
786 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
788 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
789 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
793 * Since lock can be dropped inside and path freed
794 * by completion callback even before return here,
795 * take our own path copy for reference.
797 xpt_copy_path(&path, ccb->ccb_h.path);
798 xpt_path_lock(&path);
800 xpt_path_unlock(&path);
801 xpt_release_path(&path);
809 xpt_rescan(union ccb *ccb)
813 /* Prepare request */
814 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
815 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
816 ccb->ccb_h.func_code = XPT_SCAN_BUS;
817 else 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_TGT;
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_LUN;
824 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
825 xpt_free_path(ccb->ccb_h.path);
829 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
830 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
831 xpt_action_name(ccb->ccb_h.func_code)));
833 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
834 ccb->ccb_h.cbfcnp = xpt_rescan_done;
835 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
836 /* Don't make duplicate entries for the same paths. */
838 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
839 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
840 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
841 wakeup(&xsoftc.ccb_scanq);
843 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
844 xpt_free_path(ccb->ccb_h.path);
850 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
851 xsoftc.buses_to_config++;
852 wakeup(&xsoftc.ccb_scanq);
856 /* Functions accessed by the peripheral drivers */
858 xpt_init(void *dummy)
860 struct cam_sim *xpt_sim;
861 struct cam_path *path;
862 struct cam_devq *devq;
866 TAILQ_INIT(&xsoftc.xpt_busses);
867 TAILQ_INIT(&xsoftc.ccb_scanq);
868 STAILQ_INIT(&xsoftc.highpowerq);
869 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
871 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
872 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
873 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
874 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
876 #ifdef CAM_BOOT_DELAY
878 * Override this value at compile time to assist our users
879 * who don't use loader to boot a kernel.
881 xsoftc.boot_delay = CAM_BOOT_DELAY;
884 * The xpt layer is, itself, the equivalent of a SIM.
885 * Allow 16 ccbs in the ccb pool for it. This should
886 * give decent parallelism when we probe busses and
887 * perform other XPT functions.
889 devq = cam_simq_alloc(16);
890 xpt_sim = cam_sim_alloc(xptaction,
895 /*mtx*/&xsoftc.xpt_lock,
896 /*max_dev_transactions*/0,
897 /*max_tagged_dev_transactions*/0,
902 mtx_lock(&xsoftc.xpt_lock);
903 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
904 mtx_unlock(&xsoftc.xpt_lock);
905 printf("xpt_init: xpt_bus_register failed with status %#x,"
906 " failing attach\n", status);
909 mtx_unlock(&xsoftc.xpt_lock);
912 * Looking at the XPT from the SIM layer, the XPT is
913 * the equivalent of a peripheral driver. Allocate
914 * a peripheral driver entry for us.
916 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
918 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
919 printf("xpt_init: xpt_create_path failed with status %#x,"
920 " failing attach\n", status);
924 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
925 path, NULL, 0, xpt_sim);
926 xpt_path_unlock(path);
929 if (cam_num_doneqs < 1)
930 cam_num_doneqs = 1 + mp_ncpus / 6;
931 else if (cam_num_doneqs > MAXCPU)
932 cam_num_doneqs = MAXCPU;
933 for (i = 0; i < cam_num_doneqs; i++) {
934 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
936 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
937 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
938 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
944 if (cam_num_doneqs < 1) {
945 printf("xpt_init: Cannot init completion queues "
946 "- failing attach\n");
950 * Register a callback for when interrupts are enabled.
952 xsoftc.xpt_config_hook =
953 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
954 M_CAMXPT, M_NOWAIT | M_ZERO);
955 if (xsoftc.xpt_config_hook == NULL) {
956 printf("xpt_init: Cannot malloc config hook "
957 "- failing attach\n");
960 xsoftc.xpt_config_hook->ich_func = xpt_config;
961 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
962 free (xsoftc.xpt_config_hook, M_CAMXPT);
963 printf("xpt_init: config_intrhook_establish failed "
964 "- failing attach\n");
971 xptregister(struct cam_periph *periph, void *arg)
973 struct cam_sim *xpt_sim;
975 if (periph == NULL) {
976 printf("xptregister: periph was NULL!!\n");
977 return(CAM_REQ_CMP_ERR);
980 xpt_sim = (struct cam_sim *)arg;
981 xpt_sim->softc = periph;
983 periph->softc = NULL;
989 xpt_add_periph(struct cam_periph *periph)
991 struct cam_ed *device;
994 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
995 device = periph->path->device;
996 status = CAM_REQ_CMP;
997 if (device != NULL) {
998 mtx_lock(&device->target->bus->eb_mtx);
999 device->generation++;
1000 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1001 mtx_unlock(&device->target->bus->eb_mtx);
1002 atomic_add_32(&xsoftc.xpt_generation, 1);
1009 xpt_remove_periph(struct cam_periph *periph)
1011 struct cam_ed *device;
1013 device = periph->path->device;
1014 if (device != NULL) {
1015 mtx_lock(&device->target->bus->eb_mtx);
1016 device->generation++;
1017 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1018 mtx_unlock(&device->target->bus->eb_mtx);
1019 atomic_add_32(&xsoftc.xpt_generation, 1);
1025 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1027 struct cam_path *path = periph->path;
1028 struct xpt_proto *proto;
1030 cam_periph_assert(periph, MA_OWNED);
1031 periph->flags |= CAM_PERIPH_ANNOUNCED;
1033 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1034 periph->periph_name, periph->unit_number,
1035 path->bus->sim->sim_name,
1036 path->bus->sim->unit_number,
1037 path->bus->sim->bus_id,
1039 path->target->target_id,
1040 (uintmax_t)path->device->lun_id);
1041 printf("%s%d: ", periph->periph_name, periph->unit_number);
1042 proto = xpt_proto_find(path->device->protocol);
1044 proto->ops->announce(path->device);
1046 printf("%s%d: Unknown protocol device %d\n",
1047 periph->periph_name, periph->unit_number,
1048 path->device->protocol);
1049 if (path->device->serial_num_len > 0) {
1050 /* Don't wrap the screen - print only the first 60 chars */
1051 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1052 periph->unit_number, path->device->serial_num);
1054 /* Announce transport details. */
1055 path->bus->xport->ops->announce(periph);
1056 /* Announce command queueing. */
1057 if (path->device->inq_flags & SID_CmdQue
1058 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1059 printf("%s%d: Command Queueing enabled\n",
1060 periph->periph_name, periph->unit_number);
1062 /* Announce caller's details if they've passed in. */
1063 if (announce_string != NULL)
1064 printf("%s%d: %s\n", periph->periph_name,
1065 periph->unit_number, announce_string);
1069 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1072 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1073 periph->unit_number, quirks, bit_string);
1078 xpt_denounce_periph(struct cam_periph *periph)
1080 struct cam_path *path = periph->path;
1081 struct xpt_proto *proto;
1083 cam_periph_assert(periph, MA_OWNED);
1084 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1085 periph->periph_name, periph->unit_number,
1086 path->bus->sim->sim_name,
1087 path->bus->sim->unit_number,
1088 path->bus->sim->bus_id,
1090 path->target->target_id,
1091 (uintmax_t)path->device->lun_id);
1092 printf("%s%d: ", periph->periph_name, periph->unit_number);
1093 proto = xpt_proto_find(path->device->protocol);
1095 proto->ops->denounce(path->device);
1097 printf("%s%d: Unknown protocol device %d\n",
1098 periph->periph_name, periph->unit_number,
1099 path->device->protocol);
1100 if (path->device->serial_num_len > 0)
1101 printf(" s/n %.60s", path->device->serial_num);
1102 printf(" detached\n");
1107 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1110 struct ccb_dev_advinfo cdai;
1111 struct scsi_vpd_id_descriptor *idd;
1113 xpt_path_assert(path, MA_OWNED);
1115 memset(&cdai, 0, sizeof(cdai));
1116 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1117 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1120 if (!strcmp(attr, "GEOM::ident"))
1121 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1122 else if (!strcmp(attr, "GEOM::physpath"))
1123 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1124 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1125 strcmp(attr, "GEOM::lunname") == 0) {
1126 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1127 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1131 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1132 if (cdai.buf == NULL) {
1136 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1137 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1138 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1139 if (cdai.provsiz == 0)
1141 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1142 if (strcmp(attr, "GEOM::lunid") == 0) {
1143 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1144 cdai.provsiz, scsi_devid_is_lun_naa);
1146 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1147 cdai.provsiz, scsi_devid_is_lun_eui64);
1151 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1152 cdai.provsiz, scsi_devid_is_lun_t10);
1154 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1155 cdai.provsiz, scsi_devid_is_lun_name);
1159 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1160 if (idd->length < len) {
1161 for (l = 0; l < idd->length; l++)
1162 buf[l] = idd->identifier[l] ?
1163 idd->identifier[l] : ' ';
1167 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1168 l = strnlen(idd->identifier, idd->length);
1170 bcopy(idd->identifier, buf, l);
1175 if (idd->length * 2 < len) {
1176 for (l = 0; l < idd->length; l++)
1177 sprintf(buf + l * 2, "%02x",
1178 idd->identifier[l]);
1184 if (strlcpy(buf, cdai.buf, len) >= len)
1189 if (cdai.buf != NULL)
1190 free(cdai.buf, M_CAMXPT);
1194 static dev_match_ret
1195 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1198 dev_match_ret retval;
1201 retval = DM_RET_NONE;
1204 * If we aren't given something to match against, that's an error.
1207 return(DM_RET_ERROR);
1210 * If there are no match entries, then this bus matches no
1213 if ((patterns == NULL) || (num_patterns == 0))
1214 return(DM_RET_DESCEND | DM_RET_COPY);
1216 for (i = 0; i < num_patterns; i++) {
1217 struct bus_match_pattern *cur_pattern;
1220 * If the pattern in question isn't for a bus node, we
1221 * aren't interested. However, we do indicate to the
1222 * calling routine that we should continue descending the
1223 * tree, since the user wants to match against lower-level
1226 if (patterns[i].type != DEV_MATCH_BUS) {
1227 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1228 retval |= DM_RET_DESCEND;
1232 cur_pattern = &patterns[i].pattern.bus_pattern;
1235 * If they want to match any bus node, we give them any
1238 if (cur_pattern->flags == BUS_MATCH_ANY) {
1239 /* set the copy flag */
1240 retval |= DM_RET_COPY;
1243 * If we've already decided on an action, go ahead
1246 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1251 * Not sure why someone would do this...
1253 if (cur_pattern->flags == BUS_MATCH_NONE)
1256 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1257 && (cur_pattern->path_id != bus->path_id))
1260 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1261 && (cur_pattern->bus_id != bus->sim->bus_id))
1264 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1265 && (cur_pattern->unit_number != bus->sim->unit_number))
1268 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1269 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1274 * If we get to this point, the user definitely wants
1275 * information on this bus. So tell the caller to copy the
1278 retval |= DM_RET_COPY;
1281 * If the return action has been set to descend, then we
1282 * know that we've already seen a non-bus matching
1283 * expression, therefore we need to further descend the tree.
1284 * This won't change by continuing around the loop, so we
1285 * go ahead and return. If we haven't seen a non-bus
1286 * matching expression, we keep going around the loop until
1287 * we exhaust the matching expressions. We'll set the stop
1288 * flag once we fall out of the loop.
1290 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1295 * If the return action hasn't been set to descend yet, that means
1296 * we haven't seen anything other than bus matching patterns. So
1297 * tell the caller to stop descending the tree -- the user doesn't
1298 * want to match against lower level tree elements.
1300 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1301 retval |= DM_RET_STOP;
1306 static dev_match_ret
1307 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1308 struct cam_ed *device)
1310 dev_match_ret retval;
1313 retval = DM_RET_NONE;
1316 * If we aren't given something to match against, that's an error.
1319 return(DM_RET_ERROR);
1322 * If there are no match entries, then this device matches no
1325 if ((patterns == NULL) || (num_patterns == 0))
1326 return(DM_RET_DESCEND | DM_RET_COPY);
1328 for (i = 0; i < num_patterns; i++) {
1329 struct device_match_pattern *cur_pattern;
1330 struct scsi_vpd_device_id *device_id_page;
1333 * If the pattern in question isn't for a device node, we
1334 * aren't interested.
1336 if (patterns[i].type != DEV_MATCH_DEVICE) {
1337 if ((patterns[i].type == DEV_MATCH_PERIPH)
1338 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1339 retval |= DM_RET_DESCEND;
1343 cur_pattern = &patterns[i].pattern.device_pattern;
1345 /* Error out if mutually exclusive options are specified. */
1346 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1347 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1348 return(DM_RET_ERROR);
1351 * If they want to match any device node, we give them any
1354 if (cur_pattern->flags == DEV_MATCH_ANY)
1358 * Not sure why someone would do this...
1360 if (cur_pattern->flags == DEV_MATCH_NONE)
1363 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1364 && (cur_pattern->path_id != device->target->bus->path_id))
1367 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1368 && (cur_pattern->target_id != device->target->target_id))
1371 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1372 && (cur_pattern->target_lun != device->lun_id))
1375 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1376 && (cam_quirkmatch((caddr_t)&device->inq_data,
1377 (caddr_t)&cur_pattern->data.inq_pat,
1378 1, sizeof(cur_pattern->data.inq_pat),
1379 scsi_static_inquiry_match) == NULL))
1382 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1383 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1384 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1385 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1386 device->device_id_len
1387 - SVPD_DEVICE_ID_HDR_LEN,
1388 cur_pattern->data.devid_pat.id,
1389 cur_pattern->data.devid_pat.id_len) != 0))
1394 * If we get to this point, the user definitely wants
1395 * information on this device. So tell the caller to copy
1398 retval |= DM_RET_COPY;
1401 * If the return action has been set to descend, then we
1402 * know that we've already seen a peripheral matching
1403 * expression, therefore we need to further descend the tree.
1404 * This won't change by continuing around the loop, so we
1405 * go ahead and return. If we haven't seen a peripheral
1406 * matching expression, we keep going around the loop until
1407 * we exhaust the matching expressions. We'll set the stop
1408 * flag once we fall out of the loop.
1410 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1415 * If the return action hasn't been set to descend yet, that means
1416 * we haven't seen any peripheral matching patterns. So tell the
1417 * caller to stop descending the tree -- the user doesn't want to
1418 * match against lower level tree elements.
1420 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1421 retval |= DM_RET_STOP;
1427 * Match a single peripheral against any number of match patterns.
1429 static dev_match_ret
1430 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1431 struct cam_periph *periph)
1433 dev_match_ret retval;
1437 * If we aren't given something to match against, that's an error.
1440 return(DM_RET_ERROR);
1443 * If there are no match entries, then this peripheral matches no
1446 if ((patterns == NULL) || (num_patterns == 0))
1447 return(DM_RET_STOP | DM_RET_COPY);
1450 * There aren't any nodes below a peripheral node, so there's no
1451 * reason to descend the tree any further.
1453 retval = DM_RET_STOP;
1455 for (i = 0; i < num_patterns; i++) {
1456 struct periph_match_pattern *cur_pattern;
1459 * If the pattern in question isn't for a peripheral, we
1460 * aren't interested.
1462 if (patterns[i].type != DEV_MATCH_PERIPH)
1465 cur_pattern = &patterns[i].pattern.periph_pattern;
1468 * If they want to match on anything, then we will do so.
1470 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1471 /* set the copy flag */
1472 retval |= DM_RET_COPY;
1475 * We've already set the return action to stop,
1476 * since there are no nodes below peripherals in
1483 * Not sure why someone would do this...
1485 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1488 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1489 && (cur_pattern->path_id != periph->path->bus->path_id))
1493 * For the target and lun id's, we have to make sure the
1494 * target and lun pointers aren't NULL. The xpt peripheral
1495 * has a wildcard target and device.
1497 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1498 && ((periph->path->target == NULL)
1499 ||(cur_pattern->target_id != periph->path->target->target_id)))
1502 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1503 && ((periph->path->device == NULL)
1504 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1507 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1508 && (cur_pattern->unit_number != periph->unit_number))
1511 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1512 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1517 * If we get to this point, the user definitely wants
1518 * information on this peripheral. So tell the caller to
1519 * copy the data out.
1521 retval |= DM_RET_COPY;
1524 * The return action has already been set to stop, since
1525 * peripherals don't have any nodes below them in the EDT.
1531 * If we get to this point, the peripheral that was passed in
1532 * doesn't match any of the patterns.
1538 xptedtbusfunc(struct cam_eb *bus, void *arg)
1540 struct ccb_dev_match *cdm;
1541 struct cam_et *target;
1542 dev_match_ret retval;
1544 cdm = (struct ccb_dev_match *)arg;
1547 * If our position is for something deeper in the tree, that means
1548 * that we've already seen this node. So, we keep going down.
1550 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1551 && (cdm->pos.cookie.bus == bus)
1552 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1553 && (cdm->pos.cookie.target != NULL))
1554 retval = DM_RET_DESCEND;
1556 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1559 * If we got an error, bail out of the search.
1561 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1562 cdm->status = CAM_DEV_MATCH_ERROR;
1567 * If the copy flag is set, copy this bus out.
1569 if (retval & DM_RET_COPY) {
1572 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1573 sizeof(struct dev_match_result));
1576 * If we don't have enough space to put in another
1577 * match result, save our position and tell the
1578 * user there are more devices to check.
1580 if (spaceleft < sizeof(struct dev_match_result)) {
1581 bzero(&cdm->pos, sizeof(cdm->pos));
1582 cdm->pos.position_type =
1583 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1585 cdm->pos.cookie.bus = bus;
1586 cdm->pos.generations[CAM_BUS_GENERATION]=
1587 xsoftc.bus_generation;
1588 cdm->status = CAM_DEV_MATCH_MORE;
1591 j = cdm->num_matches;
1593 cdm->matches[j].type = DEV_MATCH_BUS;
1594 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1595 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1596 cdm->matches[j].result.bus_result.unit_number =
1597 bus->sim->unit_number;
1598 strncpy(cdm->matches[j].result.bus_result.dev_name,
1599 bus->sim->sim_name, DEV_IDLEN);
1603 * If the user is only interested in busses, there's no
1604 * reason to descend to the next level in the tree.
1606 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1610 * If there is a target generation recorded, check it to
1611 * make sure the target list hasn't changed.
1613 mtx_lock(&bus->eb_mtx);
1614 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1615 && (cdm->pos.cookie.bus == bus)
1616 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1617 && (cdm->pos.cookie.target != NULL)) {
1618 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1620 mtx_unlock(&bus->eb_mtx);
1621 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1624 target = (struct cam_et *)cdm->pos.cookie.target;
1628 mtx_unlock(&bus->eb_mtx);
1630 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1634 xptedttargetfunc(struct cam_et *target, void *arg)
1636 struct ccb_dev_match *cdm;
1638 struct cam_ed *device;
1640 cdm = (struct ccb_dev_match *)arg;
1644 * If there is a device list generation recorded, check it to
1645 * make sure the device list hasn't changed.
1647 mtx_lock(&bus->eb_mtx);
1648 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1649 && (cdm->pos.cookie.bus == bus)
1650 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1651 && (cdm->pos.cookie.target == target)
1652 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1653 && (cdm->pos.cookie.device != NULL)) {
1654 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1655 target->generation) {
1656 mtx_unlock(&bus->eb_mtx);
1657 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1660 device = (struct cam_ed *)cdm->pos.cookie.device;
1664 mtx_unlock(&bus->eb_mtx);
1666 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1670 xptedtdevicefunc(struct cam_ed *device, void *arg)
1673 struct cam_periph *periph;
1674 struct ccb_dev_match *cdm;
1675 dev_match_ret retval;
1677 cdm = (struct ccb_dev_match *)arg;
1678 bus = device->target->bus;
1681 * If our position is for something deeper in the tree, that means
1682 * that we've already seen this node. So, we keep going down.
1684 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1685 && (cdm->pos.cookie.device == device)
1686 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1687 && (cdm->pos.cookie.periph != NULL))
1688 retval = DM_RET_DESCEND;
1690 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1693 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1694 cdm->status = CAM_DEV_MATCH_ERROR;
1699 * If the copy flag is set, copy this device out.
1701 if (retval & DM_RET_COPY) {
1704 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1705 sizeof(struct dev_match_result));
1708 * If we don't have enough space to put in another
1709 * match result, save our position and tell the
1710 * user there are more devices to check.
1712 if (spaceleft < sizeof(struct dev_match_result)) {
1713 bzero(&cdm->pos, sizeof(cdm->pos));
1714 cdm->pos.position_type =
1715 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1716 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1718 cdm->pos.cookie.bus = device->target->bus;
1719 cdm->pos.generations[CAM_BUS_GENERATION]=
1720 xsoftc.bus_generation;
1721 cdm->pos.cookie.target = device->target;
1722 cdm->pos.generations[CAM_TARGET_GENERATION] =
1723 device->target->bus->generation;
1724 cdm->pos.cookie.device = device;
1725 cdm->pos.generations[CAM_DEV_GENERATION] =
1726 device->target->generation;
1727 cdm->status = CAM_DEV_MATCH_MORE;
1730 j = cdm->num_matches;
1732 cdm->matches[j].type = DEV_MATCH_DEVICE;
1733 cdm->matches[j].result.device_result.path_id =
1734 device->target->bus->path_id;
1735 cdm->matches[j].result.device_result.target_id =
1736 device->target->target_id;
1737 cdm->matches[j].result.device_result.target_lun =
1739 cdm->matches[j].result.device_result.protocol =
1741 bcopy(&device->inq_data,
1742 &cdm->matches[j].result.device_result.inq_data,
1743 sizeof(struct scsi_inquiry_data));
1744 bcopy(&device->ident_data,
1745 &cdm->matches[j].result.device_result.ident_data,
1746 sizeof(struct ata_params));
1748 /* Let the user know whether this device is unconfigured */
1749 if (device->flags & CAM_DEV_UNCONFIGURED)
1750 cdm->matches[j].result.device_result.flags =
1751 DEV_RESULT_UNCONFIGURED;
1753 cdm->matches[j].result.device_result.flags =
1758 * If the user isn't interested in peripherals, don't descend
1759 * the tree any further.
1761 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1765 * If there is a peripheral list generation recorded, make sure
1766 * it hasn't changed.
1769 mtx_lock(&bus->eb_mtx);
1770 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1771 && (cdm->pos.cookie.bus == bus)
1772 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1773 && (cdm->pos.cookie.target == device->target)
1774 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1775 && (cdm->pos.cookie.device == device)
1776 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1777 && (cdm->pos.cookie.periph != NULL)) {
1778 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1779 device->generation) {
1780 mtx_unlock(&bus->eb_mtx);
1782 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1785 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1789 mtx_unlock(&bus->eb_mtx);
1792 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1796 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1798 struct ccb_dev_match *cdm;
1799 dev_match_ret retval;
1801 cdm = (struct ccb_dev_match *)arg;
1803 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1805 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1806 cdm->status = CAM_DEV_MATCH_ERROR;
1811 * If the copy flag is set, copy this peripheral out.
1813 if (retval & DM_RET_COPY) {
1816 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1817 sizeof(struct dev_match_result));
1820 * If we don't have enough space to put in another
1821 * match result, save our position and tell the
1822 * user there are more devices to check.
1824 if (spaceleft < sizeof(struct dev_match_result)) {
1825 bzero(&cdm->pos, sizeof(cdm->pos));
1826 cdm->pos.position_type =
1827 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1828 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1831 cdm->pos.cookie.bus = periph->path->bus;
1832 cdm->pos.generations[CAM_BUS_GENERATION]=
1833 xsoftc.bus_generation;
1834 cdm->pos.cookie.target = periph->path->target;
1835 cdm->pos.generations[CAM_TARGET_GENERATION] =
1836 periph->path->bus->generation;
1837 cdm->pos.cookie.device = periph->path->device;
1838 cdm->pos.generations[CAM_DEV_GENERATION] =
1839 periph->path->target->generation;
1840 cdm->pos.cookie.periph = periph;
1841 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1842 periph->path->device->generation;
1843 cdm->status = CAM_DEV_MATCH_MORE;
1847 j = cdm->num_matches;
1849 cdm->matches[j].type = DEV_MATCH_PERIPH;
1850 cdm->matches[j].result.periph_result.path_id =
1851 periph->path->bus->path_id;
1852 cdm->matches[j].result.periph_result.target_id =
1853 periph->path->target->target_id;
1854 cdm->matches[j].result.periph_result.target_lun =
1855 periph->path->device->lun_id;
1856 cdm->matches[j].result.periph_result.unit_number =
1857 periph->unit_number;
1858 strncpy(cdm->matches[j].result.periph_result.periph_name,
1859 periph->periph_name, DEV_IDLEN);
1866 xptedtmatch(struct ccb_dev_match *cdm)
1871 cdm->num_matches = 0;
1874 * Check the bus list generation. If it has changed, the user
1875 * needs to reset everything and start over.
1878 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1879 && (cdm->pos.cookie.bus != NULL)) {
1880 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1881 xsoftc.bus_generation) {
1883 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1886 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1892 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1895 * If we get back 0, that means that we had to stop before fully
1896 * traversing the EDT. It also means that one of the subroutines
1897 * has set the status field to the proper value. If we get back 1,
1898 * we've fully traversed the EDT and copied out any matching entries.
1901 cdm->status = CAM_DEV_MATCH_LAST;
1907 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1909 struct cam_periph *periph;
1910 struct ccb_dev_match *cdm;
1912 cdm = (struct ccb_dev_match *)arg;
1915 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1916 && (cdm->pos.cookie.pdrv == pdrv)
1917 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1918 && (cdm->pos.cookie.periph != NULL)) {
1919 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1920 (*pdrv)->generation) {
1922 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1925 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1931 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1935 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1937 struct ccb_dev_match *cdm;
1938 dev_match_ret retval;
1940 cdm = (struct ccb_dev_match *)arg;
1942 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1944 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1945 cdm->status = CAM_DEV_MATCH_ERROR;
1950 * If the copy flag is set, copy this peripheral out.
1952 if (retval & DM_RET_COPY) {
1955 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1956 sizeof(struct dev_match_result));
1959 * If we don't have enough space to put in another
1960 * match result, save our position and tell the
1961 * user there are more devices to check.
1963 if (spaceleft < sizeof(struct dev_match_result)) {
1964 struct periph_driver **pdrv;
1967 bzero(&cdm->pos, sizeof(cdm->pos));
1968 cdm->pos.position_type =
1969 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1973 * This may look a bit non-sensical, but it is
1974 * actually quite logical. There are very few
1975 * peripheral drivers, and bloating every peripheral
1976 * structure with a pointer back to its parent
1977 * peripheral driver linker set entry would cost
1978 * more in the long run than doing this quick lookup.
1980 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1981 if (strcmp((*pdrv)->driver_name,
1982 periph->periph_name) == 0)
1986 if (*pdrv == NULL) {
1987 cdm->status = CAM_DEV_MATCH_ERROR;
1991 cdm->pos.cookie.pdrv = pdrv;
1993 * The periph generation slot does double duty, as
1994 * does the periph pointer slot. They are used for
1995 * both edt and pdrv lookups and positioning.
1997 cdm->pos.cookie.periph = periph;
1998 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1999 (*pdrv)->generation;
2000 cdm->status = CAM_DEV_MATCH_MORE;
2004 j = cdm->num_matches;
2006 cdm->matches[j].type = DEV_MATCH_PERIPH;
2007 cdm->matches[j].result.periph_result.path_id =
2008 periph->path->bus->path_id;
2011 * The transport layer peripheral doesn't have a target or
2014 if (periph->path->target)
2015 cdm->matches[j].result.periph_result.target_id =
2016 periph->path->target->target_id;
2018 cdm->matches[j].result.periph_result.target_id =
2019 CAM_TARGET_WILDCARD;
2021 if (periph->path->device)
2022 cdm->matches[j].result.periph_result.target_lun =
2023 periph->path->device->lun_id;
2025 cdm->matches[j].result.periph_result.target_lun =
2028 cdm->matches[j].result.periph_result.unit_number =
2029 periph->unit_number;
2030 strncpy(cdm->matches[j].result.periph_result.periph_name,
2031 periph->periph_name, DEV_IDLEN);
2038 xptperiphlistmatch(struct ccb_dev_match *cdm)
2042 cdm->num_matches = 0;
2045 * At this point in the edt traversal function, we check the bus
2046 * list generation to make sure that no busses have been added or
2047 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2048 * For the peripheral driver list traversal function, however, we
2049 * don't have to worry about new peripheral driver types coming or
2050 * going; they're in a linker set, and therefore can't change
2051 * without a recompile.
2054 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2055 && (cdm->pos.cookie.pdrv != NULL))
2056 ret = xptpdrvtraverse(
2057 (struct periph_driver **)cdm->pos.cookie.pdrv,
2058 xptplistpdrvfunc, cdm);
2060 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2063 * If we get back 0, that means that we had to stop before fully
2064 * traversing the peripheral driver tree. It also means that one of
2065 * the subroutines has set the status field to the proper value. If
2066 * we get back 1, we've fully traversed the EDT and copied out any
2070 cdm->status = CAM_DEV_MATCH_LAST;
2076 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2078 struct cam_eb *bus, *next_bus;
2086 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2094 for (; bus != NULL; bus = next_bus) {
2095 retval = tr_func(bus, arg);
2097 xpt_release_bus(bus);
2101 next_bus = TAILQ_NEXT(bus, links);
2103 next_bus->refcount++;
2105 xpt_release_bus(bus);
2111 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2112 xpt_targetfunc_t *tr_func, void *arg)
2114 struct cam_et *target, *next_target;
2119 target = start_target;
2121 mtx_lock(&bus->eb_mtx);
2122 target = TAILQ_FIRST(&bus->et_entries);
2123 if (target == NULL) {
2124 mtx_unlock(&bus->eb_mtx);
2128 mtx_unlock(&bus->eb_mtx);
2130 for (; target != NULL; target = next_target) {
2131 retval = tr_func(target, arg);
2133 xpt_release_target(target);
2136 mtx_lock(&bus->eb_mtx);
2137 next_target = TAILQ_NEXT(target, links);
2139 next_target->refcount++;
2140 mtx_unlock(&bus->eb_mtx);
2141 xpt_release_target(target);
2147 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2148 xpt_devicefunc_t *tr_func, void *arg)
2151 struct cam_ed *device, *next_device;
2157 device = start_device;
2159 mtx_lock(&bus->eb_mtx);
2160 device = TAILQ_FIRST(&target->ed_entries);
2161 if (device == NULL) {
2162 mtx_unlock(&bus->eb_mtx);
2166 mtx_unlock(&bus->eb_mtx);
2168 for (; device != NULL; device = next_device) {
2169 mtx_lock(&device->device_mtx);
2170 retval = tr_func(device, arg);
2171 mtx_unlock(&device->device_mtx);
2173 xpt_release_device(device);
2176 mtx_lock(&bus->eb_mtx);
2177 next_device = TAILQ_NEXT(device, links);
2179 next_device->refcount++;
2180 mtx_unlock(&bus->eb_mtx);
2181 xpt_release_device(device);
2187 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2188 xpt_periphfunc_t *tr_func, void *arg)
2191 struct cam_periph *periph, *next_periph;
2196 bus = device->target->bus;
2198 periph = start_periph;
2201 mtx_lock(&bus->eb_mtx);
2202 periph = SLIST_FIRST(&device->periphs);
2203 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2204 periph = SLIST_NEXT(periph, periph_links);
2205 if (periph == NULL) {
2206 mtx_unlock(&bus->eb_mtx);
2211 mtx_unlock(&bus->eb_mtx);
2214 for (; periph != NULL; periph = next_periph) {
2215 retval = tr_func(periph, arg);
2217 cam_periph_release_locked(periph);
2221 mtx_lock(&bus->eb_mtx);
2222 next_periph = SLIST_NEXT(periph, periph_links);
2223 while (next_periph != NULL &&
2224 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2225 next_periph = SLIST_NEXT(next_periph, periph_links);
2227 next_periph->refcount++;
2228 mtx_unlock(&bus->eb_mtx);
2230 cam_periph_release_locked(periph);
2236 xptpdrvtraverse(struct periph_driver **start_pdrv,
2237 xpt_pdrvfunc_t *tr_func, void *arg)
2239 struct periph_driver **pdrv;
2245 * We don't traverse the peripheral driver list like we do the
2246 * other lists, because it is a linker set, and therefore cannot be
2247 * changed during runtime. If the peripheral driver list is ever
2248 * re-done to be something other than a linker set (i.e. it can
2249 * change while the system is running), the list traversal should
2250 * be modified to work like the other traversal functions.
2252 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2253 *pdrv != NULL; pdrv++) {
2254 retval = tr_func(pdrv, arg);
2264 xptpdperiphtraverse(struct periph_driver **pdrv,
2265 struct cam_periph *start_periph,
2266 xpt_periphfunc_t *tr_func, void *arg)
2268 struct cam_periph *periph, *next_periph;
2274 periph = start_periph;
2277 periph = TAILQ_FIRST(&(*pdrv)->units);
2278 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2279 periph = TAILQ_NEXT(periph, unit_links);
2280 if (periph == NULL) {
2287 for (; periph != NULL; periph = next_periph) {
2288 cam_periph_lock(periph);
2289 retval = tr_func(periph, arg);
2290 cam_periph_unlock(periph);
2292 cam_periph_release(periph);
2296 next_periph = TAILQ_NEXT(periph, unit_links);
2297 while (next_periph != NULL &&
2298 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2299 next_periph = TAILQ_NEXT(next_periph, unit_links);
2301 next_periph->refcount++;
2303 cam_periph_release(periph);
2309 xptdefbusfunc(struct cam_eb *bus, void *arg)
2311 struct xpt_traverse_config *tr_config;
2313 tr_config = (struct xpt_traverse_config *)arg;
2315 if (tr_config->depth == XPT_DEPTH_BUS) {
2316 xpt_busfunc_t *tr_func;
2318 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2320 return(tr_func(bus, tr_config->tr_arg));
2322 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2326 xptdeftargetfunc(struct cam_et *target, void *arg)
2328 struct xpt_traverse_config *tr_config;
2330 tr_config = (struct xpt_traverse_config *)arg;
2332 if (tr_config->depth == XPT_DEPTH_TARGET) {
2333 xpt_targetfunc_t *tr_func;
2335 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2337 return(tr_func(target, tr_config->tr_arg));
2339 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2343 xptdefdevicefunc(struct cam_ed *device, void *arg)
2345 struct xpt_traverse_config *tr_config;
2347 tr_config = (struct xpt_traverse_config *)arg;
2349 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2350 xpt_devicefunc_t *tr_func;
2352 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2354 return(tr_func(device, tr_config->tr_arg));
2356 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2360 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2362 struct xpt_traverse_config *tr_config;
2363 xpt_periphfunc_t *tr_func;
2365 tr_config = (struct xpt_traverse_config *)arg;
2367 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2370 * Unlike the other default functions, we don't check for depth
2371 * here. The peripheral driver level is the last level in the EDT,
2372 * so if we're here, we should execute the function in question.
2374 return(tr_func(periph, tr_config->tr_arg));
2378 * Execute the given function for every bus in the EDT.
2381 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2383 struct xpt_traverse_config tr_config;
2385 tr_config.depth = XPT_DEPTH_BUS;
2386 tr_config.tr_func = tr_func;
2387 tr_config.tr_arg = arg;
2389 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2393 * Execute the given function for every device in the EDT.
2396 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2398 struct xpt_traverse_config tr_config;
2400 tr_config.depth = XPT_DEPTH_DEVICE;
2401 tr_config.tr_func = tr_func;
2402 tr_config.tr_arg = arg;
2404 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2408 xptsetasyncfunc(struct cam_ed *device, void *arg)
2410 struct cam_path path;
2411 struct ccb_getdev cgd;
2412 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2415 * Don't report unconfigured devices (Wildcard devs,
2416 * devices only for target mode, device instances
2417 * that have been invalidated but are waiting for
2418 * their last reference count to be released).
2420 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2423 xpt_compile_path(&path,
2425 device->target->bus->path_id,
2426 device->target->target_id,
2428 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2429 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2430 xpt_action((union ccb *)&cgd);
2431 csa->callback(csa->callback_arg,
2434 xpt_release_path(&path);
2440 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2442 struct cam_path path;
2443 struct ccb_pathinq cpi;
2444 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2446 xpt_compile_path(&path, /*periph*/NULL,
2448 CAM_TARGET_WILDCARD,
2450 xpt_path_lock(&path);
2451 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2452 cpi.ccb_h.func_code = XPT_PATH_INQ;
2453 xpt_action((union ccb *)&cpi);
2454 csa->callback(csa->callback_arg,
2457 xpt_path_unlock(&path);
2458 xpt_release_path(&path);
2464 xpt_action(union ccb *start_ccb)
2467 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2468 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2469 xpt_action_name(start_ccb->ccb_h.func_code)));
2471 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2472 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2476 xpt_action_default(union ccb *start_ccb)
2478 struct cam_path *path;
2479 struct cam_sim *sim;
2482 path = start_ccb->ccb_h.path;
2483 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2484 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2485 xpt_action_name(start_ccb->ccb_h.func_code)));
2487 switch (start_ccb->ccb_h.func_code) {
2490 struct cam_ed *device;
2493 * For the sake of compatibility with SCSI-1
2494 * devices that may not understand the identify
2495 * message, we include lun information in the
2496 * second byte of all commands. SCSI-1 specifies
2497 * that luns are a 3 bit value and reserves only 3
2498 * bits for lun information in the CDB. Later
2499 * revisions of the SCSI spec allow for more than 8
2500 * luns, but have deprecated lun information in the
2501 * CDB. So, if the lun won't fit, we must omit.
2503 * Also be aware that during initial probing for devices,
2504 * the inquiry information is unknown but initialized to 0.
2505 * This means that this code will be exercised while probing
2506 * devices with an ANSI revision greater than 2.
2508 device = path->device;
2509 if (device->protocol_version <= SCSI_REV_2
2510 && start_ccb->ccb_h.target_lun < 8
2511 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2513 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2514 start_ccb->ccb_h.target_lun << 5;
2516 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2520 case XPT_CONT_TARGET_IO:
2521 start_ccb->csio.sense_resid = 0;
2522 start_ccb->csio.resid = 0;
2525 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2526 start_ccb->ataio.resid = 0;
2529 if (start_ccb->ccb_h.func_code == XPT_NVME_IO)
2530 start_ccb->nvmeio.resid = 0;
2536 struct cam_devq *devq;
2538 devq = path->bus->sim->devq;
2539 mtx_lock(&devq->send_mtx);
2540 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2541 if (xpt_schedule_devq(devq, path->device) != 0)
2543 mtx_unlock(&devq->send_mtx);
2546 case XPT_CALC_GEOMETRY:
2547 /* Filter out garbage */
2548 if (start_ccb->ccg.block_size == 0
2549 || start_ccb->ccg.volume_size == 0) {
2550 start_ccb->ccg.cylinders = 0;
2551 start_ccb->ccg.heads = 0;
2552 start_ccb->ccg.secs_per_track = 0;
2553 start_ccb->ccb_h.status = CAM_REQ_CMP;
2556 #if defined(PC98) || defined(__sparc64__)
2558 * In a PC-98 system, geometry translation depens on
2559 * the "real" device geometry obtained from mode page 4.
2560 * SCSI geometry translation is performed in the
2561 * initialization routine of the SCSI BIOS and the result
2562 * stored in host memory. If the translation is available
2563 * in host memory, use it. If not, rely on the default
2564 * translation the device driver performs.
2565 * For sparc64, we may need adjust the geometry of large
2566 * disks in order to fit the limitations of the 16-bit
2567 * fields of the VTOC8 disk label.
2569 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2570 start_ccb->ccb_h.status = CAM_REQ_CMP;
2577 union ccb* abort_ccb;
2579 abort_ccb = start_ccb->cab.abort_ccb;
2580 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2582 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2583 struct cam_ccbq *ccbq;
2584 struct cam_ed *device;
2586 device = abort_ccb->ccb_h.path->device;
2587 ccbq = &device->ccbq;
2588 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2589 abort_ccb->ccb_h.status =
2590 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2591 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2592 xpt_done(abort_ccb);
2593 start_ccb->ccb_h.status = CAM_REQ_CMP;
2596 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2597 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2599 * We've caught this ccb en route to
2600 * the SIM. Flag it for abort and the
2601 * SIM will do so just before starting
2602 * real work on the CCB.
2604 abort_ccb->ccb_h.status =
2605 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2606 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2607 start_ccb->ccb_h.status = CAM_REQ_CMP;
2611 if (XPT_FC_IS_QUEUED(abort_ccb)
2612 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2614 * It's already completed but waiting
2615 * for our SWI to get to it.
2617 start_ccb->ccb_h.status = CAM_UA_ABORT;
2621 * If we weren't able to take care of the abort request
2622 * in the XPT, pass the request down to the SIM for processing.
2626 case XPT_ACCEPT_TARGET_IO:
2628 case XPT_IMMED_NOTIFY:
2629 case XPT_NOTIFY_ACK:
2631 case XPT_IMMEDIATE_NOTIFY:
2632 case XPT_NOTIFY_ACKNOWLEDGE:
2633 case XPT_GET_SIM_KNOB_OLD:
2634 case XPT_GET_SIM_KNOB:
2635 case XPT_SET_SIM_KNOB:
2636 case XPT_GET_TRAN_SETTINGS:
2637 case XPT_SET_TRAN_SETTINGS:
2640 sim = path->bus->sim;
2641 lock = (mtx_owned(sim->mtx) == 0);
2644 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2645 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2646 (*(sim->sim_action))(sim, start_ccb);
2647 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2648 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2650 CAM_SIM_UNLOCK(sim);
2652 case XPT_PATH_STATS:
2653 start_ccb->cpis.last_reset = path->bus->last_reset;
2654 start_ccb->ccb_h.status = CAM_REQ_CMP;
2661 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2662 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2664 struct ccb_getdev *cgd;
2666 cgd = &start_ccb->cgd;
2667 cgd->protocol = dev->protocol;
2668 cgd->inq_data = dev->inq_data;
2669 cgd->ident_data = dev->ident_data;
2670 cgd->inq_flags = dev->inq_flags;
2671 cgd->nvme_data = dev->nvme_data;
2672 cgd->nvme_cdata = dev->nvme_cdata;
2673 cgd->ccb_h.status = CAM_REQ_CMP;
2674 cgd->serial_num_len = dev->serial_num_len;
2675 if ((dev->serial_num_len > 0)
2676 && (dev->serial_num != NULL))
2677 bcopy(dev->serial_num, cgd->serial_num,
2678 dev->serial_num_len);
2682 case XPT_GDEV_STATS:
2687 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2688 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2690 struct ccb_getdevstats *cgds;
2693 struct cam_devq *devq;
2695 cgds = &start_ccb->cgds;
2698 devq = bus->sim->devq;
2699 mtx_lock(&devq->send_mtx);
2700 cgds->dev_openings = dev->ccbq.dev_openings;
2701 cgds->dev_active = dev->ccbq.dev_active;
2702 cgds->allocated = dev->ccbq.allocated;
2703 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2704 cgds->held = cgds->allocated - cgds->dev_active -
2706 cgds->last_reset = tar->last_reset;
2707 cgds->maxtags = dev->maxtags;
2708 cgds->mintags = dev->mintags;
2709 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2710 cgds->last_reset = bus->last_reset;
2711 mtx_unlock(&devq->send_mtx);
2712 cgds->ccb_h.status = CAM_REQ_CMP;
2718 struct cam_periph *nperiph;
2719 struct periph_list *periph_head;
2720 struct ccb_getdevlist *cgdl;
2722 struct cam_ed *device;
2729 * Don't want anyone mucking with our data.
2731 device = path->device;
2732 periph_head = &device->periphs;
2733 cgdl = &start_ccb->cgdl;
2736 * Check and see if the list has changed since the user
2737 * last requested a list member. If so, tell them that the
2738 * list has changed, and therefore they need to start over
2739 * from the beginning.
2741 if ((cgdl->index != 0) &&
2742 (cgdl->generation != device->generation)) {
2743 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2748 * Traverse the list of peripherals and attempt to find
2749 * the requested peripheral.
2751 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2752 (nperiph != NULL) && (i <= cgdl->index);
2753 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2754 if (i == cgdl->index) {
2755 strncpy(cgdl->periph_name,
2756 nperiph->periph_name,
2758 cgdl->unit_number = nperiph->unit_number;
2763 cgdl->status = CAM_GDEVLIST_ERROR;
2767 if (nperiph == NULL)
2768 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2770 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2773 cgdl->generation = device->generation;
2775 cgdl->ccb_h.status = CAM_REQ_CMP;
2780 dev_pos_type position_type;
2781 struct ccb_dev_match *cdm;
2783 cdm = &start_ccb->cdm;
2786 * There are two ways of getting at information in the EDT.
2787 * The first way is via the primary EDT tree. It starts
2788 * with a list of busses, then a list of targets on a bus,
2789 * then devices/luns on a target, and then peripherals on a
2790 * device/lun. The "other" way is by the peripheral driver
2791 * lists. The peripheral driver lists are organized by
2792 * peripheral driver. (obviously) So it makes sense to
2793 * use the peripheral driver list if the user is looking
2794 * for something like "da1", or all "da" devices. If the
2795 * user is looking for something on a particular bus/target
2796 * or lun, it's generally better to go through the EDT tree.
2799 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2800 position_type = cdm->pos.position_type;
2804 position_type = CAM_DEV_POS_NONE;
2806 for (i = 0; i < cdm->num_patterns; i++) {
2807 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2808 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2809 position_type = CAM_DEV_POS_EDT;
2814 if (cdm->num_patterns == 0)
2815 position_type = CAM_DEV_POS_EDT;
2816 else if (position_type == CAM_DEV_POS_NONE)
2817 position_type = CAM_DEV_POS_PDRV;
2820 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2821 case CAM_DEV_POS_EDT:
2824 case CAM_DEV_POS_PDRV:
2825 xptperiphlistmatch(cdm);
2828 cdm->status = CAM_DEV_MATCH_ERROR;
2832 if (cdm->status == CAM_DEV_MATCH_ERROR)
2833 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2835 start_ccb->ccb_h.status = CAM_REQ_CMP;
2841 struct ccb_setasync *csa;
2842 struct async_node *cur_entry;
2843 struct async_list *async_head;
2846 csa = &start_ccb->csa;
2847 added = csa->event_enable;
2848 async_head = &path->device->asyncs;
2851 * If there is already an entry for us, simply
2854 cur_entry = SLIST_FIRST(async_head);
2855 while (cur_entry != NULL) {
2856 if ((cur_entry->callback_arg == csa->callback_arg)
2857 && (cur_entry->callback == csa->callback))
2859 cur_entry = SLIST_NEXT(cur_entry, links);
2862 if (cur_entry != NULL) {
2864 * If the request has no flags set,
2867 added &= ~cur_entry->event_enable;
2868 if (csa->event_enable == 0) {
2869 SLIST_REMOVE(async_head, cur_entry,
2871 xpt_release_device(path->device);
2872 free(cur_entry, M_CAMXPT);
2874 cur_entry->event_enable = csa->event_enable;
2876 csa->event_enable = added;
2878 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2880 if (cur_entry == NULL) {
2881 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2884 cur_entry->event_enable = csa->event_enable;
2885 cur_entry->event_lock =
2886 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2887 cur_entry->callback_arg = csa->callback_arg;
2888 cur_entry->callback = csa->callback;
2889 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2890 xpt_acquire_device(path->device);
2892 start_ccb->ccb_h.status = CAM_REQ_CMP;
2897 struct ccb_relsim *crs;
2900 crs = &start_ccb->crs;
2904 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2908 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2910 /* Don't ever go below one opening */
2911 if (crs->openings > 0) {
2912 xpt_dev_ccbq_resize(path, crs->openings);
2915 "number of openings is now %d\n",
2921 mtx_lock(&dev->sim->devq->send_mtx);
2922 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2924 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2927 * Just extend the old timeout and decrement
2928 * the freeze count so that a single timeout
2929 * is sufficient for releasing the queue.
2931 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2932 callout_stop(&dev->callout);
2935 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2938 callout_reset_sbt(&dev->callout,
2939 SBT_1MS * crs->release_timeout, 0,
2940 xpt_release_devq_timeout, dev, 0);
2942 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2946 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2948 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2950 * Decrement the freeze count so that a single
2951 * completion is still sufficient to unfreeze
2954 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2957 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2958 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2962 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2964 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2965 || (dev->ccbq.dev_active == 0)) {
2967 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2970 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2971 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2974 mtx_unlock(&dev->sim->devq->send_mtx);
2976 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2977 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2978 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2979 start_ccb->ccb_h.status = CAM_REQ_CMP;
2983 struct cam_path *oldpath;
2985 /* Check that all request bits are supported. */
2986 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2987 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2991 cam_dflags = CAM_DEBUG_NONE;
2992 if (cam_dpath != NULL) {
2993 oldpath = cam_dpath;
2995 xpt_free_path(oldpath);
2997 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2998 if (xpt_create_path(&cam_dpath, NULL,
2999 start_ccb->ccb_h.path_id,
3000 start_ccb->ccb_h.target_id,
3001 start_ccb->ccb_h.target_lun) !=
3003 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3005 cam_dflags = start_ccb->cdbg.flags;
3006 start_ccb->ccb_h.status = CAM_REQ_CMP;
3007 xpt_print(cam_dpath, "debugging flags now %x\n",
3011 start_ccb->ccb_h.status = CAM_REQ_CMP;
3015 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3016 xpt_freeze_devq(path, 1);
3017 start_ccb->ccb_h.status = CAM_REQ_CMP;
3019 case XPT_REPROBE_LUN:
3020 xpt_async(AC_INQ_CHANGED, path, NULL);
3021 start_ccb->ccb_h.status = CAM_REQ_CMP;
3022 xpt_done(start_ccb);
3029 xpt_print_path(start_ccb->ccb_h.path);
3030 printf("%s: CCB type %#x %s not supported\n", __func__,
3031 start_ccb->ccb_h.func_code,
3032 xpt_action_name(start_ccb->ccb_h.func_code));
3033 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3034 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3035 xpt_done(start_ccb);
3039 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3040 ("xpt_action_default: func= %#x %s status %#x\n",
3041 start_ccb->ccb_h.func_code,
3042 xpt_action_name(start_ccb->ccb_h.func_code),
3043 start_ccb->ccb_h.status));
3047 xpt_polled_action(union ccb *start_ccb)
3050 struct cam_sim *sim;
3051 struct cam_devq *devq;
3054 timeout = start_ccb->ccb_h.timeout * 10;
3055 sim = start_ccb->ccb_h.path->bus->sim;
3057 dev = start_ccb->ccb_h.path->device;
3059 mtx_unlock(&dev->device_mtx);
3062 * Steal an opening so that no other queued requests
3063 * can get it before us while we simulate interrupts.
3065 mtx_lock(&devq->send_mtx);
3066 dev->ccbq.dev_openings--;
3067 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3069 mtx_unlock(&devq->send_mtx);
3072 (*(sim->sim_poll))(sim);
3073 CAM_SIM_UNLOCK(sim);
3075 mtx_lock(&devq->send_mtx);
3077 dev->ccbq.dev_openings++;
3078 mtx_unlock(&devq->send_mtx);
3081 xpt_action(start_ccb);
3082 while(--timeout > 0) {
3084 (*(sim->sim_poll))(sim);
3085 CAM_SIM_UNLOCK(sim);
3087 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3094 * XXX Is it worth adding a sim_timeout entry
3095 * point so we can attempt recovery? If
3096 * this is only used for dumps, I don't think
3099 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3102 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3105 mtx_lock(&dev->device_mtx);
3109 * Schedule a peripheral driver to receive a ccb when its
3110 * target device has space for more transactions.
3113 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3116 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3117 cam_periph_assert(periph, MA_OWNED);
3118 if (new_priority < periph->scheduled_priority) {
3119 periph->scheduled_priority = new_priority;
3120 xpt_run_allocq(periph, 0);
3126 * Schedule a device to run on a given queue.
3127 * If the device was inserted as a new entry on the queue,
3128 * return 1 meaning the device queue should be run. If we
3129 * were already queued, implying someone else has already
3130 * started the queue, return 0 so the caller doesn't attempt
3134 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3135 u_int32_t new_priority)
3138 u_int32_t old_priority;
3140 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3142 old_priority = pinfo->priority;
3145 * Are we already queued?
3147 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3148 /* Simply reorder based on new priority */
3149 if (new_priority < old_priority) {
3150 camq_change_priority(queue, pinfo->index,
3152 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3153 ("changed priority to %d\n",
3159 /* New entry on the queue */
3160 if (new_priority < old_priority)
3161 pinfo->priority = new_priority;
3163 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3164 ("Inserting onto queue\n"));
3165 pinfo->generation = ++queue->generation;
3166 camq_insert(queue, pinfo);
3173 xpt_run_allocq_task(void *context, int pending)
3175 struct cam_periph *periph = context;
3177 cam_periph_lock(periph);
3178 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3179 xpt_run_allocq(periph, 1);
3180 cam_periph_unlock(periph);
3181 cam_periph_release(periph);
3185 xpt_run_allocq(struct cam_periph *periph, int sleep)
3187 struct cam_ed *device;
3191 cam_periph_assert(periph, MA_OWNED);
3192 if (periph->periph_allocating)
3194 periph->periph_allocating = 1;
3195 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3196 device = periph->path->device;
3199 while ((prio = min(periph->scheduled_priority,
3200 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3201 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3202 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3205 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3207 ccb = xpt_get_ccb(periph);
3210 if (periph->flags & CAM_PERIPH_RUN_TASK)
3212 cam_periph_doacquire(periph);
3213 periph->flags |= CAM_PERIPH_RUN_TASK;
3214 taskqueue_enqueue(xsoftc.xpt_taskq,
3215 &periph->periph_run_task);
3218 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3219 if (prio == periph->immediate_priority) {
3220 periph->immediate_priority = CAM_PRIORITY_NONE;
3221 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3222 ("waking cam_periph_getccb()\n"));
3223 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3225 wakeup(&periph->ccb_list);
3227 periph->scheduled_priority = CAM_PRIORITY_NONE;
3228 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3229 ("calling periph_start()\n"));
3230 periph->periph_start(periph, ccb);
3235 xpt_release_ccb(ccb);
3236 periph->periph_allocating = 0;
3240 xpt_run_devq(struct cam_devq *devq)
3244 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3246 devq->send_queue.qfrozen_cnt++;
3247 while ((devq->send_queue.entries > 0)
3248 && (devq->send_openings > 0)
3249 && (devq->send_queue.qfrozen_cnt <= 1)) {
3250 struct cam_ed *device;
3251 union ccb *work_ccb;
3252 struct cam_sim *sim;
3253 struct xpt_proto *proto;
3255 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3257 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3258 ("running device %p\n", device));
3260 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3261 if (work_ccb == NULL) {
3262 printf("device on run queue with no ccbs???\n");
3266 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3268 mtx_lock(&xsoftc.xpt_highpower_lock);
3269 if (xsoftc.num_highpower <= 0) {
3271 * We got a high power command, but we
3272 * don't have any available slots. Freeze
3273 * the device queue until we have a slot
3276 xpt_freeze_devq_device(device, 1);
3277 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3280 mtx_unlock(&xsoftc.xpt_highpower_lock);
3284 * Consume a high power slot while
3287 xsoftc.num_highpower--;
3289 mtx_unlock(&xsoftc.xpt_highpower_lock);
3291 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3292 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3293 devq->send_openings--;
3294 devq->send_active++;
3295 xpt_schedule_devq(devq, device);
3296 mtx_unlock(&devq->send_mtx);
3298 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3300 * The client wants to freeze the queue
3301 * after this CCB is sent.
3303 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3306 /* In Target mode, the peripheral driver knows best... */
3307 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3308 if ((device->inq_flags & SID_CmdQue) != 0
3309 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3310 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3313 * Clear this in case of a retried CCB that
3314 * failed due to a rejected tag.
3316 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3319 KASSERT(device == work_ccb->ccb_h.path->device,
3320 ("device (%p) / path->device (%p) mismatch",
3321 device, work_ccb->ccb_h.path->device));
3322 proto = xpt_proto_find(device->protocol);
3323 if (proto && proto->ops->debug_out)
3324 proto->ops->debug_out(work_ccb);
3327 * Device queues can be shared among multiple SIM instances
3328 * that reside on different busses. Use the SIM from the
3329 * queued device, rather than the one from the calling bus.
3332 lock = (mtx_owned(sim->mtx) == 0);
3335 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3336 (*(sim->sim_action))(sim, work_ccb);
3338 CAM_SIM_UNLOCK(sim);
3339 mtx_lock(&devq->send_mtx);
3341 devq->send_queue.qfrozen_cnt--;
3345 * This function merges stuff from the slave ccb into the master ccb, while
3346 * keeping important fields in the master ccb constant.
3349 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3353 * Pull fields that are valid for peripheral drivers to set
3354 * into the master CCB along with the CCB "payload".
3356 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3357 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3358 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3359 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3360 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3361 sizeof(union ccb) - sizeof(struct ccb_hdr));
3365 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3366 u_int32_t priority, u_int32_t flags)
3369 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3370 ccb_h->pinfo.priority = priority;
3372 ccb_h->path_id = path->bus->path_id;
3374 ccb_h->target_id = path->target->target_id;
3376 ccb_h->target_id = CAM_TARGET_WILDCARD;
3378 ccb_h->target_lun = path->device->lun_id;
3379 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3381 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3383 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3384 ccb_h->flags = flags;
3389 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3391 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3394 /* Path manipulation functions */
3396 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3397 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3399 struct cam_path *path;
3402 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3405 status = CAM_RESRC_UNAVAIL;
3408 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3409 if (status != CAM_REQ_CMP) {
3410 free(path, M_CAMPATH);
3413 *new_path_ptr = path;
3418 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3419 struct cam_periph *periph, path_id_t path_id,
3420 target_id_t target_id, lun_id_t lun_id)
3423 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3428 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3429 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3432 struct cam_et *target;
3433 struct cam_ed *device;
3436 status = CAM_REQ_CMP; /* Completed without error */
3437 target = NULL; /* Wildcarded */
3438 device = NULL; /* Wildcarded */
3441 * We will potentially modify the EDT, so block interrupts
3442 * that may attempt to create cam paths.
3444 bus = xpt_find_bus(path_id);
3446 status = CAM_PATH_INVALID;
3449 mtx_lock(&bus->eb_mtx);
3450 target = xpt_find_target(bus, target_id);
3451 if (target == NULL) {
3453 struct cam_et *new_target;
3455 new_target = xpt_alloc_target(bus, target_id);
3456 if (new_target == NULL) {
3457 status = CAM_RESRC_UNAVAIL;
3459 target = new_target;
3463 if (target != NULL) {
3464 device = xpt_find_device(target, lun_id);
3465 if (device == NULL) {
3467 struct cam_ed *new_device;
3470 (*(bus->xport->ops->alloc_device))(bus,
3473 if (new_device == NULL) {
3474 status = CAM_RESRC_UNAVAIL;
3476 device = new_device;
3480 mtx_unlock(&bus->eb_mtx);
3484 * Only touch the user's data if we are successful.
3486 if (status == CAM_REQ_CMP) {
3487 new_path->periph = perph;
3488 new_path->bus = bus;
3489 new_path->target = target;
3490 new_path->device = device;
3491 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3494 xpt_release_device(device);
3496 xpt_release_target(target);
3498 xpt_release_bus(bus);
3504 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3506 struct cam_path *new_path;
3508 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3509 if (new_path == NULL)
3510 return(CAM_RESRC_UNAVAIL);
3511 xpt_copy_path(new_path, path);
3512 *new_path_ptr = new_path;
3513 return (CAM_REQ_CMP);
3517 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3521 if (path->bus != NULL)
3522 xpt_acquire_bus(path->bus);
3523 if (path->target != NULL)
3524 xpt_acquire_target(path->target);
3525 if (path->device != NULL)
3526 xpt_acquire_device(path->device);
3530 xpt_release_path(struct cam_path *path)
3532 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3533 if (path->device != NULL) {
3534 xpt_release_device(path->device);
3535 path->device = NULL;
3537 if (path->target != NULL) {
3538 xpt_release_target(path->target);
3539 path->target = NULL;
3541 if (path->bus != NULL) {
3542 xpt_release_bus(path->bus);
3548 xpt_free_path(struct cam_path *path)
3551 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3552 xpt_release_path(path);
3553 free(path, M_CAMPATH);
3557 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3558 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3564 *bus_ref = path->bus->refcount;
3570 *periph_ref = path->periph->refcount;
3577 *target_ref = path->target->refcount;
3583 *device_ref = path->device->refcount;
3590 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3591 * in path1, 2 for match with wildcards in path2.
3594 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3598 if (path1->bus != path2->bus) {
3599 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3601 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3606 if (path1->target != path2->target) {
3607 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3610 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3615 if (path1->device != path2->device) {
3616 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3619 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3628 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3632 if (path->bus != dev->target->bus) {
3633 if (path->bus->path_id == CAM_BUS_WILDCARD)
3635 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3640 if (path->target != dev->target) {
3641 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3644 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3649 if (path->device != dev) {
3650 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3653 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3662 xpt_print_path(struct cam_path *path)
3666 printf("(nopath): ");
3668 if (path->periph != NULL)
3669 printf("(%s%d:", path->periph->periph_name,
3670 path->periph->unit_number);
3672 printf("(noperiph:");
3674 if (path->bus != NULL)
3675 printf("%s%d:%d:", path->bus->sim->sim_name,
3676 path->bus->sim->unit_number,
3677 path->bus->sim->bus_id);
3681 if (path->target != NULL)
3682 printf("%d:", path->target->target_id);
3686 if (path->device != NULL)
3687 printf("%jx): ", (uintmax_t)path->device->lun_id);
3694 xpt_print_device(struct cam_ed *device)
3698 printf("(nopath): ");
3700 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3701 device->sim->unit_number,
3702 device->sim->bus_id,
3703 device->target->target_id,
3704 (uintmax_t)device->lun_id);
3709 xpt_print(struct cam_path *path, const char *fmt, ...)
3712 xpt_print_path(path);
3719 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3723 sbuf_new(&sb, str, str_len, 0);
3726 sbuf_printf(&sb, "(nopath): ");
3728 if (path->periph != NULL)
3729 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3730 path->periph->unit_number);
3732 sbuf_printf(&sb, "(noperiph:");
3734 if (path->bus != NULL)
3735 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3736 path->bus->sim->unit_number,
3737 path->bus->sim->bus_id);
3739 sbuf_printf(&sb, "nobus:");
3741 if (path->target != NULL)
3742 sbuf_printf(&sb, "%d:", path->target->target_id);
3744 sbuf_printf(&sb, "X:");
3746 if (path->device != NULL)
3747 sbuf_printf(&sb, "%jx): ",
3748 (uintmax_t)path->device->lun_id);
3750 sbuf_printf(&sb, "X): ");
3754 return(sbuf_len(&sb));
3758 xpt_path_path_id(struct cam_path *path)
3760 return(path->bus->path_id);
3764 xpt_path_target_id(struct cam_path *path)
3766 if (path->target != NULL)
3767 return (path->target->target_id);
3769 return (CAM_TARGET_WILDCARD);
3773 xpt_path_lun_id(struct cam_path *path)
3775 if (path->device != NULL)
3776 return (path->device->lun_id);
3778 return (CAM_LUN_WILDCARD);
3782 xpt_path_sim(struct cam_path *path)
3785 return (path->bus->sim);
3789 xpt_path_periph(struct cam_path *path)
3792 return (path->periph);
3796 * Release a CAM control block for the caller. Remit the cost of the structure
3797 * to the device referenced by the path. If the this device had no 'credits'
3798 * and peripheral drivers have registered async callbacks for this notification
3802 xpt_release_ccb(union ccb *free_ccb)
3804 struct cam_ed *device;
3805 struct cam_periph *periph;
3807 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3808 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3809 device = free_ccb->ccb_h.path->device;
3810 periph = free_ccb->ccb_h.path->periph;
3812 xpt_free_ccb(free_ccb);
3813 periph->periph_allocated--;
3814 cam_ccbq_release_opening(&device->ccbq);
3815 xpt_run_allocq(periph, 0);
3818 /* Functions accessed by SIM drivers */
3820 static struct xpt_xport_ops xport_default_ops = {
3821 .alloc_device = xpt_alloc_device_default,
3822 .action = xpt_action_default,
3823 .async = xpt_dev_async_default,
3825 static struct xpt_xport xport_default = {
3826 .xport = XPORT_UNKNOWN,
3828 .ops = &xport_default_ops,
3831 CAM_XPT_XPORT(xport_default);
3834 * A sim structure, listing the SIM entry points and instance
3835 * identification info is passed to xpt_bus_register to hook the SIM
3836 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3837 * for this new bus and places it in the array of busses and assigns
3838 * it a path_id. The path_id may be influenced by "hard wiring"
3839 * information specified by the user. Once interrupt services are
3840 * available, the bus will be probed.
3843 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3845 struct cam_eb *new_bus;
3846 struct cam_eb *old_bus;
3847 struct ccb_pathinq cpi;
3848 struct cam_path *path;
3851 mtx_assert(sim->mtx, MA_OWNED);
3854 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3855 M_CAMXPT, M_NOWAIT|M_ZERO);
3856 if (new_bus == NULL) {
3857 /* Couldn't satisfy request */
3858 return (CAM_RESRC_UNAVAIL);
3861 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3862 TAILQ_INIT(&new_bus->et_entries);
3865 timevalclear(&new_bus->last_reset);
3867 new_bus->refcount = 1; /* Held until a bus_deregister event */
3868 new_bus->generation = 0;
3871 sim->path_id = new_bus->path_id =
3872 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3873 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3874 while (old_bus != NULL
3875 && old_bus->path_id < new_bus->path_id)
3876 old_bus = TAILQ_NEXT(old_bus, links);
3877 if (old_bus != NULL)
3878 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3880 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3881 xsoftc.bus_generation++;
3885 * Set a default transport so that a PATH_INQ can be issued to
3886 * the SIM. This will then allow for probing and attaching of
3887 * a more appropriate transport.
3889 new_bus->xport = &xport_default;
3891 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3892 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3893 if (status != CAM_REQ_CMP) {
3894 xpt_release_bus(new_bus);
3895 free(path, M_CAMXPT);
3896 return (CAM_RESRC_UNAVAIL);
3899 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3900 cpi.ccb_h.func_code = XPT_PATH_INQ;
3901 xpt_action((union ccb *)&cpi);
3903 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3904 struct xpt_xport **xpt;
3906 SET_FOREACH(xpt, cam_xpt_xport_set) {
3907 if ((*xpt)->xport == cpi.transport) {
3908 new_bus->xport = *xpt;
3912 if (new_bus->xport == NULL) {
3913 xpt_print_path(path);
3914 printf("No transport found for %d\n", cpi.transport);
3915 xpt_release_bus(new_bus);
3916 free(path, M_CAMXPT);
3917 return (CAM_RESRC_UNAVAIL);
3921 /* Notify interested parties */
3922 if (sim->path_id != CAM_XPT_PATH_ID) {
3924 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3925 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3926 union ccb *scan_ccb;
3928 /* Initiate bus rescan. */
3929 scan_ccb = xpt_alloc_ccb_nowait();
3930 if (scan_ccb != NULL) {
3931 scan_ccb->ccb_h.path = path;
3932 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3933 scan_ccb->crcn.flags = 0;
3934 xpt_rescan(scan_ccb);
3937 "Can't allocate CCB to scan bus\n");
3938 xpt_free_path(path);
3941 xpt_free_path(path);
3943 xpt_free_path(path);
3944 return (CAM_SUCCESS);
3948 xpt_bus_deregister(path_id_t pathid)
3950 struct cam_path bus_path;
3953 status = xpt_compile_path(&bus_path, NULL, pathid,
3954 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3955 if (status != CAM_REQ_CMP)
3958 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3959 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3961 /* Release the reference count held while registered. */
3962 xpt_release_bus(bus_path.bus);
3963 xpt_release_path(&bus_path);
3965 return (CAM_REQ_CMP);
3969 xptnextfreepathid(void)
3975 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3977 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3979 /* Find an unoccupied pathid */
3980 while (bus != NULL && bus->path_id <= pathid) {
3981 if (bus->path_id == pathid)
3983 bus = TAILQ_NEXT(bus, links);
3987 * Ensure that this pathid is not reserved for
3988 * a bus that may be registered in the future.
3990 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3992 /* Start the search over */
3999 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4006 pathid = CAM_XPT_PATH_ID;
4007 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4008 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4011 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4012 if (strcmp(dname, "scbus")) {
4013 /* Avoid a bit of foot shooting. */
4016 if (dunit < 0) /* unwired?! */
4018 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4019 if (sim_bus == val) {
4023 } else if (sim_bus == 0) {
4024 /* Unspecified matches bus 0 */
4028 printf("Ambiguous scbus configuration for %s%d "
4029 "bus %d, cannot wire down. The kernel "
4030 "config entry for scbus%d should "
4031 "specify a controller bus.\n"
4032 "Scbus will be assigned dynamically.\n",
4033 sim_name, sim_unit, sim_bus, dunit);
4038 if (pathid == CAM_XPT_PATH_ID)
4039 pathid = xptnextfreepathid();
4044 xpt_async_string(u_int32_t async_code)
4047 switch (async_code) {
4048 case AC_BUS_RESET: return ("AC_BUS_RESET");
4049 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4050 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4051 case AC_SENT_BDR: return ("AC_SENT_BDR");
4052 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4053 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4054 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4055 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4056 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4057 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4058 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4059 case AC_CONTRACT: return ("AC_CONTRACT");
4060 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4061 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4063 return ("AC_UNKNOWN");
4067 xpt_async_size(u_int32_t async_code)
4070 switch (async_code) {
4071 case AC_BUS_RESET: return (0);
4072 case AC_UNSOL_RESEL: return (0);
4073 case AC_SCSI_AEN: return (0);
4074 case AC_SENT_BDR: return (0);
4075 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4076 case AC_PATH_DEREGISTERED: return (0);
4077 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4078 case AC_LOST_DEVICE: return (0);
4079 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4080 case AC_INQ_CHANGED: return (0);
4081 case AC_GETDEV_CHANGED: return (0);
4082 case AC_CONTRACT: return (sizeof(struct ac_contract));
4083 case AC_ADVINFO_CHANGED: return (-1);
4084 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4090 xpt_async_process_dev(struct cam_ed *device, void *arg)
4092 union ccb *ccb = arg;
4093 struct cam_path *path = ccb->ccb_h.path;
4094 void *async_arg = ccb->casync.async_arg_ptr;
4095 u_int32_t async_code = ccb->casync.async_code;
4098 if (path->device != device
4099 && path->device->lun_id != CAM_LUN_WILDCARD
4100 && device->lun_id != CAM_LUN_WILDCARD)
4104 * The async callback could free the device.
4105 * If it is a broadcast async, it doesn't hold
4106 * device reference, so take our own reference.
4108 xpt_acquire_device(device);
4111 * If async for specific device is to be delivered to
4112 * the wildcard client, take the specific device lock.
4113 * XXX: We may need a way for client to specify it.
4115 if ((device->lun_id == CAM_LUN_WILDCARD &&
4116 path->device->lun_id != CAM_LUN_WILDCARD) ||
4117 (device->target->target_id == CAM_TARGET_WILDCARD &&
4118 path->target->target_id != CAM_TARGET_WILDCARD) ||
4119 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4120 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4121 mtx_unlock(&device->device_mtx);
4122 xpt_path_lock(path);
4127 (*(device->target->bus->xport->ops->async))(async_code,
4128 device->target->bus, device->target, device, async_arg);
4129 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4132 xpt_path_unlock(path);
4133 mtx_lock(&device->device_mtx);
4135 xpt_release_device(device);
4140 xpt_async_process_tgt(struct cam_et *target, void *arg)
4142 union ccb *ccb = arg;
4143 struct cam_path *path = ccb->ccb_h.path;
4145 if (path->target != target
4146 && path->target->target_id != CAM_TARGET_WILDCARD
4147 && target->target_id != CAM_TARGET_WILDCARD)
4150 if (ccb->casync.async_code == AC_SENT_BDR) {
4151 /* Update our notion of when the last reset occurred */
4152 microtime(&target->last_reset);
4155 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4159 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4162 struct cam_path *path;
4164 u_int32_t async_code;
4166 path = ccb->ccb_h.path;
4167 async_code = ccb->casync.async_code;
4168 async_arg = ccb->casync.async_arg_ptr;
4169 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4170 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4173 if (async_code == AC_BUS_RESET) {
4174 /* Update our notion of when the last reset occurred */
4175 microtime(&bus->last_reset);
4178 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4181 * If this wasn't a fully wildcarded async, tell all
4182 * clients that want all async events.
4184 if (bus != xpt_periph->path->bus) {
4185 xpt_path_lock(xpt_periph->path);
4186 xpt_async_process_dev(xpt_periph->path->device, ccb);
4187 xpt_path_unlock(xpt_periph->path);
4190 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4191 xpt_release_devq(path, 1, TRUE);
4193 xpt_release_simq(path->bus->sim, TRUE);
4194 if (ccb->casync.async_arg_size > 0)
4195 free(async_arg, M_CAMXPT);
4196 xpt_free_path(path);
4201 xpt_async_bcast(struct async_list *async_head,
4202 u_int32_t async_code,
4203 struct cam_path *path, void *async_arg)
4205 struct async_node *cur_entry;
4208 cur_entry = SLIST_FIRST(async_head);
4209 while (cur_entry != NULL) {
4210 struct async_node *next_entry;
4212 * Grab the next list entry before we call the current
4213 * entry's callback. This is because the callback function
4214 * can delete its async callback entry.
4216 next_entry = SLIST_NEXT(cur_entry, links);
4217 if ((cur_entry->event_enable & async_code) != 0) {
4218 lock = cur_entry->event_lock;
4220 CAM_SIM_LOCK(path->device->sim);
4221 cur_entry->callback(cur_entry->callback_arg,
4225 CAM_SIM_UNLOCK(path->device->sim);
4227 cur_entry = next_entry;
4232 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4237 ccb = xpt_alloc_ccb_nowait();
4239 xpt_print(path, "Can't allocate CCB to send %s\n",
4240 xpt_async_string(async_code));
4244 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4245 xpt_print(path, "Can't allocate path to send %s\n",
4246 xpt_async_string(async_code));
4250 ccb->ccb_h.path->periph = NULL;
4251 ccb->ccb_h.func_code = XPT_ASYNC;
4252 ccb->ccb_h.cbfcnp = xpt_async_process;
4253 ccb->ccb_h.flags |= CAM_UNLOCKED;
4254 ccb->casync.async_code = async_code;
4255 ccb->casync.async_arg_size = 0;
4256 size = xpt_async_size(async_code);
4257 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4258 ("xpt_async: func %#x %s aync_code %d %s\n",
4259 ccb->ccb_h.func_code,
4260 xpt_action_name(ccb->ccb_h.func_code),
4262 xpt_async_string(async_code)));
4263 if (size > 0 && async_arg != NULL) {
4264 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4265 if (ccb->casync.async_arg_ptr == NULL) {
4266 xpt_print(path, "Can't allocate argument to send %s\n",
4267 xpt_async_string(async_code));
4268 xpt_free_path(ccb->ccb_h.path);
4272 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4273 ccb->casync.async_arg_size = size;
4274 } else if (size < 0) {
4275 ccb->casync.async_arg_ptr = async_arg;
4276 ccb->casync.async_arg_size = size;
4278 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4279 xpt_freeze_devq(path, 1);
4281 xpt_freeze_simq(path->bus->sim, 1);
4286 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4287 struct cam_et *target, struct cam_ed *device,
4292 * We only need to handle events for real devices.
4294 if (target->target_id == CAM_TARGET_WILDCARD
4295 || device->lun_id == CAM_LUN_WILDCARD)
4298 printf("%s called\n", __func__);
4302 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4304 struct cam_devq *devq;
4307 devq = dev->sim->devq;
4308 mtx_assert(&devq->send_mtx, MA_OWNED);
4309 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4310 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4311 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4312 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4313 /* Remove frozen device from sendq. */
4314 if (device_is_queued(dev))
4315 camq_remove(&devq->send_queue, dev->devq_entry.index);
4320 xpt_freeze_devq(struct cam_path *path, u_int count)
4322 struct cam_ed *dev = path->device;
4323 struct cam_devq *devq;
4326 devq = dev->sim->devq;
4327 mtx_lock(&devq->send_mtx);
4328 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4329 freeze = xpt_freeze_devq_device(dev, count);
4330 mtx_unlock(&devq->send_mtx);
4335 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4337 struct cam_devq *devq;
4341 mtx_lock(&devq->send_mtx);
4342 freeze = (devq->send_queue.qfrozen_cnt += count);
4343 mtx_unlock(&devq->send_mtx);
4348 xpt_release_devq_timeout(void *arg)
4351 struct cam_devq *devq;
4353 dev = (struct cam_ed *)arg;
4354 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4355 devq = dev->sim->devq;
4356 mtx_assert(&devq->send_mtx, MA_OWNED);
4357 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4362 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4365 struct cam_devq *devq;
4367 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4370 devq = dev->sim->devq;
4371 mtx_lock(&devq->send_mtx);
4372 if (xpt_release_devq_device(dev, count, run_queue))
4373 xpt_run_devq(dev->sim->devq);
4374 mtx_unlock(&devq->send_mtx);
4378 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4381 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4382 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4383 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4384 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4385 if (count > dev->ccbq.queue.qfrozen_cnt) {
4387 printf("xpt_release_devq(): requested %u > present %u\n",
4388 count, dev->ccbq.queue.qfrozen_cnt);
4390 count = dev->ccbq.queue.qfrozen_cnt;
4392 dev->ccbq.queue.qfrozen_cnt -= count;
4393 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4395 * No longer need to wait for a successful
4396 * command completion.
4398 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4400 * Remove any timeouts that might be scheduled
4401 * to release this queue.
4403 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4404 callout_stop(&dev->callout);
4405 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4408 * Now that we are unfrozen schedule the
4409 * device so any pending transactions are
4412 xpt_schedule_devq(dev->sim->devq, dev);
4419 xpt_release_simq(struct cam_sim *sim, int run_queue)
4421 struct cam_devq *devq;
4424 mtx_lock(&devq->send_mtx);
4425 if (devq->send_queue.qfrozen_cnt <= 0) {
4427 printf("xpt_release_simq: requested 1 > present %u\n",
4428 devq->send_queue.qfrozen_cnt);
4431 devq->send_queue.qfrozen_cnt--;
4432 if (devq->send_queue.qfrozen_cnt == 0) {
4434 * If there is a timeout scheduled to release this
4435 * sim queue, remove it. The queue frozen count is
4438 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4439 callout_stop(&sim->callout);
4440 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4444 * Now that we are unfrozen run the send queue.
4446 xpt_run_devq(sim->devq);
4449 mtx_unlock(&devq->send_mtx);
4453 * XXX Appears to be unused.
4456 xpt_release_simq_timeout(void *arg)
4458 struct cam_sim *sim;
4460 sim = (struct cam_sim *)arg;
4461 xpt_release_simq(sim, /* run_queue */ TRUE);
4465 xpt_done(union ccb *done_ccb)
4467 struct cam_doneq *queue;
4470 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4471 ("xpt_done: func= %#x %s status %#x\n",
4472 done_ccb->ccb_h.func_code,
4473 xpt_action_name(done_ccb->ccb_h.func_code),
4474 done_ccb->ccb_h.status));
4475 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4478 /* Store the time the ccb was in the sim */
4479 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4480 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4481 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4482 queue = &cam_doneqs[hash];
4483 mtx_lock(&queue->cam_doneq_mtx);
4484 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4485 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4486 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4487 mtx_unlock(&queue->cam_doneq_mtx);
4489 wakeup(&queue->cam_doneq);
4493 xpt_done_direct(union ccb *done_ccb)
4496 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4497 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4498 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4501 /* Store the time the ccb was in the sim */
4502 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4503 xpt_done_process(&done_ccb->ccb_h);
4511 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4516 xpt_alloc_ccb_nowait()
4520 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4525 xpt_free_ccb(union ccb *free_ccb)
4527 free(free_ccb, M_CAMCCB);
4532 /* Private XPT functions */
4535 * Get a CAM control block for the caller. Charge the structure to the device
4536 * referenced by the path. If we don't have sufficient resources to allocate
4537 * more ccbs, we return NULL.
4540 xpt_get_ccb_nowait(struct cam_periph *periph)
4544 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4545 if (new_ccb == NULL)
4547 periph->periph_allocated++;
4548 cam_ccbq_take_opening(&periph->path->device->ccbq);
4553 xpt_get_ccb(struct cam_periph *periph)
4557 cam_periph_unlock(periph);
4558 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4559 cam_periph_lock(periph);
4560 periph->periph_allocated++;
4561 cam_ccbq_take_opening(&periph->path->device->ccbq);
4566 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4568 struct ccb_hdr *ccb_h;
4570 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4571 cam_periph_assert(periph, MA_OWNED);
4572 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4573 ccb_h->pinfo.priority != priority) {
4574 if (priority < periph->immediate_priority) {
4575 periph->immediate_priority = priority;
4576 xpt_run_allocq(periph, 0);
4578 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4581 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4582 return ((union ccb *)ccb_h);
4586 xpt_acquire_bus(struct cam_eb *bus)
4595 xpt_release_bus(struct cam_eb *bus)
4599 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4600 if (--bus->refcount > 0) {
4604 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4605 xsoftc.bus_generation++;
4607 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4608 ("destroying bus, but target list is not empty"));
4609 cam_sim_release(bus->sim);
4610 mtx_destroy(&bus->eb_mtx);
4611 free(bus, M_CAMXPT);
4614 static struct cam_et *
4615 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4617 struct cam_et *cur_target, *target;
4619 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4620 mtx_assert(&bus->eb_mtx, MA_OWNED);
4621 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4626 TAILQ_INIT(&target->ed_entries);
4628 target->target_id = target_id;
4629 target->refcount = 1;
4630 target->generation = 0;
4631 target->luns = NULL;
4632 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4633 timevalclear(&target->last_reset);
4635 * Hold a reference to our parent bus so it
4636 * will not go away before we do.
4640 /* Insertion sort into our bus's target list */
4641 cur_target = TAILQ_FIRST(&bus->et_entries);
4642 while (cur_target != NULL && cur_target->target_id < target_id)
4643 cur_target = TAILQ_NEXT(cur_target, links);
4644 if (cur_target != NULL) {
4645 TAILQ_INSERT_BEFORE(cur_target, target, links);
4647 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4654 xpt_acquire_target(struct cam_et *target)
4656 struct cam_eb *bus = target->bus;
4658 mtx_lock(&bus->eb_mtx);
4660 mtx_unlock(&bus->eb_mtx);
4664 xpt_release_target(struct cam_et *target)
4666 struct cam_eb *bus = target->bus;
4668 mtx_lock(&bus->eb_mtx);
4669 if (--target->refcount > 0) {
4670 mtx_unlock(&bus->eb_mtx);
4673 TAILQ_REMOVE(&bus->et_entries, target, links);
4675 mtx_unlock(&bus->eb_mtx);
4676 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4677 ("destroying target, but device list is not empty"));
4678 xpt_release_bus(bus);
4679 mtx_destroy(&target->luns_mtx);
4681 free(target->luns, M_CAMXPT);
4682 free(target, M_CAMXPT);
4685 static struct cam_ed *
4686 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4689 struct cam_ed *device;
4691 device = xpt_alloc_device(bus, target, lun_id);
4695 device->mintags = 1;
4696 device->maxtags = 1;
4701 xpt_destroy_device(void *context, int pending)
4703 struct cam_ed *device = context;
4705 mtx_lock(&device->device_mtx);
4706 mtx_destroy(&device->device_mtx);
4707 free(device, M_CAMDEV);
4711 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4713 struct cam_ed *cur_device, *device;
4714 struct cam_devq *devq;
4717 mtx_assert(&bus->eb_mtx, MA_OWNED);
4718 /* Make space for us in the device queue on our bus */
4719 devq = bus->sim->devq;
4720 mtx_lock(&devq->send_mtx);
4721 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4722 mtx_unlock(&devq->send_mtx);
4723 if (status != CAM_REQ_CMP)
4726 device = (struct cam_ed *)malloc(sizeof(*device),
4727 M_CAMDEV, M_NOWAIT|M_ZERO);
4731 cam_init_pinfo(&device->devq_entry);
4732 device->target = target;
4733 device->lun_id = lun_id;
4734 device->sim = bus->sim;
4735 if (cam_ccbq_init(&device->ccbq,
4736 bus->sim->max_dev_openings) != 0) {
4737 free(device, M_CAMDEV);
4740 SLIST_INIT(&device->asyncs);
4741 SLIST_INIT(&device->periphs);
4742 device->generation = 0;
4743 device->flags = CAM_DEV_UNCONFIGURED;
4744 device->tag_delay_count = 0;
4745 device->tag_saved_openings = 0;
4746 device->refcount = 1;
4747 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4748 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4749 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4751 * Hold a reference to our parent bus so it
4752 * will not go away before we do.
4756 cur_device = TAILQ_FIRST(&target->ed_entries);
4757 while (cur_device != NULL && cur_device->lun_id < lun_id)
4758 cur_device = TAILQ_NEXT(cur_device, links);
4759 if (cur_device != NULL)
4760 TAILQ_INSERT_BEFORE(cur_device, device, links);
4762 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4763 target->generation++;
4768 xpt_acquire_device(struct cam_ed *device)
4770 struct cam_eb *bus = device->target->bus;
4772 mtx_lock(&bus->eb_mtx);
4774 mtx_unlock(&bus->eb_mtx);
4778 xpt_release_device(struct cam_ed *device)
4780 struct cam_eb *bus = device->target->bus;
4781 struct cam_devq *devq;
4783 mtx_lock(&bus->eb_mtx);
4784 if (--device->refcount > 0) {
4785 mtx_unlock(&bus->eb_mtx);
4789 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4790 device->target->generation++;
4791 mtx_unlock(&bus->eb_mtx);
4793 /* Release our slot in the devq */
4794 devq = bus->sim->devq;
4795 mtx_lock(&devq->send_mtx);
4796 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4797 mtx_unlock(&devq->send_mtx);
4799 KASSERT(SLIST_EMPTY(&device->periphs),
4800 ("destroying device, but periphs list is not empty"));
4801 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4802 ("destroying device while still queued for ccbs"));
4804 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4805 callout_stop(&device->callout);
4807 xpt_release_target(device->target);
4809 cam_ccbq_fini(&device->ccbq);
4811 * Free allocated memory. free(9) does nothing if the
4812 * supplied pointer is NULL, so it is safe to call without
4815 free(device->supported_vpds, M_CAMXPT);
4816 free(device->device_id, M_CAMXPT);
4817 free(device->ext_inq, M_CAMXPT);
4818 free(device->physpath, M_CAMXPT);
4819 free(device->rcap_buf, M_CAMXPT);
4820 free(device->serial_num, M_CAMXPT);
4821 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4825 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4831 mtx_lock(&dev->sim->devq->send_mtx);
4832 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4833 mtx_unlock(&dev->sim->devq->send_mtx);
4834 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4835 || (dev->inq_flags & SID_CmdQue) != 0)
4836 dev->tag_saved_openings = newopenings;
4840 static struct cam_eb *
4841 xpt_find_bus(path_id_t path_id)
4846 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4848 bus = TAILQ_NEXT(bus, links)) {
4849 if (bus->path_id == path_id) {
4858 static struct cam_et *
4859 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4861 struct cam_et *target;
4863 mtx_assert(&bus->eb_mtx, MA_OWNED);
4864 for (target = TAILQ_FIRST(&bus->et_entries);
4866 target = TAILQ_NEXT(target, links)) {
4867 if (target->target_id == target_id) {
4875 static struct cam_ed *
4876 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4878 struct cam_ed *device;
4880 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4881 for (device = TAILQ_FIRST(&target->ed_entries);
4883 device = TAILQ_NEXT(device, links)) {
4884 if (device->lun_id == lun_id) {
4893 xpt_start_tags(struct cam_path *path)
4895 struct ccb_relsim crs;
4896 struct cam_ed *device;
4897 struct cam_sim *sim;
4900 device = path->device;
4901 sim = path->bus->sim;
4902 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4903 xpt_freeze_devq(path, /*count*/1);
4904 device->inq_flags |= SID_CmdQue;
4905 if (device->tag_saved_openings != 0)
4906 newopenings = device->tag_saved_openings;
4908 newopenings = min(device->maxtags,
4909 sim->max_tagged_dev_openings);
4910 xpt_dev_ccbq_resize(path, newopenings);
4911 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4912 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4913 crs.ccb_h.func_code = XPT_REL_SIMQ;
4914 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4916 = crs.release_timeout
4919 xpt_action((union ccb *)&crs);
4923 xpt_stop_tags(struct cam_path *path)
4925 struct ccb_relsim crs;
4926 struct cam_ed *device;
4927 struct cam_sim *sim;
4929 device = path->device;
4930 sim = path->bus->sim;
4931 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4932 device->tag_delay_count = 0;
4933 xpt_freeze_devq(path, /*count*/1);
4934 device->inq_flags &= ~SID_CmdQue;
4935 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
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_boot_delay(void *arg)
4955 xpt_config(void *arg)
4958 * Now that interrupts are enabled, go find our devices
4960 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4961 printf("xpt_config: failed to create taskqueue thread.\n");
4963 /* Setup debugging path */
4964 if (cam_dflags != CAM_DEBUG_NONE) {
4965 if (xpt_create_path(&cam_dpath, NULL,
4966 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4967 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4968 printf("xpt_config: xpt_create_path() failed for debug"
4969 " target %d:%d:%d, debugging disabled\n",
4970 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4971 cam_dflags = CAM_DEBUG_NONE;
4976 periphdriver_init(1);
4978 callout_init(&xsoftc.boot_callout, 1);
4979 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
4980 xpt_boot_delay, NULL, 0);
4981 /* Fire up rescan thread. */
4982 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4983 "cam", "scanner")) {
4984 printf("xpt_config: failed to create rescan thread.\n");
4992 xsoftc.buses_to_config++;
4997 xpt_release_boot(void)
5000 xsoftc.buses_to_config--;
5001 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5002 struct xpt_task *task;
5004 xsoftc.buses_config_done = 1;
5006 /* Call manually because we don't have any busses */
5007 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5009 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5010 taskqueue_enqueue(taskqueue_thread, &task->task);
5017 * If the given device only has one peripheral attached to it, and if that
5018 * peripheral is the passthrough driver, announce it. This insures that the
5019 * user sees some sort of announcement for every peripheral in their system.
5022 xptpassannouncefunc(struct cam_ed *device, void *arg)
5024 struct cam_periph *periph;
5027 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5028 periph = SLIST_NEXT(periph, periph_links), i++);
5030 periph = SLIST_FIRST(&device->periphs);
5032 && (strncmp(periph->periph_name, "pass", 4) == 0))
5033 xpt_announce_periph(periph, NULL);
5039 xpt_finishconfig_task(void *context, int pending)
5042 periphdriver_init(2);
5044 * Check for devices with no "standard" peripheral driver
5045 * attached. For any devices like that, announce the
5046 * passthrough driver so the user will see something.
5049 xpt_for_all_devices(xptpassannouncefunc, NULL);
5051 /* Release our hook so that the boot can continue. */
5052 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5053 free(xsoftc.xpt_config_hook, M_CAMXPT);
5054 xsoftc.xpt_config_hook = NULL;
5056 free(context, M_CAMXPT);
5060 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5061 struct cam_path *path)
5063 struct ccb_setasync csa;
5068 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5069 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5070 if (status != CAM_REQ_CMP)
5072 xpt_path_lock(path);
5076 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5077 csa.ccb_h.func_code = XPT_SASYNC_CB;
5078 csa.event_enable = event;
5079 csa.callback = cbfunc;
5080 csa.callback_arg = cbarg;
5081 xpt_action((union ccb *)&csa);
5082 status = csa.ccb_h.status;
5084 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5085 ("xpt_register_async: func %p\n", cbfunc));
5088 xpt_path_unlock(path);
5089 xpt_free_path(path);
5092 if ((status == CAM_REQ_CMP) &&
5093 (csa.event_enable & AC_FOUND_DEVICE)) {
5095 * Get this peripheral up to date with all
5096 * the currently existing devices.
5098 xpt_for_all_devices(xptsetasyncfunc, &csa);
5100 if ((status == CAM_REQ_CMP) &&
5101 (csa.event_enable & AC_PATH_REGISTERED)) {
5103 * Get this peripheral up to date with all
5104 * the currently existing busses.
5106 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5113 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5115 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5117 switch (work_ccb->ccb_h.func_code) {
5118 /* Common cases first */
5119 case XPT_PATH_INQ: /* Path routing inquiry */
5121 struct ccb_pathinq *cpi;
5123 cpi = &work_ccb->cpi;
5124 cpi->version_num = 1; /* XXX??? */
5125 cpi->hba_inquiry = 0;
5126 cpi->target_sprt = 0;
5128 cpi->hba_eng_cnt = 0;
5129 cpi->max_target = 0;
5131 cpi->initiator_id = 0;
5132 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5133 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5134 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5135 cpi->unit_number = sim->unit_number;
5136 cpi->bus_id = sim->bus_id;
5137 cpi->base_transfer_speed = 0;
5138 cpi->protocol = PROTO_UNSPECIFIED;
5139 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5140 cpi->transport = XPORT_UNSPECIFIED;
5141 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5142 cpi->ccb_h.status = CAM_REQ_CMP;
5147 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5154 * The xpt as a "controller" has no interrupt sources, so polling
5158 xptpoll(struct cam_sim *sim)
5163 xpt_lock_buses(void)
5165 mtx_lock(&xsoftc.xpt_topo_lock);
5169 xpt_unlock_buses(void)
5171 mtx_unlock(&xsoftc.xpt_topo_lock);
5175 xpt_path_mtx(struct cam_path *path)
5178 return (&path->device->device_mtx);
5182 xpt_done_process(struct ccb_hdr *ccb_h)
5184 struct cam_sim *sim;
5185 struct cam_devq *devq;
5186 struct mtx *mtx = NULL;
5188 if (ccb_h->flags & CAM_HIGH_POWER) {
5189 struct highpowerlist *hphead;
5190 struct cam_ed *device;
5192 mtx_lock(&xsoftc.xpt_highpower_lock);
5193 hphead = &xsoftc.highpowerq;
5195 device = STAILQ_FIRST(hphead);
5198 * Increment the count since this command is done.
5200 xsoftc.num_highpower++;
5203 * Any high powered commands queued up?
5205 if (device != NULL) {
5207 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5208 mtx_unlock(&xsoftc.xpt_highpower_lock);
5210 mtx_lock(&device->sim->devq->send_mtx);
5211 xpt_release_devq_device(device,
5212 /*count*/1, /*runqueue*/TRUE);
5213 mtx_unlock(&device->sim->devq->send_mtx);
5215 mtx_unlock(&xsoftc.xpt_highpower_lock);
5218 sim = ccb_h->path->bus->sim;
5220 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5221 xpt_release_simq(sim, /*run_queue*/FALSE);
5222 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5225 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5226 && (ccb_h->status & CAM_DEV_QFRZN)) {
5227 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5228 ccb_h->status &= ~CAM_DEV_QFRZN;
5232 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5233 struct cam_ed *dev = ccb_h->path->device;
5235 mtx_lock(&devq->send_mtx);
5236 devq->send_active--;
5237 devq->send_openings++;
5238 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5240 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5241 && (dev->ccbq.dev_active == 0))) {
5242 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5243 xpt_release_devq_device(dev, /*count*/1,
5244 /*run_queue*/FALSE);
5247 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5248 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5249 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5250 xpt_release_devq_device(dev, /*count*/1,
5251 /*run_queue*/FALSE);
5254 if (!device_is_queued(dev))
5255 (void)xpt_schedule_devq(devq, dev);
5257 mtx_unlock(&devq->send_mtx);
5259 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5260 mtx = xpt_path_mtx(ccb_h->path);
5263 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5264 && (--dev->tag_delay_count == 0))
5265 xpt_start_tags(ccb_h->path);
5269 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5271 mtx = xpt_path_mtx(ccb_h->path);
5281 /* Call the peripheral driver's callback */
5282 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5283 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5289 xpt_done_td(void *arg)
5291 struct cam_doneq *queue = arg;
5292 struct ccb_hdr *ccb_h;
5293 STAILQ_HEAD(, ccb_hdr) doneq;
5295 STAILQ_INIT(&doneq);
5296 mtx_lock(&queue->cam_doneq_mtx);
5298 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5299 queue->cam_doneq_sleep = 1;
5300 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5302 queue->cam_doneq_sleep = 0;
5304 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5305 mtx_unlock(&queue->cam_doneq_mtx);
5307 THREAD_NO_SLEEPING();
5308 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5309 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5310 xpt_done_process(ccb_h);
5312 THREAD_SLEEPING_OK();
5314 mtx_lock(&queue->cam_doneq_mtx);
5319 camisr_runqueue(void)
5321 struct ccb_hdr *ccb_h;
5322 struct cam_doneq *queue;
5325 /* Process global queues. */
5326 for (i = 0; i < cam_num_doneqs; i++) {
5327 queue = &cam_doneqs[i];
5328 mtx_lock(&queue->cam_doneq_mtx);
5329 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5330 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5331 mtx_unlock(&queue->cam_doneq_mtx);
5332 xpt_done_process(ccb_h);
5333 mtx_lock(&queue->cam_doneq_mtx);
5335 mtx_unlock(&queue->cam_doneq_mtx);
5345 static struct kv map[] = {
5346 { XPT_NOOP, "XPT_NOOP" },
5347 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5348 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5349 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5350 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5351 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5352 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5353 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5354 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5355 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5356 { XPT_DEBUG, "XPT_DEBUG" },
5357 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5358 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5359 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5360 { XPT_ASYNC, "XPT_ASYNC" },
5361 { XPT_ABORT, "XPT_ABORT" },
5362 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5363 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5364 { XPT_TERM_IO, "XPT_TERM_IO" },
5365 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5366 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5367 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5368 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5369 { XPT_ATA_IO, "XPT_ATA_IO" },
5370 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5371 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5372 { XPT_NVME_IO, "XPT_NVME_IO" },
5373 { XPT_MMCSD_IO, "XPT_MMCSD_IO" },
5374 { XPT_SMP_IO, "XPT_SMP_IO" },
5375 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5376 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5377 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5378 { XPT_EN_LUN, "XPT_EN_LUN" },
5379 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5380 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5381 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5382 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5383 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5384 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5385 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5390 xpt_action_name(uint32_t action)
5392 static char buffer[32]; /* Only for unknown messages -- racy */
5393 struct kv *walker = map;
5395 while (walker->name != NULL) {
5396 if (walker->v == action)
5397 return (walker->name);
5401 snprintf(buffer, sizeof(buffer), "%#x", action);