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;
1109 cdai.flags = CDAI_FLAG_NONE;
1112 if (!strcmp(attr, "GEOM::ident"))
1113 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1114 else if (!strcmp(attr, "GEOM::physpath"))
1115 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1116 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1117 strcmp(attr, "GEOM::lunname") == 0) {
1118 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1119 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1123 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1124 if (cdai.buf == NULL) {
1128 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1129 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1130 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1131 if (cdai.provsiz == 0)
1133 if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
1134 if (strcmp(attr, "GEOM::lunid") == 0) {
1135 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1136 cdai.provsiz, scsi_devid_is_lun_naa);
1138 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1139 cdai.provsiz, scsi_devid_is_lun_eui64);
1141 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1142 cdai.provsiz, scsi_devid_is_lun_uuid);
1144 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1145 cdai.provsiz, scsi_devid_is_lun_md5);
1149 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1150 cdai.provsiz, scsi_devid_is_lun_t10);
1152 idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1153 cdai.provsiz, scsi_devid_is_lun_name);
1157 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII) {
1158 if (idd->length < len) {
1159 for (l = 0; l < idd->length; l++)
1160 buf[l] = idd->identifier[l] ?
1161 idd->identifier[l] : ' ';
1165 } else if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1166 l = strnlen(idd->identifier, idd->length);
1168 bcopy(idd->identifier, buf, l);
1172 } else if ((idd->id_type & SVPD_ID_TYPE_MASK) == SVPD_ID_TYPE_UUID
1173 && idd->identifier[0] == 0x10) {
1174 if ((idd->length - 2) * 2 + 4 < len) {
1175 for (l = 2, o = 0; l < idd->length; l++) {
1176 if (l == 6 || l == 8 || l == 10 || l == 12)
1177 o += sprintf(buf + o, "-");
1178 o += sprintf(buf + o, "%02x",
1179 idd->identifier[l]);
1184 if (idd->length * 2 < len) {
1185 for (l = 0; l < idd->length; l++)
1186 sprintf(buf + l * 2, "%02x",
1187 idd->identifier[l]);
1193 if (strlcpy(buf, cdai.buf, len) >= len)
1198 if (cdai.buf != NULL)
1199 free(cdai.buf, M_CAMXPT);
1203 static dev_match_ret
1204 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1207 dev_match_ret retval;
1210 retval = DM_RET_NONE;
1213 * If we aren't given something to match against, that's an error.
1216 return(DM_RET_ERROR);
1219 * If there are no match entries, then this bus matches no
1222 if ((patterns == NULL) || (num_patterns == 0))
1223 return(DM_RET_DESCEND | DM_RET_COPY);
1225 for (i = 0; i < num_patterns; i++) {
1226 struct bus_match_pattern *cur_pattern;
1229 * If the pattern in question isn't for a bus node, we
1230 * aren't interested. However, we do indicate to the
1231 * calling routine that we should continue descending the
1232 * tree, since the user wants to match against lower-level
1235 if (patterns[i].type != DEV_MATCH_BUS) {
1236 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1237 retval |= DM_RET_DESCEND;
1241 cur_pattern = &patterns[i].pattern.bus_pattern;
1244 * If they want to match any bus node, we give them any
1247 if (cur_pattern->flags == BUS_MATCH_ANY) {
1248 /* set the copy flag */
1249 retval |= DM_RET_COPY;
1252 * If we've already decided on an action, go ahead
1255 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1260 * Not sure why someone would do this...
1262 if (cur_pattern->flags == BUS_MATCH_NONE)
1265 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1266 && (cur_pattern->path_id != bus->path_id))
1269 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1270 && (cur_pattern->bus_id != bus->sim->bus_id))
1273 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1274 && (cur_pattern->unit_number != bus->sim->unit_number))
1277 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1278 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1283 * If we get to this point, the user definitely wants
1284 * information on this bus. So tell the caller to copy the
1287 retval |= DM_RET_COPY;
1290 * If the return action has been set to descend, then we
1291 * know that we've already seen a non-bus matching
1292 * expression, therefore we need to further descend the tree.
1293 * This won't change by continuing around the loop, so we
1294 * go ahead and return. If we haven't seen a non-bus
1295 * matching expression, we keep going around the loop until
1296 * we exhaust the matching expressions. We'll set the stop
1297 * flag once we fall out of the loop.
1299 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1304 * If the return action hasn't been set to descend yet, that means
1305 * we haven't seen anything other than bus matching patterns. So
1306 * tell the caller to stop descending the tree -- the user doesn't
1307 * want to match against lower level tree elements.
1309 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1310 retval |= DM_RET_STOP;
1315 static dev_match_ret
1316 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1317 struct cam_ed *device)
1319 dev_match_ret retval;
1322 retval = DM_RET_NONE;
1325 * If we aren't given something to match against, that's an error.
1328 return(DM_RET_ERROR);
1331 * If there are no match entries, then this device matches no
1334 if ((patterns == NULL) || (num_patterns == 0))
1335 return(DM_RET_DESCEND | DM_RET_COPY);
1337 for (i = 0; i < num_patterns; i++) {
1338 struct device_match_pattern *cur_pattern;
1339 struct scsi_vpd_device_id *device_id_page;
1342 * If the pattern in question isn't for a device node, we
1343 * aren't interested.
1345 if (patterns[i].type != DEV_MATCH_DEVICE) {
1346 if ((patterns[i].type == DEV_MATCH_PERIPH)
1347 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1348 retval |= DM_RET_DESCEND;
1352 cur_pattern = &patterns[i].pattern.device_pattern;
1354 /* Error out if mutually exclusive options are specified. */
1355 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1356 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1357 return(DM_RET_ERROR);
1360 * If they want to match any device node, we give them any
1363 if (cur_pattern->flags == DEV_MATCH_ANY)
1367 * Not sure why someone would do this...
1369 if (cur_pattern->flags == DEV_MATCH_NONE)
1372 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1373 && (cur_pattern->path_id != device->target->bus->path_id))
1376 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1377 && (cur_pattern->target_id != device->target->target_id))
1380 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1381 && (cur_pattern->target_lun != device->lun_id))
1384 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1385 && (cam_quirkmatch((caddr_t)&device->inq_data,
1386 (caddr_t)&cur_pattern->data.inq_pat,
1387 1, sizeof(cur_pattern->data.inq_pat),
1388 scsi_static_inquiry_match) == NULL))
1391 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1392 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1393 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1394 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1395 device->device_id_len
1396 - SVPD_DEVICE_ID_HDR_LEN,
1397 cur_pattern->data.devid_pat.id,
1398 cur_pattern->data.devid_pat.id_len) != 0))
1403 * If we get to this point, the user definitely wants
1404 * information on this device. So tell the caller to copy
1407 retval |= DM_RET_COPY;
1410 * If the return action has been set to descend, then we
1411 * know that we've already seen a peripheral matching
1412 * expression, therefore we need to further descend the tree.
1413 * This won't change by continuing around the loop, so we
1414 * go ahead and return. If we haven't seen a peripheral
1415 * matching expression, we keep going around the loop until
1416 * we exhaust the matching expressions. We'll set the stop
1417 * flag once we fall out of the loop.
1419 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1424 * If the return action hasn't been set to descend yet, that means
1425 * we haven't seen any peripheral matching patterns. So tell the
1426 * caller to stop descending the tree -- the user doesn't want to
1427 * match against lower level tree elements.
1429 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1430 retval |= DM_RET_STOP;
1436 * Match a single peripheral against any number of match patterns.
1438 static dev_match_ret
1439 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1440 struct cam_periph *periph)
1442 dev_match_ret retval;
1446 * If we aren't given something to match against, that's an error.
1449 return(DM_RET_ERROR);
1452 * If there are no match entries, then this peripheral matches no
1455 if ((patterns == NULL) || (num_patterns == 0))
1456 return(DM_RET_STOP | DM_RET_COPY);
1459 * There aren't any nodes below a peripheral node, so there's no
1460 * reason to descend the tree any further.
1462 retval = DM_RET_STOP;
1464 for (i = 0; i < num_patterns; i++) {
1465 struct periph_match_pattern *cur_pattern;
1468 * If the pattern in question isn't for a peripheral, we
1469 * aren't interested.
1471 if (patterns[i].type != DEV_MATCH_PERIPH)
1474 cur_pattern = &patterns[i].pattern.periph_pattern;
1477 * If they want to match on anything, then we will do so.
1479 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1480 /* set the copy flag */
1481 retval |= DM_RET_COPY;
1484 * We've already set the return action to stop,
1485 * since there are no nodes below peripherals in
1492 * Not sure why someone would do this...
1494 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1497 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1498 && (cur_pattern->path_id != periph->path->bus->path_id))
1502 * For the target and lun id's, we have to make sure the
1503 * target and lun pointers aren't NULL. The xpt peripheral
1504 * has a wildcard target and device.
1506 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1507 && ((periph->path->target == NULL)
1508 ||(cur_pattern->target_id != periph->path->target->target_id)))
1511 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1512 && ((periph->path->device == NULL)
1513 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1516 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1517 && (cur_pattern->unit_number != periph->unit_number))
1520 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1521 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1526 * If we get to this point, the user definitely wants
1527 * information on this peripheral. So tell the caller to
1528 * copy the data out.
1530 retval |= DM_RET_COPY;
1533 * The return action has already been set to stop, since
1534 * peripherals don't have any nodes below them in the EDT.
1540 * If we get to this point, the peripheral that was passed in
1541 * doesn't match any of the patterns.
1547 xptedtbusfunc(struct cam_eb *bus, void *arg)
1549 struct ccb_dev_match *cdm;
1550 struct cam_et *target;
1551 dev_match_ret retval;
1553 cdm = (struct ccb_dev_match *)arg;
1556 * If our position is for something deeper in the tree, that means
1557 * that we've already seen this node. So, we keep going down.
1559 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1560 && (cdm->pos.cookie.bus == bus)
1561 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1562 && (cdm->pos.cookie.target != NULL))
1563 retval = DM_RET_DESCEND;
1565 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1568 * If we got an error, bail out of the search.
1570 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1571 cdm->status = CAM_DEV_MATCH_ERROR;
1576 * If the copy flag is set, copy this bus out.
1578 if (retval & DM_RET_COPY) {
1581 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1582 sizeof(struct dev_match_result));
1585 * If we don't have enough space to put in another
1586 * match result, save our position and tell the
1587 * user there are more devices to check.
1589 if (spaceleft < sizeof(struct dev_match_result)) {
1590 bzero(&cdm->pos, sizeof(cdm->pos));
1591 cdm->pos.position_type =
1592 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1594 cdm->pos.cookie.bus = bus;
1595 cdm->pos.generations[CAM_BUS_GENERATION]=
1596 xsoftc.bus_generation;
1597 cdm->status = CAM_DEV_MATCH_MORE;
1600 j = cdm->num_matches;
1602 cdm->matches[j].type = DEV_MATCH_BUS;
1603 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1604 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1605 cdm->matches[j].result.bus_result.unit_number =
1606 bus->sim->unit_number;
1607 strncpy(cdm->matches[j].result.bus_result.dev_name,
1608 bus->sim->sim_name, DEV_IDLEN);
1612 * If the user is only interested in busses, there's no
1613 * reason to descend to the next level in the tree.
1615 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1619 * If there is a target generation recorded, check it to
1620 * make sure the target list hasn't changed.
1622 mtx_lock(&bus->eb_mtx);
1623 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1624 && (cdm->pos.cookie.bus == bus)
1625 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1626 && (cdm->pos.cookie.target != NULL)) {
1627 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1629 mtx_unlock(&bus->eb_mtx);
1630 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1633 target = (struct cam_et *)cdm->pos.cookie.target;
1637 mtx_unlock(&bus->eb_mtx);
1639 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1643 xptedttargetfunc(struct cam_et *target, void *arg)
1645 struct ccb_dev_match *cdm;
1647 struct cam_ed *device;
1649 cdm = (struct ccb_dev_match *)arg;
1653 * If there is a device list generation recorded, check it to
1654 * make sure the device list hasn't changed.
1656 mtx_lock(&bus->eb_mtx);
1657 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1658 && (cdm->pos.cookie.bus == bus)
1659 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1660 && (cdm->pos.cookie.target == target)
1661 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1662 && (cdm->pos.cookie.device != NULL)) {
1663 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1664 target->generation) {
1665 mtx_unlock(&bus->eb_mtx);
1666 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1669 device = (struct cam_ed *)cdm->pos.cookie.device;
1673 mtx_unlock(&bus->eb_mtx);
1675 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1679 xptedtdevicefunc(struct cam_ed *device, void *arg)
1682 struct cam_periph *periph;
1683 struct ccb_dev_match *cdm;
1684 dev_match_ret retval;
1686 cdm = (struct ccb_dev_match *)arg;
1687 bus = device->target->bus;
1690 * If our position is for something deeper in the tree, that means
1691 * that we've already seen this node. So, we keep going down.
1693 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1694 && (cdm->pos.cookie.device == device)
1695 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1696 && (cdm->pos.cookie.periph != NULL))
1697 retval = DM_RET_DESCEND;
1699 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1702 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1703 cdm->status = CAM_DEV_MATCH_ERROR;
1708 * If the copy flag is set, copy this device out.
1710 if (retval & DM_RET_COPY) {
1713 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1714 sizeof(struct dev_match_result));
1717 * If we don't have enough space to put in another
1718 * match result, save our position and tell the
1719 * user there are more devices to check.
1721 if (spaceleft < sizeof(struct dev_match_result)) {
1722 bzero(&cdm->pos, sizeof(cdm->pos));
1723 cdm->pos.position_type =
1724 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1725 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1727 cdm->pos.cookie.bus = device->target->bus;
1728 cdm->pos.generations[CAM_BUS_GENERATION]=
1729 xsoftc.bus_generation;
1730 cdm->pos.cookie.target = device->target;
1731 cdm->pos.generations[CAM_TARGET_GENERATION] =
1732 device->target->bus->generation;
1733 cdm->pos.cookie.device = device;
1734 cdm->pos.generations[CAM_DEV_GENERATION] =
1735 device->target->generation;
1736 cdm->status = CAM_DEV_MATCH_MORE;
1739 j = cdm->num_matches;
1741 cdm->matches[j].type = DEV_MATCH_DEVICE;
1742 cdm->matches[j].result.device_result.path_id =
1743 device->target->bus->path_id;
1744 cdm->matches[j].result.device_result.target_id =
1745 device->target->target_id;
1746 cdm->matches[j].result.device_result.target_lun =
1748 cdm->matches[j].result.device_result.protocol =
1750 bcopy(&device->inq_data,
1751 &cdm->matches[j].result.device_result.inq_data,
1752 sizeof(struct scsi_inquiry_data));
1753 bcopy(&device->ident_data,
1754 &cdm->matches[j].result.device_result.ident_data,
1755 sizeof(struct ata_params));
1757 /* Let the user know whether this device is unconfigured */
1758 if (device->flags & CAM_DEV_UNCONFIGURED)
1759 cdm->matches[j].result.device_result.flags =
1760 DEV_RESULT_UNCONFIGURED;
1762 cdm->matches[j].result.device_result.flags =
1767 * If the user isn't interested in peripherals, don't descend
1768 * the tree any further.
1770 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1774 * If there is a peripheral list generation recorded, make sure
1775 * it hasn't changed.
1778 mtx_lock(&bus->eb_mtx);
1779 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1780 && (cdm->pos.cookie.bus == bus)
1781 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1782 && (cdm->pos.cookie.target == device->target)
1783 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1784 && (cdm->pos.cookie.device == device)
1785 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1786 && (cdm->pos.cookie.periph != NULL)) {
1787 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1788 device->generation) {
1789 mtx_unlock(&bus->eb_mtx);
1791 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1794 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1798 mtx_unlock(&bus->eb_mtx);
1801 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1805 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1807 struct ccb_dev_match *cdm;
1808 dev_match_ret retval;
1810 cdm = (struct ccb_dev_match *)arg;
1812 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1814 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1815 cdm->status = CAM_DEV_MATCH_ERROR;
1820 * If the copy flag is set, copy this peripheral out.
1822 if (retval & DM_RET_COPY) {
1825 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1826 sizeof(struct dev_match_result));
1829 * If we don't have enough space to put in another
1830 * match result, save our position and tell the
1831 * user there are more devices to check.
1833 if (spaceleft < sizeof(struct dev_match_result)) {
1834 bzero(&cdm->pos, sizeof(cdm->pos));
1835 cdm->pos.position_type =
1836 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1837 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1840 cdm->pos.cookie.bus = periph->path->bus;
1841 cdm->pos.generations[CAM_BUS_GENERATION]=
1842 xsoftc.bus_generation;
1843 cdm->pos.cookie.target = periph->path->target;
1844 cdm->pos.generations[CAM_TARGET_GENERATION] =
1845 periph->path->bus->generation;
1846 cdm->pos.cookie.device = periph->path->device;
1847 cdm->pos.generations[CAM_DEV_GENERATION] =
1848 periph->path->target->generation;
1849 cdm->pos.cookie.periph = periph;
1850 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1851 periph->path->device->generation;
1852 cdm->status = CAM_DEV_MATCH_MORE;
1856 j = cdm->num_matches;
1858 cdm->matches[j].type = DEV_MATCH_PERIPH;
1859 cdm->matches[j].result.periph_result.path_id =
1860 periph->path->bus->path_id;
1861 cdm->matches[j].result.periph_result.target_id =
1862 periph->path->target->target_id;
1863 cdm->matches[j].result.periph_result.target_lun =
1864 periph->path->device->lun_id;
1865 cdm->matches[j].result.periph_result.unit_number =
1866 periph->unit_number;
1867 strncpy(cdm->matches[j].result.periph_result.periph_name,
1868 periph->periph_name, DEV_IDLEN);
1875 xptedtmatch(struct ccb_dev_match *cdm)
1880 cdm->num_matches = 0;
1883 * Check the bus list generation. If it has changed, the user
1884 * needs to reset everything and start over.
1887 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1888 && (cdm->pos.cookie.bus != NULL)) {
1889 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1890 xsoftc.bus_generation) {
1892 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1895 bus = (struct cam_eb *)cdm->pos.cookie.bus;
1901 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1904 * If we get back 0, that means that we had to stop before fully
1905 * traversing the EDT. It also means that one of the subroutines
1906 * has set the status field to the proper value. If we get back 1,
1907 * we've fully traversed the EDT and copied out any matching entries.
1910 cdm->status = CAM_DEV_MATCH_LAST;
1916 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1918 struct cam_periph *periph;
1919 struct ccb_dev_match *cdm;
1921 cdm = (struct ccb_dev_match *)arg;
1924 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1925 && (cdm->pos.cookie.pdrv == pdrv)
1926 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1927 && (cdm->pos.cookie.periph != NULL)) {
1928 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1929 (*pdrv)->generation) {
1931 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1934 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1940 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
1944 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1946 struct ccb_dev_match *cdm;
1947 dev_match_ret retval;
1949 cdm = (struct ccb_dev_match *)arg;
1951 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1953 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1954 cdm->status = CAM_DEV_MATCH_ERROR;
1959 * If the copy flag is set, copy this peripheral out.
1961 if (retval & DM_RET_COPY) {
1964 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1965 sizeof(struct dev_match_result));
1968 * If we don't have enough space to put in another
1969 * match result, save our position and tell the
1970 * user there are more devices to check.
1972 if (spaceleft < sizeof(struct dev_match_result)) {
1973 struct periph_driver **pdrv;
1976 bzero(&cdm->pos, sizeof(cdm->pos));
1977 cdm->pos.position_type =
1978 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1982 * This may look a bit non-sensical, but it is
1983 * actually quite logical. There are very few
1984 * peripheral drivers, and bloating every peripheral
1985 * structure with a pointer back to its parent
1986 * peripheral driver linker set entry would cost
1987 * more in the long run than doing this quick lookup.
1989 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1990 if (strcmp((*pdrv)->driver_name,
1991 periph->periph_name) == 0)
1995 if (*pdrv == NULL) {
1996 cdm->status = CAM_DEV_MATCH_ERROR;
2000 cdm->pos.cookie.pdrv = pdrv;
2002 * The periph generation slot does double duty, as
2003 * does the periph pointer slot. They are used for
2004 * both edt and pdrv lookups and positioning.
2006 cdm->pos.cookie.periph = periph;
2007 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2008 (*pdrv)->generation;
2009 cdm->status = CAM_DEV_MATCH_MORE;
2013 j = cdm->num_matches;
2015 cdm->matches[j].type = DEV_MATCH_PERIPH;
2016 cdm->matches[j].result.periph_result.path_id =
2017 periph->path->bus->path_id;
2020 * The transport layer peripheral doesn't have a target or
2023 if (periph->path->target)
2024 cdm->matches[j].result.periph_result.target_id =
2025 periph->path->target->target_id;
2027 cdm->matches[j].result.periph_result.target_id =
2028 CAM_TARGET_WILDCARD;
2030 if (periph->path->device)
2031 cdm->matches[j].result.periph_result.target_lun =
2032 periph->path->device->lun_id;
2034 cdm->matches[j].result.periph_result.target_lun =
2037 cdm->matches[j].result.periph_result.unit_number =
2038 periph->unit_number;
2039 strncpy(cdm->matches[j].result.periph_result.periph_name,
2040 periph->periph_name, DEV_IDLEN);
2047 xptperiphlistmatch(struct ccb_dev_match *cdm)
2051 cdm->num_matches = 0;
2054 * At this point in the edt traversal function, we check the bus
2055 * list generation to make sure that no busses have been added or
2056 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2057 * For the peripheral driver list traversal function, however, we
2058 * don't have to worry about new peripheral driver types coming or
2059 * going; they're in a linker set, and therefore can't change
2060 * without a recompile.
2063 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2064 && (cdm->pos.cookie.pdrv != NULL))
2065 ret = xptpdrvtraverse(
2066 (struct periph_driver **)cdm->pos.cookie.pdrv,
2067 xptplistpdrvfunc, cdm);
2069 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2072 * If we get back 0, that means that we had to stop before fully
2073 * traversing the peripheral driver tree. It also means that one of
2074 * the subroutines has set the status field to the proper value. If
2075 * we get back 1, we've fully traversed the EDT and copied out any
2079 cdm->status = CAM_DEV_MATCH_LAST;
2085 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2087 struct cam_eb *bus, *next_bus;
2095 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2103 for (; bus != NULL; bus = next_bus) {
2104 retval = tr_func(bus, arg);
2106 xpt_release_bus(bus);
2110 next_bus = TAILQ_NEXT(bus, links);
2112 next_bus->refcount++;
2114 xpt_release_bus(bus);
2120 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2121 xpt_targetfunc_t *tr_func, void *arg)
2123 struct cam_et *target, *next_target;
2128 target = start_target;
2130 mtx_lock(&bus->eb_mtx);
2131 target = TAILQ_FIRST(&bus->et_entries);
2132 if (target == NULL) {
2133 mtx_unlock(&bus->eb_mtx);
2137 mtx_unlock(&bus->eb_mtx);
2139 for (; target != NULL; target = next_target) {
2140 retval = tr_func(target, arg);
2142 xpt_release_target(target);
2145 mtx_lock(&bus->eb_mtx);
2146 next_target = TAILQ_NEXT(target, links);
2148 next_target->refcount++;
2149 mtx_unlock(&bus->eb_mtx);
2150 xpt_release_target(target);
2156 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2157 xpt_devicefunc_t *tr_func, void *arg)
2160 struct cam_ed *device, *next_device;
2166 device = start_device;
2168 mtx_lock(&bus->eb_mtx);
2169 device = TAILQ_FIRST(&target->ed_entries);
2170 if (device == NULL) {
2171 mtx_unlock(&bus->eb_mtx);
2175 mtx_unlock(&bus->eb_mtx);
2177 for (; device != NULL; device = next_device) {
2178 mtx_lock(&device->device_mtx);
2179 retval = tr_func(device, arg);
2180 mtx_unlock(&device->device_mtx);
2182 xpt_release_device(device);
2185 mtx_lock(&bus->eb_mtx);
2186 next_device = TAILQ_NEXT(device, links);
2188 next_device->refcount++;
2189 mtx_unlock(&bus->eb_mtx);
2190 xpt_release_device(device);
2196 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2197 xpt_periphfunc_t *tr_func, void *arg)
2200 struct cam_periph *periph, *next_periph;
2205 bus = device->target->bus;
2207 periph = start_periph;
2210 mtx_lock(&bus->eb_mtx);
2211 periph = SLIST_FIRST(&device->periphs);
2212 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2213 periph = SLIST_NEXT(periph, periph_links);
2214 if (periph == NULL) {
2215 mtx_unlock(&bus->eb_mtx);
2220 mtx_unlock(&bus->eb_mtx);
2223 for (; periph != NULL; periph = next_periph) {
2224 retval = tr_func(periph, arg);
2226 cam_periph_release_locked(periph);
2230 mtx_lock(&bus->eb_mtx);
2231 next_periph = SLIST_NEXT(periph, periph_links);
2232 while (next_periph != NULL &&
2233 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2234 next_periph = SLIST_NEXT(next_periph, periph_links);
2236 next_periph->refcount++;
2237 mtx_unlock(&bus->eb_mtx);
2239 cam_periph_release_locked(periph);
2245 xptpdrvtraverse(struct periph_driver **start_pdrv,
2246 xpt_pdrvfunc_t *tr_func, void *arg)
2248 struct periph_driver **pdrv;
2254 * We don't traverse the peripheral driver list like we do the
2255 * other lists, because it is a linker set, and therefore cannot be
2256 * changed during runtime. If the peripheral driver list is ever
2257 * re-done to be something other than a linker set (i.e. it can
2258 * change while the system is running), the list traversal should
2259 * be modified to work like the other traversal functions.
2261 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2262 *pdrv != NULL; pdrv++) {
2263 retval = tr_func(pdrv, arg);
2273 xptpdperiphtraverse(struct periph_driver **pdrv,
2274 struct cam_periph *start_periph,
2275 xpt_periphfunc_t *tr_func, void *arg)
2277 struct cam_periph *periph, *next_periph;
2283 periph = start_periph;
2286 periph = TAILQ_FIRST(&(*pdrv)->units);
2287 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2288 periph = TAILQ_NEXT(periph, unit_links);
2289 if (periph == NULL) {
2296 for (; periph != NULL; periph = next_periph) {
2297 cam_periph_lock(periph);
2298 retval = tr_func(periph, arg);
2299 cam_periph_unlock(periph);
2301 cam_periph_release(periph);
2305 next_periph = TAILQ_NEXT(periph, unit_links);
2306 while (next_periph != NULL &&
2307 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2308 next_periph = TAILQ_NEXT(next_periph, unit_links);
2310 next_periph->refcount++;
2312 cam_periph_release(periph);
2318 xptdefbusfunc(struct cam_eb *bus, void *arg)
2320 struct xpt_traverse_config *tr_config;
2322 tr_config = (struct xpt_traverse_config *)arg;
2324 if (tr_config->depth == XPT_DEPTH_BUS) {
2325 xpt_busfunc_t *tr_func;
2327 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2329 return(tr_func(bus, tr_config->tr_arg));
2331 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2335 xptdeftargetfunc(struct cam_et *target, void *arg)
2337 struct xpt_traverse_config *tr_config;
2339 tr_config = (struct xpt_traverse_config *)arg;
2341 if (tr_config->depth == XPT_DEPTH_TARGET) {
2342 xpt_targetfunc_t *tr_func;
2344 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2346 return(tr_func(target, tr_config->tr_arg));
2348 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2352 xptdefdevicefunc(struct cam_ed *device, void *arg)
2354 struct xpt_traverse_config *tr_config;
2356 tr_config = (struct xpt_traverse_config *)arg;
2358 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2359 xpt_devicefunc_t *tr_func;
2361 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2363 return(tr_func(device, tr_config->tr_arg));
2365 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2369 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2371 struct xpt_traverse_config *tr_config;
2372 xpt_periphfunc_t *tr_func;
2374 tr_config = (struct xpt_traverse_config *)arg;
2376 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2379 * Unlike the other default functions, we don't check for depth
2380 * here. The peripheral driver level is the last level in the EDT,
2381 * so if we're here, we should execute the function in question.
2383 return(tr_func(periph, tr_config->tr_arg));
2387 * Execute the given function for every bus in the EDT.
2390 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2392 struct xpt_traverse_config tr_config;
2394 tr_config.depth = XPT_DEPTH_BUS;
2395 tr_config.tr_func = tr_func;
2396 tr_config.tr_arg = arg;
2398 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2402 * Execute the given function for every device in the EDT.
2405 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2407 struct xpt_traverse_config tr_config;
2409 tr_config.depth = XPT_DEPTH_DEVICE;
2410 tr_config.tr_func = tr_func;
2411 tr_config.tr_arg = arg;
2413 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2417 xptsetasyncfunc(struct cam_ed *device, void *arg)
2419 struct cam_path path;
2420 struct ccb_getdev cgd;
2421 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2424 * Don't report unconfigured devices (Wildcard devs,
2425 * devices only for target mode, device instances
2426 * that have been invalidated but are waiting for
2427 * their last reference count to be released).
2429 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2432 xpt_compile_path(&path,
2434 device->target->bus->path_id,
2435 device->target->target_id,
2437 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2438 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2439 xpt_action((union ccb *)&cgd);
2440 csa->callback(csa->callback_arg,
2443 xpt_release_path(&path);
2449 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2451 struct cam_path path;
2452 struct ccb_pathinq cpi;
2453 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2455 xpt_compile_path(&path, /*periph*/NULL,
2457 CAM_TARGET_WILDCARD,
2459 xpt_path_lock(&path);
2460 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2461 cpi.ccb_h.func_code = XPT_PATH_INQ;
2462 xpt_action((union ccb *)&cpi);
2463 csa->callback(csa->callback_arg,
2466 xpt_path_unlock(&path);
2467 xpt_release_path(&path);
2473 xpt_action(union ccb *start_ccb)
2476 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2477 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2478 xpt_action_name(start_ccb->ccb_h.func_code)));
2480 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2481 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2485 xpt_action_default(union ccb *start_ccb)
2487 struct cam_path *path;
2488 struct cam_sim *sim;
2491 path = start_ccb->ccb_h.path;
2492 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2493 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2494 xpt_action_name(start_ccb->ccb_h.func_code)));
2496 switch (start_ccb->ccb_h.func_code) {
2499 struct cam_ed *device;
2502 * For the sake of compatibility with SCSI-1
2503 * devices that may not understand the identify
2504 * message, we include lun information in the
2505 * second byte of all commands. SCSI-1 specifies
2506 * that luns are a 3 bit value and reserves only 3
2507 * bits for lun information in the CDB. Later
2508 * revisions of the SCSI spec allow for more than 8
2509 * luns, but have deprecated lun information in the
2510 * CDB. So, if the lun won't fit, we must omit.
2512 * Also be aware that during initial probing for devices,
2513 * the inquiry information is unknown but initialized to 0.
2514 * This means that this code will be exercised while probing
2515 * devices with an ANSI revision greater than 2.
2517 device = path->device;
2518 if (device->protocol_version <= SCSI_REV_2
2519 && start_ccb->ccb_h.target_lun < 8
2520 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2522 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2523 start_ccb->ccb_h.target_lun << 5;
2525 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2529 case XPT_CONT_TARGET_IO:
2530 start_ccb->csio.sense_resid = 0;
2531 start_ccb->csio.resid = 0;
2534 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2535 start_ccb->ataio.resid = 0;
2541 struct cam_devq *devq;
2543 devq = path->bus->sim->devq;
2544 mtx_lock(&devq->send_mtx);
2545 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2546 if (xpt_schedule_devq(devq, path->device) != 0)
2548 mtx_unlock(&devq->send_mtx);
2551 case XPT_CALC_GEOMETRY:
2552 /* Filter out garbage */
2553 if (start_ccb->ccg.block_size == 0
2554 || start_ccb->ccg.volume_size == 0) {
2555 start_ccb->ccg.cylinders = 0;
2556 start_ccb->ccg.heads = 0;
2557 start_ccb->ccg.secs_per_track = 0;
2558 start_ccb->ccb_h.status = CAM_REQ_CMP;
2561 #if defined(PC98) || defined(__sparc64__)
2563 * In a PC-98 system, geometry translation depens on
2564 * the "real" device geometry obtained from mode page 4.
2565 * SCSI geometry translation is performed in the
2566 * initialization routine of the SCSI BIOS and the result
2567 * stored in host memory. If the translation is available
2568 * in host memory, use it. If not, rely on the default
2569 * translation the device driver performs.
2570 * For sparc64, we may need adjust the geometry of large
2571 * disks in order to fit the limitations of the 16-bit
2572 * fields of the VTOC8 disk label.
2574 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2575 start_ccb->ccb_h.status = CAM_REQ_CMP;
2582 union ccb* abort_ccb;
2584 abort_ccb = start_ccb->cab.abort_ccb;
2585 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2586 struct cam_ed *device;
2587 struct cam_devq *devq;
2589 device = abort_ccb->ccb_h.path->device;
2590 devq = device->sim->devq;
2592 mtx_lock(&devq->send_mtx);
2593 if (abort_ccb->ccb_h.pinfo.index > 0) {
2594 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2595 abort_ccb->ccb_h.status =
2596 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2597 xpt_freeze_devq_device(device, 1);
2598 mtx_unlock(&devq->send_mtx);
2599 xpt_done(abort_ccb);
2600 start_ccb->ccb_h.status = CAM_REQ_CMP;
2603 mtx_unlock(&devq->send_mtx);
2605 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2606 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2608 * We've caught this ccb en route to
2609 * the SIM. Flag it for abort and the
2610 * SIM will do so just before starting
2611 * real work on the CCB.
2613 abort_ccb->ccb_h.status =
2614 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2615 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2616 start_ccb->ccb_h.status = CAM_REQ_CMP;
2620 if (XPT_FC_IS_QUEUED(abort_ccb)
2621 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2623 * It's already completed but waiting
2624 * for our SWI to get to it.
2626 start_ccb->ccb_h.status = CAM_UA_ABORT;
2630 * If we weren't able to take care of the abort request
2631 * in the XPT, pass the request down to the SIM for processing.
2635 case XPT_ACCEPT_TARGET_IO:
2637 case XPT_IMMED_NOTIFY:
2638 case XPT_NOTIFY_ACK:
2640 case XPT_IMMEDIATE_NOTIFY:
2641 case XPT_NOTIFY_ACKNOWLEDGE:
2642 case XPT_GET_SIM_KNOB_OLD:
2643 case XPT_GET_SIM_KNOB:
2644 case XPT_SET_SIM_KNOB:
2645 case XPT_GET_TRAN_SETTINGS:
2646 case XPT_SET_TRAN_SETTINGS:
2649 sim = path->bus->sim;
2650 lock = (mtx_owned(sim->mtx) == 0);
2653 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2654 ("sim->sim_action: func=%#x\n", start_ccb->ccb_h.func_code));
2655 (*(sim->sim_action))(sim, start_ccb);
2656 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2657 ("sim->sim_action: status=%#x\n", start_ccb->ccb_h.status));
2659 CAM_SIM_UNLOCK(sim);
2661 case XPT_PATH_STATS:
2662 start_ccb->cpis.last_reset = path->bus->last_reset;
2663 start_ccb->ccb_h.status = CAM_REQ_CMP;
2670 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2671 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2673 struct ccb_getdev *cgd;
2675 cgd = &start_ccb->cgd;
2676 cgd->protocol = dev->protocol;
2677 cgd->inq_data = dev->inq_data;
2678 cgd->ident_data = dev->ident_data;
2679 cgd->inq_flags = dev->inq_flags;
2680 cgd->ccb_h.status = CAM_REQ_CMP;
2681 cgd->serial_num_len = dev->serial_num_len;
2682 if ((dev->serial_num_len > 0)
2683 && (dev->serial_num != NULL))
2684 bcopy(dev->serial_num, cgd->serial_num,
2685 dev->serial_num_len);
2689 case XPT_GDEV_STATS:
2694 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2695 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2697 struct ccb_getdevstats *cgds;
2700 struct cam_devq *devq;
2702 cgds = &start_ccb->cgds;
2705 devq = bus->sim->devq;
2706 mtx_lock(&devq->send_mtx);
2707 cgds->dev_openings = dev->ccbq.dev_openings;
2708 cgds->dev_active = dev->ccbq.dev_active;
2709 cgds->allocated = dev->ccbq.allocated;
2710 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2711 cgds->held = cgds->allocated - cgds->dev_active -
2713 cgds->last_reset = tar->last_reset;
2714 cgds->maxtags = dev->maxtags;
2715 cgds->mintags = dev->mintags;
2716 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2717 cgds->last_reset = bus->last_reset;
2718 mtx_unlock(&devq->send_mtx);
2719 cgds->ccb_h.status = CAM_REQ_CMP;
2725 struct cam_periph *nperiph;
2726 struct periph_list *periph_head;
2727 struct ccb_getdevlist *cgdl;
2729 struct cam_ed *device;
2736 * Don't want anyone mucking with our data.
2738 device = path->device;
2739 periph_head = &device->periphs;
2740 cgdl = &start_ccb->cgdl;
2743 * Check and see if the list has changed since the user
2744 * last requested a list member. If so, tell them that the
2745 * list has changed, and therefore they need to start over
2746 * from the beginning.
2748 if ((cgdl->index != 0) &&
2749 (cgdl->generation != device->generation)) {
2750 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2755 * Traverse the list of peripherals and attempt to find
2756 * the requested peripheral.
2758 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2759 (nperiph != NULL) && (i <= cgdl->index);
2760 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2761 if (i == cgdl->index) {
2762 strncpy(cgdl->periph_name,
2763 nperiph->periph_name,
2765 cgdl->unit_number = nperiph->unit_number;
2770 cgdl->status = CAM_GDEVLIST_ERROR;
2774 if (nperiph == NULL)
2775 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2777 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2780 cgdl->generation = device->generation;
2782 cgdl->ccb_h.status = CAM_REQ_CMP;
2787 dev_pos_type position_type;
2788 struct ccb_dev_match *cdm;
2790 cdm = &start_ccb->cdm;
2793 * There are two ways of getting at information in the EDT.
2794 * The first way is via the primary EDT tree. It starts
2795 * with a list of busses, then a list of targets on a bus,
2796 * then devices/luns on a target, and then peripherals on a
2797 * device/lun. The "other" way is by the peripheral driver
2798 * lists. The peripheral driver lists are organized by
2799 * peripheral driver. (obviously) So it makes sense to
2800 * use the peripheral driver list if the user is looking
2801 * for something like "da1", or all "da" devices. If the
2802 * user is looking for something on a particular bus/target
2803 * or lun, it's generally better to go through the EDT tree.
2806 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2807 position_type = cdm->pos.position_type;
2811 position_type = CAM_DEV_POS_NONE;
2813 for (i = 0; i < cdm->num_patterns; i++) {
2814 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2815 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2816 position_type = CAM_DEV_POS_EDT;
2821 if (cdm->num_patterns == 0)
2822 position_type = CAM_DEV_POS_EDT;
2823 else if (position_type == CAM_DEV_POS_NONE)
2824 position_type = CAM_DEV_POS_PDRV;
2827 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2828 case CAM_DEV_POS_EDT:
2831 case CAM_DEV_POS_PDRV:
2832 xptperiphlistmatch(cdm);
2835 cdm->status = CAM_DEV_MATCH_ERROR;
2839 if (cdm->status == CAM_DEV_MATCH_ERROR)
2840 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2842 start_ccb->ccb_h.status = CAM_REQ_CMP;
2848 struct ccb_setasync *csa;
2849 struct async_node *cur_entry;
2850 struct async_list *async_head;
2853 csa = &start_ccb->csa;
2854 added = csa->event_enable;
2855 async_head = &path->device->asyncs;
2858 * If there is already an entry for us, simply
2861 cur_entry = SLIST_FIRST(async_head);
2862 while (cur_entry != NULL) {
2863 if ((cur_entry->callback_arg == csa->callback_arg)
2864 && (cur_entry->callback == csa->callback))
2866 cur_entry = SLIST_NEXT(cur_entry, links);
2869 if (cur_entry != NULL) {
2871 * If the request has no flags set,
2874 added &= ~cur_entry->event_enable;
2875 if (csa->event_enable == 0) {
2876 SLIST_REMOVE(async_head, cur_entry,
2878 xpt_release_device(path->device);
2879 free(cur_entry, M_CAMXPT);
2881 cur_entry->event_enable = csa->event_enable;
2883 csa->event_enable = added;
2885 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2887 if (cur_entry == NULL) {
2888 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2891 cur_entry->event_enable = csa->event_enable;
2892 cur_entry->event_lock =
2893 mtx_owned(path->bus->sim->mtx) ? 1 : 0;
2894 cur_entry->callback_arg = csa->callback_arg;
2895 cur_entry->callback = csa->callback;
2896 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2897 xpt_acquire_device(path->device);
2899 start_ccb->ccb_h.status = CAM_REQ_CMP;
2904 struct ccb_relsim *crs;
2907 crs = &start_ccb->crs;
2911 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2915 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2917 /* Don't ever go below one opening */
2918 if (crs->openings > 0) {
2919 xpt_dev_ccbq_resize(path, crs->openings);
2922 "number of openings is now %d\n",
2928 mtx_lock(&dev->sim->devq->send_mtx);
2929 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2931 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2934 * Just extend the old timeout and decrement
2935 * the freeze count so that a single timeout
2936 * is sufficient for releasing the queue.
2938 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2939 callout_stop(&dev->callout);
2942 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2945 callout_reset_sbt(&dev->callout,
2946 SBT_1MS * crs->release_timeout, 0,
2947 xpt_release_devq_timeout, dev, 0);
2949 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2953 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2955 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2957 * Decrement the freeze count so that a single
2958 * completion is still sufficient to unfreeze
2961 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2964 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2965 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2969 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2971 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2972 || (dev->ccbq.dev_active == 0)) {
2974 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2977 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2978 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2981 mtx_unlock(&dev->sim->devq->send_mtx);
2983 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2984 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2985 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
2986 start_ccb->ccb_h.status = CAM_REQ_CMP;
2990 struct cam_path *oldpath;
2992 /* Check that all request bits are supported. */
2993 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2994 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2998 cam_dflags = CAM_DEBUG_NONE;
2999 if (cam_dpath != NULL) {
3000 oldpath = cam_dpath;
3002 xpt_free_path(oldpath);
3004 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3005 if (xpt_create_path(&cam_dpath, NULL,
3006 start_ccb->ccb_h.path_id,
3007 start_ccb->ccb_h.target_id,
3008 start_ccb->ccb_h.target_lun) !=
3010 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3012 cam_dflags = start_ccb->cdbg.flags;
3013 start_ccb->ccb_h.status = CAM_REQ_CMP;
3014 xpt_print(cam_dpath, "debugging flags now %x\n",
3018 start_ccb->ccb_h.status = CAM_REQ_CMP;
3022 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3023 xpt_freeze_devq(path, 1);
3024 start_ccb->ccb_h.status = CAM_REQ_CMP;
3026 case XPT_REPROBE_LUN:
3027 xpt_async(AC_INQ_CHANGED, path, NULL);
3028 start_ccb->ccb_h.status = CAM_REQ_CMP;
3029 xpt_done(start_ccb);
3036 printf("%s: CCB type %#x not supported\n", __func__,
3037 start_ccb->ccb_h.func_code);
3038 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3039 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3040 xpt_done(start_ccb);
3044 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3045 ("xpt_action_default: func= %#x %s status %#x\n",
3046 start_ccb->ccb_h.func_code,
3047 xpt_action_name(start_ccb->ccb_h.func_code),
3048 start_ccb->ccb_h.status));
3052 xpt_polled_action(union ccb *start_ccb)
3055 struct cam_sim *sim;
3056 struct cam_devq *devq;
3059 timeout = start_ccb->ccb_h.timeout * 10;
3060 sim = start_ccb->ccb_h.path->bus->sim;
3062 dev = start_ccb->ccb_h.path->device;
3064 mtx_unlock(&dev->device_mtx);
3067 * Steal an opening so that no other queued requests
3068 * can get it before us while we simulate interrupts.
3070 mtx_lock(&devq->send_mtx);
3071 dev->ccbq.dev_openings--;
3072 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3074 mtx_unlock(&devq->send_mtx);
3077 (*(sim->sim_poll))(sim);
3078 CAM_SIM_UNLOCK(sim);
3080 mtx_lock(&devq->send_mtx);
3082 dev->ccbq.dev_openings++;
3083 mtx_unlock(&devq->send_mtx);
3086 xpt_action(start_ccb);
3087 while(--timeout > 0) {
3089 (*(sim->sim_poll))(sim);
3090 CAM_SIM_UNLOCK(sim);
3092 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3099 * XXX Is it worth adding a sim_timeout entry
3100 * point so we can attempt recovery? If
3101 * this is only used for dumps, I don't think
3104 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3107 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3110 mtx_lock(&dev->device_mtx);
3114 * Schedule a peripheral driver to receive a ccb when its
3115 * target device has space for more transactions.
3118 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3121 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3122 cam_periph_assert(periph, MA_OWNED);
3123 if (new_priority < periph->scheduled_priority) {
3124 periph->scheduled_priority = new_priority;
3125 xpt_run_allocq(periph, 0);
3131 * Schedule a device to run on a given queue.
3132 * If the device was inserted as a new entry on the queue,
3133 * return 1 meaning the device queue should be run. If we
3134 * were already queued, implying someone else has already
3135 * started the queue, return 0 so the caller doesn't attempt
3139 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3140 u_int32_t new_priority)
3143 u_int32_t old_priority;
3145 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3147 old_priority = pinfo->priority;
3150 * Are we already queued?
3152 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3153 /* Simply reorder based on new priority */
3154 if (new_priority < old_priority) {
3155 camq_change_priority(queue, pinfo->index,
3157 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3158 ("changed priority to %d\n",
3164 /* New entry on the queue */
3165 if (new_priority < old_priority)
3166 pinfo->priority = new_priority;
3168 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3169 ("Inserting onto queue\n"));
3170 pinfo->generation = ++queue->generation;
3171 camq_insert(queue, pinfo);
3178 xpt_run_allocq_task(void *context, int pending)
3180 struct cam_periph *periph = context;
3182 cam_periph_lock(periph);
3183 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3184 xpt_run_allocq(periph, 1);
3185 cam_periph_unlock(periph);
3186 cam_periph_release(periph);
3190 xpt_run_allocq(struct cam_periph *periph, int sleep)
3192 struct cam_ed *device;
3196 cam_periph_assert(periph, MA_OWNED);
3197 if (periph->periph_allocating)
3199 periph->periph_allocating = 1;
3200 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3201 device = periph->path->device;
3204 while ((prio = min(periph->scheduled_priority,
3205 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3206 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3207 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3210 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3212 ccb = xpt_get_ccb(periph);
3215 if (periph->flags & CAM_PERIPH_RUN_TASK)
3217 cam_periph_doacquire(periph);
3218 periph->flags |= CAM_PERIPH_RUN_TASK;
3219 taskqueue_enqueue(xsoftc.xpt_taskq,
3220 &periph->periph_run_task);
3223 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3224 if (prio == periph->immediate_priority) {
3225 periph->immediate_priority = CAM_PRIORITY_NONE;
3226 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3227 ("waking cam_periph_getccb()\n"));
3228 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3230 wakeup(&periph->ccb_list);
3232 periph->scheduled_priority = CAM_PRIORITY_NONE;
3233 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3234 ("calling periph_start()\n"));
3235 periph->periph_start(periph, ccb);
3240 xpt_release_ccb(ccb);
3241 periph->periph_allocating = 0;
3245 xpt_run_devq(struct cam_devq *devq)
3247 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3250 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3252 devq->send_queue.qfrozen_cnt++;
3253 while ((devq->send_queue.entries > 0)
3254 && (devq->send_openings > 0)
3255 && (devq->send_queue.qfrozen_cnt <= 1)) {
3256 struct cam_ed *device;
3257 union ccb *work_ccb;
3258 struct cam_sim *sim;
3260 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3262 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3263 ("running device %p\n", device));
3265 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3266 if (work_ccb == NULL) {
3267 printf("device on run queue with no ccbs???\n");
3271 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3273 mtx_lock(&xsoftc.xpt_highpower_lock);
3274 if (xsoftc.num_highpower <= 0) {
3276 * We got a high power command, but we
3277 * don't have any available slots. Freeze
3278 * the device queue until we have a slot
3281 xpt_freeze_devq_device(device, 1);
3282 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3285 mtx_unlock(&xsoftc.xpt_highpower_lock);
3289 * Consume a high power slot while
3292 xsoftc.num_highpower--;
3294 mtx_unlock(&xsoftc.xpt_highpower_lock);
3296 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3297 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3298 devq->send_openings--;
3299 devq->send_active++;
3300 xpt_schedule_devq(devq, device);
3301 mtx_unlock(&devq->send_mtx);
3303 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3305 * The client wants to freeze the queue
3306 * after this CCB is sent.
3308 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3311 /* In Target mode, the peripheral driver knows best... */
3312 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3313 if ((device->inq_flags & SID_CmdQue) != 0
3314 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3315 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3318 * Clear this in case of a retried CCB that
3319 * failed due to a rejected tag.
3321 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3324 switch (work_ccb->ccb_h.func_code) {
3326 CAM_DEBUG(work_ccb->ccb_h.path,
3327 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3328 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3330 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3331 cdb_str, sizeof(cdb_str))));
3334 CAM_DEBUG(work_ccb->ccb_h.path,
3335 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3336 ata_op_string(&work_ccb->ataio.cmd),
3337 ata_cmd_string(&work_ccb->ataio.cmd,
3338 cdb_str, sizeof(cdb_str))));
3345 * Device queues can be shared among multiple SIM instances
3346 * that reside on different busses. Use the SIM from the
3347 * queued device, rather than the one from the calling bus.
3350 lock = (mtx_owned(sim->mtx) == 0);
3353 work_ccb->ccb_h.qos.sim_data = sbinuptime(); // xxx uintprt_t too small 32bit platforms
3354 (*(sim->sim_action))(sim, work_ccb);
3356 CAM_SIM_UNLOCK(sim);
3357 mtx_lock(&devq->send_mtx);
3359 devq->send_queue.qfrozen_cnt--;
3363 * This function merges stuff from the slave ccb into the master ccb, while
3364 * keeping important fields in the master ccb constant.
3367 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3371 * Pull fields that are valid for peripheral drivers to set
3372 * into the master CCB along with the CCB "payload".
3374 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3375 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3376 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3377 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3378 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3379 sizeof(union ccb) - sizeof(struct ccb_hdr));
3383 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3384 u_int32_t priority, u_int32_t flags)
3387 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3388 ccb_h->pinfo.priority = priority;
3390 ccb_h->path_id = path->bus->path_id;
3392 ccb_h->target_id = path->target->target_id;
3394 ccb_h->target_id = CAM_TARGET_WILDCARD;
3396 ccb_h->target_lun = path->device->lun_id;
3397 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3399 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3401 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3402 ccb_h->flags = flags;
3407 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3409 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3412 /* Path manipulation functions */
3414 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3415 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3417 struct cam_path *path;
3420 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3423 status = CAM_RESRC_UNAVAIL;
3426 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3427 if (status != CAM_REQ_CMP) {
3428 free(path, M_CAMPATH);
3431 *new_path_ptr = path;
3436 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3437 struct cam_periph *periph, path_id_t path_id,
3438 target_id_t target_id, lun_id_t lun_id)
3441 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3446 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3447 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3450 struct cam_et *target;
3451 struct cam_ed *device;
3454 status = CAM_REQ_CMP; /* Completed without error */
3455 target = NULL; /* Wildcarded */
3456 device = NULL; /* Wildcarded */
3459 * We will potentially modify the EDT, so block interrupts
3460 * that may attempt to create cam paths.
3462 bus = xpt_find_bus(path_id);
3464 status = CAM_PATH_INVALID;
3467 mtx_lock(&bus->eb_mtx);
3468 target = xpt_find_target(bus, target_id);
3469 if (target == NULL) {
3471 struct cam_et *new_target;
3473 new_target = xpt_alloc_target(bus, target_id);
3474 if (new_target == NULL) {
3475 status = CAM_RESRC_UNAVAIL;
3477 target = new_target;
3481 if (target != NULL) {
3482 device = xpt_find_device(target, lun_id);
3483 if (device == NULL) {
3485 struct cam_ed *new_device;
3488 (*(bus->xport->alloc_device))(bus,
3491 if (new_device == NULL) {
3492 status = CAM_RESRC_UNAVAIL;
3494 device = new_device;
3498 mtx_unlock(&bus->eb_mtx);
3502 * Only touch the user's data if we are successful.
3504 if (status == CAM_REQ_CMP) {
3505 new_path->periph = perph;
3506 new_path->bus = bus;
3507 new_path->target = target;
3508 new_path->device = device;
3509 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3512 xpt_release_device(device);
3514 xpt_release_target(target);
3516 xpt_release_bus(bus);
3522 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3524 struct cam_path *new_path;
3526 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3527 if (new_path == NULL)
3528 return(CAM_RESRC_UNAVAIL);
3529 xpt_copy_path(new_path, path);
3530 *new_path_ptr = new_path;
3531 return (CAM_REQ_CMP);
3535 xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3539 if (path->bus != NULL)
3540 xpt_acquire_bus(path->bus);
3541 if (path->target != NULL)
3542 xpt_acquire_target(path->target);
3543 if (path->device != NULL)
3544 xpt_acquire_device(path->device);
3548 xpt_release_path(struct cam_path *path)
3550 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3551 if (path->device != NULL) {
3552 xpt_release_device(path->device);
3553 path->device = NULL;
3555 if (path->target != NULL) {
3556 xpt_release_target(path->target);
3557 path->target = NULL;
3559 if (path->bus != NULL) {
3560 xpt_release_bus(path->bus);
3566 xpt_free_path(struct cam_path *path)
3569 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3570 xpt_release_path(path);
3571 free(path, M_CAMPATH);
3575 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3576 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3582 *bus_ref = path->bus->refcount;
3588 *periph_ref = path->periph->refcount;
3595 *target_ref = path->target->refcount;
3601 *device_ref = path->device->refcount;
3608 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3609 * in path1, 2 for match with wildcards in path2.
3612 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3616 if (path1->bus != path2->bus) {
3617 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3619 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3624 if (path1->target != path2->target) {
3625 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3628 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3633 if (path1->device != path2->device) {
3634 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3637 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3646 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3650 if (path->bus != dev->target->bus) {
3651 if (path->bus->path_id == CAM_BUS_WILDCARD)
3653 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3658 if (path->target != dev->target) {
3659 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3662 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3667 if (path->device != dev) {
3668 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3671 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3680 xpt_print_path(struct cam_path *path)
3684 printf("(nopath): ");
3686 if (path->periph != NULL)
3687 printf("(%s%d:", path->periph->periph_name,
3688 path->periph->unit_number);
3690 printf("(noperiph:");
3692 if (path->bus != NULL)
3693 printf("%s%d:%d:", path->bus->sim->sim_name,
3694 path->bus->sim->unit_number,
3695 path->bus->sim->bus_id);
3699 if (path->target != NULL)
3700 printf("%d:", path->target->target_id);
3704 if (path->device != NULL)
3705 printf("%jx): ", (uintmax_t)path->device->lun_id);
3712 xpt_print_device(struct cam_ed *device)
3716 printf("(nopath): ");
3718 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3719 device->sim->unit_number,
3720 device->sim->bus_id,
3721 device->target->target_id,
3722 (uintmax_t)device->lun_id);
3727 xpt_print(struct cam_path *path, const char *fmt, ...)
3730 xpt_print_path(path);
3737 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3741 sbuf_new(&sb, str, str_len, 0);
3744 sbuf_printf(&sb, "(nopath): ");
3746 if (path->periph != NULL)
3747 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3748 path->periph->unit_number);
3750 sbuf_printf(&sb, "(noperiph:");
3752 if (path->bus != NULL)
3753 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3754 path->bus->sim->unit_number,
3755 path->bus->sim->bus_id);
3757 sbuf_printf(&sb, "nobus:");
3759 if (path->target != NULL)
3760 sbuf_printf(&sb, "%d:", path->target->target_id);
3762 sbuf_printf(&sb, "X:");
3764 if (path->device != NULL)
3765 sbuf_printf(&sb, "%jx): ",
3766 (uintmax_t)path->device->lun_id);
3768 sbuf_printf(&sb, "X): ");
3772 return(sbuf_len(&sb));
3776 xpt_path_path_id(struct cam_path *path)
3778 return(path->bus->path_id);
3782 xpt_path_target_id(struct cam_path *path)
3784 if (path->target != NULL)
3785 return (path->target->target_id);
3787 return (CAM_TARGET_WILDCARD);
3791 xpt_path_lun_id(struct cam_path *path)
3793 if (path->device != NULL)
3794 return (path->device->lun_id);
3796 return (CAM_LUN_WILDCARD);
3800 xpt_path_sim(struct cam_path *path)
3803 return (path->bus->sim);
3807 xpt_path_periph(struct cam_path *path)
3810 return (path->periph);
3814 * Release a CAM control block for the caller. Remit the cost of the structure
3815 * to the device referenced by the path. If the this device had no 'credits'
3816 * and peripheral drivers have registered async callbacks for this notification
3820 xpt_release_ccb(union ccb *free_ccb)
3822 struct cam_ed *device;
3823 struct cam_periph *periph;
3825 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3826 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3827 device = free_ccb->ccb_h.path->device;
3828 periph = free_ccb->ccb_h.path->periph;
3830 xpt_free_ccb(free_ccb);
3831 periph->periph_allocated--;
3832 cam_ccbq_release_opening(&device->ccbq);
3833 xpt_run_allocq(periph, 0);
3836 /* Functions accessed by SIM drivers */
3838 static struct xpt_xport xport_default = {
3839 .alloc_device = xpt_alloc_device_default,
3840 .action = xpt_action_default,
3841 .async = xpt_dev_async_default,
3845 * A sim structure, listing the SIM entry points and instance
3846 * identification info is passed to xpt_bus_register to hook the SIM
3847 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3848 * for this new bus and places it in the array of busses and assigns
3849 * it a path_id. The path_id may be influenced by "hard wiring"
3850 * information specified by the user. Once interrupt services are
3851 * available, the bus will be probed.
3854 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3856 struct cam_eb *new_bus;
3857 struct cam_eb *old_bus;
3858 struct ccb_pathinq cpi;
3859 struct cam_path *path;
3862 mtx_assert(sim->mtx, MA_OWNED);
3865 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3866 M_CAMXPT, M_NOWAIT|M_ZERO);
3867 if (new_bus == NULL) {
3868 /* Couldn't satisfy request */
3869 return (CAM_RESRC_UNAVAIL);
3872 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3873 TAILQ_INIT(&new_bus->et_entries);
3876 timevalclear(&new_bus->last_reset);
3878 new_bus->refcount = 1; /* Held until a bus_deregister event */
3879 new_bus->generation = 0;
3882 sim->path_id = new_bus->path_id =
3883 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3884 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3885 while (old_bus != NULL
3886 && old_bus->path_id < new_bus->path_id)
3887 old_bus = TAILQ_NEXT(old_bus, links);
3888 if (old_bus != NULL)
3889 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3891 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3892 xsoftc.bus_generation++;
3896 * Set a default transport so that a PATH_INQ can be issued to
3897 * the SIM. This will then allow for probing and attaching of
3898 * a more appropriate transport.
3900 new_bus->xport = &xport_default;
3902 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3903 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3904 if (status != CAM_REQ_CMP) {
3905 xpt_release_bus(new_bus);
3906 free(path, M_CAMXPT);
3907 return (CAM_RESRC_UNAVAIL);
3910 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3911 cpi.ccb_h.func_code = XPT_PATH_INQ;
3912 xpt_action((union ccb *)&cpi);
3914 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3915 switch (cpi.transport) {
3923 new_bus->xport = scsi_get_xport();
3927 new_bus->xport = ata_get_xport();
3930 new_bus->xport = &xport_default;
3935 /* Notify interested parties */
3936 if (sim->path_id != CAM_XPT_PATH_ID) {
3938 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3939 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3940 union ccb *scan_ccb;
3942 /* Initiate bus rescan. */
3943 scan_ccb = xpt_alloc_ccb_nowait();
3944 if (scan_ccb != NULL) {
3945 scan_ccb->ccb_h.path = path;
3946 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3947 scan_ccb->crcn.flags = 0;
3948 xpt_rescan(scan_ccb);
3951 "Can't allocate CCB to scan bus\n");
3952 xpt_free_path(path);
3955 xpt_free_path(path);
3957 xpt_free_path(path);
3958 return (CAM_SUCCESS);
3962 xpt_bus_deregister(path_id_t pathid)
3964 struct cam_path bus_path;
3967 status = xpt_compile_path(&bus_path, NULL, pathid,
3968 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3969 if (status != CAM_REQ_CMP)
3972 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3973 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3975 /* Release the reference count held while registered. */
3976 xpt_release_bus(bus_path.bus);
3977 xpt_release_path(&bus_path);
3979 return (CAM_REQ_CMP);
3983 xptnextfreepathid(void)
3989 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3991 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3993 /* Find an unoccupied pathid */
3994 while (bus != NULL && bus->path_id <= pathid) {
3995 if (bus->path_id == pathid)
3997 bus = TAILQ_NEXT(bus, links);
4001 * Ensure that this pathid is not reserved for
4002 * a bus that may be registered in the future.
4004 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4006 /* Start the search over */
4013 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4020 pathid = CAM_XPT_PATH_ID;
4021 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4022 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4025 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4026 if (strcmp(dname, "scbus")) {
4027 /* Avoid a bit of foot shooting. */
4030 if (dunit < 0) /* unwired?! */
4032 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4033 if (sim_bus == val) {
4037 } else if (sim_bus == 0) {
4038 /* Unspecified matches bus 0 */
4042 printf("Ambiguous scbus configuration for %s%d "
4043 "bus %d, cannot wire down. The kernel "
4044 "config entry for scbus%d should "
4045 "specify a controller bus.\n"
4046 "Scbus will be assigned dynamically.\n",
4047 sim_name, sim_unit, sim_bus, dunit);
4052 if (pathid == CAM_XPT_PATH_ID)
4053 pathid = xptnextfreepathid();
4058 xpt_async_string(u_int32_t async_code)
4061 switch (async_code) {
4062 case AC_BUS_RESET: return ("AC_BUS_RESET");
4063 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4064 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4065 case AC_SENT_BDR: return ("AC_SENT_BDR");
4066 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4067 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4068 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4069 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4070 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4071 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4072 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4073 case AC_CONTRACT: return ("AC_CONTRACT");
4074 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4075 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4077 return ("AC_UNKNOWN");
4081 xpt_async_size(u_int32_t async_code)
4084 switch (async_code) {
4085 case AC_BUS_RESET: return (0);
4086 case AC_UNSOL_RESEL: return (0);
4087 case AC_SCSI_AEN: return (0);
4088 case AC_SENT_BDR: return (0);
4089 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4090 case AC_PATH_DEREGISTERED: return (0);
4091 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4092 case AC_LOST_DEVICE: return (0);
4093 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4094 case AC_INQ_CHANGED: return (0);
4095 case AC_GETDEV_CHANGED: return (0);
4096 case AC_CONTRACT: return (sizeof(struct ac_contract));
4097 case AC_ADVINFO_CHANGED: return (-1);
4098 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4104 xpt_async_process_dev(struct cam_ed *device, void *arg)
4106 union ccb *ccb = arg;
4107 struct cam_path *path = ccb->ccb_h.path;
4108 void *async_arg = ccb->casync.async_arg_ptr;
4109 u_int32_t async_code = ccb->casync.async_code;
4112 if (path->device != device
4113 && path->device->lun_id != CAM_LUN_WILDCARD
4114 && device->lun_id != CAM_LUN_WILDCARD)
4118 * The async callback could free the device.
4119 * If it is a broadcast async, it doesn't hold
4120 * device reference, so take our own reference.
4122 xpt_acquire_device(device);
4125 * If async for specific device is to be delivered to
4126 * the wildcard client, take the specific device lock.
4127 * XXX: We may need a way for client to specify it.
4129 if ((device->lun_id == CAM_LUN_WILDCARD &&
4130 path->device->lun_id != CAM_LUN_WILDCARD) ||
4131 (device->target->target_id == CAM_TARGET_WILDCARD &&
4132 path->target->target_id != CAM_TARGET_WILDCARD) ||
4133 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4134 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4135 mtx_unlock(&device->device_mtx);
4136 xpt_path_lock(path);
4141 (*(device->target->bus->xport->async))(async_code,
4142 device->target->bus, device->target, device, async_arg);
4143 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4146 xpt_path_unlock(path);
4147 mtx_lock(&device->device_mtx);
4149 xpt_release_device(device);
4154 xpt_async_process_tgt(struct cam_et *target, void *arg)
4156 union ccb *ccb = arg;
4157 struct cam_path *path = ccb->ccb_h.path;
4159 if (path->target != target
4160 && path->target->target_id != CAM_TARGET_WILDCARD
4161 && target->target_id != CAM_TARGET_WILDCARD)
4164 if (ccb->casync.async_code == AC_SENT_BDR) {
4165 /* Update our notion of when the last reset occurred */
4166 microtime(&target->last_reset);
4169 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4173 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4176 struct cam_path *path;
4178 u_int32_t async_code;
4180 path = ccb->ccb_h.path;
4181 async_code = ccb->casync.async_code;
4182 async_arg = ccb->casync.async_arg_ptr;
4183 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4184 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4187 if (async_code == AC_BUS_RESET) {
4188 /* Update our notion of when the last reset occurred */
4189 microtime(&bus->last_reset);
4192 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4195 * If this wasn't a fully wildcarded async, tell all
4196 * clients that want all async events.
4198 if (bus != xpt_periph->path->bus) {
4199 xpt_path_lock(xpt_periph->path);
4200 xpt_async_process_dev(xpt_periph->path->device, ccb);
4201 xpt_path_unlock(xpt_periph->path);
4204 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4205 xpt_release_devq(path, 1, TRUE);
4207 xpt_release_simq(path->bus->sim, TRUE);
4208 if (ccb->casync.async_arg_size > 0)
4209 free(async_arg, M_CAMXPT);
4210 xpt_free_path(path);
4215 xpt_async_bcast(struct async_list *async_head,
4216 u_int32_t async_code,
4217 struct cam_path *path, void *async_arg)
4219 struct async_node *cur_entry;
4222 cur_entry = SLIST_FIRST(async_head);
4223 while (cur_entry != NULL) {
4224 struct async_node *next_entry;
4226 * Grab the next list entry before we call the current
4227 * entry's callback. This is because the callback function
4228 * can delete its async callback entry.
4230 next_entry = SLIST_NEXT(cur_entry, links);
4231 if ((cur_entry->event_enable & async_code) != 0) {
4232 lock = cur_entry->event_lock;
4234 CAM_SIM_LOCK(path->device->sim);
4235 cur_entry->callback(cur_entry->callback_arg,
4239 CAM_SIM_UNLOCK(path->device->sim);
4241 cur_entry = next_entry;
4246 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4251 ccb = xpt_alloc_ccb_nowait();
4253 xpt_print(path, "Can't allocate CCB to send %s\n",
4254 xpt_async_string(async_code));
4258 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4259 xpt_print(path, "Can't allocate path to send %s\n",
4260 xpt_async_string(async_code));
4264 ccb->ccb_h.path->periph = NULL;
4265 ccb->ccb_h.func_code = XPT_ASYNC;
4266 ccb->ccb_h.cbfcnp = xpt_async_process;
4267 ccb->ccb_h.flags |= CAM_UNLOCKED;
4268 ccb->casync.async_code = async_code;
4269 ccb->casync.async_arg_size = 0;
4270 size = xpt_async_size(async_code);
4271 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4272 ("xpt_async: func %#x %s aync_code %d %s\n",
4273 ccb->ccb_h.func_code,
4274 xpt_action_name(ccb->ccb_h.func_code),
4276 xpt_async_string(async_code)));
4277 if (size > 0 && async_arg != NULL) {
4278 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4279 if (ccb->casync.async_arg_ptr == NULL) {
4280 xpt_print(path, "Can't allocate argument to send %s\n",
4281 xpt_async_string(async_code));
4282 xpt_free_path(ccb->ccb_h.path);
4286 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4287 ccb->casync.async_arg_size = size;
4288 } else if (size < 0) {
4289 ccb->casync.async_arg_ptr = async_arg;
4290 ccb->casync.async_arg_size = size;
4292 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4293 xpt_freeze_devq(path, 1);
4295 xpt_freeze_simq(path->bus->sim, 1);
4300 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4301 struct cam_et *target, struct cam_ed *device,
4306 * We only need to handle events for real devices.
4308 if (target->target_id == CAM_TARGET_WILDCARD
4309 || device->lun_id == CAM_LUN_WILDCARD)
4312 printf("%s called\n", __func__);
4316 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4318 struct cam_devq *devq;
4321 devq = dev->sim->devq;
4322 mtx_assert(&devq->send_mtx, MA_OWNED);
4323 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4324 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4325 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4326 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4327 /* Remove frozen device from sendq. */
4328 if (device_is_queued(dev))
4329 camq_remove(&devq->send_queue, dev->devq_entry.index);
4334 xpt_freeze_devq(struct cam_path *path, u_int count)
4336 struct cam_ed *dev = path->device;
4337 struct cam_devq *devq;
4340 devq = dev->sim->devq;
4341 mtx_lock(&devq->send_mtx);
4342 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4343 freeze = xpt_freeze_devq_device(dev, count);
4344 mtx_unlock(&devq->send_mtx);
4349 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4351 struct cam_devq *devq;
4355 mtx_lock(&devq->send_mtx);
4356 freeze = (devq->send_queue.qfrozen_cnt += count);
4357 mtx_unlock(&devq->send_mtx);
4362 xpt_release_devq_timeout(void *arg)
4365 struct cam_devq *devq;
4367 dev = (struct cam_ed *)arg;
4368 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4369 devq = dev->sim->devq;
4370 mtx_assert(&devq->send_mtx, MA_OWNED);
4371 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4376 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4379 struct cam_devq *devq;
4381 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4384 devq = dev->sim->devq;
4385 mtx_lock(&devq->send_mtx);
4386 if (xpt_release_devq_device(dev, count, run_queue))
4387 xpt_run_devq(dev->sim->devq);
4388 mtx_unlock(&devq->send_mtx);
4392 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4395 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4396 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4397 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4398 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4399 if (count > dev->ccbq.queue.qfrozen_cnt) {
4401 printf("xpt_release_devq(): requested %u > present %u\n",
4402 count, dev->ccbq.queue.qfrozen_cnt);
4404 count = dev->ccbq.queue.qfrozen_cnt;
4406 dev->ccbq.queue.qfrozen_cnt -= count;
4407 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4409 * No longer need to wait for a successful
4410 * command completion.
4412 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4414 * Remove any timeouts that might be scheduled
4415 * to release this queue.
4417 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4418 callout_stop(&dev->callout);
4419 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4422 * Now that we are unfrozen schedule the
4423 * device so any pending transactions are
4426 xpt_schedule_devq(dev->sim->devq, dev);
4433 xpt_release_simq(struct cam_sim *sim, int run_queue)
4435 struct cam_devq *devq;
4438 mtx_lock(&devq->send_mtx);
4439 if (devq->send_queue.qfrozen_cnt <= 0) {
4441 printf("xpt_release_simq: requested 1 > present %u\n",
4442 devq->send_queue.qfrozen_cnt);
4445 devq->send_queue.qfrozen_cnt--;
4446 if (devq->send_queue.qfrozen_cnt == 0) {
4448 * If there is a timeout scheduled to release this
4449 * sim queue, remove it. The queue frozen count is
4452 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4453 callout_stop(&sim->callout);
4454 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4458 * Now that we are unfrozen run the send queue.
4460 xpt_run_devq(sim->devq);
4463 mtx_unlock(&devq->send_mtx);
4467 * XXX Appears to be unused.
4470 xpt_release_simq_timeout(void *arg)
4472 struct cam_sim *sim;
4474 sim = (struct cam_sim *)arg;
4475 xpt_release_simq(sim, /* run_queue */ TRUE);
4479 xpt_done(union ccb *done_ccb)
4481 struct cam_doneq *queue;
4484 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4485 ("xpt_done: func= %#x %s status %#x\n",
4486 done_ccb->ccb_h.func_code,
4487 xpt_action_name(done_ccb->ccb_h.func_code),
4488 done_ccb->ccb_h.status));
4489 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4492 /* Store the time the ccb was in the sim */
4493 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4494 hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4495 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4496 queue = &cam_doneqs[hash];
4497 mtx_lock(&queue->cam_doneq_mtx);
4498 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4499 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4500 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4501 mtx_unlock(&queue->cam_doneq_mtx);
4503 wakeup(&queue->cam_doneq);
4507 xpt_done_direct(union ccb *done_ccb)
4510 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4511 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4512 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4515 /* Store the time the ccb was in the sim */
4516 done_ccb->ccb_h.qos.sim_data = sbinuptime() - done_ccb->ccb_h.qos.sim_data;
4517 xpt_done_process(&done_ccb->ccb_h);
4525 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4530 xpt_alloc_ccb_nowait()
4534 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4539 xpt_free_ccb(union ccb *free_ccb)
4541 free(free_ccb, M_CAMCCB);
4546 /* Private XPT functions */
4549 * Get a CAM control block for the caller. Charge the structure to the device
4550 * referenced by the path. If we don't have sufficient resources to allocate
4551 * more ccbs, we return NULL.
4554 xpt_get_ccb_nowait(struct cam_periph *periph)
4558 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4559 if (new_ccb == NULL)
4561 periph->periph_allocated++;
4562 cam_ccbq_take_opening(&periph->path->device->ccbq);
4567 xpt_get_ccb(struct cam_periph *periph)
4571 cam_periph_unlock(periph);
4572 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4573 cam_periph_lock(periph);
4574 periph->periph_allocated++;
4575 cam_ccbq_take_opening(&periph->path->device->ccbq);
4580 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4582 struct ccb_hdr *ccb_h;
4584 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4585 cam_periph_assert(periph, MA_OWNED);
4586 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4587 ccb_h->pinfo.priority != priority) {
4588 if (priority < periph->immediate_priority) {
4589 periph->immediate_priority = priority;
4590 xpt_run_allocq(periph, 0);
4592 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4595 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4596 return ((union ccb *)ccb_h);
4600 xpt_acquire_bus(struct cam_eb *bus)
4609 xpt_release_bus(struct cam_eb *bus)
4613 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4614 if (--bus->refcount > 0) {
4618 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4619 xsoftc.bus_generation++;
4621 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4622 ("destroying bus, but target list is not empty"));
4623 cam_sim_release(bus->sim);
4624 mtx_destroy(&bus->eb_mtx);
4625 free(bus, M_CAMXPT);
4628 static struct cam_et *
4629 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4631 struct cam_et *cur_target, *target;
4633 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4634 mtx_assert(&bus->eb_mtx, MA_OWNED);
4635 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4640 TAILQ_INIT(&target->ed_entries);
4642 target->target_id = target_id;
4643 target->refcount = 1;
4644 target->generation = 0;
4645 target->luns = NULL;
4646 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4647 timevalclear(&target->last_reset);
4649 * Hold a reference to our parent bus so it
4650 * will not go away before we do.
4654 /* Insertion sort into our bus's target list */
4655 cur_target = TAILQ_FIRST(&bus->et_entries);
4656 while (cur_target != NULL && cur_target->target_id < target_id)
4657 cur_target = TAILQ_NEXT(cur_target, links);
4658 if (cur_target != NULL) {
4659 TAILQ_INSERT_BEFORE(cur_target, target, links);
4661 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4668 xpt_acquire_target(struct cam_et *target)
4670 struct cam_eb *bus = target->bus;
4672 mtx_lock(&bus->eb_mtx);
4674 mtx_unlock(&bus->eb_mtx);
4678 xpt_release_target(struct cam_et *target)
4680 struct cam_eb *bus = target->bus;
4682 mtx_lock(&bus->eb_mtx);
4683 if (--target->refcount > 0) {
4684 mtx_unlock(&bus->eb_mtx);
4687 TAILQ_REMOVE(&bus->et_entries, target, links);
4689 mtx_unlock(&bus->eb_mtx);
4690 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4691 ("destroying target, but device list is not empty"));
4692 xpt_release_bus(bus);
4693 mtx_destroy(&target->luns_mtx);
4695 free(target->luns, M_CAMXPT);
4696 free(target, M_CAMXPT);
4699 static struct cam_ed *
4700 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4703 struct cam_ed *device;
4705 device = xpt_alloc_device(bus, target, lun_id);
4709 device->mintags = 1;
4710 device->maxtags = 1;
4715 xpt_destroy_device(void *context, int pending)
4717 struct cam_ed *device = context;
4719 mtx_lock(&device->device_mtx);
4720 mtx_destroy(&device->device_mtx);
4721 free(device, M_CAMDEV);
4725 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4727 struct cam_ed *cur_device, *device;
4728 struct cam_devq *devq;
4731 mtx_assert(&bus->eb_mtx, MA_OWNED);
4732 /* Make space for us in the device queue on our bus */
4733 devq = bus->sim->devq;
4734 mtx_lock(&devq->send_mtx);
4735 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4736 mtx_unlock(&devq->send_mtx);
4737 if (status != CAM_REQ_CMP)
4740 device = (struct cam_ed *)malloc(sizeof(*device),
4741 M_CAMDEV, M_NOWAIT|M_ZERO);
4745 cam_init_pinfo(&device->devq_entry);
4746 device->target = target;
4747 device->lun_id = lun_id;
4748 device->sim = bus->sim;
4749 if (cam_ccbq_init(&device->ccbq,
4750 bus->sim->max_dev_openings) != 0) {
4751 free(device, M_CAMDEV);
4754 SLIST_INIT(&device->asyncs);
4755 SLIST_INIT(&device->periphs);
4756 device->generation = 0;
4757 device->flags = CAM_DEV_UNCONFIGURED;
4758 device->tag_delay_count = 0;
4759 device->tag_saved_openings = 0;
4760 device->refcount = 1;
4761 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4762 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4763 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4765 * Hold a reference to our parent bus so it
4766 * will not go away before we do.
4770 cur_device = TAILQ_FIRST(&target->ed_entries);
4771 while (cur_device != NULL && cur_device->lun_id < lun_id)
4772 cur_device = TAILQ_NEXT(cur_device, links);
4773 if (cur_device != NULL)
4774 TAILQ_INSERT_BEFORE(cur_device, device, links);
4776 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4777 target->generation++;
4782 xpt_acquire_device(struct cam_ed *device)
4784 struct cam_eb *bus = device->target->bus;
4786 mtx_lock(&bus->eb_mtx);
4788 mtx_unlock(&bus->eb_mtx);
4792 xpt_release_device(struct cam_ed *device)
4794 struct cam_eb *bus = device->target->bus;
4795 struct cam_devq *devq;
4797 mtx_lock(&bus->eb_mtx);
4798 if (--device->refcount > 0) {
4799 mtx_unlock(&bus->eb_mtx);
4803 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4804 device->target->generation++;
4805 mtx_unlock(&bus->eb_mtx);
4807 /* Release our slot in the devq */
4808 devq = bus->sim->devq;
4809 mtx_lock(&devq->send_mtx);
4810 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4811 mtx_unlock(&devq->send_mtx);
4813 KASSERT(SLIST_EMPTY(&device->periphs),
4814 ("destroying device, but periphs list is not empty"));
4815 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4816 ("destroying device while still queued for ccbs"));
4818 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4819 callout_stop(&device->callout);
4821 xpt_release_target(device->target);
4823 cam_ccbq_fini(&device->ccbq);
4825 * Free allocated memory. free(9) does nothing if the
4826 * supplied pointer is NULL, so it is safe to call without
4829 free(device->supported_vpds, M_CAMXPT);
4830 free(device->device_id, M_CAMXPT);
4831 free(device->ext_inq, M_CAMXPT);
4832 free(device->physpath, M_CAMXPT);
4833 free(device->rcap_buf, M_CAMXPT);
4834 free(device->serial_num, M_CAMXPT);
4835 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4839 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4845 mtx_lock(&dev->sim->devq->send_mtx);
4846 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4847 mtx_unlock(&dev->sim->devq->send_mtx);
4848 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4849 || (dev->inq_flags & SID_CmdQue) != 0)
4850 dev->tag_saved_openings = newopenings;
4854 static struct cam_eb *
4855 xpt_find_bus(path_id_t path_id)
4860 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4862 bus = TAILQ_NEXT(bus, links)) {
4863 if (bus->path_id == path_id) {
4872 static struct cam_et *
4873 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4875 struct cam_et *target;
4877 mtx_assert(&bus->eb_mtx, MA_OWNED);
4878 for (target = TAILQ_FIRST(&bus->et_entries);
4880 target = TAILQ_NEXT(target, links)) {
4881 if (target->target_id == target_id) {
4889 static struct cam_ed *
4890 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4892 struct cam_ed *device;
4894 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4895 for (device = TAILQ_FIRST(&target->ed_entries);
4897 device = TAILQ_NEXT(device, links)) {
4898 if (device->lun_id == lun_id) {
4907 xpt_start_tags(struct cam_path *path)
4909 struct ccb_relsim crs;
4910 struct cam_ed *device;
4911 struct cam_sim *sim;
4914 device = path->device;
4915 sim = path->bus->sim;
4916 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4917 xpt_freeze_devq(path, /*count*/1);
4918 device->inq_flags |= SID_CmdQue;
4919 if (device->tag_saved_openings != 0)
4920 newopenings = device->tag_saved_openings;
4922 newopenings = min(device->maxtags,
4923 sim->max_tagged_dev_openings);
4924 xpt_dev_ccbq_resize(path, newopenings);
4925 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4926 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4927 crs.ccb_h.func_code = XPT_REL_SIMQ;
4928 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4930 = crs.release_timeout
4933 xpt_action((union ccb *)&crs);
4937 xpt_stop_tags(struct cam_path *path)
4939 struct ccb_relsim crs;
4940 struct cam_ed *device;
4941 struct cam_sim *sim;
4943 device = path->device;
4944 sim = path->bus->sim;
4945 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4946 device->tag_delay_count = 0;
4947 xpt_freeze_devq(path, /*count*/1);
4948 device->inq_flags &= ~SID_CmdQue;
4949 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4950 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4951 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4952 crs.ccb_h.func_code = XPT_REL_SIMQ;
4953 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4955 = crs.release_timeout
4958 xpt_action((union ccb *)&crs);
4962 xpt_boot_delay(void *arg)
4969 xpt_config(void *arg)
4972 * Now that interrupts are enabled, go find our devices
4974 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4975 printf("xpt_config: failed to create taskqueue thread.\n");
4977 /* Setup debugging path */
4978 if (cam_dflags != CAM_DEBUG_NONE) {
4979 if (xpt_create_path(&cam_dpath, NULL,
4980 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4981 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4982 printf("xpt_config: xpt_create_path() failed for debug"
4983 " target %d:%d:%d, debugging disabled\n",
4984 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4985 cam_dflags = CAM_DEBUG_NONE;
4990 periphdriver_init(1);
4992 callout_init(&xsoftc.boot_callout, 1);
4993 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
4994 xpt_boot_delay, NULL, 0);
4995 /* Fire up rescan thread. */
4996 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4997 "cam", "scanner")) {
4998 printf("xpt_config: failed to create rescan thread.\n");
5006 xsoftc.buses_to_config++;
5011 xpt_release_boot(void)
5014 xsoftc.buses_to_config--;
5015 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
5016 struct xpt_task *task;
5018 xsoftc.buses_config_done = 1;
5020 /* Call manually because we don't have any busses */
5021 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
5023 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
5024 taskqueue_enqueue(taskqueue_thread, &task->task);
5031 * If the given device only has one peripheral attached to it, and if that
5032 * peripheral is the passthrough driver, announce it. This insures that the
5033 * user sees some sort of announcement for every peripheral in their system.
5036 xptpassannouncefunc(struct cam_ed *device, void *arg)
5038 struct cam_periph *periph;
5041 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5042 periph = SLIST_NEXT(periph, periph_links), i++);
5044 periph = SLIST_FIRST(&device->periphs);
5046 && (strncmp(periph->periph_name, "pass", 4) == 0))
5047 xpt_announce_periph(periph, NULL);
5053 xpt_finishconfig_task(void *context, int pending)
5056 periphdriver_init(2);
5058 * Check for devices with no "standard" peripheral driver
5059 * attached. For any devices like that, announce the
5060 * passthrough driver so the user will see something.
5063 xpt_for_all_devices(xptpassannouncefunc, NULL);
5065 /* Release our hook so that the boot can continue. */
5066 config_intrhook_disestablish(xsoftc.xpt_config_hook);
5067 free(xsoftc.xpt_config_hook, M_CAMXPT);
5068 xsoftc.xpt_config_hook = NULL;
5070 free(context, M_CAMXPT);
5074 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5075 struct cam_path *path)
5077 struct ccb_setasync csa;
5082 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5083 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5084 if (status != CAM_REQ_CMP)
5086 xpt_path_lock(path);
5090 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5091 csa.ccb_h.func_code = XPT_SASYNC_CB;
5092 csa.event_enable = event;
5093 csa.callback = cbfunc;
5094 csa.callback_arg = cbarg;
5095 xpt_action((union ccb *)&csa);
5096 status = csa.ccb_h.status;
5098 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5099 ("xpt_register_async: func %p\n", cbfunc));
5102 xpt_path_unlock(path);
5103 xpt_free_path(path);
5106 if ((status == CAM_REQ_CMP) &&
5107 (csa.event_enable & AC_FOUND_DEVICE)) {
5109 * Get this peripheral up to date with all
5110 * the currently existing devices.
5112 xpt_for_all_devices(xptsetasyncfunc, &csa);
5114 if ((status == CAM_REQ_CMP) &&
5115 (csa.event_enable & AC_PATH_REGISTERED)) {
5117 * Get this peripheral up to date with all
5118 * the currently existing busses.
5120 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5127 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5129 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5131 switch (work_ccb->ccb_h.func_code) {
5132 /* Common cases first */
5133 case XPT_PATH_INQ: /* Path routing inquiry */
5135 struct ccb_pathinq *cpi;
5137 cpi = &work_ccb->cpi;
5138 cpi->version_num = 1; /* XXX??? */
5139 cpi->hba_inquiry = 0;
5140 cpi->target_sprt = 0;
5142 cpi->hba_eng_cnt = 0;
5143 cpi->max_target = 0;
5145 cpi->initiator_id = 0;
5146 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5147 strncpy(cpi->hba_vid, "", HBA_IDLEN);
5148 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5149 cpi->unit_number = sim->unit_number;
5150 cpi->bus_id = sim->bus_id;
5151 cpi->base_transfer_speed = 0;
5152 cpi->protocol = PROTO_UNSPECIFIED;
5153 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5154 cpi->transport = XPORT_UNSPECIFIED;
5155 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5156 cpi->ccb_h.status = CAM_REQ_CMP;
5161 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5168 * The xpt as a "controller" has no interrupt sources, so polling
5172 xptpoll(struct cam_sim *sim)
5177 xpt_lock_buses(void)
5179 mtx_lock(&xsoftc.xpt_topo_lock);
5183 xpt_unlock_buses(void)
5185 mtx_unlock(&xsoftc.xpt_topo_lock);
5189 xpt_path_mtx(struct cam_path *path)
5192 return (&path->device->device_mtx);
5196 xpt_done_process(struct ccb_hdr *ccb_h)
5198 struct cam_sim *sim;
5199 struct cam_devq *devq;
5200 struct mtx *mtx = NULL;
5202 if (ccb_h->flags & CAM_HIGH_POWER) {
5203 struct highpowerlist *hphead;
5204 struct cam_ed *device;
5206 mtx_lock(&xsoftc.xpt_highpower_lock);
5207 hphead = &xsoftc.highpowerq;
5209 device = STAILQ_FIRST(hphead);
5212 * Increment the count since this command is done.
5214 xsoftc.num_highpower++;
5217 * Any high powered commands queued up?
5219 if (device != NULL) {
5221 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5222 mtx_unlock(&xsoftc.xpt_highpower_lock);
5224 mtx_lock(&device->sim->devq->send_mtx);
5225 xpt_release_devq_device(device,
5226 /*count*/1, /*runqueue*/TRUE);
5227 mtx_unlock(&device->sim->devq->send_mtx);
5229 mtx_unlock(&xsoftc.xpt_highpower_lock);
5232 sim = ccb_h->path->bus->sim;
5234 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5235 xpt_release_simq(sim, /*run_queue*/FALSE);
5236 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5239 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5240 && (ccb_h->status & CAM_DEV_QFRZN)) {
5241 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5242 ccb_h->status &= ~CAM_DEV_QFRZN;
5246 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5247 struct cam_ed *dev = ccb_h->path->device;
5249 mtx_lock(&devq->send_mtx);
5250 devq->send_active--;
5251 devq->send_openings++;
5252 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5254 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5255 && (dev->ccbq.dev_active == 0))) {
5256 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5257 xpt_release_devq_device(dev, /*count*/1,
5258 /*run_queue*/FALSE);
5261 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5262 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5263 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5264 xpt_release_devq_device(dev, /*count*/1,
5265 /*run_queue*/FALSE);
5268 if (!device_is_queued(dev))
5269 (void)xpt_schedule_devq(devq, dev);
5271 mtx_unlock(&devq->send_mtx);
5273 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5274 mtx = xpt_path_mtx(ccb_h->path);
5277 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5278 && (--dev->tag_delay_count == 0))
5279 xpt_start_tags(ccb_h->path);
5283 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5285 mtx = xpt_path_mtx(ccb_h->path);
5295 /* Call the peripheral driver's callback */
5296 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5297 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5303 xpt_done_td(void *arg)
5305 struct cam_doneq *queue = arg;
5306 struct ccb_hdr *ccb_h;
5307 STAILQ_HEAD(, ccb_hdr) doneq;
5309 STAILQ_INIT(&doneq);
5310 mtx_lock(&queue->cam_doneq_mtx);
5312 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5313 queue->cam_doneq_sleep = 1;
5314 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5316 queue->cam_doneq_sleep = 0;
5318 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5319 mtx_unlock(&queue->cam_doneq_mtx);
5321 THREAD_NO_SLEEPING();
5322 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5323 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5324 xpt_done_process(ccb_h);
5326 THREAD_SLEEPING_OK();
5328 mtx_lock(&queue->cam_doneq_mtx);
5333 camisr_runqueue(void)
5335 struct ccb_hdr *ccb_h;
5336 struct cam_doneq *queue;
5339 /* Process global queues. */
5340 for (i = 0; i < cam_num_doneqs; i++) {
5341 queue = &cam_doneqs[i];
5342 mtx_lock(&queue->cam_doneq_mtx);
5343 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5344 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5345 mtx_unlock(&queue->cam_doneq_mtx);
5346 xpt_done_process(ccb_h);
5347 mtx_lock(&queue->cam_doneq_mtx);
5349 mtx_unlock(&queue->cam_doneq_mtx);
5359 static struct kv map[] = {
5360 { XPT_NOOP, "XPT_NOOP" },
5361 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5362 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5363 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5364 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5365 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5366 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5367 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5368 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5369 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5370 { XPT_DEBUG, "XPT_DEBUG" },
5371 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5372 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5373 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5374 { XPT_ASYNC, "XPT_ASYNC" },
5375 { XPT_ABORT, "XPT_ABORT" },
5376 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5377 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5378 { XPT_TERM_IO, "XPT_TERM_IO" },
5379 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5380 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5381 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5382 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5383 { XPT_ATA_IO, "XPT_ATA_IO" },
5384 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5385 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5386 { XPT_NVME_IO, "XPT_NVME_IO" },
5387 { XPT_MMCSD_IO, "XPT_MMCSD_IO" },
5388 { XPT_SMP_IO, "XPT_SMP_IO" },
5389 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5390 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5391 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5392 { XPT_EN_LUN, "XPT_EN_LUN" },
5393 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5394 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5395 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5396 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5397 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5398 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5399 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5404 xpt_action_name(uint32_t action)
5406 static char buffer[32]; /* Only for unknown messages -- racy */
5407 struct kv *walker = map;
5409 while (walker->name != NULL) {
5410 if (walker->v == action)
5411 return (walker->name);
5415 snprintf(buffer, sizeof(buffer), "%#x", action);