2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
50 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_compat.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/scsi/scsi_pass.h>
69 #include <machine/md_var.h> /* geometry translation */
70 #include <machine/stdarg.h> /* for xpt_print below */
75 * This is the maximum number of high powered commands (e.g. start unit)
76 * that can be outstanding at a particular time.
78 #ifndef CAM_MAX_HIGHPOWER
79 #define CAM_MAX_HIGHPOWER 4
82 /* Datastructures internal to the xpt layer */
83 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
88 /* Object for defering XPT actions to a taskqueue */
96 uint32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 struct mtx xpt_highpower_lock;
100 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 int buses_config_done;
108 /* Registered busses */
109 TAILQ_HEAD(,cam_eb) xpt_busses;
110 u_int bus_generation;
112 struct intr_config_hook *xpt_config_hook;
115 struct callout boot_callout;
117 struct mtx xpt_topo_lock;
119 struct taskqueue *xpt_taskq;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
156 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
157 &xsoftc.boot_delay, 0, "Bus registration wait time");
158 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
159 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
162 struct mtx_padalign cam_doneq_mtx;
163 STAILQ_HEAD(, ccb_hdr) cam_doneq;
167 static struct cam_doneq cam_doneqs[MAXCPU];
168 static int cam_num_doneqs;
169 static struct proc *cam_proc;
171 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
172 &cam_num_doneqs, 0, "Number of completion queues/threads");
174 struct cam_periph *xpt_periph;
176 static periph_init_t xpt_periph_init;
178 static struct periph_driver xpt_driver =
180 xpt_periph_init, "xpt",
181 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
185 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
187 static d_open_t xptopen;
188 static d_close_t xptclose;
189 static d_ioctl_t xptioctl;
190 static d_ioctl_t xptdoioctl;
192 static struct cdevsw xpt_cdevsw = {
193 .d_version = D_VERSION,
201 /* Storage for debugging datastructures */
202 struct cam_path *cam_dpath;
203 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
204 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
205 &cam_dflags, 0, "Enabled debug flags");
206 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
207 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
208 &cam_debug_delay, 0, "Delay in us after each debug message");
210 /* Our boot-time initialization hook */
211 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
213 static moduledata_t cam_moduledata = {
215 cam_module_event_handler,
219 static int xpt_init(void *);
221 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
222 MODULE_VERSION(cam, 1);
225 static void xpt_async_bcast(struct async_list *async_head,
226 u_int32_t async_code,
227 struct cam_path *path,
229 static path_id_t xptnextfreepathid(void);
230 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
231 static union ccb *xpt_get_ccb(struct cam_periph *periph);
232 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
233 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
234 static void xpt_run_allocq_task(void *context, int pending);
235 static void xpt_run_devq(struct cam_devq *devq);
236 static timeout_t xpt_release_devq_timeout;
237 static void xpt_release_simq_timeout(void *arg) __unused;
238 static void xpt_acquire_bus(struct cam_eb *bus);
239 static void xpt_release_bus(struct cam_eb *bus);
240 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
241 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
243 static struct cam_et*
244 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
245 static void xpt_acquire_target(struct cam_et *target);
246 static void xpt_release_target(struct cam_et *target);
247 static struct cam_eb*
248 xpt_find_bus(path_id_t path_id);
249 static struct cam_et*
250 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
251 static struct cam_ed*
252 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
253 static void xpt_config(void *arg);
254 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
255 u_int32_t new_priority);
256 static xpt_devicefunc_t xptpassannouncefunc;
257 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
258 static void xptpoll(struct cam_sim *sim);
259 static void camisr_runqueue(void);
260 static void xpt_done_process(struct ccb_hdr *ccb_h);
261 static void xpt_done_td(void *);
262 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
263 u_int num_patterns, struct cam_eb *bus);
264 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
266 struct cam_ed *device);
267 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
269 struct cam_periph *periph);
270 static xpt_busfunc_t xptedtbusfunc;
271 static xpt_targetfunc_t xptedttargetfunc;
272 static xpt_devicefunc_t xptedtdevicefunc;
273 static xpt_periphfunc_t xptedtperiphfunc;
274 static xpt_pdrvfunc_t xptplistpdrvfunc;
275 static xpt_periphfunc_t xptplistperiphfunc;
276 static int xptedtmatch(struct ccb_dev_match *cdm);
277 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
278 static int xptbustraverse(struct cam_eb *start_bus,
279 xpt_busfunc_t *tr_func, void *arg);
280 static int xpttargettraverse(struct cam_eb *bus,
281 struct cam_et *start_target,
282 xpt_targetfunc_t *tr_func, void *arg);
283 static int xptdevicetraverse(struct cam_et *target,
284 struct cam_ed *start_device,
285 xpt_devicefunc_t *tr_func, void *arg);
286 static int xptperiphtraverse(struct cam_ed *device,
287 struct cam_periph *start_periph,
288 xpt_periphfunc_t *tr_func, void *arg);
289 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
290 xpt_pdrvfunc_t *tr_func, void *arg);
291 static int xptpdperiphtraverse(struct periph_driver **pdrv,
292 struct cam_periph *start_periph,
293 xpt_periphfunc_t *tr_func,
295 static xpt_busfunc_t xptdefbusfunc;
296 static xpt_targetfunc_t xptdeftargetfunc;
297 static xpt_devicefunc_t xptdefdevicefunc;
298 static xpt_periphfunc_t xptdefperiphfunc;
299 static void xpt_finishconfig_task(void *context, int pending);
300 static void xpt_dev_async_default(u_int32_t async_code,
302 struct cam_et *target,
303 struct cam_ed *device,
305 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
306 struct cam_et *target,
308 static xpt_devicefunc_t xptsetasyncfunc;
309 static xpt_busfunc_t xptsetasyncbusfunc;
310 static cam_status xptregister(struct cam_periph *periph,
312 static const char * xpt_action_name(uint32_t action);
313 static __inline int device_is_queued(struct cam_ed *device);
316 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
320 mtx_assert(&devq->send_mtx, MA_OWNED);
321 if ((dev->ccbq.queue.entries > 0) &&
322 (dev->ccbq.dev_openings > 0) &&
323 (dev->ccbq.queue.qfrozen_cnt == 0)) {
325 * The priority of a device waiting for controller
326 * resources is that of the highest priority CCB
330 xpt_schedule_dev(&devq->send_queue,
332 CAMQ_GET_PRIO(&dev->ccbq.queue));
340 device_is_queued(struct cam_ed *device)
342 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
348 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
352 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
356 * Only allow read-write access.
358 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
362 * We don't allow nonblocking access.
364 if ((flags & O_NONBLOCK) != 0) {
365 printf("%s: can't do nonblocking access\n", devtoname(dev));
373 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
380 * Don't automatically grab the xpt softc lock here even though this is going
381 * through the xpt device. The xpt device is really just a back door for
382 * accessing other devices and SIMs, so the right thing to do is to grab
383 * the appropriate SIM lock once the bus/SIM is located.
386 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
390 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
391 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
397 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
405 * For the transport layer CAMIOCOMMAND ioctl, we really only want
406 * to accept CCB types that don't quite make sense to send through a
407 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
415 inccb = (union ccb *)addr;
417 bus = xpt_find_bus(inccb->ccb_h.path_id);
421 switch (inccb->ccb_h.func_code) {
424 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
425 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
426 xpt_release_bus(bus);
431 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
432 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
433 xpt_release_bus(bus);
441 switch(inccb->ccb_h.func_code) {
449 ccb = xpt_alloc_ccb();
452 * Create a path using the bus, target, and lun the
455 if (xpt_create_path(&ccb->ccb_h.path, NULL,
456 inccb->ccb_h.path_id,
457 inccb->ccb_h.target_id,
458 inccb->ccb_h.target_lun) !=
464 /* Ensure all of our fields are correct */
465 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
466 inccb->ccb_h.pinfo.priority);
467 xpt_merge_ccb(ccb, inccb);
468 xpt_path_lock(ccb->ccb_h.path);
469 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
470 xpt_path_unlock(ccb->ccb_h.path);
471 bcopy(ccb, inccb, sizeof(union ccb));
472 xpt_free_path(ccb->ccb_h.path);
480 * This is an immediate CCB, so it's okay to
481 * allocate it on the stack.
485 * Create a path using the bus, target, and lun the
488 if (xpt_create_path(&ccb.ccb_h.path, NULL,
489 inccb->ccb_h.path_id,
490 inccb->ccb_h.target_id,
491 inccb->ccb_h.target_lun) !=
496 /* Ensure all of our fields are correct */
497 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
498 inccb->ccb_h.pinfo.priority);
499 xpt_merge_ccb(&ccb, inccb);
501 bcopy(&ccb, inccb, sizeof(union ccb));
502 xpt_free_path(ccb.ccb_h.path);
506 case XPT_DEV_MATCH: {
507 struct cam_periph_map_info mapinfo;
508 struct cam_path *old_path;
511 * We can't deal with physical addresses for this
512 * type of transaction.
514 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
521 * Save this in case the caller had it set to
522 * something in particular.
524 old_path = inccb->ccb_h.path;
527 * We really don't need a path for the matching
528 * code. The path is needed because of the
529 * debugging statements in xpt_action(). They
530 * assume that the CCB has a valid path.
532 inccb->ccb_h.path = xpt_periph->path;
534 bzero(&mapinfo, sizeof(mapinfo));
537 * Map the pattern and match buffers into kernel
538 * virtual address space.
540 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
543 inccb->ccb_h.path = old_path;
548 * This is an immediate CCB, we can send it on directly.
553 * Map the buffers back into user space.
555 cam_periph_unmapmem(inccb, &mapinfo);
557 inccb->ccb_h.path = old_path;
566 xpt_release_bus(bus);
570 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
571 * with the periphal driver name and unit name filled in. The other
572 * fields don't really matter as input. The passthrough driver name
573 * ("pass"), and unit number are passed back in the ccb. The current
574 * device generation number, and the index into the device peripheral
575 * driver list, and the status are also passed back. Note that
576 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
577 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
578 * (or rather should be) impossible for the device peripheral driver
579 * list to change since we look at the whole thing in one pass, and
580 * we do it with lock protection.
583 case CAMGETPASSTHRU: {
585 struct cam_periph *periph;
586 struct periph_driver **p_drv;
589 int base_periph_found;
591 ccb = (union ccb *)addr;
592 unit = ccb->cgdl.unit_number;
593 name = ccb->cgdl.periph_name;
594 base_periph_found = 0;
597 * Sanity check -- make sure we don't get a null peripheral
600 if (*ccb->cgdl.periph_name == '\0') {
605 /* Keep the list from changing while we traverse it */
608 /* first find our driver in the list of drivers */
609 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
610 if (strcmp((*p_drv)->driver_name, name) == 0)
613 if (*p_drv == NULL) {
615 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
616 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
617 *ccb->cgdl.periph_name = '\0';
618 ccb->cgdl.unit_number = 0;
624 * Run through every peripheral instance of this driver
625 * and check to see whether it matches the unit passed
626 * in by the user. If it does, get out of the loops and
627 * find the passthrough driver associated with that
630 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
631 periph = TAILQ_NEXT(periph, unit_links)) {
633 if (periph->unit_number == unit)
637 * If we found the peripheral driver that the user passed
638 * in, go through all of the peripheral drivers for that
639 * particular device and look for a passthrough driver.
641 if (periph != NULL) {
642 struct cam_ed *device;
645 base_periph_found = 1;
646 device = periph->path->device;
647 for (i = 0, periph = SLIST_FIRST(&device->periphs);
649 periph = SLIST_NEXT(periph, periph_links), i++) {
651 * Check to see whether we have a
652 * passthrough device or not.
654 if (strcmp(periph->periph_name, "pass") == 0) {
656 * Fill in the getdevlist fields.
658 strcpy(ccb->cgdl.periph_name,
659 periph->periph_name);
660 ccb->cgdl.unit_number =
662 if (SLIST_NEXT(periph, periph_links))
664 CAM_GDEVLIST_MORE_DEVS;
667 CAM_GDEVLIST_LAST_DEVICE;
668 ccb->cgdl.generation =
672 * Fill in some CCB header fields
673 * that the user may want.
676 periph->path->bus->path_id;
677 ccb->ccb_h.target_id =
678 periph->path->target->target_id;
679 ccb->ccb_h.target_lun =
680 periph->path->device->lun_id;
681 ccb->ccb_h.status = CAM_REQ_CMP;
688 * If the periph is null here, one of two things has
689 * happened. The first possibility is that we couldn't
690 * find the unit number of the particular peripheral driver
691 * that the user is asking about. e.g. the user asks for
692 * the passthrough driver for "da11". We find the list of
693 * "da" peripherals all right, but there is no unit 11.
694 * The other possibility is that we went through the list
695 * of peripheral drivers attached to the device structure,
696 * but didn't find one with the name "pass". Either way,
697 * we return ENOENT, since we couldn't find something.
699 if (periph == NULL) {
700 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
701 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
702 *ccb->cgdl.periph_name = '\0';
703 ccb->cgdl.unit_number = 0;
706 * It is unfortunate that this is even necessary,
707 * but there are many, many clueless users out there.
708 * If this is true, the user is looking for the
709 * passthrough driver, but doesn't have one in his
712 if (base_periph_found == 1) {
713 printf("xptioctl: pass driver is not in the "
715 printf("xptioctl: put \"device pass\" in "
716 "your kernel config file\n");
731 cam_module_event_handler(module_t mod, int what, void *arg)
737 if ((error = xpt_init(NULL)) != 0)
750 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
753 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
754 xpt_free_path(done_ccb->ccb_h.path);
755 xpt_free_ccb(done_ccb);
757 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
758 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
763 /* thread to handle bus rescans */
765 xpt_scanner_thread(void *dummy)
768 struct cam_path path;
772 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
773 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
775 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
776 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
780 * Since lock can be dropped inside and path freed
781 * by completion callback even before return here,
782 * take our own path copy for reference.
784 xpt_copy_path(&path, ccb->ccb_h.path);
785 xpt_path_lock(&path);
787 xpt_path_unlock(&path);
788 xpt_release_path(&path);
796 xpt_rescan(union ccb *ccb)
800 /* Prepare request */
801 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
802 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
803 ccb->ccb_h.func_code = XPT_SCAN_BUS;
804 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
805 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
806 ccb->ccb_h.func_code = XPT_SCAN_TGT;
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_LUN;
811 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
812 xpt_free_path(ccb->ccb_h.path);
816 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
817 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
818 xpt_action_name(ccb->ccb_h.func_code)));
820 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
821 ccb->ccb_h.cbfcnp = xpt_rescan_done;
822 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
823 /* Don't make duplicate entries for the same paths. */
825 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
826 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
827 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
828 wakeup(&xsoftc.ccb_scanq);
830 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
831 xpt_free_path(ccb->ccb_h.path);
837 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
838 xsoftc.buses_to_config++;
839 wakeup(&xsoftc.ccb_scanq);
843 /* Functions accessed by the peripheral drivers */
845 xpt_init(void *dummy)
847 struct cam_sim *xpt_sim;
848 struct cam_path *path;
849 struct cam_devq *devq;
853 TAILQ_INIT(&xsoftc.xpt_busses);
854 TAILQ_INIT(&xsoftc.ccb_scanq);
855 STAILQ_INIT(&xsoftc.highpowerq);
856 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
858 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
859 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
860 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
861 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
863 #ifdef CAM_BOOT_DELAY
865 * Override this value at compile time to assist our users
866 * who don't use loader to boot a kernel.
868 xsoftc.boot_delay = CAM_BOOT_DELAY;
871 * The xpt layer is, itself, the equivalent of a SIM.
872 * Allow 16 ccbs in the ccb pool for it. This should
873 * give decent parallelism when we probe busses and
874 * perform other XPT functions.
876 devq = cam_simq_alloc(16);
877 xpt_sim = cam_sim_alloc(xptaction,
882 /*mtx*/&xsoftc.xpt_lock,
883 /*max_dev_transactions*/0,
884 /*max_tagged_dev_transactions*/0,
889 mtx_lock(&xsoftc.xpt_lock);
890 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
891 mtx_unlock(&xsoftc.xpt_lock);
892 printf("xpt_init: xpt_bus_register failed with status %#x,"
893 " failing attach\n", status);
896 mtx_unlock(&xsoftc.xpt_lock);
899 * Looking at the XPT from the SIM layer, the XPT is
900 * the equivalent of a peripheral driver. Allocate
901 * a peripheral driver entry for us.
903 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
905 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
906 printf("xpt_init: xpt_create_path failed with status %#x,"
907 " failing attach\n", status);
911 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
912 path, NULL, 0, xpt_sim);
913 xpt_path_unlock(path);
916 if (cam_num_doneqs < 1)
917 cam_num_doneqs = 1 + mp_ncpus / 6;
918 else if (cam_num_doneqs > MAXCPU)
919 cam_num_doneqs = MAXCPU;
920 for (i = 0; i < cam_num_doneqs; i++) {
921 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
923 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
924 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
925 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
931 if (cam_num_doneqs < 1) {
932 printf("xpt_init: Cannot init completion queues "
933 "- failing attach\n");
937 * Register a callback for when interrupts are enabled.
939 xsoftc.xpt_config_hook =
940 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
941 M_CAMXPT, M_NOWAIT | M_ZERO);
942 if (xsoftc.xpt_config_hook == NULL) {
943 printf("xpt_init: Cannot malloc config hook "
944 "- failing attach\n");
947 xsoftc.xpt_config_hook->ich_func = xpt_config;
948 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
949 free (xsoftc.xpt_config_hook, M_CAMXPT);
950 printf("xpt_init: config_intrhook_establish failed "
951 "- failing attach\n");
958 xptregister(struct cam_periph *periph, void *arg)
960 struct cam_sim *xpt_sim;
962 if (periph == NULL) {
963 printf("xptregister: periph was NULL!!\n");
964 return(CAM_REQ_CMP_ERR);
967 xpt_sim = (struct cam_sim *)arg;
968 xpt_sim->softc = periph;
970 periph->softc = NULL;
976 xpt_add_periph(struct cam_periph *periph)
978 struct cam_ed *device;
981 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
982 device = periph->path->device;
983 status = CAM_REQ_CMP;
984 if (device != NULL) {
985 mtx_lock(&device->target->bus->eb_mtx);
986 device->generation++;
987 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
988 mtx_unlock(&device->target->bus->eb_mtx);
989 atomic_add_32(&xsoftc.xpt_generation, 1);
996 xpt_remove_periph(struct cam_periph *periph)
998 struct cam_ed *device;
1000 device = periph->path->device;
1001 if (device != NULL) {
1002 mtx_lock(&device->target->bus->eb_mtx);
1003 device->generation++;
1004 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1005 mtx_unlock(&device->target->bus->eb_mtx);
1006 atomic_add_32(&xsoftc.xpt_generation, 1);
1012 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1014 struct cam_path *path = periph->path;
1016 cam_periph_assert(periph, MA_OWNED);
1017 periph->flags |= CAM_PERIPH_ANNOUNCED;
1019 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1020 periph->periph_name, periph->unit_number,
1021 path->bus->sim->sim_name,
1022 path->bus->sim->unit_number,
1023 path->bus->sim->bus_id,
1025 path->target->target_id,
1026 (uintmax_t)path->device->lun_id);
1027 printf("%s%d: ", periph->periph_name, periph->unit_number);
1028 if (path->device->protocol == PROTO_SCSI)
1029 scsi_print_inquiry(&path->device->inq_data);
1030 else if (path->device->protocol == PROTO_ATA ||
1031 path->device->protocol == PROTO_SATAPM)
1032 ata_print_ident(&path->device->ident_data);
1033 else if (path->device->protocol == PROTO_SEMB)
1035 (struct sep_identify_data *)&path->device->ident_data);
1037 printf("Unknown protocol device\n");
1038 if (path->device->serial_num_len > 0) {
1039 /* Don't wrap the screen - print only the first 60 chars */
1040 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1041 periph->unit_number, path->device->serial_num);
1043 /* Announce transport details. */
1044 (*(path->bus->xport->announce))(periph);
1045 /* Announce command queueing. */
1046 if (path->device->inq_flags & SID_CmdQue
1047 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1048 printf("%s%d: Command Queueing enabled\n",
1049 periph->periph_name, periph->unit_number);
1051 /* Announce caller's details if they've passed in. */
1052 if (announce_string != NULL)
1053 printf("%s%d: %s\n", periph->periph_name,
1054 periph->unit_number, announce_string);
1058 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1061 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1062 periph->unit_number, quirks, bit_string);
1067 xpt_denounce_periph(struct cam_periph *periph)
1069 struct cam_path *path = periph->path;
1071 cam_periph_assert(periph, MA_OWNED);
1072 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1073 periph->periph_name, periph->unit_number,
1074 path->bus->sim->sim_name,
1075 path->bus->sim->unit_number,
1076 path->bus->sim->bus_id,
1078 path->target->target_id,
1079 (uintmax_t)path->device->lun_id);
1080 printf("%s%d: ", periph->periph_name, periph->unit_number);
1081 if (path->device->protocol == PROTO_SCSI)
1082 scsi_print_inquiry_short(&path->device->inq_data);
1083 else if (path->device->protocol == PROTO_ATA ||
1084 path->device->protocol == PROTO_SATAPM)
1085 ata_print_ident_short(&path->device->ident_data);
1086 else if (path->device->protocol == PROTO_SEMB)
1087 semb_print_ident_short(
1088 (struct sep_identify_data *)&path->device->ident_data);
1090 printf("Unknown protocol device");
1091 if (path->device->serial_num_len > 0)
1092 printf(" s/n %.60s", path->device->serial_num);
1093 printf(" detached\n");
1098 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1101 struct ccb_dev_advinfo cdai;
1102 struct scsi_vpd_id_descriptor *idd;
1104 xpt_path_assert(path, MA_OWNED);
1106 memset(&cdai, 0, sizeof(cdai));
1107 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1108 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1111 if (!strcmp(attr, "GEOM::ident"))
1112 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1113 else if (!strcmp(attr, "GEOM::physpath"))
1114 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1115 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1116 strcmp(attr, "GEOM::lunname") == 0) {
1117 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1118 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1122 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1123 if (cdai.buf == NULL) {
1127 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1128 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1129 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1130 if (cdai.provsiz == 0)
1132 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1133 if (strcmp(attr, "GEOM::lunid") == 0) {
1134 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1135 cdai.provsiz, scsi_devid_is_lun_naa);
1137 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1138 cdai.provsiz, scsi_devid_is_lun_eui64);
1142 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1143 cdai.provsiz, scsi_devid_is_lun_t10);
1145 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1146 cdai.provsiz, scsi_devid_is_lun_name);
1150 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1151 if (idd->length < len) {
1152 for (l = 0; l < idd->length; l++)
1153 buf[l] = idd->identifier[l] ?
1154 idd->identifier[l] : ' ';
1158 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1159 l = strnlen(idd->identifier, idd->length);
1161 bcopy(idd->identifier, buf, l);
1166 if (idd->length * 2 < len) {
1167 for (l = 0; l < idd->length; l++)
1168 sprintf(buf + l * 2, "%02x",
1169 idd->identifier[l]);
1175 if (strlcpy(buf, cdai.buf, len) >= len)
1180 if (cdai.buf != NULL)
1181 free(cdai.buf, M_CAMXPT);
1185 static dev_match_ret
1186 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1189 dev_match_ret retval;
1192 retval = DM_RET_NONE;
1195 * If we aren't given something to match against, that's an error.
1198 return(DM_RET_ERROR);
1201 * If there are no match entries, then this bus matches no
1204 if ((patterns == NULL) || (num_patterns == 0))
1205 return(DM_RET_DESCEND | DM_RET_COPY);
1207 for (i = 0; i < num_patterns; i++) {
1208 struct bus_match_pattern *cur_pattern;
1211 * If the pattern in question isn't for a bus node, we
1212 * aren't interested. However, we do indicate to the
1213 * calling routine that we should continue descending the
1214 * tree, since the user wants to match against lower-level
1217 if (patterns[i].type != DEV_MATCH_BUS) {
1218 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1219 retval |= DM_RET_DESCEND;
1223 cur_pattern = &patterns[i].pattern.bus_pattern;
1226 * If they want to match any bus node, we give them any
1229 if (cur_pattern->flags == BUS_MATCH_ANY) {
1230 /* set the copy flag */
1231 retval |= DM_RET_COPY;
1234 * If we've already decided on an action, go ahead
1237 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1242 * Not sure why someone would do this...
1244 if (cur_pattern->flags == BUS_MATCH_NONE)
1247 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1248 && (cur_pattern->path_id != bus->path_id))
1251 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1252 && (cur_pattern->bus_id != bus->sim->bus_id))
1255 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1256 && (cur_pattern->unit_number != bus->sim->unit_number))
1259 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1260 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1265 * If we get to this point, the user definitely wants
1266 * information on this bus. So tell the caller to copy the
1269 retval |= DM_RET_COPY;
1272 * If the return action has been set to descend, then we
1273 * know that we've already seen a non-bus matching
1274 * expression, therefore we need to further descend the tree.
1275 * This won't change by continuing around the loop, so we
1276 * go ahead and return. If we haven't seen a non-bus
1277 * matching expression, we keep going around the loop until
1278 * we exhaust the matching expressions. We'll set the stop
1279 * flag once we fall out of the loop.
1281 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1286 * If the return action hasn't been set to descend yet, that means
1287 * we haven't seen anything other than bus matching patterns. So
1288 * tell the caller to stop descending the tree -- the user doesn't
1289 * want to match against lower level tree elements.
1291 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1292 retval |= DM_RET_STOP;
1297 static dev_match_ret
1298 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1299 struct cam_ed *device)
1301 dev_match_ret retval;
1304 retval = DM_RET_NONE;
1307 * If we aren't given something to match against, that's an error.
1310 return(DM_RET_ERROR);
1313 * If there are no match entries, then this device matches no
1316 if ((patterns == NULL) || (num_patterns == 0))
1317 return(DM_RET_DESCEND | DM_RET_COPY);
1319 for (i = 0; i < num_patterns; i++) {
1320 struct device_match_pattern *cur_pattern;
1321 struct scsi_vpd_device_id *device_id_page;
1324 * If the pattern in question isn't for a device node, we
1325 * aren't interested.
1327 if (patterns[i].type != DEV_MATCH_DEVICE) {
1328 if ((patterns[i].type == DEV_MATCH_PERIPH)
1329 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1330 retval |= DM_RET_DESCEND;
1334 cur_pattern = &patterns[i].pattern.device_pattern;
1336 /* Error out if mutually exclusive options are specified. */
1337 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1338 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1339 return(DM_RET_ERROR);
1342 * If they want to match any device node, we give them any
1345 if (cur_pattern->flags == DEV_MATCH_ANY)
1349 * Not sure why someone would do this...
1351 if (cur_pattern->flags == DEV_MATCH_NONE)
1354 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1355 && (cur_pattern->path_id != device->target->bus->path_id))
1358 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1359 && (cur_pattern->target_id != device->target->target_id))
1362 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1363 && (cur_pattern->target_lun != device->lun_id))
1366 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1367 && (cam_quirkmatch((caddr_t)&device->inq_data,
1368 (caddr_t)&cur_pattern->data.inq_pat,
1369 1, sizeof(cur_pattern->data.inq_pat),
1370 scsi_static_inquiry_match) == NULL))
1373 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1374 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1375 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1376 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1377 device->device_id_len
1378 - SVPD_DEVICE_ID_HDR_LEN,
1379 cur_pattern->data.devid_pat.id,
1380 cur_pattern->data.devid_pat.id_len) != 0))
1385 * If we get to this point, the user definitely wants
1386 * information on this device. So tell the caller to copy
1389 retval |= DM_RET_COPY;
1392 * If the return action has been set to descend, then we
1393 * know that we've already seen a peripheral matching
1394 * expression, therefore we need to further descend the tree.
1395 * This won't change by continuing around the loop, so we
1396 * go ahead and return. If we haven't seen a peripheral
1397 * matching expression, we keep going around the loop until
1398 * we exhaust the matching expressions. We'll set the stop
1399 * flag once we fall out of the loop.
1401 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1406 * If the return action hasn't been set to descend yet, that means
1407 * we haven't seen any peripheral matching patterns. So tell the
1408 * caller to stop descending the tree -- the user doesn't want to
1409 * match against lower level tree elements.
1411 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1412 retval |= DM_RET_STOP;
1418 * Match a single peripheral against any number of match patterns.
1420 static dev_match_ret
1421 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1422 struct cam_periph *periph)
1424 dev_match_ret retval;
1428 * If we aren't given something to match against, that's an error.
1431 return(DM_RET_ERROR);
1434 * If there are no match entries, then this peripheral matches no
1437 if ((patterns == NULL) || (num_patterns == 0))
1438 return(DM_RET_STOP | DM_RET_COPY);
1441 * There aren't any nodes below a peripheral node, so there's no
1442 * reason to descend the tree any further.
1444 retval = DM_RET_STOP;
1446 for (i = 0; i < num_patterns; i++) {
1447 struct periph_match_pattern *cur_pattern;
1450 * If the pattern in question isn't for a peripheral, we
1451 * aren't interested.
1453 if (patterns[i].type != DEV_MATCH_PERIPH)
1456 cur_pattern = &patterns[i].pattern.periph_pattern;
1459 * If they want to match on anything, then we will do so.
1461 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1462 /* set the copy flag */
1463 retval |= DM_RET_COPY;
1466 * We've already set the return action to stop,
1467 * since there are no nodes below peripherals in
1474 * Not sure why someone would do this...
1476 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1479 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1480 && (cur_pattern->path_id != periph->path->bus->path_id))
1484 * For the target and lun id's, we have to make sure the
1485 * target and lun pointers aren't NULL. The xpt peripheral
1486 * has a wildcard target and device.
1488 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1489 && ((periph->path->target == NULL)
1490 ||(cur_pattern->target_id != periph->path->target->target_id)))
1493 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1494 && ((periph->path->device == NULL)
1495 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1498 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1499 && (cur_pattern->unit_number != periph->unit_number))
1502 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1503 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1508 * If we get to this point, the user definitely wants
1509 * information on this peripheral. So tell the caller to
1510 * copy the data out.
1512 retval |= DM_RET_COPY;
1515 * The return action has already been set to stop, since
1516 * peripherals don't have any nodes below them in the EDT.
1522 * If we get to this point, the peripheral that was passed in
1523 * doesn't match any of the patterns.
1529 xptedtbusfunc(struct cam_eb *bus, void *arg)
1531 struct ccb_dev_match *cdm;
1532 struct cam_et *target;
1533 dev_match_ret retval;
1535 cdm = (struct ccb_dev_match *)arg;
1538 * If our position is for something deeper in the tree, that means
1539 * that we've already seen this node. So, we keep going down.
1541 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1542 && (cdm->pos.cookie.bus == bus)
1543 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1544 && (cdm->pos.cookie.target != NULL))
1545 retval = DM_RET_DESCEND;
1547 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1550 * If we got an error, bail out of the search.
1552 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1553 cdm->status = CAM_DEV_MATCH_ERROR;
1558 * If the copy flag is set, copy this bus out.
1560 if (retval & DM_RET_COPY) {
1563 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1564 sizeof(struct dev_match_result));
1567 * If we don't have enough space to put in another
1568 * match result, save our position and tell the
1569 * user there are more devices to check.
1571 if (spaceleft < sizeof(struct dev_match_result)) {
1572 bzero(&cdm->pos, sizeof(cdm->pos));
1573 cdm->pos.position_type =
1574 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1576 cdm->pos.cookie.bus = bus;
1577 cdm->pos.generations[CAM_BUS_GENERATION]=
1578 xsoftc.bus_generation;
1579 cdm->status = CAM_DEV_MATCH_MORE;
1582 j = cdm->num_matches;
1584 cdm->matches[j].type = DEV_MATCH_BUS;
1585 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1586 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1587 cdm->matches[j].result.bus_result.unit_number =
1588 bus->sim->unit_number;
1589 strncpy(cdm->matches[j].result.bus_result.dev_name,
1590 bus->sim->sim_name, DEV_IDLEN);
1594 * If the user is only interested in busses, there's no
1595 * reason to descend to the next level in the tree.
1597 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1601 * If there is a target generation recorded, check it to
1602 * make sure the target list hasn't changed.
1604 mtx_lock(&bus->eb_mtx);
1605 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1606 && (cdm->pos.cookie.bus == bus)
1607 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1608 && (cdm->pos.cookie.target != NULL)) {
1609 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1611 mtx_unlock(&bus->eb_mtx);
1612 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1615 target = (struct cam_et *)cdm->pos.cookie.target;
1619 mtx_unlock(&bus->eb_mtx);
1621 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1625 xptedttargetfunc(struct cam_et *target, void *arg)
1627 struct ccb_dev_match *cdm;
1629 struct cam_ed *device;
1631 cdm = (struct ccb_dev_match *)arg;
1635 * If there is a device list generation recorded, check it to
1636 * make sure the device list hasn't changed.
1638 mtx_lock(&bus->eb_mtx);
1639 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1640 && (cdm->pos.cookie.bus == bus)
1641 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1642 && (cdm->pos.cookie.target == target)
1643 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1644 && (cdm->pos.cookie.device != NULL)) {
1645 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1646 target->generation) {
1647 mtx_unlock(&bus->eb_mtx);
1648 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1651 device = (struct cam_ed *)cdm->pos.cookie.device;
1655 mtx_unlock(&bus->eb_mtx);
1657 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1661 xptedtdevicefunc(struct cam_ed *device, void *arg)
1664 struct cam_periph *periph;
1665 struct ccb_dev_match *cdm;
1666 dev_match_ret retval;
1668 cdm = (struct ccb_dev_match *)arg;
1669 bus = device->target->bus;
1672 * If our position is for something deeper in the tree, that means
1673 * that we've already seen this node. So, we keep going down.
1675 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1676 && (cdm->pos.cookie.device == device)
1677 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1678 && (cdm->pos.cookie.periph != NULL))
1679 retval = DM_RET_DESCEND;
1681 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1684 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1685 cdm->status = CAM_DEV_MATCH_ERROR;
1690 * If the copy flag is set, copy this device out.
1692 if (retval & DM_RET_COPY) {
1695 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1696 sizeof(struct dev_match_result));
1699 * If we don't have enough space to put in another
1700 * match result, save our position and tell the
1701 * user there are more devices to check.
1703 if (spaceleft < sizeof(struct dev_match_result)) {
1704 bzero(&cdm->pos, sizeof(cdm->pos));
1705 cdm->pos.position_type =
1706 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1707 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1709 cdm->pos.cookie.bus = device->target->bus;
1710 cdm->pos.generations[CAM_BUS_GENERATION]=
1711 xsoftc.bus_generation;
1712 cdm->pos.cookie.target = device->target;
1713 cdm->pos.generations[CAM_TARGET_GENERATION] =
1714 device->target->bus->generation;
1715 cdm->pos.cookie.device = device;
1716 cdm->pos.generations[CAM_DEV_GENERATION] =
1717 device->target->generation;
1718 cdm->status = CAM_DEV_MATCH_MORE;
1721 j = cdm->num_matches;
1723 cdm->matches[j].type = DEV_MATCH_DEVICE;
1724 cdm->matches[j].result.device_result.path_id =
1725 device->target->bus->path_id;
1726 cdm->matches[j].result.device_result.target_id =
1727 device->target->target_id;
1728 cdm->matches[j].result.device_result.target_lun =
1730 cdm->matches[j].result.device_result.protocol =
1732 bcopy(&device->inq_data,
1733 &cdm->matches[j].result.device_result.inq_data,
1734 sizeof(struct scsi_inquiry_data));
1735 bcopy(&device->ident_data,
1736 &cdm->matches[j].result.device_result.ident_data,
1737 sizeof(struct ata_params));
1739 /* Let the user know whether this device is unconfigured */
1740 if (device->flags & CAM_DEV_UNCONFIGURED)
1741 cdm->matches[j].result.device_result.flags =
1742 DEV_RESULT_UNCONFIGURED;
1744 cdm->matches[j].result.device_result.flags =
1749 * If the user isn't interested in peripherals, don't descend
1750 * the tree any further.
1752 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1756 * If there is a peripheral list generation recorded, make sure
1757 * it hasn't changed.
1760 mtx_lock(&bus->eb_mtx);
1761 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1762 && (cdm->pos.cookie.bus == bus)
1763 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1764 && (cdm->pos.cookie.target == device->target)
1765 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1766 && (cdm->pos.cookie.device == device)
1767 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1768 && (cdm->pos.cookie.periph != NULL)) {
1769 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1770 device->generation) {
1771 mtx_unlock(&bus->eb_mtx);
1773 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1776 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1780 mtx_unlock(&bus->eb_mtx);
1783 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1787 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1789 struct ccb_dev_match *cdm;
1790 dev_match_ret retval;
1792 cdm = (struct ccb_dev_match *)arg;
1794 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1796 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1797 cdm->status = CAM_DEV_MATCH_ERROR;
1802 * If the copy flag is set, copy this peripheral out.
1804 if (retval & DM_RET_COPY) {
1807 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1808 sizeof(struct dev_match_result));
1811 * If we don't have enough space to put in another
1812 * match result, save our position and tell the
1813 * user there are more devices to check.
1815 if (spaceleft < sizeof(struct dev_match_result)) {
1816 bzero(&cdm->pos, sizeof(cdm->pos));
1817 cdm->pos.position_type =
1818 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1819 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1822 cdm->pos.cookie.bus = periph->path->bus;
1823 cdm->pos.generations[CAM_BUS_GENERATION]=
1824 xsoftc.bus_generation;
1825 cdm->pos.cookie.target = periph->path->target;
1826 cdm->pos.generations[CAM_TARGET_GENERATION] =
1827 periph->path->bus->generation;
1828 cdm->pos.cookie.device = periph->path->device;
1829 cdm->pos.generations[CAM_DEV_GENERATION] =
1830 periph->path->target->generation;
1831 cdm->pos.cookie.periph = periph;
1832 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1833 periph->path->device->generation;
1834 cdm->status = CAM_DEV_MATCH_MORE;
1838 j = cdm->num_matches;
1840 cdm->matches[j].type = DEV_MATCH_PERIPH;
1841 cdm->matches[j].result.periph_result.path_id =
1842 periph->path->bus->path_id;
1843 cdm->matches[j].result.periph_result.target_id =
1844 periph->path->target->target_id;
1845 cdm->matches[j].result.periph_result.target_lun =
1846 periph->path->device->lun_id;
1847 cdm->matches[j].result.periph_result.unit_number =
1848 periph->unit_number;
1849 strncpy(cdm->matches[j].result.periph_result.periph_name,
1850 periph->periph_name, DEV_IDLEN);
1857 xptedtmatch(struct ccb_dev_match *cdm)
1862 cdm->num_matches = 0;
1865 * Check the bus list generation. If it has changed, the user
1866 * needs to reset everything and start over.
1869 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1870 && (cdm->pos.cookie.bus != NULL)) {
1871 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1872 xsoftc.bus_generation) {
1874 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1877 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1883 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1886 * If we get back 0, that means that we had to stop before fully
1887 * traversing the EDT. It also means that one of the subroutines
1888 * has set the status field to the proper value. If we get back 1,
1889 * we've fully traversed the EDT and copied out any matching entries.
1892 cdm->status = CAM_DEV_MATCH_LAST;
1898 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1900 struct cam_periph *periph;
1901 struct ccb_dev_match *cdm;
1903 cdm = (struct ccb_dev_match *)arg;
1906 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1907 && (cdm->pos.cookie.pdrv == pdrv)
1908 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1909 && (cdm->pos.cookie.periph != NULL)) {
1910 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1911 (*pdrv)->generation) {
1913 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1916 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1922 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1926 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1928 struct ccb_dev_match *cdm;
1929 dev_match_ret retval;
1931 cdm = (struct ccb_dev_match *)arg;
1933 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1935 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1936 cdm->status = CAM_DEV_MATCH_ERROR;
1941 * If the copy flag is set, copy this peripheral out.
1943 if (retval & DM_RET_COPY) {
1946 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1947 sizeof(struct dev_match_result));
1950 * If we don't have enough space to put in another
1951 * match result, save our position and tell the
1952 * user there are more devices to check.
1954 if (spaceleft < sizeof(struct dev_match_result)) {
1955 struct periph_driver **pdrv;
1958 bzero(&cdm->pos, sizeof(cdm->pos));
1959 cdm->pos.position_type =
1960 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1964 * This may look a bit non-sensical, but it is
1965 * actually quite logical. There are very few
1966 * peripheral drivers, and bloating every peripheral
1967 * structure with a pointer back to its parent
1968 * peripheral driver linker set entry would cost
1969 * more in the long run than doing this quick lookup.
1971 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1972 if (strcmp((*pdrv)->driver_name,
1973 periph->periph_name) == 0)
1977 if (*pdrv == NULL) {
1978 cdm->status = CAM_DEV_MATCH_ERROR;
1982 cdm->pos.cookie.pdrv = pdrv;
1984 * The periph generation slot does double duty, as
1985 * does the periph pointer slot. They are used for
1986 * both edt and pdrv lookups and positioning.
1988 cdm->pos.cookie.periph = periph;
1989 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1990 (*pdrv)->generation;
1991 cdm->status = CAM_DEV_MATCH_MORE;
1995 j = cdm->num_matches;
1997 cdm->matches[j].type = DEV_MATCH_PERIPH;
1998 cdm->matches[j].result.periph_result.path_id =
1999 periph->path->bus->path_id;
2002 * The transport layer peripheral doesn't have a target or
2005 if (periph->path->target)
2006 cdm->matches[j].result.periph_result.target_id =
2007 periph->path->target->target_id;
2009 cdm->matches[j].result.periph_result.target_id =
2010 CAM_TARGET_WILDCARD;
2012 if (periph->path->device)
2013 cdm->matches[j].result.periph_result.target_lun =
2014 periph->path->device->lun_id;
2016 cdm->matches[j].result.periph_result.target_lun =
2019 cdm->matches[j].result.periph_result.unit_number =
2020 periph->unit_number;
2021 strncpy(cdm->matches[j].result.periph_result.periph_name,
2022 periph->periph_name, DEV_IDLEN);
2029 xptperiphlistmatch(struct ccb_dev_match *cdm)
2033 cdm->num_matches = 0;
2036 * At this point in the edt traversal function, we check the bus
2037 * list generation to make sure that no busses have been added or
2038 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2039 * For the peripheral driver list traversal function, however, we
2040 * don't have to worry about new peripheral driver types coming or
2041 * going; they're in a linker set, and therefore can't change
2042 * without a recompile.
2045 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2046 && (cdm->pos.cookie.pdrv != NULL))
2047 ret = xptpdrvtraverse(
2048 (struct periph_driver **)cdm->pos.cookie.pdrv,
2049 xptplistpdrvfunc, cdm);
2051 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2054 * If we get back 0, that means that we had to stop before fully
2055 * traversing the peripheral driver tree. It also means that one of
2056 * the subroutines has set the status field to the proper value. If
2057 * we get back 1, we've fully traversed the EDT and copied out any
2061 cdm->status = CAM_DEV_MATCH_LAST;
2067 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2069 struct cam_eb *bus, *next_bus;
2077 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2085 for (; bus != NULL; bus = next_bus) {
2086 retval = tr_func(bus, arg);
2088 xpt_release_bus(bus);
2092 next_bus = TAILQ_NEXT(bus, links);
2094 next_bus->refcount++;
2096 xpt_release_bus(bus);
2102 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2103 xpt_targetfunc_t *tr_func, void *arg)
2105 struct cam_et *target, *next_target;
2110 target = start_target;
2112 mtx_lock(&bus->eb_mtx);
2113 target = TAILQ_FIRST(&bus->et_entries);
2114 if (target == NULL) {
2115 mtx_unlock(&bus->eb_mtx);
2119 mtx_unlock(&bus->eb_mtx);
2121 for (; target != NULL; target = next_target) {
2122 retval = tr_func(target, arg);
2124 xpt_release_target(target);
2127 mtx_lock(&bus->eb_mtx);
2128 next_target = TAILQ_NEXT(target, links);
2130 next_target->refcount++;
2131 mtx_unlock(&bus->eb_mtx);
2132 xpt_release_target(target);
2138 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2139 xpt_devicefunc_t *tr_func, void *arg)
2142 struct cam_ed *device, *next_device;
2148 device = start_device;
2150 mtx_lock(&bus->eb_mtx);
2151 device = TAILQ_FIRST(&target->ed_entries);
2152 if (device == NULL) {
2153 mtx_unlock(&bus->eb_mtx);
2157 mtx_unlock(&bus->eb_mtx);
2159 for (; device != NULL; device = next_device) {
2160 mtx_lock(&device->device_mtx);
2161 retval = tr_func(device, arg);
2162 mtx_unlock(&device->device_mtx);
2164 xpt_release_device(device);
2167 mtx_lock(&bus->eb_mtx);
2168 next_device = TAILQ_NEXT(device, links);
2170 next_device->refcount++;
2171 mtx_unlock(&bus->eb_mtx);
2172 xpt_release_device(device);
2178 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2179 xpt_periphfunc_t *tr_func, void *arg)
2182 struct cam_periph *periph, *next_periph;
2187 bus = device->target->bus;
2189 periph = start_periph;
2192 mtx_lock(&bus->eb_mtx);
2193 periph = SLIST_FIRST(&device->periphs);
2194 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2195 periph = SLIST_NEXT(periph, periph_links);
2196 if (periph == NULL) {
2197 mtx_unlock(&bus->eb_mtx);
2202 mtx_unlock(&bus->eb_mtx);
2205 for (; periph != NULL; periph = next_periph) {
2206 retval = tr_func(periph, arg);
2208 cam_periph_release_locked(periph);
2212 mtx_lock(&bus->eb_mtx);
2213 next_periph = SLIST_NEXT(periph, periph_links);
2214 while (next_periph != NULL &&
2215 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2216 next_periph = SLIST_NEXT(next_periph, periph_links);
2218 next_periph->refcount++;
2219 mtx_unlock(&bus->eb_mtx);
2221 cam_periph_release_locked(periph);
2227 xptpdrvtraverse(struct periph_driver **start_pdrv,
2228 xpt_pdrvfunc_t *tr_func, void *arg)
2230 struct periph_driver **pdrv;
2236 * We don't traverse the peripheral driver list like we do the
2237 * other lists, because it is a linker set, and therefore cannot be
2238 * changed during runtime. If the peripheral driver list is ever
2239 * re-done to be something other than a linker set (i.e. it can
2240 * change while the system is running), the list traversal should
2241 * be modified to work like the other traversal functions.
2243 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2244 *pdrv != NULL; pdrv++) {
2245 retval = tr_func(pdrv, arg);
2255 xptpdperiphtraverse(struct periph_driver **pdrv,
2256 struct cam_periph *start_periph,
2257 xpt_periphfunc_t *tr_func, void *arg)
2259 struct cam_periph *periph, *next_periph;
2265 periph = start_periph;
2268 periph = TAILQ_FIRST(&(*pdrv)->units);
2269 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2270 periph = TAILQ_NEXT(periph, unit_links);
2271 if (periph == NULL) {
2278 for (; periph != NULL; periph = next_periph) {
2279 cam_periph_lock(periph);
2280 retval = tr_func(periph, arg);
2281 cam_periph_unlock(periph);
2283 cam_periph_release(periph);
2287 next_periph = TAILQ_NEXT(periph, unit_links);
2288 while (next_periph != NULL &&
2289 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2290 next_periph = TAILQ_NEXT(next_periph, unit_links);
2292 next_periph->refcount++;
2294 cam_periph_release(periph);
2300 xptdefbusfunc(struct cam_eb *bus, void *arg)
2302 struct xpt_traverse_config *tr_config;
2304 tr_config = (struct xpt_traverse_config *)arg;
2306 if (tr_config->depth == XPT_DEPTH_BUS) {
2307 xpt_busfunc_t *tr_func;
2309 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2311 return(tr_func(bus, tr_config->tr_arg));
2313 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2317 xptdeftargetfunc(struct cam_et *target, void *arg)
2319 struct xpt_traverse_config *tr_config;
2321 tr_config = (struct xpt_traverse_config *)arg;
2323 if (tr_config->depth == XPT_DEPTH_TARGET) {
2324 xpt_targetfunc_t *tr_func;
2326 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2328 return(tr_func(target, tr_config->tr_arg));
2330 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2334 xptdefdevicefunc(struct cam_ed *device, void *arg)
2336 struct xpt_traverse_config *tr_config;
2338 tr_config = (struct xpt_traverse_config *)arg;
2340 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2341 xpt_devicefunc_t *tr_func;
2343 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2345 return(tr_func(device, tr_config->tr_arg));
2347 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2351 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2353 struct xpt_traverse_config *tr_config;
2354 xpt_periphfunc_t *tr_func;
2356 tr_config = (struct xpt_traverse_config *)arg;
2358 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2361 * Unlike the other default functions, we don't check for depth
2362 * here. The peripheral driver level is the last level in the EDT,
2363 * so if we're here, we should execute the function in question.
2365 return(tr_func(periph, tr_config->tr_arg));
2369 * Execute the given function for every bus in the EDT.
2372 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2374 struct xpt_traverse_config tr_config;
2376 tr_config.depth = XPT_DEPTH_BUS;
2377 tr_config.tr_func = tr_func;
2378 tr_config.tr_arg = arg;
2380 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2384 * Execute the given function for every device in the EDT.
2387 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2389 struct xpt_traverse_config tr_config;
2391 tr_config.depth = XPT_DEPTH_DEVICE;
2392 tr_config.tr_func = tr_func;
2393 tr_config.tr_arg = arg;
2395 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2399 xptsetasyncfunc(struct cam_ed *device, void *arg)
2401 struct cam_path path;
2402 struct ccb_getdev cgd;
2403 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2406 * Don't report unconfigured devices (Wildcard devs,
2407 * devices only for target mode, device instances
2408 * that have been invalidated but are waiting for
2409 * their last reference count to be released).
2411 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2414 xpt_compile_path(&path,
2416 device->target->bus->path_id,
2417 device->target->target_id,
2419 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2420 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2421 xpt_action((union ccb *)&cgd);
2422 csa->callback(csa->callback_arg,
2425 xpt_release_path(&path);
2431 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2433 struct cam_path path;
2434 struct ccb_pathinq cpi;
2435 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2437 xpt_compile_path(&path, /*periph*/NULL,
2439 CAM_TARGET_WILDCARD,
2441 xpt_path_lock(&path);
2442 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2443 cpi.ccb_h.func_code = XPT_PATH_INQ;
2444 xpt_action((union ccb *)&cpi);
2445 csa->callback(csa->callback_arg,
2448 xpt_path_unlock(&path);
2449 xpt_release_path(&path);
2455 xpt_action(union ccb *start_ccb)
2458 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2459 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2460 xpt_action_name(start_ccb->ccb_h.func_code)));
2462 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2463 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2467 xpt_action_default(union ccb *start_ccb)
2469 struct cam_path *path;
2470 struct cam_sim *sim;
2473 path = start_ccb->ccb_h.path;
2474 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2475 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2476 xpt_action_name(start_ccb->ccb_h.func_code)));
2478 switch (start_ccb->ccb_h.func_code) {
2481 struct cam_ed *device;
2484 * For the sake of compatibility with SCSI-1
2485 * devices that may not understand the identify
2486 * message, we include lun information in the
2487 * second byte of all commands. SCSI-1 specifies
2488 * that luns are a 3 bit value and reserves only 3
2489 * bits for lun information in the CDB. Later
2490 * revisions of the SCSI spec allow for more than 8
2491 * luns, but have deprecated lun information in the
2492 * CDB. So, if the lun won't fit, we must omit.
2494 * Also be aware that during initial probing for devices,
2495 * the inquiry information is unknown but initialized to 0.
2496 * This means that this code will be exercised while probing
2497 * devices with an ANSI revision greater than 2.
2499 device = path->device;
2500 if (device->protocol_version <= SCSI_REV_2
2501 && start_ccb->ccb_h.target_lun < 8
2502 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2504 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2505 start_ccb->ccb_h.target_lun << 5;
2507 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2511 case XPT_CONT_TARGET_IO:
2512 start_ccb->csio.sense_resid = 0;
2513 start_ccb->csio.resid = 0;
2516 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2517 start_ccb->ataio.resid = 0;
2523 struct cam_devq *devq;
2525 devq = path->bus->sim->devq;
2526 mtx_lock(&devq->send_mtx);
2527 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2528 if (xpt_schedule_devq(devq, path->device) != 0)
2530 mtx_unlock(&devq->send_mtx);
2533 case XPT_CALC_GEOMETRY:
2534 /* Filter out garbage */
2535 if (start_ccb->ccg.block_size == 0
2536 || start_ccb->ccg.volume_size == 0) {
2537 start_ccb->ccg.cylinders = 0;
2538 start_ccb->ccg.heads = 0;
2539 start_ccb->ccg.secs_per_track = 0;
2540 start_ccb->ccb_h.status = CAM_REQ_CMP;
2543 #if defined(PC98) || defined(__sparc64__)
2545 * In a PC-98 system, geometry translation depens on
2546 * the "real" device geometry obtained from mode page 4.
2547 * SCSI geometry translation is performed in the
2548 * initialization routine of the SCSI BIOS and the result
2549 * stored in host memory. If the translation is available
2550 * in host memory, use it. If not, rely on the default
2551 * translation the device driver performs.
2552 * For sparc64, we may need adjust the geometry of large
2553 * disks in order to fit the limitations of the 16-bit
2554 * fields of the VTOC8 disk label.
2556 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2557 start_ccb->ccb_h.status = CAM_REQ_CMP;
2564 union ccb* abort_ccb;
2566 abort_ccb = start_ccb->cab.abort_ccb;
2567 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2569 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2570 struct cam_ccbq *ccbq;
2571 struct cam_ed *device;
2573 device = abort_ccb->ccb_h.path->device;
2574 ccbq = &device->ccbq;
2575 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2576 abort_ccb->ccb_h.status =
2577 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2578 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2579 xpt_done(abort_ccb);
2580 start_ccb->ccb_h.status = CAM_REQ_CMP;
2583 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2584 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2586 * We've caught this ccb en route to
2587 * the SIM. Flag it for abort and the
2588 * SIM will do so just before starting
2589 * real work on the CCB.
2591 abort_ccb->ccb_h.status =
2592 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2593 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2594 start_ccb->ccb_h.status = CAM_REQ_CMP;
2598 if (XPT_FC_IS_QUEUED(abort_ccb)
2599 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2601 * It's already completed but waiting
2602 * for our SWI to get to it.
2604 start_ccb->ccb_h.status = CAM_UA_ABORT;
2608 * If we weren't able to take care of the abort request
2609 * in the XPT, pass the request down to the SIM for processing.
2613 case XPT_ACCEPT_TARGET_IO:
2615 case XPT_IMMED_NOTIFY:
2616 case XPT_NOTIFY_ACK:
2618 case XPT_IMMEDIATE_NOTIFY:
2619 case XPT_NOTIFY_ACKNOWLEDGE:
2620 case XPT_GET_SIM_KNOB_OLD:
2621 case XPT_GET_SIM_KNOB:
2622 case XPT_SET_SIM_KNOB:
2623 case XPT_GET_TRAN_SETTINGS:
2624 case XPT_SET_TRAN_SETTINGS:
2627 sim = path->bus->sim;
2628 lock = (mtx_owned(sim->mtx) == 0);
2631 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2632 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2633 (*(sim->sim_action))(sim, start_ccb);
2634 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2635 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2637 CAM_SIM_UNLOCK(sim);
2639 case XPT_PATH_STATS:
2640 start_ccb->cpis.last_reset = path->bus->last_reset;
2641 start_ccb->ccb_h.status = CAM_REQ_CMP;
2648 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2649 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2651 struct ccb_getdev *cgd;
2653 cgd = &start_ccb->cgd;
2654 cgd->protocol = dev->protocol;
2655 cgd->inq_data = dev->inq_data;
2656 cgd->ident_data = dev->ident_data;
2657 cgd->inq_flags = dev->inq_flags;
2658 cgd->ccb_h.status = CAM_REQ_CMP;
2659 cgd->serial_num_len = dev->serial_num_len;
2660 if ((dev->serial_num_len > 0)
2661 && (dev->serial_num != NULL))
2662 bcopy(dev->serial_num, cgd->serial_num,
2663 dev->serial_num_len);
2667 case XPT_GDEV_STATS:
2672 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2673 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2675 struct ccb_getdevstats *cgds;
2678 struct cam_devq *devq;
2680 cgds = &start_ccb->cgds;
2683 devq = bus->sim->devq;
2684 mtx_lock(&devq->send_mtx);
2685 cgds->dev_openings = dev->ccbq.dev_openings;
2686 cgds->dev_active = dev->ccbq.dev_active;
2687 cgds->allocated = dev->ccbq.allocated;
2688 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2689 cgds->held = cgds->allocated - cgds->dev_active -
2691 cgds->last_reset = tar->last_reset;
2692 cgds->maxtags = dev->maxtags;
2693 cgds->mintags = dev->mintags;
2694 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2695 cgds->last_reset = bus->last_reset;
2696 mtx_unlock(&devq->send_mtx);
2697 cgds->ccb_h.status = CAM_REQ_CMP;
2703 struct cam_periph *nperiph;
2704 struct periph_list *periph_head;
2705 struct ccb_getdevlist *cgdl;
2707 struct cam_ed *device;
2714 * Don't want anyone mucking with our data.
2716 device = path->device;
2717 periph_head = &device->periphs;
2718 cgdl = &start_ccb->cgdl;
2721 * Check and see if the list has changed since the user
2722 * last requested a list member. If so, tell them that the
2723 * list has changed, and therefore they need to start over
2724 * from the beginning.
2726 if ((cgdl->index != 0) &&
2727 (cgdl->generation != device->generation)) {
2728 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2733 * Traverse the list of peripherals and attempt to find
2734 * the requested peripheral.
2736 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2737 (nperiph != NULL) && (i <= cgdl->index);
2738 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2739 if (i == cgdl->index) {
2740 strncpy(cgdl->periph_name,
2741 nperiph->periph_name,
2743 cgdl->unit_number = nperiph->unit_number;
2748 cgdl->status = CAM_GDEVLIST_ERROR;
2752 if (nperiph == NULL)
2753 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2755 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2758 cgdl->generation = device->generation;
2760 cgdl->ccb_h.status = CAM_REQ_CMP;
2765 dev_pos_type position_type;
2766 struct ccb_dev_match *cdm;
2768 cdm = &start_ccb->cdm;
2771 * There are two ways of getting at information in the EDT.
2772 * The first way is via the primary EDT tree. It starts
2773 * with a list of busses, then a list of targets on a bus,
2774 * then devices/luns on a target, and then peripherals on a
2775 * device/lun. The "other" way is by the peripheral driver
2776 * lists. The peripheral driver lists are organized by
2777 * peripheral driver. (obviously) So it makes sense to
2778 * use the peripheral driver list if the user is looking
2779 * for something like "da1", or all "da" devices. If the
2780 * user is looking for something on a particular bus/target
2781 * or lun, it's generally better to go through the EDT tree.
2784 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2785 position_type = cdm->pos.position_type;
2789 position_type = CAM_DEV_POS_NONE;
2791 for (i = 0; i < cdm->num_patterns; i++) {
2792 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2793 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2794 position_type = CAM_DEV_POS_EDT;
2799 if (cdm->num_patterns == 0)
2800 position_type = CAM_DEV_POS_EDT;
2801 else if (position_type == CAM_DEV_POS_NONE)
2802 position_type = CAM_DEV_POS_PDRV;
2805 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2806 case CAM_DEV_POS_EDT:
2809 case CAM_DEV_POS_PDRV:
2810 xptperiphlistmatch(cdm);
2813 cdm->status = CAM_DEV_MATCH_ERROR;
2817 if (cdm->status == CAM_DEV_MATCH_ERROR)
2818 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2820 start_ccb->ccb_h.status = CAM_REQ_CMP;
2826 struct ccb_setasync *csa;
2827 struct async_node *cur_entry;
2828 struct async_list *async_head;
2831 csa = &start_ccb->csa;
2832 added = csa->event_enable;
2833 async_head = &path->device->asyncs;
2836 * If there is already an entry for us, simply
2839 cur_entry = SLIST_FIRST(async_head);
2840 while (cur_entry != NULL) {
2841 if ((cur_entry->callback_arg == csa->callback_arg)
2842 && (cur_entry->callback == csa->callback))
2844 cur_entry = SLIST_NEXT(cur_entry, links);
2847 if (cur_entry != NULL) {
2849 * If the request has no flags set,
2852 added &= ~cur_entry->event_enable;
2853 if (csa->event_enable == 0) {
2854 SLIST_REMOVE(async_head, cur_entry,
2856 xpt_release_device(path->device);
2857 free(cur_entry, M_CAMXPT);
2859 cur_entry->event_enable = csa->event_enable;
2861 csa->event_enable = added;
2863 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2865 if (cur_entry == NULL) {
2866 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2869 cur_entry->event_enable = csa->event_enable;
2870 cur_entry->event_lock =
2871 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2872 cur_entry->callback_arg = csa->callback_arg;
2873 cur_entry->callback = csa->callback;
2874 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2875 xpt_acquire_device(path->device);
2877 start_ccb->ccb_h.status = CAM_REQ_CMP;
2882 struct ccb_relsim *crs;
2885 crs = &start_ccb->crs;
2889 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2893 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2895 /* Don't ever go below one opening */
2896 if (crs->openings > 0) {
2897 xpt_dev_ccbq_resize(path, crs->openings);
2900 "number of openings is now %d\n",
2906 mtx_lock(&dev->sim->devq->send_mtx);
2907 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2909 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2912 * Just extend the old timeout and decrement
2913 * the freeze count so that a single timeout
2914 * is sufficient for releasing the queue.
2916 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2917 callout_stop(&dev->callout);
2920 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2923 callout_reset_sbt(&dev->callout,
2924 SBT_1MS * crs->release_timeout, 0,
2925 xpt_release_devq_timeout, dev, 0);
2927 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2931 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2933 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2935 * Decrement the freeze count so that a single
2936 * completion is still sufficient to unfreeze
2939 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2942 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2943 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2947 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2949 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2950 || (dev->ccbq.dev_active == 0)) {
2952 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2955 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2956 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2959 mtx_unlock(&dev->sim->devq->send_mtx);
2961 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2962 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2963 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2964 start_ccb->ccb_h.status = CAM_REQ_CMP;
2968 struct cam_path *oldpath;
2970 /* Check that all request bits are supported. */
2971 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2972 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2976 cam_dflags = CAM_DEBUG_NONE;
2977 if (cam_dpath != NULL) {
2978 oldpath = cam_dpath;
2980 xpt_free_path(oldpath);
2982 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2983 if (xpt_create_path(&cam_dpath, NULL,
2984 start_ccb->ccb_h.path_id,
2985 start_ccb->ccb_h.target_id,
2986 start_ccb->ccb_h.target_lun) !=
2988 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2990 cam_dflags = start_ccb->cdbg.flags;
2991 start_ccb->ccb_h.status = CAM_REQ_CMP;
2992 xpt_print(cam_dpath, "debugging flags now %x\n",
2996 start_ccb->ccb_h.status = CAM_REQ_CMP;
3000 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3001 xpt_freeze_devq(path, 1);
3002 start_ccb->ccb_h.status = CAM_REQ_CMP;
3004 case XPT_REPROBE_LUN:
3005 xpt_async(AC_INQ_CHANGED, path, NULL);
3006 start_ccb->ccb_h.status = CAM_REQ_CMP;
3007 xpt_done(start_ccb);
3014 printf("%s: CCB type %#x not supported\n", __func__,
3015 start_ccb->ccb_h.func_code);
3016 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3017 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3018 xpt_done(start_ccb);
3022 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3023 ("xpt_action_default: func= %#x %s status %#x\n",
3024 start_ccb->ccb_h.func_code,
3025 xpt_action_name(start_ccb->ccb_h.func_code),
3026 start_ccb->ccb_h.status));
3030 xpt_polled_action(union ccb *start_ccb)
3033 struct cam_sim *sim;
3034 struct cam_devq *devq;
3037 timeout = start_ccb->ccb_h.timeout * 10;
3038 sim = start_ccb->ccb_h.path->bus->sim;
3040 dev = start_ccb->ccb_h.path->device;
3042 mtx_unlock(&dev->device_mtx);
3045 * Steal an opening so that no other queued requests
3046 * can get it before us while we simulate interrupts.
3048 mtx_lock(&devq->send_mtx);
3049 dev->ccbq.dev_openings--;
3050 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3052 mtx_unlock(&devq->send_mtx);
3055 (*(sim->sim_poll))(sim);
3056 CAM_SIM_UNLOCK(sim);
3058 mtx_lock(&devq->send_mtx);
3060 dev->ccbq.dev_openings++;
3061 mtx_unlock(&devq->send_mtx);
3064 xpt_action(start_ccb);
3065 while(--timeout > 0) {
3067 (*(sim->sim_poll))(sim);
3068 CAM_SIM_UNLOCK(sim);
3070 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3077 * XXX Is it worth adding a sim_timeout entry
3078 * point so we can attempt recovery? If
3079 * this is only used for dumps, I don't think
3082 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3085 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3088 mtx_lock(&dev->device_mtx);
3092 * Schedule a peripheral driver to receive a ccb when its
3093 * target device has space for more transactions.
3096 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3099 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3100 cam_periph_assert(periph, MA_OWNED);
3101 if (new_priority < periph->scheduled_priority) {
3102 periph->scheduled_priority = new_priority;
3103 xpt_run_allocq(periph, 0);
3109 * Schedule a device to run on a given queue.
3110 * If the device was inserted as a new entry on the queue,
3111 * return 1 meaning the device queue should be run. If we
3112 * were already queued, implying someone else has already
3113 * started the queue, return 0 so the caller doesn't attempt
3117 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3118 u_int32_t new_priority)
3121 u_int32_t old_priority;
3123 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3125 old_priority = pinfo->priority;
3128 * Are we already queued?
3130 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3131 /* Simply reorder based on new priority */
3132 if (new_priority < old_priority) {
3133 camq_change_priority(queue, pinfo->index,
3135 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3136 ("changed priority to %d\n",
3142 /* New entry on the queue */
3143 if (new_priority < old_priority)
3144 pinfo->priority = new_priority;
3146 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3147 ("Inserting onto queue\n"));
3148 pinfo->generation = ++queue->generation;
3149 camq_insert(queue, pinfo);
3156 xpt_run_allocq_task(void *context, int pending)
3158 struct cam_periph *periph = context;
3160 cam_periph_lock(periph);
3161 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3162 xpt_run_allocq(periph, 1);
3163 cam_periph_unlock(periph);
3164 cam_periph_release(periph);
3168 xpt_run_allocq(struct cam_periph *periph, int sleep)
3170 struct cam_ed *device;
3174 cam_periph_assert(periph, MA_OWNED);
3175 if (periph->periph_allocating)
3177 periph->periph_allocating = 1;
3178 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3179 device = periph->path->device;
3182 while ((prio = min(periph->scheduled_priority,
3183 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3184 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3185 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3188 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3190 ccb = xpt_get_ccb(periph);
3193 if (periph->flags & CAM_PERIPH_RUN_TASK)
3195 cam_periph_doacquire(periph);
3196 periph->flags |= CAM_PERIPH_RUN_TASK;
3197 taskqueue_enqueue(xsoftc.xpt_taskq,
3198 &periph->periph_run_task);
3201 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3202 if (prio == periph->immediate_priority) {
3203 periph->immediate_priority = CAM_PRIORITY_NONE;
3204 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3205 ("waking cam_periph_getccb()\n"));
3206 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3208 wakeup(&periph->ccb_list);
3210 periph->scheduled_priority = CAM_PRIORITY_NONE;
3211 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3212 ("calling periph_start()\n"));
3213 periph->periph_start(periph, ccb);
3218 xpt_release_ccb(ccb);
3219 periph->periph_allocating = 0;
3223 xpt_run_devq(struct cam_devq *devq)
3225 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3228 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3230 devq->send_queue.qfrozen_cnt++;
3231 while ((devq->send_queue.entries > 0)
3232 && (devq->send_openings > 0)
3233 && (devq->send_queue.qfrozen_cnt <= 1)) {
3234 struct cam_ed *device;
3235 union ccb *work_ccb;
3236 struct cam_sim *sim;
3238 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3240 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3241 ("running device %p\n", device));
3243 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3244 if (work_ccb == NULL) {
3245 printf("device on run queue with no ccbs???\n");
3249 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3251 mtx_lock(&xsoftc.xpt_highpower_lock);
3252 if (xsoftc.num_highpower <= 0) {
3254 * We got a high power command, but we
3255 * don't have any available slots. Freeze
3256 * the device queue until we have a slot
3259 xpt_freeze_devq_device(device, 1);
3260 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3263 mtx_unlock(&xsoftc.xpt_highpower_lock);
3267 * Consume a high power slot while
3270 xsoftc.num_highpower--;
3272 mtx_unlock(&xsoftc.xpt_highpower_lock);
3274 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3275 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3276 devq->send_openings--;
3277 devq->send_active++;
3278 xpt_schedule_devq(devq, device);
3279 mtx_unlock(&devq->send_mtx);
3281 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3283 * The client wants to freeze the queue
3284 * after this CCB is sent.
3286 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3289 /* In Target mode, the peripheral driver knows best... */
3290 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3291 if ((device->inq_flags & SID_CmdQue) != 0
3292 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3293 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3296 * Clear this in case of a retried CCB that
3297 * failed due to a rejected tag.
3299 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3302 switch (work_ccb->ccb_h.func_code) {
3304 CAM_DEBUG(work_ccb->ccb_h.path,
3305 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3306 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3308 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3309 cdb_str, sizeof(cdb_str))));
3312 CAM_DEBUG(work_ccb->ccb_h.path,
3313 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3314 ata_op_string(&work_ccb->ataio.cmd),
3315 ata_cmd_string(&work_ccb->ataio.cmd,
3316 cdb_str, sizeof(cdb_str))));
3323 * Device queues can be shared among multiple SIM instances
3324 * that reside on different busses. Use the SIM from the
3325 * queued device, rather than the one from the calling bus.
3328 lock = (mtx_owned(sim->mtx) == 0);
3331 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3332 (*(sim->sim_action))(sim, work_ccb);
3334 CAM_SIM_UNLOCK(sim);
3335 mtx_lock(&devq->send_mtx);
3337 devq->send_queue.qfrozen_cnt--;
3341 * This function merges stuff from the slave ccb into the master ccb, while
3342 * keeping important fields in the master ccb constant.
3345 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3349 * Pull fields that are valid for peripheral drivers to set
3350 * into the master CCB along with the CCB "payload".
3352 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3353 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3354 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3355 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3356 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3357 sizeof(union ccb) - sizeof(struct ccb_hdr));
3361 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3362 u_int32_t priority, u_int32_t flags)
3365 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3366 ccb_h->pinfo.priority = priority;
3368 ccb_h->path_id = path->bus->path_id;
3370 ccb_h->target_id = path->target->target_id;
3372 ccb_h->target_id = CAM_TARGET_WILDCARD;
3374 ccb_h->target_lun = path->device->lun_id;
3375 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3377 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3379 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3380 ccb_h->flags = flags;
3385 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3387 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3390 /* Path manipulation functions */
3392 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3393 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3395 struct cam_path *path;
3398 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3401 status = CAM_RESRC_UNAVAIL;
3404 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3405 if (status != CAM_REQ_CMP) {
3406 free(path, M_CAMPATH);
3409 *new_path_ptr = path;
3414 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3415 struct cam_periph *periph, path_id_t path_id,
3416 target_id_t target_id, lun_id_t lun_id)
3419 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3424 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3425 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3428 struct cam_et *target;
3429 struct cam_ed *device;
3432 status = CAM_REQ_CMP; /* Completed without error */
3433 target = NULL; /* Wildcarded */
3434 device = NULL; /* Wildcarded */
3437 * We will potentially modify the EDT, so block interrupts
3438 * that may attempt to create cam paths.
3440 bus = xpt_find_bus(path_id);
3442 status = CAM_PATH_INVALID;
3445 mtx_lock(&bus->eb_mtx);
3446 target = xpt_find_target(bus, target_id);
3447 if (target == NULL) {
3449 struct cam_et *new_target;
3451 new_target = xpt_alloc_target(bus, target_id);
3452 if (new_target == NULL) {
3453 status = CAM_RESRC_UNAVAIL;
3455 target = new_target;
3459 if (target != NULL) {
3460 device = xpt_find_device(target, lun_id);
3461 if (device == NULL) {
3463 struct cam_ed *new_device;
3466 (*(bus->xport->alloc_device))(bus,
3469 if (new_device == NULL) {
3470 status = CAM_RESRC_UNAVAIL;
3472 device = new_device;
3476 mtx_unlock(&bus->eb_mtx);
3480 * Only touch the user's data if we are successful.
3482 if (status == CAM_REQ_CMP) {
3483 new_path->periph = perph;
3484 new_path->bus = bus;
3485 new_path->target = target;
3486 new_path->device = device;
3487 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3490 xpt_release_device(device);
3492 xpt_release_target(target);
3494 xpt_release_bus(bus);
3500 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3502 struct cam_path *new_path;
3504 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3505 if (new_path == NULL)
3506 return(CAM_RESRC_UNAVAIL);
3507 xpt_copy_path(new_path, path);
3508 *new_path_ptr = new_path;
3509 return (CAM_REQ_CMP);
3513 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3517 if (path->bus != NULL)
3518 xpt_acquire_bus(path->bus);
3519 if (path->target != NULL)
3520 xpt_acquire_target(path->target);
3521 if (path->device != NULL)
3522 xpt_acquire_device(path->device);
3526 xpt_release_path(struct cam_path *path)
3528 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3529 if (path->device != NULL) {
3530 xpt_release_device(path->device);
3531 path->device = NULL;
3533 if (path->target != NULL) {
3534 xpt_release_target(path->target);
3535 path->target = NULL;
3537 if (path->bus != NULL) {
3538 xpt_release_bus(path->bus);
3544 xpt_free_path(struct cam_path *path)
3547 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3548 xpt_release_path(path);
3549 free(path, M_CAMPATH);
3553 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3554 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3560 *bus_ref = path->bus->refcount;
3566 *periph_ref = path->periph->refcount;
3573 *target_ref = path->target->refcount;
3579 *device_ref = path->device->refcount;
3586 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3587 * in path1, 2 for match with wildcards in path2.
3590 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3594 if (path1->bus != path2->bus) {
3595 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3597 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3602 if (path1->target != path2->target) {
3603 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3606 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3611 if (path1->device != path2->device) {
3612 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3615 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3624 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3628 if (path->bus != dev->target->bus) {
3629 if (path->bus->path_id == CAM_BUS_WILDCARD)
3631 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3636 if (path->target != dev->target) {
3637 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3640 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3645 if (path->device != dev) {
3646 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3649 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3658 xpt_print_path(struct cam_path *path)
3662 printf("(nopath): ");
3664 if (path->periph != NULL)
3665 printf("(%s%d:", path->periph->periph_name,
3666 path->periph->unit_number);
3668 printf("(noperiph:");
3670 if (path->bus != NULL)
3671 printf("%s%d:%d:", path->bus->sim->sim_name,
3672 path->bus->sim->unit_number,
3673 path->bus->sim->bus_id);
3677 if (path->target != NULL)
3678 printf("%d:", path->target->target_id);
3682 if (path->device != NULL)
3683 printf("%jx): ", (uintmax_t)path->device->lun_id);
3690 xpt_print_device(struct cam_ed *device)
3694 printf("(nopath): ");
3696 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3697 device->sim->unit_number,
3698 device->sim->bus_id,
3699 device->target->target_id,
3700 (uintmax_t)device->lun_id);
3705 xpt_print(struct cam_path *path, const char *fmt, ...)
3708 xpt_print_path(path);
3715 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3719 sbuf_new(&sb, str, str_len, 0);
3722 sbuf_printf(&sb, "(nopath): ");
3724 if (path->periph != NULL)
3725 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3726 path->periph->unit_number);
3728 sbuf_printf(&sb, "(noperiph:");
3730 if (path->bus != NULL)
3731 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3732 path->bus->sim->unit_number,
3733 path->bus->sim->bus_id);
3735 sbuf_printf(&sb, "nobus:");
3737 if (path->target != NULL)
3738 sbuf_printf(&sb, "%d:", path->target->target_id);
3740 sbuf_printf(&sb, "X:");
3742 if (path->device != NULL)
3743 sbuf_printf(&sb, "%jx): ",
3744 (uintmax_t)path->device->lun_id);
3746 sbuf_printf(&sb, "X): ");
3750 return(sbuf_len(&sb));
3754 xpt_path_path_id(struct cam_path *path)
3756 return(path->bus->path_id);
3760 xpt_path_target_id(struct cam_path *path)
3762 if (path->target != NULL)
3763 return (path->target->target_id);
3765 return (CAM_TARGET_WILDCARD);
3769 xpt_path_lun_id(struct cam_path *path)
3771 if (path->device != NULL)
3772 return (path->device->lun_id);
3774 return (CAM_LUN_WILDCARD);
3778 xpt_path_sim(struct cam_path *path)
3781 return (path->bus->sim);
3785 xpt_path_periph(struct cam_path *path)
3788 return (path->periph);
3792 * Release a CAM control block for the caller. Remit the cost of the structure
3793 * to the device referenced by the path. If the this device had no 'credits'
3794 * and peripheral drivers have registered async callbacks for this notification
3798 xpt_release_ccb(union ccb *free_ccb)
3800 struct cam_ed *device;
3801 struct cam_periph *periph;
3803 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3804 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3805 device = free_ccb->ccb_h.path->device;
3806 periph = free_ccb->ccb_h.path->periph;
3808 xpt_free_ccb(free_ccb);
3809 periph->periph_allocated--;
3810 cam_ccbq_release_opening(&device->ccbq);
3811 xpt_run_allocq(periph, 0);
3814 /* Functions accessed by SIM drivers */
3816 static struct xpt_xport xport_default = {
3817 .alloc_device = xpt_alloc_device_default,
3818 .action = xpt_action_default,
3819 .async = xpt_dev_async_default,
3823 * A sim structure, listing the SIM entry points and instance
3824 * identification info is passed to xpt_bus_register to hook the SIM
3825 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3826 * for this new bus and places it in the array of busses and assigns
3827 * it a path_id. The path_id may be influenced by "hard wiring"
3828 * information specified by the user. Once interrupt services are
3829 * available, the bus will be probed.
3832 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3834 struct cam_eb *new_bus;
3835 struct cam_eb *old_bus;
3836 struct ccb_pathinq cpi;
3837 struct cam_path *path;
3840 mtx_assert(sim->mtx, MA_OWNED);
3843 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3844 M_CAMXPT, M_NOWAIT|M_ZERO);
3845 if (new_bus == NULL) {
3846 /* Couldn't satisfy request */
3847 return (CAM_RESRC_UNAVAIL);
3850 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3851 TAILQ_INIT(&new_bus->et_entries);
3854 timevalclear(&new_bus->last_reset);
3856 new_bus->refcount = 1; /* Held until a bus_deregister event */
3857 new_bus->generation = 0;
3860 sim->path_id = new_bus->path_id =
3861 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3862 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3863 while (old_bus != NULL
3864 && old_bus->path_id < new_bus->path_id)
3865 old_bus = TAILQ_NEXT(old_bus, links);
3866 if (old_bus != NULL)
3867 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3869 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3870 xsoftc.bus_generation++;
3874 * Set a default transport so that a PATH_INQ can be issued to
3875 * the SIM. This will then allow for probing and attaching of
3876 * a more appropriate transport.
3878 new_bus->xport = &xport_default;
3880 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3881 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3882 if (status != CAM_REQ_CMP) {
3883 xpt_release_bus(new_bus);
3884 free(path, M_CAMXPT);
3885 return (CAM_RESRC_UNAVAIL);
3888 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3889 cpi.ccb_h.func_code = XPT_PATH_INQ;
3890 xpt_action((union ccb *)&cpi);
3892 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3893 switch (cpi.transport) {
3901 new_bus->xport = scsi_get_xport();
3905 new_bus->xport = ata_get_xport();
3908 new_bus->xport = &xport_default;
3913 /* Notify interested parties */
3914 if (sim->path_id != CAM_XPT_PATH_ID) {
3916 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3917 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3918 union ccb *scan_ccb;
3920 /* Initiate bus rescan. */
3921 scan_ccb = xpt_alloc_ccb_nowait();
3922 if (scan_ccb != NULL) {
3923 scan_ccb->ccb_h.path = path;
3924 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3925 scan_ccb->crcn.flags = 0;
3926 xpt_rescan(scan_ccb);
3929 "Can't allocate CCB to scan bus\n");
3930 xpt_free_path(path);
3933 xpt_free_path(path);
3935 xpt_free_path(path);
3936 return (CAM_SUCCESS);
3940 xpt_bus_deregister(path_id_t pathid)
3942 struct cam_path bus_path;
3945 status = xpt_compile_path(&bus_path, NULL, pathid,
3946 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3947 if (status != CAM_REQ_CMP)
3950 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3951 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3953 /* Release the reference count held while registered. */
3954 xpt_release_bus(bus_path.bus);
3955 xpt_release_path(&bus_path);
3957 return (CAM_REQ_CMP);
3961 xptnextfreepathid(void)
3967 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3969 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3971 /* Find an unoccupied pathid */
3972 while (bus != NULL && bus->path_id <= pathid) {
3973 if (bus->path_id == pathid)
3975 bus = TAILQ_NEXT(bus, links);
3979 * Ensure that this pathid is not reserved for
3980 * a bus that may be registered in the future.
3982 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3984 /* Start the search over */
3991 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3998 pathid = CAM_XPT_PATH_ID;
3999 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4000 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4003 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4004 if (strcmp(dname, "scbus")) {
4005 /* Avoid a bit of foot shooting. */
4008 if (dunit < 0) /* unwired?! */
4010 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4011 if (sim_bus == val) {
4015 } else if (sim_bus == 0) {
4016 /* Unspecified matches bus 0 */
4020 printf("Ambiguous scbus configuration for %s%d "
4021 "bus %d, cannot wire down. The kernel "
4022 "config entry for scbus%d should "
4023 "specify a controller bus.\n"
4024 "Scbus will be assigned dynamically.\n",
4025 sim_name, sim_unit, sim_bus, dunit);
4030 if (pathid == CAM_XPT_PATH_ID)
4031 pathid = xptnextfreepathid();
4036 xpt_async_string(u_int32_t async_code)
4039 switch (async_code) {
4040 case AC_BUS_RESET: return ("AC_BUS_RESET");
4041 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4042 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4043 case AC_SENT_BDR: return ("AC_SENT_BDR");
4044 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4045 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4046 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4047 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4048 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4049 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4050 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4051 case AC_CONTRACT: return ("AC_CONTRACT");
4052 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4053 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4055 return ("AC_UNKNOWN");
4059 xpt_async_size(u_int32_t async_code)
4062 switch (async_code) {
4063 case AC_BUS_RESET: return (0);
4064 case AC_UNSOL_RESEL: return (0);
4065 case AC_SCSI_AEN: return (0);
4066 case AC_SENT_BDR: return (0);
4067 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4068 case AC_PATH_DEREGISTERED: return (0);
4069 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4070 case AC_LOST_DEVICE: return (0);
4071 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4072 case AC_INQ_CHANGED: return (0);
4073 case AC_GETDEV_CHANGED: return (0);
4074 case AC_CONTRACT: return (sizeof(struct ac_contract));
4075 case AC_ADVINFO_CHANGED: return (-1);
4076 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4082 xpt_async_process_dev(struct cam_ed *device, void *arg)
4084 union ccb *ccb = arg;
4085 struct cam_path *path = ccb->ccb_h.path;
4086 void *async_arg = ccb->casync.async_arg_ptr;
4087 u_int32_t async_code = ccb->casync.async_code;
4090 if (path->device != device
4091 && path->device->lun_id != CAM_LUN_WILDCARD
4092 && device->lun_id != CAM_LUN_WILDCARD)
4096 * The async callback could free the device.
4097 * If it is a broadcast async, it doesn't hold
4098 * device reference, so take our own reference.
4100 xpt_acquire_device(device);
4103 * If async for specific device is to be delivered to
4104 * the wildcard client, take the specific device lock.
4105 * XXX: We may need a way for client to specify it.
4107 if ((device->lun_id == CAM_LUN_WILDCARD &&
4108 path->device->lun_id != CAM_LUN_WILDCARD) ||
4109 (device->target->target_id == CAM_TARGET_WILDCARD &&
4110 path->target->target_id != CAM_TARGET_WILDCARD) ||
4111 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4112 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4113 mtx_unlock(&device->device_mtx);
4114 xpt_path_lock(path);
4119 (*(device->target->bus->xport->async))(async_code,
4120 device->target->bus, device->target, device, async_arg);
4121 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4124 xpt_path_unlock(path);
4125 mtx_lock(&device->device_mtx);
4127 xpt_release_device(device);
4132 xpt_async_process_tgt(struct cam_et *target, void *arg)
4134 union ccb *ccb = arg;
4135 struct cam_path *path = ccb->ccb_h.path;
4137 if (path->target != target
4138 && path->target->target_id != CAM_TARGET_WILDCARD
4139 && target->target_id != CAM_TARGET_WILDCARD)
4142 if (ccb->casync.async_code == AC_SENT_BDR) {
4143 /* Update our notion of when the last reset occurred */
4144 microtime(&target->last_reset);
4147 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4151 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4154 struct cam_path *path;
4156 u_int32_t async_code;
4158 path = ccb->ccb_h.path;
4159 async_code = ccb->casync.async_code;
4160 async_arg = ccb->casync.async_arg_ptr;
4161 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4162 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4165 if (async_code == AC_BUS_RESET) {
4166 /* Update our notion of when the last reset occurred */
4167 microtime(&bus->last_reset);
4170 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4173 * If this wasn't a fully wildcarded async, tell all
4174 * clients that want all async events.
4176 if (bus != xpt_periph->path->bus) {
4177 xpt_path_lock(xpt_periph->path);
4178 xpt_async_process_dev(xpt_periph->path->device, ccb);
4179 xpt_path_unlock(xpt_periph->path);
4182 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4183 xpt_release_devq(path, 1, TRUE);
4185 xpt_release_simq(path->bus->sim, TRUE);
4186 if (ccb->casync.async_arg_size > 0)
4187 free(async_arg, M_CAMXPT);
4188 xpt_free_path(path);
4193 xpt_async_bcast(struct async_list *async_head,
4194 u_int32_t async_code,
4195 struct cam_path *path, void *async_arg)
4197 struct async_node *cur_entry;
4200 cur_entry = SLIST_FIRST(async_head);
4201 while (cur_entry != NULL) {
4202 struct async_node *next_entry;
4204 * Grab the next list entry before we call the current
4205 * entry's callback. This is because the callback function
4206 * can delete its async callback entry.
4208 next_entry = SLIST_NEXT(cur_entry, links);
4209 if ((cur_entry->event_enable & async_code) != 0) {
4210 lock = cur_entry->event_lock;
4212 CAM_SIM_LOCK(path->device->sim);
4213 cur_entry->callback(cur_entry->callback_arg,
4217 CAM_SIM_UNLOCK(path->device->sim);
4219 cur_entry = next_entry;
4224 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4229 ccb = xpt_alloc_ccb_nowait();
4231 xpt_print(path, "Can't allocate CCB to send %s\n",
4232 xpt_async_string(async_code));
4236 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4237 xpt_print(path, "Can't allocate path to send %s\n",
4238 xpt_async_string(async_code));
4242 ccb->ccb_h.path->periph = NULL;
4243 ccb->ccb_h.func_code = XPT_ASYNC;
4244 ccb->ccb_h.cbfcnp = xpt_async_process;
4245 ccb->ccb_h.flags |= CAM_UNLOCKED;
4246 ccb->casync.async_code = async_code;
4247 ccb->casync.async_arg_size = 0;
4248 size = xpt_async_size(async_code);
4249 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4250 ("xpt_async: func %#x %s aync_code %d %s\n",
4251 ccb->ccb_h.func_code,
4252 xpt_action_name(ccb->ccb_h.func_code),
4254 xpt_async_string(async_code)));
4255 if (size > 0 && async_arg != NULL) {
4256 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4257 if (ccb->casync.async_arg_ptr == NULL) {
4258 xpt_print(path, "Can't allocate argument to send %s\n",
4259 xpt_async_string(async_code));
4260 xpt_free_path(ccb->ccb_h.path);
4264 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4265 ccb->casync.async_arg_size = size;
4266 } else if (size < 0) {
4267 ccb->casync.async_arg_ptr = async_arg;
4268 ccb->casync.async_arg_size = size;
4270 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4271 xpt_freeze_devq(path, 1);
4273 xpt_freeze_simq(path->bus->sim, 1);
4278 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4279 struct cam_et *target, struct cam_ed *device,
4284 * We only need to handle events for real devices.
4286 if (target->target_id == CAM_TARGET_WILDCARD
4287 || device->lun_id == CAM_LUN_WILDCARD)
4290 printf("%s called\n", __func__);
4294 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4296 struct cam_devq *devq;
4299 devq = dev->sim->devq;
4300 mtx_assert(&devq->send_mtx, MA_OWNED);
4301 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4302 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4303 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4304 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4305 /* Remove frozen device from sendq. */
4306 if (device_is_queued(dev))
4307 camq_remove(&devq->send_queue, dev->devq_entry.index);
4312 xpt_freeze_devq(struct cam_path *path, u_int count)
4314 struct cam_ed *dev = path->device;
4315 struct cam_devq *devq;
4318 devq = dev->sim->devq;
4319 mtx_lock(&devq->send_mtx);
4320 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4321 freeze = xpt_freeze_devq_device(dev, count);
4322 mtx_unlock(&devq->send_mtx);
4327 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4329 struct cam_devq *devq;
4333 mtx_lock(&devq->send_mtx);
4334 freeze = (devq->send_queue.qfrozen_cnt += count);
4335 mtx_unlock(&devq->send_mtx);
4340 xpt_release_devq_timeout(void *arg)
4343 struct cam_devq *devq;
4345 dev = (struct cam_ed *)arg;
4346 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4347 devq = dev->sim->devq;
4348 mtx_assert(&devq->send_mtx, MA_OWNED);
4349 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4354 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4357 struct cam_devq *devq;
4359 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4362 devq = dev->sim->devq;
4363 mtx_lock(&devq->send_mtx);
4364 if (xpt_release_devq_device(dev, count, run_queue))
4365 xpt_run_devq(dev->sim->devq);
4366 mtx_unlock(&devq->send_mtx);
4370 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4373 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4374 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4375 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4376 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4377 if (count > dev->ccbq.queue.qfrozen_cnt) {
4379 printf("xpt_release_devq(): requested %u > present %u\n",
4380 count, dev->ccbq.queue.qfrozen_cnt);
4382 count = dev->ccbq.queue.qfrozen_cnt;
4384 dev->ccbq.queue.qfrozen_cnt -= count;
4385 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4387 * No longer need to wait for a successful
4388 * command completion.
4390 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4392 * Remove any timeouts that might be scheduled
4393 * to release this queue.
4395 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4396 callout_stop(&dev->callout);
4397 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4400 * Now that we are unfrozen schedule the
4401 * device so any pending transactions are
4404 xpt_schedule_devq(dev->sim->devq, dev);
4411 xpt_release_simq(struct cam_sim *sim, int run_queue)
4413 struct cam_devq *devq;
4416 mtx_lock(&devq->send_mtx);
4417 if (devq->send_queue.qfrozen_cnt <= 0) {
4419 printf("xpt_release_simq: requested 1 > present %u\n",
4420 devq->send_queue.qfrozen_cnt);
4423 devq->send_queue.qfrozen_cnt--;
4424 if (devq->send_queue.qfrozen_cnt == 0) {
4426 * If there is a timeout scheduled to release this
4427 * sim queue, remove it. The queue frozen count is
4430 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4431 callout_stop(&sim->callout);
4432 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4436 * Now that we are unfrozen run the send queue.
4438 xpt_run_devq(sim->devq);
4441 mtx_unlock(&devq->send_mtx);
4445 * XXX Appears to be unused.
4448 xpt_release_simq_timeout(void *arg)
4450 struct cam_sim *sim;
4452 sim = (struct cam_sim *)arg;
4453 xpt_release_simq(sim, /* run_queue */ TRUE);
4457 xpt_done(union ccb *done_ccb)
4459 struct cam_doneq *queue;
4462 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4463 ("xpt_done: func= %#x %s status %#x\n",
4464 done_ccb->ccb_h.func_code,
4465 xpt_action_name(done_ccb->ccb_h.func_code),
4466 done_ccb->ccb_h.status));
4467 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4470 /* Store the time the ccb was in the sim */
4471 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4472 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4473 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4474 queue = &cam_doneqs[hash];
4475 mtx_lock(&queue->cam_doneq_mtx);
4476 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4477 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4478 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4479 mtx_unlock(&queue->cam_doneq_mtx);
4481 wakeup(&queue->cam_doneq);
4485 xpt_done_direct(union ccb *done_ccb)
4488 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4489 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4490 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4493 /* Store the time the ccb was in the sim */
4494 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4495 xpt_done_process(&done_ccb->ccb_h);
4503 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4508 xpt_alloc_ccb_nowait()
4512 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4517 xpt_free_ccb(union ccb *free_ccb)
4519 free(free_ccb, M_CAMCCB);
4524 /* Private XPT functions */
4527 * Get a CAM control block for the caller. Charge the structure to the device
4528 * referenced by the path. If we don't have sufficient resources to allocate
4529 * more ccbs, we return NULL.
4532 xpt_get_ccb_nowait(struct cam_periph *periph)
4536 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4537 if (new_ccb == NULL)
4539 periph->periph_allocated++;
4540 cam_ccbq_take_opening(&periph->path->device->ccbq);
4545 xpt_get_ccb(struct cam_periph *periph)
4549 cam_periph_unlock(periph);
4550 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4551 cam_periph_lock(periph);
4552 periph->periph_allocated++;
4553 cam_ccbq_take_opening(&periph->path->device->ccbq);
4558 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4560 struct ccb_hdr *ccb_h;
4562 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4563 cam_periph_assert(periph, MA_OWNED);
4564 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4565 ccb_h->pinfo.priority != priority) {
4566 if (priority < periph->immediate_priority) {
4567 periph->immediate_priority = priority;
4568 xpt_run_allocq(periph, 0);
4570 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4573 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4574 return ((union ccb *)ccb_h);
4578 xpt_acquire_bus(struct cam_eb *bus)
4587 xpt_release_bus(struct cam_eb *bus)
4591 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4592 if (--bus->refcount > 0) {
4596 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4597 xsoftc.bus_generation++;
4599 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4600 ("destroying bus, but target list is not empty"));
4601 cam_sim_release(bus->sim);
4602 mtx_destroy(&bus->eb_mtx);
4603 free(bus, M_CAMXPT);
4606 static struct cam_et *
4607 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4609 struct cam_et *cur_target, *target;
4611 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4612 mtx_assert(&bus->eb_mtx, MA_OWNED);
4613 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4618 TAILQ_INIT(&target->ed_entries);
4620 target->target_id = target_id;
4621 target->refcount = 1;
4622 target->generation = 0;
4623 target->luns = NULL;
4624 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4625 timevalclear(&target->last_reset);
4627 * Hold a reference to our parent bus so it
4628 * will not go away before we do.
4632 /* Insertion sort into our bus's target list */
4633 cur_target = TAILQ_FIRST(&bus->et_entries);
4634 while (cur_target != NULL && cur_target->target_id < target_id)
4635 cur_target = TAILQ_NEXT(cur_target, links);
4636 if (cur_target != NULL) {
4637 TAILQ_INSERT_BEFORE(cur_target, target, links);
4639 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4646 xpt_acquire_target(struct cam_et *target)
4648 struct cam_eb *bus = target->bus;
4650 mtx_lock(&bus->eb_mtx);
4652 mtx_unlock(&bus->eb_mtx);
4656 xpt_release_target(struct cam_et *target)
4658 struct cam_eb *bus = target->bus;
4660 mtx_lock(&bus->eb_mtx);
4661 if (--target->refcount > 0) {
4662 mtx_unlock(&bus->eb_mtx);
4665 TAILQ_REMOVE(&bus->et_entries, target, links);
4667 mtx_unlock(&bus->eb_mtx);
4668 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4669 ("destroying target, but device list is not empty"));
4670 xpt_release_bus(bus);
4671 mtx_destroy(&target->luns_mtx);
4673 free(target->luns, M_CAMXPT);
4674 free(target, M_CAMXPT);
4677 static struct cam_ed *
4678 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4681 struct cam_ed *device;
4683 device = xpt_alloc_device(bus, target, lun_id);
4687 device->mintags = 1;
4688 device->maxtags = 1;
4693 xpt_destroy_device(void *context, int pending)
4695 struct cam_ed *device = context;
4697 mtx_lock(&device->device_mtx);
4698 mtx_destroy(&device->device_mtx);
4699 free(device, M_CAMDEV);
4703 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4705 struct cam_ed *cur_device, *device;
4706 struct cam_devq *devq;
4709 mtx_assert(&bus->eb_mtx, MA_OWNED);
4710 /* Make space for us in the device queue on our bus */
4711 devq = bus->sim->devq;
4712 mtx_lock(&devq->send_mtx);
4713 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4714 mtx_unlock(&devq->send_mtx);
4715 if (status != CAM_REQ_CMP)
4718 device = (struct cam_ed *)malloc(sizeof(*device),
4719 M_CAMDEV, M_NOWAIT|M_ZERO);
4723 cam_init_pinfo(&device->devq_entry);
4724 device->target = target;
4725 device->lun_id = lun_id;
4726 device->sim = bus->sim;
4727 if (cam_ccbq_init(&device->ccbq,
4728 bus->sim->max_dev_openings) != 0) {
4729 free(device, M_CAMDEV);
4732 SLIST_INIT(&device->asyncs);
4733 SLIST_INIT(&device->periphs);
4734 device->generation = 0;
4735 device->flags = CAM_DEV_UNCONFIGURED;
4736 device->tag_delay_count = 0;
4737 device->tag_saved_openings = 0;
4738 device->refcount = 1;
4739 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4740 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4741 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4743 * Hold a reference to our parent bus so it
4744 * will not go away before we do.
4748 cur_device = TAILQ_FIRST(&target->ed_entries);
4749 while (cur_device != NULL && cur_device->lun_id < lun_id)
4750 cur_device = TAILQ_NEXT(cur_device, links);
4751 if (cur_device != NULL)
4752 TAILQ_INSERT_BEFORE(cur_device, device, links);
4754 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4755 target->generation++;
4760 xpt_acquire_device(struct cam_ed *device)
4762 struct cam_eb *bus = device->target->bus;
4764 mtx_lock(&bus->eb_mtx);
4766 mtx_unlock(&bus->eb_mtx);
4770 xpt_release_device(struct cam_ed *device)
4772 struct cam_eb *bus = device->target->bus;
4773 struct cam_devq *devq;
4775 mtx_lock(&bus->eb_mtx);
4776 if (--device->refcount > 0) {
4777 mtx_unlock(&bus->eb_mtx);
4781 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4782 device->target->generation++;
4783 mtx_unlock(&bus->eb_mtx);
4785 /* Release our slot in the devq */
4786 devq = bus->sim->devq;
4787 mtx_lock(&devq->send_mtx);
4788 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4789 mtx_unlock(&devq->send_mtx);
4791 KASSERT(SLIST_EMPTY(&device->periphs),
4792 ("destroying device, but periphs list is not empty"));
4793 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4794 ("destroying device while still queued for ccbs"));
4796 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4797 callout_stop(&device->callout);
4799 xpt_release_target(device->target);
4801 cam_ccbq_fini(&device->ccbq);
4803 * Free allocated memory. free(9) does nothing if the
4804 * supplied pointer is NULL, so it is safe to call without
4807 free(device->supported_vpds, M_CAMXPT);
4808 free(device->device_id, M_CAMXPT);
4809 free(device->ext_inq, M_CAMXPT);
4810 free(device->physpath, M_CAMXPT);
4811 free(device->rcap_buf, M_CAMXPT);
4812 free(device->serial_num, M_CAMXPT);
4813 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4817 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4823 mtx_lock(&dev->sim->devq->send_mtx);
4824 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4825 mtx_unlock(&dev->sim->devq->send_mtx);
4826 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4827 || (dev->inq_flags & SID_CmdQue) != 0)
4828 dev->tag_saved_openings = newopenings;
4832 static struct cam_eb *
4833 xpt_find_bus(path_id_t path_id)
4838 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4840 bus = TAILQ_NEXT(bus, links)) {
4841 if (bus->path_id == path_id) {
4850 static struct cam_et *
4851 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4853 struct cam_et *target;
4855 mtx_assert(&bus->eb_mtx, MA_OWNED);
4856 for (target = TAILQ_FIRST(&bus->et_entries);
4858 target = TAILQ_NEXT(target, links)) {
4859 if (target->target_id == target_id) {
4867 static struct cam_ed *
4868 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4870 struct cam_ed *device;
4872 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4873 for (device = TAILQ_FIRST(&target->ed_entries);
4875 device = TAILQ_NEXT(device, links)) {
4876 if (device->lun_id == lun_id) {
4885 xpt_start_tags(struct cam_path *path)
4887 struct ccb_relsim crs;
4888 struct cam_ed *device;
4889 struct cam_sim *sim;
4892 device = path->device;
4893 sim = path->bus->sim;
4894 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4895 xpt_freeze_devq(path, /*count*/1);
4896 device->inq_flags |= SID_CmdQue;
4897 if (device->tag_saved_openings != 0)
4898 newopenings = device->tag_saved_openings;
4900 newopenings = min(device->maxtags,
4901 sim->max_tagged_dev_openings);
4902 xpt_dev_ccbq_resize(path, newopenings);
4903 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4904 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4905 crs.ccb_h.func_code = XPT_REL_SIMQ;
4906 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4908 = crs.release_timeout
4911 xpt_action((union ccb *)&crs);
4915 xpt_stop_tags(struct cam_path *path)
4917 struct ccb_relsim crs;
4918 struct cam_ed *device;
4919 struct cam_sim *sim;
4921 device = path->device;
4922 sim = path->bus->sim;
4923 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4924 device->tag_delay_count = 0;
4925 xpt_freeze_devq(path, /*count*/1);
4926 device->inq_flags &= ~SID_CmdQue;
4927 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4928 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4929 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4930 crs.ccb_h.func_code = XPT_REL_SIMQ;
4931 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4933 = crs.release_timeout
4936 xpt_action((union ccb *)&crs);
4940 xpt_boot_delay(void *arg)
4947 xpt_config(void *arg)
4950 * Now that interrupts are enabled, go find our devices
4952 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4953 printf("xpt_config: failed to create taskqueue thread.\n");
4955 /* Setup debugging path */
4956 if (cam_dflags != CAM_DEBUG_NONE) {
4957 if (xpt_create_path(&cam_dpath, NULL,
4958 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4959 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4960 printf("xpt_config: xpt_create_path() failed for debug"
4961 " target %d:%d:%d, debugging disabled\n",
4962 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4963 cam_dflags = CAM_DEBUG_NONE;
4968 periphdriver_init(1);
4970 callout_init(&xsoftc.boot_callout, 1);
4971 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
4972 xpt_boot_delay, NULL, 0);
4973 /* Fire up rescan thread. */
4974 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4975 "cam", "scanner")) {
4976 printf("xpt_config: failed to create rescan thread.\n");
4984 xsoftc.buses_to_config++;
4989 xpt_release_boot(void)
4992 xsoftc.buses_to_config--;
4993 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4994 struct xpt_task *task;
4996 xsoftc.buses_config_done = 1;
4998 /* Call manually because we don't have any busses */
4999 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5001 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5002 taskqueue_enqueue(taskqueue_thread, &task->task);
5009 * If the given device only has one peripheral attached to it, and if that
5010 * peripheral is the passthrough driver, announce it. This insures that the
5011 * user sees some sort of announcement for every peripheral in their system.
5014 xptpassannouncefunc(struct cam_ed *device, void *arg)
5016 struct cam_periph *periph;
5019 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5020 periph = SLIST_NEXT(periph, periph_links), i++);
5022 periph = SLIST_FIRST(&device->periphs);
5024 && (strncmp(periph->periph_name, "pass", 4) == 0))
5025 xpt_announce_periph(periph, NULL);
5031 xpt_finishconfig_task(void *context, int pending)
5034 periphdriver_init(2);
5036 * Check for devices with no "standard" peripheral driver
5037 * attached. For any devices like that, announce the
5038 * passthrough driver so the user will see something.
5041 xpt_for_all_devices(xptpassannouncefunc, NULL);
5043 /* Release our hook so that the boot can continue. */
5044 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5045 free(xsoftc.xpt_config_hook, M_CAMXPT);
5046 xsoftc.xpt_config_hook = NULL;
5048 free(context, M_CAMXPT);
5052 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5053 struct cam_path *path)
5055 struct ccb_setasync csa;
5060 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5061 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5062 if (status != CAM_REQ_CMP)
5064 xpt_path_lock(path);
5068 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5069 csa.ccb_h.func_code = XPT_SASYNC_CB;
5070 csa.event_enable = event;
5071 csa.callback = cbfunc;
5072 csa.callback_arg = cbarg;
5073 xpt_action((union ccb *)&csa);
5074 status = csa.ccb_h.status;
5076 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5077 ("xpt_register_async: func %p\n", cbfunc));
5080 xpt_path_unlock(path);
5081 xpt_free_path(path);
5084 if ((status == CAM_REQ_CMP) &&
5085 (csa.event_enable & AC_FOUND_DEVICE)) {
5087 * Get this peripheral up to date with all
5088 * the currently existing devices.
5090 xpt_for_all_devices(xptsetasyncfunc, &csa);
5092 if ((status == CAM_REQ_CMP) &&
5093 (csa.event_enable & AC_PATH_REGISTERED)) {
5095 * Get this peripheral up to date with all
5096 * the currently existing busses.
5098 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5105 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5107 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5109 switch (work_ccb->ccb_h.func_code) {
5110 /* Common cases first */
5111 case XPT_PATH_INQ: /* Path routing inquiry */
5113 struct ccb_pathinq *cpi;
5115 cpi = &work_ccb->cpi;
5116 cpi->version_num = 1; /* XXX??? */
5117 cpi->hba_inquiry = 0;
5118 cpi->target_sprt = 0;
5120 cpi->hba_eng_cnt = 0;
5121 cpi->max_target = 0;
5123 cpi->initiator_id = 0;
5124 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5125 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5126 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5127 cpi->unit_number = sim->unit_number;
5128 cpi->bus_id = sim->bus_id;
5129 cpi->base_transfer_speed = 0;
5130 cpi->protocol = PROTO_UNSPECIFIED;
5131 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5132 cpi->transport = XPORT_UNSPECIFIED;
5133 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5134 cpi->ccb_h.status = CAM_REQ_CMP;
5139 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5146 * The xpt as a "controller" has no interrupt sources, so polling
5150 xptpoll(struct cam_sim *sim)
5155 xpt_lock_buses(void)
5157 mtx_lock(&xsoftc.xpt_topo_lock);
5161 xpt_unlock_buses(void)
5163 mtx_unlock(&xsoftc.xpt_topo_lock);
5167 xpt_path_mtx(struct cam_path *path)
5170 return (&path->device->device_mtx);
5174 xpt_done_process(struct ccb_hdr *ccb_h)
5176 struct cam_sim *sim;
5177 struct cam_devq *devq;
5178 struct mtx *mtx = NULL;
5180 if (ccb_h->flags & CAM_HIGH_POWER) {
5181 struct highpowerlist *hphead;
5182 struct cam_ed *device;
5184 mtx_lock(&xsoftc.xpt_highpower_lock);
5185 hphead = &xsoftc.highpowerq;
5187 device = STAILQ_FIRST(hphead);
5190 * Increment the count since this command is done.
5192 xsoftc.num_highpower++;
5195 * Any high powered commands queued up?
5197 if (device != NULL) {
5199 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5200 mtx_unlock(&xsoftc.xpt_highpower_lock);
5202 mtx_lock(&device->sim->devq->send_mtx);
5203 xpt_release_devq_device(device,
5204 /*count*/1, /*runqueue*/TRUE);
5205 mtx_unlock(&device->sim->devq->send_mtx);
5207 mtx_unlock(&xsoftc.xpt_highpower_lock);
5210 sim = ccb_h->path->bus->sim;
5212 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5213 xpt_release_simq(sim, /*run_queue*/FALSE);
5214 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5217 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5218 && (ccb_h->status & CAM_DEV_QFRZN)) {
5219 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5220 ccb_h->status &= ~CAM_DEV_QFRZN;
5224 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5225 struct cam_ed *dev = ccb_h->path->device;
5227 mtx_lock(&devq->send_mtx);
5228 devq->send_active--;
5229 devq->send_openings++;
5230 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5232 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5233 && (dev->ccbq.dev_active == 0))) {
5234 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5235 xpt_release_devq_device(dev, /*count*/1,
5236 /*run_queue*/FALSE);
5239 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5240 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5241 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5242 xpt_release_devq_device(dev, /*count*/1,
5243 /*run_queue*/FALSE);
5246 if (!device_is_queued(dev))
5247 (void)xpt_schedule_devq(devq, dev);
5249 mtx_unlock(&devq->send_mtx);
5251 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5252 mtx = xpt_path_mtx(ccb_h->path);
5255 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5256 && (--dev->tag_delay_count == 0))
5257 xpt_start_tags(ccb_h->path);
5261 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5263 mtx = xpt_path_mtx(ccb_h->path);
5273 /* Call the peripheral driver's callback */
5274 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5275 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5281 xpt_done_td(void *arg)
5283 struct cam_doneq *queue = arg;
5284 struct ccb_hdr *ccb_h;
5285 STAILQ_HEAD(, ccb_hdr) doneq;
5287 STAILQ_INIT(&doneq);
5288 mtx_lock(&queue->cam_doneq_mtx);
5290 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5291 queue->cam_doneq_sleep = 1;
5292 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5294 queue->cam_doneq_sleep = 0;
5296 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5297 mtx_unlock(&queue->cam_doneq_mtx);
5299 THREAD_NO_SLEEPING();
5300 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5301 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5302 xpt_done_process(ccb_h);
5304 THREAD_SLEEPING_OK();
5306 mtx_lock(&queue->cam_doneq_mtx);
5311 camisr_runqueue(void)
5313 struct ccb_hdr *ccb_h;
5314 struct cam_doneq *queue;
5317 /* Process global queues. */
5318 for (i = 0; i < cam_num_doneqs; i++) {
5319 queue = &cam_doneqs[i];
5320 mtx_lock(&queue->cam_doneq_mtx);
5321 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5322 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5323 mtx_unlock(&queue->cam_doneq_mtx);
5324 xpt_done_process(ccb_h);
5325 mtx_lock(&queue->cam_doneq_mtx);
5327 mtx_unlock(&queue->cam_doneq_mtx);
5337 static struct kv map[] = {
5338 { XPT_NOOP, "XPT_NOOP" },
5339 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5340 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5341 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5342 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5343 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5344 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5345 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5346 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5347 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5348 { XPT_DEBUG, "XPT_DEBUG" },
5349 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5350 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5351 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5352 { XPT_ASYNC, "XPT_ASYNC" },
5353 { XPT_ABORT, "XPT_ABORT" },
5354 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5355 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5356 { XPT_TERM_IO, "XPT_TERM_IO" },
5357 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5358 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5359 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5360 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5361 { XPT_ATA_IO, "XPT_ATA_IO" },
5362 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5363 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5364 { XPT_NVME_IO, "XPT_NVME_IO" },
5365 { XPT_SMP_IO, "XPT_SMP_IO" },
5366 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5367 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5368 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5369 { XPT_EN_LUN, "XPT_EN_LUN" },
5370 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5371 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5372 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5373 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5374 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5375 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5376 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5381 xpt_action_name(uint32_t action)
5383 static char buffer[32]; /* Only for unknown messages -- racy */
5384 struct kv *walker = map;
5386 while (walker->name != NULL) {
5387 if (walker->v == action)
5388 return (walker->name);
5392 snprintf(buffer, sizeof(buffer), "%#x", action);