xref: /freebsd/sys/cam/cam_xpt.c (revision 56eccd2d066522a4ce21268f1d19754db957c719)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Implementation of the Common Access Method Transport (XPT) layer.
38b8a9b1dSJustin T. Gibbs  *
4bec9534dSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5bec9534dSPedro F. Giffuni  *
6c8bead2aSJustin T. Gibbs  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
759190eaaSKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
88b8a9b1dSJustin T. Gibbs  * All rights reserved.
98b8a9b1dSJustin T. Gibbs  *
108b8a9b1dSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
118b8a9b1dSJustin T. Gibbs  * modification, are permitted provided that the following conditions
128b8a9b1dSJustin T. Gibbs  * are met:
138b8a9b1dSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
148b8a9b1dSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
158b8a9b1dSJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
168b8a9b1dSJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
178b8a9b1dSJustin T. Gibbs  *    derived from this software without specific prior written permission.
188b8a9b1dSJustin T. Gibbs  *
198b8a9b1dSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
208b8a9b1dSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218b8a9b1dSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228b8a9b1dSJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
238b8a9b1dSJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248b8a9b1dSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258b8a9b1dSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268b8a9b1dSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278b8a9b1dSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288b8a9b1dSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298b8a9b1dSJustin T. Gibbs  * SUCH DAMAGE.
308b8a9b1dSJustin T. Gibbs  */
319c963d87SDavid E. O'Brien 
322379d1d6SWarner Losh #include "opt_printf.h"
332379d1d6SWarner Losh 
349c963d87SDavid E. O'Brien #include <sys/cdefs.h>
359c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$");
369c963d87SDavid E. O'Brien 
378b8a9b1dSJustin T. Gibbs #include <sys/param.h>
388532d381SConrad Meyer #include <sys/bio.h>
399a94c9c5SJohn Baldwin #include <sys/bus.h>
408b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
418b8a9b1dSJustin T. Gibbs #include <sys/types.h>
428b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
438b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
4487cfaf0eSJustin T. Gibbs #include <sys/time.h>
458b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
468b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
47227d67aaSAlexander Motin #include <sys/proc.h>
483393f8daSKenneth D. Merry #include <sys/sbuf.h>
49227d67aaSAlexander Motin #include <sys/smp.h>
502b83592fSScott Long #include <sys/taskqueue.h>
518b8a9b1dSJustin T. Gibbs 
52ef3cf714SScott Long #include <sys/lock.h>
53ef3cf714SScott Long #include <sys/mutex.h>
543b87a552SMatt Jacob #include <sys/sysctl.h>
559e6461a2SMatt Jacob #include <sys/kthread.h>
56ef3cf714SScott Long 
578b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
588b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
59e4c9cba7SWarner Losh #include <cam/cam_iosched.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
6152c9ce25SScott Long #include <cam/cam_queue.h>
628b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
638b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
648b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
658b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
6652c9ce25SScott Long #include <cam/cam_xpt_internal.h>
678b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
6825a2902cSScott Long #include <cam/cam_compat.h>
698b8a9b1dSJustin T. Gibbs 
708b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
718b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
728b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
73abef0e67SMarius Strobl 
74abef0e67SMarius Strobl #include <machine/md_var.h>	/* geometry translation */
75f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
76abef0e67SMarius Strobl 
778b8a9b1dSJustin T. Gibbs #include "opt_cam.h"
788b8a9b1dSJustin T. Gibbs 
792379d1d6SWarner Losh /* Wild guess based on not wanting to grow the stack too much */
802379d1d6SWarner Losh #define XPT_PRINT_MAXLEN	512
812379d1d6SWarner Losh #ifdef PRINTF_BUFR_SIZE
822379d1d6SWarner Losh #define XPT_PRINT_LEN	PRINTF_BUFR_SIZE
832379d1d6SWarner Losh #else
842379d1d6SWarner Losh #define XPT_PRINT_LEN	128
852379d1d6SWarner Losh #endif
862379d1d6SWarner Losh _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
872379d1d6SWarner Losh 
8852c9ce25SScott Long /*
8952c9ce25SScott Long  * This is the maximum number of high powered commands (e.g. start unit)
9052c9ce25SScott Long  * that can be outstanding at a particular time.
9152c9ce25SScott Long  */
9252c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER
9352c9ce25SScott Long #define CAM_MAX_HIGHPOWER  4
9452c9ce25SScott Long #endif
9552c9ce25SScott Long 
968b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
97362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
98596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
99596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
100596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
1018b8a9b1dSJustin T. Gibbs 
1028b8a9b1dSJustin T. Gibbs struct xpt_softc {
103636870ffSWill Andrews 	uint32_t		xpt_generation;
104636870ffSWill Andrews 
1052b83592fSScott Long 	/* number of high powered commands that can go through right now */
106daa5487fSAlexander Motin 	struct mtx		xpt_highpower_lock;
107ea541bfdSAlexander Motin 	STAILQ_HEAD(highpowerlist, cam_ed)	highpowerq;
1082b83592fSScott Long 	int			num_highpower;
1092b83592fSScott Long 
1102b83592fSScott Long 	/* queue for handling async rescan requests. */
1112b83592fSScott Long 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
11283c5d981SAlexander Motin 	int buses_to_config;
11383c5d981SAlexander Motin 	int buses_config_done;
1145d01277fSScott Long 	int announce_nosbuf;
1152b83592fSScott Long 
116db4fcadfSConrad Meyer 	/*
117db4fcadfSConrad Meyer 	 * Registered buses
118db4fcadfSConrad Meyer 	 *
119db4fcadfSConrad Meyer 	 * N.B., "busses" is an archaic spelling of "buses".  In new code
120db4fcadfSConrad Meyer 	 * "buses" is preferred.
121db4fcadfSConrad Meyer 	 */
1222b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
1232b83592fSScott Long 	u_int			bus_generation;
1242b83592fSScott Long 
12583c5d981SAlexander Motin 	int			boot_delay;
12683c5d981SAlexander Motin 	struct callout 		boot_callout;
127a4876fbfSAlexander Motin 	struct task		boot_task;
128a4876fbfSAlexander Motin 	struct root_hold_token	xpt_rootmount;
12983c5d981SAlexander Motin 
1302b83592fSScott Long 	struct mtx		xpt_topo_lock;
131227d67aaSAlexander Motin 	struct taskqueue	*xpt_taskq;
1328b8a9b1dSJustin T. Gibbs };
1338b8a9b1dSJustin T. Gibbs 
1348b8a9b1dSJustin T. Gibbs typedef enum {
1358b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1368b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1378b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1388b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1398b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1408b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1418b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1428b8a9b1dSJustin T. Gibbs } dev_match_ret;
1438b8a9b1dSJustin T. Gibbs 
1448b8a9b1dSJustin T. Gibbs typedef enum {
1458b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1468b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1478b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1488b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1498b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1508b8a9b1dSJustin T. Gibbs 
1518b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1528b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1538b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1548b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1558b8a9b1dSJustin T. Gibbs };
1568b8a9b1dSJustin T. Gibbs 
1578b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1588b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1598b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1608b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1618b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1628b8a9b1dSJustin T. Gibbs 
1638b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1648b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1658b8a9b1dSJustin T. Gibbs 
1665719711fSEdward Tomasz Napierala MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
1675719711fSEdward Tomasz Napierala 
16883c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
16983c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
170636870ffSWill Andrews SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
171636870ffSWill Andrews 	    &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
1725d01277fSScott Long SYSCTL_INT(_kern_cam, OID_AUTO, announce_nosbuf, CTLFLAG_RWTUN,
1735d01277fSScott Long 	    &xsoftc.announce_nosbuf, 0, "Don't use sbuf for announcements");
17483c5d981SAlexander Motin 
175227d67aaSAlexander Motin struct cam_doneq {
176227d67aaSAlexander Motin 	struct mtx_padalign	cam_doneq_mtx;
177227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	cam_doneq;
178227d67aaSAlexander Motin 	int			cam_doneq_sleep;
179227d67aaSAlexander Motin };
1808b8a9b1dSJustin T. Gibbs 
181227d67aaSAlexander Motin static struct cam_doneq cam_doneqs[MAXCPU];
182227d67aaSAlexander Motin static int cam_num_doneqs;
183227d67aaSAlexander Motin static struct proc *cam_proc;
184227d67aaSAlexander Motin 
185227d67aaSAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
186227d67aaSAlexander Motin            &cam_num_doneqs, 0, "Number of completion queues/threads");
1878b8a9b1dSJustin T. Gibbs 
1889a1c8571SNick Hibma struct cam_periph *xpt_periph;
1899a1c8571SNick Hibma 
1908b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1918b8a9b1dSJustin T. Gibbs 
1928b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1938b8a9b1dSJustin T. Gibbs {
1948b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1951e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1961e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1978b8a9b1dSJustin T. Gibbs };
1988b8a9b1dSJustin T. Gibbs 
1990b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
2008b8a9b1dSJustin T. Gibbs 
2018b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
2028b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
2038b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
20425a2902cSScott Long static d_ioctl_t xptdoioctl;
2058b8a9b1dSJustin T. Gibbs 
2064e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
207dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
2082b83592fSScott Long 	.d_flags =	0,
2097ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
2107ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
2117ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
2127ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
2138b8a9b1dSJustin T. Gibbs };
2148b8a9b1dSJustin T. Gibbs 
2158b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
2168b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
21761322a0aSAlexander Motin u_int32_t __read_mostly cam_dflags = CAM_DEBUG_FLAGS;
218af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
219f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
220f0f25b9cSAlexander Motin u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
221af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
222f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2238b8a9b1dSJustin T. Gibbs 
2246d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
22574bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
22674bd1c10SNick Hibma 
22774bd1c10SNick Hibma static moduledata_t cam_moduledata = {
22874bd1c10SNick Hibma 	"cam",
22974bd1c10SNick Hibma 	cam_module_event_handler,
23074bd1c10SNick Hibma 	NULL
23174bd1c10SNick Hibma };
23274bd1c10SNick Hibma 
2332b83592fSScott Long static int	xpt_init(void *);
23474bd1c10SNick Hibma 
23574bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
23674bd1c10SNick Hibma MODULE_VERSION(cam, 1);
23774bd1c10SNick Hibma 
2388b8a9b1dSJustin T. Gibbs 
2398b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2408b8a9b1dSJustin T. Gibbs 					u_int32_t async_code,
2418b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2428b8a9b1dSJustin T. Gibbs 					void *async_arg);
243434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
244434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
245227d67aaSAlexander Motin static union ccb *xpt_get_ccb(struct cam_periph *periph);
246227d67aaSAlexander Motin static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
247227d67aaSAlexander Motin static void	 xpt_run_allocq(struct cam_periph *periph, int sleep);
248227d67aaSAlexander Motin static void	 xpt_run_allocq_task(void *context, int pending);
249cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2505773ac11SJohn Baldwin static callout_func_t xpt_release_devq_timeout;
251227d67aaSAlexander Motin static void	 xpt_acquire_bus(struct cam_eb *bus);
252a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
253daa5487fSAlexander Motin static uint32_t	 xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
254227d67aaSAlexander Motin static int	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
255cccf4220SAlexander Motin 		    int run_queue);
2568b8a9b1dSJustin T. Gibbs static struct cam_et*
2578b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
258227d67aaSAlexander Motin static void	 xpt_acquire_target(struct cam_et *target);
259f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2608b8a9b1dSJustin T. Gibbs static struct cam_eb*
2618b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2628b8a9b1dSJustin T. Gibbs static struct cam_et*
2638b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2648b8a9b1dSJustin T. Gibbs static struct cam_ed*
2658b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2668b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
267a4876fbfSAlexander Motin static void	 xpt_hold_boot_locked(void);
268227d67aaSAlexander Motin static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
269227d67aaSAlexander Motin 				 u_int32_t new_priority);
2708b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2718b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
272434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
273227d67aaSAlexander Motin static void	 camisr_runqueue(void);
274227d67aaSAlexander Motin static void	 xpt_done_process(struct ccb_hdr *ccb_h);
275227d67aaSAlexander Motin static void	 xpt_done_td(void *);
2768b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2773393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2788b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2793393f8daSKenneth D. Merry 				       u_int num_patterns,
2803393f8daSKenneth D. Merry 				       struct cam_ed *device);
2818b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2823393f8daSKenneth D. Merry 				       u_int num_patterns,
2838b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2848b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2858b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2868b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2878b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2888b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2898b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2908b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2918b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2928b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2938b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2948b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2958b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2968b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2978b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2988b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2998b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
3008b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
3018b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
3028b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
3038b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
3048b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
3058b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
3068b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
3078b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
3088b8a9b1dSJustin T. Gibbs 					    void *arg);
3098b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
3108b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
3118b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
3128b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
31383c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
31452c9ce25SScott Long static void		xpt_dev_async_default(u_int32_t async_code,
31552c9ce25SScott Long 					      struct cam_eb *bus,
31652c9ce25SScott Long 					      struct cam_et *target,
31752c9ce25SScott Long 					      struct cam_ed *device,
31852c9ce25SScott Long 					      void *async_arg);
31952c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
32052c9ce25SScott Long 						 struct cam_et *target,
32152c9ce25SScott Long 						 lun_id_t lun_id);
3228b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
3238b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
3248b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
3258b8a9b1dSJustin T. Gibbs 				    void *arg);
326cccf4220SAlexander Motin static __inline int device_is_queued(struct cam_ed *device);
3278b8a9b1dSJustin T. Gibbs 
3288b8a9b1dSJustin T. Gibbs static __inline int
329cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
33030a4094fSAlexander Motin {
33130a4094fSAlexander Motin 	int	retval;
33230a4094fSAlexander Motin 
333227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
33483c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
33583c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
336cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
33730a4094fSAlexander Motin 		/*
33830a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3396bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
34030a4094fSAlexander Motin 		 * enqueued.
34130a4094fSAlexander Motin 		 */
34230a4094fSAlexander Motin 		retval =
343cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
344227d67aaSAlexander Motin 				     &dev->devq_entry,
34583c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
34630a4094fSAlexander Motin 	} else {
34730a4094fSAlexander Motin 		retval = 0;
34830a4094fSAlexander Motin 	}
34930a4094fSAlexander Motin 	return (retval);
35030a4094fSAlexander Motin }
35130a4094fSAlexander Motin 
35230a4094fSAlexander Motin static __inline int
353cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3548b8a9b1dSJustin T. Gibbs {
355227d67aaSAlexander Motin 	return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
3568b8a9b1dSJustin T. Gibbs }
3578b8a9b1dSJustin T. Gibbs 
3588b8a9b1dSJustin T. Gibbs static void
3598b8a9b1dSJustin T. Gibbs xpt_periph_init()
3608b8a9b1dSJustin T. Gibbs {
36173d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3628b8a9b1dSJustin T. Gibbs }
3638b8a9b1dSJustin T. Gibbs 
3648b8a9b1dSJustin T. Gibbs static int
36589c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3668b8a9b1dSJustin T. Gibbs {
3678b8a9b1dSJustin T. Gibbs 
3688b8a9b1dSJustin T. Gibbs 	/*
36966a0780eSKenneth D. Merry 	 * Only allow read-write access.
37066a0780eSKenneth D. Merry 	 */
37166a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
37266a0780eSKenneth D. Merry 		return(EPERM);
37366a0780eSKenneth D. Merry 
37466a0780eSKenneth D. Merry 	/*
3758b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3768b8a9b1dSJustin T. Gibbs 	 */
3778b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3782b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3798b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3808b8a9b1dSJustin T. Gibbs 	}
3818b8a9b1dSJustin T. Gibbs 
3828b8a9b1dSJustin T. Gibbs 	return(0);
3838b8a9b1dSJustin T. Gibbs }
3848b8a9b1dSJustin T. Gibbs 
3858b8a9b1dSJustin T. Gibbs static int
38689c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3878b8a9b1dSJustin T. Gibbs {
3888b8a9b1dSJustin T. Gibbs 
3898b8a9b1dSJustin T. Gibbs 	return(0);
3908b8a9b1dSJustin T. Gibbs }
3918b8a9b1dSJustin T. Gibbs 
3922b83592fSScott Long /*
3932b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3942b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3952b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3962b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3972b83592fSScott Long  */
3988b8a9b1dSJustin T. Gibbs static int
39989c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
4008b8a9b1dSJustin T. Gibbs {
4012b83592fSScott Long 	int error;
4028b8a9b1dSJustin T. Gibbs 
40325a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
404f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
40525a2902cSScott Long 	}
40625a2902cSScott Long 	return (error);
40725a2902cSScott Long }
40825a2902cSScott Long 
40925a2902cSScott Long static int
41025a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
41125a2902cSScott Long {
41225a2902cSScott Long 	int error;
41325a2902cSScott Long 
4148b8a9b1dSJustin T. Gibbs 	error = 0;
4158b8a9b1dSJustin T. Gibbs 
4168b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4178b8a9b1dSJustin T. Gibbs 	/*
4188b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4198b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4208c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4218c7a96c5SScott Long 	 * in the CAM spec.
4228b8a9b1dSJustin T. Gibbs 	 */
4238b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4248b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4258b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4262b83592fSScott Long 		struct cam_eb *bus;
4278b8a9b1dSJustin T. Gibbs 
4288b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4298fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4308fc77fffSConrad Meyer 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
4318fc77fffSConrad Meyer 			inccb->csio.bio = NULL;
4328fc77fffSConrad Meyer #endif
4338b8a9b1dSJustin T. Gibbs 
434a0bbf9e0SMark Johnston 		if (inccb->ccb_h.flags & CAM_UNLOCKED)
435a0bbf9e0SMark Johnston 			return (EINVAL);
436a0bbf9e0SMark Johnston 
4372b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4380e85f214SMatt Jacob 		if (bus == NULL)
4390e85f214SMatt Jacob 			return (EINVAL);
4400e85f214SMatt Jacob 
4410e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4420e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4430e85f214SMatt Jacob 		case XPT_RESET_BUS:
4440e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4450e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4460e85f214SMatt Jacob 				xpt_release_bus(bus);
4470e85f214SMatt Jacob 				return (EINVAL);
4480e85f214SMatt Jacob 			}
4490e85f214SMatt Jacob 			break;
4500e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4510e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4520e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4530e85f214SMatt Jacob 				xpt_release_bus(bus);
4540e85f214SMatt Jacob 				return (EINVAL);
4550e85f214SMatt Jacob 			}
4560e85f214SMatt Jacob 			break;
4570e85f214SMatt Jacob 		default:
4582b83592fSScott Long 			break;
4592b83592fSScott Long 		}
4602b83592fSScott Long 
4618b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4628b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4638b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4648c7a96c5SScott Long 		case XPT_PATH_INQ:
4658fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4668b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4670e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4688b8a9b1dSJustin T. Gibbs 
4698008a935SScott Long 			ccb = xpt_alloc_ccb();
4702b83592fSScott Long 
4718b8a9b1dSJustin T. Gibbs 			/*
4728b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4738b8a9b1dSJustin T. Gibbs 			 * user passed in.
4748b8a9b1dSJustin T. Gibbs 			 */
475e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4768b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4778b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4788b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4798b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4808b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4818b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4828b8a9b1dSJustin T. Gibbs 				break;
4838b8a9b1dSJustin T. Gibbs 			}
4848b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4858b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4868b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4878b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
488227d67aaSAlexander Motin 			xpt_path_lock(ccb->ccb_h.path);
4898b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
490227d67aaSAlexander Motin 			xpt_path_unlock(ccb->ccb_h.path);
4918b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4928b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4938b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4948b8a9b1dSJustin T. Gibbs 			break;
4958b8a9b1dSJustin T. Gibbs 
4968b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4978b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4988b8a9b1dSJustin T. Gibbs 
4998b8a9b1dSJustin T. Gibbs 			/*
500aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
5018b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
5028b8a9b1dSJustin T. Gibbs 			 */
5038b8a9b1dSJustin T. Gibbs 
5048b8a9b1dSJustin T. Gibbs 			/*
5058b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
5068b8a9b1dSJustin T. Gibbs 			 * user passed in.
5078b8a9b1dSJustin T. Gibbs 			 */
508e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
5098b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
5108b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
5118b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
5128b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
5138b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5148b8a9b1dSJustin T. Gibbs 				break;
5158b8a9b1dSJustin T. Gibbs 			}
5168b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5178b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5188b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5198b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5208b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5218b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5228b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
5238b8a9b1dSJustin T. Gibbs 			break;
5248b8a9b1dSJustin T. Gibbs 
5258b8a9b1dSJustin T. Gibbs 		}
5268b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5278b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
52859190eaaSKenneth D. Merry 			struct cam_path *old_path;
5298b8a9b1dSJustin T. Gibbs 
5308b8a9b1dSJustin T. Gibbs 			/*
5318b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5328b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5338b8a9b1dSJustin T. Gibbs 			 */
534dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
535dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5368b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5378b8a9b1dSJustin T. Gibbs 				break;
5388b8a9b1dSJustin T. Gibbs 			}
53959190eaaSKenneth D. Merry 
54059190eaaSKenneth D. Merry 			/*
54159190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
54259190eaaSKenneth D. Merry 			 * something in particular.
54359190eaaSKenneth D. Merry 			 */
54459190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
54559190eaaSKenneth D. Merry 
54659190eaaSKenneth D. Merry 			/*
54759190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
54859190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
54959190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
55059190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
55159190eaaSKenneth D. Merry 			 */
55259190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
55359190eaaSKenneth D. Merry 
5548b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5558b8a9b1dSJustin T. Gibbs 
5568b8a9b1dSJustin T. Gibbs 			/*
5578b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5588b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5598b8a9b1dSJustin T. Gibbs 			 */
560de239312SAlexander Motin 			error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
5618b8a9b1dSJustin T. Gibbs 
56259190eaaSKenneth D. Merry 			if (error) {
56359190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5648b8a9b1dSJustin T. Gibbs 				break;
56559190eaaSKenneth D. Merry 			}
5668b8a9b1dSJustin T. Gibbs 
5678b8a9b1dSJustin T. Gibbs 			/*
5688b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5698b8a9b1dSJustin T. Gibbs 			 */
5708b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
5718b8a9b1dSJustin T. Gibbs 
5728b8a9b1dSJustin T. Gibbs 			/*
5738b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5748b8a9b1dSJustin T. Gibbs 			 */
5758b8a9b1dSJustin T. Gibbs 			cam_periph_unmapmem(inccb, &mapinfo);
5768b8a9b1dSJustin T. Gibbs 
57759190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
57859190eaaSKenneth D. Merry 
5798b8a9b1dSJustin T. Gibbs 			error = 0;
5808b8a9b1dSJustin T. Gibbs 			break;
5818b8a9b1dSJustin T. Gibbs 		}
5828b8a9b1dSJustin T. Gibbs 		default:
5838fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5848b8a9b1dSJustin T. Gibbs 			break;
5858b8a9b1dSJustin T. Gibbs 		}
586daddc001SScott Long 		xpt_release_bus(bus);
5878b8a9b1dSJustin T. Gibbs 		break;
5888b8a9b1dSJustin T. Gibbs 	}
5898b8a9b1dSJustin T. Gibbs 	/*
5908b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5918b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5928b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5938b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5948b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5958b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5968b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5978b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
5988b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
5998b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
60077dc25ccSScott Long 	 * we do it with lock protection.
6018b8a9b1dSJustin T. Gibbs 	 *
6028b8a9b1dSJustin T. Gibbs 	 */
6038b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
6048b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
6058b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
6068b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
6078b8a9b1dSJustin T. Gibbs 		char   *name;
6083393f8daSKenneth D. Merry 		u_int unit;
609621a60d4SKenneth D. Merry 		int base_periph_found;
6108b8a9b1dSJustin T. Gibbs 
6118b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6128b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
6138b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
614621a60d4SKenneth D. Merry 		base_periph_found = 0;
6158fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
6168fc77fffSConrad Meyer 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
6178fc77fffSConrad Meyer 			ccb->csio.bio = NULL;
6188fc77fffSConrad Meyer #endif
619621a60d4SKenneth D. Merry 
6208b8a9b1dSJustin T. Gibbs 		/*
6218b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6228b8a9b1dSJustin T. Gibbs 		 * driver name.
6238b8a9b1dSJustin T. Gibbs 		 */
6248b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6258b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6268b8a9b1dSJustin T. Gibbs 			break;
6278b8a9b1dSJustin T. Gibbs 		}
6288b8a9b1dSJustin T. Gibbs 
6298b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6309a7c2696SAlexander Motin 		xpt_lock_buses();
6318b8a9b1dSJustin T. Gibbs 
6328b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6330b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6348b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6358b8a9b1dSJustin T. Gibbs 				break;
6368b8a9b1dSJustin T. Gibbs 
6378b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6389a7c2696SAlexander Motin 			xpt_unlock_buses();
6398b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6408b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6418b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6428b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6438b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6448b8a9b1dSJustin T. Gibbs 			break;
6458b8a9b1dSJustin T. Gibbs 		}
6468b8a9b1dSJustin T. Gibbs 
6478b8a9b1dSJustin T. Gibbs 		/*
6488b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6498b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6508b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6518b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6528b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6538b8a9b1dSJustin T. Gibbs 		 */
6548b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6558b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
6568b8a9b1dSJustin T. Gibbs 
657a29779e8SAlexander Motin 			if (periph->unit_number == unit)
6588b8a9b1dSJustin T. Gibbs 				break;
6598b8a9b1dSJustin T. Gibbs 		}
6608b8a9b1dSJustin T. Gibbs 		/*
6618b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6628b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6638b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6648b8a9b1dSJustin T. Gibbs 		 */
6658b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6668b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6678b8a9b1dSJustin T. Gibbs 			int i;
6688b8a9b1dSJustin T. Gibbs 
669621a60d4SKenneth D. Merry 			base_periph_found = 1;
6708b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
671fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6728b8a9b1dSJustin T. Gibbs 			     periph != NULL;
673fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6748b8a9b1dSJustin T. Gibbs 				/*
6758b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6768b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6778b8a9b1dSJustin T. Gibbs 				 */
6788b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6798b8a9b1dSJustin T. Gibbs 					/*
6808b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
6818b8a9b1dSJustin T. Gibbs 					 */
682b0f662feSAlan Somers 					strlcpy(ccb->cgdl.periph_name,
683b0f662feSAlan Somers 					       periph->periph_name,
684b0f662feSAlan Somers 					       sizeof(ccb->cgdl.periph_name));
6858b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
6868b8a9b1dSJustin T. Gibbs 						periph->unit_number;
687fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
6888b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6898b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
6908b8a9b1dSJustin T. Gibbs 					else
6918b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6928b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
6938b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
6948b8a9b1dSJustin T. Gibbs 						device->generation;
6958b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
6968b8a9b1dSJustin T. Gibbs 					/*
6978b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
6988b8a9b1dSJustin T. Gibbs 					 * that the user may want.
6998b8a9b1dSJustin T. Gibbs 					 */
7008b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
7018b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
7028b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
7038b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
7048b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
7058b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
7068b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
7078b8a9b1dSJustin T. Gibbs 					break;
7088b8a9b1dSJustin T. Gibbs 				}
7098b8a9b1dSJustin T. Gibbs 			}
7108b8a9b1dSJustin T. Gibbs 		}
7118b8a9b1dSJustin T. Gibbs 
7128b8a9b1dSJustin T. Gibbs 		/*
7138b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
7148b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
7158b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7168b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7178b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7188b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7198b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7208b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7218b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7228b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7238b8a9b1dSJustin T. Gibbs 		 */
7248b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7258b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7268b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7278b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7288b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7298b8a9b1dSJustin T. Gibbs 			error = ENOENT;
730621a60d4SKenneth D. Merry 			/*
731621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
732621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
733621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
734621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
735621a60d4SKenneth D. Merry 			 * kernel.
736621a60d4SKenneth D. Merry 			 */
737621a60d4SKenneth D. Merry 			if (base_periph_found == 1) {
738621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
739621a60d4SKenneth D. Merry 				       "kernel\n");
740935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
741621a60d4SKenneth D. Merry 				       "your kernel config file\n");
742621a60d4SKenneth D. Merry 			}
7438b8a9b1dSJustin T. Gibbs 		}
7449a7c2696SAlexander Motin 		xpt_unlock_buses();
7458b8a9b1dSJustin T. Gibbs 		break;
7468b8a9b1dSJustin T. Gibbs 		}
7478b8a9b1dSJustin T. Gibbs 	default:
7488b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7498b8a9b1dSJustin T. Gibbs 		break;
7508b8a9b1dSJustin T. Gibbs 	}
7518b8a9b1dSJustin T. Gibbs 
7528b8a9b1dSJustin T. Gibbs 	return(error);
7538b8a9b1dSJustin T. Gibbs }
7548b8a9b1dSJustin T. Gibbs 
75574bd1c10SNick Hibma static int
75674bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
75774bd1c10SNick Hibma {
7582b83592fSScott Long 	int error;
7592b83592fSScott Long 
7602b83592fSScott Long 	switch (what) {
7612b83592fSScott Long 	case MOD_LOAD:
7622b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7632b83592fSScott Long 			return (error);
7642b83592fSScott Long 		break;
7652b83592fSScott Long 	case MOD_UNLOAD:
76674bd1c10SNick Hibma 		return EBUSY;
7672b83592fSScott Long 	default:
7683e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
76974bd1c10SNick Hibma 	}
77074bd1c10SNick Hibma 
77174bd1c10SNick Hibma 	return 0;
77274bd1c10SNick Hibma }
77374bd1c10SNick Hibma 
77408f13879SWarner Losh static struct xpt_proto *
77508f13879SWarner Losh xpt_proto_find(cam_proto proto)
77608f13879SWarner Losh {
77708f13879SWarner Losh 	struct xpt_proto **pp;
77808f13879SWarner Losh 
77908f13879SWarner Losh 	SET_FOREACH(pp, cam_xpt_proto_set) {
78008f13879SWarner Losh 		if ((*pp)->proto == proto)
78108f13879SWarner Losh 			return *pp;
78208f13879SWarner Losh 	}
78308f13879SWarner Losh 
78408f13879SWarner Losh 	return NULL;
78508f13879SWarner Losh }
78608f13879SWarner Losh 
78783c5d981SAlexander Motin static void
78883c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
78983c5d981SAlexander Motin {
79083c5d981SAlexander Motin 
79183c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
79283c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
79383c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
79483c5d981SAlexander Motin 	} else {
79583c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
79683c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
79783c5d981SAlexander Motin 	}
79883c5d981SAlexander Motin 	xpt_release_boot();
79983c5d981SAlexander Motin }
80083c5d981SAlexander Motin 
8019e6461a2SMatt Jacob /* thread to handle bus rescans */
8029e6461a2SMatt Jacob static void
8039e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
8049e6461a2SMatt Jacob {
8059e6461a2SMatt Jacob 	union ccb	*ccb;
806227d67aaSAlexander Motin 	struct cam_path	 path;
8072b83592fSScott Long 
8082b83592fSScott Long 	xpt_lock_buses();
80983c5d981SAlexander Motin 	for (;;) {
8105a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
8112b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
8123710ae64SAlexander Motin 			       "-", 0);
81383c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
81483c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
8152b83592fSScott Long 			xpt_unlock_buses();
8162b83592fSScott Long 
817227d67aaSAlexander Motin 			/*
818227d67aaSAlexander Motin 			 * Since lock can be dropped inside and path freed
819227d67aaSAlexander Motin 			 * by completion callback even before return here,
820227d67aaSAlexander Motin 			 * take our own path copy for reference.
821227d67aaSAlexander Motin 			 */
822227d67aaSAlexander Motin 			xpt_copy_path(&path, ccb->ccb_h.path);
823227d67aaSAlexander Motin 			xpt_path_lock(&path);
82483c5d981SAlexander Motin 			xpt_action(ccb);
825227d67aaSAlexander Motin 			xpt_path_unlock(&path);
826227d67aaSAlexander Motin 			xpt_release_path(&path);
82783c5d981SAlexander Motin 
82883c5d981SAlexander Motin 			xpt_lock_buses();
8299e6461a2SMatt Jacob 		}
8309e6461a2SMatt Jacob 	}
8319e6461a2SMatt Jacob }
8329e6461a2SMatt Jacob 
8339e6461a2SMatt Jacob void
8349e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8359e6461a2SMatt Jacob {
8369e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8372b83592fSScott Long 
83883c5d981SAlexander Motin 	/* Prepare request */
8390e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
840411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
84183c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8420e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8430e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8440e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8450e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8460e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
84783c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8480e85f214SMatt Jacob 	else {
8490e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8500e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8510e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8520e85f214SMatt Jacob 		return;
8530e85f214SMatt Jacob 	}
85469be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
85569be012fSWarner Losh 	    ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
85669be012fSWarner Losh  		xpt_action_name(ccb->ccb_h.func_code)));
85769be012fSWarner Losh 
85883c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
85983c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
86083c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
86183c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8622b83592fSScott Long 	xpt_lock_buses();
86383c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8642b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8659e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8665a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8672b83592fSScott Long 				xpt_unlock_buses();
8689e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8699e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8709e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8719e6461a2SMatt Jacob 				return;
8729e6461a2SMatt Jacob 			}
8739e6461a2SMatt Jacob 		}
87483c5d981SAlexander Motin 	}
8752b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
876a4876fbfSAlexander Motin 	xpt_hold_boot_locked();
8772b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8782b83592fSScott Long 	xpt_unlock_buses();
8799e6461a2SMatt Jacob }
8809e6461a2SMatt Jacob 
8818b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8822b83592fSScott Long static int
8839e6461a2SMatt Jacob xpt_init(void *dummy)
8848b8a9b1dSJustin T. Gibbs {
8858b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8868b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
887434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8888b8a9b1dSJustin T. Gibbs 	cam_status status;
889227d67aaSAlexander Motin 	int error, i;
8908b8a9b1dSJustin T. Gibbs 
8912b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8922b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8932b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8942b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8958b8a9b1dSJustin T. Gibbs 
896daa5487fSAlexander Motin 	mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
897227d67aaSAlexander Motin 	xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
898227d67aaSAlexander Motin 	    taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
899ef3cf714SScott Long 
9006b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
9016b156f61SSean Bruno 	/*
9026b156f61SSean Bruno 	 * Override this value at compile time to assist our users
9036b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
9046b156f61SSean Bruno 	 */
9056b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
9066b156f61SSean Bruno #endif
907a4876fbfSAlexander Motin 
9088b8a9b1dSJustin T. Gibbs 	/*
9091ffe5851SPedro F. Giffuni 	 * The xpt layer is, itself, the equivalent of a SIM.
9108b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
911db4fcadfSConrad Meyer 	 * give decent parallelism when we probe buses and
9128b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
9138b8a9b1dSJustin T. Gibbs 	 */
914434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
915434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
916434bbf6eSJustin T. Gibbs 				xptpoll,
917434bbf6eSJustin T. Gibbs 				"xpt",
918434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
919434bbf6eSJustin T. Gibbs 				/*unit*/0,
9207e8baf37SAlexander Motin 				/*mtx*/NULL,
921434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
922434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
923434bbf6eSJustin T. Gibbs 				devq);
9242b83592fSScott Long 	if (xpt_sim == NULL)
9252b83592fSScott Long 		return (ENOMEM);
9268b8a9b1dSJustin T. Gibbs 
927b50569b7SScott Long 	if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
928a2821e04SMatt Jacob 		printf("xpt_init: xpt_bus_register failed with status %#x,"
929df826980SMatt Jacob 		       " failing attach\n", status);
9302b83592fSScott Long 		return (EINVAL);
931df826980SMatt Jacob 	}
9328b8a9b1dSJustin T. Gibbs 
9338b8a9b1dSJustin T. Gibbs 	/*
9348b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9351ffe5851SPedro F. Giffuni 	 * the equivalent of a peripheral driver.  Allocate
9368b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9378b8a9b1dSJustin T. Gibbs 	 */
9388b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9398b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9408b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
9418b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9428b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9432b83592fSScott Long 		return (EINVAL);
9448b8a9b1dSJustin T. Gibbs 	}
945daa5487fSAlexander Motin 	xpt_path_lock(path);
946ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9472b83592fSScott Long 			 path, NULL, 0, xpt_sim);
948daa5487fSAlexander Motin 	xpt_path_unlock(path);
9498b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
950daa5487fSAlexander Motin 
951227d67aaSAlexander Motin 	if (cam_num_doneqs < 1)
952227d67aaSAlexander Motin 		cam_num_doneqs = 1 + mp_ncpus / 6;
953227d67aaSAlexander Motin 	else if (cam_num_doneqs > MAXCPU)
954227d67aaSAlexander Motin 		cam_num_doneqs = MAXCPU;
955227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
956227d67aaSAlexander Motin 		mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
957227d67aaSAlexander Motin 		    MTX_DEF);
958227d67aaSAlexander Motin 		STAILQ_INIT(&cam_doneqs[i].cam_doneq);
959227d67aaSAlexander Motin 		error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
960227d67aaSAlexander Motin 		    &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
961227d67aaSAlexander Motin 		if (error != 0) {
962227d67aaSAlexander Motin 			cam_num_doneqs = i;
963227d67aaSAlexander Motin 			break;
964227d67aaSAlexander Motin 		}
965227d67aaSAlexander Motin 	}
966227d67aaSAlexander Motin 	if (cam_num_doneqs < 1) {
967227d67aaSAlexander Motin 		printf("xpt_init: Cannot init completion queues "
968227d67aaSAlexander Motin 		       "- failing attach\n");
969227d67aaSAlexander Motin 		return (ENOMEM);
970227d67aaSAlexander Motin 	}
971a4876fbfSAlexander Motin 
9728b8a9b1dSJustin T. Gibbs 	/*
9738b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9748b8a9b1dSJustin T. Gibbs 	 */
975a4876fbfSAlexander Motin 	config_intrhook_oneshot(xpt_config, NULL);
9768b8a9b1dSJustin T. Gibbs 
9772b83592fSScott Long 	return (0);
9788b8a9b1dSJustin T. Gibbs }
9798b8a9b1dSJustin T. Gibbs 
9808b8a9b1dSJustin T. Gibbs static cam_status
9818b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9828b8a9b1dSJustin T. Gibbs {
9832b83592fSScott Long 	struct cam_sim *xpt_sim;
9842b83592fSScott Long 
9858b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9868b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9878b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9888b8a9b1dSJustin T. Gibbs 	}
9898b8a9b1dSJustin T. Gibbs 
9902b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9912b83592fSScott Long 	xpt_sim->softc = periph;
9928b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
9932b83592fSScott Long 	periph->softc = NULL;
9948b8a9b1dSJustin T. Gibbs 
9958b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
9968b8a9b1dSJustin T. Gibbs }
9978b8a9b1dSJustin T. Gibbs 
9988b8a9b1dSJustin T. Gibbs int32_t
9998b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
10008b8a9b1dSJustin T. Gibbs {
10018b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10028b8a9b1dSJustin T. Gibbs 	int32_t	 status;
10038b8a9b1dSJustin T. Gibbs 
1004227d67aaSAlexander Motin 	TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
10058b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10068b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
10078b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1008227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10098b8a9b1dSJustin T. Gibbs 		device->generation++;
1010227d67aaSAlexander Motin 		SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1011227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1012636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10138b8a9b1dSJustin T. Gibbs 	}
10148b8a9b1dSJustin T. Gibbs 
10158b8a9b1dSJustin T. Gibbs 	return (status);
10168b8a9b1dSJustin T. Gibbs }
10178b8a9b1dSJustin T. Gibbs 
10188b8a9b1dSJustin T. Gibbs void
1019a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph)
10208b8a9b1dSJustin T. Gibbs {
10218b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10228b8a9b1dSJustin T. Gibbs 
10238b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10248b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1025227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10268b8a9b1dSJustin T. Gibbs 		device->generation++;
1027227d67aaSAlexander Motin 		SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1028227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1029636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10308b8a9b1dSJustin T. Gibbs 	}
10318b8a9b1dSJustin T. Gibbs }
10328b8a9b1dSJustin T. Gibbs 
10333393f8daSKenneth D. Merry 
10343393f8daSKenneth D. Merry void
10353393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10363393f8daSKenneth D. Merry {
103757079b17SAlexander Motin 	struct	cam_path *path = periph->path;
103808f13879SWarner Losh 	struct  xpt_proto *proto;
10393393f8daSKenneth D. Merry 
1040227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
10418d36a71bSAlexander Motin 	periph->flags |= CAM_PERIPH_ANNOUNCED;
104268153f43SScott Long 
1043abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10443393f8daSKenneth D. Merry 	       periph->periph_name, periph->unit_number,
10453393f8daSKenneth D. Merry 	       path->bus->sim->sim_name,
10463393f8daSKenneth D. Merry 	       path->bus->sim->unit_number,
10473393f8daSKenneth D. Merry 	       path->bus->sim->bus_id,
1048ad413009SAlexander Motin 	       path->bus->path_id,
10493393f8daSKenneth D. Merry 	       path->target->target_id,
1050abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
10513393f8daSKenneth D. Merry 	printf("%s%d: ", periph->periph_name, periph->unit_number);
105208f13879SWarner Losh 	proto = xpt_proto_find(path->device->protocol);
105308f13879SWarner Losh 	if (proto)
105408f13879SWarner Losh 		proto->ops->announce(path->device);
105552c9ce25SScott Long 	else
105608f13879SWarner Losh 		printf("%s%d: Unknown protocol device %d\n",
105708f13879SWarner Losh 		    periph->periph_name, periph->unit_number,
105808f13879SWarner Losh 		    path->device->protocol);
1059aa93041dSAlexander Motin 	if (path->device->serial_num_len > 0) {
10603393f8daSKenneth D. Merry 		/* Don't wrap the screen  - print only the first 60 chars */
10613393f8daSKenneth D. Merry 		printf("%s%d: Serial Number %.60s\n", periph->periph_name,
10623393f8daSKenneth D. Merry 		       periph->unit_number, path->device->serial_num);
10633393f8daSKenneth D. Merry 	}
106457079b17SAlexander Motin 	/* Announce transport details. */
106508f13879SWarner Losh 	path->bus->xport->ops->announce(periph);
106657079b17SAlexander Motin 	/* Announce command queueing. */
10673393f8daSKenneth D. Merry 	if (path->device->inq_flags & SID_CmdQue
10683393f8daSKenneth D. Merry 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10690aacc535SAlexander Motin 		printf("%s%d: Command Queueing enabled\n",
10703393f8daSKenneth D. Merry 		       periph->periph_name, periph->unit_number);
10713393f8daSKenneth D. Merry 	}
107257079b17SAlexander Motin 	/* Announce caller's details if they've passed in. */
10733393f8daSKenneth D. Merry 	if (announce_string != NULL)
10743393f8daSKenneth D. Merry 		printf("%s%d: %s\n", periph->periph_name,
10753393f8daSKenneth D. Merry 		       periph->unit_number, announce_string);
10763393f8daSKenneth D. Merry }
10778b8a9b1dSJustin T. Gibbs 
10786fb5c84eSSteven Hartland void
10795d01277fSScott Long xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
10805d01277fSScott Long     char *announce_string)
10815d01277fSScott Long {
10825d01277fSScott Long 	struct	cam_path *path = periph->path;
10835d01277fSScott Long 	struct  xpt_proto *proto;
10845d01277fSScott Long 
10855d01277fSScott Long 	cam_periph_assert(periph, MA_OWNED);
10865d01277fSScott Long 	periph->flags |= CAM_PERIPH_ANNOUNCED;
10875d01277fSScott Long 
10885d01277fSScott Long 	/* Fall back to the non-sbuf method if necessary */
10895d01277fSScott Long 	if (xsoftc.announce_nosbuf != 0) {
10905d01277fSScott Long 		xpt_announce_periph(periph, announce_string);
10915d01277fSScott Long 		return;
10925d01277fSScott Long 	}
10935d01277fSScott Long 	proto = xpt_proto_find(path->device->protocol);
10945d01277fSScott Long 	if (((proto != NULL) && (proto->ops->announce_sbuf == NULL)) ||
10955d01277fSScott Long 	    (path->bus->xport->ops->announce_sbuf == NULL)) {
10965d01277fSScott Long 		xpt_announce_periph(periph, announce_string);
10975d01277fSScott Long 		return;
10985d01277fSScott Long 	}
10995d01277fSScott Long 
11005d01277fSScott Long 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
11015d01277fSScott Long 	    periph->periph_name, periph->unit_number,
11025d01277fSScott Long 	    path->bus->sim->sim_name,
11035d01277fSScott Long 	    path->bus->sim->unit_number,
11045d01277fSScott Long 	    path->bus->sim->bus_id,
11055d01277fSScott Long 	    path->bus->path_id,
11065d01277fSScott Long 	    path->target->target_id,
11075d01277fSScott Long 	    (uintmax_t)path->device->lun_id);
11085d01277fSScott Long 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
11095d01277fSScott Long 
11105d01277fSScott Long 	if (proto)
11115d01277fSScott Long 		proto->ops->announce_sbuf(path->device, sb);
11125d01277fSScott Long 	else
11135d01277fSScott Long 		sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
11145d01277fSScott Long 		    periph->periph_name, periph->unit_number,
11155d01277fSScott Long 		    path->device->protocol);
11165d01277fSScott Long 	if (path->device->serial_num_len > 0) {
11175d01277fSScott Long 		/* Don't wrap the screen  - print only the first 60 chars */
11185d01277fSScott Long 		sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
11195d01277fSScott Long 		    periph->periph_name, periph->unit_number,
11205d01277fSScott Long 		    path->device->serial_num);
11215d01277fSScott Long 	}
11225d01277fSScott Long 	/* Announce transport details. */
11235d01277fSScott Long 	path->bus->xport->ops->announce_sbuf(periph, sb);
11245d01277fSScott Long 	/* Announce command queueing. */
11255d01277fSScott Long 	if (path->device->inq_flags & SID_CmdQue
11265d01277fSScott Long 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
11275d01277fSScott Long 		sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
11285d01277fSScott Long 		    periph->periph_name, periph->unit_number);
11295d01277fSScott Long 	}
11305d01277fSScott Long 	/* Announce caller's details if they've passed in. */
11315d01277fSScott Long 	if (announce_string != NULL)
11325d01277fSScott Long 		sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
11335d01277fSScott Long 		    periph->unit_number, announce_string);
11345d01277fSScott Long }
11355d01277fSScott Long 
11365d01277fSScott Long void
11376fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
11386fb5c84eSSteven Hartland {
11396fb5c84eSSteven Hartland 	if (quirks != 0) {
11406fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
11416fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
11426fb5c84eSSteven Hartland 	}
11436fb5c84eSSteven Hartland }
11446fb5c84eSSteven Hartland 
11458d36a71bSAlexander Motin void
11465d01277fSScott Long xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
11475d01277fSScott Long 			 int quirks, char *bit_string)
11485d01277fSScott Long {
11495d01277fSScott Long 	if (xsoftc.announce_nosbuf != 0) {
11505d01277fSScott Long 		xpt_announce_quirks(periph, quirks, bit_string);
11515d01277fSScott Long 		return;
11525d01277fSScott Long 	}
11535d01277fSScott Long 
11545d01277fSScott Long 	if (quirks != 0) {
11555d01277fSScott Long 		sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
11565d01277fSScott Long 		    periph->unit_number, quirks, bit_string);
11575d01277fSScott Long 	}
11585d01277fSScott Long }
11595d01277fSScott Long 
11605d01277fSScott Long void
11618d36a71bSAlexander Motin xpt_denounce_periph(struct cam_periph *periph)
11628d36a71bSAlexander Motin {
11638d36a71bSAlexander Motin 	struct	cam_path *path = periph->path;
116408f13879SWarner Losh 	struct  xpt_proto *proto;
11658d36a71bSAlexander Motin 
1166227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
1167abe83505SNathan Whitehorn 	printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
11688d36a71bSAlexander Motin 	       periph->periph_name, periph->unit_number,
11698d36a71bSAlexander Motin 	       path->bus->sim->sim_name,
11708d36a71bSAlexander Motin 	       path->bus->sim->unit_number,
11718d36a71bSAlexander Motin 	       path->bus->sim->bus_id,
11728d36a71bSAlexander Motin 	       path->bus->path_id,
11738d36a71bSAlexander Motin 	       path->target->target_id,
1174abe83505SNathan Whitehorn 	       (uintmax_t)path->device->lun_id);
11758d36a71bSAlexander Motin 	printf("%s%d: ", periph->periph_name, periph->unit_number);
117608f13879SWarner Losh 	proto = xpt_proto_find(path->device->protocol);
117708f13879SWarner Losh 	if (proto)
117808f13879SWarner Losh 		proto->ops->denounce(path->device);
11798d36a71bSAlexander Motin 	else
118008f13879SWarner Losh 		printf("%s%d: Unknown protocol device %d\n",
118108f13879SWarner Losh 		    periph->periph_name, periph->unit_number,
118208f13879SWarner Losh 		    path->device->protocol);
11838d36a71bSAlexander Motin 	if (path->device->serial_num_len > 0)
11848d36a71bSAlexander Motin 		printf(" s/n %.60s", path->device->serial_num);
11858d36a71bSAlexander Motin 	printf(" detached\n");
11868d36a71bSAlexander Motin }
11878d36a71bSAlexander Motin 
11885d01277fSScott Long void
11895d01277fSScott Long xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
11905d01277fSScott Long {
11915d01277fSScott Long 	struct cam_path *path = periph->path;
11925d01277fSScott Long 	struct xpt_proto *proto;
11935d01277fSScott Long 
11945d01277fSScott Long 	cam_periph_assert(periph, MA_OWNED);
11955d01277fSScott Long 
11965d01277fSScott Long 	/* Fall back to the non-sbuf method if necessary */
11975d01277fSScott Long 	if (xsoftc.announce_nosbuf != 0) {
11985d01277fSScott Long 		xpt_denounce_periph(periph);
11995d01277fSScott Long 		return;
12005d01277fSScott Long 	}
12015d01277fSScott Long 	proto = xpt_proto_find(path->device->protocol);
12025d01277fSScott Long 	if ((proto != NULL) && (proto->ops->denounce_sbuf == NULL)) {
12035d01277fSScott Long 		xpt_denounce_periph(periph);
12045d01277fSScott Long 		return;
12055d01277fSScott Long 	}
12065d01277fSScott Long 
12075d01277fSScott Long 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
12085d01277fSScott Long 	    periph->periph_name, periph->unit_number,
12095d01277fSScott Long 	    path->bus->sim->sim_name,
12105d01277fSScott Long 	    path->bus->sim->unit_number,
12115d01277fSScott Long 	    path->bus->sim->bus_id,
12125d01277fSScott Long 	    path->bus->path_id,
12135d01277fSScott Long 	    path->target->target_id,
12145d01277fSScott Long 	    (uintmax_t)path->device->lun_id);
12155d01277fSScott Long 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
12165d01277fSScott Long 
12175d01277fSScott Long 	if (proto)
12185d01277fSScott Long 		proto->ops->denounce_sbuf(path->device, sb);
12195d01277fSScott Long 	else
12205d01277fSScott Long 		sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
12215d01277fSScott Long 		    periph->periph_name, periph->unit_number,
12225d01277fSScott Long 		    path->device->protocol);
12235d01277fSScott Long 	if (path->device->serial_num_len > 0)
12245d01277fSScott Long 		sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
12255d01277fSScott Long 	sbuf_printf(sb, " detached\n");
12265d01277fSScott Long }
12278d36a71bSAlexander Motin 
12283501942bSJustin T. Gibbs int
12293501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
12303501942bSJustin T. Gibbs {
123100d72d57SAlexander Motin 	int ret = -1, l, o;
12323501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
12330feb46b0SScott Long 	struct scsi_vpd_device_id *did;
1234ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
12353501942bSJustin T. Gibbs 
1236227d67aaSAlexander Motin 	xpt_path_assert(path, MA_OWNED);
12376884b662SAlexander Motin 
12383501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
12393501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
12403501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1241ab4327bbSAlexander Motin 	cdai.flags = CDAI_FLAG_NONE;
12423501942bSJustin T. Gibbs 	cdai.bufsiz = len;
124335a9ffc3SAlexander Motin 	cdai.buf = buf;
12443501942bSJustin T. Gibbs 
12453501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
12463501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
12473501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
12483501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
124940f27d7cSAlexander Motin 	else if (strcmp(attr, "GEOM::lunid") == 0 ||
125040f27d7cSAlexander Motin 		 strcmp(attr, "GEOM::lunname") == 0) {
1251ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1252ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
125335a9ffc3SAlexander Motin 		cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT);
12543501942bSJustin T. Gibbs 		if (cdai.buf == NULL) {
12553501942bSJustin T. Gibbs 			ret = ENOMEM;
12563501942bSJustin T. Gibbs 			goto out;
12573501942bSJustin T. Gibbs 		}
125835a9ffc3SAlexander Motin 	} else
125935a9ffc3SAlexander Motin 		goto out;
126035a9ffc3SAlexander Motin 
12613501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
12623501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
12633501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
12643501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
12653501942bSJustin T. Gibbs 		goto out;
12660feb46b0SScott Long 	switch(cdai.buftype) {
12670feb46b0SScott Long 	case CDAI_TYPE_SCSI_DEVID:
12680feb46b0SScott Long 		did = (struct scsi_vpd_device_id *)cdai.buf;
126940f27d7cSAlexander Motin 		if (strcmp(attr, "GEOM::lunid") == 0) {
12700feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
12710feb46b0SScott Long 			    scsi_devid_is_lun_naa);
1272ccba7102SAlexander Motin 			if (idd == NULL)
12730feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12740feb46b0SScott Long 				    scsi_devid_is_lun_eui64);
127500d72d57SAlexander Motin 			if (idd == NULL)
12760feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12770feb46b0SScott Long 				    scsi_devid_is_lun_uuid);
127800d72d57SAlexander Motin 			if (idd == NULL)
12790feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12800feb46b0SScott Long 				    scsi_devid_is_lun_md5);
128140f27d7cSAlexander Motin 		} else
128240f27d7cSAlexander Motin 			idd = NULL;
12830feb46b0SScott Long 
1284ccba7102SAlexander Motin 		if (idd == NULL)
12850feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
12860feb46b0SScott Long 			    scsi_devid_is_lun_t10);
1287ccba7102SAlexander Motin 		if (idd == NULL)
12880feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
12890feb46b0SScott Long 			    scsi_devid_is_lun_name);
1290ccba7102SAlexander Motin 		if (idd == NULL)
12910feb46b0SScott Long 			break;
12920feb46b0SScott Long 
1293ccba7102SAlexander Motin 		ret = 0;
12940feb46b0SScott Long 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
12950feb46b0SScott Long 		    SVPD_ID_CODESET_ASCII) {
1296fa91cabfSAlexander Motin 			if (idd->length < len) {
1297fa91cabfSAlexander Motin 				for (l = 0; l < idd->length; l++)
1298fa91cabfSAlexander Motin 					buf[l] = idd->identifier[l] ?
1299fa91cabfSAlexander Motin 					    idd->identifier[l] : ' ';
1300fa91cabfSAlexander Motin 				buf[l] = 0;
1301fa91cabfSAlexander Motin 			} else
1302fa91cabfSAlexander Motin 				ret = EFAULT;
13030feb46b0SScott Long 			break;
13040feb46b0SScott Long 		}
13050feb46b0SScott Long 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
13060feb46b0SScott Long 		    SVPD_ID_CODESET_UTF8) {
1307ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1308ccba7102SAlexander Motin 			if (l < len) {
1309ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1310ccba7102SAlexander Motin 				buf[l] = 0;
1311ccba7102SAlexander Motin 			} else
1312ccba7102SAlexander Motin 				ret = EFAULT;
13130feb46b0SScott Long 			break;
13140feb46b0SScott Long 		}
13150feb46b0SScott Long 		if ((idd->id_type & SVPD_ID_TYPE_MASK) ==
13160feb46b0SScott Long 		    SVPD_ID_TYPE_UUID && idd->identifier[0] == 0x10) {
13170feb46b0SScott Long 			if ((idd->length - 2) * 2 + 4 >= len) {
13180feb46b0SScott Long 				ret = EFAULT;
13190feb46b0SScott Long 				break;
13200feb46b0SScott Long 			}
132100d72d57SAlexander Motin 			for (l = 2, o = 0; l < idd->length; l++) {
13223157c368SAlexander Motin 				if (l == 6 || l == 8 || l == 10 || l == 12)
13233157c368SAlexander Motin 				    o += sprintf(buf + o, "-");
132400d72d57SAlexander Motin 				o += sprintf(buf + o, "%02x",
132500d72d57SAlexander Motin 				    idd->identifier[l]);
132600d72d57SAlexander Motin 			}
13270feb46b0SScott Long 			break;
13280feb46b0SScott Long 		}
1329ccba7102SAlexander Motin 		if (idd->length * 2 < len) {
1330ccba7102SAlexander Motin 			for (l = 0; l < idd->length; l++)
1331ccba7102SAlexander Motin 				sprintf(buf + l * 2, "%02x",
1332ccba7102SAlexander Motin 				    idd->identifier[l]);
1333ccba7102SAlexander Motin 		} else
1334ccba7102SAlexander Motin 				ret = EFAULT;
13350feb46b0SScott Long 		break;
13360feb46b0SScott Long 	default:
133735a9ffc3SAlexander Motin 		if (cdai.provsiz < len) {
133835a9ffc3SAlexander Motin 			cdai.buf[cdai.provsiz] = 0;
13393501942bSJustin T. Gibbs 			ret = 0;
134035a9ffc3SAlexander Motin 		} else
13413501942bSJustin T. Gibbs 			ret = EFAULT;
13420feb46b0SScott Long 		break;
1343ccba7102SAlexander Motin 	}
13443501942bSJustin T. Gibbs 
13453501942bSJustin T. Gibbs out:
134635a9ffc3SAlexander Motin 	if ((char *)cdai.buf != buf)
13473501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
13483501942bSJustin T. Gibbs 	return ret;
13493501942bSJustin T. Gibbs }
13503501942bSJustin T. Gibbs 
13518b8a9b1dSJustin T. Gibbs static dev_match_ret
13523393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
13538b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
13548b8a9b1dSJustin T. Gibbs {
13558b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1356167e63e3SPedro F. Giffuni 	u_int i;
13578b8a9b1dSJustin T. Gibbs 
13588b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
13598b8a9b1dSJustin T. Gibbs 
13608b8a9b1dSJustin T. Gibbs 	/*
13618b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13628b8a9b1dSJustin T. Gibbs 	 */
13638b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
13648b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
13658b8a9b1dSJustin T. Gibbs 
13668b8a9b1dSJustin T. Gibbs 	/*
13678b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
13688b8a9b1dSJustin T. Gibbs 	 * matter what.
13698b8a9b1dSJustin T. Gibbs 	 */
13708b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
13718b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
13728b8a9b1dSJustin T. Gibbs 
13738b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
13748b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
13758b8a9b1dSJustin T. Gibbs 
13768b8a9b1dSJustin T. Gibbs 		/*
13778b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
13788b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
13798b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
13808b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
13818b8a9b1dSJustin T. Gibbs 		 * EDT elements.
13828b8a9b1dSJustin T. Gibbs 		 */
13838b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
13848b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
13858b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
13868b8a9b1dSJustin T. Gibbs 			continue;
13878b8a9b1dSJustin T. Gibbs 		}
13888b8a9b1dSJustin T. Gibbs 
13898b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
13908b8a9b1dSJustin T. Gibbs 
13918b8a9b1dSJustin T. Gibbs 		/*
13928b8a9b1dSJustin T. Gibbs 		 * If they want to match any bus node, we give them any
13938b8a9b1dSJustin T. Gibbs 		 * device node.
13948b8a9b1dSJustin T. Gibbs 		 */
13958b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_ANY) {
13968b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
13978b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
13988b8a9b1dSJustin T. Gibbs 
13998b8a9b1dSJustin T. Gibbs 			/*
14008b8a9b1dSJustin T. Gibbs 			 * If we've already decided on an action, go ahead
14018b8a9b1dSJustin T. Gibbs 			 * and return.
14028b8a9b1dSJustin T. Gibbs 			 */
14038b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
14048b8a9b1dSJustin T. Gibbs 				return(retval);
14058b8a9b1dSJustin T. Gibbs 		}
14068b8a9b1dSJustin T. Gibbs 
14078b8a9b1dSJustin T. Gibbs 		/*
14088b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
14098b8a9b1dSJustin T. Gibbs 		 */
14108b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == BUS_MATCH_NONE)
14118b8a9b1dSJustin T. Gibbs 			continue;
14128b8a9b1dSJustin T. Gibbs 
14138b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
14148b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
14158b8a9b1dSJustin T. Gibbs 			continue;
14168b8a9b1dSJustin T. Gibbs 
14178b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
14188b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
14198b8a9b1dSJustin T. Gibbs 			continue;
14208b8a9b1dSJustin T. Gibbs 
14218b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
14228b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
14238b8a9b1dSJustin T. Gibbs 			continue;
14248b8a9b1dSJustin T. Gibbs 
14258b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
14268b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
14278b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
14288b8a9b1dSJustin T. Gibbs 			continue;
14298b8a9b1dSJustin T. Gibbs 
14308b8a9b1dSJustin T. Gibbs 		/*
14318b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
14328b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
14338b8a9b1dSJustin T. Gibbs 		 * data out.
14348b8a9b1dSJustin T. Gibbs 		 */
14358b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
14368b8a9b1dSJustin T. Gibbs 
14378b8a9b1dSJustin T. Gibbs 		/*
14388b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
14398b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
14408b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
14418b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
14428b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
14438b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
14448b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
14458b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
14468b8a9b1dSJustin T. Gibbs 		 */
14478b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
14488b8a9b1dSJustin T. Gibbs 			return(retval);
14498b8a9b1dSJustin T. Gibbs 	}
14508b8a9b1dSJustin T. Gibbs 
14518b8a9b1dSJustin T. Gibbs 	/*
14528b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14538b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
14548b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
14558b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
14568b8a9b1dSJustin T. Gibbs 	 */
14578b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14588b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
14598b8a9b1dSJustin T. Gibbs 
14608b8a9b1dSJustin T. Gibbs 	return(retval);
14618b8a9b1dSJustin T. Gibbs }
14628b8a9b1dSJustin T. Gibbs 
14638b8a9b1dSJustin T. Gibbs static dev_match_ret
14643393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
14658b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
14668b8a9b1dSJustin T. Gibbs {
14678b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1468167e63e3SPedro F. Giffuni 	u_int i;
14698b8a9b1dSJustin T. Gibbs 
14708b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
14718b8a9b1dSJustin T. Gibbs 
14728b8a9b1dSJustin T. Gibbs 	/*
14738b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
14748b8a9b1dSJustin T. Gibbs 	 */
14758b8a9b1dSJustin T. Gibbs 	if (device == NULL)
14768b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
14778b8a9b1dSJustin T. Gibbs 
14788b8a9b1dSJustin T. Gibbs 	/*
14798b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
14808b8a9b1dSJustin T. Gibbs 	 * matter what.
14818b8a9b1dSJustin T. Gibbs 	 */
148259e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
14838b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
14848b8a9b1dSJustin T. Gibbs 
14858b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14868b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
14873501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
14888b8a9b1dSJustin T. Gibbs 
14898b8a9b1dSJustin T. Gibbs 		/*
14908b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
14918b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14928b8a9b1dSJustin T. Gibbs 		 */
14938b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
14948b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
14958b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
14968b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
14978b8a9b1dSJustin T. Gibbs 			continue;
14988b8a9b1dSJustin T. Gibbs 		}
14998b8a9b1dSJustin T. Gibbs 
15008b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
15018b8a9b1dSJustin T. Gibbs 
15023501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
15033501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
15043501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
15053501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
15063501942bSJustin T. Gibbs 
15078b8a9b1dSJustin T. Gibbs 		/*
15088b8a9b1dSJustin T. Gibbs 		 * If they want to match any device node, we give them any
15098b8a9b1dSJustin T. Gibbs 		 * device node.
15108b8a9b1dSJustin T. Gibbs 		 */
15113501942bSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_ANY)
15123501942bSJustin T. Gibbs 			goto copy_dev_node;
15138b8a9b1dSJustin T. Gibbs 
15148b8a9b1dSJustin T. Gibbs 		/*
15158b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
15168b8a9b1dSJustin T. Gibbs 		 */
15178b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == DEV_MATCH_NONE)
15188b8a9b1dSJustin T. Gibbs 			continue;
15198b8a9b1dSJustin T. Gibbs 
15208b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
15218b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
15228b8a9b1dSJustin T. Gibbs 			continue;
15238b8a9b1dSJustin T. Gibbs 
15248b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
15258b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
15268b8a9b1dSJustin T. Gibbs 			continue;
15278b8a9b1dSJustin T. Gibbs 
15288b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
15298b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
15308b8a9b1dSJustin T. Gibbs 			continue;
15318b8a9b1dSJustin T. Gibbs 
15328b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
15338b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
15343501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
15353501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
15368b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
15378b8a9b1dSJustin T. Gibbs 			continue;
15388b8a9b1dSJustin T. Gibbs 
15393501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
15403501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
15413501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
15423501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
15433501942bSJustin T. Gibbs 				      device->device_id_len
15443501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
15453501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
15463501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
15473501942bSJustin T. Gibbs 			continue;
15483501942bSJustin T. Gibbs 
15493501942bSJustin T. Gibbs copy_dev_node:
15508b8a9b1dSJustin T. Gibbs 		/*
15518b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15528b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
15538b8a9b1dSJustin T. Gibbs 		 * the data out.
15548b8a9b1dSJustin T. Gibbs 		 */
15558b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15568b8a9b1dSJustin T. Gibbs 
15578b8a9b1dSJustin T. Gibbs 		/*
15588b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
15598b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
15608b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
15618b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
15628b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
15638b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
15648b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
15658b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
15668b8a9b1dSJustin T. Gibbs 		 */
15678b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
15688b8a9b1dSJustin T. Gibbs 			return(retval);
15698b8a9b1dSJustin T. Gibbs 	}
15708b8a9b1dSJustin T. Gibbs 
15718b8a9b1dSJustin T. Gibbs 	/*
15728b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
15738b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
15748b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
15758b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
15768b8a9b1dSJustin T. Gibbs 	 */
15778b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
15788b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
15798b8a9b1dSJustin T. Gibbs 
15808b8a9b1dSJustin T. Gibbs 	return(retval);
15818b8a9b1dSJustin T. Gibbs }
15828b8a9b1dSJustin T. Gibbs 
15838b8a9b1dSJustin T. Gibbs /*
15848b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
15858b8a9b1dSJustin T. Gibbs  */
15868b8a9b1dSJustin T. Gibbs static dev_match_ret
15873393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
15888b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
15898b8a9b1dSJustin T. Gibbs {
15908b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1591167e63e3SPedro F. Giffuni 	u_int i;
15928b8a9b1dSJustin T. Gibbs 
15938b8a9b1dSJustin T. Gibbs 	/*
15948b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
15958b8a9b1dSJustin T. Gibbs 	 */
15968b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
15978b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
15988b8a9b1dSJustin T. Gibbs 
15998b8a9b1dSJustin T. Gibbs 	/*
16008b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
16018b8a9b1dSJustin T. Gibbs 	 * matter what.
16028b8a9b1dSJustin T. Gibbs 	 */
16038b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
16048b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
16058b8a9b1dSJustin T. Gibbs 
16068b8a9b1dSJustin T. Gibbs 	/*
16078b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
16088b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
16098b8a9b1dSJustin T. Gibbs 	 */
16108b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
16118b8a9b1dSJustin T. Gibbs 
16128b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
16138b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
16148b8a9b1dSJustin T. Gibbs 
16158b8a9b1dSJustin T. Gibbs 		/*
16168b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
16178b8a9b1dSJustin T. Gibbs 		 * aren't interested.
16188b8a9b1dSJustin T. Gibbs 		 */
16198b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
16208b8a9b1dSJustin T. Gibbs 			continue;
16218b8a9b1dSJustin T. Gibbs 
16228b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
16238b8a9b1dSJustin T. Gibbs 
16248b8a9b1dSJustin T. Gibbs 		/*
16258b8a9b1dSJustin T. Gibbs 		 * If they want to match on anything, then we will do so.
16268b8a9b1dSJustin T. Gibbs 		 */
16278b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_ANY) {
16288b8a9b1dSJustin T. Gibbs 			/* set the copy flag */
16298b8a9b1dSJustin T. Gibbs 			retval |= DM_RET_COPY;
16308b8a9b1dSJustin T. Gibbs 
16318b8a9b1dSJustin T. Gibbs 			/*
16328b8a9b1dSJustin T. Gibbs 			 * We've already set the return action to stop,
16338b8a9b1dSJustin T. Gibbs 			 * since there are no nodes below peripherals in
16348b8a9b1dSJustin T. Gibbs 			 * the tree.
16358b8a9b1dSJustin T. Gibbs 			 */
16368b8a9b1dSJustin T. Gibbs 			return(retval);
16378b8a9b1dSJustin T. Gibbs 		}
16388b8a9b1dSJustin T. Gibbs 
16398b8a9b1dSJustin T. Gibbs 		/*
16408b8a9b1dSJustin T. Gibbs 		 * Not sure why someone would do this...
16418b8a9b1dSJustin T. Gibbs 		 */
16428b8a9b1dSJustin T. Gibbs 		if (cur_pattern->flags == PERIPH_MATCH_NONE)
16438b8a9b1dSJustin T. Gibbs 			continue;
16448b8a9b1dSJustin T. Gibbs 
16458b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
16468b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
16478b8a9b1dSJustin T. Gibbs 			continue;
16488b8a9b1dSJustin T. Gibbs 
16498b8a9b1dSJustin T. Gibbs 		/*
16508b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
16518b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
16528b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
16538b8a9b1dSJustin T. Gibbs 		 */
16548b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
16558b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
16568b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
16578b8a9b1dSJustin T. Gibbs 			continue;
16588b8a9b1dSJustin T. Gibbs 
16598b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
16608b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
16618b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
16628b8a9b1dSJustin T. Gibbs 			continue;
16638b8a9b1dSJustin T. Gibbs 
16648b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
16658b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
16668b8a9b1dSJustin T. Gibbs 			continue;
16678b8a9b1dSJustin T. Gibbs 
16688b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
16698b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
16708b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
16718b8a9b1dSJustin T. Gibbs 			continue;
16728b8a9b1dSJustin T. Gibbs 
16738b8a9b1dSJustin T. Gibbs 		/*
16748b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
16758b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
16768b8a9b1dSJustin T. Gibbs 		 * copy the data out.
16778b8a9b1dSJustin T. Gibbs 		 */
16788b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
16798b8a9b1dSJustin T. Gibbs 
16808b8a9b1dSJustin T. Gibbs 		/*
16818b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
16828b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
16838b8a9b1dSJustin T. Gibbs 		 */
16848b8a9b1dSJustin T. Gibbs 		return(retval);
16858b8a9b1dSJustin T. Gibbs 	}
16868b8a9b1dSJustin T. Gibbs 
16878b8a9b1dSJustin T. Gibbs 	/*
16888b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
16898b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
16908b8a9b1dSJustin T. Gibbs 	 */
16918b8a9b1dSJustin T. Gibbs 	return(retval);
16928b8a9b1dSJustin T. Gibbs }
16938b8a9b1dSJustin T. Gibbs 
16948b8a9b1dSJustin T. Gibbs static int
16958b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
16968b8a9b1dSJustin T. Gibbs {
16978b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1698227d67aaSAlexander Motin 	struct cam_et *target;
16998b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17008b8a9b1dSJustin T. Gibbs 
17018b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
17028b8a9b1dSJustin T. Gibbs 
17038b8a9b1dSJustin T. Gibbs 	/*
17048b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
17058b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
17068b8a9b1dSJustin T. Gibbs 	 */
17078b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
17098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
17108b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
17118b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
17128b8a9b1dSJustin T. Gibbs 	else
17138b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
17148b8a9b1dSJustin T. Gibbs 
17158b8a9b1dSJustin T. Gibbs 	/*
17168b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
17178b8a9b1dSJustin T. Gibbs 	 */
17188b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17198b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17208b8a9b1dSJustin T. Gibbs 		return(0);
17218b8a9b1dSJustin T. Gibbs 	}
17228b8a9b1dSJustin T. Gibbs 
17238b8a9b1dSJustin T. Gibbs 	/*
17248b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
17258b8a9b1dSJustin T. Gibbs 	 */
17268b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
17278b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
17288b8a9b1dSJustin T. Gibbs 
17298b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
17308b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
17318b8a9b1dSJustin T. Gibbs 
17328b8a9b1dSJustin T. Gibbs 		/*
17338b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17348b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17358b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17368b8a9b1dSJustin T. Gibbs 		 */
17378b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17388b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17398b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17408b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
17418b8a9b1dSJustin T. Gibbs 
17428b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
17438b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17442b83592fSScott Long 				xsoftc.bus_generation;
17458b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17468b8a9b1dSJustin T. Gibbs 			return(0);
17478b8a9b1dSJustin T. Gibbs 		}
17488b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17498b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17508b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
17518b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
17528b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
17538b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
17548b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
1755b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.bus_result.dev_name,
1756b0f662feSAlan Somers 			bus->sim->sim_name,
1757b0f662feSAlan Somers 			sizeof(cdm->matches[j].result.bus_result.dev_name));
17588b8a9b1dSJustin T. Gibbs 	}
17598b8a9b1dSJustin T. Gibbs 
17608b8a9b1dSJustin T. Gibbs 	/*
1761db4fcadfSConrad Meyer 	 * If the user is only interested in buses, there's no
17628b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
17638b8a9b1dSJustin T. Gibbs 	 */
17648b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
17658b8a9b1dSJustin T. Gibbs 		return(1);
17668b8a9b1dSJustin T. Gibbs 
17678b8a9b1dSJustin T. Gibbs 	/*
17688b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
17698b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
17708b8a9b1dSJustin T. Gibbs 	 */
1771227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
17728b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
17738b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
17748b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1775227d67aaSAlexander Motin 	 && (cdm->pos.cookie.target != NULL)) {
1776227d67aaSAlexander Motin 		if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1777227d67aaSAlexander Motin 		    bus->generation)) {
1778227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1779227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1780227d67aaSAlexander Motin 			return (0);
1781227d67aaSAlexander Motin 		}
1782227d67aaSAlexander Motin 		target = (struct cam_et *)cdm->pos.cookie.target;
1783227d67aaSAlexander Motin 		target->refcount++;
1784227d67aaSAlexander Motin 	} else
1785227d67aaSAlexander Motin 		target = NULL;
1786227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1787227d67aaSAlexander Motin 
1788227d67aaSAlexander Motin 	return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
17898b8a9b1dSJustin T. Gibbs }
17908b8a9b1dSJustin T. Gibbs 
17918b8a9b1dSJustin T. Gibbs static int
17928b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
17938b8a9b1dSJustin T. Gibbs {
17948b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1795227d67aaSAlexander Motin 	struct cam_eb *bus;
1796227d67aaSAlexander Motin 	struct cam_ed *device;
17978b8a9b1dSJustin T. Gibbs 
17988b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1799227d67aaSAlexander Motin 	bus = target->bus;
18008b8a9b1dSJustin T. Gibbs 
18018b8a9b1dSJustin T. Gibbs 	/*
18028b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
18038b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
18048b8a9b1dSJustin T. Gibbs 	 */
1805227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
18068b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1807227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
18088b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18098b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
18108b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1811227d67aaSAlexander Motin 	 && (cdm->pos.cookie.device != NULL)) {
1812227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1813227d67aaSAlexander Motin 		    target->generation) {
1814227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
18158b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
18168b8a9b1dSJustin T. Gibbs 			return(0);
18178b8a9b1dSJustin T. Gibbs 		}
1818227d67aaSAlexander Motin 		device = (struct cam_ed *)cdm->pos.cookie.device;
1819227d67aaSAlexander Motin 		device->refcount++;
1820227d67aaSAlexander Motin 	} else
1821227d67aaSAlexander Motin 		device = NULL;
1822227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
18238b8a9b1dSJustin T. Gibbs 
1824227d67aaSAlexander Motin 	return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
18258b8a9b1dSJustin T. Gibbs }
18268b8a9b1dSJustin T. Gibbs 
18278b8a9b1dSJustin T. Gibbs static int
18288b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
18298b8a9b1dSJustin T. Gibbs {
1830227d67aaSAlexander Motin 	struct cam_eb *bus;
1831227d67aaSAlexander Motin 	struct cam_periph *periph;
18328b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18338b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18348b8a9b1dSJustin T. Gibbs 
18358b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1836227d67aaSAlexander Motin 	bus = device->target->bus;
18378b8a9b1dSJustin T. Gibbs 
18388b8a9b1dSJustin T. Gibbs 	/*
18398b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
18408b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
18418b8a9b1dSJustin T. Gibbs 	 */
18428b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
18438b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
18448b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
18458b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
18468b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
18478b8a9b1dSJustin T. Gibbs 	else
18488b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
18498b8a9b1dSJustin T. Gibbs 					device);
18508b8a9b1dSJustin T. Gibbs 
18518b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
18528b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
18538b8a9b1dSJustin T. Gibbs 		return(0);
18548b8a9b1dSJustin T. Gibbs 	}
18558b8a9b1dSJustin T. Gibbs 
18568b8a9b1dSJustin T. Gibbs 	/*
18578b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
18588b8a9b1dSJustin T. Gibbs 	 */
18598b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18608b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
18618b8a9b1dSJustin T. Gibbs 
18628b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18638b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18648b8a9b1dSJustin T. Gibbs 
18658b8a9b1dSJustin T. Gibbs 		/*
18668b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
18678b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
18688b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
18698b8a9b1dSJustin T. Gibbs 		 */
18708b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
18718b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
18728b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
18738b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
18748b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
18758b8a9b1dSJustin T. Gibbs 
18768b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
18778b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
18782b83592fSScott Long 				xsoftc.bus_generation;
18798b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
18808b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
18818b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
18828b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
18838b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
18848b8a9b1dSJustin T. Gibbs 				device->target->generation;
18858b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18868b8a9b1dSJustin T. Gibbs 			return(0);
18878b8a9b1dSJustin T. Gibbs 		}
18888b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18898b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18908b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
18918b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
18928b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
18938b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
18948b8a9b1dSJustin T. Gibbs 			device->target->target_id;
18958b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
18968b8a9b1dSJustin T. Gibbs 			device->lun_id;
189752c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
189852c9ce25SScott Long 			device->protocol;
18998b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
19008b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
19018b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
190252c9ce25SScott Long 		bcopy(&device->ident_data,
190352c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
190452c9ce25SScott Long 		      sizeof(struct ata_params));
19059deea857SKenneth D. Merry 
19069deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
19079deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
19089deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
19099deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
19109deea857SKenneth D. Merry 		else
19119deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
19129deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
19138b8a9b1dSJustin T. Gibbs 	}
19148b8a9b1dSJustin T. Gibbs 
19158b8a9b1dSJustin T. Gibbs 	/*
19168b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
19178b8a9b1dSJustin T. Gibbs 	 * the tree any further.
19188b8a9b1dSJustin T. Gibbs 	 */
19198b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
19208b8a9b1dSJustin T. Gibbs 		return(1);
19218b8a9b1dSJustin T. Gibbs 
19228b8a9b1dSJustin T. Gibbs 	/*
19238b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
19248b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
19258b8a9b1dSJustin T. Gibbs 	 */
1926227d67aaSAlexander Motin 	xpt_lock_buses();
1927227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
19288b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1929227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
19308b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
19318b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
19328b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
19338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
19348b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1935227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1936227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1937227d67aaSAlexander Motin 		    device->generation) {
1938227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1939227d67aaSAlexander Motin 			xpt_unlock_buses();
1940227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1941227d67aaSAlexander Motin 			return(0);
1942227d67aaSAlexander Motin 		}
1943227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1944227d67aaSAlexander Motin 		periph->refcount++;
1945227d67aaSAlexander Motin 	} else
1946227d67aaSAlexander Motin 		periph = NULL;
1947227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1948227d67aaSAlexander Motin 	xpt_unlock_buses();
1949227d67aaSAlexander Motin 
1950227d67aaSAlexander Motin 	return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
19518b8a9b1dSJustin T. Gibbs }
19528b8a9b1dSJustin T. Gibbs 
19538b8a9b1dSJustin T. Gibbs static int
19548b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
19558b8a9b1dSJustin T. Gibbs {
19568b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19578b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19588b8a9b1dSJustin T. Gibbs 
19598b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19608b8a9b1dSJustin T. Gibbs 
19618b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19628b8a9b1dSJustin T. Gibbs 
19638b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19648b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19658b8a9b1dSJustin T. Gibbs 		return(0);
19668b8a9b1dSJustin T. Gibbs 	}
19678b8a9b1dSJustin T. Gibbs 
19688b8a9b1dSJustin T. Gibbs 	/*
19698b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
19708b8a9b1dSJustin T. Gibbs 	 */
19718b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
19728b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
1973b0f662feSAlan Somers 		size_t l;
19748b8a9b1dSJustin T. Gibbs 
19758b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
19768b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
19778b8a9b1dSJustin T. Gibbs 
19788b8a9b1dSJustin T. Gibbs 		/*
19798b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
19808b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
19818b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
19828b8a9b1dSJustin T. Gibbs 		 */
19838b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
19848b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
19858b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
19868b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
19878b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
19888b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
19898b8a9b1dSJustin T. Gibbs 
19908b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
19918b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
19922b83592fSScott Long 				xsoftc.bus_generation;
19938b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
19948b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
19958b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
19968b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
19978b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
19988b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
19998b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
20008b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
20018b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
20028b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
20038b8a9b1dSJustin T. Gibbs 			return(0);
20048b8a9b1dSJustin T. Gibbs 		}
20058b8a9b1dSJustin T. Gibbs 
20068b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
20078b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
20088b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
20098b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
20108b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
20118b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
20128b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
20138b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
20148b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
20158b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20168b8a9b1dSJustin T. Gibbs 			periph->unit_number;
2017b0f662feSAlan Somers 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2018b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
2019b0f662feSAlan Somers 			periph->periph_name, l);
20208b8a9b1dSJustin T. Gibbs 	}
20218b8a9b1dSJustin T. Gibbs 
20228b8a9b1dSJustin T. Gibbs 	return(1);
20238b8a9b1dSJustin T. Gibbs }
20248b8a9b1dSJustin T. Gibbs 
20258b8a9b1dSJustin T. Gibbs static int
20268b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
20278b8a9b1dSJustin T. Gibbs {
2028227d67aaSAlexander Motin 	struct cam_eb *bus;
20298b8a9b1dSJustin T. Gibbs 	int ret;
20308b8a9b1dSJustin T. Gibbs 
20318b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20328b8a9b1dSJustin T. Gibbs 
20338b8a9b1dSJustin T. Gibbs 	/*
20348b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
20358b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
20368b8a9b1dSJustin T. Gibbs 	 */
2037227d67aaSAlexander Motin 	xpt_lock_buses();
20388b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2039227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus != NULL)) {
2040227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_BUS_GENERATION] !=
2041227d67aaSAlexander Motin 		    xsoftc.bus_generation) {
2042227d67aaSAlexander Motin 			xpt_unlock_buses();
20438b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
20448b8a9b1dSJustin T. Gibbs 			return(0);
20458b8a9b1dSJustin T. Gibbs 		}
2046227d67aaSAlexander Motin 		bus = (struct cam_eb *)cdm->pos.cookie.bus;
2047227d67aaSAlexander Motin 		bus->refcount++;
2048227d67aaSAlexander Motin 	} else
2049227d67aaSAlexander Motin 		bus = NULL;
2050227d67aaSAlexander Motin 	xpt_unlock_buses();
20518b8a9b1dSJustin T. Gibbs 
2052227d67aaSAlexander Motin 	ret = xptbustraverse(bus, xptedtbusfunc, cdm);
20538b8a9b1dSJustin T. Gibbs 
20548b8a9b1dSJustin T. Gibbs 	/*
20558b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
20568b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
20578b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
20588b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
20598b8a9b1dSJustin T. Gibbs 	 */
20608b8a9b1dSJustin T. Gibbs 	if (ret == 1)
20618b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
20628b8a9b1dSJustin T. Gibbs 
20638b8a9b1dSJustin T. Gibbs 	return(ret);
20648b8a9b1dSJustin T. Gibbs }
20658b8a9b1dSJustin T. Gibbs 
20668b8a9b1dSJustin T. Gibbs static int
20678b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
20688b8a9b1dSJustin T. Gibbs {
2069227d67aaSAlexander Motin 	struct cam_periph *periph;
20708b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
20718b8a9b1dSJustin T. Gibbs 
20728b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
20738b8a9b1dSJustin T. Gibbs 
2074227d67aaSAlexander Motin 	xpt_lock_buses();
20758b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
20768b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
20778b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2078227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
2079227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2080227d67aaSAlexander Motin 		    (*pdrv)->generation) {
2081227d67aaSAlexander Motin 			xpt_unlock_buses();
20828b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
20838b8a9b1dSJustin T. Gibbs 			return(0);
20848b8a9b1dSJustin T. Gibbs 		}
2085227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
2086227d67aaSAlexander Motin 		periph->refcount++;
2087227d67aaSAlexander Motin 	} else
2088227d67aaSAlexander Motin 		periph = NULL;
2089227d67aaSAlexander Motin 	xpt_unlock_buses();
20908b8a9b1dSJustin T. Gibbs 
2091227d67aaSAlexander Motin 	return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
20928b8a9b1dSJustin T. Gibbs }
20938b8a9b1dSJustin T. Gibbs 
20948b8a9b1dSJustin T. Gibbs static int
20958b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
20968b8a9b1dSJustin T. Gibbs {
20978b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
20988b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
20998b8a9b1dSJustin T. Gibbs 
21008b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
21018b8a9b1dSJustin T. Gibbs 
21028b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
21038b8a9b1dSJustin T. Gibbs 
21048b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
21058b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
21068b8a9b1dSJustin T. Gibbs 		return(0);
21078b8a9b1dSJustin T. Gibbs 	}
21088b8a9b1dSJustin T. Gibbs 
21098b8a9b1dSJustin T. Gibbs 	/*
21108b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
21118b8a9b1dSJustin T. Gibbs 	 */
21128b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
21138b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
2114b0f662feSAlan Somers 		size_t l;
21158b8a9b1dSJustin T. Gibbs 
21168b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
21178b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
21188b8a9b1dSJustin T. Gibbs 
21198b8a9b1dSJustin T. Gibbs 		/*
21208b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
21218b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
21228b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
21238b8a9b1dSJustin T. Gibbs 		 */
21248b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
21258b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
21268b8a9b1dSJustin T. Gibbs 
21278b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
21288b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
21298b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
21308b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
21318b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
21328b8a9b1dSJustin T. Gibbs 
21338b8a9b1dSJustin T. Gibbs 			/*
21348b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
21358b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
21368b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
21378b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
21388b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
21398b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
21408b8a9b1dSJustin T. Gibbs 			 */
21410b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
21428b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
21438b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
21448b8a9b1dSJustin T. Gibbs 					break;
21458b8a9b1dSJustin T. Gibbs 			}
21468b8a9b1dSJustin T. Gibbs 
214701910babSScott Long 			if (*pdrv == NULL) {
21488b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
21498b8a9b1dSJustin T. Gibbs 				return(0);
21508b8a9b1dSJustin T. Gibbs 			}
21518b8a9b1dSJustin T. Gibbs 
21528b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
21538b8a9b1dSJustin T. Gibbs 			/*
21548b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
21558b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
21568b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
21578b8a9b1dSJustin T. Gibbs 			 */
21588b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
21598b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
21608b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
21618b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
21628b8a9b1dSJustin T. Gibbs 			return(0);
21638b8a9b1dSJustin T. Gibbs 		}
21648b8a9b1dSJustin T. Gibbs 
21658b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
21668b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
21678b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
21688b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
21698b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
21708b8a9b1dSJustin T. Gibbs 
21718b8a9b1dSJustin T. Gibbs 		/*
21728b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
21738b8a9b1dSJustin T. Gibbs 		 * lun.
21748b8a9b1dSJustin T. Gibbs 		 */
21758b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
21768b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
21778b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
21788b8a9b1dSJustin T. Gibbs 		else
2179431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_id =
2180431d3a5bSAlexander Motin 				CAM_TARGET_WILDCARD;
21818b8a9b1dSJustin T. Gibbs 
21828b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
21838b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
21848b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
21858b8a9b1dSJustin T. Gibbs 		else
2186431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_lun =
2187431d3a5bSAlexander Motin 				CAM_LUN_WILDCARD;
21888b8a9b1dSJustin T. Gibbs 
21898b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
21908b8a9b1dSJustin T. Gibbs 			periph->unit_number;
2191b0f662feSAlan Somers 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2192b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
2193b0f662feSAlan Somers 			periph->periph_name, l);
21948b8a9b1dSJustin T. Gibbs 	}
21958b8a9b1dSJustin T. Gibbs 
21968b8a9b1dSJustin T. Gibbs 	return(1);
21978b8a9b1dSJustin T. Gibbs }
21988b8a9b1dSJustin T. Gibbs 
21998b8a9b1dSJustin T. Gibbs static int
22008b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
22018b8a9b1dSJustin T. Gibbs {
22028b8a9b1dSJustin T. Gibbs 	int ret;
22038b8a9b1dSJustin T. Gibbs 
22048b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
22058b8a9b1dSJustin T. Gibbs 
22068b8a9b1dSJustin T. Gibbs 	/*
22078b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
2208db4fcadfSConrad Meyer 	 * list generation to make sure that no buses have been added or
22098b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
22108b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
22118b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
22128b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
22138b8a9b1dSJustin T. Gibbs 	 * without a recompile.
22148b8a9b1dSJustin T. Gibbs 	 */
22158b8a9b1dSJustin T. Gibbs 
22168b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
22178b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
22188b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
22198b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
22208b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
22218b8a9b1dSJustin T. Gibbs 	else
22228b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
22238b8a9b1dSJustin T. Gibbs 
22248b8a9b1dSJustin T. Gibbs 	/*
22258b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
22268b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
22278b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
22288b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
22298b8a9b1dSJustin T. Gibbs 	 * matching entries.
22308b8a9b1dSJustin T. Gibbs 	 */
22318b8a9b1dSJustin T. Gibbs 	if (ret == 1)
22328b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
22338b8a9b1dSJustin T. Gibbs 
22348b8a9b1dSJustin T. Gibbs 	return(ret);
22358b8a9b1dSJustin T. Gibbs }
22368b8a9b1dSJustin T. Gibbs 
22378b8a9b1dSJustin T. Gibbs static int
22388b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
22398b8a9b1dSJustin T. Gibbs {
22408b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
22418b8a9b1dSJustin T. Gibbs 	int retval;
22428b8a9b1dSJustin T. Gibbs 
22438b8a9b1dSJustin T. Gibbs 	retval = 1;
2244227d67aaSAlexander Motin 	if (start_bus)
2245227d67aaSAlexander Motin 		bus = start_bus;
2246227d67aaSAlexander Motin 	else {
22479a7c2696SAlexander Motin 		xpt_lock_buses();
2248227d67aaSAlexander Motin 		bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2249227d67aaSAlexander Motin 		if (bus == NULL) {
22509a7c2696SAlexander Motin 			xpt_unlock_buses();
2251227d67aaSAlexander Motin 			return (retval);
2252227d67aaSAlexander Motin 		}
2253227d67aaSAlexander Motin 		bus->refcount++;
2254227d67aaSAlexander Motin 		xpt_unlock_buses();
2255227d67aaSAlexander Motin 	}
2256227d67aaSAlexander Motin 	for (; bus != NULL; bus = next_bus) {
22578b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
2258227d67aaSAlexander Motin 		if (retval == 0) {
2259227d67aaSAlexander Motin 			xpt_release_bus(bus);
2260227d67aaSAlexander Motin 			break;
2261227d67aaSAlexander Motin 		}
22629a7c2696SAlexander Motin 		xpt_lock_buses();
22638900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
2264227d67aaSAlexander Motin 		if (next_bus)
2265227d67aaSAlexander Motin 			next_bus->refcount++;
22669a7c2696SAlexander Motin 		xpt_unlock_buses();
22678900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
22688b8a9b1dSJustin T. Gibbs 	}
22698b8a9b1dSJustin T. Gibbs 	return(retval);
22708b8a9b1dSJustin T. Gibbs }
22718b8a9b1dSJustin T. Gibbs 
22728b8a9b1dSJustin T. Gibbs static int
22738b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
22748b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
22758b8a9b1dSJustin T. Gibbs {
22768b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
22778b8a9b1dSJustin T. Gibbs 	int retval;
22788b8a9b1dSJustin T. Gibbs 
22798b8a9b1dSJustin T. Gibbs 	retval = 1;
2280227d67aaSAlexander Motin 	if (start_target)
2281227d67aaSAlexander Motin 		target = start_target;
2282227d67aaSAlexander Motin 	else {
2283227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2284227d67aaSAlexander Motin 		target = TAILQ_FIRST(&bus->et_entries);
2285227d67aaSAlexander Motin 		if (target == NULL) {
2286227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
22878b8a9b1dSJustin T. Gibbs 			return (retval);
22888b8a9b1dSJustin T. Gibbs 		}
2289227d67aaSAlexander Motin 		target->refcount++;
2290227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2291227d67aaSAlexander Motin 	}
2292227d67aaSAlexander Motin 	for (; target != NULL; target = next_target) {
2293227d67aaSAlexander Motin 		retval = tr_func(target, arg);
2294227d67aaSAlexander Motin 		if (retval == 0) {
2295227d67aaSAlexander Motin 			xpt_release_target(target);
2296227d67aaSAlexander Motin 			break;
2297227d67aaSAlexander Motin 		}
2298227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2299227d67aaSAlexander Motin 		next_target = TAILQ_NEXT(target, links);
2300227d67aaSAlexander Motin 		if (next_target)
2301227d67aaSAlexander Motin 			next_target->refcount++;
2302227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2303227d67aaSAlexander Motin 		xpt_release_target(target);
2304227d67aaSAlexander Motin 	}
23058b8a9b1dSJustin T. Gibbs 	return(retval);
23068b8a9b1dSJustin T. Gibbs }
23078b8a9b1dSJustin T. Gibbs 
23088b8a9b1dSJustin T. Gibbs static int
23098b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
23108b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
23118b8a9b1dSJustin T. Gibbs {
2312227d67aaSAlexander Motin 	struct cam_eb *bus;
23138b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
23148b8a9b1dSJustin T. Gibbs 	int retval;
23158b8a9b1dSJustin T. Gibbs 
23168b8a9b1dSJustin T. Gibbs 	retval = 1;
2317227d67aaSAlexander Motin 	bus = target->bus;
2318227d67aaSAlexander Motin 	if (start_device)
2319227d67aaSAlexander Motin 		device = start_device;
2320227d67aaSAlexander Motin 	else {
2321227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2322227d67aaSAlexander Motin 		device = TAILQ_FIRST(&target->ed_entries);
2323227d67aaSAlexander Motin 		if (device == NULL) {
2324227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
23258b8a9b1dSJustin T. Gibbs 			return (retval);
23268b8a9b1dSJustin T. Gibbs 		}
2327227d67aaSAlexander Motin 		device->refcount++;
2328227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2329227d67aaSAlexander Motin 	}
2330227d67aaSAlexander Motin 	for (; device != NULL; device = next_device) {
2331227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
2332227d67aaSAlexander Motin 		retval = tr_func(device, arg);
2333227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
2334227d67aaSAlexander Motin 		if (retval == 0) {
2335227d67aaSAlexander Motin 			xpt_release_device(device);
2336227d67aaSAlexander Motin 			break;
2337227d67aaSAlexander Motin 		}
2338227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2339227d67aaSAlexander Motin 		next_device = TAILQ_NEXT(device, links);
2340227d67aaSAlexander Motin 		if (next_device)
2341227d67aaSAlexander Motin 			next_device->refcount++;
2342227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2343227d67aaSAlexander Motin 		xpt_release_device(device);
2344227d67aaSAlexander Motin 	}
23458b8a9b1dSJustin T. Gibbs 	return(retval);
23468b8a9b1dSJustin T. Gibbs }
23478b8a9b1dSJustin T. Gibbs 
23488b8a9b1dSJustin T. Gibbs static int
23498b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
23508b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
23518b8a9b1dSJustin T. Gibbs {
2352227d67aaSAlexander Motin 	struct cam_eb *bus;
23538b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
23548b8a9b1dSJustin T. Gibbs 	int retval;
23558b8a9b1dSJustin T. Gibbs 
23568b8a9b1dSJustin T. Gibbs 	retval = 1;
23578b8a9b1dSJustin T. Gibbs 
2358227d67aaSAlexander Motin 	bus = device->target->bus;
2359227d67aaSAlexander Motin 	if (start_periph)
2360227d67aaSAlexander Motin 		periph = start_periph;
2361227d67aaSAlexander Motin 	else {
23628900f4b8SKenneth D. Merry 		xpt_lock_buses();
2363227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2364227d67aaSAlexander Motin 		periph = SLIST_FIRST(&device->periphs);
2365227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2366227d67aaSAlexander Motin 			periph = SLIST_NEXT(periph, periph_links);
2367227d67aaSAlexander Motin 		if (periph == NULL) {
2368227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
23698900f4b8SKenneth D. Merry 			xpt_unlock_buses();
2370227d67aaSAlexander Motin 			return (retval);
2371227d67aaSAlexander Motin 		}
2372227d67aaSAlexander Motin 		periph->refcount++;
2373227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2374227d67aaSAlexander Motin 		xpt_unlock_buses();
2375227d67aaSAlexander Motin 	}
2376227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2377227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2378227d67aaSAlexander Motin 		if (retval == 0) {
23799813c936SAlexander Motin 			cam_periph_release_locked(periph);
2380227d67aaSAlexander Motin 			break;
2381227d67aaSAlexander Motin 		}
2382227d67aaSAlexander Motin 		xpt_lock_buses();
2383227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2384227d67aaSAlexander Motin 		next_periph = SLIST_NEXT(periph, periph_links);
2385227d67aaSAlexander Motin 		while (next_periph != NULL &&
2386227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2387840a5dd4SAlexander Motin 			next_periph = SLIST_NEXT(next_periph, periph_links);
2388227d67aaSAlexander Motin 		if (next_periph)
2389227d67aaSAlexander Motin 			next_periph->refcount++;
2390227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2391227d67aaSAlexander Motin 		xpt_unlock_buses();
2392227d67aaSAlexander Motin 		cam_periph_release_locked(periph);
2393227d67aaSAlexander Motin 	}
23948b8a9b1dSJustin T. Gibbs 	return(retval);
23958b8a9b1dSJustin T. Gibbs }
23968b8a9b1dSJustin T. Gibbs 
23978b8a9b1dSJustin T. Gibbs static int
23988b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
23998b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
24008b8a9b1dSJustin T. Gibbs {
24018b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
24028b8a9b1dSJustin T. Gibbs 	int retval;
24038b8a9b1dSJustin T. Gibbs 
24048b8a9b1dSJustin T. Gibbs 	retval = 1;
24058b8a9b1dSJustin T. Gibbs 
24068b8a9b1dSJustin T. Gibbs 	/*
24078b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
24088b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
24098b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
24108b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
24118b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
24128b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
24138b8a9b1dSJustin T. Gibbs 	 */
24140b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
24158b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
24168b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
24178b8a9b1dSJustin T. Gibbs 
24188b8a9b1dSJustin T. Gibbs 		if (retval == 0)
24198b8a9b1dSJustin T. Gibbs 			return(retval);
24208b8a9b1dSJustin T. Gibbs 	}
24218b8a9b1dSJustin T. Gibbs 
24228b8a9b1dSJustin T. Gibbs 	return(retval);
24238b8a9b1dSJustin T. Gibbs }
24248b8a9b1dSJustin T. Gibbs 
24258b8a9b1dSJustin T. Gibbs static int
24268b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
24278b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
24288b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
24298b8a9b1dSJustin T. Gibbs {
24308b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
24318b8a9b1dSJustin T. Gibbs 	int retval;
24328b8a9b1dSJustin T. Gibbs 
24338b8a9b1dSJustin T. Gibbs 	retval = 1;
24348b8a9b1dSJustin T. Gibbs 
2435227d67aaSAlexander Motin 	if (start_periph)
2436227d67aaSAlexander Motin 		periph = start_periph;
2437227d67aaSAlexander Motin 	else {
2438f1e2546aSMatt Jacob 		xpt_lock_buses();
2439227d67aaSAlexander Motin 		periph = TAILQ_FIRST(&(*pdrv)->units);
2440227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2441227d67aaSAlexander Motin 			periph = TAILQ_NEXT(periph, unit_links);
2442227d67aaSAlexander Motin 		if (periph == NULL) {
2443227d67aaSAlexander Motin 			xpt_unlock_buses();
2444227d67aaSAlexander Motin 			return (retval);
24458900f4b8SKenneth D. Merry 		}
24468900f4b8SKenneth D. Merry 		periph->refcount++;
2447dcdf6e74SAlexander Motin 		xpt_unlock_buses();
24488b8a9b1dSJustin T. Gibbs 	}
2449227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2450227d67aaSAlexander Motin 		cam_periph_lock(periph);
2451227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2452227d67aaSAlexander Motin 		cam_periph_unlock(periph);
2453227d67aaSAlexander Motin 		if (retval == 0) {
2454227d67aaSAlexander Motin 			cam_periph_release(periph);
2455227d67aaSAlexander Motin 			break;
2456227d67aaSAlexander Motin 		}
2457227d67aaSAlexander Motin 		xpt_lock_buses();
2458227d67aaSAlexander Motin 		next_periph = TAILQ_NEXT(periph, unit_links);
2459227d67aaSAlexander Motin 		while (next_periph != NULL &&
2460227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2461840a5dd4SAlexander Motin 			next_periph = TAILQ_NEXT(next_periph, unit_links);
2462227d67aaSAlexander Motin 		if (next_periph)
2463227d67aaSAlexander Motin 			next_periph->refcount++;
2464f1e2546aSMatt Jacob 		xpt_unlock_buses();
2465227d67aaSAlexander Motin 		cam_periph_release(periph);
2466227d67aaSAlexander Motin 	}
24678b8a9b1dSJustin T. Gibbs 	return(retval);
24688b8a9b1dSJustin T. Gibbs }
24698b8a9b1dSJustin T. Gibbs 
24708b8a9b1dSJustin T. Gibbs static int
24718b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
24728b8a9b1dSJustin T. Gibbs {
24738b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24748b8a9b1dSJustin T. Gibbs 
24758b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24768b8a9b1dSJustin T. Gibbs 
24778b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
24788b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
24798b8a9b1dSJustin T. Gibbs 
24808b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
24818b8a9b1dSJustin T. Gibbs 
24828b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
24838b8a9b1dSJustin T. Gibbs 	} else
24848b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
24858b8a9b1dSJustin T. Gibbs }
24868b8a9b1dSJustin T. Gibbs 
24878b8a9b1dSJustin T. Gibbs static int
24888b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
24898b8a9b1dSJustin T. Gibbs {
24908b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24918b8a9b1dSJustin T. Gibbs 
24928b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24938b8a9b1dSJustin T. Gibbs 
24948b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
24958b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
24968b8a9b1dSJustin T. Gibbs 
24978b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
24988b8a9b1dSJustin T. Gibbs 
24998b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
25008b8a9b1dSJustin T. Gibbs 	} else
25018b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
25028b8a9b1dSJustin T. Gibbs }
25038b8a9b1dSJustin T. Gibbs 
25048b8a9b1dSJustin T. Gibbs static int
25058b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
25068b8a9b1dSJustin T. Gibbs {
25078b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
25088b8a9b1dSJustin T. Gibbs 
25098b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
25108b8a9b1dSJustin T. Gibbs 
25118b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
25128b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
25138b8a9b1dSJustin T. Gibbs 
25148b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
25158b8a9b1dSJustin T. Gibbs 
25168b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
25178b8a9b1dSJustin T. Gibbs 	} else
25188b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
25198b8a9b1dSJustin T. Gibbs }
25208b8a9b1dSJustin T. Gibbs 
25218b8a9b1dSJustin T. Gibbs static int
25228b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
25238b8a9b1dSJustin T. Gibbs {
25248b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
25258b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
25268b8a9b1dSJustin T. Gibbs 
25278b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
25288b8a9b1dSJustin T. Gibbs 
25298b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
25308b8a9b1dSJustin T. Gibbs 
25318b8a9b1dSJustin T. Gibbs 	/*
25328b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
25338b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
25348b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
25358b8a9b1dSJustin T. Gibbs 	 */
25368b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
25378b8a9b1dSJustin T. Gibbs }
25388b8a9b1dSJustin T. Gibbs 
25398b8a9b1dSJustin T. Gibbs /*
25408b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
25418b8a9b1dSJustin T. Gibbs  */
25428b8a9b1dSJustin T. Gibbs static int
25438b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
25448b8a9b1dSJustin T. Gibbs {
25458b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
25468b8a9b1dSJustin T. Gibbs 
25478b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
25488b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
25498b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
25508b8a9b1dSJustin T. Gibbs 
25518b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
25528b8a9b1dSJustin T. Gibbs }
25538b8a9b1dSJustin T. Gibbs 
25548b8a9b1dSJustin T. Gibbs /*
25558b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
25568b8a9b1dSJustin T. Gibbs  */
25578b8a9b1dSJustin T. Gibbs static int
25588b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
25598b8a9b1dSJustin T. Gibbs {
25608b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
25618b8a9b1dSJustin T. Gibbs 
25628b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
25638b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
25648b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
25658b8a9b1dSJustin T. Gibbs 
25668b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
25678b8a9b1dSJustin T. Gibbs }
25688b8a9b1dSJustin T. Gibbs 
25698b8a9b1dSJustin T. Gibbs static int
25708b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
25718b8a9b1dSJustin T. Gibbs {
25728b8a9b1dSJustin T. Gibbs 	struct cam_path path;
25738b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
25747685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
25758b8a9b1dSJustin T. Gibbs 
2576c8bead2aSJustin T. Gibbs 	/*
2577c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2578c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2579c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2580c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2581c8bead2aSJustin T. Gibbs 	 */
2582c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2583c8bead2aSJustin T. Gibbs 		return (1);
2584c8bead2aSJustin T. Gibbs 
25858b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
25868b8a9b1dSJustin T. Gibbs 			 NULL,
25878b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
25888b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
25898b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2590bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
25918b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
25928b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
25937685edecSAlexander Motin 	csa->callback(csa->callback_arg,
25948b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
25958b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
25968b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
25978b8a9b1dSJustin T. Gibbs 
25988b8a9b1dSJustin T. Gibbs 	return(1);
25998b8a9b1dSJustin T. Gibbs }
2600c8bead2aSJustin T. Gibbs 
26018b8a9b1dSJustin T. Gibbs static int
26028b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
26038b8a9b1dSJustin T. Gibbs {
26048b8a9b1dSJustin T. Gibbs 	struct cam_path path;
26058b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
26067685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
26078b8a9b1dSJustin T. Gibbs 
26088b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
26096dfc67e3SAlexander Motin 			 bus->path_id,
26108b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
26118b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2612227d67aaSAlexander Motin 	xpt_path_lock(&path);
2613762a7f4fSWarner Losh 	xpt_path_inq(&cpi, &path);
26147685edecSAlexander Motin 	csa->callback(csa->callback_arg,
26158b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
26168b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
2617227d67aaSAlexander Motin 	xpt_path_unlock(&path);
26188b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
26198b8a9b1dSJustin T. Gibbs 
26208b8a9b1dSJustin T. Gibbs 	return(1);
26218b8a9b1dSJustin T. Gibbs }
26228b8a9b1dSJustin T. Gibbs 
26238b8a9b1dSJustin T. Gibbs void
26248b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
26258b8a9b1dSJustin T. Gibbs {
26269911ecf9SJustin T. Gibbs 
262782727824SWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
262869be012fSWarner Losh 	    ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
262969be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
26308b8a9b1dSJustin T. Gibbs 
26318b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
2632ded2b706SWarner Losh 	(*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
263352c9ce25SScott Long }
263452c9ce25SScott Long 
263552c9ce25SScott Long void
263652c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
263752c9ce25SScott Long {
2638da396db2SAlexander Motin 	struct cam_path *path;
2639227d67aaSAlexander Motin 	struct cam_sim *sim;
2640401ed17aSAlexander Motin 	struct mtx *mtx;
264152c9ce25SScott Long 
2642da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
264382727824SWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
264469be012fSWarner Losh 	    ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
264569be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
264652c9ce25SScott Long 
26478b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
26488b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2649d05caa00SKenneth D. Merry 	{
26503393f8daSKenneth D. Merry 		struct cam_ed *device;
2651d05caa00SKenneth D. Merry 
26528b8a9b1dSJustin T. Gibbs 		/*
26538b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
26548b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
26558b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
26568b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
26578b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
26588b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
26598b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
26608b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
26618b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
26628b8a9b1dSJustin T. Gibbs 		 *
26638b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
26648b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
26658b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
26668b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
26678b8a9b1dSJustin T. Gibbs 		 */
2668da396db2SAlexander Motin 		device = path->device;
26693393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
26708b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
26718b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
26728b8a9b1dSJustin T. Gibbs 
26738b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
26748b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
26758b8a9b1dSJustin T. Gibbs 		}
26768b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2677d05caa00SKenneth D. Merry 	}
267807c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
26798b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
26808b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
26812cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
26822cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
26832cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
268452c9ce25SScott Long 	case XPT_ATA_IO:
2685de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
268652c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2687b882a6d3SMatt Jacob 		/* FALLTHROUGH */
2688b24ced67SWarner Losh 	case XPT_NVME_IO:
2689df424515SWarner Losh 	case XPT_NVME_ADMIN:
2690a94a63f0SWarner Losh 	case XPT_MMC_IO:
269110e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
26928b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
269306e79492SKenneth D. Merry 	case XPT_SMP_IO:
26942cefde5fSJustin T. Gibbs 	{
2695227d67aaSAlexander Motin 		struct cam_devq *devq;
26962cefde5fSJustin T. Gibbs 
2697227d67aaSAlexander Motin 		devq = path->bus->sim->devq;
2698227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
2699227d67aaSAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2700227d67aaSAlexander Motin 		if (xpt_schedule_devq(devq, path->device) != 0)
2701227d67aaSAlexander Motin 			xpt_run_devq(devq);
2702227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
2703227d67aaSAlexander Motin 		break;
2704227d67aaSAlexander Motin 	}
2705227d67aaSAlexander Motin 	case XPT_CALC_GEOMETRY:
27068b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
27078b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
27088b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
27098b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
27108b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
27118b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
27128b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
27138b8a9b1dSJustin T. Gibbs 			break;
27148b8a9b1dSJustin T. Gibbs 		}
2715227d67aaSAlexander Motin 		goto call_sim;
2716bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
27172cefde5fSJustin T. Gibbs 	{
27182cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
27192cefde5fSJustin T. Gibbs 
27202cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
27212cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
272283c5d981SAlexander Motin 			struct cam_ed *device;
2723dda945f5SMark Johnston 			struct cam_devq *devq;
27242cefde5fSJustin T. Gibbs 
272583c5d981SAlexander Motin 			device = abort_ccb->ccb_h.path->device;
2726dda945f5SMark Johnston 			devq = device->sim->devq;
2727dda945f5SMark Johnston 
2728dda945f5SMark Johnston 			mtx_lock(&devq->send_mtx);
2729dda945f5SMark Johnston 			if (abort_ccb->ccb_h.pinfo.index > 0) {
2730dda945f5SMark Johnston 				cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
27312cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
27322cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2733dda945f5SMark Johnston 				xpt_freeze_devq_device(device, 1);
2734dda945f5SMark Johnston 				mtx_unlock(&devq->send_mtx);
27352cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
27362cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
27372cefde5fSJustin T. Gibbs 				break;
27382cefde5fSJustin T. Gibbs 			}
2739dda945f5SMark Johnston 			mtx_unlock(&devq->send_mtx);
2740dda945f5SMark Johnston 
27412cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
27422cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
27432cefde5fSJustin T. Gibbs 				/*
27442cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
27452cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
27462cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
27472cefde5fSJustin T. Gibbs 				 * real work on the CCB.
27482cefde5fSJustin T. Gibbs 				 */
27492cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
27502cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
27512cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
27522cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
27532cefde5fSJustin T. Gibbs 				break;
27542cefde5fSJustin T. Gibbs 			}
27552cefde5fSJustin T. Gibbs 		}
27562cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
27572cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
27582cefde5fSJustin T. Gibbs 			/*
27592cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
27602cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
27612cefde5fSJustin T. Gibbs 			 */
27622cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
27632cefde5fSJustin T. Gibbs 			break;
27642cefde5fSJustin T. Gibbs 		}
27652cefde5fSJustin T. Gibbs 		/*
27662cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
27672cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
27682cefde5fSJustin T. Gibbs 		 */
27692cefde5fSJustin T. Gibbs 	}
277007c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
27718b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
27728b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
27738b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
27748b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
27758b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
27762df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
27772df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
2778a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:
27792df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
27802df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
2781227d67aaSAlexander Motin 	case XPT_GET_TRAN_SETTINGS:
2782227d67aaSAlexander Motin 	case XPT_SET_TRAN_SETTINGS:
278387cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
2784227d67aaSAlexander Motin call_sim:
2785da396db2SAlexander Motin 		sim = path->bus->sim;
2786401ed17aSAlexander Motin 		mtx = sim->mtx;
2787401ed17aSAlexander Motin 		if (mtx && !mtx_owned(mtx))
2788401ed17aSAlexander Motin 			mtx_lock(mtx);
2789401ed17aSAlexander Motin 		else
2790401ed17aSAlexander Motin 			mtx = NULL;
2791a94a63f0SWarner Losh 
279282727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2793a94a63f0SWarner Losh 		    ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
279487cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
279582727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2796a94a63f0SWarner Losh 		    ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2797401ed17aSAlexander Motin 		if (mtx)
2798401ed17aSAlexander Motin 			mtx_unlock(mtx);
279987cfaf0eSJustin T. Gibbs 		break;
280087cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2801da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
280287cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
280387cfaf0eSJustin T. Gibbs 		break;
28048b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2805a5479bc5SJustin T. Gibbs 	{
280687cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2807a5479bc5SJustin T. Gibbs 
2808da396db2SAlexander Motin 		dev = path->device;
280987cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
28108b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
28118b8a9b1dSJustin T. Gibbs 		} else {
28128b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
28138b8a9b1dSJustin T. Gibbs 
28148b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
281552c9ce25SScott Long 			cgd->protocol = dev->protocol;
28168b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
281752c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
281830a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
28198b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
28208b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
28218b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
28228b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
28238b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
28248b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
28258b8a9b1dSJustin T. Gibbs 		}
28268b8a9b1dSJustin T. Gibbs 		break;
2827a5479bc5SJustin T. Gibbs 	}
282887cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
282987cfaf0eSJustin T. Gibbs 	{
28302ef6e7aeSAlexander Motin 		struct ccb_getdevstats *cgds = &start_ccb->cgds;
28312ef6e7aeSAlexander Motin 		struct cam_ed *dev = path->device;
28322ef6e7aeSAlexander Motin 		struct cam_eb *bus = path->bus;
28332ef6e7aeSAlexander Motin 		struct cam_et *tar = path->target;
28342ef6e7aeSAlexander Motin 		struct cam_devq *devq = bus->sim->devq;
283587cfaf0eSJustin T. Gibbs 
2836959ec258SAlexander Motin 		mtx_lock(&devq->send_mtx);
283787cfaf0eSJustin T. Gibbs 		cgds->dev_openings = dev->ccbq.dev_openings;
283887cfaf0eSJustin T. Gibbs 		cgds->dev_active = dev->ccbq.dev_active;
2839959ec258SAlexander Motin 		cgds->allocated = dev->ccbq.allocated;
2840959ec258SAlexander Motin 		cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
28412ef6e7aeSAlexander Motin 		cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
284287cfaf0eSJustin T. Gibbs 		cgds->last_reset = tar->last_reset;
284352c9ce25SScott Long 		cgds->maxtags = dev->maxtags;
284452c9ce25SScott Long 		cgds->mintags = dev->mintags;
284587cfaf0eSJustin T. Gibbs 		if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
284687cfaf0eSJustin T. Gibbs 			cgds->last_reset = bus->last_reset;
2847959ec258SAlexander Motin 		mtx_unlock(&devq->send_mtx);
284887cfaf0eSJustin T. Gibbs 		cgds->ccb_h.status = CAM_REQ_CMP;
284987cfaf0eSJustin T. Gibbs 		break;
285087cfaf0eSJustin T. Gibbs 	}
28518b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
28528b8a9b1dSJustin T. Gibbs 	{
28538b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
28548b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
28558b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
28563393f8daSKenneth D. Merry 		u_int			i;
28578b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
28588b8a9b1dSJustin T. Gibbs 		int			found;
28598b8a9b1dSJustin T. Gibbs 
28608b8a9b1dSJustin T. Gibbs 
28618b8a9b1dSJustin T. Gibbs 		found = 0;
28628b8a9b1dSJustin T. Gibbs 
28638b8a9b1dSJustin T. Gibbs 		/*
28648b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
28658b8a9b1dSJustin T. Gibbs 		 */
2866da396db2SAlexander Motin 		device = path->device;
28678b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
28688b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
28698b8a9b1dSJustin T. Gibbs 
28708b8a9b1dSJustin T. Gibbs 		/*
28718b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
28728b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
28738b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
28748b8a9b1dSJustin T. Gibbs 		 * from the beginning.
28758b8a9b1dSJustin T. Gibbs 		 */
28768b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
28778b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
28788b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
28798b8a9b1dSJustin T. Gibbs 			break;
28808b8a9b1dSJustin T. Gibbs 		}
28818b8a9b1dSJustin T. Gibbs 
28828b8a9b1dSJustin T. Gibbs 		/*
28838b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
28848b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
28858b8a9b1dSJustin T. Gibbs 		 */
2886fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
28878b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2888fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
28898b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
2890b0f662feSAlan Somers 				strlcpy(cgdl->periph_name,
28918b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
2892b0f662feSAlan Somers 					sizeof(cgdl->periph_name));
28938b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
28948b8a9b1dSJustin T. Gibbs 				found = 1;
28958b8a9b1dSJustin T. Gibbs 			}
28968b8a9b1dSJustin T. Gibbs 		}
28978b8a9b1dSJustin T. Gibbs 		if (found == 0) {
28988b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
28998b8a9b1dSJustin T. Gibbs 			break;
29008b8a9b1dSJustin T. Gibbs 		}
29018b8a9b1dSJustin T. Gibbs 
29028b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
29038b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
29048b8a9b1dSJustin T. Gibbs 		else
29058b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
29068b8a9b1dSJustin T. Gibbs 
29078b8a9b1dSJustin T. Gibbs 		cgdl->index++;
29088b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
29098b8a9b1dSJustin T. Gibbs 
29108b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
29118b8a9b1dSJustin T. Gibbs 		break;
29128b8a9b1dSJustin T. Gibbs 	}
29138b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
29148b8a9b1dSJustin T. Gibbs 	{
29158b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
29168b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
29178b8a9b1dSJustin T. Gibbs 
29188b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
29198b8a9b1dSJustin T. Gibbs 
29208b8a9b1dSJustin T. Gibbs 		/*
29218b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
29228b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
2923db4fcadfSConrad Meyer 		 * with a list of buses, then a list of targets on a bus,
29248b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
29258b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
29268b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
29278b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
29288b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
29298b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
29308b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
29318b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
29328b8a9b1dSJustin T. Gibbs 		 */
29338b8a9b1dSJustin T. Gibbs 
29348b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
29358b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
29368b8a9b1dSJustin T. Gibbs 		else {
29373393f8daSKenneth D. Merry 			u_int i;
29388b8a9b1dSJustin T. Gibbs 
29398b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
29408b8a9b1dSJustin T. Gibbs 
29418b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
29428b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
29438b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
29448b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
29458b8a9b1dSJustin T. Gibbs 					break;
29468b8a9b1dSJustin T. Gibbs 				}
29478b8a9b1dSJustin T. Gibbs 			}
29488b8a9b1dSJustin T. Gibbs 
29498b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
29508b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
29518b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
29528b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
29538b8a9b1dSJustin T. Gibbs 		}
29548b8a9b1dSJustin T. Gibbs 
29558b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
29568b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
295707c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
29588b8a9b1dSJustin T. Gibbs 			break;
29598b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
296007c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
29618b8a9b1dSJustin T. Gibbs 			break;
29628b8a9b1dSJustin T. Gibbs 		default:
29638b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
29648b8a9b1dSJustin T. Gibbs 			break;
29658b8a9b1dSJustin T. Gibbs 		}
29668b8a9b1dSJustin T. Gibbs 
29678b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
29688b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
29698b8a9b1dSJustin T. Gibbs 		else
29708b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
29718b8a9b1dSJustin T. Gibbs 
29728b8a9b1dSJustin T. Gibbs 		break;
29738b8a9b1dSJustin T. Gibbs 	}
29748b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
29758b8a9b1dSJustin T. Gibbs 	{
297684f82481SScott Long 		struct ccb_setasync *csa;
297784f82481SScott Long 		struct async_node *cur_entry;
297884f82481SScott Long 		struct async_list *async_head;
297984f82481SScott Long 		u_int32_t added;
298084f82481SScott Long 
298184f82481SScott Long 		csa = &start_ccb->csa;
298284f82481SScott Long 		added = csa->event_enable;
2983da396db2SAlexander Motin 		async_head = &path->device->asyncs;
298484f82481SScott Long 
298584f82481SScott Long 		/*
298684f82481SScott Long 		 * If there is already an entry for us, simply
298784f82481SScott Long 		 * update it.
298884f82481SScott Long 		 */
298984f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
299084f82481SScott Long 		while (cur_entry != NULL) {
299184f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
299284f82481SScott Long 			 && (cur_entry->callback == csa->callback))
299384f82481SScott Long 				break;
299484f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
299584f82481SScott Long 		}
299684f82481SScott Long 
299784f82481SScott Long 		if (cur_entry != NULL) {
299884f82481SScott Long 		 	/*
299984f82481SScott Long 			 * If the request has no flags set,
300084f82481SScott Long 			 * remove the entry.
300184f82481SScott Long 			 */
300284f82481SScott Long 			added &= ~cur_entry->event_enable;
300384f82481SScott Long 			if (csa->event_enable == 0) {
300484f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
300584f82481SScott Long 					     async_node, links);
3006da396db2SAlexander Motin 				xpt_release_device(path->device);
300784f82481SScott Long 				free(cur_entry, M_CAMXPT);
300884f82481SScott Long 			} else {
300984f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
301084f82481SScott Long 			}
30117685edecSAlexander Motin 			csa->event_enable = added;
301284f82481SScott Long 		} else {
301384f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
301484f82481SScott Long 					   M_NOWAIT);
301584f82481SScott Long 			if (cur_entry == NULL) {
301684f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
301784f82481SScott Long 				break;
301884f82481SScott Long 			}
301984f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
3020401ed17aSAlexander Motin 			cur_entry->event_lock = (path->bus->sim->mtx &&
3021401ed17aSAlexander Motin 			    mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
302284f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
302384f82481SScott Long 			cur_entry->callback = csa->callback;
302484f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
3025da396db2SAlexander Motin 			xpt_acquire_device(path->device);
302684f82481SScott Long 		}
30278b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30288b8a9b1dSJustin T. Gibbs 		break;
30298b8a9b1dSJustin T. Gibbs 	}
30308b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
30318b8a9b1dSJustin T. Gibbs 	{
30328b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
30338b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
30348b8a9b1dSJustin T. Gibbs 
30358b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
3036da396db2SAlexander Motin 		dev = path->device;
30378b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
30388b8a9b1dSJustin T. Gibbs 
30398b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
30408b8a9b1dSJustin T. Gibbs 			break;
30418b8a9b1dSJustin T. Gibbs 		}
30428b8a9b1dSJustin T. Gibbs 
30438b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
30448b8a9b1dSJustin T. Gibbs 
30458b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
30468b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
30477dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
304879ccc199SJordan K. Hubbard 				if (bootverbose) {
3049da396db2SAlexander Motin 					xpt_print(path,
30507dc3213dSAlexander Motin 					    "number of openings is now %d\n",
30518b8a9b1dSJustin T. Gibbs 					    crs->openings);
30528b8a9b1dSJustin T. Gibbs 				}
30538b8a9b1dSJustin T. Gibbs 			}
30548b8a9b1dSJustin T. Gibbs 		}
30558b8a9b1dSJustin T. Gibbs 
3056227d67aaSAlexander Motin 		mtx_lock(&dev->sim->devq->send_mtx);
30578b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
30588b8a9b1dSJustin T. Gibbs 
30598b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
30608b8a9b1dSJustin T. Gibbs 
30618b8a9b1dSJustin T. Gibbs 				/*
30628b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
30638b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
30648b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
30658b8a9b1dSJustin T. Gibbs 				 */
30668b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
30672b83592fSScott Long 				callout_stop(&dev->callout);
30688b8a9b1dSJustin T. Gibbs 			} else {
30698b8a9b1dSJustin T. Gibbs 
30708b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
30718b8a9b1dSJustin T. Gibbs 			}
30728b8a9b1dSJustin T. Gibbs 
307385c9dd9dSSteven Hartland 			callout_reset_sbt(&dev->callout,
307485c9dd9dSSteven Hartland 			    SBT_1MS * crs->release_timeout, 0,
307585c9dd9dSSteven Hartland 			    xpt_release_devq_timeout, dev, 0);
30768b8a9b1dSJustin T. Gibbs 
30778b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
30788b8a9b1dSJustin T. Gibbs 
30798b8a9b1dSJustin T. Gibbs 		}
30808b8a9b1dSJustin T. Gibbs 
30818b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
30828b8a9b1dSJustin T. Gibbs 
30838b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
30848b8a9b1dSJustin T. Gibbs 				/*
30858b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
30868b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
30878b8a9b1dSJustin T. Gibbs 				 * the queue.
30888b8a9b1dSJustin T. Gibbs 				 */
30898b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
30908b8a9b1dSJustin T. Gibbs 			} else {
30918b8a9b1dSJustin T. Gibbs 
30928b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
30938b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
30948b8a9b1dSJustin T. Gibbs 			}
30958b8a9b1dSJustin T. Gibbs 		}
30968b8a9b1dSJustin T. Gibbs 
30978b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
30988b8a9b1dSJustin T. Gibbs 
30998b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
31008b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
31018b8a9b1dSJustin T. Gibbs 
31028b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
31038b8a9b1dSJustin T. Gibbs 			} else {
31048b8a9b1dSJustin T. Gibbs 
31058b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
31068b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
31078b8a9b1dSJustin T. Gibbs 			}
31088b8a9b1dSJustin T. Gibbs 		}
3109227d67aaSAlexander Motin 		mtx_unlock(&dev->sim->devq->send_mtx);
31108b8a9b1dSJustin T. Gibbs 
3111cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3112cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3113cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
31148b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
31158b8a9b1dSJustin T. Gibbs 		break;
31168b8a9b1dSJustin T. Gibbs 	}
31178b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
311880d6987cSAlexander Motin 		struct cam_path *oldpath;
311980d6987cSAlexander Motin 
3120f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
312122c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3122f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3123f0f25b9cSAlexander Motin 			break;
3124f0f25b9cSAlexander Motin 		}
3125f0f25b9cSAlexander Motin 
312680d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
31278b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
312880d6987cSAlexander Motin 			oldpath = cam_dpath;
31298b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
313080d6987cSAlexander Motin 			xpt_free_path(oldpath);
31318b8a9b1dSJustin T. Gibbs 		}
313280d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3133e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
31348b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
31358b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
31368b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
31378b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
31388b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3139aa872be6SMatt Jacob 			} else {
314080d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
31418b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3142f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
3143f0d9af51SMatt Jacob 				    cam_dflags);
3144aa872be6SMatt Jacob 			}
314580d6987cSAlexander Motin 		} else
31468b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
31478b8a9b1dSJustin T. Gibbs 		break;
31488b8a9b1dSJustin T. Gibbs 	}
31498b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
315087cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3151da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
31528b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
31538b8a9b1dSJustin T. Gibbs 		break;
3154d68fae58SEdward Tomasz Napierala 	case XPT_REPROBE_LUN:
3155d68fae58SEdward Tomasz Napierala 		xpt_async(AC_INQ_CHANGED, path, NULL);
3156d68fae58SEdward Tomasz Napierala 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3157d68fae58SEdward Tomasz Napierala 		xpt_done(start_ccb);
3158d68fae58SEdward Tomasz Napierala 		break;
3159*56eccd2dSWarner Losh 	case XPT_ASYNC:
3160*56eccd2dSWarner Losh 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3161*56eccd2dSWarner Losh 		xpt_done(start_ccb);
3162*56eccd2dSWarner Losh 		break;
31638b8a9b1dSJustin T. Gibbs 	default:
31648b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
31658b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
31668b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
31678b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
3168bf1a8895SScott Long 		xpt_print(start_ccb->ccb_h.path,
3169bf1a8895SScott Long 		    "%s: CCB type %#x %s not supported\n", __func__,
3170b24ced67SWarner Losh 		    start_ccb->ccb_h.func_code,
3171b24ced67SWarner Losh 		    xpt_action_name(start_ccb->ccb_h.func_code));
31728b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3173b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3174b882a6d3SMatt Jacob 			xpt_done(start_ccb);
3175b882a6d3SMatt Jacob 		}
31768b8a9b1dSJustin T. Gibbs 		break;
31778b8a9b1dSJustin T. Gibbs 	}
317869be012fSWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
317969be012fSWarner Losh 	    ("xpt_action_default: func= %#x %s status %#x\n",
318069be012fSWarner Losh 		start_ccb->ccb_h.func_code,
318169be012fSWarner Losh  		xpt_action_name(start_ccb->ccb_h.func_code),
318269be012fSWarner Losh 		start_ccb->ccb_h.status));
31838b8a9b1dSJustin T. Gibbs }
31848b8a9b1dSJustin T. Gibbs 
31850cc28e3cSWarner Losh /*
31860cc28e3cSWarner Losh  * Call the sim poll routine to allow the sim to complete
31870cc28e3cSWarner Losh  * any inflight requests, then call camisr_runqueue to
31880cc28e3cSWarner Losh  * complete any CCB that the polling completed.
31890cc28e3cSWarner Losh  */
31900cc28e3cSWarner Losh void
31910cc28e3cSWarner Losh xpt_sim_poll(struct cam_sim *sim)
31920cc28e3cSWarner Losh {
31930cc28e3cSWarner Losh 	struct mtx *mtx;
31940cc28e3cSWarner Losh 
31950cc28e3cSWarner Losh 	mtx = sim->mtx;
31960cc28e3cSWarner Losh 	if (mtx)
31970cc28e3cSWarner Losh 		mtx_lock(mtx);
31980cc28e3cSWarner Losh 	(*(sim->sim_poll))(sim);
31990cc28e3cSWarner Losh 	if (mtx)
32000cc28e3cSWarner Losh 		mtx_unlock(mtx);
32010cc28e3cSWarner Losh 	camisr_runqueue();
32020cc28e3cSWarner Losh }
32030cc28e3cSWarner Losh 
3204f93a843cSWarner Losh uint32_t
3205f93a843cSWarner Losh xpt_poll_setup(union ccb *start_ccb)
32068b8a9b1dSJustin T. Gibbs {
32078b8a9b1dSJustin T. Gibbs 	u_int32_t timeout;
32088b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
32098b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
32108b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
32118b8a9b1dSJustin T. Gibbs 
32120069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
32138b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
32148b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
32158b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
32168b8a9b1dSJustin T. Gibbs 
32178b8a9b1dSJustin T. Gibbs 	/*
32188b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
32198b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
32208b8a9b1dSJustin T. Gibbs 	 */
3221227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
32228b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
3223227d67aaSAlexander Motin 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3224227d67aaSAlexander Motin 	    (--timeout > 0)) {
3225227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
32260069293bSAlexander Motin 		DELAY(100);
32270cc28e3cSWarner Losh 		xpt_sim_poll(sim);
3228227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
32298b8a9b1dSJustin T. Gibbs 	}
32308b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
3231227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
32328b8a9b1dSJustin T. Gibbs 
3233f93a843cSWarner Losh 	return (timeout);
3234f93a843cSWarner Losh }
3235f93a843cSWarner Losh 
3236f93a843cSWarner Losh void
3237f93a843cSWarner Losh xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3238f93a843cSWarner Losh {
3239f93a843cSWarner Losh 
32408b8a9b1dSJustin T. Gibbs 	while (--timeout > 0) {
32410cc28e3cSWarner Losh 		xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
32428b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
32438b8a9b1dSJustin T. Gibbs 		    != CAM_REQ_INPROG)
32448b8a9b1dSJustin T. Gibbs 			break;
32450069293bSAlexander Motin 		DELAY(100);
32468b8a9b1dSJustin T. Gibbs 	}
3247f93a843cSWarner Losh 
32488b8a9b1dSJustin T. Gibbs 	if (timeout == 0) {
32498b8a9b1dSJustin T. Gibbs 		/*
32508b8a9b1dSJustin T. Gibbs 		 * XXX Is it worth adding a sim_timeout entry
32518b8a9b1dSJustin T. Gibbs 		 * point so we can attempt recovery?  If
32528b8a9b1dSJustin T. Gibbs 		 * this is only used for dumps, I don't think
32538b8a9b1dSJustin T. Gibbs 		 * it is.
32548b8a9b1dSJustin T. Gibbs 		 */
32558b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
32568b8a9b1dSJustin T. Gibbs 	}
3257f93a843cSWarner Losh }
3258f93a843cSWarner Losh 
3259f93a843cSWarner Losh void
3260f93a843cSWarner Losh xpt_polled_action(union ccb *start_ccb)
3261f93a843cSWarner Losh {
3262f93a843cSWarner Losh 	uint32_t	timeout;
3263f93a843cSWarner Losh 	struct cam_ed	*dev;
3264f93a843cSWarner Losh 
3265f93a843cSWarner Losh 	timeout = start_ccb->ccb_h.timeout * 10;
3266f93a843cSWarner Losh 	dev = start_ccb->ccb_h.path->device;
3267f93a843cSWarner Losh 
3268f93a843cSWarner Losh 	mtx_unlock(&dev->device_mtx);
3269f93a843cSWarner Losh 
3270f93a843cSWarner Losh 	timeout = xpt_poll_setup(start_ccb);
3271f93a843cSWarner Losh 	if (timeout > 0) {
3272f93a843cSWarner Losh 		xpt_action(start_ccb);
3273f93a843cSWarner Losh 		xpt_pollwait(start_ccb, timeout);
32748b8a9b1dSJustin T. Gibbs 	} else {
32758b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
32768b8a9b1dSJustin T. Gibbs 	}
32774a612489SAlexander Motin 
3278227d67aaSAlexander Motin 	mtx_lock(&dev->device_mtx);
32798b8a9b1dSJustin T. Gibbs }
32808b8a9b1dSJustin T. Gibbs 
32818b8a9b1dSJustin T. Gibbs /*
328221cffce5SBryan Drewery  * Schedule a peripheral driver to receive a ccb when its
32838b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
32848b8a9b1dSJustin T. Gibbs  */
32858b8a9b1dSJustin T. Gibbs void
3286227d67aaSAlexander Motin xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
32878b8a9b1dSJustin T. Gibbs {
32888b8a9b1dSJustin T. Gibbs 
3289227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3290227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3291227d67aaSAlexander Motin 	if (new_priority < periph->scheduled_priority) {
3292227d67aaSAlexander Motin 		periph->scheduled_priority = new_priority;
3293227d67aaSAlexander Motin 		xpt_run_allocq(periph, 0);
32948b8a9b1dSJustin T. Gibbs 	}
32958b8a9b1dSJustin T. Gibbs }
32968b8a9b1dSJustin T. Gibbs 
32978b8a9b1dSJustin T. Gibbs 
32988b8a9b1dSJustin T. Gibbs /*
32998b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
33008b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
33018b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
33028b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
33038b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
330477dc25ccSScott Long  * to run the queue.
33058b8a9b1dSJustin T. Gibbs  */
3306227d67aaSAlexander Motin static int
33078b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
33088b8a9b1dSJustin T. Gibbs 		 u_int32_t new_priority)
33098b8a9b1dSJustin T. Gibbs {
33108b8a9b1dSJustin T. Gibbs 	int retval;
33118b8a9b1dSJustin T. Gibbs 	u_int32_t old_priority;
33128b8a9b1dSJustin T. Gibbs 
3313aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
33148b8a9b1dSJustin T. Gibbs 
3315f93a843cSWarner Losh 
33168b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
33178b8a9b1dSJustin T. Gibbs 
33188b8a9b1dSJustin T. Gibbs 	/*
33198b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
33208b8a9b1dSJustin T. Gibbs 	 */
33218b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
33228b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
33238b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
33248b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
33258b8a9b1dSJustin T. Gibbs 					     new_priority);
3326aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
33278b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
33288b8a9b1dSJustin T. Gibbs 					 new_priority));
332983c5d981SAlexander Motin 			retval = 1;
333083c5d981SAlexander Motin 		} else
33318b8a9b1dSJustin T. Gibbs 			retval = 0;
33328b8a9b1dSJustin T. Gibbs 	} else {
33338b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
33348b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
33358b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
33368b8a9b1dSJustin T. Gibbs 
3337aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
33388b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
33398bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
33408b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
33418b8a9b1dSJustin T. Gibbs 		retval = 1;
33428b8a9b1dSJustin T. Gibbs 	}
33438b8a9b1dSJustin T. Gibbs 	return (retval);
33448b8a9b1dSJustin T. Gibbs }
33458b8a9b1dSJustin T. Gibbs 
33468b8a9b1dSJustin T. Gibbs static void
3347227d67aaSAlexander Motin xpt_run_allocq_task(void *context, int pending)
33488b8a9b1dSJustin T. Gibbs {
3349227d67aaSAlexander Motin 	struct cam_periph *periph = context;
33508b8a9b1dSJustin T. Gibbs 
3351227d67aaSAlexander Motin 	cam_periph_lock(periph);
3352227d67aaSAlexander Motin 	periph->flags &= ~CAM_PERIPH_RUN_TASK;
3353227d67aaSAlexander Motin 	xpt_run_allocq(periph, 1);
3354227d67aaSAlexander Motin 	cam_periph_unlock(periph);
3355227d67aaSAlexander Motin 	cam_periph_release(periph);
3356227d67aaSAlexander Motin }
3357227d67aaSAlexander Motin 
3358227d67aaSAlexander Motin static void
3359227d67aaSAlexander Motin xpt_run_allocq(struct cam_periph *periph, int sleep)
3360227d67aaSAlexander Motin {
3361227d67aaSAlexander Motin 	struct cam_ed	*device;
3362227d67aaSAlexander Motin 	union ccb	*ccb;
3363227d67aaSAlexander Motin 	uint32_t	 prio;
3364227d67aaSAlexander Motin 
3365227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3366227d67aaSAlexander Motin 	if (periph->periph_allocating)
3367cccf4220SAlexander Motin 		return;
33687c1374d5SScott Long 	cam_periph_doacquire(periph);
3369227d67aaSAlexander Motin 	periph->periph_allocating = 1;
3370227d67aaSAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3371227d67aaSAlexander Motin 	device = periph->path->device;
3372227d67aaSAlexander Motin 	ccb = NULL;
3373227d67aaSAlexander Motin restart:
3374227d67aaSAlexander Motin 	while ((prio = min(periph->scheduled_priority,
3375227d67aaSAlexander Motin 	    periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3376227d67aaSAlexander Motin 	    (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3377227d67aaSAlexander Motin 	     device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3378cccf4220SAlexander Motin 
3379227d67aaSAlexander Motin 		if (ccb == NULL &&
3380227d67aaSAlexander Motin 		    (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3381227d67aaSAlexander Motin 			if (sleep) {
3382227d67aaSAlexander Motin 				ccb = xpt_get_ccb(periph);
3383227d67aaSAlexander Motin 				goto restart;
3384227d67aaSAlexander Motin 			}
33858ec5ab3fSAlexander Motin 			if (periph->flags & CAM_PERIPH_RUN_TASK)
33868b8a9b1dSJustin T. Gibbs 				break;
3387c33e4029SAlexander Motin 			cam_periph_doacquire(periph);
3388227d67aaSAlexander Motin 			periph->flags |= CAM_PERIPH_RUN_TASK;
3389227d67aaSAlexander Motin 			taskqueue_enqueue(xsoftc.xpt_taskq,
3390227d67aaSAlexander Motin 			    &periph->periph_run_task);
3391227d67aaSAlexander Motin 			break;
33928b8a9b1dSJustin T. Gibbs 		}
3393227d67aaSAlexander Motin 		xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3394227d67aaSAlexander Motin 		if (prio == periph->immediate_priority) {
3395227d67aaSAlexander Motin 			periph->immediate_priority = CAM_PRIORITY_NONE;
3396227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3397227d67aaSAlexander Motin 					("waking cam_periph_getccb()\n"));
3398227d67aaSAlexander Motin 			SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3399227d67aaSAlexander Motin 					  periph_links.sle);
3400227d67aaSAlexander Motin 			wakeup(&periph->ccb_list);
3401227d67aaSAlexander Motin 		} else {
3402227d67aaSAlexander Motin 			periph->scheduled_priority = CAM_PRIORITY_NONE;
3403227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3404227d67aaSAlexander Motin 					("calling periph_start()\n"));
3405227d67aaSAlexander Motin 			periph->periph_start(periph, ccb);
3406227d67aaSAlexander Motin 		}
3407227d67aaSAlexander Motin 		ccb = NULL;
3408227d67aaSAlexander Motin 	}
3409227d67aaSAlexander Motin 	if (ccb != NULL)
3410227d67aaSAlexander Motin 		xpt_release_ccb(ccb);
3411227d67aaSAlexander Motin 	periph->periph_allocating = 0;
34127c1374d5SScott Long 	cam_periph_release_locked(periph);
34138b8a9b1dSJustin T. Gibbs }
34148b8a9b1dSJustin T. Gibbs 
341583c5d981SAlexander Motin static void
3416cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
34178b8a9b1dSJustin T. Gibbs {
3418401ed17aSAlexander Motin 	struct mtx *mtx;
34198b8a9b1dSJustin T. Gibbs 
3420cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
34218b8a9b1dSJustin T. Gibbs 
3422cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
34238b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3424ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3425cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
34268b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
34278b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
34288b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
342908f13879SWarner Losh 		struct xpt_proto *proto;
34308b8a9b1dSJustin T. Gibbs 
3431227d67aaSAlexander Motin 		device = (struct cam_ed *)camq_remove(&devq->send_queue,
34325a526431SJustin T. Gibbs 							   CAMQ_HEAD);
3433aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
343450642f18SKenneth D. Merry 				("running device %p\n", device));
34358b8a9b1dSJustin T. Gibbs 
34365a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
34378b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
343857b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
34398b8a9b1dSJustin T. Gibbs 			continue;
34408b8a9b1dSJustin T. Gibbs 		}
34418b8a9b1dSJustin T. Gibbs 
34428b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
34438b8a9b1dSJustin T. Gibbs 
3444daa5487fSAlexander Motin 			mtx_lock(&xsoftc.xpt_highpower_lock);
34452b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
34468b8a9b1dSJustin T. Gibbs 				/*
34478b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
34488b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
34498b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
34508b8a9b1dSJustin T. Gibbs 				 * available.
34518b8a9b1dSJustin T. Gibbs 				 */
3452daa5487fSAlexander Motin 				xpt_freeze_devq_device(device, 1);
3453227d67aaSAlexander Motin 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3454ea541bfdSAlexander Motin 						   highpowerq_entry);
34558b8a9b1dSJustin T. Gibbs 
3456daa5487fSAlexander Motin 				mtx_unlock(&xsoftc.xpt_highpower_lock);
34578b8a9b1dSJustin T. Gibbs 				continue;
34588b8a9b1dSJustin T. Gibbs 			} else {
34598b8a9b1dSJustin T. Gibbs 				/*
34608b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
34618b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
34628b8a9b1dSJustin T. Gibbs 				 */
34632b83592fSScott Long 				xsoftc.num_highpower--;
34648b8a9b1dSJustin T. Gibbs 			}
3465daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
34668b8a9b1dSJustin T. Gibbs 		}
34678b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
34688b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
34698b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
34708b8a9b1dSJustin T. Gibbs 		devq->send_active++;
3471cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
3472227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
34738b8a9b1dSJustin T. Gibbs 
3474cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
34758b8a9b1dSJustin T. Gibbs 			/*
34768b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
34778b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
34788b8a9b1dSJustin T. Gibbs 			 */
347983c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
34808b8a9b1dSJustin T. Gibbs 		}
34818b8a9b1dSJustin T. Gibbs 
3482a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3483a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3484a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3485a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
34868b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
34878b8a9b1dSJustin T. Gibbs 			else
34888b8a9b1dSJustin T. Gibbs 				/*
3489a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3490a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
34918b8a9b1dSJustin T. Gibbs 				 */
34928b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3493a4eb4f16SMatt Jacob 		}
34948b8a9b1dSJustin T. Gibbs 
349508f13879SWarner Losh 		KASSERT(device == work_ccb->ccb_h.path->device,
349608f13879SWarner Losh 		    ("device (%p) / path->device (%p) mismatch",
349708f13879SWarner Losh 			device, work_ccb->ccb_h.path->device));
349808f13879SWarner Losh 		proto = xpt_proto_find(device->protocol);
349908f13879SWarner Losh 		if (proto && proto->ops->debug_out)
350008f13879SWarner Losh 			proto->ops->debug_out(work_ccb);
3501de9ebb68SAlexander Motin 
35028b8a9b1dSJustin T. Gibbs 		/*
3503227d67aaSAlexander Motin 		 * Device queues can be shared among multiple SIM instances
3504db4fcadfSConrad Meyer 		 * that reside on different buses.  Use the SIM from the
3505227d67aaSAlexander Motin 		 * queued device, rather than the one from the calling bus.
35068b8a9b1dSJustin T. Gibbs 		 */
3507227d67aaSAlexander Motin 		sim = device->sim;
3508401ed17aSAlexander Motin 		mtx = sim->mtx;
3509401ed17aSAlexander Motin 		if (mtx && !mtx_owned(mtx))
3510401ed17aSAlexander Motin 			mtx_lock(mtx);
3511401ed17aSAlexander Motin 		else
3512401ed17aSAlexander Motin 			mtx = NULL;
3513e4c9cba7SWarner Losh 		work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
35148b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
3515401ed17aSAlexander Motin 		if (mtx)
3516401ed17aSAlexander Motin 			mtx_unlock(mtx);
3517227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
35188b8a9b1dSJustin T. Gibbs 	}
3519cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
35208b8a9b1dSJustin T. Gibbs }
35218b8a9b1dSJustin T. Gibbs 
35228b8a9b1dSJustin T. Gibbs /*
35238b8a9b1dSJustin T. Gibbs  * This function merges stuff from the slave ccb into the master ccb, while
35248b8a9b1dSJustin T. Gibbs  * keeping important fields in the master ccb constant.
35258b8a9b1dSJustin T. Gibbs  */
35268b8a9b1dSJustin T. Gibbs void
35278b8a9b1dSJustin T. Gibbs xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
35288b8a9b1dSJustin T. Gibbs {
352968153f43SScott Long 
35308b8a9b1dSJustin T. Gibbs 	/*
35318b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
35328b8a9b1dSJustin T. Gibbs 	 * into the master CCB along with the CCB "payload".
35338b8a9b1dSJustin T. Gibbs 	 */
35348b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
35358b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
35368b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
35378b8a9b1dSJustin T. Gibbs 	master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
35388b8a9b1dSJustin T. Gibbs 	bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
35398b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
35408b8a9b1dSJustin T. Gibbs }
35418b8a9b1dSJustin T. Gibbs 
35428b8a9b1dSJustin T. Gibbs void
3543a9934668SKenneth D. Merry xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3544a9934668SKenneth D. Merry 		    u_int32_t priority, u_int32_t flags)
35458b8a9b1dSJustin T. Gibbs {
354668153f43SScott Long 
35478b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
35488b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
35498b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
35508b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
35518b8a9b1dSJustin T. Gibbs 	if (path->target)
35528b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
35538b8a9b1dSJustin T. Gibbs 	else
35548b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
35558b8a9b1dSJustin T. Gibbs 	if (path->device) {
35568b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
35578bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
35588b8a9b1dSJustin T. Gibbs 	} else {
35598b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
35608b8a9b1dSJustin T. Gibbs 	}
35618b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3562a9934668SKenneth D. Merry 	ccb_h->flags = flags;
3563b5595753SNathan Whitehorn 	ccb_h->xflags = 0;
35648b8a9b1dSJustin T. Gibbs }
35658b8a9b1dSJustin T. Gibbs 
3566a9934668SKenneth D. Merry void
3567a9934668SKenneth D. Merry xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3568a9934668SKenneth D. Merry {
3569a9934668SKenneth D. Merry 	xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3570a9934668SKenneth D. Merry }
3571a9934668SKenneth D. Merry 
35728b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
35738b8a9b1dSJustin T. Gibbs cam_status
35748b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
35758b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
35768b8a9b1dSJustin T. Gibbs {
35778b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
35788b8a9b1dSJustin T. Gibbs 	cam_status status;
35798b8a9b1dSJustin T. Gibbs 
3580596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
35818b8a9b1dSJustin T. Gibbs 
35828b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
35838b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
35848b8a9b1dSJustin T. Gibbs 		return(status);
35858b8a9b1dSJustin T. Gibbs 	}
35868b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
35878b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3588596ee08fSAlexander Motin 		free(path, M_CAMPATH);
35898b8a9b1dSJustin T. Gibbs 		path = NULL;
35908b8a9b1dSJustin T. Gibbs 	}
35918b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
35928b8a9b1dSJustin T. Gibbs 	return (status);
35938b8a9b1dSJustin T. Gibbs }
35948b8a9b1dSJustin T. Gibbs 
35952b83592fSScott Long cam_status
35962b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
35972b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
35982b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
35992b83592fSScott Long {
36002b83592fSScott Long 
3601227d67aaSAlexander Motin 	return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3602227d67aaSAlexander Motin 	    lun_id));
36032b83592fSScott Long }
36042b83592fSScott Long 
360552c9ce25SScott Long cam_status
36068b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
36078b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
36088b8a9b1dSJustin T. Gibbs {
36098b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
36108b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
36118b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
36128b8a9b1dSJustin T. Gibbs 	cam_status   status;
36138b8a9b1dSJustin T. Gibbs 
36148b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
36158b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
36168b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3617a5479bc5SJustin T. Gibbs 
3618a5479bc5SJustin T. Gibbs 	/*
3619a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3620a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3621a5479bc5SJustin T. Gibbs 	 */
36228b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
36238b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
36248b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3625c8bead2aSJustin T. Gibbs 	} else {
3626227d67aaSAlexander Motin 		xpt_lock_buses();
3627227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
36288b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
36298b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
36308b8a9b1dSJustin T. Gibbs 			/* Create one */
36318b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
36328b8a9b1dSJustin T. Gibbs 
36338b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
36348b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
36358b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
36368b8a9b1dSJustin T. Gibbs 			} else {
36378b8a9b1dSJustin T. Gibbs 				target = new_target;
36388b8a9b1dSJustin T. Gibbs 			}
36398b8a9b1dSJustin T. Gibbs 		}
3640227d67aaSAlexander Motin 		xpt_unlock_buses();
3641c8bead2aSJustin T. Gibbs 		if (target != NULL) {
36428b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
36438b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
36448b8a9b1dSJustin T. Gibbs 				/* Create one */
36458b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
36468b8a9b1dSJustin T. Gibbs 
364752c9ce25SScott Long 				new_device =
3648ded2b706SWarner Losh 				    (*(bus->xport->ops->alloc_device))(bus,
36498b8a9b1dSJustin T. Gibbs 								       target,
36508b8a9b1dSJustin T. Gibbs 								       lun_id);
36518b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
36528b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
36538b8a9b1dSJustin T. Gibbs 				} else {
36548b8a9b1dSJustin T. Gibbs 					device = new_device;
36558b8a9b1dSJustin T. Gibbs 				}
36568b8a9b1dSJustin T. Gibbs 			}
36578b8a9b1dSJustin T. Gibbs 		}
3658227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
36598b8a9b1dSJustin T. Gibbs 	}
36608b8a9b1dSJustin T. Gibbs 
36618b8a9b1dSJustin T. Gibbs 	/*
36628b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
36638b8a9b1dSJustin T. Gibbs 	 */
36648b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
36658b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
36668b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
36678b8a9b1dSJustin T. Gibbs 		new_path->target = target;
36688b8a9b1dSJustin T. Gibbs 		new_path->device = device;
36698b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
36708b8a9b1dSJustin T. Gibbs 	} else {
36718b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3672f98d7a47SAlexander Motin 			xpt_release_device(device);
36738b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3674f98d7a47SAlexander Motin 			xpt_release_target(target);
3675a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3676a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
36778b8a9b1dSJustin T. Gibbs 	}
36788b8a9b1dSJustin T. Gibbs 	return (status);
36798b8a9b1dSJustin T. Gibbs }
36808b8a9b1dSJustin T. Gibbs 
3681227d67aaSAlexander Motin cam_status
3682227d67aaSAlexander Motin xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3683227d67aaSAlexander Motin {
3684227d67aaSAlexander Motin 	struct	   cam_path *new_path;
3685227d67aaSAlexander Motin 
3686227d67aaSAlexander Motin 	new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3687227d67aaSAlexander Motin 	if (new_path == NULL)
3688227d67aaSAlexander Motin 		return(CAM_RESRC_UNAVAIL);
3689227d67aaSAlexander Motin 	xpt_copy_path(new_path, path);
3690227d67aaSAlexander Motin 	*new_path_ptr = new_path;
3691227d67aaSAlexander Motin 	return (CAM_REQ_CMP);
3692227d67aaSAlexander Motin }
3693227d67aaSAlexander Motin 
3694227d67aaSAlexander Motin void
3695227d67aaSAlexander Motin xpt_copy_path(struct cam_path *new_path, struct cam_path *path)
3696227d67aaSAlexander Motin {
3697227d67aaSAlexander Motin 
3698227d67aaSAlexander Motin 	*new_path = *path;
3699227d67aaSAlexander Motin 	if (path->bus != NULL)
3700227d67aaSAlexander Motin 		xpt_acquire_bus(path->bus);
3701227d67aaSAlexander Motin 	if (path->target != NULL)
3702227d67aaSAlexander Motin 		xpt_acquire_target(path->target);
3703227d67aaSAlexander Motin 	if (path->device != NULL)
3704227d67aaSAlexander Motin 		xpt_acquire_device(path->device);
3705227d67aaSAlexander Motin }
3706227d67aaSAlexander Motin 
370752c9ce25SScott Long void
37088b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
37098b8a9b1dSJustin T. Gibbs {
37108b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
37119dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3712f98d7a47SAlexander Motin 		xpt_release_device(path->device);
37139dd03ecfSJustin T. Gibbs 		path->device = NULL;
37149dd03ecfSJustin T. Gibbs 	}
37159dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3716f98d7a47SAlexander Motin 		xpt_release_target(path->target);
37179dd03ecfSJustin T. Gibbs 		path->target = NULL;
37189dd03ecfSJustin T. Gibbs 	}
37199dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
37209dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
37219dd03ecfSJustin T. Gibbs 		path->bus = NULL;
37229dd03ecfSJustin T. Gibbs 	}
37238b8a9b1dSJustin T. Gibbs }
37248b8a9b1dSJustin T. Gibbs 
37258b8a9b1dSJustin T. Gibbs void
37268b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
37278b8a9b1dSJustin T. Gibbs {
372868153f43SScott Long 
37298b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
37308b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3731596ee08fSAlexander Motin 	free(path, M_CAMPATH);
37328b8a9b1dSJustin T. Gibbs }
37338b8a9b1dSJustin T. Gibbs 
373415975b7bSMatt Jacob void
373515975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
373615975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
373715975b7bSMatt Jacob {
373815975b7bSMatt Jacob 
37399a7c2696SAlexander Motin 	xpt_lock_buses();
374015975b7bSMatt Jacob 	if (bus_ref) {
374115975b7bSMatt Jacob 		if (path->bus)
374215975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
374315975b7bSMatt Jacob 		else
374415975b7bSMatt Jacob 			*bus_ref = 0;
374515975b7bSMatt Jacob 	}
374615975b7bSMatt Jacob 	if (periph_ref) {
374715975b7bSMatt Jacob 		if (path->periph)
374815975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
374915975b7bSMatt Jacob 		else
375015975b7bSMatt Jacob 			*periph_ref = 0;
375115975b7bSMatt Jacob 	}
3752dcdf6e74SAlexander Motin 	xpt_unlock_buses();
375315975b7bSMatt Jacob 	if (target_ref) {
375415975b7bSMatt Jacob 		if (path->target)
375515975b7bSMatt Jacob 			*target_ref = path->target->refcount;
375615975b7bSMatt Jacob 		else
375715975b7bSMatt Jacob 			*target_ref = 0;
375815975b7bSMatt Jacob 	}
375915975b7bSMatt Jacob 	if (device_ref) {
376015975b7bSMatt Jacob 		if (path->device)
376115975b7bSMatt Jacob 			*device_ref = path->device->refcount;
376215975b7bSMatt Jacob 		else
376315975b7bSMatt Jacob 			*device_ref = 0;
376415975b7bSMatt Jacob 	}
376515975b7bSMatt Jacob }
37668b8a9b1dSJustin T. Gibbs 
37678b8a9b1dSJustin T. Gibbs /*
37682cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
37692cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
37708b8a9b1dSJustin T. Gibbs  */
37718b8a9b1dSJustin T. Gibbs int
37728b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
37738b8a9b1dSJustin T. Gibbs {
37748b8a9b1dSJustin T. Gibbs 	int retval = 0;
37758b8a9b1dSJustin T. Gibbs 
37768b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
37772cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
37788b8a9b1dSJustin T. Gibbs 			retval = 1;
37792cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
37802cefde5fSJustin T. Gibbs 			retval = 2;
37818b8a9b1dSJustin T. Gibbs 		else
37828b8a9b1dSJustin T. Gibbs 			return (-1);
37838b8a9b1dSJustin T. Gibbs 	}
37848b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
37852cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
37862cefde5fSJustin T. Gibbs 			if (retval == 0)
37878b8a9b1dSJustin T. Gibbs 				retval = 1;
37882cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
37892cefde5fSJustin T. Gibbs 			retval = 2;
37908b8a9b1dSJustin T. Gibbs 		else
37918b8a9b1dSJustin T. Gibbs 			return (-1);
37928b8a9b1dSJustin T. Gibbs 	}
37938b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
37942cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
37952cefde5fSJustin T. Gibbs 			if (retval == 0)
37968b8a9b1dSJustin T. Gibbs 				retval = 1;
37972cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
37982cefde5fSJustin T. Gibbs 			retval = 2;
37998b8a9b1dSJustin T. Gibbs 		else
38008b8a9b1dSJustin T. Gibbs 			return (-1);
38018b8a9b1dSJustin T. Gibbs 	}
38028b8a9b1dSJustin T. Gibbs 	return (retval);
38038b8a9b1dSJustin T. Gibbs }
38048b8a9b1dSJustin T. Gibbs 
38050d4f3c31SAlexander Motin int
38060d4f3c31SAlexander Motin xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
38070d4f3c31SAlexander Motin {
38080d4f3c31SAlexander Motin 	int retval = 0;
38090d4f3c31SAlexander Motin 
38100d4f3c31SAlexander Motin 	if (path->bus != dev->target->bus) {
38110d4f3c31SAlexander Motin 		if (path->bus->path_id == CAM_BUS_WILDCARD)
38120d4f3c31SAlexander Motin 			retval = 1;
38130d4f3c31SAlexander Motin 		else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
38140d4f3c31SAlexander Motin 			retval = 2;
38150d4f3c31SAlexander Motin 		else
38160d4f3c31SAlexander Motin 			return (-1);
38170d4f3c31SAlexander Motin 	}
38180d4f3c31SAlexander Motin 	if (path->target != dev->target) {
38190d4f3c31SAlexander Motin 		if (path->target->target_id == CAM_TARGET_WILDCARD) {
38200d4f3c31SAlexander Motin 			if (retval == 0)
38210d4f3c31SAlexander Motin 				retval = 1;
38220d4f3c31SAlexander Motin 		} else if (dev->target->target_id == CAM_TARGET_WILDCARD)
38230d4f3c31SAlexander Motin 			retval = 2;
38240d4f3c31SAlexander Motin 		else
38250d4f3c31SAlexander Motin 			return (-1);
38260d4f3c31SAlexander Motin 	}
38270d4f3c31SAlexander Motin 	if (path->device != dev) {
38280d4f3c31SAlexander Motin 		if (path->device->lun_id == CAM_LUN_WILDCARD) {
38290d4f3c31SAlexander Motin 			if (retval == 0)
38300d4f3c31SAlexander Motin 				retval = 1;
38310d4f3c31SAlexander Motin 		} else if (dev->lun_id == CAM_LUN_WILDCARD)
38320d4f3c31SAlexander Motin 			retval = 2;
38330d4f3c31SAlexander Motin 		else
38340d4f3c31SAlexander Motin 			return (-1);
38350d4f3c31SAlexander Motin 	}
38360d4f3c31SAlexander Motin 	return (retval);
38370d4f3c31SAlexander Motin }
38380d4f3c31SAlexander Motin 
38398b8a9b1dSJustin T. Gibbs void
38408b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
38418b8a9b1dSJustin T. Gibbs {
3842ab3e89f1SScott Long 	struct sbuf sb;
3843ab3e89f1SScott Long 	char buffer[XPT_PRINT_LEN];
384468153f43SScott Long 
3845ab3e89f1SScott Long 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3846ab3e89f1SScott Long 	xpt_path_sbuf(path, &sb);
3847ab3e89f1SScott Long 	sbuf_finish(&sb);
3848ab3e89f1SScott Long 	printf("%s", sbuf_data(&sb));
3849ab3e89f1SScott Long 	sbuf_delete(&sb);
38508b8a9b1dSJustin T. Gibbs }
38518b8a9b1dSJustin T. Gibbs 
3852f0d9af51SMatt Jacob void
38530d4f3c31SAlexander Motin xpt_print_device(struct cam_ed *device)
38540d4f3c31SAlexander Motin {
38550d4f3c31SAlexander Motin 
38560d4f3c31SAlexander Motin 	if (device == NULL)
38570d4f3c31SAlexander Motin 		printf("(nopath): ");
38580d4f3c31SAlexander Motin 	else {
3859abe83505SNathan Whitehorn 		printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
38600d4f3c31SAlexander Motin 		       device->sim->unit_number,
38610d4f3c31SAlexander Motin 		       device->sim->bus_id,
38620d4f3c31SAlexander Motin 		       device->target->target_id,
3863abe83505SNathan Whitehorn 		       (uintmax_t)device->lun_id);
38640d4f3c31SAlexander Motin 	}
38650d4f3c31SAlexander Motin }
38660d4f3c31SAlexander Motin 
38670d4f3c31SAlexander Motin void
3868f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3869f0d9af51SMatt Jacob {
3870f0d9af51SMatt Jacob 	va_list ap;
3871ab3e89f1SScott Long 	struct sbuf sb;
3872a136ca54SScott Long 	char buffer[XPT_PRINT_LEN];
3873ab3e89f1SScott Long 
3874a136ca54SScott Long 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3875ab3e89f1SScott Long 
3876ab3e89f1SScott Long 	xpt_path_sbuf(path, &sb);
3877f0d9af51SMatt Jacob 	va_start(ap, fmt);
3878ab3e89f1SScott Long 	sbuf_vprintf(&sb, fmt, ap);
3879f0d9af51SMatt Jacob 	va_end(ap);
3880ab3e89f1SScott Long 
3881ab3e89f1SScott Long 	sbuf_finish(&sb);
3882ab3e89f1SScott Long 	printf("%s", sbuf_data(&sb));
3883ab3e89f1SScott Long 	sbuf_delete(&sb);
3884f0d9af51SMatt Jacob }
3885f0d9af51SMatt Jacob 
38863393f8daSKenneth D. Merry int
38873393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
38883393f8daSKenneth D. Merry {
38893393f8daSKenneth D. Merry 	struct sbuf sb;
3890ab3e89f1SScott Long 	int len;
38913393f8daSKenneth D. Merry 
38923393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
3893ab3e89f1SScott Long 	len = xpt_path_sbuf(path, &sb);
3894ab3e89f1SScott Long 	sbuf_finish(&sb);
3895ab3e89f1SScott Long 	return (len);
3896ab3e89f1SScott Long }
3897ab3e89f1SScott Long 
3898ab3e89f1SScott Long int
3899ab3e89f1SScott Long xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3900ab3e89f1SScott Long {
39013393f8daSKenneth D. Merry 
39023393f8daSKenneth D. Merry 	if (path == NULL)
3903ab3e89f1SScott Long 		sbuf_printf(sb, "(nopath): ");
39043393f8daSKenneth D. Merry 	else {
39053393f8daSKenneth D. Merry 		if (path->periph != NULL)
3906ab3e89f1SScott Long 			sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
39073393f8daSKenneth D. Merry 				    path->periph->unit_number);
39083393f8daSKenneth D. Merry 		else
3909ab3e89f1SScott Long 			sbuf_printf(sb, "(noperiph:");
39103393f8daSKenneth D. Merry 
39113393f8daSKenneth D. Merry 		if (path->bus != NULL)
3912ab3e89f1SScott Long 			sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
39133393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
39143393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
39153393f8daSKenneth D. Merry 		else
3916ab3e89f1SScott Long 			sbuf_printf(sb, "nobus:");
39173393f8daSKenneth D. Merry 
39183393f8daSKenneth D. Merry 		if (path->target != NULL)
3919ab3e89f1SScott Long 			sbuf_printf(sb, "%d:", path->target->target_id);
39203393f8daSKenneth D. Merry 		else
3921ab3e89f1SScott Long 			sbuf_printf(sb, "X:");
39223393f8daSKenneth D. Merry 
39233393f8daSKenneth D. Merry 		if (path->device != NULL)
3924ab3e89f1SScott Long 			sbuf_printf(sb, "%jx): ",
3925abe83505SNathan Whitehorn 			    (uintmax_t)path->device->lun_id);
39263393f8daSKenneth D. Merry 		else
3927ab3e89f1SScott Long 			sbuf_printf(sb, "X): ");
39283393f8daSKenneth D. Merry 	}
39293393f8daSKenneth D. Merry 
3930ab3e89f1SScott Long 	return(sbuf_len(sb));
39313393f8daSKenneth D. Merry }
39323393f8daSKenneth D. Merry 
39338b8a9b1dSJustin T. Gibbs path_id_t
39348b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
39358b8a9b1dSJustin T. Gibbs {
39368b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
39378b8a9b1dSJustin T. Gibbs }
39388b8a9b1dSJustin T. Gibbs 
39398b8a9b1dSJustin T. Gibbs target_id_t
39408b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
39418b8a9b1dSJustin T. Gibbs {
39428b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
39438b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
39448b8a9b1dSJustin T. Gibbs 	else
39458b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
39468b8a9b1dSJustin T. Gibbs }
39478b8a9b1dSJustin T. Gibbs 
39488b8a9b1dSJustin T. Gibbs lun_id_t
39498b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
39508b8a9b1dSJustin T. Gibbs {
39518b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
39528b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
39538b8a9b1dSJustin T. Gibbs 	else
39548b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
39558b8a9b1dSJustin T. Gibbs }
39568b8a9b1dSJustin T. Gibbs 
39578b8a9b1dSJustin T. Gibbs struct cam_sim *
39588b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
39598b8a9b1dSJustin T. Gibbs {
396068153f43SScott Long 
39618b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
39628b8a9b1dSJustin T. Gibbs }
39638b8a9b1dSJustin T. Gibbs 
39648b8a9b1dSJustin T. Gibbs struct cam_periph*
39658b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
39668b8a9b1dSJustin T. Gibbs {
396768153f43SScott Long 
39688b8a9b1dSJustin T. Gibbs 	return (path->periph);
39698b8a9b1dSJustin T. Gibbs }
39708b8a9b1dSJustin T. Gibbs 
39718b8a9b1dSJustin T. Gibbs /*
39728b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
39738b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
39748b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
39758b8a9b1dSJustin T. Gibbs  * call them now.
39768b8a9b1dSJustin T. Gibbs  */
39778b8a9b1dSJustin T. Gibbs void
39788b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
39798b8a9b1dSJustin T. Gibbs {
39808b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
3981227d67aaSAlexander Motin 	struct	 cam_periph *periph;
398268153f43SScott Long 
3983aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3984227d67aaSAlexander Motin 	xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3985227d67aaSAlexander Motin 	device = free_ccb->ccb_h.path->device;
3986227d67aaSAlexander Motin 	periph = free_ccb->ccb_h.path->periph;
39872b83592fSScott Long 
39888b8a9b1dSJustin T. Gibbs 	xpt_free_ccb(free_ccb);
3989227d67aaSAlexander Motin 	periph->periph_allocated--;
3990227d67aaSAlexander Motin 	cam_ccbq_release_opening(&device->ccbq);
3991227d67aaSAlexander Motin 	xpt_run_allocq(periph, 0);
39928b8a9b1dSJustin T. Gibbs }
39938b8a9b1dSJustin T. Gibbs 
39948b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
39958b8a9b1dSJustin T. Gibbs 
3996ded2b706SWarner Losh static struct xpt_xport_ops xport_default_ops = {
399752c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
399852c9ce25SScott Long 	.action = xpt_action_default,
399952c9ce25SScott Long 	.async = xpt_dev_async_default,
400052c9ce25SScott Long };
4001ded2b706SWarner Losh static struct xpt_xport xport_default = {
4002ded2b706SWarner Losh 	.xport = XPORT_UNKNOWN,
4003ded2b706SWarner Losh 	.name = "unknown",
4004ded2b706SWarner Losh 	.ops = &xport_default_ops,
4005ded2b706SWarner Losh };
4006ded2b706SWarner Losh 
4007ded2b706SWarner Losh CAM_XPT_XPORT(xport_default);
400852c9ce25SScott Long 
40098b8a9b1dSJustin T. Gibbs /*
40108b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
40118b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
40128b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
4013db4fcadfSConrad Meyer  * for this new bus and places it in the array of buses and assigns
40148b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
40158b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
401602caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
40178b8a9b1dSJustin T. Gibbs  */
40188b8a9b1dSJustin T. Gibbs int32_t
4019b50569b7SScott Long xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
40208b8a9b1dSJustin T. Gibbs {
40218b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
4022434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
40238b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
402483c5d981SAlexander Motin 	struct cam_path *path;
402552c9ce25SScott Long 	cam_status status;
40268b8a9b1dSJustin T. Gibbs 
40278b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
40288b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4029227d67aaSAlexander Motin 					  M_CAMXPT, M_NOWAIT|M_ZERO);
40308b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
40318b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
40328b8a9b1dSJustin T. Gibbs 		return (CAM_RESRC_UNAVAIL);
40338b8a9b1dSJustin T. Gibbs 	}
40348b8a9b1dSJustin T. Gibbs 
4035227d67aaSAlexander Motin 	mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4036434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
4037fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
40388b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
403987cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
4040434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
4041a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
4042434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
404352c9ce25SScott Long 
40449a7c2696SAlexander Motin 	xpt_lock_buses();
40456dfc67e3SAlexander Motin 	sim->path_id = new_bus->path_id =
40466dfc67e3SAlexander Motin 	    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
40472b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4048434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
4049434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
4050434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
4051434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
4052434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4053434bbf6eSJustin T. Gibbs 	else
40542b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
40552b83592fSScott Long 	xsoftc.bus_generation++;
40569a7c2696SAlexander Motin 	xpt_unlock_buses();
40578b8a9b1dSJustin T. Gibbs 
405852c9ce25SScott Long 	/*
405952c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
406052c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
406152c9ce25SScott Long 	 * a more appropriate transport.
406252c9ce25SScott Long 	 */
406352c9ce25SScott Long 	new_bus->xport = &xport_default;
40648b8a9b1dSJustin T. Gibbs 
406532aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
40668b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4067627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
4068627995dcSAlexander Motin 		xpt_release_bus(new_bus);
4069627995dcSAlexander Motin 		return (CAM_RESRC_UNAVAIL);
4070627995dcSAlexander Motin 	}
407152c9ce25SScott Long 
4072762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
407352c9ce25SScott Long 
407452c9ce25SScott Long 	if (cpi.ccb_h.status == CAM_REQ_CMP) {
4075ded2b706SWarner Losh 		struct xpt_xport **xpt;
4076ded2b706SWarner Losh 
4077ded2b706SWarner Losh 		SET_FOREACH(xpt, cam_xpt_xport_set) {
4078ded2b706SWarner Losh 			if ((*xpt)->xport == cpi.transport) {
4079ded2b706SWarner Losh 				new_bus->xport = *xpt;
408052c9ce25SScott Long 				break;
4081ded2b706SWarner Losh 			}
4082ded2b706SWarner Losh 		}
4083ded2b706SWarner Losh 		if (new_bus->xport == NULL) {
4084bf1a8895SScott Long 			xpt_print(path,
4085bf1a8895SScott Long 			    "No transport found for %d\n", cpi.transport);
4086ded2b706SWarner Losh 			xpt_release_bus(new_bus);
4087ded2b706SWarner Losh 			free(path, M_CAMXPT);
4088ded2b706SWarner Losh 			return (CAM_RESRC_UNAVAIL);
40898b8a9b1dSJustin T. Gibbs 		}
409052c9ce25SScott Long 	}
409152c9ce25SScott Long 
409252c9ce25SScott Long 	/* Notify interested parties */
409352c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
409483c5d981SAlexander Motin 
409583c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
4096b01773b0SKenneth D. Merry 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4097b01773b0SKenneth D. Merry 			union	ccb *scan_ccb;
4098b01773b0SKenneth D. Merry 
409983c5d981SAlexander Motin 			/* Initiate bus rescan. */
410083c5d981SAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
4101e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
410283c5d981SAlexander Motin 				scan_ccb->ccb_h.path = path;
410383c5d981SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
410483c5d981SAlexander Motin 				scan_ccb->crcn.flags = 0;
410583c5d981SAlexander Motin 				xpt_rescan(scan_ccb);
41067f7aacb4SAlexander Motin 			} else {
4107b01773b0SKenneth D. Merry 				xpt_print(path,
4108b01773b0SKenneth D. Merry 					  "Can't allocate CCB to scan bus\n");
41097f7aacb4SAlexander Motin 				xpt_free_path(path);
41107f7aacb4SAlexander Motin 			}
4111b01773b0SKenneth D. Merry 		} else
4112b01773b0SKenneth D. Merry 			xpt_free_path(path);
4113e5736ac8SAlexander Motin 	} else
411483c5d981SAlexander Motin 		xpt_free_path(path);
41158b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
41168b8a9b1dSJustin T. Gibbs }
41178b8a9b1dSJustin T. Gibbs 
4118434bbf6eSJustin T. Gibbs int32_t
4119434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
41208b8a9b1dSJustin T. Gibbs {
4121434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
4122434bbf6eSJustin T. Gibbs 	cam_status status;
4123434bbf6eSJustin T. Gibbs 
4124434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
4125434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4126434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
4127434bbf6eSJustin T. Gibbs 		return (status);
4128434bbf6eSJustin T. Gibbs 
4129434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4130434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4131434bbf6eSJustin T. Gibbs 
4132434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
4133434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
4134434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
4135434bbf6eSJustin T. Gibbs 
4136434bbf6eSJustin T. Gibbs 	return (CAM_REQ_CMP);
4137434bbf6eSJustin T. Gibbs }
4138434bbf6eSJustin T. Gibbs 
4139434bbf6eSJustin T. Gibbs static path_id_t
4140434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
4141434bbf6eSJustin T. Gibbs {
4142434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
4143434bbf6eSJustin T. Gibbs 	path_id_t pathid;
41442398f0cdSPeter Wemm 	const char *strval;
41458b8a9b1dSJustin T. Gibbs 
41466dfc67e3SAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4147434bbf6eSJustin T. Gibbs 	pathid = 0;
41482b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4149434bbf6eSJustin T. Gibbs retry:
4150434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
41519e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
4152434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
4153434bbf6eSJustin T. Gibbs 			pathid++;
4154434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
4155434bbf6eSJustin T. Gibbs 	}
4156434bbf6eSJustin T. Gibbs 
4157434bbf6eSJustin T. Gibbs 	/*
4158434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
4159434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
4160434bbf6eSJustin T. Gibbs 	 */
416175f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4162434bbf6eSJustin T. Gibbs 		++pathid;
41638b8a9b1dSJustin T. Gibbs 		/* Start the search over */
4164434bbf6eSJustin T. Gibbs 		goto retry;
41658b8a9b1dSJustin T. Gibbs 	}
4166434bbf6eSJustin T. Gibbs 	return (pathid);
41678b8a9b1dSJustin T. Gibbs }
41688b8a9b1dSJustin T. Gibbs 
4169434bbf6eSJustin T. Gibbs static path_id_t
4170434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
41718b8a9b1dSJustin T. Gibbs {
41728b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
417375f51904SPeter Wemm 	int i, dunit, val;
4174642f0c46SPeter Wemm 	char buf[32];
41752398f0cdSPeter Wemm 	const char *dname;
41768b8a9b1dSJustin T. Gibbs 
41778b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
417875f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
41796dfc67e3SAlexander Motin 	if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
41806dfc67e3SAlexander Motin 		return (pathid);
41812398f0cdSPeter Wemm 	i = 0;
41822398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
41832398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
4184642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
4185642f0c46SPeter Wemm 			continue;
4186642f0c46SPeter Wemm 		}
418775f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
41888b8a9b1dSJustin T. Gibbs 			continue;
418975f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
419075f51904SPeter Wemm 			if (sim_bus == val) {
419175f51904SPeter Wemm 				pathid = dunit;
41928b8a9b1dSJustin T. Gibbs 				break;
41938b8a9b1dSJustin T. Gibbs 			}
41948b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
41958b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
419675f51904SPeter Wemm 			pathid = dunit;
41978b8a9b1dSJustin T. Gibbs 			break;
41988b8a9b1dSJustin T. Gibbs 		} else {
41998b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
42008b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
42018b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
42028b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
42038b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
420475f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
42058b8a9b1dSJustin T. Gibbs 			break;
42068b8a9b1dSJustin T. Gibbs 		}
42078b8a9b1dSJustin T. Gibbs 	}
42088b8a9b1dSJustin T. Gibbs 
4209434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4210434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
42118b8a9b1dSJustin T. Gibbs 	return (pathid);
42128b8a9b1dSJustin T. Gibbs }
42138b8a9b1dSJustin T. Gibbs 
421422c7d606SAlexander Motin static const char *
421522c7d606SAlexander Motin xpt_async_string(u_int32_t async_code)
421622c7d606SAlexander Motin {
421722c7d606SAlexander Motin 
421822c7d606SAlexander Motin 	switch (async_code) {
421922c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
422022c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
422122c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
422222c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
422322c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
422422c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
422522c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
422622c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
422722c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
422822c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
422922c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
423022c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
423122c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
42323631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
423322c7d606SAlexander Motin 	}
423422c7d606SAlexander Motin 	return ("AC_UNKNOWN");
423522c7d606SAlexander Motin }
423622c7d606SAlexander Motin 
4237227d67aaSAlexander Motin static int
4238227d67aaSAlexander Motin xpt_async_size(u_int32_t async_code)
42398b8a9b1dSJustin T. Gibbs {
42408b8a9b1dSJustin T. Gibbs 
4241227d67aaSAlexander Motin 	switch (async_code) {
4242227d67aaSAlexander Motin 	case AC_BUS_RESET: return (0);
4243227d67aaSAlexander Motin 	case AC_UNSOL_RESEL: return (0);
4244227d67aaSAlexander Motin 	case AC_SCSI_AEN: return (0);
4245227d67aaSAlexander Motin 	case AC_SENT_BDR: return (0);
4246227d67aaSAlexander Motin 	case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4247227d67aaSAlexander Motin 	case AC_PATH_DEREGISTERED: return (0);
4248227d67aaSAlexander Motin 	case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4249227d67aaSAlexander Motin 	case AC_LOST_DEVICE: return (0);
4250227d67aaSAlexander Motin 	case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4251227d67aaSAlexander Motin 	case AC_INQ_CHANGED: return (0);
4252227d67aaSAlexander Motin 	case AC_GETDEV_CHANGED: return (0);
4253227d67aaSAlexander Motin 	case AC_CONTRACT: return (sizeof(struct ac_contract));
4254227d67aaSAlexander Motin 	case AC_ADVINFO_CHANGED: return (-1);
4255227d67aaSAlexander Motin 	case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4256227d67aaSAlexander Motin 	}
4257227d67aaSAlexander Motin 	return (0);
4258227d67aaSAlexander Motin }
4259227d67aaSAlexander Motin 
4260227d67aaSAlexander Motin static int
4261227d67aaSAlexander Motin xpt_async_process_dev(struct cam_ed *device, void *arg)
4262227d67aaSAlexander Motin {
4263227d67aaSAlexander Motin 	union ccb *ccb = arg;
4264227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4265227d67aaSAlexander Motin 	void *async_arg = ccb->casync.async_arg_ptr;
4266227d67aaSAlexander Motin 	u_int32_t async_code = ccb->casync.async_code;
4267227d67aaSAlexander Motin 	int relock;
4268227d67aaSAlexander Motin 
4269227d67aaSAlexander Motin 	if (path->device != device
4270227d67aaSAlexander Motin 	 && path->device->lun_id != CAM_LUN_WILDCARD
4271227d67aaSAlexander Motin 	 && device->lun_id != CAM_LUN_WILDCARD)
4272227d67aaSAlexander Motin 		return (1);
42738b8a9b1dSJustin T. Gibbs 
4274a5479bc5SJustin T. Gibbs 	/*
4275227d67aaSAlexander Motin 	 * The async callback could free the device.
4276227d67aaSAlexander Motin 	 * If it is a broadcast async, it doesn't hold
4277227d67aaSAlexander Motin 	 * device reference, so take our own reference.
4278a5479bc5SJustin T. Gibbs 	 */
4279227d67aaSAlexander Motin 	xpt_acquire_device(device);
42808b8a9b1dSJustin T. Gibbs 
4281227d67aaSAlexander Motin 	/*
4282227d67aaSAlexander Motin 	 * If async for specific device is to be delivered to
4283227d67aaSAlexander Motin 	 * the wildcard client, take the specific device lock.
4284227d67aaSAlexander Motin 	 * XXX: We may need a way for client to specify it.
4285227d67aaSAlexander Motin 	 */
4286227d67aaSAlexander Motin 	if ((device->lun_id == CAM_LUN_WILDCARD &&
4287227d67aaSAlexander Motin 	     path->device->lun_id != CAM_LUN_WILDCARD) ||
4288227d67aaSAlexander Motin 	    (device->target->target_id == CAM_TARGET_WILDCARD &&
4289227d67aaSAlexander Motin 	     path->target->target_id != CAM_TARGET_WILDCARD) ||
4290227d67aaSAlexander Motin 	    (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4291227d67aaSAlexander Motin 	     path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4292227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
4293227d67aaSAlexander Motin 		xpt_path_lock(path);
4294227d67aaSAlexander Motin 		relock = 1;
4295227d67aaSAlexander Motin 	} else
4296227d67aaSAlexander Motin 		relock = 0;
4297227d67aaSAlexander Motin 
4298ded2b706SWarner Losh 	(*(device->target->bus->xport->ops->async))(async_code,
4299227d67aaSAlexander Motin 	    device->target->bus, device->target, device, async_arg);
4300227d67aaSAlexander Motin 	xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4301227d67aaSAlexander Motin 
4302227d67aaSAlexander Motin 	if (relock) {
4303227d67aaSAlexander Motin 		xpt_path_unlock(path);
4304227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
4305227d67aaSAlexander Motin 	}
4306227d67aaSAlexander Motin 	xpt_release_device(device);
4307227d67aaSAlexander Motin 	return (1);
4308227d67aaSAlexander Motin }
4309227d67aaSAlexander Motin 
4310227d67aaSAlexander Motin static int
4311227d67aaSAlexander Motin xpt_async_process_tgt(struct cam_et *target, void *arg)
4312227d67aaSAlexander Motin {
4313227d67aaSAlexander Motin 	union ccb *ccb = arg;
4314227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4315227d67aaSAlexander Motin 
4316227d67aaSAlexander Motin 	if (path->target != target
4317227d67aaSAlexander Motin 	 && path->target->target_id != CAM_TARGET_WILDCARD
4318227d67aaSAlexander Motin 	 && target->target_id != CAM_TARGET_WILDCARD)
4319227d67aaSAlexander Motin 		return (1);
4320227d67aaSAlexander Motin 
4321227d67aaSAlexander Motin 	if (ccb->casync.async_code == AC_SENT_BDR) {
4322227d67aaSAlexander Motin 		/* Update our notion of when the last reset occurred */
4323227d67aaSAlexander Motin 		microtime(&target->last_reset);
4324227d67aaSAlexander Motin 	}
4325227d67aaSAlexander Motin 
4326227d67aaSAlexander Motin 	return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4327227d67aaSAlexander Motin }
4328227d67aaSAlexander Motin 
4329227d67aaSAlexander Motin static void
4330227d67aaSAlexander Motin xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4331227d67aaSAlexander Motin {
4332227d67aaSAlexander Motin 	struct cam_eb *bus;
4333227d67aaSAlexander Motin 	struct cam_path *path;
4334227d67aaSAlexander Motin 	void *async_arg;
4335227d67aaSAlexander Motin 	u_int32_t async_code;
4336227d67aaSAlexander Motin 
4337227d67aaSAlexander Motin 	path = ccb->ccb_h.path;
4338227d67aaSAlexander Motin 	async_code = ccb->casync.async_code;
4339227d67aaSAlexander Motin 	async_arg = ccb->casync.async_arg_ptr;
4340227d67aaSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4341227d67aaSAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
43428b8a9b1dSJustin T. Gibbs 	bus = path->bus;
43438b8a9b1dSJustin T. Gibbs 
43448b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
434587cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
434687cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
43478b8a9b1dSJustin T. Gibbs 	}
43488b8a9b1dSJustin T. Gibbs 
4349227d67aaSAlexander Motin 	xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4350c8bead2aSJustin T. Gibbs 
4351c8bead2aSJustin T. Gibbs 	/*
4352c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4353c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4354c8bead2aSJustin T. Gibbs 	 */
4355227d67aaSAlexander Motin 	if (bus != xpt_periph->path->bus) {
4356227d67aaSAlexander Motin 		xpt_path_lock(xpt_periph->path);
4357227d67aaSAlexander Motin 		xpt_async_process_dev(xpt_periph->path->device, ccb);
4358227d67aaSAlexander Motin 		xpt_path_unlock(xpt_periph->path);
4359227d67aaSAlexander Motin 	}
4360227d67aaSAlexander Motin 
4361227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4362227d67aaSAlexander Motin 		xpt_release_devq(path, 1, TRUE);
4363227d67aaSAlexander Motin 	else
4364227d67aaSAlexander Motin 		xpt_release_simq(path->bus->sim, TRUE);
4365227d67aaSAlexander Motin 	if (ccb->casync.async_arg_size > 0)
4366227d67aaSAlexander Motin 		free(async_arg, M_CAMXPT);
4367227d67aaSAlexander Motin 	xpt_free_path(path);
4368227d67aaSAlexander Motin 	xpt_free_ccb(ccb);
43698b8a9b1dSJustin T. Gibbs }
43708b8a9b1dSJustin T. Gibbs 
43718b8a9b1dSJustin T. Gibbs static void
43728b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
43738b8a9b1dSJustin T. Gibbs 		u_int32_t async_code,
43748b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
43758b8a9b1dSJustin T. Gibbs {
43768b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
4377331d00baSAlexander Motin 	struct mtx *mtx;
43788b8a9b1dSJustin T. Gibbs 
43798b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
43808b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
43818b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
43828b8a9b1dSJustin T. Gibbs 		/*
43838b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
43848b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
43858b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
43868b8a9b1dSJustin T. Gibbs 		 */
43878b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
4388227d67aaSAlexander Motin 		if ((cur_entry->event_enable & async_code) != 0) {
4389331d00baSAlexander Motin 			mtx = cur_entry->event_lock ?
4390331d00baSAlexander Motin 			    path->device->sim->mtx : NULL;
4391331d00baSAlexander Motin 			if (mtx)
4392331d00baSAlexander Motin 				mtx_lock(mtx);
43938b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
43948b8a9b1dSJustin T. Gibbs 					    async_code, path,
43958b8a9b1dSJustin T. Gibbs 					    async_arg);
4396331d00baSAlexander Motin 			if (mtx)
4397331d00baSAlexander Motin 				mtx_unlock(mtx);
4398227d67aaSAlexander Motin 		}
43998b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
44008b8a9b1dSJustin T. Gibbs 	}
44018b8a9b1dSJustin T. Gibbs }
44028b8a9b1dSJustin T. Gibbs 
4403227d67aaSAlexander Motin void
4404227d67aaSAlexander Motin xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4405227d67aaSAlexander Motin {
4406227d67aaSAlexander Motin 	union ccb *ccb;
4407227d67aaSAlexander Motin 	int size;
4408227d67aaSAlexander Motin 
4409227d67aaSAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
4410227d67aaSAlexander Motin 	if (ccb == NULL) {
4411227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate CCB to send %s\n",
4412227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4413227d67aaSAlexander Motin 		return;
4414227d67aaSAlexander Motin 	}
4415227d67aaSAlexander Motin 
4416227d67aaSAlexander Motin 	if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4417227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate path to send %s\n",
4418227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4419227d67aaSAlexander Motin 		xpt_free_ccb(ccb);
4420227d67aaSAlexander Motin 		return;
4421227d67aaSAlexander Motin 	}
4422227d67aaSAlexander Motin 	ccb->ccb_h.path->periph = NULL;
4423227d67aaSAlexander Motin 	ccb->ccb_h.func_code = XPT_ASYNC;
4424227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_async_process;
4425227d67aaSAlexander Motin 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4426227d67aaSAlexander Motin 	ccb->casync.async_code = async_code;
4427227d67aaSAlexander Motin 	ccb->casync.async_arg_size = 0;
4428227d67aaSAlexander Motin 	size = xpt_async_size(async_code);
442969be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
443069be012fSWarner Losh 	    ("xpt_async: func %#x %s aync_code %d %s\n",
443169be012fSWarner Losh 		ccb->ccb_h.func_code,
443269be012fSWarner Losh 		xpt_action_name(ccb->ccb_h.func_code),
443369be012fSWarner Losh 		async_code,
443469be012fSWarner Losh 		xpt_async_string(async_code)));
4435227d67aaSAlexander Motin 	if (size > 0 && async_arg != NULL) {
4436227d67aaSAlexander Motin 		ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4437227d67aaSAlexander Motin 		if (ccb->casync.async_arg_ptr == NULL) {
4438227d67aaSAlexander Motin 			xpt_print(path, "Can't allocate argument to send %s\n",
4439227d67aaSAlexander Motin 			    xpt_async_string(async_code));
4440227d67aaSAlexander Motin 			xpt_free_path(ccb->ccb_h.path);
4441227d67aaSAlexander Motin 			xpt_free_ccb(ccb);
4442227d67aaSAlexander Motin 			return;
4443227d67aaSAlexander Motin 		}
4444227d67aaSAlexander Motin 		memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4445227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4446738fd166SAlan Somers 	} else if (size < 0) {
4447738fd166SAlan Somers 		ccb->casync.async_arg_ptr = async_arg;
4448227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4449738fd166SAlan Somers 	}
4450227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4451227d67aaSAlexander Motin 		xpt_freeze_devq(path, 1);
4452227d67aaSAlexander Motin 	else
4453227d67aaSAlexander Motin 		xpt_freeze_simq(path->bus->sim, 1);
4454*56eccd2dSWarner Losh 	xpt_action(ccb);
4455227d67aaSAlexander Motin }
4456227d67aaSAlexander Motin 
44572f22d08dSJustin T. Gibbs static void
445852c9ce25SScott Long xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
445952c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
446052c9ce25SScott Long 		      void *async_arg)
44612f22d08dSJustin T. Gibbs {
4462227d67aaSAlexander Motin 
4463227d67aaSAlexander Motin 	/*
4464227d67aaSAlexander Motin 	 * We only need to handle events for real devices.
4465227d67aaSAlexander Motin 	 */
4466227d67aaSAlexander Motin 	if (target->target_id == CAM_TARGET_WILDCARD
4467227d67aaSAlexander Motin 	 || device->lun_id == CAM_LUN_WILDCARD)
4468227d67aaSAlexander Motin 		return;
4469227d67aaSAlexander Motin 
4470b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
44712f22d08dSJustin T. Gibbs }
44722f22d08dSJustin T. Gibbs 
4473daa5487fSAlexander Motin static uint32_t
4474daa5487fSAlexander Motin xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4475daa5487fSAlexander Motin {
4476daa5487fSAlexander Motin 	struct cam_devq	*devq;
4477daa5487fSAlexander Motin 	uint32_t freeze;
4478daa5487fSAlexander Motin 
4479daa5487fSAlexander Motin 	devq = dev->sim->devq;
4480daa5487fSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4481daa5487fSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4482daa5487fSAlexander Motin 	    ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4483daa5487fSAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4484daa5487fSAlexander Motin 	freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4485daa5487fSAlexander Motin 	/* Remove frozen device from sendq. */
4486daa5487fSAlexander Motin 	if (device_is_queued(dev))
4487daa5487fSAlexander Motin 		camq_remove(&devq->send_queue, dev->devq_entry.index);
4488daa5487fSAlexander Motin 	return (freeze);
4489daa5487fSAlexander Motin }
4490daa5487fSAlexander Motin 
44918b8a9b1dSJustin T. Gibbs u_int32_t
4492cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
449383c5d981SAlexander Motin {
449483c5d981SAlexander Motin 	struct cam_ed	*dev = path->device;
4495227d67aaSAlexander Motin 	struct cam_devq	*devq;
4496227d67aaSAlexander Motin 	uint32_t	 freeze;
449783c5d981SAlexander Motin 
4498227d67aaSAlexander Motin 	devq = dev->sim->devq;
4499227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4500daa5487fSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4501daa5487fSAlexander Motin 	freeze = xpt_freeze_devq_device(dev, count);
4502227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4503227d67aaSAlexander Motin 	return (freeze);
45048b8a9b1dSJustin T. Gibbs }
45058b8a9b1dSJustin T. Gibbs 
45068b8a9b1dSJustin T. Gibbs u_int32_t
45078b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
45088b8a9b1dSJustin T. Gibbs {
4509227d67aaSAlexander Motin 	struct cam_devq	*devq;
4510227d67aaSAlexander Motin 	uint32_t	 freeze;
4511ec700f26SAlexander Motin 
4512227d67aaSAlexander Motin 	devq = sim->devq;
4513227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4514227d67aaSAlexander Motin 	freeze = (devq->send_queue.qfrozen_cnt += count);
4515227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4516227d67aaSAlexander Motin 	return (freeze);
45178b8a9b1dSJustin T. Gibbs }
45188b8a9b1dSJustin T. Gibbs 
45198b8a9b1dSJustin T. Gibbs static void
45208b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
45218b8a9b1dSJustin T. Gibbs {
4522227d67aaSAlexander Motin 	struct cam_ed *dev;
4523227d67aaSAlexander Motin 	struct cam_devq *devq;
45248b8a9b1dSJustin T. Gibbs 
4525227d67aaSAlexander Motin 	dev = (struct cam_ed *)arg;
4526227d67aaSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4527227d67aaSAlexander Motin 	devq = dev->sim->devq;
4528227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4529227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4530227d67aaSAlexander Motin 		xpt_run_devq(devq);
45318b8a9b1dSJustin T. Gibbs }
45328b8a9b1dSJustin T. Gibbs 
45338b8a9b1dSJustin T. Gibbs void
45342cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
45352cefde5fSJustin T. Gibbs {
4536227d67aaSAlexander Motin 	struct cam_ed *dev;
4537227d67aaSAlexander Motin 	struct cam_devq *devq;
453868153f43SScott Long 
45390d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
45400d4f3c31SAlexander Motin 	    count, run_queue));
4541227d67aaSAlexander Motin 	dev = path->device;
4542227d67aaSAlexander Motin 	devq = dev->sim->devq;
4543227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4544227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, count, run_queue))
4545227d67aaSAlexander Motin 		xpt_run_devq(dev->sim->devq);
4546227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
454783c5d981SAlexander Motin }
454883c5d981SAlexander Motin 
4549227d67aaSAlexander Motin static int
4550cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
45518b8a9b1dSJustin T. Gibbs {
45528b8a9b1dSJustin T. Gibbs 
4553227d67aaSAlexander Motin 	mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
45540d4f3c31SAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
45550d4f3c31SAlexander Motin 	    ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
45560d4f3c31SAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4557cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
455883c5d981SAlexander Motin #ifdef INVARIANTS
4559cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4560cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
456183c5d981SAlexander Motin #endif
4562cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
456383c5d981SAlexander Motin 	}
4564cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4565cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
45668b8a9b1dSJustin T. Gibbs 		/*
45678b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
45688b8a9b1dSJustin T. Gibbs 		 * command completion.
45698b8a9b1dSJustin T. Gibbs 		 */
45708b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
45718b8a9b1dSJustin T. Gibbs 		/*
45728b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
45738b8a9b1dSJustin T. Gibbs 		 * to release this queue.
45748b8a9b1dSJustin T. Gibbs 		 */
45758b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
45762b83592fSScott Long 			callout_stop(&dev->callout);
45778b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
45788b8a9b1dSJustin T. Gibbs 		}
45798b8a9b1dSJustin T. Gibbs 		/*
45808b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
45818b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
45828b8a9b1dSJustin T. Gibbs 		 * run.
45838b8a9b1dSJustin T. Gibbs 		 */
4584227d67aaSAlexander Motin 		xpt_schedule_devq(dev->sim->devq, dev);
4585227d67aaSAlexander Motin 	} else
4586227d67aaSAlexander Motin 		run_queue = 0;
4587227d67aaSAlexander Motin 	return (run_queue);
458883c5d981SAlexander Motin }
45898b8a9b1dSJustin T. Gibbs 
45908b8a9b1dSJustin T. Gibbs void
45918b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
45928b8a9b1dSJustin T. Gibbs {
4593227d67aaSAlexander Motin 	struct cam_devq	*devq;
45948b8a9b1dSJustin T. Gibbs 
4595227d67aaSAlexander Motin 	devq = sim->devq;
4596227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4597227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt <= 0) {
459883c5d981SAlexander Motin #ifdef INVARIANTS
459983c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4600227d67aaSAlexander Motin 		    devq->send_queue.qfrozen_cnt);
460183c5d981SAlexander Motin #endif
460283c5d981SAlexander Motin 	} else
4603227d67aaSAlexander Motin 		devq->send_queue.qfrozen_cnt--;
4604227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt == 0) {
46058b8a9b1dSJustin T. Gibbs 		/*
46068b8a9b1dSJustin T. Gibbs 		 * If there is a timeout scheduled to release this
46078b8a9b1dSJustin T. Gibbs 		 * sim queue, remove it.  The queue frozen count is
46088b8a9b1dSJustin T. Gibbs 		 * already at 0.
46098b8a9b1dSJustin T. Gibbs 		 */
46108b8a9b1dSJustin T. Gibbs 		if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
46112b83592fSScott Long 			callout_stop(&sim->callout);
46128b8a9b1dSJustin T. Gibbs 			sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
46138b8a9b1dSJustin T. Gibbs 		}
46148b8a9b1dSJustin T. Gibbs 		if (run_queue) {
46158b8a9b1dSJustin T. Gibbs 			/*
46168b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
46178b8a9b1dSJustin T. Gibbs 			 */
4618cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
461977dc25ccSScott Long 		}
462077dc25ccSScott Long 	}
4621227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
46228b8a9b1dSJustin T. Gibbs }
46238b8a9b1dSJustin T. Gibbs 
46248b8a9b1dSJustin T. Gibbs void
4625a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
46268b8a9b1dSJustin T. Gibbs {
4627227d67aaSAlexander Motin 	struct cam_doneq *queue;
4628227d67aaSAlexander Motin 	int	run, hash;
46298b8a9b1dSJustin T. Gibbs 
46308532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
46318532d381SConrad Meyer 	if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
46328532d381SConrad Meyer 	    done_ccb->csio.bio != NULL)
46338532d381SConrad Meyer 		biotrack(done_ccb->csio.bio, __func__);
46348532d381SConrad Meyer #endif
46358532d381SConrad Meyer 
463669be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
463769be012fSWarner Losh 	    ("xpt_done: func= %#x %s status %#x\n",
463869be012fSWarner Losh 		done_ccb->ccb_h.func_code,
463969be012fSWarner Losh 		xpt_action_name(done_ccb->ccb_h.func_code),
464069be012fSWarner Losh 		done_ccb->ccb_h.status));
4641227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4642227d67aaSAlexander Motin 		return;
4643227d67aaSAlexander Motin 
4644a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4645e4c9cba7SWarner Losh 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4646227d67aaSAlexander Motin 	hash = (done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4647227d67aaSAlexander Motin 	    done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4648227d67aaSAlexander Motin 	queue = &cam_doneqs[hash];
4649227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
4650227d67aaSAlexander Motin 	run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4651227d67aaSAlexander Motin 	STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
46528b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4653227d67aaSAlexander Motin 	mtx_unlock(&queue->cam_doneq_mtx);
4654227d67aaSAlexander Motin 	if (run)
4655227d67aaSAlexander Motin 		wakeup(&queue->cam_doneq);
46568b8a9b1dSJustin T. Gibbs }
46578b8a9b1dSJustin T. Gibbs 
4658711f6613SAlexander Motin void
4659227d67aaSAlexander Motin xpt_done_direct(union ccb *done_ccb)
4660711f6613SAlexander Motin {
4661711f6613SAlexander Motin 
466269be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
466369be012fSWarner Losh 	    ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4664227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4665227d67aaSAlexander Motin 		return;
4666711f6613SAlexander Motin 
4667a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4668e4c9cba7SWarner Losh 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4669227d67aaSAlexander Motin 	xpt_done_process(&done_ccb->ccb_h);
4670711f6613SAlexander Motin }
4671711f6613SAlexander Motin 
46728b8a9b1dSJustin T. Gibbs union ccb *
46738008a935SScott Long xpt_alloc_ccb()
46748b8a9b1dSJustin T. Gibbs {
46758b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
46768b8a9b1dSJustin T. Gibbs 
4677596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4678362abc44STai-hwa Liang 	return (new_ccb);
4679362abc44STai-hwa Liang }
4680362abc44STai-hwa Liang 
4681362abc44STai-hwa Liang union ccb *
46828008a935SScott Long xpt_alloc_ccb_nowait()
4683362abc44STai-hwa Liang {
4684362abc44STai-hwa Liang 	union ccb *new_ccb;
4685362abc44STai-hwa Liang 
4686596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
46878b8a9b1dSJustin T. Gibbs 	return (new_ccb);
46888b8a9b1dSJustin T. Gibbs }
46898b8a9b1dSJustin T. Gibbs 
46908b8a9b1dSJustin T. Gibbs void
46918b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
46928b8a9b1dSJustin T. Gibbs {
4693596ee08fSAlexander Motin 	free(free_ccb, M_CAMCCB);
46948b8a9b1dSJustin T. Gibbs }
46958b8a9b1dSJustin T. Gibbs 
46968b8a9b1dSJustin T. Gibbs 
46978b8a9b1dSJustin T. Gibbs 
46988b8a9b1dSJustin T. Gibbs /* Private XPT functions */
46998b8a9b1dSJustin T. Gibbs 
47008b8a9b1dSJustin T. Gibbs /*
47018b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
4702227d67aaSAlexander Motin  * referenced by the path.  If we don't have sufficient resources to allocate
4703227d67aaSAlexander Motin  * more ccbs, we return NULL.
47048b8a9b1dSJustin T. Gibbs  */
47058b8a9b1dSJustin T. Gibbs static union ccb *
4706227d67aaSAlexander Motin xpt_get_ccb_nowait(struct cam_periph *periph)
47078b8a9b1dSJustin T. Gibbs {
47088b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
47098b8a9b1dSJustin T. Gibbs 
4710d3995fddSBenno Rice 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4711227d67aaSAlexander Motin 	if (new_ccb == NULL)
47128b8a9b1dSJustin T. Gibbs 		return (NULL);
4713227d67aaSAlexander Motin 	periph->periph_allocated++;
4714227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
47158b8a9b1dSJustin T. Gibbs 	return (new_ccb);
47168b8a9b1dSJustin T. Gibbs }
47178b8a9b1dSJustin T. Gibbs 
4718227d67aaSAlexander Motin static union ccb *
4719227d67aaSAlexander Motin xpt_get_ccb(struct cam_periph *periph)
4720227d67aaSAlexander Motin {
4721227d67aaSAlexander Motin 	union ccb *new_ccb;
4722227d67aaSAlexander Motin 
4723227d67aaSAlexander Motin 	cam_periph_unlock(periph);
4724d3995fddSBenno Rice 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4725227d67aaSAlexander Motin 	cam_periph_lock(periph);
4726227d67aaSAlexander Motin 	periph->periph_allocated++;
4727227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4728227d67aaSAlexander Motin 	return (new_ccb);
4729227d67aaSAlexander Motin }
4730227d67aaSAlexander Motin 
4731227d67aaSAlexander Motin union ccb *
4732227d67aaSAlexander Motin cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4733227d67aaSAlexander Motin {
4734227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
4735227d67aaSAlexander Motin 
4736227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4737227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4738227d67aaSAlexander Motin 	while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4739227d67aaSAlexander Motin 	    ccb_h->pinfo.priority != priority) {
4740227d67aaSAlexander Motin 		if (priority < periph->immediate_priority) {
4741227d67aaSAlexander Motin 			periph->immediate_priority = priority;
4742227d67aaSAlexander Motin 			xpt_run_allocq(periph, 0);
4743227d67aaSAlexander Motin 		} else
4744227d67aaSAlexander Motin 			cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4745227d67aaSAlexander Motin 			    "cgticb", 0);
4746227d67aaSAlexander Motin 	}
4747227d67aaSAlexander Motin 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4748227d67aaSAlexander Motin 	return ((union ccb *)ccb_h);
4749227d67aaSAlexander Motin }
4750227d67aaSAlexander Motin 
4751227d67aaSAlexander Motin static void
4752227d67aaSAlexander Motin xpt_acquire_bus(struct cam_eb *bus)
4753227d67aaSAlexander Motin {
4754227d67aaSAlexander Motin 
4755227d67aaSAlexander Motin 	xpt_lock_buses();
4756227d67aaSAlexander Motin 	bus->refcount++;
4757227d67aaSAlexander Motin 	xpt_unlock_buses();
4758227d67aaSAlexander Motin }
4759227d67aaSAlexander Motin 
4760a5479bc5SJustin T. Gibbs static void
4761a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4762a5479bc5SJustin T. Gibbs {
4763a5479bc5SJustin T. Gibbs 
47649a7c2696SAlexander Motin 	xpt_lock_buses();
476515975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4766dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4767dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4768dcdf6e74SAlexander Motin 		return;
4769dcdf6e74SAlexander Motin 	}
47702b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
47712b83592fSScott Long 	xsoftc.bus_generation++;
47729a7c2696SAlexander Motin 	xpt_unlock_buses();
4773227d67aaSAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4774227d67aaSAlexander Motin 	    ("destroying bus, but target list is not empty"));
4775fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4776227d67aaSAlexander Motin 	mtx_destroy(&bus->eb_mtx);
4777362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4778a5479bc5SJustin T. Gibbs }
47798b8a9b1dSJustin T. Gibbs 
47808b8a9b1dSJustin T. Gibbs static struct cam_et *
47818b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
47828b8a9b1dSJustin T. Gibbs {
4783dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
47848b8a9b1dSJustin T. Gibbs 
4785227d67aaSAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4786227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
47873501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
47883501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4789dcdf6e74SAlexander Motin 	if (target == NULL)
4790dcdf6e74SAlexander Motin 		return (NULL);
47918b8a9b1dSJustin T. Gibbs 
4792434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
47938b8a9b1dSJustin T. Gibbs 	target->bus = bus;
47948b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
47958b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4796434bbf6eSJustin T. Gibbs 	target->generation = 0;
479782b361b1SMatt Jacob 	target->luns = NULL;
4798227d67aaSAlexander Motin 	mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4799434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4800a5479bc5SJustin T. Gibbs 	/*
4801a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4802a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4803a5479bc5SJustin T. Gibbs 	 */
4804a5479bc5SJustin T. Gibbs 	bus->refcount++;
48058b8a9b1dSJustin T. Gibbs 
48068b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
48078b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
48088b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
48098b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
48108b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
48118b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
48128b8a9b1dSJustin T. Gibbs 	} else {
48138b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
48148b8a9b1dSJustin T. Gibbs 	}
4815a5479bc5SJustin T. Gibbs 	bus->generation++;
48168b8a9b1dSJustin T. Gibbs 	return (target);
48178b8a9b1dSJustin T. Gibbs }
48188b8a9b1dSJustin T. Gibbs 
4819a5479bc5SJustin T. Gibbs static void
4820227d67aaSAlexander Motin xpt_acquire_target(struct cam_et *target)
4821227d67aaSAlexander Motin {
4822227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4823227d67aaSAlexander Motin 
4824227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4825227d67aaSAlexander Motin 	target->refcount++;
4826227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4827227d67aaSAlexander Motin }
4828227d67aaSAlexander Motin 
4829227d67aaSAlexander Motin static void
4830f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
48318b8a9b1dSJustin T. Gibbs {
4832227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4833a5479bc5SJustin T. Gibbs 
4834227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4835227d67aaSAlexander Motin 	if (--target->refcount > 0) {
4836227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4837dcdf6e74SAlexander Motin 		return;
4838227d67aaSAlexander Motin 	}
4839227d67aaSAlexander Motin 	TAILQ_REMOVE(&bus->et_entries, target, links);
4840227d67aaSAlexander Motin 	bus->generation++;
4841227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4842dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4843227d67aaSAlexander Motin 	    ("destroying target, but device list is not empty"));
4844227d67aaSAlexander Motin 	xpt_release_bus(bus);
4845227d67aaSAlexander Motin 	mtx_destroy(&target->luns_mtx);
484682b361b1SMatt Jacob 	if (target->luns)
484782b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4848362abc44STai-hwa Liang 	free(target, M_CAMXPT);
484977dc25ccSScott Long }
48508b8a9b1dSJustin T. Gibbs 
48518b8a9b1dSJustin T. Gibbs static struct cam_ed *
485252c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
485352c9ce25SScott Long 			 lun_id_t lun_id)
485452c9ce25SScott Long {
4855dcdf6e74SAlexander Motin 	struct cam_ed *device;
485652c9ce25SScott Long 
485752c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
485852c9ce25SScott Long 	if (device == NULL)
485952c9ce25SScott Long 		return (NULL);
486052c9ce25SScott Long 
486152c9ce25SScott Long 	device->mintags = 1;
486252c9ce25SScott Long 	device->maxtags = 1;
486352c9ce25SScott Long 	return (device);
486452c9ce25SScott Long }
486552c9ce25SScott Long 
4866227d67aaSAlexander Motin static void
4867227d67aaSAlexander Motin xpt_destroy_device(void *context, int pending)
4868227d67aaSAlexander Motin {
4869227d67aaSAlexander Motin 	struct cam_ed	*device = context;
4870227d67aaSAlexander Motin 
4871227d67aaSAlexander Motin 	mtx_lock(&device->device_mtx);
4872227d67aaSAlexander Motin 	mtx_destroy(&device->device_mtx);
4873227d67aaSAlexander Motin 	free(device, M_CAMDEV);
4874227d67aaSAlexander Motin }
4875227d67aaSAlexander Motin 
487652c9ce25SScott Long struct cam_ed *
48778b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
48788b8a9b1dSJustin T. Gibbs {
4879dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
48808b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4881a5479bc5SJustin T. Gibbs 	cam_status status;
48828b8a9b1dSJustin T. Gibbs 
4883227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
48848b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
48858b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4886227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4887cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4888227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4889dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4890dcdf6e74SAlexander Motin 		return (NULL);
48918b8a9b1dSJustin T. Gibbs 
48928b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4893596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4894dcdf6e74SAlexander Motin 	if (device == NULL)
4895dcdf6e74SAlexander Motin 		return (NULL);
48968b8a9b1dSJustin T. Gibbs 
4897227d67aaSAlexander Motin 	cam_init_pinfo(&device->devq_entry);
48988b8a9b1dSJustin T. Gibbs 	device->target = target;
48998b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
49002b83592fSScott Long 	device->sim = bus->sim;
49018b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
49028b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
4903596ee08fSAlexander Motin 		free(device, M_CAMDEV);
49048b8a9b1dSJustin T. Gibbs 		return (NULL);
49058b8a9b1dSJustin T. Gibbs 	}
4906434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4907434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4908434bbf6eSJustin T. Gibbs 	device->generation = 0;
4909434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4910434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4911df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4912434bbf6eSJustin T. Gibbs 	device->refcount = 1;
4913227d67aaSAlexander Motin 	mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4914227d67aaSAlexander Motin 	callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4915227d67aaSAlexander Motin 	TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4916227d67aaSAlexander Motin 	/*
4917227d67aaSAlexander Motin 	 * Hold a reference to our parent bus so it
4918227d67aaSAlexander Motin 	 * will not go away before we do.
4919227d67aaSAlexander Motin 	 */
4920227d67aaSAlexander Motin 	target->refcount++;
4921434bbf6eSJustin T. Gibbs 
4922dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4923dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4924dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4925dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4926dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4927dcdf6e74SAlexander Motin 	else
4928dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4929dcdf6e74SAlexander Motin 	target->generation++;
49308b8a9b1dSJustin T. Gibbs 	return (device);
49318b8a9b1dSJustin T. Gibbs }
49328b8a9b1dSJustin T. Gibbs 
4933f98d7a47SAlexander Motin void
4934f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
49358b8a9b1dSJustin T. Gibbs {
4936227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
49378b8a9b1dSJustin T. Gibbs 
4938227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4939f98d7a47SAlexander Motin 	device->refcount++;
4940227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4941f98d7a47SAlexander Motin }
4942f98d7a47SAlexander Motin 
4943f98d7a47SAlexander Motin void
4944f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4945f98d7a47SAlexander Motin {
4946227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
49478b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
49488b8a9b1dSJustin T. Gibbs 
4949227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4950227d67aaSAlexander Motin 	if (--device->refcount > 0) {
4951227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4952dcdf6e74SAlexander Motin 		return;
4953227d67aaSAlexander Motin 	}
4954227d67aaSAlexander Motin 
4955227d67aaSAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4956227d67aaSAlexander Motin 	device->target->generation++;
4957227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4958227d67aaSAlexander Motin 
4959227d67aaSAlexander Motin 	/* Release our slot in the devq */
4960227d67aaSAlexander Motin 	devq = bus->sim->devq;
4961227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4962227d67aaSAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
4963227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4964dcdf6e74SAlexander Motin 
4965dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4966227d67aaSAlexander Motin 	    ("destroying device, but periphs list is not empty"));
4967227d67aaSAlexander Motin 	KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4968227d67aaSAlexander Motin 	    ("destroying device while still queued for ccbs"));
49692cefde5fSJustin T. Gibbs 
49702cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
49712b83592fSScott Long 		callout_stop(&device->callout);
49722cefde5fSJustin T. Gibbs 
4973227d67aaSAlexander Motin 	xpt_release_target(device->target);
4974227d67aaSAlexander Motin 
497520a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4976e6bd5983SKenneth D. Merry 	/*
4977e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4978e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4979e6bd5983SKenneth D. Merry 	 * checking.
4980e6bd5983SKenneth D. Merry 	 */
4981e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4982e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
49833bba3152SKenneth D. Merry 	free(device->ext_inq, M_CAMXPT);
4984e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4985e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4986e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4987f439e3a4SAlexander Motin 	free(device->nvme_data, M_CAMXPT);
4988f439e3a4SAlexander Motin 	free(device->nvme_cdata, M_CAMXPT);
4989227d67aaSAlexander Motin 	taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
49908b8a9b1dSJustin T. Gibbs }
49918b8a9b1dSJustin T. Gibbs 
499252c9ce25SScott Long u_int32_t
49938b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
49948b8a9b1dSJustin T. Gibbs {
49958b8a9b1dSJustin T. Gibbs 	int	result;
49968b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
49978b8a9b1dSJustin T. Gibbs 
49988b8a9b1dSJustin T. Gibbs 	dev = path->device;
4999227d67aaSAlexander Motin 	mtx_lock(&dev->sim->devq->send_mtx);
50008b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
5001227d67aaSAlexander Motin 	mtx_unlock(&dev->sim->devq->send_mtx);
5002df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5003df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
5004df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
50058b8a9b1dSJustin T. Gibbs 	return (result);
50068b8a9b1dSJustin T. Gibbs }
50078b8a9b1dSJustin T. Gibbs 
50088b8a9b1dSJustin T. Gibbs static struct cam_eb *
50098b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
50108b8a9b1dSJustin T. Gibbs {
50118b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
50128b8a9b1dSJustin T. Gibbs 
50139a7c2696SAlexander Motin 	xpt_lock_buses();
50142b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
50158b8a9b1dSJustin T. Gibbs 	     bus != NULL;
50168b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
5017a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
5018a5479bc5SJustin T. Gibbs 			bus->refcount++;
50198b8a9b1dSJustin T. Gibbs 			break;
50208b8a9b1dSJustin T. Gibbs 		}
5021a5479bc5SJustin T. Gibbs 	}
50229a7c2696SAlexander Motin 	xpt_unlock_buses();
50238b8a9b1dSJustin T. Gibbs 	return (bus);
50248b8a9b1dSJustin T. Gibbs }
50258b8a9b1dSJustin T. Gibbs 
50268b8a9b1dSJustin T. Gibbs static struct cam_et *
50278b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
50288b8a9b1dSJustin T. Gibbs {
50298b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
50308b8a9b1dSJustin T. Gibbs 
5031227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
50328b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
50338b8a9b1dSJustin T. Gibbs 	     target != NULL;
50348b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
50358b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
50368b8a9b1dSJustin T. Gibbs 			target->refcount++;
50378b8a9b1dSJustin T. Gibbs 			break;
50388b8a9b1dSJustin T. Gibbs 		}
50398b8a9b1dSJustin T. Gibbs 	}
50408b8a9b1dSJustin T. Gibbs 	return (target);
50418b8a9b1dSJustin T. Gibbs }
50428b8a9b1dSJustin T. Gibbs 
50438b8a9b1dSJustin T. Gibbs static struct cam_ed *
50448b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
50458b8a9b1dSJustin T. Gibbs {
50468b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
50478b8a9b1dSJustin T. Gibbs 
5048227d67aaSAlexander Motin 	mtx_assert(&target->bus->eb_mtx, MA_OWNED);
50498b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
50508b8a9b1dSJustin T. Gibbs 	     device != NULL;
50518b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
50528b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
50538b8a9b1dSJustin T. Gibbs 			device->refcount++;
50548b8a9b1dSJustin T. Gibbs 			break;
50558b8a9b1dSJustin T. Gibbs 		}
50568b8a9b1dSJustin T. Gibbs 	}
50578b8a9b1dSJustin T. Gibbs 	return (device);
50588b8a9b1dSJustin T. Gibbs }
50598b8a9b1dSJustin T. Gibbs 
506030a4094fSAlexander Motin void
5061f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
5062f0adc790SJustin T. Gibbs {
5063fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
5064fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
5065fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
5066fd21cc5eSJustin T. Gibbs 	int    newopenings;
5067fd21cc5eSJustin T. Gibbs 
5068fd21cc5eSJustin T. Gibbs 	device = path->device;
5069fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
5070fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5071fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
5072fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
5073df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
5074df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
5075df8f9080SJustin T. Gibbs 	else
507652c9ce25SScott Long 		newopenings = min(device->maxtags,
5077df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
5078f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
5079581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
5080bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5081fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
5082fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5083fd21cc5eSJustin T. Gibbs 	crs.openings
5084fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
5085fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
5086fd21cc5eSJustin T. Gibbs 	    = 0;
5087fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
5088fd21cc5eSJustin T. Gibbs }
5089fd21cc5eSJustin T. Gibbs 
509030a4094fSAlexander Motin void
509130a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
509230a4094fSAlexander Motin {
509330a4094fSAlexander Motin 	struct ccb_relsim crs;
509430a4094fSAlexander Motin 	struct cam_ed *device;
509530a4094fSAlexander Motin 	struct cam_sim *sim;
509630a4094fSAlexander Motin 
509730a4094fSAlexander Motin 	device = path->device;
509830a4094fSAlexander Motin 	sim = path->bus->sim;
509930a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
510030a4094fSAlexander Motin 	device->tag_delay_count = 0;
510130a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
510230a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
510330a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5104581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
510530a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
510630a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
510730a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
510830a4094fSAlexander Motin 	crs.openings
510930a4094fSAlexander Motin 	    = crs.release_timeout
511030a4094fSAlexander Motin 	    = crs.qfrozen_cnt
511130a4094fSAlexander Motin 	    = 0;
511230a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
511330a4094fSAlexander Motin }
511430a4094fSAlexander Motin 
5115a4876fbfSAlexander Motin /*
5116a4876fbfSAlexander Motin  * Assume all possible buses are detected by this time, so allow boot
5117a4876fbfSAlexander Motin  * as soon as they all are scanned.
5118a4876fbfSAlexander Motin  */
511983c5d981SAlexander Motin static void
512083c5d981SAlexander Motin xpt_boot_delay(void *arg)
51218b8a9b1dSJustin T. Gibbs {
51222b83592fSScott Long 
512383c5d981SAlexander Motin 	xpt_release_boot();
51248b8a9b1dSJustin T. Gibbs }
51258b8a9b1dSJustin T. Gibbs 
5126a4876fbfSAlexander Motin /*
5127a4876fbfSAlexander Motin  * Now that all config hooks have completed, start boot_delay timer,
5128a4876fbfSAlexander Motin  * waiting for possibly still undetected buses (USB) to appear.
5129a4876fbfSAlexander Motin  */
51308b8a9b1dSJustin T. Gibbs static void
5131a4876fbfSAlexander Motin xpt_ch_done(void *arg)
51328b8a9b1dSJustin T. Gibbs {
5133a4876fbfSAlexander Motin 
5134a4876fbfSAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
5135a4876fbfSAlexander Motin 	callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5136a4876fbfSAlexander Motin 	    xpt_boot_delay, NULL, 0);
5137a4876fbfSAlexander Motin }
5138a4876fbfSAlexander Motin SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5139a4876fbfSAlexander Motin 
51403393f8daSKenneth D. Merry /*
51413393f8daSKenneth D. Merry  * Now that interrupts are enabled, go find our devices
51423393f8daSKenneth D. Merry  */
5143a4876fbfSAlexander Motin static void
5144a4876fbfSAlexander Motin xpt_config(void *arg)
5145a4876fbfSAlexander Motin {
5146227d67aaSAlexander Motin 	if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5147227d67aaSAlexander Motin 		printf("xpt_config: failed to create taskqueue thread.\n");
51488b8a9b1dSJustin T. Gibbs 
5149f0f25b9cSAlexander Motin 	/* Setup debugging path */
51508b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
5151227d67aaSAlexander Motin 		if (xpt_create_path(&cam_dpath, NULL,
51528b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
51538b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
51548b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
51558b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
51568b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
51578b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
51588b8a9b1dSJustin T. Gibbs 		}
51598b8a9b1dSJustin T. Gibbs 	} else
51608b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
51618b8a9b1dSJustin T. Gibbs 
516283c5d981SAlexander Motin 	periphdriver_init(1);
516383c5d981SAlexander Motin 	xpt_hold_boot();
5164a4876fbfSAlexander Motin 
516583c5d981SAlexander Motin 	/* Fire up rescan thread. */
5166227d67aaSAlexander Motin 	if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5167227d67aaSAlexander Motin 	    "cam", "scanner")) {
51686f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
51691e637ba6SAlexander Motin 	}
517083c5d981SAlexander Motin }
51718b8a9b1dSJustin T. Gibbs 
517283c5d981SAlexander Motin void
5173a4876fbfSAlexander Motin xpt_hold_boot_locked(void)
5174a4876fbfSAlexander Motin {
5175a4876fbfSAlexander Motin 
5176a4876fbfSAlexander Motin 	if (xsoftc.buses_to_config++ == 0)
5177a4876fbfSAlexander Motin 		root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5178a4876fbfSAlexander Motin }
5179a4876fbfSAlexander Motin 
5180a4876fbfSAlexander Motin void
518183c5d981SAlexander Motin xpt_hold_boot(void)
518283c5d981SAlexander Motin {
5183a4876fbfSAlexander Motin 
518483c5d981SAlexander Motin 	xpt_lock_buses();
5185a4876fbfSAlexander Motin 	xpt_hold_boot_locked();
518683c5d981SAlexander Motin 	xpt_unlock_buses();
518783c5d981SAlexander Motin }
518883c5d981SAlexander Motin 
518983c5d981SAlexander Motin void
519083c5d981SAlexander Motin xpt_release_boot(void)
519183c5d981SAlexander Motin {
519283c5d981SAlexander Motin 
5193a4876fbfSAlexander Motin 	xpt_lock_buses();
5194a4876fbfSAlexander Motin 	if (--xsoftc.buses_to_config == 0) {
5195a4876fbfSAlexander Motin 		if (xsoftc.buses_config_done == 0) {
519683c5d981SAlexander Motin 			xsoftc.buses_config_done = 1;
5197a4876fbfSAlexander Motin 			xsoftc.buses_to_config++;
5198a4876fbfSAlexander Motin 			TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5199a4876fbfSAlexander Motin 			    NULL);
5200a4876fbfSAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
520183c5d981SAlexander Motin 		} else
5202a4876fbfSAlexander Motin 			root_mount_rel(&xsoftc.xpt_rootmount);
5203a4876fbfSAlexander Motin 	}
520483c5d981SAlexander Motin 	xpt_unlock_buses();
52052863f7b1SJustin T. Gibbs }
52068b8a9b1dSJustin T. Gibbs 
52078b8a9b1dSJustin T. Gibbs /*
52088b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
52098b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
52108b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
52118b8a9b1dSJustin T. Gibbs  */
52128b8a9b1dSJustin T. Gibbs static int
52138b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
52148b8a9b1dSJustin T. Gibbs {
52158b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
52168b8a9b1dSJustin T. Gibbs 	int i;
52178b8a9b1dSJustin T. Gibbs 
52188b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
52198b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
52208b8a9b1dSJustin T. Gibbs 
52218b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
52228b8a9b1dSJustin T. Gibbs 	if ((i == 1)
52238b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
52248b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
52258b8a9b1dSJustin T. Gibbs 
52268b8a9b1dSJustin T. Gibbs 	return(1);
52278b8a9b1dSJustin T. Gibbs }
52288b8a9b1dSJustin T. Gibbs 
52298b8a9b1dSJustin T. Gibbs static void
52302b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
52318b8a9b1dSJustin T. Gibbs {
52328b8a9b1dSJustin T. Gibbs 
523383c5d981SAlexander Motin 	periphdriver_init(2);
52342b83592fSScott Long 	/*
52352b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
52362b83592fSScott Long 	 * attached.  For any devices like that, announce the
52372b83592fSScott Long 	 * passthrough driver so the user will see something.
52382b83592fSScott Long 	 */
52393089bb2eSAlexander Motin 	if (!bootverbose)
52402b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
52412b83592fSScott Long 
5242a4876fbfSAlexander Motin 	xpt_release_boot();
52432b83592fSScott Long }
52442b83592fSScott Long 
524585d92640SScott Long cam_status
524685d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
524785d92640SScott Long 		   struct cam_path *path)
524885d92640SScott Long {
524985d92640SScott Long 	struct ccb_setasync csa;
525085d92640SScott Long 	cam_status status;
525185d92640SScott Long 	int xptpath = 0;
525285d92640SScott Long 
525385d92640SScott Long 	if (path == NULL) {
525485d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
525585d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5256227d67aaSAlexander Motin 		if (status != CAM_REQ_CMP)
525785d92640SScott Long 			return (status);
5258227d67aaSAlexander Motin 		xpt_path_lock(path);
525985d92640SScott Long 		xptpath = 1;
526085d92640SScott Long 	}
526185d92640SScott Long 
526283c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
526385d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
526485d92640SScott Long 	csa.event_enable = event;
526585d92640SScott Long 	csa.callback = cbfunc;
526685d92640SScott Long 	csa.callback_arg = cbarg;
526785d92640SScott Long 	xpt_action((union ccb *)&csa);
526885d92640SScott Long 	status = csa.ccb_h.status;
52693501942bSJustin T. Gibbs 
527069be012fSWarner Losh 	CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
527169be012fSWarner Losh 	    ("xpt_register_async: func %p\n", cbfunc));
527269be012fSWarner Losh 
527385d92640SScott Long 	if (xptpath) {
5274227d67aaSAlexander Motin 		xpt_path_unlock(path);
527585d92640SScott Long 		xpt_free_path(path);
52763501942bSJustin T. Gibbs 	}
52777685edecSAlexander Motin 
52787685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
52797685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
52807685edecSAlexander Motin 		/*
52817685edecSAlexander Motin 		 * Get this peripheral up to date with all
52827685edecSAlexander Motin 		 * the currently existing devices.
52837685edecSAlexander Motin 		 */
52847685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
52857685edecSAlexander Motin 	}
52867685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
52877685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
52887685edecSAlexander Motin 		/*
52897685edecSAlexander Motin 		 * Get this peripheral up to date with all
5290db4fcadfSConrad Meyer 		 * the currently existing buses.
52917685edecSAlexander Motin 		 */
52927685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
52937685edecSAlexander Motin 	}
52943501942bSJustin T. Gibbs 
529585d92640SScott Long 	return (status);
529685d92640SScott Long }
529785d92640SScott Long 
52988b8a9b1dSJustin T. Gibbs static void
52998b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
53008b8a9b1dSJustin T. Gibbs {
53018b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
53028b8a9b1dSJustin T. Gibbs 
53038b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
53048b8a9b1dSJustin T. Gibbs 	/* Common cases first */
53058b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
53068b8a9b1dSJustin T. Gibbs 	{
53078b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
53088b8a9b1dSJustin T. Gibbs 
53098b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
53108b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
53118b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
53128b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
53138b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
53148b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
53158b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
53168b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
53178b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
53184195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
53194195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "", HBA_IDLEN);
53204195c7deSAlan Somers 		strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
53218b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
53228b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
53239deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
53243393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
53253393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
53263393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
53273393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
53288b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
53298b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
53308b8a9b1dSJustin T. Gibbs 		break;
53318b8a9b1dSJustin T. Gibbs 	}
53328b8a9b1dSJustin T. Gibbs 	default:
53338b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
53348b8a9b1dSJustin T. Gibbs 		xpt_done(work_ccb);
53358b8a9b1dSJustin T. Gibbs 		break;
53368b8a9b1dSJustin T. Gibbs 	}
53378b8a9b1dSJustin T. Gibbs }
53388b8a9b1dSJustin T. Gibbs 
53398b8a9b1dSJustin T. Gibbs /*
5340434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
5341434bbf6eSJustin T. Gibbs  * is a no-op.
5342434bbf6eSJustin T. Gibbs  */
5343434bbf6eSJustin T. Gibbs static void
5344434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
5345434bbf6eSJustin T. Gibbs {
5346434bbf6eSJustin T. Gibbs }
5347434bbf6eSJustin T. Gibbs 
53482b83592fSScott Long void
53492b83592fSScott Long xpt_lock_buses(void)
53502b83592fSScott Long {
53512b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
53522b83592fSScott Long }
53532b83592fSScott Long 
53542b83592fSScott Long void
53552b83592fSScott Long xpt_unlock_buses(void)
53562b83592fSScott Long {
53572b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
53582b83592fSScott Long }
53592b83592fSScott Long 
5360227d67aaSAlexander Motin struct mtx *
5361227d67aaSAlexander Motin xpt_path_mtx(struct cam_path *path)
53628b8a9b1dSJustin T. Gibbs {
5363227d67aaSAlexander Motin 
5364227d67aaSAlexander Motin 	return (&path->device->device_mtx);
5365227d67aaSAlexander Motin }
5366227d67aaSAlexander Motin 
5367227d67aaSAlexander Motin static void
5368227d67aaSAlexander Motin xpt_done_process(struct ccb_hdr *ccb_h)
5369227d67aaSAlexander Motin {
5370de64cba2SWarner Losh 	struct cam_sim *sim = NULL;
5371de64cba2SWarner Losh 	struct cam_devq *devq = NULL;
5372227d67aaSAlexander Motin 	struct mtx *mtx = NULL;
53738b8a9b1dSJustin T. Gibbs 
53748532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
53758532d381SConrad Meyer 	struct ccb_scsiio *csio;
53768532d381SConrad Meyer 
53778532d381SConrad Meyer 	if (ccb_h->func_code == XPT_SCSI_IO) {
53788532d381SConrad Meyer 		csio = &((union ccb *)ccb_h)->csio;
53798532d381SConrad Meyer 		if (csio->bio != NULL)
53808532d381SConrad Meyer 			biotrack(csio->bio, __func__);
53818532d381SConrad Meyer 	}
53828532d381SConrad Meyer #endif
53838532d381SConrad Meyer 
53848b8a9b1dSJustin T. Gibbs 	if (ccb_h->flags & CAM_HIGH_POWER) {
53858b8a9b1dSJustin T. Gibbs 		struct highpowerlist	*hphead;
5386ea541bfdSAlexander Motin 		struct cam_ed		*device;
53878b8a9b1dSJustin T. Gibbs 
5388daa5487fSAlexander Motin 		mtx_lock(&xsoftc.xpt_highpower_lock);
53892b83592fSScott Long 		hphead = &xsoftc.highpowerq;
53908b8a9b1dSJustin T. Gibbs 
5391ea541bfdSAlexander Motin 		device = STAILQ_FIRST(hphead);
53928b8a9b1dSJustin T. Gibbs 
53938b8a9b1dSJustin T. Gibbs 		/*
53948b8a9b1dSJustin T. Gibbs 		 * Increment the count since this command is done.
53958b8a9b1dSJustin T. Gibbs 		 */
53962b83592fSScott Long 		xsoftc.num_highpower++;
53978b8a9b1dSJustin T. Gibbs 
53988b8a9b1dSJustin T. Gibbs 		/*
53998b8a9b1dSJustin T. Gibbs 		 * Any high powered commands queued up?
54008b8a9b1dSJustin T. Gibbs 		 */
5401ea541bfdSAlexander Motin 		if (device != NULL) {
54028b8a9b1dSJustin T. Gibbs 
5403ea541bfdSAlexander Motin 			STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5404daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
54058b8a9b1dSJustin T. Gibbs 
5406227d67aaSAlexander Motin 			mtx_lock(&device->sim->devq->send_mtx);
5407ea541bfdSAlexander Motin 			xpt_release_devq_device(device,
54082cefde5fSJustin T. Gibbs 					 /*count*/1, /*runqueue*/TRUE);
5409227d67aaSAlexander Motin 			mtx_unlock(&device->sim->devq->send_mtx);
54102b83592fSScott Long 		} else
5411daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
54128b8a9b1dSJustin T. Gibbs 	}
54132b83592fSScott Long 
5414de64cba2SWarner Losh 	/*
5415a73b2e25SWarner Losh 	 * Insulate against a race where the periph is destroyed but CCBs are
5416a73b2e25SWarner Losh 	 * still not all processed. This shouldn't happen, but allows us better
5417a73b2e25SWarner Losh 	 * bug diagnostic when it does.
5418de64cba2SWarner Losh 	 */
5419de64cba2SWarner Losh 	if (ccb_h->path->bus)
5420227d67aaSAlexander Motin 		sim = ccb_h->path->bus->sim;
5421227d67aaSAlexander Motin 
5422227d67aaSAlexander Motin 	if (ccb_h->status & CAM_RELEASE_SIMQ) {
5423de64cba2SWarner Losh 		KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5424227d67aaSAlexander Motin 		xpt_release_simq(sim, /*run_queue*/FALSE);
5425227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_RELEASE_SIMQ;
5426227d67aaSAlexander Motin 	}
5427227d67aaSAlexander Motin 
5428227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5429227d67aaSAlexander Motin 	 && (ccb_h->status & CAM_DEV_QFRZN)) {
5430d718926bSAlexander Motin 		xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5431227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_DEV_QFRZN;
5432227d67aaSAlexander Motin 	}
5433227d67aaSAlexander Motin 
54349deea857SKenneth D. Merry 	if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5435227d67aaSAlexander Motin 		struct cam_ed *dev = ccb_h->path->device;
54368b8a9b1dSJustin T. Gibbs 
5437de64cba2SWarner Losh 		if (sim)
5438de64cba2SWarner Losh 			devq = sim->devq;
5439a73b2e25SWarner Losh 		KASSERT(devq, ("Periph disappeared with request pending."));
5440de64cba2SWarner Losh 
5441227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
5442227d67aaSAlexander Motin 		devq->send_active--;
5443227d67aaSAlexander Motin 		devq->send_openings++;
54448b8a9b1dSJustin T. Gibbs 		cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
54458b8a9b1dSJustin T. Gibbs 
5446b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
54478b8a9b1dSJustin T. Gibbs 		  && (dev->ccbq.dev_active == 0))) {
5448b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5449227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
5450b9c473b2SAlexander Motin 					 /*run_queue*/FALSE);
5451b9c473b2SAlexander Motin 		}
5452b9c473b2SAlexander Motin 
5453b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5454b9c473b2SAlexander Motin 		  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5455b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5456227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
545783c5d981SAlexander Motin 					 /*run_queue*/FALSE);
54588b8a9b1dSJustin T. Gibbs 		}
54598b8a9b1dSJustin T. Gibbs 
5460227d67aaSAlexander Motin 		if (!device_is_queued(dev))
5461227d67aaSAlexander Motin 			(void)xpt_schedule_devq(devq, dev);
5462d718926bSAlexander Motin 		xpt_run_devq(devq);
5463227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
5464227d67aaSAlexander Motin 
5465227d67aaSAlexander Motin 		if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5466227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5467227d67aaSAlexander Motin 			mtx_lock(mtx);
5468227d67aaSAlexander Motin 
5469fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5470fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5471fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
54723501942bSJustin T. Gibbs 		}
54738b8a9b1dSJustin T. Gibbs 	}
54748b8a9b1dSJustin T. Gibbs 
5475227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5476227d67aaSAlexander Motin 		if (mtx == NULL) {
5477227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5478227d67aaSAlexander Motin 			mtx_lock(mtx);
5479434bbf6eSJustin T. Gibbs 		}
5480227d67aaSAlexander Motin 	} else {
5481227d67aaSAlexander Motin 		if (mtx != NULL) {
5482227d67aaSAlexander Motin 			mtx_unlock(mtx);
5483227d67aaSAlexander Motin 			mtx = NULL;
5484227d67aaSAlexander Motin 		}
54858b8a9b1dSJustin T. Gibbs 	}
54868b8a9b1dSJustin T. Gibbs 
54878b8a9b1dSJustin T. Gibbs 	/* Call the peripheral driver's callback */
5488f1486b51SAlexander Motin 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5489434bbf6eSJustin T. Gibbs 	(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5490227d67aaSAlexander Motin 	if (mtx != NULL)
5491227d67aaSAlexander Motin 		mtx_unlock(mtx);
5492227d67aaSAlexander Motin }
5493227d67aaSAlexander Motin 
5494227d67aaSAlexander Motin void
5495227d67aaSAlexander Motin xpt_done_td(void *arg)
5496227d67aaSAlexander Motin {
5497227d67aaSAlexander Motin 	struct cam_doneq *queue = arg;
5498227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
5499227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	doneq;
5500227d67aaSAlexander Motin 
5501227d67aaSAlexander Motin 	STAILQ_INIT(&doneq);
5502227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
5503227d67aaSAlexander Motin 	while (1) {
5504227d67aaSAlexander Motin 		while (STAILQ_EMPTY(&queue->cam_doneq)) {
5505227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 1;
5506227d67aaSAlexander Motin 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5507227d67aaSAlexander Motin 			    PRIBIO, "-", 0);
5508227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 0;
5509227d67aaSAlexander Motin 		}
5510227d67aaSAlexander Motin 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5511227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
5512227d67aaSAlexander Motin 
5513227d67aaSAlexander Motin 		THREAD_NO_SLEEPING();
5514227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5515227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5516227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5517227d67aaSAlexander Motin 		}
5518227d67aaSAlexander Motin 		THREAD_SLEEPING_OK();
5519227d67aaSAlexander Motin 
5520227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5521227d67aaSAlexander Motin 	}
5522227d67aaSAlexander Motin }
5523227d67aaSAlexander Motin 
5524227d67aaSAlexander Motin static void
5525227d67aaSAlexander Motin camisr_runqueue(void)
5526227d67aaSAlexander Motin {
5527227d67aaSAlexander Motin 	struct	ccb_hdr *ccb_h;
5528227d67aaSAlexander Motin 	struct cam_doneq *queue;
5529227d67aaSAlexander Motin 	int i;
5530227d67aaSAlexander Motin 
5531227d67aaSAlexander Motin 	/* Process global queues. */
5532227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
5533227d67aaSAlexander Motin 		queue = &cam_doneqs[i];
5534227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5535227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5536227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5537227d67aaSAlexander Motin 			mtx_unlock(&queue->cam_doneq_mtx);
5538227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5539227d67aaSAlexander Motin 			mtx_lock(&queue->cam_doneq_mtx);
5540227d67aaSAlexander Motin 		}
5541227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
55428b8a9b1dSJustin T. Gibbs 	}
55438b8a9b1dSJustin T. Gibbs }
554469be012fSWarner Losh 
554569be012fSWarner Losh struct kv
554669be012fSWarner Losh {
554769be012fSWarner Losh 	uint32_t v;
554869be012fSWarner Losh 	const char *name;
554969be012fSWarner Losh };
555069be012fSWarner Losh 
555169be012fSWarner Losh static struct kv map[] = {
555269be012fSWarner Losh 	{ XPT_NOOP, "XPT_NOOP" },
555369be012fSWarner Losh 	{ XPT_SCSI_IO, "XPT_SCSI_IO" },
555469be012fSWarner Losh 	{ XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
555569be012fSWarner Losh 	{ XPT_GDEVLIST, "XPT_GDEVLIST" },
555669be012fSWarner Losh 	{ XPT_PATH_INQ, "XPT_PATH_INQ" },
555769be012fSWarner Losh 	{ XPT_REL_SIMQ, "XPT_REL_SIMQ" },
555869be012fSWarner Losh 	{ XPT_SASYNC_CB, "XPT_SASYNC_CB" },
555969be012fSWarner Losh 	{ XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
556069be012fSWarner Losh 	{ XPT_SCAN_BUS, "XPT_SCAN_BUS" },
556169be012fSWarner Losh 	{ XPT_DEV_MATCH, "XPT_DEV_MATCH" },
556269be012fSWarner Losh 	{ XPT_DEBUG, "XPT_DEBUG" },
556369be012fSWarner Losh 	{ XPT_PATH_STATS, "XPT_PATH_STATS" },
556469be012fSWarner Losh 	{ XPT_GDEV_STATS, "XPT_GDEV_STATS" },
556569be012fSWarner Losh 	{ XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
556669be012fSWarner Losh 	{ XPT_ASYNC, "XPT_ASYNC" },
556769be012fSWarner Losh 	{ XPT_ABORT, "XPT_ABORT" },
556869be012fSWarner Losh 	{ XPT_RESET_BUS, "XPT_RESET_BUS" },
556969be012fSWarner Losh 	{ XPT_RESET_DEV, "XPT_RESET_DEV" },
557069be012fSWarner Losh 	{ XPT_TERM_IO, "XPT_TERM_IO" },
557169be012fSWarner Losh 	{ XPT_SCAN_LUN, "XPT_SCAN_LUN" },
557269be012fSWarner Losh 	{ XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
557369be012fSWarner Losh 	{ XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
557469be012fSWarner Losh 	{ XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
557569be012fSWarner Losh 	{ XPT_ATA_IO, "XPT_ATA_IO" },
557669be012fSWarner Losh 	{ XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
557769be012fSWarner Losh 	{ XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
55787b05c3e3SWarner Losh 	{ XPT_NVME_IO, "XPT_NVME_IO" },
5579a94a63f0SWarner Losh 	{ XPT_MMC_IO, "XPT_MMC_IO" },
558069be012fSWarner Losh 	{ XPT_SMP_IO, "XPT_SMP_IO" },
558169be012fSWarner Losh 	{ XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5582df424515SWarner Losh 	{ XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
558369be012fSWarner Losh 	{ XPT_ENG_INQ, "XPT_ENG_INQ" },
558469be012fSWarner Losh 	{ XPT_ENG_EXEC, "XPT_ENG_EXEC" },
558569be012fSWarner Losh 	{ XPT_EN_LUN, "XPT_EN_LUN" },
558669be012fSWarner Losh 	{ XPT_TARGET_IO, "XPT_TARGET_IO" },
558769be012fSWarner Losh 	{ XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
558869be012fSWarner Losh 	{ XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
558969be012fSWarner Losh 	{ XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
559069be012fSWarner Losh 	{ XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
559169be012fSWarner Losh 	{ XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
559269be012fSWarner Losh 	{ XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
559369be012fSWarner Losh 	{ 0, 0 }
559469be012fSWarner Losh };
559569be012fSWarner Losh 
5596a94a63f0SWarner Losh const char *
559769be012fSWarner Losh xpt_action_name(uint32_t action)
559869be012fSWarner Losh {
559969be012fSWarner Losh 	static char buffer[32];	/* Only for unknown messages -- racy */
560069be012fSWarner Losh 	struct kv *walker = map;
560169be012fSWarner Losh 
560269be012fSWarner Losh 	while (walker->name != NULL) {
560369be012fSWarner Losh 		if (walker->v == action)
560469be012fSWarner Losh 			return (walker->name);
560569be012fSWarner Losh 		walker++;
560669be012fSWarner Losh 	}
560769be012fSWarner Losh 
560869be012fSWarner Losh 	snprintf(buffer, sizeof(buffer), "%#x", action);
560969be012fSWarner Losh 	return (buffer);
561069be012fSWarner Losh }
5611