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 /* number of high powered commands that can go through right now */
97 struct mtx xpt_highpower_lock;
98 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
101 /* queue for handling async rescan requests. */
102 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
104 int buses_config_done;
106 /* Registered busses */
107 TAILQ_HEAD(,cam_eb) xpt_busses;
108 u_int bus_generation;
110 struct intr_config_hook *xpt_config_hook;
113 struct callout boot_callout;
115 struct mtx xpt_topo_lock;
117 struct taskqueue *xpt_taskq;
122 DM_RET_FLAG_MASK = 0x0f,
125 DM_RET_DESCEND = 0x20,
127 DM_RET_ACTION_MASK = 0xf0
135 } xpt_traverse_depth;
137 struct xpt_traverse_config {
138 xpt_traverse_depth depth;
143 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
144 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
145 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
146 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
147 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
149 /* Transport layer configuration information */
150 static struct xpt_softc xsoftc;
152 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
153 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
154 &xsoftc.boot_delay, 0, "Bus registration wait time");
157 struct mtx_padalign cam_doneq_mtx;
158 STAILQ_HEAD(, ccb_hdr) cam_doneq;
162 static struct cam_doneq cam_doneqs[MAXCPU];
163 static int cam_num_doneqs;
164 static struct proc *cam_proc;
166 TUNABLE_INT("kern.cam.num_doneqs", &cam_num_doneqs);
167 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
168 &cam_num_doneqs, 0, "Number of completion queues/threads");
170 struct cam_periph *xpt_periph;
172 static periph_init_t xpt_periph_init;
174 static struct periph_driver xpt_driver =
176 xpt_periph_init, "xpt",
177 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
181 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
183 static d_open_t xptopen;
184 static d_close_t xptclose;
185 static d_ioctl_t xptioctl;
186 static d_ioctl_t xptdoioctl;
188 static struct cdevsw xpt_cdevsw = {
189 .d_version = D_VERSION,
197 /* Storage for debugging datastructures */
198 struct cam_path *cam_dpath;
199 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
200 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
201 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
202 &cam_dflags, 0, "Enabled debug flags");
203 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
204 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
205 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
206 &cam_debug_delay, 0, "Delay in us after each debug message");
208 /* Our boot-time initialization hook */
209 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
211 static moduledata_t cam_moduledata = {
213 cam_module_event_handler,
217 static int xpt_init(void *);
219 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
220 MODULE_VERSION(cam, 1);
223 static void xpt_async_bcast(struct async_list *async_head,
224 u_int32_t async_code,
225 struct cam_path *path,
227 static path_id_t xptnextfreepathid(void);
228 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
229 static union ccb *xpt_get_ccb(struct cam_periph *periph);
230 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
231 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
232 static void xpt_run_allocq_task(void *context, int pending);
233 static void xpt_run_devq(struct cam_devq *devq);
234 static timeout_t xpt_release_devq_timeout;
235 static void xpt_release_simq_timeout(void *arg) __unused;
236 static void xpt_acquire_bus(struct cam_eb *bus);
237 static void xpt_release_bus(struct cam_eb *bus);
238 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
239 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
241 static struct cam_et*
242 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
243 static void xpt_acquire_target(struct cam_et *target);
244 static void xpt_release_target(struct cam_et *target);
245 static struct cam_eb*
246 xpt_find_bus(path_id_t path_id);
247 static struct cam_et*
248 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
249 static struct cam_ed*
250 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
251 static void xpt_config(void *arg);
252 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
253 u_int32_t new_priority);
254 static xpt_devicefunc_t xptpassannouncefunc;
255 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
256 static void xptpoll(struct cam_sim *sim);
257 static void camisr_runqueue(void);
258 static void xpt_done_process(struct ccb_hdr *ccb_h);
259 static void xpt_done_td(void *);
260 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
261 u_int num_patterns, struct cam_eb *bus);
262 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
264 struct cam_ed *device);
265 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
267 struct cam_periph *periph);
268 static xpt_busfunc_t xptedtbusfunc;
269 static xpt_targetfunc_t xptedttargetfunc;
270 static xpt_devicefunc_t xptedtdevicefunc;
271 static xpt_periphfunc_t xptedtperiphfunc;
272 static xpt_pdrvfunc_t xptplistpdrvfunc;
273 static xpt_periphfunc_t xptplistperiphfunc;
274 static int xptedtmatch(struct ccb_dev_match *cdm);
275 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
276 static int xptbustraverse(struct cam_eb *start_bus,
277 xpt_busfunc_t *tr_func, void *arg);
278 static int xpttargettraverse(struct cam_eb *bus,
279 struct cam_et *start_target,
280 xpt_targetfunc_t *tr_func, void *arg);
281 static int xptdevicetraverse(struct cam_et *target,
282 struct cam_ed *start_device,
283 xpt_devicefunc_t *tr_func, void *arg);
284 static int xptperiphtraverse(struct cam_ed *device,
285 struct cam_periph *start_periph,
286 xpt_periphfunc_t *tr_func, void *arg);
287 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
288 xpt_pdrvfunc_t *tr_func, void *arg);
289 static int xptpdperiphtraverse(struct periph_driver **pdrv,
290 struct cam_periph *start_periph,
291 xpt_periphfunc_t *tr_func,
293 static xpt_busfunc_t xptdefbusfunc;
294 static xpt_targetfunc_t xptdeftargetfunc;
295 static xpt_devicefunc_t xptdefdevicefunc;
296 static xpt_periphfunc_t xptdefperiphfunc;
297 static void xpt_finishconfig_task(void *context, int pending);
298 static void xpt_dev_async_default(u_int32_t async_code,
300 struct cam_et *target,
301 struct cam_ed *device,
303 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
304 struct cam_et *target,
306 static xpt_devicefunc_t xptsetasyncfunc;
307 static xpt_busfunc_t xptsetasyncbusfunc;
308 static cam_status xptregister(struct cam_periph *periph,
310 static __inline int device_is_queued(struct cam_ed *device);
313 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
317 mtx_assert(&devq->send_mtx, MA_OWNED);
318 if ((dev->ccbq.queue.entries > 0) &&
319 (dev->ccbq.dev_openings > 0) &&
320 (dev->ccbq.queue.qfrozen_cnt == 0)) {
322 * The priority of a device waiting for controller
323 * resources is that of the highest priority CCB
327 xpt_schedule_dev(&devq->send_queue,
329 CAMQ_GET_PRIO(&dev->ccbq.queue));
337 device_is_queued(struct cam_ed *device)
339 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
345 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
349 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
353 * Only allow read-write access.
355 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
359 * We don't allow nonblocking access.
361 if ((flags & O_NONBLOCK) != 0) {
362 printf("%s: can't do nonblocking access\n", devtoname(dev));
370 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
377 * Don't automatically grab the xpt softc lock here even though this is going
378 * through the xpt device. The xpt device is really just a back door for
379 * accessing other devices and SIMs, so the right thing to do is to grab
380 * the appropriate SIM lock once the bus/SIM is located.
383 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
387 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
388 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
394 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
402 * For the transport layer CAMIOCOMMAND ioctl, we really only want
403 * to accept CCB types that don't quite make sense to send through a
404 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
412 inccb = (union ccb *)addr;
414 bus = xpt_find_bus(inccb->ccb_h.path_id);
418 switch (inccb->ccb_h.func_code) {
421 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
422 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
423 xpt_release_bus(bus);
428 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
429 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
430 xpt_release_bus(bus);
438 switch(inccb->ccb_h.func_code) {
446 ccb = xpt_alloc_ccb();
449 * Create a path using the bus, target, and lun the
452 if (xpt_create_path(&ccb->ccb_h.path, NULL,
453 inccb->ccb_h.path_id,
454 inccb->ccb_h.target_id,
455 inccb->ccb_h.target_lun) !=
461 /* Ensure all of our fields are correct */
462 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
463 inccb->ccb_h.pinfo.priority);
464 xpt_merge_ccb(ccb, inccb);
465 xpt_path_lock(ccb->ccb_h.path);
466 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
467 xpt_path_unlock(ccb->ccb_h.path);
468 bcopy(ccb, inccb, sizeof(union ccb));
469 xpt_free_path(ccb->ccb_h.path);
477 * This is an immediate CCB, so it's okay to
478 * allocate it on the stack.
482 * Create a path using the bus, target, and lun the
485 if (xpt_create_path(&ccb.ccb_h.path, NULL,
486 inccb->ccb_h.path_id,
487 inccb->ccb_h.target_id,
488 inccb->ccb_h.target_lun) !=
493 /* Ensure all of our fields are correct */
494 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
495 inccb->ccb_h.pinfo.priority);
496 xpt_merge_ccb(&ccb, inccb);
498 bcopy(&ccb, inccb, sizeof(union ccb));
499 xpt_free_path(ccb.ccb_h.path);
503 case XPT_DEV_MATCH: {
504 struct cam_periph_map_info mapinfo;
505 struct cam_path *old_path;
508 * We can't deal with physical addresses for this
509 * type of transaction.
511 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
518 * Save this in case the caller had it set to
519 * something in particular.
521 old_path = inccb->ccb_h.path;
524 * We really don't need a path for the matching
525 * code. The path is needed because of the
526 * debugging statements in xpt_action(). They
527 * assume that the CCB has a valid path.
529 inccb->ccb_h.path = xpt_periph->path;
531 bzero(&mapinfo, sizeof(mapinfo));
534 * Map the pattern and match buffers into kernel
535 * virtual address space.
537 error = cam_periph_mapmem(inccb, &mapinfo);
540 inccb->ccb_h.path = old_path;
545 * This is an immediate CCB, we can send it on directly.
550 * Map the buffers back into user space.
552 cam_periph_unmapmem(inccb, &mapinfo);
554 inccb->ccb_h.path = old_path;
563 xpt_release_bus(bus);
567 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
568 * with the periphal driver name and unit name filled in. The other
569 * fields don't really matter as input. The passthrough driver name
570 * ("pass"), and unit number are passed back in the ccb. The current
571 * device generation number, and the index into the device peripheral
572 * driver list, and the status are also passed back. Note that
573 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
574 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
575 * (or rather should be) impossible for the device peripheral driver
576 * list to change since we look at the whole thing in one pass, and
577 * we do it with lock protection.
580 case CAMGETPASSTHRU: {
582 struct cam_periph *periph;
583 struct periph_driver **p_drv;
586 int base_periph_found;
588 ccb = (union ccb *)addr;
589 unit = ccb->cgdl.unit_number;
590 name = ccb->cgdl.periph_name;
591 base_periph_found = 0;
594 * Sanity check -- make sure we don't get a null peripheral
597 if (*ccb->cgdl.periph_name == '\0') {
602 /* Keep the list from changing while we traverse it */
605 /* first find our driver in the list of drivers */
606 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
607 if (strcmp((*p_drv)->driver_name, name) == 0)
610 if (*p_drv == NULL) {
612 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
613 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
614 *ccb->cgdl.periph_name = '\0';
615 ccb->cgdl.unit_number = 0;
621 * Run through every peripheral instance of this driver
622 * and check to see whether it matches the unit passed
623 * in by the user. If it does, get out of the loops and
624 * find the passthrough driver associated with that
627 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
628 periph = TAILQ_NEXT(periph, unit_links)) {
630 if (periph->unit_number == unit)
634 * If we found the peripheral driver that the user passed
635 * in, go through all of the peripheral drivers for that
636 * particular device and look for a passthrough driver.
638 if (periph != NULL) {
639 struct cam_ed *device;
642 base_periph_found = 1;
643 device = periph->path->device;
644 for (i = 0, periph = SLIST_FIRST(&device->periphs);
646 periph = SLIST_NEXT(periph, periph_links), i++) {
648 * Check to see whether we have a
649 * passthrough device or not.
651 if (strcmp(periph->periph_name, "pass") == 0) {
653 * Fill in the getdevlist fields.
655 strcpy(ccb->cgdl.periph_name,
656 periph->periph_name);
657 ccb->cgdl.unit_number =
659 if (SLIST_NEXT(periph, periph_links))
661 CAM_GDEVLIST_MORE_DEVS;
664 CAM_GDEVLIST_LAST_DEVICE;
665 ccb->cgdl.generation =
669 * Fill in some CCB header fields
670 * that the user may want.
673 periph->path->bus->path_id;
674 ccb->ccb_h.target_id =
675 periph->path->target->target_id;
676 ccb->ccb_h.target_lun =
677 periph->path->device->lun_id;
678 ccb->ccb_h.status = CAM_REQ_CMP;
685 * If the periph is null here, one of two things has
686 * happened. The first possibility is that we couldn't
687 * find the unit number of the particular peripheral driver
688 * that the user is asking about. e.g. the user asks for
689 * the passthrough driver for "da11". We find the list of
690 * "da" peripherals all right, but there is no unit 11.
691 * The other possibility is that we went through the list
692 * of peripheral drivers attached to the device structure,
693 * but didn't find one with the name "pass". Either way,
694 * we return ENOENT, since we couldn't find something.
696 if (periph == NULL) {
697 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
698 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
699 *ccb->cgdl.periph_name = '\0';
700 ccb->cgdl.unit_number = 0;
703 * It is unfortunate that this is even necessary,
704 * but there are many, many clueless users out there.
705 * If this is true, the user is looking for the
706 * passthrough driver, but doesn't have one in his
709 if (base_periph_found == 1) {
710 printf("xptioctl: pass driver is not in the "
712 printf("xptioctl: put \"device pass\" in "
713 "your kernel config file\n");
728 cam_module_event_handler(module_t mod, int what, void *arg)
734 if ((error = xpt_init(NULL)) != 0)
747 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
750 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
751 xpt_free_path(done_ccb->ccb_h.path);
752 xpt_free_ccb(done_ccb);
754 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
755 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
760 /* thread to handle bus rescans */
762 xpt_scanner_thread(void *dummy)
765 struct cam_path path;
769 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
770 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
772 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
773 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
777 * Since lock can be dropped inside and path freed
778 * by completion callback even before return here,
779 * take our own path copy for reference.
781 xpt_copy_path(&path, ccb->ccb_h.path);
782 xpt_path_lock(&path);
784 xpt_path_unlock(&path);
785 xpt_release_path(&path);
793 xpt_rescan(union ccb *ccb)
797 /* Prepare request */
798 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
799 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
800 ccb->ccb_h.func_code = XPT_SCAN_BUS;
801 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
802 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
803 ccb->ccb_h.func_code = XPT_SCAN_TGT;
804 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
805 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
806 ccb->ccb_h.func_code = XPT_SCAN_LUN;
808 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
809 xpt_free_path(ccb->ccb_h.path);
813 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
814 ccb->ccb_h.cbfcnp = xpt_rescan_done;
815 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
816 /* Don't make duplicate entries for the same paths. */
818 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
819 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
820 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
821 wakeup(&xsoftc.ccb_scanq);
823 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
824 xpt_free_path(ccb->ccb_h.path);
830 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
831 xsoftc.buses_to_config++;
832 wakeup(&xsoftc.ccb_scanq);
836 /* Functions accessed by the peripheral drivers */
838 xpt_init(void *dummy)
840 struct cam_sim *xpt_sim;
841 struct cam_path *path;
842 struct cam_devq *devq;
846 TAILQ_INIT(&xsoftc.xpt_busses);
847 TAILQ_INIT(&xsoftc.ccb_scanq);
848 STAILQ_INIT(&xsoftc.highpowerq);
849 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
851 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
852 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
853 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
854 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
855 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
857 #ifdef CAM_BOOT_DELAY
859 * Override this value at compile time to assist our users
860 * who don't use loader to boot a kernel.
862 xsoftc.boot_delay = CAM_BOOT_DELAY;
865 * The xpt layer is, itself, the equivelent of a SIM.
866 * Allow 16 ccbs in the ccb pool for it. This should
867 * give decent parallelism when we probe busses and
868 * perform other XPT functions.
870 devq = cam_simq_alloc(16);
871 xpt_sim = cam_sim_alloc(xptaction,
876 /*mtx*/&xsoftc.xpt_lock,
877 /*max_dev_transactions*/0,
878 /*max_tagged_dev_transactions*/0,
883 mtx_lock(&xsoftc.xpt_lock);
884 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
885 mtx_unlock(&xsoftc.xpt_lock);
886 printf("xpt_init: xpt_bus_register failed with status %#x,"
887 " failing attach\n", status);
890 mtx_unlock(&xsoftc.xpt_lock);
893 * Looking at the XPT from the SIM layer, the XPT is
894 * the equivelent of a peripheral driver. Allocate
895 * a peripheral driver entry for us.
897 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
899 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
900 mtx_unlock(&xsoftc.xpt_lock);
901 printf("xpt_init: xpt_create_path failed with status %#x,"
902 " failing attach\n", status);
906 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
907 path, NULL, 0, xpt_sim);
908 xpt_path_unlock(path);
911 if (cam_num_doneqs < 1)
912 cam_num_doneqs = 1 + mp_ncpus / 6;
913 else if (cam_num_doneqs > MAXCPU)
914 cam_num_doneqs = MAXCPU;
915 for (i = 0; i < cam_num_doneqs; i++) {
916 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
918 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
919 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
920 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
926 if (cam_num_doneqs < 1) {
927 printf("xpt_init: Cannot init completion queues "
928 "- failing attach\n");
932 * Register a callback for when interrupts are enabled.
934 xsoftc.xpt_config_hook =
935 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
936 M_CAMXPT, M_NOWAIT | M_ZERO);
937 if (xsoftc.xpt_config_hook == NULL) {
938 printf("xpt_init: Cannot malloc config hook "
939 "- failing attach\n");
942 xsoftc.xpt_config_hook->ich_func = xpt_config;
943 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
944 free (xsoftc.xpt_config_hook, M_CAMXPT);
945 printf("xpt_init: config_intrhook_establish failed "
946 "- failing attach\n");
953 xptregister(struct cam_periph *periph, void *arg)
955 struct cam_sim *xpt_sim;
957 if (periph == NULL) {
958 printf("xptregister: periph was NULL!!\n");
959 return(CAM_REQ_CMP_ERR);
962 xpt_sim = (struct cam_sim *)arg;
963 xpt_sim->softc = periph;
965 periph->softc = NULL;
971 xpt_add_periph(struct cam_periph *periph)
973 struct cam_ed *device;
976 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
977 device = periph->path->device;
978 status = CAM_REQ_CMP;
979 if (device != NULL) {
980 mtx_lock(&device->target->bus->eb_mtx);
981 device->generation++;
982 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
983 mtx_unlock(&device->target->bus->eb_mtx);
990 xpt_remove_periph(struct cam_periph *periph)
992 struct cam_ed *device;
994 device = periph->path->device;
995 if (device != NULL) {
996 mtx_lock(&device->target->bus->eb_mtx);
997 device->generation++;
998 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
999 mtx_unlock(&device->target->bus->eb_mtx);
1005 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1007 struct cam_path *path = periph->path;
1009 cam_periph_assert(periph, MA_OWNED);
1010 periph->flags |= CAM_PERIPH_ANNOUNCED;
1012 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1013 periph->periph_name, periph->unit_number,
1014 path->bus->sim->sim_name,
1015 path->bus->sim->unit_number,
1016 path->bus->sim->bus_id,
1018 path->target->target_id,
1019 (uintmax_t)path->device->lun_id);
1020 printf("%s%d: ", periph->periph_name, periph->unit_number);
1021 if (path->device->protocol == PROTO_SCSI)
1022 scsi_print_inquiry(&path->device->inq_data);
1023 else if (path->device->protocol == PROTO_ATA ||
1024 path->device->protocol == PROTO_SATAPM)
1025 ata_print_ident(&path->device->ident_data);
1026 else if (path->device->protocol == PROTO_SEMB)
1028 (struct sep_identify_data *)&path->device->ident_data);
1030 printf("Unknown protocol device\n");
1031 if (path->device->serial_num_len > 0) {
1032 /* Don't wrap the screen - print only the first 60 chars */
1033 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1034 periph->unit_number, path->device->serial_num);
1036 /* Announce transport details. */
1037 (*(path->bus->xport->announce))(periph);
1038 /* Announce command queueing. */
1039 if (path->device->inq_flags & SID_CmdQue
1040 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1041 printf("%s%d: Command Queueing enabled\n",
1042 periph->periph_name, periph->unit_number);
1044 /* Announce caller's details if they've passed in. */
1045 if (announce_string != NULL)
1046 printf("%s%d: %s\n", periph->periph_name,
1047 periph->unit_number, announce_string);
1051 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1054 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1055 periph->unit_number, quirks, bit_string);
1060 xpt_denounce_periph(struct cam_periph *periph)
1062 struct cam_path *path = periph->path;
1064 cam_periph_assert(periph, MA_OWNED);
1065 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1066 periph->periph_name, periph->unit_number,
1067 path->bus->sim->sim_name,
1068 path->bus->sim->unit_number,
1069 path->bus->sim->bus_id,
1071 path->target->target_id,
1072 (uintmax_t)path->device->lun_id);
1073 printf("%s%d: ", periph->periph_name, periph->unit_number);
1074 if (path->device->protocol == PROTO_SCSI)
1075 scsi_print_inquiry_short(&path->device->inq_data);
1076 else if (path->device->protocol == PROTO_ATA ||
1077 path->device->protocol == PROTO_SATAPM)
1078 ata_print_ident_short(&path->device->ident_data);
1079 else if (path->device->protocol == PROTO_SEMB)
1080 semb_print_ident_short(
1081 (struct sep_identify_data *)&path->device->ident_data);
1083 printf("Unknown protocol device");
1084 if (path->device->serial_num_len > 0)
1085 printf(" s/n %.60s", path->device->serial_num);
1086 printf(" detached\n");
1091 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1094 struct ccb_dev_advinfo cdai;
1095 struct scsi_vpd_id_descriptor *idd;
1097 xpt_path_assert(path, MA_OWNED);
1099 memset(&cdai, 0, sizeof(cdai));
1100 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1101 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1104 if (!strcmp(attr, "GEOM::ident"))
1105 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1106 else if (!strcmp(attr, "GEOM::physpath"))
1107 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1108 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1109 strcmp(attr, "GEOM::lunname") == 0) {
1110 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1111 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1115 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1116 if (cdai.buf == NULL) {
1120 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1121 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1122 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1123 if (cdai.provsiz == 0)
1125 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1126 if (strcmp(attr, "GEOM::lunid") == 0) {
1127 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1128 cdai.provsiz, scsi_devid_is_lun_naa);
1130 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1131 cdai.provsiz, scsi_devid_is_lun_eui64);
1135 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1136 cdai.provsiz, scsi_devid_is_lun_t10);
1138 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1139 cdai.provsiz, scsi_devid_is_lun_name);
1143 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1144 (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1145 l = strnlen(idd->identifier, idd->length);
1147 bcopy(idd->identifier, buf, l);
1152 if (idd->length * 2 < len) {
1153 for (l = 0; l < idd->length; l++)
1154 sprintf(buf + l * 2, "%02x",
1155 idd->identifier[l]);
1161 if (strlcpy(buf, cdai.buf, len) >= len)
1166 if (cdai.buf != NULL)
1167 free(cdai.buf, M_CAMXPT);
1171 static dev_match_ret
1172 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1175 dev_match_ret retval;
1178 retval = DM_RET_NONE;
1181 * If we aren't given something to match against, that's an error.
1184 return(DM_RET_ERROR);
1187 * If there are no match entries, then this bus matches no
1190 if ((patterns == NULL) || (num_patterns == 0))
1191 return(DM_RET_DESCEND | DM_RET_COPY);
1193 for (i = 0; i < num_patterns; i++) {
1194 struct bus_match_pattern *cur_pattern;
1197 * If the pattern in question isn't for a bus node, we
1198 * aren't interested. However, we do indicate to the
1199 * calling routine that we should continue descending the
1200 * tree, since the user wants to match against lower-level
1203 if (patterns[i].type != DEV_MATCH_BUS) {
1204 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1205 retval |= DM_RET_DESCEND;
1209 cur_pattern = &patterns[i].pattern.bus_pattern;
1212 * If they want to match any bus node, we give them any
1215 if (cur_pattern->flags == BUS_MATCH_ANY) {
1216 /* set the copy flag */
1217 retval |= DM_RET_COPY;
1220 * If we've already decided on an action, go ahead
1223 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1228 * Not sure why someone would do this...
1230 if (cur_pattern->flags == BUS_MATCH_NONE)
1233 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1234 && (cur_pattern->path_id != bus->path_id))
1237 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1238 && (cur_pattern->bus_id != bus->sim->bus_id))
1241 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1242 && (cur_pattern->unit_number != bus->sim->unit_number))
1245 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1246 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1251 * If we get to this point, the user definitely wants
1252 * information on this bus. So tell the caller to copy the
1255 retval |= DM_RET_COPY;
1258 * If the return action has been set to descend, then we
1259 * know that we've already seen a non-bus matching
1260 * expression, therefore we need to further descend the tree.
1261 * This won't change by continuing around the loop, so we
1262 * go ahead and return. If we haven't seen a non-bus
1263 * matching expression, we keep going around the loop until
1264 * we exhaust the matching expressions. We'll set the stop
1265 * flag once we fall out of the loop.
1267 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1272 * If the return action hasn't been set to descend yet, that means
1273 * we haven't seen anything other than bus matching patterns. So
1274 * tell the caller to stop descending the tree -- the user doesn't
1275 * want to match against lower level tree elements.
1277 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1278 retval |= DM_RET_STOP;
1283 static dev_match_ret
1284 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1285 struct cam_ed *device)
1287 dev_match_ret retval;
1290 retval = DM_RET_NONE;
1293 * If we aren't given something to match against, that's an error.
1296 return(DM_RET_ERROR);
1299 * If there are no match entries, then this device matches no
1302 if ((patterns == NULL) || (num_patterns == 0))
1303 return(DM_RET_DESCEND | DM_RET_COPY);
1305 for (i = 0; i < num_patterns; i++) {
1306 struct device_match_pattern *cur_pattern;
1307 struct scsi_vpd_device_id *device_id_page;
1310 * If the pattern in question isn't for a device node, we
1311 * aren't interested.
1313 if (patterns[i].type != DEV_MATCH_DEVICE) {
1314 if ((patterns[i].type == DEV_MATCH_PERIPH)
1315 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1316 retval |= DM_RET_DESCEND;
1320 cur_pattern = &patterns[i].pattern.device_pattern;
1322 /* Error out if mutually exclusive options are specified. */
1323 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1324 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1325 return(DM_RET_ERROR);
1328 * If they want to match any device node, we give them any
1331 if (cur_pattern->flags == DEV_MATCH_ANY)
1335 * Not sure why someone would do this...
1337 if (cur_pattern->flags == DEV_MATCH_NONE)
1340 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1341 && (cur_pattern->path_id != device->target->bus->path_id))
1344 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1345 && (cur_pattern->target_id != device->target->target_id))
1348 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1349 && (cur_pattern->target_lun != device->lun_id))
1352 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1353 && (cam_quirkmatch((caddr_t)&device->inq_data,
1354 (caddr_t)&cur_pattern->data.inq_pat,
1355 1, sizeof(cur_pattern->data.inq_pat),
1356 scsi_static_inquiry_match) == NULL))
1359 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1360 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1361 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1362 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1363 device->device_id_len
1364 - SVPD_DEVICE_ID_HDR_LEN,
1365 cur_pattern->data.devid_pat.id,
1366 cur_pattern->data.devid_pat.id_len) != 0))
1371 * If we get to this point, the user definitely wants
1372 * information on this device. So tell the caller to copy
1375 retval |= DM_RET_COPY;
1378 * If the return action has been set to descend, then we
1379 * know that we've already seen a peripheral matching
1380 * expression, therefore we need to further descend the tree.
1381 * This won't change by continuing around the loop, so we
1382 * go ahead and return. If we haven't seen a peripheral
1383 * matching expression, we keep going around the loop until
1384 * we exhaust the matching expressions. We'll set the stop
1385 * flag once we fall out of the loop.
1387 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1392 * If the return action hasn't been set to descend yet, that means
1393 * we haven't seen any peripheral matching patterns. So tell the
1394 * caller to stop descending the tree -- the user doesn't want to
1395 * match against lower level tree elements.
1397 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1398 retval |= DM_RET_STOP;
1404 * Match a single peripheral against any number of match patterns.
1406 static dev_match_ret
1407 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1408 struct cam_periph *periph)
1410 dev_match_ret retval;
1414 * If we aren't given something to match against, that's an error.
1417 return(DM_RET_ERROR);
1420 * If there are no match entries, then this peripheral matches no
1423 if ((patterns == NULL) || (num_patterns == 0))
1424 return(DM_RET_STOP | DM_RET_COPY);
1427 * There aren't any nodes below a peripheral node, so there's no
1428 * reason to descend the tree any further.
1430 retval = DM_RET_STOP;
1432 for (i = 0; i < num_patterns; i++) {
1433 struct periph_match_pattern *cur_pattern;
1436 * If the pattern in question isn't for a peripheral, we
1437 * aren't interested.
1439 if (patterns[i].type != DEV_MATCH_PERIPH)
1442 cur_pattern = &patterns[i].pattern.periph_pattern;
1445 * If they want to match on anything, then we will do so.
1447 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1448 /* set the copy flag */
1449 retval |= DM_RET_COPY;
1452 * We've already set the return action to stop,
1453 * since there are no nodes below peripherals in
1460 * Not sure why someone would do this...
1462 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1465 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1466 && (cur_pattern->path_id != periph->path->bus->path_id))
1470 * For the target and lun id's, we have to make sure the
1471 * target and lun pointers aren't NULL. The xpt peripheral
1472 * has a wildcard target and device.
1474 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1475 && ((periph->path->target == NULL)
1476 ||(cur_pattern->target_id != periph->path->target->target_id)))
1479 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1480 && ((periph->path->device == NULL)
1481 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1484 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1485 && (cur_pattern->unit_number != periph->unit_number))
1488 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1489 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1494 * If we get to this point, the user definitely wants
1495 * information on this peripheral. So tell the caller to
1496 * copy the data out.
1498 retval |= DM_RET_COPY;
1501 * The return action has already been set to stop, since
1502 * peripherals don't have any nodes below them in the EDT.
1508 * If we get to this point, the peripheral that was passed in
1509 * doesn't match any of the patterns.
1515 xptedtbusfunc(struct cam_eb *bus, void *arg)
1517 struct ccb_dev_match *cdm;
1518 struct cam_et *target;
1519 dev_match_ret retval;
1521 cdm = (struct ccb_dev_match *)arg;
1524 * If our position is for something deeper in the tree, that means
1525 * that we've already seen this node. So, we keep going down.
1527 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1528 && (cdm->pos.cookie.bus == bus)
1529 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1530 && (cdm->pos.cookie.target != NULL))
1531 retval = DM_RET_DESCEND;
1533 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1536 * If we got an error, bail out of the search.
1538 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1539 cdm->status = CAM_DEV_MATCH_ERROR;
1544 * If the copy flag is set, copy this bus out.
1546 if (retval & DM_RET_COPY) {
1549 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1550 sizeof(struct dev_match_result));
1553 * If we don't have enough space to put in another
1554 * match result, save our position and tell the
1555 * user there are more devices to check.
1557 if (spaceleft < sizeof(struct dev_match_result)) {
1558 bzero(&cdm->pos, sizeof(cdm->pos));
1559 cdm->pos.position_type =
1560 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1562 cdm->pos.cookie.bus = bus;
1563 cdm->pos.generations[CAM_BUS_GENERATION]=
1564 xsoftc.bus_generation;
1565 cdm->status = CAM_DEV_MATCH_MORE;
1568 j = cdm->num_matches;
1570 cdm->matches[j].type = DEV_MATCH_BUS;
1571 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1572 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1573 cdm->matches[j].result.bus_result.unit_number =
1574 bus->sim->unit_number;
1575 strncpy(cdm->matches[j].result.bus_result.dev_name,
1576 bus->sim->sim_name, DEV_IDLEN);
1580 * If the user is only interested in busses, there's no
1581 * reason to descend to the next level in the tree.
1583 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1587 * If there is a target generation recorded, check it to
1588 * make sure the target list hasn't changed.
1590 mtx_lock(&bus->eb_mtx);
1591 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1592 && (cdm->pos.cookie.bus == bus)
1593 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1594 && (cdm->pos.cookie.target != NULL)) {
1595 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1597 mtx_unlock(&bus->eb_mtx);
1598 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1601 target = (struct cam_et *)cdm->pos.cookie.target;
1605 mtx_unlock(&bus->eb_mtx);
1607 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1611 xptedttargetfunc(struct cam_et *target, void *arg)
1613 struct ccb_dev_match *cdm;
1615 struct cam_ed *device;
1617 cdm = (struct ccb_dev_match *)arg;
1621 * If there is a device list generation recorded, check it to
1622 * make sure the device list hasn't changed.
1624 mtx_lock(&bus->eb_mtx);
1625 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1626 && (cdm->pos.cookie.bus == bus)
1627 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1628 && (cdm->pos.cookie.target == target)
1629 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1630 && (cdm->pos.cookie.device != NULL)) {
1631 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1632 target->generation) {
1633 mtx_unlock(&bus->eb_mtx);
1634 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1637 device = (struct cam_ed *)cdm->pos.cookie.device;
1641 mtx_unlock(&bus->eb_mtx);
1643 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1647 xptedtdevicefunc(struct cam_ed *device, void *arg)
1650 struct cam_periph *periph;
1651 struct ccb_dev_match *cdm;
1652 dev_match_ret retval;
1654 cdm = (struct ccb_dev_match *)arg;
1655 bus = device->target->bus;
1658 * If our position is for something deeper in the tree, that means
1659 * that we've already seen this node. So, we keep going down.
1661 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1662 && (cdm->pos.cookie.device == device)
1663 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1664 && (cdm->pos.cookie.periph != NULL))
1665 retval = DM_RET_DESCEND;
1667 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1670 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1671 cdm->status = CAM_DEV_MATCH_ERROR;
1676 * If the copy flag is set, copy this device out.
1678 if (retval & DM_RET_COPY) {
1681 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1682 sizeof(struct dev_match_result));
1685 * If we don't have enough space to put in another
1686 * match result, save our position and tell the
1687 * user there are more devices to check.
1689 if (spaceleft < sizeof(struct dev_match_result)) {
1690 bzero(&cdm->pos, sizeof(cdm->pos));
1691 cdm->pos.position_type =
1692 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1693 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1695 cdm->pos.cookie.bus = device->target->bus;
1696 cdm->pos.generations[CAM_BUS_GENERATION]=
1697 xsoftc.bus_generation;
1698 cdm->pos.cookie.target = device->target;
1699 cdm->pos.generations[CAM_TARGET_GENERATION] =
1700 device->target->bus->generation;
1701 cdm->pos.cookie.device = device;
1702 cdm->pos.generations[CAM_DEV_GENERATION] =
1703 device->target->generation;
1704 cdm->status = CAM_DEV_MATCH_MORE;
1707 j = cdm->num_matches;
1709 cdm->matches[j].type = DEV_MATCH_DEVICE;
1710 cdm->matches[j].result.device_result.path_id =
1711 device->target->bus->path_id;
1712 cdm->matches[j].result.device_result.target_id =
1713 device->target->target_id;
1714 cdm->matches[j].result.device_result.target_lun =
1716 cdm->matches[j].result.device_result.protocol =
1718 bcopy(&device->inq_data,
1719 &cdm->matches[j].result.device_result.inq_data,
1720 sizeof(struct scsi_inquiry_data));
1721 bcopy(&device->ident_data,
1722 &cdm->matches[j].result.device_result.ident_data,
1723 sizeof(struct ata_params));
1725 /* Let the user know whether this device is unconfigured */
1726 if (device->flags & CAM_DEV_UNCONFIGURED)
1727 cdm->matches[j].result.device_result.flags =
1728 DEV_RESULT_UNCONFIGURED;
1730 cdm->matches[j].result.device_result.flags =
1735 * If the user isn't interested in peripherals, don't descend
1736 * the tree any further.
1738 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1742 * If there is a peripheral list generation recorded, make sure
1743 * it hasn't changed.
1746 mtx_lock(&bus->eb_mtx);
1747 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1748 && (cdm->pos.cookie.bus == bus)
1749 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1750 && (cdm->pos.cookie.target == device->target)
1751 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1752 && (cdm->pos.cookie.device == device)
1753 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1754 && (cdm->pos.cookie.periph != NULL)) {
1755 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1756 device->generation) {
1757 mtx_unlock(&bus->eb_mtx);
1759 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1762 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1766 mtx_unlock(&bus->eb_mtx);
1769 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1773 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1775 struct ccb_dev_match *cdm;
1776 dev_match_ret retval;
1778 cdm = (struct ccb_dev_match *)arg;
1780 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1782 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1783 cdm->status = CAM_DEV_MATCH_ERROR;
1788 * If the copy flag is set, copy this peripheral out.
1790 if (retval & DM_RET_COPY) {
1793 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1794 sizeof(struct dev_match_result));
1797 * If we don't have enough space to put in another
1798 * match result, save our position and tell the
1799 * user there are more devices to check.
1801 if (spaceleft < sizeof(struct dev_match_result)) {
1802 bzero(&cdm->pos, sizeof(cdm->pos));
1803 cdm->pos.position_type =
1804 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1805 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1808 cdm->pos.cookie.bus = periph->path->bus;
1809 cdm->pos.generations[CAM_BUS_GENERATION]=
1810 xsoftc.bus_generation;
1811 cdm->pos.cookie.target = periph->path->target;
1812 cdm->pos.generations[CAM_TARGET_GENERATION] =
1813 periph->path->bus->generation;
1814 cdm->pos.cookie.device = periph->path->device;
1815 cdm->pos.generations[CAM_DEV_GENERATION] =
1816 periph->path->target->generation;
1817 cdm->pos.cookie.periph = periph;
1818 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1819 periph->path->device->generation;
1820 cdm->status = CAM_DEV_MATCH_MORE;
1824 j = cdm->num_matches;
1826 cdm->matches[j].type = DEV_MATCH_PERIPH;
1827 cdm->matches[j].result.periph_result.path_id =
1828 periph->path->bus->path_id;
1829 cdm->matches[j].result.periph_result.target_id =
1830 periph->path->target->target_id;
1831 cdm->matches[j].result.periph_result.target_lun =
1832 periph->path->device->lun_id;
1833 cdm->matches[j].result.periph_result.unit_number =
1834 periph->unit_number;
1835 strncpy(cdm->matches[j].result.periph_result.periph_name,
1836 periph->periph_name, DEV_IDLEN);
1843 xptedtmatch(struct ccb_dev_match *cdm)
1848 cdm->num_matches = 0;
1851 * Check the bus list generation. If it has changed, the user
1852 * needs to reset everything and start over.
1855 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1856 && (cdm->pos.cookie.bus != NULL)) {
1857 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1858 xsoftc.bus_generation) {
1860 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1863 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1869 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1872 * If we get back 0, that means that we had to stop before fully
1873 * traversing the EDT. It also means that one of the subroutines
1874 * has set the status field to the proper value. If we get back 1,
1875 * we've fully traversed the EDT and copied out any matching entries.
1878 cdm->status = CAM_DEV_MATCH_LAST;
1884 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1886 struct cam_periph *periph;
1887 struct ccb_dev_match *cdm;
1889 cdm = (struct ccb_dev_match *)arg;
1892 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1893 && (cdm->pos.cookie.pdrv == pdrv)
1894 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1895 && (cdm->pos.cookie.periph != NULL)) {
1896 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1897 (*pdrv)->generation) {
1899 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1902 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1908 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1912 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1914 struct ccb_dev_match *cdm;
1915 dev_match_ret retval;
1917 cdm = (struct ccb_dev_match *)arg;
1919 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1921 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1922 cdm->status = CAM_DEV_MATCH_ERROR;
1927 * If the copy flag is set, copy this peripheral out.
1929 if (retval & DM_RET_COPY) {
1932 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1933 sizeof(struct dev_match_result));
1936 * If we don't have enough space to put in another
1937 * match result, save our position and tell the
1938 * user there are more devices to check.
1940 if (spaceleft < sizeof(struct dev_match_result)) {
1941 struct periph_driver **pdrv;
1944 bzero(&cdm->pos, sizeof(cdm->pos));
1945 cdm->pos.position_type =
1946 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1950 * This may look a bit non-sensical, but it is
1951 * actually quite logical. There are very few
1952 * peripheral drivers, and bloating every peripheral
1953 * structure with a pointer back to its parent
1954 * peripheral driver linker set entry would cost
1955 * more in the long run than doing this quick lookup.
1957 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1958 if (strcmp((*pdrv)->driver_name,
1959 periph->periph_name) == 0)
1963 if (*pdrv == NULL) {
1964 cdm->status = CAM_DEV_MATCH_ERROR;
1968 cdm->pos.cookie.pdrv = pdrv;
1970 * The periph generation slot does double duty, as
1971 * does the periph pointer slot. They are used for
1972 * both edt and pdrv lookups and positioning.
1974 cdm->pos.cookie.periph = periph;
1975 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1976 (*pdrv)->generation;
1977 cdm->status = CAM_DEV_MATCH_MORE;
1981 j = cdm->num_matches;
1983 cdm->matches[j].type = DEV_MATCH_PERIPH;
1984 cdm->matches[j].result.periph_result.path_id =
1985 periph->path->bus->path_id;
1988 * The transport layer peripheral doesn't have a target or
1991 if (periph->path->target)
1992 cdm->matches[j].result.periph_result.target_id =
1993 periph->path->target->target_id;
1995 cdm->matches[j].result.periph_result.target_id =
1996 CAM_TARGET_WILDCARD;
1998 if (periph->path->device)
1999 cdm->matches[j].result.periph_result.target_lun =
2000 periph->path->device->lun_id;
2002 cdm->matches[j].result.periph_result.target_lun =
2005 cdm->matches[j].result.periph_result.unit_number =
2006 periph->unit_number;
2007 strncpy(cdm->matches[j].result.periph_result.periph_name,
2008 periph->periph_name, DEV_IDLEN);
2015 xptperiphlistmatch(struct ccb_dev_match *cdm)
2019 cdm->num_matches = 0;
2022 * At this point in the edt traversal function, we check the bus
2023 * list generation to make sure that no busses have been added or
2024 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2025 * For the peripheral driver list traversal function, however, we
2026 * don't have to worry about new peripheral driver types coming or
2027 * going; they're in a linker set, and therefore can't change
2028 * without a recompile.
2031 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2032 && (cdm->pos.cookie.pdrv != NULL))
2033 ret = xptpdrvtraverse(
2034 (struct periph_driver **)cdm->pos.cookie.pdrv,
2035 xptplistpdrvfunc, cdm);
2037 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2040 * If we get back 0, that means that we had to stop before fully
2041 * traversing the peripheral driver tree. It also means that one of
2042 * the subroutines has set the status field to the proper value. If
2043 * we get back 1, we've fully traversed the EDT and copied out any
2047 cdm->status = CAM_DEV_MATCH_LAST;
2053 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2055 struct cam_eb *bus, *next_bus;
2063 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2071 for (; bus != NULL; bus = next_bus) {
2072 retval = tr_func(bus, arg);
2074 xpt_release_bus(bus);
2078 next_bus = TAILQ_NEXT(bus, links);
2080 next_bus->refcount++;
2082 xpt_release_bus(bus);
2088 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2089 xpt_targetfunc_t *tr_func, void *arg)
2091 struct cam_et *target, *next_target;
2096 target = start_target;
2098 mtx_lock(&bus->eb_mtx);
2099 target = TAILQ_FIRST(&bus->et_entries);
2100 if (target == NULL) {
2101 mtx_unlock(&bus->eb_mtx);
2105 mtx_unlock(&bus->eb_mtx);
2107 for (; target != NULL; target = next_target) {
2108 retval = tr_func(target, arg);
2110 xpt_release_target(target);
2113 mtx_lock(&bus->eb_mtx);
2114 next_target = TAILQ_NEXT(target, links);
2116 next_target->refcount++;
2117 mtx_unlock(&bus->eb_mtx);
2118 xpt_release_target(target);
2124 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2125 xpt_devicefunc_t *tr_func, void *arg)
2128 struct cam_ed *device, *next_device;
2134 device = start_device;
2136 mtx_lock(&bus->eb_mtx);
2137 device = TAILQ_FIRST(&target->ed_entries);
2138 if (device == NULL) {
2139 mtx_unlock(&bus->eb_mtx);
2143 mtx_unlock(&bus->eb_mtx);
2145 for (; device != NULL; device = next_device) {
2146 mtx_lock(&device->device_mtx);
2147 retval = tr_func(device, arg);
2148 mtx_unlock(&device->device_mtx);
2150 xpt_release_device(device);
2153 mtx_lock(&bus->eb_mtx);
2154 next_device = TAILQ_NEXT(device, links);
2156 next_device->refcount++;
2157 mtx_unlock(&bus->eb_mtx);
2158 xpt_release_device(device);
2164 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2165 xpt_periphfunc_t *tr_func, void *arg)
2168 struct cam_periph *periph, *next_periph;
2173 bus = device->target->bus;
2175 periph = start_periph;
2178 mtx_lock(&bus->eb_mtx);
2179 periph = SLIST_FIRST(&device->periphs);
2180 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2181 periph = SLIST_NEXT(periph, periph_links);
2182 if (periph == NULL) {
2183 mtx_unlock(&bus->eb_mtx);
2188 mtx_unlock(&bus->eb_mtx);
2191 for (; periph != NULL; periph = next_periph) {
2192 retval = tr_func(periph, arg);
2194 cam_periph_release_locked(periph);
2198 mtx_lock(&bus->eb_mtx);
2199 next_periph = SLIST_NEXT(periph, periph_links);
2200 while (next_periph != NULL &&
2201 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2202 next_periph = SLIST_NEXT(periph, periph_links);
2204 next_periph->refcount++;
2205 mtx_unlock(&bus->eb_mtx);
2207 cam_periph_release_locked(periph);
2213 xptpdrvtraverse(struct periph_driver **start_pdrv,
2214 xpt_pdrvfunc_t *tr_func, void *arg)
2216 struct periph_driver **pdrv;
2222 * We don't traverse the peripheral driver list like we do the
2223 * other lists, because it is a linker set, and therefore cannot be
2224 * changed during runtime. If the peripheral driver list is ever
2225 * re-done to be something other than a linker set (i.e. it can
2226 * change while the system is running), the list traversal should
2227 * be modified to work like the other traversal functions.
2229 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2230 *pdrv != NULL; pdrv++) {
2231 retval = tr_func(pdrv, arg);
2241 xptpdperiphtraverse(struct periph_driver **pdrv,
2242 struct cam_periph *start_periph,
2243 xpt_periphfunc_t *tr_func, void *arg)
2245 struct cam_periph *periph, *next_periph;
2251 periph = start_periph;
2254 periph = TAILQ_FIRST(&(*pdrv)->units);
2255 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2256 periph = TAILQ_NEXT(periph, unit_links);
2257 if (periph == NULL) {
2264 for (; periph != NULL; periph = next_periph) {
2265 cam_periph_lock(periph);
2266 retval = tr_func(periph, arg);
2267 cam_periph_unlock(periph);
2269 cam_periph_release(periph);
2273 next_periph = TAILQ_NEXT(periph, unit_links);
2274 while (next_periph != NULL &&
2275 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2276 next_periph = TAILQ_NEXT(periph, unit_links);
2278 next_periph->refcount++;
2280 cam_periph_release(periph);
2286 xptdefbusfunc(struct cam_eb *bus, void *arg)
2288 struct xpt_traverse_config *tr_config;
2290 tr_config = (struct xpt_traverse_config *)arg;
2292 if (tr_config->depth == XPT_DEPTH_BUS) {
2293 xpt_busfunc_t *tr_func;
2295 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2297 return(tr_func(bus, tr_config->tr_arg));
2299 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2303 xptdeftargetfunc(struct cam_et *target, void *arg)
2305 struct xpt_traverse_config *tr_config;
2307 tr_config = (struct xpt_traverse_config *)arg;
2309 if (tr_config->depth == XPT_DEPTH_TARGET) {
2310 xpt_targetfunc_t *tr_func;
2312 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2314 return(tr_func(target, tr_config->tr_arg));
2316 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2320 xptdefdevicefunc(struct cam_ed *device, void *arg)
2322 struct xpt_traverse_config *tr_config;
2324 tr_config = (struct xpt_traverse_config *)arg;
2326 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2327 xpt_devicefunc_t *tr_func;
2329 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2331 return(tr_func(device, tr_config->tr_arg));
2333 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2337 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2339 struct xpt_traverse_config *tr_config;
2340 xpt_periphfunc_t *tr_func;
2342 tr_config = (struct xpt_traverse_config *)arg;
2344 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2347 * Unlike the other default functions, we don't check for depth
2348 * here. The peripheral driver level is the last level in the EDT,
2349 * so if we're here, we should execute the function in question.
2351 return(tr_func(periph, tr_config->tr_arg));
2355 * Execute the given function for every bus in the EDT.
2358 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2360 struct xpt_traverse_config tr_config;
2362 tr_config.depth = XPT_DEPTH_BUS;
2363 tr_config.tr_func = tr_func;
2364 tr_config.tr_arg = arg;
2366 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2370 * Execute the given function for every device in the EDT.
2373 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2375 struct xpt_traverse_config tr_config;
2377 tr_config.depth = XPT_DEPTH_DEVICE;
2378 tr_config.tr_func = tr_func;
2379 tr_config.tr_arg = arg;
2381 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2385 xptsetasyncfunc(struct cam_ed *device, void *arg)
2387 struct cam_path path;
2388 struct ccb_getdev cgd;
2389 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2392 * Don't report unconfigured devices (Wildcard devs,
2393 * devices only for target mode, device instances
2394 * that have been invalidated but are waiting for
2395 * their last reference count to be released).
2397 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2400 xpt_compile_path(&path,
2402 device->target->bus->path_id,
2403 device->target->target_id,
2405 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2406 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2407 xpt_action((union ccb *)&cgd);
2408 csa->callback(csa->callback_arg,
2411 xpt_release_path(&path);
2417 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2419 struct cam_path path;
2420 struct ccb_pathinq cpi;
2421 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2423 xpt_compile_path(&path, /*periph*/NULL,
2425 CAM_TARGET_WILDCARD,
2427 xpt_path_lock(&path);
2428 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2429 cpi.ccb_h.func_code = XPT_PATH_INQ;
2430 xpt_action((union ccb *)&cpi);
2431 csa->callback(csa->callback_arg,
2434 xpt_path_unlock(&path);
2435 xpt_release_path(&path);
2441 xpt_action(union ccb *start_ccb)
2444 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2446 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2447 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2451 xpt_action_default(union ccb *start_ccb)
2453 struct cam_path *path;
2454 struct cam_sim *sim;
2457 path = start_ccb->ccb_h.path;
2458 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2460 switch (start_ccb->ccb_h.func_code) {
2463 struct cam_ed *device;
2466 * For the sake of compatibility with SCSI-1
2467 * devices that may not understand the identify
2468 * message, we include lun information in the
2469 * second byte of all commands. SCSI-1 specifies
2470 * that luns are a 3 bit value and reserves only 3
2471 * bits for lun information in the CDB. Later
2472 * revisions of the SCSI spec allow for more than 8
2473 * luns, but have deprecated lun information in the
2474 * CDB. So, if the lun won't fit, we must omit.
2476 * Also be aware that during initial probing for devices,
2477 * the inquiry information is unknown but initialized to 0.
2478 * This means that this code will be exercised while probing
2479 * devices with an ANSI revision greater than 2.
2481 device = path->device;
2482 if (device->protocol_version <= SCSI_REV_2
2483 && start_ccb->ccb_h.target_lun < 8
2484 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2486 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2487 start_ccb->ccb_h.target_lun << 5;
2489 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2493 case XPT_CONT_TARGET_IO:
2494 start_ccb->csio.sense_resid = 0;
2495 start_ccb->csio.resid = 0;
2498 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2499 start_ccb->ataio.resid = 0;
2505 struct cam_devq *devq;
2507 devq = path->bus->sim->devq;
2508 mtx_lock(&devq->send_mtx);
2509 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2510 if (xpt_schedule_devq(devq, path->device) != 0)
2512 mtx_unlock(&devq->send_mtx);
2515 case XPT_CALC_GEOMETRY:
2516 /* Filter out garbage */
2517 if (start_ccb->ccg.block_size == 0
2518 || start_ccb->ccg.volume_size == 0) {
2519 start_ccb->ccg.cylinders = 0;
2520 start_ccb->ccg.heads = 0;
2521 start_ccb->ccg.secs_per_track = 0;
2522 start_ccb->ccb_h.status = CAM_REQ_CMP;
2525 #if defined(PC98) || defined(__sparc64__)
2527 * In a PC-98 system, geometry translation depens on
2528 * the "real" device geometry obtained from mode page 4.
2529 * SCSI geometry translation is performed in the
2530 * initialization routine of the SCSI BIOS and the result
2531 * stored in host memory. If the translation is available
2532 * in host memory, use it. If not, rely on the default
2533 * translation the device driver performs.
2534 * For sparc64, we may need adjust the geometry of large
2535 * disks in order to fit the limitations of the 16-bit
2536 * fields of the VTOC8 disk label.
2538 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2539 start_ccb->ccb_h.status = CAM_REQ_CMP;
2546 union ccb* abort_ccb;
2548 abort_ccb = start_ccb->cab.abort_ccb;
2549 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2551 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2552 struct cam_ccbq *ccbq;
2553 struct cam_ed *device;
2555 device = abort_ccb->ccb_h.path->device;
2556 ccbq = &device->ccbq;
2557 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2558 abort_ccb->ccb_h.status =
2559 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2560 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2561 xpt_done(abort_ccb);
2562 start_ccb->ccb_h.status = CAM_REQ_CMP;
2565 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2566 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2568 * We've caught this ccb en route to
2569 * the SIM. Flag it for abort and the
2570 * SIM will do so just before starting
2571 * real work on the CCB.
2573 abort_ccb->ccb_h.status =
2574 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2575 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2576 start_ccb->ccb_h.status = CAM_REQ_CMP;
2580 if (XPT_FC_IS_QUEUED(abort_ccb)
2581 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2583 * It's already completed but waiting
2584 * for our SWI to get to it.
2586 start_ccb->ccb_h.status = CAM_UA_ABORT;
2590 * If we weren't able to take care of the abort request
2591 * in the XPT, pass the request down to the SIM for processing.
2595 case XPT_ACCEPT_TARGET_IO:
2597 case XPT_IMMED_NOTIFY:
2598 case XPT_NOTIFY_ACK:
2600 case XPT_IMMEDIATE_NOTIFY:
2601 case XPT_NOTIFY_ACKNOWLEDGE:
2602 case XPT_GET_SIM_KNOB:
2603 case XPT_SET_SIM_KNOB:
2604 case XPT_GET_TRAN_SETTINGS:
2605 case XPT_SET_TRAN_SETTINGS:
2608 sim = path->bus->sim;
2609 lock = (mtx_owned(sim->mtx) == 0);
2612 (*(sim->sim_action))(sim, start_ccb);
2614 CAM_SIM_UNLOCK(sim);
2616 case XPT_PATH_STATS:
2617 start_ccb->cpis.last_reset = path->bus->last_reset;
2618 start_ccb->ccb_h.status = CAM_REQ_CMP;
2625 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2626 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2628 struct ccb_getdev *cgd;
2630 cgd = &start_ccb->cgd;
2631 cgd->protocol = dev->protocol;
2632 cgd->inq_data = dev->inq_data;
2633 cgd->ident_data = dev->ident_data;
2634 cgd->inq_flags = dev->inq_flags;
2635 cgd->ccb_h.status = CAM_REQ_CMP;
2636 cgd->serial_num_len = dev->serial_num_len;
2637 if ((dev->serial_num_len > 0)
2638 && (dev->serial_num != NULL))
2639 bcopy(dev->serial_num, cgd->serial_num,
2640 dev->serial_num_len);
2644 case XPT_GDEV_STATS:
2649 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2650 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2652 struct ccb_getdevstats *cgds;
2656 cgds = &start_ccb->cgds;
2659 cgds->dev_openings = dev->ccbq.dev_openings;
2660 cgds->dev_active = dev->ccbq.dev_active;
2661 cgds->devq_openings = dev->ccbq.devq_openings;
2662 cgds->devq_queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2663 cgds->held = dev->ccbq.held;
2664 cgds->last_reset = tar->last_reset;
2665 cgds->maxtags = dev->maxtags;
2666 cgds->mintags = dev->mintags;
2667 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2668 cgds->last_reset = bus->last_reset;
2669 cgds->ccb_h.status = CAM_REQ_CMP;
2675 struct cam_periph *nperiph;
2676 struct periph_list *periph_head;
2677 struct ccb_getdevlist *cgdl;
2679 struct cam_ed *device;
2686 * Don't want anyone mucking with our data.
2688 device = path->device;
2689 periph_head = &device->periphs;
2690 cgdl = &start_ccb->cgdl;
2693 * Check and see if the list has changed since the user
2694 * last requested a list member. If so, tell them that the
2695 * list has changed, and therefore they need to start over
2696 * from the beginning.
2698 if ((cgdl->index != 0) &&
2699 (cgdl->generation != device->generation)) {
2700 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2705 * Traverse the list of peripherals and attempt to find
2706 * the requested peripheral.
2708 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2709 (nperiph != NULL) && (i <= cgdl->index);
2710 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2711 if (i == cgdl->index) {
2712 strncpy(cgdl->periph_name,
2713 nperiph->periph_name,
2715 cgdl->unit_number = nperiph->unit_number;
2720 cgdl->status = CAM_GDEVLIST_ERROR;
2724 if (nperiph == NULL)
2725 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2727 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2730 cgdl->generation = device->generation;
2732 cgdl->ccb_h.status = CAM_REQ_CMP;
2737 dev_pos_type position_type;
2738 struct ccb_dev_match *cdm;
2740 cdm = &start_ccb->cdm;
2743 * There are two ways of getting at information in the EDT.
2744 * The first way is via the primary EDT tree. It starts
2745 * with a list of busses, then a list of targets on a bus,
2746 * then devices/luns on a target, and then peripherals on a
2747 * device/lun. The "other" way is by the peripheral driver
2748 * lists. The peripheral driver lists are organized by
2749 * peripheral driver. (obviously) So it makes sense to
2750 * use the peripheral driver list if the user is looking
2751 * for something like "da1", or all "da" devices. If the
2752 * user is looking for something on a particular bus/target
2753 * or lun, it's generally better to go through the EDT tree.
2756 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2757 position_type = cdm->pos.position_type;
2761 position_type = CAM_DEV_POS_NONE;
2763 for (i = 0; i < cdm->num_patterns; i++) {
2764 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2765 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2766 position_type = CAM_DEV_POS_EDT;
2771 if (cdm->num_patterns == 0)
2772 position_type = CAM_DEV_POS_EDT;
2773 else if (position_type == CAM_DEV_POS_NONE)
2774 position_type = CAM_DEV_POS_PDRV;
2777 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2778 case CAM_DEV_POS_EDT:
2781 case CAM_DEV_POS_PDRV:
2782 xptperiphlistmatch(cdm);
2785 cdm->status = CAM_DEV_MATCH_ERROR;
2789 if (cdm->status == CAM_DEV_MATCH_ERROR)
2790 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2792 start_ccb->ccb_h.status = CAM_REQ_CMP;
2798 struct ccb_setasync *csa;
2799 struct async_node *cur_entry;
2800 struct async_list *async_head;
2803 csa = &start_ccb->csa;
2804 added = csa->event_enable;
2805 async_head = &path->device->asyncs;
2808 * If there is already an entry for us, simply
2811 cur_entry = SLIST_FIRST(async_head);
2812 while (cur_entry != NULL) {
2813 if ((cur_entry->callback_arg == csa->callback_arg)
2814 && (cur_entry->callback == csa->callback))
2816 cur_entry = SLIST_NEXT(cur_entry, links);
2819 if (cur_entry != NULL) {
2821 * If the request has no flags set,
2824 added &= ~cur_entry->event_enable;
2825 if (csa->event_enable == 0) {
2826 SLIST_REMOVE(async_head, cur_entry,
2828 xpt_release_device(path->device);
2829 free(cur_entry, M_CAMXPT);
2831 cur_entry->event_enable = csa->event_enable;
2833 csa->event_enable = added;
2835 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2837 if (cur_entry == NULL) {
2838 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2841 cur_entry->event_enable = csa->event_enable;
2842 cur_entry->event_lock =
2843 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2844 cur_entry->callback_arg = csa->callback_arg;
2845 cur_entry->callback = csa->callback;
2846 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2847 xpt_acquire_device(path->device);
2849 start_ccb->ccb_h.status = CAM_REQ_CMP;
2854 struct ccb_relsim *crs;
2857 crs = &start_ccb->crs;
2861 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2865 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2867 /* Don't ever go below one opening */
2868 if (crs->openings > 0) {
2869 xpt_dev_ccbq_resize(path, crs->openings);
2872 "number of openings is now %d\n",
2878 mtx_lock(&dev->sim->devq->send_mtx);
2879 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2881 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2884 * Just extend the old timeout and decrement
2885 * the freeze count so that a single timeout
2886 * is sufficient for releasing the queue.
2888 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2889 callout_stop(&dev->callout);
2892 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2895 callout_reset(&dev->callout,
2896 (crs->release_timeout * hz) / 1000,
2897 xpt_release_devq_timeout, dev);
2899 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2903 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2905 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2907 * Decrement the freeze count so that a single
2908 * completion is still sufficient to unfreeze
2911 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2914 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2915 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2919 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2921 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2922 || (dev->ccbq.dev_active == 0)) {
2924 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2927 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2928 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2931 mtx_unlock(&dev->sim->devq->send_mtx);
2933 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2934 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2935 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2936 start_ccb->ccb_h.status = CAM_REQ_CMP;
2940 struct cam_path *oldpath;
2942 /* Check that all request bits are supported. */
2943 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2944 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2948 cam_dflags = CAM_DEBUG_NONE;
2949 if (cam_dpath != NULL) {
2950 oldpath = cam_dpath;
2952 xpt_free_path(oldpath);
2954 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2955 if (xpt_create_path(&cam_dpath, NULL,
2956 start_ccb->ccb_h.path_id,
2957 start_ccb->ccb_h.target_id,
2958 start_ccb->ccb_h.target_lun) !=
2960 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2962 cam_dflags = start_ccb->cdbg.flags;
2963 start_ccb->ccb_h.status = CAM_REQ_CMP;
2964 xpt_print(cam_dpath, "debugging flags now %x\n",
2968 start_ccb->ccb_h.status = CAM_REQ_CMP;
2972 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2973 xpt_freeze_devq(path, 1);
2974 start_ccb->ccb_h.status = CAM_REQ_CMP;
2981 printf("%s: CCB type %#x not supported\n", __func__,
2982 start_ccb->ccb_h.func_code);
2983 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2984 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2985 xpt_done(start_ccb);
2992 xpt_polled_action(union ccb *start_ccb)
2995 struct cam_sim *sim;
2996 struct cam_devq *devq;
2999 timeout = start_ccb->ccb_h.timeout * 10;
3000 sim = start_ccb->ccb_h.path->bus->sim;
3002 dev = start_ccb->ccb_h.path->device;
3004 mtx_unlock(&dev->device_mtx);
3007 * Steal an opening so that no other queued requests
3008 * can get it before us while we simulate interrupts.
3010 mtx_lock(&devq->send_mtx);
3011 dev->ccbq.devq_openings--;
3012 dev->ccbq.dev_openings--;
3013 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3015 mtx_unlock(&devq->send_mtx);
3018 (*(sim->sim_poll))(sim);
3019 CAM_SIM_UNLOCK(sim);
3021 mtx_lock(&devq->send_mtx);
3023 dev->ccbq.devq_openings++;
3024 dev->ccbq.dev_openings++;
3025 mtx_unlock(&devq->send_mtx);
3028 xpt_action(start_ccb);
3029 while(--timeout > 0) {
3031 (*(sim->sim_poll))(sim);
3032 CAM_SIM_UNLOCK(sim);
3034 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3041 * XXX Is it worth adding a sim_timeout entry
3042 * point so we can attempt recovery? If
3043 * this is only used for dumps, I don't think
3046 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3049 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3052 mtx_lock(&dev->device_mtx);
3056 * Schedule a peripheral driver to receive a ccb when it's
3057 * target device has space for more transactions.
3060 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3063 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3064 cam_periph_assert(periph, MA_OWNED);
3065 if (new_priority < periph->scheduled_priority) {
3066 periph->scheduled_priority = new_priority;
3067 xpt_run_allocq(periph, 0);
3073 * Schedule a device to run on a given queue.
3074 * If the device was inserted as a new entry on the queue,
3075 * return 1 meaning the device queue should be run. If we
3076 * were already queued, implying someone else has already
3077 * started the queue, return 0 so the caller doesn't attempt
3081 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3082 u_int32_t new_priority)
3085 u_int32_t old_priority;
3087 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3089 old_priority = pinfo->priority;
3092 * Are we already queued?
3094 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3095 /* Simply reorder based on new priority */
3096 if (new_priority < old_priority) {
3097 camq_change_priority(queue, pinfo->index,
3099 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3100 ("changed priority to %d\n",
3106 /* New entry on the queue */
3107 if (new_priority < old_priority)
3108 pinfo->priority = new_priority;
3110 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3111 ("Inserting onto queue\n"));
3112 pinfo->generation = ++queue->generation;
3113 camq_insert(queue, pinfo);
3120 xpt_run_allocq_task(void *context, int pending)
3122 struct cam_periph *periph = context;
3124 cam_periph_lock(periph);
3125 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3126 xpt_run_allocq(periph, 1);
3127 cam_periph_unlock(periph);
3128 cam_periph_release(periph);
3132 xpt_run_allocq(struct cam_periph *periph, int sleep)
3134 struct cam_ed *device;
3138 cam_periph_assert(periph, MA_OWNED);
3139 if (periph->periph_allocating)
3141 periph->periph_allocating = 1;
3142 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3143 device = periph->path->device;
3146 while ((prio = min(periph->scheduled_priority,
3147 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3148 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3149 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3152 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3154 ccb = xpt_get_ccb(periph);
3157 if (periph->flags & CAM_PERIPH_RUN_TASK)
3159 cam_periph_doacquire(periph);
3160 periph->flags |= CAM_PERIPH_RUN_TASK;
3161 taskqueue_enqueue(xsoftc.xpt_taskq,
3162 &periph->periph_run_task);
3165 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3166 if (prio == periph->immediate_priority) {
3167 periph->immediate_priority = CAM_PRIORITY_NONE;
3168 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3169 ("waking cam_periph_getccb()\n"));
3170 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3172 wakeup(&periph->ccb_list);
3174 periph->scheduled_priority = CAM_PRIORITY_NONE;
3175 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3176 ("calling periph_start()\n"));
3177 periph->periph_start(periph, ccb);
3182 xpt_release_ccb(ccb);
3183 periph->periph_allocating = 0;
3187 xpt_run_devq(struct cam_devq *devq)
3189 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3192 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3194 devq->send_queue.qfrozen_cnt++;
3195 while ((devq->send_queue.entries > 0)
3196 && (devq->send_openings > 0)
3197 && (devq->send_queue.qfrozen_cnt <= 1)) {
3198 struct cam_ed *device;
3199 union ccb *work_ccb;
3200 struct cam_sim *sim;
3202 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3205 ("running device %p\n", device));
3207 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3208 if (work_ccb == NULL) {
3209 printf("device on run queue with no ccbs???\n");
3213 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3215 mtx_lock(&xsoftc.xpt_highpower_lock);
3216 if (xsoftc.num_highpower <= 0) {
3218 * We got a high power command, but we
3219 * don't have any available slots. Freeze
3220 * the device queue until we have a slot
3223 xpt_freeze_devq_device(device, 1);
3224 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3227 mtx_unlock(&xsoftc.xpt_highpower_lock);
3231 * Consume a high power slot while
3234 xsoftc.num_highpower--;
3236 mtx_unlock(&xsoftc.xpt_highpower_lock);
3238 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3239 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3240 devq->send_openings--;
3241 devq->send_active++;
3242 xpt_schedule_devq(devq, device);
3243 mtx_unlock(&devq->send_mtx);
3245 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3247 * The client wants to freeze the queue
3248 * after this CCB is sent.
3250 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3253 /* In Target mode, the peripheral driver knows best... */
3254 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3255 if ((device->inq_flags & SID_CmdQue) != 0
3256 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3257 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3260 * Clear this in case of a retried CCB that
3261 * failed due to a rejected tag.
3263 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3266 switch (work_ccb->ccb_h.func_code) {
3268 CAM_DEBUG(work_ccb->ccb_h.path,
3269 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3270 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3272 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3273 cdb_str, sizeof(cdb_str))));
3276 CAM_DEBUG(work_ccb->ccb_h.path,
3277 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3278 ata_op_string(&work_ccb->ataio.cmd),
3279 ata_cmd_string(&work_ccb->ataio.cmd,
3280 cdb_str, sizeof(cdb_str))));
3287 * Device queues can be shared among multiple SIM instances
3288 * that reside on different busses. Use the SIM from the
3289 * queued device, rather than the one from the calling bus.
3292 lock = (mtx_owned(sim->mtx) == 0);
3295 (*(sim->sim_action))(sim, work_ccb);
3297 CAM_SIM_UNLOCK(sim);
3298 mtx_lock(&devq->send_mtx);
3300 devq->send_queue.qfrozen_cnt--;
3304 * This function merges stuff from the slave ccb into the master ccb, while
3305 * keeping important fields in the master ccb constant.
3308 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3312 * Pull fields that are valid for peripheral drivers to set
3313 * into the master CCB along with the CCB "payload".
3315 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3316 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3317 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3318 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3319 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3320 sizeof(union ccb) - sizeof(struct ccb_hdr));
3324 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3327 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3328 ccb_h->pinfo.priority = priority;
3330 ccb_h->path_id = path->bus->path_id;
3332 ccb_h->target_id = path->target->target_id;
3334 ccb_h->target_id = CAM_TARGET_WILDCARD;
3336 ccb_h->target_lun = path->device->lun_id;
3337 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3339 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3341 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3346 /* Path manipulation functions */
3348 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3349 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3351 struct cam_path *path;
3354 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3357 status = CAM_RESRC_UNAVAIL;
3360 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3361 if (status != CAM_REQ_CMP) {
3362 free(path, M_CAMPATH);
3365 *new_path_ptr = path;
3370 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3371 struct cam_periph *periph, path_id_t path_id,
3372 target_id_t target_id, lun_id_t lun_id)
3375 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3380 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3381 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3384 struct cam_et *target;
3385 struct cam_ed *device;
3388 status = CAM_REQ_CMP; /* Completed without error */
3389 target = NULL; /* Wildcarded */
3390 device = NULL; /* Wildcarded */
3393 * We will potentially modify the EDT, so block interrupts
3394 * that may attempt to create cam paths.
3396 bus = xpt_find_bus(path_id);
3398 status = CAM_PATH_INVALID;
3401 mtx_lock(&bus->eb_mtx);
3402 target = xpt_find_target(bus, target_id);
3403 if (target == NULL) {
3405 struct cam_et *new_target;
3407 new_target = xpt_alloc_target(bus, target_id);
3408 if (new_target == NULL) {
3409 status = CAM_RESRC_UNAVAIL;
3411 target = new_target;
3415 if (target != NULL) {
3416 device = xpt_find_device(target, lun_id);
3417 if (device == NULL) {
3419 struct cam_ed *new_device;
3422 (*(bus->xport->alloc_device))(bus,
3425 if (new_device == NULL) {
3426 status = CAM_RESRC_UNAVAIL;
3428 device = new_device;
3432 mtx_unlock(&bus->eb_mtx);
3436 * Only touch the user's data if we are successful.
3438 if (status == CAM_REQ_CMP) {
3439 new_path->periph = perph;
3440 new_path->bus = bus;
3441 new_path->target = target;
3442 new_path->device = device;
3443 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3446 xpt_release_device(device);
3448 xpt_release_target(target);
3450 xpt_release_bus(bus);
3456 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3458 struct cam_path *new_path;
3460 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3461 if (new_path == NULL)
3462 return(CAM_RESRC_UNAVAIL);
3463 xpt_copy_path(new_path, path);
3464 *new_path_ptr = new_path;
3465 return (CAM_REQ_CMP);
3469 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3473 if (path->bus != NULL)
3474 xpt_acquire_bus(path->bus);
3475 if (path->target != NULL)
3476 xpt_acquire_target(path->target);
3477 if (path->device != NULL)
3478 xpt_acquire_device(path->device);
3482 xpt_release_path(struct cam_path *path)
3484 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3485 if (path->device != NULL) {
3486 xpt_release_device(path->device);
3487 path->device = NULL;
3489 if (path->target != NULL) {
3490 xpt_release_target(path->target);
3491 path->target = NULL;
3493 if (path->bus != NULL) {
3494 xpt_release_bus(path->bus);
3500 xpt_free_path(struct cam_path *path)
3503 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3504 xpt_release_path(path);
3505 free(path, M_CAMPATH);
3509 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3510 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3516 *bus_ref = path->bus->refcount;
3522 *periph_ref = path->periph->refcount;
3529 *target_ref = path->target->refcount;
3535 *device_ref = path->device->refcount;
3542 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3543 * in path1, 2 for match with wildcards in path2.
3546 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3550 if (path1->bus != path2->bus) {
3551 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3553 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3558 if (path1->target != path2->target) {
3559 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3562 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3567 if (path1->device != path2->device) {
3568 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3571 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3580 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3584 if (path->bus != dev->target->bus) {
3585 if (path->bus->path_id == CAM_BUS_WILDCARD)
3587 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3592 if (path->target != dev->target) {
3593 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3596 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3601 if (path->device != dev) {
3602 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3605 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3614 xpt_print_path(struct cam_path *path)
3618 printf("(nopath): ");
3620 if (path->periph != NULL)
3621 printf("(%s%d:", path->periph->periph_name,
3622 path->periph->unit_number);
3624 printf("(noperiph:");
3626 if (path->bus != NULL)
3627 printf("%s%d:%d:", path->bus->sim->sim_name,
3628 path->bus->sim->unit_number,
3629 path->bus->sim->bus_id);
3633 if (path->target != NULL)
3634 printf("%d:", path->target->target_id);
3638 if (path->device != NULL)
3639 printf("%jx): ", (uintmax_t)path->device->lun_id);
3646 xpt_print_device(struct cam_ed *device)
3650 printf("(nopath): ");
3652 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3653 device->sim->unit_number,
3654 device->sim->bus_id,
3655 device->target->target_id,
3656 (uintmax_t)device->lun_id);
3661 xpt_print(struct cam_path *path, const char *fmt, ...)
3664 xpt_print_path(path);
3671 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3675 sbuf_new(&sb, str, str_len, 0);
3678 sbuf_printf(&sb, "(nopath): ");
3680 if (path->periph != NULL)
3681 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3682 path->periph->unit_number);
3684 sbuf_printf(&sb, "(noperiph:");
3686 if (path->bus != NULL)
3687 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3688 path->bus->sim->unit_number,
3689 path->bus->sim->bus_id);
3691 sbuf_printf(&sb, "nobus:");
3693 if (path->target != NULL)
3694 sbuf_printf(&sb, "%d:", path->target->target_id);
3696 sbuf_printf(&sb, "X:");
3698 if (path->device != NULL)
3699 sbuf_printf(&sb, "%jx): ",
3700 (uintmax_t)path->device->lun_id);
3702 sbuf_printf(&sb, "X): ");
3706 return(sbuf_len(&sb));
3710 xpt_path_path_id(struct cam_path *path)
3712 return(path->bus->path_id);
3716 xpt_path_target_id(struct cam_path *path)
3718 if (path->target != NULL)
3719 return (path->target->target_id);
3721 return (CAM_TARGET_WILDCARD);
3725 xpt_path_lun_id(struct cam_path *path)
3727 if (path->device != NULL)
3728 return (path->device->lun_id);
3730 return (CAM_LUN_WILDCARD);
3734 xpt_path_sim(struct cam_path *path)
3737 return (path->bus->sim);
3741 xpt_path_periph(struct cam_path *path)
3744 return (path->periph);
3748 xpt_path_legacy_ata_id(struct cam_path *path)
3753 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3754 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3755 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3756 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3759 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3760 path->bus->sim->unit_number < 2) {
3761 bus_id = path->bus->sim->unit_number;
3765 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3766 if (bus == path->bus)
3768 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3769 bus->sim->unit_number >= 2) ||
3770 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3771 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3772 strcmp(bus->sim->sim_name, "siisch") == 0)
3777 if (path->target != NULL) {
3778 if (path->target->target_id < 2)
3779 return (bus_id * 2 + path->target->target_id);
3783 return (bus_id * 2);
3787 * Release a CAM control block for the caller. Remit the cost of the structure
3788 * to the device referenced by the path. If the this device had no 'credits'
3789 * and peripheral drivers have registered async callbacks for this notification
3793 xpt_release_ccb(union ccb *free_ccb)
3795 struct cam_ed *device;
3796 struct cam_periph *periph;
3798 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3799 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3800 device = free_ccb->ccb_h.path->device;
3801 periph = free_ccb->ccb_h.path->periph;
3803 xpt_free_ccb(free_ccb);
3804 periph->periph_allocated--;
3805 cam_ccbq_release_opening(&device->ccbq);
3806 xpt_run_allocq(periph, 0);
3809 /* Functions accessed by SIM drivers */
3811 static struct xpt_xport xport_default = {
3812 .alloc_device = xpt_alloc_device_default,
3813 .action = xpt_action_default,
3814 .async = xpt_dev_async_default,
3818 * A sim structure, listing the SIM entry points and instance
3819 * identification info is passed to xpt_bus_register to hook the SIM
3820 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3821 * for this new bus and places it in the array of busses and assigns
3822 * it a path_id. The path_id may be influenced by "hard wiring"
3823 * information specified by the user. Once interrupt services are
3824 * available, the bus will be probed.
3827 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3829 struct cam_eb *new_bus;
3830 struct cam_eb *old_bus;
3831 struct ccb_pathinq cpi;
3832 struct cam_path *path;
3835 mtx_assert(sim->mtx, MA_OWNED);
3838 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3839 M_CAMXPT, M_NOWAIT|M_ZERO);
3840 if (new_bus == NULL) {
3841 /* Couldn't satisfy request */
3842 return (CAM_RESRC_UNAVAIL);
3845 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3846 TAILQ_INIT(&new_bus->et_entries);
3849 timevalclear(&new_bus->last_reset);
3851 new_bus->refcount = 1; /* Held until a bus_deregister event */
3852 new_bus->generation = 0;
3855 sim->path_id = new_bus->path_id =
3856 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3857 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3858 while (old_bus != NULL
3859 && old_bus->path_id < new_bus->path_id)
3860 old_bus = TAILQ_NEXT(old_bus, links);
3861 if (old_bus != NULL)
3862 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3864 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3865 xsoftc.bus_generation++;
3869 * Set a default transport so that a PATH_INQ can be issued to
3870 * the SIM. This will then allow for probing and attaching of
3871 * a more appropriate transport.
3873 new_bus->xport = &xport_default;
3875 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3876 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3877 if (status != CAM_REQ_CMP) {
3878 xpt_release_bus(new_bus);
3879 free(path, M_CAMXPT);
3880 return (CAM_RESRC_UNAVAIL);
3883 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3884 cpi.ccb_h.func_code = XPT_PATH_INQ;
3885 xpt_action((union ccb *)&cpi);
3887 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3888 switch (cpi.transport) {
3896 new_bus->xport = scsi_get_xport();
3900 new_bus->xport = ata_get_xport();
3903 new_bus->xport = &xport_default;
3908 /* Notify interested parties */
3909 if (sim->path_id != CAM_XPT_PATH_ID) {
3911 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3912 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3913 union ccb *scan_ccb;
3915 /* Initiate bus rescan. */
3916 scan_ccb = xpt_alloc_ccb_nowait();
3917 if (scan_ccb != NULL) {
3918 scan_ccb->ccb_h.path = path;
3919 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3920 scan_ccb->crcn.flags = 0;
3921 xpt_rescan(scan_ccb);
3924 "Can't allocate CCB to scan bus\n");
3925 xpt_free_path(path);
3928 xpt_free_path(path);
3930 xpt_free_path(path);
3931 return (CAM_SUCCESS);
3935 xpt_bus_deregister(path_id_t pathid)
3937 struct cam_path bus_path;
3940 status = xpt_compile_path(&bus_path, NULL, pathid,
3941 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3942 if (status != CAM_REQ_CMP)
3945 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3946 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3948 /* Release the reference count held while registered. */
3949 xpt_release_bus(bus_path.bus);
3950 xpt_release_path(&bus_path);
3952 return (CAM_REQ_CMP);
3956 xptnextfreepathid(void)
3962 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3964 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3966 /* Find an unoccupied pathid */
3967 while (bus != NULL && bus->path_id <= pathid) {
3968 if (bus->path_id == pathid)
3970 bus = TAILQ_NEXT(bus, links);
3974 * Ensure that this pathid is not reserved for
3975 * a bus that may be registered in the future.
3977 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3979 /* Start the search over */
3986 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3993 pathid = CAM_XPT_PATH_ID;
3994 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3995 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
3998 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3999 if (strcmp(dname, "scbus")) {
4000 /* Avoid a bit of foot shooting. */
4003 if (dunit < 0) /* unwired?! */
4005 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4006 if (sim_bus == val) {
4010 } else if (sim_bus == 0) {
4011 /* Unspecified matches bus 0 */
4015 printf("Ambiguous scbus configuration for %s%d "
4016 "bus %d, cannot wire down. The kernel "
4017 "config entry for scbus%d should "
4018 "specify a controller bus.\n"
4019 "Scbus will be assigned dynamically.\n",
4020 sim_name, sim_unit, sim_bus, dunit);
4025 if (pathid == CAM_XPT_PATH_ID)
4026 pathid = xptnextfreepathid();
4031 xpt_async_string(u_int32_t async_code)
4034 switch (async_code) {
4035 case AC_BUS_RESET: return ("AC_BUS_RESET");
4036 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4037 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4038 case AC_SENT_BDR: return ("AC_SENT_BDR");
4039 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4040 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4041 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4042 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4043 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4044 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4045 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4046 case AC_CONTRACT: return ("AC_CONTRACT");
4047 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4048 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4050 return ("AC_UNKNOWN");
4054 xpt_async_size(u_int32_t async_code)
4057 switch (async_code) {
4058 case AC_BUS_RESET: return (0);
4059 case AC_UNSOL_RESEL: return (0);
4060 case AC_SCSI_AEN: return (0);
4061 case AC_SENT_BDR: return (0);
4062 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4063 case AC_PATH_DEREGISTERED: return (0);
4064 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4065 case AC_LOST_DEVICE: return (0);
4066 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4067 case AC_INQ_CHANGED: return (0);
4068 case AC_GETDEV_CHANGED: return (0);
4069 case AC_CONTRACT: return (sizeof(struct ac_contract));
4070 case AC_ADVINFO_CHANGED: return (-1);
4071 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4077 xpt_async_process_dev(struct cam_ed *device, void *arg)
4079 union ccb *ccb = arg;
4080 struct cam_path *path = ccb->ccb_h.path;
4081 void *async_arg = ccb->casync.async_arg_ptr;
4082 u_int32_t async_code = ccb->casync.async_code;
4085 if (path->device != device
4086 && path->device->lun_id != CAM_LUN_WILDCARD
4087 && device->lun_id != CAM_LUN_WILDCARD)
4091 * The async callback could free the device.
4092 * If it is a broadcast async, it doesn't hold
4093 * device reference, so take our own reference.
4095 xpt_acquire_device(device);
4098 * If async for specific device is to be delivered to
4099 * the wildcard client, take the specific device lock.
4100 * XXX: We may need a way for client to specify it.
4102 if ((device->lun_id == CAM_LUN_WILDCARD &&
4103 path->device->lun_id != CAM_LUN_WILDCARD) ||
4104 (device->target->target_id == CAM_TARGET_WILDCARD &&
4105 path->target->target_id != CAM_TARGET_WILDCARD) ||
4106 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4107 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4108 mtx_unlock(&device->device_mtx);
4109 xpt_path_lock(path);
4114 (*(device->target->bus->xport->async))(async_code,
4115 device->target->bus, device->target, device, async_arg);
4116 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4119 xpt_path_unlock(path);
4120 mtx_lock(&device->device_mtx);
4122 xpt_release_device(device);
4127 xpt_async_process_tgt(struct cam_et *target, void *arg)
4129 union ccb *ccb = arg;
4130 struct cam_path *path = ccb->ccb_h.path;
4132 if (path->target != target
4133 && path->target->target_id != CAM_TARGET_WILDCARD
4134 && target->target_id != CAM_TARGET_WILDCARD)
4137 if (ccb->casync.async_code == AC_SENT_BDR) {
4138 /* Update our notion of when the last reset occurred */
4139 microtime(&target->last_reset);
4142 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4146 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4149 struct cam_path *path;
4151 u_int32_t async_code;
4153 path = ccb->ccb_h.path;
4154 async_code = ccb->casync.async_code;
4155 async_arg = ccb->casync.async_arg_ptr;
4156 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4157 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4160 if (async_code == AC_BUS_RESET) {
4161 /* Update our notion of when the last reset occurred */
4162 microtime(&bus->last_reset);
4165 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4168 * If this wasn't a fully wildcarded async, tell all
4169 * clients that want all async events.
4171 if (bus != xpt_periph->path->bus) {
4172 xpt_path_lock(xpt_periph->path);
4173 xpt_async_process_dev(xpt_periph->path->device, ccb);
4174 xpt_path_unlock(xpt_periph->path);
4177 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4178 xpt_release_devq(path, 1, TRUE);
4180 xpt_release_simq(path->bus->sim, TRUE);
4181 if (ccb->casync.async_arg_size > 0)
4182 free(async_arg, M_CAMXPT);
4183 xpt_free_path(path);
4188 xpt_async_bcast(struct async_list *async_head,
4189 u_int32_t async_code,
4190 struct cam_path *path, void *async_arg)
4192 struct async_node *cur_entry;
4195 cur_entry = SLIST_FIRST(async_head);
4196 while (cur_entry != NULL) {
4197 struct async_node *next_entry;
4199 * Grab the next list entry before we call the current
4200 * entry's callback. This is because the callback function
4201 * can delete its async callback entry.
4203 next_entry = SLIST_NEXT(cur_entry, links);
4204 if ((cur_entry->event_enable & async_code) != 0) {
4205 lock = cur_entry->event_lock;
4207 CAM_SIM_LOCK(path->device->sim);
4208 cur_entry->callback(cur_entry->callback_arg,
4212 CAM_SIM_UNLOCK(path->device->sim);
4214 cur_entry = next_entry;
4219 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4224 ccb = xpt_alloc_ccb_nowait();
4226 xpt_print(path, "Can't allocate CCB to send %s\n",
4227 xpt_async_string(async_code));
4231 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4232 xpt_print(path, "Can't allocate path to send %s\n",
4233 xpt_async_string(async_code));
4237 ccb->ccb_h.path->periph = NULL;
4238 ccb->ccb_h.func_code = XPT_ASYNC;
4239 ccb->ccb_h.cbfcnp = xpt_async_process;
4240 ccb->ccb_h.flags |= CAM_UNLOCKED;
4241 ccb->casync.async_code = async_code;
4242 ccb->casync.async_arg_size = 0;
4243 size = xpt_async_size(async_code);
4244 if (size > 0 && async_arg != NULL) {
4245 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4246 if (ccb->casync.async_arg_ptr == NULL) {
4247 xpt_print(path, "Can't allocate argument to send %s\n",
4248 xpt_async_string(async_code));
4249 xpt_free_path(ccb->ccb_h.path);
4253 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4254 ccb->casync.async_arg_size = size;
4255 } else if (size < 0)
4256 ccb->casync.async_arg_size = size;
4257 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4258 xpt_freeze_devq(path, 1);
4260 xpt_freeze_simq(path->bus->sim, 1);
4265 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4266 struct cam_et *target, struct cam_ed *device,
4271 * We only need to handle events for real devices.
4273 if (target->target_id == CAM_TARGET_WILDCARD
4274 || device->lun_id == CAM_LUN_WILDCARD)
4277 printf("%s called\n", __func__);
4281 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4283 struct cam_devq *devq;
4286 devq = dev->sim->devq;
4287 mtx_assert(&devq->send_mtx, MA_OWNED);
4288 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4289 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4290 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4291 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4292 /* Remove frozen device from sendq. */
4293 if (device_is_queued(dev))
4294 camq_remove(&devq->send_queue, dev->devq_entry.index);
4299 xpt_freeze_devq(struct cam_path *path, u_int count)
4301 struct cam_ed *dev = path->device;
4302 struct cam_devq *devq;
4305 devq = dev->sim->devq;
4306 mtx_lock(&devq->send_mtx);
4307 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4308 freeze = xpt_freeze_devq_device(dev, count);
4309 mtx_unlock(&devq->send_mtx);
4314 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4316 struct cam_devq *devq;
4320 mtx_lock(&devq->send_mtx);
4321 freeze = (devq->send_queue.qfrozen_cnt += count);
4322 mtx_unlock(&devq->send_mtx);
4327 xpt_release_devq_timeout(void *arg)
4330 struct cam_devq *devq;
4332 dev = (struct cam_ed *)arg;
4333 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4334 devq = dev->sim->devq;
4335 mtx_assert(&devq->send_mtx, MA_OWNED);
4336 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4341 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4344 struct cam_devq *devq;
4346 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4349 devq = dev->sim->devq;
4350 mtx_lock(&devq->send_mtx);
4351 if (xpt_release_devq_device(dev, count, run_queue))
4352 xpt_run_devq(dev->sim->devq);
4353 mtx_unlock(&devq->send_mtx);
4357 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4360 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4361 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4362 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4363 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4364 if (count > dev->ccbq.queue.qfrozen_cnt) {
4366 printf("xpt_release_devq(): requested %u > present %u\n",
4367 count, dev->ccbq.queue.qfrozen_cnt);
4369 count = dev->ccbq.queue.qfrozen_cnt;
4371 dev->ccbq.queue.qfrozen_cnt -= count;
4372 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4374 * No longer need to wait for a successful
4375 * command completion.
4377 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4379 * Remove any timeouts that might be scheduled
4380 * to release this queue.
4382 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4383 callout_stop(&dev->callout);
4384 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4387 * Now that we are unfrozen schedule the
4388 * device so any pending transactions are
4391 xpt_schedule_devq(dev->sim->devq, dev);
4398 xpt_release_simq(struct cam_sim *sim, int run_queue)
4400 struct cam_devq *devq;
4403 mtx_lock(&devq->send_mtx);
4404 if (devq->send_queue.qfrozen_cnt <= 0) {
4406 printf("xpt_release_simq: requested 1 > present %u\n",
4407 devq->send_queue.qfrozen_cnt);
4410 devq->send_queue.qfrozen_cnt--;
4411 if (devq->send_queue.qfrozen_cnt == 0) {
4413 * If there is a timeout scheduled to release this
4414 * sim queue, remove it. The queue frozen count is
4417 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4418 callout_stop(&sim->callout);
4419 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4423 * Now that we are unfrozen run the send queue.
4425 xpt_run_devq(sim->devq);
4428 mtx_unlock(&devq->send_mtx);
4432 * XXX Appears to be unused.
4435 xpt_release_simq_timeout(void *arg)
4437 struct cam_sim *sim;
4439 sim = (struct cam_sim *)arg;
4440 xpt_release_simq(sim, /* run_queue */ TRUE);
4444 xpt_done(union ccb *done_ccb)
4446 struct cam_doneq *queue;
4449 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4450 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4453 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4454 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4455 queue = &cam_doneqs[hash];
4456 mtx_lock(&queue->cam_doneq_mtx);
4457 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4458 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4459 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4460 mtx_unlock(&queue->cam_doneq_mtx);
4462 wakeup(&queue->cam_doneq);
4466 xpt_done_direct(union ccb *done_ccb)
4469 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done_direct\n"));
4470 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4473 xpt_done_process(&done_ccb->ccb_h);
4481 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4486 xpt_alloc_ccb_nowait()
4490 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4495 xpt_free_ccb(union ccb *free_ccb)
4497 free(free_ccb, M_CAMCCB);
4502 /* Private XPT functions */
4505 * Get a CAM control block for the caller. Charge the structure to the device
4506 * referenced by the path. If we don't have sufficient resources to allocate
4507 * more ccbs, we return NULL.
4510 xpt_get_ccb_nowait(struct cam_periph *periph)
4514 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_NOWAIT);
4515 if (new_ccb == NULL)
4517 periph->periph_allocated++;
4518 cam_ccbq_take_opening(&periph->path->device->ccbq);
4523 xpt_get_ccb(struct cam_periph *periph)
4527 cam_periph_unlock(periph);
4528 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_WAITOK);
4529 cam_periph_lock(periph);
4530 periph->periph_allocated++;
4531 cam_ccbq_take_opening(&periph->path->device->ccbq);
4536 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4538 struct ccb_hdr *ccb_h;
4540 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4541 cam_periph_assert(periph, MA_OWNED);
4542 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4543 ccb_h->pinfo.priority != priority) {
4544 if (priority < periph->immediate_priority) {
4545 periph->immediate_priority = priority;
4546 xpt_run_allocq(periph, 0);
4548 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4551 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4552 return ((union ccb *)ccb_h);
4556 xpt_acquire_bus(struct cam_eb *bus)
4565 xpt_release_bus(struct cam_eb *bus)
4569 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4570 if (--bus->refcount > 0) {
4574 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4575 xsoftc.bus_generation++;
4577 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4578 ("destroying bus, but target list is not empty"));
4579 cam_sim_release(bus->sim);
4580 mtx_destroy(&bus->eb_mtx);
4581 free(bus, M_CAMXPT);
4584 static struct cam_et *
4585 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4587 struct cam_et *cur_target, *target;
4589 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4590 mtx_assert(&bus->eb_mtx, MA_OWNED);
4591 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4596 TAILQ_INIT(&target->ed_entries);
4598 target->target_id = target_id;
4599 target->refcount = 1;
4600 target->generation = 0;
4601 target->luns = NULL;
4602 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4603 timevalclear(&target->last_reset);
4605 * Hold a reference to our parent bus so it
4606 * will not go away before we do.
4610 /* Insertion sort into our bus's target list */
4611 cur_target = TAILQ_FIRST(&bus->et_entries);
4612 while (cur_target != NULL && cur_target->target_id < target_id)
4613 cur_target = TAILQ_NEXT(cur_target, links);
4614 if (cur_target != NULL) {
4615 TAILQ_INSERT_BEFORE(cur_target, target, links);
4617 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4624 xpt_acquire_target(struct cam_et *target)
4626 struct cam_eb *bus = target->bus;
4628 mtx_lock(&bus->eb_mtx);
4630 mtx_unlock(&bus->eb_mtx);
4634 xpt_release_target(struct cam_et *target)
4636 struct cam_eb *bus = target->bus;
4638 mtx_lock(&bus->eb_mtx);
4639 if (--target->refcount > 0) {
4640 mtx_unlock(&bus->eb_mtx);
4643 TAILQ_REMOVE(&bus->et_entries, target, links);
4645 mtx_unlock(&bus->eb_mtx);
4646 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4647 ("destroying target, but device list is not empty"));
4648 xpt_release_bus(bus);
4649 mtx_destroy(&target->luns_mtx);
4651 free(target->luns, M_CAMXPT);
4652 free(target, M_CAMXPT);
4655 static struct cam_ed *
4656 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4659 struct cam_ed *device;
4661 device = xpt_alloc_device(bus, target, lun_id);
4665 device->mintags = 1;
4666 device->maxtags = 1;
4671 xpt_destroy_device(void *context, int pending)
4673 struct cam_ed *device = context;
4675 mtx_lock(&device->device_mtx);
4676 mtx_destroy(&device->device_mtx);
4677 free(device, M_CAMDEV);
4681 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4683 struct cam_ed *cur_device, *device;
4684 struct cam_devq *devq;
4687 mtx_assert(&bus->eb_mtx, MA_OWNED);
4688 /* Make space for us in the device queue on our bus */
4689 devq = bus->sim->devq;
4690 mtx_lock(&devq->send_mtx);
4691 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4692 mtx_unlock(&devq->send_mtx);
4693 if (status != CAM_REQ_CMP)
4696 device = (struct cam_ed *)malloc(sizeof(*device),
4697 M_CAMDEV, M_NOWAIT|M_ZERO);
4701 cam_init_pinfo(&device->devq_entry);
4702 device->target = target;
4703 device->lun_id = lun_id;
4704 device->sim = bus->sim;
4705 if (cam_ccbq_init(&device->ccbq,
4706 bus->sim->max_dev_openings) != 0) {
4707 free(device, M_CAMDEV);
4710 SLIST_INIT(&device->asyncs);
4711 SLIST_INIT(&device->periphs);
4712 device->generation = 0;
4713 device->flags = CAM_DEV_UNCONFIGURED;
4714 device->tag_delay_count = 0;
4715 device->tag_saved_openings = 0;
4716 device->refcount = 1;
4717 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4718 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4719 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4721 * Hold a reference to our parent bus so it
4722 * will not go away before we do.
4726 cur_device = TAILQ_FIRST(&target->ed_entries);
4727 while (cur_device != NULL && cur_device->lun_id < lun_id)
4728 cur_device = TAILQ_NEXT(cur_device, links);
4729 if (cur_device != NULL)
4730 TAILQ_INSERT_BEFORE(cur_device, device, links);
4732 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4733 target->generation++;
4738 xpt_acquire_device(struct cam_ed *device)
4740 struct cam_eb *bus = device->target->bus;
4742 mtx_lock(&bus->eb_mtx);
4744 mtx_unlock(&bus->eb_mtx);
4748 xpt_release_device(struct cam_ed *device)
4750 struct cam_eb *bus = device->target->bus;
4751 struct cam_devq *devq;
4753 mtx_lock(&bus->eb_mtx);
4754 if (--device->refcount > 0) {
4755 mtx_unlock(&bus->eb_mtx);
4759 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4760 device->target->generation++;
4761 mtx_unlock(&bus->eb_mtx);
4763 /* Release our slot in the devq */
4764 devq = bus->sim->devq;
4765 mtx_lock(&devq->send_mtx);
4766 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4767 mtx_unlock(&devq->send_mtx);
4769 KASSERT(SLIST_EMPTY(&device->periphs),
4770 ("destroying device, but periphs list is not empty"));
4771 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4772 ("destroying device while still queued for ccbs"));
4774 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4775 callout_stop(&device->callout);
4777 xpt_release_target(device->target);
4779 cam_ccbq_fini(&device->ccbq);
4781 * Free allocated memory. free(9) does nothing if the
4782 * supplied pointer is NULL, so it is safe to call without
4785 free(device->supported_vpds, M_CAMXPT);
4786 free(device->device_id, M_CAMXPT);
4787 free(device->physpath, M_CAMXPT);
4788 free(device->rcap_buf, M_CAMXPT);
4789 free(device->serial_num, M_CAMXPT);
4790 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4794 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4800 mtx_lock(&dev->sim->devq->send_mtx);
4801 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4802 mtx_unlock(&dev->sim->devq->send_mtx);
4803 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4804 || (dev->inq_flags & SID_CmdQue) != 0)
4805 dev->tag_saved_openings = newopenings;
4809 static struct cam_eb *
4810 xpt_find_bus(path_id_t path_id)
4815 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4817 bus = TAILQ_NEXT(bus, links)) {
4818 if (bus->path_id == path_id) {
4827 static struct cam_et *
4828 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4830 struct cam_et *target;
4832 mtx_assert(&bus->eb_mtx, MA_OWNED);
4833 for (target = TAILQ_FIRST(&bus->et_entries);
4835 target = TAILQ_NEXT(target, links)) {
4836 if (target->target_id == target_id) {
4844 static struct cam_ed *
4845 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4847 struct cam_ed *device;
4849 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4850 for (device = TAILQ_FIRST(&target->ed_entries);
4852 device = TAILQ_NEXT(device, links)) {
4853 if (device->lun_id == lun_id) {
4862 xpt_start_tags(struct cam_path *path)
4864 struct ccb_relsim crs;
4865 struct cam_ed *device;
4866 struct cam_sim *sim;
4869 device = path->device;
4870 sim = path->bus->sim;
4871 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4872 xpt_freeze_devq(path, /*count*/1);
4873 device->inq_flags |= SID_CmdQue;
4874 if (device->tag_saved_openings != 0)
4875 newopenings = device->tag_saved_openings;
4877 newopenings = min(device->maxtags,
4878 sim->max_tagged_dev_openings);
4879 xpt_dev_ccbq_resize(path, newopenings);
4880 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4881 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4882 crs.ccb_h.func_code = XPT_REL_SIMQ;
4883 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4885 = crs.release_timeout
4888 xpt_action((union ccb *)&crs);
4892 xpt_stop_tags(struct cam_path *path)
4894 struct ccb_relsim crs;
4895 struct cam_ed *device;
4896 struct cam_sim *sim;
4898 device = path->device;
4899 sim = path->bus->sim;
4900 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4901 device->tag_delay_count = 0;
4902 xpt_freeze_devq(path, /*count*/1);
4903 device->inq_flags &= ~SID_CmdQue;
4904 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4905 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4906 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4907 crs.ccb_h.func_code = XPT_REL_SIMQ;
4908 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4910 = crs.release_timeout
4913 xpt_action((union ccb *)&crs);
4917 xpt_boot_delay(void *arg)
4924 xpt_config(void *arg)
4927 * Now that interrupts are enabled, go find our devices
4929 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4930 printf("xpt_config: failed to create taskqueue thread.\n");
4932 /* Setup debugging path */
4933 if (cam_dflags != CAM_DEBUG_NONE) {
4934 if (xpt_create_path(&cam_dpath, NULL,
4935 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4936 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4937 printf("xpt_config: xpt_create_path() failed for debug"
4938 " target %d:%d:%d, debugging disabled\n",
4939 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4940 cam_dflags = CAM_DEBUG_NONE;
4945 periphdriver_init(1);
4947 callout_init(&xsoftc.boot_callout, 1);
4948 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4949 xpt_boot_delay, NULL);
4950 /* Fire up rescan thread. */
4951 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4952 "cam", "scanner")) {
4953 printf("xpt_config: failed to create rescan thread.\n");
4961 xsoftc.buses_to_config++;
4966 xpt_release_boot(void)
4969 xsoftc.buses_to_config--;
4970 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4971 struct xpt_task *task;
4973 xsoftc.buses_config_done = 1;
4975 /* Call manually because we don't have any busses */
4976 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4978 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4979 taskqueue_enqueue(taskqueue_thread, &task->task);
4986 * If the given device only has one peripheral attached to it, and if that
4987 * peripheral is the passthrough driver, announce it. This insures that the
4988 * user sees some sort of announcement for every peripheral in their system.
4991 xptpassannouncefunc(struct cam_ed *device, void *arg)
4993 struct cam_periph *periph;
4996 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4997 periph = SLIST_NEXT(periph, periph_links), i++);
4999 periph = SLIST_FIRST(&device->periphs);
5001 && (strncmp(periph->periph_name, "pass", 4) == 0))
5002 xpt_announce_periph(periph, NULL);
5008 xpt_finishconfig_task(void *context, int pending)
5011 periphdriver_init(2);
5013 * Check for devices with no "standard" peripheral driver
5014 * attached. For any devices like that, announce the
5015 * passthrough driver so the user will see something.
5018 xpt_for_all_devices(xptpassannouncefunc, NULL);
5020 /* Release our hook so that the boot can continue. */
5021 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5022 free(xsoftc.xpt_config_hook, M_CAMXPT);
5023 xsoftc.xpt_config_hook = NULL;
5025 free(context, M_CAMXPT);
5029 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5030 struct cam_path *path)
5032 struct ccb_setasync csa;
5037 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5038 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5039 if (status != CAM_REQ_CMP)
5041 xpt_path_lock(path);
5045 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5046 csa.ccb_h.func_code = XPT_SASYNC_CB;
5047 csa.event_enable = event;
5048 csa.callback = cbfunc;
5049 csa.callback_arg = cbarg;
5050 xpt_action((union ccb *)&csa);
5051 status = csa.ccb_h.status;
5054 xpt_path_unlock(path);
5055 xpt_free_path(path);
5058 if ((status == CAM_REQ_CMP) &&
5059 (csa.event_enable & AC_FOUND_DEVICE)) {
5061 * Get this peripheral up to date with all
5062 * the currently existing devices.
5064 xpt_for_all_devices(xptsetasyncfunc, &csa);
5066 if ((status == CAM_REQ_CMP) &&
5067 (csa.event_enable & AC_PATH_REGISTERED)) {
5069 * Get this peripheral up to date with all
5070 * the currently existing busses.
5072 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5079 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5081 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5083 switch (work_ccb->ccb_h.func_code) {
5084 /* Common cases first */
5085 case XPT_PATH_INQ: /* Path routing inquiry */
5087 struct ccb_pathinq *cpi;
5089 cpi = &work_ccb->cpi;
5090 cpi->version_num = 1; /* XXX??? */
5091 cpi->hba_inquiry = 0;
5092 cpi->target_sprt = 0;
5094 cpi->hba_eng_cnt = 0;
5095 cpi->max_target = 0;
5097 cpi->initiator_id = 0;
5098 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5099 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5100 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5101 cpi->unit_number = sim->unit_number;
5102 cpi->bus_id = sim->bus_id;
5103 cpi->base_transfer_speed = 0;
5104 cpi->protocol = PROTO_UNSPECIFIED;
5105 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5106 cpi->transport = XPORT_UNSPECIFIED;
5107 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5108 cpi->ccb_h.status = CAM_REQ_CMP;
5113 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5120 * The xpt as a "controller" has no interrupt sources, so polling
5124 xptpoll(struct cam_sim *sim)
5129 xpt_lock_buses(void)
5131 mtx_lock(&xsoftc.xpt_topo_lock);
5135 xpt_unlock_buses(void)
5137 mtx_unlock(&xsoftc.xpt_topo_lock);
5141 xpt_path_mtx(struct cam_path *path)
5144 return (&path->device->device_mtx);
5148 xpt_done_process(struct ccb_hdr *ccb_h)
5150 struct cam_sim *sim;
5151 struct cam_devq *devq;
5152 struct mtx *mtx = NULL;
5154 if (ccb_h->flags & CAM_HIGH_POWER) {
5155 struct highpowerlist *hphead;
5156 struct cam_ed *device;
5158 mtx_lock(&xsoftc.xpt_highpower_lock);
5159 hphead = &xsoftc.highpowerq;
5161 device = STAILQ_FIRST(hphead);
5164 * Increment the count since this command is done.
5166 xsoftc.num_highpower++;
5169 * Any high powered commands queued up?
5171 if (device != NULL) {
5173 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5174 mtx_unlock(&xsoftc.xpt_highpower_lock);
5176 mtx_lock(&device->sim->devq->send_mtx);
5177 xpt_release_devq_device(device,
5178 /*count*/1, /*runqueue*/TRUE);
5179 mtx_unlock(&device->sim->devq->send_mtx);
5181 mtx_unlock(&xsoftc.xpt_highpower_lock);
5184 sim = ccb_h->path->bus->sim;
5186 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5187 xpt_release_simq(sim, /*run_queue*/FALSE);
5188 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5191 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5192 && (ccb_h->status & CAM_DEV_QFRZN)) {
5193 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5194 ccb_h->status &= ~CAM_DEV_QFRZN;
5198 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5199 struct cam_ed *dev = ccb_h->path->device;
5201 mtx_lock(&devq->send_mtx);
5202 devq->send_active--;
5203 devq->send_openings++;
5204 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5206 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5207 && (dev->ccbq.dev_active == 0))) {
5208 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5209 xpt_release_devq_device(dev, /*count*/1,
5210 /*run_queue*/FALSE);
5213 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5214 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5215 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5216 xpt_release_devq_device(dev, /*count*/1,
5217 /*run_queue*/FALSE);
5220 if (!device_is_queued(dev))
5221 (void)xpt_schedule_devq(devq, dev);
5223 mtx_unlock(&devq->send_mtx);
5225 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5226 mtx = xpt_path_mtx(ccb_h->path);
5229 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5230 && (--dev->tag_delay_count == 0))
5231 xpt_start_tags(ccb_h->path);
5235 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5237 mtx = xpt_path_mtx(ccb_h->path);
5247 /* Call the peripheral driver's callback */
5248 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5249 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5255 xpt_done_td(void *arg)
5257 struct cam_doneq *queue = arg;
5258 struct ccb_hdr *ccb_h;
5259 STAILQ_HEAD(, ccb_hdr) doneq;
5261 STAILQ_INIT(&doneq);
5262 mtx_lock(&queue->cam_doneq_mtx);
5264 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5265 queue->cam_doneq_sleep = 1;
5266 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5268 queue->cam_doneq_sleep = 0;
5270 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5271 mtx_unlock(&queue->cam_doneq_mtx);
5273 THREAD_NO_SLEEPING();
5274 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5275 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5276 xpt_done_process(ccb_h);
5278 THREAD_SLEEPING_OK();
5280 mtx_lock(&queue->cam_doneq_mtx);
5285 camisr_runqueue(void)
5287 struct ccb_hdr *ccb_h;
5288 struct cam_doneq *queue;
5291 /* Process global queues. */
5292 for (i = 0; i < cam_num_doneqs; i++) {
5293 queue = &cam_doneqs[i];
5294 mtx_lock(&queue->cam_doneq_mtx);
5295 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5296 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5297 mtx_unlock(&queue->cam_doneq_mtx);
5298 xpt_done_process(ccb_h);
5299 mtx_lock(&queue->cam_doneq_mtx);
5301 mtx_unlock(&queue->cam_doneq_mtx);