xref: /freebsd/sys/cam/cam_xpt.c (revision abe835051913f1be4a682be4b0a4f3ff62a75896)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Implementation of the Common Access Method Transport (XPT) layer.
38b8a9b1dSJustin T. Gibbs  *
4c8bead2aSJustin T. Gibbs  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
559190eaaSKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
68b8a9b1dSJustin T. Gibbs  * All rights reserved.
78b8a9b1dSJustin T. Gibbs  *
88b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
108b8a9b1dSJustin T. Gibbs  * are met:
118b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
138b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
148b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
158b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
168b8a9b1dSJustin T. Gibbs  *
178b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
218b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
288b8a9b1dSJustin T. Gibbs  */
299c963d87SDavid E. O'Brien 
309c963d87SDavid E. O'Brien #include <sys/cdefs.h>
319c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
329c963d87SDavid E. O'Brien 
338b8a9b1dSJustin T. Gibbs #include <sys/param.h>
349a94c9c5SJohn Baldwin #include <sys/bus.h>
358b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
368b8a9b1dSJustin T. Gibbs #include <sys/types.h>
378b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
388b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
3987cfaf0eSJustin T. Gibbs #include <sys/time.h>
408b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
418b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
425d754af7SMatt Jacob #include <sys/interrupt.h>
43227d67aaSAlexander Motin #include <sys/proc.h>
443393f8daSKenneth D. Merry #include <sys/sbuf.h>
45227d67aaSAlexander Motin #include <sys/smp.h>
462b83592fSScott Long #include <sys/taskqueue.h>
478b8a9b1dSJustin T. Gibbs 
48ef3cf714SScott Long #include <sys/lock.h>
49ef3cf714SScott Long #include <sys/mutex.h>
503b87a552SMatt Jacob #include <sys/sysctl.h>
519e6461a2SMatt Jacob #include <sys/kthread.h>
52ef3cf714SScott Long 
538b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
548b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
558b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
5652c9ce25SScott Long #include <cam/cam_queue.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
6152c9ce25SScott Long #include <cam/cam_xpt_internal.h>
628b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
6325a2902cSScott Long #include <cam/cam_compat.h>
648b8a9b1dSJustin T. Gibbs 
658b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
668b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
678b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
68abef0e67SMarius Strobl 
69abef0e67SMarius Strobl #include <machine/md_var.h>	/* geometry translation */
70f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
71abef0e67SMarius Strobl 
728b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
738b8a9b1dSJustin T. Gibbs 
7452c9ce25SScott Long /*
7552c9ce25SScott Long  * This is the maximum number of high powered commands (e.g. start unit)
7652c9ce25SScott Long  * that can be outstanding at a particular time.
7752c9ce25SScott Long  */
7852c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER
7952c9ce25SScott Long #define CAM_MAX_HIGHPOWER  4
8052c9ce25SScott Long #endif
8152c9ce25SScott Long 
828b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
83362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
85596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
86596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
878b8a9b1dSJustin T. Gibbs 
882b83592fSScott Long /* Object for defering XPT actions to a taskqueue */
892b83592fSScott Long struct xpt_task {
902b83592fSScott Long 	struct task	task;
9184f82481SScott Long 	void		*data1;
9284f82481SScott Long 	uintptr_t	data2;
932b83592fSScott Long };
942b83592fSScott Long 
958b8a9b1dSJustin T. Gibbs typedef enum {
968b8a9b1dSJustin T. Gibbs 	XPT_FLAG_OPEN		= 0x01
978b8a9b1dSJustin T. Gibbs } xpt_flags;
988b8a9b1dSJustin T. Gibbs 
998b8a9b1dSJustin T. Gibbs struct xpt_softc {
1008b8a9b1dSJustin T. Gibbs 	xpt_flags		flags;
1012b83592fSScott Long 
1022b83592fSScott Long 	/* number of high powered commands that can go through right now */
103ea541bfdSAlexander Motin 	STAILQ_HEAD(highpowerlist, cam_ed)	highpowerq;
1042b83592fSScott Long 	int			num_highpower;
1052b83592fSScott Long 
1062b83592fSScott Long 	/* queue for handling async rescan requests. */
1072b83592fSScott Long 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
10883c5d981SAlexander Motin 	int buses_to_config;
10983c5d981SAlexander Motin 	int buses_config_done;
1102b83592fSScott Long 
1112b83592fSScott Long 	/* Registered busses */
1122b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
1132b83592fSScott Long 	u_int			bus_generation;
1142b83592fSScott Long 
1152b83592fSScott Long 	struct intr_config_hook	*xpt_config_hook;
1162b83592fSScott Long 
11783c5d981SAlexander Motin 	int			boot_delay;
11883c5d981SAlexander Motin 	struct callout 		boot_callout;
11983c5d981SAlexander Motin 
1202b83592fSScott Long 	struct mtx		xpt_topo_lock;
1212b83592fSScott Long 	struct mtx		xpt_lock;
122227d67aaSAlexander Motin 	struct taskqueue	*xpt_taskq;
1238b8a9b1dSJustin T. Gibbs };
1248b8a9b1dSJustin T. Gibbs 
1258b8a9b1dSJustin T. Gibbs typedef enum {
1268b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1278b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1288b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1298b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1308b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1318b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1328b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1338b8a9b1dSJustin T. Gibbs } dev_match_ret;
1348b8a9b1dSJustin T. Gibbs 
1358b8a9b1dSJustin T. Gibbs typedef enum {
1368b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1378b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1388b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1398b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1408b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1418b8a9b1dSJustin T. Gibbs 
1428b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1438b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1448b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1458b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1468b8a9b1dSJustin T. Gibbs };
1478b8a9b1dSJustin T. Gibbs 
1488b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1498b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1508b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1518b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1528b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1538b8a9b1dSJustin T. Gibbs 
1548b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1558b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1568b8a9b1dSJustin T. Gibbs 
15783c5d981SAlexander Motin TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
15883c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
15983c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
16083c5d981SAlexander Motin 
161227d67aaSAlexander Motin struct cam_doneq {
162227d67aaSAlexander Motin 	struct mtx_padalign	cam_doneq_mtx;
163227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	cam_doneq;
164227d67aaSAlexander Motin 	int			cam_doneq_sleep;
165227d67aaSAlexander Motin };
1668b8a9b1dSJustin T. Gibbs 
167227d67aaSAlexander Motin static struct cam_doneq cam_doneqs[MAXCPU];
168227d67aaSAlexander Motin static int cam_num_doneqs;
169227d67aaSAlexander Motin static struct proc *cam_proc;
170227d67aaSAlexander Motin 
171227d67aaSAlexander Motin TUNABLE_INT("kern.cam.num_doneqs", &cam_num_doneqs);
172227d67aaSAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
173227d67aaSAlexander Motin            &cam_num_doneqs, 0, "Number of completion queues/threads");
1748b8a9b1dSJustin T. Gibbs 
1759a1c8571SNick Hibma struct cam_periph *xpt_periph;
1769a1c8571SNick Hibma 
1778b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1788b8a9b1dSJustin T. Gibbs 
1798b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1808b8a9b1dSJustin T. Gibbs {
1818b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1821e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1831e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1848b8a9b1dSJustin T. Gibbs };
1858b8a9b1dSJustin T. Gibbs 
1860b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
1878b8a9b1dSJustin T. Gibbs 
1888b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
1898b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
1908b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
19125a2902cSScott Long static d_ioctl_t xptdoioctl;
1928b8a9b1dSJustin T. Gibbs 
1934e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
194dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1952b83592fSScott Long 	.d_flags =	0,
1967ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
1977ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
1987ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
1997ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
2008b8a9b1dSJustin T. Gibbs };
2018b8a9b1dSJustin T. Gibbs 
2028b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
2038b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
20482b361b1SMatt Jacob u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
20582b361b1SMatt Jacob TUNABLE_INT("kern.cam.dflags", &cam_dflags);
206240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
207f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
208f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
20982b361b1SMatt Jacob TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
210240577c2SMatthew D Fleming SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
211f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2128b8a9b1dSJustin T. Gibbs 
2136d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
21474bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
21574bd1c10SNick Hibma 
21674bd1c10SNick Hibma static moduledata_t cam_moduledata = {
21774bd1c10SNick Hibma 	"cam",
21874bd1c10SNick Hibma 	cam_module_event_handler,
21974bd1c10SNick Hibma 	NULL
22074bd1c10SNick Hibma };
22174bd1c10SNick Hibma 
2222b83592fSScott Long static int	xpt_init(void *);
22374bd1c10SNick Hibma 
22474bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
22574bd1c10SNick Hibma MODULE_VERSION(cam, 1);
22674bd1c10SNick Hibma 
2278b8a9b1dSJustin T. Gibbs 
2288b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2298b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
2308b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2318b8a9b1dSJustin T. Gibbs 					void *async_arg);
232434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
233434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
234227d67aaSAlexander Motin static union ccb *xpt_get_ccb(struct cam_periph *periph);
235227d67aaSAlexander Motin static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
236227d67aaSAlexander Motin static void	 xpt_run_allocq(struct cam_periph *periph, int sleep);
237227d67aaSAlexander Motin static void	 xpt_run_allocq_task(void *context, int pending);
238cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2398b8a9b1dSJustin T. Gibbs static timeout_t xpt_release_devq_timeout;
2402b83592fSScott Long static void	 xpt_release_simq_timeout(void *arg) __unused;
241227d67aaSAlexander Motin static void	 xpt_acquire_bus(struct cam_eb *bus);
242a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
243227d67aaSAlexander Motin static int	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
244cccf4220SAlexander Motin 		    int run_queue);
2458b8a9b1dSJustin T. Gibbs static struct cam_et*
2468b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
247227d67aaSAlexander Motin static void	 xpt_acquire_target(struct cam_et *target);
248f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2498b8a9b1dSJustin T. Gibbs static struct cam_eb*
2508b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2518b8a9b1dSJustin T. Gibbs static struct cam_et*
2528b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2538b8a9b1dSJustin T. Gibbs static struct cam_ed*
2548b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2558b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
256227d67aaSAlexander Motin static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
257227d67aaSAlexander Motin 				 u_int32_t new_priority);
2588b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2598b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
260434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
261227d67aaSAlexander Motin static void	 camisr_runqueue(void);
262227d67aaSAlexander Motin static void	 xpt_done_process(struct ccb_hdr *ccb_h);
263227d67aaSAlexander Motin static void	 xpt_done_td(void *);
2648b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2653393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2668b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2673393f8daSKenneth D. Merry 				       u_int num_patterns,
2683393f8daSKenneth D. Merry 				       struct cam_ed *device);
2698b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2703393f8daSKenneth D. Merry 				       u_int num_patterns,
2718b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2728b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2738b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2748b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2758b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2768b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2778b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2788b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2798b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2808b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2818b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2828b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2838b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2848b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2858b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2868b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2878b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
2888b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
2898b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
2908b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
2918b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
2928b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
2938b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
2948b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
2958b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
2968b8a9b1dSJustin T. Gibbs 					    void *arg);
2978b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
2988b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
2998b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
3008b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
30183c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
30252c9ce25SScott Long static void		xpt_dev_async_default(u_int32_t async_code,
30352c9ce25SScott Long 					      struct cam_eb *bus,
30452c9ce25SScott Long 					      struct cam_et *target,
30552c9ce25SScott Long 					      struct cam_ed *device,
30652c9ce25SScott Long 					      void *async_arg);
30752c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
30852c9ce25SScott Long 						 struct cam_et *target,
30952c9ce25SScott Long 						 lun_id_t lun_id);
3108b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
3118b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
3128b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
3138b8a9b1dSJustin T. Gibbs 				    void *arg);
314cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device);
3158b8a9b1dSJustin T. Gibbs 
3168b8a9b1dSJustin T. Gibbs static __inline int
317cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
31830a4094fSAlexander Motin {
31930a4094fSAlexander Motin 	int	retval;
32030a4094fSAlexander Motin 
321227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
32283c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
32383c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
324cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
32530a4094fSAlexander Motin 		/*
32630a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3276bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
32830a4094fSAlexander Motin 		 * enqueued.
32930a4094fSAlexander Motin 		 */
33030a4094fSAlexander Motin 		retval =
331cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
332227d67aaSAlexander Motin 				     &dev->devq_entry,
33383c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
33430a4094fSAlexander Motin 	} else {
33530a4094fSAlexander Motin 		retval = 0;
33630a4094fSAlexander Motin 	}
33730a4094fSAlexander Motin 	return (retval);
33830a4094fSAlexander Motin }
33930a4094fSAlexander Motin 
34030a4094fSAlexander Motin static __inline int
341cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3428b8a9b1dSJustin T. Gibbs {
343227d67aaSAlexander Motin 	return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
3448b8a9b1dSJustin T. Gibbs }
3458b8a9b1dSJustin T. Gibbs 
3468b8a9b1dSJustin T. Gibbs static void
3478b8a9b1dSJustin T. Gibbs xpt_periph_init()
3488b8a9b1dSJustin T. Gibbs {
34973d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3508b8a9b1dSJustin T. Gibbs }
3518b8a9b1dSJustin T. Gibbs 
3528b8a9b1dSJustin T. Gibbs static int
35389c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3548b8a9b1dSJustin T. Gibbs {
3558b8a9b1dSJustin T. Gibbs 
3568b8a9b1dSJustin T. Gibbs 	/*
35766a0780eSKenneth D. Merry 	 * Only allow read-write access.
35866a0780eSKenneth D. Merry 	 */
35966a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
36066a0780eSKenneth D. Merry 		return(EPERM);
36166a0780eSKenneth D. Merry 
36266a0780eSKenneth D. Merry 	/*
3638b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3648b8a9b1dSJustin T. Gibbs 	 */
3658b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3662b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3678b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3688b8a9b1dSJustin T. Gibbs 	}
3698b8a9b1dSJustin T. Gibbs 
3708b8a9b1dSJustin T. Gibbs 	/* Mark ourselves open */
3712b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3728b8a9b1dSJustin T. Gibbs 	xsoftc.flags |= XPT_FLAG_OPEN;
3732b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3748b8a9b1dSJustin T. Gibbs 
3758b8a9b1dSJustin T. Gibbs 	return(0);
3768b8a9b1dSJustin T. Gibbs }
3778b8a9b1dSJustin T. Gibbs 
3788b8a9b1dSJustin T. Gibbs static int
37989c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3808b8a9b1dSJustin T. Gibbs {
3818b8a9b1dSJustin T. Gibbs 
3828b8a9b1dSJustin T. Gibbs 	/* Mark ourselves closed */
3832b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
3848b8a9b1dSJustin T. Gibbs 	xsoftc.flags &= ~XPT_FLAG_OPEN;
3852b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
3868b8a9b1dSJustin T. Gibbs 
3878b8a9b1dSJustin T. Gibbs 	return(0);
3888b8a9b1dSJustin T. Gibbs }
3898b8a9b1dSJustin T. Gibbs 
3902b83592fSScott Long /*
3912b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3922b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3932b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3942b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3952b83592fSScott Long  */
3968b8a9b1dSJustin T. Gibbs static int
39789c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3988b8a9b1dSJustin T. Gibbs {
3992b83592fSScott Long 	int error;
4008b8a9b1dSJustin T. Gibbs 
40125a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
402f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
40325a2902cSScott Long 	}
40425a2902cSScott Long 	return (error);
40525a2902cSScott Long }
40625a2902cSScott Long 
40725a2902cSScott Long static int
40825a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
40925a2902cSScott Long {
41025a2902cSScott Long 	int error;
41125a2902cSScott Long 
4128b8a9b1dSJustin T. Gibbs 	error = 0;
4138b8a9b1dSJustin T. Gibbs 
4148b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4158b8a9b1dSJustin T. Gibbs 	/*
4168b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4178b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4188c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4198c7a96c5SScott Long 	 * in the CAM spec.
4208b8a9b1dSJustin T. Gibbs 	 */
4218b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4228b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4238b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4242b83592fSScott Long 		struct cam_eb *bus;
4258b8a9b1dSJustin T. Gibbs 
4268b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4278b8a9b1dSJustin T. Gibbs 
4282b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4290e85f214SMatt Jacob 		if (bus == NULL)
4300e85f214SMatt Jacob 			return (EINVAL);
4310e85f214SMatt Jacob 
4320e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4330e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4340e85f214SMatt Jacob 		case XPT_RESET_BUS:
4350e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4360e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4370e85f214SMatt Jacob 				xpt_release_bus(bus);
4380e85f214SMatt Jacob 				return (EINVAL);
4390e85f214SMatt Jacob 			}
4400e85f214SMatt Jacob 			break;
4410e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4420e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4430e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4440e85f214SMatt Jacob 				xpt_release_bus(bus);
4450e85f214SMatt Jacob 				return (EINVAL);
4460e85f214SMatt Jacob 			}
4470e85f214SMatt Jacob 			break;
4480e85f214SMatt Jacob 		default:
4492b83592fSScott Long 			break;
4502b83592fSScott Long 		}
4512b83592fSScott Long 
4528b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4538b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4548b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4558c7a96c5SScott Long 		case XPT_PATH_INQ:
4568fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4578b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4580e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4598b8a9b1dSJustin T. Gibbs 
4608008a935SScott Long 			ccb = xpt_alloc_ccb();
4612b83592fSScott Long 
4628b8a9b1dSJustin T. Gibbs 			/*
4638b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4648b8a9b1dSJustin T. Gibbs 			 * user passed in.
4658b8a9b1dSJustin T. Gibbs 			 */
466e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4678b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4688b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4698b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4708b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4718b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4728b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4738b8a9b1dSJustin T. Gibbs 				break;
4748b8a9b1dSJustin T. Gibbs 			}
4758b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4768b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4778b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4788b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
479227d67aaSAlexander Motin 			xpt_path_lock(ccb->ccb_h.path);
4808b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
481227d67aaSAlexander Motin 			xpt_path_unlock(ccb->ccb_h.path);
4828b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4838b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4848b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4858b8a9b1dSJustin T. Gibbs 			break;
4868b8a9b1dSJustin T. Gibbs 
4878b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4888b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4898b8a9b1dSJustin T. Gibbs 
4908b8a9b1dSJustin T. Gibbs 			/*
491aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
4928b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
4938b8a9b1dSJustin T. Gibbs 			 */
4948b8a9b1dSJustin T. Gibbs 
4958b8a9b1dSJustin T. Gibbs 			/*
4968b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4978b8a9b1dSJustin T. Gibbs 			 * user passed in.
4988b8a9b1dSJustin T. Gibbs 			 */
499e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
5008b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
5018b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
5028b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
5038b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
5048b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5058b8a9b1dSJustin T. Gibbs 				break;
5068b8a9b1dSJustin T. Gibbs 			}
5078b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5088b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5098b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5108b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5118b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5128b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5138b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
5148b8a9b1dSJustin T. Gibbs 			break;
5158b8a9b1dSJustin T. Gibbs 
5168b8a9b1dSJustin T. Gibbs 		}
5178b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5188b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
51959190eaaSKenneth D. Merry 			struct cam_path *old_path;
5208b8a9b1dSJustin T. Gibbs 
5218b8a9b1dSJustin T. Gibbs 			/*
5228b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5238b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5248b8a9b1dSJustin T. Gibbs 			 */
525dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
526dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5278b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5288b8a9b1dSJustin T. Gibbs 				break;
5298b8a9b1dSJustin T. Gibbs 			}
53059190eaaSKenneth D. Merry 
53159190eaaSKenneth D. Merry 			/*
53259190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
53359190eaaSKenneth D. Merry 			 * something in particular.
53459190eaaSKenneth D. Merry 			 */
53559190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
53659190eaaSKenneth D. Merry 
53759190eaaSKenneth D. Merry 			/*
53859190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
53959190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
54059190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
54159190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
54259190eaaSKenneth D. Merry 			 */
54359190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
54459190eaaSKenneth D. Merry 
5458b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5468b8a9b1dSJustin T. Gibbs 
5478b8a9b1dSJustin T. Gibbs 			/*
5488b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5498b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5508b8a9b1dSJustin T. Gibbs 			 */
5518b8a9b1dSJustin T. Gibbs 			error = cam_periph_mapmem(inccb, &mapinfo);
5528b8a9b1dSJustin T. Gibbs 
55359190eaaSKenneth D. Merry 			if (error) {
55459190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5558b8a9b1dSJustin T. Gibbs 				break;
55659190eaaSKenneth D. Merry 			}
5578b8a9b1dSJustin T. Gibbs 
5588b8a9b1dSJustin T. Gibbs 			/*
5598b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5608b8a9b1dSJustin T. Gibbs 			 */
5618b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
5628b8a9b1dSJustin T. Gibbs 
5638b8a9b1dSJustin T. Gibbs 			/*
5648b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5658b8a9b1dSJustin T. Gibbs 			 */
5668b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
5678b8a9b1dSJustin T. Gibbs 
56859190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
56959190eaaSKenneth D. Merry 
5708b8a9b1dSJustin T. Gibbs 			error = 0;
5718b8a9b1dSJustin T. Gibbs 			break;
5728b8a9b1dSJustin T. Gibbs 		}
5738b8a9b1dSJustin T. Gibbs 		default:
5748fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5758b8a9b1dSJustin T. Gibbs 			break;
5768b8a9b1dSJustin T. Gibbs 		}
577daddc001SScott Long 		xpt_release_bus(bus);
5788b8a9b1dSJustin T. Gibbs 		break;
5798b8a9b1dSJustin T. Gibbs 	}
5808b8a9b1dSJustin T. Gibbs 	/*
5818b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5828b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5838b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5848b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5858b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5868b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5878b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5888b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
5898b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
5908b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
59177dc25ccSScott Long 	 * we do it with lock protection.
5928b8a9b1dSJustin T. Gibbs 	 *
5938b8a9b1dSJustin T. Gibbs 	 */
5948b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
5958b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
5968b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
5978b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
5988b8a9b1dSJustin T. Gibbs 		char   *name;
5993393f8daSKenneth D. Merry 		u_int unit;
600621a60d4SKenneth D. Merry 		int base_periph_found;
6018b8a9b1dSJustin T. Gibbs 
6028b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6038b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
6048b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
605621a60d4SKenneth D. Merry 		base_periph_found = 0;
606621a60d4SKenneth D. Merry 
6078b8a9b1dSJustin T. Gibbs 		/*
6088b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6098b8a9b1dSJustin T. Gibbs 		 * driver name.
6108b8a9b1dSJustin T. Gibbs 		 */
6118b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6128b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6138b8a9b1dSJustin T. Gibbs 			break;
6148b8a9b1dSJustin T. Gibbs 		}
6158b8a9b1dSJustin T. Gibbs 
6168b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6179a7c2696SAlexander Motin 		xpt_lock_buses();
6188b8a9b1dSJustin T. Gibbs 
6198b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6200b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6218b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6228b8a9b1dSJustin T. Gibbs 				break;
6238b8a9b1dSJustin T. Gibbs 
6248b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6259a7c2696SAlexander Motin 			xpt_unlock_buses();
6268b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6278b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6288b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6298b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6308b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6318b8a9b1dSJustin T. Gibbs 			break;
6328b8a9b1dSJustin T. Gibbs 		}
6338b8a9b1dSJustin T. Gibbs 
6348b8a9b1dSJustin T. Gibbs 		/*
6358b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6368b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6378b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6388b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6398b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6408b8a9b1dSJustin T. Gibbs 		 */
6418b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6428b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
6438b8a9b1dSJustin T. Gibbs 
644a29779e8SAlexander Motin 			if (periph->unit_number == unit)
6458b8a9b1dSJustin T. Gibbs 				break;
6468b8a9b1dSJustin T. Gibbs 		}
6478b8a9b1dSJustin T. Gibbs 		/*
6488b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6498b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6508b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6518b8a9b1dSJustin T. Gibbs 		 */
6528b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6538b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6548b8a9b1dSJustin T. Gibbs 			int i;
6558b8a9b1dSJustin T. Gibbs 
656621a60d4SKenneth D. Merry 			base_periph_found = 1;
6578b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
658fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6598b8a9b1dSJustin T. Gibbs 			     periph != NULL;
660fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6618b8a9b1dSJustin T. Gibbs 				/*
6628b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6638b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6648b8a9b1dSJustin T. Gibbs 				 */
6658b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6668b8a9b1dSJustin T. Gibbs 					/*
6678b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
6688b8a9b1dSJustin T. Gibbs 					 */
6698b8a9b1dSJustin T. Gibbs 					strcpy(ccb->cgdl.periph_name,
6708b8a9b1dSJustin T. Gibbs 					       periph->periph_name);
6718b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
6728b8a9b1dSJustin T. Gibbs 						periph->unit_number;
673fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
6748b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6758b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
6768b8a9b1dSJustin T. Gibbs 					else
6778b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6788b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
6798b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
6808b8a9b1dSJustin T. Gibbs 						device->generation;
6818b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
6828b8a9b1dSJustin T. Gibbs 					/*
6838b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
6848b8a9b1dSJustin T. Gibbs 					 * that the user may want.
6858b8a9b1dSJustin T. Gibbs 					 */
6868b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
6878b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
6888b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
6898b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
6908b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
6918b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
6928b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
6938b8a9b1dSJustin T. Gibbs 					break;
6948b8a9b1dSJustin T. Gibbs 				}
6958b8a9b1dSJustin T. Gibbs 			}
6968b8a9b1dSJustin T. Gibbs 		}
6978b8a9b1dSJustin T. Gibbs 
6988b8a9b1dSJustin T. Gibbs 		/*
6998b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
7008b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
7018b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7028b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7038b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7048b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7058b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7068b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7078b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7088b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7098b8a9b1dSJustin T. Gibbs 		 */
7108b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7118b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7128b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7138b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7148b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7158b8a9b1dSJustin T. Gibbs 			error = ENOENT;
716621a60d4SKenneth D. Merry 			/*
717621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
718621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
719621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
720621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
721621a60d4SKenneth D. Merry 			 * kernel.
722621a60d4SKenneth D. Merry 			 */
723621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
724621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
725621a60d4SKenneth D. Merry 				       "kernel\n");
726935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
727621a60d4SKenneth D. Merry 				       "your kernel config file\n");
728621a60d4SKenneth D. Merry 			}
7298b8a9b1dSJustin T. Gibbs 		}
7309a7c2696SAlexander Motin 		xpt_unlock_buses();
7318b8a9b1dSJustin T. Gibbs 		break;
7328b8a9b1dSJustin T. Gibbs 		}
7338b8a9b1dSJustin T. Gibbs 	default:
7348b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7358b8a9b1dSJustin T. Gibbs 		break;
7368b8a9b1dSJustin T. Gibbs 	}
7378b8a9b1dSJustin T. Gibbs 
7388b8a9b1dSJustin T. Gibbs 	return(error);
7398b8a9b1dSJustin T. Gibbs }
7408b8a9b1dSJustin T. Gibbs 
74174bd1c10SNick Hibma static int
74274bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
74374bd1c10SNick Hibma {
7442b83592fSScott Long 	int error;
7452b83592fSScott Long 
7462b83592fSScott Long 	switch (what) {
7472b83592fSScott Long 	case MOD_LOAD:
7482b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7492b83592fSScott Long 			return (error);
7502b83592fSScott Long 		break;
7512b83592fSScott Long 	case MOD_UNLOAD:
75274bd1c10SNick Hibma 		return EBUSY;
7532b83592fSScott Long 	default:
7543e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
75574bd1c10SNick Hibma 	}
75674bd1c10SNick Hibma 
75774bd1c10SNick Hibma 	return 0;
75874bd1c10SNick Hibma }
75974bd1c10SNick Hibma 
76083c5d981SAlexander Motin static void
76183c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
76283c5d981SAlexander Motin {
76383c5d981SAlexander Motin 
76483c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
76583c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
76683c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
76783c5d981SAlexander Motin 	} else {
76883c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
76983c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
77083c5d981SAlexander Motin 	}
77183c5d981SAlexander Motin 	xpt_release_boot();
77283c5d981SAlexander Motin }
77383c5d981SAlexander Motin 
7749e6461a2SMatt Jacob /* thread to handle bus rescans */
7759e6461a2SMatt Jacob static void
7769e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
7779e6461a2SMatt Jacob {
7789e6461a2SMatt Jacob 	union ccb	*ccb;
779227d67aaSAlexander Motin 	struct cam_path	 path;
7802b83592fSScott Long 
7812b83592fSScott Long 	xpt_lock_buses();
78283c5d981SAlexander Motin 	for (;;) {
7835a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
7842b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
7852b83592fSScott Long 			       "ccb_scanq", 0);
78683c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
78783c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
7882b83592fSScott Long 			xpt_unlock_buses();
7892b83592fSScott Long 
790227d67aaSAlexander Motin 			/*
791227d67aaSAlexander Motin 			 * Since lock can be dropped inside and path freed
792227d67aaSAlexander Motin 			 * by completion callback even before return here,
793227d67aaSAlexander Motin 			 * take our own path copy for reference.
794227d67aaSAlexander Motin 			 */
795227d67aaSAlexander Motin 			xpt_copy_path(&path, ccb->ccb_h.path);
796227d67aaSAlexander Motin 			xpt_path_lock(&path);
79783c5d981SAlexander Motin 			xpt_action(ccb);
798227d67aaSAlexander Motin 			xpt_path_unlock(&path);
799227d67aaSAlexander Motin 			xpt_release_path(&path);
80083c5d981SAlexander Motin 
80183c5d981SAlexander Motin 			xpt_lock_buses();
8029e6461a2SMatt Jacob 		}
8039e6461a2SMatt Jacob 	}
8049e6461a2SMatt Jacob }
8059e6461a2SMatt Jacob 
8069e6461a2SMatt Jacob void
8079e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8089e6461a2SMatt Jacob {
8099e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8102b83592fSScott Long 
81183c5d981SAlexander Motin 	/* Prepare request */
8120e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
813411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
81483c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8150e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8160e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8170e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8180e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8190e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
82083c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8210e85f214SMatt Jacob 	else {
8220e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8230e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8240e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8250e85f214SMatt Jacob 		return;
8260e85f214SMatt Jacob 	}
82783c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
82883c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
82983c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
83083c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8312b83592fSScott Long 	xpt_lock_buses();
83283c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8332b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8349e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8355a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8362b83592fSScott Long 				xpt_unlock_buses();
8379e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8389e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8399e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8409e6461a2SMatt Jacob 				return;
8419e6461a2SMatt Jacob 			}
8429e6461a2SMatt Jacob 		}
84383c5d981SAlexander Motin 	}
8442b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
84583c5d981SAlexander Motin 	xsoftc.buses_to_config++;
8462b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8472b83592fSScott Long 	xpt_unlock_buses();
8489e6461a2SMatt Jacob }
8499e6461a2SMatt Jacob 
8508b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8512b83592fSScott Long static int
8529e6461a2SMatt Jacob xpt_init(void *dummy)
8538b8a9b1dSJustin T. Gibbs {
8548b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8558b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
856434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8578b8a9b1dSJustin T. Gibbs 	cam_status status;
858227d67aaSAlexander Motin 	int error, i;
8598b8a9b1dSJustin T. Gibbs 
8602b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8612b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8622b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8632b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8648b8a9b1dSJustin T. Gibbs 
8652b83592fSScott Long 	mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
8662b83592fSScott Long 	mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
867227d67aaSAlexander Motin 	xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
868227d67aaSAlexander Motin 	    taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
869ef3cf714SScott Long 
8706b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
8716b156f61SSean Bruno 	/*
8726b156f61SSean Bruno 	 * Override this value at compile time to assist our users
8736b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
8746b156f61SSean Bruno 	 */
8756b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
8766b156f61SSean Bruno #endif
8778b8a9b1dSJustin T. Gibbs 	/*
8788b8a9b1dSJustin T. Gibbs 	 * The xpt layer is, itself, the equivelent of a SIM.
8798b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
8808b8a9b1dSJustin T. Gibbs 	 * give decent parallelism when we probe busses and
8818b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
8828b8a9b1dSJustin T. Gibbs 	 */
883434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
884434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
885434bbf6eSJustin T. Gibbs 				xptpoll,
886434bbf6eSJustin T. Gibbs 				"xpt",
887434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
888434bbf6eSJustin T. Gibbs 				/*unit*/0,
8892b83592fSScott Long 				/*mtx*/&xsoftc.xpt_lock,
890434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
891434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
892434bbf6eSJustin T. Gibbs 				devq);
8932b83592fSScott Long 	if (xpt_sim == NULL)
8942b83592fSScott Long 		return (ENOMEM);
8958b8a9b1dSJustin T. Gibbs 
8962b83592fSScott Long 	mtx_lock(&xsoftc.xpt_lock);
897b50569b7SScott Long 	if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
89883c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
899a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
900df826980SMatt Jacob 		       " failing attach\n", status);
9012b83592fSScott Long 		return (EINVAL);
902df826980SMatt Jacob 	}
9038b8a9b1dSJustin T. Gibbs 
9048b8a9b1dSJustin T. Gibbs 	/*
9058b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9068b8a9b1dSJustin T. Gibbs 	 * the equivelent of a peripheral driver.  Allocate
9078b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9088b8a9b1dSJustin T. Gibbs 	 */
9098b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9108b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9118b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
91283c5d981SAlexander Motin 		mtx_unlock(&xsoftc.xpt_lock);
9138b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9148b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9152b83592fSScott Long 		return (EINVAL);
9168b8a9b1dSJustin T. Gibbs 	}
9178b8a9b1dSJustin T. Gibbs 
918ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9192b83592fSScott Long 			 path, NULL, 0, xpt_sim);
9208b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
9212b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_lock);
922227d67aaSAlexander Motin 	if (cam_num_doneqs < 1)
923227d67aaSAlexander Motin 		cam_num_doneqs = 1 + mp_ncpus / 6;
924227d67aaSAlexander Motin 	else if (cam_num_doneqs > MAXCPU)
925227d67aaSAlexander Motin 		cam_num_doneqs = MAXCPU;
926227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
927227d67aaSAlexander Motin 		mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
928227d67aaSAlexander Motin 		    MTX_DEF);
929227d67aaSAlexander Motin 		STAILQ_INIT(&cam_doneqs[i].cam_doneq);
930227d67aaSAlexander Motin 		error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
931227d67aaSAlexander Motin 		    &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
932227d67aaSAlexander Motin 		if (error != 0) {
933227d67aaSAlexander Motin 			cam_num_doneqs = i;
934227d67aaSAlexander Motin 			break;
935227d67aaSAlexander Motin 		}
936227d67aaSAlexander Motin 	}
937227d67aaSAlexander Motin 	if (cam_num_doneqs < 1) {
938227d67aaSAlexander Motin 		printf("xpt_init: Cannot init completion queues "
939227d67aaSAlexander Motin 		       "- failing attach\n");
940227d67aaSAlexander Motin 		return (ENOMEM);
941227d67aaSAlexander Motin 	}
9428b8a9b1dSJustin T. Gibbs 	/*
9438b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9448b8a9b1dSJustin T. Gibbs 	 */
9452b83592fSScott Long 	xsoftc.xpt_config_hook =
9468b8a9b1dSJustin T. Gibbs 	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
9470dd50e9bSScott Long 					      M_CAMXPT, M_NOWAIT | M_ZERO);
9482b83592fSScott Long 	if (xsoftc.xpt_config_hook == NULL) {
9498b8a9b1dSJustin T. Gibbs 		printf("xpt_init: Cannot malloc config hook "
9508b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9512b83592fSScott Long 		return (ENOMEM);
9528b8a9b1dSJustin T. Gibbs 	}
9532b83592fSScott Long 	xsoftc.xpt_config_hook->ich_func = xpt_config;
9542b83592fSScott Long 	if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
9550dd50e9bSScott Long 		free (xsoftc.xpt_config_hook, M_CAMXPT);
9568b8a9b1dSJustin T. Gibbs 		printf("xpt_init: config_intrhook_establish failed "
9578b8a9b1dSJustin T. Gibbs 		       "- failing attach\n");
9588b8a9b1dSJustin T. Gibbs 	}
9598b8a9b1dSJustin T. Gibbs 
9602b83592fSScott Long 	return (0);
9618b8a9b1dSJustin T. Gibbs }
9628b8a9b1dSJustin T. Gibbs 
9638b8a9b1dSJustin T. Gibbs static cam_status
9648b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9658b8a9b1dSJustin T. Gibbs {
9662b83592fSScott Long 	struct cam_sim *xpt_sim;
9672b83592fSScott Long 
9688b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9698b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9708b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9718b8a9b1dSJustin T. Gibbs 	}
9728b8a9b1dSJustin T. Gibbs 
9732b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9742b83592fSScott Long 	xpt_sim->softc = periph;
9758b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
9762b83592fSScott Long 	periph->softc = NULL;
9778b8a9b1dSJustin T. Gibbs 
9788b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
9798b8a9b1dSJustin T. Gibbs }
9808b8a9b1dSJustin T. Gibbs 
9818b8a9b1dSJustin T. Gibbs int32_t
9828b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
9838b8a9b1dSJustin T. Gibbs {
9848b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
9858b8a9b1dSJustin T. Gibbs 	int32_t	 status;
9868b8a9b1dSJustin T. Gibbs 
987227d67aaSAlexander Motin 	TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
9888b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
9898b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
9908b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
991227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
9928b8a9b1dSJustin T. Gibbs 		device->generation++;
993227d67aaSAlexander Motin 		SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
994227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
9958b8a9b1dSJustin T. Gibbs 	}
9968b8a9b1dSJustin T. Gibbs 
9978b8a9b1dSJustin T. Gibbs 	return (status);
9988b8a9b1dSJustin T. Gibbs }
9998b8a9b1dSJustin T. Gibbs 
10008b8a9b1dSJustin T. Gibbs void
1001a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph)
10028b8a9b1dSJustin T. Gibbs {
10038b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10048b8a9b1dSJustin T. Gibbs 
10058b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10068b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1007227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10088b8a9b1dSJustin T. Gibbs 		device->generation++;
1009227d67aaSAlexander Motin 		SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1010227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
10118b8a9b1dSJustin T. Gibbs 	}
10128b8a9b1dSJustin T. Gibbs }
10138b8a9b1dSJustin T. Gibbs 
10143393f8daSKenneth D. Merry 
10153393f8daSKenneth D. Merry void
10163393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10173393f8daSKenneth D. Merry {
101857079b17SAlexander Motin 	struct	cam_path *path = periph->path;
10193393f8daSKenneth D. Merry 
1020227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
10218d36a71bSAlexander Motin 	periph->flags |= CAM_PERIPH_ANNOUNCED;
102268153f43SScott Long 
1023*abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10243393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
10253393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
10263393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
10273393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
1028ad413009SAlexander Motin 	       path->bus->path_id,
10293393f8daSKenneth D. Merry 	       path->target->target_id,
1030*abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
10313393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
103252c9ce25SScott Long 	if (path->device->protocol == PROTO_SCSI)
10333393f8daSKenneth D. Merry 		scsi_print_inquiry(&path->device->inq_data);
103452c9ce25SScott Long 	else if (path->device->protocol == PROTO_ATA ||
103552c9ce25SScott Long 	    path->device->protocol == PROTO_SATAPM)
103652c9ce25SScott Long 		ata_print_ident(&path->device->ident_data);
10373089bb2eSAlexander Motin 	else if (path->device->protocol == PROTO_SEMB)
10383089bb2eSAlexander Motin 		semb_print_ident(
10393089bb2eSAlexander Motin 		    (struct sep_identify_data *)&path->device->ident_data);
104052c9ce25SScott Long 	else
104152c9ce25SScott Long 		printf("Unknown protocol device\n");
1042aa93041dSAlexander Motin 	if (path->device->serial_num_len > 0) {
10433393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
10443393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
10453393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
10463393f8daSKenneth D. Merry 	}
104757079b17SAlexander Motin 	/* Announce transport details. */
104857079b17SAlexander Motin 	(*(path->bus->xport->announce))(periph);
104957079b17SAlexander Motin 	/* Announce command queueing. */
10503393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
10513393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10520aacc535SAlexander Motin 		printf("%s%d: Command Queueing enabled\n",
10533393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
10543393f8daSKenneth D. Merry 	}
105557079b17SAlexander Motin 	/* Announce caller's details if they've passed in. */
10563393f8daSKenneth D. Merry 	if (announce_string != NULL)
10573393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
10583393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
10593393f8daSKenneth D. Merry }
10608b8a9b1dSJustin T. Gibbs 
10616fb5c84eSSteven Hartland void
10626fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
10636fb5c84eSSteven Hartland {
10646fb5c84eSSteven Hartland 	if (quirks != 0) {
10656fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
10666fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
10676fb5c84eSSteven Hartland 	}
10686fb5c84eSSteven Hartland }
10696fb5c84eSSteven Hartland 
10708d36a71bSAlexander Motin void
10718d36a71bSAlexander Motin xpt_denounce_periph(struct cam_periph *periph)
10728d36a71bSAlexander Motin {
10738d36a71bSAlexander Motin 	struct	cam_path *path = periph->path;
10748d36a71bSAlexander Motin 
1075227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
1076*abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10778d36a71bSAlexander Motin 	       periph->periph_name, periph->unit_number,
10788d36a71bSAlexander Motin 	       path->bus->sim->sim_name,
10798d36a71bSAlexander Motin 	       path->bus->sim->unit_number,
10808d36a71bSAlexander Motin 	       path->bus->sim->bus_id,
10818d36a71bSAlexander Motin 	       path->bus->path_id,
10828d36a71bSAlexander Motin 	       path->target->target_id,
1083*abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
10848d36a71bSAlexander Motin 	printf("%s%d: ", periph->periph_name, periph->unit_number);
10858d36a71bSAlexander Motin 	if (path->device->protocol == PROTO_SCSI)
10868d36a71bSAlexander Motin 		scsi_print_inquiry_short(&path->device->inq_data);
10878d36a71bSAlexander Motin 	else if (path->device->protocol == PROTO_ATA ||
10888d36a71bSAlexander Motin 	    path->device->protocol == PROTO_SATAPM)
10898d36a71bSAlexander Motin 		ata_print_ident_short(&path->device->ident_data);
10908d36a71bSAlexander Motin 	else if (path->device->protocol == PROTO_SEMB)
10918d36a71bSAlexander Motin 		semb_print_ident_short(
10928d36a71bSAlexander Motin 		    (struct sep_identify_data *)&path->device->ident_data);
10938d36a71bSAlexander Motin 	else
10948d36a71bSAlexander Motin 		printf("Unknown protocol device");
10958d36a71bSAlexander Motin 	if (path->device->serial_num_len > 0)
10968d36a71bSAlexander Motin 		printf(" s/n %.60s", path->device->serial_num);
10978d36a71bSAlexander Motin 	printf(" detached\n");
10988d36a71bSAlexander Motin }
10998d36a71bSAlexander Motin 
11008d36a71bSAlexander Motin 
11013501942bSJustin T. Gibbs int
11023501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
11033501942bSJustin T. Gibbs {
1104ccba7102SAlexander Motin 	int ret = -1, l;
11053501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
1106ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
11073501942bSJustin T. Gibbs 
1108227d67aaSAlexander Motin 	xpt_path_assert(path, MA_OWNED);
11096884b662SAlexander Motin 
11103501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
11113501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
11123501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
11133501942bSJustin T. Gibbs 	cdai.bufsiz = len;
11143501942bSJustin T. Gibbs 
11153501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
11163501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
11173501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
11183501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
111940f27d7cSAlexander Motin 	else if (strcmp(attr, "GEOM::lunid") == 0 ||
112040f27d7cSAlexander Motin 		 strcmp(attr, "GEOM::lunname") == 0) {
1121ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1122ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1123ccba7102SAlexander Motin 	} else
11243501942bSJustin T. Gibbs 		goto out;
11253501942bSJustin T. Gibbs 
11263501942bSJustin T. Gibbs 	cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
11273501942bSJustin T. Gibbs 	if (cdai.buf == NULL) {
11283501942bSJustin T. Gibbs 		ret = ENOMEM;
11293501942bSJustin T. Gibbs 		goto out;
11303501942bSJustin T. Gibbs 	}
11313501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
11323501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
11333501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
11343501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
11353501942bSJustin T. Gibbs 		goto out;
1136ccba7102SAlexander Motin 	if (cdai.buftype == CDAI_TYPE_SCSI_DEVID) {
113740f27d7cSAlexander Motin 		if (strcmp(attr, "GEOM::lunid") == 0) {
1138ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1139ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_naa);
1140ccba7102SAlexander Motin 			if (idd == NULL)
1141ccba7102SAlexander Motin 				idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1142ccba7102SAlexander Motin 				    cdai.provsiz, scsi_devid_is_lun_eui64);
114340f27d7cSAlexander Motin 		} else
114440f27d7cSAlexander Motin 			idd = NULL;
1145ccba7102SAlexander Motin 		if (idd == NULL)
1146ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1147ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_t10);
1148ccba7102SAlexander Motin 		if (idd == NULL)
1149ccba7102SAlexander Motin 			idd = scsi_get_devid((struct scsi_vpd_device_id *)cdai.buf,
1150ccba7102SAlexander Motin 			    cdai.provsiz, scsi_devid_is_lun_name);
1151ccba7102SAlexander Motin 		if (idd == NULL)
1152ccba7102SAlexander Motin 			goto out;
1153ccba7102SAlexander Motin 		ret = 0;
1154ccba7102SAlexander Motin 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_ASCII ||
1155ccba7102SAlexander Motin 		    (idd->proto_codeset & SVPD_ID_CODESET_MASK) == SVPD_ID_CODESET_UTF8) {
1156ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1157ccba7102SAlexander Motin 			if (l < len) {
1158ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1159ccba7102SAlexander Motin 				buf[l] = 0;
1160ccba7102SAlexander Motin 			} else
1161ccba7102SAlexander Motin 				ret = EFAULT;
1162ccba7102SAlexander Motin 		} else {
1163ccba7102SAlexander Motin 			if (idd->length * 2 < len) {
1164ccba7102SAlexander Motin 				for (l = 0; l < idd->length; l++)
1165ccba7102SAlexander Motin 					sprintf(buf + l * 2, "%02x",
1166ccba7102SAlexander Motin 					    idd->identifier[l]);
1167ccba7102SAlexander Motin 			} else
1168ccba7102SAlexander Motin 				ret = EFAULT;
1169ccba7102SAlexander Motin 		}
1170ccba7102SAlexander Motin 	} else {
11713501942bSJustin T. Gibbs 		ret = 0;
11723501942bSJustin T. Gibbs 		if (strlcpy(buf, cdai.buf, len) >= len)
11733501942bSJustin T. Gibbs 			ret = EFAULT;
1174ccba7102SAlexander Motin 	}
11753501942bSJustin T. Gibbs 
11763501942bSJustin T. Gibbs out:
11773501942bSJustin T. Gibbs 	if (cdai.buf != NULL)
11783501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
11793501942bSJustin T. Gibbs 	return ret;
11803501942bSJustin T. Gibbs }
11813501942bSJustin T. Gibbs 
11828b8a9b1dSJustin T. Gibbs static dev_match_ret
11833393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
11848b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
11858b8a9b1dSJustin T. Gibbs {
11868b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
11878b8a9b1dSJustin T. Gibbs 	int i;
11888b8a9b1dSJustin T. Gibbs 
11898b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
11908b8a9b1dSJustin T. Gibbs 
11918b8a9b1dSJustin T. Gibbs 	/*
11928b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
11938b8a9b1dSJustin T. Gibbs 	 */
11948b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
11958b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
11968b8a9b1dSJustin T. Gibbs 
11978b8a9b1dSJustin T. Gibbs 	/*
11988b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
11998b8a9b1dSJustin T. Gibbs 	 * matter what.
12008b8a9b1dSJustin T. Gibbs 	 */
12018b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
12028b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
12038b8a9b1dSJustin T. Gibbs 
12048b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
12058b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
12068b8a9b1dSJustin T. Gibbs 
12078b8a9b1dSJustin T. Gibbs 		/*
12088b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
12098b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
12108b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
12118b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
12128b8a9b1dSJustin T. Gibbs 		 * EDT elements.
12138b8a9b1dSJustin T. Gibbs 		 */
12148b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
12158b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12168b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
12178b8a9b1dSJustin T. Gibbs 			continue;
12188b8a9b1dSJustin T. Gibbs 		}
12198b8a9b1dSJustin T. Gibbs 
12208b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
12218b8a9b1dSJustin T. Gibbs 
12228b8a9b1dSJustin T. Gibbs 		/*
12238b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
12248b8a9b1dSJustin T. Gibbs 		 * device node.
12258b8a9b1dSJustin T. Gibbs 		 */
12268b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
12278b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
12288b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
12298b8a9b1dSJustin T. Gibbs 
12308b8a9b1dSJustin T. Gibbs 			/*
12318b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
12328b8a9b1dSJustin T. Gibbs 			 * and return.
12338b8a9b1dSJustin T. Gibbs 			 */
12348b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
12358b8a9b1dSJustin T. Gibbs 				return(retval);
12368b8a9b1dSJustin T. Gibbs 		}
12378b8a9b1dSJustin T. Gibbs 
12388b8a9b1dSJustin T. Gibbs 		/*
12398b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
12408b8a9b1dSJustin T. Gibbs 		 */
12418b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
12428b8a9b1dSJustin T. Gibbs 			continue;
12438b8a9b1dSJustin T. Gibbs 
12448b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
12458b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
12468b8a9b1dSJustin T. Gibbs 			continue;
12478b8a9b1dSJustin T. Gibbs 
12488b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
12498b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
12508b8a9b1dSJustin T. Gibbs 			continue;
12518b8a9b1dSJustin T. Gibbs 
12528b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
12538b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
12548b8a9b1dSJustin T. Gibbs 			continue;
12558b8a9b1dSJustin T. Gibbs 
12568b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
12578b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
12588b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
12598b8a9b1dSJustin T. Gibbs 			continue;
12608b8a9b1dSJustin T. Gibbs 
12618b8a9b1dSJustin T. Gibbs 		/*
12628b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
12638b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
12648b8a9b1dSJustin T. Gibbs 		 * data out.
12658b8a9b1dSJustin T. Gibbs 		 */
12668b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
12678b8a9b1dSJustin T. Gibbs 
12688b8a9b1dSJustin T. Gibbs 		/*
12698b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
12708b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
12718b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
12728b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
12738b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
12748b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
12758b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
12768b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
12778b8a9b1dSJustin T. Gibbs 		 */
12788b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
12798b8a9b1dSJustin T. Gibbs 			return(retval);
12808b8a9b1dSJustin T. Gibbs 	}
12818b8a9b1dSJustin T. Gibbs 
12828b8a9b1dSJustin T. Gibbs 	/*
12838b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
12848b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
12858b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
12868b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
12878b8a9b1dSJustin T. Gibbs 	 */
12888b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
12898b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
12908b8a9b1dSJustin T. Gibbs 
12918b8a9b1dSJustin T. Gibbs 	return(retval);
12928b8a9b1dSJustin T. Gibbs }
12938b8a9b1dSJustin T. Gibbs 
12948b8a9b1dSJustin T. Gibbs static dev_match_ret
12953393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
12968b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
12978b8a9b1dSJustin T. Gibbs {
12988b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
12998b8a9b1dSJustin T. Gibbs 	int i;
13008b8a9b1dSJustin T. Gibbs 
13018b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
13028b8a9b1dSJustin T. Gibbs 
13038b8a9b1dSJustin T. Gibbs 	/*
13048b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13058b8a9b1dSJustin T. Gibbs 	 */
13068b8a9b1dSJustin T. Gibbs 	if (device == NULL)
13078b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
13088b8a9b1dSJustin T. Gibbs 
13098b8a9b1dSJustin T. Gibbs 	/*
13108b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
13118b8a9b1dSJustin T. Gibbs 	 * matter what.
13128b8a9b1dSJustin T. Gibbs 	 */
131359e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
13148b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
13158b8a9b1dSJustin T. Gibbs 
13168b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
13178b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
13183501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
13198b8a9b1dSJustin T. Gibbs 
13208b8a9b1dSJustin T. Gibbs 		/*
13218b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
13228b8a9b1dSJustin T. Gibbs 		 * aren't interested.
13238b8a9b1dSJustin T. Gibbs 		 */
13248b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
13258b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
13268b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
13278b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
13288b8a9b1dSJustin T. Gibbs 			continue;
13298b8a9b1dSJustin T. Gibbs 		}
13308b8a9b1dSJustin T. Gibbs 
13318b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
13328b8a9b1dSJustin T. Gibbs 
13333501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
13343501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13353501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
13363501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
13373501942bSJustin T. Gibbs 
13388b8a9b1dSJustin T. Gibbs 		/*
13398b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
13408b8a9b1dSJustin T. Gibbs 		 * device node.
13418b8a9b1dSJustin T. Gibbs 		 */
13423501942bSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY)
13433501942bSJustin T. Gibbs 			goto copy_dev_node;
13448b8a9b1dSJustin T. Gibbs 
13458b8a9b1dSJustin T. Gibbs 		/*
13468b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
13478b8a9b1dSJustin T. Gibbs 		 */
13488b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
13498b8a9b1dSJustin T. Gibbs 			continue;
13508b8a9b1dSJustin T. Gibbs 
13518b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
13528b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
13538b8a9b1dSJustin T. Gibbs 			continue;
13548b8a9b1dSJustin T. Gibbs 
13558b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
13568b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
13578b8a9b1dSJustin T. Gibbs 			continue;
13588b8a9b1dSJustin T. Gibbs 
13598b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
13608b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
13618b8a9b1dSJustin T. Gibbs 			continue;
13628b8a9b1dSJustin T. Gibbs 
13638b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
13648b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
13653501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
13663501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
13678b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
13688b8a9b1dSJustin T. Gibbs 			continue;
13698b8a9b1dSJustin T. Gibbs 
13703501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
13713501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
13723501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
13733501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
13743501942bSJustin T. Gibbs 				      device->device_id_len
13753501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
13763501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
13773501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
13783501942bSJustin T. Gibbs 			continue;
13793501942bSJustin T. Gibbs 
13803501942bSJustin T. Gibbs copy_dev_node:
13818b8a9b1dSJustin T. Gibbs 		/*
13828b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
13838b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
13848b8a9b1dSJustin T. Gibbs 		 * the data out.
13858b8a9b1dSJustin T. Gibbs 		 */
13868b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
13878b8a9b1dSJustin T. Gibbs 
13888b8a9b1dSJustin T. Gibbs 		/*
13898b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
13908b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
13918b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
13928b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
13938b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
13948b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
13958b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
13968b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
13978b8a9b1dSJustin T. Gibbs 		 */
13988b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
13998b8a9b1dSJustin T. Gibbs 			return(retval);
14008b8a9b1dSJustin T. Gibbs 	}
14018b8a9b1dSJustin T. Gibbs 
14028b8a9b1dSJustin T. Gibbs 	/*
14038b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14048b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
14058b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
14068b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
14078b8a9b1dSJustin T. Gibbs 	 */
14088b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14098b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
14108b8a9b1dSJustin T. Gibbs 
14118b8a9b1dSJustin T. Gibbs 	return(retval);
14128b8a9b1dSJustin T. Gibbs }
14138b8a9b1dSJustin T. Gibbs 
14148b8a9b1dSJustin T. Gibbs /*
14158b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
14168b8a9b1dSJustin T. Gibbs  */
14178b8a9b1dSJustin T. Gibbs static dev_match_ret
14183393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
14198b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
14208b8a9b1dSJustin T. Gibbs {
14218b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
14228b8a9b1dSJustin T. Gibbs 	int i;
14238b8a9b1dSJustin T. Gibbs 
14248b8a9b1dSJustin T. Gibbs 	/*
14258b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
14268b8a9b1dSJustin T. Gibbs 	 */
14278b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
14288b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14298b8a9b1dSJustin T. Gibbs 
14308b8a9b1dSJustin T. Gibbs 	/*
14318b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
14328b8a9b1dSJustin T. Gibbs 	 * matter what.
14338b8a9b1dSJustin T. Gibbs 	 */
14348b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
14358b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
14368b8a9b1dSJustin T. Gibbs 
14378b8a9b1dSJustin T. Gibbs 	/*
14388b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
14398b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
14408b8a9b1dSJustin T. Gibbs 	 */
14418b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
14428b8a9b1dSJustin T. Gibbs 
14438b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14448b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
14458b8a9b1dSJustin T. Gibbs 
14468b8a9b1dSJustin T. Gibbs 		/*
14478b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
14488b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14498b8a9b1dSJustin T. Gibbs 		 */
14508b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
14518b8a9b1dSJustin T. Gibbs 			continue;
14528b8a9b1dSJustin T. Gibbs 
14538b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
14548b8a9b1dSJustin T. Gibbs 
14558b8a9b1dSJustin T. Gibbs 		/*
14568b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
14578b8a9b1dSJustin T. Gibbs 		 */
14588b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
14598b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
14608b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
14618b8a9b1dSJustin T. Gibbs 
14628b8a9b1dSJustin T. Gibbs 			/*
14638b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
14648b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
14658b8a9b1dSJustin T. Gibbs 			 * the tree.
14668b8a9b1dSJustin T. Gibbs 			 */
14678b8a9b1dSJustin T. Gibbs 			return(retval);
14688b8a9b1dSJustin T. Gibbs 		}
14698b8a9b1dSJustin T. Gibbs 
14708b8a9b1dSJustin T. Gibbs 		/*
14718b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14728b8a9b1dSJustin T. Gibbs 		 */
14738b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
14748b8a9b1dSJustin T. Gibbs 			continue;
14758b8a9b1dSJustin T. Gibbs 
14768b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
14778b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
14788b8a9b1dSJustin T. Gibbs 			continue;
14798b8a9b1dSJustin T. Gibbs 
14808b8a9b1dSJustin T. Gibbs 		/*
14818b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
14828b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
14838b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
14848b8a9b1dSJustin T. Gibbs 		 */
14858b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
14868b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
14878b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
14888b8a9b1dSJustin T. Gibbs 			continue;
14898b8a9b1dSJustin T. Gibbs 
14908b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
14918b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
14928b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
14938b8a9b1dSJustin T. Gibbs 			continue;
14948b8a9b1dSJustin T. Gibbs 
14958b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
14968b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
14978b8a9b1dSJustin T. Gibbs 			continue;
14988b8a9b1dSJustin T. Gibbs 
14998b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
15008b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
15018b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
15028b8a9b1dSJustin T. Gibbs 			continue;
15038b8a9b1dSJustin T. Gibbs 
15048b8a9b1dSJustin T. Gibbs 		/*
15058b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15068b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
15078b8a9b1dSJustin T. Gibbs 		 * copy the data out.
15088b8a9b1dSJustin T. Gibbs 		 */
15098b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15108b8a9b1dSJustin T. Gibbs 
15118b8a9b1dSJustin T. Gibbs 		/*
15128b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
15138b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
15148b8a9b1dSJustin T. Gibbs 		 */
15158b8a9b1dSJustin T. Gibbs 		return(retval);
15168b8a9b1dSJustin T. Gibbs 	}
15178b8a9b1dSJustin T. Gibbs 
15188b8a9b1dSJustin T. Gibbs 	/*
15198b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
15208b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
15218b8a9b1dSJustin T. Gibbs 	 */
15228b8a9b1dSJustin T. Gibbs 	return(retval);
15238b8a9b1dSJustin T. Gibbs }
15248b8a9b1dSJustin T. Gibbs 
15258b8a9b1dSJustin T. Gibbs static int
15268b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
15278b8a9b1dSJustin T. Gibbs {
15288b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1529227d67aaSAlexander Motin 	struct cam_et *target;
15308b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15318b8a9b1dSJustin T. Gibbs 
15328b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15338b8a9b1dSJustin T. Gibbs 
15348b8a9b1dSJustin T. Gibbs 	/*
15358b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
15368b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
15378b8a9b1dSJustin T. Gibbs 	 */
15388b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15398b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
15418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
15428b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
15438b8a9b1dSJustin T. Gibbs 	else
15448b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
15458b8a9b1dSJustin T. Gibbs 
15468b8a9b1dSJustin T. Gibbs 	/*
15478b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
15488b8a9b1dSJustin T. Gibbs 	 */
15498b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
15508b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
15518b8a9b1dSJustin T. Gibbs 		return(0);
15528b8a9b1dSJustin T. Gibbs 	}
15538b8a9b1dSJustin T. Gibbs 
15548b8a9b1dSJustin T. Gibbs 	/*
15558b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
15568b8a9b1dSJustin T. Gibbs 	 */
15578b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
15588b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
15598b8a9b1dSJustin T. Gibbs 
15608b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
15618b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
15628b8a9b1dSJustin T. Gibbs 
15638b8a9b1dSJustin T. Gibbs 		/*
15648b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
15658b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
15668b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
15678b8a9b1dSJustin T. Gibbs 		 */
15688b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
15698b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
15708b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
15718b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
15728b8a9b1dSJustin T. Gibbs 
15738b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
15748b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
15752b83592fSScott Long 				xsoftc.bus_generation;
15768b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
15778b8a9b1dSJustin T. Gibbs 			return(0);
15788b8a9b1dSJustin T. Gibbs 		}
15798b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
15808b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
15818b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
15828b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
15838b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
15848b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
15858b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
15868b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.bus_result.dev_name,
15878b8a9b1dSJustin T. Gibbs 			bus->sim->sim_name, DEV_IDLEN);
15888b8a9b1dSJustin T. Gibbs 	}
15898b8a9b1dSJustin T. Gibbs 
15908b8a9b1dSJustin T. Gibbs 	/*
15918b8a9b1dSJustin T. Gibbs 	 * If the user is only interested in busses, there's no
15928b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
15938b8a9b1dSJustin T. Gibbs 	 */
15948b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
15958b8a9b1dSJustin T. Gibbs 		return(1);
15968b8a9b1dSJustin T. Gibbs 
15978b8a9b1dSJustin T. Gibbs 	/*
15988b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
15998b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
16008b8a9b1dSJustin T. Gibbs 	 */
1601227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16028b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16038b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
16048b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1605227d67aaSAlexander Motin 	 && (cdm->pos.cookie.target != NULL)) {
1606227d67aaSAlexander Motin 		if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1607227d67aaSAlexander Motin 		    bus->generation)) {
1608227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1609227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1610227d67aaSAlexander Motin 			return (0);
1611227d67aaSAlexander Motin 		}
1612227d67aaSAlexander Motin 		target = (struct cam_et *)cdm->pos.cookie.target;
1613227d67aaSAlexander Motin 		target->refcount++;
1614227d67aaSAlexander Motin 	} else
1615227d67aaSAlexander Motin 		target = NULL;
1616227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1617227d67aaSAlexander Motin 
1618227d67aaSAlexander Motin 	return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
16198b8a9b1dSJustin T. Gibbs }
16208b8a9b1dSJustin T. Gibbs 
16218b8a9b1dSJustin T. Gibbs static int
16228b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
16238b8a9b1dSJustin T. Gibbs {
16248b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1625227d67aaSAlexander Motin 	struct cam_eb *bus;
1626227d67aaSAlexander Motin 	struct cam_ed *device;
16278b8a9b1dSJustin T. Gibbs 
16288b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1629227d67aaSAlexander Motin 	bus = target->bus;
16308b8a9b1dSJustin T. Gibbs 
16318b8a9b1dSJustin T. Gibbs 	/*
16328b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
16338b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
16348b8a9b1dSJustin T. Gibbs 	 */
1635227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16368b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1637227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
16388b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16398b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
16408b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1641227d67aaSAlexander Motin 	 && (cdm->pos.cookie.device != NULL)) {
1642227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1643227d67aaSAlexander Motin 		    target->generation) {
1644227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
16458b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
16468b8a9b1dSJustin T. Gibbs 			return(0);
16478b8a9b1dSJustin T. Gibbs 		}
1648227d67aaSAlexander Motin 		device = (struct cam_ed *)cdm->pos.cookie.device;
1649227d67aaSAlexander Motin 		device->refcount++;
1650227d67aaSAlexander Motin 	} else
1651227d67aaSAlexander Motin 		device = NULL;
1652227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
16538b8a9b1dSJustin T. Gibbs 
1654227d67aaSAlexander Motin 	return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
16558b8a9b1dSJustin T. Gibbs }
16568b8a9b1dSJustin T. Gibbs 
16578b8a9b1dSJustin T. Gibbs static int
16588b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
16598b8a9b1dSJustin T. Gibbs {
1660227d67aaSAlexander Motin 	struct cam_eb *bus;
1661227d67aaSAlexander Motin 	struct cam_periph *periph;
16628b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
16638b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
16648b8a9b1dSJustin T. Gibbs 
16658b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1666227d67aaSAlexander Motin 	bus = device->target->bus;
16678b8a9b1dSJustin T. Gibbs 
16688b8a9b1dSJustin T. Gibbs 	/*
16698b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
16708b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
16718b8a9b1dSJustin T. Gibbs 	 */
16728b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
16738b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
16748b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
16758b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
16768b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
16778b8a9b1dSJustin T. Gibbs 	else
16788b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
16798b8a9b1dSJustin T. Gibbs 					device);
16808b8a9b1dSJustin T. Gibbs 
16818b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
16828b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
16838b8a9b1dSJustin T. Gibbs 		return(0);
16848b8a9b1dSJustin T. Gibbs 	}
16858b8a9b1dSJustin T. Gibbs 
16868b8a9b1dSJustin T. Gibbs 	/*
16878b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
16888b8a9b1dSJustin T. Gibbs 	 */
16898b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
16908b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
16918b8a9b1dSJustin T. Gibbs 
16928b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
16938b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
16948b8a9b1dSJustin T. Gibbs 
16958b8a9b1dSJustin T. Gibbs 		/*
16968b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
16978b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
16988b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
16998b8a9b1dSJustin T. Gibbs 		 */
17008b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17018b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17028b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17038b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
17048b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
17058b8a9b1dSJustin T. Gibbs 
17068b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
17078b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17082b83592fSScott Long 				xsoftc.bus_generation;
17098b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
17108b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
17118b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
17128b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
17138b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
17148b8a9b1dSJustin T. Gibbs 				device->target->generation;
17158b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17168b8a9b1dSJustin T. Gibbs 			return(0);
17178b8a9b1dSJustin T. Gibbs 		}
17188b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17198b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17208b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
17218b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
17228b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
17238b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
17248b8a9b1dSJustin T. Gibbs 			device->target->target_id;
17258b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
17268b8a9b1dSJustin T. Gibbs 			device->lun_id;
172752c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
172852c9ce25SScott Long 			device->protocol;
17298b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
17308b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
17318b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
173252c9ce25SScott Long 		bcopy(&device->ident_data,
173352c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
173452c9ce25SScott Long 		      sizeof(struct ata_params));
17359deea857SKenneth D. Merry 
17369deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
17379deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
17389deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17399deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
17409deea857SKenneth D. Merry 		else
17419deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17429deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
17438b8a9b1dSJustin T. Gibbs 	}
17448b8a9b1dSJustin T. Gibbs 
17458b8a9b1dSJustin T. Gibbs 	/*
17468b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
17478b8a9b1dSJustin T. Gibbs 	 * the tree any further.
17488b8a9b1dSJustin T. Gibbs 	 */
17498b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17508b8a9b1dSJustin T. Gibbs 		return(1);
17518b8a9b1dSJustin T. Gibbs 
17528b8a9b1dSJustin T. Gibbs 	/*
17538b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
17548b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
17558b8a9b1dSJustin T. Gibbs 	 */
1756227d67aaSAlexander Motin 	xpt_lock_buses();
1757227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
17588b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1759227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
17608b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17618b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
17628b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17638b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
17648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1765227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1766227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1767227d67aaSAlexander Motin 		    device->generation) {
1768227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1769227d67aaSAlexander Motin 			xpt_unlock_buses();
1770227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1771227d67aaSAlexander Motin 			return(0);
1772227d67aaSAlexander Motin 		}
1773227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1774227d67aaSAlexander Motin 		periph->refcount++;
1775227d67aaSAlexander Motin 	} else
1776227d67aaSAlexander Motin 		periph = NULL;
1777227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1778227d67aaSAlexander Motin 	xpt_unlock_buses();
1779227d67aaSAlexander Motin 
1780227d67aaSAlexander Motin 	return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
17818b8a9b1dSJustin T. Gibbs }
17828b8a9b1dSJustin T. Gibbs 
17838b8a9b1dSJustin T. Gibbs static int
17848b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
17858b8a9b1dSJustin T. Gibbs {
17868b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
17878b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17888b8a9b1dSJustin T. Gibbs 
17898b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
17908b8a9b1dSJustin T. Gibbs 
17918b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
17928b8a9b1dSJustin T. Gibbs 
17938b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17948b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17958b8a9b1dSJustin T. Gibbs 		return(0);
17968b8a9b1dSJustin T. Gibbs 	}
17978b8a9b1dSJustin T. Gibbs 
17988b8a9b1dSJustin T. Gibbs 	/*
17998b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
18008b8a9b1dSJustin T. Gibbs 	 */
18018b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18028b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
18038b8a9b1dSJustin T. Gibbs 
18048b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18058b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18068b8a9b1dSJustin T. Gibbs 
18078b8a9b1dSJustin T. Gibbs 		/*
18088b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
18098b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
18108b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
18118b8a9b1dSJustin T. Gibbs 		 */
18128b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
18138b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
18148b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
18158b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
18168b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
18178b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
18188b8a9b1dSJustin T. Gibbs 
18198b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
18208b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
18212b83592fSScott Long 				xsoftc.bus_generation;
18228b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
18238b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
18248b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
18258b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
18268b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
18278b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
18288b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
18298b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
18308b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
18318b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18328b8a9b1dSJustin T. Gibbs 			return(0);
18338b8a9b1dSJustin T. Gibbs 		}
18348b8a9b1dSJustin T. Gibbs 
18358b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18368b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18378b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
18388b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
18398b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
18408b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
18418b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
18428b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
18438b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
18448b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
18458b8a9b1dSJustin T. Gibbs 			periph->unit_number;
18468b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
18478b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
18488b8a9b1dSJustin T. Gibbs 	}
18498b8a9b1dSJustin T. Gibbs 
18508b8a9b1dSJustin T. Gibbs 	return(1);
18518b8a9b1dSJustin T. Gibbs }
18528b8a9b1dSJustin T. Gibbs 
18538b8a9b1dSJustin T. Gibbs static int
18548b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
18558b8a9b1dSJustin T. Gibbs {
1856227d67aaSAlexander Motin 	struct cam_eb *bus;
18578b8a9b1dSJustin T. Gibbs 	int ret;
18588b8a9b1dSJustin T. Gibbs 
18598b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
18608b8a9b1dSJustin T. Gibbs 
18618b8a9b1dSJustin T. Gibbs 	/*
18628b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
18638b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
18648b8a9b1dSJustin T. Gibbs 	 */
1865227d67aaSAlexander Motin 	xpt_lock_buses();
18668b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1867227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus != NULL)) {
1868227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1869227d67aaSAlexander Motin 		    xsoftc.bus_generation) {
1870227d67aaSAlexander Motin 			xpt_unlock_buses();
18718b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18728b8a9b1dSJustin T. Gibbs 			return(0);
18738b8a9b1dSJustin T. Gibbs 		}
1874227d67aaSAlexander Motin 		bus = (struct cam_eb *)cdm->pos.cookie.bus;
1875227d67aaSAlexander Motin 		bus->refcount++;
1876227d67aaSAlexander Motin 	} else
1877227d67aaSAlexander Motin 		bus = NULL;
1878227d67aaSAlexander Motin 	xpt_unlock_buses();
18798b8a9b1dSJustin T. Gibbs 
1880227d67aaSAlexander Motin 	ret = xptbustraverse(bus, xptedtbusfunc, cdm);
18818b8a9b1dSJustin T. Gibbs 
18828b8a9b1dSJustin T. Gibbs 	/*
18838b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
18848b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
18858b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
18868b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
18878b8a9b1dSJustin T. Gibbs 	 */
18888b8a9b1dSJustin T. Gibbs 	if (ret == 1)
18898b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
18908b8a9b1dSJustin T. Gibbs 
18918b8a9b1dSJustin T. Gibbs 	return(ret);
18928b8a9b1dSJustin T. Gibbs }
18938b8a9b1dSJustin T. Gibbs 
18948b8a9b1dSJustin T. Gibbs static int
18958b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
18968b8a9b1dSJustin T. Gibbs {
1897227d67aaSAlexander Motin 	struct cam_periph *periph;
18988b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18998b8a9b1dSJustin T. Gibbs 
19008b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19018b8a9b1dSJustin T. Gibbs 
1902227d67aaSAlexander Motin 	xpt_lock_buses();
19038b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
19048b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
19058b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1906227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1907227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1908227d67aaSAlexander Motin 		    (*pdrv)->generation) {
1909227d67aaSAlexander Motin 			xpt_unlock_buses();
19108b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19118b8a9b1dSJustin T. Gibbs 			return(0);
19128b8a9b1dSJustin T. Gibbs 		}
1913227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1914227d67aaSAlexander Motin 		periph->refcount++;
1915227d67aaSAlexander Motin 	} else
1916227d67aaSAlexander Motin 		periph = NULL;
1917227d67aaSAlexander Motin 	xpt_unlock_buses();
19188b8a9b1dSJustin T. Gibbs 
1919227d67aaSAlexander Motin 	return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
19208b8a9b1dSJustin T. Gibbs }
19218b8a9b1dSJustin T. Gibbs 
19228b8a9b1dSJustin T. Gibbs static int
19238b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
19248b8a9b1dSJustin T. Gibbs {
19258b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19268b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19278b8a9b1dSJustin T. Gibbs 
19288b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19298b8a9b1dSJustin T. Gibbs 
19308b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19318b8a9b1dSJustin T. Gibbs 
19328b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19338b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19348b8a9b1dSJustin T. Gibbs 		return(0);
19358b8a9b1dSJustin T. Gibbs 	}
19368b8a9b1dSJustin T. Gibbs 
19378b8a9b1dSJustin T. Gibbs 	/*
19388b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19398b8a9b1dSJustin T. Gibbs 	 */
19408b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19418b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
19428b8a9b1dSJustin T. Gibbs 
19438b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19448b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19458b8a9b1dSJustin T. Gibbs 
19468b8a9b1dSJustin T. Gibbs 		/*
19478b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19488b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19498b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19508b8a9b1dSJustin T. Gibbs 		 */
19518b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19528b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
19538b8a9b1dSJustin T. Gibbs 
19548b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
19558b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19568b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19578b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
19588b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
19598b8a9b1dSJustin T. Gibbs 
19608b8a9b1dSJustin T. Gibbs 			/*
19618b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
19628b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
19638b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
19648b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
19658b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
19668b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
19678b8a9b1dSJustin T. Gibbs 			 */
19680b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
19698b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
19708b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
19718b8a9b1dSJustin T. Gibbs 					break;
19728b8a9b1dSJustin T. Gibbs 			}
19738b8a9b1dSJustin T. Gibbs 
197401910babSScott Long 			if (*pdrv == NULL) {
19758b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
19768b8a9b1dSJustin T. Gibbs 				return(0);
19778b8a9b1dSJustin T. Gibbs 			}
19788b8a9b1dSJustin T. Gibbs 
19798b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
19808b8a9b1dSJustin T. Gibbs 			/*
19818b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
19828b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
19838b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
19848b8a9b1dSJustin T. Gibbs 			 */
19858b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
19868b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
19878b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
19888b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
19898b8a9b1dSJustin T. Gibbs 			return(0);
19908b8a9b1dSJustin T. Gibbs 		}
19918b8a9b1dSJustin T. Gibbs 
19928b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
19938b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
19948b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
19958b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
19968b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
19978b8a9b1dSJustin T. Gibbs 
19988b8a9b1dSJustin T. Gibbs 		/*
19998b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
20008b8a9b1dSJustin T. Gibbs 		 * lun.
20018b8a9b1dSJustin T. Gibbs 		 */
20028b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
20038b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
20048b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
20058b8a9b1dSJustin T. Gibbs 		else
20068b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id = -1;
20078b8a9b1dSJustin T. Gibbs 
20088b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
20098b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
20108b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
20118b8a9b1dSJustin T. Gibbs 		else
20128b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun = -1;
20138b8a9b1dSJustin T. Gibbs 
20148b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20158b8a9b1dSJustin T. Gibbs 			periph->unit_number;
20168b8a9b1dSJustin T. Gibbs 		strncpy(cdm->matches[j].result.periph_result.periph_name,
20178b8a9b1dSJustin T. Gibbs 			periph->periph_name, DEV_IDLEN);
20188b8a9b1dSJustin T. Gibbs 	}
20198b8a9b1dSJustin T. Gibbs 
20208b8a9b1dSJustin T. Gibbs 	return(1);
20218b8a9b1dSJustin T. Gibbs }
20228b8a9b1dSJustin T. Gibbs 
20238b8a9b1dSJustin T. Gibbs static int
20248b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
20258b8a9b1dSJustin T. Gibbs {
20268b8a9b1dSJustin T. Gibbs 	int ret;
20278b8a9b1dSJustin T. Gibbs 
20288b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20298b8a9b1dSJustin T. Gibbs 
20308b8a9b1dSJustin T. Gibbs 	/*
20318b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
20328b8a9b1dSJustin T. Gibbs 	 * list generation to make sure that no busses have been added or
20338b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
20348b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
20358b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
20368b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
20378b8a9b1dSJustin T. Gibbs 	 * without a recompile.
20388b8a9b1dSJustin T. Gibbs 	 */
20398b8a9b1dSJustin T. Gibbs 
20408b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20418b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
20428b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
20438b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
20448b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
20458b8a9b1dSJustin T. Gibbs 	else
20468b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
20478b8a9b1dSJustin T. Gibbs 
20488b8a9b1dSJustin T. Gibbs 	/*
20498b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20508b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
20518b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
20528b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
20538b8a9b1dSJustin T. Gibbs 	 * matching entries.
20548b8a9b1dSJustin T. Gibbs 	 */
20558b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20568b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20578b8a9b1dSJustin T. Gibbs 
20588b8a9b1dSJustin T. Gibbs 	return(ret);
20598b8a9b1dSJustin T. Gibbs }
20608b8a9b1dSJustin T. Gibbs 
20618b8a9b1dSJustin T. Gibbs static int
20628b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
20638b8a9b1dSJustin T. Gibbs {
20648b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
20658b8a9b1dSJustin T. Gibbs 	int retval;
20668b8a9b1dSJustin T. Gibbs 
20678b8a9b1dSJustin T. Gibbs 	retval = 1;
2068227d67aaSAlexander Motin 	if (start_bus)
2069227d67aaSAlexander Motin 		bus = start_bus;
2070227d67aaSAlexander Motin 	else {
20719a7c2696SAlexander Motin 		xpt_lock_buses();
2072227d67aaSAlexander Motin 		bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2073227d67aaSAlexander Motin 		if (bus == NULL) {
20749a7c2696SAlexander Motin 			xpt_unlock_buses();
2075227d67aaSAlexander Motin 			return (retval);
2076227d67aaSAlexander Motin 		}
2077227d67aaSAlexander Motin 		bus->refcount++;
2078227d67aaSAlexander Motin 		xpt_unlock_buses();
2079227d67aaSAlexander Motin 	}
2080227d67aaSAlexander Motin 	for (; bus != NULL; bus = next_bus) {
20818b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
2082227d67aaSAlexander Motin 		if (retval == 0) {
2083227d67aaSAlexander Motin 			xpt_release_bus(bus);
2084227d67aaSAlexander Motin 			break;
2085227d67aaSAlexander Motin 		}
20869a7c2696SAlexander Motin 		xpt_lock_buses();
20878900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
2088227d67aaSAlexander Motin 		if (next_bus)
2089227d67aaSAlexander Motin 			next_bus->refcount++;
20909a7c2696SAlexander Motin 		xpt_unlock_buses();
20918900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
20928b8a9b1dSJustin T. Gibbs 	}
20938b8a9b1dSJustin T. Gibbs 	return(retval);
20948b8a9b1dSJustin T. Gibbs }
20958b8a9b1dSJustin T. Gibbs 
20968b8a9b1dSJustin T. Gibbs static int
20978b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
20988b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
20998b8a9b1dSJustin T. Gibbs {
21008b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
21018b8a9b1dSJustin T. Gibbs 	int retval;
21028b8a9b1dSJustin T. Gibbs 
21038b8a9b1dSJustin T. Gibbs 	retval = 1;
2104227d67aaSAlexander Motin 	if (start_target)
2105227d67aaSAlexander Motin 		target = start_target;
2106227d67aaSAlexander Motin 	else {
2107227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2108227d67aaSAlexander Motin 		target = TAILQ_FIRST(&bus->et_entries);
2109227d67aaSAlexander Motin 		if (target == NULL) {
2110227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21118b8a9b1dSJustin T. Gibbs 			return (retval);
21128b8a9b1dSJustin T. Gibbs 		}
2113227d67aaSAlexander Motin 		target->refcount++;
2114227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2115227d67aaSAlexander Motin 	}
2116227d67aaSAlexander Motin 	for (; target != NULL; target = next_target) {
2117227d67aaSAlexander Motin 		retval = tr_func(target, arg);
2118227d67aaSAlexander Motin 		if (retval == 0) {
2119227d67aaSAlexander Motin 			xpt_release_target(target);
2120227d67aaSAlexander Motin 			break;
2121227d67aaSAlexander Motin 		}
2122227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2123227d67aaSAlexander Motin 		next_target = TAILQ_NEXT(target, links);
2124227d67aaSAlexander Motin 		if (next_target)
2125227d67aaSAlexander Motin 			next_target->refcount++;
2126227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2127227d67aaSAlexander Motin 		xpt_release_target(target);
2128227d67aaSAlexander Motin 	}
21298b8a9b1dSJustin T. Gibbs 	return(retval);
21308b8a9b1dSJustin T. Gibbs }
21318b8a9b1dSJustin T. Gibbs 
21328b8a9b1dSJustin T. Gibbs static int
21338b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
21348b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
21358b8a9b1dSJustin T. Gibbs {
2136227d67aaSAlexander Motin 	struct cam_eb *bus;
21378b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
21388b8a9b1dSJustin T. Gibbs 	int retval;
21398b8a9b1dSJustin T. Gibbs 
21408b8a9b1dSJustin T. Gibbs 	retval = 1;
2141227d67aaSAlexander Motin 	bus = target->bus;
2142227d67aaSAlexander Motin 	if (start_device)
2143227d67aaSAlexander Motin 		device = start_device;
2144227d67aaSAlexander Motin 	else {
2145227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2146227d67aaSAlexander Motin 		device = TAILQ_FIRST(&target->ed_entries);
2147227d67aaSAlexander Motin 		if (device == NULL) {
2148227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21498b8a9b1dSJustin T. Gibbs 			return (retval);
21508b8a9b1dSJustin T. Gibbs 		}
2151227d67aaSAlexander Motin 		device->refcount++;
2152227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2153227d67aaSAlexander Motin 	}
2154227d67aaSAlexander Motin 	for (; device != NULL; device = next_device) {
2155227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
2156227d67aaSAlexander Motin 		retval = tr_func(device, arg);
2157227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
2158227d67aaSAlexander Motin 		if (retval == 0) {
2159227d67aaSAlexander Motin 			xpt_release_device(device);
2160227d67aaSAlexander Motin 			break;
2161227d67aaSAlexander Motin 		}
2162227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2163227d67aaSAlexander Motin 		next_device = TAILQ_NEXT(device, links);
2164227d67aaSAlexander Motin 		if (next_device)
2165227d67aaSAlexander Motin 			next_device->refcount++;
2166227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2167227d67aaSAlexander Motin 		xpt_release_device(device);
2168227d67aaSAlexander Motin 	}
21698b8a9b1dSJustin T. Gibbs 	return(retval);
21708b8a9b1dSJustin T. Gibbs }
21718b8a9b1dSJustin T. Gibbs 
21728b8a9b1dSJustin T. Gibbs static int
21738b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
21748b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
21758b8a9b1dSJustin T. Gibbs {
2176227d67aaSAlexander Motin 	struct cam_eb *bus;
21778b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
21788b8a9b1dSJustin T. Gibbs 	int retval;
21798b8a9b1dSJustin T. Gibbs 
21808b8a9b1dSJustin T. Gibbs 	retval = 1;
21818b8a9b1dSJustin T. Gibbs 
2182227d67aaSAlexander Motin 	bus = device->target->bus;
2183227d67aaSAlexander Motin 	if (start_periph)
2184227d67aaSAlexander Motin 		periph = start_periph;
2185227d67aaSAlexander Motin 	else {
21868900f4b8SKenneth D. Merry 		xpt_lock_buses();
2187227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2188227d67aaSAlexander Motin 		periph = SLIST_FIRST(&device->periphs);
2189227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2190227d67aaSAlexander Motin 			periph = SLIST_NEXT(periph, periph_links);
2191227d67aaSAlexander Motin 		if (periph == NULL) {
2192227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21938900f4b8SKenneth D. Merry 			xpt_unlock_buses();
2194227d67aaSAlexander Motin 			return (retval);
2195227d67aaSAlexander Motin 		}
2196227d67aaSAlexander Motin 		periph->refcount++;
2197227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2198227d67aaSAlexander Motin 		xpt_unlock_buses();
2199227d67aaSAlexander Motin 	}
2200227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2201227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2202227d67aaSAlexander Motin 		if (retval == 0) {
2203227d67aaSAlexander Motin 			cam_periph_release(periph);
2204227d67aaSAlexander Motin 			break;
2205227d67aaSAlexander Motin 		}
2206227d67aaSAlexander Motin 		xpt_lock_buses();
2207227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2208227d67aaSAlexander Motin 		next_periph = SLIST_NEXT(periph, periph_links);
2209227d67aaSAlexander Motin 		while (next_periph != NULL &&
2210227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2211227d67aaSAlexander Motin 			next_periph = SLIST_NEXT(periph, periph_links);
2212227d67aaSAlexander Motin 		if (next_periph)
2213227d67aaSAlexander Motin 			next_periph->refcount++;
2214227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2215227d67aaSAlexander Motin 		xpt_unlock_buses();
2216227d67aaSAlexander Motin 		cam_periph_release_locked(periph);
2217227d67aaSAlexander Motin 	}
22188b8a9b1dSJustin T. Gibbs 	return(retval);
22198b8a9b1dSJustin T. Gibbs }
22208b8a9b1dSJustin T. Gibbs 
22218b8a9b1dSJustin T. Gibbs static int
22228b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
22238b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
22248b8a9b1dSJustin T. Gibbs {
22258b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
22268b8a9b1dSJustin T. Gibbs 	int retval;
22278b8a9b1dSJustin T. Gibbs 
22288b8a9b1dSJustin T. Gibbs 	retval = 1;
22298b8a9b1dSJustin T. Gibbs 
22308b8a9b1dSJustin T. Gibbs 	/*
22318b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
22328b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
22338b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
22348b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
22358b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
22368b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
22378b8a9b1dSJustin T. Gibbs 	 */
22380b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
22398b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
22408b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
22418b8a9b1dSJustin T. Gibbs 
22428b8a9b1dSJustin T. Gibbs 		if (retval == 0)
22438b8a9b1dSJustin T. Gibbs 			return(retval);
22448b8a9b1dSJustin T. Gibbs 	}
22458b8a9b1dSJustin T. Gibbs 
22468b8a9b1dSJustin T. Gibbs 	return(retval);
22478b8a9b1dSJustin T. Gibbs }
22488b8a9b1dSJustin T. Gibbs 
22498b8a9b1dSJustin T. Gibbs static int
22508b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
22518b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
22528b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
22538b8a9b1dSJustin T. Gibbs {
22548b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
22558b8a9b1dSJustin T. Gibbs 	int retval;
22568b8a9b1dSJustin T. Gibbs 
22578b8a9b1dSJustin T. Gibbs 	retval = 1;
22588b8a9b1dSJustin T. Gibbs 
2259227d67aaSAlexander Motin 	if (start_periph)
2260227d67aaSAlexander Motin 		periph = start_periph;
2261227d67aaSAlexander Motin 	else {
2262f1e2546aSMatt Jacob 		xpt_lock_buses();
2263227d67aaSAlexander Motin 		periph = TAILQ_FIRST(&(*pdrv)->units);
2264227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2265227d67aaSAlexander Motin 			periph = TAILQ_NEXT(periph, unit_links);
2266227d67aaSAlexander Motin 		if (periph == NULL) {
2267227d67aaSAlexander Motin 			xpt_unlock_buses();
2268227d67aaSAlexander Motin 			return (retval);
22698900f4b8SKenneth D. Merry 		}
22708900f4b8SKenneth D. Merry 		periph->refcount++;
2271dcdf6e74SAlexander Motin 		xpt_unlock_buses();
22728b8a9b1dSJustin T. Gibbs 	}
2273227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2274227d67aaSAlexander Motin 		cam_periph_lock(periph);
2275227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2276227d67aaSAlexander Motin 		cam_periph_unlock(periph);
2277227d67aaSAlexander Motin 		if (retval == 0) {
2278227d67aaSAlexander Motin 			cam_periph_release(periph);
2279227d67aaSAlexander Motin 			break;
2280227d67aaSAlexander Motin 		}
2281227d67aaSAlexander Motin 		xpt_lock_buses();
2282227d67aaSAlexander Motin 		next_periph = TAILQ_NEXT(periph, unit_links);
2283227d67aaSAlexander Motin 		while (next_periph != NULL &&
2284227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2285227d67aaSAlexander Motin 			next_periph = TAILQ_NEXT(periph, unit_links);
2286227d67aaSAlexander Motin 		if (next_periph)
2287227d67aaSAlexander Motin 			next_periph->refcount++;
2288f1e2546aSMatt Jacob 		xpt_unlock_buses();
2289227d67aaSAlexander Motin 		cam_periph_release(periph);
2290227d67aaSAlexander Motin 	}
22918b8a9b1dSJustin T. Gibbs 	return(retval);
22928b8a9b1dSJustin T. Gibbs }
22938b8a9b1dSJustin T. Gibbs 
22948b8a9b1dSJustin T. Gibbs static int
22958b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
22968b8a9b1dSJustin T. Gibbs {
22978b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
22988b8a9b1dSJustin T. Gibbs 
22998b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23008b8a9b1dSJustin T. Gibbs 
23018b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
23028b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
23038b8a9b1dSJustin T. Gibbs 
23048b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
23058b8a9b1dSJustin T. Gibbs 
23068b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
23078b8a9b1dSJustin T. Gibbs 	} else
23088b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
23098b8a9b1dSJustin T. Gibbs }
23108b8a9b1dSJustin T. Gibbs 
23118b8a9b1dSJustin T. Gibbs static int
23128b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
23138b8a9b1dSJustin T. Gibbs {
23148b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23158b8a9b1dSJustin T. Gibbs 
23168b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23178b8a9b1dSJustin T. Gibbs 
23188b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
23198b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
23208b8a9b1dSJustin T. Gibbs 
23218b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
23228b8a9b1dSJustin T. Gibbs 
23238b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
23248b8a9b1dSJustin T. Gibbs 	} else
23258b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
23268b8a9b1dSJustin T. Gibbs }
23278b8a9b1dSJustin T. Gibbs 
23288b8a9b1dSJustin T. Gibbs static int
23298b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
23308b8a9b1dSJustin T. Gibbs {
23318b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23328b8a9b1dSJustin T. Gibbs 
23338b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23348b8a9b1dSJustin T. Gibbs 
23358b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
23368b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
23378b8a9b1dSJustin T. Gibbs 
23388b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
23398b8a9b1dSJustin T. Gibbs 
23408b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
23418b8a9b1dSJustin T. Gibbs 	} else
23428b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
23438b8a9b1dSJustin T. Gibbs }
23448b8a9b1dSJustin T. Gibbs 
23458b8a9b1dSJustin T. Gibbs static int
23468b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
23478b8a9b1dSJustin T. Gibbs {
23488b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23498b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
23508b8a9b1dSJustin T. Gibbs 
23518b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23528b8a9b1dSJustin T. Gibbs 
23538b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
23548b8a9b1dSJustin T. Gibbs 
23558b8a9b1dSJustin T. Gibbs 	/*
23568b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
23578b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
23588b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
23598b8a9b1dSJustin T. Gibbs 	 */
23608b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
23618b8a9b1dSJustin T. Gibbs }
23628b8a9b1dSJustin T. Gibbs 
23638b8a9b1dSJustin T. Gibbs /*
23648b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
23658b8a9b1dSJustin T. Gibbs  */
23668b8a9b1dSJustin T. Gibbs static int
23678b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
23688b8a9b1dSJustin T. Gibbs {
23698b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23708b8a9b1dSJustin T. Gibbs 
23718b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
23728b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23738b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23748b8a9b1dSJustin T. Gibbs 
23758b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
23768b8a9b1dSJustin T. Gibbs }
23778b8a9b1dSJustin T. Gibbs 
23788b8a9b1dSJustin T. Gibbs /*
23798b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
23808b8a9b1dSJustin T. Gibbs  */
23818b8a9b1dSJustin T. Gibbs static int
23828b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
23838b8a9b1dSJustin T. Gibbs {
23848b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
23858b8a9b1dSJustin T. Gibbs 
23868b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
23878b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
23888b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
23898b8a9b1dSJustin T. Gibbs 
23908b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
23918b8a9b1dSJustin T. Gibbs }
23928b8a9b1dSJustin T. Gibbs 
23938b8a9b1dSJustin T. Gibbs static int
23948b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
23958b8a9b1dSJustin T. Gibbs {
23968b8a9b1dSJustin T. Gibbs 	struct cam_path path;
23978b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
23987685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
23998b8a9b1dSJustin T. Gibbs 
2400c8bead2aSJustin T. Gibbs 	/*
2401c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2402c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2403c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2404c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2405c8bead2aSJustin T. Gibbs 	 */
2406c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2407c8bead2aSJustin T. Gibbs 		return (1);
2408c8bead2aSJustin T. Gibbs 
24098b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
24108b8a9b1dSJustin T. Gibbs 			 NULL,
24118b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
24128b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
24138b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2414bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
24158b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
24168b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
24177685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24188b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
24198b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
24208b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24218b8a9b1dSJustin T. Gibbs 
24228b8a9b1dSJustin T. Gibbs 	return(1);
24238b8a9b1dSJustin T. Gibbs }
2424c8bead2aSJustin T. Gibbs 
24258b8a9b1dSJustin T. Gibbs static int
24268b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
24278b8a9b1dSJustin T. Gibbs {
24288b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24298b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
24307685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24318b8a9b1dSJustin T. Gibbs 
24328b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
24336dfc67e3SAlexander Motin 			 bus->path_id,
24348b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
24358b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2436227d67aaSAlexander Motin 	xpt_path_lock(&path);
2437bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
24388b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
24398b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
24407685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24418b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
24428b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
2443227d67aaSAlexander Motin 	xpt_path_unlock(&path);
24448b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24458b8a9b1dSJustin T. Gibbs 
24468b8a9b1dSJustin T. Gibbs 	return(1);
24478b8a9b1dSJustin T. Gibbs }
24488b8a9b1dSJustin T. Gibbs 
24498b8a9b1dSJustin T. Gibbs void
24508b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
24518b8a9b1dSJustin T. Gibbs {
24529911ecf9SJustin T. Gibbs 
24538b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
24548b8a9b1dSJustin T. Gibbs 
24558b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
245652c9ce25SScott Long 	(*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
245752c9ce25SScott Long }
245852c9ce25SScott Long 
245952c9ce25SScott Long void
246052c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
246152c9ce25SScott Long {
2462da396db2SAlexander Motin 	struct cam_path *path;
2463227d67aaSAlexander Motin 	struct cam_sim *sim;
2464227d67aaSAlexander Motin 	int lock;
246552c9ce25SScott Long 
2466da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
2467da396db2SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
246852c9ce25SScott Long 
24698b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
24708b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2471d05caa00SKenneth D. Merry 	{
24723393f8daSKenneth D. Merry 		struct cam_ed *device;
2473d05caa00SKenneth D. Merry 
24748b8a9b1dSJustin T. Gibbs 		/*
24758b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
24768b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
24778b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
24788b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
24798b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
24808b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
24818b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
24828b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
24838b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
24848b8a9b1dSJustin T. Gibbs 		 *
24858b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
24868b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
24878b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
24888b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
24898b8a9b1dSJustin T. Gibbs 		 */
2490da396db2SAlexander Motin 		device = path->device;
24913393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
24928b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
24938b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
24948b8a9b1dSJustin T. Gibbs 
24958b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
24968b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
24978b8a9b1dSJustin T. Gibbs 		}
24988b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2499d05caa00SKenneth D. Merry 	}
250007c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
25018b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
25028b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
25032cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
25042cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
25052cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
250652c9ce25SScott Long 	case XPT_ATA_IO:
2507de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
250852c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2509b882a6d3SMatt Jacob 		/* FALLTHROUGH */
251010e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
25118b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
251206e79492SKenneth D. Merry 	case XPT_SMP_IO:
25132cefde5fSJustin T. Gibbs 	{
2514227d67aaSAlexander Motin 		struct cam_devq *devq;
25152cefde5fSJustin T. Gibbs 
2516227d67aaSAlexander Motin 		devq = path->bus->sim->devq;
2517227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
2518227d67aaSAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2519227d67aaSAlexander Motin 		if (xpt_schedule_devq(devq, path->device) != 0)
2520227d67aaSAlexander Motin 			xpt_run_devq(devq);
2521227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
2522227d67aaSAlexander Motin 		break;
2523227d67aaSAlexander Motin 	}
2524227d67aaSAlexander Motin 	case XPT_CALC_GEOMETRY:
25258b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
25268b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
25278b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
25288b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
25298b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
25308b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
25318b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25328b8a9b1dSJustin T. Gibbs 			break;
25338b8a9b1dSJustin T. Gibbs 		}
2534abef0e67SMarius Strobl #if defined(PC98) || defined(__sparc64__)
25358b8a9b1dSJustin T. Gibbs 		/*
25368b8a9b1dSJustin T. Gibbs 		 * In a PC-98 system, geometry translation depens on
25378b8a9b1dSJustin T. Gibbs 		 * the "real" device geometry obtained from mode page 4.
25388b8a9b1dSJustin T. Gibbs 		 * SCSI geometry translation is performed in the
25398b8a9b1dSJustin T. Gibbs 		 * initialization routine of the SCSI BIOS and the result
25408b8a9b1dSJustin T. Gibbs 		 * stored in host memory.  If the translation is available
25418b8a9b1dSJustin T. Gibbs 		 * in host memory, use it.  If not, rely on the default
25428b8a9b1dSJustin T. Gibbs 		 * translation the device driver performs.
2543abef0e67SMarius Strobl 		 * For sparc64, we may need adjust the geometry of large
2544abef0e67SMarius Strobl 		 * disks in order to fit the limitations of the 16-bit
2545abef0e67SMarius Strobl 		 * fields of the VTOC8 disk label.
25468b8a9b1dSJustin T. Gibbs 		 */
25478b8a9b1dSJustin T. Gibbs 		if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
25488b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
25498b8a9b1dSJustin T. Gibbs 			break;
25508b8a9b1dSJustin T. Gibbs 		}
25518b8a9b1dSJustin T. Gibbs #endif
2552227d67aaSAlexander Motin 		goto call_sim;
2553bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
25542cefde5fSJustin T. Gibbs 	{
25552cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
25562cefde5fSJustin T. Gibbs 
25572cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
25582cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
25592cefde5fSJustin T. Gibbs 
25602cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index >= 0) {
25612cefde5fSJustin T. Gibbs 				struct cam_ccbq *ccbq;
256283c5d981SAlexander Motin 				struct cam_ed *device;
25632cefde5fSJustin T. Gibbs 
256483c5d981SAlexander Motin 				device = abort_ccb->ccb_h.path->device;
256583c5d981SAlexander Motin 				ccbq = &device->ccbq;
25662cefde5fSJustin T. Gibbs 				cam_ccbq_remove_ccb(ccbq, abort_ccb);
25672cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25682cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25692cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25702cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
25712cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25722cefde5fSJustin T. Gibbs 				break;
25732cefde5fSJustin T. Gibbs 			}
25742cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
25752cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
25762cefde5fSJustin T. Gibbs 				/*
25772cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
25782cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
25792cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
25802cefde5fSJustin T. Gibbs 				 * real work on the CCB.
25812cefde5fSJustin T. Gibbs 				 */
25822cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
25832cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
25842cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
25852cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
25862cefde5fSJustin T. Gibbs 				break;
25872cefde5fSJustin T. Gibbs 			}
25882cefde5fSJustin T. Gibbs 		}
25892cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
25902cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
25912cefde5fSJustin T. Gibbs 			/*
25922cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
25932cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
25942cefde5fSJustin T. Gibbs 			 */
25952cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
25962cefde5fSJustin T. Gibbs 			break;
25972cefde5fSJustin T. Gibbs 		}
25982cefde5fSJustin T. Gibbs 		/*
25992cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
26002cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
26012cefde5fSJustin T. Gibbs 		 */
26022cefde5fSJustin T. Gibbs 	}
260307c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
26048b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
26058b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
26068b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
26078b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
26088b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
26092df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
26102df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
26112df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
26122df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
2613227d67aaSAlexander Motin 	case XPT_GET_TRAN_SETTINGS:
2614227d67aaSAlexander Motin 	case XPT_SET_TRAN_SETTINGS:
261587cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
2616227d67aaSAlexander Motin call_sim:
2617da396db2SAlexander Motin 		sim = path->bus->sim;
2618227d67aaSAlexander Motin 		lock = (mtx_owned(sim->mtx) == 0);
2619227d67aaSAlexander Motin 		if (lock)
2620227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
262187cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
2622227d67aaSAlexander Motin 		if (lock)
2623227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
262487cfaf0eSJustin T. Gibbs 		break;
262587cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2626da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
262787cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
262887cfaf0eSJustin T. Gibbs 		break;
26298b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2630a5479bc5SJustin T. Gibbs 	{
263187cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2632a5479bc5SJustin T. Gibbs 
2633da396db2SAlexander Motin 		dev = path->device;
263487cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
26358b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
26368b8a9b1dSJustin T. Gibbs 		} else {
26378b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
26388b8a9b1dSJustin T. Gibbs 
26398b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
264052c9ce25SScott Long 			cgd->protocol = dev->protocol;
26418b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
264252c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
264330a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
26448b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
26458b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
26468b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
26478b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
26488b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
26498b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
26508b8a9b1dSJustin T. Gibbs 		}
26518b8a9b1dSJustin T. Gibbs 		break;
2652a5479bc5SJustin T. Gibbs 	}
265387cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
265487cfaf0eSJustin T. Gibbs 	{
265587cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
265687cfaf0eSJustin T. Gibbs 
2657da396db2SAlexander Motin 		dev = path->device;
265887cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
265987cfaf0eSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
266087cfaf0eSJustin T. Gibbs 		} else {
266187cfaf0eSJustin T. Gibbs 			struct ccb_getdevstats *cgds;
266287cfaf0eSJustin T. Gibbs 			struct cam_eb *bus;
266387cfaf0eSJustin T. Gibbs 			struct cam_et *tar;
266487cfaf0eSJustin T. Gibbs 
266587cfaf0eSJustin T. Gibbs 			cgds = &start_ccb->cgds;
2666da396db2SAlexander Motin 			bus = path->bus;
2667da396db2SAlexander Motin 			tar = path->target;
266887cfaf0eSJustin T. Gibbs 			cgds->dev_openings = dev->ccbq.dev_openings;
266987cfaf0eSJustin T. Gibbs 			cgds->dev_active = dev->ccbq.dev_active;
267087cfaf0eSJustin T. Gibbs 			cgds->devq_openings = dev->ccbq.devq_openings;
2671ea541bfdSAlexander Motin 			cgds->devq_queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
267287cfaf0eSJustin T. Gibbs 			cgds->held = dev->ccbq.held;
267387cfaf0eSJustin T. Gibbs 			cgds->last_reset = tar->last_reset;
267452c9ce25SScott Long 			cgds->maxtags = dev->maxtags;
267552c9ce25SScott Long 			cgds->mintags = dev->mintags;
267687cfaf0eSJustin T. Gibbs 			if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
267787cfaf0eSJustin T. Gibbs 				cgds->last_reset = bus->last_reset;
267887cfaf0eSJustin T. Gibbs 			cgds->ccb_h.status = CAM_REQ_CMP;
267987cfaf0eSJustin T. Gibbs 		}
268087cfaf0eSJustin T. Gibbs 		break;
268187cfaf0eSJustin T. Gibbs 	}
26828b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
26838b8a9b1dSJustin T. Gibbs 	{
26848b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
26858b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
26868b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
26873393f8daSKenneth D. Merry 		u_int			i;
26888b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
26898b8a9b1dSJustin T. Gibbs 		int			found;
26908b8a9b1dSJustin T. Gibbs 
26918b8a9b1dSJustin T. Gibbs 
26928b8a9b1dSJustin T. Gibbs 		found = 0;
26938b8a9b1dSJustin T. Gibbs 
26948b8a9b1dSJustin T. Gibbs 		/*
26958b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
26968b8a9b1dSJustin T. Gibbs 		 */
2697da396db2SAlexander Motin 		device = path->device;
26988b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
26998b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
27008b8a9b1dSJustin T. Gibbs 
27018b8a9b1dSJustin T. Gibbs 		/*
27028b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
27038b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
27048b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
27058b8a9b1dSJustin T. Gibbs 		 * from the beginning.
27068b8a9b1dSJustin T. Gibbs 		 */
27078b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
27088b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
27098b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
27108b8a9b1dSJustin T. Gibbs 			break;
27118b8a9b1dSJustin T. Gibbs 		}
27128b8a9b1dSJustin T. Gibbs 
27138b8a9b1dSJustin T. Gibbs 		/*
27148b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
27158b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
27168b8a9b1dSJustin T. Gibbs 		 */
2717fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
27188b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2719fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
27208b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
27218b8a9b1dSJustin T. Gibbs 				strncpy(cgdl->periph_name,
27228b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
27238b8a9b1dSJustin T. Gibbs 					DEV_IDLEN);
27248b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
27258b8a9b1dSJustin T. Gibbs 				found = 1;
27268b8a9b1dSJustin T. Gibbs 			}
27278b8a9b1dSJustin T. Gibbs 		}
27288b8a9b1dSJustin T. Gibbs 		if (found == 0) {
27298b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27308b8a9b1dSJustin T. Gibbs 			break;
27318b8a9b1dSJustin T. Gibbs 		}
27328b8a9b1dSJustin T. Gibbs 
27338b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27348b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27358b8a9b1dSJustin T. Gibbs 		else
27368b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27378b8a9b1dSJustin T. Gibbs 
27388b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27398b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
27408b8a9b1dSJustin T. Gibbs 
27418b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
27428b8a9b1dSJustin T. Gibbs 		break;
27438b8a9b1dSJustin T. Gibbs 	}
27448b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
27458b8a9b1dSJustin T. Gibbs 	{
27468b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
27478b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
27488b8a9b1dSJustin T. Gibbs 
27498b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
27508b8a9b1dSJustin T. Gibbs 
27518b8a9b1dSJustin T. Gibbs 		/*
27528b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
27538b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
27548b8a9b1dSJustin T. Gibbs 		 * with a list of busses, then a list of targets on a bus,
27558b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
27568b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
27578b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
27588b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
27598b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
27608b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
27618b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
27628b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
27638b8a9b1dSJustin T. Gibbs 		 */
27648b8a9b1dSJustin T. Gibbs 
27658b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
27668b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
27678b8a9b1dSJustin T. Gibbs 		else {
27683393f8daSKenneth D. Merry 			u_int i;
27698b8a9b1dSJustin T. Gibbs 
27708b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
27718b8a9b1dSJustin T. Gibbs 
27728b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
27738b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
27748b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
27758b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
27768b8a9b1dSJustin T. Gibbs 					break;
27778b8a9b1dSJustin T. Gibbs 				}
27788b8a9b1dSJustin T. Gibbs 			}
27798b8a9b1dSJustin T. Gibbs 
27808b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
27818b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
27828b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
27838b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
27848b8a9b1dSJustin T. Gibbs 		}
27858b8a9b1dSJustin T. Gibbs 
27868b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
27878b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
278807c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
27898b8a9b1dSJustin T. Gibbs 			break;
27908b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
279107c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
27928b8a9b1dSJustin T. Gibbs 			break;
27938b8a9b1dSJustin T. Gibbs 		default:
27948b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
27958b8a9b1dSJustin T. Gibbs 			break;
27968b8a9b1dSJustin T. Gibbs 		}
27978b8a9b1dSJustin T. Gibbs 
27988b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
27998b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
28008b8a9b1dSJustin T. Gibbs 		else
28018b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
28028b8a9b1dSJustin T. Gibbs 
28038b8a9b1dSJustin T. Gibbs 		break;
28048b8a9b1dSJustin T. Gibbs 	}
28058b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
28068b8a9b1dSJustin T. Gibbs 	{
280784f82481SScott Long 		struct ccb_setasync *csa;
280884f82481SScott Long 		struct async_node *cur_entry;
280984f82481SScott Long 		struct async_list *async_head;
281084f82481SScott Long 		u_int32_t added;
281184f82481SScott Long 
281284f82481SScott Long 		csa = &start_ccb->csa;
281384f82481SScott Long 		added = csa->event_enable;
2814da396db2SAlexander Motin 		async_head = &path->device->asyncs;
281584f82481SScott Long 
281684f82481SScott Long 		/*
281784f82481SScott Long 		 * If there is already an entry for us, simply
281884f82481SScott Long 		 * update it.
281984f82481SScott Long 		 */
282084f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
282184f82481SScott Long 		while (cur_entry != NULL) {
282284f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
282384f82481SScott Long 			 && (cur_entry->callback == csa->callback))
282484f82481SScott Long 				break;
282584f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
282684f82481SScott Long 		}
282784f82481SScott Long 
282884f82481SScott Long 		if (cur_entry != NULL) {
282984f82481SScott Long 		 	/*
283084f82481SScott Long 			 * If the request has no flags set,
283184f82481SScott Long 			 * remove the entry.
283284f82481SScott Long 			 */
283384f82481SScott Long 			added &= ~cur_entry->event_enable;
283484f82481SScott Long 			if (csa->event_enable == 0) {
283584f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
283684f82481SScott Long 					     async_node, links);
2837da396db2SAlexander Motin 				xpt_release_device(path->device);
283884f82481SScott Long 				free(cur_entry, M_CAMXPT);
283984f82481SScott Long 			} else {
284084f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
284184f82481SScott Long 			}
28427685edecSAlexander Motin 			csa->event_enable = added;
284384f82481SScott Long 		} else {
284484f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
284584f82481SScott Long 					   M_NOWAIT);
284684f82481SScott Long 			if (cur_entry == NULL) {
284784f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
284884f82481SScott Long 				break;
284984f82481SScott Long 			}
285084f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
2851227d67aaSAlexander Motin 			cur_entry->event_lock =
2852227d67aaSAlexander Motin 			    mtx_owned(path->bus->sim->mtx) ? 1 : 0;
285384f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
285484f82481SScott Long 			cur_entry->callback = csa->callback;
285584f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2856da396db2SAlexander Motin 			xpt_acquire_device(path->device);
285784f82481SScott Long 		}
28588b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
28598b8a9b1dSJustin T. Gibbs 		break;
28608b8a9b1dSJustin T. Gibbs 	}
28618b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
28628b8a9b1dSJustin T. Gibbs 	{
28638b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
28648b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
28658b8a9b1dSJustin T. Gibbs 
28668b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
2867da396db2SAlexander Motin 		dev = path->device;
28688b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
28698b8a9b1dSJustin T. Gibbs 
28708b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
28718b8a9b1dSJustin T. Gibbs 			break;
28728b8a9b1dSJustin T. Gibbs 		}
28738b8a9b1dSJustin T. Gibbs 
28748b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
28758b8a9b1dSJustin T. Gibbs 
28768b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
28778b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
28787dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
287979ccc199SJordan K. Hubbard 				if (bootverbose) {
2880da396db2SAlexander Motin 					xpt_print(path,
28817dc3213dSAlexander Motin 					    "number of openings is now %d\n",
28828b8a9b1dSJustin T. Gibbs 					    crs->openings);
28838b8a9b1dSJustin T. Gibbs 				}
28848b8a9b1dSJustin T. Gibbs 			}
28858b8a9b1dSJustin T. Gibbs 		}
28868b8a9b1dSJustin T. Gibbs 
2887227d67aaSAlexander Motin 		mtx_lock(&dev->sim->devq->send_mtx);
28888b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
28898b8a9b1dSJustin T. Gibbs 
28908b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
28918b8a9b1dSJustin T. Gibbs 
28928b8a9b1dSJustin T. Gibbs 				/*
28938b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
28948b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
28958b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
28968b8a9b1dSJustin T. Gibbs 				 */
28978b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
28982b83592fSScott Long 				callout_stop(&dev->callout);
28998b8a9b1dSJustin T. Gibbs 			} else {
29008b8a9b1dSJustin T. Gibbs 
29018b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29028b8a9b1dSJustin T. Gibbs 			}
29038b8a9b1dSJustin T. Gibbs 
29042b83592fSScott Long 			callout_reset(&dev->callout,
29052b83592fSScott Long 			    (crs->release_timeout * hz) / 1000,
29062b83592fSScott Long 			    xpt_release_devq_timeout, dev);
29078b8a9b1dSJustin T. Gibbs 
29088b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
29098b8a9b1dSJustin T. Gibbs 
29108b8a9b1dSJustin T. Gibbs 		}
29118b8a9b1dSJustin T. Gibbs 
29128b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
29138b8a9b1dSJustin T. Gibbs 
29148b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
29158b8a9b1dSJustin T. Gibbs 				/*
29168b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
29178b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
29188b8a9b1dSJustin T. Gibbs 				 * the queue.
29198b8a9b1dSJustin T. Gibbs 				 */
29208b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29218b8a9b1dSJustin T. Gibbs 			} else {
29228b8a9b1dSJustin T. Gibbs 
29238b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
29248b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29258b8a9b1dSJustin T. Gibbs 			}
29268b8a9b1dSJustin T. Gibbs 		}
29278b8a9b1dSJustin T. Gibbs 
29288b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
29298b8a9b1dSJustin T. Gibbs 
29308b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
29318b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
29328b8a9b1dSJustin T. Gibbs 
29338b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29348b8a9b1dSJustin T. Gibbs 			} else {
29358b8a9b1dSJustin T. Gibbs 
29368b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
29378b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29388b8a9b1dSJustin T. Gibbs 			}
29398b8a9b1dSJustin T. Gibbs 		}
2940227d67aaSAlexander Motin 		mtx_unlock(&dev->sim->devq->send_mtx);
29418b8a9b1dSJustin T. Gibbs 
2942cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2943cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2944cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
29458b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29468b8a9b1dSJustin T. Gibbs 		break;
29478b8a9b1dSJustin T. Gibbs 	}
29488b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
294980d6987cSAlexander Motin 		struct cam_path *oldpath;
295080d6987cSAlexander Motin 
2951f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
295222c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2953f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2954f0f25b9cSAlexander Motin 			break;
2955f0f25b9cSAlexander Motin 		}
2956f0f25b9cSAlexander Motin 
295780d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
29588b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
295980d6987cSAlexander Motin 			oldpath = cam_dpath;
29608b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
296180d6987cSAlexander Motin 			xpt_free_path(oldpath);
29628b8a9b1dSJustin T. Gibbs 		}
296380d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
2964e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
29658b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
29668b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
29678b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
29688b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
29698b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2970aa872be6SMatt Jacob 			} else {
297180d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
29728b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
2973f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
2974f0d9af51SMatt Jacob 				    cam_dflags);
2975aa872be6SMatt Jacob 			}
297680d6987cSAlexander Motin 		} else
29778b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
29788b8a9b1dSJustin T. Gibbs 		break;
29798b8a9b1dSJustin T. Gibbs 	}
29808b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
298187cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2982da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
29838b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29848b8a9b1dSJustin T. Gibbs 		break;
29858b8a9b1dSJustin T. Gibbs 	default:
29868b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
29878b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
29888b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
29898b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
29903501942bSJustin T. Gibbs 		printf("%s: CCB type %#x not supported\n", __func__,
29913501942bSJustin T. Gibbs 		       start_ccb->ccb_h.func_code);
29928b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2993b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2994b882a6d3SMatt Jacob 			xpt_done(start_ccb);
2995b882a6d3SMatt Jacob 		}
29968b8a9b1dSJustin T. Gibbs 		break;
29978b8a9b1dSJustin T. Gibbs 	}
29988b8a9b1dSJustin T. Gibbs }
29998b8a9b1dSJustin T. Gibbs 
30008b8a9b1dSJustin T. Gibbs void
30018b8a9b1dSJustin T. Gibbs xpt_polled_action(union ccb *start_ccb)
30028b8a9b1dSJustin T. Gibbs {
30038b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
30048b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
30058b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
30068b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
30078b8a9b1dSJustin T. Gibbs 
30080069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
30098b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
30108b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
30118b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
30128b8a9b1dSJustin T. Gibbs 
3013227d67aaSAlexander Motin 	mtx_unlock(&dev->device_mtx);
30144a612489SAlexander Motin 
30158b8a9b1dSJustin T. Gibbs 	/*
30168b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
30178b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
30188b8a9b1dSJustin T. Gibbs 	 */
3019227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
30208b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings--;
30218b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
3022227d67aaSAlexander Motin 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3023227d67aaSAlexander Motin 	    (--timeout > 0)) {
3024227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
30250069293bSAlexander Motin 		DELAY(100);
3026227d67aaSAlexander Motin 		CAM_SIM_LOCK(sim);
30278b8a9b1dSJustin T. Gibbs 		(*(sim->sim_poll))(sim);
3028227d67aaSAlexander Motin 		CAM_SIM_UNLOCK(sim);
3029227d67aaSAlexander Motin 		camisr_runqueue();
3030227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
30318b8a9b1dSJustin T. Gibbs 	}
30328b8a9b1dSJustin T. Gibbs 	dev->ccbq.devq_openings++;
30338b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
3034227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
30358b8a9b1dSJustin T. Gibbs 
30368b8a9b1dSJustin T. Gibbs 	if (timeout != 0) {
30378b8a9b1dSJustin T. Gibbs 		xpt_action(start_ccb);
30388b8a9b1dSJustin T. Gibbs 		while(--timeout > 0) {
3039227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
30408b8a9b1dSJustin T. Gibbs 			(*(sim->sim_poll))(sim);
3041227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
3042227d67aaSAlexander Motin 			camisr_runqueue();
30438b8a9b1dSJustin T. Gibbs 			if ((start_ccb->ccb_h.status  & CAM_STATUS_MASK)
30448b8a9b1dSJustin T. Gibbs 			    != CAM_REQ_INPROG)
30458b8a9b1dSJustin T. Gibbs 				break;
30460069293bSAlexander Motin 			DELAY(100);
30478b8a9b1dSJustin T. Gibbs 		}
30488b8a9b1dSJustin T. Gibbs 		if (timeout == 0) {
30498b8a9b1dSJustin T. Gibbs 			/*
30508b8a9b1dSJustin T. Gibbs 			 * XXX Is it worth adding a sim_timeout entry
30518b8a9b1dSJustin T. Gibbs 			 * point so we can attempt recovery?  If
30528b8a9b1dSJustin T. Gibbs 			 * this is only used for dumps, I don't think
30538b8a9b1dSJustin T. Gibbs 			 * it is.
30548b8a9b1dSJustin T. Gibbs 			 */
30558b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
30568b8a9b1dSJustin T. Gibbs 		}
30578b8a9b1dSJustin T. Gibbs 	} else {
30588b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
30598b8a9b1dSJustin T. Gibbs 	}
30604a612489SAlexander Motin 
3061227d67aaSAlexander Motin 	mtx_lock(&dev->device_mtx);
30628b8a9b1dSJustin T. Gibbs }
30638b8a9b1dSJustin T. Gibbs 
30648b8a9b1dSJustin T. Gibbs /*
30658b8a9b1dSJustin T. Gibbs  * Schedule a peripheral driver to receive a ccb when it's
30668b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
30678b8a9b1dSJustin T. Gibbs  */
30688b8a9b1dSJustin T. Gibbs void
3069227d67aaSAlexander Motin xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
30708b8a9b1dSJustin T. Gibbs {
30718b8a9b1dSJustin T. Gibbs 
3072227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3073227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3074227d67aaSAlexander Motin 	if (new_priority < periph->scheduled_priority) {
3075227d67aaSAlexander Motin 		periph->scheduled_priority = new_priority;
3076227d67aaSAlexander Motin 		xpt_run_allocq(periph, 0);
30778b8a9b1dSJustin T. Gibbs 	}
30788b8a9b1dSJustin T. Gibbs }
30798b8a9b1dSJustin T. Gibbs 
30808b8a9b1dSJustin T. Gibbs 
30818b8a9b1dSJustin T. Gibbs /*
30828b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
30838b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
30848b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
30858b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
30868b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
308777dc25ccSScott Long  * to run the queue.
30888b8a9b1dSJustin T. Gibbs  */
3089227d67aaSAlexander Motin static int
30908b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
30918b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
30928b8a9b1dSJustin T. Gibbs {
30938b8a9b1dSJustin T. Gibbs 	int retval;
30948b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
30958b8a9b1dSJustin T. Gibbs 
3096aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
30978b8a9b1dSJustin T. Gibbs 
30988b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
30998b8a9b1dSJustin T. Gibbs 
31008b8a9b1dSJustin T. Gibbs 	/*
31018b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
31028b8a9b1dSJustin T. Gibbs 	 */
31038b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
31048b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31058b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
31068b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
31078b8a9b1dSJustin T. Gibbs 					     new_priority);
3108aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31098b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
31108b8a9b1dSJustin T. Gibbs 					 new_priority));
311183c5d981SAlexander Motin 			retval = 1;
311283c5d981SAlexander Motin 		} else
31138b8a9b1dSJustin T. Gibbs 			retval = 0;
31148b8a9b1dSJustin T. Gibbs 	} else {
31158b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
31168b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
31178b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
31188b8a9b1dSJustin T. Gibbs 
3119aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31208b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
31218bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
31228b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
31238b8a9b1dSJustin T. Gibbs 		retval = 1;
31248b8a9b1dSJustin T. Gibbs 	}
31258b8a9b1dSJustin T. Gibbs 	return (retval);
31268b8a9b1dSJustin T. Gibbs }
31278b8a9b1dSJustin T. Gibbs 
31288b8a9b1dSJustin T. Gibbs static void
3129227d67aaSAlexander Motin xpt_run_allocq_task(void *context, int pending)
31308b8a9b1dSJustin T. Gibbs {
3131227d67aaSAlexander Motin 	struct cam_periph *periph = context;
31328b8a9b1dSJustin T. Gibbs 
3133227d67aaSAlexander Motin 	cam_periph_lock(periph);
3134227d67aaSAlexander Motin 	periph->flags &= ~CAM_PERIPH_RUN_TASK;
3135227d67aaSAlexander Motin 	xpt_run_allocq(periph, 1);
3136227d67aaSAlexander Motin 	cam_periph_unlock(periph);
3137227d67aaSAlexander Motin 	cam_periph_release(periph);
3138227d67aaSAlexander Motin }
3139227d67aaSAlexander Motin 
3140227d67aaSAlexander Motin static void
3141227d67aaSAlexander Motin xpt_run_allocq(struct cam_periph *periph, int sleep)
3142227d67aaSAlexander Motin {
3143227d67aaSAlexander Motin 	struct cam_ed	*device;
3144227d67aaSAlexander Motin 	union ccb	*ccb;
3145227d67aaSAlexander Motin 	uint32_t	 prio;
3146227d67aaSAlexander Motin 
3147227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3148227d67aaSAlexander Motin 	if (periph->periph_allocating)
3149cccf4220SAlexander Motin 		return;
3150227d67aaSAlexander Motin 	periph->periph_allocating = 1;
3151227d67aaSAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3152227d67aaSAlexander Motin 	device = periph->path->device;
3153227d67aaSAlexander Motin 	ccb = NULL;
3154227d67aaSAlexander Motin restart:
3155227d67aaSAlexander Motin 	while ((prio = min(periph->scheduled_priority,
3156227d67aaSAlexander Motin 	    periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3157227d67aaSAlexander Motin 	    (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3158227d67aaSAlexander Motin 	     device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3159cccf4220SAlexander Motin 
3160227d67aaSAlexander Motin 		if (ccb == NULL &&
3161227d67aaSAlexander Motin 		    (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3162227d67aaSAlexander Motin 			if (sleep) {
3163227d67aaSAlexander Motin 				ccb = xpt_get_ccb(periph);
3164227d67aaSAlexander Motin 				goto restart;
3165227d67aaSAlexander Motin 			}
31668ec5ab3fSAlexander Motin 			if (periph->flags & CAM_PERIPH_RUN_TASK)
31678b8a9b1dSJustin T. Gibbs 				break;
31688ec5ab3fSAlexander Motin 			xpt_lock_buses();
31698ec5ab3fSAlexander Motin 			periph->refcount++;	/* Unconditionally acquire */
31708ec5ab3fSAlexander Motin 			xpt_unlock_buses();
3171227d67aaSAlexander Motin 			periph->flags |= CAM_PERIPH_RUN_TASK;
3172227d67aaSAlexander Motin 			taskqueue_enqueue(xsoftc.xpt_taskq,
3173227d67aaSAlexander Motin 			    &periph->periph_run_task);
3174227d67aaSAlexander Motin 			break;
31758b8a9b1dSJustin T. Gibbs 		}
3176227d67aaSAlexander Motin 		xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3177227d67aaSAlexander Motin 		if (prio == periph->immediate_priority) {
3178227d67aaSAlexander Motin 			periph->immediate_priority = CAM_PRIORITY_NONE;
3179227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3180227d67aaSAlexander Motin 					("waking cam_periph_getccb()\n"));
3181227d67aaSAlexander Motin 			SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3182227d67aaSAlexander Motin 					  periph_links.sle);
3183227d67aaSAlexander Motin 			wakeup(&periph->ccb_list);
3184227d67aaSAlexander Motin 		} else {
3185227d67aaSAlexander Motin 			periph->scheduled_priority = CAM_PRIORITY_NONE;
3186227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3187227d67aaSAlexander Motin 					("calling periph_start()\n"));
3188227d67aaSAlexander Motin 			periph->periph_start(periph, ccb);
3189227d67aaSAlexander Motin 		}
3190227d67aaSAlexander Motin 		ccb = NULL;
3191227d67aaSAlexander Motin 	}
3192227d67aaSAlexander Motin 	if (ccb != NULL)
3193227d67aaSAlexander Motin 		xpt_release_ccb(ccb);
3194227d67aaSAlexander Motin 	periph->periph_allocating = 0;
31958b8a9b1dSJustin T. Gibbs }
31968b8a9b1dSJustin T. Gibbs 
319783c5d981SAlexander Motin static void
3198cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
31998b8a9b1dSJustin T. Gibbs {
3200de9ebb68SAlexander Motin 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3201227d67aaSAlexander Motin 	int lock;
32028b8a9b1dSJustin T. Gibbs 
3203cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
32048b8a9b1dSJustin T. Gibbs 
3205cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
32068b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3207ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3208cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
32098b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
32108b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
32118b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
32128b8a9b1dSJustin T. Gibbs 
3213227d67aaSAlexander Motin 		device = (struct cam_ed *)camq_remove(&devq->send_queue,
32145a526431SJustin T. Gibbs 							   CAMQ_HEAD);
3215aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
321650642f18SKenneth D. Merry 				("running device %p\n", device));
32178b8a9b1dSJustin T. Gibbs 
32185a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
32198b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
322057b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
32218b8a9b1dSJustin T. Gibbs 			continue;
32228b8a9b1dSJustin T. Gibbs 		}
32238b8a9b1dSJustin T. Gibbs 
32248b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
32258b8a9b1dSJustin T. Gibbs 
32262b83592fSScott Long 			mtx_lock(&xsoftc.xpt_lock);
32272b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
32288b8a9b1dSJustin T. Gibbs 				/*
32298b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
32308b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
32318b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
32328b8a9b1dSJustin T. Gibbs 				 * available.
32338b8a9b1dSJustin T. Gibbs 				 */
323483c5d981SAlexander Motin 				xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3235227d67aaSAlexander Motin 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3236ea541bfdSAlexander Motin 						   highpowerq_entry);
32378b8a9b1dSJustin T. Gibbs 
3238469f9f44SScott Long 				mtx_unlock(&xsoftc.xpt_lock);
32398b8a9b1dSJustin T. Gibbs 				continue;
32408b8a9b1dSJustin T. Gibbs 			} else {
32418b8a9b1dSJustin T. Gibbs 				/*
32428b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
32438b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
32448b8a9b1dSJustin T. Gibbs 				 */
32452b83592fSScott Long 				xsoftc.num_highpower--;
32468b8a9b1dSJustin T. Gibbs 			}
32472b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
32488b8a9b1dSJustin T. Gibbs 		}
32498b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
32508b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
32518b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
32528b8a9b1dSJustin T. Gibbs 		devq->send_active++;
3253cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
3254227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
32558b8a9b1dSJustin T. Gibbs 
3256cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
32578b8a9b1dSJustin T. Gibbs 			/*
32588b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
32598b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
32608b8a9b1dSJustin T. Gibbs 			 */
326183c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
32628b8a9b1dSJustin T. Gibbs 		}
32638b8a9b1dSJustin T. Gibbs 
3264a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3265a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3266a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3267a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
32688b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
32698b8a9b1dSJustin T. Gibbs 			else
32708b8a9b1dSJustin T. Gibbs 				/*
3271a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3272a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
32738b8a9b1dSJustin T. Gibbs 				 */
32748b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3275a4eb4f16SMatt Jacob 		}
32768b8a9b1dSJustin T. Gibbs 
3277de9ebb68SAlexander Motin 		switch (work_ccb->ccb_h.func_code) {
3278de9ebb68SAlexander Motin 		case XPT_SCSI_IO:
3279de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3280de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. CDB: %s\n",
3281de9ebb68SAlexander Motin 			     scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3282de9ebb68SAlexander Motin 					  &device->inq_data),
3283de9ebb68SAlexander Motin 			     scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3284de9ebb68SAlexander Motin 					     cdb_str, sizeof(cdb_str))));
3285de9ebb68SAlexander Motin 			break;
3286de9ebb68SAlexander Motin 		case XPT_ATA_IO:
3287de9ebb68SAlexander Motin 			CAM_DEBUG(work_ccb->ccb_h.path,
3288de9ebb68SAlexander Motin 			    CAM_DEBUG_CDB,("%s. ACB: %s\n",
3289de9ebb68SAlexander Motin 			     ata_op_string(&work_ccb->ataio.cmd),
3290de9ebb68SAlexander Motin 			     ata_cmd_string(&work_ccb->ataio.cmd,
3291de9ebb68SAlexander Motin 					    cdb_str, sizeof(cdb_str))));
3292de9ebb68SAlexander Motin 			break;
3293de9ebb68SAlexander Motin 		default:
3294de9ebb68SAlexander Motin 			break;
3295de9ebb68SAlexander Motin 		}
3296de9ebb68SAlexander Motin 
32978b8a9b1dSJustin T. Gibbs 		/*
3298227d67aaSAlexander Motin 		 * Device queues can be shared among multiple SIM instances
3299227d67aaSAlexander Motin 		 * that reside on different busses.  Use the SIM from the
3300227d67aaSAlexander Motin 		 * queued device, rather than the one from the calling bus.
33018b8a9b1dSJustin T. Gibbs 		 */
3302227d67aaSAlexander Motin 		sim = device->sim;
3303227d67aaSAlexander Motin 		lock = (mtx_owned(sim->mtx) == 0);
3304227d67aaSAlexander Motin 		if (lock)
3305227d67aaSAlexander Motin 			CAM_SIM_LOCK(sim);
33068b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
3307227d67aaSAlexander Motin 		if (lock)
3308227d67aaSAlexander Motin 			CAM_SIM_UNLOCK(sim);
3309227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
33108b8a9b1dSJustin T. Gibbs 	}
3311cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
33128b8a9b1dSJustin T. Gibbs }
33138b8a9b1dSJustin T. Gibbs 
33148b8a9b1dSJustin T. Gibbs /*
33158b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
33168b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
33178b8a9b1dSJustin T. Gibbs  */
33188b8a9b1dSJustin T. Gibbs void
33198b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
33208b8a9b1dSJustin T. Gibbs {
332168153f43SScott Long 
33228b8a9b1dSJustin T. Gibbs 	/*
33238b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
33248b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
33258b8a9b1dSJustin T. Gibbs 	 */
33268b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
33278b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
33288b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
33298b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
33308b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
33318b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
33328b8a9b1dSJustin T. Gibbs }
33338b8a9b1dSJustin T. Gibbs 
33348b8a9b1dSJustin T. Gibbs void
33358b8a9b1dSJustin T. Gibbs xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
33368b8a9b1dSJustin T. Gibbs {
333768153f43SScott Long 
33388b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
33398b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
33408b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
33418b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
33428b8a9b1dSJustin T. Gibbs 	if (path->target)
33438b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
33448b8a9b1dSJustin T. Gibbs 	else
33458b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
33468b8a9b1dSJustin T. Gibbs 	if (path->device) {
33478b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
33488bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
33498b8a9b1dSJustin T. Gibbs 	} else {
33508b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
33518b8a9b1dSJustin T. Gibbs 	}
33528b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
33538b8a9b1dSJustin T. Gibbs 	ccb_h->flags = 0;
3354b5595753SNathan Whitehorn 	ccb_h->xflags = 0;
33558b8a9b1dSJustin T. Gibbs }
33568b8a9b1dSJustin T. Gibbs 
33578b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
33588b8a9b1dSJustin T. Gibbs cam_status
33598b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
33608b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
33618b8a9b1dSJustin T. Gibbs {
33628b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
33638b8a9b1dSJustin T. Gibbs 	cam_status status;
33648b8a9b1dSJustin T. Gibbs 
3365596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
33668b8a9b1dSJustin T. Gibbs 
33678b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
33688b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
33698b8a9b1dSJustin T. Gibbs 		return(status);
33708b8a9b1dSJustin T. Gibbs 	}
33718b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
33728b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3373596ee08fSAlexander Motin 		free(path, M_CAMPATH);
33748b8a9b1dSJustin T. Gibbs 		path = NULL;
33758b8a9b1dSJustin T. Gibbs 	}
33768b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
33778b8a9b1dSJustin T. Gibbs 	return (status);
33788b8a9b1dSJustin T. Gibbs }
33798b8a9b1dSJustin T. Gibbs 
33802b83592fSScott Long cam_status
33812b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
33822b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
33832b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
33842b83592fSScott Long {
33852b83592fSScott Long 
3386227d67aaSAlexander Motin 	return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3387227d67aaSAlexander Motin 	    lun_id));
33882b83592fSScott Long }
33892b83592fSScott Long 
339052c9ce25SScott Long cam_status
33918b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
33928b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
33938b8a9b1dSJustin T. Gibbs {
33948b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
33958b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
33968b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
33978b8a9b1dSJustin T. Gibbs 	cam_status   status;
33988b8a9b1dSJustin T. Gibbs 
33998b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
34008b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
34018b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3402a5479bc5SJustin T. Gibbs 
3403a5479bc5SJustin T. Gibbs 	/*
3404a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3405a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3406a5479bc5SJustin T. Gibbs 	 */
34078b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
34088b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
34098b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3410c8bead2aSJustin T. Gibbs 	} else {
3411227d67aaSAlexander Motin 		xpt_lock_buses();
3412227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
34138b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
34148b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
34158b8a9b1dSJustin T. Gibbs 			/* Create one */
34168b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
34178b8a9b1dSJustin T. Gibbs 
34188b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
34198b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
34208b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
34218b8a9b1dSJustin T. Gibbs 			} else {
34228b8a9b1dSJustin T. Gibbs 				target = new_target;
34238b8a9b1dSJustin T. Gibbs 			}
34248b8a9b1dSJustin T. Gibbs 		}
3425227d67aaSAlexander Motin 		xpt_unlock_buses();
3426c8bead2aSJustin T. Gibbs 		if (target != NULL) {
34278b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
34288b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
34298b8a9b1dSJustin T. Gibbs 				/* Create one */
34308b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
34318b8a9b1dSJustin T. Gibbs 
343252c9ce25SScott Long 				new_device =
343352c9ce25SScott Long 				    (*(bus->xport->alloc_device))(bus,
34348b8a9b1dSJustin T. Gibbs 								      target,
34358b8a9b1dSJustin T. Gibbs 								      lun_id);
34368b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
34378b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
34388b8a9b1dSJustin T. Gibbs 				} else {
34398b8a9b1dSJustin T. Gibbs 					device = new_device;
34408b8a9b1dSJustin T. Gibbs 				}
34418b8a9b1dSJustin T. Gibbs 			}
34428b8a9b1dSJustin T. Gibbs 		}
3443227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
34448b8a9b1dSJustin T. Gibbs 	}
34458b8a9b1dSJustin T. Gibbs 
34468b8a9b1dSJustin T. Gibbs 	/*
34478b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
34488b8a9b1dSJustin T. Gibbs 	 */
34498b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
34508b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
34518b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
34528b8a9b1dSJustin T. Gibbs 		new_path->target = target;
34538b8a9b1dSJustin T. Gibbs 		new_path->device = device;
34548b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
34558b8a9b1dSJustin T. Gibbs 	} else {
34568b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3457f98d7a47SAlexander Motin 			xpt_release_device(device);
34588b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3459f98d7a47SAlexander Motin 			xpt_release_target(target);
3460a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3461a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
34628b8a9b1dSJustin T. Gibbs 	}
34638b8a9b1dSJustin T. Gibbs 	return (status);
34648b8a9b1dSJustin T. Gibbs }
34658b8a9b1dSJustin T. Gibbs 
3466227d67aaSAlexander Motin cam_status
3467227d67aaSAlexander Motin xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3468227d67aaSAlexander Motin {
3469227d67aaSAlexander Motin 	struct	   cam_path *new_path;
3470227d67aaSAlexander Motin 
3471227d67aaSAlexander Motin 	new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3472227d67aaSAlexander Motin 	if (new_path == NULL)
3473227d67aaSAlexander Motin 		return(CAM_RESRC_UNAVAIL);
3474227d67aaSAlexander Motin 	xpt_copy_path(new_path, path);
3475227d67aaSAlexander Motin 	*new_path_ptr = new_path;
3476227d67aaSAlexander Motin 	return (CAM_REQ_CMP);
3477227d67aaSAlexander Motin }
3478227d67aaSAlexander Motin 
3479227d67aaSAlexander Motin void
3480227d67aaSAlexander Motin xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3481227d67aaSAlexander Motin {
3482227d67aaSAlexander Motin 
3483227d67aaSAlexander Motin 	*new_path = *path;
3484227d67aaSAlexander Motin 	if (path->bus != NULL)
3485227d67aaSAlexander Motin 		xpt_acquire_bus(path->bus);
3486227d67aaSAlexander Motin 	if (path->target != NULL)
3487227d67aaSAlexander Motin 		xpt_acquire_target(path->target);
3488227d67aaSAlexander Motin 	if (path->device != NULL)
3489227d67aaSAlexander Motin 		xpt_acquire_device(path->device);
3490227d67aaSAlexander Motin }
3491227d67aaSAlexander Motin 
349252c9ce25SScott Long void
34938b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
34948b8a9b1dSJustin T. Gibbs {
34958b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
34969dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3497f98d7a47SAlexander Motin 		xpt_release_device(path->device);
34989dd03ecfSJustin T. Gibbs 		path->device = NULL;
34999dd03ecfSJustin T. Gibbs 	}
35009dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3501f98d7a47SAlexander Motin 		xpt_release_target(path->target);
35029dd03ecfSJustin T. Gibbs 		path->target = NULL;
35039dd03ecfSJustin T. Gibbs 	}
35049dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
35059dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
35069dd03ecfSJustin T. Gibbs 		path->bus = NULL;
35079dd03ecfSJustin T. Gibbs 	}
35088b8a9b1dSJustin T. Gibbs }
35098b8a9b1dSJustin T. Gibbs 
35108b8a9b1dSJustin T. Gibbs void
35118b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
35128b8a9b1dSJustin T. Gibbs {
351368153f43SScott Long 
35148b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
35158b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3516596ee08fSAlexander Motin 	free(path, M_CAMPATH);
35178b8a9b1dSJustin T. Gibbs }
35188b8a9b1dSJustin T. Gibbs 
351915975b7bSMatt Jacob void
352015975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
352115975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
352215975b7bSMatt Jacob {
352315975b7bSMatt Jacob 
35249a7c2696SAlexander Motin 	xpt_lock_buses();
352515975b7bSMatt Jacob 	if (bus_ref) {
352615975b7bSMatt Jacob 		if (path->bus)
352715975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
352815975b7bSMatt Jacob 		else
352915975b7bSMatt Jacob 			*bus_ref = 0;
353015975b7bSMatt Jacob 	}
353115975b7bSMatt Jacob 	if (periph_ref) {
353215975b7bSMatt Jacob 		if (path->periph)
353315975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
353415975b7bSMatt Jacob 		else
353515975b7bSMatt Jacob 			*periph_ref = 0;
353615975b7bSMatt Jacob 	}
3537dcdf6e74SAlexander Motin 	xpt_unlock_buses();
353815975b7bSMatt Jacob 	if (target_ref) {
353915975b7bSMatt Jacob 		if (path->target)
354015975b7bSMatt Jacob 			*target_ref = path->target->refcount;
354115975b7bSMatt Jacob 		else
354215975b7bSMatt Jacob 			*target_ref = 0;
354315975b7bSMatt Jacob 	}
354415975b7bSMatt Jacob 	if (device_ref) {
354515975b7bSMatt Jacob 		if (path->device)
354615975b7bSMatt Jacob 			*device_ref = path->device->refcount;
354715975b7bSMatt Jacob 		else
354815975b7bSMatt Jacob 			*device_ref = 0;
354915975b7bSMatt Jacob 	}
355015975b7bSMatt Jacob }
35518b8a9b1dSJustin T. Gibbs 
35528b8a9b1dSJustin T. Gibbs /*
35532cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
35542cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
35558b8a9b1dSJustin T. Gibbs  */
35568b8a9b1dSJustin T. Gibbs int
35578b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
35588b8a9b1dSJustin T. Gibbs {
35598b8a9b1dSJustin T. Gibbs 	int retval = 0;
35608b8a9b1dSJustin T. Gibbs 
35618b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
35622cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
35638b8a9b1dSJustin T. Gibbs 			retval = 1;
35642cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
35652cefde5fSJustin T. Gibbs 			retval = 2;
35668b8a9b1dSJustin T. Gibbs 		else
35678b8a9b1dSJustin T. Gibbs 			return (-1);
35688b8a9b1dSJustin T. Gibbs 	}
35698b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
35702cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
35712cefde5fSJustin T. Gibbs 			if (retval == 0)
35728b8a9b1dSJustin T. Gibbs 				retval = 1;
35732cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
35742cefde5fSJustin T. Gibbs 			retval = 2;
35758b8a9b1dSJustin T. Gibbs 		else
35768b8a9b1dSJustin T. Gibbs 			return (-1);
35778b8a9b1dSJustin T. Gibbs 	}
35788b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
35792cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
35802cefde5fSJustin T. Gibbs 			if (retval == 0)
35818b8a9b1dSJustin T. Gibbs 				retval = 1;
35822cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
35832cefde5fSJustin T. Gibbs 			retval = 2;
35848b8a9b1dSJustin T. Gibbs 		else
35858b8a9b1dSJustin T. Gibbs 			return (-1);
35868b8a9b1dSJustin T. Gibbs 	}
35878b8a9b1dSJustin T. Gibbs 	return (retval);
35888b8a9b1dSJustin T. Gibbs }
35898b8a9b1dSJustin T. Gibbs 
35900d4f3c31SAlexander Motin int
35910d4f3c31SAlexander Motin xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
35920d4f3c31SAlexander Motin {
35930d4f3c31SAlexander Motin 	int retval = 0;
35940d4f3c31SAlexander Motin 
35950d4f3c31SAlexander Motin 	if (path->bus != dev->target->bus) {
35960d4f3c31SAlexander Motin 		if (path->bus->path_id == CAM_BUS_WILDCARD)
35970d4f3c31SAlexander Motin 			retval = 1;
35980d4f3c31SAlexander Motin 		else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
35990d4f3c31SAlexander Motin 			retval = 2;
36000d4f3c31SAlexander Motin 		else
36010d4f3c31SAlexander Motin 			return (-1);
36020d4f3c31SAlexander Motin 	}
36030d4f3c31SAlexander Motin 	if (path->target != dev->target) {
36040d4f3c31SAlexander Motin 		if (path->target->target_id == CAM_TARGET_WILDCARD) {
36050d4f3c31SAlexander Motin 			if (retval == 0)
36060d4f3c31SAlexander Motin 				retval = 1;
36070d4f3c31SAlexander Motin 		} else if (dev->target->target_id == CAM_TARGET_WILDCARD)
36080d4f3c31SAlexander Motin 			retval = 2;
36090d4f3c31SAlexander Motin 		else
36100d4f3c31SAlexander Motin 			return (-1);
36110d4f3c31SAlexander Motin 	}
36120d4f3c31SAlexander Motin 	if (path->device != dev) {
36130d4f3c31SAlexander Motin 		if (path->device->lun_id == CAM_LUN_WILDCARD) {
36140d4f3c31SAlexander Motin 			if (retval == 0)
36150d4f3c31SAlexander Motin 				retval = 1;
36160d4f3c31SAlexander Motin 		} else if (dev->lun_id == CAM_LUN_WILDCARD)
36170d4f3c31SAlexander Motin 			retval = 2;
36180d4f3c31SAlexander Motin 		else
36190d4f3c31SAlexander Motin 			return (-1);
36200d4f3c31SAlexander Motin 	}
36210d4f3c31SAlexander Motin 	return (retval);
36220d4f3c31SAlexander Motin }
36230d4f3c31SAlexander Motin 
36248b8a9b1dSJustin T. Gibbs void
36258b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
36268b8a9b1dSJustin T. Gibbs {
362768153f43SScott Long 
36288b8a9b1dSJustin T. Gibbs 	if (path == NULL)
36298b8a9b1dSJustin T. Gibbs 		printf("(nopath): ");
36308b8a9b1dSJustin T. Gibbs 	else {
36318b8a9b1dSJustin T. Gibbs 		if (path->periph != NULL)
36328b8a9b1dSJustin T. Gibbs 			printf("(%s%d:", path->periph->periph_name,
36338b8a9b1dSJustin T. Gibbs 			       path->periph->unit_number);
36348b8a9b1dSJustin T. Gibbs 		else
36358b8a9b1dSJustin T. Gibbs 			printf("(noperiph:");
36368b8a9b1dSJustin T. Gibbs 
36378b8a9b1dSJustin T. Gibbs 		if (path->bus != NULL)
36388b8a9b1dSJustin T. Gibbs 			printf("%s%d:%d:", path->bus->sim->sim_name,
36398b8a9b1dSJustin T. Gibbs 			       path->bus->sim->unit_number,
36408b8a9b1dSJustin T. Gibbs 			       path->bus->sim->bus_id);
36418b8a9b1dSJustin T. Gibbs 		else
36428b8a9b1dSJustin T. Gibbs 			printf("nobus:");
36438b8a9b1dSJustin T. Gibbs 
36448b8a9b1dSJustin T. Gibbs 		if (path->target != NULL)
36458b8a9b1dSJustin T. Gibbs 			printf("%d:", path->target->target_id);
36468b8a9b1dSJustin T. Gibbs 		else
36478b8a9b1dSJustin T. Gibbs 			printf("X:");
36488b8a9b1dSJustin T. Gibbs 
36498b8a9b1dSJustin T. Gibbs 		if (path->device != NULL)
3650*abe83505SNathan Whitehorn 			printf("%jx): ", (uintmax_t)path->device->lun_id);
36518b8a9b1dSJustin T. Gibbs 		else
36528b8a9b1dSJustin T. Gibbs 			printf("X): ");
36538b8a9b1dSJustin T. Gibbs 	}
36548b8a9b1dSJustin T. Gibbs }
36558b8a9b1dSJustin T. Gibbs 
3656f0d9af51SMatt Jacob void
36570d4f3c31SAlexander Motin xpt_print_device(struct cam_ed *device)
36580d4f3c31SAlexander Motin {
36590d4f3c31SAlexander Motin 
36600d4f3c31SAlexander Motin 	if (device == NULL)
36610d4f3c31SAlexander Motin 		printf("(nopath): ");
36620d4f3c31SAlexander Motin 	else {
3663*abe83505SNathan Whitehorn 		printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
36640d4f3c31SAlexander Motin 		       device->sim->unit_number,
36650d4f3c31SAlexander Motin 		       device->sim->bus_id,
36660d4f3c31SAlexander Motin 		       device->target->target_id,
3667*abe83505SNathan Whitehorn 		       (uintmax_t)device->lun_id);
36680d4f3c31SAlexander Motin 	}
36690d4f3c31SAlexander Motin }
36700d4f3c31SAlexander Motin 
36710d4f3c31SAlexander Motin void
3672f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3673f0d9af51SMatt Jacob {
3674f0d9af51SMatt Jacob 	va_list ap;
3675f0d9af51SMatt Jacob 	xpt_print_path(path);
3676f0d9af51SMatt Jacob 	va_start(ap, fmt);
3677f0d9af51SMatt Jacob 	vprintf(fmt, ap);
3678f0d9af51SMatt Jacob 	va_end(ap);
3679f0d9af51SMatt Jacob }
3680f0d9af51SMatt Jacob 
36813393f8daSKenneth D. Merry int
36823393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
36833393f8daSKenneth D. Merry {
36843393f8daSKenneth D. Merry 	struct sbuf sb;
36853393f8daSKenneth D. Merry 
36863393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
36873393f8daSKenneth D. Merry 
36883393f8daSKenneth D. Merry 	if (path == NULL)
36893393f8daSKenneth D. Merry 		sbuf_printf(&sb, "(nopath): ");
36903393f8daSKenneth D. Merry 	else {
36913393f8daSKenneth D. Merry 		if (path->periph != NULL)
36923393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
36933393f8daSKenneth D. Merry 				    path->periph->unit_number);
36943393f8daSKenneth D. Merry 		else
36953393f8daSKenneth D. Merry 			sbuf_printf(&sb, "(noperiph:");
36963393f8daSKenneth D. Merry 
36973393f8daSKenneth D. Merry 		if (path->bus != NULL)
36983393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
36993393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
37003393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
37013393f8daSKenneth D. Merry 		else
37023393f8daSKenneth D. Merry 			sbuf_printf(&sb, "nobus:");
37033393f8daSKenneth D. Merry 
37043393f8daSKenneth D. Merry 		if (path->target != NULL)
37053393f8daSKenneth D. Merry 			sbuf_printf(&sb, "%d:", path->target->target_id);
37063393f8daSKenneth D. Merry 		else
37073393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X:");
37083393f8daSKenneth D. Merry 
37093393f8daSKenneth D. Merry 		if (path->device != NULL)
3710*abe83505SNathan Whitehorn 			sbuf_printf(&sb, "%jx): ",
3711*abe83505SNathan Whitehorn 			    (uintmax_t)path->device->lun_id);
37123393f8daSKenneth D. Merry 		else
37133393f8daSKenneth D. Merry 			sbuf_printf(&sb, "X): ");
37143393f8daSKenneth D. Merry 	}
37153393f8daSKenneth D. Merry 	sbuf_finish(&sb);
37163393f8daSKenneth D. Merry 
37173393f8daSKenneth D. Merry 	return(sbuf_len(&sb));
37183393f8daSKenneth D. Merry }
37193393f8daSKenneth D. Merry 
37208b8a9b1dSJustin T. Gibbs path_id_t
37218b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
37228b8a9b1dSJustin T. Gibbs {
37238b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
37248b8a9b1dSJustin T. Gibbs }
37258b8a9b1dSJustin T. Gibbs 
37268b8a9b1dSJustin T. Gibbs target_id_t
37278b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
37288b8a9b1dSJustin T. Gibbs {
37298b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
37308b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
37318b8a9b1dSJustin T. Gibbs 	else
37328b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
37338b8a9b1dSJustin T. Gibbs }
37348b8a9b1dSJustin T. Gibbs 
37358b8a9b1dSJustin T. Gibbs lun_id_t
37368b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
37378b8a9b1dSJustin T. Gibbs {
37388b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
37398b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
37408b8a9b1dSJustin T. Gibbs 	else
37418b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
37428b8a9b1dSJustin T. Gibbs }
37438b8a9b1dSJustin T. Gibbs 
37448b8a9b1dSJustin T. Gibbs struct cam_sim *
37458b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
37468b8a9b1dSJustin T. Gibbs {
374768153f43SScott Long 
37488b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
37498b8a9b1dSJustin T. Gibbs }
37508b8a9b1dSJustin T. Gibbs 
37518b8a9b1dSJustin T. Gibbs struct cam_periph*
37528b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
37538b8a9b1dSJustin T. Gibbs {
375468153f43SScott Long 
37558b8a9b1dSJustin T. Gibbs 	return (path->periph);
37568b8a9b1dSJustin T. Gibbs }
37578b8a9b1dSJustin T. Gibbs 
37580d307e09SAlexander Motin int
37590d307e09SAlexander Motin xpt_path_legacy_ata_id(struct cam_path *path)
37600d307e09SAlexander Motin {
37610d307e09SAlexander Motin 	struct cam_eb *bus;
37620d307e09SAlexander Motin 	int bus_id;
37630d307e09SAlexander Motin 
37640d307e09SAlexander Motin 	if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
37650d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
37660d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
37670d307e09SAlexander Motin 	    strcmp(path->bus->sim->sim_name, "siisch") != 0)
37680d307e09SAlexander Motin 		return (-1);
37690d307e09SAlexander Motin 
37700d307e09SAlexander Motin 	if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
37710d307e09SAlexander Motin 	    path->bus->sim->unit_number < 2) {
37720d307e09SAlexander Motin 		bus_id = path->bus->sim->unit_number;
37730d307e09SAlexander Motin 	} else {
37740d307e09SAlexander Motin 		bus_id = 2;
37750d307e09SAlexander Motin 		xpt_lock_buses();
37760d307e09SAlexander Motin 		TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
37770d307e09SAlexander Motin 			if (bus == path->bus)
37780d307e09SAlexander Motin 				break;
37790d307e09SAlexander Motin 			if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
37800d307e09SAlexander Motin 			     bus->sim->unit_number >= 2) ||
37810d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "ahcich") == 0 ||
37820d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "mvsch") == 0 ||
37830d307e09SAlexander Motin 			    strcmp(bus->sim->sim_name, "siisch") == 0)
37840d307e09SAlexander Motin 				bus_id++;
37850d307e09SAlexander Motin 		}
37860d307e09SAlexander Motin 		xpt_unlock_buses();
37870d307e09SAlexander Motin 	}
3788ce6cf987SAlexander Motin 	if (path->target != NULL) {
3789ce6cf987SAlexander Motin 		if (path->target->target_id < 2)
37900d307e09SAlexander Motin 			return (bus_id * 2 + path->target->target_id);
37910d307e09SAlexander Motin 		else
3792ce6cf987SAlexander Motin 			return (-1);
3793ce6cf987SAlexander Motin 	} else
37940d307e09SAlexander Motin 		return (bus_id * 2);
37950d307e09SAlexander Motin }
37960d307e09SAlexander Motin 
37978b8a9b1dSJustin T. Gibbs /*
37988b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
37998b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
38008b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
38018b8a9b1dSJustin T. Gibbs  * call them now.
38028b8a9b1dSJustin T. Gibbs  */
38038b8a9b1dSJustin T. Gibbs void
38048b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
38058b8a9b1dSJustin T. Gibbs {
38068b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
3807227d67aaSAlexander Motin 	struct	 cam_periph *periph;
380868153f43SScott Long 
3809aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3810227d67aaSAlexander Motin 	xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3811227d67aaSAlexander Motin 	device = free_ccb->ccb_h.path->device;
3812227d67aaSAlexander Motin 	periph = free_ccb->ccb_h.path->periph;
38132b83592fSScott Long 
38148b8a9b1dSJustin T. Gibbs 	xpt_free_ccb(free_ccb);
3815227d67aaSAlexander Motin 	periph->periph_allocated--;
3816227d67aaSAlexander Motin 	cam_ccbq_release_opening(&device->ccbq);
3817227d67aaSAlexander Motin 	xpt_run_allocq(periph, 0);
38188b8a9b1dSJustin T. Gibbs }
38198b8a9b1dSJustin T. Gibbs 
38208b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
38218b8a9b1dSJustin T. Gibbs 
382252c9ce25SScott Long static struct xpt_xport xport_default = {
382352c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
382452c9ce25SScott Long 	.action = xpt_action_default,
382552c9ce25SScott Long 	.async = xpt_dev_async_default,
382652c9ce25SScott Long };
382752c9ce25SScott Long 
38288b8a9b1dSJustin T. Gibbs /*
38298b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
38308b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
38318b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
38328b8a9b1dSJustin T. Gibbs  * for this new bus and places it in the array of busses and assigns
38338b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
38348b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
383502caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
38368b8a9b1dSJustin T. Gibbs  */
38378b8a9b1dSJustin T. Gibbs int32_t
3838b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
38398b8a9b1dSJustin T. Gibbs {
38408b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
3841434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
38428b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
384383c5d981SAlexander Motin 	struct cam_path *path;
384452c9ce25SScott Long 	cam_status status;
38458b8a9b1dSJustin T. Gibbs 
38462b83592fSScott Long 	mtx_assert(sim->mtx, MA_OWNED);
384768153f43SScott Long 
38488b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
38498b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3850227d67aaSAlexander Motin 					  M_CAMXPT, M_NOWAIT|M_ZERO);
38518b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
38528b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
38538b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
38548b8a9b1dSJustin T. Gibbs 	}
38558b8a9b1dSJustin T. Gibbs 
3856227d67aaSAlexander Motin 	mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3857434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
3858fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
38598b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
386087cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
3861434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
3862a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3863434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
386452c9ce25SScott Long 
38659a7c2696SAlexander Motin 	xpt_lock_buses();
38666dfc67e3SAlexander Motin 	sim->path_id = new_bus->path_id =
38676dfc67e3SAlexander Motin 	    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
38682b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3869434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
3870434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
3871434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
3872434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
3873434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3874434bbf6eSJustin T. Gibbs 	else
38752b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
38762b83592fSScott Long 	xsoftc.bus_generation++;
38779a7c2696SAlexander Motin 	xpt_unlock_buses();
38788b8a9b1dSJustin T. Gibbs 
387952c9ce25SScott Long 	/*
388052c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
388152c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
388252c9ce25SScott Long 	 * a more appropriate transport.
388352c9ce25SScott Long 	 */
388452c9ce25SScott Long 	new_bus->xport = &xport_default;
38858b8a9b1dSJustin T. Gibbs 
388632aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
38878b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3888627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
3889627995dcSAlexander Motin 		xpt_release_bus(new_bus);
3890627995dcSAlexander Motin 		free(path, M_CAMXPT);
3891627995dcSAlexander Motin 		return (CAM_RESRC_UNAVAIL);
3892627995dcSAlexander Motin 	}
389352c9ce25SScott Long 
389483c5d981SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
38958b8a9b1dSJustin T. Gibbs 	cpi.ccb_h.func_code = XPT_PATH_INQ;
38968b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cpi);
389752c9ce25SScott Long 
389852c9ce25SScott Long 	if (cpi.ccb_h.status == CAM_REQ_CMP) {
389952c9ce25SScott Long 		switch (cpi.transport) {
390052c9ce25SScott Long 		case XPORT_SPI:
390152c9ce25SScott Long 		case XPORT_SAS:
390252c9ce25SScott Long 		case XPORT_FC:
390352c9ce25SScott Long 		case XPORT_USB:
390433ea30feSAlexander Motin 		case XPORT_ISCSI:
3905b5595753SNathan Whitehorn 		case XPORT_SRP:
390633ea30feSAlexander Motin 		case XPORT_PPB:
390752c9ce25SScott Long 			new_bus->xport = scsi_get_xport();
390852c9ce25SScott Long 			break;
390952c9ce25SScott Long 		case XPORT_ATA:
391052c9ce25SScott Long 		case XPORT_SATA:
391152c9ce25SScott Long 			new_bus->xport = ata_get_xport();
391252c9ce25SScott Long 			break;
391352c9ce25SScott Long 		default:
391452c9ce25SScott Long 			new_bus->xport = &xport_default;
391552c9ce25SScott Long 			break;
39168b8a9b1dSJustin T. Gibbs 		}
391752c9ce25SScott Long 	}
391852c9ce25SScott Long 
391952c9ce25SScott Long 	/* Notify interested parties */
392052c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
392183c5d981SAlexander Motin 
392283c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
3923b01773b0SKenneth D. Merry 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3924b01773b0SKenneth D. Merry 			union	ccb *scan_ccb;
3925b01773b0SKenneth D. Merry 
392683c5d981SAlexander Motin 			/* Initiate bus rescan. */
392783c5d981SAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
3928e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
392983c5d981SAlexander Motin 				scan_ccb->ccb_h.path = path;
393083c5d981SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
393183c5d981SAlexander Motin 				scan_ccb->crcn.flags = 0;
393283c5d981SAlexander Motin 				xpt_rescan(scan_ccb);
393383c5d981SAlexander Motin 			} else
3934b01773b0SKenneth D. Merry 				xpt_print(path,
3935b01773b0SKenneth D. Merry 					  "Can't allocate CCB to scan bus\n");
3936b01773b0SKenneth D. Merry 		} else
3937b01773b0SKenneth D. Merry 			xpt_free_path(path);
3938e5736ac8SAlexander Motin 	} else
393983c5d981SAlexander Motin 		xpt_free_path(path);
39408b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
39418b8a9b1dSJustin T. Gibbs }
39428b8a9b1dSJustin T. Gibbs 
3943434bbf6eSJustin T. Gibbs int32_t
3944434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
39458b8a9b1dSJustin T. Gibbs {
3946434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
3947434bbf6eSJustin T. Gibbs 	cam_status status;
3948434bbf6eSJustin T. Gibbs 
3949434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
3950434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3951434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
3952434bbf6eSJustin T. Gibbs 		return (status);
3953434bbf6eSJustin T. Gibbs 
3954434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3955434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3956434bbf6eSJustin T. Gibbs 
3957434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
3958434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
3959434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
3960434bbf6eSJustin T. Gibbs 
3961434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
3962434bbf6eSJustin T. Gibbs }
3963434bbf6eSJustin T. Gibbs 
3964434bbf6eSJustin T. Gibbs static path_id_t
3965434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
3966434bbf6eSJustin T. Gibbs {
3967434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
3968434bbf6eSJustin T. Gibbs 	path_id_t pathid;
39692398f0cdSPeter Wemm 	const char *strval;
39708b8a9b1dSJustin T. Gibbs 
39716dfc67e3SAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
3972434bbf6eSJustin T. Gibbs 	pathid = 0;
39732b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3974434bbf6eSJustin T. Gibbs retry:
3975434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
39769e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
3977434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
3978434bbf6eSJustin T. Gibbs 			pathid++;
3979434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
3980434bbf6eSJustin T. Gibbs 	}
3981434bbf6eSJustin T. Gibbs 
3982434bbf6eSJustin T. Gibbs 	/*
3983434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
3984434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
3985434bbf6eSJustin T. Gibbs 	 */
398675f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3987434bbf6eSJustin T. Gibbs 		++pathid;
39888b8a9b1dSJustin T. Gibbs 		/* Start the search over */
3989434bbf6eSJustin T. Gibbs 		goto retry;
39908b8a9b1dSJustin T. Gibbs 	}
3991434bbf6eSJustin T. Gibbs 	return (pathid);
39928b8a9b1dSJustin T. Gibbs }
39938b8a9b1dSJustin T. Gibbs 
3994434bbf6eSJustin T. Gibbs static path_id_t
3995434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
39968b8a9b1dSJustin T. Gibbs {
39978b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
399875f51904SPeter Wemm 	int i, dunit, val;
3999642f0c46SPeter Wemm 	char buf[32];
40002398f0cdSPeter Wemm 	const char *dname;
40018b8a9b1dSJustin T. Gibbs 
40028b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
400375f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
40046dfc67e3SAlexander Motin 	if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
40056dfc67e3SAlexander Motin 		return (pathid);
40062398f0cdSPeter Wemm 	i = 0;
40072398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
40082398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
4009642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
4010642f0c46SPeter Wemm 			continue;
4011642f0c46SPeter Wemm 		}
401275f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
40138b8a9b1dSJustin T. Gibbs 			continue;
401475f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
401575f51904SPeter Wemm 			if (sim_bus == val) {
401675f51904SPeter Wemm 				pathid = dunit;
40178b8a9b1dSJustin T. Gibbs 				break;
40188b8a9b1dSJustin T. Gibbs 			}
40198b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
40208b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
402175f51904SPeter Wemm 			pathid = dunit;
40228b8a9b1dSJustin T. Gibbs 			break;
40238b8a9b1dSJustin T. Gibbs 		} else {
40248b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
40258b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
40268b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
40278b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
40288b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
402975f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
40308b8a9b1dSJustin T. Gibbs 			break;
40318b8a9b1dSJustin T. Gibbs 		}
40328b8a9b1dSJustin T. Gibbs 	}
40338b8a9b1dSJustin T. Gibbs 
4034434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4035434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
40368b8a9b1dSJustin T. Gibbs 	return (pathid);
40378b8a9b1dSJustin T. Gibbs }
40388b8a9b1dSJustin T. Gibbs 
403922c7d606SAlexander Motin static const char *
404022c7d606SAlexander Motin xpt_async_string(u_int32_t async_code)
404122c7d606SAlexander Motin {
404222c7d606SAlexander Motin 
404322c7d606SAlexander Motin 	switch (async_code) {
404422c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
404522c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
404622c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
404722c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
404822c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
404922c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
405022c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
405122c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
405222c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
405322c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
405422c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
405522c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
405622c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
40573631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
405822c7d606SAlexander Motin 	}
405922c7d606SAlexander Motin 	return ("AC_UNKNOWN");
406022c7d606SAlexander Motin }
406122c7d606SAlexander Motin 
4062227d67aaSAlexander Motin static int
4063227d67aaSAlexander Motin xpt_async_size(u_int32_t async_code)
40648b8a9b1dSJustin T. Gibbs {
40658b8a9b1dSJustin T. Gibbs 
4066227d67aaSAlexander Motin 	switch (async_code) {
4067227d67aaSAlexander Motin 	case AC_BUS_RESET: return (0);
4068227d67aaSAlexander Motin 	case AC_UNSOL_RESEL: return (0);
4069227d67aaSAlexander Motin 	case AC_SCSI_AEN: return (0);
4070227d67aaSAlexander Motin 	case AC_SENT_BDR: return (0);
4071227d67aaSAlexander Motin 	case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4072227d67aaSAlexander Motin 	case AC_PATH_DEREGISTERED: return (0);
4073227d67aaSAlexander Motin 	case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4074227d67aaSAlexander Motin 	case AC_LOST_DEVICE: return (0);
4075227d67aaSAlexander Motin 	case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4076227d67aaSAlexander Motin 	case AC_INQ_CHANGED: return (0);
4077227d67aaSAlexander Motin 	case AC_GETDEV_CHANGED: return (0);
4078227d67aaSAlexander Motin 	case AC_CONTRACT: return (sizeof(struct ac_contract));
4079227d67aaSAlexander Motin 	case AC_ADVINFO_CHANGED: return (-1);
4080227d67aaSAlexander Motin 	case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4081227d67aaSAlexander Motin 	}
4082227d67aaSAlexander Motin 	return (0);
4083227d67aaSAlexander Motin }
4084227d67aaSAlexander Motin 
4085227d67aaSAlexander Motin static int
4086227d67aaSAlexander Motin xpt_async_process_dev(struct cam_ed *device, void *arg)
4087227d67aaSAlexander Motin {
4088227d67aaSAlexander Motin 	union ccb *ccb = arg;
4089227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4090227d67aaSAlexander Motin 	void *async_arg = ccb->casync.async_arg_ptr;
4091227d67aaSAlexander Motin 	u_int32_t async_code = ccb->casync.async_code;
4092227d67aaSAlexander Motin 	int relock;
4093227d67aaSAlexander Motin 
4094227d67aaSAlexander Motin 	if (path->device != device
4095227d67aaSAlexander Motin 	 && path->device->lun_id != CAM_LUN_WILDCARD
4096227d67aaSAlexander Motin 	 && device->lun_id != CAM_LUN_WILDCARD)
4097227d67aaSAlexander Motin 		return (1);
40988b8a9b1dSJustin T. Gibbs 
4099a5479bc5SJustin T. Gibbs 	/*
4100227d67aaSAlexander Motin 	 * The async callback could free the device.
4101227d67aaSAlexander Motin 	 * If it is a broadcast async, it doesn't hold
4102227d67aaSAlexander Motin 	 * device reference, so take our own reference.
4103a5479bc5SJustin T. Gibbs 	 */
4104227d67aaSAlexander Motin 	xpt_acquire_device(device);
41058b8a9b1dSJustin T. Gibbs 
4106227d67aaSAlexander Motin 	/*
4107227d67aaSAlexander Motin 	 * If async for specific device is to be delivered to
4108227d67aaSAlexander Motin 	 * the wildcard client, take the specific device lock.
4109227d67aaSAlexander Motin 	 * XXX: We may need a way for client to specify it.
4110227d67aaSAlexander Motin 	 */
4111227d67aaSAlexander Motin 	if ((device->lun_id == CAM_LUN_WILDCARD &&
4112227d67aaSAlexander Motin 	     path->device->lun_id != CAM_LUN_WILDCARD) ||
4113227d67aaSAlexander Motin 	    (device->target->target_id == CAM_TARGET_WILDCARD &&
4114227d67aaSAlexander Motin 	     path->target->target_id != CAM_TARGET_WILDCARD) ||
4115227d67aaSAlexander Motin 	    (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4116227d67aaSAlexander Motin 	     path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4117227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
4118227d67aaSAlexander Motin 		xpt_path_lock(path);
4119227d67aaSAlexander Motin 		relock = 1;
4120227d67aaSAlexander Motin 	} else
4121227d67aaSAlexander Motin 		relock = 0;
4122227d67aaSAlexander Motin 
4123227d67aaSAlexander Motin 	(*(device->target->bus->xport->async))(async_code,
4124227d67aaSAlexander Motin 	    device->target->bus, device->target, device, async_arg);
4125227d67aaSAlexander Motin 	xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4126227d67aaSAlexander Motin 
4127227d67aaSAlexander Motin 	if (relock) {
4128227d67aaSAlexander Motin 		xpt_path_unlock(path);
4129227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
4130227d67aaSAlexander Motin 	}
4131227d67aaSAlexander Motin 	xpt_release_device(device);
4132227d67aaSAlexander Motin 	return (1);
4133227d67aaSAlexander Motin }
4134227d67aaSAlexander Motin 
4135227d67aaSAlexander Motin static int
4136227d67aaSAlexander Motin xpt_async_process_tgt(struct cam_et *target, void *arg)
4137227d67aaSAlexander Motin {
4138227d67aaSAlexander Motin 	union ccb *ccb = arg;
4139227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4140227d67aaSAlexander Motin 
4141227d67aaSAlexander Motin 	if (path->target != target
4142227d67aaSAlexander Motin 	 && path->target->target_id != CAM_TARGET_WILDCARD
4143227d67aaSAlexander Motin 	 && target->target_id != CAM_TARGET_WILDCARD)
4144227d67aaSAlexander Motin 		return (1);
4145227d67aaSAlexander Motin 
4146227d67aaSAlexander Motin 	if (ccb->casync.async_code == AC_SENT_BDR) {
4147227d67aaSAlexander Motin 		/* Update our notion of when the last reset occurred */
4148227d67aaSAlexander Motin 		microtime(&target->last_reset);
4149227d67aaSAlexander Motin 	}
4150227d67aaSAlexander Motin 
4151227d67aaSAlexander Motin 	return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4152227d67aaSAlexander Motin }
4153227d67aaSAlexander Motin 
4154227d67aaSAlexander Motin static void
4155227d67aaSAlexander Motin xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4156227d67aaSAlexander Motin {
4157227d67aaSAlexander Motin 	struct cam_eb *bus;
4158227d67aaSAlexander Motin 	struct cam_path *path;
4159227d67aaSAlexander Motin 	void *async_arg;
4160227d67aaSAlexander Motin 	u_int32_t async_code;
4161227d67aaSAlexander Motin 
4162227d67aaSAlexander Motin 	path = ccb->ccb_h.path;
4163227d67aaSAlexander Motin 	async_code = ccb->casync.async_code;
4164227d67aaSAlexander Motin 	async_arg = ccb->casync.async_arg_ptr;
4165227d67aaSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4166227d67aaSAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
41678b8a9b1dSJustin T. Gibbs 	bus = path->bus;
41688b8a9b1dSJustin T. Gibbs 
41698b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
417087cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
417187cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
41728b8a9b1dSJustin T. Gibbs 	}
41738b8a9b1dSJustin T. Gibbs 
4174227d67aaSAlexander Motin 	xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4175c8bead2aSJustin T. Gibbs 
4176c8bead2aSJustin T. Gibbs 	/*
4177c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4178c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4179c8bead2aSJustin T. Gibbs 	 */
4180227d67aaSAlexander Motin 	if (bus != xpt_periph->path->bus) {
4181227d67aaSAlexander Motin 		xpt_path_lock(xpt_periph->path);
4182227d67aaSAlexander Motin 		xpt_async_process_dev(xpt_periph->path->device, ccb);
4183227d67aaSAlexander Motin 		xpt_path_unlock(xpt_periph->path);
4184227d67aaSAlexander Motin 	}
4185227d67aaSAlexander Motin 
4186227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4187227d67aaSAlexander Motin 		xpt_release_devq(path, 1, TRUE);
4188227d67aaSAlexander Motin 	else
4189227d67aaSAlexander Motin 		xpt_release_simq(path->bus->sim, TRUE);
4190227d67aaSAlexander Motin 	if (ccb->casync.async_arg_size > 0)
4191227d67aaSAlexander Motin 		free(async_arg, M_CAMXPT);
4192227d67aaSAlexander Motin 	xpt_free_path(path);
4193227d67aaSAlexander Motin 	xpt_free_ccb(ccb);
41948b8a9b1dSJustin T. Gibbs }
41958b8a9b1dSJustin T. Gibbs 
41968b8a9b1dSJustin T. Gibbs static void
41978b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
41988b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
41998b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
42008b8a9b1dSJustin T. Gibbs {
42018b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
4202227d67aaSAlexander Motin 	int lock;
42038b8a9b1dSJustin T. Gibbs 
42048b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
42058b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
42068b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
42078b8a9b1dSJustin T. Gibbs 		/*
42088b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
42098b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
42108b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
42118b8a9b1dSJustin T. Gibbs 		 */
42128b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
4213227d67aaSAlexander Motin 		if ((cur_entry->event_enable & async_code) != 0) {
4214227d67aaSAlexander Motin 			lock = cur_entry->event_lock;
4215227d67aaSAlexander Motin 			if (lock)
4216227d67aaSAlexander Motin 				CAM_SIM_LOCK(path->device->sim);
42178b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
42188b8a9b1dSJustin T. Gibbs 					    async_code, path,
42198b8a9b1dSJustin T. Gibbs 					    async_arg);
4220227d67aaSAlexander Motin 			if (lock)
4221227d67aaSAlexander Motin 				CAM_SIM_UNLOCK(path->device->sim);
4222227d67aaSAlexander Motin 		}
42238b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
42248b8a9b1dSJustin T. Gibbs 	}
42258b8a9b1dSJustin T. Gibbs }
42268b8a9b1dSJustin T. Gibbs 
4227227d67aaSAlexander Motin void
4228227d67aaSAlexander Motin xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4229227d67aaSAlexander Motin {
4230227d67aaSAlexander Motin 	union ccb *ccb;
4231227d67aaSAlexander Motin 	int size;
4232227d67aaSAlexander Motin 
4233227d67aaSAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
4234227d67aaSAlexander Motin 	if (ccb == NULL) {
4235227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate CCB to send %s\n",
4236227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4237227d67aaSAlexander Motin 		return;
4238227d67aaSAlexander Motin 	}
4239227d67aaSAlexander Motin 
4240227d67aaSAlexander Motin 	if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4241227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate path to send %s\n",
4242227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4243227d67aaSAlexander Motin 		xpt_free_ccb(ccb);
4244227d67aaSAlexander Motin 		return;
4245227d67aaSAlexander Motin 	}
4246227d67aaSAlexander Motin 	ccb->ccb_h.path->periph = NULL;
4247227d67aaSAlexander Motin 	ccb->ccb_h.func_code = XPT_ASYNC;
4248227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_async_process;
4249227d67aaSAlexander Motin 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4250227d67aaSAlexander Motin 	ccb->casync.async_code = async_code;
4251227d67aaSAlexander Motin 	ccb->casync.async_arg_size = 0;
4252227d67aaSAlexander Motin 	size = xpt_async_size(async_code);
4253227d67aaSAlexander Motin 	if (size > 0 && async_arg != NULL) {
4254227d67aaSAlexander Motin 		ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4255227d67aaSAlexander Motin 		if (ccb->casync.async_arg_ptr == NULL) {
4256227d67aaSAlexander Motin 			xpt_print(path, "Can't allocate argument to send %s\n",
4257227d67aaSAlexander Motin 			    xpt_async_string(async_code));
4258227d67aaSAlexander Motin 			xpt_free_path(ccb->ccb_h.path);
4259227d67aaSAlexander Motin 			xpt_free_ccb(ccb);
4260227d67aaSAlexander Motin 			return;
4261227d67aaSAlexander Motin 		}
4262227d67aaSAlexander Motin 		memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4263227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4264227d67aaSAlexander Motin 	} else if (size < 0)
4265227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4266227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4267227d67aaSAlexander Motin 		xpt_freeze_devq(path, 1);
4268227d67aaSAlexander Motin 	else
4269227d67aaSAlexander Motin 		xpt_freeze_simq(path->bus->sim, 1);
4270227d67aaSAlexander Motin 	xpt_done(ccb);
4271227d67aaSAlexander Motin }
4272227d67aaSAlexander Motin 
42732f22d08dSJustin T. Gibbs static void
427452c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
427552c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
427652c9ce25SScott Long 		      void *async_arg)
42772f22d08dSJustin T. Gibbs {
4278227d67aaSAlexander Motin 
4279227d67aaSAlexander Motin 	/*
4280227d67aaSAlexander Motin 	 * We only need to handle events for real devices.
4281227d67aaSAlexander Motin 	 */
4282227d67aaSAlexander Motin 	if (target->target_id == CAM_TARGET_WILDCARD
4283227d67aaSAlexander Motin 	 || device->lun_id == CAM_LUN_WILDCARD)
4284227d67aaSAlexander Motin 		return;
4285227d67aaSAlexander Motin 
4286b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
42872f22d08dSJustin T. Gibbs }
42882f22d08dSJustin T. Gibbs 
42898b8a9b1dSJustin T. Gibbs u_int32_t
4290cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
429183c5d981SAlexander Motin {
429283c5d981SAlexander Motin 	struct cam_ed	*dev = path->device;
4293227d67aaSAlexander Motin 	struct cam_devq	*devq;
4294227d67aaSAlexander Motin 	uint32_t	 freeze;
429583c5d981SAlexander Motin 
4296227d67aaSAlexander Motin 	devq = dev->sim->devq;
4297227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
42980d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq() %u->%u\n",
42990d4f3c31SAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4300227d67aaSAlexander Motin 	freeze = (dev->ccbq.queue.qfrozen_cnt += count);
430183c5d981SAlexander Motin 	/* Remove frozen device from sendq. */
4302227d67aaSAlexander Motin 	if (device_is_queued(dev))
4303227d67aaSAlexander Motin 		camq_remove(&devq->send_queue, dev->devq_entry.index);
4304227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4305227d67aaSAlexander Motin 	return (freeze);
43068b8a9b1dSJustin T. Gibbs }
43078b8a9b1dSJustin T. Gibbs 
43088b8a9b1dSJustin T. Gibbs u_int32_t
43098b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
43108b8a9b1dSJustin T. Gibbs {
4311227d67aaSAlexander Motin 	struct cam_devq	*devq;
4312227d67aaSAlexander Motin 	uint32_t	 freeze;
4313ec700f26SAlexander Motin 
4314227d67aaSAlexander Motin 	devq = sim->devq;
4315227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4316227d67aaSAlexander Motin 	freeze = (devq->send_queue.qfrozen_cnt += count);
4317227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4318227d67aaSAlexander Motin 	return (freeze);
43198b8a9b1dSJustin T. Gibbs }
43208b8a9b1dSJustin T. Gibbs 
43218b8a9b1dSJustin T. Gibbs static void
43228b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
43238b8a9b1dSJustin T. Gibbs {
4324227d67aaSAlexander Motin 	struct cam_ed *dev;
4325227d67aaSAlexander Motin 	struct cam_devq *devq;
43268b8a9b1dSJustin T. Gibbs 
4327227d67aaSAlexander Motin 	dev = (struct cam_ed *)arg;
4328227d67aaSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4329227d67aaSAlexander Motin 	devq = dev->sim->devq;
4330227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4331227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4332227d67aaSAlexander Motin 		xpt_run_devq(devq);
43338b8a9b1dSJustin T. Gibbs }
43348b8a9b1dSJustin T. Gibbs 
43358b8a9b1dSJustin T. Gibbs void
43362cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
43372cefde5fSJustin T. Gibbs {
4338227d67aaSAlexander Motin 	struct cam_ed *dev;
4339227d67aaSAlexander Motin 	struct cam_devq *devq;
434068153f43SScott Long 
43410d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
43420d4f3c31SAlexander Motin 	    count, run_queue));
4343227d67aaSAlexander Motin 	dev = path->device;
4344227d67aaSAlexander Motin 	devq = dev->sim->devq;
4345227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4346227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, count, run_queue))
4347227d67aaSAlexander Motin 		xpt_run_devq(dev->sim->devq);
4348227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
434983c5d981SAlexander Motin }
435083c5d981SAlexander Motin 
4351227d67aaSAlexander Motin static int
4352cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
43538b8a9b1dSJustin T. Gibbs {
43548b8a9b1dSJustin T. Gibbs 
4355227d67aaSAlexander Motin 	mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
43560d4f3c31SAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
43570d4f3c31SAlexander Motin 	    ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
43580d4f3c31SAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4359cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
436083c5d981SAlexander Motin #ifdef INVARIANTS
4361cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4362cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
436383c5d981SAlexander Motin #endif
4364cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
436583c5d981SAlexander Motin 	}
4366cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4367cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
43688b8a9b1dSJustin T. Gibbs 		/*
43698b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
43708b8a9b1dSJustin T. Gibbs 		 * command completion.
43718b8a9b1dSJustin T. Gibbs 		 */
43728b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
43738b8a9b1dSJustin T. Gibbs 		/*
43748b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
43758b8a9b1dSJustin T. Gibbs 		 * to release this queue.
43768b8a9b1dSJustin T. Gibbs 		 */
43778b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
43782b83592fSScott Long 			callout_stop(&dev->callout);
43798b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
43808b8a9b1dSJustin T. Gibbs 		}
43818b8a9b1dSJustin T. Gibbs 		/*
43828b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
43838b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
43848b8a9b1dSJustin T. Gibbs 		 * run.
43858b8a9b1dSJustin T. Gibbs 		 */
4386227d67aaSAlexander Motin 		xpt_schedule_devq(dev->sim->devq, dev);
4387227d67aaSAlexander Motin 	} else
4388227d67aaSAlexander Motin 		run_queue = 0;
4389227d67aaSAlexander Motin 	return (run_queue);
439083c5d981SAlexander Motin }
43918b8a9b1dSJustin T. Gibbs 
43928b8a9b1dSJustin T. Gibbs void
43938b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
43948b8a9b1dSJustin T. Gibbs {
4395227d67aaSAlexander Motin 	struct cam_devq	*devq;
43968b8a9b1dSJustin T. Gibbs 
4397227d67aaSAlexander Motin 	devq = sim->devq;
4398227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4399227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt <= 0) {
440083c5d981SAlexander Motin #ifdef INVARIANTS
440183c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4402227d67aaSAlexander Motin 		    devq->send_queue.qfrozen_cnt);
440383c5d981SAlexander Motin #endif
440483c5d981SAlexander Motin 	} else
4405227d67aaSAlexander Motin 		devq->send_queue.qfrozen_cnt--;
4406227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt == 0) {
44078b8a9b1dSJustin T. Gibbs 		/*
44088b8a9b1dSJustin T. Gibbs 		 * If there is a timeout scheduled to release this
44098b8a9b1dSJustin T. Gibbs 		 * sim queue, remove it.  The queue frozen count is
44108b8a9b1dSJustin T. Gibbs 		 * already at 0.
44118b8a9b1dSJustin T. Gibbs 		 */
44128b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
44132b83592fSScott Long 			callout_stop(&sim->callout);
44148b8a9b1dSJustin T. Gibbs 			sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
44158b8a9b1dSJustin T. Gibbs 		}
44168b8a9b1dSJustin T. Gibbs 		if (run_queue) {
44178b8a9b1dSJustin T. Gibbs 			/*
44188b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
44198b8a9b1dSJustin T. Gibbs 			 */
4420cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
442177dc25ccSScott Long 		}
442277dc25ccSScott Long 	}
4423227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
44248b8a9b1dSJustin T. Gibbs }
44258b8a9b1dSJustin T. Gibbs 
44262b83592fSScott Long /*
44272b83592fSScott Long  * XXX Appears to be unused.
44282b83592fSScott Long  */
44298b8a9b1dSJustin T. Gibbs static void
44308b8a9b1dSJustin T. Gibbs xpt_release_simq_timeout(void *arg)
44318b8a9b1dSJustin T. Gibbs {
44328b8a9b1dSJustin T. Gibbs 	struct cam_sim *sim;
44338b8a9b1dSJustin T. Gibbs 
44348b8a9b1dSJustin T. Gibbs 	sim = (struct cam_sim *)arg;
44358b8a9b1dSJustin T. Gibbs 	xpt_release_simq(sim, /* run_queue */ TRUE);
44368b8a9b1dSJustin T. Gibbs }
44378b8a9b1dSJustin T. Gibbs 
44388b8a9b1dSJustin T. Gibbs void
4439a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
44408b8a9b1dSJustin T. Gibbs {
4441227d67aaSAlexander Motin 	struct cam_doneq *queue;
4442227d67aaSAlexander Motin 	int	run, hash;
44438b8a9b1dSJustin T. Gibbs 
44448b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4445227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4446227d67aaSAlexander Motin 		return;
4447227d67aaSAlexander Motin 
4448227d67aaSAlexander Motin 	hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4449227d67aaSAlexander Motin 	    done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4450227d67aaSAlexander Motin 	queue = &cam_doneqs[hash];
4451227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
4452227d67aaSAlexander Motin 	run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4453227d67aaSAlexander Motin 	STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
44548b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4455227d67aaSAlexander Motin 	mtx_unlock(&queue->cam_doneq_mtx);
4456227d67aaSAlexander Motin 	if (run)
4457227d67aaSAlexander Motin 		wakeup(&queue->cam_doneq);
44588b8a9b1dSJustin T. Gibbs }
44598b8a9b1dSJustin T. Gibbs 
4460711f6613SAlexander Motin void
4461227d67aaSAlexander Motin xpt_done_direct(union ccb *done_ccb)
4462711f6613SAlexander Motin {
4463711f6613SAlexander Motin 
4464227d67aaSAlexander Motin 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done_direct\n"));
4465227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4466227d67aaSAlexander Motin 		return;
4467711f6613SAlexander Motin 
4468227d67aaSAlexander Motin 	xpt_done_process(&done_ccb->ccb_h);
4469711f6613SAlexander Motin }
4470711f6613SAlexander Motin 
44718b8a9b1dSJustin T. Gibbs union ccb *
44728008a935SScott Long xpt_alloc_ccb()
44738b8a9b1dSJustin T. Gibbs {
44748b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
44758b8a9b1dSJustin T. Gibbs 
4476596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4477362abc44STai-hwa Liang 	return (new_ccb);
4478362abc44STai-hwa Liang }
4479362abc44STai-hwa Liang 
4480362abc44STai-hwa Liang union ccb *
44818008a935SScott Long xpt_alloc_ccb_nowait()
4482362abc44STai-hwa Liang {
4483362abc44STai-hwa Liang 	union ccb *new_ccb;
4484362abc44STai-hwa Liang 
4485596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
44868b8a9b1dSJustin T. Gibbs 	return (new_ccb);
44878b8a9b1dSJustin T. Gibbs }
44888b8a9b1dSJustin T. Gibbs 
44898b8a9b1dSJustin T. Gibbs void
44908b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
44918b8a9b1dSJustin T. Gibbs {
4492596ee08fSAlexander Motin 	free(free_ccb, M_CAMCCB);
44938b8a9b1dSJustin T. Gibbs }
44948b8a9b1dSJustin T. Gibbs 
44958b8a9b1dSJustin T. Gibbs 
44968b8a9b1dSJustin T. Gibbs 
44978b8a9b1dSJustin T. Gibbs /* Private XPT functions */
44988b8a9b1dSJustin T. Gibbs 
44998b8a9b1dSJustin T. Gibbs /*
45008b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
4501227d67aaSAlexander Motin  * referenced by the path.  If we don't have sufficient resources to allocate
4502227d67aaSAlexander Motin  * more ccbs, we return NULL.
45038b8a9b1dSJustin T. Gibbs  */
45048b8a9b1dSJustin T. Gibbs static union ccb *
4505227d67aaSAlexander Motin xpt_get_ccb_nowait(struct cam_periph *periph)
45068b8a9b1dSJustin T. Gibbs {
45078b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
45088b8a9b1dSJustin T. Gibbs 
4509227d67aaSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_NOWAIT);
4510227d67aaSAlexander Motin 	if (new_ccb == NULL)
45118b8a9b1dSJustin T. Gibbs 		return (NULL);
4512227d67aaSAlexander Motin 	periph->periph_allocated++;
4513227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
45148b8a9b1dSJustin T. Gibbs 	return (new_ccb);
45158b8a9b1dSJustin T. Gibbs }
45168b8a9b1dSJustin T. Gibbs 
4517227d67aaSAlexander Motin static union ccb *
4518227d67aaSAlexander Motin xpt_get_ccb(struct cam_periph *periph)
4519227d67aaSAlexander Motin {
4520227d67aaSAlexander Motin 	union ccb *new_ccb;
4521227d67aaSAlexander Motin 
4522227d67aaSAlexander Motin 	cam_periph_unlock(periph);
4523227d67aaSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_WAITOK);
4524227d67aaSAlexander Motin 	cam_periph_lock(periph);
4525227d67aaSAlexander Motin 	periph->periph_allocated++;
4526227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4527227d67aaSAlexander Motin 	return (new_ccb);
4528227d67aaSAlexander Motin }
4529227d67aaSAlexander Motin 
4530227d67aaSAlexander Motin union ccb *
4531227d67aaSAlexander Motin cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4532227d67aaSAlexander Motin {
4533227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
4534227d67aaSAlexander Motin 
4535227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4536227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4537227d67aaSAlexander Motin 	while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4538227d67aaSAlexander Motin 	    ccb_h->pinfo.priority != priority) {
4539227d67aaSAlexander Motin 		if (priority < periph->immediate_priority) {
4540227d67aaSAlexander Motin 			periph->immediate_priority = priority;
4541227d67aaSAlexander Motin 			xpt_run_allocq(periph, 0);
4542227d67aaSAlexander Motin 		} else
4543227d67aaSAlexander Motin 			cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4544227d67aaSAlexander Motin 			    "cgticb", 0);
4545227d67aaSAlexander Motin 	}
4546227d67aaSAlexander Motin 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4547227d67aaSAlexander Motin 	return ((union ccb *)ccb_h);
4548227d67aaSAlexander Motin }
4549227d67aaSAlexander Motin 
4550227d67aaSAlexander Motin static void
4551227d67aaSAlexander Motin xpt_acquire_bus(struct cam_eb *bus)
4552227d67aaSAlexander Motin {
4553227d67aaSAlexander Motin 
4554227d67aaSAlexander Motin 	xpt_lock_buses();
4555227d67aaSAlexander Motin 	bus->refcount++;
4556227d67aaSAlexander Motin 	xpt_unlock_buses();
4557227d67aaSAlexander Motin }
4558227d67aaSAlexander Motin 
4559a5479bc5SJustin T. Gibbs static void
4560a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4561a5479bc5SJustin T. Gibbs {
4562a5479bc5SJustin T. Gibbs 
45639a7c2696SAlexander Motin 	xpt_lock_buses();
456415975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4565dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4566dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4567dcdf6e74SAlexander Motin 		return;
4568dcdf6e74SAlexander Motin 	}
45692b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
45702b83592fSScott Long 	xsoftc.bus_generation++;
45719a7c2696SAlexander Motin 	xpt_unlock_buses();
4572227d67aaSAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4573227d67aaSAlexander Motin 	    ("destroying bus, but target list is not empty"));
4574fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4575227d67aaSAlexander Motin 	mtx_destroy(&bus->eb_mtx);
4576362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4577a5479bc5SJustin T. Gibbs }
45788b8a9b1dSJustin T. Gibbs 
45798b8a9b1dSJustin T. Gibbs static struct cam_et *
45808b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
45818b8a9b1dSJustin T. Gibbs {
4582dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
45838b8a9b1dSJustin T. Gibbs 
4584227d67aaSAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4585227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
45863501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
45873501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4588dcdf6e74SAlexander Motin 	if (target == NULL)
4589dcdf6e74SAlexander Motin 		return (NULL);
45908b8a9b1dSJustin T. Gibbs 
4591434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
45928b8a9b1dSJustin T. Gibbs 	target->bus = bus;
45938b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
45948b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4595434bbf6eSJustin T. Gibbs 	target->generation = 0;
459682b361b1SMatt Jacob 	target->luns = NULL;
4597227d67aaSAlexander Motin 	mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4598434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4599a5479bc5SJustin T. Gibbs 	/*
4600a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4601a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4602a5479bc5SJustin T. Gibbs 	 */
4603a5479bc5SJustin T. Gibbs 	bus->refcount++;
46048b8a9b1dSJustin T. Gibbs 
46058b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
46068b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
46078b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
46088b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
46098b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
46108b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
46118b8a9b1dSJustin T. Gibbs 	} else {
46128b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
46138b8a9b1dSJustin T. Gibbs 	}
4614a5479bc5SJustin T. Gibbs 	bus->generation++;
46158b8a9b1dSJustin T. Gibbs 	return (target);
46168b8a9b1dSJustin T. Gibbs }
46178b8a9b1dSJustin T. Gibbs 
4618a5479bc5SJustin T. Gibbs static void
4619227d67aaSAlexander Motin xpt_acquire_target(struct cam_et *target)
4620227d67aaSAlexander Motin {
4621227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4622227d67aaSAlexander Motin 
4623227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4624227d67aaSAlexander Motin 	target->refcount++;
4625227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4626227d67aaSAlexander Motin }
4627227d67aaSAlexander Motin 
4628227d67aaSAlexander Motin static void
4629f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
46308b8a9b1dSJustin T. Gibbs {
4631227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4632a5479bc5SJustin T. Gibbs 
4633227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4634227d67aaSAlexander Motin 	if (--target->refcount > 0) {
4635227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4636dcdf6e74SAlexander Motin 		return;
4637227d67aaSAlexander Motin 	}
4638227d67aaSAlexander Motin 	TAILQ_REMOVE(&bus->et_entries, target, links);
4639227d67aaSAlexander Motin 	bus->generation++;
4640227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4641dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4642227d67aaSAlexander Motin 	    ("destroying target, but device list is not empty"));
4643227d67aaSAlexander Motin 	xpt_release_bus(bus);
4644227d67aaSAlexander Motin 	mtx_destroy(&target->luns_mtx);
464582b361b1SMatt Jacob 	if (target->luns)
464682b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4647362abc44STai-hwa Liang 	free(target, M_CAMXPT);
464877dc25ccSScott Long }
46498b8a9b1dSJustin T. Gibbs 
46508b8a9b1dSJustin T. Gibbs static struct cam_ed *
465152c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
465252c9ce25SScott Long 			 lun_id_t lun_id)
465352c9ce25SScott Long {
4654dcdf6e74SAlexander Motin 	struct cam_ed *device;
465552c9ce25SScott Long 
465652c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
465752c9ce25SScott Long 	if (device == NULL)
465852c9ce25SScott Long 		return (NULL);
465952c9ce25SScott Long 
466052c9ce25SScott Long 	device->mintags = 1;
466152c9ce25SScott Long 	device->maxtags = 1;
466252c9ce25SScott Long 	return (device);
466352c9ce25SScott Long }
466452c9ce25SScott Long 
4665227d67aaSAlexander Motin static void
4666227d67aaSAlexander Motin xpt_destroy_device(void *context, int pending)
4667227d67aaSAlexander Motin {
4668227d67aaSAlexander Motin 	struct cam_ed	*device = context;
4669227d67aaSAlexander Motin 
4670227d67aaSAlexander Motin 	mtx_lock(&device->device_mtx);
4671227d67aaSAlexander Motin 	mtx_destroy(&device->device_mtx);
4672227d67aaSAlexander Motin 	free(device, M_CAMDEV);
4673227d67aaSAlexander Motin }
4674227d67aaSAlexander Motin 
467552c9ce25SScott Long struct cam_ed *
46768b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
46778b8a9b1dSJustin T. Gibbs {
4678dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
46798b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4680a5479bc5SJustin T. Gibbs 	cam_status status;
46818b8a9b1dSJustin T. Gibbs 
4682227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
46838b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
46848b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4685227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4686cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4687227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4688dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4689dcdf6e74SAlexander Motin 		return (NULL);
46908b8a9b1dSJustin T. Gibbs 
46918b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4692596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4693dcdf6e74SAlexander Motin 	if (device == NULL)
4694dcdf6e74SAlexander Motin 		return (NULL);
46958b8a9b1dSJustin T. Gibbs 
4696227d67aaSAlexander Motin 	cam_init_pinfo(&device->devq_entry);
46978b8a9b1dSJustin T. Gibbs 	device->target = target;
46988b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
46992b83592fSScott Long 	device->sim = bus->sim;
47008b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
47018b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
4702596ee08fSAlexander Motin 		free(device, M_CAMDEV);
47038b8a9b1dSJustin T. Gibbs 		return (NULL);
47048b8a9b1dSJustin T. Gibbs 	}
4705434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4706434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4707434bbf6eSJustin T. Gibbs 	device->generation = 0;
4708434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4709434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4710df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4711434bbf6eSJustin T. Gibbs 	device->refcount = 1;
4712227d67aaSAlexander Motin 	mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4713227d67aaSAlexander Motin 	callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4714227d67aaSAlexander Motin 	TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4715227d67aaSAlexander Motin 	/*
4716227d67aaSAlexander Motin 	 * Hold a reference to our parent bus so it
4717227d67aaSAlexander Motin 	 * will not go away before we do.
4718227d67aaSAlexander Motin 	 */
4719227d67aaSAlexander Motin 	target->refcount++;
4720434bbf6eSJustin T. Gibbs 
4721dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4722dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4723dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4724dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4725dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4726dcdf6e74SAlexander Motin 	else
4727dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4728dcdf6e74SAlexander Motin 	target->generation++;
47298b8a9b1dSJustin T. Gibbs 	return (device);
47308b8a9b1dSJustin T. Gibbs }
47318b8a9b1dSJustin T. Gibbs 
4732f98d7a47SAlexander Motin void
4733f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
47348b8a9b1dSJustin T. Gibbs {
4735227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
47368b8a9b1dSJustin T. Gibbs 
4737227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4738f98d7a47SAlexander Motin 	device->refcount++;
4739227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4740f98d7a47SAlexander Motin }
4741f98d7a47SAlexander Motin 
4742f98d7a47SAlexander Motin void
4743f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4744f98d7a47SAlexander Motin {
4745227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
47468b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
47478b8a9b1dSJustin T. Gibbs 
4748227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4749227d67aaSAlexander Motin 	if (--device->refcount > 0) {
4750227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4751dcdf6e74SAlexander Motin 		return;
4752227d67aaSAlexander Motin 	}
4753227d67aaSAlexander Motin 
4754227d67aaSAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4755227d67aaSAlexander Motin 	device->target->generation++;
4756227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4757227d67aaSAlexander Motin 
4758227d67aaSAlexander Motin 	/* Release our slot in the devq */
4759227d67aaSAlexander Motin 	devq = bus->sim->devq;
4760227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4761227d67aaSAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
4762227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4763dcdf6e74SAlexander Motin 
4764dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4765227d67aaSAlexander Motin 	    ("destroying device, but periphs list is not empty"));
4766227d67aaSAlexander Motin 	KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4767227d67aaSAlexander Motin 	    ("destroying device while still queued for ccbs"));
47682cefde5fSJustin T. Gibbs 
47692cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
47702b83592fSScott Long 		callout_stop(&device->callout);
47712cefde5fSJustin T. Gibbs 
4772227d67aaSAlexander Motin 	xpt_release_target(device->target);
4773227d67aaSAlexander Motin 
477420a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4775e6bd5983SKenneth D. Merry 	/*
4776e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4777e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4778e6bd5983SKenneth D. Merry 	 * checking.
4779e6bd5983SKenneth D. Merry 	 */
4780e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4781e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
4782e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4783e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4784e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4785227d67aaSAlexander Motin 	taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
47868b8a9b1dSJustin T. Gibbs }
47878b8a9b1dSJustin T. Gibbs 
478852c9ce25SScott Long u_int32_t
47898b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
47908b8a9b1dSJustin T. Gibbs {
47918b8a9b1dSJustin T. Gibbs 	int	result;
47928b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
47938b8a9b1dSJustin T. Gibbs 
47948b8a9b1dSJustin T. Gibbs 	dev = path->device;
4795227d67aaSAlexander Motin 	mtx_lock(&dev->sim->devq->send_mtx);
47968b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
4797227d67aaSAlexander Motin 	mtx_unlock(&dev->sim->devq->send_mtx);
4798df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4799df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
4800df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
48018b8a9b1dSJustin T. Gibbs 	return (result);
48028b8a9b1dSJustin T. Gibbs }
48038b8a9b1dSJustin T. Gibbs 
48048b8a9b1dSJustin T. Gibbs static struct cam_eb *
48058b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
48068b8a9b1dSJustin T. Gibbs {
48078b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
48088b8a9b1dSJustin T. Gibbs 
48099a7c2696SAlexander Motin 	xpt_lock_buses();
48102b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
48118b8a9b1dSJustin T. Gibbs 	     bus != NULL;
48128b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
4813a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
4814a5479bc5SJustin T. Gibbs 			bus->refcount++;
48158b8a9b1dSJustin T. Gibbs 			break;
48168b8a9b1dSJustin T. Gibbs 		}
4817a5479bc5SJustin T. Gibbs 	}
48189a7c2696SAlexander Motin 	xpt_unlock_buses();
48198b8a9b1dSJustin T. Gibbs 	return (bus);
48208b8a9b1dSJustin T. Gibbs }
48218b8a9b1dSJustin T. Gibbs 
48228b8a9b1dSJustin T. Gibbs static struct cam_et *
48238b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
48248b8a9b1dSJustin T. Gibbs {
48258b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
48268b8a9b1dSJustin T. Gibbs 
4827227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
48288b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
48298b8a9b1dSJustin T. Gibbs 	     target != NULL;
48308b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
48318b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
48328b8a9b1dSJustin T. Gibbs 			target->refcount++;
48338b8a9b1dSJustin T. Gibbs 			break;
48348b8a9b1dSJustin T. Gibbs 		}
48358b8a9b1dSJustin T. Gibbs 	}
48368b8a9b1dSJustin T. Gibbs 	return (target);
48378b8a9b1dSJustin T. Gibbs }
48388b8a9b1dSJustin T. Gibbs 
48398b8a9b1dSJustin T. Gibbs static struct cam_ed *
48408b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
48418b8a9b1dSJustin T. Gibbs {
48428b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
48438b8a9b1dSJustin T. Gibbs 
4844227d67aaSAlexander Motin 	mtx_assert(&target->bus->eb_mtx, MA_OWNED);
48458b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
48468b8a9b1dSJustin T. Gibbs 	     device != NULL;
48478b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
48488b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
48498b8a9b1dSJustin T. Gibbs 			device->refcount++;
48508b8a9b1dSJustin T. Gibbs 			break;
48518b8a9b1dSJustin T. Gibbs 		}
48528b8a9b1dSJustin T. Gibbs 	}
48538b8a9b1dSJustin T. Gibbs 	return (device);
48548b8a9b1dSJustin T. Gibbs }
48558b8a9b1dSJustin T. Gibbs 
485630a4094fSAlexander Motin void
4857f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
4858f0adc790SJustin T. Gibbs {
4859fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
4860fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
4861fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
4862fd21cc5eSJustin T. Gibbs 	int    newopenings;
4863fd21cc5eSJustin T. Gibbs 
4864fd21cc5eSJustin T. Gibbs 	device = path->device;
4865fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
4866fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4867fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
4868fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
4869df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
4870df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
4871df8f9080SJustin T. Gibbs 	else
487252c9ce25SScott Long 		newopenings = min(device->maxtags,
4873df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
4874f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
4875581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4876bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4877fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
4878fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4879fd21cc5eSJustin T. Gibbs 	crs.openings
4880fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
4881fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
4882fd21cc5eSJustin T. Gibbs 	    = 0;
4883fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
4884fd21cc5eSJustin T. Gibbs }
4885fd21cc5eSJustin T. Gibbs 
488630a4094fSAlexander Motin void
488730a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
488830a4094fSAlexander Motin {
488930a4094fSAlexander Motin 	struct ccb_relsim crs;
489030a4094fSAlexander Motin 	struct cam_ed *device;
489130a4094fSAlexander Motin 	struct cam_sim *sim;
489230a4094fSAlexander Motin 
489330a4094fSAlexander Motin 	device = path->device;
489430a4094fSAlexander Motin 	sim = path->bus->sim;
489530a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
489630a4094fSAlexander Motin 	device->tag_delay_count = 0;
489730a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
489830a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
489930a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4900581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
490130a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
490230a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
490330a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
490430a4094fSAlexander Motin 	crs.openings
490530a4094fSAlexander Motin 	    = crs.release_timeout
490630a4094fSAlexander Motin 	    = crs.qfrozen_cnt
490730a4094fSAlexander Motin 	    = 0;
490830a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
490930a4094fSAlexander Motin }
491030a4094fSAlexander Motin 
491183c5d981SAlexander Motin static void
491283c5d981SAlexander Motin xpt_boot_delay(void *arg)
49138b8a9b1dSJustin T. Gibbs {
49142b83592fSScott Long 
491583c5d981SAlexander Motin 	xpt_release_boot();
49168b8a9b1dSJustin T. Gibbs }
49178b8a9b1dSJustin T. Gibbs 
49188b8a9b1dSJustin T. Gibbs static void
49198b8a9b1dSJustin T. Gibbs xpt_config(void *arg)
49208b8a9b1dSJustin T. Gibbs {
49213393f8daSKenneth D. Merry 	/*
49223393f8daSKenneth D. Merry 	 * Now that interrupts are enabled, go find our devices
49233393f8daSKenneth D. Merry 	 */
4924227d67aaSAlexander Motin 	if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
4925227d67aaSAlexander Motin 		printf("xpt_config: failed to create taskqueue thread.\n");
49268b8a9b1dSJustin T. Gibbs 
4927f0f25b9cSAlexander Motin 	/* Setup debugging path */
49288b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
4929227d67aaSAlexander Motin 		if (xpt_create_path(&cam_dpath, NULL,
49308b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
49318b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
49328b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
49338b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
49348b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
49358b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
49368b8a9b1dSJustin T. Gibbs 		}
49378b8a9b1dSJustin T. Gibbs 	} else
49388b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
49398b8a9b1dSJustin T. Gibbs 
494083c5d981SAlexander Motin 	periphdriver_init(1);
494183c5d981SAlexander Motin 	xpt_hold_boot();
494283c5d981SAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
494383c5d981SAlexander Motin 	callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
494483c5d981SAlexander Motin 	    xpt_boot_delay, NULL);
494583c5d981SAlexander Motin 	/* Fire up rescan thread. */
4946227d67aaSAlexander Motin 	if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
4947227d67aaSAlexander Motin 	    "cam", "scanner")) {
49486f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
49491e637ba6SAlexander Motin 	}
495083c5d981SAlexander Motin }
49518b8a9b1dSJustin T. Gibbs 
495283c5d981SAlexander Motin void
495383c5d981SAlexander Motin xpt_hold_boot(void)
495483c5d981SAlexander Motin {
495583c5d981SAlexander Motin 	xpt_lock_buses();
495683c5d981SAlexander Motin 	xsoftc.buses_to_config++;
495783c5d981SAlexander Motin 	xpt_unlock_buses();
495883c5d981SAlexander Motin }
495983c5d981SAlexander Motin 
496083c5d981SAlexander Motin void
496183c5d981SAlexander Motin xpt_release_boot(void)
496283c5d981SAlexander Motin {
496383c5d981SAlexander Motin 	xpt_lock_buses();
496483c5d981SAlexander Motin 	xsoftc.buses_to_config--;
496583c5d981SAlexander Motin 	if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
496683c5d981SAlexander Motin 		struct	xpt_task *task;
496783c5d981SAlexander Motin 
496883c5d981SAlexander Motin 		xsoftc.buses_config_done = 1;
496983c5d981SAlexander Motin 		xpt_unlock_buses();
4970ecdf1113SJustin T. Gibbs 		/* Call manually because we don't have any busses */
497183c5d981SAlexander Motin 		task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
497283c5d981SAlexander Motin 		if (task != NULL) {
497383c5d981SAlexander Motin 			TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
497483c5d981SAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &task->task);
49752863f7b1SJustin T. Gibbs 		}
497683c5d981SAlexander Motin 	} else
497783c5d981SAlexander Motin 		xpt_unlock_buses();
49782863f7b1SJustin T. Gibbs }
49798b8a9b1dSJustin T. Gibbs 
49808b8a9b1dSJustin T. Gibbs /*
49818b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
49828b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
49838b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
49848b8a9b1dSJustin T. Gibbs  */
49858b8a9b1dSJustin T. Gibbs static int
49868b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
49878b8a9b1dSJustin T. Gibbs {
49888b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
49898b8a9b1dSJustin T. Gibbs 	int i;
49908b8a9b1dSJustin T. Gibbs 
49918b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
49928b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
49938b8a9b1dSJustin T. Gibbs 
49948b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
49958b8a9b1dSJustin T. Gibbs 	if ((i == 1)
49968b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
49978b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
49988b8a9b1dSJustin T. Gibbs 
49998b8a9b1dSJustin T. Gibbs 	return(1);
50008b8a9b1dSJustin T. Gibbs }
50018b8a9b1dSJustin T. Gibbs 
50028b8a9b1dSJustin T. Gibbs static void
50032b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
50048b8a9b1dSJustin T. Gibbs {
50058b8a9b1dSJustin T. Gibbs 
500683c5d981SAlexander Motin 	periphdriver_init(2);
50072b83592fSScott Long 	/*
50082b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
50092b83592fSScott Long 	 * attached.  For any devices like that, announce the
50102b83592fSScott Long 	 * passthrough driver so the user will see something.
50112b83592fSScott Long 	 */
50123089bb2eSAlexander Motin 	if (!bootverbose)
50132b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
50142b83592fSScott Long 
50152b83592fSScott Long 	/* Release our hook so that the boot can continue. */
50162b83592fSScott Long 	config_intrhook_disestablish(xsoftc.xpt_config_hook);
50170dd50e9bSScott Long 	free(xsoftc.xpt_config_hook, M_CAMXPT);
50182b83592fSScott Long 	xsoftc.xpt_config_hook = NULL;
50192b83592fSScott Long 
50202b83592fSScott Long 	free(context, M_CAMXPT);
50212b83592fSScott Long }
50222b83592fSScott Long 
502385d92640SScott Long cam_status
502485d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
502585d92640SScott Long 		   struct cam_path *path)
502685d92640SScott Long {
502785d92640SScott Long 	struct ccb_setasync csa;
502885d92640SScott Long 	cam_status status;
502985d92640SScott Long 	int xptpath = 0;
503085d92640SScott Long 
503185d92640SScott Long 	if (path == NULL) {
503285d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
503385d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5034227d67aaSAlexander Motin 		if (status != CAM_REQ_CMP)
503585d92640SScott Long 			return (status);
5036227d67aaSAlexander Motin 		xpt_path_lock(path);
503785d92640SScott Long 		xptpath = 1;
503885d92640SScott Long 	}
503985d92640SScott Long 
504083c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
504185d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
504285d92640SScott Long 	csa.event_enable = event;
504385d92640SScott Long 	csa.callback = cbfunc;
504485d92640SScott Long 	csa.callback_arg = cbarg;
504585d92640SScott Long 	xpt_action((union ccb *)&csa);
504685d92640SScott Long 	status = csa.ccb_h.status;
50473501942bSJustin T. Gibbs 
504885d92640SScott Long 	if (xptpath) {
5049227d67aaSAlexander Motin 		xpt_path_unlock(path);
505085d92640SScott Long 		xpt_free_path(path);
50513501942bSJustin T. Gibbs 	}
50527685edecSAlexander Motin 
50537685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
50547685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
50557685edecSAlexander Motin 		/*
50567685edecSAlexander Motin 		 * Get this peripheral up to date with all
50577685edecSAlexander Motin 		 * the currently existing devices.
50587685edecSAlexander Motin 		 */
50597685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
50607685edecSAlexander Motin 	}
50617685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
50627685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
50637685edecSAlexander Motin 		/*
50647685edecSAlexander Motin 		 * Get this peripheral up to date with all
50657685edecSAlexander Motin 		 * the currently existing busses.
50667685edecSAlexander Motin 		 */
50677685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
50687685edecSAlexander Motin 	}
50693501942bSJustin T. Gibbs 
507085d92640SScott Long 	return (status);
507185d92640SScott Long }
507285d92640SScott Long 
50738b8a9b1dSJustin T. Gibbs static void
50748b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
50758b8a9b1dSJustin T. Gibbs {
50768b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
50778b8a9b1dSJustin T. Gibbs 
50788b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
50798b8a9b1dSJustin T. Gibbs 	/* Common cases first */
50808b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
50818b8a9b1dSJustin T. Gibbs 	{
50828b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
50838b8a9b1dSJustin T. Gibbs 
50848b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
50858b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
50868b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
50878b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
50888b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
50898b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
50908b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
50918b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
50928b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
50938b8a9b1dSJustin T. Gibbs 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
50948b8a9b1dSJustin T. Gibbs 		strncpy(cpi->hba_vid, "", HBA_IDLEN);
50958b8a9b1dSJustin T. Gibbs 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
50968b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
50978b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
50989deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
50993393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
51003393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
51013393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
51023393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
51038b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
51048b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
51058b8a9b1dSJustin T. Gibbs 		break;
51068b8a9b1dSJustin T. Gibbs 	}
51078b8a9b1dSJustin T. Gibbs 	default:
51088b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
51098b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
51108b8a9b1dSJustin T. Gibbs 		break;
51118b8a9b1dSJustin T. Gibbs 	}
51128b8a9b1dSJustin T. Gibbs }
51138b8a9b1dSJustin T. Gibbs 
51148b8a9b1dSJustin T. Gibbs /*
5115434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
5116434bbf6eSJustin T. Gibbs  * is a no-op.
5117434bbf6eSJustin T. Gibbs  */
5118434bbf6eSJustin T. Gibbs static void
5119434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
5120434bbf6eSJustin T. Gibbs {
5121434bbf6eSJustin T. Gibbs }
5122434bbf6eSJustin T. Gibbs 
51232b83592fSScott Long void
51242b83592fSScott Long xpt_lock_buses(void)
51252b83592fSScott Long {
51262b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
51272b83592fSScott Long }
51282b83592fSScott Long 
51292b83592fSScott Long void
51302b83592fSScott Long xpt_unlock_buses(void)
51312b83592fSScott Long {
51322b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
51332b83592fSScott Long }
51342b83592fSScott Long 
5135227d67aaSAlexander Motin struct mtx *
5136227d67aaSAlexander Motin xpt_path_mtx(struct cam_path *path)
51378b8a9b1dSJustin T. Gibbs {
5138227d67aaSAlexander Motin 
5139227d67aaSAlexander Motin 	return (&path->device->device_mtx);
5140227d67aaSAlexander Motin }
5141227d67aaSAlexander Motin 
5142227d67aaSAlexander Motin static void
5143227d67aaSAlexander Motin xpt_done_process(struct ccb_hdr *ccb_h)
5144227d67aaSAlexander Motin {
51452b83592fSScott Long 	struct cam_sim *sim;
5146227d67aaSAlexander Motin 	struct cam_devq *devq;
5147227d67aaSAlexander Motin 	struct mtx *mtx = NULL;
51488b8a9b1dSJustin T. Gibbs 
51498b8a9b1dSJustin T. Gibbs 	if (ccb_h->flags & CAM_HIGH_POWER) {
51508b8a9b1dSJustin T. Gibbs 		struct highpowerlist	*hphead;
5151ea541bfdSAlexander Motin 		struct cam_ed		*device;
51528b8a9b1dSJustin T. Gibbs 
51532b83592fSScott Long 		mtx_lock(&xsoftc.xpt_lock);
51542b83592fSScott Long 		hphead = &xsoftc.highpowerq;
51558b8a9b1dSJustin T. Gibbs 
5156ea541bfdSAlexander Motin 		device = STAILQ_FIRST(hphead);
51578b8a9b1dSJustin T. Gibbs 
51588b8a9b1dSJustin T. Gibbs 		/*
51598b8a9b1dSJustin T. Gibbs 		 * Increment the count since this command is done.
51608b8a9b1dSJustin T. Gibbs 		 */
51612b83592fSScott Long 		xsoftc.num_highpower++;
51628b8a9b1dSJustin T. Gibbs 
51638b8a9b1dSJustin T. Gibbs 		/*
51648b8a9b1dSJustin T. Gibbs 		 * Any high powered commands queued up?
51658b8a9b1dSJustin T. Gibbs 		 */
5166ea541bfdSAlexander Motin 		if (device != NULL) {
51678b8a9b1dSJustin T. Gibbs 
5168ea541bfdSAlexander Motin 			STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
51692b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
51708b8a9b1dSJustin T. Gibbs 
5171227d67aaSAlexander Motin 			mtx_lock(&device->sim->devq->send_mtx);
5172ea541bfdSAlexander Motin 			xpt_release_devq_device(device,
51732cefde5fSJustin T. Gibbs 					 /*count*/1, /*runqueue*/TRUE);
5174227d67aaSAlexander Motin 			mtx_unlock(&device->sim->devq->send_mtx);
51752b83592fSScott Long 		} else
51762b83592fSScott Long 			mtx_unlock(&xsoftc.xpt_lock);
51778b8a9b1dSJustin T. Gibbs 	}
51782b83592fSScott Long 
5179227d67aaSAlexander Motin 	sim = ccb_h->path->bus->sim;
5180227d67aaSAlexander Motin 
5181227d67aaSAlexander Motin 	if (ccb_h->status & CAM_RELEASE_SIMQ) {
5182227d67aaSAlexander Motin 		xpt_release_simq(sim, /*run_queue*/FALSE);
5183227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_RELEASE_SIMQ;
5184227d67aaSAlexander Motin 	}
5185227d67aaSAlexander Motin 
5186227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5187227d67aaSAlexander Motin 	 && (ccb_h->status & CAM_DEV_QFRZN)) {
5188227d67aaSAlexander Motin 		xpt_release_devq(ccb_h->path, /*count*/1,
5189227d67aaSAlexander Motin 				 /*run_queue*/FALSE);
5190227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_DEV_QFRZN;
5191227d67aaSAlexander Motin 	}
5192227d67aaSAlexander Motin 
5193227d67aaSAlexander Motin 	devq = sim->devq;
51949deea857SKenneth D. Merry 	if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5195227d67aaSAlexander Motin 		struct cam_ed *dev = ccb_h->path->device;
51968b8a9b1dSJustin T. Gibbs 
5197227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
5198227d67aaSAlexander Motin 		devq->send_active--;
5199227d67aaSAlexander Motin 		devq->send_openings++;
52008b8a9b1dSJustin T. Gibbs 		cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
52018b8a9b1dSJustin T. Gibbs 
5202b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
52038b8a9b1dSJustin T. Gibbs 		  && (dev->ccbq.dev_active == 0))) {
5204b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5205227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
5206b9c473b2SAlexander Motin 					 /*run_queue*/FALSE);
5207b9c473b2SAlexander Motin 		}
5208b9c473b2SAlexander Motin 
5209b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5210b9c473b2SAlexander Motin 		  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5211b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5212227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
521383c5d981SAlexander Motin 					 /*run_queue*/FALSE);
52148b8a9b1dSJustin T. Gibbs 		}
52158b8a9b1dSJustin T. Gibbs 
5216227d67aaSAlexander Motin 		if (!device_is_queued(dev))
5217227d67aaSAlexander Motin 			(void)xpt_schedule_devq(devq, dev);
5218227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
5219227d67aaSAlexander Motin 
5220227d67aaSAlexander Motin 		if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5221227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5222227d67aaSAlexander Motin 			mtx_lock(mtx);
5223227d67aaSAlexander Motin 
5224fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5225fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5226fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
52273501942bSJustin T. Gibbs 		}
52288b8a9b1dSJustin T. Gibbs 	}
52298b8a9b1dSJustin T. Gibbs 
5230227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5231227d67aaSAlexander Motin 		if (mtx == NULL) {
5232227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5233227d67aaSAlexander Motin 			mtx_lock(mtx);
5234434bbf6eSJustin T. Gibbs 		}
5235227d67aaSAlexander Motin 	} else {
5236227d67aaSAlexander Motin 		if (mtx != NULL) {
5237227d67aaSAlexander Motin 			mtx_unlock(mtx);
5238227d67aaSAlexander Motin 			mtx = NULL;
5239227d67aaSAlexander Motin 		}
52408b8a9b1dSJustin T. Gibbs 	}
52418b8a9b1dSJustin T. Gibbs 
52428b8a9b1dSJustin T. Gibbs 	/* Call the peripheral driver's callback */
5243f1486b51SAlexander Motin 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5244434bbf6eSJustin T. Gibbs 	(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5245227d67aaSAlexander Motin 	if (mtx != NULL)
5246227d67aaSAlexander Motin 		mtx_unlock(mtx);
5247227d67aaSAlexander Motin 
5248227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
5249227d67aaSAlexander Motin 	xpt_run_devq(devq);
5250227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
5251227d67aaSAlexander Motin }
5252227d67aaSAlexander Motin 
5253227d67aaSAlexander Motin void
5254227d67aaSAlexander Motin xpt_done_td(void *arg)
5255227d67aaSAlexander Motin {
5256227d67aaSAlexander Motin 	struct cam_doneq *queue = arg;
5257227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
5258227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	doneq;
5259227d67aaSAlexander Motin 
5260227d67aaSAlexander Motin 	STAILQ_INIT(&doneq);
5261227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
5262227d67aaSAlexander Motin 	while (1) {
5263227d67aaSAlexander Motin 		while (STAILQ_EMPTY(&queue->cam_doneq)) {
5264227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 1;
5265227d67aaSAlexander Motin 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5266227d67aaSAlexander Motin 			    PRIBIO, "-", 0);
5267227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 0;
5268227d67aaSAlexander Motin 		}
5269227d67aaSAlexander Motin 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5270227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
5271227d67aaSAlexander Motin 
5272227d67aaSAlexander Motin 		THREAD_NO_SLEEPING();
5273227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5274227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5275227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5276227d67aaSAlexander Motin 		}
5277227d67aaSAlexander Motin 		THREAD_SLEEPING_OK();
5278227d67aaSAlexander Motin 
5279227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5280227d67aaSAlexander Motin 	}
5281227d67aaSAlexander Motin }
5282227d67aaSAlexander Motin 
5283227d67aaSAlexander Motin static void
5284227d67aaSAlexander Motin camisr_runqueue(void)
5285227d67aaSAlexander Motin {
5286227d67aaSAlexander Motin 	struct	ccb_hdr *ccb_h;
5287227d67aaSAlexander Motin 	struct cam_doneq *queue;
5288227d67aaSAlexander Motin 	int i;
5289227d67aaSAlexander Motin 
5290227d67aaSAlexander Motin 	/* Process global queues. */
5291227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
5292227d67aaSAlexander Motin 		queue = &cam_doneqs[i];
5293227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5294227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5295227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5296227d67aaSAlexander Motin 			mtx_unlock(&queue->cam_doneq_mtx);
5297227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5298227d67aaSAlexander Motin 			mtx_lock(&queue->cam_doneq_mtx);
5299227d67aaSAlexander Motin 		}
5300227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
53018b8a9b1dSJustin T. Gibbs 	}
53028b8a9b1dSJustin T. Gibbs }
5303