2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_compat.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
69 #include <machine/md_var.h> /* geometry translation */
70 #include <machine/stdarg.h> /* for xpt_print below */
75 * This is the maximum number of high powered commands (e.g. start unit)
76 * that can be outstanding at a particular time.
78 #ifndef CAM_MAX_HIGHPOWER
79 #define CAM_MAX_HIGHPOWER 4
82 /* Datastructures internal to the xpt layer */
83 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
88 /* Object for defering XPT actions to a taskqueue */
96 uint32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 struct mtx xpt_highpower_lock;
100 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 int buses_config_done;
108 /* Registered busses */
109 TAILQ_HEAD(,cam_eb) xpt_busses;
110 u_int bus_generation;
112 struct intr_config_hook *xpt_config_hook;
115 struct callout boot_callout;
117 struct mtx xpt_topo_lock;
119 struct taskqueue *xpt_taskq;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
156 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
157 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
158 &xsoftc.boot_delay, 0, "Bus registration wait time");
159 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
160 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
163 struct mtx_padalign cam_doneq_mtx;
164 STAILQ_HEAD(, ccb_hdr) cam_doneq;
168 static struct cam_doneq cam_doneqs[MAXCPU];
169 static int cam_num_doneqs;
170 static struct proc *cam_proc;
172 TUNABLE_INT("kern.cam.num_doneqs", &cam_num_doneqs);
173 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
174 &cam_num_doneqs, 0, "Number of completion queues/threads");
176 struct cam_periph *xpt_periph;
178 static periph_init_t xpt_periph_init;
180 static struct periph_driver xpt_driver =
182 xpt_periph_init, "xpt",
183 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
187 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
189 static d_open_t xptopen;
190 static d_close_t xptclose;
191 static d_ioctl_t xptioctl;
192 static d_ioctl_t xptdoioctl;
194 static struct cdevsw xpt_cdevsw = {
195 .d_version = D_VERSION,
203 /* Storage for debugging datastructures */
204 struct cam_path *cam_dpath;
205 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
206 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
207 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
208 &cam_dflags, 0, "Enabled debug flags");
209 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
210 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
211 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
212 &cam_debug_delay, 0, "Delay in us after each debug message");
214 /* Our boot-time initialization hook */
215 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
217 static moduledata_t cam_moduledata = {
219 cam_module_event_handler,
223 static int xpt_init(void *);
225 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
226 MODULE_VERSION(cam, 1);
229 static void xpt_async_bcast(struct async_list *async_head,
230 u_int32_t async_code,
231 struct cam_path *path,
233 static path_id_t xptnextfreepathid(void);
234 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
235 static union ccb *xpt_get_ccb(struct cam_periph *periph);
236 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
237 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
238 static void xpt_run_allocq_task(void *context, int pending);
239 static void xpt_run_devq(struct cam_devq *devq);
240 static timeout_t xpt_release_devq_timeout;
241 static void xpt_release_simq_timeout(void *arg) __unused;
242 static void xpt_acquire_bus(struct cam_eb *bus);
243 static void xpt_release_bus(struct cam_eb *bus);
244 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
245 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
247 static struct cam_et*
248 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
249 static void xpt_acquire_target(struct cam_et *target);
250 static void xpt_release_target(struct cam_et *target);
251 static struct cam_eb*
252 xpt_find_bus(path_id_t path_id);
253 static struct cam_et*
254 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
255 static struct cam_ed*
256 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
257 static void xpt_config(void *arg);
258 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
259 u_int32_t new_priority);
260 static xpt_devicefunc_t xptpassannouncefunc;
261 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
262 static void xptpoll(struct cam_sim *sim);
263 static void camisr_runqueue(void);
264 static void xpt_done_process(struct ccb_hdr *ccb_h);
265 static void xpt_done_td(void *);
266 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
267 u_int num_patterns, struct cam_eb *bus);
268 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
270 struct cam_ed *device);
271 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
273 struct cam_periph *periph);
274 static xpt_busfunc_t xptedtbusfunc;
275 static xpt_targetfunc_t xptedttargetfunc;
276 static xpt_devicefunc_t xptedtdevicefunc;
277 static xpt_periphfunc_t xptedtperiphfunc;
278 static xpt_pdrvfunc_t xptplistpdrvfunc;
279 static xpt_periphfunc_t xptplistperiphfunc;
280 static int xptedtmatch(struct ccb_dev_match *cdm);
281 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
282 static int xptbustraverse(struct cam_eb *start_bus,
283 xpt_busfunc_t *tr_func, void *arg);
284 static int xpttargettraverse(struct cam_eb *bus,
285 struct cam_et *start_target,
286 xpt_targetfunc_t *tr_func, void *arg);
287 static int xptdevicetraverse(struct cam_et *target,
288 struct cam_ed *start_device,
289 xpt_devicefunc_t *tr_func, void *arg);
290 static int xptperiphtraverse(struct cam_ed *device,
291 struct cam_periph *start_periph,
292 xpt_periphfunc_t *tr_func, void *arg);
293 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
294 xpt_pdrvfunc_t *tr_func, void *arg);
295 static int xptpdperiphtraverse(struct periph_driver **pdrv,
296 struct cam_periph *start_periph,
297 xpt_periphfunc_t *tr_func,
299 static xpt_busfunc_t xptdefbusfunc;
300 static xpt_targetfunc_t xptdeftargetfunc;
301 static xpt_devicefunc_t xptdefdevicefunc;
302 static xpt_periphfunc_t xptdefperiphfunc;
303 static void xpt_finishconfig_task(void *context, int pending);
304 static void xpt_dev_async_default(u_int32_t async_code,
306 struct cam_et *target,
307 struct cam_ed *device,
309 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
310 struct cam_et *target,
312 static xpt_devicefunc_t xptsetasyncfunc;
313 static xpt_busfunc_t xptsetasyncbusfunc;
314 static cam_status xptregister(struct cam_periph *periph,
316 static __inline int device_is_queued(struct cam_ed *device);
319 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
323 mtx_assert(&devq->send_mtx, MA_OWNED);
324 if ((dev->ccbq.queue.entries > 0) &&
325 (dev->ccbq.dev_openings > 0) &&
326 (dev->ccbq.queue.qfrozen_cnt == 0)) {
328 * The priority of a device waiting for controller
329 * resources is that of the highest priority CCB
333 xpt_schedule_dev(&devq->send_queue,
335 CAMQ_GET_PRIO(&dev->ccbq.queue));
343 device_is_queued(struct cam_ed *device)
345 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
351 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
355 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
359 * Only allow read-write access.
361 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
365 * We don't allow nonblocking access.
367 if ((flags & O_NONBLOCK) != 0) {
368 printf("%s: can't do nonblocking access\n", devtoname(dev));
376 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
383 * Don't automatically grab the xpt softc lock here even though this is going
384 * through the xpt device. The xpt device is really just a back door for
385 * accessing other devices and SIMs, so the right thing to do is to grab
386 * the appropriate SIM lock once the bus/SIM is located.
389 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
393 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
394 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
400 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
408 * For the transport layer CAMIOCOMMAND ioctl, we really only want
409 * to accept CCB types that don't quite make sense to send through a
410 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
418 inccb = (union ccb *)addr;
420 bus = xpt_find_bus(inccb->ccb_h.path_id);
424 switch (inccb->ccb_h.func_code) {
427 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
428 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
429 xpt_release_bus(bus);
434 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
435 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
436 xpt_release_bus(bus);
444 switch(inccb->ccb_h.func_code) {
452 ccb = xpt_alloc_ccb();
455 * Create a path using the bus, target, and lun the
458 if (xpt_create_path(&ccb->ccb_h.path, NULL,
459 inccb->ccb_h.path_id,
460 inccb->ccb_h.target_id,
461 inccb->ccb_h.target_lun) !=
467 /* Ensure all of our fields are correct */
468 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
469 inccb->ccb_h.pinfo.priority);
470 xpt_merge_ccb(ccb, inccb);
471 xpt_path_lock(ccb->ccb_h.path);
472 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
473 xpt_path_unlock(ccb->ccb_h.path);
474 bcopy(ccb, inccb, sizeof(union ccb));
475 xpt_free_path(ccb->ccb_h.path);
483 * This is an immediate CCB, so it's okay to
484 * allocate it on the stack.
488 * Create a path using the bus, target, and lun the
491 if (xpt_create_path(&ccb.ccb_h.path, NULL,
492 inccb->ccb_h.path_id,
493 inccb->ccb_h.target_id,
494 inccb->ccb_h.target_lun) !=
499 /* Ensure all of our fields are correct */
500 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
501 inccb->ccb_h.pinfo.priority);
502 xpt_merge_ccb(&ccb, inccb);
504 bcopy(&ccb, inccb, sizeof(union ccb));
505 xpt_free_path(ccb.ccb_h.path);
509 case XPT_DEV_MATCH: {
510 struct cam_periph_map_info mapinfo;
511 struct cam_path *old_path;
514 * We can't deal with physical addresses for this
515 * type of transaction.
517 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
524 * Save this in case the caller had it set to
525 * something in particular.
527 old_path = inccb->ccb_h.path;
530 * We really don't need a path for the matching
531 * code. The path is needed because of the
532 * debugging statements in xpt_action(). They
533 * assume that the CCB has a valid path.
535 inccb->ccb_h.path = xpt_periph->path;
537 bzero(&mapinfo, sizeof(mapinfo));
540 * Map the pattern and match buffers into kernel
541 * virtual address space.
543 error = cam_periph_mapmem(inccb, &mapinfo);
546 inccb->ccb_h.path = old_path;
551 * This is an immediate CCB, we can send it on directly.
556 * Map the buffers back into user space.
558 cam_periph_unmapmem(inccb, &mapinfo);
560 inccb->ccb_h.path = old_path;
569 xpt_release_bus(bus);
573 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
574 * with the periphal driver name and unit name filled in. The other
575 * fields don't really matter as input. The passthrough driver name
576 * ("pass"), and unit number are passed back in the ccb. The current
577 * device generation number, and the index into the device peripheral
578 * driver list, and the status are also passed back. Note that
579 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
580 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
581 * (or rather should be) impossible for the device peripheral driver
582 * list to change since we look at the whole thing in one pass, and
583 * we do it with lock protection.
586 case CAMGETPASSTHRU: {
588 struct cam_periph *periph;
589 struct periph_driver **p_drv;
592 int base_periph_found;
594 ccb = (union ccb *)addr;
595 unit = ccb->cgdl.unit_number;
596 name = ccb->cgdl.periph_name;
597 base_periph_found = 0;
600 * Sanity check -- make sure we don't get a null peripheral
603 if (*ccb->cgdl.periph_name == '\0') {
608 /* Keep the list from changing while we traverse it */
611 /* first find our driver in the list of drivers */
612 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
613 if (strcmp((*p_drv)->driver_name, name) == 0)
616 if (*p_drv == NULL) {
618 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
619 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
620 *ccb->cgdl.periph_name = '\0';
621 ccb->cgdl.unit_number = 0;
627 * Run through every peripheral instance of this driver
628 * and check to see whether it matches the unit passed
629 * in by the user. If it does, get out of the loops and
630 * find the passthrough driver associated with that
633 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
634 periph = TAILQ_NEXT(periph, unit_links)) {
636 if (periph->unit_number == unit)
640 * If we found the peripheral driver that the user passed
641 * in, go through all of the peripheral drivers for that
642 * particular device and look for a passthrough driver.
644 if (periph != NULL) {
645 struct cam_ed *device;
648 base_periph_found = 1;
649 device = periph->path->device;
650 for (i = 0, periph = SLIST_FIRST(&device->periphs);
652 periph = SLIST_NEXT(periph, periph_links), i++) {
654 * Check to see whether we have a
655 * passthrough device or not.
657 if (strcmp(periph->periph_name, "pass") == 0) {
659 * Fill in the getdevlist fields.
661 strcpy(ccb->cgdl.periph_name,
662 periph->periph_name);
663 ccb->cgdl.unit_number =
665 if (SLIST_NEXT(periph, periph_links))
667 CAM_GDEVLIST_MORE_DEVS;
670 CAM_GDEVLIST_LAST_DEVICE;
671 ccb->cgdl.generation =
675 * Fill in some CCB header fields
676 * that the user may want.
679 periph->path->bus->path_id;
680 ccb->ccb_h.target_id =
681 periph->path->target->target_id;
682 ccb->ccb_h.target_lun =
683 periph->path->device->lun_id;
684 ccb->ccb_h.status = CAM_REQ_CMP;
691 * If the periph is null here, one of two things has
692 * happened. The first possibility is that we couldn't
693 * find the unit number of the particular peripheral driver
694 * that the user is asking about. e.g. the user asks for
695 * the passthrough driver for "da11". We find the list of
696 * "da" peripherals all right, but there is no unit 11.
697 * The other possibility is that we went through the list
698 * of peripheral drivers attached to the device structure,
699 * but didn't find one with the name "pass". Either way,
700 * we return ENOENT, since we couldn't find something.
702 if (periph == NULL) {
703 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
704 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
705 *ccb->cgdl.periph_name = '\0';
706 ccb->cgdl.unit_number = 0;
709 * It is unfortunate that this is even necessary,
710 * but there are many, many clueless users out there.
711 * If this is true, the user is looking for the
712 * passthrough driver, but doesn't have one in his
715 if (base_periph_found == 1) {
716 printf("xptioctl: pass driver is not in the "
718 printf("xptioctl: put \"device pass\" in "
719 "your kernel config file\n");
734 cam_module_event_handler(module_t mod, int what, void *arg)
740 if ((error = xpt_init(NULL)) != 0)
753 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
756 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
757 xpt_free_path(done_ccb->ccb_h.path);
758 xpt_free_ccb(done_ccb);
760 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
761 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
766 /* thread to handle bus rescans */
768 xpt_scanner_thread(void *dummy)
771 struct cam_path path;
775 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
776 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
778 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
779 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
783 * Since lock can be dropped inside and path freed
784 * by completion callback even before return here,
785 * take our own path copy for reference.
787 xpt_copy_path(&path, ccb->ccb_h.path);
788 xpt_path_lock(&path);
790 xpt_path_unlock(&path);
791 xpt_release_path(&path);
799 xpt_rescan(union ccb *ccb)
803 /* Prepare request */
804 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_BUS;
807 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
808 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
809 ccb->ccb_h.func_code = XPT_SCAN_TGT;
810 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
811 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
812 ccb->ccb_h.func_code = XPT_SCAN_LUN;
814 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
815 xpt_free_path(ccb->ccb_h.path);
819 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
820 ccb->ccb_h.cbfcnp = xpt_rescan_done;
821 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
822 /* Don't make duplicate entries for the same paths. */
824 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
825 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
826 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
827 wakeup(&xsoftc.ccb_scanq);
829 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
830 xpt_free_path(ccb->ccb_h.path);
836 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
837 xsoftc.buses_to_config++;
838 wakeup(&xsoftc.ccb_scanq);
842 /* Functions accessed by the peripheral drivers */
844 xpt_init(void *dummy)
846 struct cam_sim *xpt_sim;
847 struct cam_path *path;
848 struct cam_devq *devq;
852 TAILQ_INIT(&xsoftc.xpt_busses);
853 TAILQ_INIT(&xsoftc.ccb_scanq);
854 STAILQ_INIT(&xsoftc.highpowerq);
855 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
857 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
858 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
859 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
860 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
862 #ifdef CAM_BOOT_DELAY
864 * Override this value at compile time to assist our users
865 * who don't use loader to boot a kernel.
867 xsoftc.boot_delay = CAM_BOOT_DELAY;
870 * The xpt layer is, itself, the equivelent of a SIM.
871 * Allow 16 ccbs in the ccb pool for it. This should
872 * give decent parallelism when we probe busses and
873 * perform other XPT functions.
875 devq = cam_simq_alloc(16);
876 xpt_sim = cam_sim_alloc(xptaction,
881 /*mtx*/&xsoftc.xpt_lock,
882 /*max_dev_transactions*/0,
883 /*max_tagged_dev_transactions*/0,
888 mtx_lock(&xsoftc.xpt_lock);
889 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
890 mtx_unlock(&xsoftc.xpt_lock);
891 printf("xpt_init: xpt_bus_register failed with status %#x,"
892 " failing attach\n", status);
895 mtx_unlock(&xsoftc.xpt_lock);
898 * Looking at the XPT from the SIM layer, the XPT is
899 * the equivelent of a peripheral driver. Allocate
900 * a peripheral driver entry for us.
902 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
904 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
905 printf("xpt_init: xpt_create_path failed with status %#x,"
906 " failing attach\n", status);
910 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
911 path, NULL, 0, xpt_sim);
912 xpt_path_unlock(path);
915 if (cam_num_doneqs < 1)
916 cam_num_doneqs = 1 + mp_ncpus / 6;
917 else if (cam_num_doneqs > MAXCPU)
918 cam_num_doneqs = MAXCPU;
919 for (i = 0; i < cam_num_doneqs; i++) {
920 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
922 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
923 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
924 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
930 if (cam_num_doneqs < 1) {
931 printf("xpt_init: Cannot init completion queues "
932 "- failing attach\n");
936 * Register a callback for when interrupts are enabled.
938 xsoftc.xpt_config_hook =
939 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
940 M_CAMXPT, M_NOWAIT | M_ZERO);
941 if (xsoftc.xpt_config_hook == NULL) {
942 printf("xpt_init: Cannot malloc config hook "
943 "- failing attach\n");
946 xsoftc.xpt_config_hook->ich_func = xpt_config;
947 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
948 free (xsoftc.xpt_config_hook, M_CAMXPT);
949 printf("xpt_init: config_intrhook_establish failed "
950 "- failing attach\n");
957 xptregister(struct cam_periph *periph, void *arg)
959 struct cam_sim *xpt_sim;
961 if (periph == NULL) {
962 printf("xptregister: periph was NULL!!\n");
963 return(CAM_REQ_CMP_ERR);
966 xpt_sim = (struct cam_sim *)arg;
967 xpt_sim->softc = periph;
969 periph->softc = NULL;
975 xpt_add_periph(struct cam_periph *periph)
977 struct cam_ed *device;
980 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
981 device = periph->path->device;
982 status = CAM_REQ_CMP;
983 if (device != NULL) {
984 mtx_lock(&device->target->bus->eb_mtx);
985 device->generation++;
986 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
987 mtx_unlock(&device->target->bus->eb_mtx);
988 atomic_add_32(&xsoftc.xpt_generation, 1);
995 xpt_remove_periph(struct cam_periph *periph)
997 struct cam_ed *device;
999 device = periph->path->device;
1000 if (device != NULL) {
1001 mtx_lock(&device->target->bus->eb_mtx);
1002 device->generation++;
1003 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1004 mtx_unlock(&device->target->bus->eb_mtx);
1005 atomic_add_32(&xsoftc.xpt_generation, 1);
1011 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1013 struct cam_path *path = periph->path;
1015 cam_periph_assert(periph, MA_OWNED);
1016 periph->flags |= CAM_PERIPH_ANNOUNCED;
1018 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1019 periph->periph_name, periph->unit_number,
1020 path->bus->sim->sim_name,
1021 path->bus->sim->unit_number,
1022 path->bus->sim->bus_id,
1024 path->target->target_id,
1025 (uintmax_t)path->device->lun_id);
1026 printf("%s%d: ", periph->periph_name, periph->unit_number);
1027 if (path->device->protocol == PROTO_SCSI)
1028 scsi_print_inquiry(&path->device->inq_data);
1029 else if (path->device->protocol == PROTO_ATA ||
1030 path->device->protocol == PROTO_SATAPM)
1031 ata_print_ident(&path->device->ident_data);
1032 else if (path->device->protocol == PROTO_SEMB)
1034 (struct sep_identify_data *)&path->device->ident_data);
1036 printf("Unknown protocol device\n");
1037 if (path->device->serial_num_len > 0) {
1038 /* Don't wrap the screen - print only the first 60 chars */
1039 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1040 periph->unit_number, path->device->serial_num);
1042 /* Announce transport details. */
1043 (*(path->bus->xport->announce))(periph);
1044 /* Announce command queueing. */
1045 if (path->device->inq_flags & SID_CmdQue
1046 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1047 printf("%s%d: Command Queueing enabled\n",
1048 periph->periph_name, periph->unit_number);
1050 /* Announce caller's details if they've passed in. */
1051 if (announce_string != NULL)
1052 printf("%s%d: %s\n", periph->periph_name,
1053 periph->unit_number, announce_string);
1057 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1060 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1061 periph->unit_number, quirks, bit_string);
1066 xpt_denounce_periph(struct cam_periph *periph)
1068 struct cam_path *path = periph->path;
1070 cam_periph_assert(periph, MA_OWNED);
1071 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1072 periph->periph_name, periph->unit_number,
1073 path->bus->sim->sim_name,
1074 path->bus->sim->unit_number,
1075 path->bus->sim->bus_id,
1077 path->target->target_id,
1078 (uintmax_t)path->device->lun_id);
1079 printf("%s%d: ", periph->periph_name, periph->unit_number);
1080 if (path->device->protocol == PROTO_SCSI)
1081 scsi_print_inquiry_short(&path->device->inq_data);
1082 else if (path->device->protocol == PROTO_ATA ||
1083 path->device->protocol == PROTO_SATAPM)
1084 ata_print_ident_short(&path->device->ident_data);
1085 else if (path->device->protocol == PROTO_SEMB)
1086 semb_print_ident_short(
1087 (struct sep_identify_data *)&path->device->ident_data);
1089 printf("Unknown protocol device");
1090 if (path->device->serial_num_len > 0)
1091 printf(" s/n %.60s", path->device->serial_num);
1092 printf(" detached\n");
1097 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1100 struct ccb_dev_advinfo cdai;
1101 struct scsi_vpd_id_descriptor *idd;
1103 xpt_path_assert(path, MA_OWNED);
1105 memset(&cdai, 0, sizeof(cdai));
1106 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1107 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1110 if (!strcmp(attr, "GEOM::ident"))
1111 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1112 else if (!strcmp(attr, "GEOM::physpath"))
1113 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1114 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1115 strcmp(attr, "GEOM::lunname") == 0) {
1116 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1117 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1121 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1122 if (cdai.buf == NULL) {
1126 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1127 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1128 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1129 if (cdai.provsiz == 0)
1131 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1132 if (strcmp(attr, "GEOM::lunid") == 0) {
1133 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1134 cdai.provsiz, scsi_devid_is_lun_naa);
1136 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1137 cdai.provsiz, scsi_devid_is_lun_eui64);
1141 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1142 cdai.provsiz, scsi_devid_is_lun_t10);
1144 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1145 cdai.provsiz, scsi_devid_is_lun_name);
1149 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1150 if (idd->length < len) {
1151 for (l = 0; l < idd->length; l++)
1152 buf[l] = idd->identifier[l] ?
1153 idd->identifier[l] : ' ';
1157 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1158 l = strnlen(idd->identifier, idd->length);
1160 bcopy(idd->identifier, buf, l);
1165 if (idd->length * 2 < len) {
1166 for (l = 0; l < idd->length; l++)
1167 sprintf(buf + l * 2, "%02x",
1168 idd->identifier[l]);
1174 if (strlcpy(buf, cdai.buf, len) >= len)
1179 if (cdai.buf != NULL)
1180 free(cdai.buf, M_CAMXPT);
1184 static dev_match_ret
1185 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1188 dev_match_ret retval;
1191 retval = DM_RET_NONE;
1194 * If we aren't given something to match against, that's an error.
1197 return(DM_RET_ERROR);
1200 * If there are no match entries, then this bus matches no
1203 if ((patterns == NULL) || (num_patterns == 0))
1204 return(DM_RET_DESCEND | DM_RET_COPY);
1206 for (i = 0; i < num_patterns; i++) {
1207 struct bus_match_pattern *cur_pattern;
1210 * If the pattern in question isn't for a bus node, we
1211 * aren't interested. However, we do indicate to the
1212 * calling routine that we should continue descending the
1213 * tree, since the user wants to match against lower-level
1216 if (patterns[i].type != DEV_MATCH_BUS) {
1217 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1218 retval |= DM_RET_DESCEND;
1222 cur_pattern = &patterns[i].pattern.bus_pattern;
1225 * If they want to match any bus node, we give them any
1228 if (cur_pattern->flags == BUS_MATCH_ANY) {
1229 /* set the copy flag */
1230 retval |= DM_RET_COPY;
1233 * If we've already decided on an action, go ahead
1236 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1241 * Not sure why someone would do this...
1243 if (cur_pattern->flags == BUS_MATCH_NONE)
1246 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1247 && (cur_pattern->path_id != bus->path_id))
1250 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1251 && (cur_pattern->bus_id != bus->sim->bus_id))
1254 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1255 && (cur_pattern->unit_number != bus->sim->unit_number))
1258 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1259 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1264 * If we get to this point, the user definitely wants
1265 * information on this bus. So tell the caller to copy the
1268 retval |= DM_RET_COPY;
1271 * If the return action has been set to descend, then we
1272 * know that we've already seen a non-bus matching
1273 * expression, therefore we need to further descend the tree.
1274 * This won't change by continuing around the loop, so we
1275 * go ahead and return. If we haven't seen a non-bus
1276 * matching expression, we keep going around the loop until
1277 * we exhaust the matching expressions. We'll set the stop
1278 * flag once we fall out of the loop.
1280 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1285 * If the return action hasn't been set to descend yet, that means
1286 * we haven't seen anything other than bus matching patterns. So
1287 * tell the caller to stop descending the tree -- the user doesn't
1288 * want to match against lower level tree elements.
1290 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1291 retval |= DM_RET_STOP;
1296 static dev_match_ret
1297 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1298 struct cam_ed *device)
1300 dev_match_ret retval;
1303 retval = DM_RET_NONE;
1306 * If we aren't given something to match against, that's an error.
1309 return(DM_RET_ERROR);
1312 * If there are no match entries, then this device matches no
1315 if ((patterns == NULL) || (num_patterns == 0))
1316 return(DM_RET_DESCEND | DM_RET_COPY);
1318 for (i = 0; i < num_patterns; i++) {
1319 struct device_match_pattern *cur_pattern;
1320 struct scsi_vpd_device_id *device_id_page;
1323 * If the pattern in question isn't for a device node, we
1324 * aren't interested.
1326 if (patterns[i].type != DEV_MATCH_DEVICE) {
1327 if ((patterns[i].type == DEV_MATCH_PERIPH)
1328 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1329 retval |= DM_RET_DESCEND;
1333 cur_pattern = &patterns[i].pattern.device_pattern;
1335 /* Error out if mutually exclusive options are specified. */
1336 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1337 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1338 return(DM_RET_ERROR);
1341 * If they want to match any device node, we give them any
1344 if (cur_pattern->flags == DEV_MATCH_ANY)
1348 * Not sure why someone would do this...
1350 if (cur_pattern->flags == DEV_MATCH_NONE)
1353 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1354 && (cur_pattern->path_id != device->target->bus->path_id))
1357 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1358 && (cur_pattern->target_id != device->target->target_id))
1361 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1362 && (cur_pattern->target_lun != device->lun_id))
1365 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1366 && (cam_quirkmatch((caddr_t)&device->inq_data,
1367 (caddr_t)&cur_pattern->data.inq_pat,
1368 1, sizeof(cur_pattern->data.inq_pat),
1369 scsi_static_inquiry_match) == NULL))
1372 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1373 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1374 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1375 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1376 device->device_id_len
1377 - SVPD_DEVICE_ID_HDR_LEN,
1378 cur_pattern->data.devid_pat.id,
1379 cur_pattern->data.devid_pat.id_len) != 0))
1384 * If we get to this point, the user definitely wants
1385 * information on this device. So tell the caller to copy
1388 retval |= DM_RET_COPY;
1391 * If the return action has been set to descend, then we
1392 * know that we've already seen a peripheral matching
1393 * expression, therefore we need to further descend the tree.
1394 * This won't change by continuing around the loop, so we
1395 * go ahead and return. If we haven't seen a peripheral
1396 * matching expression, we keep going around the loop until
1397 * we exhaust the matching expressions. We'll set the stop
1398 * flag once we fall out of the loop.
1400 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1405 * If the return action hasn't been set to descend yet, that means
1406 * we haven't seen any peripheral matching patterns. So tell the
1407 * caller to stop descending the tree -- the user doesn't want to
1408 * match against lower level tree elements.
1410 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1411 retval |= DM_RET_STOP;
1417 * Match a single peripheral against any number of match patterns.
1419 static dev_match_ret
1420 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1421 struct cam_periph *periph)
1423 dev_match_ret retval;
1427 * If we aren't given something to match against, that's an error.
1430 return(DM_RET_ERROR);
1433 * If there are no match entries, then this peripheral matches no
1436 if ((patterns == NULL) || (num_patterns == 0))
1437 return(DM_RET_STOP | DM_RET_COPY);
1440 * There aren't any nodes below a peripheral node, so there's no
1441 * reason to descend the tree any further.
1443 retval = DM_RET_STOP;
1445 for (i = 0; i < num_patterns; i++) {
1446 struct periph_match_pattern *cur_pattern;
1449 * If the pattern in question isn't for a peripheral, we
1450 * aren't interested.
1452 if (patterns[i].type != DEV_MATCH_PERIPH)
1455 cur_pattern = &patterns[i].pattern.periph_pattern;
1458 * If they want to match on anything, then we will do so.
1460 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1461 /* set the copy flag */
1462 retval |= DM_RET_COPY;
1465 * We've already set the return action to stop,
1466 * since there are no nodes below peripherals in
1473 * Not sure why someone would do this...
1475 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1478 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1479 && (cur_pattern->path_id != periph->path->bus->path_id))
1483 * For the target and lun id's, we have to make sure the
1484 * target and lun pointers aren't NULL. The xpt peripheral
1485 * has a wildcard target and device.
1487 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1488 && ((periph->path->target == NULL)
1489 ||(cur_pattern->target_id != periph->path->target->target_id)))
1492 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1493 && ((periph->path->device == NULL)
1494 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1497 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1498 && (cur_pattern->unit_number != periph->unit_number))
1501 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1502 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1507 * If we get to this point, the user definitely wants
1508 * information on this peripheral. So tell the caller to
1509 * copy the data out.
1511 retval |= DM_RET_COPY;
1514 * The return action has already been set to stop, since
1515 * peripherals don't have any nodes below them in the EDT.
1521 * If we get to this point, the peripheral that was passed in
1522 * doesn't match any of the patterns.
1528 xptedtbusfunc(struct cam_eb *bus, void *arg)
1530 struct ccb_dev_match *cdm;
1531 struct cam_et *target;
1532 dev_match_ret retval;
1534 cdm = (struct ccb_dev_match *)arg;
1537 * If our position is for something deeper in the tree, that means
1538 * that we've already seen this node. So, we keep going down.
1540 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1541 && (cdm->pos.cookie.bus == bus)
1542 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1543 && (cdm->pos.cookie.target != NULL))
1544 retval = DM_RET_DESCEND;
1546 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1549 * If we got an error, bail out of the search.
1551 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1552 cdm->status = CAM_DEV_MATCH_ERROR;
1557 * If the copy flag is set, copy this bus out.
1559 if (retval & DM_RET_COPY) {
1562 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1563 sizeof(struct dev_match_result));
1566 * If we don't have enough space to put in another
1567 * match result, save our position and tell the
1568 * user there are more devices to check.
1570 if (spaceleft < sizeof(struct dev_match_result)) {
1571 bzero(&cdm->pos, sizeof(cdm->pos));
1572 cdm->pos.position_type =
1573 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1575 cdm->pos.cookie.bus = bus;
1576 cdm->pos.generations[CAM_BUS_GENERATION]=
1577 xsoftc.bus_generation;
1578 cdm->status = CAM_DEV_MATCH_MORE;
1581 j = cdm->num_matches;
1583 cdm->matches[j].type = DEV_MATCH_BUS;
1584 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1585 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1586 cdm->matches[j].result.bus_result.unit_number =
1587 bus->sim->unit_number;
1588 strncpy(cdm->matches[j].result.bus_result.dev_name,
1589 bus->sim->sim_name, DEV_IDLEN);
1593 * If the user is only interested in busses, there's no
1594 * reason to descend to the next level in the tree.
1596 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1600 * If there is a target generation recorded, check it to
1601 * make sure the target list hasn't changed.
1603 mtx_lock(&bus->eb_mtx);
1604 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1605 && (cdm->pos.cookie.bus == bus)
1606 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1607 && (cdm->pos.cookie.target != NULL)) {
1608 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1610 mtx_unlock(&bus->eb_mtx);
1611 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1614 target = (struct cam_et *)cdm->pos.cookie.target;
1618 mtx_unlock(&bus->eb_mtx);
1620 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1624 xptedttargetfunc(struct cam_et *target, void *arg)
1626 struct ccb_dev_match *cdm;
1628 struct cam_ed *device;
1630 cdm = (struct ccb_dev_match *)arg;
1634 * If there is a device list generation recorded, check it to
1635 * make sure the device list hasn't changed.
1637 mtx_lock(&bus->eb_mtx);
1638 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1639 && (cdm->pos.cookie.bus == bus)
1640 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1641 && (cdm->pos.cookie.target == target)
1642 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1643 && (cdm->pos.cookie.device != NULL)) {
1644 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1645 target->generation) {
1646 mtx_unlock(&bus->eb_mtx);
1647 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1650 device = (struct cam_ed *)cdm->pos.cookie.device;
1654 mtx_unlock(&bus->eb_mtx);
1656 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1660 xptedtdevicefunc(struct cam_ed *device, void *arg)
1663 struct cam_periph *periph;
1664 struct ccb_dev_match *cdm;
1665 dev_match_ret retval;
1667 cdm = (struct ccb_dev_match *)arg;
1668 bus = device->target->bus;
1671 * If our position is for something deeper in the tree, that means
1672 * that we've already seen this node. So, we keep going down.
1674 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1675 && (cdm->pos.cookie.device == device)
1676 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1677 && (cdm->pos.cookie.periph != NULL))
1678 retval = DM_RET_DESCEND;
1680 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1683 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1684 cdm->status = CAM_DEV_MATCH_ERROR;
1689 * If the copy flag is set, copy this device out.
1691 if (retval & DM_RET_COPY) {
1694 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1695 sizeof(struct dev_match_result));
1698 * If we don't have enough space to put in another
1699 * match result, save our position and tell the
1700 * user there are more devices to check.
1702 if (spaceleft < sizeof(struct dev_match_result)) {
1703 bzero(&cdm->pos, sizeof(cdm->pos));
1704 cdm->pos.position_type =
1705 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1706 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1708 cdm->pos.cookie.bus = device->target->bus;
1709 cdm->pos.generations[CAM_BUS_GENERATION]=
1710 xsoftc.bus_generation;
1711 cdm->pos.cookie.target = device->target;
1712 cdm->pos.generations[CAM_TARGET_GENERATION] =
1713 device->target->bus->generation;
1714 cdm->pos.cookie.device = device;
1715 cdm->pos.generations[CAM_DEV_GENERATION] =
1716 device->target->generation;
1717 cdm->status = CAM_DEV_MATCH_MORE;
1720 j = cdm->num_matches;
1722 cdm->matches[j].type = DEV_MATCH_DEVICE;
1723 cdm->matches[j].result.device_result.path_id =
1724 device->target->bus->path_id;
1725 cdm->matches[j].result.device_result.target_id =
1726 device->target->target_id;
1727 cdm->matches[j].result.device_result.target_lun =
1729 cdm->matches[j].result.device_result.protocol =
1731 bcopy(&device->inq_data,
1732 &cdm->matches[j].result.device_result.inq_data,
1733 sizeof(struct scsi_inquiry_data));
1734 bcopy(&device->ident_data,
1735 &cdm->matches[j].result.device_result.ident_data,
1736 sizeof(struct ata_params));
1738 /* Let the user know whether this device is unconfigured */
1739 if (device->flags & CAM_DEV_UNCONFIGURED)
1740 cdm->matches[j].result.device_result.flags =
1741 DEV_RESULT_UNCONFIGURED;
1743 cdm->matches[j].result.device_result.flags =
1748 * If the user isn't interested in peripherals, don't descend
1749 * the tree any further.
1751 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1755 * If there is a peripheral list generation recorded, make sure
1756 * it hasn't changed.
1759 mtx_lock(&bus->eb_mtx);
1760 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1761 && (cdm->pos.cookie.bus == bus)
1762 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1763 && (cdm->pos.cookie.target == device->target)
1764 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1765 && (cdm->pos.cookie.device == device)
1766 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1767 && (cdm->pos.cookie.periph != NULL)) {
1768 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1769 device->generation) {
1770 mtx_unlock(&bus->eb_mtx);
1772 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1775 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1779 mtx_unlock(&bus->eb_mtx);
1782 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1786 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1788 struct ccb_dev_match *cdm;
1789 dev_match_ret retval;
1791 cdm = (struct ccb_dev_match *)arg;
1793 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1795 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1796 cdm->status = CAM_DEV_MATCH_ERROR;
1801 * If the copy flag is set, copy this peripheral out.
1803 if (retval & DM_RET_COPY) {
1806 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1807 sizeof(struct dev_match_result));
1810 * If we don't have enough space to put in another
1811 * match result, save our position and tell the
1812 * user there are more devices to check.
1814 if (spaceleft < sizeof(struct dev_match_result)) {
1815 bzero(&cdm->pos, sizeof(cdm->pos));
1816 cdm->pos.position_type =
1817 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1818 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1821 cdm->pos.cookie.bus = periph->path->bus;
1822 cdm->pos.generations[CAM_BUS_GENERATION]=
1823 xsoftc.bus_generation;
1824 cdm->pos.cookie.target = periph->path->target;
1825 cdm->pos.generations[CAM_TARGET_GENERATION] =
1826 periph->path->bus->generation;
1827 cdm->pos.cookie.device = periph->path->device;
1828 cdm->pos.generations[CAM_DEV_GENERATION] =
1829 periph->path->target->generation;
1830 cdm->pos.cookie.periph = periph;
1831 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1832 periph->path->device->generation;
1833 cdm->status = CAM_DEV_MATCH_MORE;
1837 j = cdm->num_matches;
1839 cdm->matches[j].type = DEV_MATCH_PERIPH;
1840 cdm->matches[j].result.periph_result.path_id =
1841 periph->path->bus->path_id;
1842 cdm->matches[j].result.periph_result.target_id =
1843 periph->path->target->target_id;
1844 cdm->matches[j].result.periph_result.target_lun =
1845 periph->path->device->lun_id;
1846 cdm->matches[j].result.periph_result.unit_number =
1847 periph->unit_number;
1848 strncpy(cdm->matches[j].result.periph_result.periph_name,
1849 periph->periph_name, DEV_IDLEN);
1856 xptedtmatch(struct ccb_dev_match *cdm)
1861 cdm->num_matches = 0;
1864 * Check the bus list generation. If it has changed, the user
1865 * needs to reset everything and start over.
1868 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1869 && (cdm->pos.cookie.bus != NULL)) {
1870 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1871 xsoftc.bus_generation) {
1873 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1876 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1882 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1885 * If we get back 0, that means that we had to stop before fully
1886 * traversing the EDT. It also means that one of the subroutines
1887 * has set the status field to the proper value. If we get back 1,
1888 * we've fully traversed the EDT and copied out any matching entries.
1891 cdm->status = CAM_DEV_MATCH_LAST;
1897 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1899 struct cam_periph *periph;
1900 struct ccb_dev_match *cdm;
1902 cdm = (struct ccb_dev_match *)arg;
1905 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1906 && (cdm->pos.cookie.pdrv == pdrv)
1907 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1908 && (cdm->pos.cookie.periph != NULL)) {
1909 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1910 (*pdrv)->generation) {
1912 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1915 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1921 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1925 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1927 struct ccb_dev_match *cdm;
1928 dev_match_ret retval;
1930 cdm = (struct ccb_dev_match *)arg;
1932 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1934 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1935 cdm->status = CAM_DEV_MATCH_ERROR;
1940 * If the copy flag is set, copy this peripheral out.
1942 if (retval & DM_RET_COPY) {
1945 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1946 sizeof(struct dev_match_result));
1949 * If we don't have enough space to put in another
1950 * match result, save our position and tell the
1951 * user there are more devices to check.
1953 if (spaceleft < sizeof(struct dev_match_result)) {
1954 struct periph_driver **pdrv;
1957 bzero(&cdm->pos, sizeof(cdm->pos));
1958 cdm->pos.position_type =
1959 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1963 * This may look a bit non-sensical, but it is
1964 * actually quite logical. There are very few
1965 * peripheral drivers, and bloating every peripheral
1966 * structure with a pointer back to its parent
1967 * peripheral driver linker set entry would cost
1968 * more in the long run than doing this quick lookup.
1970 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1971 if (strcmp((*pdrv)->driver_name,
1972 periph->periph_name) == 0)
1976 if (*pdrv == NULL) {
1977 cdm->status = CAM_DEV_MATCH_ERROR;
1981 cdm->pos.cookie.pdrv = pdrv;
1983 * The periph generation slot does double duty, as
1984 * does the periph pointer slot. They are used for
1985 * both edt and pdrv lookups and positioning.
1987 cdm->pos.cookie.periph = periph;
1988 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1989 (*pdrv)->generation;
1990 cdm->status = CAM_DEV_MATCH_MORE;
1994 j = cdm->num_matches;
1996 cdm->matches[j].type = DEV_MATCH_PERIPH;
1997 cdm->matches[j].result.periph_result.path_id =
1998 periph->path->bus->path_id;
2001 * The transport layer peripheral doesn't have a target or
2004 if (periph->path->target)
2005 cdm->matches[j].result.periph_result.target_id =
2006 periph->path->target->target_id;
2008 cdm->matches[j].result.periph_result.target_id =
2009 CAM_TARGET_WILDCARD;
2011 if (periph->path->device)
2012 cdm->matches[j].result.periph_result.target_lun =
2013 periph->path->device->lun_id;
2015 cdm->matches[j].result.periph_result.target_lun =
2018 cdm->matches[j].result.periph_result.unit_number =
2019 periph->unit_number;
2020 strncpy(cdm->matches[j].result.periph_result.periph_name,
2021 periph->periph_name, DEV_IDLEN);
2028 xptperiphlistmatch(struct ccb_dev_match *cdm)
2032 cdm->num_matches = 0;
2035 * At this point in the edt traversal function, we check the bus
2036 * list generation to make sure that no busses have been added or
2037 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2038 * For the peripheral driver list traversal function, however, we
2039 * don't have to worry about new peripheral driver types coming or
2040 * going; they're in a linker set, and therefore can't change
2041 * without a recompile.
2044 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2045 && (cdm->pos.cookie.pdrv != NULL))
2046 ret = xptpdrvtraverse(
2047 (struct periph_driver **)cdm->pos.cookie.pdrv,
2048 xptplistpdrvfunc, cdm);
2050 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2053 * If we get back 0, that means that we had to stop before fully
2054 * traversing the peripheral driver tree. It also means that one of
2055 * the subroutines has set the status field to the proper value. If
2056 * we get back 1, we've fully traversed the EDT and copied out any
2060 cdm->status = CAM_DEV_MATCH_LAST;
2066 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2068 struct cam_eb *bus, *next_bus;
2076 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2084 for (; bus != NULL; bus = next_bus) {
2085 retval = tr_func(bus, arg);
2087 xpt_release_bus(bus);
2091 next_bus = TAILQ_NEXT(bus, links);
2093 next_bus->refcount++;
2095 xpt_release_bus(bus);
2101 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2102 xpt_targetfunc_t *tr_func, void *arg)
2104 struct cam_et *target, *next_target;
2109 target = start_target;
2111 mtx_lock(&bus->eb_mtx);
2112 target = TAILQ_FIRST(&bus->et_entries);
2113 if (target == NULL) {
2114 mtx_unlock(&bus->eb_mtx);
2118 mtx_unlock(&bus->eb_mtx);
2120 for (; target != NULL; target = next_target) {
2121 retval = tr_func(target, arg);
2123 xpt_release_target(target);
2126 mtx_lock(&bus->eb_mtx);
2127 next_target = TAILQ_NEXT(target, links);
2129 next_target->refcount++;
2130 mtx_unlock(&bus->eb_mtx);
2131 xpt_release_target(target);
2137 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2138 xpt_devicefunc_t *tr_func, void *arg)
2141 struct cam_ed *device, *next_device;
2147 device = start_device;
2149 mtx_lock(&bus->eb_mtx);
2150 device = TAILQ_FIRST(&target->ed_entries);
2151 if (device == NULL) {
2152 mtx_unlock(&bus->eb_mtx);
2156 mtx_unlock(&bus->eb_mtx);
2158 for (; device != NULL; device = next_device) {
2159 mtx_lock(&device->device_mtx);
2160 retval = tr_func(device, arg);
2161 mtx_unlock(&device->device_mtx);
2163 xpt_release_device(device);
2166 mtx_lock(&bus->eb_mtx);
2167 next_device = TAILQ_NEXT(device, links);
2169 next_device->refcount++;
2170 mtx_unlock(&bus->eb_mtx);
2171 xpt_release_device(device);
2177 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2178 xpt_periphfunc_t *tr_func, void *arg)
2181 struct cam_periph *periph, *next_periph;
2186 bus = device->target->bus;
2188 periph = start_periph;
2191 mtx_lock(&bus->eb_mtx);
2192 periph = SLIST_FIRST(&device->periphs);
2193 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2194 periph = SLIST_NEXT(periph, periph_links);
2195 if (periph == NULL) {
2196 mtx_unlock(&bus->eb_mtx);
2201 mtx_unlock(&bus->eb_mtx);
2204 for (; periph != NULL; periph = next_periph) {
2205 retval = tr_func(periph, arg);
2207 cam_periph_release_locked(periph);
2211 mtx_lock(&bus->eb_mtx);
2212 next_periph = SLIST_NEXT(periph, periph_links);
2213 while (next_periph != NULL &&
2214 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2215 next_periph = SLIST_NEXT(next_periph, periph_links);
2217 next_periph->refcount++;
2218 mtx_unlock(&bus->eb_mtx);
2220 cam_periph_release_locked(periph);
2226 xptpdrvtraverse(struct periph_driver **start_pdrv,
2227 xpt_pdrvfunc_t *tr_func, void *arg)
2229 struct periph_driver **pdrv;
2235 * We don't traverse the peripheral driver list like we do the
2236 * other lists, because it is a linker set, and therefore cannot be
2237 * changed during runtime. If the peripheral driver list is ever
2238 * re-done to be something other than a linker set (i.e. it can
2239 * change while the system is running), the list traversal should
2240 * be modified to work like the other traversal functions.
2242 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2243 *pdrv != NULL; pdrv++) {
2244 retval = tr_func(pdrv, arg);
2254 xptpdperiphtraverse(struct periph_driver **pdrv,
2255 struct cam_periph *start_periph,
2256 xpt_periphfunc_t *tr_func, void *arg)
2258 struct cam_periph *periph, *next_periph;
2264 periph = start_periph;
2267 periph = TAILQ_FIRST(&(*pdrv)->units);
2268 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2269 periph = TAILQ_NEXT(periph, unit_links);
2270 if (periph == NULL) {
2277 for (; periph != NULL; periph = next_periph) {
2278 cam_periph_lock(periph);
2279 retval = tr_func(periph, arg);
2280 cam_periph_unlock(periph);
2282 cam_periph_release(periph);
2286 next_periph = TAILQ_NEXT(periph, unit_links);
2287 while (next_periph != NULL &&
2288 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2289 next_periph = TAILQ_NEXT(next_periph, unit_links);
2291 next_periph->refcount++;
2293 cam_periph_release(periph);
2299 xptdefbusfunc(struct cam_eb *bus, void *arg)
2301 struct xpt_traverse_config *tr_config;
2303 tr_config = (struct xpt_traverse_config *)arg;
2305 if (tr_config->depth == XPT_DEPTH_BUS) {
2306 xpt_busfunc_t *tr_func;
2308 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2310 return(tr_func(bus, tr_config->tr_arg));
2312 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2316 xptdeftargetfunc(struct cam_et *target, void *arg)
2318 struct xpt_traverse_config *tr_config;
2320 tr_config = (struct xpt_traverse_config *)arg;
2322 if (tr_config->depth == XPT_DEPTH_TARGET) {
2323 xpt_targetfunc_t *tr_func;
2325 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2327 return(tr_func(target, tr_config->tr_arg));
2329 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2333 xptdefdevicefunc(struct cam_ed *device, void *arg)
2335 struct xpt_traverse_config *tr_config;
2337 tr_config = (struct xpt_traverse_config *)arg;
2339 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2340 xpt_devicefunc_t *tr_func;
2342 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2344 return(tr_func(device, tr_config->tr_arg));
2346 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2350 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2352 struct xpt_traverse_config *tr_config;
2353 xpt_periphfunc_t *tr_func;
2355 tr_config = (struct xpt_traverse_config *)arg;
2357 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2360 * Unlike the other default functions, we don't check for depth
2361 * here. The peripheral driver level is the last level in the EDT,
2362 * so if we're here, we should execute the function in question.
2364 return(tr_func(periph, tr_config->tr_arg));
2368 * Execute the given function for every bus in the EDT.
2371 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2373 struct xpt_traverse_config tr_config;
2375 tr_config.depth = XPT_DEPTH_BUS;
2376 tr_config.tr_func = tr_func;
2377 tr_config.tr_arg = arg;
2379 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2383 * Execute the given function for every device in the EDT.
2386 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2388 struct xpt_traverse_config tr_config;
2390 tr_config.depth = XPT_DEPTH_DEVICE;
2391 tr_config.tr_func = tr_func;
2392 tr_config.tr_arg = arg;
2394 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2398 xptsetasyncfunc(struct cam_ed *device, void *arg)
2400 struct cam_path path;
2401 struct ccb_getdev cgd;
2402 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2405 * Don't report unconfigured devices (Wildcard devs,
2406 * devices only for target mode, device instances
2407 * that have been invalidated but are waiting for
2408 * their last reference count to be released).
2410 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2413 xpt_compile_path(&path,
2415 device->target->bus->path_id,
2416 device->target->target_id,
2418 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2419 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2420 xpt_action((union ccb *)&cgd);
2421 csa->callback(csa->callback_arg,
2424 xpt_release_path(&path);
2430 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2432 struct cam_path path;
2433 struct ccb_pathinq cpi;
2434 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2436 xpt_compile_path(&path, /*periph*/NULL,
2438 CAM_TARGET_WILDCARD,
2440 xpt_path_lock(&path);
2441 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2442 cpi.ccb_h.func_code = XPT_PATH_INQ;
2443 xpt_action((union ccb *)&cpi);
2444 csa->callback(csa->callback_arg,
2447 xpt_path_unlock(&path);
2448 xpt_release_path(&path);
2454 xpt_action(union ccb *start_ccb)
2457 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2459 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2460 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2464 xpt_action_default(union ccb *start_ccb)
2466 struct cam_path *path;
2467 struct cam_sim *sim;
2470 path = start_ccb->ccb_h.path;
2471 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2473 switch (start_ccb->ccb_h.func_code) {
2476 struct cam_ed *device;
2479 * For the sake of compatibility with SCSI-1
2480 * devices that may not understand the identify
2481 * message, we include lun information in the
2482 * second byte of all commands. SCSI-1 specifies
2483 * that luns are a 3 bit value and reserves only 3
2484 * bits for lun information in the CDB. Later
2485 * revisions of the SCSI spec allow for more than 8
2486 * luns, but have deprecated lun information in the
2487 * CDB. So, if the lun won't fit, we must omit.
2489 * Also be aware that during initial probing for devices,
2490 * the inquiry information is unknown but initialized to 0.
2491 * This means that this code will be exercised while probing
2492 * devices with an ANSI revision greater than 2.
2494 device = path->device;
2495 if (device->protocol_version <= SCSI_REV_2
2496 && start_ccb->ccb_h.target_lun < 8
2497 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2499 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2500 start_ccb->ccb_h.target_lun << 5;
2502 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2506 case XPT_CONT_TARGET_IO:
2507 start_ccb->csio.sense_resid = 0;
2508 start_ccb->csio.resid = 0;
2511 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2512 start_ccb->ataio.resid = 0;
2518 struct cam_devq *devq;
2520 devq = path->bus->sim->devq;
2521 mtx_lock(&devq->send_mtx);
2522 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2523 if (xpt_schedule_devq(devq, path->device) != 0)
2525 mtx_unlock(&devq->send_mtx);
2528 case XPT_CALC_GEOMETRY:
2529 /* Filter out garbage */
2530 if (start_ccb->ccg.block_size == 0
2531 || start_ccb->ccg.volume_size == 0) {
2532 start_ccb->ccg.cylinders = 0;
2533 start_ccb->ccg.heads = 0;
2534 start_ccb->ccg.secs_per_track = 0;
2535 start_ccb->ccb_h.status = CAM_REQ_CMP;
2538 #if defined(PC98) || defined(__sparc64__)
2540 * In a PC-98 system, geometry translation depens on
2541 * the "real" device geometry obtained from mode page 4.
2542 * SCSI geometry translation is performed in the
2543 * initialization routine of the SCSI BIOS and the result
2544 * stored in host memory. If the translation is available
2545 * in host memory, use it. If not, rely on the default
2546 * translation the device driver performs.
2547 * For sparc64, we may need adjust the geometry of large
2548 * disks in order to fit the limitations of the 16-bit
2549 * fields of the VTOC8 disk label.
2551 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2552 start_ccb->ccb_h.status = CAM_REQ_CMP;
2559 union ccb* abort_ccb;
2561 abort_ccb = start_ccb->cab.abort_ccb;
2562 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2564 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2565 struct cam_ccbq *ccbq;
2566 struct cam_ed *device;
2568 device = abort_ccb->ccb_h.path->device;
2569 ccbq = &device->ccbq;
2570 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2571 abort_ccb->ccb_h.status =
2572 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2573 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2574 xpt_done(abort_ccb);
2575 start_ccb->ccb_h.status = CAM_REQ_CMP;
2578 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2579 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2581 * We've caught this ccb en route to
2582 * the SIM. Flag it for abort and the
2583 * SIM will do so just before starting
2584 * real work on the CCB.
2586 abort_ccb->ccb_h.status =
2587 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2588 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2589 start_ccb->ccb_h.status = CAM_REQ_CMP;
2593 if (XPT_FC_IS_QUEUED(abort_ccb)
2594 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2596 * It's already completed but waiting
2597 * for our SWI to get to it.
2599 start_ccb->ccb_h.status = CAM_UA_ABORT;
2603 * If we weren't able to take care of the abort request
2604 * in the XPT, pass the request down to the SIM for processing.
2608 case XPT_ACCEPT_TARGET_IO:
2610 case XPT_IMMED_NOTIFY:
2611 case XPT_NOTIFY_ACK:
2613 case XPT_IMMEDIATE_NOTIFY:
2614 case XPT_NOTIFY_ACKNOWLEDGE:
2615 case XPT_GET_SIM_KNOB:
2616 case XPT_SET_SIM_KNOB:
2617 case XPT_GET_TRAN_SETTINGS:
2618 case XPT_SET_TRAN_SETTINGS:
2621 sim = path->bus->sim;
2622 lock = (mtx_owned(sim->mtx) == 0);
2625 (*(sim->sim_action))(sim, start_ccb);
2627 CAM_SIM_UNLOCK(sim);
2629 case XPT_PATH_STATS:
2630 start_ccb->cpis.last_reset = path->bus->last_reset;
2631 start_ccb->ccb_h.status = CAM_REQ_CMP;
2638 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2639 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2641 struct ccb_getdev *cgd;
2643 cgd = &start_ccb->cgd;
2644 cgd->protocol = dev->protocol;
2645 cgd->inq_data = dev->inq_data;
2646 cgd->ident_data = dev->ident_data;
2647 cgd->inq_flags = dev->inq_flags;
2648 cgd->ccb_h.status = CAM_REQ_CMP;
2649 cgd->serial_num_len = dev->serial_num_len;
2650 if ((dev->serial_num_len > 0)
2651 && (dev->serial_num != NULL))
2652 bcopy(dev->serial_num, cgd->serial_num,
2653 dev->serial_num_len);
2657 case XPT_GDEV_STATS:
2662 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2663 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2665 struct ccb_getdevstats *cgds;
2668 struct cam_devq *devq;
2670 cgds = &start_ccb->cgds;
2673 devq = bus->sim->devq;
2674 mtx_lock(&devq->send_mtx);
2675 cgds->dev_openings = dev->ccbq.dev_openings;
2676 cgds->dev_active = dev->ccbq.dev_active;
2677 cgds->allocated = dev->ccbq.allocated;
2678 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2679 cgds->held = cgds->allocated - cgds->dev_active -
2681 cgds->last_reset = tar->last_reset;
2682 cgds->maxtags = dev->maxtags;
2683 cgds->mintags = dev->mintags;
2684 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2685 cgds->last_reset = bus->last_reset;
2686 mtx_unlock(&devq->send_mtx);
2687 cgds->ccb_h.status = CAM_REQ_CMP;
2693 struct cam_periph *nperiph;
2694 struct periph_list *periph_head;
2695 struct ccb_getdevlist *cgdl;
2697 struct cam_ed *device;
2704 * Don't want anyone mucking with our data.
2706 device = path->device;
2707 periph_head = &device->periphs;
2708 cgdl = &start_ccb->cgdl;
2711 * Check and see if the list has changed since the user
2712 * last requested a list member. If so, tell them that the
2713 * list has changed, and therefore they need to start over
2714 * from the beginning.
2716 if ((cgdl->index != 0) &&
2717 (cgdl->generation != device->generation)) {
2718 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2723 * Traverse the list of peripherals and attempt to find
2724 * the requested peripheral.
2726 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2727 (nperiph != NULL) && (i <= cgdl->index);
2728 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2729 if (i == cgdl->index) {
2730 strncpy(cgdl->periph_name,
2731 nperiph->periph_name,
2733 cgdl->unit_number = nperiph->unit_number;
2738 cgdl->status = CAM_GDEVLIST_ERROR;
2742 if (nperiph == NULL)
2743 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2745 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2748 cgdl->generation = device->generation;
2750 cgdl->ccb_h.status = CAM_REQ_CMP;
2755 dev_pos_type position_type;
2756 struct ccb_dev_match *cdm;
2758 cdm = &start_ccb->cdm;
2761 * There are two ways of getting at information in the EDT.
2762 * The first way is via the primary EDT tree. It starts
2763 * with a list of busses, then a list of targets on a bus,
2764 * then devices/luns on a target, and then peripherals on a
2765 * device/lun. The "other" way is by the peripheral driver
2766 * lists. The peripheral driver lists are organized by
2767 * peripheral driver. (obviously) So it makes sense to
2768 * use the peripheral driver list if the user is looking
2769 * for something like "da1", or all "da" devices. If the
2770 * user is looking for something on a particular bus/target
2771 * or lun, it's generally better to go through the EDT tree.
2774 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2775 position_type = cdm->pos.position_type;
2779 position_type = CAM_DEV_POS_NONE;
2781 for (i = 0; i < cdm->num_patterns; i++) {
2782 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2783 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2784 position_type = CAM_DEV_POS_EDT;
2789 if (cdm->num_patterns == 0)
2790 position_type = CAM_DEV_POS_EDT;
2791 else if (position_type == CAM_DEV_POS_NONE)
2792 position_type = CAM_DEV_POS_PDRV;
2795 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2796 case CAM_DEV_POS_EDT:
2799 case CAM_DEV_POS_PDRV:
2800 xptperiphlistmatch(cdm);
2803 cdm->status = CAM_DEV_MATCH_ERROR;
2807 if (cdm->status == CAM_DEV_MATCH_ERROR)
2808 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2810 start_ccb->ccb_h.status = CAM_REQ_CMP;
2816 struct ccb_setasync *csa;
2817 struct async_node *cur_entry;
2818 struct async_list *async_head;
2821 csa = &start_ccb->csa;
2822 added = csa->event_enable;
2823 async_head = &path->device->asyncs;
2826 * If there is already an entry for us, simply
2829 cur_entry = SLIST_FIRST(async_head);
2830 while (cur_entry != NULL) {
2831 if ((cur_entry->callback_arg == csa->callback_arg)
2832 && (cur_entry->callback == csa->callback))
2834 cur_entry = SLIST_NEXT(cur_entry, links);
2837 if (cur_entry != NULL) {
2839 * If the request has no flags set,
2842 added &= ~cur_entry->event_enable;
2843 if (csa->event_enable == 0) {
2844 SLIST_REMOVE(async_head, cur_entry,
2846 xpt_release_device(path->device);
2847 free(cur_entry, M_CAMXPT);
2849 cur_entry->event_enable = csa->event_enable;
2851 csa->event_enable = added;
2853 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2855 if (cur_entry == NULL) {
2856 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2859 cur_entry->event_enable = csa->event_enable;
2860 cur_entry->event_lock =
2861 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2862 cur_entry->callback_arg = csa->callback_arg;
2863 cur_entry->callback = csa->callback;
2864 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2865 xpt_acquire_device(path->device);
2867 start_ccb->ccb_h.status = CAM_REQ_CMP;
2872 struct ccb_relsim *crs;
2875 crs = &start_ccb->crs;
2879 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2883 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2885 /* Don't ever go below one opening */
2886 if (crs->openings > 0) {
2887 xpt_dev_ccbq_resize(path, crs->openings);
2890 "number of openings is now %d\n",
2896 mtx_lock(&dev->sim->devq->send_mtx);
2897 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2899 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2902 * Just extend the old timeout and decrement
2903 * the freeze count so that a single timeout
2904 * is sufficient for releasing the queue.
2906 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2907 callout_stop(&dev->callout);
2910 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2913 callout_reset_sbt(&dev->callout,
2914 SBT_1MS * crs->release_timeout, 0,
2915 xpt_release_devq_timeout, dev, 0);
2917 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2921 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2923 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2925 * Decrement the freeze count so that a single
2926 * completion is still sufficient to unfreeze
2929 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2932 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2933 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2937 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2939 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2940 || (dev->ccbq.dev_active == 0)) {
2942 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2945 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2946 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2949 mtx_unlock(&dev->sim->devq->send_mtx);
2951 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2952 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2953 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2954 start_ccb->ccb_h.status = CAM_REQ_CMP;
2958 struct cam_path *oldpath;
2960 /* Check that all request bits are supported. */
2961 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2962 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2966 cam_dflags = CAM_DEBUG_NONE;
2967 if (cam_dpath != NULL) {
2968 oldpath = cam_dpath;
2970 xpt_free_path(oldpath);
2972 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2973 if (xpt_create_path(&cam_dpath, NULL,
2974 start_ccb->ccb_h.path_id,
2975 start_ccb->ccb_h.target_id,
2976 start_ccb->ccb_h.target_lun) !=
2978 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2980 cam_dflags = start_ccb->cdbg.flags;
2981 start_ccb->ccb_h.status = CAM_REQ_CMP;
2982 xpt_print(cam_dpath, "debugging flags now %x\n",
2986 start_ccb->ccb_h.status = CAM_REQ_CMP;
2990 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2991 xpt_freeze_devq(path, 1);
2992 start_ccb->ccb_h.status = CAM_REQ_CMP;
2999 printf("%s: CCB type %#x not supported\n", __func__,
3000 start_ccb->ccb_h.func_code);
3001 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3002 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3003 xpt_done(start_ccb);
3010 xpt_polled_action(union ccb *start_ccb)
3013 struct cam_sim *sim;
3014 struct cam_devq *devq;
3017 timeout = start_ccb->ccb_h.timeout * 10;
3018 sim = start_ccb->ccb_h.path->bus->sim;
3020 dev = start_ccb->ccb_h.path->device;
3022 mtx_unlock(&dev->device_mtx);
3025 * Steal an opening so that no other queued requests
3026 * can get it before us while we simulate interrupts.
3028 mtx_lock(&devq->send_mtx);
3029 dev->ccbq.dev_openings--;
3030 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3032 mtx_unlock(&devq->send_mtx);
3035 (*(sim->sim_poll))(sim);
3036 CAM_SIM_UNLOCK(sim);
3038 mtx_lock(&devq->send_mtx);
3040 dev->ccbq.dev_openings++;
3041 mtx_unlock(&devq->send_mtx);
3044 xpt_action(start_ccb);
3045 while(--timeout > 0) {
3047 (*(sim->sim_poll))(sim);
3048 CAM_SIM_UNLOCK(sim);
3050 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3057 * XXX Is it worth adding a sim_timeout entry
3058 * point so we can attempt recovery? If
3059 * this is only used for dumps, I don't think
3062 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3065 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3068 mtx_lock(&dev->device_mtx);
3072 * Schedule a peripheral driver to receive a ccb when its
3073 * target device has space for more transactions.
3076 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3079 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3080 cam_periph_assert(periph, MA_OWNED);
3081 if (new_priority < periph->scheduled_priority) {
3082 periph->scheduled_priority = new_priority;
3083 xpt_run_allocq(periph, 0);
3089 * Schedule a device to run on a given queue.
3090 * If the device was inserted as a new entry on the queue,
3091 * return 1 meaning the device queue should be run. If we
3092 * were already queued, implying someone else has already
3093 * started the queue, return 0 so the caller doesn't attempt
3097 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3098 u_int32_t new_priority)
3101 u_int32_t old_priority;
3103 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3105 old_priority = pinfo->priority;
3108 * Are we already queued?
3110 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3111 /* Simply reorder based on new priority */
3112 if (new_priority < old_priority) {
3113 camq_change_priority(queue, pinfo->index,
3115 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3116 ("changed priority to %d\n",
3122 /* New entry on the queue */
3123 if (new_priority < old_priority)
3124 pinfo->priority = new_priority;
3126 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3127 ("Inserting onto queue\n"));
3128 pinfo->generation = ++queue->generation;
3129 camq_insert(queue, pinfo);
3136 xpt_run_allocq_task(void *context, int pending)
3138 struct cam_periph *periph = context;
3140 cam_periph_lock(periph);
3141 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3142 xpt_run_allocq(periph, 1);
3143 cam_periph_unlock(periph);
3144 cam_periph_release(periph);
3148 xpt_run_allocq(struct cam_periph *periph, int sleep)
3150 struct cam_ed *device;
3154 cam_periph_assert(periph, MA_OWNED);
3155 if (periph->periph_allocating)
3157 periph->periph_allocating = 1;
3158 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3159 device = periph->path->device;
3162 while ((prio = min(periph->scheduled_priority,
3163 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3164 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3165 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3168 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3170 ccb = xpt_get_ccb(periph);
3173 if (periph->flags & CAM_PERIPH_RUN_TASK)
3175 cam_periph_doacquire(periph);
3176 periph->flags |= CAM_PERIPH_RUN_TASK;
3177 taskqueue_enqueue(xsoftc.xpt_taskq,
3178 &periph->periph_run_task);
3181 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3182 if (prio == periph->immediate_priority) {
3183 periph->immediate_priority = CAM_PRIORITY_NONE;
3184 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3185 ("waking cam_periph_getccb()\n"));
3186 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3188 wakeup(&periph->ccb_list);
3190 periph->scheduled_priority = CAM_PRIORITY_NONE;
3191 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3192 ("calling periph_start()\n"));
3193 periph->periph_start(periph, ccb);
3198 xpt_release_ccb(ccb);
3199 periph->periph_allocating = 0;
3203 xpt_run_devq(struct cam_devq *devq)
3205 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3208 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3210 devq->send_queue.qfrozen_cnt++;
3211 while ((devq->send_queue.entries > 0)
3212 && (devq->send_openings > 0)
3213 && (devq->send_queue.qfrozen_cnt <= 1)) {
3214 struct cam_ed *device;
3215 union ccb *work_ccb;
3216 struct cam_sim *sim;
3218 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3220 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3221 ("running device %p\n", device));
3223 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3224 if (work_ccb == NULL) {
3225 printf("device on run queue with no ccbs???\n");
3229 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3231 mtx_lock(&xsoftc.xpt_highpower_lock);
3232 if (xsoftc.num_highpower <= 0) {
3234 * We got a high power command, but we
3235 * don't have any available slots. Freeze
3236 * the device queue until we have a slot
3239 xpt_freeze_devq_device(device, 1);
3240 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3243 mtx_unlock(&xsoftc.xpt_highpower_lock);
3247 * Consume a high power slot while
3250 xsoftc.num_highpower--;
3252 mtx_unlock(&xsoftc.xpt_highpower_lock);
3254 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3255 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3256 devq->send_openings--;
3257 devq->send_active++;
3258 xpt_schedule_devq(devq, device);
3259 mtx_unlock(&devq->send_mtx);
3261 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3263 * The client wants to freeze the queue
3264 * after this CCB is sent.
3266 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3269 /* In Target mode, the peripheral driver knows best... */
3270 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3271 if ((device->inq_flags & SID_CmdQue) != 0
3272 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3273 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3276 * Clear this in case of a retried CCB that
3277 * failed due to a rejected tag.
3279 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3282 switch (work_ccb->ccb_h.func_code) {
3284 CAM_DEBUG(work_ccb->ccb_h.path,
3285 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3286 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3288 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3289 cdb_str, sizeof(cdb_str))));
3292 CAM_DEBUG(work_ccb->ccb_h.path,
3293 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3294 ata_op_string(&work_ccb->ataio.cmd),
3295 ata_cmd_string(&work_ccb->ataio.cmd,
3296 cdb_str, sizeof(cdb_str))));
3303 * Device queues can be shared among multiple SIM instances
3304 * that reside on different busses. Use the SIM from the
3305 * queued device, rather than the one from the calling bus.
3308 lock = (mtx_owned(sim->mtx) == 0);
3311 (*(sim->sim_action))(sim, work_ccb);
3313 CAM_SIM_UNLOCK(sim);
3314 mtx_lock(&devq->send_mtx);
3316 devq->send_queue.qfrozen_cnt--;
3320 * This function merges stuff from the slave ccb into the master ccb, while
3321 * keeping important fields in the master ccb constant.
3324 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3328 * Pull fields that are valid for peripheral drivers to set
3329 * into the master CCB along with the CCB "payload".
3331 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3332 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3333 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3334 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3335 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3336 sizeof(union ccb) - sizeof(struct ccb_hdr));
3340 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3343 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3344 ccb_h->pinfo.priority = priority;
3346 ccb_h->path_id = path->bus->path_id;
3348 ccb_h->target_id = path->target->target_id;
3350 ccb_h->target_id = CAM_TARGET_WILDCARD;
3352 ccb_h->target_lun = path->device->lun_id;
3353 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3355 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3357 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3362 /* Path manipulation functions */
3364 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3365 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3367 struct cam_path *path;
3370 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3373 status = CAM_RESRC_UNAVAIL;
3376 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3377 if (status != CAM_REQ_CMP) {
3378 free(path, M_CAMPATH);
3381 *new_path_ptr = path;
3386 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3387 struct cam_periph *periph, path_id_t path_id,
3388 target_id_t target_id, lun_id_t lun_id)
3391 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3396 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3397 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3400 struct cam_et *target;
3401 struct cam_ed *device;
3404 status = CAM_REQ_CMP; /* Completed without error */
3405 target = NULL; /* Wildcarded */
3406 device = NULL; /* Wildcarded */
3409 * We will potentially modify the EDT, so block interrupts
3410 * that may attempt to create cam paths.
3412 bus = xpt_find_bus(path_id);
3414 status = CAM_PATH_INVALID;
3417 mtx_lock(&bus->eb_mtx);
3418 target = xpt_find_target(bus, target_id);
3419 if (target == NULL) {
3421 struct cam_et *new_target;
3423 new_target = xpt_alloc_target(bus, target_id);
3424 if (new_target == NULL) {
3425 status = CAM_RESRC_UNAVAIL;
3427 target = new_target;
3431 if (target != NULL) {
3432 device = xpt_find_device(target, lun_id);
3433 if (device == NULL) {
3435 struct cam_ed *new_device;
3438 (*(bus->xport->alloc_device))(bus,
3441 if (new_device == NULL) {
3442 status = CAM_RESRC_UNAVAIL;
3444 device = new_device;
3448 mtx_unlock(&bus->eb_mtx);
3452 * Only touch the user's data if we are successful.
3454 if (status == CAM_REQ_CMP) {
3455 new_path->periph = perph;
3456 new_path->bus = bus;
3457 new_path->target = target;
3458 new_path->device = device;
3459 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3462 xpt_release_device(device);
3464 xpt_release_target(target);
3466 xpt_release_bus(bus);
3472 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3474 struct cam_path *new_path;
3476 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3477 if (new_path == NULL)
3478 return(CAM_RESRC_UNAVAIL);
3479 xpt_copy_path(new_path, path);
3480 *new_path_ptr = new_path;
3481 return (CAM_REQ_CMP);
3485 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3489 if (path->bus != NULL)
3490 xpt_acquire_bus(path->bus);
3491 if (path->target != NULL)
3492 xpt_acquire_target(path->target);
3493 if (path->device != NULL)
3494 xpt_acquire_device(path->device);
3498 xpt_release_path(struct cam_path *path)
3500 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3501 if (path->device != NULL) {
3502 xpt_release_device(path->device);
3503 path->device = NULL;
3505 if (path->target != NULL) {
3506 xpt_release_target(path->target);
3507 path->target = NULL;
3509 if (path->bus != NULL) {
3510 xpt_release_bus(path->bus);
3516 xpt_free_path(struct cam_path *path)
3519 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3520 xpt_release_path(path);
3521 free(path, M_CAMPATH);
3525 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3526 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3532 *bus_ref = path->bus->refcount;
3538 *periph_ref = path->periph->refcount;
3545 *target_ref = path->target->refcount;
3551 *device_ref = path->device->refcount;
3558 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3559 * in path1, 2 for match with wildcards in path2.
3562 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3566 if (path1->bus != path2->bus) {
3567 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3569 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3574 if (path1->target != path2->target) {
3575 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3578 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3583 if (path1->device != path2->device) {
3584 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3587 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3596 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3600 if (path->bus != dev->target->bus) {
3601 if (path->bus->path_id == CAM_BUS_WILDCARD)
3603 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3608 if (path->target != dev->target) {
3609 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3612 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3617 if (path->device != dev) {
3618 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3621 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3630 xpt_print_path(struct cam_path *path)
3634 printf("(nopath): ");
3636 if (path->periph != NULL)
3637 printf("(%s%d:", path->periph->periph_name,
3638 path->periph->unit_number);
3640 printf("(noperiph:");
3642 if (path->bus != NULL)
3643 printf("%s%d:%d:", path->bus->sim->sim_name,
3644 path->bus->sim->unit_number,
3645 path->bus->sim->bus_id);
3649 if (path->target != NULL)
3650 printf("%d:", path->target->target_id);
3654 if (path->device != NULL)
3655 printf("%jx): ", (uintmax_t)path->device->lun_id);
3662 xpt_print_device(struct cam_ed *device)
3666 printf("(nopath): ");
3668 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3669 device->sim->unit_number,
3670 device->sim->bus_id,
3671 device->target->target_id,
3672 (uintmax_t)device->lun_id);
3677 xpt_print(struct cam_path *path, const char *fmt, ...)
3680 xpt_print_path(path);
3687 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3691 sbuf_new(&sb, str, str_len, 0);
3694 sbuf_printf(&sb, "(nopath): ");
3696 if (path->periph != NULL)
3697 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3698 path->periph->unit_number);
3700 sbuf_printf(&sb, "(noperiph:");
3702 if (path->bus != NULL)
3703 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3704 path->bus->sim->unit_number,
3705 path->bus->sim->bus_id);
3707 sbuf_printf(&sb, "nobus:");
3709 if (path->target != NULL)
3710 sbuf_printf(&sb, "%d:", path->target->target_id);
3712 sbuf_printf(&sb, "X:");
3714 if (path->device != NULL)
3715 sbuf_printf(&sb, "%jx): ",
3716 (uintmax_t)path->device->lun_id);
3718 sbuf_printf(&sb, "X): ");
3722 return(sbuf_len(&sb));
3726 xpt_path_path_id(struct cam_path *path)
3728 return(path->bus->path_id);
3732 xpt_path_target_id(struct cam_path *path)
3734 if (path->target != NULL)
3735 return (path->target->target_id);
3737 return (CAM_TARGET_WILDCARD);
3741 xpt_path_lun_id(struct cam_path *path)
3743 if (path->device != NULL)
3744 return (path->device->lun_id);
3746 return (CAM_LUN_WILDCARD);
3750 xpt_path_sim(struct cam_path *path)
3753 return (path->bus->sim);
3757 xpt_path_periph(struct cam_path *path)
3760 return (path->periph);
3764 xpt_path_legacy_ata_id(struct cam_path *path)
3769 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3770 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3771 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3772 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3775 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3776 path->bus->sim->unit_number < 2) {
3777 bus_id = path->bus->sim->unit_number;
3781 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3782 if (bus == path->bus)
3784 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3785 bus->sim->unit_number >= 2) ||
3786 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3787 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3788 strcmp(bus->sim->sim_name, "siisch") == 0)
3793 if (path->target != NULL) {
3794 if (path->target->target_id < 2)
3795 return (bus_id * 2 + path->target->target_id);
3799 return (bus_id * 2);
3803 * Release a CAM control block for the caller. Remit the cost of the structure
3804 * to the device referenced by the path. If the this device had no 'credits'
3805 * and peripheral drivers have registered async callbacks for this notification
3809 xpt_release_ccb(union ccb *free_ccb)
3811 struct cam_ed *device;
3812 struct cam_periph *periph;
3814 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3815 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3816 device = free_ccb->ccb_h.path->device;
3817 periph = free_ccb->ccb_h.path->periph;
3819 xpt_free_ccb(free_ccb);
3820 periph->periph_allocated--;
3821 cam_ccbq_release_opening(&device->ccbq);
3822 xpt_run_allocq(periph, 0);
3825 /* Functions accessed by SIM drivers */
3827 static struct xpt_xport xport_default = {
3828 .alloc_device = xpt_alloc_device_default,
3829 .action = xpt_action_default,
3830 .async = xpt_dev_async_default,
3834 * A sim structure, listing the SIM entry points and instance
3835 * identification info is passed to xpt_bus_register to hook the SIM
3836 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3837 * for this new bus and places it in the array of busses and assigns
3838 * it a path_id. The path_id may be influenced by "hard wiring"
3839 * information specified by the user. Once interrupt services are
3840 * available, the bus will be probed.
3843 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3845 struct cam_eb *new_bus;
3846 struct cam_eb *old_bus;
3847 struct ccb_pathinq cpi;
3848 struct cam_path *path;
3851 mtx_assert(sim->mtx, MA_OWNED);
3854 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3855 M_CAMXPT, M_NOWAIT|M_ZERO);
3856 if (new_bus == NULL) {
3857 /* Couldn't satisfy request */
3858 return (CAM_RESRC_UNAVAIL);
3861 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3862 TAILQ_INIT(&new_bus->et_entries);
3865 timevalclear(&new_bus->last_reset);
3867 new_bus->refcount = 1; /* Held until a bus_deregister event */
3868 new_bus->generation = 0;
3871 sim->path_id = new_bus->path_id =
3872 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3873 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3874 while (old_bus != NULL
3875 && old_bus->path_id < new_bus->path_id)
3876 old_bus = TAILQ_NEXT(old_bus, links);
3877 if (old_bus != NULL)
3878 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3880 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3881 xsoftc.bus_generation++;
3885 * Set a default transport so that a PATH_INQ can be issued to
3886 * the SIM. This will then allow for probing and attaching of
3887 * a more appropriate transport.
3889 new_bus->xport = &xport_default;
3891 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3892 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3893 if (status != CAM_REQ_CMP) {
3894 xpt_release_bus(new_bus);
3895 free(path, M_CAMXPT);
3896 return (CAM_RESRC_UNAVAIL);
3899 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3900 cpi.ccb_h.func_code = XPT_PATH_INQ;
3901 xpt_action((union ccb *)&cpi);
3903 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3904 switch (cpi.transport) {
3912 new_bus->xport = scsi_get_xport();
3916 new_bus->xport = ata_get_xport();
3919 new_bus->xport = &xport_default;
3924 /* Notify interested parties */
3925 if (sim->path_id != CAM_XPT_PATH_ID) {
3927 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3928 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3929 union ccb *scan_ccb;
3931 /* Initiate bus rescan. */
3932 scan_ccb = xpt_alloc_ccb_nowait();
3933 if (scan_ccb != NULL) {
3934 scan_ccb->ccb_h.path = path;
3935 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3936 scan_ccb->crcn.flags = 0;
3937 xpt_rescan(scan_ccb);
3940 "Can't allocate CCB to scan bus\n");
3941 xpt_free_path(path);
3944 xpt_free_path(path);
3946 xpt_free_path(path);
3947 return (CAM_SUCCESS);
3951 xpt_bus_deregister(path_id_t pathid)
3953 struct cam_path bus_path;
3956 status = xpt_compile_path(&bus_path, NULL, pathid,
3957 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3958 if (status != CAM_REQ_CMP)
3961 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3962 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3964 /* Release the reference count held while registered. */
3965 xpt_release_bus(bus_path.bus);
3966 xpt_release_path(&bus_path);
3968 return (CAM_REQ_CMP);
3972 xptnextfreepathid(void)
3978 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3980 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3982 /* Find an unoccupied pathid */
3983 while (bus != NULL && bus->path_id <= pathid) {
3984 if (bus->path_id == pathid)
3986 bus = TAILQ_NEXT(bus, links);
3990 * Ensure that this pathid is not reserved for
3991 * a bus that may be registered in the future.
3993 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3995 /* Start the search over */
4002 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4009 pathid = CAM_XPT_PATH_ID;
4010 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4011 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4014 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4015 if (strcmp(dname, "scbus")) {
4016 /* Avoid a bit of foot shooting. */
4019 if (dunit < 0) /* unwired?! */
4021 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4022 if (sim_bus == val) {
4026 } else if (sim_bus == 0) {
4027 /* Unspecified matches bus 0 */
4031 printf("Ambiguous scbus configuration for %s%d "
4032 "bus %d, cannot wire down. The kernel "
4033 "config entry for scbus%d should "
4034 "specify a controller bus.\n"
4035 "Scbus will be assigned dynamically.\n",
4036 sim_name, sim_unit, sim_bus, dunit);
4041 if (pathid == CAM_XPT_PATH_ID)
4042 pathid = xptnextfreepathid();
4047 xpt_async_string(u_int32_t async_code)
4050 switch (async_code) {
4051 case AC_BUS_RESET: return ("AC_BUS_RESET");
4052 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4053 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4054 case AC_SENT_BDR: return ("AC_SENT_BDR");
4055 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4056 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4057 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4058 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4059 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4060 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4061 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4062 case AC_CONTRACT: return ("AC_CONTRACT");
4063 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4064 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4066 return ("AC_UNKNOWN");
4070 xpt_async_size(u_int32_t async_code)
4073 switch (async_code) {
4074 case AC_BUS_RESET: return (0);
4075 case AC_UNSOL_RESEL: return (0);
4076 case AC_SCSI_AEN: return (0);
4077 case AC_SENT_BDR: return (0);
4078 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4079 case AC_PATH_DEREGISTERED: return (0);
4080 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4081 case AC_LOST_DEVICE: return (0);
4082 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4083 case AC_INQ_CHANGED: return (0);
4084 case AC_GETDEV_CHANGED: return (0);
4085 case AC_CONTRACT: return (sizeof(struct ac_contract));
4086 case AC_ADVINFO_CHANGED: return (-1);
4087 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4093 xpt_async_process_dev(struct cam_ed *device, void *arg)
4095 union ccb *ccb = arg;
4096 struct cam_path *path = ccb->ccb_h.path;
4097 void *async_arg = ccb->casync.async_arg_ptr;
4098 u_int32_t async_code = ccb->casync.async_code;
4101 if (path->device != device
4102 && path->device->lun_id != CAM_LUN_WILDCARD
4103 && device->lun_id != CAM_LUN_WILDCARD)
4107 * The async callback could free the device.
4108 * If it is a broadcast async, it doesn't hold
4109 * device reference, so take our own reference.
4111 xpt_acquire_device(device);
4114 * If async for specific device is to be delivered to
4115 * the wildcard client, take the specific device lock.
4116 * XXX: We may need a way for client to specify it.
4118 if ((device->lun_id == CAM_LUN_WILDCARD &&
4119 path->device->lun_id != CAM_LUN_WILDCARD) ||
4120 (device->target->target_id == CAM_TARGET_WILDCARD &&
4121 path->target->target_id != CAM_TARGET_WILDCARD) ||
4122 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4123 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4124 mtx_unlock(&device->device_mtx);
4125 xpt_path_lock(path);
4130 (*(device->target->bus->xport->async))(async_code,
4131 device->target->bus, device->target, device, async_arg);
4132 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4135 xpt_path_unlock(path);
4136 mtx_lock(&device->device_mtx);
4138 xpt_release_device(device);
4143 xpt_async_process_tgt(struct cam_et *target, void *arg)
4145 union ccb *ccb = arg;
4146 struct cam_path *path = ccb->ccb_h.path;
4148 if (path->target != target
4149 && path->target->target_id != CAM_TARGET_WILDCARD
4150 && target->target_id != CAM_TARGET_WILDCARD)
4153 if (ccb->casync.async_code == AC_SENT_BDR) {
4154 /* Update our notion of when the last reset occurred */
4155 microtime(&target->last_reset);
4158 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4162 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4165 struct cam_path *path;
4167 u_int32_t async_code;
4169 path = ccb->ccb_h.path;
4170 async_code = ccb->casync.async_code;
4171 async_arg = ccb->casync.async_arg_ptr;
4172 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4173 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4176 if (async_code == AC_BUS_RESET) {
4177 /* Update our notion of when the last reset occurred */
4178 microtime(&bus->last_reset);
4181 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4184 * If this wasn't a fully wildcarded async, tell all
4185 * clients that want all async events.
4187 if (bus != xpt_periph->path->bus) {
4188 xpt_path_lock(xpt_periph->path);
4189 xpt_async_process_dev(xpt_periph->path->device, ccb);
4190 xpt_path_unlock(xpt_periph->path);
4193 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4194 xpt_release_devq(path, 1, TRUE);
4196 xpt_release_simq(path->bus->sim, TRUE);
4197 if (ccb->casync.async_arg_size > 0)
4198 free(async_arg, M_CAMXPT);
4199 xpt_free_path(path);
4204 xpt_async_bcast(struct async_list *async_head,
4205 u_int32_t async_code,
4206 struct cam_path *path, void *async_arg)
4208 struct async_node *cur_entry;
4211 cur_entry = SLIST_FIRST(async_head);
4212 while (cur_entry != NULL) {
4213 struct async_node *next_entry;
4215 * Grab the next list entry before we call the current
4216 * entry's callback. This is because the callback function
4217 * can delete its async callback entry.
4219 next_entry = SLIST_NEXT(cur_entry, links);
4220 if ((cur_entry->event_enable & async_code) != 0) {
4221 lock = cur_entry->event_lock;
4223 CAM_SIM_LOCK(path->device->sim);
4224 cur_entry->callback(cur_entry->callback_arg,
4228 CAM_SIM_UNLOCK(path->device->sim);
4230 cur_entry = next_entry;
4235 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4240 ccb = xpt_alloc_ccb_nowait();
4242 xpt_print(path, "Can't allocate CCB to send %s\n",
4243 xpt_async_string(async_code));
4247 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4248 xpt_print(path, "Can't allocate path to send %s\n",
4249 xpt_async_string(async_code));
4253 ccb->ccb_h.path->periph = NULL;
4254 ccb->ccb_h.func_code = XPT_ASYNC;
4255 ccb->ccb_h.cbfcnp = xpt_async_process;
4256 ccb->ccb_h.flags |= CAM_UNLOCKED;
4257 ccb->casync.async_code = async_code;
4258 ccb->casync.async_arg_size = 0;
4259 size = xpt_async_size(async_code);
4260 if (size > 0 && async_arg != NULL) {
4261 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4262 if (ccb->casync.async_arg_ptr == NULL) {
4263 xpt_print(path, "Can't allocate argument to send %s\n",
4264 xpt_async_string(async_code));
4265 xpt_free_path(ccb->ccb_h.path);
4269 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4270 ccb->casync.async_arg_size = size;
4271 } else if (size < 0)
4272 ccb->casync.async_arg_size = size;
4273 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4274 xpt_freeze_devq(path, 1);
4276 xpt_freeze_simq(path->bus->sim, 1);
4281 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4282 struct cam_et *target, struct cam_ed *device,
4287 * We only need to handle events for real devices.
4289 if (target->target_id == CAM_TARGET_WILDCARD
4290 || device->lun_id == CAM_LUN_WILDCARD)
4293 printf("%s called\n", __func__);
4297 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4299 struct cam_devq *devq;
4302 devq = dev->sim->devq;
4303 mtx_assert(&devq->send_mtx, MA_OWNED);
4304 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4305 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4306 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4307 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4308 /* Remove frozen device from sendq. */
4309 if (device_is_queued(dev))
4310 camq_remove(&devq->send_queue, dev->devq_entry.index);
4315 xpt_freeze_devq(struct cam_path *path, u_int count)
4317 struct cam_ed *dev = path->device;
4318 struct cam_devq *devq;
4321 devq = dev->sim->devq;
4322 mtx_lock(&devq->send_mtx);
4323 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4324 freeze = xpt_freeze_devq_device(dev, count);
4325 mtx_unlock(&devq->send_mtx);
4330 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4332 struct cam_devq *devq;
4336 mtx_lock(&devq->send_mtx);
4337 freeze = (devq->send_queue.qfrozen_cnt += count);
4338 mtx_unlock(&devq->send_mtx);
4343 xpt_release_devq_timeout(void *arg)
4346 struct cam_devq *devq;
4348 dev = (struct cam_ed *)arg;
4349 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4350 devq = dev->sim->devq;
4351 mtx_assert(&devq->send_mtx, MA_OWNED);
4352 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4357 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4360 struct cam_devq *devq;
4362 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4365 devq = dev->sim->devq;
4366 mtx_lock(&devq->send_mtx);
4367 if (xpt_release_devq_device(dev, count, run_queue))
4368 xpt_run_devq(dev->sim->devq);
4369 mtx_unlock(&devq->send_mtx);
4373 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4376 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4377 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4378 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4379 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4380 if (count > dev->ccbq.queue.qfrozen_cnt) {
4382 printf("xpt_release_devq(): requested %u > present %u\n",
4383 count, dev->ccbq.queue.qfrozen_cnt);
4385 count = dev->ccbq.queue.qfrozen_cnt;
4387 dev->ccbq.queue.qfrozen_cnt -= count;
4388 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4390 * No longer need to wait for a successful
4391 * command completion.
4393 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4395 * Remove any timeouts that might be scheduled
4396 * to release this queue.
4398 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4399 callout_stop(&dev->callout);
4400 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4403 * Now that we are unfrozen schedule the
4404 * device so any pending transactions are
4407 xpt_schedule_devq(dev->sim->devq, dev);
4414 xpt_release_simq(struct cam_sim *sim, int run_queue)
4416 struct cam_devq *devq;
4419 mtx_lock(&devq->send_mtx);
4420 if (devq->send_queue.qfrozen_cnt <= 0) {
4422 printf("xpt_release_simq: requested 1 > present %u\n",
4423 devq->send_queue.qfrozen_cnt);
4426 devq->send_queue.qfrozen_cnt--;
4427 if (devq->send_queue.qfrozen_cnt == 0) {
4429 * If there is a timeout scheduled to release this
4430 * sim queue, remove it. The queue frozen count is
4433 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4434 callout_stop(&sim->callout);
4435 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4439 * Now that we are unfrozen run the send queue.
4441 xpt_run_devq(sim->devq);
4444 mtx_unlock(&devq->send_mtx);
4448 * XXX Appears to be unused.
4451 xpt_release_simq_timeout(void *arg)
4453 struct cam_sim *sim;
4455 sim = (struct cam_sim *)arg;
4456 xpt_release_simq(sim, /* run_queue */ TRUE);
4460 xpt_done(union ccb *done_ccb)
4462 struct cam_doneq *queue;
4465 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4466 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4469 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4470 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4471 queue = &cam_doneqs[hash];
4472 mtx_lock(&queue->cam_doneq_mtx);
4473 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4474 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4475 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4476 mtx_unlock(&queue->cam_doneq_mtx);
4478 wakeup(&queue->cam_doneq);
4482 xpt_done_direct(union ccb *done_ccb)
4485 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done_direct\n"));
4486 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4489 xpt_done_process(&done_ccb->ccb_h);
4497 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4502 xpt_alloc_ccb_nowait()
4506 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4511 xpt_free_ccb(union ccb *free_ccb)
4513 free(free_ccb, M_CAMCCB);
4518 /* Private XPT functions */
4521 * Get a CAM control block for the caller. Charge the structure to the device
4522 * referenced by the path. If we don't have sufficient resources to allocate
4523 * more ccbs, we return NULL.
4526 xpt_get_ccb_nowait(struct cam_periph *periph)
4530 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_NOWAIT);
4531 if (new_ccb == NULL)
4533 periph->periph_allocated++;
4534 cam_ccbq_take_opening(&periph->path->device->ccbq);
4539 xpt_get_ccb(struct cam_periph *periph)
4543 cam_periph_unlock(periph);
4544 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_WAITOK);
4545 cam_periph_lock(periph);
4546 periph->periph_allocated++;
4547 cam_ccbq_take_opening(&periph->path->device->ccbq);
4552 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4554 struct ccb_hdr *ccb_h;
4556 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4557 cam_periph_assert(periph, MA_OWNED);
4558 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4559 ccb_h->pinfo.priority != priority) {
4560 if (priority < periph->immediate_priority) {
4561 periph->immediate_priority = priority;
4562 xpt_run_allocq(periph, 0);
4564 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4567 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4568 return ((union ccb *)ccb_h);
4572 xpt_acquire_bus(struct cam_eb *bus)
4581 xpt_release_bus(struct cam_eb *bus)
4585 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4586 if (--bus->refcount > 0) {
4590 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4591 xsoftc.bus_generation++;
4593 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4594 ("destroying bus, but target list is not empty"));
4595 cam_sim_release(bus->sim);
4596 mtx_destroy(&bus->eb_mtx);
4597 free(bus, M_CAMXPT);
4600 static struct cam_et *
4601 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4603 struct cam_et *cur_target, *target;
4605 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4606 mtx_assert(&bus->eb_mtx, MA_OWNED);
4607 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4612 TAILQ_INIT(&target->ed_entries);
4614 target->target_id = target_id;
4615 target->refcount = 1;
4616 target->generation = 0;
4617 target->luns = NULL;
4618 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4619 timevalclear(&target->last_reset);
4621 * Hold a reference to our parent bus so it
4622 * will not go away before we do.
4626 /* Insertion sort into our bus's target list */
4627 cur_target = TAILQ_FIRST(&bus->et_entries);
4628 while (cur_target != NULL && cur_target->target_id < target_id)
4629 cur_target = TAILQ_NEXT(cur_target, links);
4630 if (cur_target != NULL) {
4631 TAILQ_INSERT_BEFORE(cur_target, target, links);
4633 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4640 xpt_acquire_target(struct cam_et *target)
4642 struct cam_eb *bus = target->bus;
4644 mtx_lock(&bus->eb_mtx);
4646 mtx_unlock(&bus->eb_mtx);
4650 xpt_release_target(struct cam_et *target)
4652 struct cam_eb *bus = target->bus;
4654 mtx_lock(&bus->eb_mtx);
4655 if (--target->refcount > 0) {
4656 mtx_unlock(&bus->eb_mtx);
4659 TAILQ_REMOVE(&bus->et_entries, target, links);
4661 mtx_unlock(&bus->eb_mtx);
4662 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4663 ("destroying target, but device list is not empty"));
4664 xpt_release_bus(bus);
4665 mtx_destroy(&target->luns_mtx);
4667 free(target->luns, M_CAMXPT);
4668 free(target, M_CAMXPT);
4671 static struct cam_ed *
4672 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4675 struct cam_ed *device;
4677 device = xpt_alloc_device(bus, target, lun_id);
4681 device->mintags = 1;
4682 device->maxtags = 1;
4687 xpt_destroy_device(void *context, int pending)
4689 struct cam_ed *device = context;
4691 mtx_lock(&device->device_mtx);
4692 mtx_destroy(&device->device_mtx);
4693 free(device, M_CAMDEV);
4697 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4699 struct cam_ed *cur_device, *device;
4700 struct cam_devq *devq;
4703 mtx_assert(&bus->eb_mtx, MA_OWNED);
4704 /* Make space for us in the device queue on our bus */
4705 devq = bus->sim->devq;
4706 mtx_lock(&devq->send_mtx);
4707 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4708 mtx_unlock(&devq->send_mtx);
4709 if (status != CAM_REQ_CMP)
4712 device = (struct cam_ed *)malloc(sizeof(*device),
4713 M_CAMDEV, M_NOWAIT|M_ZERO);
4717 cam_init_pinfo(&device->devq_entry);
4718 device->target = target;
4719 device->lun_id = lun_id;
4720 device->sim = bus->sim;
4721 if (cam_ccbq_init(&device->ccbq,
4722 bus->sim->max_dev_openings) != 0) {
4723 free(device, M_CAMDEV);
4726 SLIST_INIT(&device->asyncs);
4727 SLIST_INIT(&device->periphs);
4728 device->generation = 0;
4729 device->flags = CAM_DEV_UNCONFIGURED;
4730 device->tag_delay_count = 0;
4731 device->tag_saved_openings = 0;
4732 device->refcount = 1;
4733 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4734 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4735 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4737 * Hold a reference to our parent bus so it
4738 * will not go away before we do.
4742 cur_device = TAILQ_FIRST(&target->ed_entries);
4743 while (cur_device != NULL && cur_device->lun_id < lun_id)
4744 cur_device = TAILQ_NEXT(cur_device, links);
4745 if (cur_device != NULL)
4746 TAILQ_INSERT_BEFORE(cur_device, device, links);
4748 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4749 target->generation++;
4754 xpt_acquire_device(struct cam_ed *device)
4756 struct cam_eb *bus = device->target->bus;
4758 mtx_lock(&bus->eb_mtx);
4760 mtx_unlock(&bus->eb_mtx);
4764 xpt_release_device(struct cam_ed *device)
4766 struct cam_eb *bus = device->target->bus;
4767 struct cam_devq *devq;
4769 mtx_lock(&bus->eb_mtx);
4770 if (--device->refcount > 0) {
4771 mtx_unlock(&bus->eb_mtx);
4775 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4776 device->target->generation++;
4777 mtx_unlock(&bus->eb_mtx);
4779 /* Release our slot in the devq */
4780 devq = bus->sim->devq;
4781 mtx_lock(&devq->send_mtx);
4782 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4783 mtx_unlock(&devq->send_mtx);
4785 KASSERT(SLIST_EMPTY(&device->periphs),
4786 ("destroying device, but periphs list is not empty"));
4787 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4788 ("destroying device while still queued for ccbs"));
4790 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4791 callout_stop(&device->callout);
4793 xpt_release_target(device->target);
4795 cam_ccbq_fini(&device->ccbq);
4797 * Free allocated memory. free(9) does nothing if the
4798 * supplied pointer is NULL, so it is safe to call without
4801 free(device->supported_vpds, M_CAMXPT);
4802 free(device->device_id, M_CAMXPT);
4803 free(device->ext_inq, M_CAMXPT);
4804 free(device->physpath, M_CAMXPT);
4805 free(device->rcap_buf, M_CAMXPT);
4806 free(device->serial_num, M_CAMXPT);
4807 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4811 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4817 mtx_lock(&dev->sim->devq->send_mtx);
4818 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4819 mtx_unlock(&dev->sim->devq->send_mtx);
4820 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4821 || (dev->inq_flags & SID_CmdQue) != 0)
4822 dev->tag_saved_openings = newopenings;
4826 static struct cam_eb *
4827 xpt_find_bus(path_id_t path_id)
4832 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4834 bus = TAILQ_NEXT(bus, links)) {
4835 if (bus->path_id == path_id) {
4844 static struct cam_et *
4845 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4847 struct cam_et *target;
4849 mtx_assert(&bus->eb_mtx, MA_OWNED);
4850 for (target = TAILQ_FIRST(&bus->et_entries);
4852 target = TAILQ_NEXT(target, links)) {
4853 if (target->target_id == target_id) {
4861 static struct cam_ed *
4862 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4864 struct cam_ed *device;
4866 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4867 for (device = TAILQ_FIRST(&target->ed_entries);
4869 device = TAILQ_NEXT(device, links)) {
4870 if (device->lun_id == lun_id) {
4879 xpt_start_tags(struct cam_path *path)
4881 struct ccb_relsim crs;
4882 struct cam_ed *device;
4883 struct cam_sim *sim;
4886 device = path->device;
4887 sim = path->bus->sim;
4888 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4889 xpt_freeze_devq(path, /*count*/1);
4890 device->inq_flags |= SID_CmdQue;
4891 if (device->tag_saved_openings != 0)
4892 newopenings = device->tag_saved_openings;
4894 newopenings = min(device->maxtags,
4895 sim->max_tagged_dev_openings);
4896 xpt_dev_ccbq_resize(path, newopenings);
4897 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4898 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4899 crs.ccb_h.func_code = XPT_REL_SIMQ;
4900 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4902 = crs.release_timeout
4905 xpt_action((union ccb *)&crs);
4909 xpt_stop_tags(struct cam_path *path)
4911 struct ccb_relsim crs;
4912 struct cam_ed *device;
4913 struct cam_sim *sim;
4915 device = path->device;
4916 sim = path->bus->sim;
4917 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4918 device->tag_delay_count = 0;
4919 xpt_freeze_devq(path, /*count*/1);
4920 device->inq_flags &= ~SID_CmdQue;
4921 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4922 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4923 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4924 crs.ccb_h.func_code = XPT_REL_SIMQ;
4925 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4927 = crs.release_timeout
4930 xpt_action((union ccb *)&crs);
4934 xpt_boot_delay(void *arg)
4941 xpt_config(void *arg)
4944 * Now that interrupts are enabled, go find our devices
4946 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4947 printf("xpt_config: failed to create taskqueue thread.\n");
4949 /* Setup debugging path */
4950 if (cam_dflags != CAM_DEBUG_NONE) {
4951 if (xpt_create_path(&cam_dpath, NULL,
4952 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4953 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4954 printf("xpt_config: xpt_create_path() failed for debug"
4955 " target %d:%d:%d, debugging disabled\n",
4956 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4957 cam_dflags = CAM_DEBUG_NONE;
4962 periphdriver_init(1);
4964 callout_init(&xsoftc.boot_callout, 1);
4965 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
4966 xpt_boot_delay, NULL, 0);
4967 /* Fire up rescan thread. */
4968 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4969 "cam", "scanner")) {
4970 printf("xpt_config: failed to create rescan thread.\n");
4978 xsoftc.buses_to_config++;
4983 xpt_release_boot(void)
4986 xsoftc.buses_to_config--;
4987 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4988 struct xpt_task *task;
4990 xsoftc.buses_config_done = 1;
4992 /* Call manually because we don't have any busses */
4993 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4995 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4996 taskqueue_enqueue(taskqueue_thread, &task->task);
5003 * If the given device only has one peripheral attached to it, and if that
5004 * peripheral is the passthrough driver, announce it. This insures that the
5005 * user sees some sort of announcement for every peripheral in their system.
5008 xptpassannouncefunc(struct cam_ed *device, void *arg)
5010 struct cam_periph *periph;
5013 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5014 periph = SLIST_NEXT(periph, periph_links), i++);
5016 periph = SLIST_FIRST(&device->periphs);
5018 && (strncmp(periph->periph_name, "pass", 4) == 0))
5019 xpt_announce_periph(periph, NULL);
5025 xpt_finishconfig_task(void *context, int pending)
5028 periphdriver_init(2);
5030 * Check for devices with no "standard" peripheral driver
5031 * attached. For any devices like that, announce the
5032 * passthrough driver so the user will see something.
5035 xpt_for_all_devices(xptpassannouncefunc, NULL);
5037 /* Release our hook so that the boot can continue. */
5038 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5039 free(xsoftc.xpt_config_hook, M_CAMXPT);
5040 xsoftc.xpt_config_hook = NULL;
5042 free(context, M_CAMXPT);
5046 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5047 struct cam_path *path)
5049 struct ccb_setasync csa;
5054 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5055 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5056 if (status != CAM_REQ_CMP)
5058 xpt_path_lock(path);
5062 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5063 csa.ccb_h.func_code = XPT_SASYNC_CB;
5064 csa.event_enable = event;
5065 csa.callback = cbfunc;
5066 csa.callback_arg = cbarg;
5067 xpt_action((union ccb *)&csa);
5068 status = csa.ccb_h.status;
5071 xpt_path_unlock(path);
5072 xpt_free_path(path);
5075 if ((status == CAM_REQ_CMP) &&
5076 (csa.event_enable & AC_FOUND_DEVICE)) {
5078 * Get this peripheral up to date with all
5079 * the currently existing devices.
5081 xpt_for_all_devices(xptsetasyncfunc, &csa);
5083 if ((status == CAM_REQ_CMP) &&
5084 (csa.event_enable & AC_PATH_REGISTERED)) {
5086 * Get this peripheral up to date with all
5087 * the currently existing busses.
5089 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5096 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5098 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5100 switch (work_ccb->ccb_h.func_code) {
5101 /* Common cases first */
5102 case XPT_PATH_INQ: /* Path routing inquiry */
5104 struct ccb_pathinq *cpi;
5106 cpi = &work_ccb->cpi;
5107 cpi->version_num = 1; /* XXX??? */
5108 cpi->hba_inquiry = 0;
5109 cpi->target_sprt = 0;
5111 cpi->hba_eng_cnt = 0;
5112 cpi->max_target = 0;
5114 cpi->initiator_id = 0;
5115 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5116 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5117 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5118 cpi->unit_number = sim->unit_number;
5119 cpi->bus_id = sim->bus_id;
5120 cpi->base_transfer_speed = 0;
5121 cpi->protocol = PROTO_UNSPECIFIED;
5122 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5123 cpi->transport = XPORT_UNSPECIFIED;
5124 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5125 cpi->ccb_h.status = CAM_REQ_CMP;
5130 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5137 * The xpt as a "controller" has no interrupt sources, so polling
5141 xptpoll(struct cam_sim *sim)
5146 xpt_lock_buses(void)
5148 mtx_lock(&xsoftc.xpt_topo_lock);
5152 xpt_unlock_buses(void)
5154 mtx_unlock(&xsoftc.xpt_topo_lock);
5158 xpt_path_mtx(struct cam_path *path)
5161 return (&path->device->device_mtx);
5165 xpt_done_process(struct ccb_hdr *ccb_h)
5167 struct cam_sim *sim;
5168 struct cam_devq *devq;
5169 struct mtx *mtx = NULL;
5171 if (ccb_h->flags & CAM_HIGH_POWER) {
5172 struct highpowerlist *hphead;
5173 struct cam_ed *device;
5175 mtx_lock(&xsoftc.xpt_highpower_lock);
5176 hphead = &xsoftc.highpowerq;
5178 device = STAILQ_FIRST(hphead);
5181 * Increment the count since this command is done.
5183 xsoftc.num_highpower++;
5186 * Any high powered commands queued up?
5188 if (device != NULL) {
5190 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5191 mtx_unlock(&xsoftc.xpt_highpower_lock);
5193 mtx_lock(&device->sim->devq->send_mtx);
5194 xpt_release_devq_device(device,
5195 /*count*/1, /*runqueue*/TRUE);
5196 mtx_unlock(&device->sim->devq->send_mtx);
5198 mtx_unlock(&xsoftc.xpt_highpower_lock);
5201 sim = ccb_h->path->bus->sim;
5203 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5204 xpt_release_simq(sim, /*run_queue*/FALSE);
5205 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5208 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5209 && (ccb_h->status & CAM_DEV_QFRZN)) {
5210 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5211 ccb_h->status &= ~CAM_DEV_QFRZN;
5215 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5216 struct cam_ed *dev = ccb_h->path->device;
5218 mtx_lock(&devq->send_mtx);
5219 devq->send_active--;
5220 devq->send_openings++;
5221 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5223 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5224 && (dev->ccbq.dev_active == 0))) {
5225 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5226 xpt_release_devq_device(dev, /*count*/1,
5227 /*run_queue*/FALSE);
5230 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5231 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5232 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5233 xpt_release_devq_device(dev, /*count*/1,
5234 /*run_queue*/FALSE);
5237 if (!device_is_queued(dev))
5238 (void)xpt_schedule_devq(devq, dev);
5240 mtx_unlock(&devq->send_mtx);
5242 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5243 mtx = xpt_path_mtx(ccb_h->path);
5246 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5247 && (--dev->tag_delay_count == 0))
5248 xpt_start_tags(ccb_h->path);
5252 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5254 mtx = xpt_path_mtx(ccb_h->path);
5264 /* Call the peripheral driver's callback */
5265 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5266 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5272 xpt_done_td(void *arg)
5274 struct cam_doneq *queue = arg;
5275 struct ccb_hdr *ccb_h;
5276 STAILQ_HEAD(, ccb_hdr) doneq;
5278 STAILQ_INIT(&doneq);
5279 mtx_lock(&queue->cam_doneq_mtx);
5281 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5282 queue->cam_doneq_sleep = 1;
5283 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5285 queue->cam_doneq_sleep = 0;
5287 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5288 mtx_unlock(&queue->cam_doneq_mtx);
5290 THREAD_NO_SLEEPING();
5291 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5292 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5293 xpt_done_process(ccb_h);
5295 THREAD_SLEEPING_OK();
5297 mtx_lock(&queue->cam_doneq_mtx);
5302 camisr_runqueue(void)
5304 struct ccb_hdr *ccb_h;
5305 struct cam_doneq *queue;
5308 /* Process global queues. */
5309 for (i = 0; i < cam_num_doneqs; i++) {
5310 queue = &cam_doneqs[i];
5311 mtx_lock(&queue->cam_doneq_mtx);
5312 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5313 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5314 mtx_unlock(&queue->cam_doneq_mtx);
5315 xpt_done_process(ccb_h);
5316 mtx_lock(&queue->cam_doneq_mtx);
5318 mtx_unlock(&queue->cam_doneq_mtx);