xref: /freebsd/sys/cam/cam_xpt.c (revision eb86c6c5b462c996e44c45ba496937b75ef22da3)
1898b0535SWarner Losh /*-
28b8a9b1dSJustin T. Gibbs  * Implementation of the Common Access Method Transport (XPT) layer.
38b8a9b1dSJustin T. Gibbs  *
44d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
348b8a9b1dSJustin T. Gibbs #include <sys/param.h>
358532d381SConrad Meyer #include <sys/bio.h>
369a94c9c5SJohn Baldwin #include <sys/bus.h>
378b8a9b1dSJustin T. Gibbs #include <sys/systm.h>
388b8a9b1dSJustin T. Gibbs #include <sys/types.h>
398b8a9b1dSJustin T. Gibbs #include <sys/malloc.h>
408b8a9b1dSJustin T. Gibbs #include <sys/kernel.h>
4187cfaf0eSJustin T. Gibbs #include <sys/time.h>
428b8a9b1dSJustin T. Gibbs #include <sys/conf.h>
438b8a9b1dSJustin T. Gibbs #include <sys/fcntl.h>
44227d67aaSAlexander Motin #include <sys/proc.h>
453393f8daSKenneth D. Merry #include <sys/sbuf.h>
46227d67aaSAlexander Motin #include <sys/smp.h>
472b83592fSScott Long #include <sys/taskqueue.h>
488b8a9b1dSJustin T. Gibbs 
49ef3cf714SScott Long #include <sys/lock.h>
50ef3cf714SScott Long #include <sys/mutex.h>
513b87a552SMatt Jacob #include <sys/sysctl.h>
529e6461a2SMatt Jacob #include <sys/kthread.h>
53ef3cf714SScott Long 
548b8a9b1dSJustin T. Gibbs #include <cam/cam.h>
558b8a9b1dSJustin T. Gibbs #include <cam/cam_ccb.h>
56e4c9cba7SWarner Losh #include <cam/cam_iosched.h>
578b8a9b1dSJustin T. Gibbs #include <cam/cam_periph.h>
5852c9ce25SScott Long #include <cam/cam_queue.h>
598b8a9b1dSJustin T. Gibbs #include <cam/cam_sim.h>
608b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt.h>
618b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_sim.h>
628b8a9b1dSJustin T. Gibbs #include <cam/cam_xpt_periph.h>
6352c9ce25SScott Long #include <cam/cam_xpt_internal.h>
648b8a9b1dSJustin T. Gibbs #include <cam/cam_debug.h>
6525a2902cSScott Long #include <cam/cam_compat.h>
668b8a9b1dSJustin T. Gibbs 
678b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_all.h>
688b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_message.h>
698b8a9b1dSJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
70abef0e67SMarius Strobl 
71f0d9af51SMatt Jacob #include <machine/stdarg.h>	/* for xpt_print below */
72abef0e67SMarius Strobl 
732379d1d6SWarner Losh /* Wild guess based on not wanting to grow the stack too much */
742379d1d6SWarner Losh #define XPT_PRINT_MAXLEN	512
752379d1d6SWarner Losh #ifdef PRINTF_BUFR_SIZE
762379d1d6SWarner Losh #define XPT_PRINT_LEN	PRINTF_BUFR_SIZE
772379d1d6SWarner Losh #else
782379d1d6SWarner Losh #define XPT_PRINT_LEN	128
792379d1d6SWarner Losh #endif
802379d1d6SWarner Losh _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
812379d1d6SWarner Losh 
8252c9ce25SScott Long /*
8352c9ce25SScott Long  * This is the maximum number of high powered commands (e.g. start unit)
8452c9ce25SScott Long  * that can be outstanding at a particular time.
8552c9ce25SScott Long  */
8652c9ce25SScott Long #ifndef CAM_MAX_HIGHPOWER
8752c9ce25SScott Long #define CAM_MAX_HIGHPOWER  4
8852c9ce25SScott Long #endif
8952c9ce25SScott Long 
908b8a9b1dSJustin T. Gibbs /* Datastructures internal to the xpt layer */
91362abc44STai-hwa Liang MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
92596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
93596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
94596ee08fSAlexander Motin MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
958b8a9b1dSJustin T. Gibbs 
968b8a9b1dSJustin T. Gibbs struct xpt_softc {
97636870ffSWill Andrews 	uint32_t		xpt_generation;
98636870ffSWill Andrews 
992b83592fSScott Long 	/* number of high powered commands that can go through right now */
100daa5487fSAlexander Motin 	struct mtx		xpt_highpower_lock;
101ea541bfdSAlexander Motin 	STAILQ_HEAD(highpowerlist, cam_ed)	highpowerq;
1022b83592fSScott Long 	int			num_highpower;
1032b83592fSScott Long 
1042b83592fSScott Long 	/* queue for handling async rescan requests. */
1052b83592fSScott Long 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
10683c5d981SAlexander Motin 	int buses_to_config;
10783c5d981SAlexander Motin 	int buses_config_done;
1082b83592fSScott Long 
109db4fcadfSConrad Meyer 	/*
110db4fcadfSConrad Meyer 	 * Registered buses
111db4fcadfSConrad Meyer 	 *
112db4fcadfSConrad Meyer 	 * N.B., "busses" is an archaic spelling of "buses".  In new code
113db4fcadfSConrad Meyer 	 * "buses" is preferred.
114db4fcadfSConrad Meyer 	 */
1152b83592fSScott Long 	TAILQ_HEAD(,cam_eb)	xpt_busses;
1162b83592fSScott Long 	u_int			bus_generation;
1172b83592fSScott Long 
11883c5d981SAlexander Motin 	int			boot_delay;
11983c5d981SAlexander Motin 	struct callout 		boot_callout;
120a4876fbfSAlexander Motin 	struct task		boot_task;
121a4876fbfSAlexander Motin 	struct root_hold_token	xpt_rootmount;
12283c5d981SAlexander Motin 
1232b83592fSScott Long 	struct mtx		xpt_topo_lock;
124227d67aaSAlexander Motin 	struct taskqueue	*xpt_taskq;
1258b8a9b1dSJustin T. Gibbs };
1268b8a9b1dSJustin T. Gibbs 
1278b8a9b1dSJustin T. Gibbs typedef enum {
1288b8a9b1dSJustin T. Gibbs 	DM_RET_COPY		= 0x01,
1298b8a9b1dSJustin T. Gibbs 	DM_RET_FLAG_MASK	= 0x0f,
1308b8a9b1dSJustin T. Gibbs 	DM_RET_NONE		= 0x00,
1318b8a9b1dSJustin T. Gibbs 	DM_RET_STOP		= 0x10,
1328b8a9b1dSJustin T. Gibbs 	DM_RET_DESCEND		= 0x20,
1338b8a9b1dSJustin T. Gibbs 	DM_RET_ERROR		= 0x30,
1348b8a9b1dSJustin T. Gibbs 	DM_RET_ACTION_MASK	= 0xf0
1358b8a9b1dSJustin T. Gibbs } dev_match_ret;
1368b8a9b1dSJustin T. Gibbs 
1378b8a9b1dSJustin T. Gibbs typedef enum {
1388b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_BUS,
1398b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_TARGET,
1408b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_DEVICE,
1418b8a9b1dSJustin T. Gibbs 	XPT_DEPTH_PERIPH
1428b8a9b1dSJustin T. Gibbs } xpt_traverse_depth;
1438b8a9b1dSJustin T. Gibbs 
1448b8a9b1dSJustin T. Gibbs struct xpt_traverse_config {
1458b8a9b1dSJustin T. Gibbs 	xpt_traverse_depth	depth;
1468b8a9b1dSJustin T. Gibbs 	void			*tr_func;
1478b8a9b1dSJustin T. Gibbs 	void			*tr_arg;
1488b8a9b1dSJustin T. Gibbs };
1498b8a9b1dSJustin T. Gibbs 
1508b8a9b1dSJustin T. Gibbs typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
1518b8a9b1dSJustin T. Gibbs typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
1528b8a9b1dSJustin T. Gibbs typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
1538b8a9b1dSJustin T. Gibbs typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
1548b8a9b1dSJustin T. Gibbs typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
1558b8a9b1dSJustin T. Gibbs 
1568b8a9b1dSJustin T. Gibbs /* Transport layer configuration information */
1578b8a9b1dSJustin T. Gibbs static struct xpt_softc xsoftc;
1588b8a9b1dSJustin T. Gibbs 
1595719711fSEdward Tomasz Napierala MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
1605719711fSEdward Tomasz Napierala 
16183c5d981SAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
16283c5d981SAlexander Motin            &xsoftc.boot_delay, 0, "Bus registration wait time");
163636870ffSWill Andrews SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
164636870ffSWill Andrews 	    &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
16583c5d981SAlexander Motin 
166227d67aaSAlexander Motin struct cam_doneq {
167227d67aaSAlexander Motin 	struct mtx_padalign	cam_doneq_mtx;
168227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	cam_doneq;
169227d67aaSAlexander Motin 	int			cam_doneq_sleep;
170227d67aaSAlexander Motin };
1718b8a9b1dSJustin T. Gibbs 
172227d67aaSAlexander Motin static struct cam_doneq cam_doneqs[MAXCPU];
173466d0a25SAlexander Motin static u_int __read_mostly cam_num_doneqs;
174227d67aaSAlexander Motin static struct proc *cam_proc;
1757381bbeeSWarner Losh static struct cam_doneq cam_async;
176227d67aaSAlexander Motin 
177227d67aaSAlexander Motin SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
178227d67aaSAlexander Motin            &cam_num_doneqs, 0, "Number of completion queues/threads");
1798b8a9b1dSJustin T. Gibbs 
1809a1c8571SNick Hibma struct cam_periph *xpt_periph;
1819a1c8571SNick Hibma 
1828b8a9b1dSJustin T. Gibbs static periph_init_t xpt_periph_init;
1838b8a9b1dSJustin T. Gibbs 
1848b8a9b1dSJustin T. Gibbs static struct periph_driver xpt_driver =
1858b8a9b1dSJustin T. Gibbs {
1868b8a9b1dSJustin T. Gibbs 	xpt_periph_init, "xpt",
1871e637ba6SAlexander Motin 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
1881e637ba6SAlexander Motin 	CAM_PERIPH_DRV_EARLY
1898b8a9b1dSJustin T. Gibbs };
1908b8a9b1dSJustin T. Gibbs 
1910b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(xpt, xpt_driver);
1928b8a9b1dSJustin T. Gibbs 
1938b8a9b1dSJustin T. Gibbs static d_open_t xptopen;
1948b8a9b1dSJustin T. Gibbs static d_close_t xptclose;
1958b8a9b1dSJustin T. Gibbs static d_ioctl_t xptioctl;
19625a2902cSScott Long static d_ioctl_t xptdoioctl;
1978b8a9b1dSJustin T. Gibbs 
1984e2f199eSPoul-Henning Kamp static struct cdevsw xpt_cdevsw = {
199dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
2002b83592fSScott Long 	.d_flags =	0,
2017ac40f5fSPoul-Henning Kamp 	.d_open =	xptopen,
2027ac40f5fSPoul-Henning Kamp 	.d_close =	xptclose,
2037ac40f5fSPoul-Henning Kamp 	.d_ioctl =	xptioctl,
2047ac40f5fSPoul-Henning Kamp 	.d_name =	"xpt",
2058b8a9b1dSJustin T. Gibbs };
2068b8a9b1dSJustin T. Gibbs 
2078b8a9b1dSJustin T. Gibbs /* Storage for debugging datastructures */
2088b8a9b1dSJustin T. Gibbs struct cam_path *cam_dpath;
2097af2f2c8SWarner Losh uint32_t __read_mostly cam_dflags = CAM_DEBUG_FLAGS;
210af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
211f0f25b9cSAlexander Motin 	&cam_dflags, 0, "Enabled debug flags");
2127af2f2c8SWarner Losh uint32_t cam_debug_delay = CAM_DEBUG_DELAY;
213af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
214f0f25b9cSAlexander Motin 	&cam_debug_delay, 0, "Delay in us after each debug message");
2158b8a9b1dSJustin T. Gibbs 
2166d2a8f1cSPeter Wemm /* Our boot-time initialization hook */
21774bd1c10SNick Hibma static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
21874bd1c10SNick Hibma 
21974bd1c10SNick Hibma static moduledata_t cam_moduledata = {
22074bd1c10SNick Hibma 	"cam",
22174bd1c10SNick Hibma 	cam_module_event_handler,
22274bd1c10SNick Hibma 	NULL
22374bd1c10SNick Hibma };
22474bd1c10SNick Hibma 
2252b83592fSScott Long static int	xpt_init(void *);
22674bd1c10SNick Hibma 
22774bd1c10SNick Hibma DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
22874bd1c10SNick Hibma MODULE_VERSION(cam, 1);
22974bd1c10SNick Hibma 
2308b8a9b1dSJustin T. Gibbs static void		xpt_async_bcast(struct async_list *async_head,
2317af2f2c8SWarner Losh 					uint32_t async_code,
2328b8a9b1dSJustin T. Gibbs 					struct cam_path *path,
2338b8a9b1dSJustin T. Gibbs 					void *async_arg);
234434bbf6eSJustin T. Gibbs static path_id_t xptnextfreepathid(void);
235434bbf6eSJustin T. Gibbs static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
236227d67aaSAlexander Motin static union ccb *xpt_get_ccb(struct cam_periph *periph);
237227d67aaSAlexander Motin static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
238227d67aaSAlexander Motin static void	 xpt_run_allocq(struct cam_periph *periph, int sleep);
239227d67aaSAlexander Motin static void	 xpt_run_allocq_task(void *context, int pending);
240cccf4220SAlexander Motin static void	 xpt_run_devq(struct cam_devq *devq);
2415773ac11SJohn Baldwin static callout_func_t xpt_release_devq_timeout;
242227d67aaSAlexander Motin static void	 xpt_acquire_bus(struct cam_eb *bus);
243a5479bc5SJustin T. Gibbs static void	 xpt_release_bus(struct cam_eb *bus);
244daa5487fSAlexander Motin static uint32_t	 xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
245227d67aaSAlexander Motin static int	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
246cccf4220SAlexander Motin 		    int run_queue);
2478b8a9b1dSJustin T. Gibbs static struct cam_et*
2488b8a9b1dSJustin T. Gibbs 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
249227d67aaSAlexander Motin static void	 xpt_acquire_target(struct cam_et *target);
250f98d7a47SAlexander Motin static void	 xpt_release_target(struct cam_et *target);
2518b8a9b1dSJustin T. Gibbs static struct cam_eb*
2528b8a9b1dSJustin T. Gibbs 		 xpt_find_bus(path_id_t path_id);
2538b8a9b1dSJustin T. Gibbs static struct cam_et*
2548b8a9b1dSJustin T. Gibbs 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
2558b8a9b1dSJustin T. Gibbs static struct cam_ed*
2568b8a9b1dSJustin T. Gibbs 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
2578b8a9b1dSJustin T. Gibbs static void	 xpt_config(void *arg);
258a4876fbfSAlexander Motin static void	 xpt_hold_boot_locked(void);
259227d67aaSAlexander Motin static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
2607af2f2c8SWarner Losh 				 uint32_t new_priority);
2618b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t xptpassannouncefunc;
2628b8a9b1dSJustin T. Gibbs static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
263434bbf6eSJustin T. Gibbs static void	 xptpoll(struct cam_sim *sim);
264227d67aaSAlexander Motin static void	 camisr_runqueue(void);
265227d67aaSAlexander Motin static void	 xpt_done_process(struct ccb_hdr *ccb_h);
266227d67aaSAlexander Motin static void	 xpt_done_td(void *);
2677381bbeeSWarner Losh static void	 xpt_async_td(void *);
2688b8a9b1dSJustin T. Gibbs static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
2693393f8daSKenneth D. Merry 				    u_int num_patterns, struct cam_eb *bus);
2708b8a9b1dSJustin T. Gibbs static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
2713393f8daSKenneth D. Merry 				       u_int num_patterns,
2723393f8daSKenneth D. Merry 				       struct cam_ed *device);
2738b8a9b1dSJustin T. Gibbs static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
2743393f8daSKenneth D. Merry 				       u_int num_patterns,
2758b8a9b1dSJustin T. Gibbs 				       struct cam_periph *periph);
2768b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptedtbusfunc;
2778b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptedttargetfunc;
2788b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptedtdevicefunc;
2798b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptedtperiphfunc;
2808b8a9b1dSJustin T. Gibbs static xpt_pdrvfunc_t	xptplistpdrvfunc;
2818b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptplistperiphfunc;
2828b8a9b1dSJustin T. Gibbs static int		xptedtmatch(struct ccb_dev_match *cdm);
2838b8a9b1dSJustin T. Gibbs static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
2848b8a9b1dSJustin T. Gibbs static int		xptbustraverse(struct cam_eb *start_bus,
2858b8a9b1dSJustin T. Gibbs 				       xpt_busfunc_t *tr_func, void *arg);
2868b8a9b1dSJustin T. Gibbs static int		xpttargettraverse(struct cam_eb *bus,
2878b8a9b1dSJustin T. Gibbs 					  struct cam_et *start_target,
2888b8a9b1dSJustin T. Gibbs 					  xpt_targetfunc_t *tr_func, void *arg);
2898b8a9b1dSJustin T. Gibbs static int		xptdevicetraverse(struct cam_et *target,
2908b8a9b1dSJustin T. Gibbs 					  struct cam_ed *start_device,
2918b8a9b1dSJustin T. Gibbs 					  xpt_devicefunc_t *tr_func, void *arg);
2928b8a9b1dSJustin T. Gibbs static int		xptperiphtraverse(struct cam_ed *device,
2938b8a9b1dSJustin T. Gibbs 					  struct cam_periph *start_periph,
2948b8a9b1dSJustin T. Gibbs 					  xpt_periphfunc_t *tr_func, void *arg);
2958b8a9b1dSJustin T. Gibbs static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
2968b8a9b1dSJustin T. Gibbs 					xpt_pdrvfunc_t *tr_func, void *arg);
2978b8a9b1dSJustin T. Gibbs static int		xptpdperiphtraverse(struct periph_driver **pdrv,
2988b8a9b1dSJustin T. Gibbs 					    struct cam_periph *start_periph,
2998b8a9b1dSJustin T. Gibbs 					    xpt_periphfunc_t *tr_func,
3008b8a9b1dSJustin T. Gibbs 					    void *arg);
3018b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptdefbusfunc;
3028b8a9b1dSJustin T. Gibbs static xpt_targetfunc_t	xptdeftargetfunc;
3038b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptdefdevicefunc;
3048b8a9b1dSJustin T. Gibbs static xpt_periphfunc_t	xptdefperiphfunc;
30583c5d981SAlexander Motin static void		xpt_finishconfig_task(void *context, int pending);
3067af2f2c8SWarner Losh static void		xpt_dev_async_default(uint32_t async_code,
30752c9ce25SScott Long 					      struct cam_eb *bus,
30852c9ce25SScott Long 					      struct cam_et *target,
30952c9ce25SScott Long 					      struct cam_ed *device,
31052c9ce25SScott Long 					      void *async_arg);
31152c9ce25SScott Long static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
31252c9ce25SScott Long 						 struct cam_et *target,
31352c9ce25SScott Long 						 lun_id_t lun_id);
3148b8a9b1dSJustin T. Gibbs static xpt_devicefunc_t	xptsetasyncfunc;
3158b8a9b1dSJustin T. Gibbs static xpt_busfunc_t	xptsetasyncbusfunc;
3168b8a9b1dSJustin T. Gibbs static cam_status	xptregister(struct cam_periph *periph,
3178b8a9b1dSJustin T. Gibbs 				    void *arg);
3188b8a9b1dSJustin T. Gibbs 
3198b8a9b1dSJustin T. Gibbs static __inline int
xpt_schedule_devq(struct cam_devq * devq,struct cam_ed * dev)320cccf4220SAlexander Motin xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
32130a4094fSAlexander Motin {
32230a4094fSAlexander Motin 	int	retval;
32330a4094fSAlexander Motin 
324227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
32583c5d981SAlexander Motin 	if ((dev->ccbq.queue.entries > 0) &&
32683c5d981SAlexander Motin 	    (dev->ccbq.dev_openings > 0) &&
327cccf4220SAlexander Motin 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
32830a4094fSAlexander Motin 		/*
32930a4094fSAlexander Motin 		 * The priority of a device waiting for controller
3306bccea7cSRebecca Cran 		 * resources is that of the highest priority CCB
33130a4094fSAlexander Motin 		 * enqueued.
33230a4094fSAlexander Motin 		 */
33330a4094fSAlexander Motin 		retval =
334cccf4220SAlexander Motin 		    xpt_schedule_dev(&devq->send_queue,
335227d67aaSAlexander Motin 				     &dev->devq_entry,
33683c5d981SAlexander Motin 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
33730a4094fSAlexander Motin 	} else {
33830a4094fSAlexander Motin 		retval = 0;
33930a4094fSAlexander Motin 	}
34030a4094fSAlexander Motin 	return (retval);
34130a4094fSAlexander Motin }
34230a4094fSAlexander Motin 
34330a4094fSAlexander Motin static __inline int
device_is_queued(struct cam_ed * device)344cccf4220SAlexander Motin device_is_queued(struct cam_ed *device)
3458b8a9b1dSJustin T. Gibbs {
346227d67aaSAlexander Motin 	return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
3478b8a9b1dSJustin T. Gibbs }
3488b8a9b1dSJustin T. Gibbs 
3498b8a9b1dSJustin T. Gibbs static void
xpt_periph_init(void)3509982b3eeSConrad Meyer xpt_periph_init(void)
3518b8a9b1dSJustin T. Gibbs {
35273d26919SKenneth D. Merry 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
3538b8a9b1dSJustin T. Gibbs }
3548b8a9b1dSJustin T. Gibbs 
3558b8a9b1dSJustin T. Gibbs static int
xptopen(struct cdev * dev,int flags,int fmt,struct thread * td)35689c9c53dSPoul-Henning Kamp xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3578b8a9b1dSJustin T. Gibbs {
3588b8a9b1dSJustin T. Gibbs 
3598b8a9b1dSJustin T. Gibbs 	/*
36066a0780eSKenneth D. Merry 	 * Only allow read-write access.
36166a0780eSKenneth D. Merry 	 */
36266a0780eSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
36366a0780eSKenneth D. Merry 		return(EPERM);
36466a0780eSKenneth D. Merry 
36566a0780eSKenneth D. Merry 	/*
3668b8a9b1dSJustin T. Gibbs 	 * We don't allow nonblocking access.
3678b8a9b1dSJustin T. Gibbs 	 */
3688b8a9b1dSJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
3692b83592fSScott Long 		printf("%s: can't do nonblocking access\n", devtoname(dev));
3708b8a9b1dSJustin T. Gibbs 		return(ENODEV);
3718b8a9b1dSJustin T. Gibbs 	}
3728b8a9b1dSJustin T. Gibbs 
3738b8a9b1dSJustin T. Gibbs 	return(0);
3748b8a9b1dSJustin T. Gibbs }
3758b8a9b1dSJustin T. Gibbs 
3768b8a9b1dSJustin T. Gibbs static int
xptclose(struct cdev * dev,int flag,int fmt,struct thread * td)37789c9c53dSPoul-Henning Kamp xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
3788b8a9b1dSJustin T. Gibbs {
3798b8a9b1dSJustin T. Gibbs 
3808b8a9b1dSJustin T. Gibbs 	return(0);
3818b8a9b1dSJustin T. Gibbs }
3828b8a9b1dSJustin T. Gibbs 
3832b83592fSScott Long /*
3842b83592fSScott Long  * Don't automatically grab the xpt softc lock here even though this is going
3852b83592fSScott Long  * through the xpt device.  The xpt device is really just a back door for
3862b83592fSScott Long  * accessing other devices and SIMs, so the right thing to do is to grab
3872b83592fSScott Long  * the appropriate SIM lock once the bus/SIM is located.
3882b83592fSScott Long  */
3898b8a9b1dSJustin T. Gibbs static int
xptioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)39089c9c53dSPoul-Henning Kamp xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3918b8a9b1dSJustin T. Gibbs {
3922b83592fSScott Long 	int error;
3938b8a9b1dSJustin T. Gibbs 
39425a2902cSScott Long 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
395f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
39625a2902cSScott Long 	}
39725a2902cSScott Long 	return (error);
39825a2902cSScott Long }
39925a2902cSScott Long 
40025a2902cSScott Long static int
xptdoioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)40125a2902cSScott Long xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
40225a2902cSScott Long {
40325a2902cSScott Long 	int error;
40425a2902cSScott Long 
4058b8a9b1dSJustin T. Gibbs 	error = 0;
4068b8a9b1dSJustin T. Gibbs 
4078b8a9b1dSJustin T. Gibbs 	switch(cmd) {
4088b8a9b1dSJustin T. Gibbs 	/*
4098b8a9b1dSJustin T. Gibbs 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
4108b8a9b1dSJustin T. Gibbs 	 * to accept CCB types that don't quite make sense to send through a
4118c7a96c5SScott Long 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
4128c7a96c5SScott Long 	 * in the CAM spec.
4138b8a9b1dSJustin T. Gibbs 	 */
4148b8a9b1dSJustin T. Gibbs 	case CAMIOCOMMAND: {
4158b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
4168b8a9b1dSJustin T. Gibbs 		union ccb *inccb;
4172b83592fSScott Long 		struct cam_eb *bus;
4188b8a9b1dSJustin T. Gibbs 
4198b8a9b1dSJustin T. Gibbs 		inccb = (union ccb *)addr;
4208fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4218fc77fffSConrad Meyer 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
4228fc77fffSConrad Meyer 			inccb->csio.bio = NULL;
4238fc77fffSConrad Meyer #endif
4248b8a9b1dSJustin T. Gibbs 
425a0bbf9e0SMark Johnston 		if (inccb->ccb_h.flags & CAM_UNLOCKED)
426a0bbf9e0SMark Johnston 			return (EINVAL);
427a0bbf9e0SMark Johnston 
4282b83592fSScott Long 		bus = xpt_find_bus(inccb->ccb_h.path_id);
4290e85f214SMatt Jacob 		if (bus == NULL)
4300e85f214SMatt Jacob 			return (EINVAL);
4310e85f214SMatt Jacob 
4320e85f214SMatt Jacob 		switch (inccb->ccb_h.func_code) {
4330e85f214SMatt Jacob 		case XPT_SCAN_BUS:
4340e85f214SMatt Jacob 		case XPT_RESET_BUS:
4350e85f214SMatt Jacob 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
4360e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4370e85f214SMatt Jacob 				xpt_release_bus(bus);
4380e85f214SMatt Jacob 				return (EINVAL);
4390e85f214SMatt Jacob 			}
4400e85f214SMatt Jacob 			break;
4410e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4420e85f214SMatt Jacob 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
4430e85f214SMatt Jacob 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
4440e85f214SMatt Jacob 				xpt_release_bus(bus);
4450e85f214SMatt Jacob 				return (EINVAL);
4460e85f214SMatt Jacob 			}
4470e85f214SMatt Jacob 			break;
4480e85f214SMatt Jacob 		default:
4492b83592fSScott Long 			break;
4502b83592fSScott Long 		}
4512b83592fSScott Long 
4528b8a9b1dSJustin T. Gibbs 		switch(inccb->ccb_h.func_code) {
4538b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_BUS:
4548b8a9b1dSJustin T. Gibbs 		case XPT_RESET_BUS:
4558c7a96c5SScott Long 		case XPT_PATH_INQ:
4568fcf57f5SJustin T. Gibbs 		case XPT_ENG_INQ:
4578b8a9b1dSJustin T. Gibbs 		case XPT_SCAN_LUN:
4580e85f214SMatt Jacob 		case XPT_SCAN_TGT:
4598b8a9b1dSJustin T. Gibbs 
4608008a935SScott Long 			ccb = xpt_alloc_ccb();
4612b83592fSScott Long 
4628b8a9b1dSJustin T. Gibbs 			/*
4638b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4648b8a9b1dSJustin T. Gibbs 			 * user passed in.
4658b8a9b1dSJustin T. Gibbs 			 */
466e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
4678b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
4688b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
4698b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
4708b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
4718b8a9b1dSJustin T. Gibbs 				error = EINVAL;
4728b8a9b1dSJustin T. Gibbs 				xpt_free_ccb(ccb);
4738b8a9b1dSJustin T. Gibbs 				break;
4748b8a9b1dSJustin T. Gibbs 			}
4758b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
4768b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
4778b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
4788b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(ccb, inccb);
479227d67aaSAlexander Motin 			xpt_path_lock(ccb->ccb_h.path);
4808b8a9b1dSJustin T. Gibbs 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
481227d67aaSAlexander Motin 			xpt_path_unlock(ccb->ccb_h.path);
4828b8a9b1dSJustin T. Gibbs 			bcopy(ccb, inccb, sizeof(union ccb));
4838b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb->ccb_h.path);
4848b8a9b1dSJustin T. Gibbs 			xpt_free_ccb(ccb);
4858b8a9b1dSJustin T. Gibbs 			break;
4868b8a9b1dSJustin T. Gibbs 
4878b8a9b1dSJustin T. Gibbs 		case XPT_DEBUG: {
4888b8a9b1dSJustin T. Gibbs 			union ccb ccb;
4898b8a9b1dSJustin T. Gibbs 
4908b8a9b1dSJustin T. Gibbs 			/*
491aa872be6SMatt Jacob 			 * This is an immediate CCB, so it's okay to
4928b8a9b1dSJustin T. Gibbs 			 * allocate it on the stack.
4938b8a9b1dSJustin T. Gibbs 			 */
494076686feSEdward Tomasz Napierala 			memset(&ccb, 0, sizeof(ccb));
4958b8a9b1dSJustin T. Gibbs 
4968b8a9b1dSJustin T. Gibbs 			/*
4978b8a9b1dSJustin T. Gibbs 			 * Create a path using the bus, target, and lun the
4988b8a9b1dSJustin T. Gibbs 			 * user passed in.
4998b8a9b1dSJustin T. Gibbs 			 */
500e5dfa058SAlexander Motin 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
5018b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.path_id,
5028b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_id,
5038b8a9b1dSJustin T. Gibbs 					    inccb->ccb_h.target_lun) !=
5048b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP){
5058b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5068b8a9b1dSJustin T. Gibbs 				break;
5078b8a9b1dSJustin T. Gibbs 			}
5088b8a9b1dSJustin T. Gibbs 			/* Ensure all of our fields are correct */
5098b8a9b1dSJustin T. Gibbs 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
5108b8a9b1dSJustin T. Gibbs 				      inccb->ccb_h.pinfo.priority);
5118b8a9b1dSJustin T. Gibbs 			xpt_merge_ccb(&ccb, inccb);
5128b8a9b1dSJustin T. Gibbs 			xpt_action(&ccb);
5138b8a9b1dSJustin T. Gibbs 			bcopy(&ccb, inccb, sizeof(union ccb));
5148b8a9b1dSJustin T. Gibbs 			xpt_free_path(ccb.ccb_h.path);
5158b8a9b1dSJustin T. Gibbs 			break;
5168b8a9b1dSJustin T. Gibbs 		}
5178b8a9b1dSJustin T. Gibbs 		case XPT_DEV_MATCH: {
5188b8a9b1dSJustin T. Gibbs 			struct cam_periph_map_info mapinfo;
51959190eaaSKenneth D. Merry 			struct cam_path *old_path;
5208b8a9b1dSJustin T. Gibbs 
5218b8a9b1dSJustin T. Gibbs 			/*
5228b8a9b1dSJustin T. Gibbs 			 * We can't deal with physical addresses for this
5238b8a9b1dSJustin T. Gibbs 			 * type of transaction.
5248b8a9b1dSJustin T. Gibbs 			 */
525dd0b4fb6SKonstantin Belousov 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
526dd0b4fb6SKonstantin Belousov 			    CAM_DATA_VADDR) {
5278b8a9b1dSJustin T. Gibbs 				error = EINVAL;
5288b8a9b1dSJustin T. Gibbs 				break;
5298b8a9b1dSJustin T. Gibbs 			}
53059190eaaSKenneth D. Merry 
53159190eaaSKenneth D. Merry 			/*
53259190eaaSKenneth D. Merry 			 * Save this in case the caller had it set to
53359190eaaSKenneth D. Merry 			 * something in particular.
53459190eaaSKenneth D. Merry 			 */
53559190eaaSKenneth D. Merry 			old_path = inccb->ccb_h.path;
53659190eaaSKenneth D. Merry 
53759190eaaSKenneth D. Merry 			/*
53859190eaaSKenneth D. Merry 			 * We really don't need a path for the matching
53959190eaaSKenneth D. Merry 			 * code.  The path is needed because of the
54059190eaaSKenneth D. Merry 			 * debugging statements in xpt_action().  They
54159190eaaSKenneth D. Merry 			 * assume that the CCB has a valid path.
54259190eaaSKenneth D. Merry 			 */
54359190eaaSKenneth D. Merry 			inccb->ccb_h.path = xpt_periph->path;
54459190eaaSKenneth D. Merry 
5458b8a9b1dSJustin T. Gibbs 			bzero(&mapinfo, sizeof(mapinfo));
5468b8a9b1dSJustin T. Gibbs 
5478b8a9b1dSJustin T. Gibbs 			/*
5488b8a9b1dSJustin T. Gibbs 			 * Map the pattern and match buffers into kernel
5498b8a9b1dSJustin T. Gibbs 			 * virtual address space.
5508b8a9b1dSJustin T. Gibbs 			 */
551cd853791SKonstantin Belousov 			error = cam_periph_mapmem(inccb, &mapinfo, maxphys);
5528b8a9b1dSJustin T. Gibbs 
55359190eaaSKenneth D. Merry 			if (error) {
55459190eaaSKenneth D. Merry 				inccb->ccb_h.path = old_path;
5558b8a9b1dSJustin T. Gibbs 				break;
55659190eaaSKenneth D. Merry 			}
5578b8a9b1dSJustin T. Gibbs 
5588b8a9b1dSJustin T. Gibbs 			/*
5598b8a9b1dSJustin T. Gibbs 			 * This is an immediate CCB, we can send it on directly.
5608b8a9b1dSJustin T. Gibbs 			 */
5618b8a9b1dSJustin T. Gibbs 			xpt_action(inccb);
5628b8a9b1dSJustin T. Gibbs 
5638b8a9b1dSJustin T. Gibbs 			/*
5648b8a9b1dSJustin T. Gibbs 			 * Map the buffers back into user space.
5658b8a9b1dSJustin T. Gibbs 			 */
566d068ea16SMark Johnston 			error = cam_periph_unmapmem(inccb, &mapinfo);
5678b8a9b1dSJustin T. Gibbs 
56859190eaaSKenneth D. Merry 			inccb->ccb_h.path = old_path;
5698b8a9b1dSJustin T. Gibbs 			break;
5708b8a9b1dSJustin T. Gibbs 		}
5718b8a9b1dSJustin T. Gibbs 		default:
5728fcf57f5SJustin T. Gibbs 			error = ENOTSUP;
5738b8a9b1dSJustin T. Gibbs 			break;
5748b8a9b1dSJustin T. Gibbs 		}
575daddc001SScott Long 		xpt_release_bus(bus);
5768b8a9b1dSJustin T. Gibbs 		break;
5778b8a9b1dSJustin T. Gibbs 	}
5788b8a9b1dSJustin T. Gibbs 	/*
5798b8a9b1dSJustin T. Gibbs 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
5808b8a9b1dSJustin T. Gibbs 	 * with the periphal driver name and unit name filled in.  The other
5818b8a9b1dSJustin T. Gibbs 	 * fields don't really matter as input.  The passthrough driver name
5828b8a9b1dSJustin T. Gibbs 	 * ("pass"), and unit number are passed back in the ccb.  The current
5838b8a9b1dSJustin T. Gibbs 	 * device generation number, and the index into the device peripheral
5848b8a9b1dSJustin T. Gibbs 	 * driver list, and the status are also passed back.  Note that
5858b8a9b1dSJustin T. Gibbs 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
5868b8a9b1dSJustin T. Gibbs 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
5878b8a9b1dSJustin T. Gibbs 	 * (or rather should be) impossible for the device peripheral driver
5888b8a9b1dSJustin T. Gibbs 	 * list to change since we look at the whole thing in one pass, and
58977dc25ccSScott Long 	 * we do it with lock protection.
5908b8a9b1dSJustin T. Gibbs 	 *
5918b8a9b1dSJustin T. Gibbs 	 */
5928b8a9b1dSJustin T. Gibbs 	case CAMGETPASSTHRU: {
5938b8a9b1dSJustin T. Gibbs 		union ccb *ccb;
5948b8a9b1dSJustin T. Gibbs 		struct cam_periph *periph;
5958b8a9b1dSJustin T. Gibbs 		struct periph_driver **p_drv;
5968b8a9b1dSJustin T. Gibbs 		char   *name;
5973393f8daSKenneth D. Merry 		u_int unit;
598d095d6a3SWarner Losh 		bool base_periph_found;
5998b8a9b1dSJustin T. Gibbs 
6008b8a9b1dSJustin T. Gibbs 		ccb = (union ccb *)addr;
6018b8a9b1dSJustin T. Gibbs 		unit = ccb->cgdl.unit_number;
6028b8a9b1dSJustin T. Gibbs 		name = ccb->cgdl.periph_name;
603d095d6a3SWarner Losh 		base_periph_found = false;
6048fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
6058fc77fffSConrad Meyer 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
6068fc77fffSConrad Meyer 			ccb->csio.bio = NULL;
6078fc77fffSConrad Meyer #endif
608621a60d4SKenneth D. Merry 
6098b8a9b1dSJustin T. Gibbs 		/*
6108b8a9b1dSJustin T. Gibbs 		 * Sanity check -- make sure we don't get a null peripheral
6118b8a9b1dSJustin T. Gibbs 		 * driver name.
6128b8a9b1dSJustin T. Gibbs 		 */
6138b8a9b1dSJustin T. Gibbs 		if (*ccb->cgdl.periph_name == '\0') {
6148b8a9b1dSJustin T. Gibbs 			error = EINVAL;
6158b8a9b1dSJustin T. Gibbs 			break;
6168b8a9b1dSJustin T. Gibbs 		}
6178b8a9b1dSJustin T. Gibbs 
6188b8a9b1dSJustin T. Gibbs 		/* Keep the list from changing while we traverse it */
6199a7c2696SAlexander Motin 		xpt_lock_buses();
6208b8a9b1dSJustin T. Gibbs 
6218b8a9b1dSJustin T. Gibbs 		/* first find our driver in the list of drivers */
6220b7c27b9SPeter Wemm 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
6238b8a9b1dSJustin T. Gibbs 			if (strcmp((*p_drv)->driver_name, name) == 0)
6248b8a9b1dSJustin T. Gibbs 				break;
6258b8a9b1dSJustin T. Gibbs 
6268b8a9b1dSJustin T. Gibbs 		if (*p_drv == NULL) {
6279a7c2696SAlexander Motin 			xpt_unlock_buses();
6288b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
6298b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
6308b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
6318b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
6328b8a9b1dSJustin T. Gibbs 			error = ENOENT;
6338b8a9b1dSJustin T. Gibbs 			break;
6348b8a9b1dSJustin T. Gibbs 		}
6358b8a9b1dSJustin T. Gibbs 
6368b8a9b1dSJustin T. Gibbs 		/*
6378b8a9b1dSJustin T. Gibbs 		 * Run through every peripheral instance of this driver
6388b8a9b1dSJustin T. Gibbs 		 * and check to see whether it matches the unit passed
6398b8a9b1dSJustin T. Gibbs 		 * in by the user.  If it does, get out of the loops and
6408b8a9b1dSJustin T. Gibbs 		 * find the passthrough driver associated with that
6418b8a9b1dSJustin T. Gibbs 		 * peripheral driver.
6428b8a9b1dSJustin T. Gibbs 		 */
6438b8a9b1dSJustin T. Gibbs 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
6448b8a9b1dSJustin T. Gibbs 		     periph = TAILQ_NEXT(periph, unit_links)) {
645a29779e8SAlexander Motin 			if (periph->unit_number == unit)
6468b8a9b1dSJustin T. Gibbs 				break;
6478b8a9b1dSJustin T. Gibbs 		}
6488b8a9b1dSJustin T. Gibbs 		/*
6498b8a9b1dSJustin T. Gibbs 		 * If we found the peripheral driver that the user passed
6508b8a9b1dSJustin T. Gibbs 		 * in, go through all of the peripheral drivers for that
6518b8a9b1dSJustin T. Gibbs 		 * particular device and look for a passthrough driver.
6528b8a9b1dSJustin T. Gibbs 		 */
6538b8a9b1dSJustin T. Gibbs 		if (periph != NULL) {
6548b8a9b1dSJustin T. Gibbs 			struct cam_ed *device;
6558b8a9b1dSJustin T. Gibbs 			int i;
6568b8a9b1dSJustin T. Gibbs 
657d095d6a3SWarner Losh 			base_periph_found = true;
6588b8a9b1dSJustin T. Gibbs 			device = periph->path->device;
659fc2ffbe6SPoul-Henning Kamp 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
6608b8a9b1dSJustin T. Gibbs 			     periph != NULL;
661fc2ffbe6SPoul-Henning Kamp 			     periph = SLIST_NEXT(periph, periph_links), i++) {
6628b8a9b1dSJustin T. Gibbs 				/*
6638b8a9b1dSJustin T. Gibbs 				 * Check to see whether we have a
6648b8a9b1dSJustin T. Gibbs 				 * passthrough device or not.
6658b8a9b1dSJustin T. Gibbs 				 */
6668b8a9b1dSJustin T. Gibbs 				if (strcmp(periph->periph_name, "pass") == 0) {
6678b8a9b1dSJustin T. Gibbs 					/*
6688b8a9b1dSJustin T. Gibbs 					 * Fill in the getdevlist fields.
6698b8a9b1dSJustin T. Gibbs 					 */
670b0f662feSAlan Somers 					strlcpy(ccb->cgdl.periph_name,
671b0f662feSAlan Somers 					       periph->periph_name,
672b0f662feSAlan Somers 					       sizeof(ccb->cgdl.periph_name));
6738b8a9b1dSJustin T. Gibbs 					ccb->cgdl.unit_number =
6748b8a9b1dSJustin T. Gibbs 						periph->unit_number;
675fc2ffbe6SPoul-Henning Kamp 					if (SLIST_NEXT(periph, periph_links))
6768b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6778b8a9b1dSJustin T. Gibbs 							CAM_GDEVLIST_MORE_DEVS;
6788b8a9b1dSJustin T. Gibbs 					else
6798b8a9b1dSJustin T. Gibbs 						ccb->cgdl.status =
6808b8a9b1dSJustin T. Gibbs 						       CAM_GDEVLIST_LAST_DEVICE;
6818b8a9b1dSJustin T. Gibbs 					ccb->cgdl.generation =
6828b8a9b1dSJustin T. Gibbs 						device->generation;
6838b8a9b1dSJustin T. Gibbs 					ccb->cgdl.index = i;
6848b8a9b1dSJustin T. Gibbs 					/*
6858b8a9b1dSJustin T. Gibbs 					 * Fill in some CCB header fields
6868b8a9b1dSJustin T. Gibbs 					 * that the user may want.
6878b8a9b1dSJustin T. Gibbs 					 */
6888b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.path_id =
6898b8a9b1dSJustin T. Gibbs 						periph->path->bus->path_id;
6908b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_id =
6918b8a9b1dSJustin T. Gibbs 						periph->path->target->target_id;
6928b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.target_lun =
6938b8a9b1dSJustin T. Gibbs 						periph->path->device->lun_id;
6948b8a9b1dSJustin T. Gibbs 					ccb->ccb_h.status = CAM_REQ_CMP;
6958b8a9b1dSJustin T. Gibbs 					break;
6968b8a9b1dSJustin T. Gibbs 				}
6978b8a9b1dSJustin T. Gibbs 			}
6988b8a9b1dSJustin T. Gibbs 		}
6998b8a9b1dSJustin T. Gibbs 
7008b8a9b1dSJustin T. Gibbs 		/*
7018b8a9b1dSJustin T. Gibbs 		 * If the periph is null here, one of two things has
7028b8a9b1dSJustin T. Gibbs 		 * happened.  The first possibility is that we couldn't
7038b8a9b1dSJustin T. Gibbs 		 * find the unit number of the particular peripheral driver
7048b8a9b1dSJustin T. Gibbs 		 * that the user is asking about.  e.g. the user asks for
7058b8a9b1dSJustin T. Gibbs 		 * the passthrough driver for "da11".  We find the list of
7068b8a9b1dSJustin T. Gibbs 		 * "da" peripherals all right, but there is no unit 11.
7078b8a9b1dSJustin T. Gibbs 		 * The other possibility is that we went through the list
7088b8a9b1dSJustin T. Gibbs 		 * of peripheral drivers attached to the device structure,
7098b8a9b1dSJustin T. Gibbs 		 * but didn't find one with the name "pass".  Either way,
7108b8a9b1dSJustin T. Gibbs 		 * we return ENOENT, since we couldn't find something.
7118b8a9b1dSJustin T. Gibbs 		 */
7128b8a9b1dSJustin T. Gibbs 		if (periph == NULL) {
7138b8a9b1dSJustin T. Gibbs 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
7148b8a9b1dSJustin T. Gibbs 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
7158b8a9b1dSJustin T. Gibbs 			*ccb->cgdl.periph_name = '\0';
7168b8a9b1dSJustin T. Gibbs 			ccb->cgdl.unit_number = 0;
7178b8a9b1dSJustin T. Gibbs 			error = ENOENT;
718621a60d4SKenneth D. Merry 			/*
719621a60d4SKenneth D. Merry 			 * It is unfortunate that this is even necessary,
720621a60d4SKenneth D. Merry 			 * but there are many, many clueless users out there.
721621a60d4SKenneth D. Merry 			 * If this is true, the user is looking for the
722621a60d4SKenneth D. Merry 			 * passthrough driver, but doesn't have one in his
723621a60d4SKenneth D. Merry 			 * kernel.
724621a60d4SKenneth D. Merry 			 */
725d095d6a3SWarner Losh 			if (base_periph_found) {
726621a60d4SKenneth D. Merry 				printf("xptioctl: pass driver is not in the "
727621a60d4SKenneth D. Merry 				       "kernel\n");
728935c968aSChristian Brueffer 				printf("xptioctl: put \"device pass\" in "
729621a60d4SKenneth D. Merry 				       "your kernel config file\n");
730621a60d4SKenneth D. Merry 			}
7318b8a9b1dSJustin T. Gibbs 		}
7329a7c2696SAlexander Motin 		xpt_unlock_buses();
7338b8a9b1dSJustin T. Gibbs 		break;
7348b8a9b1dSJustin T. Gibbs 		}
7358b8a9b1dSJustin T. Gibbs 	default:
7368b8a9b1dSJustin T. Gibbs 		error = ENOTTY;
7378b8a9b1dSJustin T. Gibbs 		break;
7388b8a9b1dSJustin T. Gibbs 	}
7398b8a9b1dSJustin T. Gibbs 
7408b8a9b1dSJustin T. Gibbs 	return(error);
7418b8a9b1dSJustin T. Gibbs }
7428b8a9b1dSJustin T. Gibbs 
74374bd1c10SNick Hibma static int
cam_module_event_handler(module_t mod,int what,void * arg)74474bd1c10SNick Hibma cam_module_event_handler(module_t mod, int what, void *arg)
74574bd1c10SNick Hibma {
7462b83592fSScott Long 	int error;
7472b83592fSScott Long 
7482b83592fSScott Long 	switch (what) {
7492b83592fSScott Long 	case MOD_LOAD:
7502b83592fSScott Long 		if ((error = xpt_init(NULL)) != 0)
7512b83592fSScott Long 			return (error);
7522b83592fSScott Long 		break;
7532b83592fSScott Long 	case MOD_UNLOAD:
75474bd1c10SNick Hibma 		return EBUSY;
7552b83592fSScott Long 	default:
7563e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
75774bd1c10SNick Hibma 	}
75874bd1c10SNick Hibma 
75974bd1c10SNick Hibma 	return 0;
76074bd1c10SNick Hibma }
76174bd1c10SNick Hibma 
76208f13879SWarner Losh static struct xpt_proto *
xpt_proto_find(cam_proto proto)76308f13879SWarner Losh xpt_proto_find(cam_proto proto)
76408f13879SWarner Losh {
76508f13879SWarner Losh 	struct xpt_proto **pp;
76608f13879SWarner Losh 
76708f13879SWarner Losh 	SET_FOREACH(pp, cam_xpt_proto_set) {
76808f13879SWarner Losh 		if ((*pp)->proto == proto)
76908f13879SWarner Losh 			return *pp;
77008f13879SWarner Losh 	}
77108f13879SWarner Losh 
77208f13879SWarner Losh 	return NULL;
77308f13879SWarner Losh }
77408f13879SWarner Losh 
77583c5d981SAlexander Motin static void
xpt_rescan_done(struct cam_periph * periph,union ccb * done_ccb)77683c5d981SAlexander Motin xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
77783c5d981SAlexander Motin {
77883c5d981SAlexander Motin 
77983c5d981SAlexander Motin 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
78083c5d981SAlexander Motin 		xpt_free_path(done_ccb->ccb_h.path);
78183c5d981SAlexander Motin 		xpt_free_ccb(done_ccb);
78283c5d981SAlexander Motin 	} else {
78383c5d981SAlexander Motin 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
78483c5d981SAlexander Motin 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
78583c5d981SAlexander Motin 	}
78683c5d981SAlexander Motin 	xpt_release_boot();
78783c5d981SAlexander Motin }
78883c5d981SAlexander Motin 
7899e6461a2SMatt Jacob /* thread to handle bus rescans */
7909e6461a2SMatt Jacob static void
xpt_scanner_thread(void * dummy)7919e6461a2SMatt Jacob xpt_scanner_thread(void *dummy)
7929e6461a2SMatt Jacob {
7939e6461a2SMatt Jacob 	union ccb	*ccb;
7943dfb13c1SWarner Losh 	struct mtx	*mtx;
7953dfb13c1SWarner Losh 	struct cam_ed	*device;
7962b83592fSScott Long 
7972b83592fSScott Long 	xpt_lock_buses();
79883c5d981SAlexander Motin 	for (;;) {
7995a73cc12SKenneth D. Merry 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
8002b83592fSScott Long 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
8013710ae64SAlexander Motin 			       "-", 0);
80283c5d981SAlexander Motin 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
80383c5d981SAlexander Motin 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
8042b83592fSScott Long 			xpt_unlock_buses();
8052b83592fSScott Long 
806227d67aaSAlexander Motin 			/*
8073dfb13c1SWarner Losh 			 * We need to lock the device's mutex which we use as
8083dfb13c1SWarner Losh 			 * the path mutex. We can't do it directly because the
8093dfb13c1SWarner Losh 			 * cam_path in the ccb may wind up going away because
8103dfb13c1SWarner Losh 			 * the path lock may be dropped and the path retired in
8113dfb13c1SWarner Losh 			 * the completion callback. We do this directly to keep
8123dfb13c1SWarner Losh 			 * the reference counts in cam_path sane. We also have
8133dfb13c1SWarner Losh 			 * to copy the device pointer because ccb_h.path may
8143dfb13c1SWarner Losh 			 * be freed in the callback.
815227d67aaSAlexander Motin 			 */
8163dfb13c1SWarner Losh 			mtx = xpt_path_mtx(ccb->ccb_h.path);
8173dfb13c1SWarner Losh 			device = ccb->ccb_h.path->device;
8183dfb13c1SWarner Losh 			xpt_acquire_device(device);
8193dfb13c1SWarner Losh 			mtx_lock(mtx);
82083c5d981SAlexander Motin 			xpt_action(ccb);
8213dfb13c1SWarner Losh 			mtx_unlock(mtx);
8223dfb13c1SWarner Losh 			xpt_release_device(device);
82383c5d981SAlexander Motin 
82483c5d981SAlexander Motin 			xpt_lock_buses();
8259e6461a2SMatt Jacob 		}
8269e6461a2SMatt Jacob 	}
8279e6461a2SMatt Jacob }
8289e6461a2SMatt Jacob 
8299e6461a2SMatt Jacob void
xpt_rescan(union ccb * ccb)8309e6461a2SMatt Jacob xpt_rescan(union ccb *ccb)
8319e6461a2SMatt Jacob {
8329e6461a2SMatt Jacob 	struct ccb_hdr *hdr;
8332b83592fSScott Long 
83483c5d981SAlexander Motin 	/* Prepare request */
8350e85f214SMatt Jacob 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
836411cadaeSAlexander Motin 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
83783c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
8380e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8390e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
8400e85f214SMatt Jacob 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
8410e85f214SMatt Jacob 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
8420e85f214SMatt Jacob 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
84383c5d981SAlexander Motin 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
8440e85f214SMatt Jacob 	else {
8450e85f214SMatt Jacob 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
8460e85f214SMatt Jacob 		xpt_free_path(ccb->ccb_h.path);
8470e85f214SMatt Jacob 		xpt_free_ccb(ccb);
8480e85f214SMatt Jacob 		return;
8490e85f214SMatt Jacob 	}
85069be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
85169be012fSWarner Losh 	    ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
85269be012fSWarner Losh  		xpt_action_name(ccb->ccb_h.func_code)));
85369be012fSWarner Losh 
85483c5d981SAlexander Motin 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
85583c5d981SAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
85683c5d981SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
85783c5d981SAlexander Motin 	/* Don't make duplicate entries for the same paths. */
8582b83592fSScott Long 	xpt_lock_buses();
85983c5d981SAlexander Motin 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
8602b83592fSScott Long 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
8619e6461a2SMatt Jacob 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
8625a73cc12SKenneth D. Merry 				wakeup(&xsoftc.ccb_scanq);
8632b83592fSScott Long 				xpt_unlock_buses();
8649e6461a2SMatt Jacob 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
8659e6461a2SMatt Jacob 				xpt_free_path(ccb->ccb_h.path);
8669e6461a2SMatt Jacob 				xpt_free_ccb(ccb);
8679e6461a2SMatt Jacob 				return;
8689e6461a2SMatt Jacob 			}
8699e6461a2SMatt Jacob 		}
87083c5d981SAlexander Motin 	}
8712b83592fSScott Long 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
872a4876fbfSAlexander Motin 	xpt_hold_boot_locked();
8732b83592fSScott Long 	wakeup(&xsoftc.ccb_scanq);
8742b83592fSScott Long 	xpt_unlock_buses();
8759e6461a2SMatt Jacob }
8769e6461a2SMatt Jacob 
8778b8a9b1dSJustin T. Gibbs /* Functions accessed by the peripheral drivers */
8782b83592fSScott Long static int
xpt_init(void * dummy)8799e6461a2SMatt Jacob xpt_init(void *dummy)
8808b8a9b1dSJustin T. Gibbs {
8818b8a9b1dSJustin T. Gibbs 	struct cam_sim *xpt_sim;
8828b8a9b1dSJustin T. Gibbs 	struct cam_path *path;
883434bbf6eSJustin T. Gibbs 	struct cam_devq *devq;
8848b8a9b1dSJustin T. Gibbs 	cam_status status;
885227d67aaSAlexander Motin 	int error, i;
8868b8a9b1dSJustin T. Gibbs 
8872b83592fSScott Long 	TAILQ_INIT(&xsoftc.xpt_busses);
8882b83592fSScott Long 	TAILQ_INIT(&xsoftc.ccb_scanq);
8892b83592fSScott Long 	STAILQ_INIT(&xsoftc.highpowerq);
8902b83592fSScott Long 	xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
8918b8a9b1dSJustin T. Gibbs 
892daa5487fSAlexander Motin 	mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
893227d67aaSAlexander Motin 	xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
894227d67aaSAlexander Motin 	    taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
895ef3cf714SScott Long 
8966b156f61SSean Bruno #ifdef CAM_BOOT_DELAY
8976b156f61SSean Bruno 	/*
8986b156f61SSean Bruno 	 * Override this value at compile time to assist our users
8996b156f61SSean Bruno 	 * who don't use loader to boot a kernel.
9006b156f61SSean Bruno 	 */
9016b156f61SSean Bruno 	xsoftc.boot_delay = CAM_BOOT_DELAY;
9026b156f61SSean Bruno #endif
903a4876fbfSAlexander Motin 
9048b8a9b1dSJustin T. Gibbs 	/*
9051ffe5851SPedro F. Giffuni 	 * The xpt layer is, itself, the equivalent of a SIM.
9068b8a9b1dSJustin T. Gibbs 	 * Allow 16 ccbs in the ccb pool for it.  This should
907db4fcadfSConrad Meyer 	 * give decent parallelism when we probe buses and
9088b8a9b1dSJustin T. Gibbs 	 * perform other XPT functions.
9098b8a9b1dSJustin T. Gibbs 	 */
910434bbf6eSJustin T. Gibbs 	devq = cam_simq_alloc(16);
911*eb86c6c5SJohn Baldwin 	if (devq == NULL)
912*eb86c6c5SJohn Baldwin 		return (ENOMEM);
913434bbf6eSJustin T. Gibbs 	xpt_sim = cam_sim_alloc(xptaction,
914434bbf6eSJustin T. Gibbs 				xptpoll,
915434bbf6eSJustin T. Gibbs 				"xpt",
916434bbf6eSJustin T. Gibbs 				/*softc*/NULL,
917434bbf6eSJustin T. Gibbs 				/*unit*/0,
9187e8baf37SAlexander Motin 				/*mtx*/NULL,
919434bbf6eSJustin T. Gibbs 				/*max_dev_transactions*/0,
920434bbf6eSJustin T. Gibbs 				/*max_tagged_dev_transactions*/0,
921434bbf6eSJustin T. Gibbs 				devq);
9222b83592fSScott Long 	if (xpt_sim == NULL)
9232b83592fSScott Long 		return (ENOMEM);
9248b8a9b1dSJustin T. Gibbs 
92530f8afd0SWarner Losh 	if ((error = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
92630f8afd0SWarner Losh 		printf("xpt_init: xpt_bus_register failed with errno %d,"
92730f8afd0SWarner Losh 		       " failing attach\n", error);
9282b83592fSScott Long 		return (EINVAL);
929df826980SMatt Jacob 	}
9308b8a9b1dSJustin T. Gibbs 
9318b8a9b1dSJustin T. Gibbs 	/*
9328b8a9b1dSJustin T. Gibbs 	 * Looking at the XPT from the SIM layer, the XPT is
9331ffe5851SPedro F. Giffuni 	 * the equivalent of a peripheral driver.  Allocate
9348b8a9b1dSJustin T. Gibbs 	 * a peripheral driver entry for us.
9358b8a9b1dSJustin T. Gibbs 	 */
9368b8a9b1dSJustin T. Gibbs 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
9378b8a9b1dSJustin T. Gibbs 				      CAM_TARGET_WILDCARD,
9388b8a9b1dSJustin T. Gibbs 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
9398b8a9b1dSJustin T. Gibbs 		printf("xpt_init: xpt_create_path failed with status %#x,"
9408b8a9b1dSJustin T. Gibbs 		       " failing attach\n", status);
9412b83592fSScott Long 		return (EINVAL);
9428b8a9b1dSJustin T. Gibbs 	}
943daa5487fSAlexander Motin 	xpt_path_lock(path);
944ee9c90c7SKenneth D. Merry 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
9452b83592fSScott Long 			 path, NULL, 0, xpt_sim);
946daa5487fSAlexander Motin 	xpt_path_unlock(path);
9478b8a9b1dSJustin T. Gibbs 	xpt_free_path(path);
948daa5487fSAlexander Motin 
949227d67aaSAlexander Motin 	if (cam_num_doneqs < 1)
950227d67aaSAlexander Motin 		cam_num_doneqs = 1 + mp_ncpus / 6;
951227d67aaSAlexander Motin 	else if (cam_num_doneqs > MAXCPU)
952227d67aaSAlexander Motin 		cam_num_doneqs = MAXCPU;
953227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
954227d67aaSAlexander Motin 		mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
955227d67aaSAlexander Motin 		    MTX_DEF);
956227d67aaSAlexander Motin 		STAILQ_INIT(&cam_doneqs[i].cam_doneq);
957227d67aaSAlexander Motin 		error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
958227d67aaSAlexander Motin 		    &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
959227d67aaSAlexander Motin 		if (error != 0) {
960227d67aaSAlexander Motin 			cam_num_doneqs = i;
961227d67aaSAlexander Motin 			break;
962227d67aaSAlexander Motin 		}
963227d67aaSAlexander Motin 	}
964227d67aaSAlexander Motin 	if (cam_num_doneqs < 1) {
965227d67aaSAlexander Motin 		printf("xpt_init: Cannot init completion queues "
966227d67aaSAlexander Motin 		       "- failing attach\n");
967227d67aaSAlexander Motin 		return (ENOMEM);
968227d67aaSAlexander Motin 	}
969a4876fbfSAlexander Motin 
9707381bbeeSWarner Losh 	mtx_init(&cam_async.cam_doneq_mtx, "CAM async", NULL, MTX_DEF);
9717381bbeeSWarner Losh 	STAILQ_INIT(&cam_async.cam_doneq);
9727381bbeeSWarner Losh 	if (kproc_kthread_add(xpt_async_td, &cam_async,
9737381bbeeSWarner Losh 		&cam_proc, NULL, 0, 0, "cam", "async") != 0) {
9747381bbeeSWarner Losh 		printf("xpt_init: Cannot init async thread "
9757381bbeeSWarner Losh 		       "- failing attach\n");
9767381bbeeSWarner Losh 		return (ENOMEM);
9777381bbeeSWarner Losh 	}
9787381bbeeSWarner Losh 
9798b8a9b1dSJustin T. Gibbs 	/*
9808b8a9b1dSJustin T. Gibbs 	 * Register a callback for when interrupts are enabled.
9818b8a9b1dSJustin T. Gibbs 	 */
982a4876fbfSAlexander Motin 	config_intrhook_oneshot(xpt_config, NULL);
9838b8a9b1dSJustin T. Gibbs 
9842b83592fSScott Long 	return (0);
9858b8a9b1dSJustin T. Gibbs }
9868b8a9b1dSJustin T. Gibbs 
9878b8a9b1dSJustin T. Gibbs static cam_status
xptregister(struct cam_periph * periph,void * arg)9888b8a9b1dSJustin T. Gibbs xptregister(struct cam_periph *periph, void *arg)
9898b8a9b1dSJustin T. Gibbs {
9902b83592fSScott Long 	struct cam_sim *xpt_sim;
9912b83592fSScott Long 
9928b8a9b1dSJustin T. Gibbs 	if (periph == NULL) {
9938b8a9b1dSJustin T. Gibbs 		printf("xptregister: periph was NULL!!\n");
9948b8a9b1dSJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
9958b8a9b1dSJustin T. Gibbs 	}
9968b8a9b1dSJustin T. Gibbs 
9972b83592fSScott Long 	xpt_sim = (struct cam_sim *)arg;
9982b83592fSScott Long 	xpt_sim->softc = periph;
9998b8a9b1dSJustin T. Gibbs 	xpt_periph = periph;
10002b83592fSScott Long 	periph->softc = NULL;
10018b8a9b1dSJustin T. Gibbs 
10028b8a9b1dSJustin T. Gibbs 	return(CAM_REQ_CMP);
10038b8a9b1dSJustin T. Gibbs }
10048b8a9b1dSJustin T. Gibbs 
10058b8a9b1dSJustin T. Gibbs int32_t
xpt_add_periph(struct cam_periph * periph)10068b8a9b1dSJustin T. Gibbs xpt_add_periph(struct cam_periph *periph)
10078b8a9b1dSJustin T. Gibbs {
10088b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10098b8a9b1dSJustin T. Gibbs 	int32_t	 status;
10108b8a9b1dSJustin T. Gibbs 
1011227d67aaSAlexander Motin 	TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
10128b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10138b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;
10148b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1015227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10168b8a9b1dSJustin T. Gibbs 		device->generation++;
1017227d67aaSAlexander Motin 		SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1018227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1019636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10208b8a9b1dSJustin T. Gibbs 	}
10218b8a9b1dSJustin T. Gibbs 
10228b8a9b1dSJustin T. Gibbs 	return (status);
10238b8a9b1dSJustin T. Gibbs }
10248b8a9b1dSJustin T. Gibbs 
10258b8a9b1dSJustin T. Gibbs void
xpt_remove_periph(struct cam_periph * periph)1026a29779e8SAlexander Motin xpt_remove_periph(struct cam_periph *periph)
10278b8a9b1dSJustin T. Gibbs {
10288b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
10298b8a9b1dSJustin T. Gibbs 
10308b8a9b1dSJustin T. Gibbs 	device = periph->path->device;
10318b8a9b1dSJustin T. Gibbs 	if (device != NULL) {
1032227d67aaSAlexander Motin 		mtx_lock(&device->target->bus->eb_mtx);
10338b8a9b1dSJustin T. Gibbs 		device->generation++;
1034227d67aaSAlexander Motin 		SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1035227d67aaSAlexander Motin 		mtx_unlock(&device->target->bus->eb_mtx);
1036636870ffSWill Andrews 		atomic_add_32(&xsoftc.xpt_generation, 1);
10378b8a9b1dSJustin T. Gibbs 	}
10388b8a9b1dSJustin T. Gibbs }
10398b8a9b1dSJustin T. Gibbs 
10403393f8daSKenneth D. Merry void
xpt_announce_periph(struct cam_periph * periph,char * announce_string)10413393f8daSKenneth D. Merry xpt_announce_periph(struct cam_periph *periph, char *announce_string)
10423393f8daSKenneth D. Merry {
10430a57cdd9SJohn Baldwin 	char buf[128];
10440a57cdd9SJohn Baldwin 	struct sbuf sb;
10453393f8daSKenneth D. Merry 
10460a57cdd9SJohn Baldwin 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
10470a57cdd9SJohn Baldwin 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
10480a57cdd9SJohn Baldwin 	xpt_announce_periph_sbuf(periph, &sb, announce_string);
10490a57cdd9SJohn Baldwin 	(void)sbuf_finish(&sb);
10503393f8daSKenneth D. Merry }
10518b8a9b1dSJustin T. Gibbs 
10526fb5c84eSSteven Hartland void
xpt_announce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb,char * announce_string)10535d01277fSScott Long xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
10545d01277fSScott Long     char *announce_string)
10555d01277fSScott Long {
10565d01277fSScott Long 	struct	cam_path *path = periph->path;
10575d01277fSScott Long 	struct  xpt_proto *proto;
10585d01277fSScott Long 
10595d01277fSScott Long 	cam_periph_assert(periph, MA_OWNED);
10605d01277fSScott Long 	periph->flags |= CAM_PERIPH_ANNOUNCED;
10615d01277fSScott Long 
10625d01277fSScott Long 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
10635d01277fSScott Long 	    periph->periph_name, periph->unit_number,
10645d01277fSScott Long 	    path->bus->sim->sim_name,
10655d01277fSScott Long 	    path->bus->sim->unit_number,
10665d01277fSScott Long 	    path->bus->sim->bus_id,
10675d01277fSScott Long 	    path->bus->path_id,
10685d01277fSScott Long 	    path->target->target_id,
10695d01277fSScott Long 	    (uintmax_t)path->device->lun_id);
10705d01277fSScott Long 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1071b8271176SJohn Baldwin 	proto = xpt_proto_find(path->device->protocol);
10725d01277fSScott Long 	if (proto)
10735d01277fSScott Long 		proto->ops->announce_sbuf(path->device, sb);
10745d01277fSScott Long 	else
107576b2e390SJohn Baldwin 		sbuf_printf(sb, "Unknown protocol device %d\n",
10765d01277fSScott Long 		    path->device->protocol);
10775d01277fSScott Long 	if (path->device->serial_num_len > 0) {
10785d01277fSScott Long 		/* Don't wrap the screen  - print only the first 60 chars */
10795d01277fSScott Long 		sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
10805d01277fSScott Long 		    periph->periph_name, periph->unit_number,
10815d01277fSScott Long 		    path->device->serial_num);
10825d01277fSScott Long 	}
10835d01277fSScott Long 	/* Announce transport details. */
10845d01277fSScott Long 	path->bus->xport->ops->announce_sbuf(periph, sb);
10855d01277fSScott Long 	/* Announce command queueing. */
10865d01277fSScott Long 	if (path->device->inq_flags & SID_CmdQue
10875d01277fSScott Long 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
10885d01277fSScott Long 		sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
10895d01277fSScott Long 		    periph->periph_name, periph->unit_number);
10905d01277fSScott Long 	}
10915d01277fSScott Long 	/* Announce caller's details if they've passed in. */
10925d01277fSScott Long 	if (announce_string != NULL)
10935d01277fSScott Long 		sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
10945d01277fSScott Long 		    periph->unit_number, announce_string);
10955d01277fSScott Long }
10965d01277fSScott Long 
10975d01277fSScott Long void
xpt_announce_quirks(struct cam_periph * periph,int quirks,char * bit_string)10986fb5c84eSSteven Hartland xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
10996fb5c84eSSteven Hartland {
11006fb5c84eSSteven Hartland 	if (quirks != 0) {
11016fb5c84eSSteven Hartland 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
11026fb5c84eSSteven Hartland 		    periph->unit_number, quirks, bit_string);
11036fb5c84eSSteven Hartland 	}
11046fb5c84eSSteven Hartland }
11056fb5c84eSSteven Hartland 
11068d36a71bSAlexander Motin void
xpt_announce_quirks_sbuf(struct cam_periph * periph,struct sbuf * sb,int quirks,char * bit_string)11075d01277fSScott Long xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
11085d01277fSScott Long 			 int quirks, char *bit_string)
11095d01277fSScott Long {
11105d01277fSScott Long 	if (quirks != 0) {
11115d01277fSScott Long 		sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
11125d01277fSScott Long 		    periph->unit_number, quirks, bit_string);
11135d01277fSScott Long 	}
11145d01277fSScott Long }
11155d01277fSScott Long 
11165d01277fSScott Long void
xpt_denounce_periph(struct cam_periph * periph)11178d36a71bSAlexander Motin xpt_denounce_periph(struct cam_periph *periph)
11188d36a71bSAlexander Motin {
11190a57cdd9SJohn Baldwin 	char buf[128];
11200a57cdd9SJohn Baldwin 	struct sbuf sb;
11218d36a71bSAlexander Motin 
11220a57cdd9SJohn Baldwin 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
11230a57cdd9SJohn Baldwin 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
11240a57cdd9SJohn Baldwin 	xpt_denounce_periph_sbuf(periph, &sb);
11250a57cdd9SJohn Baldwin 	(void)sbuf_finish(&sb);
11268d36a71bSAlexander Motin }
11278d36a71bSAlexander Motin 
11285d01277fSScott Long void
xpt_denounce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb)11295d01277fSScott Long xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
11305d01277fSScott Long {
11315d01277fSScott Long 	struct cam_path *path = periph->path;
11325d01277fSScott Long 	struct xpt_proto *proto;
11335d01277fSScott Long 
11345d01277fSScott Long 	cam_periph_assert(periph, MA_OWNED);
11355d01277fSScott Long 
11365d01277fSScott Long 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
11375d01277fSScott Long 	    periph->periph_name, periph->unit_number,
11385d01277fSScott Long 	    path->bus->sim->sim_name,
11395d01277fSScott Long 	    path->bus->sim->unit_number,
11405d01277fSScott Long 	    path->bus->sim->bus_id,
11415d01277fSScott Long 	    path->bus->path_id,
11425d01277fSScott Long 	    path->target->target_id,
11435d01277fSScott Long 	    (uintmax_t)path->device->lun_id);
11445d01277fSScott Long 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1145b8271176SJohn Baldwin 	proto = xpt_proto_find(path->device->protocol);
11465d01277fSScott Long 	if (proto)
11475d01277fSScott Long 		proto->ops->denounce_sbuf(path->device, sb);
11485d01277fSScott Long 	else
114976b2e390SJohn Baldwin 		sbuf_printf(sb, "Unknown protocol device %d",
11505d01277fSScott Long 		    path->device->protocol);
11515d01277fSScott Long 	if (path->device->serial_num_len > 0)
11525d01277fSScott Long 		sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1153519b24f0SAlexander Motin 	sbuf_cat(sb, " detached\n");
11545d01277fSScott Long }
11558d36a71bSAlexander Motin 
11563501942bSJustin T. Gibbs int
xpt_getattr(char * buf,size_t len,const char * attr,struct cam_path * path)11573501942bSJustin T. Gibbs xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
11583501942bSJustin T. Gibbs {
115900d72d57SAlexander Motin 	int ret = -1, l, o;
11603501942bSJustin T. Gibbs 	struct ccb_dev_advinfo cdai;
11610feb46b0SScott Long 	struct scsi_vpd_device_id *did;
1162ccba7102SAlexander Motin 	struct scsi_vpd_id_descriptor *idd;
11633501942bSJustin T. Gibbs 
1164227d67aaSAlexander Motin 	xpt_path_assert(path, MA_OWNED);
11656884b662SAlexander Motin 
11663501942bSJustin T. Gibbs 	memset(&cdai, 0, sizeof(cdai));
11673501942bSJustin T. Gibbs 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
11683501942bSJustin T. Gibbs 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1169ab4327bbSAlexander Motin 	cdai.flags = CDAI_FLAG_NONE;
11703501942bSJustin T. Gibbs 	cdai.bufsiz = len;
117135a9ffc3SAlexander Motin 	cdai.buf = buf;
11723501942bSJustin T. Gibbs 
11733501942bSJustin T. Gibbs 	if (!strcmp(attr, "GEOM::ident"))
11743501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
11753501942bSJustin T. Gibbs 	else if (!strcmp(attr, "GEOM::physpath"))
11763501942bSJustin T. Gibbs 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
117740f27d7cSAlexander Motin 	else if (strcmp(attr, "GEOM::lunid") == 0 ||
117840f27d7cSAlexander Motin 		 strcmp(attr, "GEOM::lunname") == 0) {
1179ccba7102SAlexander Motin 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1180ccba7102SAlexander Motin 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
118135a9ffc3SAlexander Motin 		cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT);
11823501942bSJustin T. Gibbs 		if (cdai.buf == NULL) {
11833501942bSJustin T. Gibbs 			ret = ENOMEM;
11843501942bSJustin T. Gibbs 			goto out;
11853501942bSJustin T. Gibbs 		}
118635a9ffc3SAlexander Motin 	} else
118735a9ffc3SAlexander Motin 		goto out;
118835a9ffc3SAlexander Motin 
11893501942bSJustin T. Gibbs 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
11903501942bSJustin T. Gibbs 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
11913501942bSJustin T. Gibbs 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
11923501942bSJustin T. Gibbs 	if (cdai.provsiz == 0)
11933501942bSJustin T. Gibbs 		goto out;
11940feb46b0SScott Long 	switch(cdai.buftype) {
11950feb46b0SScott Long 	case CDAI_TYPE_SCSI_DEVID:
11960feb46b0SScott Long 		did = (struct scsi_vpd_device_id *)cdai.buf;
119740f27d7cSAlexander Motin 		if (strcmp(attr, "GEOM::lunid") == 0) {
11980feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
11990feb46b0SScott Long 			    scsi_devid_is_lun_naa);
1200ccba7102SAlexander Motin 			if (idd == NULL)
12010feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12020feb46b0SScott Long 				    scsi_devid_is_lun_eui64);
120300d72d57SAlexander Motin 			if (idd == NULL)
12040feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12050feb46b0SScott Long 				    scsi_devid_is_lun_uuid);
120600d72d57SAlexander Motin 			if (idd == NULL)
12070feb46b0SScott Long 				idd = scsi_get_devid(did, cdai.provsiz,
12080feb46b0SScott Long 				    scsi_devid_is_lun_md5);
120940f27d7cSAlexander Motin 		} else
121040f27d7cSAlexander Motin 			idd = NULL;
12110feb46b0SScott Long 
1212ccba7102SAlexander Motin 		if (idd == NULL)
12130feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
12140feb46b0SScott Long 			    scsi_devid_is_lun_t10);
1215ccba7102SAlexander Motin 		if (idd == NULL)
12160feb46b0SScott Long 			idd = scsi_get_devid(did, cdai.provsiz,
12170feb46b0SScott Long 			    scsi_devid_is_lun_name);
1218ccba7102SAlexander Motin 		if (idd == NULL)
12190feb46b0SScott Long 			break;
12200feb46b0SScott Long 
1221ccba7102SAlexander Motin 		ret = 0;
12220feb46b0SScott Long 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
12230feb46b0SScott Long 		    SVPD_ID_CODESET_ASCII) {
1224fa91cabfSAlexander Motin 			if (idd->length < len) {
1225fa91cabfSAlexander Motin 				for (l = 0; l < idd->length; l++)
1226fa91cabfSAlexander Motin 					buf[l] = idd->identifier[l] ?
1227fa91cabfSAlexander Motin 					    idd->identifier[l] : ' ';
1228fa91cabfSAlexander Motin 				buf[l] = 0;
1229fa91cabfSAlexander Motin 			} else
1230fa91cabfSAlexander Motin 				ret = EFAULT;
12310feb46b0SScott Long 			break;
12320feb46b0SScott Long 		}
12330feb46b0SScott Long 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
12340feb46b0SScott Long 		    SVPD_ID_CODESET_UTF8) {
1235ccba7102SAlexander Motin 			l = strnlen(idd->identifier, idd->length);
1236ccba7102SAlexander Motin 			if (l < len) {
1237ccba7102SAlexander Motin 				bcopy(idd->identifier, buf, l);
1238ccba7102SAlexander Motin 				buf[l] = 0;
1239ccba7102SAlexander Motin 			} else
1240ccba7102SAlexander Motin 				ret = EFAULT;
12410feb46b0SScott Long 			break;
12420feb46b0SScott Long 		}
12430feb46b0SScott Long 		if ((idd->id_type & SVPD_ID_TYPE_MASK) ==
12440feb46b0SScott Long 		    SVPD_ID_TYPE_UUID && idd->identifier[0] == 0x10) {
12450feb46b0SScott Long 			if ((idd->length - 2) * 2 + 4 >= len) {
12460feb46b0SScott Long 				ret = EFAULT;
12470feb46b0SScott Long 				break;
12480feb46b0SScott Long 			}
124900d72d57SAlexander Motin 			for (l = 2, o = 0; l < idd->length; l++) {
12503157c368SAlexander Motin 				if (l == 6 || l == 8 || l == 10 || l == 12)
12513157c368SAlexander Motin 				    o += sprintf(buf + o, "-");
125200d72d57SAlexander Motin 				o += sprintf(buf + o, "%02x",
125300d72d57SAlexander Motin 				    idd->identifier[l]);
125400d72d57SAlexander Motin 			}
12550feb46b0SScott Long 			break;
12560feb46b0SScott Long 		}
1257ccba7102SAlexander Motin 		if (idd->length * 2 < len) {
1258ccba7102SAlexander Motin 			for (l = 0; l < idd->length; l++)
1259ccba7102SAlexander Motin 				sprintf(buf + l * 2, "%02x",
1260ccba7102SAlexander Motin 				    idd->identifier[l]);
1261ccba7102SAlexander Motin 		} else
1262ccba7102SAlexander Motin 				ret = EFAULT;
12630feb46b0SScott Long 		break;
12640feb46b0SScott Long 	default:
126535a9ffc3SAlexander Motin 		if (cdai.provsiz < len) {
126635a9ffc3SAlexander Motin 			cdai.buf[cdai.provsiz] = 0;
12673501942bSJustin T. Gibbs 			ret = 0;
126835a9ffc3SAlexander Motin 		} else
12693501942bSJustin T. Gibbs 			ret = EFAULT;
12700feb46b0SScott Long 		break;
1271ccba7102SAlexander Motin 	}
12723501942bSJustin T. Gibbs 
12733501942bSJustin T. Gibbs out:
127435a9ffc3SAlexander Motin 	if ((char *)cdai.buf != buf)
12753501942bSJustin T. Gibbs 		free(cdai.buf, M_CAMXPT);
12763501942bSJustin T. Gibbs 	return ret;
12773501942bSJustin T. Gibbs }
12783501942bSJustin T. Gibbs 
12798b8a9b1dSJustin T. Gibbs static dev_match_ret
xptbusmatch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_eb * bus)12803393f8daSKenneth D. Merry xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
12818b8a9b1dSJustin T. Gibbs 	    struct cam_eb *bus)
12828b8a9b1dSJustin T. Gibbs {
12838b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1284167e63e3SPedro F. Giffuni 	u_int i;
12858b8a9b1dSJustin T. Gibbs 
12868b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
12878b8a9b1dSJustin T. Gibbs 
12888b8a9b1dSJustin T. Gibbs 	/*
12898b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
12908b8a9b1dSJustin T. Gibbs 	 */
12918b8a9b1dSJustin T. Gibbs 	if (bus == NULL)
12928b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
12938b8a9b1dSJustin T. Gibbs 
12948b8a9b1dSJustin T. Gibbs 	/*
12958b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this bus matches no
12968b8a9b1dSJustin T. Gibbs 	 * matter what.
12978b8a9b1dSJustin T. Gibbs 	 */
12988b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
12998b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
13008b8a9b1dSJustin T. Gibbs 
13018b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
13028b8a9b1dSJustin T. Gibbs 		struct bus_match_pattern *cur_pattern;
13038f9be1eeSAlexander Motin 		struct device_match_pattern *dp = &patterns[i].pattern.device_pattern;
13048f9be1eeSAlexander Motin 		struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
13058b8a9b1dSJustin T. Gibbs 
13068b8a9b1dSJustin T. Gibbs 		/*
13078b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a bus node, we
13088b8a9b1dSJustin T. Gibbs 		 * aren't interested.  However, we do indicate to the
13098b8a9b1dSJustin T. Gibbs 		 * calling routine that we should continue descending the
13108b8a9b1dSJustin T. Gibbs 		 * tree, since the user wants to match against lower-level
13118b8a9b1dSJustin T. Gibbs 		 * EDT elements.
13128b8a9b1dSJustin T. Gibbs 		 */
13138f9be1eeSAlexander Motin 		if (patterns[i].type == DEV_MATCH_DEVICE &&
13148f9be1eeSAlexander Motin 		    (dp->flags & DEV_MATCH_PATH) != 0 &&
13158f9be1eeSAlexander Motin 		    dp->path_id != bus->path_id)
13168f9be1eeSAlexander Motin 			continue;
13178f9be1eeSAlexander Motin 		if (patterns[i].type == DEV_MATCH_PERIPH &&
13188f9be1eeSAlexander Motin 		    (pp->flags & PERIPH_MATCH_PATH) != 0 &&
13198f9be1eeSAlexander Motin 		    pp->path_id != bus->path_id)
13208f9be1eeSAlexander Motin 			continue;
13218b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_BUS) {
13228b8a9b1dSJustin T. Gibbs 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
13238b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
13248b8a9b1dSJustin T. Gibbs 			continue;
13258b8a9b1dSJustin T. Gibbs 		}
13268b8a9b1dSJustin T. Gibbs 
13278b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.bus_pattern;
13288b8a9b1dSJustin T. Gibbs 
13298b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
13308b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != bus->path_id))
13318b8a9b1dSJustin T. Gibbs 			continue;
13328b8a9b1dSJustin T. Gibbs 
13338b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
13348b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->bus_id != bus->sim->bus_id))
13358b8a9b1dSJustin T. Gibbs 			continue;
13368b8a9b1dSJustin T. Gibbs 
13378b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
13388b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != bus->sim->unit_number))
13398b8a9b1dSJustin T. Gibbs 			continue;
13408b8a9b1dSJustin T. Gibbs 
13418b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
13428b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
13438b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
13448b8a9b1dSJustin T. Gibbs 			continue;
13458b8a9b1dSJustin T. Gibbs 
13468b8a9b1dSJustin T. Gibbs 		/*
13478b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
13488b8a9b1dSJustin T. Gibbs 		 * information on this bus.  So tell the caller to copy the
13498b8a9b1dSJustin T. Gibbs 		 * data out.
13508b8a9b1dSJustin T. Gibbs 		 */
13518b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
13528b8a9b1dSJustin T. Gibbs 
13538b8a9b1dSJustin T. Gibbs 		/*
13548b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
13558b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a non-bus matching
13568b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
13578b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
13588b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a non-bus
13598b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
13608b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
13618b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
13628b8a9b1dSJustin T. Gibbs 		 */
13638b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
13648b8a9b1dSJustin T. Gibbs 			return(retval);
13658b8a9b1dSJustin T. Gibbs 	}
13668b8a9b1dSJustin T. Gibbs 
13678b8a9b1dSJustin T. Gibbs 	/*
13688b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
13698b8a9b1dSJustin T. Gibbs 	 * we haven't seen anything other than bus matching patterns.  So
13708b8a9b1dSJustin T. Gibbs 	 * tell the caller to stop descending the tree -- the user doesn't
13718b8a9b1dSJustin T. Gibbs 	 * want to match against lower level tree elements.
13728b8a9b1dSJustin T. Gibbs 	 */
13738b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
13748b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
13758b8a9b1dSJustin T. Gibbs 
13768b8a9b1dSJustin T. Gibbs 	return(retval);
13778b8a9b1dSJustin T. Gibbs }
13788b8a9b1dSJustin T. Gibbs 
13798b8a9b1dSJustin T. Gibbs static dev_match_ret
xptdevicematch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_ed * device)13803393f8daSKenneth D. Merry xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
13818b8a9b1dSJustin T. Gibbs 	       struct cam_ed *device)
13828b8a9b1dSJustin T. Gibbs {
13838b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1384167e63e3SPedro F. Giffuni 	u_int i;
13858b8a9b1dSJustin T. Gibbs 
13868b8a9b1dSJustin T. Gibbs 	retval = DM_RET_NONE;
13878b8a9b1dSJustin T. Gibbs 
13888b8a9b1dSJustin T. Gibbs 	/*
13898b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
13908b8a9b1dSJustin T. Gibbs 	 */
13918b8a9b1dSJustin T. Gibbs 	if (device == NULL)
13928b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
13938b8a9b1dSJustin T. Gibbs 
13948b8a9b1dSJustin T. Gibbs 	/*
13958b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this device matches no
13968b8a9b1dSJustin T. Gibbs 	 * matter what.
13978b8a9b1dSJustin T. Gibbs 	 */
139859e75884SColin Percival 	if ((patterns == NULL) || (num_patterns == 0))
13998b8a9b1dSJustin T. Gibbs 		return(DM_RET_DESCEND | DM_RET_COPY);
14008b8a9b1dSJustin T. Gibbs 
14018b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
14028b8a9b1dSJustin T. Gibbs 		struct device_match_pattern *cur_pattern;
14033501942bSJustin T. Gibbs 		struct scsi_vpd_device_id *device_id_page;
14048f9be1eeSAlexander Motin 		struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
14058b8a9b1dSJustin T. Gibbs 
14068b8a9b1dSJustin T. Gibbs 		/*
14078b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a device node, we
14088b8a9b1dSJustin T. Gibbs 		 * aren't interested.
14098b8a9b1dSJustin T. Gibbs 		 */
14108f9be1eeSAlexander Motin 		if (patterns[i].type == DEV_MATCH_PERIPH &&
14118f9be1eeSAlexander Motin 		    (pp->flags & PERIPH_MATCH_TARGET) != 0 &&
14128f9be1eeSAlexander Motin 		    pp->target_id != device->target->target_id)
14138f9be1eeSAlexander Motin 			continue;
14148f9be1eeSAlexander Motin 		if (patterns[i].type == DEV_MATCH_PERIPH &&
14158f9be1eeSAlexander Motin 		    (pp->flags & PERIPH_MATCH_LUN) != 0 &&
14168f9be1eeSAlexander Motin 		    pp->target_lun != device->lun_id)
14178f9be1eeSAlexander Motin 			continue;
14188b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_DEVICE) {
14198b8a9b1dSJustin T. Gibbs 			if ((patterns[i].type == DEV_MATCH_PERIPH)
14208b8a9b1dSJustin T. Gibbs 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
14218b8a9b1dSJustin T. Gibbs 				retval |= DM_RET_DESCEND;
14228b8a9b1dSJustin T. Gibbs 			continue;
14238b8a9b1dSJustin T. Gibbs 		}
14248b8a9b1dSJustin T. Gibbs 
14258b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.device_pattern;
14268b8a9b1dSJustin T. Gibbs 
14273501942bSJustin T. Gibbs 		/* Error out if mutually exclusive options are specified. */
14283501942bSJustin T. Gibbs 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
14293501942bSJustin T. Gibbs 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
14303501942bSJustin T. Gibbs 			return(DM_RET_ERROR);
14313501942bSJustin T. Gibbs 
14328b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
14338b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != device->target->bus->path_id))
14348b8a9b1dSJustin T. Gibbs 			continue;
14358b8a9b1dSJustin T. Gibbs 
14368b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
14378b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_id != device->target->target_id))
14388b8a9b1dSJustin T. Gibbs 			continue;
14398b8a9b1dSJustin T. Gibbs 
14408b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
14418b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->target_lun != device->lun_id))
14428b8a9b1dSJustin T. Gibbs 			continue;
14438b8a9b1dSJustin T. Gibbs 
14448b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
14458b8a9b1dSJustin T. Gibbs 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
14463501942bSJustin T. Gibbs 				    (caddr_t)&cur_pattern->data.inq_pat,
14473501942bSJustin T. Gibbs 				    1, sizeof(cur_pattern->data.inq_pat),
14488b8a9b1dSJustin T. Gibbs 				    scsi_static_inquiry_match) == NULL))
14498b8a9b1dSJustin T. Gibbs 			continue;
14508b8a9b1dSJustin T. Gibbs 
14513501942bSJustin T. Gibbs 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
14523501942bSJustin T. Gibbs 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
14533501942bSJustin T. Gibbs 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
14543501942bSJustin T. Gibbs 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
14553501942bSJustin T. Gibbs 				      device->device_id_len
14563501942bSJustin T. Gibbs 				    - SVPD_DEVICE_ID_HDR_LEN,
14573501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id,
14583501942bSJustin T. Gibbs 				      cur_pattern->data.devid_pat.id_len) != 0))
14593501942bSJustin T. Gibbs 			continue;
14603501942bSJustin T. Gibbs 
14618b8a9b1dSJustin T. Gibbs 		/*
14628b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
14638b8a9b1dSJustin T. Gibbs 		 * information on this device.  So tell the caller to copy
14648b8a9b1dSJustin T. Gibbs 		 * the data out.
14658b8a9b1dSJustin T. Gibbs 		 */
14668b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
14678b8a9b1dSJustin T. Gibbs 
14688b8a9b1dSJustin T. Gibbs 		/*
14698b8a9b1dSJustin T. Gibbs 		 * If the return action has been set to descend, then we
14708b8a9b1dSJustin T. Gibbs 		 * know that we've already seen a peripheral matching
14718b8a9b1dSJustin T. Gibbs 		 * expression, therefore we need to further descend the tree.
14728b8a9b1dSJustin T. Gibbs 		 * This won't change by continuing around the loop, so we
14738b8a9b1dSJustin T. Gibbs 		 * go ahead and return.  If we haven't seen a peripheral
14748b8a9b1dSJustin T. Gibbs 		 * matching expression, we keep going around the loop until
14758b8a9b1dSJustin T. Gibbs 		 * we exhaust the matching expressions.  We'll set the stop
14768b8a9b1dSJustin T. Gibbs 		 * flag once we fall out of the loop.
14778b8a9b1dSJustin T. Gibbs 		 */
14788b8a9b1dSJustin T. Gibbs 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
14798b8a9b1dSJustin T. Gibbs 			return(retval);
14808b8a9b1dSJustin T. Gibbs 	}
14818b8a9b1dSJustin T. Gibbs 
14828b8a9b1dSJustin T. Gibbs 	/*
14838b8a9b1dSJustin T. Gibbs 	 * If the return action hasn't been set to descend yet, that means
14848b8a9b1dSJustin T. Gibbs 	 * we haven't seen any peripheral matching patterns.  So tell the
14858b8a9b1dSJustin T. Gibbs 	 * caller to stop descending the tree -- the user doesn't want to
14868b8a9b1dSJustin T. Gibbs 	 * match against lower level tree elements.
14878b8a9b1dSJustin T. Gibbs 	 */
14888b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
14898b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_STOP;
14908b8a9b1dSJustin T. Gibbs 
14918b8a9b1dSJustin T. Gibbs 	return(retval);
14928b8a9b1dSJustin T. Gibbs }
14938b8a9b1dSJustin T. Gibbs 
14948b8a9b1dSJustin T. Gibbs /*
14958b8a9b1dSJustin T. Gibbs  * Match a single peripheral against any number of match patterns.
14968b8a9b1dSJustin T. Gibbs  */
14978b8a9b1dSJustin T. Gibbs static dev_match_ret
xptperiphmatch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_periph * periph)14983393f8daSKenneth D. Merry xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
14998b8a9b1dSJustin T. Gibbs 	       struct cam_periph *periph)
15008b8a9b1dSJustin T. Gibbs {
15018b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
1502167e63e3SPedro F. Giffuni 	u_int i;
15038b8a9b1dSJustin T. Gibbs 
15048b8a9b1dSJustin T. Gibbs 	/*
15058b8a9b1dSJustin T. Gibbs 	 * If we aren't given something to match against, that's an error.
15068b8a9b1dSJustin T. Gibbs 	 */
15078b8a9b1dSJustin T. Gibbs 	if (periph == NULL)
15088b8a9b1dSJustin T. Gibbs 		return(DM_RET_ERROR);
15098b8a9b1dSJustin T. Gibbs 
15108b8a9b1dSJustin T. Gibbs 	/*
15118b8a9b1dSJustin T. Gibbs 	 * If there are no match entries, then this peripheral matches no
15128b8a9b1dSJustin T. Gibbs 	 * matter what.
15138b8a9b1dSJustin T. Gibbs 	 */
15148b8a9b1dSJustin T. Gibbs 	if ((patterns == NULL) || (num_patterns == 0))
15158b8a9b1dSJustin T. Gibbs 		return(DM_RET_STOP | DM_RET_COPY);
15168b8a9b1dSJustin T. Gibbs 
15178b8a9b1dSJustin T. Gibbs 	/*
15188b8a9b1dSJustin T. Gibbs 	 * There aren't any nodes below a peripheral node, so there's no
15198b8a9b1dSJustin T. Gibbs 	 * reason to descend the tree any further.
15208b8a9b1dSJustin T. Gibbs 	 */
15218b8a9b1dSJustin T. Gibbs 	retval = DM_RET_STOP;
15228b8a9b1dSJustin T. Gibbs 
15238b8a9b1dSJustin T. Gibbs 	for (i = 0; i < num_patterns; i++) {
15248b8a9b1dSJustin T. Gibbs 		struct periph_match_pattern *cur_pattern;
15258b8a9b1dSJustin T. Gibbs 
15268b8a9b1dSJustin T. Gibbs 		/*
15278b8a9b1dSJustin T. Gibbs 		 * If the pattern in question isn't for a peripheral, we
15288b8a9b1dSJustin T. Gibbs 		 * aren't interested.
15298b8a9b1dSJustin T. Gibbs 		 */
15308b8a9b1dSJustin T. Gibbs 		if (patterns[i].type != DEV_MATCH_PERIPH)
15318b8a9b1dSJustin T. Gibbs 			continue;
15328b8a9b1dSJustin T. Gibbs 
15338b8a9b1dSJustin T. Gibbs 		cur_pattern = &patterns[i].pattern.periph_pattern;
15348b8a9b1dSJustin T. Gibbs 
15358b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
15368b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->path_id != periph->path->bus->path_id))
15378b8a9b1dSJustin T. Gibbs 			continue;
15388b8a9b1dSJustin T. Gibbs 
15398b8a9b1dSJustin T. Gibbs 		/*
15408b8a9b1dSJustin T. Gibbs 		 * For the target and lun id's, we have to make sure the
15418b8a9b1dSJustin T. Gibbs 		 * target and lun pointers aren't NULL.  The xpt peripheral
15428b8a9b1dSJustin T. Gibbs 		 * has a wildcard target and device.
15438b8a9b1dSJustin T. Gibbs 		 */
15448b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
15458b8a9b1dSJustin T. Gibbs 		 && ((periph->path->target == NULL)
15468b8a9b1dSJustin T. Gibbs 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
15478b8a9b1dSJustin T. Gibbs 			continue;
15488b8a9b1dSJustin T. Gibbs 
15498b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
15508b8a9b1dSJustin T. Gibbs 		 && ((periph->path->device == NULL)
15518b8a9b1dSJustin T. Gibbs 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
15528b8a9b1dSJustin T. Gibbs 			continue;
15538b8a9b1dSJustin T. Gibbs 
15548b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
15558b8a9b1dSJustin T. Gibbs 		 && (cur_pattern->unit_number != periph->unit_number))
15568b8a9b1dSJustin T. Gibbs 			continue;
15578b8a9b1dSJustin T. Gibbs 
15588b8a9b1dSJustin T. Gibbs 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
15598b8a9b1dSJustin T. Gibbs 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
15608b8a9b1dSJustin T. Gibbs 			     DEV_IDLEN) != 0))
15618b8a9b1dSJustin T. Gibbs 			continue;
15628b8a9b1dSJustin T. Gibbs 
15638b8a9b1dSJustin T. Gibbs 		/*
15648b8a9b1dSJustin T. Gibbs 		 * If we get to this point, the user definitely wants
15658b8a9b1dSJustin T. Gibbs 		 * information on this peripheral.  So tell the caller to
15668b8a9b1dSJustin T. Gibbs 		 * copy the data out.
15678b8a9b1dSJustin T. Gibbs 		 */
15688b8a9b1dSJustin T. Gibbs 		retval |= DM_RET_COPY;
15698b8a9b1dSJustin T. Gibbs 
15708b8a9b1dSJustin T. Gibbs 		/*
15718b8a9b1dSJustin T. Gibbs 		 * The return action has already been set to stop, since
15728b8a9b1dSJustin T. Gibbs 		 * peripherals don't have any nodes below them in the EDT.
15738b8a9b1dSJustin T. Gibbs 		 */
15748b8a9b1dSJustin T. Gibbs 		return(retval);
15758b8a9b1dSJustin T. Gibbs 	}
15768b8a9b1dSJustin T. Gibbs 
15778b8a9b1dSJustin T. Gibbs 	/*
15788b8a9b1dSJustin T. Gibbs 	 * If we get to this point, the peripheral that was passed in
15798b8a9b1dSJustin T. Gibbs 	 * doesn't match any of the patterns.
15808b8a9b1dSJustin T. Gibbs 	 */
15818b8a9b1dSJustin T. Gibbs 	return(retval);
15828b8a9b1dSJustin T. Gibbs }
15838b8a9b1dSJustin T. Gibbs 
15848b8a9b1dSJustin T. Gibbs static int
xptedtbusfunc(struct cam_eb * bus,void * arg)15858b8a9b1dSJustin T. Gibbs xptedtbusfunc(struct cam_eb *bus, void *arg)
15868b8a9b1dSJustin T. Gibbs {
15878b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1588227d67aaSAlexander Motin 	struct cam_et *target;
15898b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
15908b8a9b1dSJustin T. Gibbs 
15918b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
15928b8a9b1dSJustin T. Gibbs 
15938b8a9b1dSJustin T. Gibbs 	/*
15948b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
15958b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
15968b8a9b1dSJustin T. Gibbs 	 */
15978b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
15988b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
15998b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16008b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target != NULL))
16018b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
16028b8a9b1dSJustin T. Gibbs 	else
16038b8a9b1dSJustin T. Gibbs 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
16048b8a9b1dSJustin T. Gibbs 
16058b8a9b1dSJustin T. Gibbs 	/*
16068b8a9b1dSJustin T. Gibbs 	 * If we got an error, bail out of the search.
16078b8a9b1dSJustin T. Gibbs 	 */
16088b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
16098b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
16108b8a9b1dSJustin T. Gibbs 		return(0);
16118b8a9b1dSJustin T. Gibbs 	}
16128b8a9b1dSJustin T. Gibbs 
16138b8a9b1dSJustin T. Gibbs 	/*
16148b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this bus out.
16158b8a9b1dSJustin T. Gibbs 	 */
16168b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
16178b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
16188b8a9b1dSJustin T. Gibbs 
16198b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
16208b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
16218b8a9b1dSJustin T. Gibbs 
16228b8a9b1dSJustin T. Gibbs 		/*
16238b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
16248b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
16258b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
16268b8a9b1dSJustin T. Gibbs 		 */
16278b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
16288b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
16298b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
16308b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
16318b8a9b1dSJustin T. Gibbs 
16328b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = bus;
16338b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
16342b83592fSScott Long 				xsoftc.bus_generation;
16358b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
16368b8a9b1dSJustin T. Gibbs 			return(0);
16378b8a9b1dSJustin T. Gibbs 		}
16388b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
16398b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
16408b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_BUS;
16418b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
16428b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
16438b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.bus_result.unit_number =
16448b8a9b1dSJustin T. Gibbs 			bus->sim->unit_number;
1645b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.bus_result.dev_name,
1646b0f662feSAlan Somers 			bus->sim->sim_name,
1647b0f662feSAlan Somers 			sizeof(cdm->matches[j].result.bus_result.dev_name));
16488b8a9b1dSJustin T. Gibbs 	}
16498b8a9b1dSJustin T. Gibbs 
16508b8a9b1dSJustin T. Gibbs 	/*
1651db4fcadfSConrad Meyer 	 * If the user is only interested in buses, there's no
16528b8a9b1dSJustin T. Gibbs 	 * reason to descend to the next level in the tree.
16538b8a9b1dSJustin T. Gibbs 	 */
16548b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
16558b8a9b1dSJustin T. Gibbs 		return(1);
16568b8a9b1dSJustin T. Gibbs 
16578b8a9b1dSJustin T. Gibbs 	/*
16588b8a9b1dSJustin T. Gibbs 	 * If there is a target generation recorded, check it to
16598b8a9b1dSJustin T. Gibbs 	 * make sure the target list hasn't changed.
16608b8a9b1dSJustin T. Gibbs 	 */
1661227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16628b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
16638b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.bus == bus)
16648b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1665227d67aaSAlexander Motin 	 && (cdm->pos.cookie.target != NULL)) {
1666227d67aaSAlexander Motin 		if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1667227d67aaSAlexander Motin 		    bus->generation)) {
1668227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1669227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1670227d67aaSAlexander Motin 			return (0);
1671227d67aaSAlexander Motin 		}
1672227d67aaSAlexander Motin 		target = (struct cam_et *)cdm->pos.cookie.target;
1673227d67aaSAlexander Motin 		target->refcount++;
1674227d67aaSAlexander Motin 	} else
1675227d67aaSAlexander Motin 		target = NULL;
1676227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1677227d67aaSAlexander Motin 
1678227d67aaSAlexander Motin 	return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
16798b8a9b1dSJustin T. Gibbs }
16808b8a9b1dSJustin T. Gibbs 
16818b8a9b1dSJustin T. Gibbs static int
xptedttargetfunc(struct cam_et * target,void * arg)16828b8a9b1dSJustin T. Gibbs xptedttargetfunc(struct cam_et *target, void *arg)
16838b8a9b1dSJustin T. Gibbs {
16848b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
1685227d67aaSAlexander Motin 	struct cam_eb *bus;
1686227d67aaSAlexander Motin 	struct cam_ed *device;
16878b8a9b1dSJustin T. Gibbs 
16888b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1689227d67aaSAlexander Motin 	bus = target->bus;
16908b8a9b1dSJustin T. Gibbs 
16918b8a9b1dSJustin T. Gibbs 	/*
16928b8a9b1dSJustin T. Gibbs 	 * If there is a device list generation recorded, check it to
16938b8a9b1dSJustin T. Gibbs 	 * make sure the device list hasn't changed.
16948b8a9b1dSJustin T. Gibbs 	 */
1695227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
16968b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1697227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
16988b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
16998b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == target)
17008b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1701227d67aaSAlexander Motin 	 && (cdm->pos.cookie.device != NULL)) {
1702227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1703227d67aaSAlexander Motin 		    target->generation) {
1704227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
17058b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
17068b8a9b1dSJustin T. Gibbs 			return(0);
17078b8a9b1dSJustin T. Gibbs 		}
1708227d67aaSAlexander Motin 		device = (struct cam_ed *)cdm->pos.cookie.device;
1709227d67aaSAlexander Motin 		device->refcount++;
1710227d67aaSAlexander Motin 	} else
1711227d67aaSAlexander Motin 		device = NULL;
1712227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
17138b8a9b1dSJustin T. Gibbs 
1714227d67aaSAlexander Motin 	return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
17158b8a9b1dSJustin T. Gibbs }
17168b8a9b1dSJustin T. Gibbs 
17178b8a9b1dSJustin T. Gibbs static int
xptedtdevicefunc(struct cam_ed * device,void * arg)17188b8a9b1dSJustin T. Gibbs xptedtdevicefunc(struct cam_ed *device, void *arg)
17198b8a9b1dSJustin T. Gibbs {
1720227d67aaSAlexander Motin 	struct cam_eb *bus;
1721227d67aaSAlexander Motin 	struct cam_periph *periph;
17228b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
17238b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
17248b8a9b1dSJustin T. Gibbs 
17258b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
1726227d67aaSAlexander Motin 	bus = device->target->bus;
17278b8a9b1dSJustin T. Gibbs 
17288b8a9b1dSJustin T. Gibbs 	/*
17298b8a9b1dSJustin T. Gibbs 	 * If our position is for something deeper in the tree, that means
17308b8a9b1dSJustin T. Gibbs 	 * that we've already seen this node.  So, we keep going down.
17318b8a9b1dSJustin T. Gibbs 	 */
17328b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
17338b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
17348b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
17358b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.periph != NULL))
17368b8a9b1dSJustin T. Gibbs 		retval = DM_RET_DESCEND;
17378b8a9b1dSJustin T. Gibbs 	else
17388b8a9b1dSJustin T. Gibbs 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
17398b8a9b1dSJustin T. Gibbs 					device);
17408b8a9b1dSJustin T. Gibbs 
17418b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
17428b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
17438b8a9b1dSJustin T. Gibbs 		return(0);
17448b8a9b1dSJustin T. Gibbs 	}
17458b8a9b1dSJustin T. Gibbs 
17468b8a9b1dSJustin T. Gibbs 	/*
17478b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this device out.
17488b8a9b1dSJustin T. Gibbs 	 */
17498b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
17508b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
17518b8a9b1dSJustin T. Gibbs 
17528b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
17538b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
17548b8a9b1dSJustin T. Gibbs 
17558b8a9b1dSJustin T. Gibbs 		/*
17568b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
17578b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
17588b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
17598b8a9b1dSJustin T. Gibbs 		 */
17608b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
17618b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
17628b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
17638b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
17648b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
17658b8a9b1dSJustin T. Gibbs 
17668b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = device->target->bus;
17678b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
17682b83592fSScott Long 				xsoftc.bus_generation;
17698b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = device->target;
17708b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
17718b8a9b1dSJustin T. Gibbs 				device->target->bus->generation;
17728b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = device;
17738b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
17748b8a9b1dSJustin T. Gibbs 				device->target->generation;
17758b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
17768b8a9b1dSJustin T. Gibbs 			return(0);
17778b8a9b1dSJustin T. Gibbs 		}
17788b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
17798b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
17808b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_DEVICE;
17818b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.path_id =
17828b8a9b1dSJustin T. Gibbs 			device->target->bus->path_id;
17838b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_id =
17848b8a9b1dSJustin T. Gibbs 			device->target->target_id;
17858b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.device_result.target_lun =
17868b8a9b1dSJustin T. Gibbs 			device->lun_id;
178752c9ce25SScott Long 		cdm->matches[j].result.device_result.protocol =
178852c9ce25SScott Long 			device->protocol;
17898b8a9b1dSJustin T. Gibbs 		bcopy(&device->inq_data,
17908b8a9b1dSJustin T. Gibbs 		      &cdm->matches[j].result.device_result.inq_data,
17918b8a9b1dSJustin T. Gibbs 		      sizeof(struct scsi_inquiry_data));
179252c9ce25SScott Long 		bcopy(&device->ident_data,
179352c9ce25SScott Long 		      &cdm->matches[j].result.device_result.ident_data,
179452c9ce25SScott Long 		      sizeof(struct ata_params));
17959deea857SKenneth D. Merry 
17969deea857SKenneth D. Merry 		/* Let the user know whether this device is unconfigured */
17979deea857SKenneth D. Merry 		if (device->flags & CAM_DEV_UNCONFIGURED)
17989deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
17999deea857SKenneth D. Merry 				DEV_RESULT_UNCONFIGURED;
18009deea857SKenneth D. Merry 		else
18019deea857SKenneth D. Merry 			cdm->matches[j].result.device_result.flags =
18029deea857SKenneth D. Merry 				DEV_RESULT_NOFLAG;
18038b8a9b1dSJustin T. Gibbs 	}
18048b8a9b1dSJustin T. Gibbs 
18058b8a9b1dSJustin T. Gibbs 	/*
18068b8a9b1dSJustin T. Gibbs 	 * If the user isn't interested in peripherals, don't descend
18078b8a9b1dSJustin T. Gibbs 	 * the tree any further.
18088b8a9b1dSJustin T. Gibbs 	 */
18098b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
18108b8a9b1dSJustin T. Gibbs 		return(1);
18118b8a9b1dSJustin T. Gibbs 
18128b8a9b1dSJustin T. Gibbs 	/*
18138b8a9b1dSJustin T. Gibbs 	 * If there is a peripheral list generation recorded, make sure
18148b8a9b1dSJustin T. Gibbs 	 * it hasn't changed.
18158b8a9b1dSJustin T. Gibbs 	 */
1816227d67aaSAlexander Motin 	xpt_lock_buses();
1817227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
18188b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1819227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus == bus)
18208b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
18218b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.target == device->target)
18228b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
18238b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.device == device)
18248b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1825227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1826227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1827227d67aaSAlexander Motin 		    device->generation) {
1828227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
1829227d67aaSAlexander Motin 			xpt_unlock_buses();
1830227d67aaSAlexander Motin 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1831227d67aaSAlexander Motin 			return(0);
1832227d67aaSAlexander Motin 		}
1833227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1834227d67aaSAlexander Motin 		periph->refcount++;
1835227d67aaSAlexander Motin 	} else
1836227d67aaSAlexander Motin 		periph = NULL;
1837227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
1838227d67aaSAlexander Motin 	xpt_unlock_buses();
1839227d67aaSAlexander Motin 
1840227d67aaSAlexander Motin 	return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
18418b8a9b1dSJustin T. Gibbs }
18428b8a9b1dSJustin T. Gibbs 
18438b8a9b1dSJustin T. Gibbs static int
xptedtperiphfunc(struct cam_periph * periph,void * arg)18448b8a9b1dSJustin T. Gibbs xptedtperiphfunc(struct cam_periph *periph, void *arg)
18458b8a9b1dSJustin T. Gibbs {
18468b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
18478b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
18488b8a9b1dSJustin T. Gibbs 
18498b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
18508b8a9b1dSJustin T. Gibbs 
18518b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
18528b8a9b1dSJustin T. Gibbs 
18538b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
18548b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
18558b8a9b1dSJustin T. Gibbs 		return(0);
18568b8a9b1dSJustin T. Gibbs 	}
18578b8a9b1dSJustin T. Gibbs 
18588b8a9b1dSJustin T. Gibbs 	/*
18598b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
18608b8a9b1dSJustin T. Gibbs 	 */
18618b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
18628b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
1863b0f662feSAlan Somers 		size_t l;
18648b8a9b1dSJustin T. Gibbs 
18658b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
18668b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
18678b8a9b1dSJustin T. Gibbs 
18688b8a9b1dSJustin T. Gibbs 		/*
18698b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
18708b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
18718b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
18728b8a9b1dSJustin T. Gibbs 		 */
18738b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
18748b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
18758b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
18768b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
18778b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
18788b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
18798b8a9b1dSJustin T. Gibbs 
18808b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.bus = periph->path->bus;
18818b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_BUS_GENERATION]=
18822b83592fSScott Long 				xsoftc.bus_generation;
18838b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.target = periph->path->target;
18848b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_TARGET_GENERATION] =
18858b8a9b1dSJustin T. Gibbs 				periph->path->bus->generation;
18868b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.device = periph->path->device;
18878b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_DEV_GENERATION] =
18888b8a9b1dSJustin T. Gibbs 				periph->path->target->generation;
18898b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
18908b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
18918b8a9b1dSJustin T. Gibbs 				periph->path->device->generation;
18928b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
18938b8a9b1dSJustin T. Gibbs 			return(0);
18948b8a9b1dSJustin T. Gibbs 		}
18958b8a9b1dSJustin T. Gibbs 
18968b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
18978b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
18988b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
18998b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
19008b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
19018b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_id =
19028b8a9b1dSJustin T. Gibbs 			periph->path->target->target_id;
19038b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.target_lun =
19048b8a9b1dSJustin T. Gibbs 			periph->path->device->lun_id;
19058b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
19068b8a9b1dSJustin T. Gibbs 			periph->unit_number;
1907b0f662feSAlan Somers 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
1908b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
1909b0f662feSAlan Somers 			periph->periph_name, l);
19108b8a9b1dSJustin T. Gibbs 	}
19118b8a9b1dSJustin T. Gibbs 
19128b8a9b1dSJustin T. Gibbs 	return(1);
19138b8a9b1dSJustin T. Gibbs }
19148b8a9b1dSJustin T. Gibbs 
19158b8a9b1dSJustin T. Gibbs static int
xptedtmatch(struct ccb_dev_match * cdm)19168b8a9b1dSJustin T. Gibbs xptedtmatch(struct ccb_dev_match *cdm)
19178b8a9b1dSJustin T. Gibbs {
1918227d67aaSAlexander Motin 	struct cam_eb *bus;
19198b8a9b1dSJustin T. Gibbs 	int ret;
19208b8a9b1dSJustin T. Gibbs 
19218b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
19228b8a9b1dSJustin T. Gibbs 
19238b8a9b1dSJustin T. Gibbs 	/*
19248b8a9b1dSJustin T. Gibbs 	 * Check the bus list generation.  If it has changed, the user
19258b8a9b1dSJustin T. Gibbs 	 * needs to reset everything and start over.
19268b8a9b1dSJustin T. Gibbs 	 */
1927227d67aaSAlexander Motin 	xpt_lock_buses();
19288b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1929227d67aaSAlexander Motin 	 && (cdm->pos.cookie.bus != NULL)) {
1930227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1931227d67aaSAlexander Motin 		    xsoftc.bus_generation) {
1932227d67aaSAlexander Motin 			xpt_unlock_buses();
19338b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19348b8a9b1dSJustin T. Gibbs 			return(0);
19358b8a9b1dSJustin T. Gibbs 		}
1936227d67aaSAlexander Motin 		bus = (struct cam_eb *)cdm->pos.cookie.bus;
1937227d67aaSAlexander Motin 		bus->refcount++;
1938227d67aaSAlexander Motin 	} else
1939227d67aaSAlexander Motin 		bus = NULL;
1940227d67aaSAlexander Motin 	xpt_unlock_buses();
19418b8a9b1dSJustin T. Gibbs 
1942227d67aaSAlexander Motin 	ret = xptbustraverse(bus, xptedtbusfunc, cdm);
19438b8a9b1dSJustin T. Gibbs 
19448b8a9b1dSJustin T. Gibbs 	/*
19458b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
19468b8a9b1dSJustin T. Gibbs 	 * traversing the EDT.  It also means that one of the subroutines
19478b8a9b1dSJustin T. Gibbs 	 * has set the status field to the proper value.  If we get back 1,
19488b8a9b1dSJustin T. Gibbs 	 * we've fully traversed the EDT and copied out any matching entries.
19498b8a9b1dSJustin T. Gibbs 	 */
19508b8a9b1dSJustin T. Gibbs 	if (ret == 1)
19518b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
19528b8a9b1dSJustin T. Gibbs 
19538b8a9b1dSJustin T. Gibbs 	return(ret);
19548b8a9b1dSJustin T. Gibbs }
19558b8a9b1dSJustin T. Gibbs 
19568b8a9b1dSJustin T. Gibbs static int
xptplistpdrvfunc(struct periph_driver ** pdrv,void * arg)19578b8a9b1dSJustin T. Gibbs xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
19588b8a9b1dSJustin T. Gibbs {
1959227d67aaSAlexander Motin 	struct cam_periph *periph;
19608b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19618b8a9b1dSJustin T. Gibbs 
19628b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19638b8a9b1dSJustin T. Gibbs 
1964227d67aaSAlexander Motin 	xpt_lock_buses();
19658b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
19668b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv == pdrv)
19678b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1968227d67aaSAlexander Motin 	 && (cdm->pos.cookie.periph != NULL)) {
1969227d67aaSAlexander Motin 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1970227d67aaSAlexander Motin 		    (*pdrv)->generation) {
1971227d67aaSAlexander Motin 			xpt_unlock_buses();
19728b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
19738b8a9b1dSJustin T. Gibbs 			return(0);
19748b8a9b1dSJustin T. Gibbs 		}
1975227d67aaSAlexander Motin 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1976227d67aaSAlexander Motin 		periph->refcount++;
1977227d67aaSAlexander Motin 	} else
1978227d67aaSAlexander Motin 		periph = NULL;
1979227d67aaSAlexander Motin 	xpt_unlock_buses();
19808b8a9b1dSJustin T. Gibbs 
1981227d67aaSAlexander Motin 	return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
19828b8a9b1dSJustin T. Gibbs }
19838b8a9b1dSJustin T. Gibbs 
19848b8a9b1dSJustin T. Gibbs static int
xptplistperiphfunc(struct cam_periph * periph,void * arg)19858b8a9b1dSJustin T. Gibbs xptplistperiphfunc(struct cam_periph *periph, void *arg)
19868b8a9b1dSJustin T. Gibbs {
19878b8a9b1dSJustin T. Gibbs 	struct ccb_dev_match *cdm;
19888b8a9b1dSJustin T. Gibbs 	dev_match_ret retval;
19898b8a9b1dSJustin T. Gibbs 
19908b8a9b1dSJustin T. Gibbs 	cdm = (struct ccb_dev_match *)arg;
19918b8a9b1dSJustin T. Gibbs 
19928b8a9b1dSJustin T. Gibbs 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
19938b8a9b1dSJustin T. Gibbs 
19948b8a9b1dSJustin T. Gibbs 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
19958b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_ERROR;
19968b8a9b1dSJustin T. Gibbs 		return(0);
19978b8a9b1dSJustin T. Gibbs 	}
19988b8a9b1dSJustin T. Gibbs 
19998b8a9b1dSJustin T. Gibbs 	/*
20008b8a9b1dSJustin T. Gibbs 	 * If the copy flag is set, copy this peripheral out.
20018b8a9b1dSJustin T. Gibbs 	 */
20028b8a9b1dSJustin T. Gibbs 	if (retval & DM_RET_COPY) {
20038b8a9b1dSJustin T. Gibbs 		int spaceleft, j;
2004b0f662feSAlan Somers 		size_t l;
20058b8a9b1dSJustin T. Gibbs 
20068b8a9b1dSJustin T. Gibbs 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
20078b8a9b1dSJustin T. Gibbs 			sizeof(struct dev_match_result));
20088b8a9b1dSJustin T. Gibbs 
20098b8a9b1dSJustin T. Gibbs 		/*
20108b8a9b1dSJustin T. Gibbs 		 * If we don't have enough space to put in another
20118b8a9b1dSJustin T. Gibbs 		 * match result, save our position and tell the
20128b8a9b1dSJustin T. Gibbs 		 * user there are more devices to check.
20138b8a9b1dSJustin T. Gibbs 		 */
20148b8a9b1dSJustin T. Gibbs 		if (spaceleft < sizeof(struct dev_match_result)) {
20158b8a9b1dSJustin T. Gibbs 			struct periph_driver **pdrv;
20168b8a9b1dSJustin T. Gibbs 
20178b8a9b1dSJustin T. Gibbs 			pdrv = NULL;
20188b8a9b1dSJustin T. Gibbs 			bzero(&cdm->pos, sizeof(cdm->pos));
20198b8a9b1dSJustin T. Gibbs 			cdm->pos.position_type =
20208b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
20218b8a9b1dSJustin T. Gibbs 				CAM_DEV_POS_PERIPH;
20228b8a9b1dSJustin T. Gibbs 
20238b8a9b1dSJustin T. Gibbs 			/*
20248b8a9b1dSJustin T. Gibbs 			 * This may look a bit non-sensical, but it is
20258b8a9b1dSJustin T. Gibbs 			 * actually quite logical.  There are very few
20268b8a9b1dSJustin T. Gibbs 			 * peripheral drivers, and bloating every peripheral
20278b8a9b1dSJustin T. Gibbs 			 * structure with a pointer back to its parent
20288b8a9b1dSJustin T. Gibbs 			 * peripheral driver linker set entry would cost
20298b8a9b1dSJustin T. Gibbs 			 * more in the long run than doing this quick lookup.
20308b8a9b1dSJustin T. Gibbs 			 */
20310b7c27b9SPeter Wemm 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
20328b8a9b1dSJustin T. Gibbs 				if (strcmp((*pdrv)->driver_name,
20338b8a9b1dSJustin T. Gibbs 				    periph->periph_name) == 0)
20348b8a9b1dSJustin T. Gibbs 					break;
20358b8a9b1dSJustin T. Gibbs 			}
20368b8a9b1dSJustin T. Gibbs 
203701910babSScott Long 			if (*pdrv == NULL) {
20388b8a9b1dSJustin T. Gibbs 				cdm->status = CAM_DEV_MATCH_ERROR;
20398b8a9b1dSJustin T. Gibbs 				return(0);
20408b8a9b1dSJustin T. Gibbs 			}
20418b8a9b1dSJustin T. Gibbs 
20428b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.pdrv = pdrv;
20438b8a9b1dSJustin T. Gibbs 			/*
20448b8a9b1dSJustin T. Gibbs 			 * The periph generation slot does double duty, as
20458b8a9b1dSJustin T. Gibbs 			 * does the periph pointer slot.  They are used for
20468b8a9b1dSJustin T. Gibbs 			 * both edt and pdrv lookups and positioning.
20478b8a9b1dSJustin T. Gibbs 			 */
20488b8a9b1dSJustin T. Gibbs 			cdm->pos.cookie.periph = periph;
20498b8a9b1dSJustin T. Gibbs 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
20508b8a9b1dSJustin T. Gibbs 				(*pdrv)->generation;
20518b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_MORE;
20528b8a9b1dSJustin T. Gibbs 			return(0);
20538b8a9b1dSJustin T. Gibbs 		}
20548b8a9b1dSJustin T. Gibbs 
20558b8a9b1dSJustin T. Gibbs 		j = cdm->num_matches;
20568b8a9b1dSJustin T. Gibbs 		cdm->num_matches++;
20578b8a9b1dSJustin T. Gibbs 		cdm->matches[j].type = DEV_MATCH_PERIPH;
20588b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.path_id =
20598b8a9b1dSJustin T. Gibbs 			periph->path->bus->path_id;
20608b8a9b1dSJustin T. Gibbs 
20618b8a9b1dSJustin T. Gibbs 		/*
20628b8a9b1dSJustin T. Gibbs 		 * The transport layer peripheral doesn't have a target or
20638b8a9b1dSJustin T. Gibbs 		 * lun.
20648b8a9b1dSJustin T. Gibbs 		 */
20658b8a9b1dSJustin T. Gibbs 		if (periph->path->target)
20668b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_id =
20678b8a9b1dSJustin T. Gibbs 				periph->path->target->target_id;
20688b8a9b1dSJustin T. Gibbs 		else
2069431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_id =
2070431d3a5bSAlexander Motin 				CAM_TARGET_WILDCARD;
20718b8a9b1dSJustin T. Gibbs 
20728b8a9b1dSJustin T. Gibbs 		if (periph->path->device)
20738b8a9b1dSJustin T. Gibbs 			cdm->matches[j].result.periph_result.target_lun =
20748b8a9b1dSJustin T. Gibbs 				periph->path->device->lun_id;
20758b8a9b1dSJustin T. Gibbs 		else
2076431d3a5bSAlexander Motin 			cdm->matches[j].result.periph_result.target_lun =
2077431d3a5bSAlexander Motin 				CAM_LUN_WILDCARD;
20788b8a9b1dSJustin T. Gibbs 
20798b8a9b1dSJustin T. Gibbs 		cdm->matches[j].result.periph_result.unit_number =
20808b8a9b1dSJustin T. Gibbs 			periph->unit_number;
2081b0f662feSAlan Somers 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2082b0f662feSAlan Somers 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
2083b0f662feSAlan Somers 			periph->periph_name, l);
20848b8a9b1dSJustin T. Gibbs 	}
20858b8a9b1dSJustin T. Gibbs 
20868b8a9b1dSJustin T. Gibbs 	return(1);
20878b8a9b1dSJustin T. Gibbs }
20888b8a9b1dSJustin T. Gibbs 
20898b8a9b1dSJustin T. Gibbs static int
xptperiphlistmatch(struct ccb_dev_match * cdm)20908b8a9b1dSJustin T. Gibbs xptperiphlistmatch(struct ccb_dev_match *cdm)
20918b8a9b1dSJustin T. Gibbs {
20928b8a9b1dSJustin T. Gibbs 	int ret;
20938b8a9b1dSJustin T. Gibbs 
20948b8a9b1dSJustin T. Gibbs 	cdm->num_matches = 0;
20958b8a9b1dSJustin T. Gibbs 
20968b8a9b1dSJustin T. Gibbs 	/*
20978b8a9b1dSJustin T. Gibbs 	 * At this point in the edt traversal function, we check the bus
2098db4fcadfSConrad Meyer 	 * list generation to make sure that no buses have been added or
20998b8a9b1dSJustin T. Gibbs 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
21008b8a9b1dSJustin T. Gibbs 	 * For the peripheral driver list traversal function, however, we
21018b8a9b1dSJustin T. Gibbs 	 * don't have to worry about new peripheral driver types coming or
21028b8a9b1dSJustin T. Gibbs 	 * going; they're in a linker set, and therefore can't change
21038b8a9b1dSJustin T. Gibbs 	 * without a recompile.
21048b8a9b1dSJustin T. Gibbs 	 */
21058b8a9b1dSJustin T. Gibbs 
21068b8a9b1dSJustin T. Gibbs 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
21078b8a9b1dSJustin T. Gibbs 	 && (cdm->pos.cookie.pdrv != NULL))
21088b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(
21098b8a9b1dSJustin T. Gibbs 				(struct periph_driver **)cdm->pos.cookie.pdrv,
21108b8a9b1dSJustin T. Gibbs 				xptplistpdrvfunc, cdm);
21118b8a9b1dSJustin T. Gibbs 	else
21128b8a9b1dSJustin T. Gibbs 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
21138b8a9b1dSJustin T. Gibbs 
21148b8a9b1dSJustin T. Gibbs 	/*
21158b8a9b1dSJustin T. Gibbs 	 * If we get back 0, that means that we had to stop before fully
21168b8a9b1dSJustin T. Gibbs 	 * traversing the peripheral driver tree.  It also means that one of
21178b8a9b1dSJustin T. Gibbs 	 * the subroutines has set the status field to the proper value.  If
21188b8a9b1dSJustin T. Gibbs 	 * we get back 1, we've fully traversed the EDT and copied out any
21198b8a9b1dSJustin T. Gibbs 	 * matching entries.
21208b8a9b1dSJustin T. Gibbs 	 */
21218b8a9b1dSJustin T. Gibbs 	if (ret == 1)
21228b8a9b1dSJustin T. Gibbs 		cdm->status = CAM_DEV_MATCH_LAST;
21238b8a9b1dSJustin T. Gibbs 
21248b8a9b1dSJustin T. Gibbs 	return(ret);
21258b8a9b1dSJustin T. Gibbs }
21268b8a9b1dSJustin T. Gibbs 
21278b8a9b1dSJustin T. Gibbs static int
xptbustraverse(struct cam_eb * start_bus,xpt_busfunc_t * tr_func,void * arg)21288b8a9b1dSJustin T. Gibbs xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
21298b8a9b1dSJustin T. Gibbs {
21308b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus, *next_bus;
21318b8a9b1dSJustin T. Gibbs 	int retval;
21328b8a9b1dSJustin T. Gibbs 
21338b8a9b1dSJustin T. Gibbs 	retval = 1;
2134227d67aaSAlexander Motin 	if (start_bus)
2135227d67aaSAlexander Motin 		bus = start_bus;
2136227d67aaSAlexander Motin 	else {
21379a7c2696SAlexander Motin 		xpt_lock_buses();
2138227d67aaSAlexander Motin 		bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2139227d67aaSAlexander Motin 		if (bus == NULL) {
21409a7c2696SAlexander Motin 			xpt_unlock_buses();
2141227d67aaSAlexander Motin 			return (retval);
2142227d67aaSAlexander Motin 		}
2143227d67aaSAlexander Motin 		bus->refcount++;
2144227d67aaSAlexander Motin 		xpt_unlock_buses();
2145227d67aaSAlexander Motin 	}
2146227d67aaSAlexander Motin 	for (; bus != NULL; bus = next_bus) {
21478b8a9b1dSJustin T. Gibbs 		retval = tr_func(bus, arg);
2148227d67aaSAlexander Motin 		if (retval == 0) {
2149227d67aaSAlexander Motin 			xpt_release_bus(bus);
2150227d67aaSAlexander Motin 			break;
2151227d67aaSAlexander Motin 		}
21529a7c2696SAlexander Motin 		xpt_lock_buses();
21538900f4b8SKenneth D. Merry 		next_bus = TAILQ_NEXT(bus, links);
2154227d67aaSAlexander Motin 		if (next_bus)
2155227d67aaSAlexander Motin 			next_bus->refcount++;
21569a7c2696SAlexander Motin 		xpt_unlock_buses();
21578900f4b8SKenneth D. Merry 		xpt_release_bus(bus);
21588b8a9b1dSJustin T. Gibbs 	}
21598b8a9b1dSJustin T. Gibbs 	return(retval);
21608b8a9b1dSJustin T. Gibbs }
21618b8a9b1dSJustin T. Gibbs 
21628b8a9b1dSJustin T. Gibbs static int
xpttargettraverse(struct cam_eb * bus,struct cam_et * start_target,xpt_targetfunc_t * tr_func,void * arg)21638b8a9b1dSJustin T. Gibbs xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
21648b8a9b1dSJustin T. Gibbs 		  xpt_targetfunc_t *tr_func, void *arg)
21658b8a9b1dSJustin T. Gibbs {
21668b8a9b1dSJustin T. Gibbs 	struct cam_et *target, *next_target;
21678b8a9b1dSJustin T. Gibbs 	int retval;
21688b8a9b1dSJustin T. Gibbs 
21698b8a9b1dSJustin T. Gibbs 	retval = 1;
2170227d67aaSAlexander Motin 	if (start_target)
2171227d67aaSAlexander Motin 		target = start_target;
2172227d67aaSAlexander Motin 	else {
2173227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2174227d67aaSAlexander Motin 		target = TAILQ_FIRST(&bus->et_entries);
2175227d67aaSAlexander Motin 		if (target == NULL) {
2176227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
21778b8a9b1dSJustin T. Gibbs 			return (retval);
21788b8a9b1dSJustin T. Gibbs 		}
2179227d67aaSAlexander Motin 		target->refcount++;
2180227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2181227d67aaSAlexander Motin 	}
2182227d67aaSAlexander Motin 	for (; target != NULL; target = next_target) {
2183227d67aaSAlexander Motin 		retval = tr_func(target, arg);
2184227d67aaSAlexander Motin 		if (retval == 0) {
2185227d67aaSAlexander Motin 			xpt_release_target(target);
2186227d67aaSAlexander Motin 			break;
2187227d67aaSAlexander Motin 		}
2188227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2189227d67aaSAlexander Motin 		next_target = TAILQ_NEXT(target, links);
2190227d67aaSAlexander Motin 		if (next_target)
2191227d67aaSAlexander Motin 			next_target->refcount++;
2192227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2193227d67aaSAlexander Motin 		xpt_release_target(target);
2194227d67aaSAlexander Motin 	}
21958b8a9b1dSJustin T. Gibbs 	return(retval);
21968b8a9b1dSJustin T. Gibbs }
21978b8a9b1dSJustin T. Gibbs 
21988b8a9b1dSJustin T. Gibbs static int
xptdevicetraverse(struct cam_et * target,struct cam_ed * start_device,xpt_devicefunc_t * tr_func,void * arg)21998b8a9b1dSJustin T. Gibbs xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
22008b8a9b1dSJustin T. Gibbs 		  xpt_devicefunc_t *tr_func, void *arg)
22018b8a9b1dSJustin T. Gibbs {
2202227d67aaSAlexander Motin 	struct cam_eb *bus;
22038b8a9b1dSJustin T. Gibbs 	struct cam_ed *device, *next_device;
22048b8a9b1dSJustin T. Gibbs 	int retval;
22058b8a9b1dSJustin T. Gibbs 
22068b8a9b1dSJustin T. Gibbs 	retval = 1;
2207227d67aaSAlexander Motin 	bus = target->bus;
2208227d67aaSAlexander Motin 	if (start_device)
2209227d67aaSAlexander Motin 		device = start_device;
2210227d67aaSAlexander Motin 	else {
2211227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2212227d67aaSAlexander Motin 		device = TAILQ_FIRST(&target->ed_entries);
2213227d67aaSAlexander Motin 		if (device == NULL) {
2214227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
22158b8a9b1dSJustin T. Gibbs 			return (retval);
22168b8a9b1dSJustin T. Gibbs 		}
2217227d67aaSAlexander Motin 		device->refcount++;
2218227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2219227d67aaSAlexander Motin 	}
2220227d67aaSAlexander Motin 	for (; device != NULL; device = next_device) {
2221227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
2222227d67aaSAlexander Motin 		retval = tr_func(device, arg);
2223227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
2224227d67aaSAlexander Motin 		if (retval == 0) {
2225227d67aaSAlexander Motin 			xpt_release_device(device);
2226227d67aaSAlexander Motin 			break;
2227227d67aaSAlexander Motin 		}
2228227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2229227d67aaSAlexander Motin 		next_device = TAILQ_NEXT(device, links);
2230227d67aaSAlexander Motin 		if (next_device)
2231227d67aaSAlexander Motin 			next_device->refcount++;
2232227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2233227d67aaSAlexander Motin 		xpt_release_device(device);
2234227d67aaSAlexander Motin 	}
22358b8a9b1dSJustin T. Gibbs 	return(retval);
22368b8a9b1dSJustin T. Gibbs }
22378b8a9b1dSJustin T. Gibbs 
22388b8a9b1dSJustin T. Gibbs static int
xptperiphtraverse(struct cam_ed * device,struct cam_periph * start_periph,xpt_periphfunc_t * tr_func,void * arg)22398b8a9b1dSJustin T. Gibbs xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
22408b8a9b1dSJustin T. Gibbs 		  xpt_periphfunc_t *tr_func, void *arg)
22418b8a9b1dSJustin T. Gibbs {
2242227d67aaSAlexander Motin 	struct cam_eb *bus;
22438b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
22448b8a9b1dSJustin T. Gibbs 	int retval;
22458b8a9b1dSJustin T. Gibbs 
22468b8a9b1dSJustin T. Gibbs 	retval = 1;
22478b8a9b1dSJustin T. Gibbs 
2248227d67aaSAlexander Motin 	bus = device->target->bus;
2249227d67aaSAlexander Motin 	if (start_periph)
2250227d67aaSAlexander Motin 		periph = start_periph;
2251227d67aaSAlexander Motin 	else {
22528900f4b8SKenneth D. Merry 		xpt_lock_buses();
2253227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2254227d67aaSAlexander Motin 		periph = SLIST_FIRST(&device->periphs);
2255227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2256227d67aaSAlexander Motin 			periph = SLIST_NEXT(periph, periph_links);
2257227d67aaSAlexander Motin 		if (periph == NULL) {
2258227d67aaSAlexander Motin 			mtx_unlock(&bus->eb_mtx);
22598900f4b8SKenneth D. Merry 			xpt_unlock_buses();
2260227d67aaSAlexander Motin 			return (retval);
2261227d67aaSAlexander Motin 		}
2262227d67aaSAlexander Motin 		periph->refcount++;
2263227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2264227d67aaSAlexander Motin 		xpt_unlock_buses();
2265227d67aaSAlexander Motin 	}
2266227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2267227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2268227d67aaSAlexander Motin 		if (retval == 0) {
22699813c936SAlexander Motin 			cam_periph_release_locked(periph);
2270227d67aaSAlexander Motin 			break;
2271227d67aaSAlexander Motin 		}
2272227d67aaSAlexander Motin 		xpt_lock_buses();
2273227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
2274227d67aaSAlexander Motin 		next_periph = SLIST_NEXT(periph, periph_links);
2275227d67aaSAlexander Motin 		while (next_periph != NULL &&
2276227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2277840a5dd4SAlexander Motin 			next_periph = SLIST_NEXT(next_periph, periph_links);
2278227d67aaSAlexander Motin 		if (next_periph)
2279227d67aaSAlexander Motin 			next_periph->refcount++;
2280227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
2281227d67aaSAlexander Motin 		xpt_unlock_buses();
2282227d67aaSAlexander Motin 		cam_periph_release_locked(periph);
2283227d67aaSAlexander Motin 	}
22848b8a9b1dSJustin T. Gibbs 	return(retval);
22858b8a9b1dSJustin T. Gibbs }
22868b8a9b1dSJustin T. Gibbs 
22878b8a9b1dSJustin T. Gibbs static int
xptpdrvtraverse(struct periph_driver ** start_pdrv,xpt_pdrvfunc_t * tr_func,void * arg)22888b8a9b1dSJustin T. Gibbs xptpdrvtraverse(struct periph_driver **start_pdrv,
22898b8a9b1dSJustin T. Gibbs 		xpt_pdrvfunc_t *tr_func, void *arg)
22908b8a9b1dSJustin T. Gibbs {
22918b8a9b1dSJustin T. Gibbs 	struct periph_driver **pdrv;
22928b8a9b1dSJustin T. Gibbs 	int retval;
22938b8a9b1dSJustin T. Gibbs 
22948b8a9b1dSJustin T. Gibbs 	retval = 1;
22958b8a9b1dSJustin T. Gibbs 
22968b8a9b1dSJustin T. Gibbs 	/*
22978b8a9b1dSJustin T. Gibbs 	 * We don't traverse the peripheral driver list like we do the
22988b8a9b1dSJustin T. Gibbs 	 * other lists, because it is a linker set, and therefore cannot be
22998b8a9b1dSJustin T. Gibbs 	 * changed during runtime.  If the peripheral driver list is ever
23008b8a9b1dSJustin T. Gibbs 	 * re-done to be something other than a linker set (i.e. it can
23018b8a9b1dSJustin T. Gibbs 	 * change while the system is running), the list traversal should
23028b8a9b1dSJustin T. Gibbs 	 * be modified to work like the other traversal functions.
23038b8a9b1dSJustin T. Gibbs 	 */
23040b7c27b9SPeter Wemm 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
23058b8a9b1dSJustin T. Gibbs 	     *pdrv != NULL; pdrv++) {
23068b8a9b1dSJustin T. Gibbs 		retval = tr_func(pdrv, arg);
23078b8a9b1dSJustin T. Gibbs 
23088b8a9b1dSJustin T. Gibbs 		if (retval == 0)
23098b8a9b1dSJustin T. Gibbs 			return(retval);
23108b8a9b1dSJustin T. Gibbs 	}
23118b8a9b1dSJustin T. Gibbs 
23128b8a9b1dSJustin T. Gibbs 	return(retval);
23138b8a9b1dSJustin T. Gibbs }
23148b8a9b1dSJustin T. Gibbs 
23158b8a9b1dSJustin T. Gibbs static int
xptpdperiphtraverse(struct periph_driver ** pdrv,struct cam_periph * start_periph,xpt_periphfunc_t * tr_func,void * arg)23168b8a9b1dSJustin T. Gibbs xptpdperiphtraverse(struct periph_driver **pdrv,
23178b8a9b1dSJustin T. Gibbs 		    struct cam_periph *start_periph,
23188b8a9b1dSJustin T. Gibbs 		    xpt_periphfunc_t *tr_func, void *arg)
23198b8a9b1dSJustin T. Gibbs {
23208b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph, *next_periph;
23218b8a9b1dSJustin T. Gibbs 	int retval;
23228b8a9b1dSJustin T. Gibbs 
23238b8a9b1dSJustin T. Gibbs 	retval = 1;
23248b8a9b1dSJustin T. Gibbs 
2325227d67aaSAlexander Motin 	if (start_periph)
2326227d67aaSAlexander Motin 		periph = start_periph;
2327227d67aaSAlexander Motin 	else {
2328f1e2546aSMatt Jacob 		xpt_lock_buses();
2329227d67aaSAlexander Motin 		periph = TAILQ_FIRST(&(*pdrv)->units);
2330227d67aaSAlexander Motin 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2331227d67aaSAlexander Motin 			periph = TAILQ_NEXT(periph, unit_links);
2332227d67aaSAlexander Motin 		if (periph == NULL) {
2333227d67aaSAlexander Motin 			xpt_unlock_buses();
2334227d67aaSAlexander Motin 			return (retval);
23358900f4b8SKenneth D. Merry 		}
23368900f4b8SKenneth D. Merry 		periph->refcount++;
2337dcdf6e74SAlexander Motin 		xpt_unlock_buses();
23388b8a9b1dSJustin T. Gibbs 	}
2339227d67aaSAlexander Motin 	for (; periph != NULL; periph = next_periph) {
2340227d67aaSAlexander Motin 		cam_periph_lock(periph);
2341227d67aaSAlexander Motin 		retval = tr_func(periph, arg);
2342227d67aaSAlexander Motin 		cam_periph_unlock(periph);
2343227d67aaSAlexander Motin 		if (retval == 0) {
2344227d67aaSAlexander Motin 			cam_periph_release(periph);
2345227d67aaSAlexander Motin 			break;
2346227d67aaSAlexander Motin 		}
2347227d67aaSAlexander Motin 		xpt_lock_buses();
2348227d67aaSAlexander Motin 		next_periph = TAILQ_NEXT(periph, unit_links);
2349227d67aaSAlexander Motin 		while (next_periph != NULL &&
2350227d67aaSAlexander Motin 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2351840a5dd4SAlexander Motin 			next_periph = TAILQ_NEXT(next_periph, unit_links);
2352227d67aaSAlexander Motin 		if (next_periph)
2353227d67aaSAlexander Motin 			next_periph->refcount++;
2354f1e2546aSMatt Jacob 		xpt_unlock_buses();
2355227d67aaSAlexander Motin 		cam_periph_release(periph);
2356227d67aaSAlexander Motin 	}
23578b8a9b1dSJustin T. Gibbs 	return(retval);
23588b8a9b1dSJustin T. Gibbs }
23598b8a9b1dSJustin T. Gibbs 
23608b8a9b1dSJustin T. Gibbs static int
xptdefbusfunc(struct cam_eb * bus,void * arg)23618b8a9b1dSJustin T. Gibbs xptdefbusfunc(struct cam_eb *bus, void *arg)
23628b8a9b1dSJustin T. Gibbs {
23638b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23648b8a9b1dSJustin T. Gibbs 
23658b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23668b8a9b1dSJustin T. Gibbs 
23678b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_BUS) {
23688b8a9b1dSJustin T. Gibbs 		xpt_busfunc_t *tr_func;
23698b8a9b1dSJustin T. Gibbs 
23708b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
23718b8a9b1dSJustin T. Gibbs 
23728b8a9b1dSJustin T. Gibbs 		return(tr_func(bus, tr_config->tr_arg));
23738b8a9b1dSJustin T. Gibbs 	} else
23748b8a9b1dSJustin T. Gibbs 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
23758b8a9b1dSJustin T. Gibbs }
23768b8a9b1dSJustin T. Gibbs 
23778b8a9b1dSJustin T. Gibbs static int
xptdeftargetfunc(struct cam_et * target,void * arg)23788b8a9b1dSJustin T. Gibbs xptdeftargetfunc(struct cam_et *target, void *arg)
23798b8a9b1dSJustin T. Gibbs {
23808b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23818b8a9b1dSJustin T. Gibbs 
23828b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
23838b8a9b1dSJustin T. Gibbs 
23848b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_TARGET) {
23858b8a9b1dSJustin T. Gibbs 		xpt_targetfunc_t *tr_func;
23868b8a9b1dSJustin T. Gibbs 
23878b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
23888b8a9b1dSJustin T. Gibbs 
23898b8a9b1dSJustin T. Gibbs 		return(tr_func(target, tr_config->tr_arg));
23908b8a9b1dSJustin T. Gibbs 	} else
23918b8a9b1dSJustin T. Gibbs 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
23928b8a9b1dSJustin T. Gibbs }
23938b8a9b1dSJustin T. Gibbs 
23948b8a9b1dSJustin T. Gibbs static int
xptdefdevicefunc(struct cam_ed * device,void * arg)23958b8a9b1dSJustin T. Gibbs xptdefdevicefunc(struct cam_ed *device, void *arg)
23968b8a9b1dSJustin T. Gibbs {
23978b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
23988b8a9b1dSJustin T. Gibbs 
23998b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24008b8a9b1dSJustin T. Gibbs 
24018b8a9b1dSJustin T. Gibbs 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
24028b8a9b1dSJustin T. Gibbs 		xpt_devicefunc_t *tr_func;
24038b8a9b1dSJustin T. Gibbs 
24048b8a9b1dSJustin T. Gibbs 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
24058b8a9b1dSJustin T. Gibbs 
24068b8a9b1dSJustin T. Gibbs 		return(tr_func(device, tr_config->tr_arg));
24078b8a9b1dSJustin T. Gibbs 	} else
24088b8a9b1dSJustin T. Gibbs 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
24098b8a9b1dSJustin T. Gibbs }
24108b8a9b1dSJustin T. Gibbs 
24118b8a9b1dSJustin T. Gibbs static int
xptdefperiphfunc(struct cam_periph * periph,void * arg)24128b8a9b1dSJustin T. Gibbs xptdefperiphfunc(struct cam_periph *periph, void *arg)
24138b8a9b1dSJustin T. Gibbs {
24148b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config *tr_config;
24158b8a9b1dSJustin T. Gibbs 	xpt_periphfunc_t *tr_func;
24168b8a9b1dSJustin T. Gibbs 
24178b8a9b1dSJustin T. Gibbs 	tr_config = (struct xpt_traverse_config *)arg;
24188b8a9b1dSJustin T. Gibbs 
24198b8a9b1dSJustin T. Gibbs 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
24208b8a9b1dSJustin T. Gibbs 
24218b8a9b1dSJustin T. Gibbs 	/*
24228b8a9b1dSJustin T. Gibbs 	 * Unlike the other default functions, we don't check for depth
24238b8a9b1dSJustin T. Gibbs 	 * here.  The peripheral driver level is the last level in the EDT,
24248b8a9b1dSJustin T. Gibbs 	 * so if we're here, we should execute the function in question.
24258b8a9b1dSJustin T. Gibbs 	 */
24268b8a9b1dSJustin T. Gibbs 	return(tr_func(periph, tr_config->tr_arg));
24278b8a9b1dSJustin T. Gibbs }
24288b8a9b1dSJustin T. Gibbs 
24298b8a9b1dSJustin T. Gibbs /*
24308b8a9b1dSJustin T. Gibbs  * Execute the given function for every bus in the EDT.
24318b8a9b1dSJustin T. Gibbs  */
24328b8a9b1dSJustin T. Gibbs static int
xpt_for_all_busses(xpt_busfunc_t * tr_func,void * arg)24338b8a9b1dSJustin T. Gibbs xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
24348b8a9b1dSJustin T. Gibbs {
24358b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24368b8a9b1dSJustin T. Gibbs 
24378b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_BUS;
24388b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24398b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24408b8a9b1dSJustin T. Gibbs 
24418b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24428b8a9b1dSJustin T. Gibbs }
24438b8a9b1dSJustin T. Gibbs 
24448b8a9b1dSJustin T. Gibbs /*
24458b8a9b1dSJustin T. Gibbs  * Execute the given function for every device in the EDT.
24468b8a9b1dSJustin T. Gibbs  */
24478b8a9b1dSJustin T. Gibbs static int
xpt_for_all_devices(xpt_devicefunc_t * tr_func,void * arg)24488b8a9b1dSJustin T. Gibbs xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
24498b8a9b1dSJustin T. Gibbs {
24508b8a9b1dSJustin T. Gibbs 	struct xpt_traverse_config tr_config;
24518b8a9b1dSJustin T. Gibbs 
24528b8a9b1dSJustin T. Gibbs 	tr_config.depth = XPT_DEPTH_DEVICE;
24538b8a9b1dSJustin T. Gibbs 	tr_config.tr_func = tr_func;
24548b8a9b1dSJustin T. Gibbs 	tr_config.tr_arg = arg;
24558b8a9b1dSJustin T. Gibbs 
24568b8a9b1dSJustin T. Gibbs 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
24578b8a9b1dSJustin T. Gibbs }
24588b8a9b1dSJustin T. Gibbs 
24598b8a9b1dSJustin T. Gibbs static int
xptsetasyncfunc(struct cam_ed * device,void * arg)24608b8a9b1dSJustin T. Gibbs xptsetasyncfunc(struct cam_ed *device, void *arg)
24618b8a9b1dSJustin T. Gibbs {
24628b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24638b8a9b1dSJustin T. Gibbs 	struct ccb_getdev cgd;
24647685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24658b8a9b1dSJustin T. Gibbs 
2466c8bead2aSJustin T. Gibbs 	/*
2467c8bead2aSJustin T. Gibbs 	 * Don't report unconfigured devices (Wildcard devs,
2468c8bead2aSJustin T. Gibbs 	 * devices only for target mode, device instances
2469c8bead2aSJustin T. Gibbs 	 * that have been invalidated but are waiting for
2470c8bead2aSJustin T. Gibbs 	 * their last reference count to be released).
2471c8bead2aSJustin T. Gibbs 	 */
2472c8bead2aSJustin T. Gibbs 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2473c8bead2aSJustin T. Gibbs 		return (1);
2474c8bead2aSJustin T. Gibbs 
2475076686feSEdward Tomasz Napierala 	memset(&cgd, 0, sizeof(cgd));
24768b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path,
24778b8a9b1dSJustin T. Gibbs 			 NULL,
24788b8a9b1dSJustin T. Gibbs 			 device->target->bus->path_id,
24798b8a9b1dSJustin T. Gibbs 			 device->target->target_id,
24808b8a9b1dSJustin T. Gibbs 			 device->lun_id);
2481bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
24828b8a9b1dSJustin T. Gibbs 	cgd.ccb_h.func_code = XPT_GDEV_TYPE;
24838b8a9b1dSJustin T. Gibbs 	xpt_action((union ccb *)&cgd);
24847685edecSAlexander Motin 	csa->callback(csa->callback_arg,
24858b8a9b1dSJustin T. Gibbs 			    AC_FOUND_DEVICE,
24868b8a9b1dSJustin T. Gibbs 			    &path, &cgd);
24878b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
24888b8a9b1dSJustin T. Gibbs 
24898b8a9b1dSJustin T. Gibbs 	return(1);
24908b8a9b1dSJustin T. Gibbs }
2491c8bead2aSJustin T. Gibbs 
24928b8a9b1dSJustin T. Gibbs static int
xptsetasyncbusfunc(struct cam_eb * bus,void * arg)24938b8a9b1dSJustin T. Gibbs xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
24948b8a9b1dSJustin T. Gibbs {
24958b8a9b1dSJustin T. Gibbs 	struct cam_path path;
24968b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
24977685edecSAlexander Motin 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
24988b8a9b1dSJustin T. Gibbs 
24998b8a9b1dSJustin T. Gibbs 	xpt_compile_path(&path, /*periph*/NULL,
25006dfc67e3SAlexander Motin 			 bus->path_id,
25018b8a9b1dSJustin T. Gibbs 			 CAM_TARGET_WILDCARD,
25028b8a9b1dSJustin T. Gibbs 			 CAM_LUN_WILDCARD);
2503227d67aaSAlexander Motin 	xpt_path_lock(&path);
2504762a7f4fSWarner Losh 	xpt_path_inq(&cpi, &path);
25057685edecSAlexander Motin 	csa->callback(csa->callback_arg,
25068b8a9b1dSJustin T. Gibbs 			    AC_PATH_REGISTERED,
25078b8a9b1dSJustin T. Gibbs 			    &path, &cpi);
2508227d67aaSAlexander Motin 	xpt_path_unlock(&path);
25098b8a9b1dSJustin T. Gibbs 	xpt_release_path(&path);
25108b8a9b1dSJustin T. Gibbs 
25118b8a9b1dSJustin T. Gibbs 	return(1);
25128b8a9b1dSJustin T. Gibbs }
25138b8a9b1dSJustin T. Gibbs 
25148b8a9b1dSJustin T. Gibbs void
xpt_action(union ccb * start_ccb)25158b8a9b1dSJustin T. Gibbs xpt_action(union ccb *start_ccb)
25168b8a9b1dSJustin T. Gibbs {
25179911ecf9SJustin T. Gibbs 
251882727824SWarner Losh 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
251969be012fSWarner Losh 	    ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
252069be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
25218b8a9b1dSJustin T. Gibbs 
25228b8a9b1dSJustin T. Gibbs 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
2523ded2b706SWarner Losh 	(*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
252452c9ce25SScott Long }
252552c9ce25SScott Long 
252652c9ce25SScott Long void
xpt_action_default(union ccb * start_ccb)252752c9ce25SScott Long xpt_action_default(union ccb *start_ccb)
252852c9ce25SScott Long {
2529da396db2SAlexander Motin 	struct cam_path *path;
2530227d67aaSAlexander Motin 	struct cam_sim *sim;
2531401ed17aSAlexander Motin 	struct mtx *mtx;
253252c9ce25SScott Long 
2533da396db2SAlexander Motin 	path = start_ccb->ccb_h.path;
253482727824SWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
253569be012fSWarner Losh 	    ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
253669be012fSWarner Losh 		xpt_action_name(start_ccb->ccb_h.func_code)));
253752c9ce25SScott Long 
25388b8a9b1dSJustin T. Gibbs 	switch (start_ccb->ccb_h.func_code) {
25398b8a9b1dSJustin T. Gibbs 	case XPT_SCSI_IO:
2540d05caa00SKenneth D. Merry 	{
25413393f8daSKenneth D. Merry 		struct cam_ed *device;
2542d05caa00SKenneth D. Merry 
25438b8a9b1dSJustin T. Gibbs 		/*
25448b8a9b1dSJustin T. Gibbs 		 * For the sake of compatibility with SCSI-1
25458b8a9b1dSJustin T. Gibbs 		 * devices that may not understand the identify
25468b8a9b1dSJustin T. Gibbs 		 * message, we include lun information in the
25478b8a9b1dSJustin T. Gibbs 		 * second byte of all commands.  SCSI-1 specifies
25488b8a9b1dSJustin T. Gibbs 		 * that luns are a 3 bit value and reserves only 3
25498b8a9b1dSJustin T. Gibbs 		 * bits for lun information in the CDB.  Later
25508b8a9b1dSJustin T. Gibbs 		 * revisions of the SCSI spec allow for more than 8
25518b8a9b1dSJustin T. Gibbs 		 * luns, but have deprecated lun information in the
25528b8a9b1dSJustin T. Gibbs 		 * CDB.  So, if the lun won't fit, we must omit.
25538b8a9b1dSJustin T. Gibbs 		 *
25548b8a9b1dSJustin T. Gibbs 		 * Also be aware that during initial probing for devices,
25558b8a9b1dSJustin T. Gibbs 		 * the inquiry information is unknown but initialized to 0.
25568b8a9b1dSJustin T. Gibbs 		 * This means that this code will be exercised while probing
25578b8a9b1dSJustin T. Gibbs 		 * devices with an ANSI revision greater than 2.
25588b8a9b1dSJustin T. Gibbs 		 */
2559da396db2SAlexander Motin 		device = path->device;
25603393f8daSKenneth D. Merry 		if (device->protocol_version <= SCSI_REV_2
25618b8a9b1dSJustin T. Gibbs 		 && start_ccb->ccb_h.target_lun < 8
25628b8a9b1dSJustin T. Gibbs 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
25638b8a9b1dSJustin T. Gibbs 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
25648b8a9b1dSJustin T. Gibbs 			    start_ccb->ccb_h.target_lun << 5;
25658b8a9b1dSJustin T. Gibbs 		}
25668b8a9b1dSJustin T. Gibbs 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2567d05caa00SKenneth D. Merry 	}
256807c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
25698b8a9b1dSJustin T. Gibbs 	case XPT_TARGET_IO:
25708b8a9b1dSJustin T. Gibbs 	case XPT_CONT_TARGET_IO:
25712cefde5fSJustin T. Gibbs 		start_ccb->csio.sense_resid = 0;
25722cefde5fSJustin T. Gibbs 		start_ccb->csio.resid = 0;
25732cefde5fSJustin T. Gibbs 		/* FALLTHROUGH */
257452c9ce25SScott Long 	case XPT_ATA_IO:
2575de9ebb68SAlexander Motin 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
257652c9ce25SScott Long 			start_ccb->ataio.resid = 0;
2577b882a6d3SMatt Jacob 		/* FALLTHROUGH */
2578b24ced67SWarner Losh 	case XPT_NVME_IO:
2579df424515SWarner Losh 	case XPT_NVME_ADMIN:
2580a94a63f0SWarner Losh 	case XPT_MMC_IO:
2581af2253f6SEmmanuel Vadot 	case XPT_MMC_GET_TRAN_SETTINGS:
2582af2253f6SEmmanuel Vadot 	case XPT_MMC_SET_TRAN_SETTINGS:
258310e1cf63SKenneth D. Merry 	case XPT_RESET_DEV:
25848b8a9b1dSJustin T. Gibbs 	case XPT_ENG_EXEC:
258506e79492SKenneth D. Merry 	case XPT_SMP_IO:
25862cefde5fSJustin T. Gibbs 	{
2587227d67aaSAlexander Motin 		struct cam_devq *devq;
25882cefde5fSJustin T. Gibbs 
2589227d67aaSAlexander Motin 		devq = path->bus->sim->devq;
2590227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
2591227d67aaSAlexander Motin 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2592227d67aaSAlexander Motin 		if (xpt_schedule_devq(devq, path->device) != 0)
2593227d67aaSAlexander Motin 			xpt_run_devq(devq);
2594227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
2595227d67aaSAlexander Motin 		break;
2596227d67aaSAlexander Motin 	}
2597227d67aaSAlexander Motin 	case XPT_CALC_GEOMETRY:
25988b8a9b1dSJustin T. Gibbs 		/* Filter out garbage */
25998b8a9b1dSJustin T. Gibbs 		if (start_ccb->ccg.block_size == 0
26008b8a9b1dSJustin T. Gibbs 		 || start_ccb->ccg.volume_size == 0) {
26018b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.cylinders = 0;
26028b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.heads = 0;
26038b8a9b1dSJustin T. Gibbs 			start_ccb->ccg.secs_per_track = 0;
26048b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
26058b8a9b1dSJustin T. Gibbs 			break;
26068b8a9b1dSJustin T. Gibbs 		}
2607227d67aaSAlexander Motin 		goto call_sim;
2608bb6087e5SJustin T. Gibbs 	case XPT_ABORT:
26092cefde5fSJustin T. Gibbs 	{
26102cefde5fSJustin T. Gibbs 		union ccb* abort_ccb;
26112cefde5fSJustin T. Gibbs 
26122cefde5fSJustin T. Gibbs 		abort_ccb = start_ccb->cab.abort_ccb;
26132cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
261483c5d981SAlexander Motin 			struct cam_ed *device;
2615dda945f5SMark Johnston 			struct cam_devq *devq;
26162cefde5fSJustin T. Gibbs 
261783c5d981SAlexander Motin 			device = abort_ccb->ccb_h.path->device;
2618dda945f5SMark Johnston 			devq = device->sim->devq;
2619dda945f5SMark Johnston 
2620dda945f5SMark Johnston 			mtx_lock(&devq->send_mtx);
2621dda945f5SMark Johnston 			if (abort_ccb->ccb_h.pinfo.index > 0) {
2622dda945f5SMark Johnston 				cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
26232cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
26242cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2625dda945f5SMark Johnston 				xpt_freeze_devq_device(device, 1);
2626dda945f5SMark Johnston 				mtx_unlock(&devq->send_mtx);
26272cefde5fSJustin T. Gibbs 				xpt_done(abort_ccb);
26282cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
26292cefde5fSJustin T. Gibbs 				break;
26302cefde5fSJustin T. Gibbs 			}
2631dda945f5SMark Johnston 			mtx_unlock(&devq->send_mtx);
2632dda945f5SMark Johnston 
26332cefde5fSJustin T. Gibbs 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
26342cefde5fSJustin T. Gibbs 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
26352cefde5fSJustin T. Gibbs 				/*
26362cefde5fSJustin T. Gibbs 				 * We've caught this ccb en route to
26372cefde5fSJustin T. Gibbs 				 * the SIM.  Flag it for abort and the
26382cefde5fSJustin T. Gibbs 				 * SIM will do so just before starting
26392cefde5fSJustin T. Gibbs 				 * real work on the CCB.
26402cefde5fSJustin T. Gibbs 				 */
26412cefde5fSJustin T. Gibbs 				abort_ccb->ccb_h.status =
26422cefde5fSJustin T. Gibbs 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
26432cefde5fSJustin T. Gibbs 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
26442cefde5fSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
26452cefde5fSJustin T. Gibbs 				break;
26462cefde5fSJustin T. Gibbs 			}
26472cefde5fSJustin T. Gibbs 		}
26482cefde5fSJustin T. Gibbs 		if (XPT_FC_IS_QUEUED(abort_ccb)
26492cefde5fSJustin T. Gibbs 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
26502cefde5fSJustin T. Gibbs 			/*
26512cefde5fSJustin T. Gibbs 			 * It's already completed but waiting
26522cefde5fSJustin T. Gibbs 			 * for our SWI to get to it.
26532cefde5fSJustin T. Gibbs 			 */
26542cefde5fSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_UA_ABORT;
26552cefde5fSJustin T. Gibbs 			break;
26562cefde5fSJustin T. Gibbs 		}
26572cefde5fSJustin T. Gibbs 		/*
26582cefde5fSJustin T. Gibbs 		 * If we weren't able to take care of the abort request
26592cefde5fSJustin T. Gibbs 		 * in the XPT, pass the request down to the SIM for processing.
26602cefde5fSJustin T. Gibbs 		 */
26612cefde5fSJustin T. Gibbs 	}
266207c6eac9SPoul-Henning Kamp 	/* FALLTHROUGH */
26638b8a9b1dSJustin T. Gibbs 	case XPT_ACCEPT_TARGET_IO:
26648b8a9b1dSJustin T. Gibbs 	case XPT_EN_LUN:
26658b8a9b1dSJustin T. Gibbs 	case XPT_IMMED_NOTIFY:
26668b8a9b1dSJustin T. Gibbs 	case XPT_NOTIFY_ACK:
26678b8a9b1dSJustin T. Gibbs 	case XPT_RESET_BUS:
26682df76c16SMatt Jacob 	case XPT_IMMEDIATE_NOTIFY:
26692df76c16SMatt Jacob 	case XPT_NOTIFY_ACKNOWLEDGE:
2670a2531862SWarner Losh 	case XPT_GET_SIM_KNOB_OLD:
26712df76c16SMatt Jacob 	case XPT_GET_SIM_KNOB:
26722df76c16SMatt Jacob 	case XPT_SET_SIM_KNOB:
2673227d67aaSAlexander Motin 	case XPT_GET_TRAN_SETTINGS:
2674227d67aaSAlexander Motin 	case XPT_SET_TRAN_SETTINGS:
267587cfaf0eSJustin T. Gibbs 	case XPT_PATH_INQ:
2676227d67aaSAlexander Motin call_sim:
2677da396db2SAlexander Motin 		sim = path->bus->sim;
2678401ed17aSAlexander Motin 		mtx = sim->mtx;
2679401ed17aSAlexander Motin 		if (mtx && !mtx_owned(mtx))
2680401ed17aSAlexander Motin 			mtx_lock(mtx);
2681401ed17aSAlexander Motin 		else
2682401ed17aSAlexander Motin 			mtx = NULL;
2683a94a63f0SWarner Losh 
268482727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2685a94a63f0SWarner Losh 		    ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
268687cfaf0eSJustin T. Gibbs 		(*(sim->sim_action))(sim, start_ccb);
268782727824SWarner Losh 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2688a94a63f0SWarner Losh 		    ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2689401ed17aSAlexander Motin 		if (mtx)
2690401ed17aSAlexander Motin 			mtx_unlock(mtx);
269187cfaf0eSJustin T. Gibbs 		break;
269287cfaf0eSJustin T. Gibbs 	case XPT_PATH_STATS:
2693da396db2SAlexander Motin 		start_ccb->cpis.last_reset = path->bus->last_reset;
269487cfaf0eSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
269587cfaf0eSJustin T. Gibbs 		break;
26968b8a9b1dSJustin T. Gibbs 	case XPT_GDEV_TYPE:
2697a5479bc5SJustin T. Gibbs 	{
269887cfaf0eSJustin T. Gibbs 		struct cam_ed *dev;
2699a5479bc5SJustin T. Gibbs 
2700da396db2SAlexander Motin 		dev = path->device;
270187cfaf0eSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
27028b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
27038b8a9b1dSJustin T. Gibbs 		} else {
27048b8a9b1dSJustin T. Gibbs 			struct ccb_getdev *cgd;
27058b8a9b1dSJustin T. Gibbs 
27068b8a9b1dSJustin T. Gibbs 			cgd = &start_ccb->cgd;
270752c9ce25SScott Long 			cgd->protocol = dev->protocol;
27088b8a9b1dSJustin T. Gibbs 			cgd->inq_data = dev->inq_data;
270952c9ce25SScott Long 			cgd->ident_data = dev->ident_data;
271030a4094fSAlexander Motin 			cgd->inq_flags = dev->inq_flags;
27118b8a9b1dSJustin T. Gibbs 			cgd->ccb_h.status = CAM_REQ_CMP;
27128b8a9b1dSJustin T. Gibbs 			cgd->serial_num_len = dev->serial_num_len;
27138b8a9b1dSJustin T. Gibbs 			if ((dev->serial_num_len > 0)
27148b8a9b1dSJustin T. Gibbs 			 && (dev->serial_num != NULL))
27158b8a9b1dSJustin T. Gibbs 				bcopy(dev->serial_num, cgd->serial_num,
27168b8a9b1dSJustin T. Gibbs 				      dev->serial_num_len);
27178b8a9b1dSJustin T. Gibbs 		}
27188b8a9b1dSJustin T. Gibbs 		break;
2719a5479bc5SJustin T. Gibbs 	}
272087cfaf0eSJustin T. Gibbs 	case XPT_GDEV_STATS:
272187cfaf0eSJustin T. Gibbs 	{
27222ef6e7aeSAlexander Motin 		struct ccb_getdevstats *cgds = &start_ccb->cgds;
27232ef6e7aeSAlexander Motin 		struct cam_ed *dev = path->device;
27242ef6e7aeSAlexander Motin 		struct cam_eb *bus = path->bus;
27252ef6e7aeSAlexander Motin 		struct cam_et *tar = path->target;
27262ef6e7aeSAlexander Motin 		struct cam_devq *devq = bus->sim->devq;
272787cfaf0eSJustin T. Gibbs 
2728959ec258SAlexander Motin 		mtx_lock(&devq->send_mtx);
272987cfaf0eSJustin T. Gibbs 		cgds->dev_openings = dev->ccbq.dev_openings;
273087cfaf0eSJustin T. Gibbs 		cgds->dev_active = dev->ccbq.dev_active;
2731959ec258SAlexander Motin 		cgds->allocated = dev->ccbq.allocated;
2732959ec258SAlexander Motin 		cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
27332ef6e7aeSAlexander Motin 		cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
273487cfaf0eSJustin T. Gibbs 		cgds->last_reset = tar->last_reset;
273552c9ce25SScott Long 		cgds->maxtags = dev->maxtags;
273652c9ce25SScott Long 		cgds->mintags = dev->mintags;
273787cfaf0eSJustin T. Gibbs 		if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
273887cfaf0eSJustin T. Gibbs 			cgds->last_reset = bus->last_reset;
2739959ec258SAlexander Motin 		mtx_unlock(&devq->send_mtx);
274087cfaf0eSJustin T. Gibbs 		cgds->ccb_h.status = CAM_REQ_CMP;
274187cfaf0eSJustin T. Gibbs 		break;
274287cfaf0eSJustin T. Gibbs 	}
27438b8a9b1dSJustin T. Gibbs 	case XPT_GDEVLIST:
27448b8a9b1dSJustin T. Gibbs 	{
27458b8a9b1dSJustin T. Gibbs 		struct cam_periph	*nperiph;
27468b8a9b1dSJustin T. Gibbs 		struct periph_list	*periph_head;
27478b8a9b1dSJustin T. Gibbs 		struct ccb_getdevlist	*cgdl;
27483393f8daSKenneth D. Merry 		u_int			i;
27498b8a9b1dSJustin T. Gibbs 		struct cam_ed		*device;
2750d095d6a3SWarner Losh 		bool			found;
27518b8a9b1dSJustin T. Gibbs 
2752d095d6a3SWarner Losh 		found = false;
27538b8a9b1dSJustin T. Gibbs 
27548b8a9b1dSJustin T. Gibbs 		/*
27558b8a9b1dSJustin T. Gibbs 		 * Don't want anyone mucking with our data.
27568b8a9b1dSJustin T. Gibbs 		 */
2757da396db2SAlexander Motin 		device = path->device;
27588b8a9b1dSJustin T. Gibbs 		periph_head = &device->periphs;
27598b8a9b1dSJustin T. Gibbs 		cgdl = &start_ccb->cgdl;
27608b8a9b1dSJustin T. Gibbs 
27618b8a9b1dSJustin T. Gibbs 		/*
27628b8a9b1dSJustin T. Gibbs 		 * Check and see if the list has changed since the user
27638b8a9b1dSJustin T. Gibbs 		 * last requested a list member.  If so, tell them that the
27648b8a9b1dSJustin T. Gibbs 		 * list has changed, and therefore they need to start over
27658b8a9b1dSJustin T. Gibbs 		 * from the beginning.
27668b8a9b1dSJustin T. Gibbs 		 */
27678b8a9b1dSJustin T. Gibbs 		if ((cgdl->index != 0) &&
27688b8a9b1dSJustin T. Gibbs 		    (cgdl->generation != device->generation)) {
27698b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
27708b8a9b1dSJustin T. Gibbs 			break;
27718b8a9b1dSJustin T. Gibbs 		}
27728b8a9b1dSJustin T. Gibbs 
27738b8a9b1dSJustin T. Gibbs 		/*
27748b8a9b1dSJustin T. Gibbs 		 * Traverse the list of peripherals and attempt to find
27758b8a9b1dSJustin T. Gibbs 		 * the requested peripheral.
27768b8a9b1dSJustin T. Gibbs 		 */
2777fc2ffbe6SPoul-Henning Kamp 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
27788b8a9b1dSJustin T. Gibbs 		     (nperiph != NULL) && (i <= cgdl->index);
2779fc2ffbe6SPoul-Henning Kamp 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
27808b8a9b1dSJustin T. Gibbs 			if (i == cgdl->index) {
2781b0f662feSAlan Somers 				strlcpy(cgdl->periph_name,
27828b8a9b1dSJustin T. Gibbs 					nperiph->periph_name,
2783b0f662feSAlan Somers 					sizeof(cgdl->periph_name));
27848b8a9b1dSJustin T. Gibbs 				cgdl->unit_number = nperiph->unit_number;
2785d095d6a3SWarner Losh 				found = true;
27868b8a9b1dSJustin T. Gibbs 			}
27878b8a9b1dSJustin T. Gibbs 		}
2788d095d6a3SWarner Losh 		if (!found) {
27898b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_ERROR;
27908b8a9b1dSJustin T. Gibbs 			break;
27918b8a9b1dSJustin T. Gibbs 		}
27928b8a9b1dSJustin T. Gibbs 
27938b8a9b1dSJustin T. Gibbs 		if (nperiph == NULL)
27948b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
27958b8a9b1dSJustin T. Gibbs 		else
27968b8a9b1dSJustin T. Gibbs 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
27978b8a9b1dSJustin T. Gibbs 
27988b8a9b1dSJustin T. Gibbs 		cgdl->index++;
27998b8a9b1dSJustin T. Gibbs 		cgdl->generation = device->generation;
28008b8a9b1dSJustin T. Gibbs 
28018b8a9b1dSJustin T. Gibbs 		cgdl->ccb_h.status = CAM_REQ_CMP;
28028b8a9b1dSJustin T. Gibbs 		break;
28038b8a9b1dSJustin T. Gibbs 	}
28048b8a9b1dSJustin T. Gibbs 	case XPT_DEV_MATCH:
28058b8a9b1dSJustin T. Gibbs 	{
28068b8a9b1dSJustin T. Gibbs 		dev_pos_type position_type;
28078b8a9b1dSJustin T. Gibbs 		struct ccb_dev_match *cdm;
28088b8a9b1dSJustin T. Gibbs 
28098b8a9b1dSJustin T. Gibbs 		cdm = &start_ccb->cdm;
28108b8a9b1dSJustin T. Gibbs 
28118b8a9b1dSJustin T. Gibbs 		/*
28128b8a9b1dSJustin T. Gibbs 		 * There are two ways of getting at information in the EDT.
28138b8a9b1dSJustin T. Gibbs 		 * The first way is via the primary EDT tree.  It starts
2814db4fcadfSConrad Meyer 		 * with a list of buses, then a list of targets on a bus,
28158b8a9b1dSJustin T. Gibbs 		 * then devices/luns on a target, and then peripherals on a
28168b8a9b1dSJustin T. Gibbs 		 * device/lun.  The "other" way is by the peripheral driver
28178b8a9b1dSJustin T. Gibbs 		 * lists.  The peripheral driver lists are organized by
28188b8a9b1dSJustin T. Gibbs 		 * peripheral driver.  (obviously)  So it makes sense to
28198b8a9b1dSJustin T. Gibbs 		 * use the peripheral driver list if the user is looking
28208b8a9b1dSJustin T. Gibbs 		 * for something like "da1", or all "da" devices.  If the
28218b8a9b1dSJustin T. Gibbs 		 * user is looking for something on a particular bus/target
28228b8a9b1dSJustin T. Gibbs 		 * or lun, it's generally better to go through the EDT tree.
28238b8a9b1dSJustin T. Gibbs 		 */
28248b8a9b1dSJustin T. Gibbs 
28258b8a9b1dSJustin T. Gibbs 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
28268b8a9b1dSJustin T. Gibbs 			position_type = cdm->pos.position_type;
28278b8a9b1dSJustin T. Gibbs 		else {
28283393f8daSKenneth D. Merry 			u_int i;
28298b8a9b1dSJustin T. Gibbs 
28308b8a9b1dSJustin T. Gibbs 			position_type = CAM_DEV_POS_NONE;
28318b8a9b1dSJustin T. Gibbs 
28328b8a9b1dSJustin T. Gibbs 			for (i = 0; i < cdm->num_patterns; i++) {
28338b8a9b1dSJustin T. Gibbs 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
28348b8a9b1dSJustin T. Gibbs 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
28358b8a9b1dSJustin T. Gibbs 					position_type = CAM_DEV_POS_EDT;
28368b8a9b1dSJustin T. Gibbs 					break;
28378b8a9b1dSJustin T. Gibbs 				}
28388b8a9b1dSJustin T. Gibbs 			}
28398b8a9b1dSJustin T. Gibbs 
28408b8a9b1dSJustin T. Gibbs 			if (cdm->num_patterns == 0)
28418b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_EDT;
28428b8a9b1dSJustin T. Gibbs 			else if (position_type == CAM_DEV_POS_NONE)
28438b8a9b1dSJustin T. Gibbs 				position_type = CAM_DEV_POS_PDRV;
28448b8a9b1dSJustin T. Gibbs 		}
28458b8a9b1dSJustin T. Gibbs 
28468b8a9b1dSJustin T. Gibbs 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
28478b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_EDT:
284807c6eac9SPoul-Henning Kamp 			xptedtmatch(cdm);
28498b8a9b1dSJustin T. Gibbs 			break;
28508b8a9b1dSJustin T. Gibbs 		case CAM_DEV_POS_PDRV:
285107c6eac9SPoul-Henning Kamp 			xptperiphlistmatch(cdm);
28528b8a9b1dSJustin T. Gibbs 			break;
28538b8a9b1dSJustin T. Gibbs 		default:
28548b8a9b1dSJustin T. Gibbs 			cdm->status = CAM_DEV_MATCH_ERROR;
28558b8a9b1dSJustin T. Gibbs 			break;
28568b8a9b1dSJustin T. Gibbs 		}
28578b8a9b1dSJustin T. Gibbs 
28588b8a9b1dSJustin T. Gibbs 		if (cdm->status == CAM_DEV_MATCH_ERROR)
28598b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
28608b8a9b1dSJustin T. Gibbs 		else
28618b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
28628b8a9b1dSJustin T. Gibbs 
28638b8a9b1dSJustin T. Gibbs 		break;
28648b8a9b1dSJustin T. Gibbs 	}
28658b8a9b1dSJustin T. Gibbs 	case XPT_SASYNC_CB:
28668b8a9b1dSJustin T. Gibbs 	{
286784f82481SScott Long 		struct ccb_setasync *csa;
286884f82481SScott Long 		struct async_node *cur_entry;
286984f82481SScott Long 		struct async_list *async_head;
28707af2f2c8SWarner Losh 		uint32_t added;
287184f82481SScott Long 
287284f82481SScott Long 		csa = &start_ccb->csa;
287384f82481SScott Long 		added = csa->event_enable;
2874da396db2SAlexander Motin 		async_head = &path->device->asyncs;
287584f82481SScott Long 
287684f82481SScott Long 		/*
287784f82481SScott Long 		 * If there is already an entry for us, simply
287884f82481SScott Long 		 * update it.
287984f82481SScott Long 		 */
288084f82481SScott Long 		cur_entry = SLIST_FIRST(async_head);
288184f82481SScott Long 		while (cur_entry != NULL) {
288284f82481SScott Long 			if ((cur_entry->callback_arg == csa->callback_arg)
288384f82481SScott Long 			 && (cur_entry->callback == csa->callback))
288484f82481SScott Long 				break;
288584f82481SScott Long 			cur_entry = SLIST_NEXT(cur_entry, links);
288684f82481SScott Long 		}
288784f82481SScott Long 
288884f82481SScott Long 		if (cur_entry != NULL) {
288984f82481SScott Long 		 	/*
289084f82481SScott Long 			 * If the request has no flags set,
289184f82481SScott Long 			 * remove the entry.
289284f82481SScott Long 			 */
289384f82481SScott Long 			added &= ~cur_entry->event_enable;
289484f82481SScott Long 			if (csa->event_enable == 0) {
289584f82481SScott Long 				SLIST_REMOVE(async_head, cur_entry,
289684f82481SScott Long 					     async_node, links);
2897da396db2SAlexander Motin 				xpt_release_device(path->device);
289884f82481SScott Long 				free(cur_entry, M_CAMXPT);
289984f82481SScott Long 			} else {
290084f82481SScott Long 				cur_entry->event_enable = csa->event_enable;
290184f82481SScott Long 			}
29027685edecSAlexander Motin 			csa->event_enable = added;
290384f82481SScott Long 		} else {
290484f82481SScott Long 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
290584f82481SScott Long 					   M_NOWAIT);
290684f82481SScott Long 			if (cur_entry == NULL) {
290784f82481SScott Long 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
290884f82481SScott Long 				break;
290984f82481SScott Long 			}
291084f82481SScott Long 			cur_entry->event_enable = csa->event_enable;
2911401ed17aSAlexander Motin 			cur_entry->event_lock = (path->bus->sim->mtx &&
2912401ed17aSAlexander Motin 			    mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
291384f82481SScott Long 			cur_entry->callback_arg = csa->callback_arg;
291484f82481SScott Long 			cur_entry->callback = csa->callback;
291584f82481SScott Long 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2916da396db2SAlexander Motin 			xpt_acquire_device(path->device);
291784f82481SScott Long 		}
29188b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29198b8a9b1dSJustin T. Gibbs 		break;
29208b8a9b1dSJustin T. Gibbs 	}
29218b8a9b1dSJustin T. Gibbs 	case XPT_REL_SIMQ:
29228b8a9b1dSJustin T. Gibbs 	{
29238b8a9b1dSJustin T. Gibbs 		struct ccb_relsim *crs;
29248b8a9b1dSJustin T. Gibbs 		struct cam_ed *dev;
29258b8a9b1dSJustin T. Gibbs 
29268b8a9b1dSJustin T. Gibbs 		crs = &start_ccb->crs;
2927da396db2SAlexander Motin 		dev = path->device;
29288b8a9b1dSJustin T. Gibbs 		if (dev == NULL) {
29298b8a9b1dSJustin T. Gibbs 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
29308b8a9b1dSJustin T. Gibbs 			break;
29318b8a9b1dSJustin T. Gibbs 		}
29328b8a9b1dSJustin T. Gibbs 
29338b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
29348b8a9b1dSJustin T. Gibbs 			/* Don't ever go below one opening */
29358b8a9b1dSJustin T. Gibbs 			if (crs->openings > 0) {
29367dc3213dSAlexander Motin 				xpt_dev_ccbq_resize(path, crs->openings);
293779ccc199SJordan K. Hubbard 				if (bootverbose) {
2938da396db2SAlexander Motin 					xpt_print(path,
29397dc3213dSAlexander Motin 					    "number of openings is now %d\n",
29408b8a9b1dSJustin T. Gibbs 					    crs->openings);
29418b8a9b1dSJustin T. Gibbs 				}
29428b8a9b1dSJustin T. Gibbs 			}
29438b8a9b1dSJustin T. Gibbs 		}
29448b8a9b1dSJustin T. Gibbs 
2945227d67aaSAlexander Motin 		mtx_lock(&dev->sim->devq->send_mtx);
29468b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
29478b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
29488b8a9b1dSJustin T. Gibbs 				/*
29498b8a9b1dSJustin T. Gibbs 				 * Just extend the old timeout and decrement
29508b8a9b1dSJustin T. Gibbs 				 * the freeze count so that a single timeout
29518b8a9b1dSJustin T. Gibbs 				 * is sufficient for releasing the queue.
29528b8a9b1dSJustin T. Gibbs 				 */
29538b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29542b83592fSScott Long 				callout_stop(&dev->callout);
29558b8a9b1dSJustin T. Gibbs 			} else {
29568b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29578b8a9b1dSJustin T. Gibbs 			}
29588b8a9b1dSJustin T. Gibbs 
295985c9dd9dSSteven Hartland 			callout_reset_sbt(&dev->callout,
29600e5c50bfSAlexander Motin 			    SBT_1MS * crs->release_timeout, SBT_1MS,
296185c9dd9dSSteven Hartland 			    xpt_release_devq_timeout, dev, 0);
29628b8a9b1dSJustin T. Gibbs 
29638b8a9b1dSJustin T. Gibbs 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
29648b8a9b1dSJustin T. Gibbs 		}
29658b8a9b1dSJustin T. Gibbs 
29668b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
29678b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
29688b8a9b1dSJustin T. Gibbs 				/*
29698b8a9b1dSJustin T. Gibbs 				 * Decrement the freeze count so that a single
29708b8a9b1dSJustin T. Gibbs 				 * completion is still sufficient to unfreeze
29718b8a9b1dSJustin T. Gibbs 				 * the queue.
29728b8a9b1dSJustin T. Gibbs 				 */
29738b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29748b8a9b1dSJustin T. Gibbs 			} else {
29758b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
29768b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29778b8a9b1dSJustin T. Gibbs 			}
29788b8a9b1dSJustin T. Gibbs 		}
29798b8a9b1dSJustin T. Gibbs 
29808b8a9b1dSJustin T. Gibbs 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
29818b8a9b1dSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
29828b8a9b1dSJustin T. Gibbs 			 || (dev->ccbq.dev_active == 0)) {
29838b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
29848b8a9b1dSJustin T. Gibbs 			} else {
29858b8a9b1dSJustin T. Gibbs 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
29868b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
29878b8a9b1dSJustin T. Gibbs 			}
29888b8a9b1dSJustin T. Gibbs 		}
2989227d67aaSAlexander Motin 		mtx_unlock(&dev->sim->devq->send_mtx);
29908b8a9b1dSJustin T. Gibbs 
2991cccf4220SAlexander Motin 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
2992cccf4220SAlexander Motin 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
2993cccf4220SAlexander Motin 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
29948b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
29958b8a9b1dSJustin T. Gibbs 		break;
29968b8a9b1dSJustin T. Gibbs 	}
29978b8a9b1dSJustin T. Gibbs 	case XPT_DEBUG: {
299880d6987cSAlexander Motin 		struct cam_path *oldpath;
299980d6987cSAlexander Motin 
3000f0f25b9cSAlexander Motin 		/* Check that all request bits are supported. */
300122c7d606SAlexander Motin 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3002f0f25b9cSAlexander Motin 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3003f0f25b9cSAlexander Motin 			break;
3004f0f25b9cSAlexander Motin 		}
3005f0f25b9cSAlexander Motin 
300680d6987cSAlexander Motin 		cam_dflags = CAM_DEBUG_NONE;
30078b8a9b1dSJustin T. Gibbs 		if (cam_dpath != NULL) {
300880d6987cSAlexander Motin 			oldpath = cam_dpath;
30098b8a9b1dSJustin T. Gibbs 			cam_dpath = NULL;
301080d6987cSAlexander Motin 			xpt_free_path(oldpath);
30118b8a9b1dSJustin T. Gibbs 		}
301280d6987cSAlexander Motin 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3013e5dfa058SAlexander Motin 			if (xpt_create_path(&cam_dpath, NULL,
30148b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.path_id,
30158b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_id,
30168b8a9b1dSJustin T. Gibbs 					    start_ccb->ccb_h.target_lun) !=
30178b8a9b1dSJustin T. Gibbs 					    CAM_REQ_CMP) {
30188b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3019aa872be6SMatt Jacob 			} else {
302080d6987cSAlexander Motin 				cam_dflags = start_ccb->cdbg.flags;
30218b8a9b1dSJustin T. Gibbs 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3022f0d9af51SMatt Jacob 				xpt_print(cam_dpath, "debugging flags now %x\n",
3023f0d9af51SMatt Jacob 				    cam_dflags);
3024aa872be6SMatt Jacob 			}
302580d6987cSAlexander Motin 		} else
30268b8a9b1dSJustin T. Gibbs 			start_ccb->ccb_h.status = CAM_REQ_CMP;
30278b8a9b1dSJustin T. Gibbs 		break;
30288b8a9b1dSJustin T. Gibbs 	}
30298b8a9b1dSJustin T. Gibbs 	case XPT_NOOP:
303087cfaf0eSJustin T. Gibbs 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3031da396db2SAlexander Motin 			xpt_freeze_devq(path, 1);
30328b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30338b8a9b1dSJustin T. Gibbs 		break;
3034d68fae58SEdward Tomasz Napierala 	case XPT_REPROBE_LUN:
3035d68fae58SEdward Tomasz Napierala 		xpt_async(AC_INQ_CHANGED, path, NULL);
3036d68fae58SEdward Tomasz Napierala 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3037d68fae58SEdward Tomasz Napierala 		xpt_done(start_ccb);
3038d68fae58SEdward Tomasz Napierala 		break;
303956eccd2dSWarner Losh 	case XPT_ASYNC:
30407381bbeeSWarner Losh 		/*
30417381bbeeSWarner Losh 		 * Queue the async operation so it can be run from a sleepable
30427381bbeeSWarner Losh 		 * context.
30437381bbeeSWarner Losh 		 */
304456eccd2dSWarner Losh 		start_ccb->ccb_h.status = CAM_REQ_CMP;
30457381bbeeSWarner Losh 		mtx_lock(&cam_async.cam_doneq_mtx);
30467381bbeeSWarner Losh 		STAILQ_INSERT_TAIL(&cam_async.cam_doneq, &start_ccb->ccb_h, sim_links.stqe);
30477381bbeeSWarner Losh 		start_ccb->ccb_h.pinfo.index = CAM_ASYNC_INDEX;
30487381bbeeSWarner Losh 		mtx_unlock(&cam_async.cam_doneq_mtx);
30497381bbeeSWarner Losh 		wakeup(&cam_async.cam_doneq);
305056eccd2dSWarner Losh 		break;
30518b8a9b1dSJustin T. Gibbs 	default:
30528b8a9b1dSJustin T. Gibbs 	case XPT_SDEV_TYPE:
30538b8a9b1dSJustin T. Gibbs 	case XPT_TERM_IO:
30548b8a9b1dSJustin T. Gibbs 	case XPT_ENG_INQ:
30558b8a9b1dSJustin T. Gibbs 		/* XXX Implement */
3056bf1a8895SScott Long 		xpt_print(start_ccb->ccb_h.path,
3057bf1a8895SScott Long 		    "%s: CCB type %#x %s not supported\n", __func__,
3058b24ced67SWarner Losh 		    start_ccb->ccb_h.func_code,
3059b24ced67SWarner Losh 		    xpt_action_name(start_ccb->ccb_h.func_code));
30608b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3061b882a6d3SMatt Jacob 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3062b882a6d3SMatt Jacob 			xpt_done(start_ccb);
3063b882a6d3SMatt Jacob 		}
30648b8a9b1dSJustin T. Gibbs 		break;
30658b8a9b1dSJustin T. Gibbs 	}
306669be012fSWarner Losh 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
306769be012fSWarner Losh 	    ("xpt_action_default: func= %#x %s status %#x\n",
306869be012fSWarner Losh 		start_ccb->ccb_h.func_code,
306969be012fSWarner Losh  		xpt_action_name(start_ccb->ccb_h.func_code),
307069be012fSWarner Losh 		start_ccb->ccb_h.status));
30718b8a9b1dSJustin T. Gibbs }
30728b8a9b1dSJustin T. Gibbs 
30730cc28e3cSWarner Losh /*
30740cc28e3cSWarner Losh  * Call the sim poll routine to allow the sim to complete
30750cc28e3cSWarner Losh  * any inflight requests, then call camisr_runqueue to
30760cc28e3cSWarner Losh  * complete any CCB that the polling completed.
30770cc28e3cSWarner Losh  */
30780cc28e3cSWarner Losh void
xpt_sim_poll(struct cam_sim * sim)30790cc28e3cSWarner Losh xpt_sim_poll(struct cam_sim *sim)
30800cc28e3cSWarner Losh {
30810cc28e3cSWarner Losh 	struct mtx *mtx;
30820cc28e3cSWarner Losh 
3083447b3557SJohn Baldwin 	KASSERT(cam_sim_pollable(sim), ("%s: non-pollable sim", __func__));
30840cc28e3cSWarner Losh 	mtx = sim->mtx;
30850cc28e3cSWarner Losh 	if (mtx)
30860cc28e3cSWarner Losh 		mtx_lock(mtx);
30870cc28e3cSWarner Losh 	(*(sim->sim_poll))(sim);
30880cc28e3cSWarner Losh 	if (mtx)
30890cc28e3cSWarner Losh 		mtx_unlock(mtx);
30900cc28e3cSWarner Losh 	camisr_runqueue();
30910cc28e3cSWarner Losh }
30920cc28e3cSWarner Losh 
3093f93a843cSWarner Losh uint32_t
xpt_poll_setup(union ccb * start_ccb)3094f93a843cSWarner Losh xpt_poll_setup(union ccb *start_ccb)
30958b8a9b1dSJustin T. Gibbs {
30967af2f2c8SWarner Losh 	uint32_t timeout;
30978b8a9b1dSJustin T. Gibbs 	struct	  cam_sim *sim;
30988b8a9b1dSJustin T. Gibbs 	struct	  cam_devq *devq;
30998b8a9b1dSJustin T. Gibbs 	struct	  cam_ed *dev;
31008b8a9b1dSJustin T. Gibbs 
31010069293bSAlexander Motin 	timeout = start_ccb->ccb_h.timeout * 10;
31028b8a9b1dSJustin T. Gibbs 	sim = start_ccb->ccb_h.path->bus->sim;
31038b8a9b1dSJustin T. Gibbs 	devq = sim->devq;
31048b8a9b1dSJustin T. Gibbs 	dev = start_ccb->ccb_h.path->device;
31058b8a9b1dSJustin T. Gibbs 
3106447b3557SJohn Baldwin 	KASSERT(cam_sim_pollable(sim), ("%s: non-pollable sim", __func__));
3107447b3557SJohn Baldwin 
31088b8a9b1dSJustin T. Gibbs 	/*
31098b8a9b1dSJustin T. Gibbs 	 * Steal an opening so that no other queued requests
31108b8a9b1dSJustin T. Gibbs 	 * can get it before us while we simulate interrupts.
31118b8a9b1dSJustin T. Gibbs 	 */
3112227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
31138b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings--;
3114227d67aaSAlexander Motin 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3115227d67aaSAlexander Motin 	    (--timeout > 0)) {
3116227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
31170069293bSAlexander Motin 		DELAY(100);
31180cc28e3cSWarner Losh 		xpt_sim_poll(sim);
3119227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
31208b8a9b1dSJustin T. Gibbs 	}
31218b8a9b1dSJustin T. Gibbs 	dev->ccbq.dev_openings++;
3122227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
31238b8a9b1dSJustin T. Gibbs 
3124f93a843cSWarner Losh 	return (timeout);
3125f93a843cSWarner Losh }
3126f93a843cSWarner Losh 
3127f93a843cSWarner Losh void
xpt_pollwait(union ccb * start_ccb,uint32_t timeout)3128f93a843cSWarner Losh xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3129f93a843cSWarner Losh {
3130f93a843cSWarner Losh 
3131e6405c8cSJohn Baldwin 	KASSERT(cam_sim_pollable(start_ccb->ccb_h.path->bus->sim),
3132e6405c8cSJohn Baldwin 	    ("%s: non-pollable sim", __func__));
31338b8a9b1dSJustin T. Gibbs 	while (--timeout > 0) {
31340cc28e3cSWarner Losh 		xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
31358b8a9b1dSJustin T. Gibbs 		if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
31368b8a9b1dSJustin T. Gibbs 		    != CAM_REQ_INPROG)
31378b8a9b1dSJustin T. Gibbs 			break;
31380069293bSAlexander Motin 		DELAY(100);
31398b8a9b1dSJustin T. Gibbs 	}
3140f93a843cSWarner Losh 
31418b8a9b1dSJustin T. Gibbs 	if (timeout == 0) {
31428b8a9b1dSJustin T. Gibbs 		/*
31438b8a9b1dSJustin T. Gibbs 		 * XXX Is it worth adding a sim_timeout entry
31448b8a9b1dSJustin T. Gibbs 		 * point so we can attempt recovery?  If
31458b8a9b1dSJustin T. Gibbs 		 * this is only used for dumps, I don't think
31468b8a9b1dSJustin T. Gibbs 		 * it is.
31478b8a9b1dSJustin T. Gibbs 		 */
31488b8a9b1dSJustin T. Gibbs 		start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
31498b8a9b1dSJustin T. Gibbs 	}
3150f93a843cSWarner Losh }
3151f93a843cSWarner Losh 
31528b8a9b1dSJustin T. Gibbs /*
315321cffce5SBryan Drewery  * Schedule a peripheral driver to receive a ccb when its
31548b8a9b1dSJustin T. Gibbs  * target device has space for more transactions.
31558b8a9b1dSJustin T. Gibbs  */
31568b8a9b1dSJustin T. Gibbs void
xpt_schedule(struct cam_periph * periph,uint32_t new_priority)31577af2f2c8SWarner Losh xpt_schedule(struct cam_periph *periph, uint32_t new_priority)
31588b8a9b1dSJustin T. Gibbs {
31598b8a9b1dSJustin T. Gibbs 
3160227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3161227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3162227d67aaSAlexander Motin 	if (new_priority < periph->scheduled_priority) {
3163227d67aaSAlexander Motin 		periph->scheduled_priority = new_priority;
3164227d67aaSAlexander Motin 		xpt_run_allocq(periph, 0);
31658b8a9b1dSJustin T. Gibbs 	}
31668b8a9b1dSJustin T. Gibbs }
31678b8a9b1dSJustin T. Gibbs 
31688b8a9b1dSJustin T. Gibbs /*
31698b8a9b1dSJustin T. Gibbs  * Schedule a device to run on a given queue.
31708b8a9b1dSJustin T. Gibbs  * If the device was inserted as a new entry on the queue,
31718b8a9b1dSJustin T. Gibbs  * return 1 meaning the device queue should be run. If we
31728b8a9b1dSJustin T. Gibbs  * were already queued, implying someone else has already
31738b8a9b1dSJustin T. Gibbs  * started the queue, return 0 so the caller doesn't attempt
317477dc25ccSScott Long  * to run the queue.
31758b8a9b1dSJustin T. Gibbs  */
3176227d67aaSAlexander Motin static int
xpt_schedule_dev(struct camq * queue,cam_pinfo * pinfo,uint32_t new_priority)31778b8a9b1dSJustin T. Gibbs xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
31787af2f2c8SWarner Losh 		 uint32_t new_priority)
31798b8a9b1dSJustin T. Gibbs {
31808b8a9b1dSJustin T. Gibbs 	int retval;
31817af2f2c8SWarner Losh 	uint32_t old_priority;
31828b8a9b1dSJustin T. Gibbs 
3183aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
31848b8a9b1dSJustin T. Gibbs 
31858b8a9b1dSJustin T. Gibbs 	old_priority = pinfo->priority;
31868b8a9b1dSJustin T. Gibbs 
31878b8a9b1dSJustin T. Gibbs 	/*
31888b8a9b1dSJustin T. Gibbs 	 * Are we already queued?
31898b8a9b1dSJustin T. Gibbs 	 */
31908b8a9b1dSJustin T. Gibbs 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
31918b8a9b1dSJustin T. Gibbs 		/* Simply reorder based on new priority */
31928b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority) {
31938b8a9b1dSJustin T. Gibbs 			camq_change_priority(queue, pinfo->index,
31948b8a9b1dSJustin T. Gibbs 					     new_priority);
3195aa872be6SMatt Jacob 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
31968b8a9b1dSJustin T. Gibbs 					("changed priority to %d\n",
31978b8a9b1dSJustin T. Gibbs 					 new_priority));
319883c5d981SAlexander Motin 			retval = 1;
319983c5d981SAlexander Motin 		} else
32008b8a9b1dSJustin T. Gibbs 			retval = 0;
32018b8a9b1dSJustin T. Gibbs 	} else {
32028b8a9b1dSJustin T. Gibbs 		/* New entry on the queue */
32038b8a9b1dSJustin T. Gibbs 		if (new_priority < old_priority)
32048b8a9b1dSJustin T. Gibbs 			pinfo->priority = new_priority;
32058b8a9b1dSJustin T. Gibbs 
3206aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
32078b8a9b1dSJustin T. Gibbs 				("Inserting onto queue\n"));
32088bad620dSJustin T. Gibbs 		pinfo->generation = ++queue->generation;
32098b8a9b1dSJustin T. Gibbs 		camq_insert(queue, pinfo);
32108b8a9b1dSJustin T. Gibbs 		retval = 1;
32118b8a9b1dSJustin T. Gibbs 	}
32128b8a9b1dSJustin T. Gibbs 	return (retval);
32138b8a9b1dSJustin T. Gibbs }
32148b8a9b1dSJustin T. Gibbs 
32158b8a9b1dSJustin T. Gibbs static void
xpt_run_allocq_task(void * context,int pending)3216227d67aaSAlexander Motin xpt_run_allocq_task(void *context, int pending)
32178b8a9b1dSJustin T. Gibbs {
3218227d67aaSAlexander Motin 	struct cam_periph *periph = context;
32198b8a9b1dSJustin T. Gibbs 
3220227d67aaSAlexander Motin 	cam_periph_lock(periph);
3221227d67aaSAlexander Motin 	periph->flags &= ~CAM_PERIPH_RUN_TASK;
3222227d67aaSAlexander Motin 	xpt_run_allocq(periph, 1);
3223227d67aaSAlexander Motin 	cam_periph_unlock(periph);
3224227d67aaSAlexander Motin 	cam_periph_release(periph);
3225227d67aaSAlexander Motin }
3226227d67aaSAlexander Motin 
3227227d67aaSAlexander Motin static void
xpt_run_allocq(struct cam_periph * periph,int sleep)3228227d67aaSAlexander Motin xpt_run_allocq(struct cam_periph *periph, int sleep)
3229227d67aaSAlexander Motin {
3230227d67aaSAlexander Motin 	struct cam_ed	*device;
3231227d67aaSAlexander Motin 	union ccb	*ccb;
3232227d67aaSAlexander Motin 	uint32_t	 prio;
3233227d67aaSAlexander Motin 
3234227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
3235227d67aaSAlexander Motin 	if (periph->periph_allocating)
3236cccf4220SAlexander Motin 		return;
32377c1374d5SScott Long 	cam_periph_doacquire(periph);
3238227d67aaSAlexander Motin 	periph->periph_allocating = 1;
3239227d67aaSAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3240227d67aaSAlexander Motin 	device = periph->path->device;
3241227d67aaSAlexander Motin 	ccb = NULL;
3242227d67aaSAlexander Motin restart:
3243227d67aaSAlexander Motin 	while ((prio = min(periph->scheduled_priority,
3244227d67aaSAlexander Motin 	    periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3245227d67aaSAlexander Motin 	    (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3246227d67aaSAlexander Motin 	     device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3247227d67aaSAlexander Motin 		if (ccb == NULL &&
3248227d67aaSAlexander Motin 		    (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3249227d67aaSAlexander Motin 			if (sleep) {
3250227d67aaSAlexander Motin 				ccb = xpt_get_ccb(periph);
3251227d67aaSAlexander Motin 				goto restart;
3252227d67aaSAlexander Motin 			}
32538ec5ab3fSAlexander Motin 			if (periph->flags & CAM_PERIPH_RUN_TASK)
32548b8a9b1dSJustin T. Gibbs 				break;
3255c33e4029SAlexander Motin 			cam_periph_doacquire(periph);
3256227d67aaSAlexander Motin 			periph->flags |= CAM_PERIPH_RUN_TASK;
3257227d67aaSAlexander Motin 			taskqueue_enqueue(xsoftc.xpt_taskq,
3258227d67aaSAlexander Motin 			    &periph->periph_run_task);
3259227d67aaSAlexander Motin 			break;
32608b8a9b1dSJustin T. Gibbs 		}
3261227d67aaSAlexander Motin 		xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3262227d67aaSAlexander Motin 		if (prio == periph->immediate_priority) {
3263227d67aaSAlexander Motin 			periph->immediate_priority = CAM_PRIORITY_NONE;
3264227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3265227d67aaSAlexander Motin 					("waking cam_periph_getccb()\n"));
3266227d67aaSAlexander Motin 			SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3267227d67aaSAlexander Motin 					  periph_links.sle);
3268227d67aaSAlexander Motin 			wakeup(&periph->ccb_list);
3269227d67aaSAlexander Motin 		} else {
3270227d67aaSAlexander Motin 			periph->scheduled_priority = CAM_PRIORITY_NONE;
3271227d67aaSAlexander Motin 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3272227d67aaSAlexander Motin 					("calling periph_start()\n"));
3273227d67aaSAlexander Motin 			periph->periph_start(periph, ccb);
3274227d67aaSAlexander Motin 		}
3275227d67aaSAlexander Motin 		ccb = NULL;
3276227d67aaSAlexander Motin 	}
3277227d67aaSAlexander Motin 	if (ccb != NULL)
3278227d67aaSAlexander Motin 		xpt_release_ccb(ccb);
3279227d67aaSAlexander Motin 	periph->periph_allocating = 0;
32807c1374d5SScott Long 	cam_periph_release_locked(periph);
32818b8a9b1dSJustin T. Gibbs }
32828b8a9b1dSJustin T. Gibbs 
328383c5d981SAlexander Motin static void
xpt_run_devq(struct cam_devq * devq)3284cccf4220SAlexander Motin xpt_run_devq(struct cam_devq *devq)
32858b8a9b1dSJustin T. Gibbs {
3286401ed17aSAlexander Motin 	struct mtx *mtx;
32878b8a9b1dSJustin T. Gibbs 
3288cccf4220SAlexander Motin 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
32898b8a9b1dSJustin T. Gibbs 
3290cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt++;
32918b8a9b1dSJustin T. Gibbs 	while ((devq->send_queue.entries > 0)
3292ec700f26SAlexander Motin 	    && (devq->send_openings > 0)
3293cccf4220SAlexander Motin 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
32948b8a9b1dSJustin T. Gibbs 		struct	cam_ed *device;
32958b8a9b1dSJustin T. Gibbs 		union ccb *work_ccb;
32968b8a9b1dSJustin T. Gibbs 		struct	cam_sim *sim;
329708f13879SWarner Losh 		struct xpt_proto *proto;
32988b8a9b1dSJustin T. Gibbs 
3299227d67aaSAlexander Motin 		device = (struct cam_ed *)camq_remove(&devq->send_queue,
33005a526431SJustin T. Gibbs 							   CAMQ_HEAD);
3301aa872be6SMatt Jacob 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
330250642f18SKenneth D. Merry 				("running device %p\n", device));
33038b8a9b1dSJustin T. Gibbs 
33045a526431SJustin T. Gibbs 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
33058b8a9b1dSJustin T. Gibbs 		if (work_ccb == NULL) {
330657b89bbcSNate Lawson 			printf("device on run queue with no ccbs???\n");
33078b8a9b1dSJustin T. Gibbs 			continue;
33088b8a9b1dSJustin T. Gibbs 		}
33098b8a9b1dSJustin T. Gibbs 
33108b8a9b1dSJustin T. Gibbs 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3311daa5487fSAlexander Motin 			mtx_lock(&xsoftc.xpt_highpower_lock);
33122b83592fSScott Long 		 	if (xsoftc.num_highpower <= 0) {
33138b8a9b1dSJustin T. Gibbs 				/*
33148b8a9b1dSJustin T. Gibbs 				 * We got a high power command, but we
33158b8a9b1dSJustin T. Gibbs 				 * don't have any available slots.  Freeze
33168b8a9b1dSJustin T. Gibbs 				 * the device queue until we have a slot
33178b8a9b1dSJustin T. Gibbs 				 * available.
33188b8a9b1dSJustin T. Gibbs 				 */
3319daa5487fSAlexander Motin 				xpt_freeze_devq_device(device, 1);
3320227d67aaSAlexander Motin 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3321ea541bfdSAlexander Motin 						   highpowerq_entry);
33228b8a9b1dSJustin T. Gibbs 
3323daa5487fSAlexander Motin 				mtx_unlock(&xsoftc.xpt_highpower_lock);
33248b8a9b1dSJustin T. Gibbs 				continue;
33258b8a9b1dSJustin T. Gibbs 			} else {
33268b8a9b1dSJustin T. Gibbs 				/*
33278b8a9b1dSJustin T. Gibbs 				 * Consume a high power slot while
33288b8a9b1dSJustin T. Gibbs 				 * this ccb runs.
33298b8a9b1dSJustin T. Gibbs 				 */
33302b83592fSScott Long 				xsoftc.num_highpower--;
33318b8a9b1dSJustin T. Gibbs 			}
3332daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
33338b8a9b1dSJustin T. Gibbs 		}
33348b8a9b1dSJustin T. Gibbs 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
33358b8a9b1dSJustin T. Gibbs 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
33368b8a9b1dSJustin T. Gibbs 		devq->send_openings--;
33378b8a9b1dSJustin T. Gibbs 		devq->send_active++;
3338cccf4220SAlexander Motin 		xpt_schedule_devq(devq, device);
3339227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
33408b8a9b1dSJustin T. Gibbs 
3341cccf4220SAlexander Motin 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
33428b8a9b1dSJustin T. Gibbs 			/*
33438b8a9b1dSJustin T. Gibbs 			 * The client wants to freeze the queue
33448b8a9b1dSJustin T. Gibbs 			 * after this CCB is sent.
33458b8a9b1dSJustin T. Gibbs 			 */
334683c5d981SAlexander Motin 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
33478b8a9b1dSJustin T. Gibbs 		}
33488b8a9b1dSJustin T. Gibbs 
3349a4eb4f16SMatt Jacob 		/* In Target mode, the peripheral driver knows best... */
3350a4eb4f16SMatt Jacob 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3351a4eb4f16SMatt Jacob 			if ((device->inq_flags & SID_CmdQue) != 0
3352a4eb4f16SMatt Jacob 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
33538b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
33548b8a9b1dSJustin T. Gibbs 			else
33558b8a9b1dSJustin T. Gibbs 				/*
3356a4eb4f16SMatt Jacob 				 * Clear this in case of a retried CCB that
3357a4eb4f16SMatt Jacob 				 * failed due to a rejected tag.
33588b8a9b1dSJustin T. Gibbs 				 */
33598b8a9b1dSJustin T. Gibbs 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3360a4eb4f16SMatt Jacob 		}
33618b8a9b1dSJustin T. Gibbs 
336208f13879SWarner Losh 		KASSERT(device == work_ccb->ccb_h.path->device,
336308f13879SWarner Losh 		    ("device (%p) / path->device (%p) mismatch",
336408f13879SWarner Losh 			device, work_ccb->ccb_h.path->device));
336508f13879SWarner Losh 		proto = xpt_proto_find(device->protocol);
336608f13879SWarner Losh 		if (proto && proto->ops->debug_out)
336708f13879SWarner Losh 			proto->ops->debug_out(work_ccb);
3368de9ebb68SAlexander Motin 
33698b8a9b1dSJustin T. Gibbs 		/*
3370227d67aaSAlexander Motin 		 * Device queues can be shared among multiple SIM instances
3371db4fcadfSConrad Meyer 		 * that reside on different buses.  Use the SIM from the
3372227d67aaSAlexander Motin 		 * queued device, rather than the one from the calling bus.
33738b8a9b1dSJustin T. Gibbs 		 */
3374227d67aaSAlexander Motin 		sim = device->sim;
3375401ed17aSAlexander Motin 		mtx = sim->mtx;
3376401ed17aSAlexander Motin 		if (mtx && !mtx_owned(mtx))
3377401ed17aSAlexander Motin 			mtx_lock(mtx);
3378401ed17aSAlexander Motin 		else
3379401ed17aSAlexander Motin 			mtx = NULL;
3380e4c9cba7SWarner Losh 		work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
33818b8a9b1dSJustin T. Gibbs 		(*(sim->sim_action))(sim, work_ccb);
3382401ed17aSAlexander Motin 		if (mtx)
3383401ed17aSAlexander Motin 			mtx_unlock(mtx);
3384227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
33858b8a9b1dSJustin T. Gibbs 	}
3386cccf4220SAlexander Motin 	devq->send_queue.qfrozen_cnt--;
33878b8a9b1dSJustin T. Gibbs }
33888b8a9b1dSJustin T. Gibbs 
33898b8a9b1dSJustin T. Gibbs /*
3390f66ca1b1SWarner Losh  * This function merges stuff from the src ccb into the dst ccb, while keeping
3391f66ca1b1SWarner Losh  * important fields in the dst ccb constant.
33928b8a9b1dSJustin T. Gibbs  */
33938b8a9b1dSJustin T. Gibbs void
xpt_merge_ccb(union ccb * dst_ccb,union ccb * src_ccb)3394f66ca1b1SWarner Losh xpt_merge_ccb(union ccb *dst_ccb, union ccb *src_ccb)
33958b8a9b1dSJustin T. Gibbs {
339668153f43SScott Long 
33978b8a9b1dSJustin T. Gibbs 	/*
33988b8a9b1dSJustin T. Gibbs 	 * Pull fields that are valid for peripheral drivers to set
3399f66ca1b1SWarner Losh 	 * into the dst CCB along with the CCB "payload".
34008b8a9b1dSJustin T. Gibbs 	 */
3401f66ca1b1SWarner Losh 	dst_ccb->ccb_h.retry_count = src_ccb->ccb_h.retry_count;
3402f66ca1b1SWarner Losh 	dst_ccb->ccb_h.func_code = src_ccb->ccb_h.func_code;
3403f66ca1b1SWarner Losh 	dst_ccb->ccb_h.timeout = src_ccb->ccb_h.timeout;
3404f66ca1b1SWarner Losh 	dst_ccb->ccb_h.flags = src_ccb->ccb_h.flags;
3405f66ca1b1SWarner Losh 	bcopy(&(&src_ccb->ccb_h)[1], &(&dst_ccb->ccb_h)[1],
34068b8a9b1dSJustin T. Gibbs 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
34078b8a9b1dSJustin T. Gibbs }
34088b8a9b1dSJustin T. Gibbs 
34098b8a9b1dSJustin T. Gibbs void
xpt_setup_ccb_flags(struct ccb_hdr * ccb_h,struct cam_path * path,uint32_t priority,uint32_t flags)3410a9934668SKenneth D. Merry xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
34117af2f2c8SWarner Losh 		    uint32_t priority, uint32_t flags)
34128b8a9b1dSJustin T. Gibbs {
341368153f43SScott Long 
34148b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
34158b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.priority = priority;
34168b8a9b1dSJustin T. Gibbs 	ccb_h->path = path;
34178b8a9b1dSJustin T. Gibbs 	ccb_h->path_id = path->bus->path_id;
34188b8a9b1dSJustin T. Gibbs 	if (path->target)
34198b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = path->target->target_id;
34208b8a9b1dSJustin T. Gibbs 	else
34218b8a9b1dSJustin T. Gibbs 		ccb_h->target_id = CAM_TARGET_WILDCARD;
34228b8a9b1dSJustin T. Gibbs 	if (path->device) {
34238b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = path->device->lun_id;
34248bad620dSJustin T. Gibbs 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
34258b8a9b1dSJustin T. Gibbs 	} else {
34268b8a9b1dSJustin T. Gibbs 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
34278b8a9b1dSJustin T. Gibbs 	}
34288b8a9b1dSJustin T. Gibbs 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3429a9934668SKenneth D. Merry 	ccb_h->flags = flags;
3430b5595753SNathan Whitehorn 	ccb_h->xflags = 0;
34318b8a9b1dSJustin T. Gibbs }
34328b8a9b1dSJustin T. Gibbs 
3433a9934668SKenneth D. Merry void
xpt_setup_ccb(struct ccb_hdr * ccb_h,struct cam_path * path,uint32_t priority)34347af2f2c8SWarner Losh xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, uint32_t priority)
3435a9934668SKenneth D. Merry {
3436a9934668SKenneth D. Merry 	xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3437a9934668SKenneth D. Merry }
3438a9934668SKenneth D. Merry 
34398b8a9b1dSJustin T. Gibbs /* Path manipulation functions */
34408b8a9b1dSJustin T. Gibbs cam_status
xpt_create_path(struct cam_path ** new_path_ptr,struct cam_periph * perph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)34418b8a9b1dSJustin T. Gibbs xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
34428b8a9b1dSJustin T. Gibbs 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
34438b8a9b1dSJustin T. Gibbs {
34448b8a9b1dSJustin T. Gibbs 	struct	   cam_path *path;
34458b8a9b1dSJustin T. Gibbs 	cam_status status;
34468b8a9b1dSJustin T. Gibbs 
3447596ee08fSAlexander Motin 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
34488b8a9b1dSJustin T. Gibbs 
34498b8a9b1dSJustin T. Gibbs 	if (path == NULL) {
34508b8a9b1dSJustin T. Gibbs 		status = CAM_RESRC_UNAVAIL;
34518b8a9b1dSJustin T. Gibbs 		return(status);
34528b8a9b1dSJustin T. Gibbs 	}
34538b8a9b1dSJustin T. Gibbs 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
34548b8a9b1dSJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
3455596ee08fSAlexander Motin 		free(path, M_CAMPATH);
34568b8a9b1dSJustin T. Gibbs 		path = NULL;
34578b8a9b1dSJustin T. Gibbs 	}
34588b8a9b1dSJustin T. Gibbs 	*new_path_ptr = path;
34598b8a9b1dSJustin T. Gibbs 	return (status);
34608b8a9b1dSJustin T. Gibbs }
34618b8a9b1dSJustin T. Gibbs 
34622b83592fSScott Long cam_status
xpt_create_path_unlocked(struct cam_path ** new_path_ptr,struct cam_periph * periph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)34632b83592fSScott Long xpt_create_path_unlocked(struct cam_path **new_path_ptr,
34642b83592fSScott Long 			 struct cam_periph *periph, path_id_t path_id,
34652b83592fSScott Long 			 target_id_t target_id, lun_id_t lun_id)
34662b83592fSScott Long {
34672b83592fSScott Long 
3468227d67aaSAlexander Motin 	return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3469227d67aaSAlexander Motin 	    lun_id));
34702b83592fSScott Long }
34712b83592fSScott Long 
347252c9ce25SScott Long cam_status
xpt_compile_path(struct cam_path * new_path,struct cam_periph * perph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)34738b8a9b1dSJustin T. Gibbs xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
34748b8a9b1dSJustin T. Gibbs 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
34758b8a9b1dSJustin T. Gibbs {
34768b8a9b1dSJustin T. Gibbs 	struct	     cam_eb *bus;
34778b8a9b1dSJustin T. Gibbs 	struct	     cam_et *target;
34788b8a9b1dSJustin T. Gibbs 	struct	     cam_ed *device;
34798b8a9b1dSJustin T. Gibbs 	cam_status   status;
34808b8a9b1dSJustin T. Gibbs 
34818b8a9b1dSJustin T. Gibbs 	status = CAM_REQ_CMP;	/* Completed without error */
34828b8a9b1dSJustin T. Gibbs 	target = NULL;		/* Wildcarded */
34838b8a9b1dSJustin T. Gibbs 	device = NULL;		/* Wildcarded */
3484a5479bc5SJustin T. Gibbs 
3485a5479bc5SJustin T. Gibbs 	/*
3486a5479bc5SJustin T. Gibbs 	 * We will potentially modify the EDT, so block interrupts
3487a5479bc5SJustin T. Gibbs 	 * that may attempt to create cam paths.
3488a5479bc5SJustin T. Gibbs 	 */
34898b8a9b1dSJustin T. Gibbs 	bus = xpt_find_bus(path_id);
34908b8a9b1dSJustin T. Gibbs 	if (bus == NULL) {
34918b8a9b1dSJustin T. Gibbs 		status = CAM_PATH_INVALID;
3492c8bead2aSJustin T. Gibbs 	} else {
3493227d67aaSAlexander Motin 		xpt_lock_buses();
3494227d67aaSAlexander Motin 		mtx_lock(&bus->eb_mtx);
34958b8a9b1dSJustin T. Gibbs 		target = xpt_find_target(bus, target_id);
34968b8a9b1dSJustin T. Gibbs 		if (target == NULL) {
34978b8a9b1dSJustin T. Gibbs 			/* Create one */
34988b8a9b1dSJustin T. Gibbs 			struct cam_et *new_target;
34998b8a9b1dSJustin T. Gibbs 
35008b8a9b1dSJustin T. Gibbs 			new_target = xpt_alloc_target(bus, target_id);
35018b8a9b1dSJustin T. Gibbs 			if (new_target == NULL) {
35028b8a9b1dSJustin T. Gibbs 				status = CAM_RESRC_UNAVAIL;
35038b8a9b1dSJustin T. Gibbs 			} else {
35048b8a9b1dSJustin T. Gibbs 				target = new_target;
35058b8a9b1dSJustin T. Gibbs 			}
35068b8a9b1dSJustin T. Gibbs 		}
3507227d67aaSAlexander Motin 		xpt_unlock_buses();
3508c8bead2aSJustin T. Gibbs 		if (target != NULL) {
35098b8a9b1dSJustin T. Gibbs 			device = xpt_find_device(target, lun_id);
35108b8a9b1dSJustin T. Gibbs 			if (device == NULL) {
35118b8a9b1dSJustin T. Gibbs 				/* Create one */
35128b8a9b1dSJustin T. Gibbs 				struct cam_ed *new_device;
35138b8a9b1dSJustin T. Gibbs 
351452c9ce25SScott Long 				new_device =
3515ded2b706SWarner Losh 				    (*(bus->xport->ops->alloc_device))(bus,
35168b8a9b1dSJustin T. Gibbs 								       target,
35178b8a9b1dSJustin T. Gibbs 								       lun_id);
35188b8a9b1dSJustin T. Gibbs 				if (new_device == NULL) {
35198b8a9b1dSJustin T. Gibbs 					status = CAM_RESRC_UNAVAIL;
35208b8a9b1dSJustin T. Gibbs 				} else {
35218b8a9b1dSJustin T. Gibbs 					device = new_device;
35228b8a9b1dSJustin T. Gibbs 				}
35238b8a9b1dSJustin T. Gibbs 			}
35248b8a9b1dSJustin T. Gibbs 		}
3525227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
35268b8a9b1dSJustin T. Gibbs 	}
35278b8a9b1dSJustin T. Gibbs 
35288b8a9b1dSJustin T. Gibbs 	/*
35298b8a9b1dSJustin T. Gibbs 	 * Only touch the user's data if we are successful.
35308b8a9b1dSJustin T. Gibbs 	 */
35318b8a9b1dSJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
35328b8a9b1dSJustin T. Gibbs 		new_path->periph = perph;
35338b8a9b1dSJustin T. Gibbs 		new_path->bus = bus;
35348b8a9b1dSJustin T. Gibbs 		new_path->target = target;
35358b8a9b1dSJustin T. Gibbs 		new_path->device = device;
35368b8a9b1dSJustin T. Gibbs 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
35378b8a9b1dSJustin T. Gibbs 	} else {
35388b8a9b1dSJustin T. Gibbs 		if (device != NULL)
3539f98d7a47SAlexander Motin 			xpt_release_device(device);
35408b8a9b1dSJustin T. Gibbs 		if (target != NULL)
3541f98d7a47SAlexander Motin 			xpt_release_target(target);
3542a5479bc5SJustin T. Gibbs 		if (bus != NULL)
3543a5479bc5SJustin T. Gibbs 			xpt_release_bus(bus);
35448b8a9b1dSJustin T. Gibbs 	}
35458b8a9b1dSJustin T. Gibbs 	return (status);
35468b8a9b1dSJustin T. Gibbs }
35478b8a9b1dSJustin T. Gibbs 
354850aa1dafSWarner Losh int
xpt_clone_path(struct cam_path ** new_path_ptr,struct cam_path * path)3549227d67aaSAlexander Motin xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3550227d67aaSAlexander Motin {
3551227d67aaSAlexander Motin 	struct	   cam_path *new_path;
3552227d67aaSAlexander Motin 
3553227d67aaSAlexander Motin 	new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3554227d67aaSAlexander Motin 	if (new_path == NULL)
355550aa1dafSWarner Losh 		return (ENOMEM);
3556227d67aaSAlexander Motin 	*new_path = *path;
3557227d67aaSAlexander Motin 	if (path->bus != NULL)
3558227d67aaSAlexander Motin 		xpt_acquire_bus(path->bus);
3559227d67aaSAlexander Motin 	if (path->target != NULL)
3560227d67aaSAlexander Motin 		xpt_acquire_target(path->target);
3561227d67aaSAlexander Motin 	if (path->device != NULL)
3562227d67aaSAlexander Motin 		xpt_acquire_device(path->device);
35633dfb13c1SWarner Losh 	*new_path_ptr = new_path;
356450aa1dafSWarner Losh 	return (0);
3565227d67aaSAlexander Motin }
3566227d67aaSAlexander Motin 
356752c9ce25SScott Long void
xpt_release_path(struct cam_path * path)35688b8a9b1dSJustin T. Gibbs xpt_release_path(struct cam_path *path)
35698b8a9b1dSJustin T. Gibbs {
35708b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
35719dd03ecfSJustin T. Gibbs 	if (path->device != NULL) {
3572f98d7a47SAlexander Motin 		xpt_release_device(path->device);
35739dd03ecfSJustin T. Gibbs 		path->device = NULL;
35749dd03ecfSJustin T. Gibbs 	}
35759dd03ecfSJustin T. Gibbs 	if (path->target != NULL) {
3576f98d7a47SAlexander Motin 		xpt_release_target(path->target);
35779dd03ecfSJustin T. Gibbs 		path->target = NULL;
35789dd03ecfSJustin T. Gibbs 	}
35799dd03ecfSJustin T. Gibbs 	if (path->bus != NULL) {
35809dd03ecfSJustin T. Gibbs 		xpt_release_bus(path->bus);
35819dd03ecfSJustin T. Gibbs 		path->bus = NULL;
35829dd03ecfSJustin T. Gibbs 	}
35838b8a9b1dSJustin T. Gibbs }
35848b8a9b1dSJustin T. Gibbs 
35858b8a9b1dSJustin T. Gibbs void
xpt_free_path(struct cam_path * path)35868b8a9b1dSJustin T. Gibbs xpt_free_path(struct cam_path *path)
35878b8a9b1dSJustin T. Gibbs {
358868153f43SScott Long 
35898b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
35908b8a9b1dSJustin T. Gibbs 	xpt_release_path(path);
3591596ee08fSAlexander Motin 	free(path, M_CAMPATH);
35928b8a9b1dSJustin T. Gibbs }
35938b8a9b1dSJustin T. Gibbs 
359415975b7bSMatt Jacob void
xpt_path_counts(struct cam_path * path,uint32_t * bus_ref,uint32_t * periph_ref,uint32_t * target_ref,uint32_t * device_ref)359515975b7bSMatt Jacob xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
359615975b7bSMatt Jacob     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
359715975b7bSMatt Jacob {
359815975b7bSMatt Jacob 
35999a7c2696SAlexander Motin 	xpt_lock_buses();
360015975b7bSMatt Jacob 	if (bus_ref) {
360115975b7bSMatt Jacob 		if (path->bus)
360215975b7bSMatt Jacob 			*bus_ref = path->bus->refcount;
360315975b7bSMatt Jacob 		else
360415975b7bSMatt Jacob 			*bus_ref = 0;
360515975b7bSMatt Jacob 	}
360615975b7bSMatt Jacob 	if (periph_ref) {
360715975b7bSMatt Jacob 		if (path->periph)
360815975b7bSMatt Jacob 			*periph_ref = path->periph->refcount;
360915975b7bSMatt Jacob 		else
361015975b7bSMatt Jacob 			*periph_ref = 0;
361115975b7bSMatt Jacob 	}
3612dcdf6e74SAlexander Motin 	xpt_unlock_buses();
361315975b7bSMatt Jacob 	if (target_ref) {
361415975b7bSMatt Jacob 		if (path->target)
361515975b7bSMatt Jacob 			*target_ref = path->target->refcount;
361615975b7bSMatt Jacob 		else
361715975b7bSMatt Jacob 			*target_ref = 0;
361815975b7bSMatt Jacob 	}
361915975b7bSMatt Jacob 	if (device_ref) {
362015975b7bSMatt Jacob 		if (path->device)
362115975b7bSMatt Jacob 			*device_ref = path->device->refcount;
362215975b7bSMatt Jacob 		else
362315975b7bSMatt Jacob 			*device_ref = 0;
362415975b7bSMatt Jacob 	}
362515975b7bSMatt Jacob }
36268b8a9b1dSJustin T. Gibbs 
36278b8a9b1dSJustin T. Gibbs /*
36282cefde5fSJustin T. Gibbs  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
36292cefde5fSJustin T. Gibbs  * in path1, 2 for match with wildcards in path2.
36308b8a9b1dSJustin T. Gibbs  */
36318b8a9b1dSJustin T. Gibbs int
xpt_path_comp(struct cam_path * path1,struct cam_path * path2)36328b8a9b1dSJustin T. Gibbs xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
36338b8a9b1dSJustin T. Gibbs {
36348b8a9b1dSJustin T. Gibbs 	int retval = 0;
36358b8a9b1dSJustin T. Gibbs 
36368b8a9b1dSJustin T. Gibbs 	if (path1->bus != path2->bus) {
36372cefde5fSJustin T. Gibbs 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
36388b8a9b1dSJustin T. Gibbs 			retval = 1;
36392cefde5fSJustin T. Gibbs 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
36402cefde5fSJustin T. Gibbs 			retval = 2;
36418b8a9b1dSJustin T. Gibbs 		else
36428b8a9b1dSJustin T. Gibbs 			return (-1);
36438b8a9b1dSJustin T. Gibbs 	}
36448b8a9b1dSJustin T. Gibbs 	if (path1->target != path2->target) {
36452cefde5fSJustin T. Gibbs 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
36462cefde5fSJustin T. Gibbs 			if (retval == 0)
36478b8a9b1dSJustin T. Gibbs 				retval = 1;
36482cefde5fSJustin T. Gibbs 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
36492cefde5fSJustin T. Gibbs 			retval = 2;
36508b8a9b1dSJustin T. Gibbs 		else
36518b8a9b1dSJustin T. Gibbs 			return (-1);
36528b8a9b1dSJustin T. Gibbs 	}
36538b8a9b1dSJustin T. Gibbs 	if (path1->device != path2->device) {
36542cefde5fSJustin T. Gibbs 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
36552cefde5fSJustin T. Gibbs 			if (retval == 0)
36568b8a9b1dSJustin T. Gibbs 				retval = 1;
36572cefde5fSJustin T. Gibbs 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
36582cefde5fSJustin T. Gibbs 			retval = 2;
36598b8a9b1dSJustin T. Gibbs 		else
36608b8a9b1dSJustin T. Gibbs 			return (-1);
36618b8a9b1dSJustin T. Gibbs 	}
36628b8a9b1dSJustin T. Gibbs 	return (retval);
36638b8a9b1dSJustin T. Gibbs }
36648b8a9b1dSJustin T. Gibbs 
36650d4f3c31SAlexander Motin int
xpt_path_comp_dev(struct cam_path * path,struct cam_ed * dev)36660d4f3c31SAlexander Motin xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
36670d4f3c31SAlexander Motin {
36680d4f3c31SAlexander Motin 	int retval = 0;
36690d4f3c31SAlexander Motin 
36700d4f3c31SAlexander Motin 	if (path->bus != dev->target->bus) {
36710d4f3c31SAlexander Motin 		if (path->bus->path_id == CAM_BUS_WILDCARD)
36720d4f3c31SAlexander Motin 			retval = 1;
36730d4f3c31SAlexander Motin 		else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
36740d4f3c31SAlexander Motin 			retval = 2;
36750d4f3c31SAlexander Motin 		else
36760d4f3c31SAlexander Motin 			return (-1);
36770d4f3c31SAlexander Motin 	}
36780d4f3c31SAlexander Motin 	if (path->target != dev->target) {
36790d4f3c31SAlexander Motin 		if (path->target->target_id == CAM_TARGET_WILDCARD) {
36800d4f3c31SAlexander Motin 			if (retval == 0)
36810d4f3c31SAlexander Motin 				retval = 1;
36820d4f3c31SAlexander Motin 		} else if (dev->target->target_id == CAM_TARGET_WILDCARD)
36830d4f3c31SAlexander Motin 			retval = 2;
36840d4f3c31SAlexander Motin 		else
36850d4f3c31SAlexander Motin 			return (-1);
36860d4f3c31SAlexander Motin 	}
36870d4f3c31SAlexander Motin 	if (path->device != dev) {
36880d4f3c31SAlexander Motin 		if (path->device->lun_id == CAM_LUN_WILDCARD) {
36890d4f3c31SAlexander Motin 			if (retval == 0)
36900d4f3c31SAlexander Motin 				retval = 1;
36910d4f3c31SAlexander Motin 		} else if (dev->lun_id == CAM_LUN_WILDCARD)
36920d4f3c31SAlexander Motin 			retval = 2;
36930d4f3c31SAlexander Motin 		else
36940d4f3c31SAlexander Motin 			return (-1);
36950d4f3c31SAlexander Motin 	}
36960d4f3c31SAlexander Motin 	return (retval);
36970d4f3c31SAlexander Motin }
36980d4f3c31SAlexander Motin 
36998b8a9b1dSJustin T. Gibbs void
xpt_print_path(struct cam_path * path)37008b8a9b1dSJustin T. Gibbs xpt_print_path(struct cam_path *path)
37018b8a9b1dSJustin T. Gibbs {
3702ab3e89f1SScott Long 	struct sbuf sb;
3703ab3e89f1SScott Long 	char buffer[XPT_PRINT_LEN];
370468153f43SScott Long 
3705ab3e89f1SScott Long 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3706ab3e89f1SScott Long 	xpt_path_sbuf(path, &sb);
3707ab3e89f1SScott Long 	sbuf_finish(&sb);
3708ab3e89f1SScott Long 	printf("%s", sbuf_data(&sb));
3709ab3e89f1SScott Long 	sbuf_delete(&sb);
37108b8a9b1dSJustin T. Gibbs }
37118b8a9b1dSJustin T. Gibbs 
371270f2356dSWarner Losh static void
xpt_device_sbuf(struct cam_ed * device,struct sbuf * sb)371370f2356dSWarner Losh xpt_device_sbuf(struct cam_ed *device, struct sbuf *sb)
37140d4f3c31SAlexander Motin {
37150d4f3c31SAlexander Motin 	if (device == NULL)
3716519b24f0SAlexander Motin 		sbuf_cat(sb, "(nopath): ");
37170d4f3c31SAlexander Motin 	else {
371870f2356dSWarner Losh 		sbuf_printf(sb, "(noperiph:%s%d:%d:%d:%jx): ",
371970f2356dSWarner Losh 		    device->sim->sim_name,
37200d4f3c31SAlexander Motin 		    device->sim->unit_number,
37210d4f3c31SAlexander Motin 		    device->sim->bus_id,
37220d4f3c31SAlexander Motin 		    device->target->target_id,
3723abe83505SNathan Whitehorn 		    (uintmax_t)device->lun_id);
37240d4f3c31SAlexander Motin 	}
37250d4f3c31SAlexander Motin }
37260d4f3c31SAlexander Motin 
37270d4f3c31SAlexander Motin void
xpt_print(struct cam_path * path,const char * fmt,...)3728f0d9af51SMatt Jacob xpt_print(struct cam_path *path, const char *fmt, ...)
3729f0d9af51SMatt Jacob {
3730f0d9af51SMatt Jacob 	va_list ap;
3731ab3e89f1SScott Long 	struct sbuf sb;
3732a136ca54SScott Long 	char buffer[XPT_PRINT_LEN];
3733ab3e89f1SScott Long 
3734a136ca54SScott Long 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3735ab3e89f1SScott Long 
3736ab3e89f1SScott Long 	xpt_path_sbuf(path, &sb);
3737f0d9af51SMatt Jacob 	va_start(ap, fmt);
3738ab3e89f1SScott Long 	sbuf_vprintf(&sb, fmt, ap);
3739f0d9af51SMatt Jacob 	va_end(ap);
3740ab3e89f1SScott Long 
3741ab3e89f1SScott Long 	sbuf_finish(&sb);
3742ab3e89f1SScott Long 	printf("%s", sbuf_data(&sb));
3743ab3e89f1SScott Long 	sbuf_delete(&sb);
3744f0d9af51SMatt Jacob }
3745f0d9af51SMatt Jacob 
37466332e0f1SAlexander Motin char *
xpt_path_string(struct cam_path * path,char * str,size_t str_len)37473393f8daSKenneth D. Merry xpt_path_string(struct cam_path *path, char *str, size_t str_len)
37483393f8daSKenneth D. Merry {
37493393f8daSKenneth D. Merry 	struct sbuf sb;
37503393f8daSKenneth D. Merry 
37513393f8daSKenneth D. Merry 	sbuf_new(&sb, str, str_len, 0);
37526332e0f1SAlexander Motin 	xpt_path_sbuf(path, &sb);
3753ab3e89f1SScott Long 	sbuf_finish(&sb);
37546332e0f1SAlexander Motin 	return (str);
3755ab3e89f1SScott Long }
3756ab3e89f1SScott Long 
37576332e0f1SAlexander Motin void
xpt_path_sbuf(struct cam_path * path,struct sbuf * sb)3758ab3e89f1SScott Long xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3759ab3e89f1SScott Long {
37603393f8daSKenneth D. Merry 
37613393f8daSKenneth D. Merry 	if (path == NULL)
3762519b24f0SAlexander Motin 		sbuf_cat(sb, "(nopath): ");
37633393f8daSKenneth D. Merry 	else {
37643393f8daSKenneth D. Merry 		if (path->periph != NULL)
3765ab3e89f1SScott Long 			sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
37663393f8daSKenneth D. Merry 				    path->periph->unit_number);
37673393f8daSKenneth D. Merry 		else
3768519b24f0SAlexander Motin 			sbuf_cat(sb, "(noperiph:");
37693393f8daSKenneth D. Merry 
37703393f8daSKenneth D. Merry 		if (path->bus != NULL)
3771ab3e89f1SScott Long 			sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
37723393f8daSKenneth D. Merry 				    path->bus->sim->unit_number,
37733393f8daSKenneth D. Merry 				    path->bus->sim->bus_id);
37743393f8daSKenneth D. Merry 		else
3775519b24f0SAlexander Motin 			sbuf_cat(sb, "nobus:");
37763393f8daSKenneth D. Merry 
37773393f8daSKenneth D. Merry 		if (path->target != NULL)
3778ab3e89f1SScott Long 			sbuf_printf(sb, "%d:", path->target->target_id);
37793393f8daSKenneth D. Merry 		else
3780519b24f0SAlexander Motin 			sbuf_cat(sb, "X:");
37813393f8daSKenneth D. Merry 
37823393f8daSKenneth D. Merry 		if (path->device != NULL)
3783ab3e89f1SScott Long 			sbuf_printf(sb, "%jx): ",
3784abe83505SNathan Whitehorn 			    (uintmax_t)path->device->lun_id);
37853393f8daSKenneth D. Merry 		else
3786519b24f0SAlexander Motin 			sbuf_cat(sb, "X): ");
37873393f8daSKenneth D. Merry 	}
37883393f8daSKenneth D. Merry }
37893393f8daSKenneth D. Merry 
37908b8a9b1dSJustin T. Gibbs path_id_t
xpt_path_path_id(struct cam_path * path)37918b8a9b1dSJustin T. Gibbs xpt_path_path_id(struct cam_path *path)
37928b8a9b1dSJustin T. Gibbs {
37938b8a9b1dSJustin T. Gibbs 	return(path->bus->path_id);
37948b8a9b1dSJustin T. Gibbs }
37958b8a9b1dSJustin T. Gibbs 
37968b8a9b1dSJustin T. Gibbs target_id_t
xpt_path_target_id(struct cam_path * path)37978b8a9b1dSJustin T. Gibbs xpt_path_target_id(struct cam_path *path)
37988b8a9b1dSJustin T. Gibbs {
37998b8a9b1dSJustin T. Gibbs 	if (path->target != NULL)
38008b8a9b1dSJustin T. Gibbs 		return (path->target->target_id);
38018b8a9b1dSJustin T. Gibbs 	else
38028b8a9b1dSJustin T. Gibbs 		return (CAM_TARGET_WILDCARD);
38038b8a9b1dSJustin T. Gibbs }
38048b8a9b1dSJustin T. Gibbs 
38058b8a9b1dSJustin T. Gibbs lun_id_t
xpt_path_lun_id(struct cam_path * path)38068b8a9b1dSJustin T. Gibbs xpt_path_lun_id(struct cam_path *path)
38078b8a9b1dSJustin T. Gibbs {
38088b8a9b1dSJustin T. Gibbs 	if (path->device != NULL)
38098b8a9b1dSJustin T. Gibbs 		return (path->device->lun_id);
38108b8a9b1dSJustin T. Gibbs 	else
38118b8a9b1dSJustin T. Gibbs 		return (CAM_LUN_WILDCARD);
38128b8a9b1dSJustin T. Gibbs }
38138b8a9b1dSJustin T. Gibbs 
38148b8a9b1dSJustin T. Gibbs struct cam_sim *
xpt_path_sim(struct cam_path * path)38158b8a9b1dSJustin T. Gibbs xpt_path_sim(struct cam_path *path)
38168b8a9b1dSJustin T. Gibbs {
381768153f43SScott Long 
38188b8a9b1dSJustin T. Gibbs 	return (path->bus->sim);
38198b8a9b1dSJustin T. Gibbs }
38208b8a9b1dSJustin T. Gibbs 
38218b8a9b1dSJustin T. Gibbs struct cam_periph*
xpt_path_periph(struct cam_path * path)38228b8a9b1dSJustin T. Gibbs xpt_path_periph(struct cam_path *path)
38238b8a9b1dSJustin T. Gibbs {
382468153f43SScott Long 
38258b8a9b1dSJustin T. Gibbs 	return (path->periph);
38268b8a9b1dSJustin T. Gibbs }
38278b8a9b1dSJustin T. Gibbs 
38288b8a9b1dSJustin T. Gibbs /*
38298b8a9b1dSJustin T. Gibbs  * Release a CAM control block for the caller.  Remit the cost of the structure
38308b8a9b1dSJustin T. Gibbs  * to the device referenced by the path.  If the this device had no 'credits'
38318b8a9b1dSJustin T. Gibbs  * and peripheral drivers have registered async callbacks for this notification
38328b8a9b1dSJustin T. Gibbs  * call them now.
38338b8a9b1dSJustin T. Gibbs  */
38348b8a9b1dSJustin T. Gibbs void
xpt_release_ccb(union ccb * free_ccb)38358b8a9b1dSJustin T. Gibbs xpt_release_ccb(union ccb *free_ccb)
38368b8a9b1dSJustin T. Gibbs {
38378b8a9b1dSJustin T. Gibbs 	struct	 cam_ed *device;
3838227d67aaSAlexander Motin 	struct	 cam_periph *periph;
383968153f43SScott Long 
3840aa872be6SMatt Jacob 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3841227d67aaSAlexander Motin 	xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3842227d67aaSAlexander Motin 	device = free_ccb->ccb_h.path->device;
3843227d67aaSAlexander Motin 	periph = free_ccb->ccb_h.path->periph;
38442b83592fSScott Long 
38458b8a9b1dSJustin T. Gibbs 	xpt_free_ccb(free_ccb);
3846227d67aaSAlexander Motin 	periph->periph_allocated--;
3847227d67aaSAlexander Motin 	cam_ccbq_release_opening(&device->ccbq);
3848227d67aaSAlexander Motin 	xpt_run_allocq(periph, 0);
38498b8a9b1dSJustin T. Gibbs }
38508b8a9b1dSJustin T. Gibbs 
38518b8a9b1dSJustin T. Gibbs /* Functions accessed by SIM drivers */
38528b8a9b1dSJustin T. Gibbs 
3853ded2b706SWarner Losh static struct xpt_xport_ops xport_default_ops = {
385452c9ce25SScott Long 	.alloc_device = xpt_alloc_device_default,
385552c9ce25SScott Long 	.action = xpt_action_default,
385652c9ce25SScott Long 	.async = xpt_dev_async_default,
385752c9ce25SScott Long };
3858ded2b706SWarner Losh static struct xpt_xport xport_default = {
3859ded2b706SWarner Losh 	.xport = XPORT_UNKNOWN,
3860ded2b706SWarner Losh 	.name = "unknown",
3861ded2b706SWarner Losh 	.ops = &xport_default_ops,
3862ded2b706SWarner Losh };
3863ded2b706SWarner Losh 
3864ded2b706SWarner Losh CAM_XPT_XPORT(xport_default);
386552c9ce25SScott Long 
38668b8a9b1dSJustin T. Gibbs /*
38678b8a9b1dSJustin T. Gibbs  * A sim structure, listing the SIM entry points and instance
38688b8a9b1dSJustin T. Gibbs  * identification info is passed to xpt_bus_register to hook the SIM
38698b8a9b1dSJustin T. Gibbs  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
3870db4fcadfSConrad Meyer  * for this new bus and places it in the array of buses and assigns
38718b8a9b1dSJustin T. Gibbs  * it a path_id.  The path_id may be influenced by "hard wiring"
38728b8a9b1dSJustin T. Gibbs  * information specified by the user.  Once interrupt services are
387302caf36eSEdward Tomasz Napierala  * available, the bus will be probed.
38748b8a9b1dSJustin T. Gibbs  */
387530f8afd0SWarner Losh int
xpt_bus_register(struct cam_sim * sim,device_t parent,uint32_t bus)387630f8afd0SWarner Losh xpt_bus_register(struct cam_sim *sim, device_t parent, uint32_t bus)
38778b8a9b1dSJustin T. Gibbs {
38788b8a9b1dSJustin T. Gibbs 	struct cam_eb *new_bus;
3879434bbf6eSJustin T. Gibbs 	struct cam_eb *old_bus;
38808b8a9b1dSJustin T. Gibbs 	struct ccb_pathinq cpi;
388183c5d981SAlexander Motin 	struct cam_path *path;
388252c9ce25SScott Long 	cam_status status;
38838b8a9b1dSJustin T. Gibbs 
38848b8a9b1dSJustin T. Gibbs 	sim->bus_id = bus;
38858b8a9b1dSJustin T. Gibbs 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3886227d67aaSAlexander Motin 					  M_CAMXPT, M_NOWAIT|M_ZERO);
38878b8a9b1dSJustin T. Gibbs 	if (new_bus == NULL) {
38888b8a9b1dSJustin T. Gibbs 		/* Couldn't satisfy request */
388930f8afd0SWarner Losh 		return (ENOMEM);
38908b8a9b1dSJustin T. Gibbs 	}
38918b8a9b1dSJustin T. Gibbs 
3892227d67aaSAlexander Motin 	mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3893434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&new_bus->et_entries);
3894fa6099fdSEdward Tomasz Napierala 	cam_sim_hold(sim);
38958b8a9b1dSJustin T. Gibbs 	new_bus->sim = sim;
389687cfaf0eSJustin T. Gibbs 	timevalclear(&new_bus->last_reset);
3897434bbf6eSJustin T. Gibbs 	new_bus->flags = 0;
3898a5479bc5SJustin T. Gibbs 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3899434bbf6eSJustin T. Gibbs 	new_bus->generation = 0;
390040990d54SWarner Losh 	new_bus->parent_dev = parent;
390152c9ce25SScott Long 
39029a7c2696SAlexander Motin 	xpt_lock_buses();
39036dfc67e3SAlexander Motin 	sim->path_id = new_bus->path_id =
39046dfc67e3SAlexander Motin 	    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
39052b83592fSScott Long 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3906434bbf6eSJustin T. Gibbs 	while (old_bus != NULL
3907434bbf6eSJustin T. Gibbs 	    && old_bus->path_id < new_bus->path_id)
3908434bbf6eSJustin T. Gibbs 		old_bus = TAILQ_NEXT(old_bus, links);
3909434bbf6eSJustin T. Gibbs 	if (old_bus != NULL)
3910434bbf6eSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3911434bbf6eSJustin T. Gibbs 	else
39122b83592fSScott Long 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
39132b83592fSScott Long 	xsoftc.bus_generation++;
39149a7c2696SAlexander Motin 	xpt_unlock_buses();
39158b8a9b1dSJustin T. Gibbs 
391652c9ce25SScott Long 	/*
391752c9ce25SScott Long 	 * Set a default transport so that a PATH_INQ can be issued to
391852c9ce25SScott Long 	 * the SIM.  This will then allow for probing and attaching of
391952c9ce25SScott Long 	 * a more appropriate transport.
392052c9ce25SScott Long 	 */
392152c9ce25SScott Long 	new_bus->xport = &xport_default;
39228b8a9b1dSJustin T. Gibbs 
392332aa80a6SAlexander Motin 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
39248b8a9b1dSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3925627995dcSAlexander Motin 	if (status != CAM_REQ_CMP) {
3926627995dcSAlexander Motin 		xpt_release_bus(new_bus);
392730f8afd0SWarner Losh 		return (ENOMEM);
3928627995dcSAlexander Motin 	}
392952c9ce25SScott Long 
3930762a7f4fSWarner Losh 	xpt_path_inq(&cpi, path);
393152c9ce25SScott Long 
3932e932f0d2SJohn Baldwin 	/*
3933e932f0d2SJohn Baldwin 	 * Use the results of PATH_INQ to pick a transport.  Note that
3934e932f0d2SJohn Baldwin 	 * the xpt bus (which uses XPORT_UNSPECIFIED) always uses
3935e932f0d2SJohn Baldwin 	 * xport_default instead of a transport from
3936e932f0d2SJohn Baldwin 	 * cam_xpt_port_set.
3937e932f0d2SJohn Baldwin 	 */
3938e932f0d2SJohn Baldwin 	if (cam_ccb_success((union ccb *)&cpi) &&
3939e932f0d2SJohn Baldwin 	    cpi.transport != XPORT_UNSPECIFIED) {
3940ded2b706SWarner Losh 		struct xpt_xport **xpt;
3941ded2b706SWarner Losh 
3942ded2b706SWarner Losh 		SET_FOREACH(xpt, cam_xpt_xport_set) {
3943ded2b706SWarner Losh 			if ((*xpt)->xport == cpi.transport) {
3944ded2b706SWarner Losh 				new_bus->xport = *xpt;
394552c9ce25SScott Long 				break;
3946ded2b706SWarner Losh 			}
3947ded2b706SWarner Losh 		}
3948e932f0d2SJohn Baldwin 		if (new_bus->xport == &xport_default) {
3949bf1a8895SScott Long 			xpt_print(path,
3950bf1a8895SScott Long 			    "No transport found for %d\n", cpi.transport);
3951ded2b706SWarner Losh 			xpt_release_bus(new_bus);
3952e932f0d2SJohn Baldwin 			xpt_free_path(path);
395330f8afd0SWarner Losh 			return (EINVAL);
39548b8a9b1dSJustin T. Gibbs 		}
395552c9ce25SScott Long 	}
395652c9ce25SScott Long 
395752c9ce25SScott Long 	/* Notify interested parties */
395852c9ce25SScott Long 	if (sim->path_id != CAM_XPT_PATH_ID) {
395983c5d981SAlexander Motin 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
3960b01773b0SKenneth D. Merry 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3961b01773b0SKenneth D. Merry 			union	ccb *scan_ccb;
3962b01773b0SKenneth D. Merry 
396383c5d981SAlexander Motin 			/* Initiate bus rescan. */
396483c5d981SAlexander Motin 			scan_ccb = xpt_alloc_ccb_nowait();
3965e5736ac8SAlexander Motin 			if (scan_ccb != NULL) {
396683c5d981SAlexander Motin 				scan_ccb->ccb_h.path = path;
396783c5d981SAlexander Motin 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
396883c5d981SAlexander Motin 				scan_ccb->crcn.flags = 0;
396983c5d981SAlexander Motin 				xpt_rescan(scan_ccb);
39707f7aacb4SAlexander Motin 			} else {
3971b01773b0SKenneth D. Merry 				xpt_print(path,
3972b01773b0SKenneth D. Merry 					  "Can't allocate CCB to scan bus\n");
39737f7aacb4SAlexander Motin 				xpt_free_path(path);
39747f7aacb4SAlexander Motin 			}
3975b01773b0SKenneth D. Merry 		} else
3976b01773b0SKenneth D. Merry 			xpt_free_path(path);
3977e5736ac8SAlexander Motin 	} else
397883c5d981SAlexander Motin 		xpt_free_path(path);
39798b8a9b1dSJustin T. Gibbs 	return (CAM_SUCCESS);
39808b8a9b1dSJustin T. Gibbs }
39818b8a9b1dSJustin T. Gibbs 
398230f8afd0SWarner Losh int
xpt_bus_deregister(path_id_t pathid)3983434bbf6eSJustin T. Gibbs xpt_bus_deregister(path_id_t pathid)
39848b8a9b1dSJustin T. Gibbs {
3985434bbf6eSJustin T. Gibbs 	struct cam_path bus_path;
3986434bbf6eSJustin T. Gibbs 	cam_status status;
3987434bbf6eSJustin T. Gibbs 
3988434bbf6eSJustin T. Gibbs 	status = xpt_compile_path(&bus_path, NULL, pathid,
3989434bbf6eSJustin T. Gibbs 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3990434bbf6eSJustin T. Gibbs 	if (status != CAM_REQ_CMP)
399130f8afd0SWarner Losh 		return (ENOMEM);
3992434bbf6eSJustin T. Gibbs 
3993434bbf6eSJustin T. Gibbs 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3994434bbf6eSJustin T. Gibbs 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3995434bbf6eSJustin T. Gibbs 
3996434bbf6eSJustin T. Gibbs 	/* Release the reference count held while registered. */
3997434bbf6eSJustin T. Gibbs 	xpt_release_bus(bus_path.bus);
3998434bbf6eSJustin T. Gibbs 	xpt_release_path(&bus_path);
3999434bbf6eSJustin T. Gibbs 
400030f8afd0SWarner Losh 	return (CAM_SUCCESS);
4001434bbf6eSJustin T. Gibbs }
4002434bbf6eSJustin T. Gibbs 
4003434bbf6eSJustin T. Gibbs static path_id_t
xptnextfreepathid(void)4004434bbf6eSJustin T. Gibbs xptnextfreepathid(void)
4005434bbf6eSJustin T. Gibbs {
4006434bbf6eSJustin T. Gibbs 	struct cam_eb *bus;
4007434bbf6eSJustin T. Gibbs 	path_id_t pathid;
40082398f0cdSPeter Wemm 	const char *strval;
40098b8a9b1dSJustin T. Gibbs 
40106dfc67e3SAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4011434bbf6eSJustin T. Gibbs 	pathid = 0;
40122b83592fSScott Long 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4013434bbf6eSJustin T. Gibbs retry:
4014434bbf6eSJustin T. Gibbs 	/* Find an unoccupied pathid */
40159e6461a2SMatt Jacob 	while (bus != NULL && bus->path_id <= pathid) {
4016434bbf6eSJustin T. Gibbs 		if (bus->path_id == pathid)
4017434bbf6eSJustin T. Gibbs 			pathid++;
4018434bbf6eSJustin T. Gibbs 		bus = TAILQ_NEXT(bus, links);
4019434bbf6eSJustin T. Gibbs 	}
4020434bbf6eSJustin T. Gibbs 
4021434bbf6eSJustin T. Gibbs 	/*
4022434bbf6eSJustin T. Gibbs 	 * Ensure that this pathid is not reserved for
4023434bbf6eSJustin T. Gibbs 	 * a bus that may be registered in the future.
4024434bbf6eSJustin T. Gibbs 	 */
402575f51904SPeter Wemm 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4026434bbf6eSJustin T. Gibbs 		++pathid;
40278b8a9b1dSJustin T. Gibbs 		/* Start the search over */
4028434bbf6eSJustin T. Gibbs 		goto retry;
40298b8a9b1dSJustin T. Gibbs 	}
4030434bbf6eSJustin T. Gibbs 	return (pathid);
40318b8a9b1dSJustin T. Gibbs }
40328b8a9b1dSJustin T. Gibbs 
4033434bbf6eSJustin T. Gibbs static path_id_t
xptpathid(const char * sim_name,int sim_unit,int sim_bus)4034434bbf6eSJustin T. Gibbs xptpathid(const char *sim_name, int sim_unit, int sim_bus)
40358b8a9b1dSJustin T. Gibbs {
40368b8a9b1dSJustin T. Gibbs 	path_id_t pathid;
403775f51904SPeter Wemm 	int i, dunit, val;
4038642f0c46SPeter Wemm 	char buf[32];
40392398f0cdSPeter Wemm 	const char *dname;
40408b8a9b1dSJustin T. Gibbs 
40418b8a9b1dSJustin T. Gibbs 	pathid = CAM_XPT_PATH_ID;
404275f51904SPeter Wemm 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
40436dfc67e3SAlexander Motin 	if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
40446dfc67e3SAlexander Motin 		return (pathid);
40452398f0cdSPeter Wemm 	i = 0;
40462398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
40472398f0cdSPeter Wemm 		if (strcmp(dname, "scbus")) {
4048642f0c46SPeter Wemm 			/* Avoid a bit of foot shooting. */
4049642f0c46SPeter Wemm 			continue;
4050642f0c46SPeter Wemm 		}
405175f51904SPeter Wemm 		if (dunit < 0)		/* unwired?! */
40528b8a9b1dSJustin T. Gibbs 			continue;
405375f51904SPeter Wemm 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
405475f51904SPeter Wemm 			if (sim_bus == val) {
405575f51904SPeter Wemm 				pathid = dunit;
40568b8a9b1dSJustin T. Gibbs 				break;
40578b8a9b1dSJustin T. Gibbs 			}
40588b8a9b1dSJustin T. Gibbs 		} else if (sim_bus == 0) {
40598b8a9b1dSJustin T. Gibbs 			/* Unspecified matches bus 0 */
406075f51904SPeter Wemm 			pathid = dunit;
40618b8a9b1dSJustin T. Gibbs 			break;
40628b8a9b1dSJustin T. Gibbs 		} else {
40638b8a9b1dSJustin T. Gibbs 			printf("Ambiguous scbus configuration for %s%d "
40648b8a9b1dSJustin T. Gibbs 			       "bus %d, cannot wire down.  The kernel "
40658b8a9b1dSJustin T. Gibbs 			       "config entry for scbus%d should "
40668b8a9b1dSJustin T. Gibbs 			       "specify a controller bus.\n"
40678b8a9b1dSJustin T. Gibbs 			       "Scbus will be assigned dynamically.\n",
406875f51904SPeter Wemm 			       sim_name, sim_unit, sim_bus, dunit);
40698b8a9b1dSJustin T. Gibbs 			break;
40708b8a9b1dSJustin T. Gibbs 		}
40718b8a9b1dSJustin T. Gibbs 	}
40728b8a9b1dSJustin T. Gibbs 
4073434bbf6eSJustin T. Gibbs 	if (pathid == CAM_XPT_PATH_ID)
4074434bbf6eSJustin T. Gibbs 		pathid = xptnextfreepathid();
40758b8a9b1dSJustin T. Gibbs 	return (pathid);
40768b8a9b1dSJustin T. Gibbs }
40778b8a9b1dSJustin T. Gibbs 
407822c7d606SAlexander Motin static const char *
xpt_async_string(uint32_t async_code)40797af2f2c8SWarner Losh xpt_async_string(uint32_t async_code)
408022c7d606SAlexander Motin {
408122c7d606SAlexander Motin 
408222c7d606SAlexander Motin 	switch (async_code) {
408322c7d606SAlexander Motin 	case AC_BUS_RESET: return ("AC_BUS_RESET");
408422c7d606SAlexander Motin 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
408522c7d606SAlexander Motin 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
408622c7d606SAlexander Motin 	case AC_SENT_BDR: return ("AC_SENT_BDR");
408722c7d606SAlexander Motin 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
408822c7d606SAlexander Motin 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
408922c7d606SAlexander Motin 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
409022c7d606SAlexander Motin 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
409122c7d606SAlexander Motin 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
409222c7d606SAlexander Motin 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
409322c7d606SAlexander Motin 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
409422c7d606SAlexander Motin 	case AC_CONTRACT: return ("AC_CONTRACT");
409522c7d606SAlexander Motin 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
40963631c638SAlexander Motin 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
409722c7d606SAlexander Motin 	}
409822c7d606SAlexander Motin 	return ("AC_UNKNOWN");
409922c7d606SAlexander Motin }
410022c7d606SAlexander Motin 
4101227d67aaSAlexander Motin static int
xpt_async_size(uint32_t async_code)41027af2f2c8SWarner Losh xpt_async_size(uint32_t async_code)
41038b8a9b1dSJustin T. Gibbs {
41048b8a9b1dSJustin T. Gibbs 
4105227d67aaSAlexander Motin 	switch (async_code) {
4106227d67aaSAlexander Motin 	case AC_BUS_RESET: return (0);
4107227d67aaSAlexander Motin 	case AC_UNSOL_RESEL: return (0);
4108227d67aaSAlexander Motin 	case AC_SCSI_AEN: return (0);
4109227d67aaSAlexander Motin 	case AC_SENT_BDR: return (0);
4110227d67aaSAlexander Motin 	case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4111227d67aaSAlexander Motin 	case AC_PATH_DEREGISTERED: return (0);
4112227d67aaSAlexander Motin 	case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4113227d67aaSAlexander Motin 	case AC_LOST_DEVICE: return (0);
4114227d67aaSAlexander Motin 	case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4115227d67aaSAlexander Motin 	case AC_INQ_CHANGED: return (0);
4116227d67aaSAlexander Motin 	case AC_GETDEV_CHANGED: return (0);
4117227d67aaSAlexander Motin 	case AC_CONTRACT: return (sizeof(struct ac_contract));
4118227d67aaSAlexander Motin 	case AC_ADVINFO_CHANGED: return (-1);
4119227d67aaSAlexander Motin 	case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4120227d67aaSAlexander Motin 	}
4121227d67aaSAlexander Motin 	return (0);
4122227d67aaSAlexander Motin }
4123227d67aaSAlexander Motin 
4124227d67aaSAlexander Motin static int
xpt_async_process_dev(struct cam_ed * device,void * arg)4125227d67aaSAlexander Motin xpt_async_process_dev(struct cam_ed *device, void *arg)
4126227d67aaSAlexander Motin {
4127227d67aaSAlexander Motin 	union ccb *ccb = arg;
4128227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4129227d67aaSAlexander Motin 	void *async_arg = ccb->casync.async_arg_ptr;
41307af2f2c8SWarner Losh 	uint32_t async_code = ccb->casync.async_code;
4131d095d6a3SWarner Losh 	bool relock;
4132227d67aaSAlexander Motin 
4133227d67aaSAlexander Motin 	if (path->device != device
4134227d67aaSAlexander Motin 	 && path->device->lun_id != CAM_LUN_WILDCARD
4135227d67aaSAlexander Motin 	 && device->lun_id != CAM_LUN_WILDCARD)
4136227d67aaSAlexander Motin 		return (1);
41378b8a9b1dSJustin T. Gibbs 
4138a5479bc5SJustin T. Gibbs 	/*
4139227d67aaSAlexander Motin 	 * The async callback could free the device.
4140227d67aaSAlexander Motin 	 * If it is a broadcast async, it doesn't hold
4141227d67aaSAlexander Motin 	 * device reference, so take our own reference.
4142a5479bc5SJustin T. Gibbs 	 */
4143227d67aaSAlexander Motin 	xpt_acquire_device(device);
41448b8a9b1dSJustin T. Gibbs 
4145227d67aaSAlexander Motin 	/*
4146227d67aaSAlexander Motin 	 * If async for specific device is to be delivered to
4147227d67aaSAlexander Motin 	 * the wildcard client, take the specific device lock.
4148227d67aaSAlexander Motin 	 * XXX: We may need a way for client to specify it.
4149227d67aaSAlexander Motin 	 */
4150227d67aaSAlexander Motin 	if ((device->lun_id == CAM_LUN_WILDCARD &&
4151227d67aaSAlexander Motin 	     path->device->lun_id != CAM_LUN_WILDCARD) ||
4152227d67aaSAlexander Motin 	    (device->target->target_id == CAM_TARGET_WILDCARD &&
4153227d67aaSAlexander Motin 	     path->target->target_id != CAM_TARGET_WILDCARD) ||
4154227d67aaSAlexander Motin 	    (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4155227d67aaSAlexander Motin 	     path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4156227d67aaSAlexander Motin 		mtx_unlock(&device->device_mtx);
4157227d67aaSAlexander Motin 		xpt_path_lock(path);
4158d095d6a3SWarner Losh 		relock = true;
4159227d67aaSAlexander Motin 	} else
4160d095d6a3SWarner Losh 		relock = false;
4161227d67aaSAlexander Motin 
4162ded2b706SWarner Losh 	(*(device->target->bus->xport->ops->async))(async_code,
4163227d67aaSAlexander Motin 	    device->target->bus, device->target, device, async_arg);
4164227d67aaSAlexander Motin 	xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4165227d67aaSAlexander Motin 
4166227d67aaSAlexander Motin 	if (relock) {
4167227d67aaSAlexander Motin 		xpt_path_unlock(path);
4168227d67aaSAlexander Motin 		mtx_lock(&device->device_mtx);
4169227d67aaSAlexander Motin 	}
4170227d67aaSAlexander Motin 	xpt_release_device(device);
4171227d67aaSAlexander Motin 	return (1);
4172227d67aaSAlexander Motin }
4173227d67aaSAlexander Motin 
4174227d67aaSAlexander Motin static int
xpt_async_process_tgt(struct cam_et * target,void * arg)4175227d67aaSAlexander Motin xpt_async_process_tgt(struct cam_et *target, void *arg)
4176227d67aaSAlexander Motin {
4177227d67aaSAlexander Motin 	union ccb *ccb = arg;
4178227d67aaSAlexander Motin 	struct cam_path *path = ccb->ccb_h.path;
4179227d67aaSAlexander Motin 
4180227d67aaSAlexander Motin 	if (path->target != target
4181227d67aaSAlexander Motin 	 && path->target->target_id != CAM_TARGET_WILDCARD
4182227d67aaSAlexander Motin 	 && target->target_id != CAM_TARGET_WILDCARD)
4183227d67aaSAlexander Motin 		return (1);
4184227d67aaSAlexander Motin 
4185227d67aaSAlexander Motin 	if (ccb->casync.async_code == AC_SENT_BDR) {
4186227d67aaSAlexander Motin 		/* Update our notion of when the last reset occurred */
4187227d67aaSAlexander Motin 		microtime(&target->last_reset);
4188227d67aaSAlexander Motin 	}
4189227d67aaSAlexander Motin 
4190227d67aaSAlexander Motin 	return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4191227d67aaSAlexander Motin }
4192227d67aaSAlexander Motin 
4193227d67aaSAlexander Motin static void
xpt_async_process(struct cam_periph * periph,union ccb * ccb)4194227d67aaSAlexander Motin xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4195227d67aaSAlexander Motin {
4196227d67aaSAlexander Motin 	struct cam_eb *bus;
4197227d67aaSAlexander Motin 	struct cam_path *path;
4198227d67aaSAlexander Motin 	void *async_arg;
41997af2f2c8SWarner Losh 	uint32_t async_code;
4200227d67aaSAlexander Motin 
4201227d67aaSAlexander Motin 	path = ccb->ccb_h.path;
4202227d67aaSAlexander Motin 	async_code = ccb->casync.async_code;
4203227d67aaSAlexander Motin 	async_arg = ccb->casync.async_arg_ptr;
4204227d67aaSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4205227d67aaSAlexander Motin 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
42068b8a9b1dSJustin T. Gibbs 	bus = path->bus;
42078b8a9b1dSJustin T. Gibbs 
42088b8a9b1dSJustin T. Gibbs 	if (async_code == AC_BUS_RESET) {
420987cfaf0eSJustin T. Gibbs 		/* Update our notion of when the last reset occurred */
421087cfaf0eSJustin T. Gibbs 		microtime(&bus->last_reset);
42118b8a9b1dSJustin T. Gibbs 	}
42128b8a9b1dSJustin T. Gibbs 
4213227d67aaSAlexander Motin 	xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4214c8bead2aSJustin T. Gibbs 
4215c8bead2aSJustin T. Gibbs 	/*
4216c8bead2aSJustin T. Gibbs 	 * If this wasn't a fully wildcarded async, tell all
4217c8bead2aSJustin T. Gibbs 	 * clients that want all async events.
4218c8bead2aSJustin T. Gibbs 	 */
4219227d67aaSAlexander Motin 	if (bus != xpt_periph->path->bus) {
4220227d67aaSAlexander Motin 		xpt_path_lock(xpt_periph->path);
4221227d67aaSAlexander Motin 		xpt_async_process_dev(xpt_periph->path->device, ccb);
4222227d67aaSAlexander Motin 		xpt_path_unlock(xpt_periph->path);
4223227d67aaSAlexander Motin 	}
4224227d67aaSAlexander Motin 
4225227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4226227d67aaSAlexander Motin 		xpt_release_devq(path, 1, TRUE);
4227227d67aaSAlexander Motin 	else
4228227d67aaSAlexander Motin 		xpt_release_simq(path->bus->sim, TRUE);
4229227d67aaSAlexander Motin 	if (ccb->casync.async_arg_size > 0)
4230227d67aaSAlexander Motin 		free(async_arg, M_CAMXPT);
4231227d67aaSAlexander Motin 	xpt_free_path(path);
4232227d67aaSAlexander Motin 	xpt_free_ccb(ccb);
42338b8a9b1dSJustin T. Gibbs }
42348b8a9b1dSJustin T. Gibbs 
42358b8a9b1dSJustin T. Gibbs static void
xpt_async_bcast(struct async_list * async_head,uint32_t async_code,struct cam_path * path,void * async_arg)42368b8a9b1dSJustin T. Gibbs xpt_async_bcast(struct async_list *async_head,
42377af2f2c8SWarner Losh 		uint32_t async_code,
42388b8a9b1dSJustin T. Gibbs 		struct cam_path *path, void *async_arg)
42398b8a9b1dSJustin T. Gibbs {
42408b8a9b1dSJustin T. Gibbs 	struct async_node *cur_entry;
4241331d00baSAlexander Motin 	struct mtx *mtx;
42428b8a9b1dSJustin T. Gibbs 
42438b8a9b1dSJustin T. Gibbs 	cur_entry = SLIST_FIRST(async_head);
42448b8a9b1dSJustin T. Gibbs 	while (cur_entry != NULL) {
42458b8a9b1dSJustin T. Gibbs 		struct async_node *next_entry;
42468b8a9b1dSJustin T. Gibbs 		/*
42478b8a9b1dSJustin T. Gibbs 		 * Grab the next list entry before we call the current
42488b8a9b1dSJustin T. Gibbs 		 * entry's callback.  This is because the callback function
42498b8a9b1dSJustin T. Gibbs 		 * can delete its async callback entry.
42508b8a9b1dSJustin T. Gibbs 		 */
42518b8a9b1dSJustin T. Gibbs 		next_entry = SLIST_NEXT(cur_entry, links);
4252227d67aaSAlexander Motin 		if ((cur_entry->event_enable & async_code) != 0) {
4253331d00baSAlexander Motin 			mtx = cur_entry->event_lock ?
4254331d00baSAlexander Motin 			    path->device->sim->mtx : NULL;
4255331d00baSAlexander Motin 			if (mtx)
4256331d00baSAlexander Motin 				mtx_lock(mtx);
42578b8a9b1dSJustin T. Gibbs 			cur_entry->callback(cur_entry->callback_arg,
42588b8a9b1dSJustin T. Gibbs 					    async_code, path,
42598b8a9b1dSJustin T. Gibbs 					    async_arg);
4260331d00baSAlexander Motin 			if (mtx)
4261331d00baSAlexander Motin 				mtx_unlock(mtx);
4262227d67aaSAlexander Motin 		}
42638b8a9b1dSJustin T. Gibbs 		cur_entry = next_entry;
42648b8a9b1dSJustin T. Gibbs 	}
42658b8a9b1dSJustin T. Gibbs }
42668b8a9b1dSJustin T. Gibbs 
4267227d67aaSAlexander Motin void
xpt_async(uint32_t async_code,struct cam_path * path,void * async_arg)42687af2f2c8SWarner Losh xpt_async(uint32_t async_code, struct cam_path *path, void *async_arg)
4269227d67aaSAlexander Motin {
4270227d67aaSAlexander Motin 	union ccb *ccb;
4271227d67aaSAlexander Motin 	int size;
4272227d67aaSAlexander Motin 
4273227d67aaSAlexander Motin 	ccb = xpt_alloc_ccb_nowait();
4274227d67aaSAlexander Motin 	if (ccb == NULL) {
4275227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate CCB to send %s\n",
4276227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4277227d67aaSAlexander Motin 		return;
4278227d67aaSAlexander Motin 	}
4279227d67aaSAlexander Motin 
428050aa1dafSWarner Losh 	if (xpt_clone_path(&ccb->ccb_h.path, path) != 0) {
4281227d67aaSAlexander Motin 		xpt_print(path, "Can't allocate path to send %s\n",
4282227d67aaSAlexander Motin 		    xpt_async_string(async_code));
4283227d67aaSAlexander Motin 		xpt_free_ccb(ccb);
4284227d67aaSAlexander Motin 		return;
4285227d67aaSAlexander Motin 	}
4286227d67aaSAlexander Motin 	ccb->ccb_h.path->periph = NULL;
4287227d67aaSAlexander Motin 	ccb->ccb_h.func_code = XPT_ASYNC;
4288227d67aaSAlexander Motin 	ccb->ccb_h.cbfcnp = xpt_async_process;
4289227d67aaSAlexander Motin 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4290227d67aaSAlexander Motin 	ccb->casync.async_code = async_code;
4291227d67aaSAlexander Motin 	ccb->casync.async_arg_size = 0;
4292227d67aaSAlexander Motin 	size = xpt_async_size(async_code);
429369be012fSWarner Losh 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
429469be012fSWarner Losh 	    ("xpt_async: func %#x %s aync_code %d %s\n",
429569be012fSWarner Losh 		ccb->ccb_h.func_code,
429669be012fSWarner Losh 		xpt_action_name(ccb->ccb_h.func_code),
429769be012fSWarner Losh 		async_code,
429869be012fSWarner Losh 		xpt_async_string(async_code)));
4299227d67aaSAlexander Motin 	if (size > 0 && async_arg != NULL) {
4300227d67aaSAlexander Motin 		ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4301227d67aaSAlexander Motin 		if (ccb->casync.async_arg_ptr == NULL) {
4302227d67aaSAlexander Motin 			xpt_print(path, "Can't allocate argument to send %s\n",
4303227d67aaSAlexander Motin 			    xpt_async_string(async_code));
4304227d67aaSAlexander Motin 			xpt_free_path(ccb->ccb_h.path);
4305227d67aaSAlexander Motin 			xpt_free_ccb(ccb);
4306227d67aaSAlexander Motin 			return;
4307227d67aaSAlexander Motin 		}
4308227d67aaSAlexander Motin 		memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4309227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4310738fd166SAlan Somers 	} else if (size < 0) {
4311738fd166SAlan Somers 		ccb->casync.async_arg_ptr = async_arg;
4312227d67aaSAlexander Motin 		ccb->casync.async_arg_size = size;
4313738fd166SAlan Somers 	}
4314227d67aaSAlexander Motin 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4315227d67aaSAlexander Motin 		xpt_freeze_devq(path, 1);
4316227d67aaSAlexander Motin 	else
4317227d67aaSAlexander Motin 		xpt_freeze_simq(path->bus->sim, 1);
431856eccd2dSWarner Losh 	xpt_action(ccb);
4319227d67aaSAlexander Motin }
4320227d67aaSAlexander Motin 
43212f22d08dSJustin T. Gibbs static void
xpt_dev_async_default(uint32_t async_code,struct cam_eb * bus,struct cam_et * target,struct cam_ed * device,void * async_arg)43227af2f2c8SWarner Losh xpt_dev_async_default(uint32_t async_code, struct cam_eb *bus,
432352c9ce25SScott Long 		      struct cam_et *target, struct cam_ed *device,
432452c9ce25SScott Long 		      void *async_arg)
43252f22d08dSJustin T. Gibbs {
4326227d67aaSAlexander Motin 
4327227d67aaSAlexander Motin 	/*
4328227d67aaSAlexander Motin 	 * We only need to handle events for real devices.
4329227d67aaSAlexander Motin 	 */
4330227d67aaSAlexander Motin 	if (target->target_id == CAM_TARGET_WILDCARD
4331227d67aaSAlexander Motin 	 || device->lun_id == CAM_LUN_WILDCARD)
4332227d67aaSAlexander Motin 		return;
4333227d67aaSAlexander Motin 
4334b882a6d3SMatt Jacob 	printf("%s called\n", __func__);
43352f22d08dSJustin T. Gibbs }
43362f22d08dSJustin T. Gibbs 
4337daa5487fSAlexander Motin static uint32_t
xpt_freeze_devq_device(struct cam_ed * dev,u_int count)4338daa5487fSAlexander Motin xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4339daa5487fSAlexander Motin {
4340daa5487fSAlexander Motin 	struct cam_devq	*devq;
4341daa5487fSAlexander Motin 	uint32_t freeze;
4342daa5487fSAlexander Motin 
4343daa5487fSAlexander Motin 	devq = dev->sim->devq;
4344daa5487fSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4345daa5487fSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4346daa5487fSAlexander Motin 	    ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4347daa5487fSAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4348daa5487fSAlexander Motin 	freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4349daa5487fSAlexander Motin 	/* Remove frozen device from sendq. */
4350daa5487fSAlexander Motin 	if (device_is_queued(dev))
4351daa5487fSAlexander Motin 		camq_remove(&devq->send_queue, dev->devq_entry.index);
4352daa5487fSAlexander Motin 	return (freeze);
4353daa5487fSAlexander Motin }
4354daa5487fSAlexander Motin 
43557af2f2c8SWarner Losh uint32_t
xpt_freeze_devq(struct cam_path * path,u_int count)4356cccf4220SAlexander Motin xpt_freeze_devq(struct cam_path *path, u_int count)
435783c5d981SAlexander Motin {
435883c5d981SAlexander Motin 	struct cam_ed	*dev = path->device;
4359227d67aaSAlexander Motin 	struct cam_devq	*devq;
4360227d67aaSAlexander Motin 	uint32_t	 freeze;
436183c5d981SAlexander Motin 
4362227d67aaSAlexander Motin 	devq = dev->sim->devq;
4363227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4364daa5487fSAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4365daa5487fSAlexander Motin 	freeze = xpt_freeze_devq_device(dev, count);
4366227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4367227d67aaSAlexander Motin 	return (freeze);
43688b8a9b1dSJustin T. Gibbs }
43698b8a9b1dSJustin T. Gibbs 
43707af2f2c8SWarner Losh uint32_t
xpt_freeze_simq(struct cam_sim * sim,u_int count)43718b8a9b1dSJustin T. Gibbs xpt_freeze_simq(struct cam_sim *sim, u_int count)
43728b8a9b1dSJustin T. Gibbs {
4373227d67aaSAlexander Motin 	struct cam_devq	*devq;
4374227d67aaSAlexander Motin 	uint32_t	 freeze;
4375ec700f26SAlexander Motin 
4376227d67aaSAlexander Motin 	devq = sim->devq;
4377227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4378227d67aaSAlexander Motin 	freeze = (devq->send_queue.qfrozen_cnt += count);
4379227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4380227d67aaSAlexander Motin 	return (freeze);
43818b8a9b1dSJustin T. Gibbs }
43828b8a9b1dSJustin T. Gibbs 
43838b8a9b1dSJustin T. Gibbs static void
xpt_release_devq_timeout(void * arg)43848b8a9b1dSJustin T. Gibbs xpt_release_devq_timeout(void *arg)
43858b8a9b1dSJustin T. Gibbs {
4386227d67aaSAlexander Motin 	struct cam_ed *dev;
4387227d67aaSAlexander Motin 	struct cam_devq *devq;
43888b8a9b1dSJustin T. Gibbs 
4389227d67aaSAlexander Motin 	dev = (struct cam_ed *)arg;
4390227d67aaSAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4391227d67aaSAlexander Motin 	devq = dev->sim->devq;
4392227d67aaSAlexander Motin 	mtx_assert(&devq->send_mtx, MA_OWNED);
4393227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4394227d67aaSAlexander Motin 		xpt_run_devq(devq);
43958b8a9b1dSJustin T. Gibbs }
43968b8a9b1dSJustin T. Gibbs 
43978b8a9b1dSJustin T. Gibbs void
xpt_release_devq(struct cam_path * path,u_int count,int run_queue)43982cefde5fSJustin T. Gibbs xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
43992cefde5fSJustin T. Gibbs {
4400227d67aaSAlexander Motin 	struct cam_ed *dev;
4401227d67aaSAlexander Motin 	struct cam_devq *devq;
440268153f43SScott Long 
44030d4f3c31SAlexander Motin 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
44040d4f3c31SAlexander Motin 	    count, run_queue));
4405227d67aaSAlexander Motin 	dev = path->device;
4406227d67aaSAlexander Motin 	devq = dev->sim->devq;
4407227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4408227d67aaSAlexander Motin 	if (xpt_release_devq_device(dev, count, run_queue))
4409227d67aaSAlexander Motin 		xpt_run_devq(dev->sim->devq);
4410227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
441183c5d981SAlexander Motin }
441283c5d981SAlexander Motin 
4413227d67aaSAlexander Motin static int
xpt_release_devq_device(struct cam_ed * dev,u_int count,int run_queue)4414cccf4220SAlexander Motin xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
44158b8a9b1dSJustin T. Gibbs {
44168b8a9b1dSJustin T. Gibbs 
4417227d67aaSAlexander Motin 	mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
44180d4f3c31SAlexander Motin 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
44190d4f3c31SAlexander Motin 	    ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
44200d4f3c31SAlexander Motin 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4421cccf4220SAlexander Motin 	if (count > dev->ccbq.queue.qfrozen_cnt) {
442283c5d981SAlexander Motin #ifdef INVARIANTS
4423cccf4220SAlexander Motin 		printf("xpt_release_devq(): requested %u > present %u\n",
4424cccf4220SAlexander Motin 		    count, dev->ccbq.queue.qfrozen_cnt);
442583c5d981SAlexander Motin #endif
4426cccf4220SAlexander Motin 		count = dev->ccbq.queue.qfrozen_cnt;
442783c5d981SAlexander Motin 	}
4428cccf4220SAlexander Motin 	dev->ccbq.queue.qfrozen_cnt -= count;
4429cccf4220SAlexander Motin 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
44308b8a9b1dSJustin T. Gibbs 		/*
44318b8a9b1dSJustin T. Gibbs 		 * No longer need to wait for a successful
44328b8a9b1dSJustin T. Gibbs 		 * command completion.
44338b8a9b1dSJustin T. Gibbs 		 */
44348b8a9b1dSJustin T. Gibbs 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
44358b8a9b1dSJustin T. Gibbs 		/*
44368b8a9b1dSJustin T. Gibbs 		 * Remove any timeouts that might be scheduled
44378b8a9b1dSJustin T. Gibbs 		 * to release this queue.
44388b8a9b1dSJustin T. Gibbs 		 */
44398b8a9b1dSJustin T. Gibbs 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
44402b83592fSScott Long 			callout_stop(&dev->callout);
44418b8a9b1dSJustin T. Gibbs 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
44428b8a9b1dSJustin T. Gibbs 		}
44438b8a9b1dSJustin T. Gibbs 		/*
44448b8a9b1dSJustin T. Gibbs 		 * Now that we are unfrozen schedule the
44458b8a9b1dSJustin T. Gibbs 		 * device so any pending transactions are
44468b8a9b1dSJustin T. Gibbs 		 * run.
44478b8a9b1dSJustin T. Gibbs 		 */
4448227d67aaSAlexander Motin 		xpt_schedule_devq(dev->sim->devq, dev);
4449227d67aaSAlexander Motin 	} else
4450227d67aaSAlexander Motin 		run_queue = 0;
4451227d67aaSAlexander Motin 	return (run_queue);
445283c5d981SAlexander Motin }
44538b8a9b1dSJustin T. Gibbs 
44548b8a9b1dSJustin T. Gibbs void
xpt_release_simq(struct cam_sim * sim,int run_queue)44558b8a9b1dSJustin T. Gibbs xpt_release_simq(struct cam_sim *sim, int run_queue)
44568b8a9b1dSJustin T. Gibbs {
4457227d67aaSAlexander Motin 	struct cam_devq	*devq;
44588b8a9b1dSJustin T. Gibbs 
4459227d67aaSAlexander Motin 	devq = sim->devq;
4460227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4461227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt <= 0) {
446283c5d981SAlexander Motin #ifdef INVARIANTS
446383c5d981SAlexander Motin 		printf("xpt_release_simq: requested 1 > present %u\n",
4464227d67aaSAlexander Motin 		    devq->send_queue.qfrozen_cnt);
446583c5d981SAlexander Motin #endif
446683c5d981SAlexander Motin 	} else
4467227d67aaSAlexander Motin 		devq->send_queue.qfrozen_cnt--;
4468227d67aaSAlexander Motin 	if (devq->send_queue.qfrozen_cnt == 0) {
44698b8a9b1dSJustin T. Gibbs 		if (run_queue) {
44708b8a9b1dSJustin T. Gibbs 			/*
44718b8a9b1dSJustin T. Gibbs 			 * Now that we are unfrozen run the send queue.
44728b8a9b1dSJustin T. Gibbs 			 */
4473cccf4220SAlexander Motin 			xpt_run_devq(sim->devq);
447477dc25ccSScott Long 		}
447577dc25ccSScott Long 	}
4476227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
44778b8a9b1dSJustin T. Gibbs }
44788b8a9b1dSJustin T. Gibbs 
44798b8a9b1dSJustin T. Gibbs void
xpt_done(union ccb * done_ccb)4480a5479bc5SJustin T. Gibbs xpt_done(union ccb *done_ccb)
44818b8a9b1dSJustin T. Gibbs {
4482227d67aaSAlexander Motin 	struct cam_doneq *queue;
4483227d67aaSAlexander Motin 	int	run, hash;
44848b8a9b1dSJustin T. Gibbs 
44858532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
44868532d381SConrad Meyer 	if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
44878532d381SConrad Meyer 	    done_ccb->csio.bio != NULL)
44888532d381SConrad Meyer 		biotrack(done_ccb->csio.bio, __func__);
44898532d381SConrad Meyer #endif
44908532d381SConrad Meyer 
449169be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
449269be012fSWarner Losh 	    ("xpt_done: func= %#x %s status %#x\n",
449369be012fSWarner Losh 		done_ccb->ccb_h.func_code,
449469be012fSWarner Losh 		xpt_action_name(done_ccb->ccb_h.func_code),
449569be012fSWarner Losh 		done_ccb->ccb_h.status));
4496227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4497227d67aaSAlexander Motin 		return;
4498227d67aaSAlexander Motin 
4499a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4500e4c9cba7SWarner Losh 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
45014afa62beSWarner Losh 	done_ccb->ccb_h.status |= CAM_QOS_VALID;
4502466d0a25SAlexander Motin 	hash = (u_int)(done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4503227d67aaSAlexander Motin 	    done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4504227d67aaSAlexander Motin 	queue = &cam_doneqs[hash];
4505227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
4506227d67aaSAlexander Motin 	run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4507227d67aaSAlexander Motin 	STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
45088b8a9b1dSJustin T. Gibbs 	done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4509227d67aaSAlexander Motin 	mtx_unlock(&queue->cam_doneq_mtx);
4510ed8ef7aeSMark Johnston 	if (run && !dumping)
4511227d67aaSAlexander Motin 		wakeup(&queue->cam_doneq);
45128b8a9b1dSJustin T. Gibbs }
45138b8a9b1dSJustin T. Gibbs 
4514711f6613SAlexander Motin void
xpt_done_direct(union ccb * done_ccb)4515227d67aaSAlexander Motin xpt_done_direct(union ccb *done_ccb)
4516711f6613SAlexander Motin {
4517711f6613SAlexander Motin 
451869be012fSWarner Losh 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
451969be012fSWarner Losh 	    ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4520227d67aaSAlexander Motin 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4521227d67aaSAlexander Motin 		return;
4522711f6613SAlexander Motin 
4523a6e0c5daSWarner Losh 	/* Store the time the ccb was in the sim */
4524e4c9cba7SWarner Losh 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4525abea0c6bSWarner Losh 	done_ccb->ccb_h.status |= CAM_QOS_VALID;
4526227d67aaSAlexander Motin 	xpt_done_process(&done_ccb->ccb_h);
4527711f6613SAlexander Motin }
4528711f6613SAlexander Motin 
45298b8a9b1dSJustin T. Gibbs union ccb *
xpt_alloc_ccb(void)45309982b3eeSConrad Meyer xpt_alloc_ccb(void)
45318b8a9b1dSJustin T. Gibbs {
45328b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
45338b8a9b1dSJustin T. Gibbs 
4534596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4535362abc44STai-hwa Liang 	return (new_ccb);
4536362abc44STai-hwa Liang }
4537362abc44STai-hwa Liang 
4538362abc44STai-hwa Liang union ccb *
xpt_alloc_ccb_nowait(void)45399982b3eeSConrad Meyer xpt_alloc_ccb_nowait(void)
4540362abc44STai-hwa Liang {
4541362abc44STai-hwa Liang 	union ccb *new_ccb;
4542362abc44STai-hwa Liang 
4543596ee08fSAlexander Motin 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
45448b8a9b1dSJustin T. Gibbs 	return (new_ccb);
45458b8a9b1dSJustin T. Gibbs }
45468b8a9b1dSJustin T. Gibbs 
45478b8a9b1dSJustin T. Gibbs void
xpt_free_ccb(union ccb * free_ccb)45488b8a9b1dSJustin T. Gibbs xpt_free_ccb(union ccb *free_ccb)
45498b8a9b1dSJustin T. Gibbs {
45503394d423SEdward Tomasz Napierala 	struct cam_periph *periph;
45513394d423SEdward Tomasz Napierala 
45523394d423SEdward Tomasz Napierala 	if (free_ccb->ccb_h.alloc_flags & CAM_CCB_FROM_UMA) {
45533394d423SEdward Tomasz Napierala 		/*
45543394d423SEdward Tomasz Napierala 		 * Looks like a CCB allocated from a periph UMA zone.
45553394d423SEdward Tomasz Napierala 		 */
45563394d423SEdward Tomasz Napierala 		periph = free_ccb->ccb_h.path->periph;
45573394d423SEdward Tomasz Napierala 		uma_zfree(periph->ccb_zone, free_ccb);
45583394d423SEdward Tomasz Napierala 	} else {
4559596ee08fSAlexander Motin 		free(free_ccb, M_CAMCCB);
45608b8a9b1dSJustin T. Gibbs 	}
45613394d423SEdward Tomasz Napierala }
45628b8a9b1dSJustin T. Gibbs 
45638b8a9b1dSJustin T. Gibbs /* Private XPT functions */
45648b8a9b1dSJustin T. Gibbs 
45658b8a9b1dSJustin T. Gibbs /*
45668b8a9b1dSJustin T. Gibbs  * Get a CAM control block for the caller. Charge the structure to the device
4567227d67aaSAlexander Motin  * referenced by the path.  If we don't have sufficient resources to allocate
4568227d67aaSAlexander Motin  * more ccbs, we return NULL.
45698b8a9b1dSJustin T. Gibbs  */
45708b8a9b1dSJustin T. Gibbs static union ccb *
xpt_get_ccb_nowait(struct cam_periph * periph)4571227d67aaSAlexander Motin xpt_get_ccb_nowait(struct cam_periph *periph)
45728b8a9b1dSJustin T. Gibbs {
45738b8a9b1dSJustin T. Gibbs 	union ccb *new_ccb;
45743394d423SEdward Tomasz Napierala 	int alloc_flags;
45758b8a9b1dSJustin T. Gibbs 
45763394d423SEdward Tomasz Napierala 	if (periph->ccb_zone != NULL) {
45773394d423SEdward Tomasz Napierala 		alloc_flags = CAM_CCB_FROM_UMA;
45783394d423SEdward Tomasz Napierala 		new_ccb = uma_zalloc(periph->ccb_zone, M_ZERO|M_NOWAIT);
45793394d423SEdward Tomasz Napierala 	} else {
45803394d423SEdward Tomasz Napierala 		alloc_flags = 0;
4581d3995fddSBenno Rice 		new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
45823394d423SEdward Tomasz Napierala 	}
4583227d67aaSAlexander Motin 	if (new_ccb == NULL)
45848b8a9b1dSJustin T. Gibbs 		return (NULL);
45853394d423SEdward Tomasz Napierala 	new_ccb->ccb_h.alloc_flags = alloc_flags;
4586227d67aaSAlexander Motin 	periph->periph_allocated++;
4587227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
45888b8a9b1dSJustin T. Gibbs 	return (new_ccb);
45898b8a9b1dSJustin T. Gibbs }
45908b8a9b1dSJustin T. Gibbs 
4591227d67aaSAlexander Motin static union ccb *
xpt_get_ccb(struct cam_periph * periph)4592227d67aaSAlexander Motin xpt_get_ccb(struct cam_periph *periph)
4593227d67aaSAlexander Motin {
4594227d67aaSAlexander Motin 	union ccb *new_ccb;
45953394d423SEdward Tomasz Napierala 	int alloc_flags;
4596227d67aaSAlexander Motin 
4597227d67aaSAlexander Motin 	cam_periph_unlock(periph);
45983394d423SEdward Tomasz Napierala 	if (periph->ccb_zone != NULL) {
45993394d423SEdward Tomasz Napierala 		alloc_flags = CAM_CCB_FROM_UMA;
46003394d423SEdward Tomasz Napierala 		new_ccb = uma_zalloc(periph->ccb_zone, M_ZERO|M_WAITOK);
46013394d423SEdward Tomasz Napierala 	} else {
46023394d423SEdward Tomasz Napierala 		alloc_flags = 0;
4603d3995fddSBenno Rice 		new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
46043394d423SEdward Tomasz Napierala 	}
46053394d423SEdward Tomasz Napierala 	new_ccb->ccb_h.alloc_flags = alloc_flags;
4606227d67aaSAlexander Motin 	cam_periph_lock(periph);
4607227d67aaSAlexander Motin 	periph->periph_allocated++;
4608227d67aaSAlexander Motin 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4609227d67aaSAlexander Motin 	return (new_ccb);
4610227d67aaSAlexander Motin }
4611227d67aaSAlexander Motin 
4612227d67aaSAlexander Motin union ccb *
cam_periph_getccb(struct cam_periph * periph,uint32_t priority)46137af2f2c8SWarner Losh cam_periph_getccb(struct cam_periph *periph, uint32_t priority)
4614227d67aaSAlexander Motin {
4615227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
4616227d67aaSAlexander Motin 
4617227d67aaSAlexander Motin 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4618227d67aaSAlexander Motin 	cam_periph_assert(periph, MA_OWNED);
4619227d67aaSAlexander Motin 	while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4620227d67aaSAlexander Motin 	    ccb_h->pinfo.priority != priority) {
4621227d67aaSAlexander Motin 		if (priority < periph->immediate_priority) {
4622227d67aaSAlexander Motin 			periph->immediate_priority = priority;
4623227d67aaSAlexander Motin 			xpt_run_allocq(periph, 0);
4624227d67aaSAlexander Motin 		} else
4625227d67aaSAlexander Motin 			cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4626227d67aaSAlexander Motin 			    "cgticb", 0);
4627227d67aaSAlexander Motin 	}
4628227d67aaSAlexander Motin 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4629227d67aaSAlexander Motin 	return ((union ccb *)ccb_h);
4630227d67aaSAlexander Motin }
4631227d67aaSAlexander Motin 
4632227d67aaSAlexander Motin static void
xpt_acquire_bus(struct cam_eb * bus)4633227d67aaSAlexander Motin xpt_acquire_bus(struct cam_eb *bus)
4634227d67aaSAlexander Motin {
4635227d67aaSAlexander Motin 
4636227d67aaSAlexander Motin 	xpt_lock_buses();
4637227d67aaSAlexander Motin 	bus->refcount++;
4638227d67aaSAlexander Motin 	xpt_unlock_buses();
4639227d67aaSAlexander Motin }
4640227d67aaSAlexander Motin 
4641a5479bc5SJustin T. Gibbs static void
xpt_release_bus(struct cam_eb * bus)4642a5479bc5SJustin T. Gibbs xpt_release_bus(struct cam_eb *bus)
4643a5479bc5SJustin T. Gibbs {
4644a5479bc5SJustin T. Gibbs 
46459a7c2696SAlexander Motin 	xpt_lock_buses();
464615975b7bSMatt Jacob 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4647dcdf6e74SAlexander Motin 	if (--bus->refcount > 0) {
4648dcdf6e74SAlexander Motin 		xpt_unlock_buses();
4649dcdf6e74SAlexander Motin 		return;
4650dcdf6e74SAlexander Motin 	}
46512b83592fSScott Long 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
46522b83592fSScott Long 	xsoftc.bus_generation++;
46539a7c2696SAlexander Motin 	xpt_unlock_buses();
4654227d67aaSAlexander Motin 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4655227d67aaSAlexander Motin 	    ("destroying bus, but target list is not empty"));
4656fa6099fdSEdward Tomasz Napierala 	cam_sim_release(bus->sim);
4657227d67aaSAlexander Motin 	mtx_destroy(&bus->eb_mtx);
4658362abc44STai-hwa Liang 	free(bus, M_CAMXPT);
4659a5479bc5SJustin T. Gibbs }
46608b8a9b1dSJustin T. Gibbs 
46618b8a9b1dSJustin T. Gibbs static struct cam_et *
xpt_alloc_target(struct cam_eb * bus,target_id_t target_id)46628b8a9b1dSJustin T. Gibbs xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
46638b8a9b1dSJustin T. Gibbs {
4664dcdf6e74SAlexander Motin 	struct cam_et *cur_target, *target;
46658b8a9b1dSJustin T. Gibbs 
4666227d67aaSAlexander Motin 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4667227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
46683501942bSJustin T. Gibbs 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
46693501942bSJustin T. Gibbs 					 M_NOWAIT|M_ZERO);
4670dcdf6e74SAlexander Motin 	if (target == NULL)
4671dcdf6e74SAlexander Motin 		return (NULL);
46728b8a9b1dSJustin T. Gibbs 
4673434bbf6eSJustin T. Gibbs 	TAILQ_INIT(&target->ed_entries);
46748b8a9b1dSJustin T. Gibbs 	target->bus = bus;
46758b8a9b1dSJustin T. Gibbs 	target->target_id = target_id;
46768b8a9b1dSJustin T. Gibbs 	target->refcount = 1;
4677434bbf6eSJustin T. Gibbs 	target->generation = 0;
467882b361b1SMatt Jacob 	target->luns = NULL;
4679227d67aaSAlexander Motin 	mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4680434bbf6eSJustin T. Gibbs 	timevalclear(&target->last_reset);
4681a5479bc5SJustin T. Gibbs 	/*
4682a5479bc5SJustin T. Gibbs 	 * Hold a reference to our parent bus so it
4683a5479bc5SJustin T. Gibbs 	 * will not go away before we do.
4684a5479bc5SJustin T. Gibbs 	 */
4685a5479bc5SJustin T. Gibbs 	bus->refcount++;
46868b8a9b1dSJustin T. Gibbs 
46878b8a9b1dSJustin T. Gibbs 	/* Insertion sort into our bus's target list */
46888b8a9b1dSJustin T. Gibbs 	cur_target = TAILQ_FIRST(&bus->et_entries);
46898b8a9b1dSJustin T. Gibbs 	while (cur_target != NULL && cur_target->target_id < target_id)
46908b8a9b1dSJustin T. Gibbs 		cur_target = TAILQ_NEXT(cur_target, links);
46918b8a9b1dSJustin T. Gibbs 	if (cur_target != NULL) {
46928b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_BEFORE(cur_target, target, links);
46938b8a9b1dSJustin T. Gibbs 	} else {
46948b8a9b1dSJustin T. Gibbs 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
46958b8a9b1dSJustin T. Gibbs 	}
4696a5479bc5SJustin T. Gibbs 	bus->generation++;
46978b8a9b1dSJustin T. Gibbs 	return (target);
46988b8a9b1dSJustin T. Gibbs }
46998b8a9b1dSJustin T. Gibbs 
4700a5479bc5SJustin T. Gibbs static void
xpt_acquire_target(struct cam_et * target)4701227d67aaSAlexander Motin xpt_acquire_target(struct cam_et *target)
4702227d67aaSAlexander Motin {
4703227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4704227d67aaSAlexander Motin 
4705227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4706227d67aaSAlexander Motin 	target->refcount++;
4707227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4708227d67aaSAlexander Motin }
4709227d67aaSAlexander Motin 
4710227d67aaSAlexander Motin static void
xpt_release_target(struct cam_et * target)4711f98d7a47SAlexander Motin xpt_release_target(struct cam_et *target)
47128b8a9b1dSJustin T. Gibbs {
4713227d67aaSAlexander Motin 	struct cam_eb *bus = target->bus;
4714a5479bc5SJustin T. Gibbs 
4715227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4716227d67aaSAlexander Motin 	if (--target->refcount > 0) {
4717227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4718dcdf6e74SAlexander Motin 		return;
4719227d67aaSAlexander Motin 	}
4720227d67aaSAlexander Motin 	TAILQ_REMOVE(&bus->et_entries, target, links);
4721227d67aaSAlexander Motin 	bus->generation++;
4722227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4723dcdf6e74SAlexander Motin 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4724227d67aaSAlexander Motin 	    ("destroying target, but device list is not empty"));
4725227d67aaSAlexander Motin 	xpt_release_bus(bus);
4726227d67aaSAlexander Motin 	mtx_destroy(&target->luns_mtx);
472782b361b1SMatt Jacob 	if (target->luns)
472882b361b1SMatt Jacob 		free(target->luns, M_CAMXPT);
4729362abc44STai-hwa Liang 	free(target, M_CAMXPT);
473077dc25ccSScott Long }
47318b8a9b1dSJustin T. Gibbs 
47328b8a9b1dSJustin T. Gibbs static struct cam_ed *
xpt_alloc_device_default(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)473352c9ce25SScott Long xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
473452c9ce25SScott Long 			 lun_id_t lun_id)
473552c9ce25SScott Long {
4736dcdf6e74SAlexander Motin 	struct cam_ed *device;
473752c9ce25SScott Long 
473852c9ce25SScott Long 	device = xpt_alloc_device(bus, target, lun_id);
473952c9ce25SScott Long 	if (device == NULL)
474052c9ce25SScott Long 		return (NULL);
474152c9ce25SScott Long 
474252c9ce25SScott Long 	device->mintags = 1;
474352c9ce25SScott Long 	device->maxtags = 1;
474452c9ce25SScott Long 	return (device);
474552c9ce25SScott Long }
474652c9ce25SScott Long 
4747227d67aaSAlexander Motin static void
xpt_destroy_device(void * context,int pending)4748227d67aaSAlexander Motin xpt_destroy_device(void *context, int pending)
4749227d67aaSAlexander Motin {
4750227d67aaSAlexander Motin 	struct cam_ed	*device = context;
4751227d67aaSAlexander Motin 
4752227d67aaSAlexander Motin 	mtx_lock(&device->device_mtx);
4753227d67aaSAlexander Motin 	mtx_destroy(&device->device_mtx);
4754227d67aaSAlexander Motin 	free(device, M_CAMDEV);
4755227d67aaSAlexander Motin }
4756227d67aaSAlexander Motin 
475752c9ce25SScott Long struct cam_ed *
xpt_alloc_device(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)47588b8a9b1dSJustin T. Gibbs xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
47598b8a9b1dSJustin T. Gibbs {
4760dcdf6e74SAlexander Motin 	struct cam_ed	*cur_device, *device;
47618b8a9b1dSJustin T. Gibbs 	struct cam_devq	*devq;
4762a5479bc5SJustin T. Gibbs 	cam_status status;
47638b8a9b1dSJustin T. Gibbs 
4764227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
47658b8a9b1dSJustin T. Gibbs 	/* Make space for us in the device queue on our bus */
47668b8a9b1dSJustin T. Gibbs 	devq = bus->sim->devq;
4767227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4768cccf4220SAlexander Motin 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4769227d67aaSAlexander Motin 	mtx_unlock(&devq->send_mtx);
4770dcdf6e74SAlexander Motin 	if (status != CAM_REQ_CMP)
4771dcdf6e74SAlexander Motin 		return (NULL);
47728b8a9b1dSJustin T. Gibbs 
47738b8a9b1dSJustin T. Gibbs 	device = (struct cam_ed *)malloc(sizeof(*device),
4774596ee08fSAlexander Motin 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4775dcdf6e74SAlexander Motin 	if (device == NULL)
4776dcdf6e74SAlexander Motin 		return (NULL);
47778b8a9b1dSJustin T. Gibbs 
4778227d67aaSAlexander Motin 	cam_init_pinfo(&device->devq_entry);
47798b8a9b1dSJustin T. Gibbs 	device->target = target;
47808b8a9b1dSJustin T. Gibbs 	device->lun_id = lun_id;
47812b83592fSScott Long 	device->sim = bus->sim;
47828b8a9b1dSJustin T. Gibbs 	if (cam_ccbq_init(&device->ccbq,
47838b8a9b1dSJustin T. Gibbs 			  bus->sim->max_dev_openings) != 0) {
4784596ee08fSAlexander Motin 		free(device, M_CAMDEV);
47858b8a9b1dSJustin T. Gibbs 		return (NULL);
47868b8a9b1dSJustin T. Gibbs 	}
4787434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->asyncs);
4788434bbf6eSJustin T. Gibbs 	SLIST_INIT(&device->periphs);
4789434bbf6eSJustin T. Gibbs 	device->generation = 0;
4790434bbf6eSJustin T. Gibbs 	device->flags = CAM_DEV_UNCONFIGURED;
4791434bbf6eSJustin T. Gibbs 	device->tag_delay_count = 0;
4792df8f9080SJustin T. Gibbs 	device->tag_saved_openings = 0;
4793434bbf6eSJustin T. Gibbs 	device->refcount = 1;
4794227d67aaSAlexander Motin 	mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4795227d67aaSAlexander Motin 	callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4796227d67aaSAlexander Motin 	TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4797227d67aaSAlexander Motin 	/*
4798227d67aaSAlexander Motin 	 * Hold a reference to our parent bus so it
4799227d67aaSAlexander Motin 	 * will not go away before we do.
4800227d67aaSAlexander Motin 	 */
4801227d67aaSAlexander Motin 	target->refcount++;
4802434bbf6eSJustin T. Gibbs 
4803dcdf6e74SAlexander Motin 	cur_device = TAILQ_FIRST(&target->ed_entries);
4804dcdf6e74SAlexander Motin 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4805dcdf6e74SAlexander Motin 		cur_device = TAILQ_NEXT(cur_device, links);
4806dcdf6e74SAlexander Motin 	if (cur_device != NULL)
4807dcdf6e74SAlexander Motin 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4808dcdf6e74SAlexander Motin 	else
4809dcdf6e74SAlexander Motin 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4810dcdf6e74SAlexander Motin 	target->generation++;
48118b8a9b1dSJustin T. Gibbs 	return (device);
48128b8a9b1dSJustin T. Gibbs }
48138b8a9b1dSJustin T. Gibbs 
4814f98d7a47SAlexander Motin void
xpt_acquire_device(struct cam_ed * device)4815f98d7a47SAlexander Motin xpt_acquire_device(struct cam_ed *device)
48168b8a9b1dSJustin T. Gibbs {
4817227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
48188b8a9b1dSJustin T. Gibbs 
4819227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4820f98d7a47SAlexander Motin 	device->refcount++;
4821227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4822f98d7a47SAlexander Motin }
4823f98d7a47SAlexander Motin 
4824f98d7a47SAlexander Motin void
xpt_release_device(struct cam_ed * device)4825f98d7a47SAlexander Motin xpt_release_device(struct cam_ed *device)
4826f98d7a47SAlexander Motin {
4827227d67aaSAlexander Motin 	struct cam_eb *bus = device->target->bus;
48288b8a9b1dSJustin T. Gibbs 	struct cam_devq *devq;
48298b8a9b1dSJustin T. Gibbs 
4830227d67aaSAlexander Motin 	mtx_lock(&bus->eb_mtx);
4831227d67aaSAlexander Motin 	if (--device->refcount > 0) {
4832227d67aaSAlexander Motin 		mtx_unlock(&bus->eb_mtx);
4833dcdf6e74SAlexander Motin 		return;
4834227d67aaSAlexander Motin 	}
4835227d67aaSAlexander Motin 
4836227d67aaSAlexander Motin 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4837227d67aaSAlexander Motin 	device->target->generation++;
4838227d67aaSAlexander Motin 	mtx_unlock(&bus->eb_mtx);
4839227d67aaSAlexander Motin 
4840227d67aaSAlexander Motin 	/* Release our slot in the devq */
4841227d67aaSAlexander Motin 	devq = bus->sim->devq;
4842227d67aaSAlexander Motin 	mtx_lock(&devq->send_mtx);
4843227d67aaSAlexander Motin 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
4844dcdf6e74SAlexander Motin 
4845dcdf6e74SAlexander Motin 	KASSERT(SLIST_EMPTY(&device->periphs),
4846227d67aaSAlexander Motin 	    ("destroying device, but periphs list is not empty"));
4847227d67aaSAlexander Motin 	KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4848227d67aaSAlexander Motin 	    ("destroying device while still queued for ccbs"));
48492cefde5fSJustin T. Gibbs 
485003caca36SKenneth D. Merry 	/* The send_mtx must be held when accessing the callout */
48512cefde5fSJustin T. Gibbs 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
48522b83592fSScott Long 		callout_stop(&device->callout);
48532cefde5fSJustin T. Gibbs 
485403caca36SKenneth D. Merry 	mtx_unlock(&devq->send_mtx);
485503caca36SKenneth D. Merry 
4856227d67aaSAlexander Motin 	xpt_release_target(device->target);
4857227d67aaSAlexander Motin 
485820a7933fSAlexander Motin 	cam_ccbq_fini(&device->ccbq);
4859e6bd5983SKenneth D. Merry 	/*
4860e6bd5983SKenneth D. Merry 	 * Free allocated memory.  free(9) does nothing if the
4861e6bd5983SKenneth D. Merry 	 * supplied pointer is NULL, so it is safe to call without
4862e6bd5983SKenneth D. Merry 	 * checking.
4863e6bd5983SKenneth D. Merry 	 */
4864e6bd5983SKenneth D. Merry 	free(device->supported_vpds, M_CAMXPT);
4865e6bd5983SKenneth D. Merry 	free(device->device_id, M_CAMXPT);
48663bba3152SKenneth D. Merry 	free(device->ext_inq, M_CAMXPT);
4867e6bd5983SKenneth D. Merry 	free(device->physpath, M_CAMXPT);
4868e6bd5983SKenneth D. Merry 	free(device->rcap_buf, M_CAMXPT);
4869e6bd5983SKenneth D. Merry 	free(device->serial_num, M_CAMXPT);
4870f439e3a4SAlexander Motin 	free(device->nvme_data, M_CAMXPT);
4871f439e3a4SAlexander Motin 	free(device->nvme_cdata, M_CAMXPT);
4872227d67aaSAlexander Motin 	taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
48738b8a9b1dSJustin T. Gibbs }
48748b8a9b1dSJustin T. Gibbs 
48757af2f2c8SWarner Losh uint32_t
xpt_dev_ccbq_resize(struct cam_path * path,int newopenings)48768b8a9b1dSJustin T. Gibbs xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
48778b8a9b1dSJustin T. Gibbs {
48788b8a9b1dSJustin T. Gibbs 	int	result;
48798b8a9b1dSJustin T. Gibbs 	struct	cam_ed *dev;
48808b8a9b1dSJustin T. Gibbs 
48818b8a9b1dSJustin T. Gibbs 	dev = path->device;
4882227d67aaSAlexander Motin 	mtx_lock(&dev->sim->devq->send_mtx);
48838b8a9b1dSJustin T. Gibbs 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
4884227d67aaSAlexander Motin 	mtx_unlock(&dev->sim->devq->send_mtx);
4885df8f9080SJustin T. Gibbs 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4886df8f9080SJustin T. Gibbs 	 || (dev->inq_flags & SID_CmdQue) != 0)
4887df8f9080SJustin T. Gibbs 		dev->tag_saved_openings = newopenings;
48888b8a9b1dSJustin T. Gibbs 	return (result);
48898b8a9b1dSJustin T. Gibbs }
48908b8a9b1dSJustin T. Gibbs 
48918b8a9b1dSJustin T. Gibbs static struct cam_eb *
xpt_find_bus(path_id_t path_id)48928b8a9b1dSJustin T. Gibbs xpt_find_bus(path_id_t path_id)
48938b8a9b1dSJustin T. Gibbs {
48948b8a9b1dSJustin T. Gibbs 	struct cam_eb *bus;
48958b8a9b1dSJustin T. Gibbs 
48969a7c2696SAlexander Motin 	xpt_lock_buses();
48972b83592fSScott Long 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
48988b8a9b1dSJustin T. Gibbs 	     bus != NULL;
48998b8a9b1dSJustin T. Gibbs 	     bus = TAILQ_NEXT(bus, links)) {
4900a5479bc5SJustin T. Gibbs 		if (bus->path_id == path_id) {
4901a5479bc5SJustin T. Gibbs 			bus->refcount++;
49028b8a9b1dSJustin T. Gibbs 			break;
49038b8a9b1dSJustin T. Gibbs 		}
4904a5479bc5SJustin T. Gibbs 	}
49059a7c2696SAlexander Motin 	xpt_unlock_buses();
49068b8a9b1dSJustin T. Gibbs 	return (bus);
49078b8a9b1dSJustin T. Gibbs }
49088b8a9b1dSJustin T. Gibbs 
49098b8a9b1dSJustin T. Gibbs static struct cam_et *
xpt_find_target(struct cam_eb * bus,target_id_t target_id)49108b8a9b1dSJustin T. Gibbs xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
49118b8a9b1dSJustin T. Gibbs {
49128b8a9b1dSJustin T. Gibbs 	struct cam_et *target;
49138b8a9b1dSJustin T. Gibbs 
4914227d67aaSAlexander Motin 	mtx_assert(&bus->eb_mtx, MA_OWNED);
49158b8a9b1dSJustin T. Gibbs 	for (target = TAILQ_FIRST(&bus->et_entries);
49168b8a9b1dSJustin T. Gibbs 	     target != NULL;
49178b8a9b1dSJustin T. Gibbs 	     target = TAILQ_NEXT(target, links)) {
49188b8a9b1dSJustin T. Gibbs 		if (target->target_id == target_id) {
49198b8a9b1dSJustin T. Gibbs 			target->refcount++;
49208b8a9b1dSJustin T. Gibbs 			break;
49218b8a9b1dSJustin T. Gibbs 		}
49228b8a9b1dSJustin T. Gibbs 	}
49238b8a9b1dSJustin T. Gibbs 	return (target);
49248b8a9b1dSJustin T. Gibbs }
49258b8a9b1dSJustin T. Gibbs 
49268b8a9b1dSJustin T. Gibbs static struct cam_ed *
xpt_find_device(struct cam_et * target,lun_id_t lun_id)49278b8a9b1dSJustin T. Gibbs xpt_find_device(struct cam_et *target, lun_id_t lun_id)
49288b8a9b1dSJustin T. Gibbs {
49298b8a9b1dSJustin T. Gibbs 	struct cam_ed *device;
49308b8a9b1dSJustin T. Gibbs 
4931227d67aaSAlexander Motin 	mtx_assert(&target->bus->eb_mtx, MA_OWNED);
49328b8a9b1dSJustin T. Gibbs 	for (device = TAILQ_FIRST(&target->ed_entries);
49338b8a9b1dSJustin T. Gibbs 	     device != NULL;
49348b8a9b1dSJustin T. Gibbs 	     device = TAILQ_NEXT(device, links)) {
49358b8a9b1dSJustin T. Gibbs 		if (device->lun_id == lun_id) {
49368b8a9b1dSJustin T. Gibbs 			device->refcount++;
49378b8a9b1dSJustin T. Gibbs 			break;
49388b8a9b1dSJustin T. Gibbs 		}
49398b8a9b1dSJustin T. Gibbs 	}
49408b8a9b1dSJustin T. Gibbs 	return (device);
49418b8a9b1dSJustin T. Gibbs }
49428b8a9b1dSJustin T. Gibbs 
494330a4094fSAlexander Motin void
xpt_start_tags(struct cam_path * path)4944f0adc790SJustin T. Gibbs xpt_start_tags(struct cam_path *path)
4945f0adc790SJustin T. Gibbs {
4946fd21cc5eSJustin T. Gibbs 	struct ccb_relsim crs;
4947fd21cc5eSJustin T. Gibbs 	struct cam_ed *device;
4948fd21cc5eSJustin T. Gibbs 	struct cam_sim *sim;
4949fd21cc5eSJustin T. Gibbs 	int    newopenings;
4950fd21cc5eSJustin T. Gibbs 
4951fd21cc5eSJustin T. Gibbs 	device = path->device;
4952fd21cc5eSJustin T. Gibbs 	sim = path->bus->sim;
4953fd21cc5eSJustin T. Gibbs 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4954fd21cc5eSJustin T. Gibbs 	xpt_freeze_devq(path, /*count*/1);
4955fd21cc5eSJustin T. Gibbs 	device->inq_flags |= SID_CmdQue;
4956df8f9080SJustin T. Gibbs 	if (device->tag_saved_openings != 0)
4957df8f9080SJustin T. Gibbs 		newopenings = device->tag_saved_openings;
4958df8f9080SJustin T. Gibbs 	else
495952c9ce25SScott Long 		newopenings = min(device->maxtags,
4960df8f9080SJustin T. Gibbs 				  sim->max_tagged_dev_openings);
4961f0adc790SJustin T. Gibbs 	xpt_dev_ccbq_resize(path, newopenings);
4962581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4963076686feSEdward Tomasz Napierala 	memset(&crs, 0, sizeof(crs));
4964bbfa4aa1SAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4965fd21cc5eSJustin T. Gibbs 	crs.ccb_h.func_code = XPT_REL_SIMQ;
4966fd21cc5eSJustin T. Gibbs 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4967fd21cc5eSJustin T. Gibbs 	crs.openings
4968fd21cc5eSJustin T. Gibbs 	    = crs.release_timeout
4969fd21cc5eSJustin T. Gibbs 	    = crs.qfrozen_cnt
4970fd21cc5eSJustin T. Gibbs 	    = 0;
4971fd21cc5eSJustin T. Gibbs 	xpt_action((union ccb *)&crs);
4972fd21cc5eSJustin T. Gibbs }
4973fd21cc5eSJustin T. Gibbs 
497430a4094fSAlexander Motin void
xpt_stop_tags(struct cam_path * path)497530a4094fSAlexander Motin xpt_stop_tags(struct cam_path *path)
497630a4094fSAlexander Motin {
497730a4094fSAlexander Motin 	struct ccb_relsim crs;
497830a4094fSAlexander Motin 	struct cam_ed *device;
497930a4094fSAlexander Motin 	struct cam_sim *sim;
498030a4094fSAlexander Motin 
498130a4094fSAlexander Motin 	device = path->device;
498230a4094fSAlexander Motin 	sim = path->bus->sim;
498330a4094fSAlexander Motin 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
498430a4094fSAlexander Motin 	device->tag_delay_count = 0;
498530a4094fSAlexander Motin 	xpt_freeze_devq(path, /*count*/1);
498630a4094fSAlexander Motin 	device->inq_flags &= ~SID_CmdQue;
498730a4094fSAlexander Motin 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4988581b2e78SAlexander Motin 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
4989076686feSEdward Tomasz Napierala 	memset(&crs, 0, sizeof(crs));
499030a4094fSAlexander Motin 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
499130a4094fSAlexander Motin 	crs.ccb_h.func_code = XPT_REL_SIMQ;
499230a4094fSAlexander Motin 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
499330a4094fSAlexander Motin 	crs.openings
499430a4094fSAlexander Motin 	    = crs.release_timeout
499530a4094fSAlexander Motin 	    = crs.qfrozen_cnt
499630a4094fSAlexander Motin 	    = 0;
499730a4094fSAlexander Motin 	xpt_action((union ccb *)&crs);
499830a4094fSAlexander Motin }
499930a4094fSAlexander Motin 
5000a4876fbfSAlexander Motin /*
5001a4876fbfSAlexander Motin  * Assume all possible buses are detected by this time, so allow boot
5002a4876fbfSAlexander Motin  * as soon as they all are scanned.
5003a4876fbfSAlexander Motin  */
500483c5d981SAlexander Motin static void
xpt_boot_delay(void * arg)500583c5d981SAlexander Motin xpt_boot_delay(void *arg)
50068b8a9b1dSJustin T. Gibbs {
50072b83592fSScott Long 
500883c5d981SAlexander Motin 	xpt_release_boot();
50098b8a9b1dSJustin T. Gibbs }
50108b8a9b1dSJustin T. Gibbs 
5011a4876fbfSAlexander Motin /*
5012a4876fbfSAlexander Motin  * Now that all config hooks have completed, start boot_delay timer,
5013a4876fbfSAlexander Motin  * waiting for possibly still undetected buses (USB) to appear.
5014a4876fbfSAlexander Motin  */
50158b8a9b1dSJustin T. Gibbs static void
xpt_ch_done(void * arg)5016a4876fbfSAlexander Motin xpt_ch_done(void *arg)
50178b8a9b1dSJustin T. Gibbs {
5018a4876fbfSAlexander Motin 
5019a4876fbfSAlexander Motin 	callout_init(&xsoftc.boot_callout, 1);
50200e5c50bfSAlexander Motin 	callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay,
50210e5c50bfSAlexander Motin 	    SBT_1MS, xpt_boot_delay, NULL, 0);
5022a4876fbfSAlexander Motin }
5023a4876fbfSAlexander Motin SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5024a4876fbfSAlexander Motin 
50253393f8daSKenneth D. Merry /*
50263393f8daSKenneth D. Merry  * Now that interrupts are enabled, go find our devices
50273393f8daSKenneth D. Merry  */
5028a4876fbfSAlexander Motin static void
xpt_config(void * arg)5029a4876fbfSAlexander Motin xpt_config(void *arg)
5030a4876fbfSAlexander Motin {
5031227d67aaSAlexander Motin 	if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5032227d67aaSAlexander Motin 		printf("xpt_config: failed to create taskqueue thread.\n");
50338b8a9b1dSJustin T. Gibbs 
5034f0f25b9cSAlexander Motin 	/* Setup debugging path */
50358b8a9b1dSJustin T. Gibbs 	if (cam_dflags != CAM_DEBUG_NONE) {
5036227d67aaSAlexander Motin 		if (xpt_create_path(&cam_dpath, NULL,
50378b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
50388b8a9b1dSJustin T. Gibbs 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
50398b8a9b1dSJustin T. Gibbs 			printf("xpt_config: xpt_create_path() failed for debug"
50408b8a9b1dSJustin T. Gibbs 			       " target %d:%d:%d, debugging disabled\n",
50418b8a9b1dSJustin T. Gibbs 			       CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
50428b8a9b1dSJustin T. Gibbs 			cam_dflags = CAM_DEBUG_NONE;
50438b8a9b1dSJustin T. Gibbs 		}
50448b8a9b1dSJustin T. Gibbs 	} else
50458b8a9b1dSJustin T. Gibbs 		cam_dpath = NULL;
50468b8a9b1dSJustin T. Gibbs 
504783c5d981SAlexander Motin 	periphdriver_init(1);
504883c5d981SAlexander Motin 	xpt_hold_boot();
5049a4876fbfSAlexander Motin 
505083c5d981SAlexander Motin 	/* Fire up rescan thread. */
5051227d67aaSAlexander Motin 	if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5052227d67aaSAlexander Motin 	    "cam", "scanner")) {
50536f15a274SAlexander Motin 		printf("xpt_config: failed to create rescan thread.\n");
50541e637ba6SAlexander Motin 	}
505583c5d981SAlexander Motin }
50568b8a9b1dSJustin T. Gibbs 
505783c5d981SAlexander Motin void
xpt_hold_boot_locked(void)5058a4876fbfSAlexander Motin xpt_hold_boot_locked(void)
5059a4876fbfSAlexander Motin {
5060a4876fbfSAlexander Motin 
5061a4876fbfSAlexander Motin 	if (xsoftc.buses_to_config++ == 0)
5062a4876fbfSAlexander Motin 		root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5063a4876fbfSAlexander Motin }
5064a4876fbfSAlexander Motin 
5065a4876fbfSAlexander Motin void
xpt_hold_boot(void)506683c5d981SAlexander Motin xpt_hold_boot(void)
506783c5d981SAlexander Motin {
5068a4876fbfSAlexander Motin 
506983c5d981SAlexander Motin 	xpt_lock_buses();
5070a4876fbfSAlexander Motin 	xpt_hold_boot_locked();
507183c5d981SAlexander Motin 	xpt_unlock_buses();
507283c5d981SAlexander Motin }
507383c5d981SAlexander Motin 
507483c5d981SAlexander Motin void
xpt_release_boot(void)507583c5d981SAlexander Motin xpt_release_boot(void)
507683c5d981SAlexander Motin {
507783c5d981SAlexander Motin 
5078a4876fbfSAlexander Motin 	xpt_lock_buses();
5079a4876fbfSAlexander Motin 	if (--xsoftc.buses_to_config == 0) {
5080a4876fbfSAlexander Motin 		if (xsoftc.buses_config_done == 0) {
508183c5d981SAlexander Motin 			xsoftc.buses_config_done = 1;
5082a4876fbfSAlexander Motin 			xsoftc.buses_to_config++;
5083a4876fbfSAlexander Motin 			TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5084a4876fbfSAlexander Motin 			    NULL);
5085a4876fbfSAlexander Motin 			taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
508683c5d981SAlexander Motin 		} else
5087a4876fbfSAlexander Motin 			root_mount_rel(&xsoftc.xpt_rootmount);
5088a4876fbfSAlexander Motin 	}
508983c5d981SAlexander Motin 	xpt_unlock_buses();
50902863f7b1SJustin T. Gibbs }
50918b8a9b1dSJustin T. Gibbs 
50928b8a9b1dSJustin T. Gibbs /*
50938b8a9b1dSJustin T. Gibbs  * If the given device only has one peripheral attached to it, and if that
50948b8a9b1dSJustin T. Gibbs  * peripheral is the passthrough driver, announce it.  This insures that the
50958b8a9b1dSJustin T. Gibbs  * user sees some sort of announcement for every peripheral in their system.
50968b8a9b1dSJustin T. Gibbs  */
50978b8a9b1dSJustin T. Gibbs static int
xptpassannouncefunc(struct cam_ed * device,void * arg)50988b8a9b1dSJustin T. Gibbs xptpassannouncefunc(struct cam_ed *device, void *arg)
50998b8a9b1dSJustin T. Gibbs {
51008b8a9b1dSJustin T. Gibbs 	struct cam_periph *periph;
51018b8a9b1dSJustin T. Gibbs 	int i;
51028b8a9b1dSJustin T. Gibbs 
51038b8a9b1dSJustin T. Gibbs 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
51048b8a9b1dSJustin T. Gibbs 	     periph = SLIST_NEXT(periph, periph_links), i++);
51058b8a9b1dSJustin T. Gibbs 
51068b8a9b1dSJustin T. Gibbs 	periph = SLIST_FIRST(&device->periphs);
51078b8a9b1dSJustin T. Gibbs 	if ((i == 1)
51088b8a9b1dSJustin T. Gibbs 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
51098b8a9b1dSJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
51108b8a9b1dSJustin T. Gibbs 
51118b8a9b1dSJustin T. Gibbs 	return(1);
51128b8a9b1dSJustin T. Gibbs }
51138b8a9b1dSJustin T. Gibbs 
51148b8a9b1dSJustin T. Gibbs static void
xpt_finishconfig_task(void * context,int pending)51152b83592fSScott Long xpt_finishconfig_task(void *context, int pending)
51168b8a9b1dSJustin T. Gibbs {
51178b8a9b1dSJustin T. Gibbs 
511883c5d981SAlexander Motin 	periphdriver_init(2);
51192b83592fSScott Long 	/*
51202b83592fSScott Long 	 * Check for devices with no "standard" peripheral driver
51212b83592fSScott Long 	 * attached.  For any devices like that, announce the
51222b83592fSScott Long 	 * passthrough driver so the user will see something.
51232b83592fSScott Long 	 */
51243089bb2eSAlexander Motin 	if (!bootverbose)
51252b83592fSScott Long 		xpt_for_all_devices(xptpassannouncefunc, NULL);
51262b83592fSScott Long 
5127a4876fbfSAlexander Motin 	xpt_release_boot();
51282b83592fSScott Long }
51292b83592fSScott Long 
513085d92640SScott Long cam_status
xpt_register_async(int event,ac_callback_t * cbfunc,void * cbarg,struct cam_path * path)513185d92640SScott Long xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
513285d92640SScott Long 		   struct cam_path *path)
513385d92640SScott Long {
513485d92640SScott Long 	struct ccb_setasync csa;
513585d92640SScott Long 	cam_status status;
5136d095d6a3SWarner Losh 	bool xptpath = false;
513785d92640SScott Long 
513885d92640SScott Long 	if (path == NULL) {
513985d92640SScott Long 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
514085d92640SScott Long 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5141227d67aaSAlexander Motin 		if (status != CAM_REQ_CMP)
514285d92640SScott Long 			return (status);
5143227d67aaSAlexander Motin 		xpt_path_lock(path);
5144d095d6a3SWarner Losh 		xptpath = true;
514585d92640SScott Long 	}
514685d92640SScott Long 
5147076686feSEdward Tomasz Napierala 	memset(&csa, 0, sizeof(csa));
514883c5d981SAlexander Motin 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
514985d92640SScott Long 	csa.ccb_h.func_code = XPT_SASYNC_CB;
515085d92640SScott Long 	csa.event_enable = event;
515185d92640SScott Long 	csa.callback = cbfunc;
515285d92640SScott Long 	csa.callback_arg = cbarg;
515385d92640SScott Long 	xpt_action((union ccb *)&csa);
515485d92640SScott Long 	status = csa.ccb_h.status;
51553501942bSJustin T. Gibbs 
515669be012fSWarner Losh 	CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
515769be012fSWarner Losh 	    ("xpt_register_async: func %p\n", cbfunc));
515869be012fSWarner Losh 
515985d92640SScott Long 	if (xptpath) {
5160227d67aaSAlexander Motin 		xpt_path_unlock(path);
516185d92640SScott Long 		xpt_free_path(path);
51623501942bSJustin T. Gibbs 	}
51637685edecSAlexander Motin 
51647685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
51657685edecSAlexander Motin 	    (csa.event_enable & AC_FOUND_DEVICE)) {
51667685edecSAlexander Motin 		/*
51677685edecSAlexander Motin 		 * Get this peripheral up to date with all
51687685edecSAlexander Motin 		 * the currently existing devices.
51697685edecSAlexander Motin 		 */
51707685edecSAlexander Motin 		xpt_for_all_devices(xptsetasyncfunc, &csa);
51717685edecSAlexander Motin 	}
51727685edecSAlexander Motin 	if ((status == CAM_REQ_CMP) &&
51737685edecSAlexander Motin 	    (csa.event_enable & AC_PATH_REGISTERED)) {
51747685edecSAlexander Motin 		/*
51757685edecSAlexander Motin 		 * Get this peripheral up to date with all
5176db4fcadfSConrad Meyer 		 * the currently existing buses.
51777685edecSAlexander Motin 		 */
51787685edecSAlexander Motin 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
51797685edecSAlexander Motin 	}
51803501942bSJustin T. Gibbs 
518185d92640SScott Long 	return (status);
518285d92640SScott Long }
518385d92640SScott Long 
51848b8a9b1dSJustin T. Gibbs static void
xptaction(struct cam_sim * sim,union ccb * work_ccb)51858b8a9b1dSJustin T. Gibbs xptaction(struct cam_sim *sim, union ccb *work_ccb)
51868b8a9b1dSJustin T. Gibbs {
51878b8a9b1dSJustin T. Gibbs 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
51888b8a9b1dSJustin T. Gibbs 
51898b8a9b1dSJustin T. Gibbs 	switch (work_ccb->ccb_h.func_code) {
51908b8a9b1dSJustin T. Gibbs 	/* Common cases first */
51918b8a9b1dSJustin T. Gibbs 	case XPT_PATH_INQ:		/* Path routing inquiry */
51928b8a9b1dSJustin T. Gibbs 	{
51938b8a9b1dSJustin T. Gibbs 		struct ccb_pathinq *cpi;
51948b8a9b1dSJustin T. Gibbs 
51958b8a9b1dSJustin T. Gibbs 		cpi = &work_ccb->cpi;
51968b8a9b1dSJustin T. Gibbs 		cpi->version_num = 1; /* XXX??? */
51978b8a9b1dSJustin T. Gibbs 		cpi->hba_inquiry = 0;
51988b8a9b1dSJustin T. Gibbs 		cpi->target_sprt = 0;
51998b8a9b1dSJustin T. Gibbs 		cpi->hba_misc = 0;
52008b8a9b1dSJustin T. Gibbs 		cpi->hba_eng_cnt = 0;
52018b8a9b1dSJustin T. Gibbs 		cpi->max_target = 0;
52028b8a9b1dSJustin T. Gibbs 		cpi->max_lun = 0;
52038b8a9b1dSJustin T. Gibbs 		cpi->initiator_id = 0;
52044195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
52054195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "", HBA_IDLEN);
52064195c7deSAlan Somers 		strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
52078b8a9b1dSJustin T. Gibbs 		cpi->unit_number = sim->unit_number;
52088b8a9b1dSJustin T. Gibbs 		cpi->bus_id = sim->bus_id;
52099deea857SKenneth D. Merry 		cpi->base_transfer_speed = 0;
52103393f8daSKenneth D. Merry 		cpi->protocol = PROTO_UNSPECIFIED;
52113393f8daSKenneth D. Merry 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
52123393f8daSKenneth D. Merry 		cpi->transport = XPORT_UNSPECIFIED;
52133393f8daSKenneth D. Merry 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
52148b8a9b1dSJustin T. Gibbs 		cpi->ccb_h.status = CAM_REQ_CMP;
52158b8a9b1dSJustin T. Gibbs 		break;
52168b8a9b1dSJustin T. Gibbs 	}
52178b8a9b1dSJustin T. Gibbs 	default:
52188b8a9b1dSJustin T. Gibbs 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
52198b8a9b1dSJustin T. Gibbs 		break;
52208b8a9b1dSJustin T. Gibbs 	}
52211247272eSWarner Losh 	xpt_done(work_ccb);
52228b8a9b1dSJustin T. Gibbs }
52238b8a9b1dSJustin T. Gibbs 
52248b8a9b1dSJustin T. Gibbs /*
5225434bbf6eSJustin T. Gibbs  * The xpt as a "controller" has no interrupt sources, so polling
5226434bbf6eSJustin T. Gibbs  * is a no-op.
5227434bbf6eSJustin T. Gibbs  */
5228434bbf6eSJustin T. Gibbs static void
xptpoll(struct cam_sim * sim)5229434bbf6eSJustin T. Gibbs xptpoll(struct cam_sim *sim)
5230434bbf6eSJustin T. Gibbs {
5231434bbf6eSJustin T. Gibbs }
5232434bbf6eSJustin T. Gibbs 
52332b83592fSScott Long void
xpt_lock_buses(void)52342b83592fSScott Long xpt_lock_buses(void)
52352b83592fSScott Long {
52362b83592fSScott Long 	mtx_lock(&xsoftc.xpt_topo_lock);
52372b83592fSScott Long }
52382b83592fSScott Long 
52392b83592fSScott Long void
xpt_unlock_buses(void)52402b83592fSScott Long xpt_unlock_buses(void)
52412b83592fSScott Long {
52422b83592fSScott Long 	mtx_unlock(&xsoftc.xpt_topo_lock);
52432b83592fSScott Long }
52442b83592fSScott Long 
5245227d67aaSAlexander Motin struct mtx *
xpt_path_mtx(struct cam_path * path)5246227d67aaSAlexander Motin xpt_path_mtx(struct cam_path *path)
52478b8a9b1dSJustin T. Gibbs {
5248227d67aaSAlexander Motin 
5249227d67aaSAlexander Motin 	return (&path->device->device_mtx);
5250227d67aaSAlexander Motin }
5251227d67aaSAlexander Motin 
5252227d67aaSAlexander Motin static void
xpt_done_process(struct ccb_hdr * ccb_h)5253227d67aaSAlexander Motin xpt_done_process(struct ccb_hdr *ccb_h)
5254227d67aaSAlexander Motin {
5255de64cba2SWarner Losh 	struct cam_sim *sim = NULL;
5256de64cba2SWarner Losh 	struct cam_devq *devq = NULL;
5257227d67aaSAlexander Motin 	struct mtx *mtx = NULL;
52588b8a9b1dSJustin T. Gibbs 
52598532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
52608532d381SConrad Meyer 	struct ccb_scsiio *csio;
52618532d381SConrad Meyer 
52628532d381SConrad Meyer 	if (ccb_h->func_code == XPT_SCSI_IO) {
52638532d381SConrad Meyer 		csio = &((union ccb *)ccb_h)->csio;
52648532d381SConrad Meyer 		if (csio->bio != NULL)
52658532d381SConrad Meyer 			biotrack(csio->bio, __func__);
52668532d381SConrad Meyer 	}
52678532d381SConrad Meyer #endif
52688532d381SConrad Meyer 
52698b8a9b1dSJustin T. Gibbs 	if (ccb_h->flags & CAM_HIGH_POWER) {
52708b8a9b1dSJustin T. Gibbs 		struct highpowerlist	*hphead;
5271ea541bfdSAlexander Motin 		struct cam_ed		*device;
52728b8a9b1dSJustin T. Gibbs 
5273daa5487fSAlexander Motin 		mtx_lock(&xsoftc.xpt_highpower_lock);
52742b83592fSScott Long 		hphead = &xsoftc.highpowerq;
52758b8a9b1dSJustin T. Gibbs 
5276ea541bfdSAlexander Motin 		device = STAILQ_FIRST(hphead);
52778b8a9b1dSJustin T. Gibbs 
52788b8a9b1dSJustin T. Gibbs 		/*
52798b8a9b1dSJustin T. Gibbs 		 * Increment the count since this command is done.
52808b8a9b1dSJustin T. Gibbs 		 */
52812b83592fSScott Long 		xsoftc.num_highpower++;
52828b8a9b1dSJustin T. Gibbs 
52838b8a9b1dSJustin T. Gibbs 		/*
52848b8a9b1dSJustin T. Gibbs 		 * Any high powered commands queued up?
52858b8a9b1dSJustin T. Gibbs 		 */
5286ea541bfdSAlexander Motin 		if (device != NULL) {
5287ea541bfdSAlexander Motin 			STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5288daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
52898b8a9b1dSJustin T. Gibbs 
5290227d67aaSAlexander Motin 			mtx_lock(&device->sim->devq->send_mtx);
5291ea541bfdSAlexander Motin 			xpt_release_devq_device(device,
52922cefde5fSJustin T. Gibbs 					 /*count*/1, /*runqueue*/TRUE);
5293227d67aaSAlexander Motin 			mtx_unlock(&device->sim->devq->send_mtx);
52942b83592fSScott Long 		} else
5295daa5487fSAlexander Motin 			mtx_unlock(&xsoftc.xpt_highpower_lock);
52968b8a9b1dSJustin T. Gibbs 	}
52972b83592fSScott Long 
5298de64cba2SWarner Losh 	/*
5299a73b2e25SWarner Losh 	 * Insulate against a race where the periph is destroyed but CCBs are
5300a73b2e25SWarner Losh 	 * still not all processed. This shouldn't happen, but allows us better
5301a73b2e25SWarner Losh 	 * bug diagnostic when it does.
5302de64cba2SWarner Losh 	 */
5303de64cba2SWarner Losh 	if (ccb_h->path->bus)
5304227d67aaSAlexander Motin 		sim = ccb_h->path->bus->sim;
5305227d67aaSAlexander Motin 
5306227d67aaSAlexander Motin 	if (ccb_h->status & CAM_RELEASE_SIMQ) {
5307de64cba2SWarner Losh 		KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5308227d67aaSAlexander Motin 		xpt_release_simq(sim, /*run_queue*/FALSE);
5309227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_RELEASE_SIMQ;
5310227d67aaSAlexander Motin 	}
5311227d67aaSAlexander Motin 
5312227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5313227d67aaSAlexander Motin 	 && (ccb_h->status & CAM_DEV_QFRZN)) {
5314d718926bSAlexander Motin 		xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5315227d67aaSAlexander Motin 		ccb_h->status &= ~CAM_DEV_QFRZN;
5316227d67aaSAlexander Motin 	}
5317227d67aaSAlexander Motin 
53189deea857SKenneth D. Merry 	if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5319227d67aaSAlexander Motin 		struct cam_ed *dev = ccb_h->path->device;
53208b8a9b1dSJustin T. Gibbs 
5321de64cba2SWarner Losh 		if (sim)
5322de64cba2SWarner Losh 			devq = sim->devq;
53237defd758SWarner Losh 		KASSERT(devq, ("Periph disappeared with CCB %p %s request pending.",
53247defd758SWarner Losh 			ccb_h, xpt_action_name(ccb_h->func_code)));
5325de64cba2SWarner Losh 
5326227d67aaSAlexander Motin 		mtx_lock(&devq->send_mtx);
5327227d67aaSAlexander Motin 		devq->send_active--;
5328227d67aaSAlexander Motin 		devq->send_openings++;
53298b8a9b1dSJustin T. Gibbs 		cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
53308b8a9b1dSJustin T. Gibbs 
5331b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
53328b8a9b1dSJustin T. Gibbs 		  && (dev->ccbq.dev_active == 0))) {
5333b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5334227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
5335b9c473b2SAlexander Motin 					 /*run_queue*/FALSE);
5336b9c473b2SAlexander Motin 		}
5337b9c473b2SAlexander Motin 
5338b9c473b2SAlexander Motin 		if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5339b9c473b2SAlexander Motin 		  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5340b9c473b2SAlexander Motin 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5341227d67aaSAlexander Motin 			xpt_release_devq_device(dev, /*count*/1,
534283c5d981SAlexander Motin 					 /*run_queue*/FALSE);
53438b8a9b1dSJustin T. Gibbs 		}
53448b8a9b1dSJustin T. Gibbs 
5345227d67aaSAlexander Motin 		if (!device_is_queued(dev))
5346227d67aaSAlexander Motin 			(void)xpt_schedule_devq(devq, dev);
5347d718926bSAlexander Motin 		xpt_run_devq(devq);
5348227d67aaSAlexander Motin 		mtx_unlock(&devq->send_mtx);
5349227d67aaSAlexander Motin 
5350227d67aaSAlexander Motin 		if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5351227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5352227d67aaSAlexander Motin 			mtx_lock(mtx);
5353227d67aaSAlexander Motin 
5354fd21cc5eSJustin T. Gibbs 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5355fd21cc5eSJustin T. Gibbs 			 && (--dev->tag_delay_count == 0))
5356fd21cc5eSJustin T. Gibbs 				xpt_start_tags(ccb_h->path);
53573501942bSJustin T. Gibbs 		}
53588b8a9b1dSJustin T. Gibbs 	}
53598b8a9b1dSJustin T. Gibbs 
5360227d67aaSAlexander Motin 	if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5361227d67aaSAlexander Motin 		if (mtx == NULL) {
5362227d67aaSAlexander Motin 			mtx = xpt_path_mtx(ccb_h->path);
5363227d67aaSAlexander Motin 			mtx_lock(mtx);
5364434bbf6eSJustin T. Gibbs 		}
5365227d67aaSAlexander Motin 	} else {
5366227d67aaSAlexander Motin 		if (mtx != NULL) {
5367227d67aaSAlexander Motin 			mtx_unlock(mtx);
5368227d67aaSAlexander Motin 			mtx = NULL;
5369227d67aaSAlexander Motin 		}
53708b8a9b1dSJustin T. Gibbs 	}
53718b8a9b1dSJustin T. Gibbs 
53728b8a9b1dSJustin T. Gibbs 	/* Call the peripheral driver's callback */
5373f1486b51SAlexander Motin 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5374434bbf6eSJustin T. Gibbs 	(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5375227d67aaSAlexander Motin 	if (mtx != NULL)
5376227d67aaSAlexander Motin 		mtx_unlock(mtx);
5377227d67aaSAlexander Motin }
5378227d67aaSAlexander Motin 
53797381bbeeSWarner Losh /*
53807381bbeeSWarner Losh  * Parameterize instead and use xpt_done_td?
53817381bbeeSWarner Losh  */
53827381bbeeSWarner Losh static void
xpt_async_td(void * arg)53837381bbeeSWarner Losh xpt_async_td(void *arg)
53847381bbeeSWarner Losh {
53857381bbeeSWarner Losh 	struct cam_doneq *queue = arg;
53867381bbeeSWarner Losh 	struct ccb_hdr *ccb_h;
53877381bbeeSWarner Losh 	STAILQ_HEAD(, ccb_hdr)	doneq;
53887381bbeeSWarner Losh 
53897381bbeeSWarner Losh 	STAILQ_INIT(&doneq);
53907381bbeeSWarner Losh 	mtx_lock(&queue->cam_doneq_mtx);
53917381bbeeSWarner Losh 	while (1) {
53927381bbeeSWarner Losh 		while (STAILQ_EMPTY(&queue->cam_doneq))
53937381bbeeSWarner Losh 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
53947381bbeeSWarner Losh 			    PRIBIO, "-", 0);
53957381bbeeSWarner Losh 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
53967381bbeeSWarner Losh 		mtx_unlock(&queue->cam_doneq_mtx);
53977381bbeeSWarner Losh 
53987381bbeeSWarner Losh 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
53997381bbeeSWarner Losh 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
54007381bbeeSWarner Losh 			xpt_done_process(ccb_h);
54017381bbeeSWarner Losh 		}
54027381bbeeSWarner Losh 
54037381bbeeSWarner Losh 		mtx_lock(&queue->cam_doneq_mtx);
54047381bbeeSWarner Losh 	}
54057381bbeeSWarner Losh }
54067381bbeeSWarner Losh 
5407227d67aaSAlexander Motin void
xpt_done_td(void * arg)5408227d67aaSAlexander Motin xpt_done_td(void *arg)
5409227d67aaSAlexander Motin {
5410227d67aaSAlexander Motin 	struct cam_doneq *queue = arg;
5411227d67aaSAlexander Motin 	struct ccb_hdr *ccb_h;
5412227d67aaSAlexander Motin 	STAILQ_HEAD(, ccb_hdr)	doneq;
5413227d67aaSAlexander Motin 
5414227d67aaSAlexander Motin 	STAILQ_INIT(&doneq);
5415227d67aaSAlexander Motin 	mtx_lock(&queue->cam_doneq_mtx);
5416227d67aaSAlexander Motin 	while (1) {
5417227d67aaSAlexander Motin 		while (STAILQ_EMPTY(&queue->cam_doneq)) {
5418227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 1;
5419227d67aaSAlexander Motin 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5420227d67aaSAlexander Motin 			    PRIBIO, "-", 0);
5421227d67aaSAlexander Motin 			queue->cam_doneq_sleep = 0;
5422227d67aaSAlexander Motin 		}
5423227d67aaSAlexander Motin 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5424227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
5425227d67aaSAlexander Motin 
5426227d67aaSAlexander Motin 		THREAD_NO_SLEEPING();
5427227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5428227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5429227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5430227d67aaSAlexander Motin 		}
5431227d67aaSAlexander Motin 		THREAD_SLEEPING_OK();
5432227d67aaSAlexander Motin 
5433227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5434227d67aaSAlexander Motin 	}
5435227d67aaSAlexander Motin }
5436227d67aaSAlexander Motin 
5437227d67aaSAlexander Motin static void
camisr_runqueue(void)5438227d67aaSAlexander Motin camisr_runqueue(void)
5439227d67aaSAlexander Motin {
5440227d67aaSAlexander Motin 	struct	ccb_hdr *ccb_h;
5441227d67aaSAlexander Motin 	struct cam_doneq *queue;
5442227d67aaSAlexander Motin 	int i;
5443227d67aaSAlexander Motin 
5444227d67aaSAlexander Motin 	/* Process global queues. */
5445227d67aaSAlexander Motin 	for (i = 0; i < cam_num_doneqs; i++) {
5446227d67aaSAlexander Motin 		queue = &cam_doneqs[i];
5447227d67aaSAlexander Motin 		mtx_lock(&queue->cam_doneq_mtx);
5448227d67aaSAlexander Motin 		while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5449227d67aaSAlexander Motin 			STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5450227d67aaSAlexander Motin 			mtx_unlock(&queue->cam_doneq_mtx);
5451227d67aaSAlexander Motin 			xpt_done_process(ccb_h);
5452227d67aaSAlexander Motin 			mtx_lock(&queue->cam_doneq_mtx);
5453227d67aaSAlexander Motin 		}
5454227d67aaSAlexander Motin 		mtx_unlock(&queue->cam_doneq_mtx);
54558b8a9b1dSJustin T. Gibbs 	}
54568b8a9b1dSJustin T. Gibbs }
545769be012fSWarner Losh 
54581ed40162SWarner Losh /**
54591ed40162SWarner Losh  * @brief Return the device_t associated with the path
54601ed40162SWarner Losh  *
54611ed40162SWarner Losh  * When a SIM is created, it registers a bus with a NEWBUS device_t. This is
54621ed40162SWarner Losh  * stored in the internal cam_eb bus structure. There is no guarnatee any given
54631ed40162SWarner Losh  * path will have a @c device_t associated with it (it's legal to call @c
54641ed40162SWarner Losh  * xpt_bus_register with a @c NULL @c device_t.
54651ed40162SWarner Losh  *
54661ed40162SWarner Losh  * @param path		Path to return the device_t for.
54671ed40162SWarner Losh  */
54681ed40162SWarner Losh device_t
xpt_path_sim_device(const struct cam_path * path)54691ed40162SWarner Losh xpt_path_sim_device(const struct cam_path *path)
54701ed40162SWarner Losh {
54711ed40162SWarner Losh 	return (path->bus->parent_dev);
54721ed40162SWarner Losh }
54731ed40162SWarner Losh 
547469be012fSWarner Losh struct kv
547569be012fSWarner Losh {
547669be012fSWarner Losh 	uint32_t v;
547769be012fSWarner Losh 	const char *name;
547869be012fSWarner Losh };
547969be012fSWarner Losh 
548069be012fSWarner Losh static struct kv map[] = {
548169be012fSWarner Losh 	{ XPT_NOOP, "XPT_NOOP" },
548269be012fSWarner Losh 	{ XPT_SCSI_IO, "XPT_SCSI_IO" },
548369be012fSWarner Losh 	{ XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
548469be012fSWarner Losh 	{ XPT_GDEVLIST, "XPT_GDEVLIST" },
548569be012fSWarner Losh 	{ XPT_PATH_INQ, "XPT_PATH_INQ" },
548669be012fSWarner Losh 	{ XPT_REL_SIMQ, "XPT_REL_SIMQ" },
548769be012fSWarner Losh 	{ XPT_SASYNC_CB, "XPT_SASYNC_CB" },
548869be012fSWarner Losh 	{ XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
548969be012fSWarner Losh 	{ XPT_SCAN_BUS, "XPT_SCAN_BUS" },
549069be012fSWarner Losh 	{ XPT_DEV_MATCH, "XPT_DEV_MATCH" },
549169be012fSWarner Losh 	{ XPT_DEBUG, "XPT_DEBUG" },
549269be012fSWarner Losh 	{ XPT_PATH_STATS, "XPT_PATH_STATS" },
549369be012fSWarner Losh 	{ XPT_GDEV_STATS, "XPT_GDEV_STATS" },
549469be012fSWarner Losh 	{ XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
549569be012fSWarner Losh 	{ XPT_ASYNC, "XPT_ASYNC" },
549669be012fSWarner Losh 	{ XPT_ABORT, "XPT_ABORT" },
549769be012fSWarner Losh 	{ XPT_RESET_BUS, "XPT_RESET_BUS" },
549869be012fSWarner Losh 	{ XPT_RESET_DEV, "XPT_RESET_DEV" },
549969be012fSWarner Losh 	{ XPT_TERM_IO, "XPT_TERM_IO" },
550069be012fSWarner Losh 	{ XPT_SCAN_LUN, "XPT_SCAN_LUN" },
550169be012fSWarner Losh 	{ XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
550269be012fSWarner Losh 	{ XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
550369be012fSWarner Losh 	{ XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
550469be012fSWarner Losh 	{ XPT_ATA_IO, "XPT_ATA_IO" },
550569be012fSWarner Losh 	{ XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
550669be012fSWarner Losh 	{ XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
55077b05c3e3SWarner Losh 	{ XPT_NVME_IO, "XPT_NVME_IO" },
5508a94a63f0SWarner Losh 	{ XPT_MMC_IO, "XPT_MMC_IO" },
550969be012fSWarner Losh 	{ XPT_SMP_IO, "XPT_SMP_IO" },
551069be012fSWarner Losh 	{ XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5511df424515SWarner Losh 	{ XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
551269be012fSWarner Losh 	{ XPT_ENG_INQ, "XPT_ENG_INQ" },
551369be012fSWarner Losh 	{ XPT_ENG_EXEC, "XPT_ENG_EXEC" },
551469be012fSWarner Losh 	{ XPT_EN_LUN, "XPT_EN_LUN" },
551569be012fSWarner Losh 	{ XPT_TARGET_IO, "XPT_TARGET_IO" },
551669be012fSWarner Losh 	{ XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
551769be012fSWarner Losh 	{ XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
551869be012fSWarner Losh 	{ XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
551969be012fSWarner Losh 	{ XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
552069be012fSWarner Losh 	{ XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
552169be012fSWarner Losh 	{ XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
552269be012fSWarner Losh 	{ 0, 0 }
552369be012fSWarner Losh };
552469be012fSWarner Losh 
5525a94a63f0SWarner Losh const char *
xpt_action_name(uint32_t action)552669be012fSWarner Losh xpt_action_name(uint32_t action)
552769be012fSWarner Losh {
552869be012fSWarner Losh 	static char buffer[32];	/* Only for unknown messages -- racy */
552969be012fSWarner Losh 	struct kv *walker = map;
553069be012fSWarner Losh 
553169be012fSWarner Losh 	while (walker->name != NULL) {
553269be012fSWarner Losh 		if (walker->v == action)
553369be012fSWarner Losh 			return (walker->name);
553469be012fSWarner Losh 		walker++;
553569be012fSWarner Losh 	}
553669be012fSWarner Losh 
553769be012fSWarner Losh 	snprintf(buffer, sizeof(buffer), "%#x", action);
553869be012fSWarner Losh 	return (buffer);
553969be012fSWarner Losh }
554070f2356dSWarner Losh 
554170f2356dSWarner Losh void
xpt_cam_path_debug(struct cam_path * path,const char * fmt,...)554270f2356dSWarner Losh xpt_cam_path_debug(struct cam_path *path, const char *fmt, ...)
554370f2356dSWarner Losh {
554470f2356dSWarner Losh 	struct sbuf sbuf;
554570f2356dSWarner Losh 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
554670f2356dSWarner Losh 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN);
554770f2356dSWarner Losh 	va_list ap;
554870f2356dSWarner Losh 
554970f2356dSWarner Losh 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
555070f2356dSWarner Losh 	xpt_path_sbuf(path, sb);
555170f2356dSWarner Losh 	va_start(ap, fmt);
555270f2356dSWarner Losh 	sbuf_vprintf(sb, fmt, ap);
555370f2356dSWarner Losh 	va_end(ap);
555470f2356dSWarner Losh 	sbuf_finish(sb);
555570f2356dSWarner Losh 	sbuf_delete(sb);
555670f2356dSWarner Losh 	if (cam_debug_delay != 0)
555770f2356dSWarner Losh 		DELAY(cam_debug_delay);
555870f2356dSWarner Losh }
555970f2356dSWarner Losh 
556070f2356dSWarner Losh void
xpt_cam_dev_debug(struct cam_ed * dev,const char * fmt,...)556170f2356dSWarner Losh xpt_cam_dev_debug(struct cam_ed *dev, const char *fmt, ...)
556270f2356dSWarner Losh {
556370f2356dSWarner Losh 	struct sbuf sbuf;
556470f2356dSWarner Losh 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
556570f2356dSWarner Losh 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN);
556670f2356dSWarner Losh 	va_list ap;
556770f2356dSWarner Losh 
556870f2356dSWarner Losh 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
556970f2356dSWarner Losh 	xpt_device_sbuf(dev, sb);
557070f2356dSWarner Losh 	va_start(ap, fmt);
557170f2356dSWarner Losh 	sbuf_vprintf(sb, fmt, ap);
557270f2356dSWarner Losh 	va_end(ap);
557370f2356dSWarner Losh 	sbuf_finish(sb);
557470f2356dSWarner Losh 	sbuf_delete(sb);
557570f2356dSWarner Losh 	if (cam_debug_delay != 0)
557670f2356dSWarner Losh 		DELAY(cam_debug_delay);
557770f2356dSWarner Losh }
557870f2356dSWarner Losh 
557970f2356dSWarner Losh void
xpt_cam_debug(const char * fmt,...)558070f2356dSWarner Losh xpt_cam_debug(const char *fmt, ...)
558170f2356dSWarner Losh {
558270f2356dSWarner Losh 	struct sbuf sbuf;
558370f2356dSWarner Losh 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
558470f2356dSWarner Losh 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN);
558570f2356dSWarner Losh 	va_list ap;
558670f2356dSWarner Losh 
558770f2356dSWarner Losh 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
5588519b24f0SAlexander Motin 	sbuf_cat(sb, "cam_debug: ");
558970f2356dSWarner Losh 	va_start(ap, fmt);
559070f2356dSWarner Losh 	sbuf_vprintf(sb, fmt, ap);
559170f2356dSWarner Losh 	va_end(ap);
559270f2356dSWarner Losh 	sbuf_finish(sb);
559370f2356dSWarner Losh 	sbuf_delete(sb);
559470f2356dSWarner Losh 	if (cam_debug_delay != 0)
559570f2356dSWarner Losh 		DELAY(cam_debug_delay);
559670f2356dSWarner Losh }
5597